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

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