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