ditty.bpm = 75; const kick = synth.def( (phase, env, tick, options) => Math.sin(phase * 2 * Math.PI * (1.5 - tick * 4)) * env.value); const tri = synth.def( (phase, env, tick, options) => { const v = (phase % 1) * 4; return (v < 2 ? v - 1 : 3 - v) * env.value }, { attack: 0.0125, release: 0.25, amp: 0.8 }); const square = synth.def( (phase, env, tick, options) => (phase % 1 < 0.5 ? 1 : -1) * env.value, { env: adsr2 }); let duration = 1; let melody = "g--g-ag-g-g-gagf-g--g-ag-g-g-gagfc--c-ec-c-c-cecG-d--d-D-D-D---".split("").map(n => { if (n==="g") return {n:g2, duration} else if (n==="G") return {n:g1, duration: 2} else if (n==="a") return {n:as2, duration} else if (n==="c") return {n:c3, duration} else if (n==="d") return {n:d3, duration} else if (n==="D") return {n:d2, duration} else if (n==="e") return {n:ds3, duration} else if (n==="f") return {n:f2, duration: 2} return null; }); loop(() => { let synths = [pick(tri,sine), pick(tri,sine,square)]; let octave = [pick(-12,0,0,0,12), pick(-12,0,12)]; let attack = [range(0.0001, 0.01), range(0.0001, 0.002)]; let release = [range(0.225, 0.825), range(0.225, 0.625) * 0.25]; sleep(0.25); for (let note of melody) { if (note) { synths[0].play(note.n+octave[0], { attack: attack[0], release: note.duration * release[0], pan: pick(0.25, 0.75), amp: 0.5 }); synths[0].play(note.n+octave[1], { attack: attack[1], release: release[1], amp: 0.35}); } sleep( 0.25 ); } }, { name: 'melody' }); loop(() => { kick.play(c2, { attack: 0.025, release: 0.3, amp: 1.5 }); sleep( 0.5 ); }, { name: 'kick' }); function pick(...a) { return a[a.length*Math.random()|0] } function range(a,b) { return a+(b-a)*Math.random(); }