comparison data/fileio/MIDIFileReader.h @ 148:1a42221a1522

* Reorganising code base. This revision will not compile.
author Chris Cannam
date Mon, 31 Jul 2006 11:49:58 +0000
parents
children 21b9b25bff48
comparison
equal deleted inserted replaced
147:3a13b0d4934e 148:1a42221a1522
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2
3 /*
4 Sonic Visualiser
5 An audio file viewer and annotation editor.
6 Centre for Digital Music, Queen Mary, University of London.
7
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2 of the
11 License, or (at your option) any later version. See the file
12 COPYING included with this distribution for more information.
13 */
14
15
16 /*
17 This is a modified version of a source file from the
18 Rosegarden MIDI and audio sequencer and notation editor.
19 This file copyright 2000-2006 Richard Bown and Chris Cannam.
20 */
21
22 #ifndef _MIDI_FILE_READER_H_
23 #define _MIDI_FILE_READER_H_
24
25 #include "DataFileReader.h"
26 #include "base/RealTime.h"
27
28 #include <map>
29 #include <set>
30 #include <vector>
31
32 #include <QObject>
33
34 class MIDIEvent;
35
36 class MIDIFileReader : public DataFileReader, public QObject
37 {
38 public:
39 MIDIFileReader(QString path, size_t mainModelSampleRate);
40 virtual ~MIDIFileReader();
41
42 virtual bool isOK() const;
43 virtual QString getError() const;
44 virtual Model *load() const;
45
46 typedef unsigned char MIDIByte;
47
48 protected:
49 typedef std::vector<MIDIEvent *> MIDITrack;
50 typedef std::map<unsigned int, MIDITrack> MIDIComposition;
51 typedef std::pair<RealTime, double> TempoChange; // time, qpm
52 typedef std::map<unsigned long, TempoChange> TempoMap; // key is MIDI time
53
54 typedef enum {
55 MIDI_SINGLE_TRACK_FILE = 0x00,
56 MIDI_SIMULTANEOUS_TRACK_FILE = 0x01,
57 MIDI_SEQUENTIAL_TRACK_FILE = 0x02,
58 MIDI_FILE_BAD_FORMAT = 0xFF
59 } MIDIFileFormatType;
60
61 bool parseFile();
62 bool parseHeader(const std::string &midiHeader);
63 bool parseTrack(unsigned int &trackNum);
64
65 Model *loadTrack(unsigned int trackNum,
66 Model *existingModel = 0,
67 int minProgress = 0,
68 int progressAmount = 100) const;
69
70 bool consolidateNoteOffEvents(unsigned int track);
71 void updateTempoMap(unsigned int track);
72 void calculateTempoTimestamps();
73 RealTime getTimeForMIDITime(unsigned long midiTime) const;
74
75 // Internal convenience functions
76 //
77 int midiBytesToInt(const std::string &bytes);
78 long midiBytesToLong(const std::string &bytes);
79
80 long getNumberFromMIDIBytes(int firstByte = -1);
81
82 MIDIByte getMIDIByte();
83 std::string getMIDIBytes(unsigned long bytes);
84
85 bool skipToNextTrack();
86
87 int m_timingDivision; // pulses per quarter note
88 MIDIFileFormatType m_format;
89 unsigned int m_numberOfTracks;
90
91 long m_trackByteCount;
92 bool m_decrementCount;
93
94 std::map<int, QString> m_trackNames;
95 std::set<unsigned int> m_loadableTracks;
96 std::set<unsigned int> m_percussionTracks;
97 MIDIComposition m_midiComposition;
98 TempoMap m_tempoMap;
99
100 QString m_path;
101 std::ifstream *m_midiFile;
102 size_t m_fileSize;
103 QString m_error;
104 size_t m_mainModelSampleRate;
105 };
106
107
108 #endif // _MIDI_FILE_READER_H_