MIDI库的代码基本应用框架如下:
#include <MIDI.h>
MIDI_CREATE_INSTANCE(HardwareSerial, Serial, HardwareMIDI); //配置MIDI接口使用Serial,名称为HardwareMIDI
//-----------------------------
byte data[20];
void setup()
{
Serial.begin(31250);//配置串口波特率(MIDI)
//以下为MIDI信息接收程序的配置
HardwareMIDI.setHandleNoteOn(HandleHardwareNoteOn); //音符开
HardwareMIDI.setHandleNoteOff(HandleHardwareNoteOff);
HardwareMIDI.setHandleControlChange(HandleHardwareControlChange); //控制信息
HardwareMIDI.setHandleProgramChange(HandleHardwareProgramChange); //程序改变
HardwareMIDI.setHandlePitchBend(HandleHardwarePitchBend); //弯音轮
HardwareMIDI.setHandleAfterTouchChannel(HandleHardwareAfterTouchChannel); //触后通道
HardwareMIDI.setHandleAfterTouchPoly(HandleHardwareAfterTouchPoly); //触后音符
HardwareMIDI.setHandleSystemExclusive(HandleHardwareSystemExclusive); //系统信息
HardwareMIDI.turnThruOff(); //防止MIDI回路
}
//================================================================
void HandleHardwareNoteOn(byte channel, byte note, byte velocity)
{
// if(note >= 0x3c){HardwareMIDI.sendNoteOn(note,127,1);}
}
void HandleHardwareNoteOff(byte channel, byte note, byte velocity)
{
}
void HandleHardwareControlChange(byte channel, byte note, byte velocity)
{
}
void HandleHardwareProgramChange(byte channel, byte number)
{
}
void HandleHardwarePitchBend(byte channel, int bend)
{
}
void HandleHardwareAfterTouchChannel(byte channel, byte pressure)
{
}
void HandleHardwareAfterTouchPoly(byte channel, byte note, byte pressure)
{
}
void HandleHardwareSystemExclusive(byte *array, unsigned int size)
{
unsigned int number = HardwareMIDI.getSysExArrayLength ();
const byte *pMsg = HardwareMIDI.getSysExArray();
for (int n = 0; n < number; n++) {
//data[n]为接收到的系统信息
data[n] = pMsg[n];
}
}
//================================================================
void loop()
{
HardwareMIDI.read();//别忘了在循环中添加这行
}