|
1 | 1 | /**
|
2 |
| - * NOTE: you may need to restart browser for midi to work |
| 2 | + * NOTE: you may need to restart your browser |
| 3 | + * if midi input/output devices were not connected |
| 4 | + * when you opened the browser session. |
| 5 | + * |
| 6 | + * p5.midi.event |
3 | 7 | *
|
4 | 8 | * methods:
|
5 | 9 | *
|
6 |
| - * MIDI.init |
| 10 | + * p5.midi.init() |
7 | 11 | *
|
8 |
| - * // callback |
9 |
| - * MIDI.onInput() { |
10 |
| - * MIDI.event |
| 12 | + * p5.midi.onInput(e) { |
| 13 | + * // do something with |
| 14 | + * // p5.midi.event |
| 15 | + * // which is the same thing as "e" |
11 | 16 | * }
|
12 | 17 | *
|
13 |
| - * MIDI.send() |
| 18 | + * p5.midi.send(channel, val) |
14 | 19 | *
|
15 | 20 | */
|
16 | 21 |
|
|
22 | 27 | outputArray: [],
|
23 | 28 | inputArray: [],
|
24 | 29 |
|
25 |
| - // the current midi event |
| 30 | + /** |
| 31 | + * the p5.midi.event holds the most recent midi input event. |
| 32 | + * |
| 33 | + * |
| 34 | + * @property A Uint8Array containing the MIDI data bytes of a single MIDI message |
| 35 | + */ |
26 | 36 | event: undefined,
|
27 | 37 |
|
28 | 38 | /**
|
|
109 | 119 | */
|
110 | 120 | MIDIMessageEventHandler: function(event) {
|
111 | 121 | ___MIDI.event = event;
|
112 |
| - ___MIDI.onInput(this.event); |
| 122 | + ___MIDI.onInput(event); |
113 | 123 | },
|
114 | 124 |
|
115 |
| - // to be overwritten by the user |
116 |
| - // [0] - channel |
117 |
| - // [1] - note |
118 |
| - // [2] - velocity |
119 |
| - onInput: function() { |
| 125 | + |
| 126 | + /** |
| 127 | + * Callback from midi input event. |
| 128 | + * |
| 129 | + * Should be overwritten by the user, similar to mouseClicked(), like this: |
| 130 | + * p5.midi.onInput(){ |
| 131 | + * var note = p5.midi.event[1]; |
| 132 | + * var velocity = p5.midi.event[2]; |
| 133 | + * |
| 134 | + * // do something with note and velocity |
| 135 | + * } |
| 136 | + * |
| 137 | + * @method onInput |
| 138 | + * @param {Array} event Event will be passed in automatically |
| 139 | + * or can be referenced as p5.midi.event |
| 140 | + */ |
| 141 | + onInput: function(event) { |
120 | 142 | console.log('input: ', this.event);
|
121 | 143 | },
|
122 | 144 |
|
|
127 | 149 | * MIDI.send('attack', 60, 127) --> triggers attack on middle C
|
128 | 150 | * MIDI.send('release', 60, 0) --> triggers release of middle C
|
129 | 151 | *
|
130 |
| - * @param {String} message 'attack' or 'release' |
| 152 | + * @param {String} message 'attack', 'release' or # betw 0 to 127 |
131 | 153 | * @param {Number} note 0 to 127
|
132 | 154 | * @param {Number} velocity 0 to 127
|
133 | 155 | */
|
|
155 | 177 |
|
156 | 178 | msg[2] = velocity;
|
157 | 179 |
|
158 |
| - this.outputArray.forEach(function(o) { |
159 |
| - console.log(o.name); |
160 |
| - o.send(msg); |
| 180 | + // send the message to all connected outputs |
| 181 | + this.outputArray.forEach(function(output) { |
| 182 | + output.send(msg); |
161 | 183 | });
|
162 | 184 |
|
163 | 185 | }
|
164 | 186 | }
|
165 | 187 |
|
| 188 | + // initialize midi and create p5 object if one doesn't exist on the page |
166 | 189 | if (!window.p5) {
|
167 | 190 | p5 = {};
|
168 | 191 | }
|
169 |
| - p5.MIDI = ___MIDI; |
170 |
| - window.addEventListener('load', p5.MIDI.init() ); |
| 192 | + p5.midi = ___MIDI; |
| 193 | + window.addEventListener('load', p5.midi.init() ); |
| 194 | + |
171 | 195 | })();
|
0 commit comments