Introduction: Programable MIDI Synth With Intel Edison and Csound

About: I'm an Electronics Engineer, specialized on Open Source, Open Standards and IoT. IoT and Open Source Developer Evangelist at Intel in Brazil.

A MIDI controller is like a computer's keyboard, but instead of sending characters to the computer, it send notes (and a lot of additional information about how the note is being played). You can learn more about MIDI Controllers here.

Usually, a MIDI controller is very inexpensive when compared to a complete synthesizer, but the MIDI controllers requires a PC (or MAC) and a lot of additional software to work. Just to give a comparison, the MIDI Synth that is build with this tutorial generates a timbre very similar to one of the timbres generated by the Roland Jupiter 8 Analog Synth, that is being sold for a price around US$ 12k (here).

If you don't have US$12k to invest right now to play the Axel Foley´s theme, but have a MIDI Controller, an USB audio card and an Intel Edison, you should try that tutorial (and yes, you can also play some Krafwerk, Van Halen and a lot of other cool stuff with it).

We will use Csound on this tutorial, and in a nutshell, you can generate anything you want with it. From Hammond Organs to Moog, your creativity and Csound skills are everything you need to explore this wonderful audio programming language.

On the end of this Instructable, there is a video with the Synth's demo.

Step 1: What We Need

We'll use an Intel Edison with the Arduino Breakout Board.

My MIDI Controller is a Behringer UMA25S, and it has an embedded Audio Card. You can use two separate devices to do the same.

We'll also need an Intel Edison power supply and in my case I needed an active USB Hub (a powered USB Hub), to power the MIDI Controller. The specs for the Intel Edison Breakout Board power supply can be found here.

I also used an external speaker (I love it loud) and headphones during the process.

Step 2: Setting Up the Intel Edison Board and Installing the Necessary Additional Packages

If this is your first contact with an Intel Edison board, please read the Getting Started Guide.

Connect your Edison board to your machine, following the steps found here.

Update your Intel Edison board firmware, downloading the necessary software from this page.

To proceed, make sure that your Edison board is connected to the Internet. To learn more about connecting the board to the Internet, please follow the "Step 3: Get your board online" here. To connect the board to an existing wifi connection, please follow the instructions available on this page.

You will also need to have your Edison board connected to an external power supply, and connected to your computer thought the debug port (the lower USB connector) or using SSH.

On the board's Linux console, we will need to configure an additional packages repository. This repository is not supported by Intel (so use it at your own risk).

To add the repository, edit the file base-feeds.conf at /etc/opkg/ (eg. #vi /etc/opkg/base-feeds.conf) and paste in the following lines:

src/gz all   http://repo.opkg.net/edison/repo/all
src/gz edison   http://repo.opkg.net/edison/repo/edison
src/gz core2-32   http://repo.opkg.net/edison/repo/core2-32

After that, update your packages list with the command

#opkg update

Now we'll install all necessary packages to get everything up and running, with the command:

#opkg install alsa-server alsa-lib-dev alsa-dev alsa-utils-aconnect alsa-utils-speakertest alsa-utils-midi libsndfile-bin libsndfile-dev flex bison git

Step 3: Setting Up the Sound Adapter

To connect the MIDI controller to the USB host port on Edison board (type A connector), I used a powered USB hub, to provide enough power to the controller. To use this USB port on the board, you need to change the switch below it (SW1) to up (to the Type A connector side), as show on above image. Typically this switch is on the opposite direction, allowing to power the board and to load Arduino Sketches.

Check if your sound card and MIDI controller were recognized:

#cat /proc/asound/cards
0 [Loopback ]: Loopback - Loopback Loopback 1 1 [UMA25S ]: USB-Audio - UMA25S BEHRINGER UMA25S at usb-dwc3-host.2-1.4.1, full speed 2 [CODEC ]: USB-Audio - USB Audio CODEC Burr-Brown from TI USB Audio CODEC at usb-dwc3-host.2-1.4.2, full speed

The amidi tool will display more info about your MIDI controller.

#amidi -l

Dir Device Name IO hw:1,0,0 UMA25S MIDI 1

To check all your audio devices recognized by alsa, type:

#aplay -L
null Discard all samples (playback) or generate zero samples (capture) default:CARD=Loopback Loopback, Loopback PCM Default Audio Device sysdefault:CARD=Loopback Loopback, Loopback PCM Default Audio Device default:CARD=CODEC USB Audio CODEC, USB Audio Default Audio Device sysdefault:CARD=CODEC USB Audio CODEC, USB Audio Default Audio Device

To set the default alsa device to your sound card, first check the hw number of the card:

# cat /proc/asound/cards
0 [Loopback ]: Loopback - Loopback Loopback 1 1 [UMA25S ]: USB-Audio - UMA25S BEHRINGER UMA25S at usb-dwc3-host.2-1.4.1, full speed 2 [CODEC ]: USB-Audio - USB Audio CODEC Burr-Brown from TI USB Audio CODEC at usb-dwc3-host.2-1.4.2, full speed

In my case, the default is 2.

Create a file named .asoundrc on your home folder (/home/root) with the following content (replace X with our hardware id):

pcm.!default {
type hw card X }

ctl.!default { type hw card X }

At this point, you'll need to reload the alsa configuration to change the default audio adapter. To do that you can simply log off and log in or run the command:

#alsactl kill rescan

To adjust the output volume, use alsamixer.

#alsamixer

If everything is fine, alsamixer will open showing the volume bar for your sound card.

To test the sound output, use the speaker-test tool (you should hear a sine wave sound and stereo output):

#speaker-test -tsine -c2

To end the test, press CTRL+C.

Step 4: Download, Compile and Install CSound

The instructions to download, compile and install csound can be found here.

Follow the the second list of steps, (1-10) from the "Building Csound 6" section. One important thing is that on Step 7, add the -DALSA_HEADER=/usr/include parameter or it will not compile the alsa module. So your cmake command line should be like:

<p>cmake -DALSA_HEADER=/usr/include ../csound</p>

On steps 9 and 10, the sudo command is not needed (you are already logged in as root, right ?)

By default, libs will be copied to /usr/local/lib. You can add that to the path or simply copy the libs to the /usr/lib folder with the command:

<p>cp -r /usr/local/lib/* /usr/lib</p>

Step 5: Configure Csound

Run the sample GetDeviceList.csd available here, to get the hw id of your Audion sound card (BTW, this should be the same id we used before, but now it will be informed by Csound):

# csound GetDeviceList.csd

You will see some lines and finally, something like this:

*** ALSA: must specify a device name, not a number (e.g. -odac:hw:0,0)
"hw:0,0" - Loopback PCM : Loopback PCM : playback 8 : capture 8 "hw:0,1" - Loopback PCM : Loopback PCM : playback 8 : capture 8 "hw:2,0" - USB Audio : USB Audio : playback 1 : capture 1

In my board, the hw ID is 2.

Download the csound midi synth code available here to your board:

#wget http://raw.githubusercontent.com/homembit/MIDI_Edison/master/midisynthesizer.csd

Edit the configuration (<CsOptions> section at the beginning of the file) to something like:

<CsOptions>
-odac:hw:X -b256 -B1024 -Ma -+rtaudio=alsa -+rtmidi=alsa </CsOptions>

The important part here is the -odac:hw:X, were X is your sound card hw ID (in my case, -odac:hw:2).

This synth was written by Victor Lazzarini and published at the Csound mail list. Victor authorized me to redistribute the code, and I would like to thank him for the code and also for helping other people on forums. His answers and explanations about several things were fundamental for helping me to put all those pieces together. Muito obrigado, Victor! (yes, we are both Brazilians :)).

TIP: Save this config line to some file on your board, because you'll need it to run several other Csound programs that are available on the web.

Step 6: Enjoy Your Synth!

Finally, to execute our Csound program, simply type:

<p>#csound midisynthesizer.csd</p>

Enjoy your Synth !


References:
Intel Developer Zone - IoT

Alsa Project

Csound Project

Csound Community

Csound Repository

Csound Documentation