Showing the changes to src/host_web (1 of 1 file). Show all
Unified · Split
| @@ -14,57 +14,76 @@ · 13 unmodified lines | ||
| 14 | 14 | <body onresize="chuckEditor.resize();"> |
| 15 | 15 | <div> |
| 16 | 16 | <div id="program">// MIDI 1: Note on / off events |
| 17 | −global Event noteOn, noteOff; | |
| 18 | −global int whichNote; | |
| 19 | −global int velocity; | |
| 17 | +global Event midiMessage; | |
| 18 | +global int midiCommand; | |
| 19 | +global int midiNote; | |
| 20 | +global int midiVelocity; | |
| 20 | 21 | |
| 21 | 22 | TriOsc t => ADSR adsr => dac; |
| 22 | 23 | adsr.set( 0.1::second, 0.3::second, 0.5, 0.7::second ); |
| 23 | 24 | false => int alreadyOn; |
| 24 | 25 | 0 => int mostRecentOn; |
| 25 | 26 | |
| 26 | −fun void RespondToNoteOn() | |
| 27 | +fun void RespondToMidi() | |
| 27 | 28 | { |
| 28 | 29 | while( true ) |
| 29 | 30 | { |
| 30 | − noteOn => now; | |
| 31 | − <<< "on:", whichNote, velocity >>>; | |
| 32 | − whichNote => Std.mtof => float newFreq; | |
| 33 | − velocity * 1.0 / 127.0 => float newGain; | |
| 34 | − | |
| 35 | − if( !alreadyOn ) | |
| 31 | + midiMessage => now; | |
| 32 | + if( midiCommand >= 144 && midiCommand < 160 ) | |
| 36 | 33 | { |
| 37 | − adsr.keyOn(1); | |
| 38 | − newFreq => t.freq; | |
| 39 | − newGain => adsr.gain; | |
| 40 | − whichNote => mostRecentOn; | |
| 41 | − true => alreadyOn; | |
| 34 | + if( midiVelocity > 0 ) | |
| 35 | + { | |
| 36 | + NoteOn( midiNote, midiVelocity ); | |
| 37 | + } | |
| 38 | + else | |
| 39 | + { | |
| 40 | + NoteOff( midiNote ); | |
| 41 | + } | |
| 42 | + } | |
| 43 | + else if( midiCommand >= 128 && midiCommand < 144 ) | |
| 44 | + { | |
| 45 | + NoteOff( midiNote ); | |
| 42 | 46 | } |
| 43 | 47 | else |
| 44 | 48 | { |
| 45 | − spork ~ SlideFreq( newFreq, 0.05::second ); | |
| 46 | − spork ~ SlideGain( newGain, 0.05::second ); | |
| 49 | + <<< "unknown midi command:", midiCommand, midiNote, midiVelocity >>>; | |
| 47 | 50 | } |
| 48 | 51 | } |
| 49 | 52 | } |
| 50 | −spork ~ RespondToNoteOn(); | |
| 53 | +spork ~ RespondToMidi(); | |
| 51 | 54 | |
| 52 | −fun void RespondToNoteOff() | |
| 55 | +fun void NoteOn( int note, int velocity ) | |
| 53 | 56 | { |
| 54 | − while( true ) | |
| 57 | + <<< "on:", note, velocity >>>; | |
| 58 | + note => Std.mtof => float newFreq; | |
| 59 | + velocity * 1.0 / 127.0 => float newGain; | |
| 60 | + | |
| 61 | + if( !alreadyOn ) | |
| 55 | 62 | { |
| 56 | − noteOff => now; | |
| 57 | − <<< "off:", whichNote, velocity >>>; | |
| 58 | − | |
| 59 | − if( whichNote == mostRecentOn ) | |
| 60 | − { | |
| 61 | − adsr.keyOff(1); | |
| 62 | − false => alreadyOn; | |
| 63 | − 0 => mostRecentOn; | |
| 64 | − } | |
| 63 | + adsr.keyOn(1); | |
| 64 | + newFreq => t.freq; | |
| 65 | + newGain => adsr.gain; | |
| 66 | + note => mostRecentOn; | |
| 67 | + true => alreadyOn; | |
| 68 | + } | |
| 69 | + else | |
| 70 | + { | |
| 71 | + spork ~ SlideFreq( newFreq, 0.05::second ); | |
| 72 | + spork ~ SlideGain( newGain, 0.05::second ); | |
| 73 | + } | |
| 74 | +} | |
| 75 | + | |
| 76 | +fun void NoteOff( int note ) | |
| 77 | +{ | |
| 78 | + <<< "off:", note >>>; | |
| 79 | + if( note == mostRecentOn ) | |
| 80 | + { | |
| 81 | + adsr.keyOff(1); | |
| 82 | + false => alreadyOn; | |
| 83 | + 0 => mostRecentOn; | |
| 65 | 84 | } |
| 66 | 85 | } |
| 67 | −spork ~ RespondToNoteOff(); | |
| 86 | + | |
| 68 | 87 | |
| 69 | 88 | fun void SlideFreq( float newFreq, dur slideTime ) |
| 70 | 89 | { |
| @@ -154,18 +173,13 @@ while( true ) · 83 unmodified lines | ||
| 154 | 173 | function connectToDevice(device) |
| 155 | 174 | { |
| 156 | 175 | chuckPrint( 'Connecting to device:', device.name, "(" + device.manufacturer + ")" ); |
| 157 | − device.onmidimessage = function(m) { | |
| 176 | + device.onmidimessage = function(m) | |
| 177 | + { | |
| 158 | 178 | const [command, key, velocity] = m.data; |
| 159 | − theChuck.setInt( "whichNote", key ); | |
| 160 | − theChuck.setInt( "velocity", velocity ); | |
| 161 | − if( command === 145 ) | |
| 162 | − { | |
| 163 | − theChuck.broadcastEvent( "noteOn" ); | |
| 164 | − } | |
| 165 | − else if( command === 129 ) | |
| 166 | − { | |
| 167 | − theChuck.broadcastEvent( "noteOff" ); | |
| 168 | − } | |
| 179 | + theChuck.setInt( "midiCommand", command ); | |
| 180 | + theChuck.setInt( "midiNote", key ); | |
| 181 | + theChuck.setInt( "midiVelocity", velocity ); | |
| 182 | + theChuck.broadcastEvent( "midiMessage" ); | |
| 169 | 183 | } |
| 170 | 184 | } |
| 171 | 185 | |