From e826b92a672d30891e69c4eafdc4e0364e9245bf Mon Sep 17 00:00:00 2001 From: Juan Gallardo Date: Wed, 28 May 2014 10:51:00 -0700 Subject: [PATCH] Adding audiosynth Dynamic waveform audio synthesizer, written in Javascript. Author is Keith Horwood. The source code is currently hosted at https://github.com/keithwhor/audiosynth , which is where I got this from. There was no version number. So I contacted the creator of the library on Twitter. He said that we could version it at 1.0.0 https://twitter.com/keithwhor/status/471706382379798528 --- ajax/libs/audiosynth/1.0.0/audiosynth.js | 350 +++++++++++++++++++++++ ajax/libs/audiosynth/LICENSE | 20 ++ ajax/libs/audiosynth/package.json | 19 ++ 3 files changed, 389 insertions(+) create mode 100644 ajax/libs/audiosynth/1.0.0/audiosynth.js create mode 100644 ajax/libs/audiosynth/LICENSE create mode 100644 ajax/libs/audiosynth/package.json diff --git a/ajax/libs/audiosynth/1.0.0/audiosynth.js b/ajax/libs/audiosynth/1.0.0/audiosynth.js new file mode 100644 index 000000000..104c49026 --- /dev/null +++ b/ajax/libs/audiosynth/1.0.0/audiosynth.js @@ -0,0 +1,350 @@ +var Synth, AudioSynth, AudioSynthInstrument; +!function(){ + + var URL = window.URL || window.webkitURL; + var Blob = window.Blob; + + if(!URL || !Blob) { + throw new Error('This browser does not support AudioSynth'); + } + + var _encapsulated = false; + var AudioSynthInstance = null; + var pack = function(c,arg){ return [new Uint8Array([arg, arg >> 8]), new Uint8Array([arg, arg >> 8, arg >> 16, arg >> 24])][c]; }; + var setPrivateVar = function(n,v,w,e){Object.defineProperty(this,n,{value:v,writable:!!w,enumerable:!!e});}; + var setPublicVar = function(n,v,w){setPrivateVar.call(this,n,v,w,true);}; + AudioSynthInstrument = function AudioSynthInstrument(){this.__init__.apply(this,arguments);}; + var setPriv = setPrivateVar.bind(AudioSynthInstrument.prototype); + var setPub = setPublicVar.bind(AudioSynthInstrument.prototype); + setPriv('__init__', function(a,b,c) { + if(!_encapsulated) { throw new Error('AudioSynthInstrument can only be instantiated from the createInstrument method of the AudioSynth object.'); } + setPrivateVar.call(this, '_parent', a); + setPublicVar.call(this, 'name', b); + setPrivateVar.call(this, '_soundID', c); + }); + setPub('play', function(note, octave, duration) { + return this._parent.play(this._soundID, note, octave, duration); + }); + setPub('generate', function(note, octave, duration) { + return this._parent.generate(this._soundID, note, octave, duration); + }); + AudioSynth = function AudioSynth(){if(AudioSynthInstance instanceof AudioSynth){return AudioSynthInstance;}else{ this.__init__(); return this; }}; + setPriv = setPrivateVar.bind(AudioSynth.prototype); + setPub = setPublicVar.bind(AudioSynth.prototype); + setPriv('_debug',false,true); + setPriv('_bitsPerSample',16); + setPriv('_channels',1); + setPriv('_sampleRate',44100,true); + setPub('setSampleRate', function(v) { + this._sampleRate = Math.max(Math.min(v|0,44100), 4000); + this._clearCache(); + return this._sampleRate; + }); + setPub('getSampleRate', function() { return this._sampleRate; }); + setPriv('_volume',32768,true); + setPub('setVolume', function(v) { + v = parseFloat(v); if(isNaN(v)) { v = 0; } + v = Math.round(v*32768); + this._volume = Math.max(Math.min(v|0,32768), 0); + this._clearCache(); + return this._volume; + }); + setPub('getVolume', function() { return Math.round(this._volume/32768*10000)/10000; }); + setPriv('_notes',{'C':261.63,'C#':277.18,'D':293.66,'D#':311.13,'E':329.63,'F':346.23,'F#':369.99,'G':392.00,'G#':415.30,'A':440.00,'A#':466.16,'B':493.88}); + setPriv('_fileCache',[],true); + setPriv('_temp',{},true); + setPriv('_sounds',[],true); + setPriv('_mod',[function(i,s,f,x){return Math.sin((2 * Math.PI)*(i/s)*f+x);}]); + setPriv('_resizeCache', function() { + var f = this._fileCache; + var l = this._sounds.length; + while(f.length> 8; + + } + + for (; i !== decayLen; i++) { + + val = volume * Math.pow((1-((i-(sampleRate*attack))/(sampleRate*(time-attack)))),dampen) * waveFunc.call(waveBind, i, sampleRate, frequency, volume); + + data[i << 1] = val; + data[(i << 1) + 1] = val >> 8; + + } + + var out = [ + 'RIFF', + pack(1, 4 + (8 + 24/* chunk 1 length */) + (8 + 8/* chunk 2 length */)), // Length + 'WAVE', + // chunk 1 + 'fmt ', // Sub-chunk identifier + pack(1, 16), // Chunk length + pack(0, 1), // Audio format (1 is linear quantization) + pack(0, channels), + pack(1, sampleRate), + pack(1, sampleRate * channels * bitsPerSample / 8), // Byte rate + pack(0, channels * bitsPerSample / 8), + pack(0, bitsPerSample), + // chunk 2 + 'data', // Sub-chunk identifier + pack(1, data.length * channels * bitsPerSample / 8), // Chunk length + data + ]; + var blob = new Blob(out, {type: 'audio/wav'}); + var dataURI = URL.createObjectURL(blob); + this._fileCache[sound][octave-1][note][time] = dataURI; + if(this._debug) { console.log((new Date).valueOf() - t, 'ms to generate'); } + return dataURI; + } + }); + setPub('play', function(sound, note, octave, duration) { + var src = this.generate(sound, note, octave, duration); + var audio = new Audio(src); + audio.play(); + return true; + }); + setPub('debug', function() { this._debug = true; }); + setPub('createInstrument', function(sound) { + var n = 0; + var found = false; + if(typeof(sound)=='string') { + for(var i=0;i=(valueTable.length-1)?0:playVal+1] + valueTable[playVal]) * 0.5; + + if(playVal>=Math.floor(period)) { + if(playVal=p_hundredth) { + // Reset + resetPlay = true; + valueTable[playVal+1] = (valueTable[0] + valueTable[playVal+1]) * 0.5; + vars.periodCount++; + } + } else { + resetPlay = true; + } + } + + var _return = valueTable[playVal]; + if(resetPlay) { vars.playVal = 0; } else { vars.playVal++; } + + return _return; + + } + } +}, +{ + name: 'edm', + attack: function() { return 0.002; }, + dampen: function() { return 1; }, + wave: function(i, sampleRate, frequency) { + var base = this.modulate[0]; + var mod = this.modulate.slice(1); + return mod[0]( + i, + sampleRate, + frequency, + mod[9]( + i, + sampleRate, + frequency, + mod[2]( + i, + sampleRate, + frequency, + Math.pow(base(i, sampleRate, frequency, 0), 3) + + Math.pow(base(i, sampleRate, frequency, 0.5), 5) + + Math.pow(base(i, sampleRate, frequency, 1), 7) + ) + ) + + mod[8]( + i, + sampleRate, + frequency, + base(i, sampleRate, frequency, 1.75) + ) + ); + } +}); \ No newline at end of file diff --git a/ajax/libs/audiosynth/LICENSE b/ajax/libs/audiosynth/LICENSE new file mode 100644 index 000000000..3f58024f0 --- /dev/null +++ b/ajax/libs/audiosynth/LICENSE @@ -0,0 +1,20 @@ +The MIT License (MIT) + +Copyright (c) 2013 Keith William Horwood + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/ajax/libs/audiosynth/package.json b/ajax/libs/audiosynth/package.json new file mode 100644 index 000000000..23e3ed8f2 --- /dev/null +++ b/ajax/libs/audiosynth/package.json @@ -0,0 +1,19 @@ +{ + "name": "audiosynth", + "filename": "audiosynth.js", + "version": "1.0.0", + "description": "Dynamic waveform audio synthesizer, written in Javascript. Generate musical notes dynamically and play them in your browser using the HTML5 Audio Element.", + "repository": { + "type": "git", + "url": "https://github.com/keithwhor/audiosynth" + }, + "keywords": [ + "audiosynth", + "audio", + "synthesizer", + "waveform" + ], + "author": "Keith Horwood", + "homepage": "http://keithwhor.github.io/audiosynth/", + "license": "MIT" +} \ No newline at end of file