Created
May 1, 2012 20:50
-
-
Save kindohm/2571285 to your computer and use it in GitHub Desktop.
audiolib.js fft
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function () { | |
var device, | |
osc1, | |
lfo1, | |
lfo2, | |
lfo3, | |
lfo4, | |
fft, | |
fps = 30, | |
channelCount = 2, | |
width, | |
height, | |
context, | |
playing = false; | |
function audioCallback (buffer, channels) { | |
if (!playing) return; | |
var i, sample, bufferLength = buffer.length; | |
for (i = 0; i < bufferLength; i += channels) { | |
lfo1.generate(); | |
lfo2.generate(); | |
lfo3.generate(); | |
lfo4.generate(); | |
lfo2.fm = lfo1.getMix(); | |
lfo3.fm = lfo2.getMix(); | |
lfo4.fm = lfo3.getMix(); | |
osc1.fm = lfo4.getMix(); | |
osc1.generate(); | |
sample = osc1.getMix(); | |
fft.pushSample(sample); | |
for (n = 0; n < channelCount; n++){ | |
buffer[i + n] = sample; | |
} | |
} | |
} | |
window.addEventListener('load', function() { | |
var canvas, button; | |
// set up audio stuff | |
device = audioLib.AudioDevice(audioCallback, channelCount); | |
osc1 = audioLib.Oscillator(device.sampleRate, 600); | |
osc1.waveShape = 'triangle'; | |
lfo1 = audioLib.Oscillator(device.sampleRate, 0.1); | |
lfo2 = audioLib.Oscillator(device.sampleRate, 10.3); | |
lfo3 = audioLib.Oscillator(device.sampleRate, 0.7); | |
lfo4 = audioLib.Oscillator(device.sampleRate, 6.2); | |
fft = audioLib.FFT(device.sampleRate, 4096); | |
// set up UI display stuff | |
canvas = document.getElementById('canvas'); | |
context = canvas.getContext('2d'); | |
width = canvas.width; | |
height = canvas.height; | |
gradient = context.createLinearGradient(0, 0, 0, height / 2); | |
gradient.addColorStop(0, "#ff0000"); | |
gradient.addColorStop(1, "#0000ff"); | |
context.fillStyle = gradient; | |
context.lineWidth = 1; | |
button = document.getElementById('playButton'); | |
button.onclick = function () { | |
playing = !playing; | |
button.innerHTML = playing ? 'pause' : 'play'; | |
}; | |
}); | |
function drawFFT () { | |
var length, count; | |
length = fft.spectrum.length / 8; | |
context.clearRect(0, 0, width, height); | |
context.beginPath(); | |
context.moveTo(0, height); | |
for (count = 0; count < length; count++) { | |
context.lineTo(count / length * width, | |
fft.spectrum[count] * -height * 2 + height); | |
} | |
context.moveTo(width,0); | |
context.closePath(); | |
context.fill(); | |
context.stroke(); | |
} | |
Sink.doInterval(function(){ | |
drawFFT(); | |
}, 1000/fps); | |
})(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>audiolib.js FFT demo</title> | |
<script type="text/javascript" src="audiolib.min.js"></script> | |
<script type="text/javascript" src="fft.js"></script> | |
</head> | |
<body> | |
<p><button id="playButton">play</button></p> | |
<canvas id="canvas" width="600" height="200" style="background: #eee;"></canvas> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment