-
Notifications
You must be signed in to change notification settings - Fork 1
/
webaudio-tinysynth.js
26 lines (26 loc) · 41.1 KB
/
webaudio-tinysynth.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/*
All code from: https://github.com/g200kg/webaudio-tinysynth
I had to modify the library slightly to make it work on Khan Academy
*/
!function(t){"use strict";function $($){Object.assign($,{properties:{masterVol:{type:Number,value:.5,observer:"setMasterVol"},reverbLev:{type:Number,value:.3,observer:"setReverbLev"},quality:{type:Number,value:1,observer:"setQuality"},debug:{type:Number,value:0},src:{type:String,value:null,observer:"loadMIDIfromSrc"},loop:{type:Number,value:0},internalcontext:{type:Number,value:1},tsmode:{type:Number,value:0},voices:{type:Number,value:64},useReverb:{type:Number,value:1},width:{type:String,value:"300px",observer:"layout"},height:{type:String,value:"32px",observer:"layout"},graph:{type:Number,value:1},disabledrop:{type:Number,value:0},perfmon:{type:Number,value:0}},layout:()=>{this.canvas.style.width=this.width,this.canvas.style.height=this.height},program:[{name:"Acoustic Grand Piano"},{name:"Bright Acoustic Piano"},{name:"Electric Grand Piano"},{name:"Honky-tonk Piano"},{name:"Electric Piano 1"},{name:"Electric Piano 2"},{name:"Harpsichord"},{name:"Clavi"},{name:"Celesta"},{name:"Glockenspiel"},{name:"Music Box"},{name:"Vibraphone"},{name:"Marimba"},{name:"Xylophone"},{name:"Tubular Bells"},{name:"Dulcimer"},{name:"Drawbar Organ"},{name:"Percussive Organ"},{name:"Rock Organ"},{name:"Church Organ"},{name:"Reed Organ"},{name:"Accordion"},{name:"Harmonica"},{name:"Tango Accordion"},{name:"Acoustic Guitar (nylon)"},{name:"Acoustic Guitar (steel)"},{name:"Electric Guitar (jazz)"},{name:"Electric Guitar (clean)"},{name:"Electric Guitar (muted)"},{name:"Overdriven Guitar"},{name:"Distortion Guitar"},{name:"Guitar harmonics"},{name:"Acoustic Bass"},{name:"Electric Bass (finger)"},{name:"Electric Bass (pick)"},{name:"Fretless Bass"},{name:"Slap Bass 1"},{name:"Slap Bass 2"},{name:"Synth Bass 1"},{name:"Synth Bass 2"},{name:"Violin"},{name:"Viola"},{name:"Cello"},{name:"Contrabass"},{name:"Tremolo Strings"},{name:"Pizzicato Strings"},{name:"Orchestral Harp"},{name:"Timpani"},{name:"String Ensemble 1"},{name:"String Ensemble 2"},{name:"SynthStrings 1"},{name:"SynthStrings 2"},{name:"Choir Aahs"},{name:"Voice Oohs"},{name:"Synth Voice"},{name:"Orchestra Hit"},{name:"Trumpet"},{name:"Trombone"},{name:"Tuba"},{name:"Muted Trumpet"},{name:"French Horn"},{name:"Brass Section"},{name:"SynthBrass 1"},{name:"SynthBrass 2"},{name:"Soprano Sax"},{name:"Alto Sax"},{name:"Tenor Sax"},{name:"Baritone Sax"},{name:"Oboe"},{name:"English Horn"},{name:"Bassoon"},{name:"Clarinet"},{name:"Piccolo"},{name:"Flute"},{name:"Recorder"},{name:"Pan Flute"},{name:"Blown Bottle"},{name:"Shakuhachi"},{name:"Whistle"},{name:"Ocarina"},{name:"Lead 1 (square)"},{name:"Lead 2 (sawtooth)"},{name:"Lead 3 (calliope)"},{name:"Lead 4 (chiff)"},{name:"Lead 5 (charang)"},{name:"Lead 6 (voice)"},{name:"Lead 7 (fifths)"},{name:"Lead 8 (bass + lead)"},{name:"Pad 1 (new age)"},{name:"Pad 2 (warm)"},{name:"Pad 3 (polysynth)"},{name:"Pad 4 (choir)"},{name:"Pad 5 (bowed)"},{name:"Pad 6 (metallic)"},{name:"Pad 7 (halo)"},{name:"Pad 8 (sweep)"},{name:"FX 1 (rain)"},{name:"FX 2 (soundtrack)"},{name:"FX 3 (crystal)"},{name:"FX 4 (atmosphere)"},{name:"FX 5 (brightness)"},{name:"FX 6 (goblins)"},{name:"FX 7 (echoes)"},{name:"FX 8 (sci-fi)"},{name:"Sitar"},{name:"Banjo"},{name:"Shamisen"},{name:"Koto"},{name:"Kalimba"},{name:"Bag pipe"},{name:"Fiddle"},{name:"Shanai"},{name:"Tinkle Bell"},{name:"Agogo"},{name:"Steel Drums"},{name:"Woodblock"},{name:"Taiko Drum"},{name:"Melodic Tom"},{name:"Synth Drum"},{name:"Reverse Cymbal"},{name:"Guitar Fret Noise"},{name:"Breath Noise"},{name:"Seashore"},{name:"Bird Tweet"},{name:"Telephone Ring"},{name:"Helicopter"},{name:"Applause"},{name:"Gunshot"},],drummap:[{name:"Acoustic Bass Drum"},{name:"Bass Drum 1"},{name:"Side Stick"},{name:"Acoustic Snare"},{name:"Hand Clap"},{name:"Electric Snare"},{name:"Low Floor Tom"},{name:"Closed Hi Hat"},{name:"High Floor Tom"},{name:"Pedal Hi-Hat"},{name:"Low Tom"},{name:"Open Hi-Hat"},{name:"Low-Mid Tom"},{name:"Hi-Mid Tom"},{name:"Crash Cymbal 1"},{name:"High Tom"},{name:"Ride Cymbal 1"},{name:"Chinese Cymbal"},{name:"Ride Bell"},{name:"Tambourine"},{name:"Splash Cymbal"},{name:"Cowbell"},{name:"Crash Cymbal 2"},{name:"Vibraslap"},{name:"Ride Cymbal 2"},{name:"Hi Bongo"},{name:"Low Bongo"},{name:"Mute Hi Conga"},{name:"Open Hi Conga"},{name:"Low Conga"},{name:"High Timbale"},{name:"Low Timbale"},{name:"High Agogo"},{name:"Low Agogo"},{name:"Cabasa"},{name:"Maracas"},{name:"Short Whistle"},{name:"Long Whistle"},{name:"Short Guiro"},{name:"Long Guiro"},{name:"Claves"},{name:"Hi Wood Block"},{name:"Low Wood Block"},{name:"Mute Cuica"},{name:"Open Cuica"},{name:"Mute Triangle"},{name:"Open Triangle"},],program1:[[{w:"sine",v:.4,d:.7,r:.1},{w:"triangle",v:3,d:.7,s:.1,g:1,a:.01,k:-1.2}],[{w:"triangle",v:.4,d:.7,r:.1},{w:"triangle",v:4,t:3,d:.4,s:.1,g:1,k:-1,a:.01}],[{w:"sine",d:.7,r:.1},{w:"triangle",v:4,f:2,d:.5,s:.5,g:1,k:-1}],[{w:"sine",d:.7,v:.2},{w:"triangle",v:4,t:3,f:2,d:.3,g:1,k:-1,a:.01,s:.5}],[{w:"sine",v:.35,d:.7},{w:"sine",v:3,t:7,f:1,d:1,s:1,g:1,k:-.7}],[{w:"sine",v:.35,d:.7},{w:"sine",v:8,t:7,f:1,d:.5,s:1,g:1,k:-.7}],[{w:"sawtooth",v:.34,d:2},{w:"sine",v:8,f:.1,d:2,s:1,r:2,g:1}],[{w:"triangle",v:.34,d:1.5},{w:"square",v:6,f:.1,d:1.5,s:.5,r:2,g:1}],[{w:"sine",d:.3,r:.3},{w:"sine",v:7,t:11,d:.03,g:1}],[{w:"sine",d:.3,r:.3},{w:"sine",v:11,t:6,d:.2,s:.4,g:1}],[{w:"sine",v:.2,d:.3,r:.3},{w:"sine",v:11,t:5,d:.1,s:.4,g:1}],[{w:"sine",v:.2,d:.6,r:.6},{w:"triangle",v:11,t:5,f:1,s:.5,g:1}],[{w:"sine",v:.3,d:.2,r:.2},{w:"sine",v:6,t:5,d:.02,g:1}],[{w:"sine",v:.3,d:.2,r:.2},{w:"sine",v:7,t:11,d:.03,g:1}],[{w:"sine",v:.2,d:1,r:1},{w:"sine",v:11,t:3.5,d:1,r:1,g:1}],[{w:"triangle",v:.2,d:.5,r:.2},{w:"sine",v:6,t:2.5,d:.2,s:.1,r:.2,g:1}],[{w:"w9999",v:.22,s:.9},{w:"w9999",v:.22,t:2,f:2,s:.9}],[{w:"w9999",v:.2,s:1},{w:"sine",v:11,t:6,f:2,s:.1,g:1,h:.006,r:.002,d:.002},{w:"w9999",v:.2,t:2,f:1,h:0,s:1}],[{w:"w9999",v:.2,d:.1,s:.9},{w:"w9999",v:.25,t:4,f:2,s:.5}],[{w:"w9999",v:.3,a:.04,s:.9},{w:"w9999",v:.2,t:8,f:2,a:.04,s:.9}],[{w:"sine",v:.2,a:.02,d:.05,s:1},{w:"sine",v:6,t:3,f:1,a:.02,d:.05,s:1,g:1}],[{w:"triangle",v:.2,a:.02,d:.05,s:.8},{w:"square",v:7,t:3,f:1,d:.05,s:1.5,g:1}],[{w:"square",v:.2,a:.02,d:.2,s:.5},{w:"square",v:1,d:.03,s:2,g:1}],[{w:"square",v:.2,a:.02,d:.1,s:.8},{w:"square",v:1,a:.3,d:.1,s:2,g:1}],[{w:"sine",v:.3,d:.5,f:1},{w:"triangle",v:5,t:3,f:-1,d:1,s:.1,g:1}],[{w:"sine",v:.4,d:.6,f:1},{w:"triangle",v:12,t:3,d:.6,s:.1,g:1,f:-1}],[{w:"triangle",v:.3,d:1,f:1},{w:"triangle",v:6,f:-1,d:.4,s:.5,g:1,t:3}],[{w:"sine",v:.3,d:1,f:-1},{w:"triangle",v:11,f:1,d:.4,s:.5,g:1,t:3}],[{w:"sine",v:.4,d:.1,r:.01},{w:"sine",v:7,g:1}],[{w:"triangle",v:.4,d:1,f:1},{w:"square",v:4,f:-1,d:1,s:.7,g:1}],[{w:"triangle",v:.35,d:1,f:1},{w:"square",v:7,f:-1,d:.3,s:.5,g:1}],[{w:"sine",v:.2,t:1.5,a:.005,h:.2,d:.6},{w:"sine",v:11,t:5,f:2,d:1,s:.5,g:1}],[{w:"sine",d:.3},{w:"sine",v:4,t:3,d:1,s:1,g:1}],[{w:"sine",d:.3},{w:"sine",v:4,t:3,d:1,s:1,g:1}],[{w:"w9999",d:.3,v:.7,s:.5},{w:"sawtooth",v:1.2,d:.02,s:.5,g:1,h:0,r:.02}],[{w:"sine",d:.3},{w:"sine",v:4,t:3,d:1,s:1,g:1}],[{w:"triangle",v:.3,t:2,d:1},{w:"triangle",v:15,t:2.5,d:.04,s:.1,g:1}],[{w:"triangle",v:.3,t:2,d:1},{w:"triangle",v:15,t:2.5,d:.04,s:.1,g:1}],[{w:"triangle",d:.7},{w:"square",v:.4,t:.5,f:1,d:.2,s:10,g:1}],[{w:"triangle",d:.7},{w:"square",v:.4,t:.5,f:1,d:.2,s:10,g:1}],[{w:"sawtooth",v:.4,a:.1,d:11},{w:"sine",v:5,d:11,s:.2,g:1}],[{w:"sawtooth",v:.4,a:.1,d:11},{w:"sine",v:5,d:11,s:.2,g:1}],[{w:"sawtooth",v:.4,a:.1,d:11},{w:"sine",v:5,t:.5,d:11,s:.2,g:1}],[{w:"sawtooth",v:.4,a:.1,d:11},{w:"sine",v:5,t:.5,d:11,s:.2,g:1}],[{w:"sine",v:.4,a:.1,d:11},{w:"sine",v:6,f:2.5,d:.05,s:1.1,g:1}],[{w:"sine",v:.3,d:.1,r:.1},{w:"square",v:4,t:3,d:1,s:.2,g:1}],[{w:"sine",v:.3,d:.5,r:.5},{w:"sine",v:7,t:2,f:2,d:1,r:1,g:1}],[{w:"triangle",v:.6,h:.03,d:.3,r:.3,t:.5},{w:"n0",v:8,t:1.5,d:.08,r:.08,g:1}],[{w:"sawtooth",v:.3,a:.03,s:.5},{w:"sawtooth",v:.2,t:2,f:2,d:1,s:2}],[{w:"sawtooth",v:.3,f:-2,a:.03,s:.5},{w:"sawtooth",v:.2,t:2,f:2,d:1,s:2}],[{w:"sawtooth",v:.2,a:.02,s:1},{w:"sawtooth",v:.2,t:2,f:2,a:1,d:1,s:1}],[{w:"sawtooth",v:.2,a:.02,s:1},{w:"sawtooth",v:.2,f:2,a:.02,d:1,s:1}],[{w:"triangle",v:.3,a:.03,s:1},{w:"sine",v:3,t:5,f:1,d:1,s:1,g:1}],[{w:"sine",v:.4,a:.03,s:.9},{w:"sine",v:1,t:2,f:3,d:.03,s:.2,g:1}],[{w:"triangle",v:.6,a:.05,s:.5},{w:"sine",v:1,f:.8,d:.2,s:.2,g:1}],[{w:"square",v:.15,a:.01,d:.2,r:.2,t:.5,h:.03},{w:"square",v:4,f:.5,d:.2,r:11,a:.01,g:1,h:.02},{w:"square",v:.15,t:4,f:1,a:.02,d:.15,r:.15,h:.03},{g:3,w:"square",v:4,f:-.5,a:.01,h:.02,d:.15,r:11}],[{w:"square",v:.2,a:.01,d:1,s:.6,r:.04},{w:"sine",v:1,d:.1,s:4,g:1}],[{w:"square",v:.2,a:.02,d:1,s:.5,r:.08},{w:"sine",v:1,d:.1,s:4,g:1}],[{w:"square",v:.2,a:.04,d:1,s:.4,r:.08},{w:"sine",v:1,d:.1,s:4,g:1}],[{w:"square",v:.15,a:.04,s:1},{w:"sine",v:2,d:.1,g:1}],[{w:"square",v:.2,a:.02,d:1,s:.5,r:.08},{w:"sine",v:1,d:.1,s:4,g:1}],[{w:"square",v:.2,a:.02,d:1,s:.6,r:.08},{w:"sine",v:1,f:.2,d:.1,s:4,g:1}],[{w:"square",v:.2,a:.02,d:.5,s:.7,r:.08},{w:"sine",v:1,d:.1,s:4,g:1}],[{w:"square",v:.2,a:.02,d:1,s:.5,r:.08},{w:"sine",v:1,d:.1,s:4,g:1}],[{w:"square",v:.2,a:.02,d:2,s:.6},{w:"sine",v:2,d:1,g:1}],[{w:"square",v:.2,a:.02,d:2,s:.6},{w:"sine",v:2,d:1,g:1}],[{w:"square",v:.2,a:.02,d:1,s:.6},{w:"sine",v:2,d:1,g:1}],[{w:"square",v:.2,a:.02,d:1,s:.6},{w:"sine",v:2,d:1,g:1}],[{w:"sine",v:.4,a:.02,d:.7,s:.5},{w:"square",v:5,t:2,d:.2,s:.5,g:1}],[{w:"sine",v:.3,a:.05,d:.2,s:.8},{w:"sawtooth",v:6,f:.1,d:.1,s:.3,g:1}],[{w:"sine",v:.3,a:.03,d:.2,s:.4},{w:"square",v:7,f:.2,d:1,s:.1,g:1}],[{w:"square",v:.2,a:.05,d:.1,s:.8},{w:"square",v:4,d:.1,s:1.1,g:1}],[{w:"sine",a:.02,d:2},{w:"sine",v:6,t:2,d:.04,g:1}],[{w:"sine",v:.7,a:.03,d:.4,s:.4},{w:"sine",v:4,t:2,f:.2,d:.4,g:1}],[{w:"sine",v:.7,a:.02,d:.4,s:.6},{w:"sine",v:3,t:2,d:0,s:1,g:1}],[{w:"sine",v:.4,a:.06,d:.3,s:.3},{w:"sine",v:7,t:2,d:.2,s:.2,g:1}],[{w:"sine",a:.02,d:.3,s:.3},{w:"sawtooth",v:3,t:2,d:.3,g:1}],[{w:"sine",v:.4,a:.02,d:2,s:.1},{w:"sawtooth",v:8,t:2,f:1,d:.5,g:1}],[{w:"sine",v:.7,a:.03,d:.5,s:.3},{w:"sine",v:.003,t:0,f:4,d:.1,s:.002,g:1}],[{w:"sine",v:.7,a:.02,d:2},{w:"sine",v:1,t:2,f:1,d:.02,g:1}],[{w:"square",v:.3,d:1,s:.5},{w:"square",v:1,f:.2,d:1,s:.5,g:1}],[{w:"sawtooth",v:.3,d:2,s:.5},{w:"square",v:2,f:.1,s:.5,g:1}],[{w:"triangle",v:.5,a:.05,d:2,s:.6},{w:"sine",v:4,t:2,g:1}],[{w:"triangle",v:.3,a:.01,d:2,s:.3},{w:"sine",v:22,t:2,f:1,d:.03,s:.2,g:1}],[{w:"sawtooth",v:.3,d:1,s:.5},{w:"sine",v:11,t:11,a:.2,d:.05,s:.3,g:1}],[{w:"sine",v:.3,a:.06,d:1,s:.5},{w:"sine",v:7,f:1,d:1,s:.2,g:1}],[{w:"sawtooth",v:.3,a:.03,d:.7,s:.3,r:.2},{w:"sawtooth",v:.3,t:.75,d:.7,a:.1,s:.3,r:.2}],[{w:"triangle",v:.3,a:.01,d:.7,s:.5},{w:"square",v:5,t:.5,d:.7,s:.5,g:1}],[{w:"triangle",v:.3,a:.02,d:.3,s:.3,r:.3},{w:"square",v:3,t:4,f:1,a:.02,d:.1,s:1,g:1},{w:"triangle",v:.08,t:.5,a:.1,h:0,d:.1,s:.5,r:.1,b:0,c:0}],[{w:"sine",v:.3,a:.05,d:1,s:.7,r:.3},{w:"sine",v:2,f:1,d:.3,s:1,g:1}],[{w:"square",v:.3,a:.03,d:.5,s:.3,r:.1},{w:"square",v:4,f:1,a:.03,d:.1,g:1}],[{w:"triangle",v:.3,a:.08,d:1,s:.3,r:.1},{w:"square",v:2,f:1,d:.3,s:.3,g:1,t:4,a:.08}],[{w:"sine",v:.3,a:.05,d:1,s:.3,r:.1},{w:"sine",v:.1,t:2.001,f:1,d:1,s:50,g:1}],[{w:"triangle",v:.3,a:.03,d:.7,s:.3,r:.2},{w:"sine",v:12,t:7,f:1,d:.5,s:1.7,g:1}],[{w:"sine",v:.3,a:.05,d:1,s:.3,r:.1},{w:"sawtooth",v:22,t:6,d:.06,s:.3,g:1}],[{w:"triangle",v:.3,a:.05,d:11,r:.3},{w:"triangle",v:1,d:1,s:8,g:1}],[{w:"sawtooth",v:.3,d:4,s:.8,r:.1},{w:"square",v:1,t:2,f:8,a:1,d:1,s:1,r:.1,g:1}],[{w:"triangle",v:.3,d:1,s:.5,t:.8,a:.2,p:1.25,q:.2},{w:"sawtooth",v:.2,a:.2,d:.3,s:1,t:1.2,p:1.25,q:.2}],[{w:"sine",v:.3,d:1,s:.3},{w:"square",v:22,t:11,d:.5,s:.1,g:1}],[{w:"sawtooth",v:.3,a:.04,d:1,s:.8,r:.1},{w:"square",v:1,t:.5,d:1,s:2,g:1}],[{w:"triangle",v:.3,d:1,s:.3},{w:"sine",v:22,t:6,d:.6,s:.05,g:1}],[{w:"sine",v:.6,a:.1,d:.05,s:.4},{w:"sine",v:5,t:5,f:1,d:.05,s:.3,g:1}],[{w:"sine",a:.1,d:.05,s:.4,v:.8},{w:"sine",v:5,t:5,f:1,d:.05,s:.3,g:1}],[{w:"square",v:.3,a:.1,d:.1,s:.4},{w:"square",v:1,f:1,d:.3,s:.1,g:1}],[{w:"sawtooth",v:.3,d:.5,r:.5},{w:"sawtooth",v:11,t:5,d:.05,g:1}],[{w:"square",v:.3,d:.2,r:.2},{w:"square",v:7,t:3,d:.05,g:1}],[{w:"triangle",d:.2,r:.2},{w:"square",v:9,t:3,d:.1,r:.1,g:1}],[{w:"triangle",d:.3,r:.3},{w:"square",v:6,t:3,d:1,r:1,g:1}],[{w:"triangle",v:.4,d:.2,r:.2},{w:"square",v:22,t:12,d:.1,r:.1,g:1}],[{w:"sine",v:.25,a:.02,d:.05,s:.8},{w:"square",v:1,t:2,d:.03,s:11,g:1}],[{w:"sine",v:.3,a:.05,d:11},{w:"square",v:7,t:3,f:1,s:.7,g:1}],[{w:"square",v:.3,a:.05,d:.1,s:.8},{w:"square",v:4,d:.1,s:1.1,g:1}],[{w:"sine",v:.4,d:.3,r:.3},{w:"sine",v:7,t:9,d:.1,r:.1,g:1}],[{w:"sine",v:.7,d:.1,r:.1},{w:"sine",v:22,t:7,d:.05,g:1}],[{w:"sine",v:.6,d:.15,r:.15},{w:"square",v:11,t:3.2,d:.1,r:.1,g:1}],[{w:"sine",v:.8,d:.07,r:.07},{w:"square",v:11,t:7,r:.01,g:1}],[{w:"triangle",v:.7,t:.5,d:.2,r:.2,p:.95},{w:"n0",v:9,g:1,d:.2,r:.2}],[{w:"sine",v:.7,d:.1,r:.1,p:.9},{w:"square",v:14,t:2,d:.005,r:.005,g:1}],[{w:"square",d:.15,r:.15,p:.5},{w:"square",v:4,t:5,d:.001,r:.001,g:1}],[{w:"n1",v:.3,a:1,s:1,d:.15,r:0,t:.5}],[{w:"sine",t:12.5,d:0,r:0,p:.5,v:.3,h:.2,q:.5},{g:1,w:"sine",v:1,t:2,d:0,r:0,s:1},{g:1,w:"n0",v:.2,t:2,a:.6,h:0,d:.1,r:.1,b:0,c:0}],[{w:"n0",v:.2,a:.05,h:.02,d:.02,r:.02}],[{w:"n0",v:.4,a:1,d:1,t:.25}],[{w:"sine",v:.3,a:.1,d:1,s:.5},{w:"sine",v:4,t:0,f:1.5,d:1,s:1,r:.1,g:1},{g:1,w:"sine",v:4,t:0,f:2,a:.6,h:0,d:.1,s:1,r:.1,b:0,c:0}],[{w:"square",v:.3,t:.25,d:11,s:1},{w:"square",v:12,t:0,f:8,d:1,s:1,r:11,g:1}],[{w:"n0",v:.4,t:.5,a:1,d:11,s:1,r:.5},{w:"square",v:1,t:0,f:14,d:1,s:1,r:11,g:1}],[{w:"sine",t:0,f:1221,a:.2,d:1,r:.25,s:1},{g:1,w:"n0",v:3,t:.5,d:1,s:1,r:1}],[{w:"sine",d:.4,r:.4,p:.1,t:2.5,v:1},{w:"n0",v:12,t:2,d:1,r:1,g:1}],],program0:[[{w:"triangle",v:.5,d:.7}],[{w:"triangle",v:.5,d:.7}],[{w:"triangle",v:.5,d:.7}],[{w:"triangle",v:.5,d:.7}],[{w:"triangle",v:.5,d:.7}],[{w:"triangle",v:.5,d:.7}],[{w:"sawtooth",v:.3,d:.7}],[{w:"sawtooth",v:.3,d:.7}],[{w:"sine",v:.5,d:.3,r:.3}],[{w:"triangle",v:.5,d:.3,r:.3}],[{w:"square",v:.2,d:.3,r:.3}],[{w:"square",v:.2,d:.3,r:.3}],[{w:"sine",v:.5,d:.1,r:.1}],[{w:"sine",v:.5,d:.1,r:.1}],[{w:"square",v:.2,d:1,r:1}],[{w:"sawtooth",v:.3,d:.7,r:.7}],[{w:"sine",v:.5,a:.01,s:1}],[{w:"sine",v:.7,d:.02,s:.7}],[{w:"square",v:.2,s:1}],[{w:"triangle",v:.5,a:.01,s:1}],[{w:"square",v:.2,a:.02,s:1}],[{w:"square",v:.2,a:.02,s:1}],[{w:"square",v:.2,a:.02,s:1}],[{w:"square",v:.2,a:.05,s:1}],[{w:"triangle",v:.5,d:.5}],[{w:"square",v:.2,d:.6}],[{w:"square",v:.2,d:.6}],[{w:"triangle",v:.8,d:.6}],[{w:"triangle",v:.4,d:.05}],[{w:"square",v:.2,d:1}],[{w:"square",v:.2,d:1}],[{w:"sine",v:.4,d:.6}],[{w:"triangle",v:.7,d:.4}],[{w:"triangle",v:.7,d:.7}],[{w:"triangle",v:.7,d:.7}],[{w:"triangle",v:.7,d:.7}],[{w:"square",v:.3,d:.2}],[{w:"square",v:.3,d:.2}],[{w:"square",v:.3,d:.1,s:.2}],[{w:"sawtooth",v:.4,d:.1,s:.2}],[{w:"sawtooth",v:.2,a:.02,s:1}],[{w:"sawtooth",v:.2,a:.02,s:1}],[{w:"sawtooth",v:.2,a:.02,s:1}],[{w:"sawtooth",v:.2,a:.02,s:1}],[{w:"sawtooth",v:.2,a:.02,s:1}],[{w:"sawtooth",v:.3,d:.1}],[{w:"sawtooth",v:.3,d:.5,r:.5}],[{w:"triangle",v:.6,d:.1,r:.1,h:.03,p:.8}],[{w:"sawtooth",v:.2,a:.02,s:1}],[{w:"sawtooth",v:.2,a:.02,s:1}],[{w:"sawtooth",v:.2,a:.02,s:1}],[{w:"sawtooth",v:.2,a:.02,s:1}],[{w:"triangle",v:.3,a:.03,s:1}],[{w:"sine",v:.3,a:.03,s:1}],[{w:"triangle",v:.3,a:.05,s:1}],[{w:"sawtooth",v:.5,a:.01,d:.1}],[{w:"square",v:.3,a:.05,d:.2,s:.6}],[{w:"square",v:.3,a:.05,d:.2,s:.6}],[{w:"square",v:.3,a:.05,d:.2,s:.6}],[{w:"square",v:.2,a:.05,d:.01,s:1}],[{w:"square",v:.3,a:.05,s:1}],[{w:"square",v:.3,s:.7}],[{w:"square",v:.3,s:.7}],[{w:"square",v:.3,s:.7}],[{w:"square",v:.3,a:.02,d:2}],[{w:"square",v:.3,a:.02,d:2}],[{w:"square",v:.3,a:.03,d:2}],[{w:"square",v:.3,a:.04,d:2}],[{w:"square",v:.3,a:.02,d:2}],[{w:"square",v:.3,a:.05,d:2}],[{w:"square",v:.3,a:.03,d:2}],[{w:"square",v:.3,a:.03,d:2}],[{w:"sine",v:.7,a:.02,d:2}],[{w:"sine",v:.7,a:.02,d:2}],[{w:"sine",v:.7,a:.02,d:2}],[{w:"sine",v:.7,a:.02,d:2}],[{w:"sine",v:.7,a:.02,d:2}],[{w:"sine",v:.7,a:.02,d:2}],[{w:"sine",v:.7,a:.02,d:2}],[{w:"sine",v:.7,a:.02,d:2}],[{w:"square",v:.3,s:.7}],[{w:"sawtooth",v:.4,s:.7}],[{w:"triangle",v:.5,s:.7}],[{w:"sawtooth",v:.4,s:.7}],[{w:"sawtooth",v:.4,d:12}],[{w:"sine",v:.4,a:.06,d:12}],[{w:"sawtooth",v:.4,d:12}],[{w:"sawtooth",v:.4,d:12}],[{w:"sawtooth",v:.3,d:12}],[{w:"triangle",v:.5,d:12}],[{w:"square",v:.3,d:12}],[{w:"triangle",v:.5,a:.08,d:11}],[{w:"sawtooth",v:.5,a:.05,d:11}],[{w:"sawtooth",v:.5,d:11}],[{w:"triangle",v:.5,d:11}],[{w:"triangle",v:.5,d:11}],[{w:"triangle",v:.5,d:11}],[{w:"triangle",v:.5,d:11}],[{w:"square",v:.3,d:11}],[{w:"sawtooth",v:.5,a:.04,d:11}],[{w:"sawtooth",v:.5,d:11}],[{w:"triangle",v:.5,a:.8,d:11}],[{w:"triangle",v:.5,d:11}],[{w:"square",v:.3,d:11}],[{w:"sawtooth",v:.3,d:1,r:1}],[{w:"sawtooth",v:.5,d:.3}],[{w:"sawtooth",v:.5,d:.3,r:.3}],[{w:"sawtooth",v:.5,d:.3,r:.3}],[{w:"square",v:.3,d:.2,r:.2}],[{w:"square",v:.3,a:.02,d:2}],[{w:"sawtooth",v:.2,a:.02,d:.7}],[{w:"triangle",v:.5,d:1}],[{w:"sawtooth",v:.3,d:.3,r:.3}],[{w:"sine",v:.8,d:.1,r:.1}],[{w:"square",v:.2,d:.1,r:.1,p:1.05}],[{w:"sine",v:.8,d:.05,r:.05}],[{w:"triangle",v:.5,d:.1,r:.1,p:.96}],[{w:"triangle",v:.5,d:.1,r:.1,p:.97}],[{w:"square",v:.3,d:.1,r:.1}],[{w:"n1",v:.3,a:1,s:1,d:.15,r:0,t:.5}],[{w:"triangle",v:.5,d:.03,t:0,f:1332,r:.001,p:1.1}],[{w:"n0",v:.2,t:.1,d:.02,a:.05,h:.02,r:.02}],[{w:"n0",v:.4,a:1,d:1,t:.25}],[{w:"sine",v:.3,a:.8,d:1,t:0,f:1832}],[{w:"triangle",d:.5,t:0,f:444,s:1}],[{w:"n0",v:.4,d:1,t:0,f:22,s:1}],[{w:"n0",v:.5,a:.2,d:11,t:0,f:44}],[{w:"n0",v:.5,t:.25,d:.4,r:.4}],],drummap1:[[{w:"triangle",t:0,f:70,v:1,d:.05,h:.03,p:.9,q:.1},{w:"n0",g:1,t:6,v:17,r:.01,h:0,p:0}],[{w:"triangle",t:0,f:88,v:1,d:.05,h:.03,p:.5,q:.1},{w:"n0",g:1,t:5,v:42,r:.01,h:0,p:0}],[{w:"n0",f:222,p:0,t:0,r:.01,h:0}],[{w:"triangle",v:.3,f:180,d:.05,t:0,h:.03,p:.9,q:.1},{w:"n0",v:.6,t:0,f:70,h:.02,r:.01,p:0},{g:1,w:"square",v:2,t:0,f:360,r:.01,b:0,c:0}],[{w:"square",f:1150,v:.34,t:0,r:.03,h:.025,d:.03},{g:1,w:"n0",t:0,f:13,h:.025,d:.1,s:1,r:.1,v:1}],[{w:"triangle",f:200,v:1,d:.06,t:0,r:.06},{w:"n0",g:1,t:0,f:400,v:12,r:.02,d:.02}],[{w:"triangle",f:100,v:.9,d:.12,h:.02,p:.5,t:0,r:.12},{g:1,w:"n0",v:5,t:.4,h:.015,d:.005,r:.005}],[{w:"n1",f:390,v:.25,r:.01,t:0}],[{w:"triangle",f:120,v:.9,d:.12,h:.02,p:.5,t:0,r:.12},{g:1,w:"n0",v:5,t:.5,h:.015,d:.005,r:.005}],[{w:"n1",v:.25,f:390,r:.03,t:0,h:.005,d:.03}],[{w:"triangle",f:140,v:.9,d:.12,h:.02,p:.5,t:0,r:.12},{g:1,w:"n0",v:5,t:.3,h:.015,d:.005,r:.005}],[{w:"n1",v:.25,f:390,t:0,d:.2,r:.2},{w:"n0",v:.3,t:0,c:0,f:440,h:.005,d:.05}],[{w:"triangle",f:155,v:.9,d:.12,h:.02,p:.5,t:0,r:.12},{g:1,w:"n0",v:5,t:.3,h:.015,d:.005,r:.005}],[{w:"triangle",f:180,v:.9,d:.12,h:.02,p:.5,t:0,r:.12},{g:1,w:"n0",v:5,t:.3,h:.015,d:.005,r:.005}],[{w:"n1",v:.3,f:1200,d:.2,r:.2,h:.05,t:0},{w:"n1",t:0,v:1,d:.1,r:.1,p:1.2,f:440}],[{w:"triangle",f:220,v:.9,d:.12,h:.02,p:.5,t:0,r:.12},{g:1,w:"n0",v:5,t:.3,h:.015,d:.005,r:.005}],[{w:"n1",f:500,v:.15,d:.4,r:.4,h:0,t:0},{w:"n0",v:.1,t:0,r:.01,f:440}],[{w:"n1",v:.3,f:800,d:.2,r:.2,h:.05,t:0},{w:"square",t:0,v:1,d:.1,r:.1,p:.1,f:220,g:1}],[{w:"sine",f:1651,v:.15,d:.2,r:.2,h:0,t:0},{w:"sawtooth",g:1,t:1.21,v:7.2,d:.1,r:11,h:1},{g:1,w:"n0",v:3.1,t:.152,d:.002,r:.002}],null,[{w:"n1",v:.3,f:1200,d:.2,r:.2,h:.05,t:0},{w:"n1",t:0,v:1,d:.1,r:.1,p:1.2,f:440}],null,[{w:"n1",v:.3,f:555,d:.25,r:.25,h:.05,t:0},{w:"n1",t:0,v:1,d:.1,r:.1,f:440,a:.005,h:.02}],[{w:"sawtooth",f:776,v:.2,d:.3,t:0,r:.3},{g:1,w:"n0",v:2,t:0,f:776,a:.005,h:.02,d:.1,s:1,r:.1,c:0},{g:11,w:"sine",v:.1,t:0,f:22,d:.3,r:.3,b:0,c:0}],[{w:"n1",f:440,v:.15,d:.4,r:.4,h:0,t:0},{w:"n0",v:.4,t:0,r:.01,f:440}],null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,[{w:"sine",f:1720,v:.3,d:.02,t:0,r:.02},{w:"square",g:1,t:0,f:2876,v:6,d:.2,s:1,r:.2}],[{w:"sine",f:1720,v:.3,d:.25,t:0,r:.25},{w:"square",g:1,t:0,f:2876,v:6,d:.2,s:1,r:.2}],],drummap0:[[{w:"triangle",t:0,f:110,v:1,d:.05,h:.02,p:.1}],[{w:"triangle",t:0,f:150,v:.8,d:.1,p:.1,h:.02,r:.01}],[{w:"n0",f:392,v:.5,d:.01,p:0,t:0,r:.05}],[{w:"n0",f:33,d:.05,t:0}],[{w:"n0",f:100,v:.7,d:.03,t:0,r:.03,h:.02}],[{w:"n0",f:44,v:.7,d:.02,p:.1,t:0,h:.02}],[{w:"triangle",f:240,v:.9,d:.1,h:.02,p:.1,t:0}],[{w:"n0",f:440,v:.2,r:.01,t:0}],[{w:"triangle",f:270,v:.9,d:.1,h:.02,p:.1,t:0}],[{w:"n0",f:440,v:.2,d:.04,r:.04,t:0}],[{w:"triangle",f:300,v:.9,d:.1,h:.02,p:.1,t:0}],[{w:"n0",f:440,v:.2,d:.1,r:.1,h:.02,t:0}],[{w:"triangle",f:320,v:.9,d:.1,h:.02,p:.1,t:0}],[{w:"triangle",f:360,v:.9,d:.1,h:.02,p:.1,t:0}],[{w:"n0",f:150,v:.2,d:.1,r:.1,h:.05,t:0,p:.1}],[{w:"triangle",f:400,v:.9,d:.1,h:.02,p:.1,t:0}],[{w:"n0",f:150,v:.2,d:.1,r:.01,h:.05,t:0,p:.1}],[{w:"n0",f:150,v:.2,d:.1,r:.01,h:.05,t:0,p:.1}],[{w:"n0",f:440,v:.3,d:.1,p:.9,t:0,r:.1}],[{w:"n0",f:200,v:.2,d:.05,p:.9,t:0}],[{w:"n0",f:440,v:.3,d:.12,p:.9,t:0}],[{w:"sine",f:800,v:.4,d:.06,t:0}],[{w:"n0",f:150,v:.2,d:.1,r:.01,h:.05,t:0,p:.1}],[{w:"n0",f:33,v:.3,d:.2,p:.9,t:0}],[{w:"n0",f:300,v:.3,d:.14,p:.9,t:0}],[{w:"sine",f:200,d:.06,t:0}],[{w:"sine",f:150,d:.06,t:0}],[{w:"sine",f:300,t:0}],[{w:"sine",f:300,d:.06,t:0}],[{w:"sine",f:250,d:.06,t:0}],[{w:"square",f:300,v:.3,d:.06,p:.8,t:0}],[{w:"square",f:260,v:.3,d:.06,p:.8,t:0}],[{w:"sine",f:850,v:.5,d:.07,t:0}],[{w:"sine",f:790,v:.5,d:.07,t:0}],[{w:"n0",f:440,v:.3,a:.05,t:0}],[{w:"n0",f:440,v:.3,a:.05,t:0}],[{w:"triangle",f:1800,v:.4,p:.9,t:0,h:.03}],[{w:"triangle",f:1800,v:.3,p:.9,t:0,h:.13}],[{w:"n0",f:330,v:.3,a:.02,t:0,r:.01}],[{w:"n0",f:330,v:.3,a:.02,t:0,h:.04,r:.01}],[{w:"n0",f:440,v:.3,t:0}],[{w:"sine",f:800,t:0}],[{w:"sine",f:700,t:0}],[{w:"n0",f:330,v:.3,t:0}],[{w:"n0",f:330,v:.3,t:0,h:.1,r:.01,p:.7}],[{w:"sine",t:0,f:1200,v:.3,r:.01}],[{w:"sine",t:0,f:1200,v:.3,d:.2,r:.2}],],_guiInit:()=>{this.canvas&&(this.ctx=this.canvas.getContext("2d"),this.ctx.fillStyle="#000",this.ctx.fillRect(0,0,300,32),this.canvas.addEventListener("dragover",this.dragOver.bind(this),!1),this.canvas.addEventListener("dragleave",this.dragLeave.bind(this),!1),this.canvas.addEventListener("drop",this.execDrop.bind(this),!1),this.canvas.addEventListener("click",this.click.bind(this),!1),this.canvas.addEventListener("mousedown",this.pointerdown.bind(this),!1),this.canvas.addEventListener("mousemove",this.pointermove.bind(this),!1),this.canvas.addEventListener("touchstart",this.pointerdown.bind(this),!1),this.canvas.addEventListener("touchend",this.pointerup.bind(this),!1),this.canvas.addEventListener("touchcancel",this.pointerup.bind(this),!1),this.canvas.addEventListener("touchmove",this.pointermove.bind(this),!1))},_guiUpdate:()=>{if(this.canvas){this.ctx.fillStyle="#000",this.ctx.fillRect(0,0,300,32);var t=8,$=20;if(this.song?(t=4,$=24):(this.ctx.fillStyle="#fff",this.ctx.fillText("TinySynth",8,20)),this.graph){this.ctx.fillStyle="#800",this.ctx.fillRect(80,t,132,4),this.ctx.fillRect(80,$,132,4),this.ctx.fillStyle="#f00";for(let e=this.notetab.length-1;e>=0;--e){let s=this.notetab[e];(!s.f||this.rhythm[s.ch])&&(this.ctx.fillRect(80+s.n,t,4,4),this.ctx.fillRect(80+8*s.ch,$,6,4))}}this.perfmon&&(this.ctx.fillStyle="#fff",this.ctx.fillRect(180,30,28,-12),this.ctx.fillStyle="#000",this.ctx.fillText(this.notetab.length,185,28)),this.ctx.fillStyle="#fff",this.ctx.fillRect(250,15,32,2),this.ctx.fillStyle="#fff",this.ctx.strokeStyle="#000",this.ctx.beginPath(),this.ctx.arc(250+32*this.masterVol,16,6,0,6.28,0),this.ctx.moveTo(220,12),this.ctx.lineTo(224,12),this.ctx.lineTo(230,6),this.ctx.lineTo(230,26),this.ctx.lineTo(224,20),this.ctx.lineTo(220,20),this.ctx.fill(),this.ctx.stroke(),this.ctx.strokeStyle="#fff",this.ctx.lineWidth=2,this.ctx.beginPath(),this.ctx.arc(230,16,4,-1,1,!1),this.ctx.stroke(),this.ctx.beginPath(),this.ctx.arc(230,16,8,-1,1,!1),this.ctx.stroke(),0==this.masterVol&&(this.ctx.strokeStyle="#000",this.ctx.lineWidth=4,this.ctx.beginPath(),this.ctx.moveTo(220,7),this.ctx.lineTo(238,25),this.ctx.stroke(),this.ctx.strokeStyle="#fff",this.ctx.lineWidth=2,this.ctx.stroke()),this.song&&(this.ctx.fillStyle="#fff",this.ctx.fillRect(4,2,28,28),this.ctx.fillRect(80,15,128,2),this.ctx.fillStyle="#000",this.playing?(this.ctx.fillRect(12,10,4,12),this.ctx.fillRect(22,10,4,12)):(this.ctx.beginPath(),this.ctx.moveTo(12,9),this.ctx.lineTo(25,16),this.ctx.lineTo(12,23),this.ctx.fill()),this.ctx.fillStyle="#fff",this.ctx.fillText(this.toTime(this.playTick),38,14),this.ctx.fillText(this.toTime(this.maxTick),38,28),this.ctx.strokeStyle="#000",this.ctx.beginPath(),this.ctx.arc(80+this.playTick/this.maxTick*128,16,6,0,6.28,0),this.ctx.fill(),this.ctx.stroke()),this.waitdrop&&(this.ctx.fillStyle="rgba(0,0,0,0.7)",this.ctx.fillRect(0,0,300,32),this.ctx.fillStyle="#fff",this.ctx.fillText("Drop MIDI File Here",100,20))}},toTime:t=>{t=240*t/this.song.timebase/this.song.tempo|0;let $=t/60|0,e=t%60;return("00"+$).substr(-2)+":"+("00"+e).substr(-2)},preventScroll(t){t.preventDefault()},pointerup:t=>{document.body.removeEventListener("touchstart",this.preventScroll,!1)},getPos(t){var $=t.target.getBoundingClientRect();return $.right!=$.left?{x:(t.clientX-$.left)*300/($.right-$.left),y:t.clientY-$.top}:{x:0,y:0}},pointerdown:t=>{let $=t;if(t.touches&&($=t.touches[0]),this.downpos=this.getPos($),t.touches||1&$.buttons){if(this.song&&this.downpos.x>=80&&this.downpos.x<=208){let e=(this.downpos.x-80)/128*this.maxTick;this.locateMIDI(e),document.body.addEventListener("touchstart",this.preventScroll,!1)}if(this.downpos.x>=250&&this.downpos.x<282){let s=(this.downpos.x-250)/32;this.setMasterVol(s),document.body.addEventListener("touchstart",this.preventScroll,!1)}}},pointermove:t=>{let $=t;if(t.touches&&($=t.touches[0]),t.touches||1&$.buttons){let e=this.getPos($);if(this.song&&e.x>=70&&e.x<=208){e.x<80&&(e.x=80);let s=(e.x-80)/128*this.maxTick;this.locateMIDI(s)}if(e.x>=250&&e.x<282){let i=(e.x-250)/32;this.setMasterVol(i)}}},click:t=>{let $=this.getPos(t);$.x<40&&this.song&&(this.playing?this.stopMIDI():this.song&&this.playMIDI()),$.x>=215&&$.x<243&&this.downpos.x>=215&&this.downpos.x<243&&(this.masterVol>0?(this.lastMasterVol=this.masterVol,this.masterVol=0):this.masterVol=this.lastMasterVol)},dragLeave:t=>{this.waitdrop=0},dragOver:t=>{this.waitdrop=1,t.stopPropagation(),t.preventDefault(),t.dataTransfer.dropEffect="copy"},execDrop:t=>{this.waitdrop=0;let $=t.dataTransfer.files;if(0==this.disabledrop){var e=new FileReader;e.onload=(function(t){this.loadMIDI(e.result)}).bind(this),e.readAsArrayBuffer($[0])}t.stopPropagation(),t.preventDefault()},ready:()=>new Promise(t=>{let $=setInterval(()=>{this.isReady&&(clearInterval($),this.debug&&console.log("Initialized."),t())},100)}),init:()=>{this.pg=[],this.vol=[],this.ex=[],this.bend=[],this.rpnidx=[],this.brange=[],this.sustain=[],this.notetab=[],this.rhythm=[],this.masterTuningC=0,this.masterTuningF=0,this.tuningC=[],this.tuningF=[],this.scaleTuning=[],this.maxTick=0,this.playTick=0,this.playing=0,this.releaseRatio=3.5;for(let $=0;$<16;++$)this.pg[$]=0,this.vol[$]=1.86000372000744,this.bend[$]=0,this.brange[$]=256,this.tuningC[$]=0,this.tuningF[$]=0,this.scaleTuning[$]=[0,0,0,0,0,0,0,0,0,0,0,0],this.rhythm[$]=0;this.rhythm[9]=1,this.preroll=.2,this.relcnt=0,setInterval((function(){if(++this.relcnt>=3){this.relcnt=0;for(let t=this.notetab.length-1;t>=0;--t){var $=this.notetab[t];this.actx.currentTime>$.e&&(this._pruneNote($),this.notetab.splice(t,1))}}if(this.playing&&this.song.ev.length>0){let e=this.song.ev[this.playIndex];for(;this.actx.currentTime+this.preroll>this.playTime;)if(65361==e.m[0]?(this.song.tempo=e.m[1],this.tick2Time=240/this.song.tempo/this.song.timebase):this.send(e.m,this.playTime),++this.playIndex,this.playIndex>=this.song.ev.length){if(this.loop)e=this.song.ev[this.playIndex=0],this.playTick=e.t;else{this.playTick=this.maxTick,this.playing=0;break}}else e=this.song.ev[this.playIndex],this.playTime+=(e.t-this.playTick)*this.tick2Time,this.playTick=e.t}}).bind(this),60),this.debug&&console.log("internalcontext:"+this.internalcontext),this.internalcontext&&(t.AudioContext=t.AudioContext||t.webkitAudioContext,this.setAudioContext(new AudioContext)),this.isReady=1},setMasterVol:t=>{void 0!=t&&(this.masterVol=t),this.out&&(this.out.gain.value=this.masterVol)},setReverbLev:t=>{void 0!=t&&(this.reverbLev=t);var $=parseFloat(this.reverbLev);this.rev&&!isNaN($)&&(this.rev.gain.value=8*$)},setLoop:t=>{this.loop=t},setVoices:t=>{this.voices=t},getPlayStatus:()=>({play:this.playing,maxTick:this.maxTick,curTick:this.playTick}),locateMIDI:t=>{let $,e=this.playing;for(this.stopMIDI(),$=0;$<this.song.ev.length&&t>this.song.ev[$].t;++$){var s=this.song.ev[$],i=15&s.m[0];switch(240&s.m[0]){case 176:switch(s.m[1]){case 1:this.setModulation(i,s.m[2]);break;case 7:this.setChVol(i,s.m[2]);break;case 10:this.setPan(i,s.m[2]);break;case 11:this.setExpression(i,s.m[2]);break;case 64:this.setSustain(i,s.m[2])}break;case 192:this.pg[15&s.m[0]]=s.m[1]}65361==s.m[0]&&(this.song.tempo=s.m[1])}this.song.ev[$]?(this.playIndex=$,this.playTick=this.song.ev[$].t):(this.playIndex=0,this.playTick=this.maxTick),e&&this.playMIDI()},getTimbreName:(t,$)=>0==t?this.program[$].name:this.drummap[$-35].name,loadMIDIfromSrc:()=>{this.loadMIDIUrl(this.src)},loadMIDIUrl:t=>{if(t){var $=new XMLHttpRequest;$.open("GET",t,!0),$.responseType="arraybuffer",$.loadMIDI=this.loadMIDI.bind(this),$.onload=function(t){200==this.status&&this.loadMIDI(this.response)},$.send()}},reset:()=>{for(let t=0;t<16;++t)this.setProgram(t,0),this.setBendRange(t,256),this.setModulation(t,0),this.setChVol(t,100),this.setPan(t,64),this.resetAllControllers(t),this.allSoundOff(t),this.rhythm[t]=0,this.tuningC[t]=0,this.tuningF[t]=0;this.masterTuningC=0,this.masterTuningF=0,this.rhythm[9]=1},stopMIDI:()=>{this.playing=0;for(var t=0;t<16;++t)this.allSoundOff(t)},playMIDI:()=>{if(!this.song)return;let t=this.actx.createOscillator();t.connect(this.actx.destination),t.frequency.value=0,t.start(0),t.stop(this.actx.currentTime+.001),this.playTick>=this.maxTick&&(this.playTick=0,this.playIndex=0),this.playTime=this.actx.currentTime+.1,this.tick2Time=240/this.song.tempo/this.song.timebase,this.playing=1},loadMIDI:t=>{function $(t,$){return(t[$]<<8)+t[$+1]}function e(t,$){return(t[$]<<24)+(t[$+1]<<16)+(t[$+2]<<8)+t[$+3]}function s(t,$,e){return String.fromCharCode.apply(null,t.slice($,$+e))}function i(t,$){var e,s;for(e=0,r=1;128&(s=t[$]);)e=(e<<7)+(127&s),++r,++$;return(e<<7)+s}function a(t,$,e,a){var n=e[a];switch(r=1,(128&n)==0&&(n=h,r=0),h=n,240&n){case 192:case 208:t.ev.push({t:$,m:[n,e[a+r]]}),r+=1;break;case 240:switch(n){case 240:case 247:var o=i(e,a+1);_=1+r;var l=Array.from(e.slice(a+_,a+_+o));l.unshift(240),t.ev.push({t:$,m:l}),r+=o+1;break;case 255:var o=i(e,a+2);switch(_=2+r,r=o+r+2,e[a+1]){case 2:t.copyright+=s(e,a+_,r-3);break;case 1:case 3:case 4:case 9:t.text=s(e,a+_,r-_);break;case 47:return 1;case 81:var d,v,w=Math.floor(6e7/(d=e,(d[v=a+3]<<16)+(d[v+1]<<8)+d[v+2]));t.ev.push({t:$,m:[65361,w]})}}break;default:t.ev.push({t:$,m:[n,e[a+r],e[a+r+1]]}),r+=2}return 0}this.stopMIDI();var n=new Uint8Array(t),r=0,_=0,h=144,o=0,l=n.slice(0,4);if("77,84,104,100"==l.toString()){var d=e(n,4);$(n,8);var v=$(n,10);this.maxTick=0;var w=4*$(n,12);o=d+8,this.song={copyright:"",text:"",tempo:120,timebase:w,ev:[]};for(let c=0;c<v;++c){if(l=n.slice(o,o+4),d=e(n,o+4),"77,84,114,107"==l.toString()){var g=0,u=0;for(this.notetab.length=0;;){g+=i(n,o+8+u),u+=r;var f=a(this.song,g,n,o+8+u);if(u+=r,f)break}g>this.maxTick&&(this.maxTick=g)}o+=d+8}this.song.ev.sort(function(t,$){return t.t-$.t}),this.reset(),this.locateMIDI(0)}},setQuality:t=>{void 0!=t&&(this.quality=t);for(let $=0;$<128;++$)this.setTimbre(0,$,this.program0[$]);for(let e=0;e<this.drummap0.length;++e)this.setTimbre(1,e+35,this.drummap0[e]);if(this.quality){for(let s=0;s<this.program1.length;++s)this.setTimbre(0,s,this.program1[s]);for(let i=0;i<this.drummap.length;++i)this.drummap1[i]&&this.setTimbre(1,i+35,this.drummap1[i])}},setTimbre:(t,$,e)=>{let s={g:0,w:"sine",t:1,f:0,v:.5,a:0,h:.01,d:.01,s:0,r:.05,p:1,q:1,k:0};function i(t){for($=0;$<t.length;++$)for(let e in s)t[$].hasOwnProperty(e)&&void 0!==t[$][e]||(t[$][e]=s[e]);return t}t&&$>=35&&$<=81&&(this.drummap[$-35].p=i(e)),0==t&&$>=0&&$<=127&&(this.program[$].p=i(e))},_pruneNote:t=>{for(let $=t.o.length-1;$>=0;--$){if(t.o[$].frequency?t.o[$].frequency.cancelScheduledValues(0):t.o[$].playbackRate.cancelScheduledValues(0),t.g[$].gain.cancelScheduledValues(0),t.o[$].stop(),t.o[$].detune)try{this.chmod[t.ch].disconnect(t.o[$].detune)}catch(e){}t.g[$].gain.value=0}},_limitVoices:(t,$)=>{this.notetab.sort(function(t,$){return t.f!=$.f?t.f-$.f:t.e!=$.e?$.e-t.e:$.t-t.t});for(let e=this.notetab.length-1;e>=0;--e){var s=this.notetab[e];(this.actx.currentTime>s.e||e>=this.voices-1)&&(this._pruneNote(s),this.notetab.splice(e,1))}},_note:(t,$,e,s,i)=>{let a,n,r,_=[],h=[],o=[],l=[],d=[],v=440*Math.pow(2,(e-69+this.masterTuningC+this.tuningC[$]+(this.masterTuningF+this.tuningF[$]/8192+this.scaleTuning[$][e%12]))/12);this._limitVoices($,e);for(let w=0;w<i.length;++w){r=i[w];let c=t+r.a+r.h;(0==r.g?(a=this.chvol[$],n=s*s/16384,l[w]=v*r.t+r.f):r.g>10?(a=h[r.g-11].gain,n=1,l[w]=l[r.g-11]*r.t+r.f):_[r.g-1].frequency?(a=_[r.g-1].frequency,n=l[r.g-1],l[w]=l[r.g-1]*r.t+r.f):(a=_[r.g-1].playbackRate,n=l[r.g-1]/440,l[w]=l[r.g-1]*r.t+r.f),"n"===r.w[0])?(_[w]=this.actx.createBufferSource(),_[w].buffer=this.noiseBuf[r.w],_[w].loop=!0,_[w].playbackRate.value=l[w]/440,1!=r.p&&this._setParamTarget(_[w].playbackRate,l[w]/440*r.p,t,r.q),_[w].detune&&(this.chmod[$].connect(_[w].detune),_[w].detune.value=this.bend[$])):(_[w]=this.actx.createOscillator(),_[w].frequency.value=l[w],1!=r.p&&this._setParamTarget(_[w].frequency,l[w]*r.p,t,r.q),"w"==r.w[0]?_[w].setPeriodicWave(this.wave[r.w]):_[w].type=r.w,_[w].detune&&(this.chmod[$].connect(_[w].detune),_[w].detune.value=this.bend[$])),h[w]=this.actx.createGain(),d[w]=r.r,_[w].connect(h[w]),h[w].connect(a),o[w]=n*r.v,r.k&&(o[w]*=Math.pow(2,(e-60)/12*r.k)),r.a?(h[w].gain.value=0,h[w].gain.setValueAtTime(0,t),h[w].gain.linearRampToValueAtTime(o[w],t+r.a)):h[w].gain.setValueAtTime(o[w],t),this._setParamTarget(h[w].gain,r.s*o[w],c,r.d),_[w].start(t),this.rhythm[$]&&(_[w].onended=()=>{try{_[w].detune&&this.chmod[$].disconnect(_[w].detune)}catch(t){}},_[w].stop(t+i[0].d*this.releaseRatio))}this.rhythm[$]||this.notetab.push({t:t,e:99999,ch:$,n:e,o:_,g:h,t2:t+r.a,v:o,r:d,f:0})},_setParamTarget(t,$,e,s){0!=s?t.setTargetAtTime($,e,s):t.setValueAtTime($,e)},_releaseNote:(t,$)=>{if(9!=t.ch)for(let e=t.g.length-1;e>=0;--e)t.g[e].gain.cancelScheduledValues($),$==t.t2?t.g[e].gain.setValueAtTime(t.v[e],$):$<t.t2&&t.g[e].gain.setValueAtTime(t.v[e]*($-t.t)/(t.t2-t.t),$),this._setParamTarget(t.g[e].gain,0,$,t.r[e]);t.e=$+t.r[0]*this.releaseRatio,t.f=1},setModulation:(t,$,e)=>{this.chmod[t].gain.setValueAtTime(100*$/127,this._tsConv(e))},setChVol:(t,$,e)=>{this.vol[t]=3*$*$/16129,this.chvol[t].gain.setValueAtTime(this.vol[t]*this.ex[t],this._tsConv(e))},setPan:(t,$,e)=>{this.chpan[t]&&this.chpan[t].pan.setValueAtTime(($-64)/64,this._tsConv(e))},setExpression:(t,$,e)=>{this.ex[t]=$*$/16129,this.chvol[t].gain.setValueAtTime(this.vol[t]*this.ex[t],this._tsConv(e))},setSustain:(t,$,e)=>{if(this.sustain[t]=$,e=this._tsConv(e),$<64)for(let s=this.notetab.length-1;s>=0;--s){let i=this.notetab[s];e>=i.t&&i.ch==t&&1==i.f&&this._releaseNote(i,e)}},allSoundOff:t=>{for(let $=this.notetab.length-1;$>=0;--$){let e=this.notetab[$];e.ch==t&&(this._pruneNote(e),this.notetab.splice($,1))}},resetAllControllers:t=>{this.bend[t]=0,this.ex[t]=1,this.rpnidx[t]=16383,this.sustain[t]=0,this.chvol[t]&&(this.chvol[t].gain.value=this.vol[t]*this.ex[t],this.chmod[t].gain.value=0)},setBendRange:(t,$)=>{this.brange[t]=$},setProgram:(t,$)=>{this.debug&&console.log("Pg("+t+")="+$),this.pg[t]=$},_tsConv:t=>(void 0==t||t<=0?(t=0,this.actx&&(t=this.actx.currentTime)):this.tsmode&&(t=.001*t-this.tsdiff),t),setBend:(t,$,e)=>{e=this._tsConv(e);let s=100*this.brange[t]/127;this.bend[t]=($-8192)*s/8192;for(let i=this.notetab.length-1;i>=0;--i){let a=this.notetab[i];if(a.ch==t)for(let n=a.o.length-1;n>=0;--n)a.o[n].frequency&&a.o[n].detune&&a.o[n].detune.setValueAtTime(this.bend[t],e)}},noteOff:(t,$,e)=>{if(!this.rhythm[t]){e=this._tsConv(e);for(let s=this.notetab.length-1;s>=0;--s){let i=this.notetab[s];e>=i.t&&i.ch==t&&i.n==$&&0==i.f&&(i.f=1,this.sustain[t]<64&&this._releaseNote(i,e))}}},noteOn:(t,$,e,s)=>{if(0==e){this.noteOff(t,$,s);return}if(s=this._tsConv(s),this.rhythm[t]){$>=35&&$<=81&&this._note(s,t,$,e,this.drummap[$-35].p);return}this._note(s,t,$,e,this.program[this.pg[t]].p)},setTsMode:t=>{this.tsmode=t},send:(t,$)=>{let e=15&t[0],s=-16&t[0];if(!(s<128)&&!(s>=256))switch("suspended"==this.audioContext.state&&this.audioContext.resume(),s){case 176:switch(t[1]){case 1:this.setModulation(e,t[2],$);break;case 7:this.setChVol(e,t[2],$);break;case 10:this.setPan(e,t[2],$);break;case 11:this.setExpression(e,t[2],$);break;case 64:this.setSustain(e,t[2],$);break;case 98:case 99:this.rpnidx[e]=16383;break;case 100:this.rpnidx[e]=16256&this.rpnidx[e]|t[2];break;case 101:this.rpnidx[e]=127&this.rpnidx[e]|t[2]<<7;break;case 6:switch(this.rpnidx[e]){case 0:this.brange[e]=(t[2]<<7)+(127&this.brange[e]);break;case 1:this.tuningF[e]=(t[2]<<7)+(this.tuningF[e]+8192&127)-8192;break;case 2:this.tuningC[e]=t[2]-64}break;case 38:switch(this.rpnidx[e]){case 0:this.brange[e]=16256&this.brange[e]|t[2];break;case 1:this.tuningF[e]=(this.tuningF[e]+8192&16256|t[2])-8192}break;case 120:case 123:case 124:case 125:case 126:case 127:this.allSoundOff(e);break;case 121:this.resetAllControllers(e)}break;case 192:this.setProgram(e,t[1]);break;case 224:this.setBend(e,t[1]+(t[2]<<7),$);break;case 144:this.noteOn(e,t[1],t[2],$);break;case 128:this.noteOff(e,t[1],$);break;case 240:if(255==t[0]){this.reset();break}if(254!=t[0]&&this.debug){var i=[];for(let a=0;a<t.length;++a)i.push(t[a].toString(16))}if(240==t[0]&&(127==t[1]&&4==t[3]&&(3==t[4]&&t.length>=8&&(this.masterTuningF=(128*t[6]+t[5]-8192)/8192),4==t[4]&&t.length>=8&&(this.masterTuningC=t[6]-64)),65==t[1]&&66==t[3]&&18==t[4]&&64==t[5])){if((240&t[6])==16&&11==t.length){let n=[9,0,1,2,3,4,5,6,7,8,10,11,12,13,14,15][15&t[6]];21==t[7]?this.rhythm[n]=t[8]:t[7]>=64&&t[7]<=75&&(this.scaleTuning[n][t[7]-64]=(t[8]-64)/100)}else 0==t[6]&&(0==t[7]&&14==t.length?this.masterTuningF=(4096*t[8]+256*t[9]+16*t[10]+t[11]-1024)/1e3:5==t[7]&&11==t.length&&(this.masterTuningC=t[8]-64))}}},_createWave:t=>{let $=new Float32Array(t.length),e=new Float32Array(t.length);for(let s=1;s<t.length;++s)$[s]=t[s];return this.actx.createPeriodicWave(e,$)},getAudioContext:()=>this.actx,setAudioContext:(t,$)=>{this.audioContext=this.actx=t,this.dest=$,$||(this.dest=t.destination),this.tsdiff=.001*performance.now()-this.actx.currentTime,this.debug&&console.log("TSDiff:"+this.tsdiff),this.out=this.actx.createGain(),this.comp=this.actx.createDynamicsCompressor();var e=.5*this.actx.sampleRate|0;this.convBuf=this.actx.createBuffer(2,e,this.actx.sampleRate),this.noiseBuf={},this.noiseBuf.n0=this.actx.createBuffer(1,e,this.actx.sampleRate),this.noiseBuf.n1=this.actx.createBuffer(1,e,this.actx.sampleRate);var s=this.convBuf.getChannelData(0),i=this.convBuf.getChannelData(1),a=this.noiseBuf.n0.getChannelData(0),n=this.noiseBuf.n1.getChannelData(0);for(let r=0;r<e;++r)r/e<Math.random()&&(s[r]=Math.exp(-3*r/e)*(Math.random()-.5)*.5,i[r]=Math.exp(-3*r/e)*(Math.random()-.5)*.5),a[r]=2*Math.random()-1;for(let _=0;_<64;++_){let h=10*Math.random()+1,o=10*Math.random()+1;for(let l=0;l<e;++l){var d=Math.sin(l/e*2*Math.PI*440*h)*Math.sin(l/e*2*Math.PI*440*o);n[l]+=d/8}}this.useReverb&&(this.conv=this.actx.createConvolver(),this.conv.buffer=this.convBuf,this.rev=this.actx.createGain(),this.rev.gain.value=this.reverbLev,this.out.connect(this.conv),this.conv.connect(this.rev),this.rev.connect(this.comp)),this.setMasterVol(),this.out.connect(this.comp),this.comp.connect(this.dest),this.chvol=[],this.chmod=[],this.chpan=[],this.wave={w9999:this._createWave("w9999")},this.lfo=this.actx.createOscillator(),this.lfo.frequency.value=5,this.lfo.start(0);for(let v=0;v<16;++v)this.chvol[v]=this.actx.createGain(),this.actx.createStereoPanner?(this.chpan[v]=this.actx.createStereoPanner(),this.chvol[v].connect(this.chpan[v]),this.chpan[v].connect(this.out)):(this.chpan[v]=null,this.chvol[v].connect(this.out)),this.chmod[v]=this.actx.createGain(),this.lfo.connect(this.chmod[v]),this.pg[v]=0,this.resetAllControllers(v);this.setReverbLev(),this.reset(),this.send([144,60,1]),this.send([144,60,0])}})}if(t&&t.customElements){class e extends HTMLElement{constructor(){super()}connectedCallback(){let t=document.createElement("div");t.innerHTML=`<canvas
id='wa-canvas' width='300' height='32'
touch-action='none' tabindex='0'
style='
position:relative;
margin:0;
border:none;
width:300px;
height:32px;
'
></canvas>
<div id='wa-logo'
style='
display:none;
position:absolute;
top:5px;
left:5px;
color:#fff;
font-size:8px;
background:rgba(0,0,0,0.5);
'
>TinySynth</div>`,this.getAttr=(t,$)=>{let e=this.getAttribute(t);if(""==e||null==e)return $;if("number"==typeof $){if("true"==e)return 1;if(isNaN(e=+e))return 0}return e},this.canvas=t.children[0],this.appendChild(t),$.bind(this)(this);let e=this.properties;for(let s in e){let i=e[s];i.observer?(this["_"+s]=i.value,Object.defineProperty(this,s,{get:()=>this["_"+s],set:t=>{this["_"+s]=t,this[i.observer]()}})):this[s]=i}for(let a in e){let n=e[a];this[a]=this.getAttr(a,n.value)}this.setQuality(1),this.init(),this._guiInit.bind(this)(),setInterval(this._guiUpdate.bind(this),100)}}t.customElements.get("webaudio-tinysynth")||t.customElements.define("webaudio-tinysynth",e)}class s{constructor(t){for(let e in $.bind(this)(this),this.properties)this[e]=this.properties[e].value;this.setQuality(1),t&&(void 0!=t.useReverb&&(this.useReverb=t.useReverb),void 0!=t.quality&&this.setQuality(t.quality),void 0!=t.voices&&this.setVoices(t.voices)),this.init()}}"object"==typeof exports&&"undefined"!=typeof module?module.exports=s:"function"==typeof define&&define.amd?define(function(){return s}):t.WebAudioTinySynth=s}(this);