Arduino Forum

Using Arduino => Audio => Topic started by: KingOfSka on Jun 04, 2012, 12:51 pm

Title: Leonardo direct MIDI USB
Post by: KingOfSka on Jun 04, 2012, 12:51 pm
Hi all!
I was using the hacked FTDI drivers / roland serial MIDI drivers to have old arduino (duemilanove) show up as MIDI port, and send/receive data directly from ableton withouth having further converters,
i saw that now Leonardo has been released! Would it be possible to use this board to have Arduino <=> Ableton Live communicate directly via MIDI over USB or i would need additional software converters ?
Thanks in advance!
Title: Re: Leonardo direct MIDI USB
Post by: yancheelo on Jun 07, 2012, 07:35 am
Hi!

yes it is possible, and I am very interested in this topic also. Please read: http://hifiduino.wordpress.com/2011/09/30/arduino-leonardo/, you can see that the new Arduino supports USB-HID class (HID=Human Interface Device), and in this class cathegory you will not need any driver on your computer, it will use the default one for MIDI.

The big step ahead of leonardo is that the USB interface-protocol is fully programmable, hence your Leonardo can act as USB mouse, keyboard, MIDI, as well as USB storage or USB-serial converter.
The big problems is that you need to have a USB protocolstack running on Leonardo, and for me it is not yet clear how to do it. I have had a look at the following stacks:
- LUFA
- AVR USB Stack
- Teensy
- FreakUSB

but I wanted something where the source code is simple and clear to understand (I want to learn some USB internals). I am looking into this direction...

Best,
Y.
Title: Re: Leonardo direct MIDI USB
Post by: KingOfSka on Jun 12, 2012, 11:21 am
thanks a lot so it was like i was thinking, it could be done but no one has already come out with an easy to understand and working tutorial :)
I will try to learn something from the stacks you have mentioned, and report here any news! thanks again and good luck !
Title: Re: Leonardo direct MIDI USB
Post by: yancheelo on Jun 19, 2012, 09:16 am
Another interesting software is V-USB (http://www.obdev.at/products/vusb/index.html), which provides an USB stack and also an emulation of the USB physical layer using the old Arduino I/O ports.
Maybe the code can be changed so that instead of using the I/O ports, we configure directly the USB HW interface of Leonardo, in order to let Leonoardo to take care of the physical layer.

I am sorry if something is not precise, there is big confusion/lack of docs/explained code for the new USB capability of leonardo...

Y.
Title: Re: Leonardo direct MIDI USB
Post by: ideofon on Jun 20, 2012, 09:07 pm
The ones I would put my hope to are:

Moco: http://morecatlab.akiba.coocan.jp/lab/index.php/aruino/midi-firmware-for-arduino-uno-moco/?lang=en (http://morecatlab.akiba.coocan.jp/lab/index.php/aruino/midi-firmware-for-arduino-uno-moco/?lang=en)
and 
HIDUINO: http://dimitridiakopoulos.com/hiduino (http://dimitridiakopoulos.com/hiduino) github: https://github.com/ddiakopoulos/hiduino (https://github.com/ddiakopoulos/hiduino)

They are both based on LUFA.

.kris
Title: Re: Leonardo direct MIDI USB
Post by: OrC886 on Aug 07, 2012, 02:28 am
from morecatlab:
Quote
Moco firmware can use as USB-MIDI <-> USB-Serial bridge with a 8u2 or 32u4 board, like Adafruit's 32u4 breakout board.


and:
Quote
The Arduino Leonardo is a microcontroller board based on the ATmega32u4



I tried the MocoLUFA on an Arduino UNO R1 and it worked well
While R3 has the Atmega16U2 chip, the Leonardo seems like the best solution for me.
I'll try in a few days and post my results.
Title: Re: Leonardo direct MIDI USB
Post by: mothra78 on Aug 10, 2012, 12:56 pm
hi, I am also looking into using the leonardo to be a midi input for my ipad or a midi output using a custom drum kit.
So far no luck to get the usb midi code for the leonardo, any help would be appreciated.

Thanks
Title: Re: Leonardo direct MIDI USB
Post by: OrC886 on Aug 22, 2012, 01:16 am
No luck over here as well...Flip doesn't recognize the 32u4 for some reason  :~
I'm quite disappointed with the Leonardo, and would love to have my 8u2 uno back on stores.
Title: Re: Leonardo direct MIDI USB
Post by: OrC886 on Aug 22, 2012, 01:19 am
BTW you could always use this: http://projectgus.github.com/hairless-midiserial/#downloads

What's nice about it, is that its both for Windows and for mac, so you could develop your projects on windows, but run them on mac too.

And for now, Teensy has an onboard MIDI driverm which makes it better than the Leo. for this usage.
Title: Re: Leonardo direct MIDI USB
Post by: mothra78 on Aug 22, 2012, 09:56 pm
thanks for the hint I will try with a teensy 2.0
Title: Re: Leonardo direct MIDI USB
Post by: rkistner on Mar 30, 2013, 09:23 pm
Unfortunately, to add support for MIDI you have to hack in the Arduino core files. I've hacked in a MIDI-USB implementation in an alternative set of core files, which is available at https://github.com/rkistner/arcore (https://github.com/rkistner/arcore). The MIDI-USB protocol is fairly simple (once you get around the USB interface descriptors), and the implementation is quite similar to CDC (I based my implementation off that).

In the long run it might be better if we could use an existing stack with MIDI support, but it seemed complicated to replace Arduino's USB stack with another one.
Title: Re: Leonardo direct MIDI USB
Post by: OrC886 on Mar 31, 2013, 12:13 pm
Looks interesting
Might try it sometime
Title: Re: Leonardo direct MIDI USB
Post by: masavee on May 29, 2013, 02:17 pm
interesting but i ask now,i know can do but how (im newbie)
leonardo or uno who have better thats ?
i need 32 button interface hid interface
8 analog inbut and 16 encoder inbut.
how make hid interface to have all thats ?
ino to in arduino, what more need ?
i understand leonardo have hid ready board no need change bootloader, and uno no have hid compatiple need bootloader new write.

and hid device can adding 32 button, 16 encoder,8 analog inbut.
but how :(
i need only buttons and encoders my computer.
analog can take old joystick.mean usb board if need,
Title: Re: Leonardo direct MIDI USB
Post by: OrC886 on May 30, 2013, 09:07 am
This thread is about MIDI
Please don't add unrelevant information
Title: Re: Leonardo direct MIDI USB
Post by: boguz on Jun 10, 2013, 07:47 am
This is interesting topic.

i have made a midi controller with Uno R3 about one year ago (http://popo.webatu.com/) and now i'm starting to think about building some kind of midi Foot Controller.
I don't know much about electronics or programming and i had A LOT of help here from the forum to complete this last project, but it was also a great way to learn by doing.

The thing is that needing a serial to midi converter is a little bit annoying. It works, but there are some inconveniences (like for instance, not everyone has Processing and a Serial do Medi Converter Sketch).

So, when i heard that Leonardo had native ubs hid i was excited about it.

But now when i have read your thread i was a little bit less enthusiastic.
Is it not so easy to use the Leonardo to create a "Plug and Play" device?
If i would wrote a sketch that works on Uno R3 would it also work on a Arduino Leonardo?

My idea is to create a foot controller with maybe 8+2 foot switches, maybe 4 "banks" and some LEDs to use with softwares like Ableton.
Would you say that Leonardo would be better than Uno R3?

Thank you!
Title: Re: Leonardo direct MIDI USB
Post by: OrC886 on Jun 10, 2013, 08:05 am
I think teensy has a board with MIDI driver embedded
Title: Re: Leonardo direct MIDI USB
Post by: pharaohamps on Sep 02, 2013, 04:22 pm

Unfortunately, to add support for MIDI you have to hack in the Arduino core files. I've hacked in a MIDI-USB implementation in an alternative set of core files, which is available at https://github.com/rkistner/arcore (https://github.com/rkistner/arcore). The MIDI-USB protocol is fairly simple (once you get around the USB interface descriptors), and the implementation is quite similar to CDC (I based my implementation off that).

In the long run it might be better if we could use an existing stack with MIDI support, but it seemed complicated to replace Arduino's USB stack with another one.


I've been working with arcore for a few days and so far it seems to be working nicely.

Here's an example sketch that lets you send MIDI from the host (PC or Mac) and the Arduino will mirror that back out to an external device.  The nice thing about using arcore is that it's all in the application space, the bootloader can be anything you like.  Arcore does eat some application space, whereas most of the LUFA bootloaders fit in the same sized booloader partition and don't encroach.

Sketch:

Code: [Select]
// USB MIDI -> USB Serial Bridge
// Disaster Area Designs LLC
// http://www.disasterareadesigns.com
// Using rkistner's arcore modifications
// https://github.com/rkistner/arcore

void setup() {

  // Initialize UART serial port, pins 0 and 1
  // at correct baud rate for MIDI transmission
  Serial1.begin(31250);

}

void loop() {
 
  // USB MIDI THRU
 
    while(MIDIUSB.available() > 0) {
   
    // USB MIDI core interprets USB MIDI and passes out MIDIEvents when a completed message is received
     
    MIDIEvent e;
    e = MIDIUSB.read();
   
    // Flush out queue to make sure we don't hang on to incomplete messages
    MIDIUSB.flush();
   
    // MIDIEvents are "structs," with 4 members:
    // type, m1, m2, m3.
    // All MIDI messages consist of at least one byte.
    // We need to pass the first byte out to the serial port.
   
    Serial1.write(e.m1);
   
    // Not all MIDI messages have all 3 data bytes
    // So we use the contents of the MIDIEvent to test and make
    // sure we only send out the required number of bytes.
       
    switch (e.type) {
     
      // single-byte messages
      case 0x5:
      case 0xF:
        break;
     
      // two-byte messages
      case 0x2:
      case 0x6:
      case 0xC:
      case 0xD:
        Serial1.write(e.m2);
        break;
     
      // all other valid messages are three-byte
      default:
        Serial1.write(e.m2);
        Serial1.write(e.m3);
        break;
    }
   
  }
 
}


If you wanted to use the UART on the Arduino to send MIDI back to the host, you'd have to test based on the message type and pre-pend the USB message type.

A USB MIDI event is a little more complicated than the old-style serial MIDI message.  It is a 32-bit message, with any unused bits padded with zeroes.  Serial MIDI messages are of varying length, which kind of stinks if you're trying to read them in.

The first byte of USB MIDI consists of a 4 bit "cable number," and a 4-byte "code index number."  The cable number is similar to the MIDI channel, except that it indicates the virtual MIDI interface that's part of the USB device.  The arcore MIDI device has a cable number of 0x00.

The code index number is next, and it indicates the message type.

Generally these are the same as the first bits of the serial message codes, for example MIDI CC is code index 0xB, and the serial message is 0xBn, where "n" is the MIDI channel.  The code index numbers also indicate how many bytes are present in the message.  Anything after the indicated number of bytes can be safely discarded, since they're padding rather than real data.

arcore packs this code index number into the "type" member of the MIDIEvent struct.  You can access it at any time by reading in an available MIDIEvent and then using the member.  See the code above for an example.

The next three bytes are the MIDI message.  Not all three bytes will always be needed, but passing zeroes to a connected device that doesn't expect them will mess things up so we need to not send them.

Interestingly, you can still send messages on any of the 16 MIDI channels per cable over USB.  arcore just uses the one cable, but in theory you could implement more cables for more interfaces if you needed them.
Title: Re: Leonardo direct MIDI USB
Post by: OrC886 on Sep 02, 2013, 04:37 pm
I'm not sure this is related to this post

Can it identify itself as a MIDI device to a PC?
Title: Re: Leonardo direct MIDI USB
Post by: NicoHood on Apr 06, 2015, 04:42 pm
Now available for testing:
https://github.com/NicoHood/HID/tree/Midi-Test

Only works with 32u4 (leonardo/micro) and is activated by default. Select one of the USB-Cores, but no HID will work.
I really need feedback about this, I just made it recognize by the PC but had no chance to test it yet.

Example:
https://github.com/rkistner/arcore/blob/master/examples/MidiEcho/MidiEcho.ino