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.

904 lines
35KB

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