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

#include "mbed.h"
#include "USBMIDI.h"

USBMIDI midi;

//Bus of buttons
BusInOut buttons(p21, p22, p23, p24, p25, p26, p29);

int main() {
    uint8_t p_bus = 0;
    uint8_t bus = 0;
    uint8_t add;
    while (1) {
        //if buttons state changes, send a MIDI message
        bus = buttons.read();
        if (bus != p_bus) {
            p_bus = bus;
            if (p_bus) {
                for (int i = 0; i < 7; i++) {
                    add = (p_bus & (1 << i)) ? (i + 1) : 0;
                    if (add) {
                        midi.write(MIDIMessage::NoteOn(48 + add));
                        midi.write(MIDIMessage::NoteOff(48 + add));
                    }
                }
            }
        }
        wait(0.001);
    }
}

Import program

00001 #include "mbed.h"
00002 #include "USBMIDI.h"
00003 
00004 USBMIDI midi;
00005 
00006 //Bus of buttons
00007 BusInOut buttons(p21, p22, p23, p24, p25, p26, p29);
00008 
00009 int main() {
00010     uint8_t p_bus = 0;
00011     uint8_t bus = 0;
00012     uint8_t add;
00013     while (1) {
00014         //if buttons state changes, send a MIDI message
00015         bus = buttons.read();
00016         if (bus != p_bus) {
00017             p_bus = bus;
00018             if (p_bus) {
00019                 for (int i = 0; i < 7; i++) {
00020                     add = (p_bus & (1 << i)) ? (i + 1) : 0;
00021                     if (add) {
00022                         midi.write(MIDIMessage::NoteOn(48 + add));
00023                         midi.write(MIDIMessage::NoteOff(48 + add));
00024                     }
00025                 }
00026             }
00027         }
00028         wait(0.001);
00029     }
00030 }

USBMouse
USBKeyboard
USBHID
USBMouseKeyboard


All wikipages