Midignusbuino

From SGMK-SSAM-WIKI
Revision as of 16:38, 17 December 2012 by Mirdej (talk | contribs) (→‎Examples)
Jump to navigation Jump to search

The Midignusbuino is a plug-n-play USB-MIDI device that can be used to build MIDI controllers. Most operating systems have built in MIDI support, so no drivers are needed, and almost any music software understands MIDI… It is built on the Gnusbuino platform, is more or less compatible with the Arduino environment and can be programmed through the Arduino IDE

Building / Installing

Hardware schematics and source code are in the SV repository at http://gnusb.svn.sourceforge.net/viewvc/gnusb/branches/gnusbuino/

You can either download the two folders and add their contents to the corresponding folders in your Arduino working directory, or you can pull the latest version directly through svn.

- cd "the/path/to/Arduino/"    (for me on a Mac it is cd ~/Users/me/Documents/Arduino)
- svn co https://gnusb.svn.sourceforge.net/svnroot/gnusb/branches/gnusbuino/hardware
- svn co https://gnusb.svn.sourceforge.net/svnroot/gnusb/branches/gnusbuino/libraries

later on, you can always update to the latest version by typing

- svn update

Some notes on etching and building the circuit

PCB layout and Eagle files are in harware/gnusbuino/pcb/

  • USB resistors should be 68 Ohms and 1k6 Ohms, the other resistor values are not critical
  • Zeners should be 3.3-3.6V
  • Electrolytic capacitor max 10uF (as per USB spec)

Burning the bootloader

The Gnusbuino board does not have a SPI interface connector (as you'd only need it once for burning the bootloader - afterwards you can program it directly over USB). So it is a good idea to burn the bootloader BEFORE you solder the chip on. Reopen the Arduino Application and you should see the Midignusbuino in the board menu:

Use your favorite AVR programmer and chose "Burn Bootloader" from the Tools menu. I normally use a Gnusb-Prog, a modified gnusb to flash AVR chips, see here: http://www.anyma.ch/blogs/research/2011/08/19/gnusb-procreation/

Programming the Midignusbuino

Plug in the Midignusbuino and press the reset button. The yellow LED lights up, indicating that we are in bootloader mode. When the Gnusbuino is in bootloader mode, it will call itself "USBasp" in the USB devices list. In fact, the bootloader mimics the popular USBasp AVR programmer, but it does not program another chip but itself… Because of this, you don't have to chose a serial interface in the Tools menu

Write your Arduino sketch and hit upload as with any normal Arduino. When the upload is finished, both LEDs light up for a short time, then the yellow led goes out and the green one stays on. This means the Gnusbuino has successfully enumerated as a standard MIDI-USB device and is ready to use. On the Mac, you can use the "System Profiler" application (in /Application/Utilities/ ) to check if the gnusb is really there:

The MIDI library

The Midignusbuino comes with a small library (aptly called MIDI) to help building MIDI controllers and interfaces.

Please note that this library will not work on other Arduinos - it s tightly integrated with the Gnusbuino core to communicate through the V-USB virtual USB driver (though with some tweaks it could maybe be made to allow for standard old-school MIDI on DIN jacks through the serial interface on Arduinos…)

You have to include this library at the top of your sketch to be able to use it

- #include "MIDI.h"

Main functions of the MIDI class (library)

MIDI.write

MIDI.write(command,key,value);

Sends a MIDI Message back to the host computer. Does not return anything.

MIDI.read

result = MIDI.read(&message)

Receives MIDI data from host computer and puts it into the variable MidiMessage.

Returns 1 if there is new data or 0 if no MIDI data is present.

Note that you need to declare a global variable (called message in this example) of type MIDIMessage and pass a pointer to this variable to the MIDI.read() function. That's why there is an ampersand character (&) in front of "message" in the function call.

MIDIMessage message
…
if (MIDI.read(&message)) {
     do something with message
}

The MIDIMessage type has three fields that can accessed this way:

message.command           -> the MIDI message type, e.g. NOTE ON or CONTROL CHANGE
message.key               -> first byte of data, usually the key number pressed (pitch) or the controller number
message.value             -> the velocity of NOTE ON messages, or the controller value

Constants

The MIDI library defines the following constants to help in creating / sorting out MIDI messages:

MIDI messages

Constant Data Description
MIDI_NOTEOFF pitch, velocity Note Off - a key has been released. Please note that many MIDI implementations actually send a Note On message with a velocity of 0 instead of Note off
MIDI_NOTEON pitch, velocity A key has been pressed.
MIDI_POLYAFTERTOUCH pitch, pressure This message is most often sent by pressing down on the key after it "bottoms out"
MIDI_CONTROLCHANGE controller number, value This message is sent when a controller value changes. Controllers include devices such as pedals and levers.
MIDI_PROGRAMCHANGE program number Program Change. This message sent when the patch number changes
MIDI_CHANNELAFTERTOUCH pressure Channel Pressure (After-touch). This message is most often sent by pressing down on the key after it "bottoms out". This message is different from polyphonic after-touch. Use this message to send the single greatest pressure value (of all the current depressed keys)
MIDI_PITCHBEND LSB, MSB Pitch Wheel Change. This message is sent to indicate a change in the pitch wheel. The pitch wheel is measured by a fourteen bit value. Center (no pitch change) is 2000H. Sensitivity is a function of the transmitter. (LSB) are the least significant 7 bits. (MSB) are the most significant 7 bits.

additionally, the following MIDI Real Time messages are defined, but not thoroughly tested, yet: MIDI_QUARTERFRAME MIDI_SONGPOS , MIDI_SONGSELECT ,MIDI_TIMINGCLOCK , MIDI_START ,MIDI_CONTINUE ,MIDI_STOP


Note Names

Notes can both be specified as pitch numbers (60 equals a C4) or by using the note name prepended by an underscore

Constant Pitch value
_C-1 0
_Cis-1 1
_D-1 2
etc..
_C0 12
_Cis0 13
_D0 14
_Dis0 15
etc..
_F9 125
_Fis9 126
_G9 127

Examples

Example code for sending MIDI controller values from a potentiometer connected to the Midignusbuino:

/*---------------------------------------------------------------------------------------------

  Gnusbuino MIDI Library 2012 by Michael Egger
 
  SEND CONTROL CHANGE EXAMPLE
  Read a potentiometer and send its value as a continuous controller message  
  
  This example code is in the public domain.

--------------------------------------------------------------------------------------------- */
/* The circuit:
 * Potentiometer attached to analog input 0, center pin of the potentiometer to the analog pin
 * one side pin (either one) to ground,  the other side pin to +5V
 */

 
#include "MIDI.h"            // you have to include the Gnusbuino MIDI library


int sensorValue = 0;         // variable to store the value coming from the sensor
int sentValue = -1;          // we only want to send data when there's something changing
                             // so we have to keep track of the last value that was sent to the host

void setup() {               // nothing to do in setup, pins are inputs by default
}


void loop() {
  
  sensorValue = analogRead(A1) / 8;                       // analogRead returns 0-1023, we need 0-127
  if (sensorValue != sentValue) {                         // compare actual readout to last sent value    
       
      //MIDI.write(MIDI_CONTROLCHANGE, controller number , controller value )

        MIDI.write(MIDI_CONTROLCHANGE,1,sensorValue);     // put new control change message into MIDI sending queue
        sentValue = sensorValue;                          // store last sent value
  }
  
    delay(10);              // give some time for sending, otherwhise the MIDI queue could fill up
}


The library includes various more examples to help getting started with sending/receiving MIDI over USB. You can access these examples through the File->Examples menu: