Quick hack to make NSX-39 (Poke-Miku) USB MIDI device to speak "mbed" from mbed which acts as an USB host.

Dependencies:   FatFileSystem mbed

Fork of MIDI_BlueUSB by Radio Junk Box

Description of the project

This is quick hack to control Poke-miku (NSX-39) from mbed. The mbed acts as an USB host and controls USB MIDI device NSX-39. It speaks "mbed" if you send "s¥n" from virtual USB serial (connected to PC or Mac) or push SW connected to p21. It plays MIDI file "test.mid" on local file-system if you push SW connected to p22. You can find files that I have tested at the bottom. The standard MIDI file support is still preliminary. See TestShell.cpp for the hack. This program is derived from MIDI_BlueUSB (http://mbed.org/users/radiojunkbox/code/MIDI_BlueUSB/) by Radio Junk Box.

ポケミク(NSX-39)を無改造のままmbedから鳴らせるようにしてみました。mbedがUSB hostになって、USB MIDIデバイスのポケミクを鳴らします。mbedのバーチャルシリアル(USBシリアル)にPCからs\nを送るか、p21につないだスイッチを押すとmbedとしゃべります。p22につないだスイッチを押すと、ローカルファイルシステム(.binと同じ場所)に保存した test.mid を再生します。試したファイルは下にある test1.mid と test2.mid です。MIDIファイルのサポートはまだまだ完全とはいえません。

tested MIDI files

Video: Poke-miku speaks `mbed'

MidiUSB.cpp

Committer:
non
Date:
2014-04-29
Revision:
4:cd0d8ce967d8
Parent:
1:892f8922bdc4

File content as of revision 4:cd0d8ce967d8:


/*
Copyright (c) 2012 RadioJunkBox

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#include "mbed.h"
#include "USBHost.h"
#include "Utils.h"

#define MIDIEVT(_class,_subclass,_protocol) (((_class) << 16) | ((_subclass) << 8) | _protocol)
u8 ms_buf[255];  // MIDI Streaming Buffer

// Ring Buffer
#define         BUFSIZE             32      // size of ring buffer (ex 4,8,16,32...)
int             gPtr_buf_in, gPtr_buf_out;
unsigned char   gRxBuf[BUFSIZE];

// Serial MIDI Port
#define         MIDI_RATE           31250   // 31.25kbps
Serial          gMIDI(p9,p10);
Ticker          gTickerMIDI;

// Transmit to Serial MIDI Port
void TxSerialMidi()
{
    unsigned char rb;

    // ring buffer empty?
    if(gPtr_buf_in != gPtr_buf_out)
    {
        // get 1byte from ring buffer
        gPtr_buf_out++;
        gPtr_buf_out &= (BUFSIZE - 1);
        rb = gRxBuf[gPtr_buf_out];
        gMIDI.putc(rb);
    }      
}

// Intialize Serial MIDI Port  
void InitSerialMIDI()
{
    gPtr_buf_in = gPtr_buf_out = 0;
    gMIDI.baud(MIDI_RATE);
    gTickerMIDI.attach_us(&TxSerialMidi, 500);  // 500us
}

//  Received MIDI Steram
void MidiEventCallback(int device, int endpoint, int status, u8* data, int len, void* userData)
{
    int i;
    int ptr = 0;

    while((data[ptr] != 0) && (ptr < len))
    {        
        printf("MIDI %02X %02X %02X %02X", device, endpoint, status,len);
        printfBytes("",data,4);
        for( i = 1; i<4; i++)
        {
            gPtr_buf_in++;
            gPtr_buf_in &= (BUFSIZE - 1);
            gRxBuf[gPtr_buf_in] = data[ptr+i];
        }
        ptr+=4;
    }
    USBBulkTransfer(device,endpoint,data,len,MidiEventCallback,userData);
}

//  Add MIDI Interface
void AddMidiInterface(int device, InterfaceDescriptor* id, EndpointDescriptor* ed,int len)
{
    if ((ed->bmAttributes & 3) != ENDPOINT_BULK || !(ed->bEndpointAddress & 0x80))
        return;
    
    // Make bulk enpoints for MIDI devices
    u32 evt = MIDIEVT(id->bInterfaceClass,id->bInterfaceSubClass,id->bInterfaceProtocol);
    u8* dst = ms_buf;

    if(dst)
    {
        printf("Add Midi Interface %02X %08X\r\n",ed->bEndpointAddress,evt);
        USBBulkTransfer(device,ed->bEndpointAddress,dst,len,MidiEventCallback,(void*)evt);
    }
}

//  Detected Midi Device
int OnMidiInsert(int device)
{
    u8 buffer[255];
    int err = GetDescriptor(device,DESCRIPTOR_TYPE_CONFIGURATION,0,buffer,255);
    if (err < 0)
        return err;

    int len = buffer[2] | (buffer[3] << 8);
    u8* d = buffer;
    u8* end = d + len;

    printf("OnMidiInsert %02X %02X\r\n",device,len);

    while (d < end)
    {
        if (d[1] == DESCRIPTOR_TYPE_INTERFACE)
        {
            InterfaceDescriptor* id = (InterfaceDescriptor*)d;
            d += d[0];
            while (d < end)
            {
                    if (d[1] == DESCRIPTOR_TYPE_ENDPOINT)
                    {
                        printfBytes("MIDI Endpoint",d,d[0]);
                        AddMidiInterface(device,id,(EndpointDescriptor*)d,d[4]);
                    }
                    d += d[0];
            }
        }
        d += d[0];
    }
    return 0;
}

void MIDItxHandler() {
  /*  if (bulk_out) {
        if (bulk_out->getState() == USB_TYPE_IDLE) {
            tx.call();
        }
    } */
}