The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

188 lines
7.0KB

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