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 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 library
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 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 }