connect method

Future<bool> connect()

Implementation

Future<bool> connect() async {
  outPort = calloc<Pointer<a.snd_rawmidi_>>();
  inPort = calloc<Pointer<a.snd_rawmidi_>>();

  Pointer<Char> name = '${hardwareId(cardId, deviceId)},0'.toNativeUtf8().cast<Char>();
  //print('open out port ${stringFromNative(name)}');
  var status = 0;
  if ((status = alsa.snd_rawmidi_open(inPort!, outPort!, name, a.SND_RAWMIDI_SYNC)) < 0) {
    print('error: cannot open card number $cardId ${stringFromNative(alsa.snd_strerror(status))}');
    return false;
  }

  connected = true;

  errorPort = ReceivePort();
  receivePort = ReceivePort();
  _isolate = await Isolate.spawn(
    _rxIsolate,
    Tuple2(receivePort!.sendPort, inPort!.value.address),
    onError: errorPort!.sendPort,
  );

  errorPort?.listen((message) {
    print('isolate error message $message');
    disconnect();
  });

  receivePort?.listen((data) {
    // print("rx data $data $_rxStreamCtrl ${_rxStreamCtrl.sink}");
    var packet = MidiMessage(data, DateTime.now().millisecondsSinceEpoch, this);
    _rxStreamCtrl.add(packet);
  });

  return true;
}