AlsaMidiDevice constructor

AlsaMidiDevice(
  1. Pointer<snd_ctl_> ctl,
  2. int cardId,
  3. int deviceId,
  4. String name,
  5. String type,
  6. StreamController<MidiMessage> _rxStreamCtrl,
)

Implementation

AlsaMidiDevice(
  this.ctl,
  this.cardId,
  this.deviceId,
  this.name,
  this.type,
  this._rxStreamCtrl,
) {
  // Fetch device info
  var info = calloc<Pointer<a.snd_rawmidi_info_>>();
  alsa.snd_rawmidi_info_malloc(info);
  alsa.snd_rawmidi_info_set_device(info.value, deviceId);

  var status = alsa.snd_ctl_rawmidi_info(ctl, info.value);
  if (status < 0) {
    print('error: cannot get device info.value ${alsa.snd_strerror(status).cast<Utf8>().toDartString()}');
    return;
  }

  // Get input ports
  alsa.snd_rawmidi_info_set_stream(info.value, SND_RAWMIDI_STREAM_INPUT);
  status = alsa.snd_ctl_rawmidi_info(ctl, info.value);
  final inCount = alsa.snd_rawmidi_info_get_subdevices_count(info.value);
  for (var i = 0; i < inCount; i++) {
    if (alsa.snd_rawmidi_info_get_subdevice(info.value) < 0) {
      print('error: snd_rawmidi_info_get_subdevice in [$i] $status ${alsa.snd_rawmidi_info_get_subdevice_name(info.value).cast<Utf8>().toDartString()}');
    } else {
      _inputPorts.add('$i');
    }
  }

  // Get output ports
  alsa.snd_rawmidi_info_set_stream(info.value, SND_RAWMIDI_STREAM_OUTPUT);
  status = alsa.snd_ctl_rawmidi_info(ctl, info.value);
  final outCount = alsa.snd_rawmidi_info_get_subdevices_count(info.value);
  for (var i = 0; i < outCount; i++) {
    if (alsa.snd_rawmidi_info_get_subdevice(info.value) < 0) {
      print('error: snd_rawmidi_info_get_subdevice out [$i] $status ${alsa.snd_rawmidi_info_get_subdevice_name(info.value).cast<Utf8>().toDartString()}');
    } else {
      _outputPorts.add('$i');
    }
  }
  calloc.free(info);
}