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.

193 lines
7.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. #ifndef JUCE_MIDIFILE_H_INCLUDED
  24. #define JUCE_MIDIFILE_H_INCLUDED
  25. //==============================================================================
  26. /**
  27. Reads/writes standard midi format files.
  28. To read a midi file, create a MidiFile object and call its readFrom() method. You
  29. can then get the individual midi tracks from it using the getTrack() method.
  30. To write a file, create a MidiFile object, add some MidiMessageSequence objects
  31. to it using the addTrack() method, and then call its writeTo() method to stream
  32. it out.
  33. @see MidiMessageSequence
  34. */
  35. class JUCE_API MidiFile
  36. {
  37. public:
  38. //==============================================================================
  39. /** Creates an empty MidiFile object.
  40. */
  41. MidiFile();
  42. /** Destructor. */
  43. ~MidiFile();
  44. /** Creates a copy of another MidiFile. */
  45. MidiFile (const MidiFile& other);
  46. /** Copies from another MidiFile object */
  47. MidiFile& operator= (const MidiFile& other);
  48. //==============================================================================
  49. /** Returns the number of tracks in the file.
  50. @see getTrack, addTrack
  51. */
  52. int getNumTracks() const noexcept;
  53. /** Returns a pointer to one of the tracks in the file.
  54. @returns a pointer to the track, or nullptr if the index is out-of-range
  55. @see getNumTracks, addTrack
  56. */
  57. const MidiMessageSequence* getTrack (int index) const noexcept;
  58. /** Adds a midi track to the file.
  59. This will make its own internal copy of the sequence that is passed-in.
  60. @see getNumTracks, getTrack
  61. */
  62. void addTrack (const MidiMessageSequence& trackSequence);
  63. /** Removes all midi tracks from the file.
  64. @see getNumTracks
  65. */
  66. void clear();
  67. /** Returns the raw time format code that will be written to a stream.
  68. After reading a midi file, this method will return the time-format that
  69. was read from the file's header. It can be changed using the setTicksPerQuarterNote()
  70. or setSmpteTimeFormat() methods.
  71. If the value returned is positive, it indicates the number of midi ticks
  72. per quarter-note - see setTicksPerQuarterNote().
  73. It it's negative, the upper byte indicates the frames-per-second (but negative), and
  74. the lower byte is the number of ticks per frame - see setSmpteTimeFormat().
  75. */
  76. short getTimeFormat() const noexcept;
  77. /** Sets the time format to use when this file is written to a stream.
  78. If this is called, the file will be written as bars/beats using the
  79. specified resolution, rather than SMPTE absolute times, as would be
  80. used if setSmpteTimeFormat() had been called instead.
  81. @param ticksPerQuarterNote e.g. 96, 960
  82. @see setSmpteTimeFormat
  83. */
  84. void setTicksPerQuarterNote (int ticksPerQuarterNote) noexcept;
  85. /** Sets the time format to use when this file is written to a stream.
  86. If this is called, the file will be written using absolute times, rather
  87. than bars/beats as would be the case if setTicksPerBeat() had been called
  88. instead.
  89. @param framesPerSecond must be 24, 25, 29 or 30
  90. @param subframeResolution the sub-second resolution, e.g. 4 (midi time code),
  91. 8, 10, 80 (SMPTE bit resolution), or 100. For millisecond
  92. timing, setSmpteTimeFormat (25, 40)
  93. @see setTicksPerBeat
  94. */
  95. void setSmpteTimeFormat (int framesPerSecond,
  96. int subframeResolution) noexcept;
  97. //==============================================================================
  98. /** Makes a list of all the tempo-change meta-events from all tracks in the midi file.
  99. Useful for finding the positions of all the tempo changes in a file.
  100. @param tempoChangeEvents a list to which all the events will be added
  101. */
  102. void findAllTempoEvents (MidiMessageSequence& tempoChangeEvents) const;
  103. /** Makes a list of all the time-signature meta-events from all tracks in the midi file.
  104. Useful for finding the positions of all the tempo changes in a file.
  105. @param timeSigEvents a list to which all the events will be added
  106. */
  107. void findAllTimeSigEvents (MidiMessageSequence& timeSigEvents) const;
  108. /** Makes a list of all the time-signature meta-events from all tracks in the midi file.
  109. @param keySigEvents a list to which all the events will be added
  110. */
  111. void findAllKeySigEvents (MidiMessageSequence& keySigEvents) const;
  112. /** Returns the latest timestamp in any of the tracks.
  113. (Useful for finding the length of the file).
  114. */
  115. double getLastTimestamp() const;
  116. //==============================================================================
  117. /** Reads a midi file format stream.
  118. After calling this, you can get the tracks that were read from the file by using the
  119. getNumTracks() and getTrack() methods.
  120. The timestamps of the midi events in the tracks will represent their positions in
  121. terms of midi ticks. To convert them to seconds, use the convertTimestampTicksToSeconds()
  122. method.
  123. @returns true if the stream was read successfully
  124. */
  125. bool readFrom (InputStream& sourceStream);
  126. /** Writes the midi tracks as a standard midi file.
  127. The midiFileType value is written as the file's format type, which can be 0, 1
  128. or 2 - see the midi file spec for more info about that.
  129. @returns true if the operation succeeded.
  130. */
  131. bool writeTo (OutputStream& destStream, int midiFileType = 1);
  132. /** Converts the timestamp of all the midi events from midi ticks to seconds.
  133. This will use the midi time format and tempo/time signature info in the
  134. tracks to convert all the timestamps to absolute values in seconds.
  135. */
  136. void convertTimestampTicksToSeconds();
  137. private:
  138. //==============================================================================
  139. OwnedArray<MidiMessageSequence> tracks;
  140. short timeFormat;
  141. void readNextTrack (const uint8*, int size);
  142. bool writeTrack (OutputStream&, int trackNum);
  143. JUCE_LEAK_DETECTOR (MidiFile)
  144. };
  145. #endif // JUCE_MIDIFILE_H_INCLUDED