audio-frequency-tempered
Distributes the data returned from AnalyserNode.getByteFrequencyData() according to a logarithmic scale. Low frequency bins are shared by multiple bars, while high frequency ones are bundled together.
The technique comes from Henrique Vianna's audioMotion-analyzer.
Install
npm install audio-frequency-tempered
Example
const { createAudioBars, updateAudioBars } = require('audio-frequency-tempered');
// create audio context, analyser, data array
// omitted: source, gain, connect, etc.
const audioCtx = new AudioContext();
const analyser = audioCtx.createAnalyser();
const audioData = new Uint8Array(analyser.frequencyBinCount);
// create audio bars
const audioBars = createAudioBars({ groupLevel: 8 });
// on update
analyser.getByteFrequencyData(audioData);
updateAudioBars(audioData);
audioBars.forEach(bar => console.log(bar));
Output:
{ value: 0.6078, factor: 1, iniBin: 6, endBin: 9 }
{ value: 0.8196, factor: 1, iniBin: 10, endBin: 18 }
{ value: 0.8980, factor: 1, iniBin: 19, endBin: 37 }
{ value: 0.7254, factor: 1, iniBin: 38, endBin: 73 }
{ value: 0.5215, factor: 1, iniBin: 74, endBin: 146 }
{ value: 0.4352, factor: 1, iniBin: 147, endBin: 292 }
{ value: 0.4627, factor: 1, iniBin: 293, endBin: 584 }
{ value: 0.4313, factor: 1, iniBin: 585, endBin: 1167 }
{ value: 0.3411, factor: 1, iniBin: 1168, endBin: 2333 }
{ value: 0.0000, factor: 1, iniBin: 2334, endBin: 3110 }
Demo
Usage
createAudioBars(options)
-
options
-
groupLevel
(default5
) 1 to 8, where 1 = 1/24 octave and 8 = full octave -
sampleRate
(default44100
) audioContext.sampleRate -
frequencyBinCount
(default1024
) analyser.frequencyBinCount -
minFreq
(default20
) minimum frequency (Hz) -
maxFreq
(default22000
) maximum frequency (Hz)
-
Returns an array of 'bar' objects { iniBin, endBin, factor, value }
-
iniBin
- initial bin of the analyser frequency data -
endBin
- end bin of the analyser frequency data -
factor
- interpolation factor - when multiple bars share the same bin -
value
- normalised maximum energy value of the frequencies betweeniniBin
andendBin
updateAudioBars(audioData)
-
audioData
Uint8Array passed to analyser.getByteFrequencyData
Returns null
getAudioBars()
Returns the previously created array of audio bars.
getAudioEnergy()
Returns the average audio energy (sum of bar values / number of bars).
See Also
License
MIT, see LICENSE for details.