Skip to content

Commit c07b3a1

Browse files
Merge pull request #272 from mjwilson-google/examples
Move remaining examples to explainer, modernize / fix errors in examples
2 parents 400deb4 + 887aaa3 commit c07b3a1

File tree

2 files changed

+34
-39
lines changed

2 files changed

+34
-39
lines changed

explainer.md

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,21 +62,48 @@ function listInputsAndOutputs( midiAccess ) {
6262
}
6363
```
6464

65+
### Adding inputs and outputs to select boxes
66+
67+
```js
68+
// to tell how many entries there are:
69+
let numberOfMIDIInputs = inputs.size;
70+
71+
// add each of the ports to a <select> box
72+
for (let input of inputs.values()) {
73+
let opt = document.createElement("option");
74+
opt.text = input.name;
75+
document.getElementById("inputportselector").add(opt);
76+
}
77+
78+
// to tell how many entries there are:
79+
let numberOfMIDIOutputs = outputs.size;
80+
81+
// add each of the ports to a <select> box
82+
for (let output of outputs.values()) {
83+
let opt = document.createElement("option");
84+
opt.text = output.name;
85+
document.getElementById("outputportselector").add(opt);
86+
}
87+
```
88+
6589
### Handling MIDI Input
6690
This example prints incoming MIDI messages on a single arbitrary input port to
6791
the console log.
6892

6993
```js
7094
function onMIDIMessage( event ) {
7195
let str = "MIDI message received at timestamp " + event.timeStamp + "[" + event.data.length + " bytes]: ";
72-
for (let i=0; i&lt;event.data.length; i++) {
96+
for (let i=0; i<event.data.length; i++) {
7397
str += "0x" + event.data[i].toString(16) + " ";
7498
}
7599
console.log( str );
76100
}
77101

78-
function startLoggingMIDIInput( midiAccess, indexOfPort ) {
79-
midiAccess.inputs.forEach( function(entry) {entry.onmidimessage = onMIDIMessage;});
102+
function startLoggingMIDIInput( midiAccess ) {
103+
for (let entry of midiAccess.inputs) {
104+
let input = entry[1];
105+
input.onmidimessage = onMIDIMessage;
106+
}
80107
}
81108
```
82109

@@ -169,7 +196,7 @@ function onMIDIInit(midi) {
169196

170197
let haveAtLeastOneDevice=false;
171198
let inputs=midiAccess.inputs.values();
172-
for ( let input = inputs.next(); input &amp;& !input.done; input = inputs.next()) {
199+
for ( let input = inputs.next(); input && !input.done; input = inputs.next()) {
173200
input.value.onmidimessage = MIDIMessageEventHandler;
174201
haveAtLeastOneDevice = true;
175202
}

index.html

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -451,25 +451,9 @@ <h3 id="MIDIInputMap">
451451
{{MIDIInput}} instance and key is its ID.
452452
</p>
453453
<p>
454-
This type is used to represent all the currently available MIDI input
455-
ports. This enables:
454+
This type is used to represent all the currently available [=MIDI input
455+
ports=].
456456
</p>
457-
<pre class="example"> // to tell how many entries there are:
458-
let numberOfMIDIInputs = inputs.size;
459-
460-
// add each of the ports to a &lt;select&gt; box
461-
inputs.forEach( function( port, key ) {
462-
let opt = document.createElement("option");
463-
opt.text = port.name;
464-
document.getElementById("inputportselector").add(opt);
465-
});
466-
467-
// or you could express in ECMAScript 6 as:
468-
for (let input of inputs.values()) {
469-
let opt = document.createElement("option");
470-
opt.text = input.name;
471-
document.getElementById("inputportselector").add(opt);
472-
}</pre>
473457
</section>
474458
<section data-dfn-for="MIDIOutputMap">
475459
<h3 id="MIDIOutputMap">
@@ -486,24 +470,8 @@ <h3 id="MIDIOutputMap">
486470
</p>
487471
<p>
488472
This type is used to represent all the currently available [=MIDI
489-
output ports=]. This enables:
473+
output ports=].
490474
</p>
491-
<pre class="example"> // to tell how many entries there are:
492-
let numberOfMIDIOutputs = outputs.size;
493-
494-
// add each of the ports to a &lt;select&gt; box
495-
outputs.forEach( function( port, key ) {
496-
let opt = document.createElement("option");
497-
opt.text = port.name;
498-
document.getElementById("outputportselector").add(opt);
499-
});
500-
501-
// or you could express in ECMAScript 6 as:
502-
for (let output of outputs.values()) {
503-
let opt = document.createElement("option");
504-
opt.text = output.name;
505-
document.getElementById("outputportselector").add(opt);
506-
}</pre>
507475
</section>
508476
<section data-dfn-for="MIDIAccess" data-link-for="MIDIAccess">
509477
<h2 id="MIDIAccess">

0 commit comments

Comments
 (0)