I created the getSpectrum method using the getByteFrequencyData method in the Node Web Audio API Analyzer. The array of returned audio data refers to the sound source (either to the element or to the Audio () element) volume , a value from 0 to 1.
Using the volume of the audio source, I am trying to normalize every value obtained from getByteFrequencyData so that the user of getSpectrum does not need to worry about the volume when they render the audio data.
This is the striped version of getSpectrum.
var audioData = new Uint8Array(analyser.binCount); var spectrum = []; analyser.getByteFrequencyData(audioData); for (var i = 0; i < audioData.length; i++) { var value = audioData[i];
The W3C specification refers to the equation used to calculate the return value given maxDecibels and minDecibels. With my rudimentary understanding, I tried to change the math to get a normalized value, but I can't get it to work correctly. I am having trouble achieving this using only a volume value from 0 to 1.
Any incitement would be greatly appreciated! Heres a working example of a problem. Changing the volume slider will show the problem.
Update 7/22/16: Thanks to @ raymond-toy's answer, I figured out how to convert the value 0 to 1 to decibels.
volumeDB = Math.abs((Math.log(volume)/Math.LN10)*20);
After getting the DB, I inverted the equation in the W3C specification,
value = ((audioDataValue * volumeDB) / 255) - volumeDB
Unfortunately, value still ends relative to volume . Does anyone see what I'm missing?
source share