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

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