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.

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