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.

190 lines
7.5KB

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