OpenShot Audio Library | OpenShotAudio 0.4.0
Loading...
Searching...
No Matches
juce_MidiMessage.h
1/*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 The code included in this file is provided under the terms of the ISC license
11 http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12 To use, copy, modify, and/or distribute this software for any purpose with or
13 without fee is hereby granted provided that the above copyright notice and
14 this permission notice appear in all copies.
15
16 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18 DISCLAIMED.
19
20 ==============================================================================
21*/
22
23namespace juce
24{
25
26//==============================================================================
34class JUCE_API MidiMessage
35{
36public:
37 //==============================================================================
46 MidiMessage (int byte1, int byte2, int byte3, double timeStamp = 0) noexcept;
47
55 MidiMessage (int byte1, int byte2, double timeStamp = 0) noexcept;
56
63 MidiMessage (int byte1, double timeStamp = 0) noexcept;
64
66 template <typename... Data>
67 MidiMessage (int byte1, int byte2, int byte3, Data... otherBytes) : size (3 + sizeof... (otherBytes))
68 {
69 // this checks that the length matches the data..
70 jassert (size > 3 || byte1 >= 0xf0 || getMessageLengthFromFirstByte ((uint8) byte1) == size);
71
72 const uint8 data[] = { (uint8) byte1, (uint8) byte2, (uint8) byte3, static_cast<uint8> (otherBytes)... };
73 memcpy (allocateSpace (size), data, (size_t) size);
74 }
75
76
78 MidiMessage (const void* data, int numBytes, double timeStamp = 0);
79
100 MidiMessage (const void* data, int maxBytesToUse,
101 int& numBytesUsed, uint8 lastStatusByte,
102 double timeStamp = 0,
103 bool sysexHasEmbeddedLength = true);
104
110 MidiMessage() noexcept;
111
113 MidiMessage (const MidiMessage&);
114
116 MidiMessage (const MidiMessage&, double newTimeStamp);
117
119 ~MidiMessage() noexcept;
120
122 MidiMessage& operator= (const MidiMessage& other);
123
125 MidiMessage (MidiMessage&&) noexcept;
126
128 MidiMessage& operator= (MidiMessage&&) noexcept;
129
130 //==============================================================================
134 const uint8* getRawData() const noexcept { return getData(); }
135
139 int getRawDataSize() const noexcept { return size; }
140
141 //==============================================================================
145 String getDescription() const;
146
147 //==============================================================================
164 double getTimeStamp() const noexcept { return timeStamp; }
165
170 void setTimeStamp (double newTimestamp) noexcept { timeStamp = newTimestamp; }
171
175 void addToTimeStamp (double delta) noexcept { timeStamp += delta; }
176
180 MidiMessage withTimeStamp (double newTimestamp) const;
181
182 //==============================================================================
189 int getChannel() const noexcept;
190
196 bool isForChannel (int channelNumber) const noexcept;
197
202 void setChannel (int newChannelNumber) noexcept;
203
204 //==============================================================================
207 bool isSysEx() const noexcept;
208
213 const uint8* getSysExData() const noexcept;
214
219 int getSysExDataSize() const noexcept;
220
222 Span<const std::byte> getSysExDataSpan() const noexcept
223 {
224 return { reinterpret_cast<const std::byte*> (getSysExData()),
225 (size_t) getSysExDataSize() };
226 }
227
228 //==============================================================================
239 bool isNoteOn (bool returnTrueForVelocity0 = false) const noexcept;
240
248 static MidiMessage noteOn (int channel, int noteNumber, float velocity) noexcept;
249
257 static MidiMessage noteOn (int channel, int noteNumber, uint8 velocity) noexcept;
258
266 bool isNoteOff (bool returnTrueForNoteOnVelocity0 = true) const noexcept;
267
275 static MidiMessage noteOff (int channel, int noteNumber, float velocity) noexcept;
276
284 static MidiMessage noteOff (int channel, int noteNumber, uint8 velocity) noexcept;
285
292 static MidiMessage noteOff (int channel, int noteNumber) noexcept;
293
298 bool isNoteOnOrOff() const noexcept;
299
304 int getNoteNumber() const noexcept;
305
309 void setNoteNumber (int newNoteNumber) noexcept;
310
311 //==============================================================================
319 uint8 getVelocity() const noexcept;
320
328 float getFloatVelocity() const noexcept;
329
337 void setVelocity (float newVelocity) noexcept;
338
346 void multiplyVelocity (float scaleFactor) noexcept;
347
348 //==============================================================================
350 bool isSustainPedalOn() const noexcept;
352 bool isSustainPedalOff() const noexcept;
353
355 bool isSostenutoPedalOn() const noexcept;
357 bool isSostenutoPedalOff() const noexcept;
358
360 bool isSoftPedalOn() const noexcept;
362 bool isSoftPedalOff() const noexcept;
363
364 //==============================================================================
368 bool isProgramChange() const noexcept;
369
374 int getProgramChangeNumber() const noexcept;
375
382 static MidiMessage programChange (int channel, int programNumber) noexcept;
383
384 //==============================================================================
388 bool isPitchWheel() const noexcept;
389
398 int getPitchWheelValue() const noexcept;
399
406 static MidiMessage pitchWheel (int channel, int position) noexcept;
407
408 //==============================================================================
417 bool isAftertouch() const noexcept;
418
426 int getAfterTouchValue() const noexcept;
427
435 static MidiMessage aftertouchChange (int channel,
436 int noteNumber,
437 int aftertouchAmount) noexcept;
438
447 bool isChannelPressure() const noexcept;
448
454 int getChannelPressureValue() const noexcept;
455
462 static MidiMessage channelPressureChange (int channel, int pressure) noexcept;
463
464 //==============================================================================
469 bool isController() const noexcept;
470
478 int getControllerNumber() const noexcept;
479
487 int getControllerValue() const noexcept;
488
492 bool isControllerOfType (int controllerType) const noexcept;
493
500 static MidiMessage controllerEvent (int channel,
501 int controllerType,
502 int value) noexcept;
503
507 bool isAllNotesOff() const noexcept;
508
512 bool isAllSoundOff() const noexcept;
513
517 bool isResetAllControllers() const noexcept;
518
523 static MidiMessage allNotesOff (int channel) noexcept;
524
529 static MidiMessage allSoundOff (int channel) noexcept;
530
534 static MidiMessage allControllersOff (int channel) noexcept;
535
536 //==============================================================================
545 bool isMetaEvent() const noexcept;
546
555 int getMetaEventType() const noexcept;
556
560 const uint8* getMetaEventData() const noexcept;
561
565 int getMetaEventLength() const noexcept;
566
567 //==============================================================================
569 bool isTrackMetaEvent() const noexcept;
570
572 bool isEndOfTrackMetaEvent() const noexcept;
573
577 static MidiMessage endOfTrack() noexcept;
578
582 bool isTrackNameEvent() const noexcept;
583
587 bool isTextMetaEvent() const noexcept;
588
592 String getTextFromTextMetaEvent() const;
593
595 static MidiMessage textMetaEvent (int type, StringRef text);
596
597 //==============================================================================
601 bool isTempoMetaEvent() const noexcept;
602
609 double getTempoMetaEventTickLength (short timeFormat) const noexcept;
610
614 double getTempoSecondsPerQuarterNote() const noexcept;
615
619 static MidiMessage tempoMetaEvent (int microsecondsPerQuarterNote) noexcept;
620
621 //==============================================================================
625 bool isTimeSignatureMetaEvent() const noexcept;
626
630 void getTimeSignatureInfo (int& numerator, int& denominator) const noexcept;
631
635 static MidiMessage timeSignatureMetaEvent (int numerator, int denominator);
636
637 //==============================================================================
641 bool isKeySignatureMetaEvent() const noexcept;
642
650 int getKeySignatureNumberOfSharpsOrFlats() const noexcept;
651
655 bool isKeySignatureMajorKey() const noexcept;
656
663 static MidiMessage keySignatureMetaEvent (int numberOfSharpsOrFlats, bool isMinorKey);
664
665 //==============================================================================
673 bool isMidiChannelMetaEvent() const noexcept;
674
680 int getMidiChannelMetaEventChannel() const noexcept;
681
687 static MidiMessage midiChannelMetaEvent (int channel) noexcept;
688
689 //==============================================================================
691 bool isActiveSense() const noexcept;
692
693 //==============================================================================
697 bool isMidiStart() const noexcept;
698
700 static MidiMessage midiStart() noexcept;
701
705 bool isMidiContinue() const noexcept;
706
708 static MidiMessage midiContinue() noexcept;
709
713 bool isMidiStop() const noexcept;
714
716 static MidiMessage midiStop() noexcept;
717
721 bool isMidiClock() const noexcept;
722
724 static MidiMessage midiClock() noexcept;
725
729 bool isSongPositionPointer() const noexcept;
730
734 int getSongPositionPointerMidiBeat() const noexcept;
735
744 static MidiMessage songPositionPointer (int positionInMidiBeats) noexcept;
745
746 //==============================================================================
750 bool isQuarterFrame() const noexcept;
751
756 int getQuarterFrameSequenceNumber() const noexcept;
757
761 int getQuarterFrameValue() const noexcept;
762
768 static MidiMessage quarterFrame (int sequenceNumber, int value) noexcept;
769
774 {
775 fps24 = 0,
776 fps25 = 1,
777 fps30drop = 2,
778 fps30 = 3
779 };
780
782 bool isFullFrame() const noexcept;
783
789 void getFullFrameParameters (int& hours,
790 int& minutes,
791 int& seconds,
792 int& frames,
793 SmpteTimecodeType& timecodeType) const noexcept;
794
796 static MidiMessage fullFrame (int hours,
797 int minutes,
798 int seconds,
799 int frames,
800 SmpteTimecodeType timecodeType);
801
802 //==============================================================================
808 {
809 mmc_stop = 1,
810 mmc_play = 2,
811 mmc_deferredplay = 3,
812 mmc_fastforward = 4,
813 mmc_rewind = 5,
814 mmc_recordStart = 6,
815 mmc_recordStop = 7,
816 mmc_pause = 9
817 };
818
822 bool isMidiMachineControlMessage() const noexcept;
823
829 MidiMachineControlCommand getMidiMachineControlCommand() const noexcept;
830
832 static MidiMessage midiMachineControlCommand (MidiMachineControlCommand command);
833
838 bool isMidiMachineControlGoto (int& hours,
839 int& minutes,
840 int& seconds,
841 int& frames) const noexcept;
842
847 static MidiMessage midiMachineControlGoto (int hours,
848 int minutes,
849 int seconds,
850 int frames);
851
852 //==============================================================================
856 static MidiMessage masterVolume (float volume);
857
858 //==============================================================================
862 static MidiMessage createSysExMessage (const void* sysexData,
863 int dataSize);
864
868 static MidiMessage createSysExMessage (Span<const std::byte> data);
869
870 //==============================================================================
871 #ifndef DOXYGEN
878 [[deprecated ("This signature has been deprecated in favour of the safer readVariableLengthValue.")]]
879 static int readVariableLengthVal (const uint8* data, int& numBytesUsed) noexcept;
880 #endif
881
887 struct VariableLengthValue
888 {
889 VariableLengthValue() = default;
890
891 VariableLengthValue (int valueIn, int bytesUsedIn)
892 : value (valueIn), bytesUsed (bytesUsedIn) {}
893
894 bool isValid() const noexcept { return bytesUsed > 0; }
895
896 int value = 0;
897 int bytesUsed = 0;
898 };
899
909 static VariableLengthValue readVariableLengthValue (const uint8* data,
910 int maxBytesToUse) noexcept;
911
917 static int getMessageLengthFromFirstByte (uint8 firstByte) noexcept;
918
919 //==============================================================================
934 static String getMidiNoteName (int noteNumber,
935 bool useSharps,
936 bool includeOctaveNumber,
937 int octaveNumForMiddleC);
938
944 static double getMidiNoteInHertz (int noteNumber, double frequencyOfA = 440.0) noexcept;
945
947 static bool isMidiNoteBlack (int noteNumber) noexcept;
948
954 static const char* getGMInstrumentName (int midiInstrumentNumber);
955
959 static const char* getGMInstrumentBankName (int midiBankNumber);
960
964 static const char* getRhythmInstrumentName (int midiNoteNumber);
965
969 static const char* getControllerName (int controllerNumber);
970
972 static uint8 floatValueToMidiByte (float valueBetween0and1) noexcept;
973
975 static uint16 pitchbendToPitchwheelPos (float pitchbendInSemitones,
976 float pitchbendRangeInSemitones) noexcept;
977
978private:
979 //==============================================================================
980 #ifndef DOXYGEN
981 union PackedData
982 {
983 uint8* allocatedData;
984 uint8 asBytes[sizeof (uint8*)];
985 };
986
987 PackedData packedData;
988 double timeStamp = 0;
989 int size;
990 #endif
991
992 inline bool isHeapAllocated() const noexcept { return size > (int) sizeof (packedData); }
993 inline uint8* getData() const noexcept { return isHeapAllocated() ? packedData.allocatedData : (uint8*) packedData.asBytes; }
994 uint8* allocateSpace (int);
995};
996
997} // namespace juce
MidiMessage(int byte1, int byte2, int byte3, Data... otherBytes)
const uint8 * getSysExData() const noexcept
static const char * getGMInstrumentBankName(int midiBankNumber)
static bool isMidiNoteBlack(int noteNumber) noexcept
int getSysExDataSize() const noexcept
const uint8 * getRawData() const noexcept
static double getMidiNoteInHertz(int noteNumber, double frequencyOfA=440.0) noexcept
Span< const std::byte > getSysExDataSpan() const noexcept
static uint16 pitchbendToPitchwheelPos(float pitchbendInSemitones, float pitchbendRangeInSemitones) noexcept
double getTimeStamp() const noexcept
static const char * getGMInstrumentName(int midiInstrumentNumber)
MidiMessage(int byte1, int byte2, int byte3, double timeStamp=0) noexcept
static uint8 floatValueToMidiByte(float valueBetween0and1) noexcept
static int getMessageLengthFromFirstByte(uint8 firstByte) noexcept
static String getMidiNoteName(int noteNumber, bool useSharps, bool includeOctaveNumber, int octaveNumForMiddleC)
void addToTimeStamp(double delta) noexcept
void setTimeStamp(double newTimestamp) noexcept
static const char * getRhythmInstrumentName(int midiNoteNumber)
static VariableLengthValue readVariableLengthValue(const uint8 *data, int maxBytesToUse) noexcept
static const char * getControllerName(int controllerNumber)
int getRawDataSize() const noexcept