| Year | 2010 |

I had the chance to be part of the Openframeworks lab during the Decode events weekend at the V&A museum. It was an absolutely great weekend and I had the opportunity to meet really nice and creative people.
I was in the drawing group, and I created a little MIDI-driven drawing tool during the lab. I think MIDI devices are really interesting since open lots of interface possibilities: Keyboards, mixing tables and a wide range of sophisticated controllers that provide an analogue touch to Human-Computer Interaction: when thinking of live audiovisual performances, MIDI devices are a very natural way to control things.
In this post I’ll be sharing how to set up OpenFramworks to work with MIDI devices. I assume you have OF already running in your machine, so that’s what you need once OF is up and running:
* The ofxMidiIn add-on that can be found here
* A MIDI device: I have used a M-Audio X-session Pro
Download the ofxMidiIn add-on, decompress the ZIP, and copy the ofxMidi folder into the OF addons folder. I would suggest you make sure that the name is “ofxMidi” (without the version number such as “_002″).
Using the ofxMidi in your project

You need basically to do two things in order to have ofxMidi running within your OF project:
* Add the ofxMidi add-on folder into your project.
* Include the core MIDI framework: this is the OS specific MIDI framework. In Xcode you just right-click on the project’s libs folder and choose add->Existing frameworks (see picture below) and select the core.MIDI.framework folder.
If you are in Windows or Linux, try to look at the OF forum for more information.

In your testApp.h you’ll need to include the ofxMidi library and declare a listener function and an ofxMidiIn object:
#ifndef _TEST_APP
#define _TEST_APP
#include "ofMain.h"
#include "ofxMidi.h"
class testApp : public ofBaseApp{
public:
void setup();
void update();
void draw();
void keyPressed (int key);
void keyReleased(int key);
void mouseMoved(int x, int y );
void mouseDragged(int x, int y, int button);
void mousePressed(int x, int y, int button);
void mouseReleased(int x, int y, int button);
void windowResized(int w, int h);
// this is your listener function
void newMessage(ofxMidiEventArgs &args);
// this is your ofxMidiIn object
ofxMidiIn midiIn;
};
#endif
In your testApp.cpp file you need to tell your ofxMidiIn object to open a connection with your device using a certain port and register your listener function. You would usually do that in your setup method.
In most of the cases you’ll be using only the port 0 (default). My guess is that if you have different devices plugged in your machine you can handle all of them since they will stack in your system (starting at port 0). I am just guessing and I might be quite wrong!
void testApp::setup(){
midiIn.openPort(); // opens a connection with the device at port 0 (default)
ofAddListener(midiIn.newMessageEvent, this, &testApp::newMessage);
}
Here an example of the listener function just printing messages in the console:
void testApp::newMessage(ofxMidiEventArgs &args){
cout << "MIDI message [port: " << args.port << ", channel: " << args.channel << ", status: " << args.status << ", byteOne: " << args.byteOne << ", byteTwo: " << args.byteTwo << ", timestamp: " << args.timestamp << "]" << endl;
}
The MIDI message structure (the ofxMidiEventArgs Object)
A MIDI message has the following parameters:
* port: The port where the MIDI message is coming from (integer)
* channel: I guess a MIDI channel (integer)
* status: not sure what this is (integer)
* byteOne: this is the control id. Every knob, button or slider have their own id represented by an integer number.
* byteTwo: the current value of the control. This is an integer number from 0 to 127
* timestamp: I guess is the timestamp of the current message but not really sure. Maybe is related to the elapsed time since the port was opened. (double)
So basically I have been only using two parameters: byteOne for identifying the control being touched, and byteTwo to grab the actual value of the control. Quite simple!
You can download the source of the “printing MIDI messages example” (includes Xcode project file).
In my next post, I will explain the drawing tool and will share the source as well.
Was a really great weekend! This morning I felt the post-lab depression
By the way, thanks a lot to Arturo for his help on this.
| Posted on: | 02 / March / 2010 – 00:03:17 |
| Filed under: | Lab |
| Tagged width: | midi, openframeworks |
thanks for sharing it!
i had to rebuild the xcode project because of the OS version,
but very useful and well explained
thanks again
really cool.
this has helped me get ofxMidi working and has not always been easy
made a couple of changes to also do simple MidiOut
added:
ofxMidiOut midiOut; //in header
and
midiOut.listPorts();
midiOut.openPort(3);//It seems openPort needs a number on my machine
//might be best to be explicit on this anyhow – my guess is default is zero
then void testApp::mousePressed(int x, int y, int button){
midiOut.sendNoteOn(1, 60,100);
}
or whatever.
So thanks very much. Hope this note might help others setting it up.
Andrew
hey! I was at the lab too (I’m enrico from the human pong thing
). it’s really been an amazing weekend.
I’m working with midi these days and this blog entry really saved my day! thanks!
[...] my previous post I described how to set up MIDI devices to work in your OpenFrameworks projects. In this post [...]
Thanks for this tutorial!! I was loocking something about midi in Of and now my problem is solved…
Best!!