History

make midi example more correct and also more reusable

Jack Atherton committed 7 years ago

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

Showing the changes to src/host_web (1 of 1 file). Show all

Unified · Split

src/host_web/chuckscript/examples/midi1/index.html+55−41
@@ -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 events16 <div id="program">// MIDI 1: Note on / off events
17global Event noteOn, noteOff;17global Event midiMessage;
18global int whichNote;18global int midiCommand;
19global int velocity;19global int midiNote;
20global int midiVelocity;
20 21
21TriOsc t => ADSR adsr => dac;22TriOsc t => ADSR adsr => dac;
22adsr.set( 0.1::second, 0.3::second, 0.5, 0.7::second );23adsr.set( 0.1::second, 0.3::second, 0.5, 0.7::second );
23false => int alreadyOn;24false => int alreadyOn;
240 => int mostRecentOn;250 => int mostRecentOn;
25 26
26fun void RespondToNoteOn()27fun 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 else47 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}
50spork ~ RespondToNoteOn();53spork ~ RespondToMidi();
51 54
52fun void RespondToNoteOff()55fun 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
76fun 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}
67spork ~ RespondToNoteOff();86
68 87
69fun void SlideFreq( float newFreq, dur slideTime )88fun 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

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