arduino的MIDI library应用之接收MIDI信息(一)

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();//别忘了在循环中添加这行
} 
发布了24 篇原创文章 · 获赞 17 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/huanghaoAudio/article/details/79543658