WebAudio: live input
Live input, aka getUserMedia: it exists in Chrome Canary for audio too. Great times to be a web developer, right?
Let's check it out.
Here's the demo, but first a prerequisite: go chrome://flags, search for "Web Audio Input" and enable it. Restart Chrome Canary.
With a guitar
I wanted to have a little trickier setup and capture guitar sound not just voice with a microphone.
As always, it was bigger hurdle to get guitar sound to the computer, than anything else JavaScript-wise.
I have a guitar amp that has a mini-USB out. This goes to the USB of the computer. Wrestle, system settings, garage band to the rescue.... eventually the computer makes sound.
Capturing
I was assuming the stream you get from getuserMedia can go directly to an HTML <audio> src. No such luck. Works for video but not yet for audio.
So... WebAudio API saves the day.
Setting up audio context (like in the previous post), shimming getUserMedia and setting up callbacks for it:
// for logging function fire(e, data) { log.innerHTML += "\n" + e + " " + (data || ''); } // globals var audio_context; var volume; // one-off initialization (function init(g){ try { audio_context = new (g.AudioContext || g.webkitAudioContext); fire('Audio context OK'); // shim navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia; fire('navigator.getUserMedia ' + (navigator.getUserMedia ? 'OK' : 'fail')); // use navigator.getUserMedia( {audio:true}, iCanHazUserMedia, function(e){fire('No live audio input ' + e);} ); } catch (e) { alert('No web audio support in this browser'); } }(window));
When the user loads the page, here's what they see:

In my case I select the guitar amp and click "Allow" button.
This little window informs me the page is using the audio input:

Playing back
Now that the user has allowed audio access, let's play back the audio we receive, but pass it through a volume control.
All this work happens in the iCanhazUserMedia(), the success callback to getUserMedia.
function iCanHazUserMedia(stream) { fire('I haz live stream'); var input = audio_context.createMediaStreamSource(stream); volume = audio_context.createGainNode(); volume.gain.value = 0.8; input.connect(volume); volume.connect(audio_context.destination); fire('input connected to destination'); }
What we have here (ignoring fire()):
- setup an input stream from the user stream, this is the first node in the audio chain
- setup a volume (Gain) node with initial volume 0.8 out of 1
- connect input to volume to output/speakers
And this is it!
Additionally an input type=range max=1 step=0.1 can change the volume via volume.gain.value = value;
Go play! Isn't it amazing that you can now grab microphone or any other audio input and play around with it? All in JavaScript, all in the browser without any plugins.
Moar!
This was a very basic exploratory/primer example. For more:
- Pedalboard.js with more effects besides volume
- One of Chris Wilson's demos has even more effects
- and how 'bout a guitar tuna
This entry was posted on Sunday, October 28th, 2012 and is filed under JavaScript, Music, WebAudio. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
Get notification for future posts: follow me on Twitter or subscribe to my RSS feed

October 28th, 2012 at 1:08 pm
We want a video!
Related: Did you see Stuart’s performance from JSConf EU? https://www.youtube.com/watch?v=PN8Eg1K9xjE
October 29th, 2012 at 6:47 pm
Haven;t yet started watching the 2012 vids, will do, thanks for posting them!
Video?… not sure how to record screenshot and sound the browser makes (not input from microphone and such)
October 30th, 2012 at 1:00 am
Put up a camera showing you play guitar and take the audio from the audio-out of the computer running the browser.
October 30th, 2012 at 3:02 pm
good idea, I also have this iShowU thing, seems it will do the job
That video of Stuart is awesome!
March 15th, 2013 at 2:24 am
a video would be nice, and maybe a link to Chrome Canary along with any predictions on when all the browsers will come around.
April 9th, 2013 at 12:30 am
I’m not positive the place you’re getting your info, however great topic. I needs to spend some time studying much more or working out more. Thank you for excellent information I was on the lookout for this info for my mission.
May 12th, 2013 at 5:48 pm
Hi, i feel that i noticed you visited my blog so i came to return the desire?.I am trying to in finding things to improve my site!I guess its good enough to make use of some of your ideas!!
May 17th, 2013 at 10:04 pm
It’s a fantastic along with practical item of facts. I’m just pleased you distributed this useful facts along with us. Make sure you be us up to date this way. Thanks for expressing.