Permalink
Browse files

Initial commit.

  • Loading branch information...
stoive committed Mar 29, 2011
0 parents commit 2591ce7d83a9427b16422f7a84997ca721cefec0
Showing with 3,135 additions and 0 deletions.
  1. 0 LICENCE
  2. 0 README
  3. +2,261 −0 allplugins-require.js
  4. +186 −0 aud.i-o.js
  5. BIN audiofeed.swf
  6. +11 −0 example.htm
  7. +98 −0 lib/audio.js
  8. +46 −0 lib/riff.js
  9. +29 −0 main.js
  10. +74 −0 types/audio/wav.js
  11. +430 −0 types/image/bitmap.js
No changes.
0 README
No changes.

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -0,0 +1,186 @@
+/*
+
+Copyright (c) 2011, Steven Thurlow
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
+
+* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
+* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
+* Neither the name of aud.i-o nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+
+*/
+
+window['aud.i-o'] = (function(){
+ var createAudioInputSourceNodeQueue = [];
+ var isAudioLoaded = false;
+ return {
+ createAudioInputSourceNode: function(callbackName, id) {
+ if (!isAudioLoaded) {
+ createAudioInputSourceNodeQueue.push(function(){
+ this.flashObject.getFeed(callbackName, id);
+ });
+ }
+ else
+ this.flashObject.getFeed(callbackName, id);
+ },
+ createFlashObject: function() {
+ if (this.flashObject) return;
+
+ this.flashObject = document.createElement('object');
+ this.flashObject.id = "audioFeed";
+ this.flashObject.width = 300;
+ this.flashObject.height = 200;
+ this.flashObject.style.display = 'block';
+ this.flashObject.style.left = '10px';
+
+ var param1 = document.createElement('param');
+ param1.setAttribute('name', 'movie');
+ param1.setAttribute('value', 'audiofeed.swf');
+ this.flashObject.appendChild(param1);
+
+ var param2 = document.createElement('param');
+ param2.setAttribute('name', 'allowScriptAccess');
+ param2.setAttribute('value', '*');
+ this.flashObject.appendChild(param2);
+
+ var embed = document.createElement('embed');
+ embed.setAttribute('src', 'audiofeed.swf');
+ embed.setAttribute('name', 'audioFeed');
+ embed.setAttribute('swliveconnect', 'true');
+ embed.setAttribute('type', 'application/x-shockwave-flash');
+
+ this.flashObject.appendChild(embed);
+
+ var div = document.createElement('div');
+ div.id = 'audioFlashPromptNotice';
+ div.style.position = 'absolute';
+ div.style.top = "-220px";
+ div.style.left = ((document.width - 320) / 2).toString() + "px";
+ div.style.border = '3px solid black';
+ div.style['background-color'] = "white";
+ div.style['border-top'] = "none";
+ div.style['border-radius'] = "0px 0px 10px 10px";
+ div.style['padding-bottom'] = "10px";
+ div.style['-webkit-transition'] = "top 1s ease-out";
+ div.appendChild(this.flashObject);
+
+ var self = this;
+ window["flAudioLoaded"] = function() {
+ isAudioLoaded = true;
+ for (var i = 0; i < createAudioInputSourceNodeQueue.length; i++) createAudioInputSourceNodeQueue[i].apply(self);
+ };
+
+ document.body.appendChild(div);
+
+ setTimeout(function() {div.style.top = "0px"}, 0);
+
+ window["flAudioPermissionSelected"] = function() {
+ div.style.top = "-220px";
+ };
+ },
+ flashObject: null
+ };
+})();
+
+if (!AudioBuffer) {
+ var AudioBuffer = function(buffers) {
+ this.buffers = buffers;
+ this.numberOfChannels
+ }
+ AudioBuffer.prototype = {
+ buffers: [],
+ gain: 1.0,
+ sampleRate: 44100,
+ get bufLength() {
+ return this.buffers[0].length;
+ },
+ get duration() {
+ return (1 / sampleRate) * length;
+ },
+ get numberOfChannels() {
+ return this.buffers.length;
+ },
+ getChannelData: function(channel) {
+ return this.buffers[channel];
+ }
+ };
+}
+
+if (!AudioProcessingEvent) {
+ var AudioProcessingEvent = function(node, playbackTime, inputBuffer, outputBuffer) {
+ this.node = node;
+ this.playbackTime = playbackTime;
+ this.inputBuffer = inputBuffer;
+ this.outputBuffer = outputBuffer;
+ }
+ AudioProcessingEvent.prototype = {
+ node: null,
+ playbackTime: null,
+ inputBuffer: null,
+ outputBuffer: null
+ }
+}
+
+if (!AudioContext) {
+ var AudioContext = function() {};
+ AudioContext.prototype = {
+ inputNodes: {},
+ obj: null,
+ createAudioInputSourceNode: function(id) {
+ window['aud.i-o'].createFlashObject();
+ if (typeof(id) != "number") id = -1;
+
+ if (!this.inputNodes[id]) {
+ this.inputNodes[id] = {};
+ this.inputNodes[id].__proto__ = AudioInputSourceNode.prototype;
+ }
+ var logged = false;
+ var callbackName = "flAudioCallback" + (id >= 0 ? id.toString() : "");
+ var self = this;
+ window[callbackName] = function(data) {
+ var buffer = Float32Array ? new AudioBuffer([new Float32Array(data)]) : new AudioBuffer([data]);
+ var event = new AudioProcessingEvent(self.inputNodes[id], new Date().valueOf(), buffer, null);
+ if (self.inputNodes[id].onaudioprocess) self.inputNodes[id].onaudioprocess(event);
+ }
+
+ window['aud.i-o'].createAudioInputSourceNode(callbackName, id);
+
+ return this.inputNodes[id];
+ },
+ get getAudioInputSources() {
+ return window['aud.i-o'].flashObject.getFeeds();
+ }
+ };
+}
+
+if (!AudioNode) {
+ var AudioNode = function() { throw "Invalid Constructor"; }
+ AudioNode.prototype = {
+ connect: function(destination, output, input) {
+ },
+ disconnect: function(output) {
+ },
+ context: null,
+ numberOfInputs: 0,
+ numberOfOutputs: 0
+ };
+}
+
+if (!AudioSourceNode) {
+ var AudioSourceNode = function() { throw "Invalid Constructor"; }
+ AudioSourceNode.prototype = {};
+ AudioSourceNode.prototype.__proto__ = AudioNode.prototype;
+}
+
+if (!AudioInputSourceNode) {
+ var AudioInputSourceNode = function(id) { throw "Invalid Constructor"; }
+ AudioInputSourceNode.prototype = {
+ onaudioprocess: null,
+ bufferSize: 0
+ };
+ AudioInputSourceNode.prototype.__proto__ = AudioSourceNode.prototype;
+}
Binary file not shown.
@@ -0,0 +1,11 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <title>Example page</title>
+ <script data-main="main" src="allplugins-require.js"></script>
+ <script src="aud.i-o.js"></script>
+ </head>
+ <body>
+ <h1>Example page</h1>
+ </body>
+</html>
@@ -0,0 +1,98 @@
+/*
+
+Copyright (c) 2011, Steven Thurlow
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
+
+* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
+* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
+* Neither the name of Steven Thurlow nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+*/
+
+define(function() {
+ if (!Math.sign) {
+ // Todo: compare this method with if (>0) 1; if (<0) -1; 0;
+ // Todo: consider negative and positive zero, infinity
+ Math.sign = function(x) { if (isFinite(x)) {return (x / Math.abs(x) || 0)} throw "Write your own damn Math.sign(x)."; };
+ }
+
+ function nthOrderError(i, arr, order, seeds) {
+ if (order > 0) {
+ return nthOrderError(i, arr, order-1, seeds) - nthOrderError(i-1, arr, order-1, seeds);
+ }
+ else {
+ if (i >= 0) return arr[i];
+ else return (seeds[seeds.length + i] || 0);
+ }
+ }
+
+ function nthOrderPrediction(i, arr, order, seeds) {
+ function val(i) { return (arr[i] || seeds[seeds.length + i] || 0); }
+ if (order == 0)
+ return 0;
+ if (order == 1)
+ return val(i-1);
+ if (order == 2)
+ return 2*val(i-1) - val(i-2);
+ if (order == 3)
+ return 3*val(i-1) - 3*val(i-2) + val(i-3);
+ }
+
+ return {
+ quantize: function(samples, bits) {
+ //var err = 0;
+ var steps = Math.pow(2, bits) / 2;
+ var stepSize = 1 / steps;
+
+ return Array.prototype.map.call(samples, function(curr, i, out) {
+ //var val = (curr + err) / stepSize;
+ var val = curr / stepSize;
+ if (val >= 0) val = Math.round(val >= steps ? steps - 1 : val);
+ else val = Math.round(val < -steps ? -steps : val);
+
+ //err = curr + err - val * stepSize;
+ return val;
+ });
+ },
+ unquantize: function(samples, bits, method) {
+
+ var steps = Math.pow(2, bits) / 2;
+ var stepSize = dataFrame.range / steps;
+
+ return Array.prototype.map.call(samples, function(curr, i, out) {
+ return curr * stepSize;
+ });
+ },
+ quantizationMethods: {
+ adaptive: null,
+ fixed: null
+ },
+ unquantizationMethods: {
+ adaptive: null,
+ fixed: null
+ },
+ muLaw: function(x, bits) {
+ var mu = Math.pow(2, bits) - 1;
+ return Math.sign(x) * (Math.log(1 + mu * Math.abs(x)) / Math.log(1 + mu));
+ },
+ inverseMuLaw: function(x, bits) {
+ var mu = Math.pow(2, bits) - 1;
+ return Math.sign(x) * (1 / mu) * (Math.pow(1 + mu, Math.abs(x)) - 1);
+ },
+ predict: function(samples, order, seeds) {
+ seeds = seeds || [];
+ return Array.prototype.map.call(samples, function(curr, i, out) {
+ return nthOrderError(i, out, order, seeds);
+ });
+ },
+ restore: function(samples, order, seeds) {
+ return Array.prototype.map.call(samples, function(curr,i,input) {
+ return (nthOrderPrediction(i, output, order, seeds) + curr);
+ });
+ }
+ };
+});
@@ -0,0 +1,46 @@
+define(function() {
+ var chunk = function(ckID, ckSize, buffer, offset) {
+ if (!(ckID && ckSize)) {
+ throw "Invalid arguments.";
+ }
+ if (ckID.length != 4) {
+ throw "RIFF chunk ID must be exactly 4 bytes long"
+ }
+
+ if (buffer && typeof(offset) === "number") {
+ this.dataView = new DataView(buffer, offset, ckSize + 8);
+ }
+ else {
+ this.dataView = new DataView(new ArrayBuffer(ckSize + 8));
+ }
+
+ this.ckID = ckID;
+ this.dataView.setInt32(4, ckSize, true);
+ };
+ chunk.prototype = {
+ get ckID() {
+ var str = "";
+ str += String.fromCharCode(this.dataView.getUint8(0));
+ str += String.fromCharCode(this.dataView.getUint8(1));
+ str += String.fromCharCode(this.dataView.getUint8(2));
+ str += String.fromCharCode(this.dataView.getUint8(3));
+ return str;
+ },
+ set ckID(value) {
+ if (value.length != 4) throw "Invalid argument.";
+ this.dataView.setUint8(0, (value).charCodeAt(0));
+ this.dataView.setUint8(1, (value).charCodeAt(1));
+ this.dataView.setUint8(2, (value).charCodeAt(2));
+ this.dataView.setUint8(3, (value).charCodeAt(3));
+ },
+ get ckSize() {
+ return this.dataView.byteLength - 8;
+ },
+ get ckData() {
+ return new DataView(this.dataView.buffer, this.dataView.byteOffset + 8, this.ckSize);
+ }
+ };
+ return {
+ Chunk: chunk
+ }
+});
29 main.js
@@ -0,0 +1,29 @@
+require.ready(function(){
+ var ac = new AudioContext();
+ var aisn = ac.createAudioInputSourceNode();
+ var samples = [];var Wav;
+ var data;
+ var write = true;
+ aisn.onaudioprocess = function(event) {
+ if (samples.length < 100000) {
+ samples = samples.concat(Array.prototype.slice.call(event.inputBuffer.getChannelData(0)))
+ }
+ else if (write) {
+ write=false;
+ require(['types/audio/wav'], function(wav) {
+ Wav=wav;
+ data = Wav.encode(16, [samples]);
+ var a = document.createElement('a');
+ a.innerText = "Listen to the last " + (100000 / 44100) + "seconds of audio as a WAV file";
+ var base64 = "";
+ var ui8a = new Uint8Array(data);
+ for (var i = 0; i < ui8a.length; i++) {
+ var byte = ui8a[i].toString(16);
+ base64 += "%" + (byte.length == 1 ? "0" : "") + byte;
+ }
+ a.href = "data:audio/wav," + base64;
+ document.body.appendChild(a);
+ });
+ }
+ };
+});
Oops, something went wrong.

0 comments on commit 2591ce7

Please sign in to comment.