The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

1149 lines
44KB

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