Saturday, July 27, 2013

Synthesizer

Goal: To connect my MIDI Keyboard (M-Audio: KeyRig 49) to the Raspberry Pi and play music.

I used to have software on my PC to enable me to play my MIDI Keyboard, but it became a pain to load up the PC, run the software etc..etc.. Then beware if you changed something and saved on exit by mistake...

Along came the Raspberry Pi (and my son)

I wanted him to be able to play on the Midi Keyboard, without any complex setup.

I now have a Raspberry Pi plugged into the Midi Keyboard, and it can synthesize almost any instrument I want.  I defaulted it to several instrument, and can change it on the fly via the Midi Keyboard program function.

How: FluidSynth and some config and scripts.

It took me several hours to figure it all out mind you!  And no-one seemed to do what I wanted to do.

Instructions:
1) Install FluidSynth
 sudo apt-get install fluidsynth 

2) Create a new config file in the Pi home folder
 sudo nano /home/pi/config.txt


Place the following code in the config file:
echo "Loading Config"
gain 5
select <channel> <sound font> <bank> <instrument>
...
channels
e.g. select <channel> <sound font> <bank> <instrument>
select 0 1 0 123

My config file looks like this currently:

echo "Loading Config JPB v0.1"
gain 5
select 1 1 0 69
select 2 1 0 70
select 3 1 0 105
select 4 1 0 114
select 5 1 0 115
select 6 1 128 48
channels
This loads several instrument into different channels, in my case, a English horn in channel 1, Basson in 2, Banjo in 3 etc.
For a full list of instrument and banks run fluidsynth (with the SoundFont file) and type "inst 1" (List also provided at the bottom of this post for reference.)

3) Create a auto run script:
sudo nano /home/pi/start_fluid

#!/bin/bash
# Script to start FluidSynth & aconnect
echo Attempting to start FluidSynth
amixer cset numid=3 1
sudo fluidsynth -si -a alsa -m alsa_seq /usr/share/sounds/sf2/FluidR3_GM.sf2 &
sleep 5
sudo pkill fluidsynth
amixer cset numid=3 1
sudo fluidsynth -si -f /home/pi/config.txt -a alsa -m alsa_seq /usr/share/sounds/sf2/FluidR3_GM.sf2 &
sleep 10
aconnect 20:0 128:0

I had some issue where if you start FluidSynth for the first time, the sound would be all scrambled.  So I set the volume to 0%, run it, then kill it, then set the volume to 90% and reload.
My Midi Keyboard loaded on 20:0, run aconnect -i to look for the input of your controller.

4) do the usual chmod 755 on the script and add it to /etc/rc.local file  (before the exit 0)
e.g.
 sudo nano /etc/rc.local  

The file should look something like this: ( I just added the /home/pi/start_fluid at the end...)
 #  
 # By default this script does nothing.  
 # Print the IP address  
 _IP=$(hostname -I) || true  
 if [ "$_IP" ]; then  
  printf "My IP address is %s\n" "$_IP"  
 fi  
 /home/pi/start_fluid  
 exit 0  

Instruments: <bank>-<instrument> Name of instrument
 Instruments:  
 000-000 Yamaha Grand Piano  
 000-001 Bright Yamaha Grand  
 000-002 Electric Piano  
 000-003 Honky Tonk  
 000-004 Rhodes EP  
 000-005 Legend EP 2  
 000-006 Harpsichord  
 000-007 Clavinet  
 000-008 Celesta  
 000-009 Glockenspiel  
 000-010 Music Box  
 000-011 Vibraphone  
 000-012 Marimba  
 000-013 Xylophone  
 000-014 Tubular Bells  
 000-015 Dulcimer  
 000-016 DrawbarOrgan  
 000-017 Percussive Organ  
 000-018 Rock Organ  
 000-019 Church Organ  
 000-020 Reed Organ  
 000-021 Accordian  
 000-022 Harmonica  
 000-023 Bandoneon  
 000-024 Nylon String Guitar  
 000-025 Steel String Guitar  
 000-026 Jazz Guitar  
 000-027 Clean Guitar  
 000-028 Palm Muted Guitar  
 000-029 Overdrive Guitar  
 000-030 Distortion Guitar  
 000-031 Guitar Harmonics  
 000-032 Acoustic Bass  
 000-033 Fingered Bass  
 000-034 Picked Bass  
 000-035 Fretless Bass  
 000-036 Slap Bass  
 000-037 Pop Bass  
 000-038 Synth Bass 1  
 000-039 Synth Bass 2  
 000-040 Violin  
 000-041 Viola  
 000-042 Cello  
 000-043 Contrabass  
 000-044 Tremolo  
 000-045 Pizzicato Section  
 000-046 Harp  
 000-047 Timpani  
 000-048 Strings  
 000-049 Slow Strings  
 000-050 Synth Strings 1  
 000-051 Synth Strings 2  
 000-052 Ahh Choir  
 000-053 Ohh Voices  
 000-054 Synth Voice  
 000-055 Orchestra Hit  
 000-056 Trumpet  
 000-057 Trombone  
 000-058 Tuba  
 000-059 Muted Trumpet  
 000-060 French Horns  
 000-061 Brass Section  
 000-062 Synth Brass 1  
 000-063 Synth Brass 2  
 000-064 Soprano Sax  
 000-065 Alto Sax  
 000-066 Tenor Sax  
 000-067 Baritone Sax  
 000-068 Oboe  
 000-069 English Horn  
 000-070 Bassoon  
 000-071 Clarinet  
 000-072 Piccolo  
 000-073 Flute  
 000-074 Recorder  
 000-075 Pan Flute  
 000-076 Bottle Chiff  
 000-077 Shakuhachi  
 000-078 Whistle  
 000-079 Ocarina  
 000-080 Square Lead  
 000-081 Saw Wave  
 000-082 Calliope Lead  
 000-083 Chiffer Lead  
 000-084 Charang  
 000-085 Solo Vox  
 000-086 Fifth Sawtooth Wave  
 000-087 Bass & Lead  
 000-088 Fantasia  
 000-089 Warm Pad  
 000-090 Polysynth  
 000-091 Space Voice  
 000-092 Bowed Glass  
 000-093 Metal Pad  
 000-094 Halo Pad  
 000-095 Sweep Pad  
 000-096 Ice Rain  
 000-097 Soundtrack  
 000-098 Crystal  
 000-099 Atmosphere  
 000-100 Brightness  
 000-101 Goblin  
 000-102 Echo Drops  
 000-103 Star Theme  
 000-104 Sitar  
 000-105 Banjo  
 000-106 Shamisen  
 000-107 Koto  
 000-108 Kalimba  
 000-109 BagPipe  
 000-110 Fiddle  
 000-111 Shenai  
 000-112 Tinker Bell  
 000-113 Agogo  
 000-114 Steel Drums  
 000-115 Woodblock  
 000-116 Taiko Drum  
 000-117 Melodic Tom  
 000-118 Synth Drum  
 000-119 Reverse Cymbal  
 000-120 Fret Noise  
 000-121 Breath Noise  
 000-122 Sea Shore  
 000-123 Bird Tweet  
 000-124 Telephone  
 000-125 Helicopter  
 000-126 Applause  
 000-127 Gun Shot  
 008-004 Detuned EP 1  
 008-005 Detuned EP 2  
 008-006 Coupled Harpsichord  
 008-014 Church Bell  
 008-016 Detuned Organ 1  
 008-017 Detuned Organ 2  
 008-019 Church Organ 2  
 008-021 Italian Accordion  
 008-024 Ukulele  
 008-025 12 String Guitar  
 008-026 Hawaiian Guitar  
 008-028 Funk Guitar  
 008-030 Feedback Guitar  
 008-031 Guitar Feedback  
 008-038 Synth Bass 3  
 008-039 Synth Bass 4  
 008-040 Slow Violin  
 008-048 Orchestral Pad  
 008-050 Synth Strings 3  
 008-061 Brass 2  
 008-062 Synth Brass 3  
 008-063 Synth Brass 4  
 008-080 Sine Wave  
 008-107 Taisho Koto  
 008-115 Castanets  
 008-116 Concert Bass Drum  
 008-117 Melo Tom 2  
 008-118 808 Tom  
 009-125 Burst Noise  
 016-025 Mandolin  
 128-000 Standard  
 128-001 Standard 1  
 128-002 Standard 2  
 128-003 Standard 3  
 128-004 Standard 4  
 128-005 Standard 5  
 128-006 Standard 6  
 128-007 Standard 7  
 128-008 Room  
 128-009 Room 1  
 128-010 Room 2  
 128-011 Room 3  
 128-012 Room 4  
 128-013 Room 5  
 128-014 Room 6  
 128-015 Room 7  
 128-016 Power  
 128-017 Power 1  
 128-018 Power 2  
 128-019 Power 3  
 128-024 Electronic  
 128-025 TR-808  
 128-032 Jazz  
 128-033 Jazz 1  
 128-034 Jazz 2  
 128-035 Jazz 3  
 128-036 Jazz 4  
 128-040 Brush  
 128-041 Brush 1  
 128-042 Brush 2  
 128-048 Orchestra Kit  



Ping me if you get stuck and I will try to help :)

Additional:
You can download the soundfile from: (If it does not exists /usr/share/sounds/sf2)
http://ftp.uk.debian.org/debian/pool/main/f/fluid-soundfont/fluid-soundfont_3.1.orig.tar.gz

Welcome

Welcome.

After doing several Raspberry Pi projects, I've decided to start documenting them, (mostly for my own records, but also) so other people can benefit :)
https://docs.google.com/document/d/1cARrPUryp-X37QZy29hCMA3zVddDCg4NmBCIZEflel8/edit