The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

1050 lines
34KB

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