From 187da0f140fae93fdc42f6d435b80d6dee72384c Mon Sep 17 00:00:00 2001 From: Matt Date: Sat, 9 May 2015 21:48:34 -0400 Subject: [PATCH] Generate commands onload Rather an on user input. Removes lag from click to camera firing. --- webapp/index.html | 2 +- webapp/js/fsk.js | 15 ++++++++++----- webapp/js/intval.js | 26 ++++++++++++++++++-------- 3 files changed, 29 insertions(+), 14 deletions(-) diff --git a/webapp/index.html b/webapp/index.html index 04e700d..c7e62c5 100644 --- a/webapp/index.html +++ b/webapp/index.html @@ -22,7 +22,7 @@
0
-
+
0
diff --git a/webapp/js/fsk.js b/webapp/js/fsk.js index d5a3b69..b053c73 100644 --- a/webapp/js/fsk.js +++ b/webapp/js/fsk.js @@ -1,4 +1,4 @@ -var dataURI, audio, myAudioContext, mySource, myBuffer; +var dataURI, audio, myAudioContext, mySource, myBuffer, bufferArray={}; if ('AudioContext' in window) { myAudioContext = new AudioContext(); @@ -51,6 +51,7 @@ var fsk = {}; fsk.generate = function (str) { if (str.length === 0) return; + //console.time('generate'); var utf8 = toUTF8(str); //console.log(utf8); @@ -66,7 +67,7 @@ fsk.generate = function (str) { var data = "RIFF" + chr32(size + 36) + "WAVE" + "fmt " + chr32(16, 0x00010001, sampleRate, sampleRate, 0x00080001) + "data" + chr32(size); - + var pushData = function (freq, samples) { for (var i = 0; i < samples; i++) { var v = 128 + 127 * Math.sin((2 * Math.PI) * (i / sampleRate) * freq); @@ -87,13 +88,17 @@ fsk.generate = function (str) { var arrayBuff = Base64Binary.decodeArrayBuffer(dataURI); myAudioContext.decodeAudioData(arrayBuff, function (audioData) { - myBuffer = audioData; + bufferArray[str] = audioData; }); } -fsk.play = function () { +fsk.play = function (str) { mySource = myAudioContext.createBufferSource(); - mySource.buffer = myBuffer; + if (typeof str !== 'undefined') { + mySource.buffer = bufferArray[str]; + } else { + console.error('No AudioBuffer found for string "' + str + '"'); + } mySource.connect(myAudioContext.destination); if ('AudioContext' in window) { mySource.start(0); diff --git a/webapp/js/intval.js b/webapp/js/intval.js index 9144764..8102a55 100644 --- a/webapp/js/intval.js +++ b/webapp/js/intval.js @@ -9,17 +9,23 @@ ctrl.pos = 0; ctrl.loop = false; ctrl.delay = { - f : 500, - B : 700, + f : 1100, + b : 1200, x : 1000 }; ctrl.play = function (val) { - fsk.generate(val); - setTimeout(function () { - fsk.play(); - ctrl.posCmd(val); - }, 10); + fsk.play(val); + fsk.play(val); + ctrl.posCmd(val); +}; + +ctrl.generate = function () { + var cmds = Object.keys(ctrl.delay); + for (var i = 0; i < cmds.length; i++) { + console.log('Generating AudioBuffer for ' + cmds[i]); + fsk.generate(cmds[i]); + } }; ctrl.sendCmd = function (elem) { @@ -97,7 +103,7 @@ ctrl.highlight = function (i) { if (i !== undefined && i !== false) { //console.log(i); $('.seq .row_f > div').eq(i).addClass('highlight'); - $('.seq .row_B > div').eq(i).addClass('highlight'); + $('.seq .row_b > div').eq(i).addClass('highlight'); $('.seq .row_x > div').eq(i).addClass('highlight'); } }; @@ -229,6 +235,10 @@ ctrl.onload = function () { if (seq !== undefined && seq !== false) { ctrl.seq = seq; } + + //pre-generate audiobuffers for more responsive buttons + ctrl.generate(); + ctrl.layout(); //FastClick.attach(document.body); };