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

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