Skip to content

Instantly share code, notes, and snippets.

@max-mapper
Created July 30, 2013 04:41
Show Gist options
  • Save max-mapper/6110294 to your computer and use it in GitHub Desktop.
Save max-mapper/6110294 to your computer and use it in GitHub Desktop.
requirebin sketch
var master = new (webkitAudioContext || AudioContext)
var jsynth = require('jsynth')
var tune = require('tune')
var teoria = require('teoria')
var tones = []
var notes = ['first', 'second', 'third', 'fourth', 'fifth']
notes.map(function(i) {
var scale = teoria.note('a').scale('lydian')
var scaleNote = scale.get(i)
var newNote = teoria.note(scaleNote.toString())
var newScale = newNote.scale('lydian').simple()
newScale.map(function(n){ tones.push(n.toUpperCase()) })
})
console.log(tones)
var tuned = tune(tones, {tempo: 8});
var synth = jsynth(master, function(t) {
return tuned(t * 2)
})
synth.connect(master.destination)
require=function(t,e,n){function r(n,o){if(!e[n]){if(!t[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 a=e[n]={exports:{}};t[n][0].call(a.exports,function(e){var i=t[n][1][e];return r(i?i:e)},a,a.exports)}return e[n].exports}for(var i="function"==typeof require&&require,o=0;n.length>o;o++)r(n[o]);return r}({jsynth:[function(t,e){e.exports=t("ylhY/J")},{}],"ylhY/J":[function(t,e){e.exports=function(t,e){"function"==typeof t&&(e=t,t=new webkitAudioContext);var n=t.createScriptProcessor(2048,1,1);return n.fn=e,n.i=n.t=0,window._SAMPLERATE=n.sampleRate=n.rate=t.sampleRate,n.duration=1/0,n.recording=!1,n.onaudioprocess=function(t){var e=t.outputBuffer.getChannelData(0),r=t.inputBuffer.getChannelData(0);n.tick(e,r)},n.tick=function(t,e){t=t||n._buffer,e=e||[];for(var r=0;t.length>r;r+=1)if(n.t=n.i/n.rate,n.i+=1,t[r]=n.fn(n.t,n.i,e[r]),n.i>=n.duration){n.stop();break}return t},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(t){n.reset();var e=n.context.createBuffer(1,t,n.context.sampleRate),r=e.getChannelData(0);return n.tick(r),e},n}},{}]},{},[]),require=function(t,e,n){function r(n,o){if(!e[n]){if(!t[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 a=e[n]={exports:{}};t[n][0].call(a.exports,function(e){var i=t[n][1][e];return r(i?i:e)},a,a.exports)}return e[n].exports}for(var i="function"==typeof require&&require,o=0;n.length>o;o++)r(n[o]);return r}({tune:[function(t,e){e.exports=t("CdHJ/H")},{}],"CdHJ/H":[function(t,e){"use strict";var n=t("util"),r=function(t){var e=this;return this._octave=4,t=this._parseNotes(t),function(n,r){r=e._options(r);var i=Math.floor(n*r.tempo%t.length);return r.volume*Math.sin(2*Math.PI*t[i]*n)}},i=e.exports=function(t){return new r(t)};i.Tune=r,r.prototype._hz=function(t){return 261.63*Math.pow(Math.pow(2,1/12),t||0)},r.prototype._parseNotes=function(t){var e=this;return"string"==typeof t&&(t=[t]),t.forEach(function(r,i){t[i]=n.isArray(r)?e._parseNotes(r):e._parseNote(r)}),t},r.prototype._parseNote=function(t){if("number"==typeof t)return t;if("."===t)return 0;var e,n="A A# B C C# D D# E F F# G G#".split(" "),r=(e=t.match(/^[a-z]/i))?e[0].toUpperCase():"C",i=(e=t.slice(1).match(/[0-9]/i))?e[0]:this._octave,o=(e=t.slice(1).match(/#|b/i))?e[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(t){return t=t||{},t.tempo=t.tempo||4,t.volume=t.volume||1,t}},{util:1}],1:[function(t,e,n){function r(t){return t instanceof Array||Array.isArray(t)||t&&t!==Object.prototype&&r(t.__proto__)}function i(t){return t instanceof RegExp||"object"==typeof t&&"[object RegExp]"===Object.prototype.toString.call(t)}function o(t){if(t instanceof Date)return!0;if("object"!=typeof t)return!1;var e=Date.prototype&&a(Date.prototype),n=t.__proto__&&a(t.__proto__);return JSON.stringify(n)===JSON.stringify(e)}t("events"),n.isArray=r,n.isDate=function(t){return"[object Date]"===Object.prototype.toString.call(t)},n.isRegExp=function(t){return"[object RegExp]"===Object.prototype.toString.call(t)},n.print=function(){},n.puts=function(){},n.debug=function(){},n.inspect=function(t,e,u,c){function f(t,u){if(t&&"function"==typeof t.inspect&&t!==n&&(!t.constructor||t.constructor.prototype!==t))return t.inspect(u);switch(typeof t){case"undefined":return h("undefined","undefined");case"string":var c="'"+JSON.stringify(t).replace(/^"|"$/g,"").replace(/'/g,"\\'").replace(/\\"/g,'"')+"'";return h(c,"string");case"number":return h(""+t,"number");case"boolean":return h(""+t,"boolean")}if(null===t)return h("null","null");var d=s(t),p=e?a(t):d;if("function"==typeof t&&0===p.length){if(i(t))return h(""+t,"regexp");var v=t.name?": "+t.name:"";return h("[Function"+v+"]","special")}if(o(t)&&0===p.length)return h(t.toUTCString(),"date");var m,g,y;if(r(t)?(g="Array",y=["[","]"]):(g="Object",y=["{","}"]),"function"==typeof t){var b=t.name?": "+t.name:"";m=i(t)?" "+t:" [Function"+b+"]"}else m="";if(o(t)&&(m=" "+t.toUTCString()),0===p.length)return y[0]+m+y[1];if(0>u)return i(t)?h(""+t,"regexp"):h("[Object]","special");l.push(t);var w=p.map(function(e){var n,i;if(t.__lookupGetter__&&(t.__lookupGetter__(e)?i=t.__lookupSetter__(e)?h("[Getter/Setter]","special"):h("[Getter]","special"):t.__lookupSetter__(e)&&(i=h("[Setter]","special"))),0>d.indexOf(e)&&(n="["+e+"]"),i||(0>l.indexOf(t[e])?(i=null===u?f(t[e]):f(t[e],u-1),i.indexOf("\n")>-1&&(i=r(t)?i.split("\n").map(function(t){return" "+t}).join("\n").substr(2):"\n"+i.split("\n").map(function(t){return" "+t}).join("\n"))):i=h("[Circular]","special")),n===void 0){if("Array"===g&&e.match(/^\d+$/))return i;n=JSON.stringify(""+e),n.match(/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/)?(n=n.substr(1,n.length-2),n=h(n,"name")):(n=n.replace(/'/g,"\\'").replace(/\\"/g,'"').replace(/(^"|"$)/g,"'"),n=h(n,"string"))}return n+": "+i});l.pop();var _=0,M=w.reduce(function(t,e){return _++,e.indexOf("\n")>=0&&_++,t+e.length+1},0);return w=M>50?y[0]+(""===m?"":m+"\n ")+" "+w.join(",\n ")+" "+y[1]:y[0]+m+" "+w.join(", ")+" "+y[1]}var l=[],h=function(t,e){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"}[e];return r?"["+n[r][0]+"m"+t+"["+n[r][1]+"m":t};return c||(h=function(t){return t}),f(t,u===void 0?2:u)},n.log=function(){},n.pump=null;var s=Object.keys||function(t){var e=[];for(var n in t)e.push(n);return e},a=Object.getOwnPropertyNames||function(t){var e=[];for(var n in t)Object.hasOwnProperty.call(t,n)&&e.push(n);return e},u=Object.create||function(t,e){var n;if(null===t)n={__proto__:null};else{if("object"!=typeof t)throw new TypeError("typeof prototype["+typeof t+"] != 'object'");var r=function(){};r.prototype=t,n=new r,n.__proto__=t}return e!==void 0&&Object.defineProperties&&Object.defineProperties(n,e),n};n.inherits=function(t,e){t.super_=e,t.prototype=u(e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}})};var c=/%[sdj%]/g;n.format=function(t){if("string"!=typeof t){for(var e=[],r=0;arguments.length>r;r++)e.push(n.inspect(arguments[r]));return e.join(" ")}for(var r=1,i=arguments,o=i.length,s=(t+"").replace(c,function(t){if("%%"===t)return"%";if(r>=o)return t;switch(t){case"%s":return i[r++]+"";case"%d":return Number(i[r++]);case"%j":return JSON.stringify(i[r++]);default:return t}}),a=i[r];o>r;a=i[++r])s+=null===a||"object"!=typeof a?" "+a:" "+n.inspect(a);return s}},{events:2}],3:[function(t,e){var n=e.exports={};n.nextTick=function(){var t="undefined"!=typeof window&&window.setImmediate,e="undefined"!=typeof window&&window.postMessage&&window.addEventListener;if(t)return function(t){return window.setImmediate(t)};if(e){var n=[];return window.addEventListener("message",function(t){if(t.source===window&&"process-tick"===t.data&&(t.stopPropagation(),n.length>0)){var e=n.shift();e()}},!0),function(t){n.push(t),window.postMessage("process-tick","*")}}return function(t){setTimeout(t,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(t,e,n){(function(t){function e(t,e){if(t.indexOf)return t.indexOf(e);for(var n=0;t.length>n;n++)if(e===t[n])return n;return-1}t.EventEmitter||(t.EventEmitter=function(){});var r=n.EventEmitter=t.EventEmitter,i="function"==typeof Array.isArray?Array.isArray:function(t){return"[object Array]"===Object.prototype.toString.call(t)},o=10;r.prototype.setMaxListeners=function(t){this._events||(this._events={}),this._events.maxListeners=t},r.prototype.emit=function(t){if("error"===t&&(!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 e=this._events[t];if(!e)return!1;if("function"==typeof e){switch(arguments.length){case 1:e.call(this);break;case 2:e.call(this,arguments[1]);break;case 3:e.call(this,arguments[1],arguments[2]);break;default:var n=Array.prototype.slice.call(arguments,1);e.apply(this,n)}return!0}if(i(e)){for(var n=Array.prototype.slice.call(arguments,1),r=e.slice(),o=0,s=r.length;s>o;o++)r[o].apply(this,n);return!0}return!1},r.prototype.addListener=function(t,e){if("function"!=typeof e)throw Error("addListener only takes instances of Function");if(this._events||(this._events={}),this.emit("newListener",t,e),this._events[t])if(i(this._events[t])){if(!this._events[t].warned){var n;n=void 0!==this._events.maxListeners?this._events.maxListeners:o,n&&n>0&&this._events[t].length>n&&(this._events[t].warned=!0,console.error("(node) warning: possible EventEmitter memory leak detected. %d listeners added. Use emitter.setMaxListeners() to increase limit.",this._events[t].length),console.trace())}this._events[t].push(e)}else this._events[t]=[this._events[t],e];else this._events[t]=e;return this},r.prototype.on=r.prototype.addListener,r.prototype.once=function(t,e){var n=this;return n.on(t,function r(){n.removeListener(t,r),e.apply(this,arguments)}),this},r.prototype.removeListener=function(t,n){if("function"!=typeof n)throw Error("removeListener only takes instances of Function");if(!this._events||!this._events[t])return this;var r=this._events[t];if(i(r)){var o=e(r,n);if(0>o)return this;r.splice(o,1),0==r.length&&delete this._events[t]}else this._events[t]===n&&delete this._events[t];return this},r.prototype.removeAllListeners=function(t){return 0===arguments.length?(this._events={},this):(t&&this._events&&this._events[t]&&(this._events[t]=null),this)},r.prototype.listeners=function(t){return this._events||(this._events={}),this._events[t]||(this._events[t]=[]),i(this._events[t])||(this._events[t]=[this._events[t]]),this._events[t]}})(t("__browserify_process"))},{__browserify_process:3}]},{},[]),require=function(t,e,n){function r(n,o){if(!e[n]){if(!t[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 a=e[n]={exports:{}};t[n][0].call(a.exports,function(e){var i=t[n][1][e];return r(i?i:e)},a,a.exports)}return e[n].exports}for(var i="function"==typeof require&&require,o=0;n.length>o;o++)r(n[o]);return r}({teoria:[function(t,e){e.exports=t("cE2zlW")},{}],cE2zlW:[function(t,e,n){(function(){"use strict";function t(t,e){return[t[0]+e[0],t[1]+e[1]]}function r(t,e){return[t[0]-e[0],t[1]-e[1]]}function i(t,e){return"number"==typeof e?[t[0]*e,t[1]*e]:[t[0]*e[0],t[1]*e[1]]}function o(t){return t[0]+t[1]}function s(t,e,n){for(;n>0;n--)t+=e;return t}function a(t,e){e=e||{},this.duration={value:e.value||4,dots:e.dots||0},this.coord=t}function u(t){this.coord=t}function c(t,e){function n(t){for(var e=0,n=t.length;n>e;e++)h[e+1]=t[e];d=t.length}e=e||"",this.name=t.name().toUpperCase()+t.accidental()+e,this.symbol=e,this.root=t,this.intervals=[],this._voicing=[];var r,i,o,s,a,u,c="quality",f=[],h=["P1","M3","P5","m7","M9","P11","M13"],d=2;for(e=e.replace(/[,\s\(\)]/g,""),a=e.split("/"),2===a.length?(e=a[0],a=a[1]):a=null,r=0,i=e.length;i>r&&(o=e[r]);r++){switch(c){case"quality":s=i>=r+3?e.substr(r,3):null,u=s in x?s:o in x?o:"",n(x[u]),r+=u.length-1,c="extension";break;case"extension":if(o="1"===o&&e[r+1]?parseFloat(e.substr(r,2)):parseFloat(o),isNaN(o)||6===o)6===o?(h[3]="M6",d=3>d?3:d):r-=1;else{if(d=(o-1)/2,d!==Math.round(d))throw Error("Invalid interval extension: "+o.toString(10));("o"===u||"dim"===u)&&(h[3]="d7"),r+=(o+"").length-1}c="alterations";break;case"alterations":var p,v=e.substr(r).split(/(#|b|add|maj|sus|M)/),m=!1,g=!1;if(1===v.length)throw Error("Invalid alterations");if(0!==v[0].length)throw Error("Invalid token: '"+v[0]+"'");for(var y=1,b=v.length;b>y;y++)switch(p=v[y+1],v[y]){case"M":case"maj":d=3>d?3:d,"7"===p&&y++,h[3]="M7";break;case"sus":var w="P4";("2"===p||"4"===p)&&(y++,"2"===p&&(w="M2")),h[1]=w;break;case"add":p&&!isNaN(+p)&&("9"===p?f.push("M9"):"11"===p?f.push("P11"):"13"===p&&f.push("M13"),y+=p.length);break;case"b":m=!0;break;case"#":g=!0;break;default:if(0===v[y].length)break;var _,M,A=+v[y];if(isNaN(A)||(A+"").length!==v[y].length)throw Error("Invalid token: '"+v[y]+"'");if(6===A){h[3]=g?"A6":m?"m6":"M6",d=3>d?3:d;continue}if(M=(A-1)/2,M>d&&(d=M),5>A||7===A||M!==Math.round(M))throw Error("Invalid interval alteration: "+A);_=h[M][0],g?"d"===_?_="m":"m"===_?_="M":("M"===_||"P"===_)&&(_="A"):m&&("A"===_?_="M":"M"===_?_="m":("m"===_||"P"===_)&&(_="d")),g=m=!1,h[M]=_+A}c="ended"}if("ended"===c)break}for(a&&"9"===a&&(f.push("M9"),a=null),this.intervals=h.slice(0,d+1).concat(f).map(function(t){return l.interval(t)}),r=0,i=this.intervals.length;i>r;r++)this._voicing[r]=this.intervals[r];if(a){var P,E,j=this.intervals;for(E=l.note(a+(t.octave()+1)),P=l.interval.between(t,E),a=P.simple(),P=P.invert(),P.direction("down"),this._voicing=[P],r=0;i>r;r++)j[r].simple()!==a&&this._voicing.push(j[r])}}function f(t,e){var n,r;if(!(t instanceof a))throw Error("Invalid Tonic");if("string"==typeof e){if(n=e,e=l.scale.scales[e],!e)throw Error("Invalid Scale")}else for(r in l.scale.scales)if(l.scale.scales.hasOwnProperty(r)&&""+l.scale.scales[r]==""+e){n=r;break}this.name=n,this.tonic=t,this.scale=e}var l={},h={c:[0,0],d:[-1,2],e:[-2,4],f:[1,-1],g:[0,1],a:[-1,3],b:[-2,5],h:[-2,5]},d={unison:[0,0],second:[3,-5],third:[2,-3],fourth:[1,-1],fifth:[0,1],sixth:[3,-4],seventh:[2,-2],octave:[1,0]},p=["second","sixth","third","seventh","fourth","unison","fifth"],v=["unison","second","third","fourth","fifth","sixth","seventh","octave","ninth","tenth","eleventh","twelfth","thirteenth","fourteenth","fifteenth"],m=["f","c","g","d","a","e","b"],g=["bb","b","","#","x"],y=[-4,7],b=t(h.a,[4,0]),w={.25:"longa",.5:"breve",1:"whole",2:"half",4:"quarter",8:"eighth",16:"sixteenth",32:"thirty-second",64:"sixty-fourth",128:"hundred-twenty-eighth"},_={P:"perfect",M:"major",m:"minor",A:"augmented",AA:"doubly augmented",d:"diminished",dd:"doubly diminished"},M={perfect:["dd","d","P","A","AA"],minor:["dd","d","m","M","A","AA"]},x={min:["m3","P5"],m:["m3","P5"],"-":["m3","P5"],M:["M3","P5"],"":["M3","P5"],"+":["M3","A5"],aug:["M3","A5"],dim:["m3","d5"],o:["m3","d5"],maj:["M3","P5","M7"],dom:["M3","P5","m7"],"ø":["m3","d5","m7"],5:["P5"]},A={major:"M",minor:"m",augmented:"aug",diminished:"dim","half-diminished":"7b5",power:"5",dominant:"7"},P={unison:1,first:1,second:2,third:3,fourth:4,fifth:5,sixth:6,seventh:7,octave:8,ninth:9,eleventh:11,thirteenth:13},E={dd1:"daw",d1:"de",P1:"do",A1:"di",AA1:"dai",d2:"raw",m2:"ra",M2:"re",A2:"ri",AA2:"rai",d3:"maw",m3:"me",M3:"mi",A3:"mai",dd4:"faw",d4:"fe",P4:"fa",A4:"fi",AA4:"fai",dd5:"saw",d5:"se",P5:"so",A5:"si",AA5:"sai",d6:"law",m6:"le",M6:"la",A6:"li",AA6:"lai",d7:"taw",m7:"te",M7:"ti",A7:"tai",dd8:"daw",d8:"de",P8:"do",A8:"di",AA8:"dai"};l.note=function(t,e){return"string"==typeof t?l.note.fromString(t,e):new a(t,e)},l.note.fromKey=function(e){var n=Math.floor((e-4)/12),s=e-12*n-4,a=m[(2*Math.round(s/2)+1)%7],u=t(r(h[a],b),[n+1,0]),c=e-49-o(i(u,[12,7]));return l.note(c?t(u,i(y,c)):u)},l.note.fromFrequency=function(t,e){var n,r,i;return e=e||440,n=49+12*((Math.log(t)-Math.log(e))/Math.log(2)),n=Math.round(n),i=e*Math.pow(2,(n-49)/12),r=1200*(Math.log(t/i)/Math.log(2)),{note:l.note.fromKey(n),cents:r}},l.note.fromMIDI=function(t){return l.note.fromKey(t-20)},l.note.fromString=function(e,n){var o,s,u,c,f,l,d=/^([a-h])(x|#|bb|b?)(-?\d*)/i,p=/^([a-h])(x|#|bb|b?)([,\']*)$/i;if(o=e.match(d),o&&e===o[0]&&o[3].length)s=o[1],u=+o[3];else{if(e=e.replace(/\u2032/g,"'").replace(/\u0375/g,","),o=e.match(p),!o||e!==o[0])throw Error("Invalid note format");if(s=o[1],u=o[3],l=s===s.toLowerCase(),u.length)if(u.match(/^'+$/)&&l)u=3+u.length;else{if(!u.match(/^,+$/)||l)throw Error("Format must respect the Helmholtz format");u=2-u.length}else u=l?3:2}return c=o[2].length?o[2].toLowerCase():"",s=s.toLowerCase(),f=[h[s][0],h[s][1]],f=t(f,[u,0]),f=t(f,i(y,g.indexOf(c)-2)),new a(r(f,b),n)},l.chord=function(t,e){if("string"==typeof t){var n,r;if(n=t.match(/^([a-h])(x|#|bb|b?)/i),n&&n[0])return r="number"==typeof e?e.toString(10):"4",new c(l.note(n[0].toLowerCase()+r),t.substr(n[0].length))}else if(t instanceof a)return new c(t,e);throw Error("Invalid Chord. Couldn't find note name")},l.interval=function(t,e){if("string"==typeof t)return l.interval.toCoord(t);if("string"==typeof e&&t instanceof a)return l.interval.from(t,l.interval.toCoord(e));if(e instanceof a&&t instanceof a)return l.interval.between(t,e);throw Error("Invalid parameters")},l.interval.toCoord=function(e){var n,r,o,s,a,c,f,l,h,p,m=/^(AA|A|P|M|m|d|dd)(-?\d+)$/;if(n=e.match(m),!n)throw Error("Invalid simple format interval");if(s=n[1],r=+n[2],p=0>r,r=p?-r:r,a=r>8?r%7?r%7:7:r,c=(r-a)/7,f=d[v[a-1]],o=t(f,[c,0]),l=1>=f[0]?"perfect":"minor","perfect"===l&&("M"===s||"m"===s)||"minor"===l&&"P"===s)throw Error("Invalid interval quality");return h=M[l].indexOf(s)-2,o=t(o,i(y,h)),o=p?i(o,-1):o,new u(o)},l.interval.from=function(e,n){return new a(t(e.coord,n.coord))},l.interval.between=function(t,e){return new u(r(e.coord,t.coord))},l.interval.invert=function(t){return""+l.interval(t).invert()},l.scale=function(t,e){return t instanceof a||(t=l.note(t)),new f(t,e)},l.scale.scales={},a.prototype={octave:function(){return this.coord[0]+b[0]-h[this.name()][0]+4*this.accidentalValue()},name:function(){return m[this.coord[1]+b[1]-7*this.accidentalValue()+1]},accidentalValue:function(){return Math.round((this.coord[1]+b[1]-2)/7)},accidental:function(){return g[this.accidentalValue()+2]},key:function(t){return t?7*this.coord[0]+4*this.coord[1]+29:12*this.coord[0]+7*this.coord[1]+49},fq:function(t){return t=t||440,t*Math.pow(2,(12*this.coord[0]+7*this.coord[1])/12)},chroma:function(){var t=(o(i(this.coord,[12,7]))-3)%12;return 0>t?t+12:t},scale:function(t){return l.scale(this,t)},interval:function(t){return l.interval(this,t)},transpose:function(t){var e=l.interval(this,t);return this.coord=e.coord,this},chord:function(t){return t=t in A?A[t]:t,new c(this,t)},helmholtz:function(){var t=this.octave(),e=this.name();e=3>t?e.toUpperCase():e.toLowerCase();var n=3>t?",":"'",r=2>t?2-t:t-3;return s(e+this.accidental(),n,r)},scientific:function(){return this.name().toUpperCase()+this.accidental()+this.octave()},enharmonics:function(e){var n=this.key(),r=e?2:3;return["m3","m2","m-2","m-3"].map(this.interval.bind(this)).filter(function(e){var o=e.accidentalValue(),s=n-(e.key()-o);return r>s&&s>-r?(e.coord=t(e.coord,i(y,s-o)),!0):void 0})},solfege:function(t,e){if(!(t instanceof f))throw Error("Invalid Scale");var n,r,i,o=t.tonic.interval(this);return"down"===o.direction()&&(o=o.invert()),e&&(i=(this.key(!0)-t.tonic.key(!0))/7,i=i>=0?Math.floor(i):-Math.ceil(-i),r=i>=0?"'":","),n=E[o.simple(!0)],e?s(n,r,Math.abs(i)):n},durationName:function(){return w[this.duration.value]},durationInSeconds:function(t,e){var n=60/t/(this.duration.value/4)/(e/4);return 2*n-n/Math.pow(2,this.duration.dots)},scaleDegree:function(t){var e=t.tonic.interval(this),n=e.number();return e="down"===e.direction()||n>7&&0===(n-1)%7?e.invert():e,t.scale.indexOf(e.simple(!0))+1},toString:function(t){return this.name()+this.accidental()+(t?"":this.octave())}},u.prototype={name:function(){return v[this.number()-1]},semitones:function(){return o(i(this.coord,[12,7]))},number:function(){return Math.abs(this.value())},value:function(){var t,e,n=r(this.coord,i(y,Math.floor((this.coord[1]-2)/7)+1));return t=p[n[1]+5],e=P[t]+7*(n[0]-d[t][0]),e>0?e:e-2},type:function(){return 1>=d[this.base()][0]?"perfect":"minor"},base:function(){var t,e=r(this.coord,i(y,this.qualityValue()))[1];return e=this.value()>0?e+5:-(e-5)%7,e=0>e?p.length+e:e,t=p[e],"unison"===t&&this.number()>=8&&(t="octave"),t},direction:function(t){if(t){var e=this.value()>=1?"up":"down";return e!==t&&(this.coord=i(this.coord,-1)),this}return this.value()>=1?"up":"down"},simple:function(t){var e=this.value();return e=e>8||-8>e?e%7?e%7:7:e,this.quality()+(t?Math.abs(e):e)},compound:function(t){var e=t?this.number():this.value();return this.quality()+e},isCompound:function(){return this.number()>8},octaves:function(){var t=r(this.coord,i(y,this.qualityValue())),e=t[0]-d[this.base()][0];return e},invert:function(){var e=this.base(),n=this.qualityValue(),r="minor"===this.type()?-(n-1):-n,o=d[v[9-P[e]-1]];return o=t(o,i(y,r)),new u(o)},quality:function(t){var e=M[this.type()][this.qualityValue()+2];return t?_[e]:e},qualityValue:function(){return"down"===this.direction()?Math.floor((-this.coord[1]-2)/7)+1:Math.floor((this.coord[1]-2)/7)+1},equal:function(t){return 0===o(r(this.coord,t.coord))},greater:function(t){var e=this.semitones(),n=t.semitones();return e===n?this.number()>t.number():e>n},smaller:function(t){return!this.equal(t)&&!this.greater(t)},toString:function(){return this.compound()}},c.prototype={notes:function(){for(var t=this.voicing(),e=[],n=0,r=t.length;r>n;n++)e.push(l.interval.from(this.root,t[n]));return e},voicing:function(t){if(!t)return this._voicing;this._voicing=[];for(var e=0,n=t.length;n>e;e++)this._voicing[e]=l.interval(t[e]);return this},resetVoicing:function(){this._voicing=this.intervals},dominant:function(t){return t=t||"",new c(this.root.interval("P5"),t)},subdominant:function(t){return t=t||"",new c(this.root.interval("P4"),t)},parallel:function(t){t=t||"";var e=this.quality();if("triad"!==this.chordType()||"diminished"===e||"augmented"===e)throw Error("Only major/minor triads have parallel chords");return"major"===e?new c(this.root.interval("m3","down"),"m"):new c(this.root.interval("m3","up"))},quality:function(){for(var t,e,n,r=this.intervals,i=0,o=r.length;o>i;i++)3===r[i].number()?t=r[i]:5===r[i].number()?e=r[i]:7===r[i].number()&&(n=r[i]);return t?(t="down"===t.direction()?t.invert():t,t=t.simple(),e&&(e="down"===e.direction?e.invert():e,e=e.simple()),n&&(n="down"===n.direction?n.invert():n,n=n.simple()),"M3"===t?"A5"===e?"augmented":"P5"===e?"m7"===n?"dominant":"major":"major":"m3"===t?"P5"===e?"minor":"d5"===e?"m7"===n?"half-diminished":"diminished":"minor":void 0):void 0},chordType:function(){var t,e,n,r,i,o=this.intervals.length;if(2===o)return"dyad";if(3===o){for(e={first:!1,third:!1,fifth:!1},r=0;o>r;r++)t=this.intervals[r],n=t.invert(),t.base()in e?e[t.base()]=!0:n.base()in e&&(e[n.base()]=!0);i=e.first&&e.third&&e.fifth?"triad":"trichord"}else if(4===o){for(e={first:!1,third:!1,fifth:!1,seventh:!1},r=0;o>r;r++)t=this.intervals[r],n=t.invert(),t.base()in e?e[t.base()]=!0:n.base()in e&&(e[n.base()]=!0);e.first&&e.third&&e.fifth&&e.seventh&&(i="tetrad")}return i||"unknown"},get:function(t){if("string"==typeof t&&t in P){var e,n,r=this.intervals;for(t=P[t],e=0,n=r.length;n>e;e++)if(r[e].number()===t)return l.interval.from(this.root,r[e]);return null}throw Error("Invalid interval name")},interval:function(t){return new c(this.root.interval(t),this.symbol)},transpose:function(t){return this.root.transpose(t),this.name=this.root.name().toUpperCase()+this.root.accidental()+this.symbol,this},toString:function(){return this.name}},f.prototype={notes:function(){for(var t=[],e=0,n=this.scale.length;n>e;e++)t.push(l.interval(this.tonic,this.scale[e]));return t},simple:function(){return this.notes().map(function(t){return t.toString(!0)})},type:function(){var t=this.scale.length-2;return 8>t?["di","tri","tetra","penta","hexa","hepta","octa"][t]+"tonic":void 0},get:function(t){return t="string"==typeof t&&t in P?P[t]:t,this.tonic.interval(this.scale[t-1])},solfege:function(t,e){return t?this.get(t).solfege(this,e):this.notes().map(function(t){return t.solfege(this,e)})},interval:function(t){return new f(this.tonic.interval(t),this.scale)},transpose:function(t){var e=this.interval(t);return this.scale=e.scale,this.tonic=e.tonic,this}},l.scale.scales.ionian=l.scale.scales.major=["P1","M2","M3","P4","P5","M6","M7"],l.scale.scales.dorian=["P1","M2","m3","P4","P5","M6","m7"],l.scale.scales.phrygian=["P1","m2","m3","P4","P5","m6","m7"],l.scale.scales.lydian=["P1","M2","M3","A4","P5","M6","M7"],l.scale.scales.mixolydian=["P1","M2","M3","P4","P5","M6","m7"],l.scale.scales.aeolian=l.scale.scales.minor=["P1","M2","m3","P4","P5","m6","m7"],l.scale.scales.locrian=["P1","m2","m3","P4","d5","m6","m7"],l.scale.scales.majorpentatonic=["P1","M2","M3","P5","M6"],l.scale.scales.minorpentatonic=["P1","m3","P4","P5","m7"],l.scale.scales.chromatic=l.scale.scales.harmonicchromatic=["P1","m2","M2","m3","M3","P4","A4","P5","m6","M6","m7","M7"],l.TeoriaNote=a,l.TeoriaChord=c,l.TeoriaScale=f,l.TeoriaInterval=u,n!==void 0?(e!==void 0&&e.exports&&(n=e.exports=l),n.teoria=l):this!==void 0?this.teoria=l:"undefined"!=typeof window&&(window.teoria=l)})()},{}]},{},[]);var master=new(webkitAudioContext||AudioContext),jsynth=require("jsynth"),tune=require("tune"),teoria=require("teoria"),tones=[],notes=["first","second","third","fourth","fifth"];notes.map(function(t){var e=teoria.note("a").scale("lydian"),n=e.get(t),r=teoria.note(""+n),i=r.scale("lydian").simple();i.map(function(t){tones.push(t.toUpperCase())})}),console.log(tones);var tuned=tune(tones,{tempo:8}),synth=jsynth(master,function(t){return tuned(2*t)});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