svcore  1.9
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 /*
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 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,
65  int mainModelSampleRate);
66  virtual ~MIDIFileReader();
67 
68  virtual bool isOK() const;
69  virtual QString getError() const;
70  virtual Model *load() const;
71 
72 protected:
73  typedef std::vector<MIDIEvent *> MIDITrack;
74  typedef std::map<unsigned int, MIDITrack> MIDIComposition;
75  typedef std::pair<RealTime, double> TempoChange; // time, qpm
76  typedef std::map<unsigned long, TempoChange> TempoMap; // key is MIDI time
77 
78  typedef enum {
84 
85  bool parseFile();
86  bool parseHeader(const std::string &midiHeader);
87  bool parseTrack(unsigned int &trackNum);
88 
89  Model *loadTrack(unsigned int trackNum,
90  Model *existingModel = 0,
91  int minProgress = 0,
92  int progressAmount = 100) const;
93 
94  bool consolidateNoteOffEvents(unsigned int track);
95  void updateTempoMap(unsigned int track);
97  RealTime getTimeForMIDITime(unsigned long midiTime) const;
98 
99  // Internal convenience functions
100  //
101  int midiBytesToInt(const std::string &bytes);
102  long midiBytesToLong(const std::string &bytes);
103 
104  long getNumberFromMIDIBytes(int firstByte = -1);
105 
107  std::string getMIDIBytes(unsigned long bytes);
108 
109  bool skipToNextTrack();
110 
111  bool m_smpte;
112  int m_timingDivision; // pulses per quarter note
113  int m_fps; // if smpte
114  int m_subframes; // if smpte
116  unsigned int m_numberOfTracks;
117 
120 
121  std::map<int, QString> m_trackNames;
122  std::set<unsigned int> m_loadableTracks;
123  std::set<unsigned int> m_percussionTracks;
126 
127  QString m_path;
128  std::ifstream *m_midiFile;
130  QString m_error;
132 
134 };
135 
136 
137 #endif // _MIDI_FILE_READER_H_
std::set< unsigned int > m_percussionTracks
Model * loadTrack(unsigned int trackNum, Model *existingModel=0, int minProgress=0, int progressAmount=100) const
TempoMap m_tempoMap
std::pair< RealTime, double > TempoChange
std::map< unsigned long, TempoChange > TempoMap
MIDIComposition m_midiComposition
unsigned int m_numberOfTracks
virtual QString getError() const
bool parseHeader(const std::string &midiHeader)
MIDIFileFormatType m_format
long midiBytesToLong(const std::string &bytes)
MIDIFileReader(QString path, MIDIFileImportPreferenceAcquirer *pref, int mainModelSampleRate)
MIDIByte getMIDIByte()
long getNumberFromMIDIBytes(int firstByte=-1)
RealTime getTimeForMIDITime(unsigned long midiTime) const
std::ifstream * m_midiFile
MIDIFileImportPreferenceAcquirer * m_acquirer
int midiBytesToInt(const std::string &bytes)
virtual TrackPreference getTrackImportPreference(QStringList trackNames, bool haveSomePercussion, QString &singleTrack) const =0
bool parseTrack(unsigned int &trackNum)
Model is the base class for all data models that represent any sort of data on a time scale based on ...
Definition: Model.h:35
bool consolidateNoteOffEvents(unsigned int track)
void calculateTempoTimestamps()
std::map< int, QString > m_trackNames
void updateTempoMap(unsigned int track)
virtual bool isOK() const
Return true if the file appears to be of the correct type.
virtual void showError(QString error)=0
virtual ~MIDIFileReader()
std::string getMIDIBytes(unsigned long bytes)
virtual Model * load() const
Read the file and return the corresponding data model.
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:35