Skip to content

Instantly share code, notes, and snippets.

@kane-thornwyrd
Created May 31, 2012 12:26
Show Gist options
  • Save kane-thornwyrd/2843058 to your computer and use it in GitHub Desktop.
Save kane-thornwyrd/2843058 to your computer and use it in GitHub Desktop.
A start for maybe a 10k JS Demo
<!DOCTYPE html>
<html>
<head>
<title></title>
<script>
function AudioDataDestination(sR) {
var aO = new Audio(), cWpos = 0, pBs = sR / 2, ta = null, taPos;
aO.mozSetup(1, sR);
setInterval(function() {
var w;
if(ta) {
w = aO.mozWriteAudio(ta.subarray(taPos));
cWpos +=
taPos += w;
if(taPos < ta.length) return;
ta = null;
}
// Check if we need add some data to the audio output.
var curPos = aO.mozCurrentSampleOffset(),
ava = curPos + pBs - cWpos;
if(ava > 0) {
// Request some sound data from the callback function.
var soundData = new Float32Array(ava);
var k = 2* Math.PI * frequency / sR;
for (var i=0, size=soundData.length; i<size; i++) {
var o = k* currentSoundSample++;
soundData[i] = Math.sin(( ((o*4)*(((o>>>11)&1)?1:0)*(((o>>>11)&8)?0:1) + (o*2)*(((o>>>10)&1)?1:0)*(((o>>>10)&128)?0:1) +(o*4)*(((o>>>10)&1)?1:0)*(((o>>>10)&128)?1:0) + (o*Math.sin(o)*(((o>>>10)&4)?1:0)*(((o>>>10)&1)?1:0))) % 256) * (7/10));
}
// Writting the data.
w = aO.mozWriteAudio(soundData);
if(w < soundData.length) {
// Not all the data was written, saving the tail.
ta = soundData;
taPos = w;
}
cWpos += w;
}
}, 100);
}
// Control and generate the sound.
var frequency = 0, currentSoundSample;
var sampleRate = 8192;
var audioDestination = new AudioDataDestination(sampleRate);
function start(freq) {
currentSoundSample = 0;
frequency = freq;
}
function stop() {
frequency = 0;
}
start(1024);
</script>
</head>
<body>
<button onclick="stop()">stop</button>
<button onclick="start(1024)">Normal</button>
<button onclick="start(2048)">Speed !!!</button>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment