Home:ALL Converter>How to get microphone input volume value with web audio api?

How to get microphone input volume value with web audio api?

Ask Time:2014-01-21T09:39:56         Author:kikkpunk

Json Formatter

I am using the microphone input with web audio api and need to get the volume value.

Right now I have already got the microphone to work: http://updates.html5rocks.com/2012/09/Live-Web-Audio-Input-Enabled

Also, i know there's a method manipulating the volume of audio file: http://www.html5rocks.com/en/tutorials/webaudio/intro/

    // Create a gain node.
    var gainNode = context.createGain();
    // Connect the source to the gain node.
    source.connect(gainNode);
    // Connect the gain node to the destination.
    gainNode.connect(context.destination);
   // Reduce the volume.
   gainNode.gain.value = 0.5;

But how to combine these two and get the input volume value? I just need to the value, no need to manipulate it.

Does anybody know?

Author:kikkpunk,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/21247571/how-to-get-microphone-input-volume-value-with-web-audio-api
cwilso :

There are two main reasons to want to get the \"volume\":\n\n\ndetect when the source \"clips\" - i.e. the absolute value of the signal goes over a preset level, usually very near 1.0, where it will start clipping.\ngive the user a feel for how loud their signal is.\n\n\nThe reason I list these separately is because the first requires that you process every sample - because you might miss a short transient otherwise. For this, you'll need to use a ScriptProcessor node, and you'll have to iterate through every sample in the buffer inside onaudioprocess to look for absolute values above your clip level. You could just determine the RMS level then, too - just sum the squares of each sample, divide by N and take the square root. DO NOT render from within onaudioprocess, though - set values that you access on requestAnimationFrame.\n\nYou can also use an AnalyserNode to do the level detection, and just average out the data, kind of like what the other answer does with getAverageVolume. However, the other answer is NOT a good use of ScriptProcessor - in fact, it's doing no processing of the script node at all, not even passing the data through, it's just using it like a timer callback. You would be FAR better served by using requestAnimationFrame as the visual callback; don't ever set layout or visual parameters from inside onaudioprocess like this, or you're begging to thrash your audio system. If you don't need clip detection, just do the getByteFrequencyCount/getAverageVolumefrom above on an AnalyserNode (but you should minimize the number of bands in the Analyser - 64 is the minimum, I think), and you should pre-allocate and reuse a Uint8Array rather than allocating it each time (that will amp up the garbage collection).",
2014-01-21T22:48:19
Netorica :

I did a volume display for a playing audio when I was studying HTML 5.\n\nI followed this great tutorial \n\nhttp://www.smartjava.org/content/exploring-html5-web-audio-visualizing-sound\n\n // setup a analyzer\n analyser = context.createAnalyser();\n analyser.smoothingTimeConstant = 0.3;\n analyser.fftSize = 1024;\n\n javascriptNode = context.createScriptProcessor(2048, 1, 1);\n\n\n javascriptNode.onaudioprocess = function() {\n\n // get the average, bincount is fftsize / 2\n var array = new Uint8Array(analyser.frequencyBinCount);\n analyser.getByteFrequencyData(array);\n var average = getAverageVolume(array)\n\n console.log('VOLUME:' + average); //here's the volume\n }\n\n function getAverageVolume(array) {\n var values = 0;\n var average;\n\n var length = array.length;\n\n // get all the frequency amplitudes\n for (var i = 0; i < length; i++) {\n values += array[i];\n }\n\n average = values / length;\n return average;\n }\n\n\nNOTE: I just don't know if it will work on an audio input came from a microphone",
2014-01-21T01:56:58
yy