The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

998 lines
40KB

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