History

make midi example more correct and also more reusable

Jack Atherton committed 7 years ago

Commit fc2a1b61 parent d92511b1 file · +55 −41Browse files

Unified · Split

src/host_web/chuckscript/examples/midi1/index.html+55−41
@@ -14,57 +14,76 @@ · 13 unmodified lines
1414 <body onresize="chuckEditor.resize();">
1515 <div>
1616 <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;
2021
2122 TriOsc t => ADSR adsr => dac;
2223 adsr.set( 0.1::second, 0.3::second, 0.5, 0.7::second );
2324 false => int alreadyOn;
2425 0 => int mostRecentOn;
2526
26−fun void RespondToNoteOn()
27+fun void RespondToMidi()
2728 {
2829 while( true )
2930 {
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 )
3633 {
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 );
4246 }
4347 else
4448 {
45− spork ~ SlideFreq( newFreq, 0.05::second );
46− spork ~ SlideGain( newGain, 0.05::second );
49+ <<< "unknown midi command:", midiCommand, midiNote, midiVelocity >>>;
4750 }
4851 }
4952 }
50−spork ~ RespondToNoteOn();
53+spork ~ RespondToMidi();
5154
52−fun void RespondToNoteOff()
55+fun void NoteOn( int note, int velocity )
5356 {
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 )
5562 {
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;
6584 }
6685 }
67−spork ~ RespondToNoteOff();
86+
6887
6988 fun void SlideFreq( float newFreq, dur slideTime )
7089 {
@@ -154,18 +173,13 @@ while( true ) · 83 unmodified lines
154173 function connectToDevice(device)
155174 {
156175 chuckPrint( 'Connecting to device:', device.name, "(" + device.manufacturer + ")" );
157− device.onmidimessage = function(m) {
176+ device.onmidimessage = function(m)
177+ {
158178 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" );
169183 }
170184 }
171185

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t