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.

1036 lines
34KB

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