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.

136 lines
5.1KB

  1. #ifndef STK_MIDIFILEIN_H
  2. #define STK_MIDIFILEIN_H
  3. #include "Stk.h"
  4. #include <string>
  5. #include <vector>
  6. #include <fstream>
  7. #include <sstream>
  8. namespace stk {
  9. /**********************************************************************/
  10. /*! \class MidiFileIn
  11. \brief A standard MIDI file reading/parsing class.
  12. This class can be used to read events from a standard MIDI file.
  13. Event bytes are copied to a C++ vector and must be subsequently
  14. interpreted by the user. The function getNextMidiEvent() skips
  15. meta and sysex events, returning only MIDI channel messages.
  16. Event delta-times are returned in the form of "ticks" and a
  17. function is provided to determine the current "seconds per tick".
  18. Tempo changes are internally tracked by the class and reflected in
  19. the values returned by the function getTickSeconds().
  20. by Gary P. Scavone, 2003 - 2010.
  21. */
  22. /**********************************************************************/
  23. class MidiFileIn : public Stk
  24. {
  25. public:
  26. //! Default constructor.
  27. /*!
  28. If an error occurs while opening or parsing the file header, an
  29. StkError exception will be thrown.
  30. */
  31. MidiFileIn( std::string fileName );
  32. //! Class destructor.
  33. ~MidiFileIn();
  34. //! Return the MIDI file format (0, 1, or 2).
  35. int getFileFormat() const { return format_; };
  36. //! Return the number of tracks in the MIDI file.
  37. unsigned int getNumberOfTracks() const { return nTracks_; };
  38. //! Return the MIDI file division value from the file header.
  39. /*!
  40. Note that this value must be "parsed" in accordance with the
  41. MIDI File Specification. In particular, if the MSB is set, the
  42. file uses time-code representations for delta-time values.
  43. */
  44. int getDivision() const { return division_; };
  45. //! Move the specified track event reader to the beginning of its track.
  46. /*!
  47. The relevant track tempo value is reset as well. If an invalid
  48. track number is specified, an StkError exception will be thrown.
  49. */
  50. void rewindTrack( unsigned int track = 0 );
  51. //! Get the current value, in seconds, of delta-time ticks for the specified track.
  52. /*!
  53. This value can change as events are read (via "Set Tempo"
  54. Meta-Events). Therefore, one should call this function after
  55. every call to getNextEvent() or getNextMidiEvent(). If an
  56. invalid track number is specified, an StkError exception will be
  57. thrown.
  58. */
  59. double getTickSeconds( unsigned int track = 0 );
  60. //! Fill the user-provided vector with the next event in the specified track and return the event delta-time in ticks.
  61. /*!
  62. MIDI File events consist of a delta time and a sequence of event
  63. bytes. This function returns the delta-time value and writes
  64. the subsequent event bytes directly to the event vector. The
  65. user must parse the event bytes in accordance with the MIDI File
  66. Specification. All returned MIDI channel events are complete
  67. ... a status byte is provided even when running status is used
  68. in the file. If the track has reached its end, no bytes will be
  69. written and the event vector size will be zero. If an invalid
  70. track number is specified or an error occurs while reading the
  71. file, an StkError exception will be thrown.
  72. */
  73. unsigned long getNextEvent( std::vector<unsigned char> *event, unsigned int track = 0 );
  74. //! Fill the user-provided vector with the next MIDI channel event in the specified track and return the event delta time in ticks.
  75. /*!
  76. All returned MIDI events are complete ... a status byte is
  77. provided even when running status is used in the file. Meta and
  78. sysex events in the track are skipped though "Set Tempo" events
  79. are properly parsed for use by the getTickSeconds() function.
  80. If the track has reached its end, no bytes will be written and
  81. the event vector size will be zero. If an invalid track number
  82. is specified or an error occurs while reading the file, an
  83. StkError exception will be thrown.
  84. */
  85. unsigned long getNextMidiEvent( std::vector<unsigned char> *midiEvent, unsigned int track = 0 );
  86. protected:
  87. // This protected class function is used for reading variable-length
  88. // MIDI file values. It is assumed that this function is called with
  89. // the file read pointer positioned at the start of a
  90. // variable-length value. The function returns true if the value is
  91. // successfully parsed. Otherwise, it returns false.
  92. bool readVariableLength( unsigned long *value );
  93. std::ifstream file_;
  94. unsigned int nTracks_;
  95. int format_;
  96. int division_;
  97. bool usingTimeCode_;
  98. std::vector<double> tickSeconds_;
  99. std::vector<long> trackPointers_;
  100. std::vector<long> trackOffsets_;
  101. std::vector<long> trackLengths_;
  102. std::vector<char> trackStatus_;
  103. // This structure and the following variables are used to save and
  104. // keep track of a format 1 tempo map (and the initial tickSeconds
  105. // parameter for formats 0 and 2).
  106. struct TempoChange {
  107. unsigned long count;
  108. double tickSeconds;
  109. };
  110. std::vector<TempoChange> tempoEvents_;
  111. std::vector<unsigned long> trackCounters_;
  112. std::vector<unsigned int> trackTempoIndex_;
  113. };
  114. } // stk namespace
  115. #endif