Search code examples
javascriptdartdart-htmlweb-midi

type error when calling "send" on MidiOutput proxy


Apparently there's is a problem with the method navigator.requestMIDIAccess() in dart. https://code.google.com/p/dart/issues/detail?id=21805

So I tried the workaround using javascript proxies. Listing the midi ports on the console is no problem. However when I try to send a midi note, an error gets raised: 'Type Error'

I can't find out howto pass the 'note-on' parameter? output.callMethod('send', [[144 , 60, 127]]); outputs[0] gives me "Microsoft GS Wavetable Synth"

import 'dart:html';
import 'dart:js';

void main() {
  final JsObject w = new JsObject.fromBrowserObject(window);
  final JsObject n = w['navigator'];

  if (n.hasProperty('requestMIDIAccess')) {
    n.callMethod('requestMIDIAccess').callMethod('then', [(JsObject midiAccess) {
        JsObject inputs = new JsObject.jsify(midiAccess.callMethod('outputs'));
        for (JsObject input in inputs) {
         print(input['name']);
        }
        ;
        sendMiddleC(midiAccess, 1);
      }]);
  }

}

void sendMiddleC(midiAccess, portID) {
  JsObject outputs = midiAccess.callMethod('outputs');
  JsObject output = outputs[0];
  output.callMethod('send', [[144 , 60, 127]]);
}

Solution

  • The data array passed to the send method needs to be converted from a Dart array to a Javascript array, using dart:js as follows:

    output.callMethod('send', [new JsArray.from([144 , 60, 127])]);