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
43KB

  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. }
  28. //==============================================================================
  29. uint8 MidiMessage::floatValueToMidiByte (const float v) noexcept
  30. {
  31. return MidiHelpers::validVelocity (roundToInt (v * 127.0f));
  32. }
  33. uint16 MidiMessage::pitchbendToPitchwheelPos (const float pitchbend,
  34. const float pitchbendRange) noexcept
  35. {
  36. // can't translate a pitchbend value that is outside of the given range!
  37. jassert (std::abs (pitchbend) <= pitchbendRange);
  38. return pitchbend > 0.0f ? jmap (pitchbend, 0.0f, pitchbendRange, 8192.0f, 16383.0f)
  39. : jmap (pitchbend, -pitchbendRange, 0.0f, 0.0f, 8192.0f);
  40. }
  41. //==============================================================================
  42. int MidiMessage::readVariableLengthVal (const uint8* data, int& numBytesUsed) noexcept
  43. {
  44. numBytesUsed = 0;
  45. int v = 0, i;
  46. do
  47. {
  48. i = (int) *data++;
  49. if (++numBytesUsed > 6)
  50. break;
  51. v = (v << 7) + (i & 0x7f);
  52. } while (i & 0x80);
  53. return v;
  54. }
  55. int MidiMessage::getMessageLengthFromFirstByte (const uint8 firstByte) noexcept
  56. {
  57. // this method only works for valid starting bytes of a short midi message
  58. jassert (firstByte >= 0x80 && firstByte != 0xf0 && firstByte != 0xf7);
  59. static const char messageLengths[] =
  60. {
  61. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  62. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  63. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  64. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  65. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  66. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  67. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  68. 1, 2, 3, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
  69. };
  70. return messageLengths [firstByte & 0x7f];
  71. }
  72. //==============================================================================
  73. MidiMessage::MidiMessage() noexcept
  74. : timeStamp (0), size (2)
  75. {
  76. preallocatedData.asBytes[0] = 0xf0;
  77. preallocatedData.asBytes[1] = 0xf7;
  78. }
  79. MidiMessage::MidiMessage (const void* const d, const int dataSize, const double t)
  80. : timeStamp (t),
  81. size (dataSize)
  82. {
  83. jassert (dataSize > 0);
  84. memcpy (allocateSpace (dataSize), d, (size_t) dataSize);
  85. // check that the length matches the data..
  86. jassert (size > 3 || *(uint8*)d >= 0xf0 || getMessageLengthFromFirstByte (*(uint8*)d) == size);
  87. }
  88. MidiMessage::MidiMessage (const int byte1, const double t) noexcept
  89. : timeStamp (t), size (1)
  90. {
  91. preallocatedData.asBytes[0] = (uint8) byte1;
  92. // check that the length matches the data..
  93. jassert (byte1 >= 0xf0 || getMessageLengthFromFirstByte ((uint8) byte1) == 1);
  94. }
  95. MidiMessage::MidiMessage (const int byte1, const int byte2, const double t) noexcept
  96. : timeStamp (t), size (2)
  97. {
  98. preallocatedData.asBytes[0] = (uint8) byte1;
  99. preallocatedData.asBytes[1] = (uint8) byte2;
  100. // check that the length matches the data..
  101. jassert (byte1 >= 0xf0 || getMessageLengthFromFirstByte ((uint8) byte1) == 2);
  102. }
  103. MidiMessage::MidiMessage (const int byte1, const int byte2, const int byte3, const double t) noexcept
  104. : timeStamp (t), size (3)
  105. {
  106. preallocatedData.asBytes[0] = (uint8) byte1;
  107. preallocatedData.asBytes[1] = (uint8) byte2;
  108. preallocatedData.asBytes[2] = (uint8) byte3;
  109. // check that the length matches the data..
  110. jassert (byte1 >= 0xf0 || getMessageLengthFromFirstByte ((uint8) byte1) == 3);
  111. }
  112. MidiMessage::MidiMessage (const MidiMessage& other)
  113. : timeStamp (other.timeStamp), size (other.size)
  114. {
  115. if (other.allocatedData != nullptr)
  116. {
  117. allocatedData.malloc ((size_t) size);
  118. memcpy (allocatedData, other.allocatedData, (size_t) size);
  119. }
  120. else
  121. {
  122. preallocatedData.asInt32 = other.preallocatedData.asInt32;
  123. }
  124. }
  125. MidiMessage::MidiMessage (const MidiMessage& other, const double newTimeStamp)
  126. : timeStamp (newTimeStamp), size (other.size)
  127. {
  128. if (other.allocatedData != nullptr)
  129. {
  130. allocatedData.malloc ((size_t) size);
  131. memcpy (allocatedData, other.allocatedData, (size_t) size);
  132. }
  133. else
  134. {
  135. preallocatedData.asInt32 = other.preallocatedData.asInt32;
  136. }
  137. }
  138. MidiMessage::MidiMessage (const void* srcData, int sz, int& numBytesUsed, const uint8 lastStatusByte,
  139. double t, bool sysexHasEmbeddedLength)
  140. : timeStamp (t)
  141. {
  142. const uint8* src = static_cast<const uint8*> (srcData);
  143. unsigned int byte = (unsigned int) *src;
  144. if (byte < 0x80)
  145. {
  146. byte = (unsigned int) (uint8) lastStatusByte;
  147. numBytesUsed = -1;
  148. }
  149. else
  150. {
  151. numBytesUsed = 0;
  152. --sz;
  153. ++src;
  154. }
  155. if (byte >= 0x80)
  156. {
  157. if (byte == 0xf0)
  158. {
  159. const uint8* d = src;
  160. bool haveReadAllLengthBytes = ! sysexHasEmbeddedLength;
  161. int numVariableLengthSysexBytes = 0;
  162. while (d < src + sz)
  163. {
  164. if (*d >= 0x80)
  165. {
  166. if (*d == 0xf7)
  167. {
  168. ++d; // include the trailing 0xf7 when we hit it
  169. break;
  170. }
  171. if (haveReadAllLengthBytes) // if we see a 0x80 bit set after the initial data length
  172. break; // bytes, assume it's the end of the sysex
  173. ++numVariableLengthSysexBytes;
  174. }
  175. else if (! haveReadAllLengthBytes)
  176. {
  177. haveReadAllLengthBytes = true;
  178. ++numVariableLengthSysexBytes;
  179. }
  180. ++d;
  181. }
  182. src += numVariableLengthSysexBytes;
  183. size = 1 + (int) (d - src);
  184. uint8* dest = allocateSpace (size);
  185. *dest = (uint8) byte;
  186. memcpy (dest + 1, src, (size_t) (size - 1));
  187. numBytesUsed += numVariableLengthSysexBytes; // (these aren't counted in the size)
  188. }
  189. else if (byte == 0xff)
  190. {
  191. int n;
  192. const int bytesLeft = readVariableLengthVal (src + 1, n);
  193. size = jmin (sz + 1, n + 2 + bytesLeft);
  194. uint8* dest = allocateSpace (size);
  195. *dest = (uint8) byte;
  196. memcpy (dest + 1, src, (size_t) size - 1);
  197. }
  198. else
  199. {
  200. preallocatedData.asInt32 = 0;
  201. size = getMessageLengthFromFirstByte ((uint8) byte);
  202. preallocatedData.asBytes[0] = (uint8) byte;
  203. if (size > 1)
  204. {
  205. preallocatedData.asBytes[1] = src[0];
  206. if (size > 2)
  207. preallocatedData.asBytes[2] = src[1];
  208. }
  209. }
  210. numBytesUsed += size;
  211. }
  212. else
  213. {
  214. preallocatedData.asInt32 = 0;
  215. size = 0;
  216. }
  217. }
  218. MidiMessage& MidiMessage::operator= (const MidiMessage& other)
  219. {
  220. if (this != &other)
  221. {
  222. timeStamp = other.timeStamp;
  223. size = other.size;
  224. if (other.allocatedData != nullptr)
  225. {
  226. allocatedData.malloc ((size_t) size);
  227. memcpy (allocatedData, other.allocatedData, (size_t) size);
  228. }
  229. else
  230. {
  231. allocatedData.free();
  232. preallocatedData.asInt32 = other.preallocatedData.asInt32;
  233. }
  234. }
  235. return *this;
  236. }
  237. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  238. MidiMessage::MidiMessage (MidiMessage&& other) noexcept
  239. : timeStamp (other.timeStamp), size (other.size)
  240. {
  241. if (other.allocatedData != nullptr)
  242. allocatedData.swapWith (other.allocatedData);
  243. else
  244. preallocatedData.asInt32 = other.preallocatedData.asInt32;
  245. }
  246. MidiMessage& MidiMessage::operator= (MidiMessage&& other) noexcept
  247. {
  248. jassert (this != &other); // shouldn't be possible
  249. timeStamp = other.timeStamp;
  250. size = other.size;
  251. allocatedData.swapWith (other.allocatedData);
  252. preallocatedData.asInt32 = other.preallocatedData.asInt32;
  253. return *this;
  254. }
  255. #endif
  256. MidiMessage::~MidiMessage() {}
  257. uint8* MidiMessage::allocateSpace (int bytes)
  258. {
  259. if (bytes > 4)
  260. {
  261. allocatedData.malloc ((size_t) bytes);
  262. return allocatedData;
  263. }
  264. return preallocatedData.asBytes;
  265. }
  266. String MidiMessage::getDescription() const
  267. {
  268. if (isNoteOn()) return "Note on " + MidiMessage::getMidiNoteName (getNoteNumber(), true, true, 3) + " Velocity " + String (getVelocity()) + " Channel " + String (getChannel());
  269. if (isNoteOff()) return "Note off " + MidiMessage::getMidiNoteName (getNoteNumber(), true, true, 3) + " Velocity " + String (getVelocity()) + " Channel " + String (getChannel());
  270. if (isProgramChange()) return "Program change " + String (getProgramChangeNumber()) + " Channel " + String (getChannel());
  271. if (isPitchWheel()) return "Pitch wheel " + String (getPitchWheelValue()) + " Channel " + String (getChannel());
  272. if (isAftertouch()) return "Aftertouch " + MidiMessage::getMidiNoteName (getNoteNumber(), true, true, 3) + ": " + String (getAfterTouchValue()) + " Channel " + String (getChannel());
  273. if (isChannelPressure()) return "Channel pressure " + String (getChannelPressureValue()) + " Channel " + String (getChannel());
  274. if (isAllNotesOff()) return "All notes off Channel " + String (getChannel());
  275. if (isAllSoundOff()) return "All sound off Channel " + String (getChannel());
  276. if (isMetaEvent()) return "Meta event";
  277. if (isController())
  278. {
  279. String name (MidiMessage::getControllerName (getControllerNumber()));
  280. if (name.isEmpty())
  281. name = String (getControllerNumber());
  282. return "Controller " + name + ": " + String (getControllerValue()) + " Channel " + String (getChannel());
  283. }
  284. return String::toHexString (getRawData(), getRawDataSize());
  285. }
  286. int MidiMessage::getChannel() const noexcept
  287. {
  288. const uint8* const data = getRawData();
  289. if ((data[0] & 0xf0) != 0xf0)
  290. return (data[0] & 0xf) + 1;
  291. return 0;
  292. }
  293. bool MidiMessage::isForChannel (const int channel) const noexcept
  294. {
  295. jassert (channel > 0 && channel <= 16); // valid channels are numbered 1 to 16
  296. const uint8* const data = getRawData();
  297. return ((data[0] & 0xf) == channel - 1)
  298. && ((data[0] & 0xf0) != 0xf0);
  299. }
  300. void MidiMessage::setChannel (const int channel) noexcept
  301. {
  302. jassert (channel > 0 && channel <= 16); // valid channels are numbered 1 to 16
  303. uint8* const data = getData();
  304. if ((data[0] & 0xf0) != (uint8) 0xf0)
  305. data[0] = (uint8) ((data[0] & (uint8) 0xf0)
  306. | (uint8)(channel - 1));
  307. }
  308. bool MidiMessage::isNoteOn (const bool returnTrueForVelocity0) const noexcept
  309. {
  310. const uint8* const data = getRawData();
  311. return ((data[0] & 0xf0) == 0x90)
  312. && (returnTrueForVelocity0 || data[2] != 0);
  313. }
  314. bool MidiMessage::isNoteOff (const bool returnTrueForNoteOnVelocity0) const noexcept
  315. {
  316. const uint8* const data = getRawData();
  317. return ((data[0] & 0xf0) == 0x80)
  318. || (returnTrueForNoteOnVelocity0 && (data[2] == 0) && ((data[0] & 0xf0) == 0x90));
  319. }
  320. bool MidiMessage::isNoteOnOrOff() const noexcept
  321. {
  322. const uint8* const data = getRawData();
  323. const int d = data[0] & 0xf0;
  324. return (d == 0x90) || (d == 0x80);
  325. }
  326. int MidiMessage::getNoteNumber() const noexcept
  327. {
  328. return getRawData()[1];
  329. }
  330. void MidiMessage::setNoteNumber (const int newNoteNumber) noexcept
  331. {
  332. if (isNoteOnOrOff() || isAftertouch())
  333. getData()[1] = (uint8) (newNoteNumber & 127);
  334. }
  335. uint8 MidiMessage::getVelocity() const noexcept
  336. {
  337. if (isNoteOnOrOff())
  338. return getRawData()[2];
  339. return 0;
  340. }
  341. float MidiMessage::getFloatVelocity() const noexcept
  342. {
  343. return getVelocity() * (1.0f / 127.0f);
  344. }
  345. void MidiMessage::setVelocity (const float newVelocity) noexcept
  346. {
  347. if (isNoteOnOrOff())
  348. getData()[2] = floatValueToMidiByte (newVelocity);
  349. }
  350. void MidiMessage::multiplyVelocity (const float scaleFactor) noexcept
  351. {
  352. if (isNoteOnOrOff())
  353. {
  354. uint8* const data = getData();
  355. data[2] = MidiHelpers::validVelocity (roundToInt (scaleFactor * data[2]));
  356. }
  357. }
  358. bool MidiMessage::isAftertouch() const noexcept
  359. {
  360. return (getRawData()[0] & 0xf0) == 0xa0;
  361. }
  362. int MidiMessage::getAfterTouchValue() const noexcept
  363. {
  364. jassert (isAftertouch());
  365. return getRawData()[2];
  366. }
  367. MidiMessage MidiMessage::aftertouchChange (const int channel,
  368. const int noteNum,
  369. const int aftertouchValue) noexcept
  370. {
  371. jassert (channel > 0 && channel <= 16); // valid channels are numbered 1 to 16
  372. jassert (isPositiveAndBelow (noteNum, (int) 128));
  373. jassert (isPositiveAndBelow (aftertouchValue, (int) 128));
  374. return MidiMessage (MidiHelpers::initialByte (0xa0, channel),
  375. noteNum & 0x7f,
  376. aftertouchValue & 0x7f);
  377. }
  378. bool MidiMessage::isChannelPressure() const noexcept
  379. {
  380. return (getRawData()[0] & 0xf0) == 0xd0;
  381. }
  382. int MidiMessage::getChannelPressureValue() const noexcept
  383. {
  384. jassert (isChannelPressure());
  385. return getRawData()[1];
  386. }
  387. MidiMessage MidiMessage::channelPressureChange (const int channel, const int pressure) noexcept
  388. {
  389. jassert (channel > 0 && channel <= 16); // valid channels are numbered 1 to 16
  390. jassert (isPositiveAndBelow (pressure, (int) 128));
  391. return MidiMessage (MidiHelpers::initialByte (0xd0, channel), pressure & 0x7f);
  392. }
  393. bool MidiMessage::isSustainPedalOn() const noexcept { return isControllerOfType (0x40) && getRawData()[2] >= 64; }
  394. bool MidiMessage::isSustainPedalOff() const noexcept { return isControllerOfType (0x40) && getRawData()[2] < 64; }
  395. bool MidiMessage::isSostenutoPedalOn() const noexcept { return isControllerOfType (0x42) && getRawData()[2] >= 64; }
  396. bool MidiMessage::isSostenutoPedalOff() const noexcept { return isControllerOfType (0x42) && getRawData()[2] < 64; }
  397. bool MidiMessage::isSoftPedalOn() const noexcept { return isControllerOfType (0x43) && getRawData()[2] >= 64; }
  398. bool MidiMessage::isSoftPedalOff() const noexcept { return isControllerOfType (0x43) && getRawData()[2] < 64; }
  399. bool MidiMessage::isProgramChange() const noexcept
  400. {
  401. return (getRawData()[0] & 0xf0) == 0xc0;
  402. }
  403. int MidiMessage::getProgramChangeNumber() const noexcept
  404. {
  405. jassert (isProgramChange());
  406. return getRawData()[1];
  407. }
  408. MidiMessage MidiMessage::programChange (const int channel, const int programNumber) noexcept
  409. {
  410. jassert (channel > 0 && channel <= 16); // valid channels are numbered 1 to 16
  411. return MidiMessage (MidiHelpers::initialByte (0xc0, channel), programNumber & 0x7f);
  412. }
  413. bool MidiMessage::isPitchWheel() const noexcept
  414. {
  415. return (getRawData()[0] & 0xf0) == 0xe0;
  416. }
  417. int MidiMessage::getPitchWheelValue() const noexcept
  418. {
  419. jassert (isPitchWheel());
  420. const uint8* const data = getRawData();
  421. return data[1] | (data[2] << 7);
  422. }
  423. MidiMessage MidiMessage::pitchWheel (const int channel, const int position) noexcept
  424. {
  425. jassert (channel > 0 && channel <= 16); // valid channels are numbered 1 to 16
  426. jassert (isPositiveAndBelow (position, (int) 0x4000));
  427. return MidiMessage (MidiHelpers::initialByte (0xe0, channel),
  428. position & 127, (position >> 7) & 127);
  429. }
  430. bool MidiMessage::isController() const noexcept
  431. {
  432. return (getRawData()[0] & 0xf0) == 0xb0;
  433. }
  434. bool MidiMessage::isControllerOfType (const int controllerType) const noexcept
  435. {
  436. const uint8* const data = getRawData();
  437. return (data[0] & 0xf0) == 0xb0 && data[1] == controllerType;
  438. }
  439. int MidiMessage::getControllerNumber() const noexcept
  440. {
  441. jassert (isController());
  442. return getRawData()[1];
  443. }
  444. int MidiMessage::getControllerValue() const noexcept
  445. {
  446. jassert (isController());
  447. return getRawData()[2];
  448. }
  449. MidiMessage MidiMessage::controllerEvent (const int channel, const int controllerType, const int value) noexcept
  450. {
  451. // the channel must be between 1 and 16 inclusive
  452. jassert (channel > 0 && channel <= 16);
  453. return MidiMessage (MidiHelpers::initialByte (0xb0, channel),
  454. controllerType & 127, value & 127);
  455. }
  456. MidiMessage MidiMessage::noteOn (const int channel, const int noteNumber, const uint8 velocity) noexcept
  457. {
  458. jassert (channel > 0 && channel <= 16);
  459. jassert (isPositiveAndBelow (noteNumber, (int) 128));
  460. return MidiMessage (MidiHelpers::initialByte (0x90, channel),
  461. noteNumber & 127, MidiHelpers::validVelocity (velocity));
  462. }
  463. MidiMessage MidiMessage::noteOn (const int channel, const int noteNumber, const float velocity) noexcept
  464. {
  465. return noteOn (channel, noteNumber, floatValueToMidiByte (velocity));
  466. }
  467. MidiMessage MidiMessage::noteOff (const int channel, const int noteNumber, uint8 velocity) noexcept
  468. {
  469. jassert (channel > 0 && channel <= 16);
  470. jassert (isPositiveAndBelow (noteNumber, (int) 128));
  471. return MidiMessage (MidiHelpers::initialByte (0x80, channel),
  472. noteNumber & 127, MidiHelpers::validVelocity (velocity));
  473. }
  474. MidiMessage MidiMessage::noteOff (const int channel, const int noteNumber, float velocity) noexcept
  475. {
  476. return noteOff (channel, noteNumber, floatValueToMidiByte (velocity));
  477. }
  478. MidiMessage MidiMessage::noteOff (const int channel, const int noteNumber) noexcept
  479. {
  480. jassert (channel > 0 && channel <= 16);
  481. jassert (isPositiveAndBelow (noteNumber, (int) 128));
  482. return MidiMessage (MidiHelpers::initialByte (0x80, channel), noteNumber & 127, 0);
  483. }
  484. MidiMessage MidiMessage::allNotesOff (const int channel) noexcept
  485. {
  486. return controllerEvent (channel, 123, 0);
  487. }
  488. bool MidiMessage::isAllNotesOff() const noexcept
  489. {
  490. const uint8* const data = getRawData();
  491. return (data[0] & 0xf0) == 0xb0 && data[1] == 123;
  492. }
  493. MidiMessage MidiMessage::allSoundOff (const int channel) noexcept
  494. {
  495. return controllerEvent (channel, 120, 0);
  496. }
  497. bool MidiMessage::isAllSoundOff() const noexcept
  498. {
  499. const uint8* const data = getRawData();
  500. return (data[0] & 0xf0) == 0xb0 && data[1] == 120;
  501. }
  502. MidiMessage MidiMessage::allControllersOff (const int channel) noexcept
  503. {
  504. return controllerEvent (channel, 121, 0);
  505. }
  506. MidiMessage MidiMessage::masterVolume (const float volume)
  507. {
  508. const int vol = jlimit (0, 0x3fff, roundToInt (volume * 0x4000));
  509. const uint8 buf[] = { 0xf0, 0x7f, 0x7f, 0x04, 0x01,
  510. (uint8) (vol & 0x7f),
  511. (uint8) (vol >> 7),
  512. 0xf7 };
  513. return MidiMessage (buf, 8);
  514. }
  515. //==============================================================================
  516. bool MidiMessage::isSysEx() const noexcept
  517. {
  518. return *getRawData() == 0xf0;
  519. }
  520. MidiMessage MidiMessage::createSysExMessage (const void* sysexData, const int dataSize)
  521. {
  522. HeapBlock<uint8> m ((size_t) dataSize + 2);
  523. m[0] = 0xf0;
  524. memcpy (m + 1, sysexData, (size_t) dataSize);
  525. m[dataSize + 1] = 0xf7;
  526. return MidiMessage (m, dataSize + 2);
  527. }
  528. const uint8* MidiMessage::getSysExData() const noexcept
  529. {
  530. return isSysEx() ? getRawData() + 1 : nullptr;
  531. }
  532. int MidiMessage::getSysExDataSize() const noexcept
  533. {
  534. return isSysEx() ? size - 2 : 0;
  535. }
  536. //==============================================================================
  537. bool MidiMessage::isMetaEvent() const noexcept { return *getRawData() == 0xff; }
  538. bool MidiMessage::isActiveSense() const noexcept { return *getRawData() == 0xfe; }
  539. int MidiMessage::getMetaEventType() const noexcept
  540. {
  541. const uint8* const data = getRawData();
  542. return *data != 0xff ? -1 : data[1];
  543. }
  544. int MidiMessage::getMetaEventLength() const noexcept
  545. {
  546. const uint8* const data = getRawData();
  547. if (*data == 0xff)
  548. {
  549. int n;
  550. return jmin (size - 2, readVariableLengthVal (data + 2, n));
  551. }
  552. return 0;
  553. }
  554. const uint8* MidiMessage::getMetaEventData() const noexcept
  555. {
  556. jassert (isMetaEvent());
  557. int n;
  558. const uint8* d = getRawData() + 2;
  559. readVariableLengthVal (d, n);
  560. return d + n;
  561. }
  562. bool MidiMessage::isTrackMetaEvent() const noexcept { return getMetaEventType() == 0; }
  563. bool MidiMessage::isEndOfTrackMetaEvent() const noexcept { return getMetaEventType() == 47; }
  564. bool MidiMessage::isTextMetaEvent() const noexcept
  565. {
  566. const int t = getMetaEventType();
  567. return t > 0 && t < 16;
  568. }
  569. String MidiMessage::getTextFromTextMetaEvent() const
  570. {
  571. const char* const textData = reinterpret_cast<const char*> (getMetaEventData());
  572. return String (CharPointer_UTF8 (textData),
  573. CharPointer_UTF8 (textData + getMetaEventLength()));
  574. }
  575. MidiMessage MidiMessage::textMetaEvent (int type, StringRef text)
  576. {
  577. jassert (type > 0 && type < 16);
  578. MidiMessage result;
  579. const size_t textSize = text.text.sizeInBytes() - 1;
  580. uint8 header[8];
  581. size_t n = sizeof (header);
  582. header[--n] = (uint8) (textSize & 0x7f);
  583. for (size_t i = textSize; (i >>= 7) != 0;)
  584. header[--n] = (uint8) ((i & 0x7f) | 0x80);
  585. header[--n] = (uint8) type;
  586. header[--n] = 0xff;
  587. const size_t headerLen = sizeof (header) - n;
  588. uint8* const dest = result.allocateSpace ((int) (headerLen + textSize));
  589. result.size = (int) (headerLen + textSize);
  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 = 29.97; 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. }