MIDI for Hatari

I have recently struggled with efficient debugging of the MIDI routines in EPSS when using virtual Atari development (Hatari).

MIDI works in Hatari running on Linux (via ELSA) and Mac (via PortMIDI, dont know how good though) but it has never worked for Windows, which is the platform I use.

I decided to do something about that and after some tests I decide the quickest way would be to develop a "mapper" that could send and receive MIDI data from Hatari and pass it on to Windows MIDI.

I made the mapper in C# under Visual Studio Community and found a great MIDI library to work with: A MIDI File Slicer and MIDI Library in C# - CodeProject. Super credits for this goes to "honey the codewitch" for her excellent work with this!

I manged quite easily to get a file based communication for sending in MIDI to Hatari. Instead of the normal behaviour in Hatari, keeping the file set as MIDI in file open all the time, I instead change this to

  1. Hatari checks if the file exists
  2. If it exists, open it and read in the contents.
  3. After all is read, remove the file.

In the MIDI mapper I can then check if the file exist, in that case wait (as Atari then is reading the file) or else, create the file and fill it with the MIDI message received from the device set as MIDI in device.

This concept seems to work fine so that really solved my problem with EPSS debugging, as I then can generate true MIDI from my MIDI controller and see how EPSS reacts in real time in Hatari.

I wanted of course also solve MIDI out from Hatari in a similar way. This was a bit trickier though as I did not want to keep polling a file in Windows. I tried to use the FileWatcher but this never was accurate enough.

So I started to look at Named Pipes which seemed to be a much better concept. As this is pure Windows behaviour I was unsure how good this would be supported in Hatari when compiling it, but it showed that it was no problem including windows.h and then access the Windows specific functionality, like CreateFile with pipe flags, file handles and so on.

I thought the best would be to act as a pipe server on the C# side and let Hatari act as the client connecting to the server when it needs to pipe data. It was quite a big mess on the c# side before I managed to get a stable implementation of the Named Pipes and lots of trial and errors and googling different approaches.

After managing to get the MIDI bytes from Atari was the next issue, MIDI on PC always works with complete MIDI messages and I have not found any way of just sending the MIDI bytes byte by byte RAW to the MIDI interface. So I now collect the bytes more intelligent in the MIDI Mapper and when I see that I have a valid MIDI message I can forward it to the MIDI device and that works fine.

I recorded a clip on how it currently works and uploaded it to YouTube with some explanatory text here: (501) Hatari for Windows and MIDI - YouTube

If anybody are interested in testing this out, I can make the repos available and do a fork of the Hatari sources and put my changes there.