melanchall / drywetmidi

.NET library to read, write, process MIDI files and to work with MIDI devices
https://melanchall.github.io/drywetmidi
MIT License
545 stars 75 forks source link

Multipacket SysEx is not received correctly on macOS #232

Closed James-Morrell closed 1 year ago

James-Morrell commented 1 year ago

I'm trying to use DryWetMidi on MacOS, to receive SysEx messages from a MIDI USB device.

The sysEx size is 251 Bytes. The Windows build works fine, but with the MacOS build only the first two bytes are received in the SysExEvent.

(MacOS Version 12.61 Monterey)

Let me know if you need any more information. Thanks, James

melanchall commented 1 year ago

Hi James,

Yes, I definitely need more information:

  1. Can you please check this sysex message receiving by any other MIDI software? Is it OK?
  2. Does the problem occur only with this specific 251-byte sysex message or with any sysex one?
  3. Can you please provide bytes of the message so I can try to reproduce the error within my unit tests?

Also please be patient, I'm not a macOS user, so you can be asked a lot of questions by me to clarify what's going on :)

Thanks, Max

James-Morrell commented 1 year ago

Hi Max,

Thanks for the quick response, here are my answers:

  1. It works fine in other midi software including SysEx Librarian & MidiView
  2. I'll check this next, will post a response asap.
  3. Here are the bytes in Hex format: 00 21 5F 00 00 01 03 01 01 06 00 00 02 00 00 4C 00 00 41 00 00 52 00 00 49 00 00 20 00 00 42 00 00 20 00 00 53 00 00 4F 00 00 4C 00 00 4F 00 00 20 00 00 20 00 00 20 00 00 20 00 00 20 00 00 02 00 00 3D 04 00 00 00 00 74 03 00 64 00 00 74 03 00 00 00 00 74 03 00 64 00 00 01 00 00 1C 00 00 00 00 00 00 00 00 7F 7F 03 05 00 00 64 00 00 7F 7F 03 05 00 00 64 00 00 7F 7F 03 05 00 00 64 00 00 00 00 00 3C 00 00 00 00 00 00 00 00 64 00 00 64 00 00 64 00 00 64 00 00 00 00 00 00 00 00 2C 02 00 14 00 00 28 00 00 3C 00 00 64 00 00 64 00 00 64 00 00 00 00 00 6C 7F 03 6C 7F 03 04 00 00 0A 00 00 00 00 00 7F 7F 03 01 00 00 48 01 00 00 00 00 6C 7F 03 7F 7F 03 04 00 00 7F 7F 03 00 00 00 7F 7F 03 01 00 00 48 01 00 00 00 00 30 7F 03 14 00 00 48 01 00 00 00 00 F7

Feel free to ask me anymore questions I will try to help get this issue solved. James

melanchall commented 1 year ago

Regarding bytes: are you sure thare is no F0 byte at the start of the message?

James-Morrell commented 1 year ago

Sorry, there is a F0 at the start of the message. Just that SysExEvent.Data's print has truncated it.

I've just tried this with another device and send a variety of SysEx messages, and it seems to do the same. The simplest one which displays the issues is: F0 7F 60 40 F7

Thanks James

James-Morrell commented 1 year ago

Just as an update, the sysEx messages are fine with 2 bytes or less.

I've just tried another API using RtMidi on mac os, and that works: RtMidi.Core

Maybe that can be some help for reference?

melanchall commented 1 year ago

Thanks! I'll look into it tomorrow.

melanchall commented 1 year ago

Well, I've created unit tests with the provided bytes arrays. Running them on virtual machine with Monterey 12.6.1 ends with success, sysex events are received correctly. So we need to do some tests on your machine.

I've prepared the archive: TestKit.zip. Unzip it please somewhere on your computer.


First of all, run the LoopbackDevice program from the folder in a separate Terminal window:

./LoopbackDevice "MIDI A" "MIDI B" "MIDI C"

This commad will create three virtual loopback devices. Devices are present in the system as long as the program running, so please don't close the Terminal.


Now let's open the SendDataViaDwm.sln solution in IDE (VS for Mac or Rider) and run it. We expect to see the message that sysex F0 7F 60 40 F7 is received. Please write me if bytes are correct in the message (F0 will be omitted).


Then let's open the SendDataViaBytes.sln solution in IDE (VS for Mac or Rider) and run it. We expect to see the message that sysex F0 7F 60 40 F7 is received. Please write me if bytes are correct in the message (F0 will be omitted).

James-Morrell commented 1 year ago

Hi Max,

Just ran the test's on the Mac device I have, The "SendDataViaDwm.sln" worked correctly. But the "SendDataViaBytes.sln" did not. I received no message with the "SendDataViaBytes.sln" and only the Press any key to exit... displayed.

melanchall commented 1 year ago

Thanks James!

Well, I'll try to provide new version of the SendDataViaBytes to try to understand why it's not working. For now, can you please confirm that you see wrong data with SendDataViaDwm if you send sysex via your device the problem occurred with?

James-Morrell commented 1 year ago

Just checked SendDataViaDwm with two different MIDI USB devices, I sent the SysEx from my windows machine. With both devices it only shows the first two bytes 7F 60. On the loopback it is fine.

melanchall commented 1 year ago

Pretty strange... Hm, it looks like we need to write some code in C in XCode. I'll try to provide you the code tomorrow.

melanchall commented 1 year ago

Hi James,

I've prepared a new solution: ViewPacketsData.zip. Replace name of the device with the desired one from where you receive broken data:

InputDevice.GetByName("MIDI A");

Just follow the instructions from the utility and provide me an output text please. I made the solution without any tests so please write me about any errors if any.

James-Morrell commented 1 year ago

Hi Max,

I've just run the ViewPacketsData, here's the output from the terminal:

Packet 0 / 1 data:
    F0 0 21
Packet 0 / 1 data:
    5F 0 0
Packet 0 / 1 data:
    1 3 1 1 6 0
Packet 0 / 1 data:
    0 0 0
Packet 0 / 1 data:
    0 4C 0 0 41 0 0 52 0 0 49 0 0 20 0 0 42 0 0 20 0 0 38 0 0 54 0 0 48 0 0 20 0 0 4E 0 0 4F 0 0 54 0 0 45 0 0 20 0
Packet 0 / 1 data:
    0 1 0 0 F 2 0 0 0 0 74 3 0 64 0 0 74 3 0 0 0 0 74 3 0 64 0 0 1 0
Packet 0 / 1 data:
    0 18 0 0 1E 0 0 1C 0 0 7F 7F 3 28 0
Packet 0 / 1 data:
    0 64 0 0 7F 7F
Packet 0 / 1 data:
    3 5 0
Packet 0 / 1 data:
    0 64 0
Packet 0 / 1 data:
    0 7F 7F
Packet 0 / 1 data:
    3 5 0
Packet 0 / 1 data:
    0 64 0
Packet 0 / 1 data:
    0 0 0
Packet 0 / 1 data:
    0 2C 0 0 1 0 0 0 0 0 64 0 0 64 0 0 64 0 0 64 0 0 0 0 0 0 0 0 2C 2 0 14 0 0 28 0 0 3C 0 0 64 0 0 64 0 0 64 0
Packet 0 / 1 data:
    0 0 0 0 6C 7F 3 6C 7F 3 4 0 0 A 0 0 0 0 0 7F 7F 3 1 0 0 48 1 0 0 0 0 6C 7F 3 7F 7F 3 4 0 0 7F 7F 3 0 0 0 7F 7F
Packet 0 / 1 data:
    3 1 0 0 48 1 0 0 0 0 30 7F 3 14 0
Packet 0 / 1 data:
    0 48 1
Packet 0 / 1 data:
    0 0 0
Packet 0 / 1 data:
    0 F7
melanchall commented 1 year ago

Hi James,

Is that output on sending one single event or multiple ones? If multiple, can you please just send a single one (for example, F0 7F 60 40 F7)?

melanchall commented 1 year ago

Can you please run a new version of the solution: ViewPacketsData.zip? I have an assumption what's going on.

melanchall commented 1 year ago

@James-Morrell Any news?

James-Morrell commented 1 year ago

Hi Max,

Sorry I've been away for a couple of days, will look at it some point today :)

James

melanchall commented 1 year ago

Hi James,

Here another one new version: ViewPacketsData.zip :)

James-Morrell commented 1 year ago

Hi @melanchall

Here's the output for the latest ViewPacketsData.zip

Waiting for packets information...
Just send problem sysex and wait some time, then just press any key to see the log
 Message received at [2022-12-16T14:47:01.2838960Z]!
Packet 0 / 1 data:
    F0 00 21
Message received at [2022-12-16T14:47:01.3909050Z]!
Packet 0 / 1 data:
    5F 00 00
Message received at [2022-12-16T14:47:01.3910180Z]!
Packet 0 / 1 data:
    01 03 01 01 06 00 00 00 00 00 4C 00 00 41 00 00 52 00
Message received at [2022-12-16T14:47:01.3911710Z]!
Packet 0 / 1 data:
    00 49 00 00 20 00
Message received at [2022-12-16T14:47:01.3912260Z]!
Packet 0 / 1 data:
    00 42 00 00 20 00 00 38 00 00 54 00 00 48 00 00 20 00 00 4E 00 00 4F 00 00 54 00 00 45 00 00 20 00 00 01 00 00 0F 02 00 00 00 00 74 03 00 64 00
Message received at [2022-12-16T14:47:01.3912940Z]!
Packet 0 / 1 data:
    00 74 03 00 00 00 00 74 03 00 64 00 00 01 00 00 18 00 00 1E 00 00 1C 00 00 7F 7F 03 28 00 00 64 00 00 7F 7F 03 05 00 00 64 00 00 7F 7F
Message received at [2022-12-16T14:47:01.4502970Z]!
Packet 0 / 1 data:
    03 05 00
Message received at [2022-12-16T14:47:01.4503970Z]!
Packet 0 / 1 data:
    00 64 00
Message received at [2022-12-16T14:47:01.4505440Z]!
Packet 0 / 1 data:
    00 00 00
Message received at [2022-12-16T14:47:01.4506370Z]!
Packet 0 / 1 data:
    00 2C 00 00 01 00 00 00 00 00 64 00 00 64 00 00 64 00 00 64 00 00 00 00 00 00 00 00 2C 02 00 14 00 00 28 00 00 3C 00 00 64 00 00 64 00 00 64 00
Message received at [2022-12-16T14:47:01.4507270Z]!
Packet 0 / 1 data:
    00 00 00 00 6C 7F 03 6C 7F 03 04 00 00 0A 00 00 00 00 00 7F 7F 03 01 00 00 48 01 00 00 00 00 6C 7F 03 7F 7F 03 04 00 00 7F 7F 03 00 00 00 7F 7F
Message received at [2022-12-16T14:47:01.4507960Z]!
Packet 0 / 1 data:
    03 01 00 00 48 01 00 00 00 00 30 7F 03 14 00
Message received at [2022-12-16T14:47:01.4558240Z]!
Packet 0 / 1 data:
    00 48 01
Message received at [2022-12-16T14:47:01.4559230Z]!
Packet 0 / 1 data:
    00 00 00
Message received at [2022-12-16T14:47:01.4560460Z]!
Packet 0 / 1 data:
    00 F7

Press any key to exit...

Do you want the other output from the previous version, it is the same but without the time stamps.

James

melanchall commented 1 year ago

Hi James,

Thank you a lot! Now I know for sure what's going on. Your message was split into multiple separate messages. DryWetMIDI right now assumes that single packet == single complete message. Which is obviously wrong according to your tests.

It's definitely the bug in my library. It should be pretty easy to fix, but I think I can handle it in next year only (I'm going to vacation right now :)).

I appreciate your assistance with diagnosing the problem. Thanks!

James-Morrell commented 1 year ago

Hi Max, Glad I could help, let me know if you need anymore tests this end!

Have a good holiday! James

melanchall commented 1 year ago

Thanks James! If I'll be able to fix the bug before next year, I definitely let you know.

melanchall commented 1 year ago

Hi James,

Looks like the bug has been fixed now according to my unit tests. I've published new prerelease version of the library - 6.1.4-prerelease2. @James-Morrell Can you please install it into your project and let me know whether the problem is gone or not?

Thanks, Max

James-Morrell commented 1 year ago

Hi Max,

Sorry for the late response, been away for the holidays!

Just tested 6.1.4-prerelease2 and it's all working!

Kind regards, James

melanchall commented 1 year ago

Hi James,

Great! Thanks for the issue and your assistance! You've made the library better.

Thanks, Max

melanchall commented 1 year ago

:rocket: 6.1.4 version is released now!

Prerelease NuGet packages will be unlisted soon, so please update the package references to the new version.

Thanks for using the library!