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.

1151 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. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  237. MidiMessage::MidiMessage (MidiMessage&& other) noexcept
  238. : timeStamp (other.timeStamp), size (other.size)
  239. {
  240. packedData.allocatedData = other.packedData.allocatedData;
  241. other.size = 0;
  242. }
  243. MidiMessage& MidiMessage::operator= (MidiMessage&& other) noexcept
  244. {
  245. packedData.allocatedData = other.packedData.allocatedData;
  246. timeStamp = other.timeStamp;
  247. size = other.size;
  248. other.size = 0;
  249. return *this;
  250. }
  251. #endif
  252. MidiMessage::~MidiMessage() noexcept
  253. {
  254. if (isHeapAllocated())
  255. std::free (packedData.allocatedData);
  256. }
  257. uint8* MidiMessage::allocateSpace (int bytes)
  258. {
  259. if (bytes > (int) sizeof (packedData))
  260. {
  261. uint8* d = static_cast<uint8*> (std::malloc ((size_t) bytes));
  262. packedData.allocatedData = d;
  263. return d;
  264. }
  265. return packedData.asBytes;
  266. }
  267. String MidiMessage::getDescription() const
  268. {
  269. if (isNoteOn()) return "Note on " + MidiMessage::getMidiNoteName (getNoteNumber(), true, true, 3) + " Velocity " + String (getVelocity()) + " Channel " + String (getChannel());
  270. if (isNoteOff()) return "Note off " + MidiMessage::getMidiNoteName (getNoteNumber(), true, true, 3) + " Velocity " + String (getVelocity()) + " Channel " + String (getChannel());
  271. if (isProgramChange()) return "Program change " + String (getProgramChangeNumber()) + " Channel " + String (getChannel());
  272. if (isPitchWheel()) return "Pitch wheel " + String (getPitchWheelValue()) + " Channel " + String (getChannel());
  273. if (isAftertouch()) return "Aftertouch " + MidiMessage::getMidiNoteName (getNoteNumber(), true, true, 3) + ": " + String (getAfterTouchValue()) + " Channel " + String (getChannel());
  274. if (isChannelPressure()) return "Channel pressure " + String (getChannelPressureValue()) + " Channel " + String (getChannel());
  275. if (isAllNotesOff()) return "All notes off Channel " + String (getChannel());
  276. if (isAllSoundOff()) return "All sound off Channel " + String (getChannel());
  277. if (isMetaEvent()) return "Meta event";
  278. if (isController())
  279. {
  280. String name (MidiMessage::getControllerName (getControllerNumber()));
  281. if (name.isEmpty())
  282. name = String (getControllerNumber());
  283. return "Controller " + name + ": " + String (getControllerValue()) + " Channel " + String (getChannel());
  284. }
  285. return String::toHexString (getRawData(), getRawDataSize());
  286. }
  287. int MidiMessage::getChannel() const noexcept
  288. {
  289. const uint8* const data = getRawData();
  290. if ((data[0] & 0xf0) != 0xf0)
  291. return (data[0] & 0xf) + 1;
  292. return 0;
  293. }
  294. bool MidiMessage::isForChannel (const int channel) const noexcept
  295. {
  296. jassert (channel > 0 && channel <= 16); // valid channels are numbered 1 to 16
  297. const uint8* const data = getRawData();
  298. return ((data[0] & 0xf) == channel - 1)
  299. && ((data[0] & 0xf0) != 0xf0);
  300. }
  301. void MidiMessage::setChannel (const int channel) noexcept
  302. {
  303. jassert (channel > 0 && channel <= 16); // valid channels are numbered 1 to 16
  304. uint8* const data = getData();
  305. if ((data[0] & 0xf0) != (uint8) 0xf0)
  306. data[0] = (uint8) ((data[0] & (uint8) 0xf0)
  307. | (uint8)(channel - 1));
  308. }
  309. bool MidiMessage::isNoteOn (const bool returnTrueForVelocity0) const noexcept
  310. {
  311. const uint8* const data = getRawData();
  312. return ((data[0] & 0xf0) == 0x90)
  313. && (returnTrueForVelocity0 || data[2] != 0);
  314. }
  315. bool MidiMessage::isNoteOff (const bool returnTrueForNoteOnVelocity0) const noexcept
  316. {
  317. const uint8* const data = getRawData();
  318. return ((data[0] & 0xf0) == 0x80)
  319. || (returnTrueForNoteOnVelocity0 && (data[2] == 0) && ((data[0] & 0xf0) == 0x90));
  320. }
  321. bool MidiMessage::isNoteOnOrOff() const noexcept
  322. {
  323. const uint8* const data = getRawData();
  324. const int d = data[0] & 0xf0;
  325. return (d == 0x90) || (d == 0x80);
  326. }
  327. int MidiMessage::getNoteNumber() const noexcept
  328. {
  329. return getRawData()[1];
  330. }
  331. void MidiMessage::setNoteNumber (const int newNoteNumber) noexcept
  332. {
  333. if (isNoteOnOrOff() || isAftertouch())
  334. getData()[1] = (uint8) (newNoteNumber & 127);
  335. }
  336. uint8 MidiMessage::getVelocity() const noexcept
  337. {
  338. if (isNoteOnOrOff())
  339. return getRawData()[2];
  340. return 0;
  341. }
  342. float MidiMessage::getFloatVelocity() const noexcept
  343. {
  344. return getVelocity() * (1.0f / 127.0f);
  345. }
  346. void MidiMessage::setVelocity (const float newVelocity) noexcept
  347. {
  348. if (isNoteOnOrOff())
  349. getData()[2] = floatValueToMidiByte (newVelocity);
  350. }
  351. void MidiMessage::multiplyVelocity (const float scaleFactor) noexcept
  352. {
  353. if (isNoteOnOrOff())
  354. {
  355. uint8* const data = getData();
  356. data[2] = MidiHelpers::validVelocity (roundToInt (scaleFactor * data[2]));
  357. }
  358. }
  359. bool MidiMessage::isAftertouch() const noexcept
  360. {
  361. return (getRawData()[0] & 0xf0) == 0xa0;
  362. }
  363. int MidiMessage::getAfterTouchValue() const noexcept
  364. {
  365. jassert (isAftertouch());
  366. return getRawData()[2];
  367. }
  368. MidiMessage MidiMessage::aftertouchChange (const int channel,
  369. const int noteNum,
  370. const int aftertouchValue) noexcept
  371. {
  372. jassert (channel > 0 && channel <= 16); // valid channels are numbered 1 to 16
  373. jassert (isPositiveAndBelow (noteNum, (int) 128));
  374. jassert (isPositiveAndBelow (aftertouchValue, (int) 128));
  375. return MidiMessage (MidiHelpers::initialByte (0xa0, channel),
  376. noteNum & 0x7f,
  377. aftertouchValue & 0x7f);
  378. }
  379. bool MidiMessage::isChannelPressure() const noexcept
  380. {
  381. return (getRawData()[0] & 0xf0) == 0xd0;
  382. }
  383. int MidiMessage::getChannelPressureValue() const noexcept
  384. {
  385. jassert (isChannelPressure());
  386. return getRawData()[1];
  387. }
  388. MidiMessage MidiMessage::channelPressureChange (const int channel, const int pressure) noexcept
  389. {
  390. jassert (channel > 0 && channel <= 16); // valid channels are numbered 1 to 16
  391. jassert (isPositiveAndBelow (pressure, (int) 128));
  392. return MidiMessage (MidiHelpers::initialByte (0xd0, channel), pressure & 0x7f);
  393. }
  394. bool MidiMessage::isSustainPedalOn() const noexcept { return isControllerOfType (0x40) && getRawData()[2] >= 64; }
  395. bool MidiMessage::isSustainPedalOff() const noexcept { return isControllerOfType (0x40) && getRawData()[2] < 64; }
  396. bool MidiMessage::isSostenutoPedalOn() const noexcept { return isControllerOfType (0x42) && getRawData()[2] >= 64; }
  397. bool MidiMessage::isSostenutoPedalOff() const noexcept { return isControllerOfType (0x42) && getRawData()[2] < 64; }
  398. bool MidiMessage::isSoftPedalOn() const noexcept { return isControllerOfType (0x43) && getRawData()[2] >= 64; }
  399. bool MidiMessage::isSoftPedalOff() const noexcept { return isControllerOfType (0x43) && getRawData()[2] < 64; }
  400. bool MidiMessage::isProgramChange() const noexcept
  401. {
  402. return (getRawData()[0] & 0xf0) == 0xc0;
  403. }
  404. int MidiMessage::getProgramChangeNumber() const noexcept
  405. {
  406. jassert (isProgramChange());
  407. return getRawData()[1];
  408. }
  409. MidiMessage MidiMessage::programChange (const int channel, const int programNumber) noexcept
  410. {
  411. jassert (channel > 0 && channel <= 16); // valid channels are numbered 1 to 16
  412. return MidiMessage (MidiHelpers::initialByte (0xc0, channel), programNumber & 0x7f);
  413. }
  414. bool MidiMessage::isPitchWheel() const noexcept
  415. {
  416. return (getRawData()[0] & 0xf0) == 0xe0;
  417. }
  418. int MidiMessage::getPitchWheelValue() const noexcept
  419. {
  420. jassert (isPitchWheel());
  421. const uint8* const data = getRawData();
  422. return data[1] | (data[2] << 7);
  423. }
  424. MidiMessage MidiMessage::pitchWheel (const int channel, const int position) noexcept
  425. {
  426. jassert (channel > 0 && channel <= 16); // valid channels are numbered 1 to 16
  427. jassert (isPositiveAndBelow (position, (int) 0x4000));
  428. return MidiMessage (MidiHelpers::initialByte (0xe0, channel),
  429. position & 127, (position >> 7) & 127);
  430. }
  431. bool MidiMessage::isController() const noexcept
  432. {
  433. return (getRawData()[0] & 0xf0) == 0xb0;
  434. }
  435. bool MidiMessage::isControllerOfType (const int controllerType) const noexcept
  436. {
  437. const uint8* const data = getRawData();
  438. return (data[0] & 0xf0) == 0xb0 && data[1] == controllerType;
  439. }
  440. int MidiMessage::getControllerNumber() const noexcept
  441. {
  442. jassert (isController());
  443. return getRawData()[1];
  444. }
  445. int MidiMessage::getControllerValue() const noexcept
  446. {
  447. jassert (isController());
  448. return getRawData()[2];
  449. }
  450. MidiMessage MidiMessage::controllerEvent (const int channel, const int controllerType, const int value) noexcept
  451. {
  452. // the channel must be between 1 and 16 inclusive
  453. jassert (channel > 0 && channel <= 16);
  454. return MidiMessage (MidiHelpers::initialByte (0xb0, channel),
  455. controllerType & 127, value & 127);
  456. }
  457. MidiMessage MidiMessage::noteOn (const int channel, const int noteNumber, const uint8 velocity) noexcept
  458. {
  459. jassert (channel > 0 && channel <= 16);
  460. jassert (isPositiveAndBelow (noteNumber, (int) 128));
  461. return MidiMessage (MidiHelpers::initialByte (0x90, channel),
  462. noteNumber & 127, MidiHelpers::validVelocity (velocity));
  463. }
  464. MidiMessage MidiMessage::noteOn (const int channel, const int noteNumber, const float velocity) noexcept
  465. {
  466. return noteOn (channel, noteNumber, floatValueToMidiByte (velocity));
  467. }
  468. MidiMessage MidiMessage::noteOff (const int channel, const int noteNumber, uint8 velocity) noexcept
  469. {
  470. jassert (channel > 0 && channel <= 16);
  471. jassert (isPositiveAndBelow (noteNumber, (int) 128));
  472. return MidiMessage (MidiHelpers::initialByte (0x80, channel),
  473. noteNumber & 127, MidiHelpers::validVelocity (velocity));
  474. }
  475. MidiMessage MidiMessage::noteOff (const int channel, const int noteNumber, float velocity) noexcept
  476. {
  477. return noteOff (channel, noteNumber, floatValueToMidiByte (velocity));
  478. }
  479. MidiMessage MidiMessage::noteOff (const int channel, const int noteNumber) noexcept
  480. {
  481. jassert (channel > 0 && channel <= 16);
  482. jassert (isPositiveAndBelow (noteNumber, (int) 128));
  483. return MidiMessage (MidiHelpers::initialByte (0x80, channel), noteNumber & 127, 0);
  484. }
  485. MidiMessage MidiMessage::allNotesOff (const int channel) noexcept
  486. {
  487. return controllerEvent (channel, 123, 0);
  488. }
  489. bool MidiMessage::isAllNotesOff() const noexcept
  490. {
  491. const uint8* const data = getRawData();
  492. return (data[0] & 0xf0) == 0xb0 && data[1] == 123;
  493. }
  494. MidiMessage MidiMessage::allSoundOff (const int channel) noexcept
  495. {
  496. return controllerEvent (channel, 120, 0);
  497. }
  498. bool MidiMessage::isAllSoundOff() const noexcept
  499. {
  500. const uint8* const data = getRawData();
  501. return (data[0] & 0xf0) == 0xb0 && data[1] == 120;
  502. }
  503. MidiMessage MidiMessage::allControllersOff (const int channel) noexcept
  504. {
  505. return controllerEvent (channel, 121, 0);
  506. }
  507. MidiMessage MidiMessage::masterVolume (const float volume)
  508. {
  509. const int vol = jlimit (0, 0x3fff, roundToInt (volume * 0x4000));
  510. const uint8 buf[] = { 0xf0, 0x7f, 0x7f, 0x04, 0x01,
  511. (uint8) (vol & 0x7f),
  512. (uint8) (vol >> 7),
  513. 0xf7 };
  514. return MidiMessage (buf, 8);
  515. }
  516. //==============================================================================
  517. bool MidiMessage::isSysEx() const noexcept
  518. {
  519. return *getRawData() == 0xf0;
  520. }
  521. MidiMessage MidiMessage::createSysExMessage (const void* sysexData, const int dataSize)
  522. {
  523. HeapBlock<uint8> m ((size_t) dataSize + 2);
  524. m[0] = 0xf0;
  525. memcpy (m + 1, sysexData, (size_t) dataSize);
  526. m[dataSize + 1] = 0xf7;
  527. return MidiMessage (m, dataSize + 2);
  528. }
  529. const uint8* MidiMessage::getSysExData() const noexcept
  530. {
  531. return isSysEx() ? getRawData() + 1 : nullptr;
  532. }
  533. int MidiMessage::getSysExDataSize() const noexcept
  534. {
  535. return isSysEx() ? size - 2 : 0;
  536. }
  537. //==============================================================================
  538. bool MidiMessage::isMetaEvent() const noexcept { return *getRawData() == 0xff; }
  539. bool MidiMessage::isActiveSense() const noexcept { return *getRawData() == 0xfe; }
  540. int MidiMessage::getMetaEventType() const noexcept
  541. {
  542. const uint8* const data = getRawData();
  543. return *data != 0xff ? -1 : data[1];
  544. }
  545. int MidiMessage::getMetaEventLength() const noexcept
  546. {
  547. const uint8* const data = getRawData();
  548. if (*data == 0xff)
  549. {
  550. int n;
  551. return jmin (size - 2, readVariableLengthVal (data + 2, n));
  552. }
  553. return 0;
  554. }
  555. const uint8* MidiMessage::getMetaEventData() const noexcept
  556. {
  557. jassert (isMetaEvent());
  558. int n;
  559. const uint8* d = getRawData() + 2;
  560. readVariableLengthVal (d, n);
  561. return d + n;
  562. }
  563. bool MidiMessage::isTrackMetaEvent() const noexcept { return getMetaEventType() == 0; }
  564. bool MidiMessage::isEndOfTrackMetaEvent() const noexcept { return getMetaEventType() == 47; }
  565. bool MidiMessage::isTextMetaEvent() const noexcept
  566. {
  567. const int t = getMetaEventType();
  568. return t > 0 && t < 16;
  569. }
  570. String MidiMessage::getTextFromTextMetaEvent() const
  571. {
  572. const char* const textData = reinterpret_cast<const char*> (getMetaEventData());
  573. return String (CharPointer_UTF8 (textData),
  574. CharPointer_UTF8 (textData + getMetaEventLength()));
  575. }
  576. MidiMessage MidiMessage::textMetaEvent (int type, StringRef text)
  577. {
  578. jassert (type > 0 && type < 16);
  579. MidiMessage result;
  580. const size_t textSize = text.text.sizeInBytes() - 1;
  581. uint8 header[8];
  582. size_t n = sizeof (header);
  583. header[--n] = (uint8) (textSize & 0x7f);
  584. for (size_t i = textSize; (i >>= 7) != 0;)
  585. header[--n] = (uint8) ((i & 0x7f) | 0x80);
  586. header[--n] = (uint8) type;
  587. header[--n] = 0xff;
  588. const size_t headerLen = sizeof (header) - n;
  589. const int totalSize = (int) (headerLen + textSize);
  590. uint8* const dest = result.allocateSpace (totalSize);
  591. result.size = totalSize;
  592. memcpy (dest, header + n, headerLen);
  593. memcpy (dest + headerLen, text.text.getAddress(), textSize);
  594. return result;
  595. }
  596. bool MidiMessage::isTrackNameEvent() const noexcept { const uint8* data = getRawData(); return (data[1] == 3) && (*data == 0xff); }
  597. bool MidiMessage::isTempoMetaEvent() const noexcept { const uint8* data = getRawData(); return (data[1] == 81) && (*data == 0xff); }
  598. bool MidiMessage::isMidiChannelMetaEvent() const noexcept { const uint8* data = getRawData(); return (data[1] == 0x20) && (*data == 0xff) && (data[2] == 1); }
  599. int MidiMessage::getMidiChannelMetaEventChannel() const noexcept
  600. {
  601. jassert (isMidiChannelMetaEvent());
  602. return getRawData()[3] + 1;
  603. }
  604. double MidiMessage::getTempoSecondsPerQuarterNote() const noexcept
  605. {
  606. if (! isTempoMetaEvent())
  607. return 0.0;
  608. const uint8* const d = getMetaEventData();
  609. return (((unsigned int) d[0] << 16)
  610. | ((unsigned int) d[1] << 8)
  611. | d[2])
  612. / 1000000.0;
  613. }
  614. double MidiMessage::getTempoMetaEventTickLength (const short timeFormat) const noexcept
  615. {
  616. if (timeFormat > 0)
  617. {
  618. if (! isTempoMetaEvent())
  619. return 0.5 / timeFormat;
  620. return getTempoSecondsPerQuarterNote() / timeFormat;
  621. }
  622. else
  623. {
  624. const int frameCode = (-timeFormat) >> 8;
  625. double framesPerSecond;
  626. switch (frameCode)
  627. {
  628. case 24: framesPerSecond = 24.0; break;
  629. case 25: framesPerSecond = 25.0; break;
  630. case 29: framesPerSecond = 29.97; break;
  631. case 30: framesPerSecond = 30.0; break;
  632. default: framesPerSecond = 30.0; break;
  633. }
  634. return (1.0 / framesPerSecond) / (timeFormat & 0xff);
  635. }
  636. }
  637. MidiMessage MidiMessage::tempoMetaEvent (int microsecondsPerQuarterNote) noexcept
  638. {
  639. const uint8 d[] = { 0xff, 81, 3,
  640. (uint8) (microsecondsPerQuarterNote >> 16),
  641. (uint8) (microsecondsPerQuarterNote >> 8),
  642. (uint8) microsecondsPerQuarterNote };
  643. return MidiMessage (d, 6, 0.0);
  644. }
  645. bool MidiMessage::isTimeSignatureMetaEvent() const noexcept
  646. {
  647. const uint8* const data = getRawData();
  648. return (data[1] == 0x58) && (*data == (uint8) 0xff);
  649. }
  650. void MidiMessage::getTimeSignatureInfo (int& numerator, int& denominator) const noexcept
  651. {
  652. if (isTimeSignatureMetaEvent())
  653. {
  654. const uint8* const d = getMetaEventData();
  655. numerator = d[0];
  656. denominator = 1 << d[1];
  657. }
  658. else
  659. {
  660. numerator = 4;
  661. denominator = 4;
  662. }
  663. }
  664. MidiMessage MidiMessage::timeSignatureMetaEvent (const int numerator, const int denominator)
  665. {
  666. int n = 1;
  667. int powerOfTwo = 0;
  668. while (n < denominator)
  669. {
  670. n <<= 1;
  671. ++powerOfTwo;
  672. }
  673. const uint8 d[] = { 0xff, 0x58, 0x04, (uint8) numerator, (uint8) powerOfTwo, 1, 96 };
  674. return MidiMessage (d, 7, 0.0);
  675. }
  676. MidiMessage MidiMessage::midiChannelMetaEvent (const int channel) noexcept
  677. {
  678. const uint8 d[] = { 0xff, 0x20, 0x01, (uint8) jlimit (0, 0xff, channel - 1) };
  679. return MidiMessage (d, 4, 0.0);
  680. }
  681. bool MidiMessage::isKeySignatureMetaEvent() const noexcept
  682. {
  683. return getMetaEventType() == 0x59;
  684. }
  685. int MidiMessage::getKeySignatureNumberOfSharpsOrFlats() const noexcept
  686. {
  687. return (int) (int8) getMetaEventData()[0];
  688. }
  689. bool MidiMessage::isKeySignatureMajorKey() const noexcept
  690. {
  691. return getMetaEventData()[1] == 0;
  692. }
  693. MidiMessage MidiMessage::keySignatureMetaEvent (int numberOfSharpsOrFlats, bool isMinorKey)
  694. {
  695. jassert (numberOfSharpsOrFlats >= -7 && numberOfSharpsOrFlats <= 7);
  696. const uint8 d[] = { 0xff, 0x59, 0x02, (uint8) numberOfSharpsOrFlats, isMinorKey ? (uint8) 1 : (uint8) 0 };
  697. return MidiMessage (d, 5, 0.0);
  698. }
  699. MidiMessage MidiMessage::endOfTrack() noexcept
  700. {
  701. return MidiMessage (0xff, 0x2f, 0, 0.0);
  702. }
  703. //==============================================================================
  704. bool MidiMessage::isSongPositionPointer() const noexcept { return *getRawData() == 0xf2; }
  705. int MidiMessage::getSongPositionPointerMidiBeat() const noexcept { const uint8* data = getRawData(); return data[1] | (data[2] << 7); }
  706. MidiMessage MidiMessage::songPositionPointer (const int positionInMidiBeats) noexcept
  707. {
  708. return MidiMessage (0xf2,
  709. positionInMidiBeats & 127,
  710. (positionInMidiBeats >> 7) & 127);
  711. }
  712. bool MidiMessage::isMidiStart() const noexcept { return *getRawData() == 0xfa; }
  713. MidiMessage MidiMessage::midiStart() noexcept { return MidiMessage (0xfa); }
  714. bool MidiMessage::isMidiContinue() const noexcept { return *getRawData() == 0xfb; }
  715. MidiMessage MidiMessage::midiContinue() noexcept { return MidiMessage (0xfb); }
  716. bool MidiMessage::isMidiStop() const noexcept { return *getRawData() == 0xfc; }
  717. MidiMessage MidiMessage::midiStop() noexcept { return MidiMessage (0xfc); }
  718. bool MidiMessage::isMidiClock() const noexcept { return *getRawData() == 0xf8; }
  719. MidiMessage MidiMessage::midiClock() noexcept { return MidiMessage (0xf8); }
  720. bool MidiMessage::isQuarterFrame() const noexcept { return *getRawData() == 0xf1; }
  721. int MidiMessage::getQuarterFrameSequenceNumber() const noexcept { return ((int) getRawData()[1]) >> 4; }
  722. int MidiMessage::getQuarterFrameValue() const noexcept { return ((int) getRawData()[1]) & 0x0f; }
  723. MidiMessage MidiMessage::quarterFrame (const int sequenceNumber, const int value) noexcept
  724. {
  725. return MidiMessage (0xf1, (sequenceNumber << 4) | value);
  726. }
  727. bool MidiMessage::isFullFrame() const noexcept
  728. {
  729. const uint8* const data = getRawData();
  730. return data[0] == 0xf0
  731. && data[1] == 0x7f
  732. && size >= 10
  733. && data[3] == 0x01
  734. && data[4] == 0x01;
  735. }
  736. void MidiMessage::getFullFrameParameters (int& hours, int& minutes, int& seconds, int& frames,
  737. MidiMessage::SmpteTimecodeType& timecodeType) const noexcept
  738. {
  739. jassert (isFullFrame());
  740. const uint8* const data = getRawData();
  741. timecodeType = (SmpteTimecodeType) (data[5] >> 5);
  742. hours = data[5] & 0x1f;
  743. minutes = data[6];
  744. seconds = data[7];
  745. frames = data[8];
  746. }
  747. MidiMessage MidiMessage::fullFrame (const int hours, const int minutes,
  748. const int seconds, const int frames,
  749. MidiMessage::SmpteTimecodeType timecodeType)
  750. {
  751. const uint8 d[] = { 0xf0, 0x7f, 0x7f, 0x01, 0x01,
  752. (uint8) ((hours & 0x01f) | (timecodeType << 5)),
  753. (uint8) minutes,
  754. (uint8) seconds,
  755. (uint8) frames,
  756. 0xf7 };
  757. return MidiMessage (d, 10, 0.0);
  758. }
  759. bool MidiMessage::isMidiMachineControlMessage() const noexcept
  760. {
  761. const uint8* const data = getRawData();
  762. return data[0] == 0xf0
  763. && data[1] == 0x7f
  764. && data[3] == 0x06
  765. && size > 5;
  766. }
  767. MidiMessage::MidiMachineControlCommand MidiMessage::getMidiMachineControlCommand() const noexcept
  768. {
  769. jassert (isMidiMachineControlMessage());
  770. return (MidiMachineControlCommand) getRawData()[4];
  771. }
  772. MidiMessage MidiMessage::midiMachineControlCommand (MidiMessage::MidiMachineControlCommand command)
  773. {
  774. const uint8 d[] = { 0xf0, 0x7f, 0, 6, (uint8) command, 0xf7 };
  775. return MidiMessage (d, 6, 0.0);
  776. }
  777. //==============================================================================
  778. bool MidiMessage::isMidiMachineControlGoto (int& hours, int& minutes, int& seconds, int& frames) const noexcept
  779. {
  780. const uint8* const data = getRawData();
  781. if (size >= 12
  782. && data[0] == 0xf0
  783. && data[1] == 0x7f
  784. && data[3] == 0x06
  785. && data[4] == 0x44
  786. && data[5] == 0x06
  787. && data[6] == 0x01)
  788. {
  789. hours = data[7] % 24; // (that some machines send out hours > 24)
  790. minutes = data[8];
  791. seconds = data[9];
  792. frames = data[10];
  793. return true;
  794. }
  795. return false;
  796. }
  797. MidiMessage MidiMessage::midiMachineControlGoto (int hours, int minutes, int seconds, int frames)
  798. {
  799. const uint8 d[] = { 0xf0, 0x7f, 0, 6, 0x44, 6, 1,
  800. (uint8) hours,
  801. (uint8) minutes,
  802. (uint8) seconds,
  803. (uint8) frames,
  804. 0xf7 };
  805. return MidiMessage (d, 12, 0.0);
  806. }
  807. //==============================================================================
  808. String MidiMessage::getMidiNoteName (int note, bool useSharps, bool includeOctaveNumber, int octaveNumForMiddleC)
  809. {
  810. static const char* const sharpNoteNames[] = { "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B" };
  811. static const char* const flatNoteNames[] = { "C", "Db", "D", "Eb", "E", "F", "Gb", "G", "Ab", "A", "Bb", "B" };
  812. if (isPositiveAndBelow (note, (int) 128))
  813. {
  814. String s (useSharps ? sharpNoteNames [note % 12]
  815. : flatNoteNames [note % 12]);
  816. if (includeOctaveNumber)
  817. s << (note / 12 + (octaveNumForMiddleC - 5));
  818. return s;
  819. }
  820. return String();
  821. }
  822. double MidiMessage::getMidiNoteInHertz (const int noteNumber, const double frequencyOfA) noexcept
  823. {
  824. return frequencyOfA * pow (2.0, (noteNumber - 69) / 12.0);
  825. }
  826. bool MidiMessage::isMidiNoteBlack (int noteNumber) noexcept
  827. {
  828. return ((1 << (noteNumber % 12)) & 0x054a) != 0;
  829. }
  830. const char* MidiMessage::getGMInstrumentName (const int n)
  831. {
  832. static const char* names[] =
  833. {
  834. NEEDS_TRANS("Acoustic Grand Piano"), NEEDS_TRANS("Bright Acoustic Piano"), NEEDS_TRANS("Electric Grand Piano"), NEEDS_TRANS("Honky-tonk Piano"),
  835. NEEDS_TRANS("Electric Piano 1"), NEEDS_TRANS("Electric Piano 2"), NEEDS_TRANS("Harpsichord"), NEEDS_TRANS("Clavinet"),
  836. NEEDS_TRANS("Celesta"), NEEDS_TRANS("Glockenspiel"), NEEDS_TRANS("Music Box"), NEEDS_TRANS("Vibraphone"),
  837. NEEDS_TRANS("Marimba"), NEEDS_TRANS("Xylophone"), NEEDS_TRANS("Tubular Bells"), NEEDS_TRANS("Dulcimer"),
  838. NEEDS_TRANS("Drawbar Organ"), NEEDS_TRANS("Percussive Organ"), NEEDS_TRANS("Rock Organ"), NEEDS_TRANS("Church Organ"),
  839. NEEDS_TRANS("Reed Organ"), NEEDS_TRANS("Accordion"), NEEDS_TRANS("Harmonica"), NEEDS_TRANS("Tango Accordion"),
  840. NEEDS_TRANS("Acoustic Guitar (nylon)"), NEEDS_TRANS("Acoustic Guitar (steel)"), NEEDS_TRANS("Electric Guitar (jazz)"), NEEDS_TRANS("Electric Guitar (clean)"),
  841. NEEDS_TRANS("Electric Guitar (mute)"), NEEDS_TRANS("Overdriven Guitar"), NEEDS_TRANS("Distortion Guitar"), NEEDS_TRANS("Guitar Harmonics"),
  842. NEEDS_TRANS("Acoustic Bass"), NEEDS_TRANS("Electric Bass (finger)"), NEEDS_TRANS("Electric Bass (pick)"), NEEDS_TRANS("Fretless Bass"),
  843. NEEDS_TRANS("Slap Bass 1"), NEEDS_TRANS("Slap Bass 2"), NEEDS_TRANS("Synth Bass 1"), NEEDS_TRANS("Synth Bass 2"),
  844. NEEDS_TRANS("Violin"), NEEDS_TRANS("Viola"), NEEDS_TRANS("Cello"), NEEDS_TRANS("Contrabass"),
  845. NEEDS_TRANS("Tremolo Strings"), NEEDS_TRANS("Pizzicato Strings"), NEEDS_TRANS("Orchestral Harp"), NEEDS_TRANS("Timpani"),
  846. NEEDS_TRANS("String Ensemble 1"), NEEDS_TRANS("String Ensemble 2"), NEEDS_TRANS("SynthStrings 1"), NEEDS_TRANS("SynthStrings 2"),
  847. NEEDS_TRANS("Choir Aahs"), NEEDS_TRANS("Voice Oohs"), NEEDS_TRANS("Synth Voice"), NEEDS_TRANS("Orchestra Hit"),
  848. NEEDS_TRANS("Trumpet"), NEEDS_TRANS("Trombone"), NEEDS_TRANS("Tuba"), NEEDS_TRANS("Muted Trumpet"),
  849. NEEDS_TRANS("French Horn"), NEEDS_TRANS("Brass Section"), NEEDS_TRANS("SynthBrass 1"), NEEDS_TRANS("SynthBrass 2"),
  850. NEEDS_TRANS("Soprano Sax"), NEEDS_TRANS("Alto Sax"), NEEDS_TRANS("Tenor Sax"), NEEDS_TRANS("Baritone Sax"),
  851. NEEDS_TRANS("Oboe"), NEEDS_TRANS("English Horn"), NEEDS_TRANS("Bassoon"), NEEDS_TRANS("Clarinet"),
  852. NEEDS_TRANS("Piccolo"), NEEDS_TRANS("Flute"), NEEDS_TRANS("Recorder"), NEEDS_TRANS("Pan Flute"),
  853. NEEDS_TRANS("Blown Bottle"), NEEDS_TRANS("Shakuhachi"), NEEDS_TRANS("Whistle"), NEEDS_TRANS("Ocarina"),
  854. NEEDS_TRANS("Lead 1 (square)"), NEEDS_TRANS("Lead 2 (sawtooth)"), NEEDS_TRANS("Lead 3 (calliope)"), NEEDS_TRANS("Lead 4 (chiff)"),
  855. NEEDS_TRANS("Lead 5 (charang)"), NEEDS_TRANS("Lead 6 (voice)"), NEEDS_TRANS("Lead 7 (fifths)"), NEEDS_TRANS("Lead 8 (bass+lead)"),
  856. NEEDS_TRANS("Pad 1 (new age)"), NEEDS_TRANS("Pad 2 (warm)"), NEEDS_TRANS("Pad 3 (polysynth)"), NEEDS_TRANS("Pad 4 (choir)"),
  857. NEEDS_TRANS("Pad 5 (bowed)"), NEEDS_TRANS("Pad 6 (metallic)"), NEEDS_TRANS("Pad 7 (halo)"), NEEDS_TRANS("Pad 8 (sweep)"),
  858. NEEDS_TRANS("FX 1 (rain)"), NEEDS_TRANS("FX 2 (soundtrack)"), NEEDS_TRANS("FX 3 (crystal)"), NEEDS_TRANS("FX 4 (atmosphere)"),
  859. NEEDS_TRANS("FX 5 (brightness)"), NEEDS_TRANS("FX 6 (goblins)"), NEEDS_TRANS("FX 7 (echoes)"), NEEDS_TRANS("FX 8 (sci-fi)"),
  860. NEEDS_TRANS("Sitar"), NEEDS_TRANS("Banjo"), NEEDS_TRANS("Shamisen"), NEEDS_TRANS("Koto"),
  861. NEEDS_TRANS("Kalimba"), NEEDS_TRANS("Bag pipe"), NEEDS_TRANS("Fiddle"), NEEDS_TRANS("Shanai"),
  862. NEEDS_TRANS("Tinkle Bell"), NEEDS_TRANS("Agogo"), NEEDS_TRANS("Steel Drums"), NEEDS_TRANS("Woodblock"),
  863. NEEDS_TRANS("Taiko Drum"), NEEDS_TRANS("Melodic Tom"), NEEDS_TRANS("Synth Drum"), NEEDS_TRANS("Reverse Cymbal"),
  864. NEEDS_TRANS("Guitar Fret Noise"), NEEDS_TRANS("Breath Noise"), NEEDS_TRANS("Seashore"), NEEDS_TRANS("Bird Tweet"),
  865. NEEDS_TRANS("Telephone Ring"), NEEDS_TRANS("Helicopter"), NEEDS_TRANS("Applause"), NEEDS_TRANS("Gunshot")
  866. };
  867. return isPositiveAndBelow (n, numElementsInArray (names)) ? names[n] : nullptr;
  868. }
  869. const char* MidiMessage::getGMInstrumentBankName (const int n)
  870. {
  871. static const char* names[] =
  872. {
  873. NEEDS_TRANS("Piano"), NEEDS_TRANS("Chromatic Percussion"), NEEDS_TRANS("Organ"), NEEDS_TRANS("Guitar"),
  874. NEEDS_TRANS("Bass"), NEEDS_TRANS("Strings"), NEEDS_TRANS("Ensemble"), NEEDS_TRANS("Brass"),
  875. NEEDS_TRANS("Reed"), NEEDS_TRANS("Pipe"), NEEDS_TRANS("Synth Lead"), NEEDS_TRANS("Synth Pad"),
  876. NEEDS_TRANS("Synth Effects"), NEEDS_TRANS("Ethnic"), NEEDS_TRANS("Percussive"), NEEDS_TRANS("Sound Effects")
  877. };
  878. return isPositiveAndBelow (n, numElementsInArray (names)) ? names[n] : nullptr;
  879. }
  880. const char* MidiMessage::getRhythmInstrumentName (const int n)
  881. {
  882. static const char* names[] =
  883. {
  884. NEEDS_TRANS("Acoustic Bass Drum"), NEEDS_TRANS("Bass Drum 1"), NEEDS_TRANS("Side Stick"), NEEDS_TRANS("Acoustic Snare"),
  885. NEEDS_TRANS("Hand Clap"), NEEDS_TRANS("Electric Snare"), NEEDS_TRANS("Low Floor Tom"), NEEDS_TRANS("Closed Hi-Hat"),
  886. NEEDS_TRANS("High Floor Tom"), NEEDS_TRANS("Pedal Hi-Hat"), NEEDS_TRANS("Low Tom"), NEEDS_TRANS("Open Hi-Hat"),
  887. NEEDS_TRANS("Low-Mid Tom"), NEEDS_TRANS("Hi-Mid Tom"), NEEDS_TRANS("Crash Cymbal 1"), NEEDS_TRANS("High Tom"),
  888. NEEDS_TRANS("Ride Cymbal 1"), NEEDS_TRANS("Chinese Cymbal"), NEEDS_TRANS("Ride Bell"), NEEDS_TRANS("Tambourine"),
  889. NEEDS_TRANS("Splash Cymbal"), NEEDS_TRANS("Cowbell"), NEEDS_TRANS("Crash Cymbal 2"), NEEDS_TRANS("Vibraslap"),
  890. NEEDS_TRANS("Ride Cymbal 2"), NEEDS_TRANS("Hi Bongo"), NEEDS_TRANS("Low Bongo"), NEEDS_TRANS("Mute Hi Conga"),
  891. NEEDS_TRANS("Open Hi Conga"), NEEDS_TRANS("Low Conga"), NEEDS_TRANS("High Timbale"), NEEDS_TRANS("Low Timbale"),
  892. NEEDS_TRANS("High Agogo"), NEEDS_TRANS("Low Agogo"), NEEDS_TRANS("Cabasa"), NEEDS_TRANS("Maracas"),
  893. NEEDS_TRANS("Short Whistle"), NEEDS_TRANS("Long Whistle"), NEEDS_TRANS("Short Guiro"), NEEDS_TRANS("Long Guiro"),
  894. NEEDS_TRANS("Claves"), NEEDS_TRANS("Hi Wood Block"), NEEDS_TRANS("Low Wood Block"), NEEDS_TRANS("Mute Cuica"),
  895. NEEDS_TRANS("Open Cuica"), NEEDS_TRANS("Mute Triangle"), NEEDS_TRANS("Open Triangle")
  896. };
  897. return (n >= 35 && n <= 81) ? names [n - 35] : nullptr;
  898. }
  899. const char* MidiMessage::getControllerName (const int n)
  900. {
  901. static const char* names[] =
  902. {
  903. NEEDS_TRANS("Bank Select"), NEEDS_TRANS("Modulation Wheel (coarse)"), NEEDS_TRANS("Breath controller (coarse)"),
  904. nullptr,
  905. NEEDS_TRANS("Foot Pedal (coarse)"), NEEDS_TRANS("Portamento Time (coarse)"), NEEDS_TRANS("Data Entry (coarse)"),
  906. NEEDS_TRANS("Volume (coarse)"), NEEDS_TRANS("Balance (coarse)"),
  907. nullptr,
  908. NEEDS_TRANS("Pan position (coarse)"), NEEDS_TRANS("Expression (coarse)"), NEEDS_TRANS("Effect Control 1 (coarse)"),
  909. NEEDS_TRANS("Effect Control 2 (coarse)"),
  910. nullptr, nullptr,
  911. NEEDS_TRANS("General Purpose Slider 1"), NEEDS_TRANS("General Purpose Slider 2"),
  912. NEEDS_TRANS("General Purpose Slider 3"), NEEDS_TRANS("General Purpose Slider 4"),
  913. nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
  914. NEEDS_TRANS("Bank Select (fine)"), NEEDS_TRANS("Modulation Wheel (fine)"), NEEDS_TRANS("Breath controller (fine)"),
  915. nullptr,
  916. NEEDS_TRANS("Foot Pedal (fine)"), NEEDS_TRANS("Portamento Time (fine)"), NEEDS_TRANS("Data Entry (fine)"), NEEDS_TRANS("Volume (fine)"),
  917. NEEDS_TRANS("Balance (fine)"), nullptr, NEEDS_TRANS("Pan position (fine)"), NEEDS_TRANS("Expression (fine)"),
  918. NEEDS_TRANS("Effect Control 1 (fine)"), NEEDS_TRANS("Effect Control 2 (fine)"),
  919. nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
  920. nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
  921. NEEDS_TRANS("Hold Pedal (on/off)"), NEEDS_TRANS("Portamento (on/off)"), NEEDS_TRANS("Sustenuto Pedal (on/off)"), NEEDS_TRANS("Soft Pedal (on/off)"),
  922. NEEDS_TRANS("Legato Pedal (on/off)"), NEEDS_TRANS("Hold 2 Pedal (on/off)"), NEEDS_TRANS("Sound Variation"), NEEDS_TRANS("Sound Timbre"),
  923. NEEDS_TRANS("Sound Release Time"), NEEDS_TRANS("Sound Attack Time"), NEEDS_TRANS("Sound Brightness"), NEEDS_TRANS("Sound Control 6"),
  924. NEEDS_TRANS("Sound Control 7"), NEEDS_TRANS("Sound Control 8"), NEEDS_TRANS("Sound Control 9"), NEEDS_TRANS("Sound Control 10"),
  925. NEEDS_TRANS("General Purpose Button 1 (on/off)"), NEEDS_TRANS("General Purpose Button 2 (on/off)"),
  926. NEEDS_TRANS("General Purpose Button 3 (on/off)"), NEEDS_TRANS("General Purpose Button 4 (on/off)"),
  927. nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
  928. NEEDS_TRANS("Reverb Level"), NEEDS_TRANS("Tremolo Level"), NEEDS_TRANS("Chorus Level"), NEEDS_TRANS("Celeste Level"),
  929. NEEDS_TRANS("Phaser Level"), NEEDS_TRANS("Data Button increment"), NEEDS_TRANS("Data Button decrement"), NEEDS_TRANS("Non-registered Parameter (fine)"),
  930. NEEDS_TRANS("Non-registered Parameter (coarse)"), NEEDS_TRANS("Registered Parameter (fine)"), NEEDS_TRANS("Registered Parameter (coarse)"),
  931. nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
  932. nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
  933. NEEDS_TRANS("All Sound Off"), NEEDS_TRANS("All Controllers Off"), NEEDS_TRANS("Local Keyboard (on/off)"), NEEDS_TRANS("All Notes Off"),
  934. NEEDS_TRANS("Omni Mode Off"), NEEDS_TRANS("Omni Mode On"), NEEDS_TRANS("Mono Operation"), NEEDS_TRANS("Poly Operation")
  935. };
  936. return isPositiveAndBelow (n, numElementsInArray (names)) ? names[n] : nullptr;
  937. }