andrew@0: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ andrew@0: andrew@0: /* andrew@0: This is a modified version of a source file from the andrew@0: Rosegarden MIDI and audio sequencer and notation editor. andrew@0: This file copyright 2000-2010 Richard Bown and Chris Cannam. andrew@0: andrew@0: Permission is hereby granted, free of charge, to any person andrew@0: obtaining a copy of this software and associated documentation andrew@0: files (the "Software"), to deal in the Software without andrew@0: restriction, including without limitation the rights to use, copy, andrew@0: modify, merge, publish, distribute, sublicense, and/or sell copies andrew@0: of the Software, and to permit persons to whom the Software is andrew@0: furnished to do so, subject to the following conditions: andrew@0: andrew@0: The above copyright notice and this permission notice shall be andrew@0: included in all copies or substantial portions of the Software. andrew@0: andrew@0: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, andrew@0: EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF andrew@0: MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND andrew@0: NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR andrew@0: ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF andrew@0: CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION andrew@0: WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. andrew@0: andrew@0: Except as contained in this notice, the names of the authors andrew@0: shall not be used in advertising or otherwise to promote the sale, andrew@0: use or other dealings in this Software without prior written andrew@0: authorization. andrew@0: */ andrew@0: andrew@0: #ifndef _MIDI_FILE_READER_H_ andrew@0: #define _MIDI_FILE_READER_H_ andrew@0: andrew@0: #include "MIDIComposition.h" andrew@0: andrew@0: #include andrew@0: #include andrew@0: andrew@0: typedef unsigned char MIDIByte; andrew@0: andrew@0: class MIDIFileReader andrew@0: { andrew@0: public: andrew@0: MIDIFileReader(std::string path); andrew@0: virtual ~MIDIFileReader(); andrew@0: andrew@0: virtual bool isOK() const; andrew@0: virtual std::string getError() const; andrew@0: andrew@0: virtual MIDIComposition load() const; andrew@0: andrew@0: MIDIConstants::MIDIFileFormatType getFormat() const { return m_format; } andrew@0: int getTimingDivision() const { return m_timingDivision; } andrew@0: andrew@0: protected: andrew@0: andrew@0: bool parseFile(); andrew@0: bool parseHeader(const std::string &midiHeader); andrew@0: bool parseTrack(unsigned int trackNum); andrew@0: bool consolidateNoteOffEvents(unsigned int track); andrew@0: andrew@0: // Internal convenience functions andrew@0: // andrew@0: int midiBytesToInt(const std::string &bytes); andrew@0: long midiBytesToLong(const std::string &bytes); andrew@0: andrew@0: long getNumberFromMIDIBytes(int firstByte = -1); andrew@0: andrew@0: MIDIByte getMIDIByte(); andrew@0: std::string getMIDIBytes(unsigned long bytes); andrew@0: andrew@0: bool skipToNextTrack(); andrew@0: andrew@0: int m_timingDivision; // pulses per quarter note andrew@0: MIDIConstants::MIDIFileFormatType m_format; andrew@0: unsigned int m_numberOfTracks; andrew@0: andrew@0: long m_trackByteCount; andrew@0: bool m_decrementCount; andrew@0: andrew@0: std::map m_trackNames; andrew@0: MIDIComposition m_midiComposition; andrew@0: andrew@0: std::string m_path; andrew@0: std::ifstream *m_midiFile; andrew@0: size_t m_fileSize; andrew@0: std::string m_error; andrew@24: andrew@0: }; andrew@0: andrew@0: andrew@0: #endif // _MIDI_FILE_READER_H_