You are viewing an older revision! See the latest version
USBMIDI
Beta only!
This library is in beta, and only works with the betamode compiler and the beta libraries.
To use these, ensure you have enabled /betamode for the compiler, and that you import these examples as the basis for your experiments to ensure the beta mbed library is pulled in.
The USBMIDI interface can be used to send and receive 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)
USB pins are available on p31 (D+) and p32 (D-)
Hello World¶
// Hello World example for the USBMIDI library #include "mbed.h" #include "USBMIDI.h" USBMIDI midi; int main() { while (1) { for(int i=48; i<83; i++) { // send some messages! midi.write(MIDIMessage::NoteOn(i)); wait(0.25); midi.write(MIDIMessage::NoteOff(i)); wait(0.5); } } }
Import programUSBMIDI_HelloWorld
USBMIDI Hello World
API¶
[Not converted]
More example¶
In this example, you can control the MIDI message sent with buttons
// Hello World example for the USBMIDI library #include "mbed.h" #include "USBMIDI.h" //USBMIDI object USBMIDI midi; // Leds which will be switch on or off according to a MIDImessage BusOut leds(LED1, LED2, LED3, LED4); BusInOut buttons(p22, p23, p24, p25); void show_message(MIDIMessage msg) { switch (msg.type()) { case MIDIMessage::NoteOnType: switch (msg.key()) { case 48: leds = (1 << 0); break; case 49: leds = (1 << 1); break; case 50: leds = (1 << 2); break; case 51: leds = (1 << 3); break; } break; case MIDIMessage::NoteOffType: default: leds = 0; } } int main() { uint8_t bus = 0; uint8_t p_bus = 0; // call back for messages received midi.attach(show_message); while (1) { //if buttons state changes, send a MIDI message bus = buttons.read(); for (int i = 0; i < 4; i++) { if ( (bus & (1 << i)) != (p_bus & (1 << i))) { if (bus & (1 << i)) { midi.write(MIDIMessage::NoteOn(48 + i)); } else if ( !(bus & (1 << i)) ) { midi.write(MIDIMessage::NoteOff(48 + i)); } } } wait(0.001); p_bus = bus; } }
Import programUSBMIDI_Receive
USBMIDI example