scribbletune 5.2.0 → 5.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +276 -22
- package/dist/browser.cjs +1183 -0
- package/dist/browser.cjs.map +1 -0
- package/dist/browser.js +1135 -1
- package/dist/browser.js.map +1 -1
- package/dist/cli.cjs +813 -0
- package/dist/{index.mjs → index.cjs} +225 -169
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +323 -0
- package/dist/index.d.ts +303 -350
- package/dist/index.js +524 -1
- package/dist/index.js.map +1 -1
- package/dist/scribbletune.global.js +1944 -0
- package/dist/scribbletune.global.js.map +1 -0
- package/package.json +32 -40
- package/dist/lib/arp.d.ts +0 -10
- package/dist/lib/browser-clip.d.ts +0 -14
- package/dist/lib/browser-index.d.ts +0 -7
- package/dist/lib/channel.d.ts +0 -61
- package/dist/lib/clip.d.ts +0 -2
- package/dist/lib/index.d.ts +0 -7
- package/dist/lib/midi.d.ts +0 -11
- package/dist/lib/progression.d.ts +0 -25
- package/dist/lib/session.d.ts +0 -14
- package/dist/lib/types/arp-params.d.ts +0 -6
- package/dist/lib/types/channel-params.d.ts +0 -92
- package/dist/lib/types/channel-pattern.d.ts +0 -24
- package/dist/lib/types/clip-params.d.ts +0 -104
- package/dist/lib/types/event-fn.d.ts +0 -7
- package/dist/lib/types/index.d.ts +0 -14
- package/dist/lib/types/note-object.d.ts +0 -6
- package/dist/lib/types/nvp.d.ts +0 -4
- package/dist/lib/types/play-params.d.ts +0 -15
- package/dist/lib/types/player-observer-fn.d.ts +0 -6
- package/dist/lib/types/progression-scale.d.ts +0 -2
- package/dist/lib/types/seq-fn.d.ts +0 -2
- package/dist/lib/types/sizzle-style.d.ts +0 -2
- package/dist/lib/types/synth-params.d.ts +0 -20
- package/dist/lib/types/tpd.d.ts +0 -15
- package/dist/lib/utils.d.ts +0 -56
- package/dist/scribbletune.js +0 -2
- package/dist/scribbletune.js.map +0 -1
package/dist/browser.js
CHANGED
|
@@ -1,2 +1,1136 @@
|
|
|
1
|
-
(()=>{"use strict";var t={712(t){var e,n=Object.defineProperty,r=Object.getOwnPropertyDescriptor,o=Object.getOwnPropertyNames,i=Object.prototype.hasOwnProperty,a={};((t,e)=>{for(var r in e)n(t,r,{get:e[r],enumerable:!0})})(a,{chord:()=>m,chords:()=>d,getIndicesFromScale:()=>v,inlineChord:()=>p,scale:()=>y,scales:()=>f}),t.exports=(e=a,((t,e,a,s)=>{if(e&&"object"==typeof e||"function"==typeof e)for(let a of o(e))i.call(t,a)||void 0===a||n(t,a,{get:()=>e[a],enumerable:!(s=r(e,a))||s.enumerable});return t})(n({},"__esModule",{value:!0}),e));var s={"C#":"Db","D#":"Eb","F#":"Gb","G#":"Ab","A#":"Bb",CB:"B",FB:"E","E#":"F","B#":"C"},c=["C","Db","D","Eb","E","F","Gb","G","Ab","A","Bb","B"],u={"major pentatonic":"101010010100","ionian pentatonic":"100011010001","mixolydian pentatonic":"100011010010",ritusen:"101001010100",egyptian:"101001010010","neopolitan major pentatonic":"100011100010","vietnamese 1":"100101011000",pelog:"110100011000",kumoijoshi:"110001011000",hirajoshi:"101100011000",iwato:"110001100010","in-sen":"110001010010","lydian pentatonic":"100010110001","malkos raga":"100101001010","locrian pentatonic":"100101100010","minor pentatonic":"100101010010","minor six pentatonic":"100101010100","flat three pentatonic":"101100010100","flat six pentatonic":"101010011000",scriabin:"110010010100","whole tone pentatonic":"100010101010","lydian #5P pentatonic":"100010101001","lydian dominant pentatonic":"100010110010","minor #7M pentatonic":"100101010001","super locrian pentatonic":"100110100010","minor hexatonic":"101101010001",augmented:"100110011001","major blues":"101110010100",piongio:"101001010110","prometheus neopolitan":"110010100110",prometheus:"101010100110","mystery #1":"110010101010","six tone symmetric":"110011001100","whole tone":"101010101010","messiaen's mode #5":"110001110001","minor blues":"100101110010","locrian major":"101011101010","double harmonic lydian":"110010111001","harmonic minor":"101101011001",altered:"110110101010","locrian #2":"101101101010","mixolydian b6":"101011011010","lydian dominant":"101010110110",lydian:"101010110101","lydian augmented":"101010101101","dorian b2":"110101010110","melodic minor":"101101010101",locrian:"110101101010",ultralocrian:"110110101100","locrian 6":"110101100110","augmented heptatonic":"100111011001","romanian minor":"101100110110","dorian #4":"101100110110","lydian diminished":"101100110101",phrygian:"110101011010","leading whole tone":"101010101011","lydian minor":"101010111010","phrygian dominant":"110011011010",balinese:"110101011001","neopolitan major":"110101010101",aeolian:"101101011010","harmonic major":"101011011001","double harmonic major":"110011011001",dorian:"101101010110","hungarian minor":"101100111001","hungarian major":"100110110110",oriental:"110011100110",flamenco:"110110110010","todi raga":"110100111001",mixolydian:"101011010110",persian:"110011101001",major:"101011010101",enigmatic:"110010101011","major augmented":"101011001101","lydian #9":"100110110101","messiaen's mode #4":"111001111001","purvi raga":"110011111001","spanish heptatonic":"110111011010",bebop:"101011010111","bebop minor":"101111010110","bebop major":"101011011101","bebop locrian":"110101111010","minor bebop":"101101011011",diminished:"101101101101",ichikosucho:"101011110101","minor six diminished":"101101011101","half-whole diminished":"110110110110","kafi raga":"100111010111","messiaen's mode #6":"101011101011","composite blues":"101111110110","messiaen's mode #3":"101110111011","messiaen's mode #7":"111101111101",chromatic:"111111111111",ionian:"101011010101",minor:"101101011010",Kanakangi:"111001011100",Ratnangi:"111001011010",Ganamurti:"111001011001",Vanaspati:"111001010110",Manavati:"111001010101",Tanarupi:"111001010011",Senavati:"110101011100",Hanumatodi:"110101011010",Dhenuka:"110101011001",Natakapriya:"110101010110",Kokilapriya:"110101010101",Rupavati:"110101010011",Gayakapriya:"110011011100",Vakulabharanam:"110011011010",Mayamalavagowla:"110011011001",Chakravakam:"110011010110",Suryakantam:"110011010101",Hatakambari:"110011010011",Jhankaradhwani:"101101011100",Natabhairavi:"101101011010",Keeravani:"101101011001",Kharaharapriya:"101101010110",Gourimanohari:"101101010101",Varunapriya:"101101010011",Mararanjani:"101011011100",Charukesi:"101011011010",Sarasangi:"101011011001",Harikambhoji:"101011010110",Dheerasankarabaranam:"101011010101",Naganandini:"101011010011",Yagapriya:"100111011100",Ragavardhini:"100111011010",Gangeyabhushani:"100111011001",Vagadheeswari:"100111010110",Shulini:"100111010101",Chalanata:"100111010011",Salagam:"111000111100",Jalarnavam:"111000111010",Jhalavarali:"111000111001",Navaneetam:"111000110110",Pavani:"111000110101",Raghupriya:"111000110011",Gavambhodi:"110100111100",Bhavapriya:"110100111010",Shubhapantuvarali:"110100111001",Shadvidamargini:"110100110110",Suvarnangi:"110100110101",Divyamani:"110100110011",Dhavalambari:"110010111100",Namanarayani:"110010111010",Kamavardhini:"110010111001",Ramapriya:"110010110110",Gamanashrama:"110010110101",Vishwambari:"110010110011",Shamalangi:"101100111100",Shanmukhapriya:"101100111010",Simhendramadhyamam:"101100111001",Hemavati:"101100110110",Dharmavati:"101100110101",Neetimati:"101100110011",Kantamani:"101010111100",Rishabhapriya:"101010111010",Latangi:"101010111001",Vachaspati:"101010110110",Mechakalyani:"101010110101",Chitrambari:"101010110011",Sucharitra:"100110111100","Jyoti swarupini":"100110111010",Dhatuvardani:"100110111001",Nasikabhushini:"100110110110",Kosalam:"100110110101",Rasikapriya:"100110110011"},l={"5th":"100000010000","M7#5sus4":"100001001001","7#5sus4":"100001001010",sus4:"100001010000",M7sus4:"100001010001","7sus4":"100001010010","7no5":"100010000010",aug:"100010001000",M7b6:"100010001001","maj7#5":"100010001001","7#5":"100010001010","7b13":"100010001010",M:"100010010000",maj7:"100010010001","7th":"100010010010","6th":"100010010100","7add6":"100010010110","7b6":"100010011010",Mb5:"100010100000",M7b5:"100010100001","7b5":"100010100010","maj#4":"100010110001","7#11":"100010110010","M6#11":"100010110100","7#11b13":"100010111010","m#5":"100100001000",mb6M7:"100100001001","m7#5":"100100001010",m:"100100010000","m/ma7":"100100010001",m7:"100100010010",m6:"100100010100",mMaj7b6:"100100011001",dim:"100100100000",oM7:"100100100001",m7b5:"100100100010",dim7:"100100100100",o7M7:"100100100101","4th":"100101000010",madd4:"100101010000",m7add11:"100101010010","+add#9":"100110001000","7#5#9":"100110001010","7#9":"100110010010","13#9":"100110010110","7#9b13":"100110011010","maj7#9#11":"100110110001","7#9#11":"100110110010","13#9#11":"100110110110","7#9#11b13":"100110111010",sus2:"101000010000","M9#5sus4":"101001001001",sus24:"101001010000",M9sus4:"101001010001","11th":"101001010010","9sus4":"101001010010","13sus4":"101001010110","9no5":"101010000010","13no5":"101010000110","M#5add9":"101010001000","maj9#5":"101010001001","9#5":"101010001010","9b13":"101010001010",Madd9:"101010010000",maj9:"101010010001","9th":"101010010010","6/9":"101010010100",maj13:"101010010101",M7add13:"101010010101","13th":"101010010110",M9b5:"101010100001","9b5":"101010100010","13b5":"101010100110","9#5#11":"101010101010","maj9#11":"101010110001","9#11":"101010110010","69#11":"101010110100","M13#11":"101010110101","13#11":"101010110110","9#11b13":"101010111010","m9#5":"101100001010",madd9:"101100010000",mM9:"101100010001",m9:"101100010010",m69:"101100010100",m13:"101100010110",mMaj9b6:"101100011001",m9b5:"101100100010",m11A:"101101001010",m11:"101101010010",b9sus:"110001010010","11b9":"110001010010","7sus4b9b13":"110001011010",alt7:"110010000010","7#5b9":"110010001010",Maddb9:"110010010000",M7b9:"110010010001","7b9":"110010010010","13b9":"110010010110","7b9b13":"110010011010","7#5b9#11":"110010101010","7b9#11":"110010110010","13b9#11":"110010110110","7b9b13#11":"110010111010",mb6b9:"110100001000","7b9#9":"110110010010"};function h({scale:t,chord:e}){const n=t||e,r=t?"scale":"chord";if("string"!=typeof n)throw new Error(`${n} is not a valid input for ${r}`);const o=n.trim(),i=o.indexOf(" ");let a,h;-1===i?(a=o.slice(1),h=o[0],"b"!==o[1]&&"#"!==o[1]||(a=o.slice(2),h+=o[1])):(a=o.slice(-1===i?1:i+1),h=o.slice(0,i));const p=function(t){return s[t.toUpperCase()]||t.charAt(0).toUpperCase()+t.slice(1)}(h.replace(/\d/g,"")),d=""!==h.replace(/\D/g,"")?+h.replace(/\D/g,""):4;if(isNaN(d))throw new Error(`${h[0]} does not have a valid octave`);if(!u[a]&&!l[a])throw new Error(`${o} is not a valid ${r}`);const f=function(t,e){const n=c.indexOf(t);if(-1===n)throw new Error(`${t} is not a valid root note`);const r=c.map(t=>t+e),o=c.map(t=>t+(e+1));return r.concat(o).slice(n)}(p,d),m=[];let y=0,v=0;const b=t?u:l;for(;y<b[a].length;)"1"===b[a][y]&&m.push(f[v]),y++,v++;return m}function p(t){const e="b9sus";let n,r,o=4;return t.includes(e)?(r=e,n=t.slice(0,t.indexOf(e))):(n=t[0],r=t.slice(1),"b"!==t[1]&&"#"!==t[1]||(n+=t[1],r=t.slice(2))),t.includes("_")&&(o=+t.split("_")[1],r=r.slice(0,r.indexOf("_"))),h({chord:n+o+" "+r})}function d(){return Object.keys(l)}function f(){return Object.keys(u)}function m(t){return h({chord:t})}function y(t){return h({scale:t})}function v(t){const e=u[t]||t,n=[];for(let t=0;t<e.length;t++)"1"===e[t]&&n.push(t);return n.push(12),n}},716(t,e,n){var r=this&&this.__spreadArray||function(t,e,n){if(n||2===arguments.length)for(var r,o=0,i=e.length;o<i;o++)!r&&o in e||(r||(r=Array.prototype.slice.call(e,0,o)),r[o]=e[o]);return t.concat(r||Array.prototype.slice.call(e))};Object.defineProperty(e,"__esModule",{value:!0}),e.arp=void 0;var o=n(712),i=n(552),a=function(t,e){var n=function(t){if(!t)throw new Error("Empty element");var e=t.replace(/\d/,""),n=t.replace(/\D/g,"")||4;if(!e)throw new Error("Incorrect note");return e+(+n+1)},o=t.map(n),i=o.map(n);return r(r(r([],t,!0),o,!0),i,!0).slice(0,e)};e.arp=function(t){var e=[],n={count:4,order:"0123",chords:""};if("string"==typeof t)n.chords=t;else{if(t.order&&t.order.match(/\D/g))throw new TypeError("Invalid value for order");if(t.count>8||t.count<2)throw new TypeError("Invalid value for count");t.count&&!t.order&&(n.order=Array.from(Array(t.count).keys()).join("")),Object.assign(n,t)}if("string"==typeof n.chords)n.chords.split(" ").forEach(function(t,i){try{var s=a((0,o.inlineChord)(t),n.count),c=n.order.split("").map(function(t){return s[t]});e=r(r([],e,!0),c,!0)}catch(e){throw new Error("Cannot decode chord ".concat(i+1,' "').concat(t,'" in given "').concat(n.chords,'"'))}});else{if(!Array.isArray(n.chords))throw new TypeError("Invalid value for chords");n.chords.forEach(function(t,o){try{var s=a(t,n.count),c=n.order.split("").map(function(t){return s[t]});e=r(r([],e,!0),c,!0)}catch(e){throw new Error("".concat((0,i.errorHasMessage)(e)?e.message:e," in chord ").concat(o+1,' "').concat(t,'"'))}})}return e}},76(t,e,n){var r=this&&this.__assign||function(){return r=Object.assign||function(t){for(var e,n=1,r=arguments.length;n<r;n++)for(var o in e=arguments[n])Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o]);return t},r.apply(this,arguments)},o=this&&this.__awaiter||function(t,e,n,r){return new(n||(n=Promise))(function(o,i){function a(t){try{c(r.next(t))}catch(t){i(t)}}function s(t){try{c(r.throw(t))}catch(t){i(t)}}function c(t){var e;t.done?o(t.value):(e=t.value,e instanceof n?e:new n(function(t){t(e)})).then(a,s)}c((r=r.apply(t,e||[])).next())})},i=this&&this.__generator||function(t,e){var n,r,o,i={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]},a=Object.create(("function"==typeof Iterator?Iterator:Object).prototype);return a.next=s(0),a.throw=s(1),a.return=s(2),"function"==typeof Symbol&&(a[Symbol.iterator]=function(){return this}),a;function s(s){return function(c){return function(s){if(n)throw new TypeError("Generator is already executing.");for(;a&&(a=0,s[0]&&(i=0)),i;)try{if(n=1,r&&(o=2&s[0]?r.return:s[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,s[1])).done)return o;switch(r=0,o&&(s=[2&s[0],o.value]),s[0]){case 0:case 1:o=s;break;case 4:return i.label++,{value:s[1],done:!1};case 5:i.label++,r=s[1],s=[0];continue;case 7:s=i.ops.pop(),i.trys.pop();continue;default:if(!((o=(o=i.trys).length>0&&o[o.length-1])||6!==s[0]&&2!==s[0])){i=0;continue}if(3===s[0]&&(!o||s[1]>o[0]&&s[1]<o[3])){i.label=s[1];break}if(6===s[0]&&i.label<o[1]){i.label=o[1],o=s;break}if(o&&i.label<o[2]){i.label=o[2],i.ops.push(s);break}o[2]&&i.ops.pop(),i.trys.pop();continue}s=e.call(t,i)}catch(t){s=[6,t],r=0}finally{n=o=0}if(5&s[0])throw s[1];return{value:s[0]?s[1]:void 0,done:!0}}([s,c])}}};Object.defineProperty(e,"__esModule",{value:!0}),e.clip=e.renderingDuration=e.totalPatternDuration=e.recursivelyApplyPatternToDurations=e.getDuration=e.getNote=void 0;var a=n(552),s="4n";e.getNote=function(t,e,n){return"R"===t&&e.randomNotes&&e.randomNotes.length>0?e.randomNotes[(0,a.randomInt)(e.randomNotes.length-1)]:e.notes?e.notes[n%(e.notes.length||1)]:""},e.getDuration=function(t,e){return t.durations?t.durations[e%t.durations.length]:t.dur||t.subdiv||"8n"},e.recursivelyApplyPatternToDurations=function(t,n,r){return void 0===r&&(r=[]),t.forEach(function(t){"string"==typeof t&&("x"!==t&&"R"!==t||r.push(n),"_"===t&&r.length&&(r[r.length-1]+=n)),Array.isArray(t)&&(0,e.recursivelyApplyPatternToDurations)(t,n/t.length,r)}),r};var c=function(t,n,r){if(r=r||Tone.getContext(),!t.pattern)throw new Error("No pattern provided!");return t.durations||t.dur||(t.durations=(0,e.recursivelyApplyPatternToDurations)((0,a.expandStr)(t.pattern),Tone.Ticks(t.subdiv||s).toSeconds())),new Tone.Sequence({callback:n.getSeqFn(t),events:(0,a.expandStr)(t.pattern),subdivision:t.subdiv||s,context:r})};e.totalPatternDuration=function(t,e){return"number"==typeof e?e*(0,a.expandStr)(t).length:Tone.Ticks(e).toSeconds()*(0,a.expandStr)(t).length},e.renderingDuration=function(t,n,r,o){var i=t.split("").filter(function(t){return"x"===t}).length,a=t.split("").filter(function(t){return"R"===t}).length,s=(null==o?void 0:o.length)?i:i+a,c=r.length||1;return(0,e.totalPatternDuration)(t,n)/s*function(t,e){for(var n=t<e?[t,e]:[e,t],r=n[0],o=n[1],i=o;i%r!==0;)i+=o;return i}(c,s)};var u,l=0;e.clip=function(t,n){if("string"==typeof(t=r(r({},{notes:["C4"],pattern:"x",shuffle:!1,sizzle:!1,sizzleReps:1,arpegiate:!1,subdiv:"4n",align:"1m",alignOffset:"0",amp:100,accentLow:70,randomNotes:null,offlineRendering:!1}),t||{})).notes&&(t.notes=t.notes.replace(/\s{2,}/g," "),t.notes=t.notes.split(" ")),t.notes=t.notes?t.notes.map(a.convertChordsToNotes):[],/[^x\-_[\]R]/.test(t.pattern))throw new TypeError("pattern can only comprise x - _ [ ] R, found ".concat(t.pattern));return t.shuffle&&(t.notes=(0,a.shuffle)(t.notes)),t.randomNotes&&"string"==typeof t.randomNotes&&(t.randomNotes=t.randomNotes.replace(/\s{2,}/g," ").split(/\s/)),t.randomNotes&&(t.randomNotes=t.randomNotes.map(a.convertChordsToNotes)),t.offlineRendering?function(t,e){u||(u=Tone.getContext()),l++;var n=new Tone.Player({context:u,loop:!0});return Tone.Offline(function(e){return o(void 0,void 0,void 0,function(){var n;return i(this,function(r){switch(r.label){case 0:return n=c(t,e),[4,Tone.loaded()];case 1:return r.sent(),n.start(),e.transport.start(),[2]}})})},e).then(function(e){var r;n.buffer=e,0===--l&&(Tone.setContext(u),null===(r=t.offlineRenderingCallback)||void 0===r||r.call(t))}),n.toDestination(),n.sync(),n}(t,(0,e.renderingDuration)(t.pattern,t.subdiv||s,t.notes,t.randomNotes)):c(t,n,u)}},376(t,e,n){var r=this&&this.__createBinding||(Object.create?function(t,e,n,r){void 0===r&&(r=n);var o=Object.getOwnPropertyDescriptor(e,n);o&&!("get"in o?!e.__esModule:o.writable||o.configurable)||(o={enumerable:!0,get:function(){return e[n]}}),Object.defineProperty(t,r,o)}:function(t,e,n,r){void 0===r&&(r=n),t[r]=e[n]}),o=this&&this.__exportStar||function(t,e){for(var n in t)"default"===n||Object.prototype.hasOwnProperty.call(e,n)||r(e,t,n)};Object.defineProperty(e,"__esModule",{value:!0}),e.Session=e.midi=e.arp=e.progression=e.getChordsByProgression=e.getChordDegrees=e.clip=e.chords=e.chord=e.modes=e.scales=e.mode=e.scale=void 0;var i=n(712);Object.defineProperty(e,"scales",{enumerable:!0,get:function(){return i.scales}}),Object.defineProperty(e,"modes",{enumerable:!0,get:function(){return i.scales}}),Object.defineProperty(e,"chords",{enumerable:!0,get:function(){return i.chords}}),Object.defineProperty(e,"scale",{enumerable:!0,get:function(){return i.scale}}),Object.defineProperty(e,"mode",{enumerable:!0,get:function(){return i.scale}}),Object.defineProperty(e,"chord",{enumerable:!0,get:function(){return i.chord}});var a=n(76);Object.defineProperty(e,"clip",{enumerable:!0,get:function(){return a.clip}});var s=n(514);Object.defineProperty(e,"getChordDegrees",{enumerable:!0,get:function(){return s.getChordDegrees}}),Object.defineProperty(e,"getChordsByProgression",{enumerable:!0,get:function(){return s.getChordsByProgression}}),Object.defineProperty(e,"progression",{enumerable:!0,get:function(){return s.progression}});var c=n(716);Object.defineProperty(e,"arp",{enumerable:!0,get:function(){return c.arp}});var u=n(884);Object.defineProperty(e,"midi",{enumerable:!0,get:function(){return u.midi}});var l=n(185);Object.defineProperty(e,"Session",{enumerable:!0,get:function(){return l.Session}}),o(n(491),e)},126(t,e,n){var r=this&&this.__assign||function(){return r=Object.assign||function(t){for(var e,n=1,r=arguments.length;n<r;n++)for(var o in e=arguments[n])Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o]);return t},r.apply(this,arguments)},o=this&&this.__rest||function(t,e){var n={};for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&e.indexOf(r)<0&&(n[r]=t[r]);if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(r=Object.getOwnPropertySymbols(t);o<r.length;o++)e.indexOf(r[o])<0&&Object.prototype.propertyIsEnumerable.call(t,r[o])&&(n[r[o]]=t[r[o]])}return n};Object.defineProperty(e,"__esModule",{value:!0}),e.Channel=void 0;var i=n(76),a=n(552),s=function(t){var e=Tone.Transport.position.split(":");if("0"===e[0]&&"0"===e[1])return 0;var n=Tone.Transport.ticks,r=(null==t?void 0:t.align)||"1m",o=(null==t?void 0:t.alignOffset)||"0",i=Tone.Ticks(r).toTicks(),a=Tone.Ticks(o).toTicks();return Tone.Ticks(Math.floor(n/i+1)*i+a)},c=function(){function t(t){var e=this;this.idx=t.idx||0,this.name=t.name||"ch "+t.idx,this.activePatternIdx=-1,this.channelClips=[],this.clipNoteCount=0,t.clips,t.samples,t.sample,t.synth;var n=o(t,["clips","samples","sample","synth"]),i=(n.external,n.sampler,n.buffer,o(n,["external","sampler","buffer"])),s=(i.player,i.instrument,i.volume,o(i,["player","instrument","volume"])),c=s.eventCb,u=s.playerCb,l=(s.effects,o(s,["eventCb","playerCb","effects"])),h=l.context,p=void 0===h?Tone.getContext():h,d=o(l,["context"]);this.eventCbFn=c,this.playerCbFn=u,this.hasLoaded=!1,this.hasFailed=!1,this.initializerTask=this.initOutputProducer(p,t).then(function(){return e.initInstrument(p,t).then(function(){return e.adjustInstrument(p,t).then(function(){return e.initEffects(p,t)})})});var f=!1;try{t.clips.forEach(function(t,n){try{e.addClip(r(r({},t),d))}catch(t){throw new Error("".concat((0,a.errorHasMessage)(t)?t.message:t," in clip ").concat(n+1))}},this)}catch(t){f=t}this.initializerTask.then(function(){if(f)throw f;e.hasLoaded=!0,e.eventCb("loaded",{})}).catch(function(t){e.hasFailed=t,e.eventCb("error",{e:t})})}return t.setTransportTempo=function(t){Tone.Transport.bpm.value=t},t.startTransport=function(){Tone.start(),Tone.Transport.start()},t.stopTransport=function(t){void 0===t&&(t=!0),Tone.Transport.stop(),t&&Tone.Transport.cancel()},t.prototype.setVolume=function(t){var e,n;this.instrument&&(this.instrument.volume.value=t),this.external&&(null===(n=(e=this.external).setVolume)||void 0===n||n.call(e,t))},t.prototype.startClip=function(t,e){var n=this,r=this.channelClips[t];e=e||(0===e?0:s(r)),this.activePatternIdx>-1&&this.activePatternIdx!==t&&this.stopClip(this.activePatternIdx,e),r&&"started"!==r.state&&(this.counterResetTask=Tone.Transport.scheduleOnce(function(){n.clipNoteCount=0},e),this.activePatternIdx=t,null==r||r.start(e))},t.prototype.stopClip=function(t,e){var n=this.channelClips[t];e=e||(0===e?0:s(n)),null==n||n.stop(e),t===this.activePatternIdx&&(this.activePatternIdx=-1)},t.prototype.addClip=function(t,e){var n=this;e=e||this.channelClips.length,t.pattern?(this.channelClips[e]=(0,i.clip)(r({},t),this),["align","alignOffset"].forEach(function(r){t[r]&&(n.channelClips[e][r]=t[r])})):this.channelClips[e]=null},t.prototype.getSeqFn=function(t){var e=this;return this.external?function(n,r){var o,a;if("x"===r||"R"===r){var s=e.clipNoteCount;if(e.hasLoaded){var c=(0,i.getNote)(r,t,s)[0],u=(0,i.getDuration)(t,s),l=Tone.Time(u).toSeconds();e.playerCb({note:c,duration:u,time:n,counter:s});try{null===(a=(o=e.external).triggerAttackRelease)||void 0===a||a.call(o,c,l,n)}catch(t){e.eventCb("error",{e:t})}}e.clipNoteCount++}}:this.instrument instanceof Tone.Player?function(t,n){if("x"===n||"R"===n){var r=e.clipNoteCount;if(e.hasLoaded){e.playerCb({note:"",duration:"",time:t,counter:r});try{e.instrument.start(t)}catch(t){e.eventCb("error",{e:t})}}e.clipNoteCount++}}:this.instrument instanceof Tone.PolySynth||this.instrument instanceof Tone.Sampler?function(n,r){if("x"===r||"R"===r){var o=e.clipNoteCount;if(e.hasLoaded){var a=(0,i.getNote)(r,t,o),s=(0,i.getDuration)(t,o);e.playerCb({note:a,duration:s,time:n,counter:o});try{e.instrument.triggerAttackRelease(a,s,n)}catch(t){e.eventCb("error",{e:t})}}e.clipNoteCount++}}:this.instrument instanceof Tone.NoiseSynth?function(n,r){if("x"===r||"R"===r){var o=e.clipNoteCount;if(e.hasLoaded){var a=(0,i.getDuration)(t,o);e.playerCb({note:"",duration:a,time:n,counter:o});try{e.instrument.triggerAttackRelease(a,n)}catch(t){e.eventCb("error",{e:t})}}e.clipNoteCount++}}:function(n,r){if("x"===r||"R"===r){var o=e.clipNoteCount;if(e.hasLoaded){var a=(0,i.getNote)(r,t,o)[0],s=(0,i.getDuration)(t,o);e.playerCb({note:a,duration:s,time:n,counter:o});try{e.instrument.triggerAttackRelease(a,s,n)}catch(t){e.eventCb("error",{e:t})}}e.clipNoteCount++}}},t.prototype.eventCb=function(t,e){"function"==typeof this.eventCbFn&&(e.channel=this,this.eventCbFn(t,e))},t.prototype.playerCb=function(t){"function"==typeof this.playerCbFn&&(t.channel=this,this.playerCbFn(t))},t.prototype.checkToneObjLoaded=function(t,e){var n=this,r=t instanceof Tone.Sampler;if("loaded"in t){if(t.loaded)return void e();if(r)return;var o=!1;if(["buffer","_buffer","_buffers"].forEach(function(r){r in t&&(n.checkToneObjLoaded(t[r],e),o=!0)}),o)return}if(t instanceof Tone.ToneAudioBuffer||t instanceof Tone.ToneBufferSource||"loaded"in t&&"onload"in t){var i=t.onload;t.onload=function(){i&&"function"==typeof i&&(t.onload=i,i()),e()}}else e()},t.prototype.recreateToneObjectInContext=function(t,e){var n=this;return e=e||Tone.getContext(),new Promise(function(o,i){if(t instanceof Tone.PolySynth){var a=Tone.PolySynth(Tone[t._dummyVoice.name],r(r({},t.get()),{context:e}));n.checkToneObjLoaded(a,function(){return o(a)})}else if(t instanceof Tone.Player)var s=Tone.Player({url:t._buffer,context:e,onload:function(){return n.checkToneObjLoaded(s,function(){return o(s)})}});else if(t instanceof Tone.Sampler)var c=t.get(),u={attack:c.attack,curve:c.curve,release:c.release,volume:c.volume},l={baseUrl:t._buffers.baseUrl,urls:Object.fromEntries(t._buffers._buffers.entries())},h=Tone.Sampler(r(r(r({},u),l),{context:e,onload:function(){return n.checkToneObjLoaded(h,function(){return o(h)})}}));else{var p=Tone[t.name](r(r({},t.get()),{context:e,onload:function(){return n.checkToneObjLoaded(p,function(){return o(p)})}}));n.checkToneObjLoaded(p,function(){return o(p)})}})},t.prototype.initOutputProducer=function(t,e){var n=this;return t=t||Tone.getContext(),new Promise(function(o,i){if(e.synth){if(e.instrument)throw new Error("Either synth or instrument can be provided, but not both.");if(e.synth.synth){var a=e.synth.synth,s=e.synth.preset||{};n.instrument=new Tone[a](r(r({},s),{context:t,onload:function(){return n.checkToneObjLoaded(n.instrument,o)}})),n.checkToneObjLoaded(n.instrument,o)}else n.instrument=e.synth,console.warn('The "synth" parameter with instrument will be deprecated in the future. Please use the "instrument" parameter instead.'),n.checkToneObjLoaded(n.instrument,o)}else if("string"==typeof e.instrument)n.instrument=new Tone[e.instrument]({context:t}),n.checkToneObjLoaded(n.instrument,o);else if(e.instrument)n.instrument=e.instrument,n.checkToneObjLoaded(n.instrument,o);else if(e.sample||e.buffer)n.instrument=new Tone.Player({url:e.sample||e.buffer,context:t,onload:function(){return n.checkToneObjLoaded(n.instrument,o)}});else if(e.samples)n.instrument=new Tone.Sampler({urls:e.samples,context:t,onload:function(){return n.checkToneObjLoaded(n.instrument,o)}});else if(e.sampler)n.instrument=e.sampler,n.checkToneObjLoaded(n.instrument,o);else if(e.player)n.instrument=e.player,n.checkToneObjLoaded(n.instrument,o);else{if(!e.external)throw new Error("One of required synth|instrument|sample|sampler|samples|buffer|player|external is not provided!");if(n.external=r({},e.external),n.instrument={context:t,volume:{value:0}},e.external.init)return e.external.init(t.rawContext).then(function(){o()}).catch(function(t){var e;i(new Error("".concat(t.message," loading external output module of channel idx ").concat(n.idx,", ").concat(null!==(e=n.name)&&void 0!==e?e:"(no name)")))});o()}if(!n.instrument)throw new Error("Failed instantiating instrument from given params.")})},t.prototype.initInstrument=function(t,e){var n,r=this;return t=t||Tone.getContext(),e.external||(null===(n=this.instrument)||void 0===n?void 0:n.context)===t?new Promise(function(t,e){t()}):this.recreateToneObjectInContext(this.instrument,t).then(function(t){r.instrument=t})},t.prototype.adjustInstrument=function(t,e){var n=this;return t=t||Tone.getContext(),new Promise(function(t,r){e.volume&&n.setVolume(e.volume),t()})},t.prototype.initEffects=function(t,e){var n=this;t=t||Tone.getContext();var r,o=function(t){return"function"==typeof t.start?t.start():t},i=(r=e.effects)?Array.isArray(r)?r:[r]:[];if(e.external){if(0!==i.length)throw new Error("Effects cannot be used with external output");return Promise.resolve()}return Promise.all(i.map(function(e){return new Promise(function(r,o){if("string"==typeof e)r(new Tone[e]({context:t}));else{if(e.context!==t)return n.recreateToneObjectInContext(e,t);r(e)}}).then(function(t){return t.toDestination()})})).then(function(t){return t.map(o)}).then(function(t){var e;(e=n.instrument).chain.apply(e,t).toDestination()})},Object.defineProperty(t.prototype,"clips",{get:function(){return this.channelClips},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"activeClipIdx",{get:function(){return this.activePatternIdx},enumerable:!1,configurable:!0}),t}();e.Channel=c},884(t,e,n){var r=this&&this.__importDefault||function(t){return t&&t.__esModule?t:{default:t}};Object.defineProperty(e,"__esModule",{value:!0}),e.midi=void 0;var o=r(n(383)),i=n(142);e.midi=function(t,e,n){void 0===e&&(e="music.mid");var r=s(t,n).toBytes();return null===e?r:(e.endsWith(".mid")||(e+=".mid"),"undefined"!=typeof window&&window.URL&&"function"==typeof window.URL.createObjectURL?a(r,e):(o.default.writeFileSync(e,r,"binary"),void console.log("MIDI file generated: ".concat(e,"."))))};var a=function(t,e){for(var n=new Uint8Array(t.length),r=0;r<t.length;r++){var o=t.charCodeAt(r);n[r]=o}var i=new Blob([n],{type:"audio/midi"}),a=document.createElement("a");return a.href="undefined"!=typeof window&&void 0!==window.URL&&void 0!==window.URL.createObjectURL&&window.URL.createObjectURL(i)||"",a.download=e,a.innerText="Download MIDI file",a},s=function(t,e){var n=new i.File,r=new i.Track;"number"==typeof e&&r.setTempo(e),n.addTrack(r);for(var o=0,a=t;o<a.length;o++){var s=a[o],c=s.level||127;s.note?"string"==typeof s.note?(r.noteOn(0,s.note,s.length,c),r.noteOff(0,s.note,s.length,c)):r.addChord(0,s.note,s.length,c):r.noteOff(0,"",s.length)}return n}},514(t,e,n){Object.defineProperty(e,"__esModule",{value:!0}),e.progression=e.getChordsByProgression=e.getChordDegrees=void 0;var r=n(712),o=n(552);e.getChordDegrees=function(t){var e={ionian:["I","ii","iii","IV","V","vi","vii°"],dorian:["i","ii","III","IV","v","vi°","VII"],phrygian:["i","II","III","iv","v°","VI","vii"],lydian:["I","II","iii","iv°","V","vi","vii"],mixolydian:["I","ii","iii°","IV","v","vi","VII"],aeolian:["i","ii°","III","iv","v","VI","VII"],locrian:["i°","II","iii","iv","V","VI","vii"],"melodic minor":["i","ii","III+","IV","V","vi°","vii°"],"harmonic minor":["i","ii°","III+","iv","V","VI","vii°"]};return e.major=e.ionian,e.minor=e.aeolian,e[t]||[]};var i={i:0,ii:1,iii:2,iv:3,v:4,vi:5,vii:6};e.getChordsByProgression=function(t,e){var n=t.split(" ");n[0].match(/\d/)||(n[0]+="4",t=n.join(" "));var o=(0,r.scale)(t);return e.replace(/\s*,+\s*/g," ").split(" ").map(function(t){var e=function(t){var e=t.replace(/\W/g,""),n="M";return e.toLowerCase()===e&&(n="m"),t.indexOf("°")>-1?n+"7b5":t.indexOf("+")>-1?n+"#5":t.indexOf("7")>-1?"M"===n?"maj7":"m7":n}(t),n=i[t.replace(/\W|\d/g,"").toLowerCase()],r=o[n],a=r.replace(/\D+/,"");return r.replace(/\d/,"")+e+"_"+a}).toString().replace(/,/g," ")};var a=function(t){var e=t.T,n=t.P,r=t.D;return function(t){void 0===t&&(t=4);var i=[];i.push((0,o.pickOne)(e));var a=1;for(a<t-1&&(i.push((0,o.pickOne)(n)),a++),a<t-1&&(0,o.dice)()&&(i.push((0,o.pickOne)(n)),a++),a<t-1&&(i.push((0,o.pickOne)(r)),a++),a<t-1&&(i.push((0,o.pickOne)(n)),a++),a<t-1&&(i.push((0,o.pickOne)(r)),a++),a<t-1&&(0,o.dice)()&&(i.push((0,o.pickOne)(n)),a++);a<t;)i.push((0,o.pickOne)(r)),a++;return i}},s=a({T:["I","vi"],P:["ii","IV"],D:["V"]}),c=a({T:["i","VI"],P:["ii","iv"],D:["V"]});e.progression=function(t,e){return void 0===e&&(e=4),"major"===t||"M"===t?s(e):"minor"===t||"m"===t?c(e):[]}},185(t,e,n){Object.defineProperty(e,"__esModule",{value:!0}),e.Session=void 0;var r=n(126),o=function(){function t(t){var e=this;t=t||[],this.sessionChannels=t.map(function(t,n){return t.idx=t.idx||n,t.idx=e.uniqueIdx(e.sessionChannels,t.idx),new r.Channel(t)})}return t.prototype.uniqueIdx=function(t,e){if(!t)return e||0;var n=t.reduce(function(t,e){return!t.find(function(t){return t===e.idx})&&t.concat(e.idx)||t},[]);if(!e||n.find(function(t){return t===e})){for(var r=t.length;n.find(function(t){return t===r});)r+=1;return r}return e},t.prototype.createChannel=function(t){t.idx=this.uniqueIdx(this.sessionChannels,t.idx);var e=new r.Channel(t);return this.sessionChannels.push(e),e},Object.defineProperty(t.prototype,"channels",{get:function(){return this.sessionChannels},enumerable:!1,configurable:!0}),t.prototype.setTransportTempo=function(t){r.Channel.setTransportTempo(t)},t.prototype.startTransport=function(){r.Channel.startTransport()},t.prototype.stopTransport=function(t){void 0===t&&(t=!0),r.Channel.stopTransport(t)},t.prototype.startRow=function(t){this.sessionChannels.forEach(function(e){e.startClip(t)})},t.prototype.play=function(t){var e=this,n=t.channelPatterns,r=t.clipDuration||"4:0:0",o=Tone.Time(r).toSeconds(),i=function(t,e){t.forEach(function(t){return t.stop(e)})};n.forEach(function(t){var n=t.channelIdx,r=t.pattern,a=[],s=0,c="-";r.split("").forEach(function(t){t!==c&&"_"!==t&&(i(a,s),a=function(t,n,r){return"-"===n?[]:e.channels.filter(function(e){return e.idx===t}).map(function(t){var e;return null===(e=t.clips[n])||void 0===e?void 0:e.start(r)})}(n,t,s)),c=t,s+=o}),i(a,s)})},t}();e.Session=o},491(t,e){Object.defineProperty(e,"__esModule",{value:!0})},552(t,e,n){Object.defineProperty(e,"__esModule",{value:!0}),e.randomInt=e.convertChordsToNotes=e.convertChordToNotes=e.errorHasMessage=e.flat=e.dice=e.pickOne=e.sizzleMap=e.shuffle=e.expandStr=e.isNote=void 0;var r=n(712);e.isNote=function(t){return/^[a-gA-G](?:#|b)?\d$/.test(t)},e.expandStr=function(t){return t=(t=(t=(t=JSON.stringify(t.split(""))).replace(/,"\[",/g,", [")).replace(/"\[",/g,"[")).replace(/,"\]"/g,"]"),JSON.parse(t)},e.shuffle=function(t,e){void 0===e&&(e=!0);var n=t.length-1;return t.forEach(function(r,o){if(!(o>=n)){var i=e?Math.floor(Math.random()*(n-o))+1+o:Math.floor(Math.random()*(n+1-o))+o;t[o]=t[i],t[i]=r}}),t},e.sizzleMap=function(t){void 0===t&&(t=127);var e=Math.PI,n=[e/6,e/4,e/3,e/2,2*e/3,3*e/4,5*e/6,e],r=[0,e/6,e/4,e/3,e/2,2*e/3,3*e/4,5*e/6];return r.reverse(),n.concat(r).map(function(e){return Math.round(Math.sin(e)*t)})},e.pickOne=function(t){return t.length>1?t[Math.round(Math.random())]:t[0]},e.dice=function(){return!!Math.round(Math.random())},e.flat=function(t){return t.reduce(function(t,e){return t.concat(e)},[])},e.errorHasMessage=function(t){return"string"==typeof t.message},e.convertChordToNotes=function(t){var e,n,o,i;try{e=(0,r.inlineChord)(t)}catch(t){o=t}try{n=(0,r.chord)(t.replace(/_/g," "))}catch(t){i=t}if(!o&&!i){if(e.toString()!==n.toString())throw new Error("Chord ".concat(t," cannot decode, guessing ").concat(e," or ").concat(n));return e}return o?i?(0,r.chord)(t):n:e},e.convertChordsToNotes=function(t){if("string"==typeof t&&(0,e.isNote)(t))return[t];if(Array.isArray(t))return t.forEach(function(t){if(Array.isArray(t))t.forEach(function(t){if("string"!=typeof t||!(0,e.isNote)(t))throw new TypeError("array of arrays must comprise valid notes")});else if("string"!=typeof t||!(0,e.isNote)(t))throw new TypeError("array must comprise valid notes")}),t;if(!Array.isArray(t)){var n=(0,e.convertChordToNotes)(t);if(n&&n.length)return n}throw new Error("Chord ".concat(t," not found"))},e.randomInt=function(t){return void 0===t&&(t=1),Math.round(Math.random()*t)}},383(t){t.exports=require("fs")},142(t,e){Object.defineProperty(e,"__esModule",{value:!0});var n={a:21,b:23,c:12,d:14,e:16,f:17,g:19},r={12:"c",13:"c#",14:"d",15:"d#",16:"e",17:"f",18:"f#",19:"g",20:"g#",21:"a",22:"a#",23:"b"},o={"a#":"bb","c#":"db","d#":"eb","f#":"gb","g#":"ab"};function i(t){const e=/([a-g])(#+|b+)?([0-9]+)$/i.exec(t);if(!e)throw new Error(`Invalid note name: ${t}`);const r=e[1].toLowerCase(),o=e[2]||"";return 12*parseInt(e[3],10)+n[r]+("#"===o.substring(0,1)?1:-1)*o.length}function a(t){return"number"!=typeof t&&/[^0-9]/.test(t)?i(t):parseInt(String(t),10)}function s(t){let e=Math.floor(6e7/t);const n=[];do{n.unshift(255&e),e>>=8}while(e);for(;n.length<3;)n.push(0);return n}function c(t){return String.fromCharCode(...t)}function u(t,e){let n=t;if(e)for(;n.length/2<e;)n="0"+n;const r=[];for(let t=n.length-1;t>=0;t-=2){const e=0===t?n[t]:n[t-1]+n[t];r.unshift(parseInt(e,16))}return r}function l(t){let e=127&t,n=t>>7;for(;n;)e<<=8,e|=127&n|128,n>>=7;const r=[];for(;r.push(255&e),128&e;)e>>=8;return r}var h={midi_letter_pitches:n,midi_pitches_letter:r,midi_flattened_notes:o,midiPitchFromNote:i,ensureMidiPitch:a,noteFromMidiPitch:function(t,e){let n=0,i=t;t>23&&(n=Math.floor(t/12)-1,i=t-12*n);let a=r[String(i)];if(!a)throw new Error(`Invalid MIDI pitch: ${t}`);return e&&a.indexOf("#")>0&&(a=o[a]),a+n},mpqnFromBpm:s,bpmFromMpqn:function(t){let e;if(Array.isArray(t)){e=0;for(let n=0,r=t.length-1;r>=0;++n,--r)e|=t[n]<<8*r}else e=t;return Math.floor(6e7/e)},codes2Str:c,str2Bytes:u,translateTickTime:l},p={NOTE_OFF:128,NOTE_ON:144,AFTER_TOUCH:160,CONTROLLER:176,PROGRAM_CHANGE:192,CHANNEL_AFTERTOUCH:208,PITCH_BEND:224},d={SEQUENCE:0,TEXT:1,COPYRIGHT:2,TRACK_NAME:3,INSTRUMENT:4,LYRIC:5,MARKER:6,CUE_POINT:7,CHANNEL_PREFIX:32,END_OF_TRACK:47,TEMPO:81,SMPTE:84,TIME_SIG:88,KEY_SIG:89,SEQ_EVENT:127},f=class t{static NOTE_OFF=p.NOTE_OFF;static NOTE_ON=p.NOTE_ON;static AFTER_TOUCH=p.AFTER_TOUCH;static CONTROLLER=p.CONTROLLER;static PROGRAM_CHANGE=p.PROGRAM_CHANGE;static CHANNEL_AFTERTOUCH=p.CHANNEL_AFTERTOUCH;static PITCH_BEND=p.PITCH_BEND;time;type;channel;param1;param2;constructor(t){this.setTime(t.time),this.setType(t.type),this.setChannel(t.channel),this.setParam1(t.param1),void 0!==t.param2&&this.setParam2(t.param2)}setTime(t){this.time=l(t||0)}setType(e){if(e<t.NOTE_OFF||e>t.PITCH_BEND)throw new Error("Trying to set an unknown event: "+e);this.type=e}setChannel(t){if(t<0||t>15)throw new Error("Channel is out of bounds.");this.channel=t}setParam1(t){this.param1=t}setParam2(t){this.param2=t}toBytes(){const t=[],e=this.type|15&this.channel;return t.push(...this.time),t.push(e),t.push(this.param1),void 0!==this.param2&&null!==this.param2&&t.push(this.param2),t}},m=class{static SEQUENCE=d.SEQUENCE;static TEXT=d.TEXT;static COPYRIGHT=d.COPYRIGHT;static TRACK_NAME=d.TRACK_NAME;static INSTRUMENT=d.INSTRUMENT;static LYRIC=d.LYRIC;static MARKER=d.MARKER;static CUE_POINT=d.CUE_POINT;static CHANNEL_PREFIX=d.CHANNEL_PREFIX;static END_OF_TRACK=d.END_OF_TRACK;static TEMPO=d.TEMPO;static SMPTE=d.SMPTE;static TIME_SIG=d.TIME_SIG;static KEY_SIG=d.KEY_SIG;static SEQ_EVENT=d.SEQ_EVENT;time;type;data;constructor(t){this.time=l(0),this.type=0,this.setTime(t.time),this.setType(t.type),this.setData(t.data)}setTime(t){this.time=l(t||0)}setType(t){this.type=t}setData(t){this.data=t}toBytes(){if(!this.type)throw new Error("Type for meta-event not specified.");const t=[];if(t.push(...this.time),t.push(255,this.type),Array.isArray(this.data))t.push(this.data.length),t.push(...this.data);else if("number"==typeof this.data)t.push(1,this.data);else if(null!==this.data&&void 0!==this.data){t.push(this.data.length);const e=this.data.split("").map(t=>t.charCodeAt(0));t.push(...e)}else t.push(0);return t}},y=class t{static START_BYTES=[77,84,114,107];static END_BYTES=[0,255,47,0];events;constructor(t){this.events=t?.events??[]}addEvent(t){return this.events.push(t),this}addNoteOn(t,e,n,r){return this.events.push(new f({type:f.NOTE_ON,channel:t,param1:a(e),param2:r||90,time:n||0})),this}addNoteOff(t,e,n,r){return this.events.push(new f({type:f.NOTE_OFF,channel:t,param1:a(e),param2:r||90,time:n||0})),this}addNote(t,e,n,r,o){return this.addNoteOn(t,e,r,o),n&&this.addNoteOff(t,e,n,o),this}addChord(t,e,n,r){if(!Array.isArray(e)||0===e.length)throw new Error("Chord must be a non-empty array of pitches");return e.forEach(e=>{this.addNoteOn(t,e,0,r)}),e.forEach((e,r)=>{0===r?this.addNoteOff(t,e,n):this.addNoteOff(t,e)}),this}setInstrument(t,e,n){return this.events.push(new f({type:f.PROGRAM_CHANGE,channel:t,param1:e,time:n||0})),this}setTempo(t,e){return this.events.push(new m({type:m.TEMPO,data:s(t),time:e||0})),this}setTimeSignature(t,e,n){const r=Math.log2(e);if(r!==Math.floor(r))throw new Error("Time signature denominator must be an exact power of 2!");return this.events.push(new m({type:m.TIME_SIG,data:[255&t,255&Math.floor(r),24,8],time:n||0})),this}setKeySignature(t,e,n){return this.events.push(new m({type:m.KEY_SIG,data:[255&t,e?1:0],time:n||0})),this}toBytes(){let e=0;const n=[],r=t.START_BYTES,o=t.END_BYTES;this.events.forEach(t=>{const r=t.toBytes();e+=r.length,n.push(...r)}),e+=o.length;const i=u(e.toString(16),4);return r.concat(i,n,o)}};y.prototype.noteOn=y.prototype.addNoteOn,y.prototype.noteOff=y.prototype.addNoteOff,y.prototype.note=y.prototype.addNote,y.prototype.chord=y.prototype.addChord,y.prototype.instrument=y.prototype.setInstrument,y.prototype.tempo=y.prototype.setTempo,y.prototype.timeSignature=y.prototype.setTimeSignature,y.prototype.keySignature=y.prototype.setKeySignature;var v=class t{static HDR_CHUNKID="MThd";static HDR_CHUNK_SIZE="\0\0\0";static HDR_TYPE0="\0\0";static HDR_TYPE1="\0";ticks;tracks;constructor(t){const e=t||{};if(void 0!==e.ticks){if("number"!=typeof e.ticks)throw new Error("Ticks per beat must be a number!");if(e.ticks<=0||e.ticks>=32768||e.ticks%1!=0)throw new Error("Ticks per beat must be an integer between 1 and 32767!")}this.ticks=e.ticks||128,this.tracks=[]}addTrack(t){if(t)return this.tracks.push(t),this;{const t=new y;return this.tracks.push(t),t}}toBytes(){const e=this.tracks.length.toString(16);let n=t.HDR_CHUNKID+t.HDR_CHUNK_SIZE;return parseInt(e,16)>1?n+=t.HDR_TYPE1:n+=t.HDR_TYPE0,n+=c(u(e,2)),n+=String.fromCharCode(this.ticks/256,this.ticks%256),this.tracks.forEach(t=>{n+=c(t.toBytes())}),n}toUint8Array(){const t=this.toBytes(),e=new Uint8Array(t.length);for(let n=0;n<t.length;n++)e[n]=t.charCodeAt(n);return e}toBlob(t){return new Blob([this.toUint8Array()],{type:t?"application/octet-stream":"audio/x-midi"})}},b={Util:h,File:v,Track:y,Event:f,MetaEvent:m,DEFAULT_VOLUME:90,DEFAULT_DURATION:128,DEFAULT_CHANNEL:0};e.DEFAULT_CHANNEL=0,e.DEFAULT_DURATION=128,e.DEFAULT_VOLUME=90,e.Event=f,e.File=v,e.MetaEvent=m,e.MetaEventType=d,e.MidiEvent=f,e.MidiEventType=p,e.Track=y,e.Util=h,e.default=b}},e={},n=function n(r){var o=e[r];if(void 0!==o)return o.exports;var i=e[r]={exports:{}};return t[r].call(i.exports,i,i.exports,n),i.exports}(376),r=exports;for(var o in n)r[o]=n[o];n.__esModule&&Object.defineProperty(r,"__esModule",{value:!0})})();
|
|
1
|
+
// src/browser-index.ts
|
|
2
|
+
import { chord as chord2, chords, scale as scale2, scales } from "harmonics";
|
|
3
|
+
|
|
4
|
+
// src/arp.ts
|
|
5
|
+
import { inlineChord as inlineChord2 } from "harmonics";
|
|
6
|
+
|
|
7
|
+
// src/utils.ts
|
|
8
|
+
import { chord, inlineChord } from "harmonics";
|
|
9
|
+
var isNote = (str) => /^[a-gA-G](?:#|b)?\d$/.test(str);
|
|
10
|
+
var expandStr = (str) => {
|
|
11
|
+
str = JSON.stringify(str.split(""));
|
|
12
|
+
str = str.replace(/,"\[",/g, ", [");
|
|
13
|
+
str = str.replace(/"\[",/g, "[");
|
|
14
|
+
str = str.replace(/,"\]"/g, "]");
|
|
15
|
+
return JSON.parse(str);
|
|
16
|
+
};
|
|
17
|
+
var shuffle = (arr, fullShuffle = true) => {
|
|
18
|
+
const lastIndex = arr.length - 1;
|
|
19
|
+
arr.forEach((el, idx) => {
|
|
20
|
+
if (idx >= lastIndex) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
const rnd = fullShuffle ? (
|
|
24
|
+
// Pick random number from idx+1 to lastIndex (Modified algorithm, (N-1)! combinations)
|
|
25
|
+
// Math.random -> [0, 1) -> [0, lastIndex-idx ) --floor-> [0, lastIndex-idx-1]
|
|
26
|
+
// rnd = [0, lastIndex-idx-1] + 1 + idx = [1 + idx, lastIndex]
|
|
27
|
+
// (Original algorithm would pick rnd = [idx, lastIndex], thus any element could arrive back into its slot)
|
|
28
|
+
Math.floor(Math.random() * (lastIndex - idx)) + 1 + idx
|
|
29
|
+
) : (
|
|
30
|
+
// Pick random number from idx to lastIndex (Unmodified Richard Durstenfeld, N! combinations)
|
|
31
|
+
Math.floor(Math.random() * (lastIndex + 1 - idx)) + idx
|
|
32
|
+
);
|
|
33
|
+
arr[idx] = arr[rnd];
|
|
34
|
+
arr[rnd] = el;
|
|
35
|
+
});
|
|
36
|
+
return arr;
|
|
37
|
+
};
|
|
38
|
+
var pickOne = (arr) => arr[Math.floor(Math.random() * arr.length)];
|
|
39
|
+
var dice = () => !!Math.round(Math.random());
|
|
40
|
+
var errorHasMessage = (x) => {
|
|
41
|
+
return typeof x === "object" && x !== null && "message" in x && typeof x.message === "string";
|
|
42
|
+
};
|
|
43
|
+
var convertChordToNotes = (el) => {
|
|
44
|
+
let c1;
|
|
45
|
+
let c2;
|
|
46
|
+
let e1;
|
|
47
|
+
let e2;
|
|
48
|
+
try {
|
|
49
|
+
c1 = inlineChord(el);
|
|
50
|
+
} catch (e) {
|
|
51
|
+
e1 = e;
|
|
52
|
+
}
|
|
53
|
+
try {
|
|
54
|
+
c2 = chord(el.replace(/_/g, " "));
|
|
55
|
+
} catch (e) {
|
|
56
|
+
e2 = e;
|
|
57
|
+
}
|
|
58
|
+
if (!e1 && !e2) {
|
|
59
|
+
if (c1.toString() !== c2.toString()) {
|
|
60
|
+
throw new Error(`Chord ${el} cannot decode, guessing ${c1} or ${c2}`);
|
|
61
|
+
}
|
|
62
|
+
return c1;
|
|
63
|
+
}
|
|
64
|
+
if (!e1) {
|
|
65
|
+
return c1;
|
|
66
|
+
}
|
|
67
|
+
if (!e2) {
|
|
68
|
+
return c2;
|
|
69
|
+
}
|
|
70
|
+
return chord(el);
|
|
71
|
+
};
|
|
72
|
+
var convertChordsToNotes = (el) => {
|
|
73
|
+
if (typeof el === "string" && isNote(el)) {
|
|
74
|
+
return [el];
|
|
75
|
+
}
|
|
76
|
+
if (Array.isArray(el)) {
|
|
77
|
+
el.forEach((n) => {
|
|
78
|
+
if (Array.isArray(n)) {
|
|
79
|
+
n.forEach((n1) => {
|
|
80
|
+
if (typeof n1 !== "string" || !isNote(n1)) {
|
|
81
|
+
throw new TypeError("array of arrays must comprise valid notes");
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
} else if (typeof n !== "string" || !isNote(n)) {
|
|
85
|
+
throw new TypeError("array must comprise valid notes");
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
return el;
|
|
89
|
+
}
|
|
90
|
+
if (!Array.isArray(el)) {
|
|
91
|
+
const c = convertChordToNotes(el);
|
|
92
|
+
if (c?.length) {
|
|
93
|
+
return c;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
throw new Error(`Chord ${el} not found`);
|
|
97
|
+
};
|
|
98
|
+
var randomInt = (num = 1) => Math.round(Math.random() * num);
|
|
99
|
+
|
|
100
|
+
// src/arp.ts
|
|
101
|
+
var DEFAULT_OCTAVE = 4;
|
|
102
|
+
var fillArr = (arr, len) => {
|
|
103
|
+
const bumpOctave = (el) => {
|
|
104
|
+
if (!el) {
|
|
105
|
+
throw new Error("Empty element");
|
|
106
|
+
}
|
|
107
|
+
const note = el.replace(/\d/, "");
|
|
108
|
+
const oct = el.replace(/\D/g, "") || DEFAULT_OCTAVE;
|
|
109
|
+
if (!note) {
|
|
110
|
+
throw new Error("Incorrect note");
|
|
111
|
+
}
|
|
112
|
+
return note + (+oct + 1);
|
|
113
|
+
};
|
|
114
|
+
const arr1 = arr.map(bumpOctave);
|
|
115
|
+
const arr2 = arr1.map(bumpOctave);
|
|
116
|
+
const finalArr = [...arr, ...arr1, ...arr2];
|
|
117
|
+
return finalArr.slice(0, len);
|
|
118
|
+
};
|
|
119
|
+
var arp = (chordsOrParams) => {
|
|
120
|
+
let finalArr = [];
|
|
121
|
+
const params = {
|
|
122
|
+
count: 4,
|
|
123
|
+
order: "0123",
|
|
124
|
+
chords: ""
|
|
125
|
+
};
|
|
126
|
+
if (typeof chordsOrParams === "string") {
|
|
127
|
+
params.chords = chordsOrParams;
|
|
128
|
+
} else {
|
|
129
|
+
if (chordsOrParams.order?.match(/\D/g)) {
|
|
130
|
+
throw new TypeError("Invalid value for order");
|
|
131
|
+
}
|
|
132
|
+
if (chordsOrParams.count > 8 || chordsOrParams.count < 2) {
|
|
133
|
+
throw new TypeError("Invalid value for count");
|
|
134
|
+
}
|
|
135
|
+
if (chordsOrParams.count && !chordsOrParams.order) {
|
|
136
|
+
params.order = Array.from(Array(chordsOrParams.count).keys()).join("");
|
|
137
|
+
}
|
|
138
|
+
Object.assign(params, chordsOrParams);
|
|
139
|
+
}
|
|
140
|
+
if (typeof params.chords === "string") {
|
|
141
|
+
const chordsArr = params.chords.split(" ");
|
|
142
|
+
chordsArr.forEach((c, i) => {
|
|
143
|
+
try {
|
|
144
|
+
const filledArr = fillArr(inlineChord2(c), params.count);
|
|
145
|
+
const reorderedArr = params.order.split("").map((idx) => filledArr[Number(idx)]);
|
|
146
|
+
finalArr = [...finalArr, ...reorderedArr];
|
|
147
|
+
} catch (_e) {
|
|
148
|
+
throw new Error(
|
|
149
|
+
`Cannot decode chord ${i + 1} "${c}" in given "${params.chords}"`
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
} else if (Array.isArray(params.chords)) {
|
|
154
|
+
params.chords.forEach((c, i) => {
|
|
155
|
+
try {
|
|
156
|
+
const filledArr = fillArr(c, params.count);
|
|
157
|
+
const reorderedArr = params.order.split("").map((idx) => filledArr[Number(idx)]);
|
|
158
|
+
finalArr = [...finalArr, ...reorderedArr];
|
|
159
|
+
} catch (e) {
|
|
160
|
+
throw new Error(
|
|
161
|
+
`${errorHasMessage(e) ? e.message : e} in chord ${i + 1} "${c}"`
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
} else {
|
|
166
|
+
throw new TypeError("Invalid value for chords");
|
|
167
|
+
}
|
|
168
|
+
return finalArr;
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
// src/channel/instrument-factory.ts
|
|
172
|
+
function checkToneObjLoaded(toneObject, resolve) {
|
|
173
|
+
const skipRecursion = toneObject instanceof Tone.Sampler;
|
|
174
|
+
if ("loaded" in toneObject) {
|
|
175
|
+
if (toneObject.loaded) {
|
|
176
|
+
resolve();
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
if (skipRecursion) {
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
let handled = false;
|
|
183
|
+
["buffer", "_buffer", "_buffers"].forEach((key) => {
|
|
184
|
+
if (key in toneObject) {
|
|
185
|
+
checkToneObjLoaded(toneObject[key], resolve);
|
|
186
|
+
handled = true;
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
if (handled) {
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
const hasOnload = toneObject instanceof Tone.ToneAudioBuffer || toneObject instanceof Tone.ToneBufferSource || // Falback for "future" objects
|
|
194
|
+
"loaded" in toneObject && "onload" in toneObject;
|
|
195
|
+
if (!hasOnload) {
|
|
196
|
+
resolve();
|
|
197
|
+
} else {
|
|
198
|
+
const oldOnLoad = toneObject.onload;
|
|
199
|
+
toneObject.onload = () => {
|
|
200
|
+
if (oldOnLoad && typeof oldOnLoad === "function") {
|
|
201
|
+
toneObject.onload = oldOnLoad;
|
|
202
|
+
oldOnLoad();
|
|
203
|
+
}
|
|
204
|
+
resolve();
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
function recreateToneObjectInContext(toneObject, context) {
|
|
209
|
+
context = context || Tone.getContext();
|
|
210
|
+
return new Promise((resolve, _reject) => {
|
|
211
|
+
if (toneObject instanceof Tone.PolySynth) {
|
|
212
|
+
const newObj = new Tone[toneObject._dummyVoice.name]({
|
|
213
|
+
...toneObject.get(),
|
|
214
|
+
context
|
|
215
|
+
});
|
|
216
|
+
checkToneObjLoaded(newObj, () => resolve(newObj));
|
|
217
|
+
} else if (toneObject instanceof Tone.Player) {
|
|
218
|
+
const newObj = new Tone.Player({
|
|
219
|
+
url: toneObject._buffer,
|
|
220
|
+
context,
|
|
221
|
+
onload: () => checkToneObjLoaded(newObj, () => resolve(newObj))
|
|
222
|
+
});
|
|
223
|
+
} else if (toneObject instanceof Tone.Sampler) {
|
|
224
|
+
const { attack, curve, release, volume } = toneObject.get();
|
|
225
|
+
const paramsFromSampler = {
|
|
226
|
+
attack,
|
|
227
|
+
curve,
|
|
228
|
+
release,
|
|
229
|
+
volume
|
|
230
|
+
};
|
|
231
|
+
const paramsFromBuffers = {
|
|
232
|
+
baseUrl: toneObject._buffers.baseUrl,
|
|
233
|
+
urls: Object.fromEntries(toneObject._buffers._buffers.entries())
|
|
234
|
+
};
|
|
235
|
+
const newObj = new Tone.Sampler({
|
|
236
|
+
...paramsFromSampler,
|
|
237
|
+
...paramsFromBuffers,
|
|
238
|
+
context,
|
|
239
|
+
onload: () => checkToneObjLoaded(
|
|
240
|
+
newObj,
|
|
241
|
+
() => resolve(newObj)
|
|
242
|
+
)
|
|
243
|
+
});
|
|
244
|
+
} else {
|
|
245
|
+
const newObj = new Tone[toneObject.name]({
|
|
246
|
+
...toneObject.get(),
|
|
247
|
+
context,
|
|
248
|
+
onload: () => checkToneObjLoaded(newObj, () => resolve(newObj))
|
|
249
|
+
});
|
|
250
|
+
checkToneObjLoaded(newObj, () => resolve(newObj));
|
|
251
|
+
}
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
function createInstrument(context, params, channelMeta) {
|
|
255
|
+
let instrument;
|
|
256
|
+
let external;
|
|
257
|
+
context = context || Tone.getContext();
|
|
258
|
+
const loadPromise = new Promise((resolve, reject) => {
|
|
259
|
+
if (params.synth) {
|
|
260
|
+
if (params.instrument) {
|
|
261
|
+
throw new Error(
|
|
262
|
+
"Either synth or instrument can be provided, but not both."
|
|
263
|
+
);
|
|
264
|
+
}
|
|
265
|
+
if (params.synth.synth) {
|
|
266
|
+
const synthName = params.synth.synth;
|
|
267
|
+
const preset = params.synth.preset || {};
|
|
268
|
+
instrument = new Tone[synthName]({
|
|
269
|
+
...preset,
|
|
270
|
+
context,
|
|
271
|
+
// Use onload for cases when synthName calls out Tone.Sample/Player/Sampler.
|
|
272
|
+
// It could be a universal way to load Tone.js instruments.
|
|
273
|
+
onload: () => checkToneObjLoaded(instrument, resolve)
|
|
274
|
+
// This onload is ignored in all synths. Therefore we call checkToneObjLoaded() again below.
|
|
275
|
+
// It is safe to call resolve() multiple times for Promise<void>
|
|
276
|
+
});
|
|
277
|
+
checkToneObjLoaded(instrument, resolve);
|
|
278
|
+
} else {
|
|
279
|
+
instrument = params.synth;
|
|
280
|
+
console.warn(
|
|
281
|
+
'The "synth" parameter with instrument will be deprecated in the future. Please use the "instrument" parameter instead.'
|
|
282
|
+
);
|
|
283
|
+
checkToneObjLoaded(instrument, resolve);
|
|
284
|
+
}
|
|
285
|
+
} else if (typeof params.instrument === "string") {
|
|
286
|
+
instrument = new Tone[params.instrument]({ context });
|
|
287
|
+
checkToneObjLoaded(instrument, resolve);
|
|
288
|
+
} else if (params.instrument) {
|
|
289
|
+
instrument = params.instrument;
|
|
290
|
+
checkToneObjLoaded(instrument, resolve);
|
|
291
|
+
} else if (params.sample || params.buffer) {
|
|
292
|
+
instrument = new Tone.Player({
|
|
293
|
+
url: params.sample || params.buffer,
|
|
294
|
+
context,
|
|
295
|
+
onload: () => checkToneObjLoaded(instrument, resolve)
|
|
296
|
+
});
|
|
297
|
+
} else if (params.samples) {
|
|
298
|
+
instrument = new Tone.Sampler({
|
|
299
|
+
urls: params.samples,
|
|
300
|
+
context,
|
|
301
|
+
onload: () => checkToneObjLoaded(instrument, resolve)
|
|
302
|
+
});
|
|
303
|
+
} else if (params.sampler) {
|
|
304
|
+
instrument = params.sampler;
|
|
305
|
+
checkToneObjLoaded(instrument, resolve);
|
|
306
|
+
} else if (params.player) {
|
|
307
|
+
instrument = params.player;
|
|
308
|
+
checkToneObjLoaded(instrument, resolve);
|
|
309
|
+
} else if (params.external) {
|
|
310
|
+
external = { ...params.external };
|
|
311
|
+
instrument = {
|
|
312
|
+
context,
|
|
313
|
+
volume: { value: 0 }
|
|
314
|
+
};
|
|
315
|
+
if (params.external.init) {
|
|
316
|
+
return params.external.init(context.rawContext).then(() => {
|
|
317
|
+
resolve();
|
|
318
|
+
}).catch((e) => {
|
|
319
|
+
reject(
|
|
320
|
+
new Error(
|
|
321
|
+
`${errorHasMessage(e) ? e.message : e} loading external output module of channel idx ${channelMeta.idx}, ${channelMeta.name ?? "(no name)"}`
|
|
322
|
+
)
|
|
323
|
+
);
|
|
324
|
+
});
|
|
325
|
+
} else {
|
|
326
|
+
resolve();
|
|
327
|
+
}
|
|
328
|
+
} else {
|
|
329
|
+
throw new Error(
|
|
330
|
+
"One of required synth|instrument|sample|sampler|samples|buffer|player|external is not provided!"
|
|
331
|
+
);
|
|
332
|
+
}
|
|
333
|
+
if (!instrument) {
|
|
334
|
+
throw new Error("Failed instantiating instrument from given params.");
|
|
335
|
+
}
|
|
336
|
+
});
|
|
337
|
+
const initPromise = loadPromise.then(() => {
|
|
338
|
+
if (!external && instrument?.context !== context) {
|
|
339
|
+
return recreateToneObjectInContext(instrument, context).then((newObj) => {
|
|
340
|
+
instrument = newObj;
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
}).then(() => {
|
|
344
|
+
if (params.volume) {
|
|
345
|
+
instrument.volume.value = params.volume;
|
|
346
|
+
external?.setVolume?.(params.volume);
|
|
347
|
+
}
|
|
348
|
+
return instrument;
|
|
349
|
+
});
|
|
350
|
+
return { instrument, external, initPromise };
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
// src/channel/sequence-builder.ts
|
|
354
|
+
function buildSequenceCallback(params, host, playerCb, eventCb) {
|
|
355
|
+
if (host.external) {
|
|
356
|
+
const ext = host.external;
|
|
357
|
+
return (time, el) => {
|
|
358
|
+
if (el === "x" || el === "R") {
|
|
359
|
+
const counter = host.clipNoteCount;
|
|
360
|
+
if (host.hasLoaded) {
|
|
361
|
+
const note = getNote(el, params, counter)[0];
|
|
362
|
+
const duration = getDuration(params, counter);
|
|
363
|
+
const durSeconds = Tone.Time(duration).toSeconds();
|
|
364
|
+
playerCb({ note, duration, time, counter });
|
|
365
|
+
try {
|
|
366
|
+
ext.triggerAttackRelease?.(note, durSeconds, time);
|
|
367
|
+
} catch (e) {
|
|
368
|
+
eventCb("error", { e });
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
host.clipNoteCount++;
|
|
372
|
+
}
|
|
373
|
+
};
|
|
374
|
+
}
|
|
375
|
+
if (host.instrument instanceof Tone.Player) {
|
|
376
|
+
return (time, el) => {
|
|
377
|
+
if (el === "x" || el === "R") {
|
|
378
|
+
const counter = host.clipNoteCount;
|
|
379
|
+
if (host.hasLoaded) {
|
|
380
|
+
playerCb({ note: "", duration: "", time, counter });
|
|
381
|
+
try {
|
|
382
|
+
host.instrument.start(time);
|
|
383
|
+
} catch (e) {
|
|
384
|
+
eventCb("error", { e });
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
host.clipNoteCount++;
|
|
388
|
+
}
|
|
389
|
+
};
|
|
390
|
+
}
|
|
391
|
+
if (host.instrument instanceof Tone.PolySynth || host.instrument instanceof Tone.Sampler) {
|
|
392
|
+
return (time, el) => {
|
|
393
|
+
if (el === "x" || el === "R") {
|
|
394
|
+
const counter = host.clipNoteCount;
|
|
395
|
+
if (host.hasLoaded) {
|
|
396
|
+
const note = getNote(el, params, counter);
|
|
397
|
+
const duration = getDuration(params, counter);
|
|
398
|
+
playerCb({ note, duration, time, counter });
|
|
399
|
+
try {
|
|
400
|
+
host.instrument.triggerAttackRelease(note, duration, time);
|
|
401
|
+
} catch (e) {
|
|
402
|
+
eventCb("error", { e });
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
host.clipNoteCount++;
|
|
406
|
+
}
|
|
407
|
+
};
|
|
408
|
+
}
|
|
409
|
+
if (host.instrument instanceof Tone.NoiseSynth) {
|
|
410
|
+
return (time, el) => {
|
|
411
|
+
if (el === "x" || el === "R") {
|
|
412
|
+
const counter = host.clipNoteCount;
|
|
413
|
+
if (host.hasLoaded) {
|
|
414
|
+
const duration = getDuration(params, counter);
|
|
415
|
+
playerCb({ note: "", duration, time, counter });
|
|
416
|
+
try {
|
|
417
|
+
host.instrument.triggerAttackRelease(
|
|
418
|
+
duration,
|
|
419
|
+
time
|
|
420
|
+
);
|
|
421
|
+
} catch (e) {
|
|
422
|
+
eventCb("error", { e });
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
host.clipNoteCount++;
|
|
426
|
+
}
|
|
427
|
+
};
|
|
428
|
+
}
|
|
429
|
+
return (time, el) => {
|
|
430
|
+
if (el === "x" || el === "R") {
|
|
431
|
+
const counter = host.clipNoteCount;
|
|
432
|
+
if (host.hasLoaded) {
|
|
433
|
+
const note = getNote(el, params, counter)[0];
|
|
434
|
+
const duration = getDuration(params, counter);
|
|
435
|
+
playerCb({ note, duration, time, counter });
|
|
436
|
+
try {
|
|
437
|
+
host.instrument.triggerAttackRelease(note, duration, time);
|
|
438
|
+
} catch (e) {
|
|
439
|
+
eventCb("error", { e });
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
host.clipNoteCount++;
|
|
443
|
+
}
|
|
444
|
+
};
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
// src/clip-utils.ts
|
|
448
|
+
var defaultParams = {
|
|
449
|
+
notes: ["C4"],
|
|
450
|
+
pattern: "x",
|
|
451
|
+
shuffle: false,
|
|
452
|
+
sizzle: false,
|
|
453
|
+
sizzleReps: 1,
|
|
454
|
+
arpegiate: false,
|
|
455
|
+
subdiv: "4n",
|
|
456
|
+
amp: 100,
|
|
457
|
+
accentLow: 70,
|
|
458
|
+
randomNotes: null,
|
|
459
|
+
offlineRendering: false
|
|
460
|
+
};
|
|
461
|
+
var validatePattern = (pattern) => {
|
|
462
|
+
if (/[^x\-_[\]R]/.test(pattern)) {
|
|
463
|
+
throw new TypeError(
|
|
464
|
+
`pattern can only comprise x - _ [ ] R, found ${pattern}`
|
|
465
|
+
);
|
|
466
|
+
}
|
|
467
|
+
};
|
|
468
|
+
var preprocessClipParams = (params, extraDefaults) => {
|
|
469
|
+
params = { ...defaultParams, ...extraDefaults, ...params || {} };
|
|
470
|
+
if (typeof params.notes === "string") {
|
|
471
|
+
params.notes = params.notes.replace(/\s{2,}/g, " ").split(" ");
|
|
472
|
+
}
|
|
473
|
+
params.notes = params.notes ? params.notes.map(convertChordsToNotes) : [];
|
|
474
|
+
validatePattern(params.pattern);
|
|
475
|
+
if (params.shuffle) {
|
|
476
|
+
params.notes = shuffle(params.notes);
|
|
477
|
+
}
|
|
478
|
+
if (params.randomNotes && typeof params.randomNotes === "string") {
|
|
479
|
+
params.randomNotes = params.randomNotes.replace(/\s{2,}/g, " ").split(/\s/);
|
|
480
|
+
}
|
|
481
|
+
if (params.randomNotes) {
|
|
482
|
+
params.randomNotes = params.randomNotes.map(
|
|
483
|
+
convertChordsToNotes
|
|
484
|
+
);
|
|
485
|
+
}
|
|
486
|
+
return params;
|
|
487
|
+
};
|
|
488
|
+
|
|
489
|
+
// src/browser-clip.ts
|
|
490
|
+
var defaultSubdiv = "4n";
|
|
491
|
+
var defaultDur = "8n";
|
|
492
|
+
var getNote = (el, params, counter) => {
|
|
493
|
+
if (el === "R" && params.randomNotes && params.randomNotes.length > 0) {
|
|
494
|
+
return params.randomNotes[randomInt(params.randomNotes.length - 1)];
|
|
495
|
+
}
|
|
496
|
+
if (params.notes) {
|
|
497
|
+
return params.notes[counter % (params.notes.length || 1)];
|
|
498
|
+
}
|
|
499
|
+
return "";
|
|
500
|
+
};
|
|
501
|
+
var getDuration = (params, counter) => {
|
|
502
|
+
return params.durations ? params.durations[counter % params.durations.length] : params.dur || params.subdiv || defaultDur;
|
|
503
|
+
};
|
|
504
|
+
var recursivelyApplyPatternToDurations = (patternArr, length, durations = []) => {
|
|
505
|
+
patternArr.forEach((char) => {
|
|
506
|
+
if (typeof char === "string") {
|
|
507
|
+
if (char === "x" || char === "R") {
|
|
508
|
+
durations.push(length);
|
|
509
|
+
}
|
|
510
|
+
if (char === "_" && durations.length) {
|
|
511
|
+
durations[durations.length - 1] += length;
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
if (Array.isArray(char)) {
|
|
515
|
+
recursivelyApplyPatternToDurations(char, length / char.length, durations);
|
|
516
|
+
}
|
|
517
|
+
});
|
|
518
|
+
return durations;
|
|
519
|
+
};
|
|
520
|
+
var generateSequence = (params, channel, context) => {
|
|
521
|
+
context = context || Tone.getContext();
|
|
522
|
+
if (!params.pattern) {
|
|
523
|
+
throw new Error("No pattern provided!");
|
|
524
|
+
}
|
|
525
|
+
if (!params.durations && !params.dur) {
|
|
526
|
+
params.durations = recursivelyApplyPatternToDurations(
|
|
527
|
+
expandStr(params.pattern),
|
|
528
|
+
Tone.Ticks(params.subdiv || defaultSubdiv).toSeconds()
|
|
529
|
+
);
|
|
530
|
+
}
|
|
531
|
+
let callback;
|
|
532
|
+
if (channel) {
|
|
533
|
+
callback = channel.getSeqFn(params);
|
|
534
|
+
} else if (params.sample) {
|
|
535
|
+
const player = new Tone.Player({ url: params.sample, context });
|
|
536
|
+
player.toDestination();
|
|
537
|
+
player.sync();
|
|
538
|
+
const host = {
|
|
539
|
+
instrument: player,
|
|
540
|
+
hasLoaded: false,
|
|
541
|
+
clipNoteCount: 0
|
|
542
|
+
};
|
|
543
|
+
checkToneObjLoaded(player, () => {
|
|
544
|
+
host.hasLoaded = true;
|
|
545
|
+
});
|
|
546
|
+
const noop = () => {
|
|
547
|
+
};
|
|
548
|
+
callback = buildSequenceCallback(params, host, noop, noop);
|
|
549
|
+
} else {
|
|
550
|
+
throw new Error(
|
|
551
|
+
"Either a Channel or a sample URL must be provided to create a clip."
|
|
552
|
+
);
|
|
553
|
+
}
|
|
554
|
+
return new Tone.Sequence({
|
|
555
|
+
callback,
|
|
556
|
+
events: expandStr(params.pattern),
|
|
557
|
+
subdivision: params.subdiv || defaultSubdiv,
|
|
558
|
+
context
|
|
559
|
+
});
|
|
560
|
+
};
|
|
561
|
+
var totalPatternDuration = (pattern, subdivOrLength) => {
|
|
562
|
+
return typeof subdivOrLength === "number" ? subdivOrLength * expandStr(pattern).length : Tone.Ticks(subdivOrLength).toSeconds() * expandStr(pattern).length;
|
|
563
|
+
};
|
|
564
|
+
var leastCommonMultiple = (n1, n2) => {
|
|
565
|
+
const [smallest, largest] = n1 < n2 ? [n1, n2] : [n2, n1];
|
|
566
|
+
let i = largest;
|
|
567
|
+
while (i % smallest !== 0) {
|
|
568
|
+
i += largest;
|
|
569
|
+
}
|
|
570
|
+
return i;
|
|
571
|
+
};
|
|
572
|
+
var renderingDuration = (pattern, subdivOrLength, notes, randomNotes) => {
|
|
573
|
+
const patternRegularNotesCount = pattern.split("").filter((c) => {
|
|
574
|
+
return c === "x";
|
|
575
|
+
}).length;
|
|
576
|
+
const patternRandomNotesCount = pattern.split("").filter((c) => {
|
|
577
|
+
return c === "R";
|
|
578
|
+
}).length;
|
|
579
|
+
const patternNotesCount = randomNotes?.length ? patternRegularNotesCount : patternRegularNotesCount + patternRandomNotesCount;
|
|
580
|
+
const notesCount = notes.length || 1;
|
|
581
|
+
return totalPatternDuration(pattern, subdivOrLength) / patternNotesCount * leastCommonMultiple(notesCount, patternNotesCount);
|
|
582
|
+
};
|
|
583
|
+
var ongoingRenderingCounter = 0;
|
|
584
|
+
var originalContext;
|
|
585
|
+
var offlineRenderClip = (params, duration) => {
|
|
586
|
+
if (!originalContext) {
|
|
587
|
+
originalContext = Tone.getContext();
|
|
588
|
+
}
|
|
589
|
+
ongoingRenderingCounter++;
|
|
590
|
+
const player = new Tone.Player({ context: originalContext, loop: true });
|
|
591
|
+
Tone.Offline(async (context) => {
|
|
592
|
+
const sequence = generateSequence(params, context);
|
|
593
|
+
await Tone.loaded();
|
|
594
|
+
sequence.start();
|
|
595
|
+
context.transport.start();
|
|
596
|
+
}, duration).then((buffer) => {
|
|
597
|
+
player.buffer = buffer;
|
|
598
|
+
ongoingRenderingCounter--;
|
|
599
|
+
if (ongoingRenderingCounter === 0) {
|
|
600
|
+
Tone.setContext(originalContext);
|
|
601
|
+
params.offlineRenderingCallback?.();
|
|
602
|
+
}
|
|
603
|
+
});
|
|
604
|
+
player.toDestination();
|
|
605
|
+
player.sync();
|
|
606
|
+
return player;
|
|
607
|
+
};
|
|
608
|
+
var clip = (params, channel) => {
|
|
609
|
+
params = preprocessClipParams(params, { align: "1m", alignOffset: "0" });
|
|
610
|
+
if (params.offlineRendering) {
|
|
611
|
+
return offlineRenderClip(
|
|
612
|
+
params,
|
|
613
|
+
renderingDuration(
|
|
614
|
+
params.pattern,
|
|
615
|
+
params.subdiv || defaultSubdiv,
|
|
616
|
+
params.notes || [],
|
|
617
|
+
params.randomNotes
|
|
618
|
+
)
|
|
619
|
+
);
|
|
620
|
+
}
|
|
621
|
+
return generateSequence(params, channel, originalContext);
|
|
622
|
+
};
|
|
623
|
+
|
|
624
|
+
// src/midi.ts
|
|
625
|
+
import fs from "fs";
|
|
626
|
+
import { File, Track } from "@scribbletune/midi";
|
|
627
|
+
var midi = (notes, fileName = "music.mid", bpm) => {
|
|
628
|
+
const file = createFileFromNotes(notes, bpm);
|
|
629
|
+
const bytes = file.toBytes();
|
|
630
|
+
if (fileName === null) {
|
|
631
|
+
return bytes;
|
|
632
|
+
}
|
|
633
|
+
if (!fileName.endsWith(".mid")) {
|
|
634
|
+
fileName = `${fileName}.mid`;
|
|
635
|
+
}
|
|
636
|
+
if (typeof window !== "undefined" && window.URL && typeof window.URL.createObjectURL === "function") {
|
|
637
|
+
return createDownloadLink(bytes, fileName);
|
|
638
|
+
}
|
|
639
|
+
fs.writeFileSync(fileName, bytes, "binary");
|
|
640
|
+
console.log(`MIDI file generated: ${fileName}.`);
|
|
641
|
+
};
|
|
642
|
+
var createDownloadLink = (b, fileName) => {
|
|
643
|
+
const bytes = new Uint8Array(b.length);
|
|
644
|
+
for (let i = 0; i < b.length; i++) {
|
|
645
|
+
const ascii = b.charCodeAt(i);
|
|
646
|
+
bytes[i] = ascii;
|
|
647
|
+
}
|
|
648
|
+
const blob = new Blob([bytes], { type: "audio/midi" });
|
|
649
|
+
const link = document.createElement("a");
|
|
650
|
+
link.href = typeof window !== "undefined" && typeof window.URL !== "undefined" && typeof window.URL.createObjectURL !== "undefined" && window.URL.createObjectURL(blob) || "";
|
|
651
|
+
link.download = fileName;
|
|
652
|
+
link.innerText = "Download MIDI file";
|
|
653
|
+
return link;
|
|
654
|
+
};
|
|
655
|
+
var createFileFromNotes = (notes, bpm) => {
|
|
656
|
+
const file = new File();
|
|
657
|
+
const track = new Track();
|
|
658
|
+
if (typeof bpm === "number") {
|
|
659
|
+
track.setTempo(bpm);
|
|
660
|
+
}
|
|
661
|
+
file.addTrack(track);
|
|
662
|
+
for (const noteObj of notes) {
|
|
663
|
+
const level = noteObj.level || 127;
|
|
664
|
+
if (noteObj.note) {
|
|
665
|
+
if (typeof noteObj.note === "string") {
|
|
666
|
+
track.noteOn(0, noteObj.note, noteObj.length, level);
|
|
667
|
+
track.noteOff(0, noteObj.note, noteObj.length, level);
|
|
668
|
+
} else {
|
|
669
|
+
track.addChord(0, noteObj.note, noteObj.length, level);
|
|
670
|
+
}
|
|
671
|
+
} else {
|
|
672
|
+
track.noteOff(0, "", noteObj.length);
|
|
673
|
+
}
|
|
674
|
+
}
|
|
675
|
+
return file;
|
|
676
|
+
};
|
|
677
|
+
|
|
678
|
+
// src/progression.ts
|
|
679
|
+
import { scale } from "harmonics";
|
|
680
|
+
var getChordDegrees = (mode) => {
|
|
681
|
+
const theRomans = {
|
|
682
|
+
ionian: ["I", "ii", "iii", "IV", "V", "vi", "vii\xB0"],
|
|
683
|
+
dorian: ["i", "ii", "III", "IV", "v", "vi\xB0", "VII"],
|
|
684
|
+
phrygian: ["i", "II", "III", "iv", "v\xB0", "VI", "vii"],
|
|
685
|
+
lydian: ["I", "II", "iii", "iv\xB0", "V", "vi", "vii"],
|
|
686
|
+
mixolydian: ["I", "ii", "iii\xB0", "IV", "v", "vi", "VII"],
|
|
687
|
+
aeolian: ["i", "ii\xB0", "III", "iv", "v", "VI", "VII"],
|
|
688
|
+
locrian: ["i\xB0", "II", "iii", "iv", "V", "VI", "vii"],
|
|
689
|
+
"melodic minor": ["i", "ii", "III+", "IV", "V", "vi\xB0", "vii\xB0"],
|
|
690
|
+
"harmonic minor": ["i", "ii\xB0", "III+", "iv", "V", "VI", "vii\xB0"]
|
|
691
|
+
};
|
|
692
|
+
theRomans.major = theRomans.ionian;
|
|
693
|
+
theRomans.minor = theRomans.aeolian;
|
|
694
|
+
return theRomans[mode] || [];
|
|
695
|
+
};
|
|
696
|
+
var idxByDegree = {
|
|
697
|
+
i: 0,
|
|
698
|
+
ii: 1,
|
|
699
|
+
iii: 2,
|
|
700
|
+
iv: 3,
|
|
701
|
+
v: 4,
|
|
702
|
+
vi: 5,
|
|
703
|
+
vii: 6
|
|
704
|
+
};
|
|
705
|
+
var getChordName = (roman) => {
|
|
706
|
+
const str = roman.replace(/\W/g, "");
|
|
707
|
+
let prefix = "M";
|
|
708
|
+
if (str.toLowerCase() === str) {
|
|
709
|
+
prefix = "m";
|
|
710
|
+
}
|
|
711
|
+
if (roman.indexOf("\xB0") > -1) {
|
|
712
|
+
return `${prefix}7b5`;
|
|
713
|
+
}
|
|
714
|
+
if (roman.indexOf("+") > -1) {
|
|
715
|
+
return `${prefix}#5`;
|
|
716
|
+
}
|
|
717
|
+
if (roman.indexOf("7") > -1) {
|
|
718
|
+
return prefix === "M" ? "maj7" : "m7";
|
|
719
|
+
}
|
|
720
|
+
return prefix;
|
|
721
|
+
};
|
|
722
|
+
var getChordsByProgression = (noteOctaveScale, chordDegress) => {
|
|
723
|
+
const noteOctaveScaleArr = noteOctaveScale.split(" ");
|
|
724
|
+
if (!noteOctaveScaleArr[0].match(/\d/)) {
|
|
725
|
+
noteOctaveScaleArr[0] += "4";
|
|
726
|
+
noteOctaveScale = noteOctaveScaleArr.join(" ");
|
|
727
|
+
}
|
|
728
|
+
const mode = scale(noteOctaveScale);
|
|
729
|
+
const chordDegreesArr = chordDegress.replace(/\s*,+\s*/g, " ").split(" ");
|
|
730
|
+
const chordFamily = chordDegreesArr.map((roman) => {
|
|
731
|
+
const chordName = getChordName(roman);
|
|
732
|
+
const scaleId = idxByDegree[roman.replace(/\W|\d/g, "").toLowerCase()];
|
|
733
|
+
const note = mode[scaleId];
|
|
734
|
+
const oct = note.replace(/\D+/, "");
|
|
735
|
+
return `${note.replace(/\d/, "") + chordName}_${oct}`;
|
|
736
|
+
});
|
|
737
|
+
return chordFamily.toString().replace(/,/g, " ");
|
|
738
|
+
};
|
|
739
|
+
var getProgFactory = ({ T, P, D }) => {
|
|
740
|
+
return (count = 4) => {
|
|
741
|
+
const chords2 = [];
|
|
742
|
+
chords2.push(pickOne(T));
|
|
743
|
+
let i = 1;
|
|
744
|
+
if (i < count - 1) {
|
|
745
|
+
chords2.push(pickOne(P));
|
|
746
|
+
i++;
|
|
747
|
+
}
|
|
748
|
+
if (i < count - 1 && dice()) {
|
|
749
|
+
chords2.push(pickOne(P));
|
|
750
|
+
i++;
|
|
751
|
+
}
|
|
752
|
+
if (i < count - 1) {
|
|
753
|
+
chords2.push(pickOne(D));
|
|
754
|
+
i++;
|
|
755
|
+
}
|
|
756
|
+
if (i < count - 1) {
|
|
757
|
+
chords2.push(pickOne(P));
|
|
758
|
+
i++;
|
|
759
|
+
}
|
|
760
|
+
if (i < count - 1) {
|
|
761
|
+
chords2.push(pickOne(D));
|
|
762
|
+
i++;
|
|
763
|
+
}
|
|
764
|
+
if (i < count - 1 && dice()) {
|
|
765
|
+
chords2.push(pickOne(P));
|
|
766
|
+
i++;
|
|
767
|
+
}
|
|
768
|
+
while (i < count) {
|
|
769
|
+
chords2.push(pickOne(D));
|
|
770
|
+
i++;
|
|
771
|
+
}
|
|
772
|
+
return chords2;
|
|
773
|
+
};
|
|
774
|
+
};
|
|
775
|
+
var M = getProgFactory({ T: ["I", "vi"], P: ["ii", "IV"], D: ["V"] });
|
|
776
|
+
var m = getProgFactory({ T: ["i", "VI"], P: ["ii", "iv"], D: ["V"] });
|
|
777
|
+
var progression = (scaleType, count = 4) => {
|
|
778
|
+
if (scaleType === "major" || scaleType === "M") {
|
|
779
|
+
return M(count);
|
|
780
|
+
}
|
|
781
|
+
if (scaleType === "minor" || scaleType === "m") {
|
|
782
|
+
return m(count);
|
|
783
|
+
}
|
|
784
|
+
return [];
|
|
785
|
+
};
|
|
786
|
+
|
|
787
|
+
// src/channel/effects-chain.ts
|
|
788
|
+
function initEffects(instrument, context, params) {
|
|
789
|
+
context = context || Tone.getContext();
|
|
790
|
+
const createEffect = (effect) => {
|
|
791
|
+
return new Promise((resolve, _reject) => {
|
|
792
|
+
if (typeof effect === "string") {
|
|
793
|
+
resolve(
|
|
794
|
+
new Tone[effect]({
|
|
795
|
+
context
|
|
796
|
+
})
|
|
797
|
+
);
|
|
798
|
+
} else if (effect.context !== context) {
|
|
799
|
+
return recreateToneObjectInContext(
|
|
800
|
+
effect,
|
|
801
|
+
context
|
|
802
|
+
);
|
|
803
|
+
} else {
|
|
804
|
+
resolve(effect);
|
|
805
|
+
}
|
|
806
|
+
}).then((effectOut) => {
|
|
807
|
+
return effectOut.toDestination();
|
|
808
|
+
});
|
|
809
|
+
};
|
|
810
|
+
const startEffect = (eff) => {
|
|
811
|
+
return typeof eff.start === "function" ? eff.start() : eff;
|
|
812
|
+
};
|
|
813
|
+
const toArray = (someVal) => {
|
|
814
|
+
if (!someVal) {
|
|
815
|
+
return [];
|
|
816
|
+
}
|
|
817
|
+
if (Array.isArray(someVal)) {
|
|
818
|
+
return someVal;
|
|
819
|
+
}
|
|
820
|
+
return [someVal];
|
|
821
|
+
};
|
|
822
|
+
const effectsIn = toArray(params.effects);
|
|
823
|
+
if (params.external) {
|
|
824
|
+
if (effectsIn.length !== 0) {
|
|
825
|
+
throw new Error("Effects cannot be used with external output");
|
|
826
|
+
}
|
|
827
|
+
return Promise.resolve();
|
|
828
|
+
}
|
|
829
|
+
return Promise.all(effectsIn.map(createEffect)).then((results) => results.map(startEffect)).then((effects) => {
|
|
830
|
+
instrument.chain(...effects).toDestination();
|
|
831
|
+
});
|
|
832
|
+
}
|
|
833
|
+
|
|
834
|
+
// src/channel.ts
|
|
835
|
+
var getNextPos = (clip2) => {
|
|
836
|
+
const transportPosTicks = Tone.Transport.ticks;
|
|
837
|
+
if (transportPosTicks < Tone.Ticks("4n").toTicks()) {
|
|
838
|
+
return 0;
|
|
839
|
+
}
|
|
840
|
+
const align = clip2?.align || "1m";
|
|
841
|
+
const alignOffset = clip2?.alignOffset || "0";
|
|
842
|
+
const alignTicks = Tone.Ticks(align).toTicks();
|
|
843
|
+
const alignOffsetTicks = Tone.Ticks(alignOffset).toTicks();
|
|
844
|
+
const nextPosTicks = Tone.Ticks(
|
|
845
|
+
Math.floor(transportPosTicks / alignTicks + 1) * alignTicks + alignOffsetTicks
|
|
846
|
+
);
|
|
847
|
+
return nextPosTicks;
|
|
848
|
+
};
|
|
849
|
+
var Channel = class {
|
|
850
|
+
constructor(params) {
|
|
851
|
+
this.idx = params.idx || 0;
|
|
852
|
+
this.name = params.name || `ch ${params.idx}`;
|
|
853
|
+
this.activePatternIdx = -1;
|
|
854
|
+
this.channelClips = [];
|
|
855
|
+
this.clipNoteCount = 0;
|
|
856
|
+
const { clips, samples, sample, synth, ...params1 } = params;
|
|
857
|
+
const { external, sampler, buffer, ...params2 } = params1;
|
|
858
|
+
const { player, instrument, volume, ...params3 } = params2;
|
|
859
|
+
const { eventCb, playerCb, effects, ...params4 } = params3;
|
|
860
|
+
const { context = Tone.getContext(), ...originalParamsFiltered } = params4;
|
|
861
|
+
this.eventCbFn = eventCb;
|
|
862
|
+
this.playerCbFn = playerCb;
|
|
863
|
+
this.hasLoaded = false;
|
|
864
|
+
this.hasFailed = false;
|
|
865
|
+
const result = createInstrument(context, params, {
|
|
866
|
+
idx: this.idx,
|
|
867
|
+
name: this.name
|
|
868
|
+
});
|
|
869
|
+
this.instrument = result.instrument;
|
|
870
|
+
this.external = result.external;
|
|
871
|
+
this.initializerTask = result.initPromise.then((finalInstrument) => {
|
|
872
|
+
this.instrument = finalInstrument;
|
|
873
|
+
return initEffects(this.instrument, context, params);
|
|
874
|
+
});
|
|
875
|
+
let clipsFailed = false;
|
|
876
|
+
try {
|
|
877
|
+
(params.clips ?? []).forEach((c, i) => {
|
|
878
|
+
try {
|
|
879
|
+
this.addClip({
|
|
880
|
+
...c,
|
|
881
|
+
...originalParamsFiltered
|
|
882
|
+
});
|
|
883
|
+
} catch (e) {
|
|
884
|
+
throw new Error(
|
|
885
|
+
`${errorHasMessage(e) ? e.message : e} in clip ${i + 1}`
|
|
886
|
+
);
|
|
887
|
+
}
|
|
888
|
+
}, this);
|
|
889
|
+
} catch (e) {
|
|
890
|
+
clipsFailed = e;
|
|
891
|
+
}
|
|
892
|
+
this.initializerTask.then(() => {
|
|
893
|
+
if (clipsFailed) {
|
|
894
|
+
throw clipsFailed;
|
|
895
|
+
}
|
|
896
|
+
this.hasLoaded = true;
|
|
897
|
+
this.eventCb("loaded", {});
|
|
898
|
+
}).catch((e) => {
|
|
899
|
+
this.hasFailed = e;
|
|
900
|
+
this.eventCb("error", { e });
|
|
901
|
+
});
|
|
902
|
+
}
|
|
903
|
+
/** Set the global transport tempo in BPM. */
|
|
904
|
+
static setTransportTempo(valueBpm) {
|
|
905
|
+
Tone.Transport.bpm.value = valueBpm;
|
|
906
|
+
}
|
|
907
|
+
/** Resume the audio context and start the global transport. */
|
|
908
|
+
static startTransport() {
|
|
909
|
+
Tone.start();
|
|
910
|
+
Tone.Transport.start();
|
|
911
|
+
}
|
|
912
|
+
/**
|
|
913
|
+
* Stop the global transport.
|
|
914
|
+
* @param deleteEvents - If true (default), cancels all scheduled transport events.
|
|
915
|
+
*/
|
|
916
|
+
static stopTransport(deleteEvents = true) {
|
|
917
|
+
Tone.Transport.stop();
|
|
918
|
+
if (deleteEvents) {
|
|
919
|
+
Tone.Transport.cancel();
|
|
920
|
+
}
|
|
921
|
+
}
|
|
922
|
+
/** Set the volume (in dB) of this channel's instrument and external output. */
|
|
923
|
+
setVolume(volume) {
|
|
924
|
+
if (this.instrument) {
|
|
925
|
+
this.instrument.volume.value = volume;
|
|
926
|
+
}
|
|
927
|
+
if (this.external) {
|
|
928
|
+
this.external.setVolume?.(volume);
|
|
929
|
+
}
|
|
930
|
+
}
|
|
931
|
+
/**
|
|
932
|
+
* Start the clip at the given index, stopping any other active clip first.
|
|
933
|
+
* @param idx - Clip index in this channel
|
|
934
|
+
* @param position - Transport time to start at; defaults to the next aligned position
|
|
935
|
+
*/
|
|
936
|
+
startClip(idx, position) {
|
|
937
|
+
const clip2 = this.channelClips[idx];
|
|
938
|
+
position = position || (position === 0 ? 0 : getNextPos(clip2));
|
|
939
|
+
if (this.activePatternIdx > -1 && this.activePatternIdx !== idx) {
|
|
940
|
+
this.stopClip(this.activePatternIdx, position);
|
|
941
|
+
}
|
|
942
|
+
if (clip2 && clip2.state !== "started") {
|
|
943
|
+
this.counterResetTask = Tone.Transport.scheduleOnce(
|
|
944
|
+
() => {
|
|
945
|
+
this.clipNoteCount = 0;
|
|
946
|
+
},
|
|
947
|
+
position
|
|
948
|
+
);
|
|
949
|
+
this.activePatternIdx = idx;
|
|
950
|
+
clip2?.start(position);
|
|
951
|
+
}
|
|
952
|
+
}
|
|
953
|
+
/**
|
|
954
|
+
* Stop the clip at the given index.
|
|
955
|
+
* @param idx - Clip index in this channel
|
|
956
|
+
* @param position - Transport time to stop at; defaults to the next aligned position
|
|
957
|
+
*/
|
|
958
|
+
stopClip(idx, position) {
|
|
959
|
+
const clip2 = this.channelClips[idx];
|
|
960
|
+
position = position || (position === 0 ? 0 : getNextPos(clip2));
|
|
961
|
+
clip2?.stop(position);
|
|
962
|
+
if (idx === this.activePatternIdx) {
|
|
963
|
+
this.activePatternIdx = -1;
|
|
964
|
+
}
|
|
965
|
+
}
|
|
966
|
+
/**
|
|
967
|
+
* Add a clip to this channel. If the clip has a pattern, a Tone.Sequence is
|
|
968
|
+
* created; otherwise an empty (null) slot is reserved.
|
|
969
|
+
* @param clipParams - Clip configuration
|
|
970
|
+
* @param idx - Slot index; defaults to the next available position
|
|
971
|
+
*/
|
|
972
|
+
addClip(clipParams, idx) {
|
|
973
|
+
idx = idx || this.channelClips.length;
|
|
974
|
+
if (clipParams.pattern) {
|
|
975
|
+
this.channelClips[idx] = clip(
|
|
976
|
+
{
|
|
977
|
+
...clipParams
|
|
978
|
+
},
|
|
979
|
+
this
|
|
980
|
+
);
|
|
981
|
+
const seq = this.channelClips[idx];
|
|
982
|
+
if (seq && clipParams.align) seq.align = clipParams.align;
|
|
983
|
+
if (seq && clipParams.alignOffset)
|
|
984
|
+
seq.alignOffset = clipParams.alignOffset;
|
|
985
|
+
} else {
|
|
986
|
+
this.channelClips[idx] = null;
|
|
987
|
+
}
|
|
988
|
+
}
|
|
989
|
+
/**
|
|
990
|
+
* @param {Object} ClipParams clip parameters
|
|
991
|
+
* @return {Function} function that can be used as the callback in Tone.Sequence https://tonejs.github.io/docs/Sequence
|
|
992
|
+
*/
|
|
993
|
+
getSeqFn(params) {
|
|
994
|
+
return buildSequenceCallback(
|
|
995
|
+
params,
|
|
996
|
+
this,
|
|
997
|
+
(p) => this.playerCb(p),
|
|
998
|
+
(e, p) => this.eventCb(e, p)
|
|
999
|
+
);
|
|
1000
|
+
}
|
|
1001
|
+
/** Invoke the user-provided event callback, if set. */
|
|
1002
|
+
eventCb(event, params) {
|
|
1003
|
+
if (typeof this.eventCbFn === "function") {
|
|
1004
|
+
params.channel = this;
|
|
1005
|
+
this.eventCbFn(event, params);
|
|
1006
|
+
}
|
|
1007
|
+
}
|
|
1008
|
+
/** Invoke the user-provided player observer callback, if set. */
|
|
1009
|
+
playerCb(params) {
|
|
1010
|
+
if (typeof this.playerCbFn === "function") {
|
|
1011
|
+
params.channel = this;
|
|
1012
|
+
this.playerCbFn(params);
|
|
1013
|
+
}
|
|
1014
|
+
}
|
|
1015
|
+
/** All clips (sequences) belonging to this channel. */
|
|
1016
|
+
get clips() {
|
|
1017
|
+
return this.channelClips;
|
|
1018
|
+
}
|
|
1019
|
+
/** Index of the currently playing clip, or -1 if none. */
|
|
1020
|
+
get activeClipIdx() {
|
|
1021
|
+
return this.activePatternIdx;
|
|
1022
|
+
}
|
|
1023
|
+
};
|
|
1024
|
+
|
|
1025
|
+
// src/session.ts
|
|
1026
|
+
var Session = class {
|
|
1027
|
+
/** Create a session, optionally pre-populated with channels. */
|
|
1028
|
+
constructor(arr) {
|
|
1029
|
+
arr = arr || [];
|
|
1030
|
+
this.sessionChannels = arr.map((ch, i) => {
|
|
1031
|
+
ch.idx = ch.idx || i;
|
|
1032
|
+
ch.idx = this.uniqueIdx(this.sessionChannels, ch.idx);
|
|
1033
|
+
return new Channel(ch);
|
|
1034
|
+
});
|
|
1035
|
+
}
|
|
1036
|
+
/** Return a unique channel index, generating a new one if `idx` is taken or missing. */
|
|
1037
|
+
uniqueIdx(channels, idx) {
|
|
1038
|
+
if (!channels) {
|
|
1039
|
+
return idx || 0;
|
|
1040
|
+
}
|
|
1041
|
+
const idxs = channels.reduce((acc, c) => {
|
|
1042
|
+
return !acc.find((i) => i === c.idx) && acc.concat(c.idx) || acc;
|
|
1043
|
+
}, []);
|
|
1044
|
+
if (!idx || idxs.find((i) => i === idx)) {
|
|
1045
|
+
let newIdx = channels.length;
|
|
1046
|
+
while (idxs.find((i) => i === newIdx)) {
|
|
1047
|
+
newIdx = newIdx + 1;
|
|
1048
|
+
}
|
|
1049
|
+
return newIdx;
|
|
1050
|
+
}
|
|
1051
|
+
return idx;
|
|
1052
|
+
}
|
|
1053
|
+
/** Create a new channel with a unique index and add it to the session. */
|
|
1054
|
+
createChannel(ch) {
|
|
1055
|
+
ch.idx = this.uniqueIdx(this.sessionChannels, ch.idx);
|
|
1056
|
+
const newChannel = new Channel(ch);
|
|
1057
|
+
this.sessionChannels.push(newChannel);
|
|
1058
|
+
return newChannel;
|
|
1059
|
+
}
|
|
1060
|
+
/** All channels in this session. */
|
|
1061
|
+
get channels() {
|
|
1062
|
+
return this.sessionChannels;
|
|
1063
|
+
}
|
|
1064
|
+
/** Set the global transport tempo in BPM. */
|
|
1065
|
+
setTransportTempo(valueBpm) {
|
|
1066
|
+
Channel.setTransportTempo(valueBpm);
|
|
1067
|
+
}
|
|
1068
|
+
/** Resume the audio context and start the global transport. */
|
|
1069
|
+
startTransport() {
|
|
1070
|
+
Channel.startTransport();
|
|
1071
|
+
}
|
|
1072
|
+
/**
|
|
1073
|
+
* Stop the global transport.
|
|
1074
|
+
* @param deleteEvents - If true (default), cancels all scheduled transport events.
|
|
1075
|
+
*/
|
|
1076
|
+
stopTransport(deleteEvents = true) {
|
|
1077
|
+
Channel.stopTransport(deleteEvents);
|
|
1078
|
+
}
|
|
1079
|
+
/** Start the clip at the given index across all channels simultaneously. */
|
|
1080
|
+
startRow(idx) {
|
|
1081
|
+
this.sessionChannels.forEach((ch) => {
|
|
1082
|
+
ch.startClip(idx);
|
|
1083
|
+
});
|
|
1084
|
+
}
|
|
1085
|
+
/**
|
|
1086
|
+
* Schedule clip playback across channels using a song-structure pattern.
|
|
1087
|
+
* Each channel pattern is a string where each character is a clip index
|
|
1088
|
+
* (or `-` for silence, `_` to sustain the previous clip).
|
|
1089
|
+
*/
|
|
1090
|
+
play(params) {
|
|
1091
|
+
const channelPatterns = params.channelPatterns;
|
|
1092
|
+
const clipDuration = params.clipDuration || "4:0:0";
|
|
1093
|
+
const clipDurationInSeconds = Tone.Time(clipDuration).toSeconds();
|
|
1094
|
+
const stopClips = (clips, time) => {
|
|
1095
|
+
clips.forEach((c) => {
|
|
1096
|
+
c.stop(time);
|
|
1097
|
+
});
|
|
1098
|
+
};
|
|
1099
|
+
const startClips = (channelIdx, clipIdx, time) => {
|
|
1100
|
+
if (clipIdx === "-") return [];
|
|
1101
|
+
const clips = this.channels.filter((c) => c.idx === channelIdx).map((c) => c.clips[Number(clipIdx)]).filter((c) => c != null);
|
|
1102
|
+
for (const c of clips) c.start(time);
|
|
1103
|
+
return clips;
|
|
1104
|
+
};
|
|
1105
|
+
channelPatterns.forEach(({ channelIdx, pattern }) => {
|
|
1106
|
+
let clips = [];
|
|
1107
|
+
let time = 0;
|
|
1108
|
+
let prevClipIdx = "-";
|
|
1109
|
+
pattern.split("").forEach((clipIdx) => {
|
|
1110
|
+
if (clipIdx !== prevClipIdx && clipIdx !== "_") {
|
|
1111
|
+
stopClips(clips, time);
|
|
1112
|
+
clips = startClips(channelIdx, clipIdx, time);
|
|
1113
|
+
}
|
|
1114
|
+
prevClipIdx = clipIdx;
|
|
1115
|
+
time += clipDurationInSeconds;
|
|
1116
|
+
});
|
|
1117
|
+
stopClips(clips, time);
|
|
1118
|
+
});
|
|
1119
|
+
}
|
|
1120
|
+
};
|
|
1121
|
+
export {
|
|
1122
|
+
Session,
|
|
1123
|
+
arp,
|
|
1124
|
+
chord2 as chord,
|
|
1125
|
+
chords,
|
|
1126
|
+
clip,
|
|
1127
|
+
getChordDegrees,
|
|
1128
|
+
getChordsByProgression,
|
|
1129
|
+
midi,
|
|
1130
|
+
scale2 as mode,
|
|
1131
|
+
scales as modes,
|
|
1132
|
+
progression,
|
|
1133
|
+
scale2 as scale,
|
|
1134
|
+
scales
|
|
1135
|
+
};
|
|
2
1136
|
//# sourceMappingURL=browser.js.map
|