find frequencies from audio file in javascript

To find frequencies from an audio file in JavaScript, you can use the Web Audio API. Here's an example code snippet to get you started:

index.tsx
// create an AudioContext
const audioContext = new AudioContext();

// load the audio file into a buffer
fetch('audiofile.wav')
  .then(response => response.arrayBuffer())
  .then(arrayBuffer => audioContext.decodeAudioData(arrayBuffer))
  .then(audioBuffer => {
    
    // create an AnalyserNode
    const analyserNode = audioContext.createAnalyser();
    analyserNode.fftSize = 2048;
    
    // connect the AudioBufferSourceNode to the AnalyserNode
    const sourceNode = audioContext.createBufferSource();
    sourceNode.buffer = audioBuffer;
    sourceNode.connect(analyserNode);
    analyserNode.connect(audioContext.destination);
    
    // get the frequency data from the AnalyserNode
    const frequencyData = new Float32Array(analyserNode.frequencyBinCount);
    analyserNode.getFloatFrequencyData(frequencyData);

    // analyze the frequency data to find the dominant frequency
    const maxFrequencyIndex = frequencyData.indexOf(Math.max(...frequencyData));
    const dominantFrequency = audioContext.sampleRate * maxFrequencyIndex / frequencyData.length;

    console.log(`The dominant frequency is ${dominantFrequency} Hz`);
  });
1148 chars
30 lines

This code creates an AudioContext object and loads an audio file into a buffer. It then creates an AnalyserNode, connects the buffer to it, and gets the frequency data from the node. Finally, it analyzes the frequency data to find the dominant frequency and logs it to the console. Note that this is just a simple example and there are many ways to analyze the frequency data to get more information about the audio signal.

gistlibby LogSnag