@@ -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
6690This example prints incoming MIDI messages on a single arbitrary input port to
6791the console log.
6892
6993``` js
7094function 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 }
0 commit comments