Joined: 18 May 2002
Location: (Inactive member)
|Posted: Sun Dec 29, 2002 11:22 pm Post subject: MIDI with ALSA
|MIDI with ALSA
This tip builds on the ALSA Configuration Guide and is intended to help you get MIDI support working, in particular using the serial port to connect directly to a keyboard/synth that has a serial 'To host' connector. You should read the ALSA Configuration Guide before reading this tip. This information has been put together from various sources, but there a dearth of useful documentation on this aspect of ALSA and some of it is out of date.
The snd-serial-u16550.o and snd-virmidi.o modules are used. They create MIDI devices that are treated as if they were physical sound cards and need to be configured as such in /etc/modules.d/alsa. virmidi is used to create virtual MIDI loopback devices.
Why use the serial port for MIDI?
- Some sound modules support faster baud-rates than MIDI (for better timing)
- You already have a suitable NULL modem cable
- Your sound card has a MIDI out but you don't already have the right cable
- Your sound card doesn't have MIDI out and you don't want to buy an external interface
- Your synth has additional features not accessible via its standard MIDI in
What you will need
- ALSA set up according to the guide
- A NULL-modem cable to connect your serial port to the synth
What doesn't work?
- I have not yet found out how to get the OSS compatibility device /dev/sequencer to send output to the serial MIDI port. This means that most legacy OSS applications still won't be able to use the external synth. I think this should be possible and welcome any suggestions.
- The KDE Control Center still doesn't recognise any MIDI devices. Is this because KDE is not yet fully ALSA-compliant?
Compiling the drivers
There are two ALSA drivers (in addition to the one for your sound card that you have already installed when you followed the ALSA Configuration Guide) that are of interest. The serial-u16550 driver enables the serial port for MIDI, and the virmidi driver creates a kind of MIDI patch panel that allows you to route MIDI from one application to another (e.g. a sequencer to Timidity). These two drivers are treated as if they were actual sound cards, so you need to compile them by re-emerging alsa-driver, for example:
|# env ALSA_CARDS='virmidi serial-u16550 intel8x0' emerge alsa-driver |
Before installing the serial MIDI driver the serial port must be reset. This is because by default a system console is usually present on the serial port. So first, find out the IRQ and port address of your serial port like this:
|# /bin/setserial /dev/ttyS0
/dev/ttyS0, UART: unknown, Port: 0x03f8, IRQ: 4
If your results are different, you'll have to modify the corresponding line below accordingly.
Now go to /etc/modules.d/alsa. First increase the sound card limit. If you have one real sound card, add to this the serial driver 'sound card' and the virmidi 'sound card' to make three:
|options snd snd_major=116 snd_cards_limit=3 |
Now add the additional 'sound cards'. For example, I have an Intel 810 on-board sound card in addition to serial and virmidi:
|## ALSA portion
alias snd-card-0 snd-intel8x0
# Before loading snd-serial-u16550 module, reset the serial port so
# that it can be used.
pre-install snd-serial-u16550 /bin/setserial /dev/ttyS0 uart none
alias snd-card-1 snd-serial-u16550
options snd-serial-u16550 snd_port=0x3f8 snd_irq=4 snd_speed=38400
alias snd-card-2 snd-virmidi
When ALSA starts up, it tries to load snd-card-x where x is from zero to one less than the sound card limit set above. These lines tell it the real names of the sound card driver modules and any additional configuration needed. 38400 baud is the usual setting for the 'To host' connector on a sound module. Some support higher speeds - consult your manual. (The 'Host select' switch on the synth should usually be set to PC-2 or similar.)
OSS looks for sound-slot-x in much the same way. So for OSS compatibility, we tell it that sound-slot-0 is snd-card-0 (which is snd-intel8x0 or whatever) and so on:
|## OSS/Free portion
alias sound-slot-0 snd-card-0
alias sound-slot-1 snd-card-1
alias sound-slot-2 snd-card-2
Now we need to define the sound services that are provided by each 'card'. There are several possible services and in the OSS world they correspond to entries in /dev. Each service has a minor number and some of them have OSS compatibility modules. Below are listed the services I know about:
|device minor(s) compat module explanation
mixerX 0 + 16*X snd-mixer-oss.o Mixers, where X is 00 or 01
sequencer 1 snd-seq-oss.o Global sequencer device, first type.
Used by applications such as 'playmidi'.
midiX 2 + 16*X Raw MIDI ports, X is 00-03
dspX 3 + 16*X snd-pcm-oss.o Digital (PCM) audio, X is 00 or 01
audioX 4 + 16*X snd-pcm-oss.o Sun .au format audio, X is 00 or 01
sndstat 6 Human-readable info. Not ALSA.
music / 8 snd-seq-oss.o Alternate global sequencer device. Used by
sequencer2 applications such as jazz++. May not work.
adspX 12 snd-pcm-oss.o X is 00, 01, 02 or 03.
patmgr1 33 Sequencer patch manager device.
In my setup, my Intel sound card doesn't support a sequencer so I want this on the external synth on the serial port (but note I haven't made this work properly yet). Conversely, the external serial port won't support digital audio. Here's an example from my /etc/modules.d/alsa file:
|# OSS/Free portion - card #1
# Intel810 Sound card
alias sound-service-0-0 snd-mixer-oss
alias sound-service-0-3 snd-pcm-oss
alias sound-service-0-12 snd-pcm-oss
## OSS/Free portion - card #2
# Serial MIDI 'card'
alias sound-service-1-1 snd-seq-oss
alias sound-service-1-8 snd-seq-oss
# OSS/Free portion - card #2
# Virtual MIDI 'card'
There is no OSS compatibility module to specify for raw MIDI, so the Virtual MIDI section is empty. By cross-checking with the table above, you should be able to see how the services are assigned. The first number is the sound slot number and the second is the minor number of the device. Finally, ensure that you have the following settings too, which ensure that when /dev/mixer, /dev/dsp, and /dev/midi are requested by an OSS application, the relevant compatibility modules are loaded:
|alias /dev/mixer snd-mixer-oss
alias /dev/dsp snd-pcm-oss
alias /dev/midi snd-seq-oss
When you've done this, run update-modules to get the changes incorporated in your main modules.conf file. When this is done, you can restart ALSA with
|# /etc/init.d/alsasound restart |
It seems you can ignore the messages about no state present for the MIDI-only 'cards', since they don't have any controls to save in the card state information.
You can test out your MIDI connection with any ALSA-aware sequencer/MIDI player. pmidi is suitable. There is as yet no ebuild but it is easy to install. With pmidi, list the available MIDI ports:
|$ pmidi -l
Port Client name Port name
72:0 External MIDI 1 MIDI 1-0
80:0 Virtual Raw MIDI 2-0 VirMIDI 2-0
81:0 Virtual Raw MIDI 2-1 VirMIDI 2-1
82:0 Virtual Raw MIDI 2-2 VirMIDI 2-2
83:0 Virtual Raw MIDI 2-3 VirMIDI 2-3
Port 72:0 is the serial MIDI in this case, so play a file with
|pmidi -p72:0 myfile.mid |
There is a good HOWTO on using soft synths with virmidi at linux-sound.org. Essentially, virmidi allows you to loop the output sent to a virtual MIDI out interface back to another virtual MIDI in interface. This allows you to connect the output of one MIDI application to the input of another.
That's the end of this tip. If you know how to solve the problem with getting /dev/sequencer to work with an external synth using the serial port, or if you know how to get the KDE Control Center to recognise the ALSA MIDI devices, please shout up.