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_MidiFile.h 6.9KB

11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #ifndef JUCE_MIDIFILE_H_INCLUDED
  18. #define JUCE_MIDIFILE_H_INCLUDED
  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. */
  29. class JUCE_API MidiFile
  30. {
  31. public:
  32. //==============================================================================
  33. /** Creates an empty MidiFile object.
  34. */
  35. MidiFile();
  36. /** Destructor. */
  37. ~MidiFile();
  38. //==============================================================================
  39. /** Returns the number of tracks in the file.
  40. @see getTrack, addTrack
  41. */
  42. int getNumTracks() const noexcept;
  43. /** Returns a pointer to one of the tracks in the file.
  44. @returns a pointer to the track, or nullptr if the index is out-of-range
  45. @see getNumTracks, addTrack
  46. */
  47. const MidiMessageSequence* getTrack (int index) const noexcept;
  48. /** Adds a midi track to the file.
  49. This will make its own internal copy of the sequence that is passed-in.
  50. @see getNumTracks, getTrack
  51. */
  52. void addTrack (const MidiMessageSequence& trackSequence);
  53. /** Removes all midi tracks from the file.
  54. @see getNumTracks
  55. */
  56. void clear();
  57. /** Returns the raw time format code that will be written to a stream.
  58. After reading a midi file, this method will return the time-format that
  59. was read from the file's header. It can be changed using the setTicksPerQuarterNote()
  60. or setSmpteTimeFormat() methods.
  61. If the value returned is positive, it indicates the number of midi ticks
  62. per quarter-note - see setTicksPerQuarterNote().
  63. It it's negative, the upper byte indicates the frames-per-second (but negative), and
  64. the lower byte is the number of ticks per frame - see setSmpteTimeFormat().
  65. */
  66. short getTimeFormat() const noexcept;
  67. /** Sets the time format to use when this file is written to a stream.
  68. If this is called, the file will be written as bars/beats using the
  69. specified resolution, rather than SMPTE absolute times, as would be
  70. used if setSmpteTimeFormat() had been called instead.
  71. @param ticksPerQuarterNote e.g. 96, 960
  72. @see setSmpteTimeFormat
  73. */
  74. void setTicksPerQuarterNote (int ticksPerQuarterNote) 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 using absolute times, rather
  77. than bars/beats as would be the case if setTicksPerBeat() had been called
  78. instead.
  79. @param framesPerSecond must be 24, 25, 29 or 30
  80. @param subframeResolution the sub-second resolution, e.g. 4 (midi time code),
  81. 8, 10, 80 (SMPTE bit resolution), or 100. For millisecond
  82. timing, setSmpteTimeFormat (25, 40)
  83. @see setTicksPerBeat
  84. */
  85. void setSmpteTimeFormat (int framesPerSecond,
  86. int subframeResolution) noexcept;
  87. //==============================================================================
  88. /** Makes a list of all the tempo-change meta-events from all tracks in the midi file.
  89. Useful for finding the positions of all the tempo changes in a file.
  90. @param tempoChangeEvents a list to which all the events will be added
  91. */
  92. void findAllTempoEvents (MidiMessageSequence& tempoChangeEvents) const;
  93. /** Makes a list of all the time-signature meta-events from all tracks in the midi file.
  94. Useful for finding the positions of all the tempo changes in a file.
  95. @param timeSigEvents a list to which all the events will be added
  96. */
  97. void findAllTimeSigEvents (MidiMessageSequence& timeSigEvents) const;
  98. /** Makes a list of all the time-signature meta-events from all tracks in the midi file.
  99. @param keySigEvents a list to which all the events will be added
  100. */
  101. void findAllKeySigEvents (MidiMessageSequence& keySigEvents) const;
  102. /** Returns the latest timestamp in any of the tracks.
  103. (Useful for finding the length of the file).
  104. */
  105. double getLastTimestamp() const;
  106. //==============================================================================
  107. /** Reads a midi file format stream.
  108. After calling this, you can get the tracks that were read from the file by using the
  109. getNumTracks() and getTrack() methods.
  110. The timestamps of the midi events in the tracks will represent their positions in
  111. terms of midi ticks. To convert them to seconds, use the convertTimestampTicksToSeconds()
  112. method.
  113. @returns true if the stream was read successfully
  114. */
  115. bool readFrom (InputStream& sourceStream);
  116. /** Writes the midi tracks as a standard midi file.
  117. @returns true if the operation succeeded.
  118. */
  119. bool writeTo (OutputStream& destStream);
  120. /** Converts the timestamp of all the midi events from midi ticks to seconds.
  121. This will use the midi time format and tempo/time signature info in the
  122. tracks to convert all the timestamps to absolute values in seconds.
  123. */
  124. void convertTimestampTicksToSeconds();
  125. private:
  126. //==============================================================================
  127. OwnedArray<MidiMessageSequence> tracks;
  128. short timeFormat;
  129. void readNextTrack (const uint8*, int size);
  130. void writeTrack (OutputStream&, int trackNum);
  131. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MidiFile)
  132. };
  133. #endif // JUCE_MIDIFILE_H_INCLUDED