Skip to content

Instantly share code, notes, and snippets.

@amark
Created June 17, 2019 22:31
Show Gist options
  • Save amark/b95dff8b74c977a5c563c692347cf2a2 to your computer and use it in GitHub Desktop.
Save amark/b95dff8b74c977a5c563c692347cf2a2 to your computer and use it in GitHub Desktop.
<script src="https://cdnjs.cloudflare.com/ajax/libs/tone/13.8.12/Tone.js"></script>
<textarea id="foo" style="width: 100%; height: 100%;">this track continues into the new line &
here
but this is a different track playing simultaneously with first.</textarea>
<script>;(function(){
foo.onmouseup = function(){ play(foo.value) }
window.play = function play(S){
var N = [], f = true;
S = split(S, '\n\n'); // split song into sections if haven't already
S.forEach(function(s, T){
if(!f){ return N.push(s) } f = false; // handle recurse
if(typeof s == 'string'){ s = s.replace('&\n', '') } // parse once
var n = []; // do we have next?
T = split(s, '\n');
T.forEach(function(t){ // For each track, of the first section, of the current version of the song
new Tone.Synth().toMaster().triggerAttackRelease(convert(t[0])+'4', '0.1s'); // ... play the first note of each track!
t = t.slice(1); if(t.length){ n.push(t) } // handle recurse
});
if(n.length){ N.push(n) }
});
if(N.length){ setTimeout(function(){ play(N) }, 100) }
}
function convert(t){
var notes = 'ABCDEFG'.split('');
return notes[t.charCodeAt(0) % notes.length]
}
function split(t, on){
return typeof t == 'string'? t.split(on) : t;
}
}());</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment