Skip to content

0R Getting frequencies and amplitudes from audio source

Luis Murillo Baltodano edited this page Aug 2, 2020 · 1 revision

How to use the analyseAudio function

Use the analyseAudio function

analyzer = analyseAudio(audio)

Make sure you call this function after an user input, like a click or another type of event because there might be problems with autoplay blocking for media sources

Use the analyzer to get frequencies and amplitudes

analyzer.getFrequencies()
analyzer.getAmplitudes()

These will return a 1024 elements long array full of frequencies or amplitudes

Getting the average

analyzer.getFrequencies().average
analyzer.getAmplitudes().average

Example

let analyser

preset(({analyseAudio, setAudioToggle}) => {
  setAudioToggle({
    src: 'https://luisarmando-testcoder.github.io/keeper/mp3/moving.mp3'
  }, audio => {
    if (!analyser) analyser = analyseAudio(audio)
    // To avoid reasining the analyzer if it is already connected previously to a different MediaElementSourceNode
  })
})
Clone this wiki locally