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

juce_MidiMessage.cpp 42KB

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