Recent changes
Slingshot user guide
tag Guide, user
NFCLamp user guide
tag Guide, user
Homepage
MPL115A2
Compiler Error 42
From the mbed microcontroller Cookbook.  

USBMIDI

A library for sending and receiving MIDI messages over USB using the standard USB-MIDI protocol.

Using this library, you can do things like send MIDI messages to a computer (such as to record in a sequencer, or trigger a software synthesiser) and receive messages from a computer (such as actuate things based on MIDI events)

Hello World

» Import this program

00001 // Hello World example for the USBMIDI library
00002 
00003 #include "mbed.h"
00004 #include "USBMIDI.h"
00005 
00006 void show_message(MIDIMessage msg) {
00007     switch (msg.type()) {
00008         case MIDIMessage::NoteOnType:
00009             printf("NoteOn key:%d, velocity: %d, channel: %d\n", msg.key(), msg.velocity(), msg.channel());
00010             break;
00011         case MIDIMessage::NoteOffType:
00012             printf("NoteOff key:%d, velocity: %d, channel: %d\n", msg.key(), msg.velocity(), msg.channel());
00013             break;
00014         case MIDIMessage::ControlChangeType:    
00015             printf("ControlChange controller: %d, data: %d\n", msg.controller(), msg.value());
00016             break;
00017         case MIDIMessage::PitchWheelType:
00018             printf("PitchWheel channel: %d, pitch: %d\n", msg.channel(), msg.pitch());
00019             break;
00020         default:
00021             printf("Another message\n");
00022     }    
00023 }
00024 
00025 USBMIDI midi;
00026 
00027 int main() {          
00028     midi.attach(show_message);         // call back for messages received    
00029     while (1) {    
00030         for(int i=48; i<83; i++) {     // send some messages!
00031             midi.write(MIDIMessage::NoteOn(i));
00032             wait(0.25);
00033             midi.write(MIDIMessage::NoteOff(i));
00034             wait(0.5);
00035         }
00036     }
00037 }

Connect up the target USB pins to a computer (D+, D-, GND); running the program should make a USB MIDI (Audio class) interface appear, and send/receive keys.

In this video, the mbed is sending ascending key presses over USB to the Mac, which can be seen on the keyboard higlighting in green and heard triggering garageband. Also, when you hit hit the keyboard, it sends a MIDI message to the mbed, which in the program just puts a message on the terminal (but could do anything)

Library

» Import this library into a program

Public Member Functions

USBMIDI ()
Create the USBMIDI interface.
void write ( MIDIMessage m)
Send a MIDIMessage .
bool writeable ()
Check if it possible to send a MIDIMessage .
void attach (void(*fptr)( MIDIMessage ))
Attach a callback for when a MIDIEvent is received.

Example

An example, using MIDI messages to trigger some motors as a simple drum sequencer

» Import this program

00001 // Drums via MIDI!
00002 #include "mbed.h"
00003 #include "USBMIDI.h"
00004 
00005 DigitalOut drum[] = {p5, p6, p7, p8}; // the four outputs
00006 
00007 void drums(int id, bool on) {
00008     if(id >= 0 && id <= 3) {
00009         drum[id] = on;
00010     }
00011 }
00012 
00013 void do_message(MIDIMessage msg) {
00014     switch (msg.type()) {
00015         case MIDIMessage::NoteOnType:
00016             drums(msg.key() - 64, msg.velocity() != 0);
00017             break;
00018         case MIDIMessage::NoteOffType:
00019             drums(msg.key() - 64, 0);
00020             break;
00021     }    
00022 }
00023 
00024 USBMIDI midi;
00025 
00026 int main() {          
00027     midi.attach(do_message);         // call back for messages received    
00028     while (1);
00029 }



calendar Page history
Last modified 16 Mar 2011, by   user Simon Ford   tag MIDI, USB, USBMIDI | 6 comments  

6 comments on USBMIDI:

22 Feb 2011

Amazing!! :)

23 Feb 2011

reposted

05 Apr 2011

reposted in Forum

06 Dec 2011

i can't get the mbed to recieve midi events.

16 Jan 2012

is it possible to change the name that the USBMidi device appears on windows ?

16 Jan 2012

Yes you can change the name on this method:

Code

uint8_t * USBMIDI::stringIproductDesc() {
    static uint8_t stringIproductDescriptor[] = {
        0x16,                                                       //bLength
        STRING_DESCRIPTOR,                                          //bDescriptorType 0x03
        'M',0,'b',0,'e',0,'d',0,' ',0,'A',0,'u',0,'d',0,'i',0,'o',0 //bString iProduct - Mbed Audio
    };
    return stringIproductDescriptor;
}

You can change the third line ('M', 0,...) with your string and don't forget to change the length (bLength) of the descriptor

Please login to post comments.