tone-rhythm
Version:
Generate an array of Tone.Transport times for use in Tone.Part
7 lines • 15.6 kB
JavaScript
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.toneRhythm=e():t.toneRhythm=e()}(window,function(){return function(t){var e={};function n(r){if(e[r])return e[r].exports;var o=e[r]={i:r,l:!1,exports:{}};return t[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}return n.m=t,n.c=e,n.d=function(t,e,r){n.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:r})},n.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},n.t=function(t,e){if(1&e&&(t=n(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var o in t)n.d(r,o,function(e){return t[e]}.bind(null,o));return r},n.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return n.d(e,"a",e),e},n.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},n.p="",n(n.s=2)}([function(t,e,n){var r;
/**
* Tone.js v0.12.80
* @author Yotam Mann
* @license http://opensource.org/licenses/MIT MIT License
* @copyright 2014-2018 Yotam Mann
*/void 0===(r=function(){"use strict";var t=function(){if(!(this instanceof t))throw new Error("constructor needs to be called with the 'new' keyword")};t.prototype.toString=function(){for(var e in t){var n=e[0].match(/^[A-Z]$/),r=t[e]===this.constructor;if(t.isFunction(t[e])&&n&&r)return e}return"Tone"},t.prototype.dispose=function(){return this},t.prototype.set=function(e,n,r){if(t.isObject(e))r=n;else if(t.isString(e)){var o={};o[e]=n,e=o}t:for(var i in e){n=e[i];var s=this;if(-1!==i.indexOf(".")){for(var a=i.split("."),u=0;u<a.length-1;u++)if((s=s[a[u]])instanceof t){a.splice(0,u+1);var c=a.join(".");s.set(c,n);continue t}i=a[a.length-1]}var f=s[i];t.isUndef(f)||(t.Signal&&f instanceof t.Signal||t.Param&&f instanceof t.Param?f.value!==n&&(t.isUndef(r)?f.value=n:f.rampTo(n,r)):f instanceof AudioParam?f.value!==n&&(f.value=n):t.TimeBase&&f instanceof t.TimeBase?s[i]=n:f instanceof t?f.set(n):f!==n&&(s[i]=n))}return this},t.prototype.get=function(e){t.isUndef(e)?e=this._collectDefaults(this.constructor):t.isString(e)&&(e=[e]);for(var n={},r=0;r<e.length;r++){var o=e[r],i=this,s=n;if(-1!==o.indexOf(".")){for(var a=o.split("."),u=0;u<a.length-1;u++){var c=a[u];s[c]=s[c]||{},s=s[c],i=i[c]}o=a[a.length-1]}var f=i[o];t.isObject(e[o])?s[o]=f.get():t.Signal&&f instanceof t.Signal?s[o]=f.value:t.Param&&f instanceof t.Param?s[o]=f.value:f instanceof AudioParam?s[o]=f.value:f instanceof t?s[o]=f.get():!t.isFunction(f)&&t.isDefined(f)&&(s[o]=f)}return n},t.prototype._collectDefaults=function(e){var n=[];if(t.isDefined(e.defaults)&&(n=Object.keys(e.defaults)),t.isDefined(e._super))for(var r=this._collectDefaults(e._super),o=0;o<r.length;o++)-1===n.indexOf(r[o])&&n.push(r[o]);return n},t.defaults=function(e,n,r){var o={};if(1===e.length&&t.isObject(e[0]))o=e[0];else for(var i=0;i<n.length;i++)o[n[i]]=e[i];return t.isDefined(r.defaults)?t.defaultArg(o,r.defaults):t.isObject(r)?t.defaultArg(o,r):o},t.defaultArg=function(e,n){if(t.isObject(e)&&t.isObject(n)){var r={};for(var o in e)r[o]=t.defaultArg(n[o],e[o]);for(var i in n)r[i]=t.defaultArg(e[i],n[i]);return r}return t.isUndef(e)?n:e},t.connectSeries=function(){for(var e=arguments[0],n=1;n<arguments.length;n++){var r=arguments[n];e.connect(r),e=r}return t},t.isUndef=function(t){return void 0===t},t.isDefined=function(e){return!t.isUndef(e)},t.isFunction=function(t){return"function"==typeof t},t.isNumber=function(t){return"number"==typeof t},t.isObject=function(t){return"[object Object]"===Object.prototype.toString.call(t)&&t.constructor===Object},t.isBoolean=function(t){return"boolean"==typeof t},t.isArray=function(t){return Array.isArray(t)},t.isString=function(t){return"string"==typeof t},t.isNote=function(e){return t.isString(e)&&/^([a-g]{1}(?:b|#|x|bb)?)(-?[0-9]+)/i.test(e)},t.noOp=function(){},t.prototype._readOnly=function(t){if(Array.isArray(t))for(var e=0;e<t.length;e++)this._readOnly(t[e]);else Object.defineProperty(this,t,{writable:!1,enumerable:!0})},t.prototype._writable=function(t){if(Array.isArray(t))for(var e=0;e<t.length;e++)this._writable(t[e]);else Object.defineProperty(this,t,{writable:!0})},t.State={Started:"started",Stopped:"stopped",Paused:"paused"},t.equalPowerScale=function(t){var e=.5*Math.PI;return Math.sin(t*e)},t.dbToGain=function(t){return Math.pow(10,t/20)},t.gainToDb=function(t){return Math.log(t)/Math.LN10*20},t.intervalToFrequencyRatio=function(t){return Math.pow(2,t/12)},t.prototype.now=function(){return t.context.now()},t.now=function(){return t.context.now()},t.extend=function(e,n){function r(){}t.isUndef(n)&&(n=t),r.prototype=n.prototype,e.prototype=new r,e.prototype.constructor=e,e._super=n};var e=null;return Object.defineProperty(t,"context",{get:function(){return e},set:function(n){e=t.Context&&n instanceof t.Context?n:new t.Context(n),t.Context.emit("init",e)}}),Object.defineProperty(t.prototype,"context",{get:function(){return t.context}}),t.setContext=function(e){t.context=e},Object.defineProperty(t.prototype,"blockTime",{get:function(){return 128/this.context.sampleRate}}),Object.defineProperty(t.prototype,"sampleTime",{get:function(){return 1/this.context.sampleRate}}),Object.defineProperty(t,"supported",{get:function(){var t=window.hasOwnProperty("AudioContext")||window.hasOwnProperty("webkitAudioContext"),e=window.hasOwnProperty("Promise"),n=window.hasOwnProperty("Worker");return t&&e&&n}}),Object.defineProperty(t,"initialized",{get:function(){return null!==e}}),t.getContext=function(e){if(t.initialized)e(t.context);else{var n=function(){e(t.context),t.Context.off("init",n)};t.Context.on("init",n)}return t},t.version="r12",t}.call(e,n,e,t))||(t.exports=r)},function(t,e,n){var r,o;r=[n(0)],void 0===(o=function(t){return t.TimeBase=function(e,n){if(!(this instanceof t.TimeBase))return new t.TimeBase(e,n);if(this._val=e,this._units=n,t.isUndef(this._units)&&t.isString(this._val)&&parseFloat(this._val)==this._val&&"+"!==this._val.charAt(0))this._val=parseFloat(this._val),this._units=this._defaultUnits;else if(e&&e.constructor===this.constructor)this._val=e._val,this._units=e._units;else if(e instanceof t.TimeBase)switch(this._defaultUnits){case"s":this._val=e.toSeconds();break;case"i":this._val=e.toTicks();break;case"hz":this._val=e.toFrequency();break;case"midi":this._val=e.toMidi();break;default:throw new Error("Unrecognized default units "+this._defaultUnits)}},t.extend(t.TimeBase),t.TimeBase.prototype._expressions={n:{regexp:/^(\d+)n(\.?)$/i,method:function(t,e){t=parseInt(t);var n="."===e?1.5:1;return 1===t?this._beatsToUnits(this._getTimeSignature())*n:this._beatsToUnits(4/t)*n}},t:{regexp:/^(\d+)t$/i,method:function(t){return t=parseInt(t),this._beatsToUnits(8/(3*parseInt(t)))}},m:{regexp:/^(\d+)m$/i,method:function(t){return this._beatsToUnits(parseInt(t)*this._getTimeSignature())}},i:{regexp:/^(\d+)i$/i,method:function(t){return this._ticksToUnits(parseInt(t))}},hz:{regexp:/^(\d+(?:\.\d+)?)hz$/i,method:function(t){return this._frequencyToUnits(parseFloat(t))}},tr:{regexp:/^(\d+(?:\.\d+)?):(\d+(?:\.\d+)?):?(\d+(?:\.\d+)?)?$/,method:function(t,e,n){var r=0;return t&&"0"!==t&&(r+=this._beatsToUnits(this._getTimeSignature()*parseFloat(t))),e&&"0"!==e&&(r+=this._beatsToUnits(parseFloat(e))),n&&"0"!==n&&(r+=this._beatsToUnits(parseFloat(n)/4)),r}},s:{regexp:/^(\d+(?:\.\d+)?)s$/,method:function(t){return this._secondsToUnits(parseFloat(t))}},samples:{regexp:/^(\d+)samples$/,method:function(t){return parseInt(t)/this.context.sampleRate}},default:{regexp:/^(\d+(?:\.\d+)?)$/,method:function(t){return this._expressions[this._defaultUnits].method.call(this,t)}}},t.TimeBase.prototype._defaultUnits="s",t.TimeBase.prototype._getBpm=function(){return t.Transport?t.Transport.bpm.value:120},t.TimeBase.prototype._getTimeSignature=function(){return t.Transport?t.Transport.timeSignature:4},t.TimeBase.prototype._getPPQ=function(){return t.Transport?t.Transport.PPQ:192},t.TimeBase.prototype._now=function(){return this.now()},t.TimeBase.prototype._frequencyToUnits=function(t){return 1/t},t.TimeBase.prototype._beatsToUnits=function(t){return 60/this._getBpm()*t},t.TimeBase.prototype._secondsToUnits=function(t){return t},t.TimeBase.prototype._ticksToUnits=function(t){return t*(this._beatsToUnits(1)/this._getPPQ())},t.TimeBase.prototype._noArg=function(){return this._now()},t.TimeBase.prototype.valueOf=function(){if(t.isUndef(this._val))return this._noArg();if(t.isString(this._val)&&t.isUndef(this._units))for(var e in this._expressions)if(this._expressions[e].regexp.test(this._val.trim())){this._units=e;break}if(t.isDefined(this._units)){var n=this._expressions[this._units],r=this._val.toString().trim().match(n.regexp);return r?n.method.apply(this,r.slice(1)):n.method.call(this,parseFloat(this._val))}return this._val},t.TimeBase.prototype.toSeconds=function(){return this.valueOf()},t.TimeBase.prototype.toFrequency=function(){return 1/this.toSeconds()},t.TimeBase.prototype.toSamples=function(){return this.toSeconds()*this.context.sampleRate},t.TimeBase.prototype.toMilliseconds=function(){return 1e3*this.toSeconds()},t.TimeBase.prototype.dispose=function(){this._val=null,this._units=null},t.TimeBase}.apply(e,r))||(t.exports=o)},function(t,e,n){function r(t){return function(t){if(Array.isArray(t)){for(var e=0,n=new Array(t.length);e<t.length;e++)n[e]=t[e];return n}}(t)||function(t){if(Symbol.iterator in Object(t)||"[object Arguments]"===Object.prototype.toString.call(t))return Array.from(t)}(t)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance")}()}function o(t,e){return function(t){if(Array.isArray(t))return t}(t)||function(t,e){var n=[],r=!0,o=!1,i=void 0;try{for(var s,a=t[Symbol.iterator]();!(r=(s=a.next()).done)&&(n.push(s.value),!e||n.length!==e);r=!0);}catch(t){o=!0,i=t}finally{try{r||null==a.return||a.return()}finally{if(o)throw i}}return n}(t,e)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance")}()}function i(t){return(i="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}var s=n(3),a=function(t){if(!new Set(["string","number"]).has(i(t)))throw TypeError("Expected type string or type number.");var e=s(t).toBarsBeatsSixteenths(),n=o(e.split("."),2),r=n[0],a=n[1];return new Set(["001","002","003","004"]).has(a)?r:e},u=function(t){return Array.isArray(t)?("r"===t[0]&&(t=t.slice(1)),t.reduce(function(t,e){return a(s(t)+s(e))})):t},c=function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0,n=[e];return t.forEach(function(t){if(Array.isArray(t))if(e=u(r(t).concat([e])),"r"===t[0]){var o=n.length-1;n[o]=e}else n.push(e);else e=u([t,e]),n.push(e)}),n.slice(0,-1)};t.exports={getBarsBeats:a,addTimes:u,getTransportTimes:c,mergeMusicDataPart:function(t){var e=t.notes,n=t.rhythms,r=t.velocities,o=t.startTime,i=t.times;if(!n||!Array.isArray(n))throw TypeError('Expected "rhythms" property with type "Array"');return i||(i=c(n,o)),n.map(function(t,n){var o={time:i[n],duration:u(t)};return e&&(o.note=e[n]),r&&(o.velocity=r[n]),o})}}},function(t,e,n){var r,o;r=[n(0),n(1),n(4)],void 0===(o=function(t){return t.Time=function(e,n){if(!(this instanceof t.Time))return new t.Time(e,n);t.TimeBase.call(this,e,n)},t.extend(t.Time,t.TimeBase),t.Time.prototype._expressions=Object.assign({},t.TimeBase.prototype._expressions,{quantize:{regexp:/^@(.+)/,method:function(e){if(t.Transport){var n=new this.constructor(e);return t.Transport.nextSubdivision(n)}return 0}},now:{regexp:/^\+(.+)/,method:function(t){return this._now()+new this.constructor(t)}}}),t.Time.prototype.quantize=function(e,n){n=t.defaultArg(n,1);var r=new this.constructor(e),o=this.valueOf();return o+(Math.round(o/r)*r-o)*n},t.Time.prototype.toNotation=function(){for(var e=this.toSeconds(),n=["1m"],r=1;r<8;r++){var o=Math.pow(2,r);n.push(o+"n."),n.push(o+"n"),n.push(o+"t")}n.push("0");var i=n[0],s=t.Time(n[0]).toSeconds();return n.forEach(function(n){var r=t.Time(n).toSeconds();Math.abs(r-e)<Math.abs(s-e)&&(i=n,s=r)}),i},t.Time.prototype.toBarsBeatsSixteenths=function(){var t=this._beatsToUnits(1),e=this.valueOf()/t,n=Math.floor(e/this._getTimeSignature()),r=e%1*4;return e=Math.floor(e)%this._getTimeSignature(),(r=r.toString()).length>3&&(r=parseFloat(parseFloat(r).toFixed(3))),[n,e,r].join(":")},t.Time.prototype.toTicks=function(){var t=this._beatsToUnits(1),e=this.valueOf()/t;return Math.round(e*this._getPPQ())},t.Time.prototype.toSeconds=function(){return this.valueOf()},t.Time.prototype.toMidi=function(){return t.Frequency.ftom(this.toFrequency())},t.Time}.apply(e,r))||(t.exports=o)},function(t,e,n){var r,o;r=[n(0),n(1)],void 0===(o=function(t){t.Frequency=function(e,n){if(!(this instanceof t.Frequency))return new t.Frequency(e,n);t.TimeBase.call(this,e,n)},t.extend(t.Frequency,t.TimeBase),t.Frequency.prototype._expressions=Object.assign({},t.TimeBase.prototype._expressions,{midi:{regexp:/^(\d+(?:\.\d+)?midi)/,method:function(e){return"midi"===this._defaultUnits?e:t.Frequency.mtof(e)}},note:{regexp:/^([a-g]{1}(?:b|#|x|bb)?)(-?[0-9]+)/i,method:function(n,r){var o=e[n.toLowerCase()]+12*(parseInt(r)+1);return"midi"===this._defaultUnits?o:t.Frequency.mtof(o)}},tr:{regexp:/^(\d+(?:\.\d+)?):(\d+(?:\.\d+)?):?(\d+(?:\.\d+)?)?/,method:function(t,e,n){var r=1;return t&&"0"!==t&&(r*=this._beatsToUnits(this._getTimeSignature()*parseFloat(t))),e&&"0"!==e&&(r*=this._beatsToUnits(parseFloat(e))),n&&"0"!==n&&(r*=this._beatsToUnits(parseFloat(n)/4)),r}}}),t.Frequency.prototype.transpose=function(e){return new this.constructor(this.valueOf()*t.intervalToFrequencyRatio(e))},t.Frequency.prototype.harmonize=function(t){return t.map(function(t){return this.transpose(t)}.bind(this))},t.Frequency.prototype.toMidi=function(){return t.Frequency.ftom(this.valueOf())},t.Frequency.prototype.toNote=function(){var e=this.toFrequency(),r=Math.log2(e/t.Frequency.A4),o=Math.round(12*r)+57,i=Math.floor(o/12);return i<0&&(o+=-12*i),n[o%12]+i.toString()},t.Frequency.prototype.toSeconds=function(){return 1/t.TimeBase.prototype.toSeconds.call(this)},t.Frequency.prototype.toFrequency=function(){return t.TimeBase.prototype.toFrequency.call(this)},t.Frequency.prototype.toTicks=function(){var e=this._beatsToUnits(1),n=this.valueOf()/e;return Math.floor(n*t.Transport.PPQ)},t.Frequency.prototype._noArg=function(){return 0},t.Frequency.prototype._frequencyToUnits=function(t){return t},t.Frequency.prototype._ticksToUnits=function(e){return 1/(60*e/(t.Transport.bpm.value*t.Transport.PPQ))},t.Frequency.prototype._beatsToUnits=function(e){return 1/t.TimeBase.prototype._beatsToUnits.call(this,e)},t.Frequency.prototype._secondsToUnits=function(t){return 1/t},t.Frequency.prototype._defaultUnits="hz";var e={cbb:-2,cb:-1,c:0,"c#":1,cx:2,dbb:0,db:1,d:2,"d#":3,dx:4,ebb:2,eb:3,e:4,"e#":5,ex:6,fbb:3,fb:4,f:5,"f#":6,fx:7,gbb:5,gb:6,g:7,"g#":8,gx:9,abb:7,ab:8,a:9,"a#":10,ax:11,bbb:9,bb:10,b:11,"b#":12,bx:13},n=["C","C#","D","D#","E","F","F#","G","G#","A","A#","B"];return t.Frequency.A4=440,t.Frequency.mtof=function(e){return t.Frequency.A4*Math.pow(2,(e-69)/12)},t.Frequency.ftom=function(e){return 69+Math.round(12*Math.log2(e/t.Frequency.A4))},t.Frequency}.apply(e,r))||(t.exports=o)}])});