MIDIEvents.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * MIDIEvents.cpp
  3. * --------------
  4. * Purpose: MIDI event handling, event lists, ...
  5. * Notes : (currently none)
  6. * Authors: OpenMPT Devs
  7. * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
  8. */
  9. #include "stdafx.h"
  10. #include "MIDIEvents.h"
  11. OPENMPT_NAMESPACE_BEGIN
  12. namespace MIDIEvents
  13. {
  14. // Build a generic MIDI event
  15. uint32 Event(EventType eventType, uint8 midiChannel, uint8 dataByte1, uint8 dataByte2)
  16. {
  17. return (eventType << 4) | (midiChannel & 0x0F) | (dataByte1 << 8) | (dataByte2 << 16);
  18. }
  19. // Build a MIDI CC event
  20. uint32 CC(MidiCC midiCC, uint8 midiChannel, uint8 param)
  21. {
  22. return Event(evControllerChange, midiChannel, static_cast<uint8>(midiCC), param);
  23. }
  24. // Build a MIDI Pitchbend event
  25. uint32 PitchBend(uint8 midiChannel, uint16 bendAmount)
  26. {
  27. return Event(evPitchBend, midiChannel, static_cast<uint8>(bendAmount & 0x7F), static_cast<uint8>(bendAmount >> 7));
  28. }
  29. // Build a MIDI Program Change event
  30. uint32 ProgramChange(uint8 midiChannel, uint8 program)
  31. {
  32. return Event(evProgramChange, midiChannel, program, 0);
  33. }
  34. // Build a MIDI Note Off event
  35. uint32 NoteOff(uint8 midiChannel, uint8 note, uint8 velocity)
  36. {
  37. return Event(evNoteOff, midiChannel, note, velocity);
  38. }
  39. // Build a MIDI Note On event
  40. uint32 NoteOn(uint8 midiChannel, uint8 note, uint8 velocity)
  41. {
  42. return Event(evNoteOn, midiChannel, note, velocity);
  43. }
  44. // Build a MIDI System Event
  45. uint8 System(SystemEvent eventType)
  46. {
  47. return static_cast<uint8>((evSystem << 4) | eventType);
  48. }
  49. // Get MIDI channel from a MIDI event
  50. uint8 GetChannelFromEvent(uint32 midiMsg)
  51. {
  52. return static_cast<uint8>((midiMsg & 0xF));
  53. }
  54. // Get MIDI Event type from a MIDI event
  55. EventType GetTypeFromEvent(uint32 midiMsg)
  56. {
  57. return static_cast<EventType>(((midiMsg >> 4) & 0xF));
  58. }
  59. // Get first data byte from a MIDI event
  60. uint8 GetDataByte1FromEvent(uint32 midiMsg)
  61. {
  62. return static_cast<uint8>(((midiMsg >> 8) & 0xFF));
  63. }
  64. // Get second data byte from a MIDI event
  65. uint8 GetDataByte2FromEvent(uint32 midiMsg)
  66. {
  67. return static_cast<uint8>(((midiMsg >> 16) & 0xFF));
  68. }
  69. // Get the length of a MIDI event in bytes
  70. uint8 GetEventLength(uint8 firstByte)
  71. {
  72. uint8 msgSize = 3;
  73. switch(firstByte & 0xF0)
  74. {
  75. case 0xC0:
  76. case 0xD0:
  77. msgSize = 2;
  78. break;
  79. case 0xF0:
  80. switch(firstByte)
  81. {
  82. case 0xF1:
  83. case 0xF3:
  84. msgSize = 2;
  85. break;
  86. case 0xF2:
  87. msgSize = 3;
  88. break;
  89. default:
  90. msgSize = 1;
  91. break;
  92. }
  93. break;
  94. }
  95. return msgSize;
  96. }
  97. // MIDI CC Names
  98. const char* const MidiCCNames[MIDICC_end + 1] =
  99. {
  100. "BankSelect [Coarse]", //0
  101. "ModulationWheel [Coarse]", //1
  102. "Breathcontroller [Coarse]", //2
  103. "", //3
  104. "FootPedal [Coarse]", //4
  105. "PortamentoTime [Coarse]", //5
  106. "DataEntry [Coarse]", //6
  107. "Volume [Coarse]", //7
  108. "Balance [Coarse]", //8
  109. "", //9
  110. "Panposition [Coarse]", //10
  111. "Expression [Coarse]", //11
  112. "EffectControl1 [Coarse]", //12
  113. "EffectControl2 [Coarse]", //13
  114. "", //14
  115. "", //15
  116. "GeneralPurposeSlider1", //16
  117. "GeneralPurposeSlider2", //17
  118. "GeneralPurposeSlider3", //18
  119. "GeneralPurposeSlider4", //19
  120. "", //20
  121. "", //21
  122. "", //22
  123. "", //23
  124. "", //24
  125. "", //25
  126. "", //26
  127. "", //27
  128. "", //28
  129. "", //29
  130. "", //30
  131. "", //31
  132. "BankSelect [Fine]", //32
  133. "ModulationWheel [Fine]", //33
  134. "Breathcontroller [Fine]", //34
  135. "", //35
  136. "FootPedal [Fine]", //36
  137. "PortamentoTime [Fine]", //37
  138. "DataEntry [Fine]", //38
  139. "Volume [Fine]", //39
  140. "Balance [Fine]", //40
  141. "", //41
  142. "Panposition [Fine]", //42
  143. "Expression [Fine]", //43
  144. "EffectControl1 [Fine]", //44
  145. "EffectControl2 [Fine]", //45
  146. "", //46
  147. "", //47
  148. "", //48
  149. "", //49
  150. "", //50
  151. "", //51
  152. "", //52
  153. "", //53
  154. "", //54
  155. "", //55
  156. "", //56
  157. "", //57
  158. "", //58
  159. "", //59
  160. "", //60
  161. "", //61
  162. "", //62
  163. "", //63
  164. "HoldPedal [OnOff]", //64
  165. "Portamento [OnOff]", //65
  166. "SustenutoPedal [OnOff]", //66
  167. "SoftPedal [OnOff]", //67
  168. "LegatoPedal [OnOff]", //68
  169. "Hold2Pedal [OnOff]", //69
  170. "SoundVariation", //70
  171. "SoundTimbre", //71
  172. "SoundReleaseTime", //72
  173. "SoundAttackTime", //73
  174. "SoundBrightness", //74
  175. "SoundControl6", //75
  176. "SoundControl7", //76
  177. "SoundControl8", //77
  178. "SoundControl9", //78
  179. "SoundControl10", //79
  180. "GeneralPurposeButton1 [OnOff]",//80
  181. "GeneralPurposeButton2 [OnOff]",//81
  182. "GeneralPurposeButton3 [OnOff]",//82
  183. "GeneralPurposeButton4 [OnOff]",//83
  184. "", //84
  185. "", //85
  186. "", //86
  187. "", //87
  188. "", //88
  189. "", //89
  190. "", //90
  191. "EffectsLevel", //91
  192. "TremoloLevel", //92
  193. "ChorusLevel", //93
  194. "CelesteLevel", //94
  195. "PhaserLevel", //95
  196. "DataButtonIncrement", //96
  197. "DataButtonDecrement", //97
  198. "NonRegisteredParameter [Fine]",//98
  199. "NonRegisteredParameter [Coarse]",//99
  200. "RegisteredParameter [Fine]", //100
  201. "RegisteredParameter [Coarse]", //101
  202. "", //102
  203. "", //103
  204. "", //104
  205. "", //105
  206. "", //106
  207. "", //107
  208. "", //108
  209. "", //109
  210. "", //110
  211. "", //111
  212. "", //112
  213. "", //113
  214. "", //114
  215. "", //115
  216. "", //116
  217. "", //117
  218. "", //118
  219. "", //119
  220. "AllSoundOff", //120
  221. "AllControllersOff", //121
  222. "LocalKeyboard [OnOff]", //122
  223. "AllNotesOff", //123
  224. "OmniModeOff", //124
  225. "OmniModeOn", //125
  226. "MonoOperation", //126
  227. "PolyOperation", //127
  228. };
  229. } // End namespace
  230. OPENMPT_NAMESPACE_END