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.

juce_MidiMessage.h 35KB

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