0

I'm trying to connect to my MIDI device to a Flutter app running on Windows. I'm using win32 and dart ffi. I have the following:

final Pointer<HMIDIIN> hMidiDevice = malloc();

Pointer<NativeFunction<MidiInProc>> callbackPointer =
    Pointer.fromFunction(midiInCallback);

final result = midiInOpen(
  hMidiDevice,
  0,
  callbackPointer.address,
  0,
  CALLBACK_FUNCTION,
);
midiInStart(hMidiDevice.value);

midiInOpen takes a pointer to a function as 3rd argument. Here is my callback method:

static void midiInCallback(
    int hMidiIn,
    int wMsg,
    int dwInstance,
    int dwParam1,
    int dwParam2,
) {
    print('Message: $wMsg dwParam1: $dwParam1');
}

This compiles and works with a connected USB MIDI device. However, when I press a key on my MIDI device, then I get the following error:

../../third_party/dart/runtime/vm/runtime_entry.cc: 3657: error: Cannot invoke native callback outside an isolate.
pid=11004, thread=21860, isolate_group=(nil)(0000000000000000), isolate=(nil)(0000000000000000)
isolate_instructions=0, vm_instructions=7ffef50837c0
  pc 0x00007ffef51a3732 fp 0x00000057468ff990 angle::PlatformMethods::operator=+0x322d8a
-- End of DumpStackTrace

What does it mean and what can I do so that my callback is called with MIDI data?

user4401
  • 394
  • 1
  • 4
  • 15
  • You can only call from C to Dart if that piece of C code is itself invoked by a Dart to C call. Your midi callback is on some other random thread which cannot call to Dart. – Richard Heap Aug 16 '22 at 09:11
  • Is that then somehow an issue with https://pub.dev/documentation/win32/latest/win32/midiInOpen.html that the callback is called from another thread or is there a way to force the Dart thread to be used? – user4401 Aug 16 '22 at 13:32
  • 1
    No, you can't access the Dart thread. However, you can signal it using something called ports. Search "dart ffi nativeport". I did that and found this example that you might be able to adapt. https://gist.github.com/espresso3389/be5674ab4e3154f0b7c43715dcef3d8d – Richard Heap Aug 16 '22 at 21:10

0 Answers0