Debian Codegraph @

src/host-web/webchuck/examples/midi1/index.html

raw symbols SCIP
1 <!doctype html>
2 <html lang="en-us">
3 <head>
4 <meta charset="utf-8">
5 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
6 <title>ChucK + JS => ChucKScript</title>
7 <link rel="stylesheet" href="../../css/editor.css">
8 <style>
9 #midi {
10 margin: 0px 15px;
11 }
12 </style>
13 </head>
14 <body onresize="chuckEditor.resize();">
15 <div>
16 <div id="program">// MIDI 1: Note on / off events
17 global Event midiMessage;
18 global int midiCommand;
19 global int midiNote;
20 global int midiVelocity;
21
22 TriOsc t => ADSR adsr => dac;
23 adsr.set( 0.1::second, 0.3::second, 0.5, 0.7::second );
24 false => int alreadyOn;
25 0 => int mostRecentOn;
26
27 fun void RespondToMidi()
28 {
29 while( true )
30 {
31 midiMessage => now;
32 if( midiCommand >= 144 && midiCommand < 160 )
33 {
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 );
46 }
47 else
48 {
49 <<< "unknown midi command:", midiCommand, midiNote, midiVelocity >>>;
50 }
51 }
52 }
53 spork ~ RespondToMidi();
54
55 fun void NoteOn( int note, int velocity )
56 {
57 <<< "on:", note, velocity >>>;
58 note => Std.mtof => float newFreq;
59 velocity * 1.0 / 127.0 => float newGain;
60
61 if( !alreadyOn )
62 {
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;
84 }
85 }
86
87
88 fun void SlideFreq( float newFreq, dur slideTime )
89 {
90 t.freq() => float oldFreq;
91 1000 => int numSteps;
92 slideTime / numSteps => dur delta;
93 ( newFreq - oldFreq ) / numSteps => float freqDelta;
94 for( int i; i < numSteps; i++ )
95 {
96 oldFreq + freqDelta * i => t.freq;
97 delta => now;
98 }
99 newFreq => t.freq;
100 }
101
102 fun void SlideGain( float newGain, dur slideTime )
103 {
104 t.gain() => float oldGain;
105 1000 => int numSteps;
106 slideTime / numSteps => dur delta;
107 ( newGain - oldGain ) / numSteps => float gainDelta;
108 for( int i; i < numSteps; i++ )
109 {
110 oldGain + gainDelta * i => t.gain;
111 delta => now;
112 }
113 newGain => t.gain;
114 }
115
116 while( true )
117 {
118 1::second => now;
119 }
120 </div>
121 </div>
122 <div class="buttons">
123 <button class="chuckButton" id="startChuck" disabled="disabled">Start ChucK</button>
124 <button class="chuckButton" id="compileButton" disabled="disabled">Compile and Run</button>
125 <button class="chuckButton" id="replaceButton" disabled="disabled">Replace</button>
126 <button class="chuckButton" id="removeButton" disabled="disabled">Remove</button>
127 <button class="chuckButton" id="clearButton" disabled="disabled">Clear VM and Globals</button>
128 <button class="chuckButton" id="micButton" disabled="disabled">Enable Microphone</button>
129 <button class="chuckButton" id="fileFormButton">Show File Uploader</button>
130 </div>
131 <div id="fileForm" class="hidden">
132 <p>File name to use in ChucK: <input type="text" id="uploadFilename"></p>
133 <p>Local file to upload: <input type="file" id="uploadFile" name="uploadFile"></p>
134 <p><button class="chuckButton" id="fileButton" disabled="disabled">Upload File to Virtual File System</button></p>
135 </div>
136 <div id="midi">
137 <p>Click the desired MIDI device:</p>
138 <p>(Please note, the Web MIDI API is not implemented yet in Firefox or Safari.)</p>
139 <ul id="midi-list">
140
141 </ul>
142 </div>
143 <div>
144 <textarea id="output"></textarea>
145 <div id="shreds">
146 <table id="shredstable">
147 <col width="60px">
148 <col width="1*">
149 <col width="50px">
150 <col width="60px">
151 <tr><th>shred</th><th>code</th><th>time</th><th>remove</th>
152 </table>
153 </div>
154 <div class="clearfix"></div>
155 </div>
156 <script type="text/javascript" src="../../js/ace-min-noconflict/ace.js" charset="utf-8"></script>
157 <script type="text/javascript" src="../../js/editor.js"></script>
158 <script type="text/javascript" src="../../js/defer.js"></script>
159 <script>
160 // override
161 var whereIsChuck = "../../js";
162 </script>
163 <script type="text/javascript" src="../../js/webchuck_host.js"></script>
164 <script type="text/javascript" src="../../js/host.js"></script>
165 <script>
166 // make editor a bit smaller
167 chuckEditor.setOptions({
168 maxLines: 20,
169 minLines: 20
170 });
171
172 // modified from: https://codepen.io/kulak-at/pen/mxVNbx
173 const list = document.getElementById('midi-list');
174
175 function connectToDevice(device)
176 {
177 chuckPrint( 'Connecting to device:', device.name, "(" + device.manufacturer + ")" );
178 device.onmidimessage = function(m)
179 {
180 const [command, key, velocity] = m.data;
181 theChuck.setInt( "midiCommand", command );
182 theChuck.setInt( "midiNote", key );
183 theChuck.setInt( "midiVelocity", velocity );
184 theChuck.broadcastEvent( "midiMessage" );
185 }
186 }
187
188 function replaceElements(inputs) {
189 while(list.firstChild) {
190 list.removeChild(list.firstChild)
191 }
192 const elements = inputs.map(e => {
193 console.log(e);
194 const el = document.createElement('li')
195 el.innerHTML = `<a href="javascript:;">${e.name} (${e.manufacturer})</a>`;
196 el.addEventListener('click', connectToDevice.bind(null, e));
197 return el;
198 });
199
200 elements.forEach(e => list.appendChild(e));
201 }
202
203 theChuckReady.then( function() {
204 navigator.requestMIDIAccess().then(function(access)
205 {
206 console.log('access', access);
207 replaceElements(Array.from(access.inputs.values()));
208 access.onstatechange = function(e) {
209 replaceElements(Array.from(this.inputs.values()));
210 }
211
212 });
213 });
214
215 </script>
216 </body>
217 </html>
218
219