Ancher
Posts: 6
Joined: Sat Nov 15, 2014 10:41 am

Raspberry Pi Midi-keyboard

Sun Nov 16, 2014 2:17 pm

Hi everyone!

Here's a little details on my Raspberry Pi Midi-keyboard project I made as a degree work at school. This is my first attempt on making something with a RaspPi. https://www.youtube.com/watch?v=mpeTHLr1vlc

I've been interested in music and technology (and music technology) for almost my entire life, and when we had to come up with something to create as a degree work at our (finnish) college, I thougt it would be awesome to combine those two and create an instrument. As I thought of a method of implementation, I came across to my RaspPi I'd had for a while, which was perfect for the project.

I'm using Debian Wheezy as the distro, for it suits well with my needs. I made the program with Python, which I'd never used before. Luckily I learned it a little along the way. As the sound driver I used ALSA which comes within the distro. The software-synthesis happens with Timidity, a software synthesizer that renders midi data in real time without an external synthesizer (please correct me if I said it wrong). With Timidy its simple to change your soundfonts by just changing the .sf2-file path to timidity's configuration file.

The code:
The program itself is quite simple. I use Pygame to recognize the keypresses and output the midi data. If the key is pressed, the defined midi value plays. If the key is raised, the sound turns off.

Code: Select all

def keypress():
        event = pg.event.wait()
        if event.type == pg.KEYDOWN:
                if notes.get(key, 0):
                          midiout.note_on(notes.get(key) + noteOffset, velocity)
                return "d_" + key
        if event.type == pg.KEYUP:
                if notes.get(key, 0):
                          midiout.note_off(notes.get(key) + noteOffset, 0)
                return "u_" + key
The notes-variable contains all the key values, for example:

Code: Select all

notes = { "n":72, "j":73 } 
etc. You can just put the key names there, and the number defines what note plays (72 is C in the 4th octave).
The rest of the code just defines different fuctions for some keys on the keyboard. With the up and down arrow keys you can change volume, left and right changes the instrument and so forth. You can really change them to do anything you like. As the keyboards layout I used a pattern made by Paul von Janko (google it). I also made a layout like in an accordion. You can change it to anything.

Mechanics:
For the project I used an old Keytronic keyboard that I bought from a local flea market for 1€. The RaspPi is a model B+, which fits under the keyboard (with the help of two drawer handles :D). I also used a ps/2-usb adapter to connect the keyboard to the RaspPi. For the speaker I used a 10€ travel speaker (which could be better...).

Pics:
https://www.dropbox.com/s/l0rry4g4ff5r0 ... 6.jpg?dl=0
https://www.dropbox.com/s/q794vuxj3pq22 ... 5.jpg?dl=0
https://www.dropbox.com/s/vg8g7gsoelg7c ... 4.jpg?dl=0

So, there's a quick introduction to my project. Please feel free to leave feedback, good or bad. Cheers!



EDIT (3.10.2017):

The needed packages for the project are Python 2.7 and Pygame for it, ALSA (Advanced Linux Sound Architecture) and Timidity. You can install them straight through apt-get or by downloading them with wget.
Timidity needs to be set to use ALSA to produce the sounds and this can be done by going to /etc/init.d/timidity (It’s there so it launches when the device boots) and adding parameters
”-iAD -B2,10 -Os”
The parameters “iAD” and “-Os” make Timidity use ALSA as the sound driver and “-B2,10” sets the buffer size to 1024. The bigger the buffer size is, the more there’s latency (if it’s too small the sound distorts).
You can change the sound “fonts” by changing the .sf2-file path in Timidity’s config file at /etc/timidity/timidity.cfg.

The program’s source code can be found at my GitHub: https://github.com/anttus/Raspberry-Mid ... eyboard.py

Images:
https://www.dropbox.com/s/jwsw89qqeajnu ... 1.jpg?dl=0
https://www.dropbox.com/s/kd2ug7sfeuvd6 ... 2.jpg?dl=0
https://www.dropbox.com/s/47nzrtr2ekidg ... 3.jpg?dl=0
https://www.dropbox.com/s/xc321690q0ksw ... 4.jpg?dl=0
https://www.dropbox.com/s/iqrdor88limg8 ... 5.jpg?dl=0
https://www.dropbox.com/s/flqxupxjbtqul ... 6.jpg?dl=0

Hopefully this clears out the project a little. :)
Last edited by Ancher on Tue Oct 03, 2017 3:29 pm, edited 3 times in total.

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 9542
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: Raspberry Pi Midi-keyboard

Sun Nov 16, 2014 3:22 pm

are you using the PI's USART for a MIDI interface? With a couple of optocouplers and some DIN connectors you can build a real MIDI interface.

The PI supports the special MIDI baud rate that is needed.

Ancher
Posts: 6
Joined: Sat Nov 15, 2014 10:41 am

Re: Raspberry Pi Midi-keyboard

Sun Nov 16, 2014 3:42 pm

mahjongg wrote:are you using the PI's USART for a MIDI interface? With a couple of optocouplers and some DIN connectors you can build a real MIDI interface.

The PI supports the special MIDI baud rate that is needed.
I do not use Pi's USART. Thanks for the tip, though, I really hadn't thought about that... :)

User avatar
TonyD
Posts: 439
Joined: Thu Sep 08, 2011 10:58 am
Location: Newcastle, UK
Contact: Website

Re: Raspberry Pi Midi-keyboard

Mon Nov 17, 2014 4:29 pm

mahjongg wrote:...The PI supports the special MIDI baud rate that is needed.
Where would I find more information on setting the 31250-baud rate on the Pi?
Tony

Ravenous
Posts: 1956
Joined: Fri Feb 24, 2012 1:01 pm
Location: UK

Re: Raspberry Pi Midi-keyboard

Mon Nov 17, 2014 5:22 pm

I like the idea. Especially the raspi mounted underneath between the handles.

This reminds me - a while ago this project was on the front page:
http://www.raspberrypi.org/joytone/
That one might be interesting to you, as normal PC keyboard keys are roughly in a "hexagonal" arrangement. So with the right "isomorphic" arrangement of keys you can play in any key with a simple change of position. Of course there are only four rows of keys on a regular keyboard so the amount of different keys would be limited, but it might be worth trying :)

EDIT: I just googled Paul von Janko, and realised his keyboard arrangement IS isomorphic. So I think you've already built one! :oops:

Ancher
Posts: 6
Joined: Sat Nov 15, 2014 10:41 am

Re: Raspberry Pi Midi-keyboard

Mon Nov 17, 2014 5:30 pm

Ravenous wrote:I like the idea. Especially the raspi mounted underneath between the handles.

This reminds me - a while ago this project was on the front page:
http://www.raspberrypi.org/joytone/
That one might be interesting to you, as normal PC keyboard keys are roughly in a "hexagonal" arrangement. So with the right "isomorphic" arrangement of keys you can play in any key with a simple change of position. Of course there are only four rows of keys on a regular keyboard so the amount of different keys would be limited, but it might be worth trying :)

EDIT: I just googled Paul von Janko, and realised his keyboard arrangement IS isomorphic. So I think you've already built one! :oops:
That Joytone is something very, very cool! :D And yes, I think the Janko's layout is sort of isomorphic and it would be nice if there was like double the amount of keys on the keyboard...

Nerelod
Posts: 10
Joined: Sat Sep 30, 2017 1:42 pm

Re: Raspberry Pi Midi-keyboard

Sat Sep 30, 2017 1:45 pm

Awesome project, I wanted to do this for a while, but I have no one to help me. So if you can make a more in depth guide of how to set up all of the software, it would make me very happy. Thanks.

mlepage
Posts: 89
Joined: Tue Jun 12, 2012 1:58 am

Re: Raspberry Pi Midi-keyboard

Sat Sep 30, 2017 4:28 pm

That is wild. Great job.

I've been pondering the idea of getting a cheap MIDI keyboard and using a Teensy (microcontroller board) to make a digital software synth with its analog outputs. Some day...

Ancher
Posts: 6
Joined: Sat Nov 15, 2014 10:41 am

Re: Raspberry Pi Midi-keyboard

Tue Oct 03, 2017 3:31 pm

Nerelod wrote:
Sat Sep 30, 2017 1:45 pm
Awesome project, I wanted to do this for a while, but I have no one to help me. So if you can make a more in depth guide of how to set up all of the software, it would make me very happy. Thanks.
Thanks! I've edited the description a little and added the source code. Hopefully it helps you. :)

Ancher
Posts: 6
Joined: Sat Nov 15, 2014 10:41 am

Re: Raspberry Pi Midi-keyboard

Tue Oct 03, 2017 3:32 pm

mlepage wrote:
Sat Sep 30, 2017 4:28 pm
That is wild. Great job.

I've been pondering the idea of getting a cheap MIDI keyboard and using a Teensy (microcontroller board) to make a digital software synth with its analog outputs. Some day...
Thanks! That sounds like a cool project. Let me know how it turns out. :)

Nerelod
Posts: 10
Joined: Sat Sep 30, 2017 1:42 pm

Re: Raspberry Pi Midi-keyboard

Thu Oct 05, 2017 9:23 pm

Awesome, Im going to start working on my version now, Great project!

Nerelod
Posts: 10
Joined: Sat Sep 30, 2017 1:42 pm

Re: Raspberry Pi Midi-keyboard

Thu Oct 05, 2017 10:16 pm

Sorry but im a new and my teacher is not good at this and I got this error:
Traceback (most recent call last):
File "/home/pi/Desktop/MusicKey/Raspberry-MidiKeyboard/midikeyboard.py", line 23 in <module>
midiout = pg.midi.Output(3)
File "/usr/lib/python2.7/dist-packages/pygame/midi.py", line 414, in __init__
raise MidiException("Device id invalid, out of range.")
MidiException: 'Device id invalid, out of range.'

Ancher
Posts: 6
Joined: Sat Nov 15, 2014 10:41 am

Re: Raspberry Pi Midi-keyboard

Sat Oct 07, 2017 8:49 am

Nerelod wrote:
Thu Oct 05, 2017 10:16 pm
Sorry but im a new and my teacher is not good at this and I got this error:
Traceback (most recent call last):
File "/home/pi/Desktop/MusicKey/Raspberry-MidiKeyboard/midikeyboard.py", line 23 in <module>
midiout = pg.midi.Output(3)
File "/usr/lib/python2.7/dist-packages/pygame/midi.py", line 414, in __init__
raise MidiException("Device id invalid, out of range.")
MidiException: 'Device id invalid, out of range.'
The port number needs to be the same as it is in ALSA configuration, I think.

Nerelod
Posts: 10
Joined: Sat Sep 30, 2017 1:42 pm

Re: Raspberry Pi Midi-keyboard

Sat Oct 07, 2017 1:12 pm

Ancher wrote:
Sat Oct 07, 2017 8:49 am
Nerelod wrote:
Thu Oct 05, 2017 10:16 pm
Sorry but im a new and my teacher is not good at this and I got this error:
Traceback (most recent call last):
File "/home/pi/Desktop/MusicKey/Raspberry-MidiKeyboard/midikeyboard.py", line 23 in <module>
midiout = pg.midi.Output(3)
File "/usr/lib/python2.7/dist-packages/pygame/midi.py", line 414, in __init__
raise MidiException("Device id invalid, out of range.")
MidiException: 'Device id invalid, out of range.'
The port number needs to be the same as it is in ALSA configuration, I think.
I will spend the rest of my evening working on this, I am determined to finish this project. So anymore details are very much welcome, im new to linux. Thanks! :D

Return to “Graphics, sound and multimedia”

Who is online

Users browsing this forum: No registered users and 11 guests