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.

178 lines
6.9KB

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