Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

juce_MidiMessage.cpp 34KB

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