hatari and midi

A forum about the Hatari ST/STE/Falcon emulator - the current version is v2.3.0

Moderators: simonsunnyboy, thothy, Moderator Team

midimark
Atariator
Atariator
Posts: 24
Joined: Sat Jan 09, 2021 7:48 pm

hatari and midi

Post by midimark »

Is there a way to get Hatari and midi to work. I am using Linux Ubuntu studio.
Any help is appreciated!
Mark
User avatar
robv
Atari maniac
Atari maniac
Posts: 93
Joined: Mon Nov 09, 2020 7:39 pm
Location: Netherlands

Re: hatari and midi

Post by robv »

My Ubuntu box passed away recently, but the idea would be:
- create a pair of virtual serial ports (tty2tty or similar Linux tool)
- connect Hatari midi to one end of the virtual pair (let's say ttyS1) in Hatari settings
- run Hairless serial-midi bridge https://projectgus.github.io/hairless-midiserial/
- connect the serial end of Hairless to the other end of the virtual serial pair (let's say ttyS0)
- the midi end of Hairless should go to a Linux software synth
I have a similar setup with MagicPC under Windows and it works fine. Mind you, I'm not a Hatari user, but just suggesting a general idea.
Perhaps you can direct Hatari midi straight to a Linux software synth, not sure about that.
Rob
engaged with Atari ST since 1987
midimark
Atariator
Atariator
Posts: 24
Joined: Sat Jan 09, 2021 7:48 pm

Re: hatari and midi

Post by midimark »

thanks! I will keep working on it
User avatar
Eero Tamminen
Fuji Shaped Bastard
Fuji Shaped Bastard
Posts: 2384
Joined: Sun Jul 31, 2011 1:11 pm

Re: hatari and midi

Post by Eero Tamminen »

midimark wrote: Sun Feb 21, 2021 12:04 am Is there a way to get Hatari and midi to work. I am using Linux Ubuntu studio.
See Hatari documentation: https://hatari.tuxfamily.org/doc/midi-linux.txt

(Note that PortMidi support is buggy and limits MIDI port usage strictly to MIDI, so raw MIDI access is preferred.)
midimark
Atariator
Atariator
Posts: 24
Joined: Sat Jan 09, 2021 7:48 pm

Re: hatari and midi

Post by midimark »

He Eero thanks for your reply.
When I am in the "devices" menu for Hatari, I only see "input to a file /dev/snd/midic0 etc same for output.
So, I don't see the output to a synth input, even though I started Qsynth before Hatari.
So, I am still a bit confused.
Thanks,
Mark
midimark
Atariator
Atariator
Posts: 24
Joined: Sat Jan 09, 2021 7:48 pm

Re: hatari and midi

Post by midimark »

well, i bungled my way through the instructions, and I got Hatari to work with qsynth in Linux. Not totally sure how, but thanks to those who replied!
User avatar
Eero Tamminen
Fuji Shaped Bastard
Fuji Shaped Bastard
Posts: 2384
Joined: Sun Jul 31, 2011 1:11 pm

Re: hatari and midi

Post by Eero Tamminen »

If I remember right, PortMidi support isn't enabled on Hatari Linux packages. It's only enabled for pre-built Mac & Windows binaries, because that's the only option that works on them.
copson
Atariator
Atariator
Posts: 17
Joined: Sun Aug 24, 2008 7:46 pm
Location: Edinburgh, Scotland

Re: hatari and midi

Post by copson »

Hi,
I found this thread and wanted to discuss a bit about Hatari for Windows and MIDI, I hope it is ok?

I have been trying to use Hatari for my EPSS development and it works fine in general. The tricky thing for me always comes to when I want to try to test some MIDI. As I use Hatari for Windows I had to setup Ubuntu on a vm running Hatari where I could test some MIDI by the help of the ELSA devices, virtual keyboard etc and this worked ok, but this still did not allow me to use real MIDI in the VM type I used, Hyper-V on Win10, only virtual MIDI keyboard as input.

I tried to compile my own Hatari Windows version with PortMidi enabled but failed to get it working. I think I managed to get a few MIDI event registered at some point, but it then stopped and I never dug any deeper in that. EDIT: I found the Hatari bugs.txt and it basically confirms my tests, only a few bytes seem to go through and then it refuses to accept anything else.

As I had the Hatari source code fully compiling, I decided to "roll my own" and made some simple changes to the midi.c in Hatari: instead of using the file specified as the MIDI-in device as a device I used it as a file and let Hatari open the file, read the contents and delete the file when it had read all the bytes in it. In a separate PC process, I then took real MIDI device input and wrote it this file, if it didnt alredy exist (as this then meant that Hatari was still reading from it). This approach seems promising in my first tests yesterday evening.

As this is just a personal "hack" to simplify my own development for now, I thought I should check the interest in leverage this to something that could be useful for others? I also might have overcomplicated things here, so if anybody knows a good way of getting proper MIDI working on Windows in latest Hatari, please explain! :)
copson
Atariator
Atariator
Posts: 17
Joined: Sun Aug 24, 2008 7:46 pm
Location: Edinburgh, Scotland

Re: hatari and midi

Post by copson »

In reply to myself, I have now realized both MIDI in and MIDI out on Hatari for Windows and you can see how it works on this video: https://youtu.be/DVo6fiKYBdU.
User avatar
Cyprian
10 GOTO 10
10 GOTO 10
Posts: 2229
Joined: Fri Oct 04, 2002 11:23 am
Location: Warsaw, Poland

Re: hatari and midi

Post by Cyprian »

@copson well done.
Lack of MIDI support under Windows was the most important drawback. Would be possible to add your fix to the official Hatari release?
Mega ST 1 / 7800 / Portfolio / Lynx II / Jaguar / TT030 / Mega STe / 800 XL / 1040 STe / Falcon030 / 65 XE / 520 STm / SM124 / SC1435
DDD HDD / AT Speed C16 / TF536 / SDrive / PAK68/3 / Lynx Multi Card / LDW Super 2000 / XCA12 / SkunkBoard / CosmosEx / SatanDisk / UltraSatan / USB Floppy Drive Emulator / Eiffel / SIO2PC / Crazy Dots / PAM Net
Hatari / Steem SSE / Aranym / Saint
http://260ste.atari.org
midimark
Atariator
Atariator
Posts: 24
Joined: Sat Jan 09, 2021 7:48 pm

Re: hatari and midi

Post by midimark »

Hello, I thought I would post how I got Hatari and midi hooked up at least on my machine. It might be helpful I hope.
I am using Ubuntu Studio, "Jack" is set up under Ubuntu studio, but is available for other linux flavors, and maybe Mac and PC?
Jack manages audio and midi connections. First start Qsynth with a suitable sound font loaded (GM) Then boot up Jack and look at the connections window. Qsynth under settings should be set to alsa-seq. In the terminal type in sudo modprobe snd-virmidi. This should reveal the virtual midi connections
under the ALSA tab in Jack. Then you can in Jack connect the virtual midi connections to qsynth.
Then in Hatari, go to the devices area and enable midi and for the input and output use the first couple of numbers that now should be listed for example
"midic0d1 or 2" They should be visible when you go to "browse"
Hope that isn't too confusing.
I got this to work in Hatari 2.21 but not in 2.3.1 (?)
If I can learn to compile a program from source then maybe I can compile Hatari with port midi, since it seems like the versions I have been able to install in Linux are not compiled that way except 2.2.1
midimark
Atariator
Atariator
Posts: 24
Joined: Sat Jan 09, 2021 7:48 pm

Re: hatari and midi

Post by midimark »

OOps I meant neither versions 2.2.1 or 2.3.1 are compiled with port midi, with linux anyway,as far as I could tell.
copson
Atariator
Atariator
Posts: 17
Joined: Sun Aug 24, 2008 7:46 pm
Location: Edinburgh, Scotland

Re: hatari and midi

Post by copson »

midimark wrote: Mon Mar 01, 2021 4:31 pm Hello, I thought I would post how I got Hatari and midi hooked up at least on my machine. It might be helpful I hope.
I am using Ubuntu Studio, "Jack" is set up under Ubuntu studio, but is available for other linux flavors, and maybe Mac and PC?
Jack manages audio and midi connections. First start Qsynth with a suitable sound font loaded (GM) Then boot up Jack and look at the connections window. Qsynth under settings should be set to alsa-seq. In the terminal type in sudo modprobe snd-virmidi. This should reveal the virtual midi connections
under the ALSA tab in Jack. Then you can in Jack connect the virtual midi connections to qsynth.
Then in Hatari, go to the devices area and enable midi and for the input and output use the first couple of numbers that now should be listed for example
"midic0d1 or 2" They should be visible when you go to "browse"
Hope that isn't too confusing.
I got this to work in Hatari 2.21 but not in 2.3.1 (?)
If I can learn to compile a program from source then maybe I can compile Hatari with port midi, since it seems like the versions I have been able to install in Linux are not compiled that way except 2.2.1
As far as I can see, getting PortMidi to work in Hatari for Windows will need a bit more work in the source codes. The bugs regarding PortMidi mentioned here: https://hatari.tuxfamily.org/doc/bugs.txt are still present in reasonably current Hatari for Windows compiled with PortMidi, that is what I first tried a few months ago. I don't know about the status of Midi in Mac versions. PortMidi is however a bit unsupported at the moment it seems, so it will be difficult to get modern bug fixed library as far as I have seen, so it might be a bit of a dead end at the moment.

The instructions you describe will work good in Ubuntu to get Midi working though. I got MIDI working fine with the ALSA in Hatari for Ubuntu and running Ubuntu in a Virtual Machine also worked but I really wanted to run the native Hatari for Windows for my use case. Unfortunately Windows doen't use devices the same way Linux (and maybe also Mac?) does which is why it is, to my knowledge, not possible to get Midi working on current Hatari for Windows.

And for those who want to start compiling Hatari for Windows, I would not have gotten anywhere myself if I was not being pointed to this excellent guide by Steven Tattersall: http://clarets.org/steve/projects/20201 ... ndows.html Definitely worth mentioning!
And using the Ubuntu 20.20 WSL2 environment on Windows 10 and then using Steven's guide makes it so much simpler to get going.
Last edited by copson on Mon Mar 01, 2021 5:48 pm, edited 1 time in total.
User avatar
Cyprian
10 GOTO 10
10 GOTO 10
Posts: 2229
Joined: Fri Oct 04, 2002 11:23 am
Location: Warsaw, Poland

Re: hatari and midi

Post by Cyprian »

there is no midic0d1 or midic0d2 under Windows
Mega ST 1 / 7800 / Portfolio / Lynx II / Jaguar / TT030 / Mega STe / 800 XL / 1040 STe / Falcon030 / 65 XE / 520 STm / SM124 / SC1435
DDD HDD / AT Speed C16 / TF536 / SDrive / PAK68/3 / Lynx Multi Card / LDW Super 2000 / XCA12 / SkunkBoard / CosmosEx / SatanDisk / UltraSatan / USB Floppy Drive Emulator / Eiffel / SIO2PC / Crazy Dots / PAM Net
Hatari / Steem SSE / Aranym / Saint
http://260ste.atari.org
copson
Atariator
Atariator
Posts: 17
Joined: Sun Aug 24, 2008 7:46 pm
Location: Edinburgh, Scotland

Re: hatari and midi

Post by copson »

Cyprian wrote: Mon Mar 01, 2021 2:18 pm @copson well done.
Lack of MIDI support under Windows was the most important drawback. Would be possible to add your fix to the official Hatari release?
The "fix" is really two parts:
1. some minor code changes to the Hatari source code (currently only a few lines in file.c/file.h and midi.c). I am not familiar with the process of "adding a fix" to Hatari but I think it involves a bit of involvement with the Hatari development team to coordinate it, regression test it etc, something I have not yet done or planned for at the moment. Also even if the code is small, it still needs to be tidied up a bit and written correctly with flags and tested in all environments so I won't cause any compiling error for the other builds. I have not done that either.

2. the "Hatari Midi Mapper" component I have done which is just a Windows executable that I can put up on https://epss.copson.se for download.

I could start by creating a fork of the hatari repo on github and add the changes there and you can then pick it from there? I probably then also commit a working Hatari.exe so it should be possible to just copy the exe from there. I will not promise I will maintain the fork forever and keep it updated with main Hatari though :)

As the Hatari MIDI Mapper is a executable with a little UI, it is a bit of a "hack" as the settings are mixed between Hatari and the MIDI mapper. In a proper "Hatari" windows solution I would probably instead design the mapper as a headless Windows Service and then move the UI for setting the MIDI ports to Hatari Settings (where they are today when you use PortMidi). Then use a bit more high level protocol between Hatari and the service preferably based on sockets or even REST instead of my current mix of files and named pipes. It could then even be used for more things than just MIDI, COM-port, printer, cartridge based communications (mapping for example NetUSBee network access over to Windows network devices) and so on...
midimark
Atariator
Atariator
Posts: 24
Joined: Sat Jan 09, 2021 7:48 pm

Re: hatari and midi

Post by midimark »

I just don't understand why I can get midi (in Linux) and Hatari to work in V. 2.2.1 but not in V. 2.3.1???
User avatar
Eero Tamminen
Fuji Shaped Bastard
Fuji Shaped Bastard
Posts: 2384
Joined: Sun Jul 31, 2011 1:11 pm

Re: hatari and midi

Post by Eero Tamminen »

copson wrote: Mon Mar 01, 2021 5:23 pm The bugs regarding PortMidi mentioned here: https://hatari.tuxfamily.org/doc/bugs.txt are still present in reasonably current Hatari for Windows compiled with PortMidi, that is what I first tried a few months ago. I don't know about the status of Midi in Mac versions. PortMidi is however a bit unsupported at the moment it seems, so it will be difficult to get modern bug fixed library as far as I have seen, so it might be a bit of a dead end at the moment.
My current assumption is that the bug is in the Hatari PortMidi integration code (converting Atari MIDI stream bytes to PortMidi MIDI events) contributed to Hatari, not in the PortMidi library itself.

What is needed:
1) Test different MIDI events to isolate what specific kind of MIDI event(s) are not converted correctly (causes PortMidi to reject the generated MIDI event)
2) Debug Hatari PortMidi integration code to find out why those events are not accepted by PortMidi (probably requires reading PortMidi library code too, as PortMidi docs are completely insufficient for that)

Step 1) could be done by Hatari user that doesn't know anything about coding, just MIDI.
User avatar
Eero Tamminen
Fuji Shaped Bastard
Fuji Shaped Bastard
Posts: 2384
Joined: Sun Jul 31, 2011 1:11 pm

Re: hatari and midi

Post by Eero Tamminen »

midimark wrote: Mon Mar 01, 2021 10:00 pm I just don't understand why I can get midi (in Linux) and Hatari to work in V. 2.2.1 but not in V. 2.3.1???
What error you get?
midimark
Atariator
Atariator
Posts: 24
Joined: Sat Jan 09, 2021 7:48 pm

Re: hatari and midi

Post by midimark »

I get midi i/0 failed. I set up the same as with 2.2.1, but 2.2.1 works. Could be that the 2 versions were compiled differently. 2.3.1 comes from repository, but 2.2.1 is installed so I access it in the terminal.
copson
Atariator
Atariator
Posts: 17
Joined: Sun Aug 24, 2008 7:46 pm
Location: Edinburgh, Scotland

Re: hatari and midi

Post by copson »

Eero Tamminen wrote: Tue Mar 02, 2021 11:08 am
copson wrote: Mon Mar 01, 2021 5:23 pm The bugs regarding PortMidi mentioned here: https://hatari.tuxfamily.org/doc/bugs.txt are still present in reasonably current Hatari for Windows compiled with PortMidi, that is what I first tried a few months ago. I don't know about the status of Midi in Mac versions. PortMidi is however a bit unsupported at the moment it seems, so it will be difficult to get modern bug fixed library as far as I have seen, so it might be a bit of a dead end at the moment.
My current assumption is that the bug is in the Hatari PortMidi integration code (converting Atari MIDI stream bytes to PortMidi MIDI events) contributed to Hatari, not in the PortMidi library itself.

What is needed:
1) Test different MIDI events to isolate what specific kind of MIDI event(s) are not converted correctly (causes PortMidi to reject the generated MIDI event)
2) Debug Hatari PortMidi integration code to find out why those events are not accepted by PortMidi (probably requires reading PortMidi library code too, as PortMidi docs are completely insufficient for that)

Step 1) could be done by Hatari user that doesn't know anything about coding, just MIDI.
A quick look in midi.c, I think I can see one issue: the "running status" is not supported as the runningStatus byte is commented out.
When byte is higher than 0x80, we have a status byte and it should be saved, as the commented code suggests.
However, it has also to be used when the first byte after a complete message is a non-status byte and it seems that this logic is lacking.

Something like this could maybe work:

Code: Select all

/**
 * collect bytes until valid MIDI event has been formed / four bytes of sysex data has been gathered
 * returns PmEvent when done, or NULL if it needs still more data
 * see MIDI 1.0 Detailed Spec 4.2, pages A-1..A-2 for discussion on running status
 */
static PmEvent* Midi_BuildEvent(Uint8 byte)
{
	static const Uint8 shifts[] = { 0,8,16,24 };
	static PmEvent midiEvent = { 0,0 };
	static Uint32 midimsg;
	static Uint8 runningStatus = 0;
	static Uint8 bytesToWait = 0;
	static Uint8 bytesCollected = 0;
	static bool processingSysex = false;
	static bool expectStatus = false;

	// -- status byte
	if (byte & 0x80)
	{
		if (byte >= 0xF8)
		{
			midiEvent.message = Pm_Message(byte,0,0);
			return &midiEvent;
		}
		else
		{
			processingSysex = false;
			if (byte >= 0xF0)
			{
				runningStatus = 0;
				if (byte == 0xF0)
				{
					processingSysex = true;
					bytesCollected = 1;
				}
				else if (byte == 0xF7)
				{
					midiEvent.message = midimsg | (((Uint32)byte) << shifts[bytesCollected]);
					midimsg = bytesToWait = bytesCollected = 0;
					return &midiEvent;
				}
				else
					bytesCollected = 0;
			}
			else
			{
				runningStatus = byte;
				bytesCollected = 0;
			}
		}
		midimsg = byte;
		bytesToWait = Midi_GetDataLength(byte);
		expectStatus = false;
	}

	// -- data byte
	else
	{
	    if (expectStatus) {
		// reuse the previous status here.
		midimsg |= ((Uint32)runningStatus) << shifts[++bytesCollected];
		expectStatus = false;
	    }

		if (processingSysex)
			midimsg |= ((Uint32)byte) << shifts[bytesCollected++];
		else
			midimsg |= ((Uint32)byte) << shifts[++bytesCollected];
		if (bytesCollected >= bytesToWait)
		{
			midiEvent.message = midimsg;
			midimsg = 0;
			bytesCollected = 0;
			bytesToWait = processingSysex ? 4 : 0;
			expectStatus = true;
			return &midiEvent;
		}
	}
	
	return NULL;
}
Unfortunately not tested as I haven't figured out how to build with PortMIDI... Do you know where I declare that PortMIDI should be used for Linux/Windows cross compiling?
User avatar
Eero Tamminen
Fuji Shaped Bastard
Fuji Shaped Bastard
Posts: 2384
Joined: Sun Jul 31, 2011 1:11 pm

Re: hatari and midi

Post by Eero Tamminen »

copson wrote: Wed Mar 03, 2021 6:27 pm Unfortunately not tested as I haven't figured out how to build with PortMIDI... Do you know where I declare that PortMIDI should be used for Linux/Windows cross compiling?
In general CMake should detect things from you build environment. This is what's used to detect PortMidi with Hatari:
https://git.tuxfamily.org/hatari/hatari ... Midi.cmake

I.e. CMake/compiler needs to find "portmidi.h" file and be able to link with "portmidi" library. E.g. on Debian/Ubuntu "portmidi-dev" package installs them to standard paths, so CMake/compiler finds them fine after one installs that package.

If PortMidi files are in some non-standard location in your environment, you need to set some environment or CMake variables that allow CMake / compiler to locate them. You might e.g. try running CMake with "-DPORTMIDI_INCLUDE_DIR=<dir> -DPORTMIDI_LIBRARY=<path to lib>" options.
User avatar
Eero Tamminen
Fuji Shaped Bastard
Fuji Shaped Bastard
Posts: 2384
Joined: Sun Jul 31, 2011 1:11 pm

Re: hatari and midi

Post by Eero Tamminen »

midimark wrote: Tue Mar 02, 2021 5:54 pm I get midi i/0 failed.
Could you provide the complete error message? Preferably with Hatari "--trace midi" option output.
copson
Atariator
Atariator
Posts: 17
Joined: Sun Aug 24, 2008 7:46 pm
Location: Edinburgh, Scotland

Re: hatari and midi

Post by copson »

Eero Tamminen wrote: Wed Mar 03, 2021 7:56 pm
copson wrote: Wed Mar 03, 2021 6:27 pm Unfortunately not tested as I haven't figured out how to build with PortMIDI... Do you know where I declare that PortMIDI should be used for Linux/Windows cross compiling?
In general CMake should detect things from you build environment. This is what's used to detect PortMidi with Hatari:
https://git.tuxfamily.org/hatari/hatari ... Midi.cmake

I.e. CMake/compiler needs to find "portmidi.h" file and be able to link with "portmidi" library. E.g. on Debian/Ubuntu "portmidi-dev" package installs them to standard paths, so CMake/compiler finds them fine after one installs that package.

If PortMidi files are in some non-standard location in your environment, you need to set some environment or CMake variables that allow CMake / compiler to locate them. You might e.g. try running CMake with "-DPORTMIDI_INCLUDE_DIR=<dir> -DPORTMIDI_LIBRARY=<path to lib>" options.
Thanks for the information, I got a bit further:
1. Installed portmidi-dev:
$ sudo apt-get install libportmidi-dev
Reading package lists... Done
Building dependency tree
Reading state information... Done
libportmidi-dev is already the newest version (1:217-6).

2. ./configure --cross-compile-win64_64 does not find it:
-- Could NOT find PortMidi (missing: PORTMIDI_LIBRARY PORTMIDI_INCLUDE_DIR)

ok, not default paths then maybe...

3. Checking where it is:
:/usr$ find . -name *portmidi*
./include/portmidi.h
./lib/x86_64-linux-gnu/libportmidi.so
./lib/x86_64-linux-gnu/libportmidi.so.0.0.0
./lib/x86_64-linux-gnu/libportmidi.so.0
./lib/x86_64-linux-gnu/libportmidi.a
./share/doc/libportmidi-dev
./share/doc/libportmidi0

4. Running cmake again with explicit paths after removing build caches:
cmake . -DCMAKE_BUILD_TYPE=Debug -DCMAKE_TOOLCHAIN_FILE=cmake/Toolchain-mingw32-win64_64.cmake -DPORTMIDI_INCLUDE_DIR=/usr/include -DPORTMIDI_LIBRARY=/lib/x86_64-linux-gnu/libportmidi

5. Getting closer:
-- Found PortMidi: /lib/x86_64-linux-gnu/libportmidi
-- Looking for Pm_Initialize
-- Looking for Pm_Initialize - not found

So it looks like the libs I get with apt-get libportmidi-dev are not quite right then?
But... I am trying to compile the Windows version using Linux crosscompiling... So then I woudl need a PortMIDI for Windows, which may explain the errors with this approach, i.e. I trying to compile a Windows exe using linux libraries :)

So I somehow have to get hold of the correct PortMIDI libs for Windows, right?
gemtwo
Atarian
Atarian
Posts: 8
Joined: Thu May 18, 2017 1:16 pm

Re: hatari and midi

Post by gemtwo »

Ok Copson, keep it coming and we will all be rewarded with your blessing work :)
I will try this soon!
User avatar
Eero Tamminen
Fuji Shaped Bastard
Fuji Shaped Bastard
Posts: 2384
Joined: Sun Jul 31, 2011 1:11 pm

Re: hatari and midi

Post by Eero Tamminen »

copson wrote: Thu Mar 04, 2021 12:03 pm So I somehow have to get hold of the correct PortMIDI libs for Windows, right?
Yes, otherwise CMake test of linking/running PortMidi function fails.
Post Reply

Return to “Hatari”