00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef TSE3_MIDIFILE_H
00018 #define TSE3_MIDIFILE_H
00019
00020 #include "tse3/listen/MidiFile.h"
00021
00022 #include "tse3/Notifier.h"
00023 #include "tse3/Playable.h"
00024
00025 #include <string>
00026 #include <iosfwd>
00027 #include <cstddef>
00028
00029 namespace TSE3
00030 {
00031 class Song;
00032 class Track;
00033 class MidiFileImport;
00034 class Progress;
00035
00061 class MidiFileImport : public Playable,
00062 public Notifier<MidiFileImportListener>
00063 {
00064 public:
00065
00081 MidiFileImport(const std::string &filename,
00082 int verbose = 0,
00083 std::ostream &out = std::cout);
00084 ~MidiFileImport();
00085
00098 Song *load(Progress *progress = 0);
00099
00103 virtual PlayableIterator *iterator(Clock index);
00104
00116 virtual Clock lastClock() const;
00117
00118 friend class MidiFileImportIterator;
00119
00120 private:
00121
00125 void loadHeader();
00126
00133 void loadMTrk(size_t &pos, Song *song, int mtrkNo);
00134
00138 void loadMeta(size_t &pos, Song *song, Track *track,
00139 int mtrkNo, Clock time, int &port,
00140 Clock &end);
00141
00150 int readFixed(size_t &pos, int length);
00151
00159 int readVariable(size_t &pos);
00160
00161 std::string filename;
00162 int verbose;
00163 std::ostream &out;
00164
00165 unsigned char *file;
00166 std::streampos fileSize;
00167
00168 int filePPQN, fileFormat;
00169 size_t fileNoMTrks, fileHeaderSize;
00170 Clock fileLastClock;
00171 };
00172
00182 class MidiFileExport
00183 {
00184 public:
00185
00203 MidiFileExport(int format = 1,
00204 bool compact = true,
00205 int verbose = 0,
00206 std::ostream &d_out = std::cout);
00207 ~MidiFileExport();
00208
00218 void save(const std::string &filename, Song *song,
00219 Progress *progress = 0);
00220
00224 void save(std::ostream &out, Song *song, Progress *progress = 0);
00225
00226
00227
00228
00229
00238 int format() const { return _format; }
00239
00248 size_t size() const { return _size; }
00249
00258 int noMTrks() const { return _noMTrks; }
00259
00269 bool compact() const { return _compact; }
00270
00271 private:
00272
00280 void writeMTrk(std::ostream &out, PlayableIterator *p,
00281 const std::string &title = "");
00282
00287 void writeMTrk_outputLoop(std::ostream &out, PlayableIterator *p);
00288
00294 void writeMidiEvent(std::ostream &out, const MidiEvent &e);
00295
00301 void writeMetaEvent(std::ostream &out, const MidiEvent &e);
00302
00309 void writeFixed(std::ostream &out, int value, int length);
00310
00317 void writeVariable(std::ostream &out, int value);
00318
00326 void writeString(std::ostream &out, const std::string &str,
00327 bool terminate = false);
00328
00329 int verbose;
00330 std::ostream &diag_out;
00331 int _format;
00332 bool _compact;
00333
00334
00335
00336
00337 TSE3::Song *song;
00338 size_t _noMTrks;
00339 std::streampos MTrkPos;
00340 size_t MTrkSize;
00341
00342 unsigned int runningStatus;
00343 Clock lastEventClock;
00344 size_t _size;
00345 };
00346 }
00347
00348 #endif