connect method

bool connect()

Connect to the device, ie. open input and output ports NOTE: Currently only the first input/output port is considered

Implementation

bool connect() {
  // Open input

  var mIn = _ins.entries.firstOrNull;
  if (mIn != null) {
    var id = mIn.key;
    int result = midiInOpen(
        hMidiInDevicePtr, id, callbackAddress, 0, MIDI_WAVE_OPEN_TYPE.CALLBACK_FUNCTION);
    if (result != 0) {
      print("OPEN ERROR($result): ${midiErrorMessage(result)}");
      return false;
    } else {
      // Setup buffer
      _midiInBuffer = malloc<BYTE>(_bufferSize);
      _midiInHeader = malloc<MIDIHDR>();
      _midiInHeader.ref.lpData = _midiInBuffer as LPSTR;
      _midiInHeader.ref.dwBufferLength = _bufferSize;
      _midiInHeader.ref.dwFlags = 0;

      result = midiInPrepareHeader(
          hMidiInDevicePtr.value, _midiInHeader, sizeOf<MIDIHDR>());
      if (result != 0) {
        print("HDR PREP ERROR: ${midiErrorMessage(result)}");
        return false;
      }

      result = midiInAddBuffer(
          hMidiInDevicePtr.value, _midiInHeader, sizeOf<MIDIHDR>());
      if (result != 0) {
        print("HDR ADD ERROR: ${midiErrorMessage(result)}");
        return false;
      }

      result = midiInStart(hMidiInDevicePtr.value);
      if (result != 0) {
        print("START ERROR: ${midiErrorMessage(result)}");
        return false;
      }
    }
  }

  // Open output
  var mOut = _outs.entries.firstOrNull;
  if (mOut != null) {
    var id = mOut.key;

    int result = midiOutOpen(hMidiOutDevicePtr, id, 0, 0, MIDI_WAVE_OPEN_TYPE.CALLBACK_NULL);
    if (result != 0) {
      print("OUT OPEN ERROR: result");
      return false;
    }

    _midiOutBuffer = malloc<BYTE>(_bufferSize);
    _midiOutHeader = malloc<MIDIHDR>();
  }
  connected = true;
  _setupStreamController.add("deviceConnected");
  return true;
}