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.

MidiMessage.cpp 44KB

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