Arduino MIDI Library Version 3.2
|
The main class for MIDI handling.
See member descriptions to know how to use it, or check out the examples supplied with the library.
More...
#include <MIDI.h>
Public Member Functions | |
MIDI_Class () | |
Default constructor for MIDI_Class. | |
~MIDI_Class () | |
Default destructor for MIDI_Class. | |
void | begin (const byte inChannel=1) |
Call the begin method in the setup() function of the Arduino. | |
void | sendNoteOn (byte NoteNumber, byte Velocity, byte Channel) |
Send a Note On message. | |
void | sendNoteOff (byte NoteNumber, byte Velocity, byte Channel) |
Send a Note Off message (a real Note Off, not a Note On with null velocity) | |
void | sendProgramChange (byte ProgramNumber, byte Channel) |
Send a Program Change message. | |
void | sendControlChange (byte ControlNumber, byte ControlValue, byte Channel) |
Send a Control Change message. | |
void | sendPitchBend (int PitchValue, byte Channel) |
Send a Pitch Bend message using a signed integer value. | |
void | sendPitchBend (unsigned int PitchValue, byte Channel) |
Send a Pitch Bend message using an unsigned integer value. | |
void | sendPitchBend (double PitchValue, byte Channel) |
Send a Pitch Bend message using a floating point value. | |
void | sendPolyPressure (byte NoteNumber, byte Pressure, byte Channel) |
Send a Polyphonic AfterTouch message (applies to only one specified note) | |
void | sendAfterTouch (byte Pressure, byte Channel) |
Send a MonoPhonic AfterTouch message (applies to all notes) | |
void | sendSysEx (int length, const byte *const array, bool ArrayContainsBoundaries=false) |
Generate and send a System Exclusive frame. | |
void | sendTimeCodeQuarterFrame (byte TypeNibble, byte ValuesNibble) |
Send a MIDI Time Code Quarter Frame. | |
void | sendTimeCodeQuarterFrame (byte data) |
Send a MIDI Time Code Quarter Frame. | |
void | sendSongPosition (unsigned int Beats) |
Send a Song Position Pointer message. | |
void | sendSongSelect (byte SongNumber) |
Send a Song Select message. | |
void | sendTuneRequest () |
Send a Tune Request message. | |
void | sendRealTime (kMIDIType Type) |
Send a Real Time (one byte) message. | |
void | send (kMIDIType type, byte param1, byte param2, byte channel) |
Generate and send a MIDI message from the values given. | |
bool | read () |
Read a MIDI message from the serial port using the main input channel (see setInputChannel() for reference). | |
bool | read (const byte Channel) |
Reading/thru-ing method, the same as read() with a given input channel to read on. | |
kMIDIType | getType () const |
Get the last received message's type. | |
byte | getChannel () const |
Get the channel of the message stored in the structure. | |
byte | getData1 () const |
Get the first data byte of the last received message. | |
byte | getData2 () const |
Get the second data byte of the last received message. | |
const byte * | getSysExArray () const |
Get the System Exclusive byte array. | |
unsigned int | getSysExArrayLength () const |
Get the lenght of the System Exclusive array. | |
bool | check () const |
Check if a valid message is stored in the structure. | |
byte | getInputChannel () const |
void | setInputChannel (const byte Channel) |
Set the value for the input MIDI channel. | |
void | setHandleNoteOff (void(*fptr)(byte channel, byte note, byte velocity)) |
void | setHandleNoteOn (void(*fptr)(byte channel, byte note, byte velocity)) |
void | setHandleAfterTouchPoly (void(*fptr)(byte channel, byte note, byte pressure)) |
void | setHandleControlChange (void(*fptr)(byte channel, byte number, byte value)) |
void | setHandleProgramChange (void(*fptr)(byte channel, byte number)) |
void | setHandleAfterTouchChannel (void(*fptr)(byte channel, byte pressure)) |
void | setHandlePitchBend (void(*fptr)(byte channel, int bend)) |
void | setHandleSystemExclusive (void(*fptr)(byte *array, byte size)) |
void | setHandleTimeCodeQuarterFrame (void(*fptr)(byte data)) |
void | setHandleSongPosition (void(*fptr)(unsigned int beats)) |
void | setHandleSongSelect (void(*fptr)(byte songnumber)) |
void | setHandleTuneRequest (void(*fptr)(void)) |
void | setHandleClock (void(*fptr)(void)) |
void | setHandleStart (void(*fptr)(void)) |
void | setHandleContinue (void(*fptr)(void)) |
void | setHandleStop (void(*fptr)(void)) |
void | setHandleActiveSensing (void(*fptr)(void)) |
void | setHandleSystemReset (void(*fptr)(void)) |
void | disconnectCallbackFromType (kMIDIType Type) |
Detach an external function from the given type. | |
kThruFilterMode | getFilterMode () const |
bool | getThruState () const |
void | turnThruOn (kThruFilterMode inThruFilterMode=Full) |
Setter method: turn message mirroring on. | |
void | turnThruOff () |
Setter method: turn message mirroring off. | |
void | setThruFilterMode (const kThruFilterMode inThruFilterMode) |
Set the filter for thru mirroring. | |
Static Public Member Functions | |
static const kMIDIType | getTypeFromStatusByte (const byte inStatus) |
Extract an enumerated MIDI type from a status byte. |
The main class for MIDI handling.
See member descriptions to know how to use it, or check out the examples supplied with the library.
MIDI_Class::MIDI_Class | ( | ) |
Default constructor for MIDI_Class.
Definition at line 22 of file MIDI.cpp.
{ #if USE_CALLBACKS // Initialise callbacks to NULL pointer mNoteOffCallback = NULL; mNoteOnCallback = NULL; mAfterTouchPolyCallback = NULL; mControlChangeCallback = NULL; mProgramChangeCallback = NULL; mAfterTouchChannelCallback = NULL; mPitchBendCallback = NULL; mSystemExclusiveCallback = NULL; mTimeCodeQuarterFrameCallback = NULL; mSongPositionCallback = NULL; mSongSelectCallback = NULL; mTuneRequestCallback = NULL; mClockCallback = NULL; mStartCallback = NULL; mContinueCallback = NULL; mStopCallback = NULL; mActiveSensingCallback = NULL; mSystemResetCallback = NULL; #endif }
MIDI_Class::~MIDI_Class | ( | ) |
Default destructor for MIDI_Class.
This is not really useful for the Arduino, as it is never called...
Definition at line 56 of file MIDI.cpp.
{ }
void MIDI_Class::begin | ( | const byte | inChannel = 1 | ) |
Call the begin method in the setup() function of the Arduino.
All parameters are set to their default values:
Definition at line 68 of file MIDI.cpp.
{ // Initialise the Serial port USE_SERIAL_PORT.begin(MIDI_BAUDRATE); #if COMPILE_MIDI_OUT #if USE_RUNNING_STATUS mRunningStatus_TX = InvalidType; #endif // USE_RUNNING_STATUS #endif // COMPILE_MIDI_OUT #if COMPILE_MIDI_IN mInputChannel = inChannel; mRunningStatus_RX = InvalidType; mPendingMessageIndex = 0; mPendingMessageExpectedLenght = 0; mMessage.valid = false; mMessage.type = InvalidType; mMessage.channel = 0; mMessage.data1 = 0; mMessage.data2 = 0; #endif // COMPILE_MIDI_IN #if (COMPILE_MIDI_IN && COMPILE_MIDI_OUT && COMPILE_MIDI_THRU) // Thru mThruFilterMode = Full; mThruActivated = true; #endif // Thru }
bool MIDI_Class::check | ( | ) | const |
void MIDI_Class::disconnectCallbackFromType | ( | kMIDIType | Type | ) |
Detach an external function from the given type.
Use this method to cancel the effects of setHandle********.
Type | The type of message to unbind. When a message of this type is received, no function will be called. |
Definition at line 949 of file MIDI.cpp.
{ switch (Type) { case NoteOff: mNoteOffCallback = NULL; break; case NoteOn: mNoteOnCallback = NULL; break; case AfterTouchPoly: mAfterTouchPolyCallback = NULL; break; case ControlChange: mControlChangeCallback = NULL; break; case ProgramChange: mProgramChangeCallback = NULL; break; case AfterTouchChannel: mAfterTouchChannelCallback = NULL; break; case PitchBend: mPitchBendCallback = NULL; break; case SystemExclusive: mSystemExclusiveCallback = NULL; break; case TimeCodeQuarterFrame: mTimeCodeQuarterFrameCallback = NULL; break; case SongPosition: mSongPositionCallback = NULL; break; case SongSelect: mSongSelectCallback = NULL; break; case TuneRequest: mTuneRequestCallback = NULL; break; case Clock: mClockCallback = NULL; break; case Start: mStartCallback = NULL; break; case Continue: mContinueCallback = NULL; break; case Stop: mStopCallback = NULL; break; case ActiveSensing: mActiveSensingCallback = NULL; break; case SystemReset: mSystemResetCallback = NULL; break; default: break; } }
byte MIDI_Class::getChannel | ( | ) | const |
byte MIDI_Class::getData1 | ( | ) | const |
byte MIDI_Class::getData2 | ( | ) | const |
kThruFilterMode MIDI_Class::getFilterMode | ( | ) | const [inline] |
byte MIDI_Class::getInputChannel | ( | ) | const [inline] |
const byte * MIDI_Class::getSysExArray | ( | ) | const |
Get the System Exclusive byte array.
Definition at line 878 of file MIDI.cpp.
{ return mMessage.sysex_array; }
unsigned int MIDI_Class::getSysExArrayLength | ( | ) | const |
Get the lenght of the System Exclusive array.
It is coded using data1 as LSB and data2 as MSB.
Definition at line 890 of file MIDI.cpp.
{ unsigned int coded_size = ((unsigned int)(mMessage.data2) << 8) | mMessage.data1; return (coded_size > MIDI_SYSEX_ARRAY_SIZE) ? MIDI_SYSEX_ARRAY_SIZE : coded_size; }
bool MIDI_Class::getThruState | ( | ) | const [inline] |
kMIDIType MIDI_Class::getType | ( | ) | const |
Extract an enumerated MIDI type from a status byte.
This is a utility static method, used internally, made public so you can handle kMIDITypes more easily.
Definition at line 203 of file MIDI.h.
{ if ((inStatus < 0x80) || (inStatus == 0xF4) || (inStatus == 0xF5) || (inStatus == 0xF9) || (inStatus == 0xFD)) return InvalidType; // data bytes and undefined. if (inStatus < 0xF0) return (kMIDIType)(inStatus & 0xF0); // Channel message, remove channel nibble. else return (kMIDIType)inStatus; }
bool MIDI_Class::read | ( | const byte | Channel | ) |
Reading/thru-ing method, the same as read() with a given input channel to read on.
Definition at line 481 of file MIDI.cpp.
{ if (inChannel >= MIDI_CHANNEL_OFF) return false; // MIDI Input disabled. if (parse(inChannel)) { if (input_filter(inChannel)) { #if (COMPILE_MIDI_OUT && COMPILE_MIDI_THRU) thru_filter(inChannel); #endif #if USE_CALLBACKS launchCallback(); #endif return true; } } return false; }
bool MIDI_Class::read | ( | ) |
Read a MIDI message from the serial port using the main input channel (see setInputChannel() for reference).
Returned value: true if any valid message has been stored in the structure, false if not. A valid message is a message that matches the input channel.
If the Thru is enabled and the messages matches the filter, it is sent back on the MIDI output.
Definition at line 472 of file MIDI.cpp.
{ return read(mInputChannel); }
Generate and send a MIDI message from the values given.
type | The message type (see type defines for reference) |
data1 | The first data byte. |
data2 | The second data byte (if the message contains only 1 data byte, set this one to 0). |
channel | The output channel on which the message will be sent (values from 1 to 16). Note: you cannot send to OMNI. |
This is an internal method, use it only if you need to send raw data from your code, at your own risks.
Definition at line 132 of file MIDI.cpp.
{ // Then test if channel is valid if (channel >= MIDI_CHANNEL_OFF || channel == MIDI_CHANNEL_OMNI || type < NoteOff) { #if USE_RUNNING_STATUS mRunningStatus_TX = InvalidType; #endif return; // Don't send anything } if (type <= PitchBend) { // Channel messages // Protection: remove MSBs on data data1 &= 0x7F; data2 &= 0x7F; byte statusbyte = genstatus(type,channel); #if USE_RUNNING_STATUS // Check Running Status if (mRunningStatus_TX != statusbyte) { // New message, memorise and send header mRunningStatus_TX = statusbyte; USE_SERIAL_PORT.write(mRunningStatus_TX); } #else // Don't care about running status, send the Control byte. USE_SERIAL_PORT.write(statusbyte); #endif // Then send data USE_SERIAL_PORT.write(data1); if (type != ProgramChange && type != AfterTouchChannel) { USE_SERIAL_PORT.write(data2); } return; } if (type >= TuneRequest && type <= SystemReset) { // System Real-time and 1 byte. sendRealTime(type); } }
Send a MonoPhonic AfterTouch message (applies to all notes)
Pressure | The amount of AfterTouch to apply to all notes. |
Channel | The channel on which the message will be sent (1 to 16). |
Definition at line 261 of file MIDI.cpp.
{ send(AfterTouchChannel,Pressure,0,Channel); }
Send a Control Change message.
ControlNumber | The controller number (0 to 127). See the detailed description here: http://www.somascape.org/midi/tech/spec.html#ctrlnums |
ControlValue | The value for the specified controller (0 to 127). |
Channel | The channel on which the message will be sent (1 to 16). |
Definition at line 232 of file MIDI.cpp.
{ send(ControlChange,ControlNumber,ControlValue,Channel); }
Send a Note Off message (a real Note Off, not a Note On with null velocity)
NoteNumber | Pitch value in the MIDI format (0 to 127). Take a look at the values, names and frequencies of notes here: http://www.phys.unsw.edu.au/jw/notes.html |
Velocity | Release velocity (0 to 127). |
Channel | The channel on which the message will be sent (1 to 16). |
Send a Note On message.
NoteNumber | Pitch value in the MIDI format (0 to 127). Take a look at the values, names and frequencies of notes here: http://www.phys.unsw.edu.au/jw/notes.html |
Velocity | Note attack velocity (0 to 127). A NoteOn with 0 velocity is considered as a NoteOff. |
Channel | The channel on which the message will be sent (1 to 16). |
void MIDI_Class::sendPitchBend | ( | double | PitchValue, |
byte | Channel | ||
) |
Send a Pitch Bend message using a floating point value.
PitchValue | The amount of bend to send (in a floating point format), between -1.0f (maximum downwards bend) and +1.0f (max upwards bend), center value is 0.0f. |
Channel | The channel on which the message will be sent (1 to 16). |
Definition at line 301 of file MIDI.cpp.
{ unsigned int pitchval = (PitchValue+1.f)*8192; if (pitchval > 16383) pitchval = 16383; // overflow protection sendPitchBend(pitchval,Channel); }
void MIDI_Class::sendPitchBend | ( | int | PitchValue, |
byte | Channel | ||
) |
Send a Pitch Bend message using a signed integer value.
PitchValue | The amount of bend to send (in a signed integer format), between -8192 (maximum downwards bend) and 8191 (max upwards bend), center value is 0. |
Channel | The channel on which the message will be sent (1 to 16). |
Definition at line 274 of file MIDI.cpp.
{ unsigned int bend = PitchValue + 8192; sendPitchBend(bend,Channel); }
void MIDI_Class::sendPitchBend | ( | unsigned int | PitchValue, |
byte | Channel | ||
) |
Send a Pitch Bend message using an unsigned integer value.
PitchValue | The amount of bend to send (in a signed integer format), between 0 (maximum downwards bend) and 16383 (max upwards bend), center value is 8192. |
Channel | The channel on which the message will be sent (1 to 16). |
Send a Polyphonic AfterTouch message (applies to only one specified note)
NoteNumber | The note to apply AfterTouch to (0 to 127). |
Pressure | The amount of AfterTouch to apply (0 to 127). |
Channel | The channel on which the message will be sent (1 to 16). |
Definition at line 247 of file MIDI.cpp.
{ send(AfterTouchPoly,NoteNumber,Pressure,Channel); }
Send a Program Change message.
ProgramNumber | The Program to select (0 to 127). |
Channel | The channel on which the message will be sent (1 to 16). |
Definition at line 218 of file MIDI.cpp.
{ send(ProgramChange,ProgramNumber,0,Channel); }
void MIDI_Class::sendRealTime | ( | kMIDIType | Type | ) |
Send a Real Time (one byte) message.
Type | The available Real Time types are: Start, Stop, Continue, Clock, ActiveSensing and SystemReset. You can also send a Tune Request with this method. |
Definition at line 435 of file MIDI.cpp.
{ switch (Type) { case TuneRequest: // Not really real-time, but one byte anyway. case Clock: case Start: case Stop: case Continue: case ActiveSensing: case SystemReset: USE_SERIAL_PORT.write((byte)Type); break; default: // Invalid Real Time marker break; } // Do not cancel Running Status for real-time messages as they can be interleaved within any message. // Though, TuneRequest can be sent here, and as it is a System Common message, it must reset Running Status. #if USE_RUNNING_STATUS if (Type == TuneRequest) mRunningStatus_TX = InvalidType; #endif }
void MIDI_Class::sendSongPosition | ( | unsigned int | Beats | ) |
Send a Song Position Pointer message.
Beats | The number of beats since the start of the song. |
Definition at line 401 of file MIDI.cpp.
{ USE_SERIAL_PORT.write((byte)SongPosition); USE_SERIAL_PORT.write(Beats & 0x7F); USE_SERIAL_PORT.write((Beats >> 7) & 0x7F); #if USE_RUNNING_STATUS mRunningStatus_TX = InvalidType; #endif }
void MIDI_Class::sendSongSelect | ( | byte | SongNumber | ) |
Send a Song Select message.
Definition at line 416 of file MIDI.cpp.
{ USE_SERIAL_PORT.write((byte)SongSelect); USE_SERIAL_PORT.write(SongNumber & 0x7F); #if USE_RUNNING_STATUS mRunningStatus_TX = InvalidType; #endif }
void MIDI_Class::sendSysEx | ( | int | length, |
const byte *const | array, | ||
bool | ArrayContainsBoundaries = false |
||
) |
Generate and send a System Exclusive frame.
length | The size of the array to send |
array | The byte array containing the data to send |
ArrayContainsBoundaries | When set to 'true', 0xF0 & 0xF7 bytes (start & stop SysEx) will NOT be sent (and therefore must be included in the array). default value is set to 'false' for compatibility with previous versions of the library. |
Definition at line 318 of file MIDI.cpp.
{ if (ArrayContainsBoundaries == false) { USE_SERIAL_PORT.write(0xF0); for (int i=0;i<length;++i) { USE_SERIAL_PORT.write(array[i]); } USE_SERIAL_PORT.write(0xF7); } else { for (int i=0;i<length;++i) { USE_SERIAL_PORT.write(array[i]); } } #if USE_RUNNING_STATUS mRunningStatus_TX = InvalidType; #endif }
void MIDI_Class::sendTimeCodeQuarterFrame | ( | byte | data | ) |
Send a MIDI Time Code Quarter Frame.
See MIDI Specification for more information.
data | if you want to encode directly the nibbles in your program, you can send the byte here. |
Definition at line 385 of file MIDI.cpp.
{ USE_SERIAL_PORT.write((byte)TimeCodeQuarterFrame); USE_SERIAL_PORT.write(data); #if USE_RUNNING_STATUS mRunningStatus_TX = InvalidType; #endif }
Send a MIDI Time Code Quarter Frame.
See MIDI Specification for more information.
TypeNibble | MTC type |
ValuesNibble | MTC data |
Definition at line 371 of file MIDI.cpp.
{ byte data = ( ((TypeNibble & 0x07) << 4) | (ValuesNibble & 0x0F) ); sendTimeCodeQuarterFrame(data); }
void MIDI_Class::sendTuneRequest | ( | ) |
Send a Tune Request message.
When a MIDI unit receives this message, it should tune its oscillators (if equipped with any)
Definition at line 357 of file MIDI.cpp.
{ sendRealTime(TuneRequest); }
void MIDI_Class::setHandleActiveSensing | ( | void(*)(void) | fptr | ) |
void MIDI_Class::setHandleClock | ( | void(*)(void) | fptr | ) |
void MIDI_Class::setHandleContinue | ( | void(*)(void) | fptr | ) |
void MIDI_Class::setHandlePitchBend | ( | void(*)(byte channel, int bend) | fptr | ) |
void MIDI_Class::setHandleSongPosition | ( | void(*)(unsigned int beats) | fptr | ) |
void MIDI_Class::setHandleSongSelect | ( | void(*)(byte songnumber) | fptr | ) |
void MIDI_Class::setHandleStart | ( | void(*)(void) | fptr | ) |
void MIDI_Class::setHandleStop | ( | void(*)(void) | fptr | ) |
void MIDI_Class::setHandleSystemReset | ( | void(*)(void) | fptr | ) |
void MIDI_Class::setHandleTimeCodeQuarterFrame | ( | void(*)(byte data) | fptr | ) |
void MIDI_Class::setHandleTuneRequest | ( | void(*)(void) | fptr | ) |
void MIDI_Class::setInputChannel | ( | const byte | Channel | ) |
void MIDI_Class::setThruFilterMode | ( | const kThruFilterMode | inThruFilterMode | ) |
Set the filter for thru mirroring.
inThruFilterMode | a filter mode |
Definition at line 1035 of file MIDI.cpp.
{ mThruFilterMode = inThruFilterMode; if (mThruFilterMode != Off) mThruActivated = true; else mThruActivated = false; }
void MIDI_Class::turnThruOff | ( | ) |
void MIDI_Class::turnThruOn | ( | kThruFilterMode | inThruFilterMode = Full | ) |