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 37KB

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