Archive

Archive for October, 2014

Project Mozart2: How to make a music synthesizer out of your Raspberry Pi and Atmega32

October 18, 2014 Leave a comment

This is a nice and simple project for all those who has their interest at the intersection of electronics, computers, and music. I will show how I made a musical keyboard. As the project uses Jack (Jack Audio Connection Kit) to finally create the Music, it will be very easy to extend this project for any kind of sound application that you can think of.

What you need are:

  1. an Atmega32 or any similar microcontroller that has got 12 or more GPIO pins and a UART.
  2. a Raspberry Pi with some form of Linux. (A linux PC with a serial port also will work!)
  3. 2 1k resistor to make the level shifter circuit
  4. Something that looks like a piano keyboard. (Synthesizer needs to know of the pitch you are interested in playing!). If you don’t have one, probably you have to make some kind switch matrix, with switches arranged in a line
Mozart_Overview

Top level design

Inside the Mega32

We use 12 GPIO pins of the Mega32 in a 4×8 arrangement to create a 32 key key-matrix, as shown in picture. The 4 lines on the left are configured to output and are activated sequentially one after the other. At any given time only one line will be active. The other 8 GPIOs of the controller are configured as input, and they will be active or inactive depending on what all keys of the keyboard are pressed. The firmware will keep repeating the pattern on the GPIO outputs, and keep reading the input pins repeatedly, and very fast. The firmware keeps on checking checking changes in keys from scan to scan. Once it detects a key press or a key release, it formats a 4byte frame containing bytes indicating

  1. frame start indicator byte
  2. Key press/release indicator byte
  3. the pitch indicator
  4. the amplitude/key velocity indicator (hard-coded to 64)

The firmware then sends the frame out to the Raspberry Pi through the UART port.

The key matrix

The key matrix as originally used looks like in picture

The switch matrix

The switch matrix

However this kind of switch matrix has a problem known as phantom-key/Ghost key. This is felt like if you press certain pairs of keys, the mega32 controller will think that two other keys(ghost keys) are also pressed.

You can go to http://www.dribin.org/dave/keyboard/one_html/ for a nice explanation, and a solution for this. The solution is to add one diode each in series with each of the switch.

UART and Level Shifter

We use only the Tx port of the Atmega32 and the Rx port of Raspberry Pi, with a baud rate of 115200. I was running the atmega32 at 5V. But the Raspberry Pi IOs are only 3.3V capable. So a level shifter was needed. If you can run your atmega off a 3.3V supply, you can avoid the level shifter. I just used a resistive divided(don’t remember the exact values — it doesn’t really matter). Something made of a 2k and 1k resistor should work, I think.

The Raspberry Pi

The ‘MIDI encoder’ is the only block to be implemented in the Raspberry Pi side. Rest everything is off the shelf. We first configure the UART port and then keep reading the data out of UART from file /dev/ttyAMA0. Once we get a full frame from mega23, we re-encode it to MIDI format as accepted by JACK. The software then calls a Jack API to transfer the MIDI data to Jack. Jack then transports it to synthesizer(I used Hexter). Synthesizer converts MIDI commands to PCM audio, and gives back to Jack. Jack sends it to the Linux Audio stack, and it gets converted to an analog signal at the audio jack. You can connect an active speaker/headphone to hear the music.

Here are a few pictures of my prototype

The key-matrix, the level shifter(on breadboard) and the mega32

The key-matrix, the level shifter(on breadboard) and the mega32

Level shifter, Mega32, and the Raspberry Pi

Level shifter, Mega32, and the Raspberry Pi

The code at raspberry side (including a code::blocks project file) is available at: https://github.com/nitrojacob/s2jmidi. The serial format is described in the README file.