Synthesis and Visualization
Mithun Selvaratnam
Audio in the Web Browser
If simple playback is all you need, go for it! However, <audio> has several limitations that keep it from being reliable for audio-intensive applications.
// HTML
<audio src="audio.mp3" id="demo" preload="auto"></audio>
// JavaScript
var audio = document.getElementById('demo')
audio.play()
var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
var oscillator = audioCtx.createOscillator();
oscillator.connect(audioCtx.destination);
Think of audio context as a flowchart of nodes, starting with an sound source and ending with an output.
Hold on...oscillator? filter? gain? What does all of it MEAN?
Audio Analysis
var waveAnalyser = audioCtx.createAnalyser();
waveAnalyser.fftSize = 2048;
// creates an array of 8-bit unsigned integers
// frequencyBinCount is a number equal to half of the FFTSize (Fast Fourier Transform)
// FFT is used to interpret frequency or time data of waveforms
var dataArray = new Uint8Array(waveAnalyser.frequencyBinCount);
// will fill dataArray with values corresponding to changes in amplitude over time
waveAnalyser.getByteTimeDomainData(dataArray);
// will fill dataArray with values corresponding to changes in amplitude over frequency
waveAnalyser.getByteFrequencyData(dataArray);
// the values in dataArray can then be passed into a visualizing tool like <canvas>
Conclusion
References