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.

939 lines
37KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. #pragma once
  18. //==============================================================================
  19. /**
  20. Encapsulates a MIDI message.
  21. @see MidiMessageSequence, MidiOutput, MidiInput
  22. */
  23. class JUCE_API MidiMessage
  24. {
  25. public:
  26. //==============================================================================
  27. /** Creates a 3-byte short midi message.
  28. @param byte1 message byte 1
  29. @param byte2 message byte 2
  30. @param byte3 message byte 3
  31. @param timeStamp the time to give the midi message - this value doesn't
  32. use any particular units, so will be application-specific
  33. */
  34. MidiMessage (int byte1, int byte2, int byte3, double timeStamp = 0) noexcept;
  35. /** Creates a 2-byte short midi message.
  36. @param byte1 message byte 1
  37. @param byte2 message byte 2
  38. @param timeStamp the time to give the midi message - this value doesn't
  39. use any particular units, so will be application-specific
  40. */
  41. MidiMessage (int byte1, int byte2, double timeStamp = 0) noexcept;
  42. /** Creates a 1-byte short midi message.
  43. @param byte1 message byte 1
  44. @param timeStamp the time to give the midi message - this value doesn't
  45. use any particular units, so will be application-specific
  46. */
  47. MidiMessage (int byte1, double timeStamp = 0) noexcept;
  48. /** Creates a midi message from a list of bytes. */
  49. template <typename... Data>
  50. MidiMessage (int byte1, int byte2, int byte3, Data... otherBytes) : size (3 + sizeof... (otherBytes))
  51. {
  52. // this checks that the length matches the data..
  53. jassert (size > 3 || byte1 >= 0xf0 || getMessageLengthFromFirstByte ((uint8) byte1) == size);
  54. const uint8 data[] = { (uint8) byte1, (uint8) byte2, (uint8) byte3, static_cast<uint8> (otherBytes)... };
  55. memcpy (allocateSpace (size), data, (size_t) size);
  56. }
  57. /** Creates a midi message from a block of data. */
  58. MidiMessage (const void* data, int numBytes, double timeStamp = 0);
  59. /** Reads the next midi message from some data.
  60. This will read as many bytes from a data stream as it needs to make a
  61. complete message, and will return the number of bytes it used. This lets
  62. you read a sequence of midi messages from a file or stream.
  63. @param data the data to read from
  64. @param maxBytesToUse the maximum number of bytes it's allowed to read
  65. @param numBytesUsed returns the number of bytes that were actually needed
  66. @param lastStatusByte in a sequence of midi messages, the initial byte
  67. can be dropped from a message if it's the same as the
  68. first byte of the previous message, so this lets you
  69. supply the byte to use if the first byte of the message
  70. has in fact been dropped.
  71. @param timeStamp the time to give the midi message - this value doesn't
  72. use any particular units, so will be application-specific
  73. @param sysexHasEmbeddedLength when reading sysexes, this flag indicates whether
  74. to expect the data to begin with a variable-length field
  75. indicating its size
  76. */
  77. MidiMessage (const void* data, int maxBytesToUse,
  78. int& numBytesUsed, uint8 lastStatusByte,
  79. double timeStamp = 0,
  80. bool sysexHasEmbeddedLength = true);
  81. /** Creates an active-sense message.
  82. Since the MidiMessage has to contain a valid message, this default constructor
  83. just initialises it with an empty sysex message.
  84. */
  85. MidiMessage() noexcept;
  86. /** Creates a copy of another midi message. */
  87. MidiMessage (const MidiMessage&);
  88. /** Creates a copy of another midi message, with a different timestamp. */
  89. MidiMessage (const MidiMessage&, double newTimeStamp);
  90. /** Destructor. */
  91. ~MidiMessage() noexcept;
  92. /** Copies this message from another one. */
  93. MidiMessage& operator= (const MidiMessage& other);
  94. /** Move constructor */
  95. MidiMessage (MidiMessage&&) noexcept;
  96. /** Move assignment operator */
  97. MidiMessage& operator= (MidiMessage&&) noexcept;
  98. //==============================================================================
  99. /** Returns a pointer to the raw midi data.
  100. @see getRawDataSize
  101. */
  102. const uint8* getRawData() const noexcept { return getData(); }
  103. /** Returns the number of bytes of data in the message.
  104. @see getRawData
  105. */
  106. int getRawDataSize() const noexcept { return size; }
  107. //==============================================================================
  108. /** Returns a human-readable description of the midi message as a string,
  109. for example "Note On C#3 Velocity 120 Channel 1".
  110. */
  111. String getDescription() const;
  112. //==============================================================================
  113. /** Returns the timestamp associated with this message.
  114. The exact meaning of this time and its units will vary, as messages are used in
  115. a variety of different contexts.
  116. If you're getting the message from a midi file, this could be a time in seconds, or
  117. a number of ticks - see MidiFile::convertTimestampTicksToSeconds().
  118. If the message is being used in a MidiBuffer, it might indicate the number of
  119. audio samples from the start of the buffer.
  120. If the message was created by a MidiInput, see MidiInputCallback::handleIncomingMidiMessage()
  121. for details of the way that it initialises this value.
  122. @see setTimeStamp, addToTimeStamp
  123. */
  124. double getTimeStamp() const noexcept { return timeStamp; }
  125. /** Changes the message's associated timestamp.
  126. The units for the timestamp will be application-specific - see the notes for getTimeStamp().
  127. @see addToTimeStamp, getTimeStamp
  128. */
  129. void setTimeStamp (double newTimestamp) noexcept { timeStamp = newTimestamp; }
  130. /** Adds a value to the message's timestamp.
  131. The units for the timestamp will be application-specific.
  132. */
  133. void addToTimeStamp (double delta) noexcept { timeStamp += delta; }
  134. //==============================================================================
  135. /** Returns the midi channel associated with the message.
  136. @returns a value 1 to 16 if the message has a channel, or 0 if it hasn't (e.g.
  137. if it's a sysex)
  138. @see isForChannel, setChannel
  139. */
  140. int getChannel() const noexcept;
  141. /** Returns true if the message applies to the given midi channel.
  142. @param channelNumber the channel number to look for, in the range 1 to 16
  143. @see getChannel, setChannel
  144. */
  145. bool isForChannel (int channelNumber) const noexcept;
  146. /** Changes the message's midi channel.
  147. This won't do anything for non-channel messages like sysexes.
  148. @param newChannelNumber the channel number to change it to, in the range 1 to 16
  149. */
  150. void setChannel (int newChannelNumber) noexcept;
  151. //==============================================================================
  152. /** Returns true if this is a system-exclusive message.
  153. */
  154. bool isSysEx() const noexcept;
  155. /** Returns a pointer to the sysex data inside the message.
  156. If this event isn't a sysex event, it'll return 0.
  157. @see getSysExDataSize
  158. */
  159. const uint8* getSysExData() const noexcept;
  160. /** Returns the size of the sysex data.
  161. This value excludes the 0xf0 header byte and the 0xf7 at the end.
  162. @see getSysExData
  163. */
  164. int getSysExDataSize() const noexcept;
  165. //==============================================================================
  166. /** Returns true if this message is a 'key-down' event.
  167. @param returnTrueForVelocity0 if true, then if this event is a note-on with
  168. velocity 0, it will still be considered to be a note-on and the
  169. method will return true. If returnTrueForVelocity0 is false, then
  170. if this is a note-on event with velocity 0, it'll be regarded as
  171. a note-off, and the method will return false
  172. @see isNoteOff, getNoteNumber, getVelocity, noteOn
  173. */
  174. bool isNoteOn (bool returnTrueForVelocity0 = false) const noexcept;
  175. /** Creates a key-down message (using a floating-point velocity).
  176. @param channel the midi channel, in the range 1 to 16
  177. @param noteNumber the key number, 0 to 127
  178. @param velocity in the range 0 to 1.0
  179. @see isNoteOn
  180. */
  181. static MidiMessage noteOn (int channel, int noteNumber, float velocity) noexcept;
  182. /** Creates a key-down message (using an integer velocity).
  183. @param channel the midi channel, in the range 1 to 16
  184. @param noteNumber the key number, 0 to 127
  185. @param velocity in the range 0 to 127
  186. @see isNoteOn
  187. */
  188. static MidiMessage noteOn (int channel, int noteNumber, uint8 velocity) noexcept;
  189. /** Returns true if this message is a 'key-up' event.
  190. If returnTrueForNoteOnVelocity0 is true, then his will also return true
  191. for a note-on event with a velocity of 0.
  192. @see isNoteOn, getNoteNumber, getVelocity, noteOff
  193. */
  194. bool isNoteOff (bool returnTrueForNoteOnVelocity0 = true) const noexcept;
  195. /** Creates a key-up message.
  196. @param channel the midi channel, in the range 1 to 16
  197. @param noteNumber the key number, 0 to 127
  198. @param velocity in the range 0 to 1.0
  199. @see isNoteOff
  200. */
  201. static MidiMessage noteOff (int channel, int noteNumber, float velocity) noexcept;
  202. /** Creates a key-up message.
  203. @param channel the midi channel, in the range 1 to 16
  204. @param noteNumber the key number, 0 to 127
  205. @param velocity in the range 0 to 127
  206. @see isNoteOff
  207. */
  208. static MidiMessage noteOff (int channel, int noteNumber, uint8 velocity) noexcept;
  209. /** Creates a key-up message.
  210. @param channel the midi channel, in the range 1 to 16
  211. @param noteNumber the key number, 0 to 127
  212. @see isNoteOff
  213. */
  214. static MidiMessage noteOff (int channel, int noteNumber) noexcept;
  215. /** Returns true if this message is a 'key-down' or 'key-up' event.
  216. @see isNoteOn, isNoteOff
  217. */
  218. bool isNoteOnOrOff() const noexcept;
  219. /** Returns the midi note number for note-on and note-off messages.
  220. If the message isn't a note-on or off, the value returned is undefined.
  221. @see isNoteOff, getMidiNoteName, getMidiNoteInHertz, setNoteNumber
  222. */
  223. int getNoteNumber() const noexcept;
  224. /** Changes the midi note number of a note-on or note-off message.
  225. If the message isn't a note on or off, this will do nothing.
  226. */
  227. void setNoteNumber (int newNoteNumber) noexcept;
  228. //==============================================================================
  229. /** Returns the velocity of a note-on or note-off message.
  230. The value returned will be in the range 0 to 127.
  231. If the message isn't a note-on or off event, it will return 0.
  232. @see getFloatVelocity
  233. */
  234. uint8 getVelocity() const noexcept;
  235. /** Returns the velocity of a note-on or note-off message.
  236. The value returned will be in the range 0 to 1.0
  237. If the message isn't a note-on or off event, it will return 0.
  238. @see getVelocity, setVelocity
  239. */
  240. float getFloatVelocity() const noexcept;
  241. /** Changes the velocity of a note-on or note-off message.
  242. If the message isn't a note on or off, this will do nothing.
  243. @param newVelocity the new velocity, in the range 0 to 1.0
  244. @see getFloatVelocity, multiplyVelocity
  245. */
  246. void setVelocity (float newVelocity) noexcept;
  247. /** Multiplies the velocity of a note-on or note-off message by a given amount.
  248. If the message isn't a note on or off, this will do nothing.
  249. @param scaleFactor the value by which to multiply the velocity
  250. @see setVelocity
  251. */
  252. void multiplyVelocity (float scaleFactor) noexcept;
  253. //==============================================================================
  254. /** Returns true if this message is a 'sustain pedal down' controller message. */
  255. bool isSustainPedalOn() const noexcept;
  256. /** Returns true if this message is a 'sustain pedal up' controller message. */
  257. bool isSustainPedalOff() const noexcept;
  258. /** Returns true if this message is a 'sostenuto pedal down' controller message. */
  259. bool isSostenutoPedalOn() const noexcept;
  260. /** Returns true if this message is a 'sostenuto pedal up' controller message. */
  261. bool isSostenutoPedalOff() const noexcept;
  262. /** Returns true if this message is a 'soft pedal down' controller message. */
  263. bool isSoftPedalOn() const noexcept;
  264. /** Returns true if this message is a 'soft pedal up' controller message. */
  265. bool isSoftPedalOff() const noexcept;
  266. //==============================================================================
  267. /** Returns true if the message is a program (patch) change message.
  268. @see getProgramChangeNumber, getGMInstrumentName
  269. */
  270. bool isProgramChange() const noexcept;
  271. /** Returns the new program number of a program change message.
  272. If the message isn't a program change, the value returned is undefined.
  273. @see isProgramChange, getGMInstrumentName
  274. */
  275. int getProgramChangeNumber() const noexcept;
  276. /** Creates a program-change message.
  277. @param channel the midi channel, in the range 1 to 16
  278. @param programNumber the midi program number, 0 to 127
  279. @see isProgramChange, getGMInstrumentName
  280. */
  281. static MidiMessage programChange (int channel, int programNumber) noexcept;
  282. //==============================================================================
  283. /** Returns true if the message is a pitch-wheel move.
  284. @see getPitchWheelValue, pitchWheel
  285. */
  286. bool isPitchWheel() const noexcept;
  287. /** Returns the pitch wheel position from a pitch-wheel move message.
  288. The value returned is a 14-bit number from 0 to 0x3fff, indicating the wheel position.
  289. If called for messages which aren't pitch wheel events, the number returned will be
  290. nonsense.
  291. @see isPitchWheel
  292. */
  293. int getPitchWheelValue() const noexcept;
  294. /** Creates a pitch-wheel move message.
  295. @param channel the midi channel, in the range 1 to 16
  296. @param position the wheel position, in the range 0 to 16383
  297. @see isPitchWheel
  298. */
  299. static MidiMessage pitchWheel (int channel, int position) noexcept;
  300. //==============================================================================
  301. /** Returns true if the message is an aftertouch event.
  302. For aftertouch events, use the getNoteNumber() method to find out the key
  303. that it applies to, and getAftertouchValue() to find out the amount. Use
  304. getChannel() to find out the channel.
  305. @see getAftertouchValue, getNoteNumber
  306. */
  307. bool isAftertouch() const noexcept;
  308. /** Returns the amount of aftertouch from an aftertouch messages.
  309. The value returned is in the range 0 to 127, and will be nonsense for messages
  310. other than aftertouch messages.
  311. @see isAftertouch
  312. */
  313. int getAfterTouchValue() const noexcept;
  314. /** Creates an aftertouch message.
  315. @param channel the midi channel, in the range 1 to 16
  316. @param noteNumber the key number, 0 to 127
  317. @param aftertouchAmount the amount of aftertouch, 0 to 127
  318. @see isAftertouch
  319. */
  320. static MidiMessage aftertouchChange (int channel,
  321. int noteNumber,
  322. int aftertouchAmount) noexcept;
  323. /** Returns true if the message is a channel-pressure change event.
  324. This is like aftertouch, but common to the whole channel rather than a specific
  325. note. Use getChannelPressureValue() to find out the pressure, and getChannel()
  326. to find out the channel.
  327. @see channelPressureChange
  328. */
  329. bool isChannelPressure() const noexcept;
  330. /** Returns the pressure from a channel pressure change message.
  331. @returns the pressure, in the range 0 to 127
  332. @see isChannelPressure, channelPressureChange
  333. */
  334. int getChannelPressureValue() const noexcept;
  335. /** Creates a channel-pressure change event.
  336. @param channel the midi channel: 1 to 16
  337. @param pressure the pressure, 0 to 127
  338. @see isChannelPressure
  339. */
  340. static MidiMessage channelPressureChange (int channel, int pressure) noexcept;
  341. //==============================================================================
  342. /** Returns true if this is a midi controller message.
  343. @see getControllerNumber, getControllerValue, controllerEvent
  344. */
  345. bool isController() const noexcept;
  346. /** Returns the controller number of a controller message.
  347. The name of the controller can be looked up using the getControllerName() method.
  348. Note that the value returned is invalid for messages that aren't controller changes.
  349. @see isController, getControllerName, getControllerValue
  350. */
  351. int getControllerNumber() const noexcept;
  352. /** Returns the controller value from a controller message.
  353. A value 0 to 127 is returned to indicate the new controller position.
  354. Note that the value returned is invalid for messages that aren't controller changes.
  355. @see isController, getControllerNumber
  356. */
  357. int getControllerValue() const noexcept;
  358. /** Returns true if this message is a controller message and if it has the specified
  359. controller type.
  360. */
  361. bool isControllerOfType (int controllerType) const noexcept;
  362. /** Creates a controller message.
  363. @param channel the midi channel, in the range 1 to 16
  364. @param controllerType the type of controller
  365. @param value the controller value
  366. @see isController
  367. */
  368. static MidiMessage controllerEvent (int channel,
  369. int controllerType,
  370. int value) noexcept;
  371. /** Checks whether this message is an all-notes-off message.
  372. @see allNotesOff
  373. */
  374. bool isAllNotesOff() const noexcept;
  375. /** Checks whether this message is an all-sound-off message.
  376. @see allSoundOff
  377. */
  378. bool isAllSoundOff() const noexcept;
  379. /** Creates an all-notes-off message.
  380. @param channel the midi channel, in the range 1 to 16
  381. @see isAllNotesOff
  382. */
  383. static MidiMessage allNotesOff (int channel) noexcept;
  384. /** Creates an all-sound-off message.
  385. @param channel the midi channel, in the range 1 to 16
  386. @see isAllSoundOff
  387. */
  388. static MidiMessage allSoundOff (int channel) noexcept;
  389. /** Creates an all-controllers-off message.
  390. @param channel the midi channel, in the range 1 to 16
  391. */
  392. static MidiMessage allControllersOff (int channel) noexcept;
  393. //==============================================================================
  394. /** Returns true if this event is a meta-event.
  395. Meta-events are things like tempo changes, track names, etc.
  396. @see getMetaEventType, isTrackMetaEvent, isEndOfTrackMetaEvent,
  397. isTextMetaEvent, isTrackNameEvent, isTempoMetaEvent, isTimeSignatureMetaEvent,
  398. isKeySignatureMetaEvent, isMidiChannelMetaEvent
  399. */
  400. bool isMetaEvent() const noexcept;
  401. /** Returns a meta-event's type number.
  402. If the message isn't a meta-event, this will return -1.
  403. @see isMetaEvent, isTrackMetaEvent, isEndOfTrackMetaEvent,
  404. isTextMetaEvent, isTrackNameEvent, isTempoMetaEvent, isTimeSignatureMetaEvent,
  405. isKeySignatureMetaEvent, isMidiChannelMetaEvent
  406. */
  407. int getMetaEventType() const noexcept;
  408. /** Returns a pointer to the data in a meta-event.
  409. @see isMetaEvent, getMetaEventLength
  410. */
  411. const uint8* getMetaEventData() const noexcept;
  412. /** Returns the length of the data for a meta-event.
  413. @see isMetaEvent, getMetaEventData
  414. */
  415. int getMetaEventLength() const noexcept;
  416. //==============================================================================
  417. /** Returns true if this is a 'track' meta-event. */
  418. bool isTrackMetaEvent() const noexcept;
  419. /** Returns true if this is an 'end-of-track' meta-event. */
  420. bool isEndOfTrackMetaEvent() const noexcept;
  421. /** Creates an end-of-track meta-event.
  422. @see isEndOfTrackMetaEvent
  423. */
  424. static MidiMessage endOfTrack() noexcept;
  425. /** Returns true if this is an 'track name' meta-event.
  426. You can use the getTextFromTextMetaEvent() method to get the track's name.
  427. */
  428. bool isTrackNameEvent() const noexcept;
  429. /** Returns true if this is a 'text' meta-event.
  430. @see getTextFromTextMetaEvent
  431. */
  432. bool isTextMetaEvent() const noexcept;
  433. /** Returns the text from a text meta-event.
  434. @see isTextMetaEvent
  435. */
  436. String getTextFromTextMetaEvent() const;
  437. /** Creates a text meta-event. */
  438. static MidiMessage textMetaEvent (int type, StringRef text);
  439. //==============================================================================
  440. /** Returns true if this is a 'tempo' meta-event.
  441. @see getTempoMetaEventTickLength, getTempoSecondsPerQuarterNote
  442. */
  443. bool isTempoMetaEvent() const noexcept;
  444. /** Returns the tick length from a tempo meta-event.
  445. @param timeFormat the 16-bit time format value from the midi file's header.
  446. @returns the tick length (in seconds).
  447. @see isTempoMetaEvent
  448. */
  449. double getTempoMetaEventTickLength (short timeFormat) const noexcept;
  450. /** Calculates the seconds-per-quarter-note from a tempo meta-event.
  451. @see isTempoMetaEvent, getTempoMetaEventTickLength
  452. */
  453. double getTempoSecondsPerQuarterNote() const noexcept;
  454. /** Creates a tempo meta-event.
  455. @see isTempoMetaEvent
  456. */
  457. static MidiMessage tempoMetaEvent (int microsecondsPerQuarterNote) noexcept;
  458. //==============================================================================
  459. /** Returns true if this is a 'time-signature' meta-event.
  460. @see getTimeSignatureInfo
  461. */
  462. bool isTimeSignatureMetaEvent() const noexcept;
  463. /** Returns the time-signature values from a time-signature meta-event.
  464. @see isTimeSignatureMetaEvent
  465. */
  466. void getTimeSignatureInfo (int& numerator, int& denominator) const noexcept;
  467. /** Creates a time-signature meta-event.
  468. @see isTimeSignatureMetaEvent
  469. */
  470. static MidiMessage timeSignatureMetaEvent (int numerator, int denominator);
  471. //==============================================================================
  472. /** Returns true if this is a 'key-signature' meta-event.
  473. @see getKeySignatureNumberOfSharpsOrFlats, isKeySignatureMajorKey
  474. */
  475. bool isKeySignatureMetaEvent() const noexcept;
  476. /** Returns the key from a key-signature meta-event.
  477. This method must only be called if isKeySignatureMetaEvent() is true.
  478. A positive number here indicates the number of sharps in the key signature,
  479. and a negative number indicates a number of flats. So e.g. 3 = F# + C# + G#,
  480. -2 = Bb + Eb
  481. @see isKeySignatureMetaEvent, isKeySignatureMajorKey
  482. */
  483. int getKeySignatureNumberOfSharpsOrFlats() const noexcept;
  484. /** Returns true if this key-signature event is major, or false if it's minor.
  485. This method must only be called if isKeySignatureMetaEvent() is true.
  486. */
  487. bool isKeySignatureMajorKey() const noexcept;
  488. /** Creates a key-signature meta-event.
  489. @param numberOfSharpsOrFlats if positive, this indicates the number of sharps
  490. in the key; if negative, the number of flats
  491. @param isMinorKey if true, the key is minor; if false, it is major
  492. @see isKeySignatureMetaEvent
  493. */
  494. static MidiMessage keySignatureMetaEvent (int numberOfSharpsOrFlats, bool isMinorKey);
  495. //==============================================================================
  496. /** Returns true if this is a 'channel' meta-event.
  497. A channel meta-event specifies the midi channel that should be used
  498. for subsequent meta-events.
  499. @see getMidiChannelMetaEventChannel
  500. */
  501. bool isMidiChannelMetaEvent() const noexcept;
  502. /** Returns the channel number from a channel meta-event.
  503. @returns the channel, in the range 1 to 16.
  504. @see isMidiChannelMetaEvent
  505. */
  506. int getMidiChannelMetaEventChannel() const noexcept;
  507. /** Creates a midi channel meta-event.
  508. @param channel the midi channel, in the range 1 to 16
  509. @see isMidiChannelMetaEvent
  510. */
  511. static MidiMessage midiChannelMetaEvent (int channel) noexcept;
  512. //==============================================================================
  513. /** Returns true if this is an active-sense message. */
  514. bool isActiveSense() const noexcept;
  515. //==============================================================================
  516. /** Returns true if this is a midi start event.
  517. @see midiStart
  518. */
  519. bool isMidiStart() const noexcept;
  520. /** Creates a midi start event. */
  521. static MidiMessage midiStart() noexcept;
  522. /** Returns true if this is a midi continue event.
  523. @see midiContinue
  524. */
  525. bool isMidiContinue() const noexcept;
  526. /** Creates a midi continue event. */
  527. static MidiMessage midiContinue() noexcept;
  528. /** Returns true if this is a midi stop event.
  529. @see midiStop
  530. */
  531. bool isMidiStop() const noexcept;
  532. /** Creates a midi stop event. */
  533. static MidiMessage midiStop() noexcept;
  534. /** Returns true if this is a midi clock event.
  535. @see midiClock, songPositionPointer
  536. */
  537. bool isMidiClock() const noexcept;
  538. /** Creates a midi clock event. */
  539. static MidiMessage midiClock() noexcept;
  540. /** Returns true if this is a song-position-pointer message.
  541. @see getSongPositionPointerMidiBeat, songPositionPointer
  542. */
  543. bool isSongPositionPointer() const noexcept;
  544. /** Returns the midi beat-number of a song-position-pointer message.
  545. @see isSongPositionPointer, songPositionPointer
  546. */
  547. int getSongPositionPointerMidiBeat() const noexcept;
  548. /** Creates a song-position-pointer message.
  549. The position is a number of midi beats from the start of the song, where 1 midi
  550. beat is 6 midi clocks, and there are 24 midi clocks in a quarter-note. So there
  551. are 4 midi beats in a quarter-note.
  552. @see isSongPositionPointer, getSongPositionPointerMidiBeat
  553. */
  554. static MidiMessage songPositionPointer (int positionInMidiBeats) noexcept;
  555. //==============================================================================
  556. /** Returns true if this is a quarter-frame midi timecode message.
  557. @see quarterFrame, getQuarterFrameSequenceNumber, getQuarterFrameValue
  558. */
  559. bool isQuarterFrame() const noexcept;
  560. /** Returns the sequence number of a quarter-frame midi timecode message.
  561. This will be a value between 0 and 7.
  562. @see isQuarterFrame, getQuarterFrameValue, quarterFrame
  563. */
  564. int getQuarterFrameSequenceNumber() const noexcept;
  565. /** Returns the value from a quarter-frame message.
  566. This will be the lower nybble of the message's data-byte, a value between 0 and 15
  567. */
  568. int getQuarterFrameValue() const noexcept;
  569. /** Creates a quarter-frame MTC message.
  570. @param sequenceNumber a value 0 to 7 for the upper nybble of the message's data byte
  571. @param value a value 0 to 15 for the lower nybble of the message's data byte
  572. */
  573. static MidiMessage quarterFrame (int sequenceNumber, int value) noexcept;
  574. /** SMPTE timecode types.
  575. Used by the getFullFrameParameters() and fullFrame() methods.
  576. */
  577. enum SmpteTimecodeType
  578. {
  579. fps24 = 0,
  580. fps25 = 1,
  581. fps30drop = 2,
  582. fps30 = 3
  583. };
  584. /** Returns true if this is a full-frame midi timecode message. */
  585. bool isFullFrame() const noexcept;
  586. /** Extracts the timecode information from a full-frame midi timecode message.
  587. You should only call this on messages where you've used isFullFrame() to
  588. check that they're the right kind.
  589. */
  590. void getFullFrameParameters (int& hours,
  591. int& minutes,
  592. int& seconds,
  593. int& frames,
  594. SmpteTimecodeType& timecodeType) const noexcept;
  595. /** Creates a full-frame MTC message. */
  596. static MidiMessage fullFrame (int hours,
  597. int minutes,
  598. int seconds,
  599. int frames,
  600. SmpteTimecodeType timecodeType);
  601. //==============================================================================
  602. /** Types of MMC command.
  603. @see isMidiMachineControlMessage, getMidiMachineControlCommand, midiMachineControlCommand
  604. */
  605. enum MidiMachineControlCommand
  606. {
  607. mmc_stop = 1,
  608. mmc_play = 2,
  609. mmc_deferredplay = 3,
  610. mmc_fastforward = 4,
  611. mmc_rewind = 5,
  612. mmc_recordStart = 6,
  613. mmc_recordStop = 7,
  614. mmc_pause = 9
  615. };
  616. /** Checks whether this is an MMC message.
  617. If it is, you can use the getMidiMachineControlCommand() to find out its type.
  618. */
  619. bool isMidiMachineControlMessage() const noexcept;
  620. /** For an MMC message, this returns its type.
  621. Make sure it's actually an MMC message with isMidiMachineControlMessage() before
  622. calling this method.
  623. */
  624. MidiMachineControlCommand getMidiMachineControlCommand() const noexcept;
  625. /** Creates an MMC message. */
  626. static MidiMessage midiMachineControlCommand (MidiMachineControlCommand command);
  627. /** Checks whether this is an MMC "goto" message.
  628. If it is, the parameters passed-in are set to the time that the message contains.
  629. @see midiMachineControlGoto
  630. */
  631. bool isMidiMachineControlGoto (int& hours,
  632. int& minutes,
  633. int& seconds,
  634. int& frames) const noexcept;
  635. /** Creates an MMC "goto" message.
  636. This messages tells the device to go to a specific frame.
  637. @see isMidiMachineControlGoto
  638. */
  639. static MidiMessage midiMachineControlGoto (int hours,
  640. int minutes,
  641. int seconds,
  642. int frames);
  643. //==============================================================================
  644. /** Creates a master-volume change message.
  645. @param volume the volume, 0 to 1.0
  646. */
  647. static MidiMessage masterVolume (float volume);
  648. //==============================================================================
  649. /** Creates a system-exclusive message.
  650. The data passed in is wrapped with header and tail bytes of 0xf0 and 0xf7.
  651. */
  652. static MidiMessage createSysExMessage (const void* sysexData,
  653. int dataSize);
  654. //==============================================================================
  655. /** Reads a midi variable-length integer.
  656. @param data the data to read the number from
  657. @param numBytesUsed on return, this will be set to the number of bytes that were read
  658. */
  659. static int readVariableLengthVal (const uint8* data,
  660. int& numBytesUsed) noexcept;
  661. /** Based on the first byte of a short midi message, this uses a lookup table
  662. to return the message length (either 1, 2, or 3 bytes).
  663. The value passed in must be 0x80 or higher.
  664. */
  665. static int getMessageLengthFromFirstByte (uint8 firstByte) noexcept;
  666. //==============================================================================
  667. /** Returns the name of a midi note number.
  668. E.g "C", "D#", etc.
  669. @param noteNumber the midi note number, 0 to 127
  670. @param useSharps if true, sharpened notes are used, e.g. "C#", otherwise
  671. they'll be flattened, e.g. "Db"
  672. @param includeOctaveNumber if true, the octave number will be appended to the string,
  673. e.g. "C#4"
  674. @param octaveNumForMiddleC if an octave number is being appended, this indicates the
  675. number that will be used for middle C's octave
  676. @see getMidiNoteInHertz
  677. */
  678. static String getMidiNoteName (int noteNumber,
  679. bool useSharps,
  680. bool includeOctaveNumber,
  681. int octaveNumForMiddleC);
  682. /** Returns the frequency of a midi note number.
  683. The frequencyOfA parameter is an optional frequency for 'A', normally 440-444Hz for concert pitch.
  684. @see getMidiNoteName
  685. */
  686. static double getMidiNoteInHertz (int noteNumber, double frequencyOfA = 440.0) noexcept;
  687. /** Returns true if the given midi note number is a black key. */
  688. static bool isMidiNoteBlack (int noteNumber) noexcept;
  689. /** Returns the standard name of a GM instrument, or nullptr if unknown for this index.
  690. @param midiInstrumentNumber the program number 0 to 127
  691. @see getProgramChangeNumber
  692. */
  693. static const char* getGMInstrumentName (int midiInstrumentNumber);
  694. /** Returns the name of a bank of GM instruments, or nullptr if unknown for this bank number.
  695. @param midiBankNumber the bank, 0 to 15
  696. */
  697. static const char* getGMInstrumentBankName (int midiBankNumber);
  698. /** Returns the standard name of a channel 10 percussion sound, or nullptr if unknown for this note number.
  699. @param midiNoteNumber the key number, 35 to 81
  700. */
  701. static const char* getRhythmInstrumentName (int midiNoteNumber);
  702. /** Returns the name of a controller type number, or nullptr if unknown for this controller number.
  703. @see getControllerNumber
  704. */
  705. static const char* getControllerName (int controllerNumber);
  706. /** Converts a floating-point value between 0 and 1 to a MIDI 7-bit value between 0 and 127. */
  707. static uint8 floatValueToMidiByte (float valueBetween0and1) noexcept;
  708. /** Converts a pitchbend value in semitones to a MIDI 14-bit pitchwheel position value. */
  709. static uint16 pitchbendToPitchwheelPos (float pitchbendInSemitones,
  710. float pitchbendRangeInSemitones) noexcept;
  711. private:
  712. //==============================================================================
  713. #ifndef DOXYGEN
  714. union PackedData
  715. {
  716. uint8* allocatedData;
  717. uint8 asBytes[sizeof (uint8*)];
  718. };
  719. PackedData packedData;
  720. double timeStamp = 0;
  721. int size;
  722. #endif
  723. inline bool isHeapAllocated() const noexcept { return size > (int) sizeof (packedData); }
  724. inline uint8* getData() const noexcept { return isHeapAllocated() ? packedData.allocatedData : (uint8*) packedData.asBytes; }
  725. uint8* allocateSpace (int);
  726. };