Skip to content

Instantly share code, notes, and snippets.

@guybrush
Forked from shama/index.js
Last active December 20, 2015 14:19
Show Gist options
  • Save guybrush/6145857 to your computer and use it in GitHub Desktop.
Save guybrush/6145857 to your computer and use it in GitHub Desktop.
var Context = typeof webkitAudioContext == 'undefined'
? AudioContext
: webkitAudioContext
var master = new Context
var jsynth = require('jsynth')
var tune = require('tune')
var ff = [];
[
'C D E G',
'A B C E',
'C D E G',
'A B C E',
'A C F G',
'A B D G',
'Ab C Eb G',
'A Bb D F',
].forEach(function(chord) {
chord = chord.split(' ')
var oct = 2, arp = [];
for (var i = 0; i < (chord.length - 1) * 6; i++) {
if (i % 4 === 0) oct++
arp.push(chord[i % 4] + oct)
}
ff = ff.concat(arp.concat(arp.slice(0, -1).reverse()))
});
ff = tune(ff)
var synth = jsynth(master, function(t) {
return ff(t * 2)
})
synth.connect(master.destination)
require=function(e,t,n){function r(n,o){if(!t[n]){if(!e[n]){var s="function"==typeof require&&require;if(!o&&s)return s(n,!0);if(i)return i(n,!0);throw Error("Cannot find module '"+n+"'")}var u=t[n]={exports:{}};e[n][0].call(u.exports,function(t){var i=e[n][1][t];return r(i?i:t)},u,u.exports)}return t[n].exports}for(var i="function"==typeof require&&require,o=0;n.length>o;o++)r(n[o]);return r}({jsynth:[function(e,t){t.exports=e("uQroT4")},{}],uQroT4:[function(e,t){t.exports=function(e,t){"function"==typeof e&&(t=e,e=new webkitAudioContext);var n=e.createScriptProcessor(2048,1,1);return n.fn=t,n.i=n.t=0,window._SAMPLERATE=n.sampleRate=n.rate=e.sampleRate,n.duration=1/0,n.recording=!1,n.onaudioprocess=function(e){var t=e.outputBuffer.getChannelData(0),r=e.inputBuffer.getChannelData(0);n.tick(t,r)},n.tick=function(e,t){e=e||n._buffer,t=t||[];for(var r=0;e.length>r;r+=1)if(n.t=n.i/n.rate,n.i+=1,e[r]=n.fn(n.t,n.i,t[r]),n.i>=n.duration){n.stop();break}return e},n.stop=function(){n.disconnect(),n.playing=!1,n.recording},n.play=function(){n.playing||(n.connect(n.context.destination),n.playing=!0)},n.record=function(){},n.reset=function(){n.i=n.t=0},n.createSample=function(e){n.reset();var t=n.context.createBuffer(1,e,n.context.sampleRate),r=t.getChannelData(0);return n.tick(r),t},n}},{}]},{},[]),require=function(e,t,n){function r(n,o){if(!t[n]){if(!e[n]){var s="function"==typeof require&&require;if(!o&&s)return s(n,!0);if(i)return i(n,!0);throw Error("Cannot find module '"+n+"'")}var u=t[n]={exports:{}};e[n][0].call(u.exports,function(t){var i=e[n][1][t];return r(i?i:t)},u,u.exports)}return t[n].exports}for(var i="function"==typeof require&&require,o=0;n.length>o;o++)r(n[o]);return r}({tune:[function(e,t){t.exports=e("z8bpzl")},{}],z8bpzl:[function(e,t){"use strict";var n=e("util"),r=function(e){var t=this;return this._octave=4,e=this._parseNotes(e),function(n,r){r=t._options(r);var i=Math.floor(n*r.tempo%e.length);return r.volume*Math.sin(2*Math.PI*e[i]*n)}},i=t.exports=function(e){return new r(e)};i.Tune=r,r.prototype._hz=function(e){return 261.63*Math.pow(Math.pow(2,1/12),e||0)},r.prototype._parseNotes=function(e){var t=this;return"string"==typeof e&&(e=[e]),e.forEach(function(r,i){e[i]=n.isArray(r)?t._parseNotes(r):t._parseNote(r)}),e},r.prototype._parseNote=function(e){if("number"==typeof e)return e;if("."===e)return 0;var t,n="A A# B C C# D D# E F F# G G#".split(" "),r=(t=e.match(/^[a-z]/i))?t[0].toUpperCase():"C",i=(t=e.slice(1).match(/[0-9]/i))?t[0]:this._octave,o=(t=e.slice(1).match(/#|b/i))?t[0]:"";"b"===o&&(r=n[n.indexOf(r)-1],o=""),this._octave=Number(i)||this._octave;var s=n.indexOf(r+o)-n.indexOf("C")+(i-4)*n.length;return this._hz(s)},r.prototype._options=function(e){return e=e||{},e.tempo=e.tempo||4,e.volume=e.volume||1,e}},{util:1}],1:[function(e,t,n){function r(e){return e instanceof Array||Array.isArray(e)||e&&e!==Object.prototype&&r(e.__proto__)}function i(e){return e instanceof RegExp||"object"==typeof e&&"[object RegExp]"===Object.prototype.toString.call(e)}function o(e){if(e instanceof Date)return!0;if("object"!=typeof e)return!1;var t=Date.prototype&&u(Date.prototype),n=e.__proto__&&u(e.__proto__);return JSON.stringify(n)===JSON.stringify(t)}e("events"),n.isArray=r,n.isDate=function(e){return"[object Date]"===Object.prototype.toString.call(e)},n.isRegExp=function(e){return"[object RegExp]"===Object.prototype.toString.call(e)},n.print=function(){},n.puts=function(){},n.debug=function(){},n.inspect=function(e,t,a,c){function f(e,a){if(e&&"function"==typeof e.inspect&&e!==n&&(!e.constructor||e.constructor.prototype!==e))return e.inspect(a);switch(typeof e){case"undefined":return l("undefined","undefined");case"string":var c="'"+JSON.stringify(e).replace(/^"|"$/g,"").replace(/'/g,"\\'").replace(/\\"/g,'"')+"'";return l(c,"string");case"number":return l(""+e,"number");case"boolean":return l(""+e,"boolean")}if(null===e)return l("null","null");var h=s(e),v=t?u(e):h;if("function"==typeof e&&0===v.length){if(i(e))return l(""+e,"regexp");var _=e.name?": "+e.name:"";return l("[Function"+_+"]","special")}if(o(e)&&0===v.length)return l(e.toUTCString(),"date");var d,y,g;if(r(e)?(y="Array",g=["[","]"]):(y="Object",g=["{","}"]),"function"==typeof e){var m=e.name?": "+e.name:"";d=i(e)?" "+e:" [Function"+m+"]"}else d="";if(o(e)&&(d=" "+e.toUTCString()),0===v.length)return g[0]+d+g[1];if(0>a)return i(e)?l(""+e,"regexp"):l("[Object]","special");p.push(e);var b=v.map(function(t){var n,i;if(e.__lookupGetter__&&(e.__lookupGetter__(t)?i=e.__lookupSetter__(t)?l("[Getter/Setter]","special"):l("[Getter]","special"):e.__lookupSetter__(t)&&(i=l("[Setter]","special"))),0>h.indexOf(t)&&(n="["+t+"]"),i||(0>p.indexOf(e[t])?(i=null===a?f(e[t]):f(e[t],a-1),i.indexOf("\n")>-1&&(i=r(e)?i.split("\n").map(function(e){return" "+e}).join("\n").substr(2):"\n"+i.split("\n").map(function(e){return" "+e}).join("\n"))):i=l("[Circular]","special")),n===void 0){if("Array"===y&&t.match(/^\d+$/))return i;n=JSON.stringify(""+t),n.match(/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/)?(n=n.substr(1,n.length-2),n=l(n,"name")):(n=n.replace(/'/g,"\\'").replace(/\\"/g,'"').replace(/(^"|"$)/g,"'"),n=l(n,"string"))}return n+": "+i});p.pop();var w=0,x=b.reduce(function(e,t){return w++,t.indexOf("\n")>=0&&w++,e+t.length+1},0);return b=x>50?g[0]+(""===d?"":d+"\n ")+" "+b.join(",\n ")+" "+g[1]:g[0]+d+" "+b.join(", ")+" "+g[1]}var p=[],l=function(e,t){var n={bold:[1,22],italic:[3,23],underline:[4,24],inverse:[7,27],white:[37,39],grey:[90,39],black:[30,39],blue:[34,39],cyan:[36,39],green:[32,39],magenta:[35,39],red:[31,39],yellow:[33,39]},r={special:"cyan",number:"blue","boolean":"yellow",undefined:"grey","null":"bold",string:"green",date:"magenta",regexp:"red"}[t];return r?"["+n[r][0]+"m"+e+"["+n[r][1]+"m":e};return c||(l=function(e){return e}),f(e,a===void 0?2:a)},n.log=function(){},n.pump=null;var s=Object.keys||function(e){var t=[];for(var n in e)t.push(n);return t},u=Object.getOwnPropertyNames||function(e){var t=[];for(var n in e)Object.hasOwnProperty.call(e,n)&&t.push(n);return t},a=Object.create||function(e,t){var n;if(null===e)n={__proto__:null};else{if("object"!=typeof e)throw new TypeError("typeof prototype["+typeof e+"] != 'object'");var r=function(){};r.prototype=e,n=new r,n.__proto__=e}return t!==void 0&&Object.defineProperties&&Object.defineProperties(n,t),n};n.inherits=function(e,t){e.super_=t,e.prototype=a(t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}})};var c=/%[sdj%]/g;n.format=function(e){if("string"!=typeof e){for(var t=[],r=0;arguments.length>r;r++)t.push(n.inspect(arguments[r]));return t.join(" ")}for(var r=1,i=arguments,o=i.length,s=(e+"").replace(c,function(e){if("%%"===e)return"%";if(r>=o)return e;switch(e){case"%s":return i[r++]+"";case"%d":return Number(i[r++]);case"%j":return JSON.stringify(i[r++]);default:return e}}),u=i[r];o>r;u=i[++r])s+=null===u||"object"!=typeof u?" "+u:" "+n.inspect(u);return s}},{events:2}],3:[function(e,t){var n=t.exports={};n.nextTick=function(){var e="undefined"!=typeof window&&window.setImmediate,t="undefined"!=typeof window&&window.postMessage&&window.addEventListener;if(e)return function(e){return window.setImmediate(e)};if(t){var n=[];return window.addEventListener("message",function(e){if(e.source===window&&"process-tick"===e.data&&(e.stopPropagation(),n.length>0)){var t=n.shift();t()}},!0),function(e){n.push(e),window.postMessage("process-tick","*")}}return function(e){setTimeout(e,0)}}(),n.title="browser",n.browser=!0,n.env={},n.argv=[],n.binding=function(){throw Error("process.binding is not supported")},n.cwd=function(){return"/"},n.chdir=function(){throw Error("process.chdir is not supported")}},{}],2:[function(e,t,n){(function(e){function t(e,t){if(e.indexOf)return e.indexOf(t);for(var n=0;e.length>n;n++)if(t===e[n])return n;return-1}e.EventEmitter||(e.EventEmitter=function(){});var r=n.EventEmitter=e.EventEmitter,i="function"==typeof Array.isArray?Array.isArray:function(e){return"[object Array]"===Object.prototype.toString.call(e)},o=10;r.prototype.setMaxListeners=function(e){this._events||(this._events={}),this._events.maxListeners=e},r.prototype.emit=function(e){if("error"===e&&(!this._events||!this._events.error||i(this._events.error)&&!this._events.error.length))throw arguments[1]instanceof Error?arguments[1]:Error("Uncaught, unspecified 'error' event.");if(!this._events)return!1;var t=this._events[e];if(!t)return!1;if("function"==typeof t){switch(arguments.length){case 1:t.call(this);break;case 2:t.call(this,arguments[1]);break;case 3:t.call(this,arguments[1],arguments[2]);break;default:var n=Array.prototype.slice.call(arguments,1);t.apply(this,n)}return!0}if(i(t)){for(var n=Array.prototype.slice.call(arguments,1),r=t.slice(),o=0,s=r.length;s>o;o++)r[o].apply(this,n);return!0}return!1},r.prototype.addListener=function(e,t){if("function"!=typeof t)throw Error("addListener only takes instances of Function");if(this._events||(this._events={}),this.emit("newListener",e,t),this._events[e])if(i(this._events[e])){if(!this._events[e].warned){var n;n=void 0!==this._events.maxListeners?this._events.maxListeners:o,n&&n>0&&this._events[e].length>n&&(this._events[e].warned=!0,console.error("(node) warning: possible EventEmitter memory leak detected. %d listeners added. Use emitter.setMaxListeners() to increase limit.",this._events[e].length),console.trace())}this._events[e].push(t)}else this._events[e]=[this._events[e],t];else this._events[e]=t;return this},r.prototype.on=r.prototype.addListener,r.prototype.once=function(e,t){var n=this;return n.on(e,function r(){n.removeListener(e,r),t.apply(this,arguments)}),this},r.prototype.removeListener=function(e,n){if("function"!=typeof n)throw Error("removeListener only takes instances of Function");if(!this._events||!this._events[e])return this;var r=this._events[e];if(i(r)){var o=t(r,n);if(0>o)return this;r.splice(o,1),0==r.length&&delete this._events[e]}else this._events[e]===n&&delete this._events[e];return this},r.prototype.removeAllListeners=function(e){return 0===arguments.length?(this._events={},this):(e&&this._events&&this._events[e]&&(this._events[e]=null),this)},r.prototype.listeners=function(e){return this._events||(this._events={}),this._events[e]||(this._events[e]=[]),i(this._events[e])||(this._events[e]=[this._events[e]]),this._events[e]}})(e("__browserify_process"))},{__browserify_process:3}]},{},[]);var master=new(webkitAudioContext||AudioContext),jsynth=require("jsynth"),tune=require("tune"),ff=[];["C D E G","A B C E","C D E G","A B C E","A C F G","A B D G","Ab C Eb G","A Bb D F"].forEach(function(e){e=e.split(" ");for(var t=2,n=[],r=0;6*(e.length-1)>r;r++)0===r%4&&t++,n.push(e[r%4]+t);ff=ff.concat(n.concat(n.slice(0,-1).reverse()))}),ff=tune(ff);var synth=jsynth(master,function(e){return ff(2*e)});synth.connect(master.destination);
<style type='text/css'>html, body { margin: 0; padding: 0; border: 0; }
body, html { height: 100%; width: 100%; }</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment