
Getting (Linux) ALSA midi support to work with Hatari
=====================================================

If you don't have a real MIDI sequencer, you can use the MIDI synthesizer
of your sound card (if available) or use a software synthetizer.

For (Debian) package names and links to software referenced in this
text, see end of the text. Most of the distros should have in their
repositories packages at least for some of them though.

There are also some notes about midi networking.


Using a soundcard with built-in MIDI synthesis capability 
---------------------------------------------------------

If your soundcard is capable of playing MIDI sound (i.e. you can play a .mid
file with the "aplaymidi" command using the appropriate port), you can use this
synthesis device for Hatari, too. However, you still might have to install and
connect a virtual midi device, so that Hatari can access it through a
/dev/snd/midiC*D* device file (see instructions below).

Please note that you might also have to load instrument patches into your sound
card first, for example with the program "sfxload" for AWE64 based sound cards,
or with the program "sbiload" for OPL3 based sound cards.


Making MIDI soft-synthetizer to work with ALSA
-----------------------------------------------

Make Timidity into an ALSA output device with:
  timidity -Os -iA
(-O: output=alsa, -i: interface=alsa)

To make it use less CPU and be more responsive, use:
  timidity -Os -iA -B2,8 -EFreverb=0 -EFchorus=0
(-B: 2,8=set small buffers, -EFx=0: disable effects)

Make vkeybd (virtual midi keyboard app) into an ALSA input device with:
  vkeybd

View the resulting (software) ALSA input and output devices:
  aconnect -i -o

Then connect the vkeybd input to timidity output with:
  kaconnect
or if you don't have this UI program, just use 'aconnect' directly:
  aconnect <sender port> <receiver port>

Now you can use the virtual midi keyboard for testing the sound
synthesis.


Finally You can test how well midi files are played.
Check which ALSA port Timidity provides:
  aplaymidi -l

And use that port for playing a midi file:
  aplaymidi -p <port, e.g. 129:0> test.mid
(or use 'pmidi')

Note: Remember that you need to re-connect the (virtual) device
ports each time you restart them.


Using FluidSynth instead of Timidity
------------------------------------

Instead of Timidity, you also use other soft-synthetizers,
like FluidSynth:
  fluidsynth --audio-driver=alsa --midi-driver=alsa_seq soundfont.sf2

You could play a bit with other options to get more performance,
sound volume etc:
  --reverb=no --chorus=no -o synth.polyphony=16 --gain=0.6

And if you don't like the FluidSynth shell, use:
  --no-shell --server

FYI: FluidSynth v1.0.7a (in Debian Etch & Ubuntu Hardy) has buffer
overruns, but they're fixed in v1.0.8 (in Debian Lenny etc).


Making it all to work with Hatari
---------------------------------

Hatari requires midi hardware devices to work, it doesn't support
ALSA directly.  To get the software synth ALSA devices to appear
as HW midi devices, run following as *root*:
  modprobe snd-virmidi

When you list your ALSA output devices with:
  aconnect -o
You should see in addition to Timidity also 4 virtual hardware
devices.

Then connect (either with aconnect or kaconnect) the first virtual
HW port to the same Timidity port where you connected the virtual
midi keyboard.

Check which number was assigned by ALSA to the new virtual midi card:
  cat /proc/asound/cards

And give to Hatari the corresponding ALSA midi device.  In my case
VirMidi was Card 1 and as the port used above was first one, I give
Hatari the following midi device:
  hatari --midi /dev/snd/midiC1D0

Note: I also needed --nosound option because Hatari wanted exclusive
access to the PCM (sound) device and Timidity was already connected
to it.  This may not be the case if you've compiled Hatari with another
/ newer SDL_mixer library versions.


Sending the midi data over network
----------------------------------

If you direct the data to stdout, you can use e.g. ssh to pipe
it over network:
  hatari --midi /dev/stdout --log /dev/stderr |\
    ssh user@remote.site "cat>/dev/snd/midiC1D0"

Note that logging is re-directed to stderr so that it doesn't
mess the midi output.

Same can be done with socat so that the connections are bi-directional.
Once Hatari supports midi input, you can use socat like this to do
midi-networking between (emulated) Ataris:
  @remote.site:
    socat PIPE:redirect.pipe TCP4-LISTEN:www
    hatari --midi redirect.pipe --log stderr
  @local.site:
    socat PIPE:redirect.pipe TCP4:remote.site:www
    hatari --midi redirect.pipe --log stderr


Software
--------

In Debian, the tools mentioned above come from following packages:
- alsa-utils (aconnect, aplaymidi)
- alsa-tools (sbiload)
- awesfx (sfxload)
- pmidi
- vkeybd
- fluidsynth
- fluid-soundfont-* (soundfonts)
- timidity
- socat
See http://packages.debian.org/ for more details on them.

Below are upstream links to some of these tools.

Vkeybd:
  http://alsa.opensrc.org/Vkeybd

Patch (ALSA connecting) utilities:
  http://alsa.opensrc.org/AlsaMidiPatchbays    

FluidSynth:
  http://www.iiwu.org/fluidsynth/

Soundfonts:
  http://alsa.opensrc.org/SoundFontHandling

List of some soft-synthetizers:
  http://alsa.opensrc.org/SoftSynths

Kaconnect:
  http://alsamodular.sourceforge.net/

socat:
  http://www.dest-unreach.org/socat/

MidiMaze:
  http://en.wikipedia.org/wiki/MIDI_Maze


Overviews
---------

ALSA midi overview:
  http://alsa.opensrc.org/AlsaMidiOverview

How to set up soundcards with hardware MIDI synthesis capability (AWE & OPL3)
  https://help.ubuntu.com/community/Midi/HardwareSynthesisSetup

Virtual midi hardware setup:
  http://www.tldp.org/HOWTO/MIDI-HOWTO-10.html

Timidity Howto:
  http://lau.linuxaudio.org/TiMidity-howto.html

Midi with ALSA (old):
  http://www.linuxfocus.org/English/September2002/article259.shtml

Midi on Linux:
  http://www.linuxjournal.com/article/7773
