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.

941 lines
37KB

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