I'm trying to create a synth using python with raspberry pi. I've written the code for manipulate the gpio input (i've modified the electronic of an old pianola and connected it to raspberry with the gpio) but I don't know how to create a sound. I want raspberry to reproduce a sound on the audio-output when I'm pressing a specific key (for example the pressing of the 9th key will callback a functionthat will reproduce the D note etc..). I gave a look to nsound (http://nsound.sourceforge.net/) and to PySynth (http://home.arcor.de/mdoege/pysynth/) but I can't get how to directly reproduce a sound without writing it before in a .wav file.

Any suggestions? Thanks

  • Has your question been answered? If so, please mark which one it is by clicking on the green check-mark. We are trying to get this site's Q:A ratio and % answered up to par. Thanks! – RPiAwesomeness Jan 11 '14 at 13:15

You need two things in order to accomplish your task:

  1. You need a Python library that can output MIDI (e.g. python-midi or python-rtmidi)
  2. You need a soft synth. You can use Timidity for that, which can be used as an ALSA sequencer device.

You can install timidity using apt. You have to start timidity in ALSA server mode:

timidity -iA -B2,8 -Os -EFreverb=0

This line is taken from the above link. The settings may vary, important is the -iA. The other parameters deal with buffering and disabling the reverb effect, to save CPU cycles.

You can test the timidity server by using aplaymidi:

aplaymidi -l
aplaymidi -p 65:0 somemidifile.mid

Where 65:0 has to be replaced by the MIDI sequencer that you see after running aplaymidi -l.

On the Python side, I cannot give you much advice, except reading the docs for the python-midi package. There you also have to connect to the ALSA sequencer device from above and send your MIDI events to that port.

| improve this answer | |

If you don't mind programming in C, there's a working audio generation / playing sample in the /opt/vc/src/hello_pi/hello_audio folder:

cd /opt/vc/src/hello_pi/libs/ilclient
cd /opt/vc/src/hello_pi/hello_audio

It outputs audio to the headphone connector by default, but when used as ./hello_audio.bin 1 the output changes to HDMI.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.