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.

198 lines
7.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - 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. Reads/writes standard midi format files.
  22. To read a midi file, create a MidiFile object and call its readFrom() method. You
  23. can then get the individual midi tracks from it using the getTrack() method.
  24. To write a file, create a MidiFile object, add some MidiMessageSequence objects
  25. to it using the addTrack() method, and then call its writeTo() method to stream
  26. it out.
  27. @see MidiMessageSequence
  28. @tags{Audio}
  29. */
  30. class JUCE_API MidiFile
  31. {
  32. public:
  33. //==============================================================================
  34. /** Creates an empty MidiFile object. */
  35. MidiFile();
  36. /** Destructor. */
  37. ~MidiFile();
  38. /** Creates a copy of another MidiFile. */
  39. MidiFile (const MidiFile&);
  40. /** Copies from another MidiFile object */
  41. MidiFile& operator= (const MidiFile&);
  42. /** Creates a copy of another MidiFile. */
  43. MidiFile (MidiFile&&);
  44. /** Copies from another MidiFile object */
  45. MidiFile& operator= (MidiFile&&);
  46. //==============================================================================
  47. /** Returns the number of tracks in the file.
  48. @see getTrack, addTrack
  49. */
  50. int getNumTracks() const noexcept;
  51. /** Returns a pointer to one of the tracks in the file.
  52. @returns a pointer to the track, or nullptr if the index is out-of-range
  53. @see getNumTracks, addTrack
  54. */
  55. const MidiMessageSequence* getTrack (int index) const noexcept;
  56. /** Adds a midi track to the file.
  57. This will make its own internal copy of the sequence that is passed-in.
  58. @see getNumTracks, getTrack
  59. */
  60. void addTrack (const MidiMessageSequence& trackSequence);
  61. /** Removes all midi tracks from the file.
  62. @see getNumTracks
  63. */
  64. void clear();
  65. /** Returns the raw time format code that will be written to a stream.
  66. After reading a midi file, this method will return the time-format that
  67. was read from the file's header. It can be changed using the setTicksPerQuarterNote()
  68. or setSmpteTimeFormat() methods.
  69. If the value returned is positive, it indicates the number of midi ticks
  70. per quarter-note - see setTicksPerQuarterNote().
  71. It it's negative, the upper byte indicates the frames-per-second (but negative), and
  72. the lower byte is the number of ticks per frame - see setSmpteTimeFormat().
  73. */
  74. short getTimeFormat() const noexcept;
  75. /** Sets the time format to use when this file is written to a stream.
  76. If this is called, the file will be written as bars/beats using the
  77. specified resolution, rather than SMPTE absolute times, as would be
  78. used if setSmpteTimeFormat() had been called instead.
  79. @param ticksPerQuarterNote e.g. 96, 960
  80. @see setSmpteTimeFormat
  81. */
  82. void setTicksPerQuarterNote (int ticksPerQuarterNote) noexcept;
  83. /** Sets the time format to use when this file is written to a stream.
  84. If this is called, the file will be written using absolute times, rather
  85. than bars/beats as would be the case if setTicksPerBeat() had been called
  86. instead.
  87. @param framesPerSecond must be 24, 25, 29 or 30
  88. @param subframeResolution the sub-second resolution, e.g. 4 (midi time code),
  89. 8, 10, 80 (SMPTE bit resolution), or 100. For millisecond
  90. timing, setSmpteTimeFormat (25, 40)
  91. @see setTicksPerBeat
  92. */
  93. void setSmpteTimeFormat (int framesPerSecond,
  94. int subframeResolution) noexcept;
  95. //==============================================================================
  96. /** Makes a list of all the tempo-change meta-events from all tracks in the midi file.
  97. Useful for finding the positions of all the tempo changes in a file.
  98. @param tempoChangeEvents a list to which all the events will be added
  99. */
  100. void findAllTempoEvents (MidiMessageSequence& tempoChangeEvents) const;
  101. /** Makes a list of all the time-signature meta-events from all tracks in the midi file.
  102. Useful for finding the positions of all the tempo changes in a file.
  103. @param timeSigEvents a list to which all the events will be added
  104. */
  105. void findAllTimeSigEvents (MidiMessageSequence& timeSigEvents) const;
  106. /** Makes a list of all the time-signature meta-events from all tracks in the midi file.
  107. @param keySigEvents a list to which all the events will be added
  108. */
  109. void findAllKeySigEvents (MidiMessageSequence& keySigEvents) const;
  110. /** Returns the latest timestamp in any of the tracks.
  111. (Useful for finding the length of the file).
  112. */
  113. double getLastTimestamp() const;
  114. //==============================================================================
  115. /** Reads a midi file format stream.
  116. After calling this, you can get the tracks that were read from the file by using the
  117. getNumTracks() and getTrack() methods.
  118. The timestamps of the midi events in the tracks will represent their positions in
  119. terms of midi ticks. To convert them to seconds, use the convertTimestampTicksToSeconds()
  120. method.
  121. @param sourceStream the source stream
  122. @param createMatchingNoteOffs if true, any missing note-offs for previous note-ons will
  123. be automatically added at the end of the file by calling
  124. MidiMessageSequence::updateMatchedPairs on each track.
  125. @returns true if the stream was read successfully
  126. */
  127. bool readFrom (InputStream& sourceStream, bool createMatchingNoteOffs = true);
  128. /** Writes the midi tracks as a standard midi file.
  129. The midiFileType value is written as the file's format type, which can be 0, 1
  130. or 2 - see the midi file spec for more info about that.
  131. @param destStream the destination stream
  132. @param midiFileType the type of midi file
  133. @returns true if the operation succeeded.
  134. */
  135. bool writeTo (OutputStream& destStream, int midiFileType = 1) const;
  136. /** Converts the timestamp of all the midi events from midi ticks to seconds.
  137. This will use the midi time format and tempo/time signature info in the
  138. tracks to convert all the timestamps to absolute values in seconds.
  139. */
  140. void convertTimestampTicksToSeconds();
  141. private:
  142. //==============================================================================
  143. OwnedArray<MidiMessageSequence> tracks;
  144. short timeFormat;
  145. void readNextTrack (const uint8*, int, bool);
  146. bool writeTrack (OutputStream&, const MidiMessageSequence&) const;
  147. JUCE_LEAK_DETECTOR (MidiFile)
  148. };
  149. } // namespace juce