USBMIDI

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)

The USB connector should be attached to

  • p31 (D+), p32 (D-) and GND for the LPC1768 and the LPC11U24
  • The on-board USB connector of the FRDM-KL25Z

Hello World

» Import this program

// 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);
        }
    }
}

API

» Import this library into a program

Public Member Functions

  USBMIDI (uint16_t vendor_id=0x0700, uint16_t product_id=0x0101, uint16_t product_release=0x0001)
  Constructor.
void  write ( MIDIMessage m)
  Send a MIDIMessage .
void  attach (void(*fptr)( MIDIMessage ))
  Attach a callback for when a MIDIEvent is received.

More example

In this example, you can control the MIDI message sent with buttons

» Import this program

// 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;
    }
}



1 related question:

1 comment:

21 Nov 2011

This looks really cool. I notice USBDevice is setup with classes for mouse, keyboard, serial, and MIDI. Is it possible to have a USB MIDI device and a USB Serial device (and maybe a mouse and keyboard :)) all appear as devices, all at the same time? That would be a really powerful capability.

Terry

Posting comments for this page has been disabled