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

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