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.

MidiMessage.h 34KB

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