|
Intro to Physical Computing Syllabus Research & Learning Other Class pages
ITP Help Pages |
Controlling Garage Band with ArduinoOverviewThis tutorial shows you how to connect an Arduino making MIDI sounds to MIDI applications via a USB-to-MIDI interface. For this lab you'll need:
Click on any image for a larger view. Arduino + GarageBandHere we will control GarageBand from switches connected to the Arduino board. Connect switches to digital input on the Arduino like you did in the digital lab.
Connect power and ground on the breadboard to power and ground from the microcontroller. Connect Switches to digital input on the Arduino. Add the MIDI connector.
Program the ModuleLook over the code and read a description before you use it.
/*
* Convert Arduino to a MIDI controller using 6 digital inputs.
*
* This sketch is set up to send 6 MIDI notes on MIDI channel 1,
* but it can be easily reconfigured for other notes and channels
*
* Created 3 Nov 2008
* By Hyeki Min
*/
// define the pins we use
int switchPin1 = 8;
int switchPin2 = 9;
int switchPin3 = 10;
int switchPin4 = 11;
int switchPin5 = 12;
int switchPin6 = 13;
// general midi notes
char note1 = 60; //Middle C
char note2 = 62; //D
char note3 = 64; //E
char note4 = 65; //F
char note5 = 67; //G
char note6 = 69; //A
// Variables
int switchState1 = LOW;
int switchState2 = LOW;
int switchState3 = LOW;
int switchState4 = LOW;
int switchState5 = LOW;
int switchState6 = LOW;
int currentSwitchState1 = LOW;
int currentSwitchState2 = LOW;
int currentSwitchState3 = LOW;
int currentSwitchState4 = LOW;
int currentSwitchState5 = LOW;
int currentSwitchState6 = LOW;
void setup() {
// set the states of the I/O pins:
pinMode(switchPin1, INPUT);
pinMode(switchPin2, INPUT);
pinMode(switchPin3, INPUT);
pinMode(switchPin4, INPUT);
pinMode(switchPin5, INPUT);
pinMode(switchPin6, INPUT);
// set MIDI baud rate :
Serial.begin(31250);
}
void loop() {
//switchPin1
currentSwitchState1 = digitalRead(switchPin1);
if( currentSwitchState1 == LOW && switchState1 == HIGH ) // push
//Note on channel 1 (0x90), some note value (note), middle velocity (0x45):
noteOn(0x90, note1, 0x45);
if( currentSwitchState1 == HIGH && switchState1 == LOW ) // release
//Note on channel 1 (0x90), some note value (note), silent velocity (0x00):
noteOn(0x90, note1, 0x00);
switchState1 = currentSwitchState1;
//switchPin2
currentSwitchState2 = digitalRead(switchPin2);
if( currentSwitchState2 == LOW && switchState2 == HIGH ) // push
//Note on channel 1 (0x90), some note value (note), middle velocity (0x45):
noteOn(0x90, note2, 0x45);
if( currentSwitchState2 == HIGH && switchState2 == LOW ) // release
//Note on channel 1 (0x90), some note value (note), silent velocity (0x00):
noteOn(0x90, note2, 0x00);
switchState2 = currentSwitchState2;
//switchPin3
currentSwitchState3 = digitalRead(switchPin3);
if( currentSwitchState3 == LOW && switchState3 == HIGH ) // push
//Note on channel 1 (0x90), some note value (note), middle velocity (0x45):
noteOn(0x90, note3, 0x45);
if( currentSwitchState3 == HIGH && switchState3 == LOW ) // release
//Note on channel 1 (0x90), some note value (note), silent velocity (0x00):
noteOn(0x90, note3, 0x00);
switchState3 = currentSwitchState3;
//switchPin4
currentSwitchState4 = digitalRead(switchPin4);
if( currentSwitchState4 == LOW && switchState4 == HIGH ) // push
//Note on channel 1 (0x90), some note value (note), middle velocity (0x45):
noteOn(0x90, note4, 0x45);
if( currentSwitchState4 == HIGH && switchState4 == LOW ) // release
//Note on channel 1 (0x90), some note value (note), silent velocity (0x00):
noteOn(0x90, note4, 0x00);
switchState4 = currentSwitchState4;
//switchPin5
currentSwitchState5 = digitalRead(switchPin5);
if( currentSwitchState5 == LOW && switchState5 == HIGH ) // push
//Note on channel 1 (0x90), some note value (note), middle velocity (0x45):
noteOn(0x90, note5, 0x45);
if( currentSwitchState5 == HIGH && switchState5 == LOW ) // release
//Note on channel 1 (0x90), some note value (note), silent velocity (0x00):
noteOn(0x90, note5, 0x00);
switchState5 = currentSwitchState5;
//switchPin6
currentSwitchState6 = digitalRead(switchPin6);
if( currentSwitchState6 == LOW && switchState6 == HIGH ) // push
//Note on channel 1 (0x90), some note value (note), middle velocity (0x45):
noteOn(0x90, note6, 0x45);
if( currentSwitchState6 == HIGH && switchState6 == LOW ) // release
//Note on channel 1 (0x90), some note value (note), silent velocity (0x00):
noteOn(0x90, note6, 0x00);
switchState6 = currentSwitchState6;
}
// Send a MIDI note-on/off message.
void noteOn(char cmd, char data1, char data2) {
Serial.print(cmd, BYTE);
Serial.print(data1, BYTE);
Serial.print(data2, BYTE);
}
MIDI InterfaceSince we are using our circuit as a hardware MIDI device, a MIDI interface is needed. The more gear you have the more ports you need. You can find 1x1, 2x2, 4x4 and 8x8 MIDI interfaces commonly available. A simple m-audio MIDIsport 2x2 will do just fine for most people. This would give you 32 channels on 2 ports. That means only 2 devices can go into the computer, but as many as 32 could go out, given that each device only has 1 channel. The MIDISport 2X2 can be found in the ER. You would need to install the drivers. These can be found in the drivers directory of the M-Audio Site.
If your MIDI interface does not appear, make sure it is powered on and connected to your computer. If your MIDI interface is grayed out, that means this interface was previously present when you ran a MIDI application, but the driver could not locate it, or the driver is no longer installed. If your MIDI interface appears and is not grayed out, like shown above, this means that your device’s driver has successfully located the hardware and all is well. You can begin using your MIDI applications without any further setup.
GarageBand SetupNow, open up GarageBand - note that if it's already open, you'll have to relaunch it for the new MIDI interfaces to be recognized. This setting is found in preferences menu for GarageBand. In the preferences panel, click on "Audio/MIDI" and you'll see GarageBands Audio and MIDI input settings. An example of the Audio/MIDI setup panel is shown below:
Now that you've got your MIDI Interface working properly you can explore the vast collection of soft synthesizer sounds with GarageBand. MIDI Note Numbers for Different OctavesThe MIDI specification only defines note number 60 as "Middle C", and all other notes are relative. The absolute octave number designations shown here are based on Middle C = C4, which is an arbitrary assignment. There is a discrepancy that occurs between various models of MIDI devices and software programs, and that concerns the octave numbers for note names. If your MIDI software/device considers octave 0 as being the lowest octave of the MIDI note range, then middle C's note name is C5. The numbers used are 0 to 127. The lowest note upon a MIDI controller is a C and this is assigned note number 0. The C# below it would have a note number of 1. The D note below that would have a note number of 2. So "Middle C" is note number 60.
Organization MIDI commands and data in a byte of informationMIDI bytes range between 0 and 255. Note that a byte is a binary number that contains 8 digits. Furthermore, command bytes are split into half. The most significant half contains the actual MIDI command, and the second half contains the MIDI channel for which the command is for. For example, 0x91 is the note-on command for the second MIDI channel.
The messages from 0x80 to 0xEF are called Channel Messages because the second four bits of the command specify which channel the message affects. The messages from 0xF0 to 0xFF are called System Messages. they do not affect any particular channel.
|