SlideShare a Scribd company logo
1 of 61
Play them off, Keyboard
App:
Building a piano in Javascript
sophie.omg.lol
Hi, I’m Sophie!
� Web Lead @ Monzo Bank, UK
� Hobbyist musician & choir
conductor
� localghost.dev
� @sophie@social.lol
sophie.omg.lol
sophie.omg.lol
sophie.omg.lol
First: some theory
sophie.omg.lol
sophie.omg.lol
A B C D E F G
C♯
D♭
D♯
E♭
F♯
G♭
G♯
A♭
A♯
B♭
sophie.omg.lol
C D E F G A B C D E F G A B C D E F G A B
C♯
D♭
D♯
E♭
F♯
G♭
G♯
A♭
A♯
B♭
C♯
D♭
D♯
E♭
F♯
G♭
G♯
A♭
A♯
B♭
C♯
D♭
D♯
E♭
F♯
G♭
G♯
A♭
A♯
B♭
sophie.omg.lol
sophie.omg.lol
sophie.omg.lol
sophie.omg.lol
sophie.omg.lol
G A B C D E F G
C♯
D♭
D♯
E♭
F♯
G♭
G♯
A♭
A♯
B♭
G♯
A♭
sophie.omg.lol
The Web Audio API
sophie.omg.lol
sophie.omg.lol
An oscillator
(thing that makes sound)
sophie.omg.lol
A waveform
(what sound to make)
sophie.omg.lol
Frequencies
(what notes to play)
sophie.omg.lol
A piano interface
(to play with)
sophie.omg.lol
sophie.omg.lol
sophie.omg.lol
Different shaped waves =
different sounds
sophie.omg.lol
Different shaped waves =
different sounds
sophie.omg.lol
Different shaped waves =
different sounds
sophie.omg.lol
Different shaped waves =
different sounds
sophie.omg.lol
Different shaped waves =
different sounds
sophie.omg.lol
sophie.omg.lol
We need to calculate
note frequencies
sophie.omg.lol
A4
(A in octave 4)
440Hz
sophie.omg.lol
Magic
Math
C5 523.3Hz
sophie.omg.lol
We can calculate the frequency of a
note in relation to A4
f = 440 ⨉ 2n/12
sophie.omg.lol
sophie.omg.lol
sophie.omg.lol
sophie.omg.lol
sophie.omg.lol
sophie.omg.lol
sophie.omg.lol
sophie.omg.lol
sophie.omg.lol
sophie.omg.lol
sophie.omg.lol
sophie.omg.lol
sophie.omg.lol
sophie.omg.lol
sophie.omg.lol
G A B C D E F G
C♯
D♭
D♯
E♭
F♯
G♭
G♯
A♭
A♯
B♭
G♯
A♭
sophie.omg.lol
sophie.omg.lol
sophie.omg.lol
sophie.omg.lol
✅Play notes with your mouse
✅Play scales
✅Play chords
sophie.omg.lol
But wait! There’s more!
sophie.omg.lol
Web MIDI API
sophie.omg.lol
Command: Key down
Note: 55
Velocity: 100
sophie.omg.lol
sophie.omg.lol
sophie.omg.lol
[144, 55, 100]
sophie.omg.lol
144: note ON
128: note OFF
sophie.omg.lol
sophie.omg.lol
sophie.omg.lol
https://github.com/sophiekoonin/virtualpiano
virtualpiano.vercel.app
Thank you!
🔗 sophie.omg.lol

More Related Content

What's hot

What's hot (19)

Basics of ATmega32
Basics of ATmega32Basics of ATmega32
Basics of ATmega32
 
I2C Protocol
I2C ProtocolI2C Protocol
I2C Protocol
 
TRIAC
TRIACTRIAC
TRIAC
 
DTMF base Mobile controlled appliances control
DTMF base Mobile controlled  appliances controlDTMF base Mobile controlled  appliances control
DTMF base Mobile controlled appliances control
 
Microprocessor based Temperature Controller
Microprocessor based Temperature ControllerMicroprocessor based Temperature Controller
Microprocessor based Temperature Controller
 
GPIO.pptx
GPIO.pptxGPIO.pptx
GPIO.pptx
 
PIN Specification of 8086 Microprocessor
PIN Specification of 8086 MicroprocessorPIN Specification of 8086 Microprocessor
PIN Specification of 8086 Microprocessor
 
8051 microcontroller introduction
8051 microcontroller introduction8051 microcontroller introduction
8051 microcontroller introduction
 
PIC 16F877A by PARTHIBAN. S.
PIC 16F877A   by PARTHIBAN. S.PIC 16F877A   by PARTHIBAN. S.
PIC 16F877A by PARTHIBAN. S.
 
Hardware View of Intel 8051
Hardware View of Intel 8051Hardware View of Intel 8051
Hardware View of Intel 8051
 
Sensors and Actuators
Sensors and ActuatorsSensors and Actuators
Sensors and Actuators
 
Mba in india vs abroad
Mba in india vs abroadMba in india vs abroad
Mba in india vs abroad
 
LED Blinking Using Raspberry Pi
LED Blinking Using Raspberry PiLED Blinking Using Raspberry Pi
LED Blinking Using Raspberry Pi
 
555 TIMER IC & its APPLICATION
555 TIMER IC & its APPLICATION555 TIMER IC & its APPLICATION
555 TIMER IC & its APPLICATION
 
Ppt ssc
Ppt sscPpt ssc
Ppt ssc
 
report on mini project Bicycle lock
 report on mini project Bicycle lock  report on mini project Bicycle lock
report on mini project Bicycle lock
 
Subji kothi.pptx
Subji kothi.pptxSubji kothi.pptx
Subji kothi.pptx
 
Question paper with solution the 8051 microcontroller based embedded systems...
Question paper with solution  the 8051 microcontroller based embedded systems...Question paper with solution  the 8051 microcontroller based embedded systems...
Question paper with solution the 8051 microcontroller based embedded systems...
 
8255
82558255
8255
 

More from Wey Wey Web

More from Wey Wey Web (20)

Portable, secure, and lightweight: Wasm runtimes and their use-cases - Natali...
Portable, secure, and lightweight: Wasm runtimes and their use-cases - Natali...Portable, secure, and lightweight: Wasm runtimes and their use-cases - Natali...
Portable, secure, and lightweight: Wasm runtimes and their use-cases - Natali...
 
Auditing Design Systems for Accessibility - Anna E. Cook
Auditing Design Systems for Accessibility - Anna E. CookAuditing Design Systems for Accessibility - Anna E. Cook
Auditing Design Systems for Accessibility - Anna E. Cook
 
Adaptive Designs, Beyond Pixel Perfection - Stephanie Walter
Adaptive Designs, Beyond Pixel Perfection - Stephanie WalterAdaptive Designs, Beyond Pixel Perfection - Stephanie Walter
Adaptive Designs, Beyond Pixel Perfection - Stephanie Walter
 
Sharing is caring: what to know before you build a Research Repository - Juli...
Sharing is caring: what to know before you build a Research Repository - Juli...Sharing is caring: what to know before you build a Research Repository - Juli...
Sharing is caring: what to know before you build a Research Repository - Juli...
 
Unlocking collaboration: A framework for developers and designers - Alicia Ca...
Unlocking collaboration: A framework for developers and designers - Alicia Ca...Unlocking collaboration: A framework for developers and designers - Alicia Ca...
Unlocking collaboration: A framework for developers and designers - Alicia Ca...
 
3 reasons to switch to OKLCH - Anton Lovchikov
3 reasons to switch to OKLCH - Anton Lovchikov3 reasons to switch to OKLCH - Anton Lovchikov
3 reasons to switch to OKLCH - Anton Lovchikov
 
ChatGPT and AI for web developers - Maximiliano Firtman
ChatGPT and AI for web developers - Maximiliano FirtmanChatGPT and AI for web developers - Maximiliano Firtman
ChatGPT and AI for web developers - Maximiliano Firtman
 
Declarative Design - Jeremy Keith - Wey Wey Wey 2023
Declarative Design - Jeremy Keith - Wey Wey Wey 2023Declarative Design - Jeremy Keith - Wey Wey Wey 2023
Declarative Design - Jeremy Keith - Wey Wey Wey 2023
 
Form follows emotion - Isabella De Cuppis
Form follows emotion - Isabella De CuppisForm follows emotion - Isabella De Cuppis
Form follows emotion - Isabella De Cuppis
 
UX for emerging tech - Josephine Scholtes
UX for emerging tech - Josephine ScholtesUX for emerging tech - Josephine Scholtes
UX for emerging tech - Josephine Scholtes
 
Collaborative software with State Machines - Laura Kalbag
Collaborative software with State Machines -  Laura KalbagCollaborative software with State Machines -  Laura Kalbag
Collaborative software with State Machines - Laura Kalbag
 
Lessons Learned from building Session Replay - Francesco Novy
Lessons Learned from building Session Replay - Francesco NovyLessons Learned from building Session Replay - Francesco Novy
Lessons Learned from building Session Replay - Francesco Novy
 
Let's get visual. Visual testing in your project - Ramona Schwering
Let's get visual. Visual testing in your project - Ramona SchweringLet's get visual. Visual testing in your project - Ramona Schwering
Let's get visual. Visual testing in your project - Ramona Schwering
 
Solving Common Web Component Problems - Simon MacDonald
Solving Common Web Component Problems - Simon MacDonaldSolving Common Web Component Problems - Simon MacDonald
Solving Common Web Component Problems - Simon MacDonald
 
The Future is Malleable - Aleksandra Sikora
The Future is Malleable - Aleksandra SikoraThe Future is Malleable - Aleksandra Sikora
The Future is Malleable - Aleksandra Sikora
 
Trending tools & methodologies for UX - Josephine Scholtes.pdf
Trending tools & methodologies for UX - Josephine Scholtes.pdfTrending tools & methodologies for UX - Josephine Scholtes.pdf
Trending tools & methodologies for UX - Josephine Scholtes.pdf
 
Decoding Web Accessibility through Testing - Anuradha Kumari
Decoding Web Accessibility through Testing  - Anuradha KumariDecoding Web Accessibility through Testing  - Anuradha Kumari
Decoding Web Accessibility through Testing - Anuradha Kumari
 
Good Security is one question away - Wiktoria Dalach
Good Security is one  question away - Wiktoria DalachGood Security is one  question away - Wiktoria Dalach
Good Security is one question away - Wiktoria Dalach
 
Dynamic CSS Secrets - Lea Verou
Dynamic CSS Secrets - Lea Verou Dynamic CSS Secrets - Lea Verou
Dynamic CSS Secrets - Lea Verou
 
The Misty Report - Douglas Crockford
The Misty Report - Douglas CrockfordThe Misty Report - Douglas Crockford
The Misty Report - Douglas Crockford
 

Recently uploaded

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Recently uploaded (20)

Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 

Building a piano with the Web Audio API - Sophie

Editor's Notes

  1. USE CHROME!!! This is a talk to show you the potential of the web
  2. The link in the corner?
  3. tell you how I built it the musical theory behind it, show you a bit of the APIs that power it - the Web Audio API and Web MIDI API First, let’s see what it does
  4. Start with musical theory, science This theory actually went into building the app
  5. I know you’re thinking “this is a web conf!”- it will all become clear Sound = vibrations that travel as waves. The number of vibrations in a set time period = frequency of the sound frequency is measured in Hertz which is one cycle per second. Keys on a piano - hammers on strings - vibrate at frequencies
  6. Western music - set of tones with specific frequencies UK and US has 12 notes - each with a letter, A to G. Division based on frequency Only 7? 5 accidentals. similar to the ones either side, so they don’t get their own letter. Instead we give them a symbol and call them sharp or flat
  7. Talk about notes as on keyboard, starting from C keyboard has 61 and starts from C, piano has 88 notes and starts from A Repeat in groups from lowest to highest
  8. These groups of repeating letters are called octaves 61 keys - 5 octaves 88 keys - 7 ¼ octaves. A4 and A5 demo (next)
  9. Weird ability of brain - same note in diff octaves sounds same - same note but different pitch (high/low) NEXT
  10. Each octave jump - double freq 440Hz -> 880Hz
  11. - Music tends to use only some notes, not all of them. - scales are ordered sequence of notes - a set of tones you can use to build music - like a colour palette - emotions - music uses notes of particular scale - key This picture shows two octaves of the C major scale written in musical notation. Scales follow strict patterns
  12. G major - pattern for major scale. Different notes but same pattern as the one I just showed you One semitone or half tone = distance between one note and its neighbour Tone/whole tone = two semitones Tone, tone, semitone, tone, tone, tone, semitone. Every major scale Other scales have different patterns but are fundamentally the same
  13. Okay! Time for some JavaScript. Enough musical theory. Let’s talk about the Web Audio API. Play audio Mix Analyse Visualise Synthesize
  14. Recap: sound waves + frequencies Synthesizers generate these waves electronically to make sound. We’re basically going to build a little synthesizer. Here’s what we need…
  15. Circuit that produces alternating waveform -> constant tone
  16. Define the waveform Makes a particular sound
  17. frequencies of all the notes on the piano to tell the synthesizer what note to play
  18. And then we need to actually build those piano keys.
  19. initialise an AudioContext. This is like a big graph that contains lots of audio modules linked together exists on the window object in the big 4 browsers. only want one of these in the app: multiple -> lots of sounds at once
  20. createOscillator Connect to audio context destination (default system output) Not doing anything yet
  21. Create waveform The shape of this waveform determines the sound that’s produced. 4 really common waveforms - sine, square, triangle, sawtooth. a sine wave will make a very different sound to a square wave, for example (play audio).
  22. Sine wave
  23. Square
  24. Triangle
  25. Sawtooth
  26. I chose triangle Start oscillator App playing fixed tone @ 440Hz. How do we do different tone?
  27. We can calculate them using a formula§
  28. A4 is an important note A above middle C on the piano - 4th octave Standardised as ISO-16 - standard orchestral tuning frequency Nice round number
  29. Formula to calculate a note’s frequency based on its position relative to another note, which we know the frequency of Makes sense to use 440Hz as a constant
  30. where 𝑛 is the number of semitones between A4 and the note we’re looking for. For example, the note C5 is the next C above A4 on the piano. If you count it out, that’s a difference of 3 semitones.
  31. Translated equation to code. This function takes a letter and number combination -> freq Get interval between A and our note within the same octave Get octave interval - octave difference multiplied by semitones Add together
  32. Translated equation to code. This function takes a letter and number combination -> freq Get interval between A and our note within the same octave Get octave interval - octave difference multiplied by semitones Add together
  33. Run equation to calculate frequency
  34. For convenience we round it to 1 decimal place.
  35. Once we have the frequency, it’s simple to set the oscillator frequency. So now we can make an oscillator play whatever note we want.
  36. Now can build piano Did the OG one in React but had problems with things getting out of sync Vanilla JS has been much easier Each key is a button, positioned with CSS grid Each note has ID - index of array of notes Title attribute
  37. Each key has event listeners for mouse down and mouse up We either play or stop the note Pedal - don’t stop on mouse up
  38. Playing a note - new audio channel for each note press Create a gain node to ramp the sound up and down to make it sound a bit smoother CLICK
  39. We set the gain to zero to start with and ramp it up over 0.06s - if you’re familiar with music terms this is controlling the attack of the note, the onset
  40. We init our oscillator, and connect it to the gain node Think of these like a chain of modules, each one has to be connected to the last
  41. Then we set the wave type and the frequency,
  42. When osc stops, disconnect gain node Garbage collection
  43. When we click a note, init audio - need to respond to user gesture - then find note and init channel, then start osc Storing all the channels in global var
  44. Stopping a note - on mouse up, ramp down to 0 very quickly then stop oscillator Can’t repeatedly stop and start oscillators - need a new one for each one Oscillator disconnects itself when turned off. Every time I play a note I overwrite the channel for that note so the old one will get GC’d
  45. Automatically play notes of scale Patterns of scale -> generate notes Table of intervals for scales Each number represents a semitone interval from the previous note Start at 0 - root note Recap major scale pattern as numbers
  46. Generating scales Generate octave Iterate through scale pattern Cursor currentPosition that keeps track of which note we’re on based on index Add each interval to currentPosition to find next note
  47. Playing scale: change oscillator frequency at regular intervals Only want one oscillator Set timeouts per note - 0.5 sec - multiply by index of note in array so staggered Timeout elapses- osc frequency changes Store timeouts in a global variable if we want to stop
  48. For chords: different patterns, fewer notes, same principle. Get the oscillator to stop playing 2 seconds in Find the frequencies new oscillator for each note.
  49. We’ve got it to…
  50. This is a midi controller... Open browser and demo midi controller
  51. Connects to Musical Instrument Digital Interface devices Chrome, Edge and Opera - not Firefox
  52. When you press a note on the controller it sends a message to the computer with info about what was pressed The browser is listening for those messages
  53. The data we get from the midi message looks like this Command, note, velocity Ignoring velocity
  54. The two commands we care about
  55. Midi standard assigns numbers to each key Default for my midi controller is octaves 3+4, starting at 48 My keyboard has 0-24 When I get a number, have to subtract 48 to get index
  56. Midi standard assigns numbers to each key Default for my midi controller is octaves 3+4, starting at 48 My keyboard has 0-24 When I get a number, have to subtract 48 to get index One more demo for good measure
  57. I’m happy to answer any questions you might have.