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.

921 lines
36KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI 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 1.0
  186. @see isNoteOff
  187. */
  188. static MidiMessage noteOff (int channel, int noteNumber, float velocity) noexcept;
  189. /** Creates a key-up message.
  190. @param channel the midi channel, in the range 1 to 16
  191. @param noteNumber the key number, 0 to 127
  192. @param velocity in the range 0 to 127
  193. @see isNoteOff
  194. */
  195. static MidiMessage noteOff (int channel, int noteNumber, uint8 velocity) noexcept;
  196. /** Creates a key-up message.
  197. @param channel the midi channel, in the range 1 to 16
  198. @param noteNumber the key number, 0 to 127
  199. @see isNoteOff
  200. */
  201. static MidiMessage noteOff (int channel, int noteNumber) noexcept;
  202. /** Returns true if this message is a 'key-down' or 'key-up' event.
  203. @see isNoteOn, isNoteOff
  204. */
  205. bool isNoteOnOrOff() const noexcept;
  206. /** Returns the midi note number for note-on and note-off messages.
  207. If the message isn't a note-on or off, the value returned is undefined.
  208. @see isNoteOff, getMidiNoteName, getMidiNoteInHertz, setNoteNumber
  209. */
  210. int getNoteNumber() const noexcept;
  211. /** Changes the midi note number of a note-on or note-off message.
  212. If the message isn't a note on or off, this will do nothing.
  213. */
  214. void setNoteNumber (int newNoteNumber) noexcept;
  215. //==============================================================================
  216. /** Returns the velocity of a note-on or note-off message.
  217. The value returned will be in the range 0 to 127.
  218. If the message isn't a note-on or off event, it will return 0.
  219. @see getFloatVelocity
  220. */
  221. uint8 getVelocity() const noexcept;
  222. /** Returns the velocity of a note-on or note-off message.
  223. The value returned will be in the range 0 to 1.0
  224. If the message isn't a note-on or off event, it will return 0.
  225. @see getVelocity, setVelocity
  226. */
  227. float getFloatVelocity() const noexcept;
  228. /** Changes the velocity of a note-on or note-off message.
  229. If the message isn't a note on or off, this will do nothing.
  230. @param newVelocity the new velocity, in the range 0 to 1.0
  231. @see getFloatVelocity, multiplyVelocity
  232. */
  233. void setVelocity (float newVelocity) noexcept;
  234. /** Multiplies the velocity of a note-on or note-off message by a given amount.
  235. If the message isn't a note on or off, this will do nothing.
  236. @param scaleFactor the value by which to multiply the velocity
  237. @see setVelocity
  238. */
  239. void multiplyVelocity (float scaleFactor) noexcept;
  240. //==============================================================================
  241. /** Returns true if this message is a 'sustain pedal down' controller message. */
  242. bool isSustainPedalOn() const noexcept;
  243. /** Returns true if this message is a 'sustain pedal up' controller message. */
  244. bool isSustainPedalOff() const noexcept;
  245. /** Returns true if this message is a 'sostenuto pedal down' controller message. */
  246. bool isSostenutoPedalOn() const noexcept;
  247. /** Returns true if this message is a 'sostenuto pedal up' controller message. */
  248. bool isSostenutoPedalOff() const noexcept;
  249. /** Returns true if this message is a 'soft pedal down' controller message. */
  250. bool isSoftPedalOn() const noexcept;
  251. /** Returns true if this message is a 'soft pedal up' controller message. */
  252. bool isSoftPedalOff() const noexcept;
  253. //==============================================================================
  254. /** Returns true if the message is a program (patch) change message.
  255. @see getProgramChangeNumber, getGMInstrumentName
  256. */
  257. bool isProgramChange() const noexcept;
  258. /** Returns the new program number of a program change message.
  259. If the message isn't a program change, the value returned is undefined.
  260. @see isProgramChange, getGMInstrumentName
  261. */
  262. int getProgramChangeNumber() const noexcept;
  263. /** Creates a program-change message.
  264. @param channel the midi channel, in the range 1 to 16
  265. @param programNumber the midi program number, 0 to 127
  266. @see isProgramChange, getGMInstrumentName
  267. */
  268. static MidiMessage programChange (int channel, int programNumber) noexcept;
  269. //==============================================================================
  270. /** Returns true if the message is a pitch-wheel move.
  271. @see getPitchWheelValue, pitchWheel
  272. */
  273. bool isPitchWheel() const noexcept;
  274. /** Returns the pitch wheel position from a pitch-wheel move message.
  275. The value returned is a 14-bit number from 0 to 0x3fff, indicating the wheel position.
  276. If called for messages which aren't pitch wheel events, the number returned will be
  277. nonsense.
  278. @see isPitchWheel
  279. */
  280. int getPitchWheelValue() const noexcept;
  281. /** Creates a pitch-wheel move message.
  282. @param channel the midi channel, in the range 1 to 16
  283. @param position the wheel position, in the range 0 to 16383
  284. @see isPitchWheel
  285. */
  286. static MidiMessage pitchWheel (int channel, int position) noexcept;
  287. //==============================================================================
  288. /** Returns true if the message is an aftertouch event.
  289. For aftertouch events, use the getNoteNumber() method to find out the key
  290. that it applies to, and getAftertouchValue() to find out the amount. Use
  291. getChannel() to find out the channel.
  292. @see getAftertouchValue, getNoteNumber
  293. */
  294. bool isAftertouch() const noexcept;
  295. /** Returns the amount of aftertouch from an aftertouch messages.
  296. The value returned is in the range 0 to 127, and will be nonsense for messages
  297. other than aftertouch messages.
  298. @see isAftertouch
  299. */
  300. int getAfterTouchValue() const noexcept;
  301. /** Creates an aftertouch message.
  302. @param channel the midi channel, in the range 1 to 16
  303. @param noteNumber the key number, 0 to 127
  304. @param aftertouchAmount the amount of aftertouch, 0 to 127
  305. @see isAftertouch
  306. */
  307. static MidiMessage aftertouchChange (int channel,
  308. int noteNumber,
  309. int aftertouchAmount) noexcept;
  310. /** Returns true if the message is a channel-pressure change event.
  311. This is like aftertouch, but common to the whole channel rather than a specific
  312. note. Use getChannelPressureValue() to find out the pressure, and getChannel()
  313. to find out the channel.
  314. @see channelPressureChange
  315. */
  316. bool isChannelPressure() const noexcept;
  317. /** Returns the pressure from a channel pressure change message.
  318. @returns the pressure, in the range 0 to 127
  319. @see isChannelPressure, channelPressureChange
  320. */
  321. int getChannelPressureValue() const noexcept;
  322. /** Creates a channel-pressure change event.
  323. @param channel the midi channel: 1 to 16
  324. @param pressure the pressure, 0 to 127
  325. @see isChannelPressure
  326. */
  327. static MidiMessage channelPressureChange (int channel, int pressure) noexcept;
  328. //==============================================================================
  329. /** Returns true if this is a midi controller message.
  330. @see getControllerNumber, getControllerValue, controllerEvent
  331. */
  332. bool isController() const noexcept;
  333. /** Returns the controller number of a controller message.
  334. The name of the controller can be looked up using the getControllerName() method.
  335. Note that the value returned is invalid for messages that aren't controller changes.
  336. @see isController, getControllerName, getControllerValue
  337. */
  338. int getControllerNumber() const noexcept;
  339. /** Returns the controller value from a controller message.
  340. A value 0 to 127 is returned to indicate the new controller position.
  341. Note that the value returned is invalid for messages that aren't controller changes.
  342. @see isController, getControllerNumber
  343. */
  344. int getControllerValue() const noexcept;
  345. /** Returns true if this message is a controller message and if it has the specified
  346. controller type.
  347. */
  348. bool isControllerOfType (int controllerType) const noexcept;
  349. /** Creates a controller message.
  350. @param channel the midi channel, in the range 1 to 16
  351. @param controllerType the type of controller
  352. @param value the controller value
  353. @see isController
  354. */
  355. static MidiMessage controllerEvent (int channel,
  356. int controllerType,
  357. int value) noexcept;
  358. /** Checks whether this message is an all-notes-off message.
  359. @see allNotesOff
  360. */
  361. bool isAllNotesOff() const noexcept;
  362. /** Checks whether this message is an all-sound-off message.
  363. @see allSoundOff
  364. */
  365. bool isAllSoundOff() const noexcept;
  366. /** Creates an all-notes-off message.
  367. @param channel the midi channel, in the range 1 to 16
  368. @see isAllNotesOff
  369. */
  370. static MidiMessage allNotesOff (int channel) noexcept;
  371. /** Creates an all-sound-off message.
  372. @param channel the midi channel, in the range 1 to 16
  373. @see isAllSoundOff
  374. */
  375. static MidiMessage allSoundOff (int channel) noexcept;
  376. /** Creates an all-controllers-off message.
  377. @param channel the midi channel, in the range 1 to 16
  378. */
  379. static MidiMessage allControllersOff (int channel) noexcept;
  380. //==============================================================================
  381. /** Returns true if this event is a meta-event.
  382. Meta-events are things like tempo changes, track names, etc.
  383. @see getMetaEventType, isTrackMetaEvent, isEndOfTrackMetaEvent,
  384. isTextMetaEvent, isTrackNameEvent, isTempoMetaEvent, isTimeSignatureMetaEvent,
  385. isKeySignatureMetaEvent, isMidiChannelMetaEvent
  386. */
  387. bool isMetaEvent() const noexcept;
  388. /** Returns a meta-event's type number.
  389. If the message isn't a meta-event, this will return -1.
  390. @see isMetaEvent, isTrackMetaEvent, isEndOfTrackMetaEvent,
  391. isTextMetaEvent, isTrackNameEvent, isTempoMetaEvent, isTimeSignatureMetaEvent,
  392. isKeySignatureMetaEvent, isMidiChannelMetaEvent
  393. */
  394. int getMetaEventType() const noexcept;
  395. /** Returns a pointer to the data in a meta-event.
  396. @see isMetaEvent, getMetaEventLength
  397. */
  398. const uint8* getMetaEventData() const noexcept;
  399. /** Returns the length of the data for a meta-event.
  400. @see isMetaEvent, getMetaEventData
  401. */
  402. int getMetaEventLength() const noexcept;
  403. //==============================================================================
  404. /** Returns true if this is a 'track' meta-event. */
  405. bool isTrackMetaEvent() const noexcept;
  406. /** Returns true if this is an 'end-of-track' meta-event. */
  407. bool isEndOfTrackMetaEvent() const noexcept;
  408. /** Creates an end-of-track meta-event.
  409. @see isEndOfTrackMetaEvent
  410. */
  411. static MidiMessage endOfTrack() noexcept;
  412. /** Returns true if this is an 'track name' meta-event.
  413. You can use the getTextFromTextMetaEvent() method to get the track's name.
  414. */
  415. bool isTrackNameEvent() const noexcept;
  416. /** Returns true if this is a 'text' meta-event.
  417. @see getTextFromTextMetaEvent
  418. */
  419. bool isTextMetaEvent() const noexcept;
  420. /** Returns the text from a text meta-event.
  421. @see isTextMetaEvent
  422. */
  423. String getTextFromTextMetaEvent() const;
  424. /** Creates a text meta-event. */
  425. static MidiMessage textMetaEvent (int type, StringRef text);
  426. //==============================================================================
  427. /** Returns true if this is a 'tempo' meta-event.
  428. @see getTempoMetaEventTickLength, getTempoSecondsPerQuarterNote
  429. */
  430. bool isTempoMetaEvent() const noexcept;
  431. /** Returns the tick length from a tempo meta-event.
  432. @param timeFormat the 16-bit time format value from the midi file's header.
  433. @returns the tick length (in seconds).
  434. @see isTempoMetaEvent
  435. */
  436. double getTempoMetaEventTickLength (short timeFormat) const noexcept;
  437. /** Calculates the seconds-per-quarter-note from a tempo meta-event.
  438. @see isTempoMetaEvent, getTempoMetaEventTickLength
  439. */
  440. double getTempoSecondsPerQuarterNote() const noexcept;
  441. /** Creates a tempo meta-event.
  442. @see isTempoMetaEvent
  443. */
  444. static MidiMessage tempoMetaEvent (int microsecondsPerQuarterNote) noexcept;
  445. //==============================================================================
  446. /** Returns true if this is a 'time-signature' meta-event.
  447. @see getTimeSignatureInfo
  448. */
  449. bool isTimeSignatureMetaEvent() const noexcept;
  450. /** Returns the time-signature values from a time-signature meta-event.
  451. @see isTimeSignatureMetaEvent
  452. */
  453. void getTimeSignatureInfo (int& numerator, int& denominator) const noexcept;
  454. /** Creates a time-signature meta-event.
  455. @see isTimeSignatureMetaEvent
  456. */
  457. static MidiMessage timeSignatureMetaEvent (int numerator, int denominator);
  458. //==============================================================================
  459. /** Returns true if this is a 'key-signature' meta-event.
  460. @see getKeySignatureNumberOfSharpsOrFlats, isKeySignatureMajorKey
  461. */
  462. bool isKeySignatureMetaEvent() const noexcept;
  463. /** Returns the key from a key-signature meta-event.
  464. This method must only be called if isKeySignatureMetaEvent() is true.
  465. A positive number here indicates the number of sharps in the key signature,
  466. and a negative number indicates a number of flats. So e.g. 3 = F# + C# + G#,
  467. -2 = Bb + Eb
  468. @see isKeySignatureMetaEvent, isKeySignatureMajorKey
  469. */
  470. int getKeySignatureNumberOfSharpsOrFlats() const noexcept;
  471. /** Returns true if this key-signature event is major, or false if it's minor.
  472. This method must only be called if isKeySignatureMetaEvent() is true.
  473. */
  474. bool isKeySignatureMajorKey() const noexcept;
  475. /** Creates a key-signature meta-event.
  476. @param numberOfSharpsOrFlats if positive, this indicates the number of sharps
  477. in the key; if negative, the number of flats
  478. @param isMinorKey if true, the key is minor; if false, it is major
  479. @see isKeySignatureMetaEvent
  480. */
  481. static MidiMessage keySignatureMetaEvent (int numberOfSharpsOrFlats, bool isMinorKey);
  482. //==============================================================================
  483. /** Returns true if this is a 'channel' meta-event.
  484. A channel meta-event specifies the midi channel that should be used
  485. for subsequent meta-events.
  486. @see getMidiChannelMetaEventChannel
  487. */
  488. bool isMidiChannelMetaEvent() const noexcept;
  489. /** Returns the channel number from a channel meta-event.
  490. @returns the channel, in the range 1 to 16.
  491. @see isMidiChannelMetaEvent
  492. */
  493. int getMidiChannelMetaEventChannel() const noexcept;
  494. /** Creates a midi channel meta-event.
  495. @param channel the midi channel, in the range 1 to 16
  496. @see isMidiChannelMetaEvent
  497. */
  498. static MidiMessage midiChannelMetaEvent (int channel) noexcept;
  499. //==============================================================================
  500. /** Returns true if this is an active-sense message. */
  501. bool isActiveSense() const noexcept;
  502. //==============================================================================
  503. /** Returns true if this is a midi start event.
  504. @see midiStart
  505. */
  506. bool isMidiStart() const noexcept;
  507. /** Creates a midi start event. */
  508. static MidiMessage midiStart() noexcept;
  509. /** Returns true if this is a midi continue event.
  510. @see midiContinue
  511. */
  512. bool isMidiContinue() const noexcept;
  513. /** Creates a midi continue event. */
  514. static MidiMessage midiContinue() noexcept;
  515. /** Returns true if this is a midi stop event.
  516. @see midiStop
  517. */
  518. bool isMidiStop() const noexcept;
  519. /** Creates a midi stop event. */
  520. static MidiMessage midiStop() noexcept;
  521. /** Returns true if this is a midi clock event.
  522. @see midiClock, songPositionPointer
  523. */
  524. bool isMidiClock() const noexcept;
  525. /** Creates a midi clock event. */
  526. static MidiMessage midiClock() noexcept;
  527. /** Returns true if this is a song-position-pointer message.
  528. @see getSongPositionPointerMidiBeat, songPositionPointer
  529. */
  530. bool isSongPositionPointer() const noexcept;
  531. /** Returns the midi beat-number of a song-position-pointer message.
  532. @see isSongPositionPointer, songPositionPointer
  533. */
  534. int getSongPositionPointerMidiBeat() const noexcept;
  535. /** Creates a song-position-pointer message.
  536. The position is a number of midi beats from the start of the song, where 1 midi
  537. beat is 6 midi clocks, and there are 24 midi clocks in a quarter-note. So there
  538. are 4 midi beats in a quarter-note.
  539. @see isSongPositionPointer, getSongPositionPointerMidiBeat
  540. */
  541. static MidiMessage songPositionPointer (int positionInMidiBeats) noexcept;
  542. //==============================================================================
  543. /** Returns true if this is a quarter-frame midi timecode message.
  544. @see quarterFrame, getQuarterFrameSequenceNumber, getQuarterFrameValue
  545. */
  546. bool isQuarterFrame() const noexcept;
  547. /** Returns the sequence number of a quarter-frame midi timecode message.
  548. This will be a value between 0 and 7.
  549. @see isQuarterFrame, getQuarterFrameValue, quarterFrame
  550. */
  551. int getQuarterFrameSequenceNumber() const noexcept;
  552. /** Returns the value from a quarter-frame message.
  553. This will be the lower nybble of the message's data-byte, a value between 0 and 15
  554. */
  555. int getQuarterFrameValue() const noexcept;
  556. /** Creates a quarter-frame MTC message.
  557. @param sequenceNumber a value 0 to 7 for the upper nybble of the message's data byte
  558. @param value a value 0 to 15 for the lower nybble of the message's data byte
  559. */
  560. static MidiMessage quarterFrame (int sequenceNumber, int value) noexcept;
  561. /** SMPTE timecode types.
  562. Used by the getFullFrameParameters() and fullFrame() methods.
  563. */
  564. enum SmpteTimecodeType
  565. {
  566. fps24 = 0,
  567. fps25 = 1,
  568. fps30drop = 2,
  569. fps30 = 3
  570. };
  571. /** Returns true if this is a full-frame midi timecode message. */
  572. bool isFullFrame() const noexcept;
  573. /** Extracts the timecode information from a full-frame midi timecode message.
  574. You should only call this on messages where you've used isFullFrame() to
  575. check that they're the right kind.
  576. */
  577. void getFullFrameParameters (int& hours,
  578. int& minutes,
  579. int& seconds,
  580. int& frames,
  581. SmpteTimecodeType& timecodeType) const noexcept;
  582. /** Creates a full-frame MTC message. */
  583. static MidiMessage fullFrame (int hours,
  584. int minutes,
  585. int seconds,
  586. int frames,
  587. SmpteTimecodeType timecodeType);
  588. //==============================================================================
  589. /** Types of MMC command.
  590. @see isMidiMachineControlMessage, getMidiMachineControlCommand, midiMachineControlCommand
  591. */
  592. enum MidiMachineControlCommand
  593. {
  594. mmc_stop = 1,
  595. mmc_play = 2,
  596. mmc_deferredplay = 3,
  597. mmc_fastforward = 4,
  598. mmc_rewind = 5,
  599. mmc_recordStart = 6,
  600. mmc_recordStop = 7,
  601. mmc_pause = 9
  602. };
  603. /** Checks whether this is an MMC message.
  604. If it is, you can use the getMidiMachineControlCommand() to find out its type.
  605. */
  606. bool isMidiMachineControlMessage() const noexcept;
  607. /** For an MMC message, this returns its type.
  608. Make sure it's actually an MMC message with isMidiMachineControlMessage() before
  609. calling this method.
  610. */
  611. MidiMachineControlCommand getMidiMachineControlCommand() const noexcept;
  612. /** Creates an MMC message. */
  613. static MidiMessage midiMachineControlCommand (MidiMachineControlCommand command);
  614. /** Checks whether this is an MMC "goto" message.
  615. If it is, the parameters passed-in are set to the time that the message contains.
  616. @see midiMachineControlGoto
  617. */
  618. bool isMidiMachineControlGoto (int& hours,
  619. int& minutes,
  620. int& seconds,
  621. int& frames) const noexcept;
  622. /** Creates an MMC "goto" message.
  623. This messages tells the device to go to a specific frame.
  624. @see isMidiMachineControlGoto
  625. */
  626. static MidiMessage midiMachineControlGoto (int hours,
  627. int minutes,
  628. int seconds,
  629. int frames);
  630. //==============================================================================
  631. /** Creates a master-volume change message.
  632. @param volume the volume, 0 to 1.0
  633. */
  634. static MidiMessage masterVolume (float volume);
  635. //==============================================================================
  636. /** Creates a system-exclusive message.
  637. The data passed in is wrapped with header and tail bytes of 0xf0 and 0xf7.
  638. */
  639. static MidiMessage createSysExMessage (const void* sysexData,
  640. int dataSize);
  641. //==============================================================================
  642. /** Reads a midi variable-length integer.
  643. @param data the data to read the number from
  644. @param numBytesUsed on return, this will be set to the number of bytes that were read
  645. */
  646. static int readVariableLengthVal (const uint8* data,
  647. int& numBytesUsed) noexcept;
  648. /** Based on the first byte of a short midi message, this uses a lookup table
  649. to return the message length (either 1, 2, or 3 bytes).
  650. The value passed in must be 0x80 or higher.
  651. */
  652. static int getMessageLengthFromFirstByte (const uint8 firstByte) noexcept;
  653. //==============================================================================
  654. /** Returns the name of a midi note number.
  655. E.g "C", "D#", etc.
  656. @param noteNumber the midi note number, 0 to 127
  657. @param useSharps if true, sharpened notes are used, e.g. "C#", otherwise
  658. they'll be flattened, e.g. "Db"
  659. @param includeOctaveNumber if true, the octave number will be appended to the string,
  660. e.g. "C#4"
  661. @param octaveNumForMiddleC if an octave number is being appended, this indicates the
  662. number that will be used for middle C's octave
  663. @see getMidiNoteInHertz
  664. */
  665. static String getMidiNoteName (int noteNumber,
  666. bool useSharps,
  667. bool includeOctaveNumber,
  668. int octaveNumForMiddleC);
  669. /** Returns the frequency of a midi note number.
  670. The frequencyOfA parameter is an optional frequency for 'A', normally 440-444Hz for concert pitch.
  671. @see getMidiNoteName
  672. */
  673. static double getMidiNoteInHertz (int noteNumber, const double frequencyOfA = 440.0) noexcept;
  674. /** Returns true if the given midi note number is a black key. */
  675. static bool isMidiNoteBlack (int noteNumber) noexcept;
  676. /** Returns the standard name of a GM instrument, or nullptr if unknown for this index.
  677. @param midiInstrumentNumber the program number 0 to 127
  678. @see getProgramChangeNumber
  679. */
  680. static const char* getGMInstrumentName (int midiInstrumentNumber);
  681. /** Returns the name of a bank of GM instruments, or nullptr if unknown for this bank number.
  682. @param midiBankNumber the bank, 0 to 15
  683. */
  684. static const char* getGMInstrumentBankName (int midiBankNumber);
  685. /** Returns the standard name of a channel 10 percussion sound, or nullptr if unknown for this note number.
  686. @param midiNoteNumber the key number, 35 to 81
  687. */
  688. static const char* getRhythmInstrumentName (int midiNoteNumber);
  689. /** Returns the name of a controller type number, or nullptr if unknown for this controller number.
  690. @see getControllerNumber
  691. */
  692. static const char* getControllerName (int controllerNumber);
  693. private:
  694. //==============================================================================
  695. double timeStamp;
  696. HeapBlock<uint8> allocatedData;
  697. int size;
  698. #ifndef DOXYGEN
  699. union
  700. {
  701. uint8 asBytes[4];
  702. uint32 asInt32;
  703. } preallocatedData;
  704. #endif
  705. inline uint8* getData() noexcept { return allocatedData != nullptr ? allocatedData.getData() : preallocatedData.asBytes; }
  706. uint8* allocateSpace (int);
  707. };
  708. #endif // JUCE_MIDIMESSAGE_H_INCLUDED