Friday, 31 August 2012

FLUIDSYNTH on the Raspberry PI

I installed  FluidSynth (a real-time software synthesizer based on the SoundFont 2 specifications) to test out my MIDI prototype hardware on the Raspberry Pi and ttymidi. Installation is straight forward.

sudo apt-get install fluidsynth



As I will only be using FluidSynth via the command line I chose to work from two PuTTY ssh. One to launch FluidSynth ond the other ttymidi.


In one ssh the command line.
 
 
fluidsynth -a alsa -m alsa_seq /usr/share/sounds/sf2/FluidR3_GM.sf2

And the other ssh.

ttymidi -s /dev/ttyAMA0 -b 38400 -v &
aconnect -iol
aconnect 129:0 128:0

Where 129:0 is the ttymidi output ( midi input) and 128:0 is the midi input for FluidSynth.

TTYMIDI on the Raspberry Pi

With Linux being Linux, there are several ways to do most things and MIDI over a serial port is no exception. On Intel X86 platform for example, ALSA comes with a snd-serial-u16550 driver, unfortunately at the time this is not supported on the Raspberry PI. There is however a simple alternative thanks to Thiago Teixeira who created ttymidi which creates a serial to ALSA interface.




I will not go into too much detail in this post as the basics are well covered at his site. The keyboard you can see is Virtual MIDI Piano Keyboard VMPK running on Windows 7 with a PuTTY remote shell.
 
 
sudo apt-get install libasound2-dev

wget http://www.varal.org/ttymidi/ttymidi.tar.gz
tar -zxvf ttymidi.tar.gz
cd ttymidi/
make
sudo make install




ttymidi -s /dev/ttyAMA0 -b 38400  &

Will run ttymidi in the back ground.


ttymidi -s /dev/ttyAMA0 -b 38400  -v

Will run ttymidi in verbose mode (midi traffic displayed on screen).


aconnect -iol

Will display available ALSA connecters, see fluidsynth

aconnect

Is used to connect inputs and outputs within ALSA.

aplaymidi -p 128:1 your_midid_file.mid will send a MIDI file to the MIDI out.




Serial Port MIDI on the Raspberry Pi

I noticed a project at Raspberry Pi was having to use a microcontroller to buffer 38400 Baud serial port data down to the Midi standard of 31250. As part of an ongoing larger project I solved this problem by changing init_uart_clock = 2441406 in /boot/config.txt on the raspberry Pi.












My proto-type MIDI hardware is the usual 6N138 opto isolator for the MIDI Input and a 7414 for the output shown below. The output from the6N138 is 3.3v and the output from the 7414 is at 5V. This was chosen because although the MIDI specification usually talks about the signal as a current loop, there are a few devices that are MIDI powered and require 5v. MIDI powered devices work by doing away with the opto-isolater and the safety it provides.

 
 
 
 
There are a couple of changes that are needed as by default, Raspbian "wheezy" Linux distro, sets it up a serial port tty. Using a consol make these changes can be made as follows.
 
sudo nano/boot/cmdline.txt
 
Edit the line

dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4  elevator=deadline rootwait
to
 
dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait

exit saving the changes


Next by changing init_uart_clock=3000000 to init_uart_clock=2441406, all baud rates will be reduced by about 81%. This is not an ideal solution as if you wanted to use the serial port for other projects all the standard baud rate timings would be out by 19%. However, from a MIDI point of view you can simply open /dev/ttyAMA0 at 38400 baud and you are up and running.
 
Again using a consol.
 
sudo nano /boot/config.txt
 
Add these lines to the end of the file
 
# change uart clock to 2441406 for midi 31250 baud rate
 
init_uart_clock=2441406
 
init_uart_baud=38400
 
save and exit, and reboot the Pi.

 
The Raspberry Pi can now talk MIDI by opening /dev/ttyAMA0 at 38400 baud or connected to ALSA using ttymidi driving FluidSynth.