Skip to content

Commit a615f30

Browse files
committed
add some documentation
1 parent 2d179d8 commit a615f30

File tree

1 file changed

+43
-19
lines changed

1 file changed

+43
-19
lines changed

p5.midi.js

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
/**
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
37
*
48
* methods:
59
*
6-
* MIDI.init
10+
* p5.midi.init()
711
*
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"
1116
* }
1217
*
13-
* MIDI.send()
18+
* p5.midi.send(channel, val)
1419
*
1520
*/
1621

@@ -22,7 +27,12 @@
2227
outputArray: [],
2328
inputArray: [],
2429

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+
*/
2636
event: undefined,
2737

2838
/**
@@ -109,14 +119,26 @@
109119
*/
110120
MIDIMessageEventHandler: function(event) {
111121
___MIDI.event = event;
112-
___MIDI.onInput(this.event);
122+
___MIDI.onInput(event);
113123
},
114124

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) {
120142
console.log('input: ', this.event);
121143
},
122144

@@ -127,7 +149,7 @@
127149
* MIDI.send('attack', 60, 127) --> triggers attack on middle C
128150
* MIDI.send('release', 60, 0) --> triggers release of middle C
129151
*
130-
* @param {String} message 'attack' or 'release'
152+
* @param {String} message 'attack', 'release' or # betw 0 to 127
131153
* @param {Number} note 0 to 127
132154
* @param {Number} velocity 0 to 127
133155
*/
@@ -155,17 +177,19 @@
155177

156178
msg[2] = velocity;
157179

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);
161183
});
162184

163185
}
164186
}
165187

188+
// initialize midi and create p5 object if one doesn't exist on the page
166189
if (!window.p5) {
167190
p5 = {};
168191
}
169-
p5.MIDI = ___MIDI;
170-
window.addEventListener('load', p5.MIDI.init() );
192+
p5.midi = ___MIDI;
193+
window.addEventListener('load', p5.midi.init() );
194+
171195
})();

0 commit comments

Comments
 (0)