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.

924 lines
28KB

  1. /*
  2. ==============================================================================
  3. This file is part of the Water library.
  4. Copyright (c) 2016 ROLI Ltd.
  5. Copyright (C) 2017-2022 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. wassert (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. wassert (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. wassert (dataSize > 0);
  91. // this checks that the length matches the data..
  92. wassert (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. wassert (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. wassert (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. wassert (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. MidiMessage::~MidiMessage() noexcept
  238. {
  239. if (isHeapAllocated())
  240. std::free (packedData.allocatedData);
  241. }
  242. uint8* MidiMessage::allocateSpace (int bytes)
  243. {
  244. if (bytes > (int) sizeof (packedData))
  245. {
  246. uint8* d = static_cast<uint8*> (std::malloc ((size_t) bytes));
  247. packedData.allocatedData = d;
  248. return d;
  249. }
  250. return packedData.asBytes;
  251. }
  252. int MidiMessage::getChannel() const noexcept
  253. {
  254. const uint8* const data = getData();
  255. if ((data[0] & 0xf0) != 0xf0)
  256. return (data[0] & 0xf) + 1;
  257. return 0;
  258. }
  259. bool MidiMessage::isForChannel (const int channel) const noexcept
  260. {
  261. wassert (channel > 0 && channel <= 16); // valid channels are numbered 1 to 16
  262. const uint8* const data = getData();
  263. return ((data[0] & 0xf) == channel - 1)
  264. && ((data[0] & 0xf0) != 0xf0);
  265. }
  266. void MidiMessage::setChannel (const int channel) noexcept
  267. {
  268. wassert (channel > 0 && channel <= 16); // valid channels are numbered 1 to 16
  269. uint8* const data = getData();
  270. if ((data[0] & 0xf0) != (uint8) 0xf0)
  271. data[0] = (uint8) ((data[0] & (uint8) 0xf0)
  272. | (uint8)(channel - 1));
  273. }
  274. bool MidiMessage::isNoteOn (const bool returnTrueForVelocity0) const noexcept
  275. {
  276. const uint8* const data = getData();
  277. return ((data[0] & 0xf0) == 0x90)
  278. && (returnTrueForVelocity0 || data[2] != 0);
  279. }
  280. bool MidiMessage::isNoteOff (const bool returnTrueForNoteOnVelocity0) const noexcept
  281. {
  282. const uint8* const data = getData();
  283. return ((data[0] & 0xf0) == 0x80)
  284. || (returnTrueForNoteOnVelocity0 && (data[2] == 0) && ((data[0] & 0xf0) == 0x90));
  285. }
  286. bool MidiMessage::isNoteOnOrOff() const noexcept
  287. {
  288. const uint8* const data = getData();
  289. const int d = data[0] & 0xf0;
  290. return (d == 0x90) || (d == 0x80);
  291. }
  292. int MidiMessage::getNoteNumber() const noexcept
  293. {
  294. return getData()[1];
  295. }
  296. void MidiMessage::setNoteNumber (const int newNoteNumber) noexcept
  297. {
  298. if (isNoteOnOrOff() || isAftertouch())
  299. getData()[1] = (uint8) (newNoteNumber & 127);
  300. }
  301. uint8 MidiMessage::getVelocity() const noexcept
  302. {
  303. if (isNoteOnOrOff())
  304. return getData()[2];
  305. return 0;
  306. }
  307. float MidiMessage::getFloatVelocity() const noexcept
  308. {
  309. return getVelocity() * (1.0f / 127.0f);
  310. }
  311. void MidiMessage::setVelocity (const float newVelocity) noexcept
  312. {
  313. if (isNoteOnOrOff())
  314. getData()[2] = floatValueToMidiByte (newVelocity);
  315. }
  316. void MidiMessage::multiplyVelocity (const float scaleFactor) noexcept
  317. {
  318. if (isNoteOnOrOff())
  319. {
  320. uint8* const data = getData();
  321. data[2] = MidiHelpers::validVelocity (roundToInt (scaleFactor * data[2]));
  322. }
  323. }
  324. bool MidiMessage::isAftertouch() const noexcept
  325. {
  326. return (getData()[0] & 0xf0) == 0xa0;
  327. }
  328. int MidiMessage::getAfterTouchValue() const noexcept
  329. {
  330. wassert (isAftertouch());
  331. return getData()[2];
  332. }
  333. MidiMessage MidiMessage::aftertouchChange (const int channel,
  334. const int noteNum,
  335. const int aftertouchValue) noexcept
  336. {
  337. wassert (channel > 0 && channel <= 16); // valid channels are numbered 1 to 16
  338. wassert (isPositiveAndBelow (noteNum, (int) 128));
  339. wassert (isPositiveAndBelow (aftertouchValue, (int) 128));
  340. return MidiMessage (MidiHelpers::initialByte (0xa0, channel),
  341. noteNum & 0x7f,
  342. aftertouchValue & 0x7f);
  343. }
  344. bool MidiMessage::isChannelPressure() const noexcept
  345. {
  346. return (getData()[0] & 0xf0) == 0xd0;
  347. }
  348. int MidiMessage::getChannelPressureValue() const noexcept
  349. {
  350. wassert (isChannelPressure());
  351. return getData()[1];
  352. }
  353. MidiMessage MidiMessage::channelPressureChange (const int channel, const int pressure) noexcept
  354. {
  355. wassert (channel > 0 && channel <= 16); // valid channels are numbered 1 to 16
  356. wassert (isPositiveAndBelow (pressure, (int) 128));
  357. return MidiMessage (MidiHelpers::initialByte (0xd0, channel), pressure & 0x7f);
  358. }
  359. bool MidiMessage::isSustainPedalOn() const noexcept { return isControllerOfType (0x40) && getData()[2] >= 64; }
  360. bool MidiMessage::isSustainPedalOff() const noexcept { return isControllerOfType (0x40) && getData()[2] < 64; }
  361. bool MidiMessage::isSostenutoPedalOn() const noexcept { return isControllerOfType (0x42) && getData()[2] >= 64; }
  362. bool MidiMessage::isSostenutoPedalOff() const noexcept { return isControllerOfType (0x42) && getData()[2] < 64; }
  363. bool MidiMessage::isSoftPedalOn() const noexcept { return isControllerOfType (0x43) && getData()[2] >= 64; }
  364. bool MidiMessage::isSoftPedalOff() const noexcept { return isControllerOfType (0x43) && getData()[2] < 64; }
  365. bool MidiMessage::isProgramChange() const noexcept
  366. {
  367. return (getData()[0] & 0xf0) == 0xc0;
  368. }
  369. int MidiMessage::getProgramChangeNumber() const noexcept
  370. {
  371. wassert (isProgramChange());
  372. return getData()[1];
  373. }
  374. MidiMessage MidiMessage::programChange (const int channel, const int programNumber) noexcept
  375. {
  376. wassert (channel > 0 && channel <= 16); // valid channels are numbered 1 to 16
  377. return MidiMessage (MidiHelpers::initialByte (0xc0, channel), programNumber & 0x7f);
  378. }
  379. bool MidiMessage::isPitchWheel() const noexcept
  380. {
  381. return (getData()[0] & 0xf0) == 0xe0;
  382. }
  383. int MidiMessage::getPitchWheelValue() const noexcept
  384. {
  385. wassert (isPitchWheel());
  386. const uint8* const data = getData();
  387. return data[1] | (data[2] << 7);
  388. }
  389. MidiMessage MidiMessage::pitchWheel (const int channel, const int position) noexcept
  390. {
  391. wassert (channel > 0 && channel <= 16); // valid channels are numbered 1 to 16
  392. wassert (isPositiveAndBelow (position, (int) 0x4000));
  393. return MidiMessage (MidiHelpers::initialByte (0xe0, channel),
  394. position & 127, (position >> 7) & 127);
  395. }
  396. bool MidiMessage::isController() const noexcept
  397. {
  398. return (getData()[0] & 0xf0) == 0xb0;
  399. }
  400. bool MidiMessage::isControllerOfType (const int controllerType) const noexcept
  401. {
  402. const uint8* const data = getData();
  403. return (data[0] & 0xf0) == 0xb0 && data[1] == controllerType;
  404. }
  405. int MidiMessage::getControllerNumber() const noexcept
  406. {
  407. wassert (isController());
  408. return getData()[1];
  409. }
  410. int MidiMessage::getControllerValue() const noexcept
  411. {
  412. wassert (isController());
  413. return getData()[2];
  414. }
  415. MidiMessage MidiMessage::controllerEvent (const int channel, const int controllerType, const int value) noexcept
  416. {
  417. // the channel must be between 1 and 16 inclusive
  418. wassert (channel > 0 && channel <= 16);
  419. return MidiMessage (MidiHelpers::initialByte (0xb0, channel),
  420. controllerType & 127, value & 127);
  421. }
  422. MidiMessage MidiMessage::noteOn (const int channel, const int noteNumber, const uint8 velocity) noexcept
  423. {
  424. wassert (channel > 0 && channel <= 16);
  425. wassert (isPositiveAndBelow (noteNumber, (int) 128));
  426. return MidiMessage (MidiHelpers::initialByte (0x90, channel),
  427. noteNumber & 127, MidiHelpers::validVelocity (velocity));
  428. }
  429. MidiMessage MidiMessage::noteOn (const int channel, const int noteNumber, const float velocity) noexcept
  430. {
  431. return noteOn (channel, noteNumber, floatValueToMidiByte (velocity));
  432. }
  433. MidiMessage MidiMessage::noteOff (const int channel, const int noteNumber, uint8 velocity) noexcept
  434. {
  435. wassert (channel > 0 && channel <= 16);
  436. wassert (isPositiveAndBelow (noteNumber, (int) 128));
  437. return MidiMessage (MidiHelpers::initialByte (0x80, channel),
  438. noteNumber & 127, MidiHelpers::validVelocity (velocity));
  439. }
  440. MidiMessage MidiMessage::noteOff (const int channel, const int noteNumber, float velocity) noexcept
  441. {
  442. return noteOff (channel, noteNumber, floatValueToMidiByte (velocity));
  443. }
  444. MidiMessage MidiMessage::noteOff (const int channel, const int noteNumber) noexcept
  445. {
  446. wassert (channel > 0 && channel <= 16);
  447. wassert (isPositiveAndBelow (noteNumber, (int) 128));
  448. return MidiMessage (MidiHelpers::initialByte (0x80, channel), noteNumber & 127, 0);
  449. }
  450. MidiMessage MidiMessage::allNotesOff (const int channel) noexcept
  451. {
  452. return controllerEvent (channel, 123, 0);
  453. }
  454. bool MidiMessage::isAllNotesOff() const noexcept
  455. {
  456. const uint8* const data = getData();
  457. return (data[0] & 0xf0) == 0xb0 && data[1] == 123;
  458. }
  459. MidiMessage MidiMessage::allSoundOff (const int channel) noexcept
  460. {
  461. return controllerEvent (channel, 120, 0);
  462. }
  463. bool MidiMessage::isAllSoundOff() const noexcept
  464. {
  465. const uint8* const data = getData();
  466. return (data[0] & 0xf0) == 0xb0 && data[1] == 120;
  467. }
  468. MidiMessage MidiMessage::allControllersOff (const int channel) noexcept
  469. {
  470. return controllerEvent (channel, 121, 0);
  471. }
  472. MidiMessage MidiMessage::masterVolume (const float volume)
  473. {
  474. const int vol = jlimit (0, 0x3fff, roundToInt (volume * 0x4000));
  475. const uint8 buf[] = { 0xf0, 0x7f, 0x7f, 0x04, 0x01,
  476. (uint8) (vol & 0x7f),
  477. (uint8) (vol >> 7),
  478. 0xf7 };
  479. return MidiMessage (buf, 8);
  480. }
  481. //==============================================================================
  482. bool MidiMessage::isSysEx() const noexcept
  483. {
  484. return *getData() == 0xf0;
  485. }
  486. MidiMessage MidiMessage::createSysExMessage (const void* sysexData, const int dataSize)
  487. {
  488. HeapBlock<uint8> m;
  489. CARLA_SAFE_ASSERT_RETURN(m.malloc((size_t) dataSize + 2U), MidiMessage());
  490. m[0] = 0xf0;
  491. memcpy (m + 1, sysexData, (size_t) dataSize);
  492. m[dataSize + 1] = 0xf7;
  493. return MidiMessage (m, dataSize + 2);
  494. }
  495. const uint8* MidiMessage::getSysExData() const noexcept
  496. {
  497. return isSysEx() ? getData() + 1 : nullptr;
  498. }
  499. int MidiMessage::getSysExDataSize() const noexcept
  500. {
  501. return isSysEx() ? size - 2 : 0;
  502. }
  503. //==============================================================================
  504. bool MidiMessage::isMetaEvent() const noexcept { return *getData() == 0xff; }
  505. bool MidiMessage::isActiveSense() const noexcept { return *getData() == 0xfe; }
  506. int MidiMessage::getMetaEventType() const noexcept
  507. {
  508. const uint8* const data = getData();
  509. return *data != 0xff ? -1 : data[1];
  510. }
  511. int MidiMessage::getMetaEventLength() const noexcept
  512. {
  513. const uint8* const data = getData();
  514. if (*data == 0xff)
  515. {
  516. int n;
  517. return jmin (size - 2, readVariableLengthVal (data + 2, n));
  518. }
  519. return 0;
  520. }
  521. const uint8* MidiMessage::getMetaEventData() const noexcept
  522. {
  523. wassert (isMetaEvent());
  524. int n;
  525. const uint8* d = getData() + 2;
  526. readVariableLengthVal (d, n);
  527. return d + n;
  528. }
  529. bool MidiMessage::isTempoMetaEvent() const noexcept { const uint8* data = getData(); return (data[1] == 81) && (*data == 0xff); }
  530. bool MidiMessage::isMidiChannelMetaEvent() const noexcept { const uint8* data = getData(); return (data[1] == 0x20) && (*data == 0xff) && (data[2] == 1); }
  531. int MidiMessage::getMidiChannelMetaEventChannel() const noexcept
  532. {
  533. wassert (isMidiChannelMetaEvent());
  534. return getData()[3] + 1;
  535. }
  536. double MidiMessage::getTempoSecondsPerQuarterNote() const noexcept
  537. {
  538. if (! isTempoMetaEvent())
  539. return 0.0;
  540. const uint8* const d = getMetaEventData();
  541. return (((unsigned int) d[0] << 16)
  542. | ((unsigned int) d[1] << 8)
  543. | d[2])
  544. / 1000000.0;
  545. }
  546. double MidiMessage::getTempoMetaEventTickLength (const short timeFormat) const noexcept
  547. {
  548. if (timeFormat > 0)
  549. {
  550. if (! isTempoMetaEvent())
  551. return 0.5 / timeFormat;
  552. return getTempoSecondsPerQuarterNote() / timeFormat;
  553. }
  554. else
  555. {
  556. const int frameCode = (-timeFormat) >> 8;
  557. double framesPerSecond;
  558. switch (frameCode)
  559. {
  560. case 24: framesPerSecond = 24.0; break;
  561. case 25: framesPerSecond = 25.0; break;
  562. case 29: framesPerSecond = 29.97; break;
  563. case 30: framesPerSecond = 30.0; break;
  564. default: framesPerSecond = 30.0; break;
  565. }
  566. return (1.0 / framesPerSecond) / (timeFormat & 0xff);
  567. }
  568. }
  569. MidiMessage MidiMessage::tempoMetaEvent (int microsecondsPerQuarterNote) noexcept
  570. {
  571. const uint8 d[] = { 0xff, 81, 3,
  572. (uint8) (microsecondsPerQuarterNote >> 16),
  573. (uint8) (microsecondsPerQuarterNote >> 8),
  574. (uint8) microsecondsPerQuarterNote };
  575. return MidiMessage (d, 6, 0.0);
  576. }
  577. bool MidiMessage::isTimeSignatureMetaEvent() const noexcept
  578. {
  579. const uint8* const data = getData();
  580. return (data[1] == 0x58) && (*data == (uint8) 0xff);
  581. }
  582. void MidiMessage::getTimeSignatureInfo (int& numerator, int& denominator) const noexcept
  583. {
  584. if (isTimeSignatureMetaEvent())
  585. {
  586. const uint8* const d = getMetaEventData();
  587. numerator = d[0];
  588. denominator = 1 << d[1];
  589. }
  590. else
  591. {
  592. numerator = 4;
  593. denominator = 4;
  594. }
  595. }
  596. MidiMessage MidiMessage::timeSignatureMetaEvent (const int numerator, const int denominator)
  597. {
  598. int n = 1;
  599. int powerOfTwo = 0;
  600. while (n < denominator)
  601. {
  602. n <<= 1;
  603. ++powerOfTwo;
  604. }
  605. const uint8 d[] = { 0xff, 0x58, 0x04, (uint8) numerator, (uint8) powerOfTwo, 1, 96 };
  606. return MidiMessage (d, 7, 0.0);
  607. }
  608. MidiMessage MidiMessage::midiChannelMetaEvent (const int channel) noexcept
  609. {
  610. const uint8 d[] = { 0xff, 0x20, 0x01, (uint8) jlimit (0, 0xff, channel - 1) };
  611. return MidiMessage (d, 4, 0.0);
  612. }
  613. bool MidiMessage::isKeySignatureMetaEvent() const noexcept
  614. {
  615. return getMetaEventType() == 0x59;
  616. }
  617. int MidiMessage::getKeySignatureNumberOfSharpsOrFlats() const noexcept
  618. {
  619. return (int) (int8) getMetaEventData()[0];
  620. }
  621. bool MidiMessage::isKeySignatureMajorKey() const noexcept
  622. {
  623. return getMetaEventData()[1] == 0;
  624. }
  625. MidiMessage MidiMessage::keySignatureMetaEvent (int numberOfSharpsOrFlats, bool isMinorKey)
  626. {
  627. wassert (numberOfSharpsOrFlats >= -7 && numberOfSharpsOrFlats <= 7);
  628. const uint8 d[] = { 0xff, 0x59, 0x02, (uint8) numberOfSharpsOrFlats, isMinorKey ? (uint8) 1 : (uint8) 0 };
  629. return MidiMessage (d, 5, 0.0);
  630. }
  631. //==============================================================================
  632. bool MidiMessage::isSongPositionPointer() const noexcept { return *getData() == 0xf2; }
  633. int MidiMessage::getSongPositionPointerMidiBeat() const noexcept { const uint8* data = getData(); return data[1] | (data[2] << 7); }
  634. MidiMessage MidiMessage::songPositionPointer (const int positionInMidiBeats) noexcept
  635. {
  636. return MidiMessage (0xf2,
  637. positionInMidiBeats & 127,
  638. (positionInMidiBeats >> 7) & 127);
  639. }
  640. bool MidiMessage::isMidiStart() const noexcept { return *getData() == 0xfa; }
  641. MidiMessage MidiMessage::midiStart() noexcept { return MidiMessage (0xfa); }
  642. bool MidiMessage::isMidiContinue() const noexcept { return *getData() == 0xfb; }
  643. MidiMessage MidiMessage::midiContinue() noexcept { return MidiMessage (0xfb); }
  644. bool MidiMessage::isMidiStop() const noexcept { return *getData() == 0xfc; }
  645. MidiMessage MidiMessage::midiStop() noexcept { return MidiMessage (0xfc); }
  646. bool MidiMessage::isMidiClock() const noexcept { return *getData() == 0xf8; }
  647. MidiMessage MidiMessage::midiClock() noexcept { return MidiMessage (0xf8); }
  648. bool MidiMessage::isQuarterFrame() const noexcept { return *getData() == 0xf1; }
  649. int MidiMessage::getQuarterFrameSequenceNumber() const noexcept { return ((int) getData()[1]) >> 4; }
  650. int MidiMessage::getQuarterFrameValue() const noexcept { return ((int) getData()[1]) & 0x0f; }
  651. MidiMessage MidiMessage::quarterFrame (const int sequenceNumber, const int value) noexcept
  652. {
  653. return MidiMessage (0xf1, (sequenceNumber << 4) | value);
  654. }
  655. bool MidiMessage::isFullFrame() const noexcept
  656. {
  657. const uint8* const data = getData();
  658. return data[0] == 0xf0
  659. && data[1] == 0x7f
  660. && size >= 10
  661. && data[3] == 0x01
  662. && data[4] == 0x01;
  663. }
  664. void MidiMessage::getFullFrameParameters (int& hours, int& minutes, int& seconds, int& frames,
  665. MidiMessage::SmpteTimecodeType& timecodeType) const noexcept
  666. {
  667. wassert (isFullFrame());
  668. const uint8* const data = getData();
  669. timecodeType = (SmpteTimecodeType) (data[5] >> 5);
  670. hours = data[5] & 0x1f;
  671. minutes = data[6];
  672. seconds = data[7];
  673. frames = data[8];
  674. }
  675. MidiMessage MidiMessage::fullFrame (const int hours, const int minutes,
  676. const int seconds, const int frames,
  677. MidiMessage::SmpteTimecodeType timecodeType)
  678. {
  679. const uint8 d[] = { 0xf0, 0x7f, 0x7f, 0x01, 0x01,
  680. (uint8) ((hours & 0x01f) | (timecodeType << 5)),
  681. (uint8) minutes,
  682. (uint8) seconds,
  683. (uint8) frames,
  684. 0xf7 };
  685. return MidiMessage (d, 10, 0.0);
  686. }
  687. bool MidiMessage::isMidiMachineControlMessage() const noexcept
  688. {
  689. const uint8* const data = getData();
  690. return data[0] == 0xf0
  691. && data[1] == 0x7f
  692. && data[3] == 0x06
  693. && size > 5;
  694. }
  695. MidiMessage::MidiMachineControlCommand MidiMessage::getMidiMachineControlCommand() const noexcept
  696. {
  697. wassert (isMidiMachineControlMessage());
  698. return (MidiMachineControlCommand) getData()[4];
  699. }
  700. MidiMessage MidiMessage::midiMachineControlCommand (MidiMessage::MidiMachineControlCommand command)
  701. {
  702. const uint8 d[] = { 0xf0, 0x7f, 0, 6, (uint8) command, 0xf7 };
  703. return MidiMessage (d, 6, 0.0);
  704. }
  705. //==============================================================================
  706. bool MidiMessage::isMidiMachineControlGoto (int& hours, int& minutes, int& seconds, int& frames) const noexcept
  707. {
  708. const uint8* const data = getData();
  709. if (size >= 12
  710. && data[0] == 0xf0
  711. && data[1] == 0x7f
  712. && data[3] == 0x06
  713. && data[4] == 0x44
  714. && data[5] == 0x06
  715. && data[6] == 0x01)
  716. {
  717. hours = data[7] % 24; // (that some machines send out hours > 24)
  718. minutes = data[8];
  719. seconds = data[9];
  720. frames = data[10];
  721. return true;
  722. }
  723. return false;
  724. }
  725. MidiMessage MidiMessage::midiMachineControlGoto (int hours, int minutes, int seconds, int frames)
  726. {
  727. const uint8 d[] = { 0xf0, 0x7f, 0, 6, 0x44, 6, 1,
  728. (uint8) hours,
  729. (uint8) minutes,
  730. (uint8) seconds,
  731. (uint8) frames,
  732. 0xf7 };
  733. return MidiMessage (d, 12, 0.0);
  734. }
  735. //==============================================================================
  736. double MidiMessage::getMidiNoteInHertz (const int noteNumber, const double frequencyOfA) noexcept
  737. {
  738. return frequencyOfA * pow (2.0, (noteNumber - 69) / 12.0);
  739. }
  740. bool MidiMessage::isMidiNoteBlack (int noteNumber) noexcept
  741. {
  742. return ((1 << (noteNumber % 12)) & 0x054a) != 0;
  743. }
  744. }