MIDIFileReader.h
Go to the documentation of this file.
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  This is a modified version of a source file from the
17  Rosegarden MIDI and audio sequencer and notation editor.
18  This file copyright 2000-2006 Richard Bown and Chris Cannam.
19 */
20 
21 #ifndef SV_MIDI_FILE_READER_H
22 #define SV_MIDI_FILE_READER_H
23 
24 #include "DataFileReader.h"
25 #include "base/RealTime.h"
26 
27 #include <map>
28 #include <set>
29 #include <vector>
30 
31 #include <QObject>
32 
33 class MIDIEvent;
35 
36 typedef unsigned char MIDIByte;
37 
38 class MIDIFileImportPreferenceAcquirer // welcome to our grand marble foyer
39 {
40 public:
46  };
47 
49 
51  (QStringList trackNames, bool haveSomePercussion,
52  QString &singleTrack) const = 0;
53 
54  virtual void showError(QString error) = 0;
55 };
56 
57 
59 {
60  Q_OBJECT
61 
62 public:
63  MIDIFileReader(QString path,
64  MIDIFileImportPreferenceAcquirer *pref, // may be null
65  sv_samplerate_t mainModelSampleRate,
66  ProgressReporter *reporter = 0);
67  virtual ~MIDIFileReader();
68 
69  bool isOK() const override;
70  QString getError() const override;
71  Model *load() const override;
72 
73 protected:
74  typedef std::vector<MIDIEvent *> MIDITrack;
75  typedef std::map<unsigned int, MIDITrack> MIDIComposition;
76  typedef std::pair<RealTime, double> TempoChange; // time, qpm
77  typedef std::map<unsigned long, TempoChange> TempoMap; // key is MIDI time
78 
79  typedef enum {
80  MIDI_SINGLE_TRACK_FILE = 0x00,
81  MIDI_SIMULTANEOUS_TRACK_FILE = 0x01,
82  MIDI_SEQUENTIAL_TRACK_FILE = 0x02,
83  MIDI_FILE_BAD_FORMAT = 0xFF
85 
86  bool parseFile();
87  bool parseHeader(const std::string &midiHeader);
88  bool parseTrack(unsigned int &trackNum);
89 
90  Model *loadTrack(unsigned int trackNum,
91  Model *existingModel = 0,
92  int minProgress = 0,
93  int progressAmount = 100) const;
94 
95  bool consolidateNoteOffEvents(unsigned int track);
96  void updateTempoMap(unsigned int track);
97  void calculateTempoTimestamps();
98  RealTime getTimeForMIDITime(unsigned long midiTime) const;
99 
100  // Internal convenience functions
101  //
102  int midiBytesToInt(const std::string &bytes);
103  long midiBytesToLong(const std::string &bytes);
104 
105  long getNumberFromMIDIBytes(int firstByte = -1);
106 
107  MIDIByte getMIDIByte();
108  std::string getMIDIBytes(unsigned long bytes);
109 
110  bool skipToNextTrack();
111 
112  bool m_smpte;
113  int m_timingDivision; // pulses per quarter note
114  int m_fps; // if smpte
115  int m_subframes; // if smpte
117  unsigned int m_numberOfTracks;
118 
121 
122  std::map<int, QString> m_trackNames;
123  std::set<unsigned int> m_loadableTracks;
124  std::set<unsigned int> m_percussionTracks;
125  MIDIComposition m_midiComposition;
126  TempoMap m_tempoMap;
127 
128  QString m_path;
129  std::ifstream *m_midiFile;
130  size_t m_fileSize;
131  QString m_error;
133 
135 };
136 
137 
138 #endif // _MIDI_FILE_READER_H_
double sv_samplerate_t
Sample rate.
Definition: BaseTypes.h:51
std::set< unsigned int > m_percussionTracks
TempoMap m_tempoMap
std::pair< RealTime, double > TempoChange
std::map< unsigned long, TempoChange > TempoMap
MIDIComposition m_midiComposition
unsigned int m_numberOfTracks
MIDIFileFormatType m_format
std::ifstream * m_midiFile
MIDIFileImportPreferenceAcquirer * m_acquirer
virtual TrackPreference getTrackImportPreference(QStringList trackNames, bool haveSomePercussion, QString &singleTrack) const =0
Model is the base class for all data models that represent any sort of data on a time scale based on ...
Definition: Model.h:51
sv_samplerate_t m_mainModelSampleRate
std::map< int, QString > m_trackNames
virtual void showError(QString error)=0
std::set< unsigned int > m_loadableTracks
std::map< unsigned int, MIDITrack > MIDIComposition
std::vector< MIDIEvent * > MIDITrack
unsigned char MIDIByte
RealTime represents time values to nanosecond precision with accurate arithmetic and frame-rate conve...
Definition: RealTime.h:42