Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

juce_MidiMessage.cpp 41KB

9 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
8 years ago
9 years ago
9 years ago
9 years ago
10 years ago
10 years ago
10 years ago
11 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. namespace MidiHelpers
  18. {
  19. inline uint8 initialByte (const int type, const int channel) noexcept
  20. {
  21. return (uint8) (type | jlimit (0, 15, channel - 1));
  22. }
  23. inline uint8 validVelocity (const int v) noexcept
  24. {
  25. return (uint8) jlimit (0, 127, v);
  26. }
  27. inline uint8 floatVelocityToByte (const float v) noexcept
  28. {
  29. return validVelocity (roundToInt (v * 127.0f));
  30. }
  31. }
  32. //==============================================================================
  33. int MidiMessage::readVariableLengthVal (const uint8* data, int& numBytesUsed) noexcept
  34. {
  35. numBytesUsed = 0;
  36. int v = 0, i;
  37. do
  38. {
  39. i = (int) *data++;
  40. if (++numBytesUsed > 6)
  41. break;
  42. v = (v << 7) + (i & 0x7f);
  43. } while (i & 0x80);
  44. return v;
  45. }
  46. int MidiMessage::getMessageLengthFromFirstByte (const uint8 firstByte) noexcept
  47. {
  48. // this method only works for valid starting bytes of a short midi message
  49. jassert (firstByte >= 0x80 && firstByte != 0xf0 && firstByte != 0xf7);
  50. static const char messageLengths[] =
  51. {
  52. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  53. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  54. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  55. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  56. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  57. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  58. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  59. 1, 2, 3, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
  60. };
  61. return messageLengths [firstByte & 0x7f];
  62. }
  63. //==============================================================================
  64. MidiMessage::MidiMessage() noexcept
  65. : timeStamp (0), size (2)
  66. {
  67. preallocatedData.asBytes[0] = 0xf0;
  68. preallocatedData.asBytes[1] = 0xf7;
  69. }
  70. MidiMessage::MidiMessage (const void* const d, const int dataSize, const double t)
  71. : timeStamp (t),
  72. size (dataSize)
  73. {
  74. jassert (dataSize > 0);
  75. memcpy (allocateSpace (dataSize), d, (size_t) dataSize);
  76. // check that the length matches the data..
  77. jassert (size > 3 || *(uint8*)d >= 0xf0 || getMessageLengthFromFirstByte (*(uint8*)d) == size);
  78. }
  79. MidiMessage::MidiMessage (const int byte1, const double t) noexcept
  80. : timeStamp (t), size (1)
  81. {
  82. preallocatedData.asBytes[0] = (uint8) byte1;
  83. // check that the length matches the data..
  84. jassert (byte1 >= 0xf0 || getMessageLengthFromFirstByte ((uint8) byte1) == 1);
  85. }
  86. MidiMessage::MidiMessage (const int byte1, const int byte2, const double t) noexcept
  87. : timeStamp (t), size (2)
  88. {
  89. preallocatedData.asBytes[0] = (uint8) byte1;
  90. preallocatedData.asBytes[1] = (uint8) byte2;
  91. // check that the length matches the data..
  92. jassert (byte1 >= 0xf0 || getMessageLengthFromFirstByte ((uint8) byte1) == 2);
  93. }
  94. MidiMessage::MidiMessage (const int byte1, const int byte2, const int byte3, const double t) noexcept
  95. : timeStamp (t), size (3)
  96. {
  97. preallocatedData.asBytes[0] = (uint8) byte1;
  98. preallocatedData.asBytes[1] = (uint8) byte2;
  99. preallocatedData.asBytes[2] = (uint8) byte3;
  100. // check that the length matches the data..
  101. jassert (byte1 >= 0xf0 || getMessageLengthFromFirstByte ((uint8) byte1) == 3);
  102. }
  103. MidiMessage::MidiMessage (const MidiMessage& other)
  104. : timeStamp (other.timeStamp), size (other.size)
  105. {
  106. if (other.allocatedData != nullptr)
  107. {
  108. allocatedData.malloc ((size_t) size);
  109. memcpy (allocatedData, other.allocatedData, (size_t) size);
  110. }
  111. else
  112. {
  113. preallocatedData.asInt32 = other.preallocatedData.asInt32;
  114. }
  115. }
  116. MidiMessage::MidiMessage (const MidiMessage& other, const double newTimeStamp)
  117. : timeStamp (newTimeStamp), size (other.size)
  118. {
  119. if (other.allocatedData != nullptr)
  120. {
  121. allocatedData.malloc ((size_t) size);
  122. memcpy (allocatedData, other.allocatedData, (size_t) size);
  123. }
  124. else
  125. {
  126. preallocatedData.asInt32 = other.preallocatedData.asInt32;
  127. }
  128. }
  129. MidiMessage::MidiMessage (const void* srcData, int sz, int& numBytesUsed, const uint8 lastStatusByte,
  130. double t, bool sysexHasEmbeddedLength)
  131. : timeStamp (t)
  132. {
  133. const uint8* src = static_cast<const uint8*> (srcData);
  134. unsigned int byte = (unsigned int) *src;
  135. if (byte < 0x80)
  136. {
  137. byte = (unsigned int) (uint8) lastStatusByte;
  138. numBytesUsed = -1;
  139. }
  140. else
  141. {
  142. numBytesUsed = 0;
  143. --sz;
  144. ++src;
  145. }
  146. if (byte >= 0x80)
  147. {
  148. if (byte == 0xf0)
  149. {
  150. const uint8* d = src;
  151. bool haveReadAllLengthBytes = ! sysexHasEmbeddedLength;
  152. int numVariableLengthSysexBytes = 0;
  153. while (d < src + sz)
  154. {
  155. if (*d >= 0x80)
  156. {
  157. if (*d == 0xf7)
  158. {
  159. ++d; // include the trailing 0xf7 when we hit it
  160. break;
  161. }
  162. if (haveReadAllLengthBytes) // if we see a 0x80 bit set after the initial data length
  163. break; // bytes, assume it's the end of the sysex
  164. ++numVariableLengthSysexBytes;
  165. }
  166. else if (! haveReadAllLengthBytes)
  167. {
  168. haveReadAllLengthBytes = true;
  169. ++numVariableLengthSysexBytes;
  170. }
  171. ++d;
  172. }
  173. src += numVariableLengthSysexBytes;
  174. size = 1 + (int) (d - src);
  175. uint8* dest = allocateSpace (size);
  176. *dest = (uint8) byte;
  177. memcpy (dest + 1, src, (size_t) (size - 1));
  178. numBytesUsed += numVariableLengthSysexBytes; // (these aren't counted in the size)
  179. }
  180. else if (byte == 0xff)
  181. {
  182. int n;
  183. const int bytesLeft = readVariableLengthVal (src + 1, n);
  184. size = jmin (sz + 1, n + 2 + bytesLeft);
  185. uint8* dest = allocateSpace (size);
  186. *dest = (uint8) byte;
  187. memcpy (dest + 1, src, (size_t) size - 1);
  188. }
  189. else
  190. {
  191. preallocatedData.asInt32 = 0;
  192. size = getMessageLengthFromFirstByte ((uint8) byte);
  193. preallocatedData.asBytes[0] = (uint8) byte;
  194. if (size > 1)
  195. {
  196. preallocatedData.asBytes[1] = src[0];
  197. if (size > 2)
  198. preallocatedData.asBytes[2] = src[1];
  199. }
  200. }
  201. numBytesUsed += size;
  202. }
  203. else
  204. {
  205. preallocatedData.asInt32 = 0;
  206. size = 0;
  207. }
  208. }
  209. MidiMessage& MidiMessage::operator= (const MidiMessage& other)
  210. {
  211. if (this != &other)
  212. {
  213. timeStamp = other.timeStamp;
  214. size = other.size;
  215. if (other.allocatedData != nullptr)
  216. {
  217. allocatedData.malloc ((size_t) size);
  218. memcpy (allocatedData, other.allocatedData, (size_t) size);
  219. }
  220. else
  221. {
  222. allocatedData.free();
  223. preallocatedData.asInt32 = other.preallocatedData.asInt32;
  224. }
  225. }
  226. return *this;
  227. }
  228. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  229. MidiMessage::MidiMessage (MidiMessage&& other) noexcept
  230. : timeStamp (other.timeStamp), size (other.size)
  231. {
  232. if (other.allocatedData != nullptr)
  233. allocatedData.swapWith (other.allocatedData);
  234. else
  235. preallocatedData.asInt32 = other.preallocatedData.asInt32;
  236. }
  237. MidiMessage& MidiMessage::operator= (MidiMessage&& other) noexcept
  238. {
  239. jassert (this != &other); // shouldn't be possible
  240. timeStamp = other.timeStamp;
  241. size = other.size;
  242. allocatedData.swapWith (other.allocatedData);
  243. preallocatedData.asInt32 = other.preallocatedData.asInt32;
  244. return *this;
  245. }
  246. #endif
  247. MidiMessage::~MidiMessage() {}
  248. uint8* MidiMessage::allocateSpace (int bytes)
  249. {
  250. if (bytes > 4)
  251. {
  252. allocatedData.malloc ((size_t) bytes);
  253. return allocatedData;
  254. }
  255. return preallocatedData.asBytes;
  256. }
  257. int MidiMessage::getChannel() const noexcept
  258. {
  259. const uint8* const data = getRawData();
  260. if ((data[0] & 0xf0) != 0xf0)
  261. return (data[0] & 0xf) + 1;
  262. return 0;
  263. }
  264. bool MidiMessage::isForChannel (const int channel) const noexcept
  265. {
  266. jassert (channel > 0 && channel <= 16); // valid channels are numbered 1 to 16
  267. const uint8* const data = getRawData();
  268. return ((data[0] & 0xf) == channel - 1)
  269. && ((data[0] & 0xf0) != 0xf0);
  270. }
  271. void MidiMessage::setChannel (const int channel) noexcept
  272. {
  273. jassert (channel > 0 && channel <= 16); // valid channels are numbered 1 to 16
  274. uint8* const data = getData();
  275. if ((data[0] & 0xf0) != (uint8) 0xf0)
  276. data[0] = (uint8) ((data[0] & (uint8) 0xf0)
  277. | (uint8)(channel - 1));
  278. }
  279. bool MidiMessage::isNoteOn (const bool returnTrueForVelocity0) const noexcept
  280. {
  281. const uint8* const data = getRawData();
  282. return ((data[0] & 0xf0) == 0x90)
  283. && (returnTrueForVelocity0 || data[2] != 0);
  284. }
  285. bool MidiMessage::isNoteOff (const bool returnTrueForNoteOnVelocity0) const noexcept
  286. {
  287. const uint8* const data = getRawData();
  288. return ((data[0] & 0xf0) == 0x80)
  289. || (returnTrueForNoteOnVelocity0 && (data[2] == 0) && ((data[0] & 0xf0) == 0x90));
  290. }
  291. bool MidiMessage::isNoteOnOrOff() const noexcept
  292. {
  293. const uint8* const data = getRawData();
  294. const int d = data[0] & 0xf0;
  295. return (d == 0x90) || (d == 0x80);
  296. }
  297. int MidiMessage::getNoteNumber() const noexcept
  298. {
  299. return getRawData()[1];
  300. }
  301. void MidiMessage::setNoteNumber (const int newNoteNumber) noexcept
  302. {
  303. if (isNoteOnOrOff() || isAftertouch())
  304. getData()[1] = (uint8) (newNoteNumber & 127);
  305. }
  306. uint8 MidiMessage::getVelocity() const noexcept
  307. {
  308. if (isNoteOnOrOff())
  309. return getRawData()[2];
  310. return 0;
  311. }
  312. float MidiMessage::getFloatVelocity() const noexcept
  313. {
  314. return getVelocity() * (1.0f / 127.0f);
  315. }
  316. void MidiMessage::setVelocity (const float newVelocity) noexcept
  317. {
  318. if (isNoteOnOrOff())
  319. getData()[2] = MidiHelpers::floatVelocityToByte (newVelocity);
  320. }
  321. void MidiMessage::multiplyVelocity (const float scaleFactor) noexcept
  322. {
  323. if (isNoteOnOrOff())
  324. {
  325. uint8* const data = getData();
  326. data[2] = MidiHelpers::validVelocity (roundToInt (scaleFactor * data[2]));
  327. }
  328. }
  329. bool MidiMessage::isAftertouch() const noexcept
  330. {
  331. return (getRawData()[0] & 0xf0) == 0xa0;
  332. }
  333. int MidiMessage::getAfterTouchValue() const noexcept
  334. {
  335. jassert (isAftertouch());
  336. return getRawData()[2];
  337. }
  338. MidiMessage MidiMessage::aftertouchChange (const int channel,
  339. const int noteNum,
  340. const int aftertouchValue) noexcept
  341. {
  342. jassert (channel > 0 && channel <= 16); // valid channels are numbered 1 to 16
  343. jassert (isPositiveAndBelow (noteNum, (int) 128));
  344. jassert (isPositiveAndBelow (aftertouchValue, (int) 128));
  345. return MidiMessage (MidiHelpers::initialByte (0xa0, channel),
  346. noteNum & 0x7f,
  347. aftertouchValue & 0x7f);
  348. }
  349. bool MidiMessage::isChannelPressure() const noexcept
  350. {
  351. return (getRawData()[0] & 0xf0) == 0xd0;
  352. }
  353. int MidiMessage::getChannelPressureValue() const noexcept
  354. {
  355. jassert (isChannelPressure());
  356. return getRawData()[1];
  357. }
  358. MidiMessage MidiMessage::channelPressureChange (const int channel, const int pressure) noexcept
  359. {
  360. jassert (channel > 0 && channel <= 16); // valid channels are numbered 1 to 16
  361. jassert (isPositiveAndBelow (pressure, (int) 128));
  362. return MidiMessage (MidiHelpers::initialByte (0xd0, channel), pressure & 0x7f);
  363. }
  364. bool MidiMessage::isSustainPedalOn() const noexcept { return isControllerOfType (0x40) && getRawData()[2] >= 64; }
  365. bool MidiMessage::isSustainPedalOff() const noexcept { return isControllerOfType (0x40) && getRawData()[2] < 64; }
  366. bool MidiMessage::isSostenutoPedalOn() const noexcept { return isControllerOfType (0x42) && getRawData()[2] >= 64; }
  367. bool MidiMessage::isSostenutoPedalOff() const noexcept { return isControllerOfType (0x42) && getRawData()[2] < 64; }
  368. bool MidiMessage::isSoftPedalOn() const noexcept { return isControllerOfType (0x43) && getRawData()[2] >= 64; }
  369. bool MidiMessage::isSoftPedalOff() const noexcept { return isControllerOfType (0x43) && getRawData()[2] < 64; }
  370. bool MidiMessage::isProgramChange() const noexcept
  371. {
  372. return (getRawData()[0] & 0xf0) == 0xc0;
  373. }
  374. int MidiMessage::getProgramChangeNumber() const noexcept
  375. {
  376. jassert (isProgramChange());
  377. return getRawData()[1];
  378. }
  379. MidiMessage MidiMessage::programChange (const int channel, const int programNumber) noexcept
  380. {
  381. jassert (channel > 0 && channel <= 16); // valid channels are numbered 1 to 16
  382. return MidiMessage (MidiHelpers::initialByte (0xc0, channel), programNumber & 0x7f);
  383. }
  384. bool MidiMessage::isPitchWheel() const noexcept
  385. {
  386. return (getRawData()[0] & 0xf0) == 0xe0;
  387. }
  388. int MidiMessage::getPitchWheelValue() const noexcept
  389. {
  390. jassert (isPitchWheel());
  391. const uint8* const data = getRawData();
  392. return data[1] | (data[2] << 7);
  393. }
  394. MidiMessage MidiMessage::pitchWheel (const int channel, const int position) noexcept
  395. {
  396. jassert (channel > 0 && channel <= 16); // valid channels are numbered 1 to 16
  397. jassert (isPositiveAndBelow (position, (int) 0x4000));
  398. return MidiMessage (MidiHelpers::initialByte (0xe0, channel),
  399. position & 127, (position >> 7) & 127);
  400. }
  401. bool MidiMessage::isController() const noexcept
  402. {
  403. return (getRawData()[0] & 0xf0) == 0xb0;
  404. }
  405. bool MidiMessage::isControllerOfType (const int controllerType) const noexcept
  406. {
  407. const uint8* const data = getRawData();
  408. return (data[0] & 0xf0) == 0xb0 && data[1] == controllerType;
  409. }
  410. int MidiMessage::getControllerNumber() const noexcept
  411. {
  412. jassert (isController());
  413. return getRawData()[1];
  414. }
  415. int MidiMessage::getControllerValue() const noexcept
  416. {
  417. jassert (isController());
  418. return getRawData()[2];
  419. }
  420. MidiMessage MidiMessage::controllerEvent (const int channel, const int controllerType, const int value) noexcept
  421. {
  422. // the channel must be between 1 and 16 inclusive
  423. jassert (channel > 0 && channel <= 16);
  424. return MidiMessage (MidiHelpers::initialByte (0xb0, channel),
  425. controllerType & 127, value & 127);
  426. }
  427. MidiMessage MidiMessage::noteOn (const int channel, const int noteNumber, const uint8 velocity) noexcept
  428. {
  429. jassert (channel > 0 && channel <= 16);
  430. jassert (isPositiveAndBelow (noteNumber, (int) 128));
  431. return MidiMessage (MidiHelpers::initialByte (0x90, channel),
  432. noteNumber & 127, MidiHelpers::validVelocity (velocity));
  433. }
  434. MidiMessage MidiMessage::noteOn (const int channel, const int noteNumber, const float velocity) noexcept
  435. {
  436. return noteOn (channel, noteNumber, MidiHelpers::floatVelocityToByte (velocity));
  437. }
  438. MidiMessage MidiMessage::noteOff (const int channel, const int noteNumber, uint8 velocity) noexcept
  439. {
  440. jassert (channel > 0 && channel <= 16);
  441. jassert (isPositiveAndBelow (noteNumber, (int) 128));
  442. return MidiMessage (MidiHelpers::initialByte (0x80, channel),
  443. noteNumber & 127, MidiHelpers::validVelocity (velocity));
  444. }
  445. MidiMessage MidiMessage::noteOff (const int channel, const int noteNumber, float velocity) noexcept
  446. {
  447. return noteOff (channel, noteNumber, MidiHelpers::floatVelocityToByte (velocity));
  448. }
  449. MidiMessage MidiMessage::noteOff (const int channel, const int noteNumber) noexcept
  450. {
  451. jassert (channel > 0 && channel <= 16);
  452. jassert (isPositiveAndBelow (noteNumber, (int) 128));
  453. return MidiMessage (MidiHelpers::initialByte (0x80, channel), noteNumber & 127, 0);
  454. }
  455. MidiMessage MidiMessage::allNotesOff (const int channel) noexcept
  456. {
  457. return controllerEvent (channel, 123, 0);
  458. }
  459. bool MidiMessage::isAllNotesOff() const noexcept
  460. {
  461. const uint8* const data = getRawData();
  462. return (data[0] & 0xf0) == 0xb0 && data[1] == 123;
  463. }
  464. MidiMessage MidiMessage::allSoundOff (const int channel) noexcept
  465. {
  466. return controllerEvent (channel, 120, 0);
  467. }
  468. bool MidiMessage::isAllSoundOff() const noexcept
  469. {
  470. const uint8* const data = getRawData();
  471. return (data[0] & 0xf0) == 0xb0 && data[1] == 120;
  472. }
  473. MidiMessage MidiMessage::allControllersOff (const int channel) noexcept
  474. {
  475. return controllerEvent (channel, 121, 0);
  476. }
  477. MidiMessage MidiMessage::masterVolume (const float volume)
  478. {
  479. const int vol = jlimit (0, 0x3fff, roundToInt (volume * 0x4000));
  480. const uint8 buf[] = { 0xf0, 0x7f, 0x7f, 0x04, 0x01,
  481. (uint8) (vol & 0x7f),
  482. (uint8) (vol >> 7),
  483. 0xf7 };
  484. return MidiMessage (buf, 8);
  485. }
  486. //==============================================================================
  487. bool MidiMessage::isSysEx() const noexcept
  488. {
  489. return *getRawData() == 0xf0;
  490. }
  491. MidiMessage MidiMessage::createSysExMessage (const void* sysexData, const int dataSize)
  492. {
  493. HeapBlock<uint8> m ((size_t) dataSize + 2);
  494. m[0] = 0xf0;
  495. memcpy (m + 1, sysexData, (size_t) dataSize);
  496. m[dataSize + 1] = 0xf7;
  497. return MidiMessage (m, dataSize + 2);
  498. }
  499. const uint8* MidiMessage::getSysExData() const noexcept
  500. {
  501. return isSysEx() ? getRawData() + 1 : nullptr;
  502. }
  503. int MidiMessage::getSysExDataSize() const noexcept
  504. {
  505. return isSysEx() ? size - 2 : 0;
  506. }
  507. //==============================================================================
  508. bool MidiMessage::isMetaEvent() const noexcept { return *getRawData() == 0xff; }
  509. bool MidiMessage::isActiveSense() const noexcept { return *getRawData() == 0xfe; }
  510. int MidiMessage::getMetaEventType() const noexcept
  511. {
  512. const uint8* const data = getRawData();
  513. return *data != 0xff ? -1 : data[1];
  514. }
  515. int MidiMessage::getMetaEventLength() const noexcept
  516. {
  517. const uint8* const data = getRawData();
  518. if (*data == 0xff)
  519. {
  520. int n;
  521. return jmin (size - 2, readVariableLengthVal (data + 2, n));
  522. }
  523. return 0;
  524. }
  525. const uint8* MidiMessage::getMetaEventData() const noexcept
  526. {
  527. jassert (isMetaEvent());
  528. int n;
  529. const uint8* d = getRawData() + 2;
  530. readVariableLengthVal (d, n);
  531. return d + n;
  532. }
  533. bool MidiMessage::isTrackMetaEvent() const noexcept { return getMetaEventType() == 0; }
  534. bool MidiMessage::isEndOfTrackMetaEvent() const noexcept { return getMetaEventType() == 47; }
  535. bool MidiMessage::isTextMetaEvent() const noexcept
  536. {
  537. const int t = getMetaEventType();
  538. return t > 0 && t < 16;
  539. }
  540. String MidiMessage::getTextFromTextMetaEvent() const
  541. {
  542. const char* const textData = reinterpret_cast<const char*> (getMetaEventData());
  543. return String (CharPointer_UTF8 (textData),
  544. CharPointer_UTF8 (textData + getMetaEventLength()));
  545. }
  546. MidiMessage MidiMessage::textMetaEvent (int type, StringRef text)
  547. {
  548. jassert (type > 0 && type < 16);
  549. MidiMessage result;
  550. const size_t textSize = text.text.sizeInBytes() - 1;
  551. uint8 header[8];
  552. size_t n = sizeof (header);
  553. header[--n] = (uint8) (textSize & 0x7f);
  554. for (size_t i = textSize; (i >>= 7) != 0;)
  555. header[--n] = (uint8) ((i & 0x7f) | 0x80);
  556. header[--n] = (uint8) type;
  557. header[--n] = 0xff;
  558. const size_t headerLen = sizeof (header) - n;
  559. uint8* const dest = result.allocateSpace ((int) (headerLen + textSize));
  560. result.size = (int) (headerLen + textSize);
  561. memcpy (dest, header + n, headerLen);
  562. memcpy (dest + headerLen, text.text.getAddress(), textSize);
  563. return result;
  564. }
  565. bool MidiMessage::isTrackNameEvent() const noexcept { const uint8* data = getRawData(); return (data[1] == 3) && (*data == 0xff); }
  566. bool MidiMessage::isTempoMetaEvent() const noexcept { const uint8* data = getRawData(); return (data[1] == 81) && (*data == 0xff); }
  567. bool MidiMessage::isMidiChannelMetaEvent() const noexcept { const uint8* data = getRawData(); return (data[1] == 0x20) && (*data == 0xff) && (data[2] == 1); }
  568. int MidiMessage::getMidiChannelMetaEventChannel() const noexcept
  569. {
  570. jassert (isMidiChannelMetaEvent());
  571. return getRawData()[3] + 1;
  572. }
  573. double MidiMessage::getTempoSecondsPerQuarterNote() const noexcept
  574. {
  575. if (! isTempoMetaEvent())
  576. return 0.0;
  577. const uint8* const d = getMetaEventData();
  578. return (((unsigned int) d[0] << 16)
  579. | ((unsigned int) d[1] << 8)
  580. | d[2])
  581. / 1000000.0;
  582. }
  583. double MidiMessage::getTempoMetaEventTickLength (const short timeFormat) const noexcept
  584. {
  585. if (timeFormat > 0)
  586. {
  587. if (! isTempoMetaEvent())
  588. return 0.5 / timeFormat;
  589. return getTempoSecondsPerQuarterNote() / timeFormat;
  590. }
  591. else
  592. {
  593. const int frameCode = (-timeFormat) >> 8;
  594. double framesPerSecond;
  595. switch (frameCode)
  596. {
  597. case 24: framesPerSecond = 24.0; break;
  598. case 25: framesPerSecond = 25.0; break;
  599. case 29: framesPerSecond = 29.97; break;
  600. case 30: framesPerSecond = 30.0; break;
  601. default: framesPerSecond = 30.0; break;
  602. }
  603. return (1.0 / framesPerSecond) / (timeFormat & 0xff);
  604. }
  605. }
  606. MidiMessage MidiMessage::tempoMetaEvent (int microsecondsPerQuarterNote) noexcept
  607. {
  608. const uint8 d[] = { 0xff, 81, 3,
  609. (uint8) (microsecondsPerQuarterNote >> 16),
  610. (uint8) (microsecondsPerQuarterNote >> 8),
  611. (uint8) microsecondsPerQuarterNote };
  612. return MidiMessage (d, 6, 0.0);
  613. }
  614. bool MidiMessage::isTimeSignatureMetaEvent() const noexcept
  615. {
  616. const uint8* const data = getRawData();
  617. return (data[1] == 0x58) && (*data == (uint8) 0xff);
  618. }
  619. void MidiMessage::getTimeSignatureInfo (int& numerator, int& denominator) const noexcept
  620. {
  621. if (isTimeSignatureMetaEvent())
  622. {
  623. const uint8* const d = getMetaEventData();
  624. numerator = d[0];
  625. denominator = 1 << d[1];
  626. }
  627. else
  628. {
  629. numerator = 4;
  630. denominator = 4;
  631. }
  632. }
  633. MidiMessage MidiMessage::timeSignatureMetaEvent (const int numerator, const int denominator)
  634. {
  635. int n = 1;
  636. int powerOfTwo = 0;
  637. while (n < denominator)
  638. {
  639. n <<= 1;
  640. ++powerOfTwo;
  641. }
  642. const uint8 d[] = { 0xff, 0x58, 0x04, (uint8) numerator, (uint8) powerOfTwo, 1, 96 };
  643. return MidiMessage (d, 7, 0.0);
  644. }
  645. MidiMessage MidiMessage::midiChannelMetaEvent (const int channel) noexcept
  646. {
  647. const uint8 d[] = { 0xff, 0x20, 0x01, (uint8) jlimit (0, 0xff, channel - 1) };
  648. return MidiMessage (d, 4, 0.0);
  649. }
  650. bool MidiMessage::isKeySignatureMetaEvent() const noexcept
  651. {
  652. return getMetaEventType() == 0x59;
  653. }
  654. int MidiMessage::getKeySignatureNumberOfSharpsOrFlats() const noexcept
  655. {
  656. return (int) getMetaEventData()[0];
  657. }
  658. bool MidiMessage::isKeySignatureMajorKey() const noexcept
  659. {
  660. return getMetaEventData()[1] == 0;
  661. }
  662. MidiMessage MidiMessage::keySignatureMetaEvent (int numberOfSharpsOrFlats, bool isMinorKey)
  663. {
  664. jassert (numberOfSharpsOrFlats >= -7 && numberOfSharpsOrFlats <= 7);
  665. const uint8 d[] = { 0xff, 0x59, 0x02, (uint8) numberOfSharpsOrFlats, isMinorKey ? (uint8) 1 : (uint8) 0 };
  666. return MidiMessage (d, 5, 0.0);
  667. }
  668. MidiMessage MidiMessage::endOfTrack() noexcept
  669. {
  670. return MidiMessage (0xff, 0x2f, 0, 0.0);
  671. }
  672. //==============================================================================
  673. bool MidiMessage::isSongPositionPointer() const noexcept { return *getRawData() == 0xf2; }
  674. int MidiMessage::getSongPositionPointerMidiBeat() const noexcept { const uint8* data = getRawData(); return data[1] | (data[2] << 7); }
  675. MidiMessage MidiMessage::songPositionPointer (const int positionInMidiBeats) noexcept
  676. {
  677. return MidiMessage (0xf2,
  678. positionInMidiBeats & 127,
  679. (positionInMidiBeats >> 7) & 127);
  680. }
  681. bool MidiMessage::isMidiStart() const noexcept { return *getRawData() == 0xfa; }
  682. MidiMessage MidiMessage::midiStart() noexcept { return MidiMessage (0xfa); }
  683. bool MidiMessage::isMidiContinue() const noexcept { return *getRawData() == 0xfb; }
  684. MidiMessage MidiMessage::midiContinue() noexcept { return MidiMessage (0xfb); }
  685. bool MidiMessage::isMidiStop() const noexcept { return *getRawData() == 0xfc; }
  686. MidiMessage MidiMessage::midiStop() noexcept { return MidiMessage (0xfc); }
  687. bool MidiMessage::isMidiClock() const noexcept { return *getRawData() == 0xf8; }
  688. MidiMessage MidiMessage::midiClock() noexcept { return MidiMessage (0xf8); }
  689. bool MidiMessage::isQuarterFrame() const noexcept { return *getRawData() == 0xf1; }
  690. int MidiMessage::getQuarterFrameSequenceNumber() const noexcept { return ((int) getRawData()[1]) >> 4; }
  691. int MidiMessage::getQuarterFrameValue() const noexcept { return ((int) getRawData()[1]) & 0x0f; }
  692. MidiMessage MidiMessage::quarterFrame (const int sequenceNumber, const int value) noexcept
  693. {
  694. return MidiMessage (0xf1, (sequenceNumber << 4) | value);
  695. }
  696. bool MidiMessage::isFullFrame() const noexcept
  697. {
  698. const uint8* const data = getRawData();
  699. return data[0] == 0xf0
  700. && data[1] == 0x7f
  701. && size >= 10
  702. && data[3] == 0x01
  703. && data[4] == 0x01;
  704. }
  705. void MidiMessage::getFullFrameParameters (int& hours, int& minutes, int& seconds, int& frames,
  706. MidiMessage::SmpteTimecodeType& timecodeType) const noexcept
  707. {
  708. jassert (isFullFrame());
  709. const uint8* const data = getRawData();
  710. timecodeType = (SmpteTimecodeType) (data[5] >> 5);
  711. hours = data[5] & 0x1f;
  712. minutes = data[6];
  713. seconds = data[7];
  714. frames = data[8];
  715. }
  716. MidiMessage MidiMessage::fullFrame (const int hours, const int minutes,
  717. const int seconds, const int frames,
  718. MidiMessage::SmpteTimecodeType timecodeType)
  719. {
  720. const uint8 d[] = { 0xf0, 0x7f, 0x7f, 0x01, 0x01,
  721. (uint8) ((hours & 0x01f) | (timecodeType << 5)),
  722. (uint8) minutes,
  723. (uint8) seconds,
  724. (uint8) frames,
  725. 0xf7 };
  726. return MidiMessage (d, 10, 0.0);
  727. }
  728. bool MidiMessage::isMidiMachineControlMessage() const noexcept
  729. {
  730. const uint8* const data = getRawData();
  731. return data[0] == 0xf0
  732. && data[1] == 0x7f
  733. && data[3] == 0x06
  734. && size > 5;
  735. }
  736. MidiMessage::MidiMachineControlCommand MidiMessage::getMidiMachineControlCommand() const noexcept
  737. {
  738. jassert (isMidiMachineControlMessage());
  739. return (MidiMachineControlCommand) getRawData()[4];
  740. }
  741. MidiMessage MidiMessage::midiMachineControlCommand (MidiMessage::MidiMachineControlCommand command)
  742. {
  743. const uint8 d[] = { 0xf0, 0x7f, 0, 6, (uint8) command, 0xf7 };
  744. return MidiMessage (d, 6, 0.0);
  745. }
  746. //==============================================================================
  747. bool MidiMessage::isMidiMachineControlGoto (int& hours, int& minutes, int& seconds, int& frames) const noexcept
  748. {
  749. const uint8* const data = getRawData();
  750. if (size >= 12
  751. && data[0] == 0xf0
  752. && data[1] == 0x7f
  753. && data[3] == 0x06
  754. && data[4] == 0x44
  755. && data[5] == 0x06
  756. && data[6] == 0x01)
  757. {
  758. hours = data[7] % 24; // (that some machines send out hours > 24)
  759. minutes = data[8];
  760. seconds = data[9];
  761. frames = data[10];
  762. return true;
  763. }
  764. return false;
  765. }
  766. MidiMessage MidiMessage::midiMachineControlGoto (int hours, int minutes, int seconds, int frames)
  767. {
  768. const uint8 d[] = { 0xf0, 0x7f, 0, 6, 0x44, 6, 1,
  769. (uint8) hours,
  770. (uint8) minutes,
  771. (uint8) seconds,
  772. (uint8) frames,
  773. 0xf7 };
  774. return MidiMessage (d, 12, 0.0);
  775. }
  776. //==============================================================================
  777. String MidiMessage::getMidiNoteName (int note, bool useSharps, bool includeOctaveNumber, int octaveNumForMiddleC)
  778. {
  779. static const char* const sharpNoteNames[] = { "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B" };
  780. static const char* const flatNoteNames[] = { "C", "Db", "D", "Eb", "E", "F", "Gb", "G", "Ab", "A", "Bb", "B" };
  781. if (isPositiveAndBelow (note, (int) 128))
  782. {
  783. String s (useSharps ? sharpNoteNames [note % 12]
  784. : flatNoteNames [note % 12]);
  785. if (includeOctaveNumber)
  786. s << (note / 12 + (octaveNumForMiddleC - 5));
  787. return s;
  788. }
  789. return String();
  790. }
  791. double MidiMessage::getMidiNoteInHertz (int noteNumber, const double frequencyOfA) noexcept
  792. {
  793. return frequencyOfA * pow (2.0, (noteNumber - 69) / 12.0);
  794. }
  795. bool MidiMessage::isMidiNoteBlack (int noteNumber) noexcept
  796. {
  797. return ((1 << (noteNumber % 12)) & 0x054a) != 0;
  798. }
  799. const char* MidiMessage::getGMInstrumentName (const int n)
  800. {
  801. static const char* names[] =
  802. {
  803. NEEDS_TRANS("Acoustic Grand Piano"), NEEDS_TRANS("Bright Acoustic Piano"), NEEDS_TRANS("Electric Grand Piano"), NEEDS_TRANS("Honky-tonk Piano"),
  804. NEEDS_TRANS("Electric Piano 1"), NEEDS_TRANS("Electric Piano 2"), NEEDS_TRANS("Harpsichord"), NEEDS_TRANS("Clavinet"),
  805. NEEDS_TRANS("Celesta"), NEEDS_TRANS("Glockenspiel"), NEEDS_TRANS("Music Box"), NEEDS_TRANS("Vibraphone"),
  806. NEEDS_TRANS("Marimba"), NEEDS_TRANS("Xylophone"), NEEDS_TRANS("Tubular Bells"), NEEDS_TRANS("Dulcimer"),
  807. NEEDS_TRANS("Drawbar Organ"), NEEDS_TRANS("Percussive Organ"), NEEDS_TRANS("Rock Organ"), NEEDS_TRANS("Church Organ"),
  808. NEEDS_TRANS("Reed Organ"), NEEDS_TRANS("Accordion"), NEEDS_TRANS("Harmonica"), NEEDS_TRANS("Tango Accordion"),
  809. NEEDS_TRANS("Acoustic Guitar (nylon)"), NEEDS_TRANS("Acoustic Guitar (steel)"), NEEDS_TRANS("Electric Guitar (jazz)"), NEEDS_TRANS("Electric Guitar (clean)"),
  810. NEEDS_TRANS("Electric Guitar (mute)"), NEEDS_TRANS("Overdriven Guitar"), NEEDS_TRANS("Distortion Guitar"), NEEDS_TRANS("Guitar Harmonics"),
  811. NEEDS_TRANS("Acoustic Bass"), NEEDS_TRANS("Electric Bass (finger)"), NEEDS_TRANS("Electric Bass (pick)"), NEEDS_TRANS("Fretless Bass"),
  812. NEEDS_TRANS("Slap Bass 1"), NEEDS_TRANS("Slap Bass 2"), NEEDS_TRANS("Synth Bass 1"), NEEDS_TRANS("Synth Bass 2"),
  813. NEEDS_TRANS("Violin"), NEEDS_TRANS("Viola"), NEEDS_TRANS("Cello"), NEEDS_TRANS("Contrabass"),
  814. NEEDS_TRANS("Tremolo Strings"), NEEDS_TRANS("Pizzicato Strings"), NEEDS_TRANS("Orchestral Harp"), NEEDS_TRANS("Timpani"),
  815. NEEDS_TRANS("String Ensemble 1"), NEEDS_TRANS("String Ensemble 2"), NEEDS_TRANS("SynthStrings 1"), NEEDS_TRANS("SynthStrings 2"),
  816. NEEDS_TRANS("Choir Aahs"), NEEDS_TRANS("Voice Oohs"), NEEDS_TRANS("Synth Voice"), NEEDS_TRANS("Orchestra Hit"),
  817. NEEDS_TRANS("Trumpet"), NEEDS_TRANS("Trombone"), NEEDS_TRANS("Tuba"), NEEDS_TRANS("Muted Trumpet"),
  818. NEEDS_TRANS("French Horn"), NEEDS_TRANS("Brass Section"), NEEDS_TRANS("SynthBrass 1"), NEEDS_TRANS("SynthBrass 2"),
  819. NEEDS_TRANS("Soprano Sax"), NEEDS_TRANS("Alto Sax"), NEEDS_TRANS("Tenor Sax"), NEEDS_TRANS("Baritone Sax"),
  820. NEEDS_TRANS("Oboe"), NEEDS_TRANS("English Horn"), NEEDS_TRANS("Bassoon"), NEEDS_TRANS("Clarinet"),
  821. NEEDS_TRANS("Piccolo"), NEEDS_TRANS("Flute"), NEEDS_TRANS("Recorder"), NEEDS_TRANS("Pan Flute"),
  822. NEEDS_TRANS("Blown Bottle"), NEEDS_TRANS("Shakuhachi"), NEEDS_TRANS("Whistle"), NEEDS_TRANS("Ocarina"),
  823. NEEDS_TRANS("Lead 1 (square)"), NEEDS_TRANS("Lead 2 (sawtooth)"), NEEDS_TRANS("Lead 3 (calliope)"), NEEDS_TRANS("Lead 4 (chiff)"),
  824. NEEDS_TRANS("Lead 5 (charang)"), NEEDS_TRANS("Lead 6 (voice)"), NEEDS_TRANS("Lead 7 (fifths)"), NEEDS_TRANS("Lead 8 (bass+lead)"),
  825. NEEDS_TRANS("Pad 1 (new age)"), NEEDS_TRANS("Pad 2 (warm)"), NEEDS_TRANS("Pad 3 (polysynth)"), NEEDS_TRANS("Pad 4 (choir)"),
  826. NEEDS_TRANS("Pad 5 (bowed)"), NEEDS_TRANS("Pad 6 (metallic)"), NEEDS_TRANS("Pad 7 (halo)"), NEEDS_TRANS("Pad 8 (sweep)"),
  827. NEEDS_TRANS("FX 1 (rain)"), NEEDS_TRANS("FX 2 (soundtrack)"), NEEDS_TRANS("FX 3 (crystal)"), NEEDS_TRANS("FX 4 (atmosphere)"),
  828. NEEDS_TRANS("FX 5 (brightness)"), NEEDS_TRANS("FX 6 (goblins)"), NEEDS_TRANS("FX 7 (echoes)"), NEEDS_TRANS("FX 8 (sci-fi)"),
  829. NEEDS_TRANS("Sitar"), NEEDS_TRANS("Banjo"), NEEDS_TRANS("Shamisen"), NEEDS_TRANS("Koto"),
  830. NEEDS_TRANS("Kalimba"), NEEDS_TRANS("Bag pipe"), NEEDS_TRANS("Fiddle"), NEEDS_TRANS("Shanai"),
  831. NEEDS_TRANS("Tinkle Bell"), NEEDS_TRANS("Agogo"), NEEDS_TRANS("Steel Drums"), NEEDS_TRANS("Woodblock"),
  832. NEEDS_TRANS("Taiko Drum"), NEEDS_TRANS("Melodic Tom"), NEEDS_TRANS("Synth Drum"), NEEDS_TRANS("Reverse Cymbal"),
  833. NEEDS_TRANS("Guitar Fret Noise"), NEEDS_TRANS("Breath Noise"), NEEDS_TRANS("Seashore"), NEEDS_TRANS("Bird Tweet"),
  834. NEEDS_TRANS("Telephone Ring"), NEEDS_TRANS("Helicopter"), NEEDS_TRANS("Applause"), NEEDS_TRANS("Gunshot")
  835. };
  836. return isPositiveAndBelow (n, numElementsInArray (names)) ? names[n] : nullptr;
  837. }
  838. const char* MidiMessage::getGMInstrumentBankName (const int n)
  839. {
  840. static const char* names[] =
  841. {
  842. NEEDS_TRANS("Piano"), NEEDS_TRANS("Chromatic Percussion"), NEEDS_TRANS("Organ"), NEEDS_TRANS("Guitar"),
  843. NEEDS_TRANS("Bass"), NEEDS_TRANS("Strings"), NEEDS_TRANS("Ensemble"), NEEDS_TRANS("Brass"),
  844. NEEDS_TRANS("Reed"), NEEDS_TRANS("Pipe"), NEEDS_TRANS("Synth Lead"), NEEDS_TRANS("Synth Pad"),
  845. NEEDS_TRANS("Synth Effects"), NEEDS_TRANS("Ethnic"), NEEDS_TRANS("Percussive"), NEEDS_TRANS("Sound Effects")
  846. };
  847. return isPositiveAndBelow (n, numElementsInArray (names)) ? names[n] : nullptr;
  848. }
  849. const char* MidiMessage::getRhythmInstrumentName (const int n)
  850. {
  851. static const char* names[] =
  852. {
  853. NEEDS_TRANS("Acoustic Bass Drum"), NEEDS_TRANS("Bass Drum 1"), NEEDS_TRANS("Side Stick"), NEEDS_TRANS("Acoustic Snare"),
  854. NEEDS_TRANS("Hand Clap"), NEEDS_TRANS("Electric Snare"), NEEDS_TRANS("Low Floor Tom"), NEEDS_TRANS("Closed Hi-Hat"),
  855. NEEDS_TRANS("High Floor Tom"), NEEDS_TRANS("Pedal Hi-Hat"), NEEDS_TRANS("Low Tom"), NEEDS_TRANS("Open Hi-Hat"),
  856. NEEDS_TRANS("Low-Mid Tom"), NEEDS_TRANS("Hi-Mid Tom"), NEEDS_TRANS("Crash Cymbal 1"), NEEDS_TRANS("High Tom"),
  857. NEEDS_TRANS("Ride Cymbal 1"), NEEDS_TRANS("Chinese Cymbal"), NEEDS_TRANS("Ride Bell"), NEEDS_TRANS("Tambourine"),
  858. NEEDS_TRANS("Splash Cymbal"), NEEDS_TRANS("Cowbell"), NEEDS_TRANS("Crash Cymbal 2"), NEEDS_TRANS("Vibraslap"),
  859. NEEDS_TRANS("Ride Cymbal 2"), NEEDS_TRANS("Hi Bongo"), NEEDS_TRANS("Low Bongo"), NEEDS_TRANS("Mute Hi Conga"),
  860. NEEDS_TRANS("Open Hi Conga"), NEEDS_TRANS("Low Conga"), NEEDS_TRANS("High Timbale"), NEEDS_TRANS("Low Timbale"),
  861. NEEDS_TRANS("High Agogo"), NEEDS_TRANS("Low Agogo"), NEEDS_TRANS("Cabasa"), NEEDS_TRANS("Maracas"),
  862. NEEDS_TRANS("Short Whistle"), NEEDS_TRANS("Long Whistle"), NEEDS_TRANS("Short Guiro"), NEEDS_TRANS("Long Guiro"),
  863. NEEDS_TRANS("Claves"), NEEDS_TRANS("Hi Wood Block"), NEEDS_TRANS("Low Wood Block"), NEEDS_TRANS("Mute Cuica"),
  864. NEEDS_TRANS("Open Cuica"), NEEDS_TRANS("Mute Triangle"), NEEDS_TRANS("Open Triangle")
  865. };
  866. return (n >= 35 && n <= 81) ? names [n - 35] : nullptr;
  867. }
  868. const char* MidiMessage::getControllerName (const int n)
  869. {
  870. static const char* names[] =
  871. {
  872. NEEDS_TRANS("Bank Select"), NEEDS_TRANS("Modulation Wheel (coarse)"), NEEDS_TRANS("Breath controller (coarse)"),
  873. nullptr,
  874. NEEDS_TRANS("Foot Pedal (coarse)"), NEEDS_TRANS("Portamento Time (coarse)"), NEEDS_TRANS("Data Entry (coarse)"),
  875. NEEDS_TRANS("Volume (coarse)"), NEEDS_TRANS("Balance (coarse)"),
  876. nullptr,
  877. NEEDS_TRANS("Pan position (coarse)"), NEEDS_TRANS("Expression (coarse)"), NEEDS_TRANS("Effect Control 1 (coarse)"),
  878. NEEDS_TRANS("Effect Control 2 (coarse)"),
  879. nullptr, nullptr,
  880. NEEDS_TRANS("General Purpose Slider 1"), NEEDS_TRANS("General Purpose Slider 2"),
  881. NEEDS_TRANS("General Purpose Slider 3"), NEEDS_TRANS("General Purpose Slider 4"),
  882. nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
  883. NEEDS_TRANS("Bank Select (fine)"), NEEDS_TRANS("Modulation Wheel (fine)"), NEEDS_TRANS("Breath controller (fine)"),
  884. nullptr,
  885. NEEDS_TRANS("Foot Pedal (fine)"), NEEDS_TRANS("Portamento Time (fine)"), NEEDS_TRANS("Data Entry (fine)"), NEEDS_TRANS("Volume (fine)"),
  886. NEEDS_TRANS("Balance (fine)"), nullptr, NEEDS_TRANS("Pan position (fine)"), NEEDS_TRANS("Expression (fine)"),
  887. NEEDS_TRANS("Effect Control 1 (fine)"), NEEDS_TRANS("Effect Control 2 (fine)"),
  888. nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
  889. nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
  890. NEEDS_TRANS("Hold Pedal (on/off)"), NEEDS_TRANS("Portamento (on/off)"), NEEDS_TRANS("Sustenuto Pedal (on/off)"), NEEDS_TRANS("Soft Pedal (on/off)"),
  891. NEEDS_TRANS("Legato Pedal (on/off)"), NEEDS_TRANS("Hold 2 Pedal (on/off)"), NEEDS_TRANS("Sound Variation"), NEEDS_TRANS("Sound Timbre"),
  892. NEEDS_TRANS("Sound Release Time"), NEEDS_TRANS("Sound Attack Time"), NEEDS_TRANS("Sound Brightness"), NEEDS_TRANS("Sound Control 6"),
  893. NEEDS_TRANS("Sound Control 7"), NEEDS_TRANS("Sound Control 8"), NEEDS_TRANS("Sound Control 9"), NEEDS_TRANS("Sound Control 10"),
  894. NEEDS_TRANS("General Purpose Button 1 (on/off)"), NEEDS_TRANS("General Purpose Button 2 (on/off)"),
  895. NEEDS_TRANS("General Purpose Button 3 (on/off)"), NEEDS_TRANS("General Purpose Button 4 (on/off)"),
  896. nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
  897. NEEDS_TRANS("Reverb Level"), NEEDS_TRANS("Tremolo Level"), NEEDS_TRANS("Chorus Level"), NEEDS_TRANS("Celeste Level"),
  898. NEEDS_TRANS("Phaser Level"), NEEDS_TRANS("Data Button increment"), NEEDS_TRANS("Data Button decrement"), NEEDS_TRANS("Non-registered Parameter (fine)"),
  899. NEEDS_TRANS("Non-registered Parameter (coarse)"), NEEDS_TRANS("Registered Parameter (fine)"), NEEDS_TRANS("Registered Parameter (coarse)"),
  900. nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
  901. nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
  902. NEEDS_TRANS("All Sound Off"), NEEDS_TRANS("All Controllers Off"), NEEDS_TRANS("Local Keyboard (on/off)"), NEEDS_TRANS("All Notes Off"),
  903. NEEDS_TRANS("Omni Mode Off"), NEEDS_TRANS("Omni Mode On"), NEEDS_TRANS("Mono Operation"), NEEDS_TRANS("Poly Operation")
  904. };
  905. return isPositiveAndBelow (n, numElementsInArray (names)) ? names[n] : nullptr;
  906. }