Revision 5:7fde3cc109dc
| MIDIComposition.h | ||
|---|---|---|
| 34 | 34 |
#define _MIDI_COMPOSITION_H_ |
| 35 | 35 |
|
| 36 | 36 |
#include "MIDIEvent.h" |
| 37 |
#include <QLinkedList>
|
|
| 38 |
#include <QMap>
|
|
| 37 |
#include <vector>
|
|
| 38 |
#include <map>
|
|
| 39 | 39 |
|
| 40 |
typedef QLinkedList<MIDIEvent> MIDITrack;
|
|
| 41 |
typedef QMap<unsigned int, MIDITrack> MIDIComposition;
|
|
| 40 |
typedef std::vector<MIDIEvent> MIDITrack;
|
|
| 41 |
typedef std::map<unsigned int, MIDITrack> MIDIComposition;
|
|
| 42 | 42 |
|
| 43 | 43 |
#endif |
| MIDIEvent.h | ||
|---|---|---|
| 33 | 33 |
#ifndef _MIDI_EVENT_H_ |
| 34 | 34 |
#define _MIDI_EVENT_H_ |
| 35 | 35 |
|
| 36 |
#include <QString> |
|
| 37 | 36 |
#include <string> |
| 38 | 37 |
#include <iostream> |
| 39 | 38 |
|
| ... | ... | |
| 227 | 226 |
class MIDIException : virtual public std::exception |
| 228 | 227 |
{
|
| 229 | 228 |
public: |
| 230 |
MIDIException(QString message) throw() : m_message(message) {
|
|
| 231 |
std::cerr << "WARNING: MIDI exception: " |
|
| 232 |
<< message.toLocal8Bit().data() << std::endl; |
|
| 229 |
MIDIException(std::string message) throw() : m_message(message) {
|
|
| 230 |
std::cerr << "WARNING: MIDI exception: " << message.c_str() << std::endl; |
|
| 233 | 231 |
} |
| 234 | 232 |
virtual ~MIDIException() throw() { }
|
| 235 | 233 |
|
| 236 | 234 |
virtual const char *what() const throw() {
|
| 237 |
return m_message.toLocal8Bit().data();
|
|
| 235 |
return m_message.c_str();
|
|
| 238 | 236 |
} |
| 239 | 237 |
|
| 240 | 238 |
protected: |
| 241 |
QString m_message;
|
|
| 239 |
std::string m_message;
|
|
| 242 | 240 |
}; |
| 243 | 241 |
|
| 244 | 242 |
#endif |
| MIDIFileReader.cpp | ||
|---|---|---|
| 39 | 39 |
#include "MIDIFileReader.h" |
| 40 | 40 |
#include "MIDIEvent.h" |
| 41 | 41 |
|
| 42 |
#include <QString> |
|
| 43 |
#include <QVector> |
|
| 44 |
|
|
| 45 | 42 |
#include <sstream> |
| 46 | 43 |
|
| 47 | 44 |
using std::string; |
| ... | ... | |
| 56 | 53 |
|
| 57 | 54 |
//#define DEBUG_MIDI_FILE_READER 1 |
| 58 | 55 |
|
| 56 |
#define throw_exception(...) do { \
|
|
| 57 |
char message[128]; \ |
|
| 58 |
snprintf(message, 128, __VA_ARGS__); \ |
|
| 59 |
throw MIDIException(std::string(message)); \ |
|
| 60 |
} while (0) |
|
| 61 |
|
|
| 59 | 62 |
|
| 60 |
MIDIFileReader::MIDIFileReader(QString path) : |
|
| 63 |
|
|
| 64 |
MIDIFileReader::MIDIFileReader(std::string path) : |
|
| 61 | 65 |
m_timingDivision(0), |
| 62 | 66 |
m_format(MIDI_FILE_BAD_FORMAT), |
| 63 | 67 |
m_numberOfTracks(0), |
| ... | ... | |
| 82 | 86 |
return (m_error == ""); |
| 83 | 87 |
} |
| 84 | 88 |
|
| 85 |
QString
|
|
| 89 |
std::string
|
|
| 86 | 90 |
MIDIFileReader::getError() const |
| 87 | 91 |
{
|
| 88 | 92 |
return m_error; |
| ... | ... | |
| 92 | 96 |
MIDIFileReader::midiBytesToLong(const string& bytes) |
| 93 | 97 |
{
|
| 94 | 98 |
if (bytes.length() != 4) {
|
| 95 |
throw MIDIException(QObject::tr("Wrong length for long data in MIDI stream (%1, should be %2)").arg(bytes.length()).arg(4));
|
|
| 99 |
throw_exception("Wrong length for long data in MIDI stream (%d, should be %d)", (int)bytes.length(), 4);
|
|
| 96 | 100 |
} |
| 97 | 101 |
|
| 98 | 102 |
long longRet = ((long)(((MIDIByte)bytes[0]) << 24)) | |
| ... | ... | |
| 107 | 111 |
MIDIFileReader::midiBytesToInt(const string& bytes) |
| 108 | 112 |
{
|
| 109 | 113 |
if (bytes.length() != 2) {
|
| 110 |
throw MIDIException(QObject::tr("Wrong length for int data in MIDI stream (%1, should be %2)").arg(bytes.length()).arg(2));
|
|
| 114 |
throw_exception("Wrong length for int data in MIDI stream (%d, should be %d)", (int)bytes.length(), 2);
|
|
| 111 | 115 |
} |
| 112 | 116 |
|
| 113 | 117 |
int intRet = ((int)(((MIDIByte)bytes[0]) << 8)) | |
| ... | ... | |
| 124 | 128 |
MIDIFileReader::getMIDIByte() |
| 125 | 129 |
{
|
| 126 | 130 |
if (!m_midiFile) {
|
| 127 |
throw MIDIException(QObject::tr("getMIDIByte called but no MIDI file open"));
|
|
| 131 |
throw_exception("getMIDIByte called but no MIDI file open");
|
|
| 128 | 132 |
} |
| 129 | 133 |
|
| 130 | 134 |
if (m_midiFile->eof()) {
|
| 131 |
throw MIDIException(QObject::tr("End of MIDI file encountered while reading"));
|
|
| 135 |
throw_exception("End of MIDI file encountered while reading");
|
|
| 132 | 136 |
} |
| 133 | 137 |
|
| 134 | 138 |
if (m_decrementCount && m_trackByteCount <= 0) {
|
| 135 |
throw MIDIException(QObject::tr("Attempt to get more bytes than expected on Track"));
|
|
| 139 |
throw_exception("Attempt to get more bytes than expected on Track");
|
|
| 136 | 140 |
} |
| 137 | 141 |
|
| 138 | 142 |
char byte; |
| ... | ... | |
| 141 | 145 |
return (MIDIByte)byte; |
| 142 | 146 |
} |
| 143 | 147 |
|
| 144 |
throw MIDIException(QObject::tr("Attempt to read past MIDI file end"));
|
|
| 148 |
throw_exception("Attempt to read past MIDI file end");
|
|
| 145 | 149 |
} |
| 146 | 150 |
|
| 147 | 151 |
|
| ... | ... | |
| 153 | 157 |
MIDIFileReader::getMIDIBytes(unsigned long numberOfBytes) |
| 154 | 158 |
{
|
| 155 | 159 |
if (!m_midiFile) {
|
| 156 |
throw MIDIException(QObject::tr("getMIDIBytes called but no MIDI file open"));
|
|
| 160 |
throw_exception("getMIDIBytes called but no MIDI file open");
|
|
| 157 | 161 |
} |
| 158 | 162 |
|
| 159 | 163 |
if (m_midiFile->eof()) {
|
| 160 |
throw MIDIException(QObject::tr("End of MIDI file encountered while reading"));
|
|
| 164 |
throw_exception("End of MIDI file encountered while reading");
|
|
| 161 | 165 |
} |
| 162 | 166 |
|
| 163 | 167 |
if (m_decrementCount && (numberOfBytes > (unsigned long)m_trackByteCount)) {
|
| 164 |
throw MIDIException(QObject::tr("Attempt to get more bytes than available on Track (%1, only have %2)").arg(numberOfBytes).arg(m_trackByteCount));
|
|
| 168 |
throw_exception("Attempt to get more bytes than available on Track (%lu, only have %ld)", numberOfBytes, m_trackByteCount);
|
|
| 165 | 169 |
} |
| 166 | 170 |
|
| 167 | 171 |
string stringRet; |
| ... | ... | |
| 177 | 181 |
// |
| 178 | 182 |
if (stringRet.length() < numberOfBytes) {
|
| 179 | 183 |
stringRet = ""; |
| 180 |
throw MIDIException(QObject::tr("Attempt to read past MIDI file end"));
|
|
| 184 |
throw_exception("Attempt to read past MIDI file end");
|
|
| 181 | 185 |
} |
| 182 | 186 |
|
| 183 | 187 |
// decrement the byte count |
| ... | ... | |
| 194 | 198 |
MIDIFileReader::getNumberFromMIDIBytes(int firstByte) |
| 195 | 199 |
{
|
| 196 | 200 |
if (!m_midiFile) {
|
| 197 |
throw MIDIException(QObject::tr("getNumberFromMIDIBytes called but no MIDI file open"));
|
|
| 201 |
throw_exception("getNumberFromMIDIBytes called but no MIDI file open");
|
|
| 198 | 202 |
} |
| 199 | 203 |
|
| 200 | 204 |
long longRet = 0; |
| ... | ... | |
| 228 | 232 |
MIDIFileReader::skipToNextTrack() |
| 229 | 233 |
{
|
| 230 | 234 |
if (!m_midiFile) {
|
| 231 |
throw MIDIException(QObject::tr("skipToNextTrack called but no MIDI file open"));
|
|
| 235 |
throw_exception("skipToNextTrack called but no MIDI file open");
|
|
| 232 | 236 |
} |
| 233 | 237 |
|
| 234 | 238 |
string buffer, buffer2; |
| ... | ... | |
| 265 | 269 |
#endif |
| 266 | 270 |
|
| 267 | 271 |
// Open the file |
| 268 |
m_midiFile = new ifstream(m_path.toLocal8Bit().data(), |
|
| 269 |
ios::in | ios::binary); |
|
| 272 |
m_midiFile = new ifstream(m_path.c_str(), ios::in | ios::binary); |
|
| 270 | 273 |
|
| 271 | 274 |
if (!*m_midiFile) {
|
| 272 | 275 |
m_error = "File not found or not readable."; |
| ... | ... | |
| 429 | 432 |
cerr << "WARNING: Invalid event code " << eventCode |
| 430 | 433 |
<< " in MIDI file" << endl; |
| 431 | 434 |
#endif |
| 432 |
throw MIDIException(QObject::tr("Invalid event code %1 found").arg(int(eventCode)));
|
|
| 435 |
throw_exception("Invalid event code %d found", int(eventCode));
|
|
| 433 | 436 |
} |
| 434 | 437 |
|
| 435 | 438 |
deltaTime = getNumberFromMIDIBytes(); |
| ... | ... | |
| 444 | 447 |
if (!(midiByte & MIDI_STATUS_BYTE_MASK)) {
|
| 445 | 448 |
|
| 446 | 449 |
if (runningStatus < 0) {
|
| 447 |
throw MIDIException(QObject::tr("Running status used for first event in track"));
|
|
| 450 |
throw_exception("Running status used for first event in track");
|
|
| 448 | 451 |
} |
| 449 | 452 |
|
| 450 | 453 |
eventCode = (MIDIByte)runningStatus; |
| MIDIFileReader.h | ||
|---|---|---|
| 33 | 33 |
#ifndef _MIDI_FILE_READER_H_ |
| 34 | 34 |
#define _MIDI_FILE_READER_H_ |
| 35 | 35 |
|
| 36 |
#include <QObject> |
|
| 37 |
#include <QList> |
|
| 38 |
#include <QMap> |
|
| 39 |
#include <QSet> |
|
| 36 |
#include "MIDIComposition.h" |
|
| 40 | 37 |
|
| 38 |
#include <set> |
|
| 41 | 39 |
#include <iostream> |
| 42 | 40 |
|
| 43 |
#include "MIDIComposition.h" |
|
| 44 |
|
|
| 45 | 41 |
typedef unsigned char MIDIByte; |
| 46 | 42 |
|
| 47 | 43 |
class MIDIFileReader |
| 48 | 44 |
{
|
| 49 | 45 |
public: |
| 50 |
MIDIFileReader(QString path);
|
|
| 46 |
MIDIFileReader(std::string path);
|
|
| 51 | 47 |
virtual ~MIDIFileReader(); |
| 52 | 48 |
|
| 53 | 49 |
virtual bool isOK() const; |
| 54 |
virtual QString getError() const;
|
|
| 50 |
virtual std::string getError() const;
|
|
| 55 | 51 |
|
| 56 | 52 |
virtual MIDIComposition load() const; |
| 57 | 53 |
|
| ... | ... | |
| 84 | 80 |
long m_trackByteCount; |
| 85 | 81 |
bool m_decrementCount; |
| 86 | 82 |
|
| 87 |
QMap<int, QString> m_trackNames;
|
|
| 83 |
std::map<int, std::string> m_trackNames;
|
|
| 88 | 84 |
MIDIComposition m_midiComposition; |
| 89 | 85 |
|
| 90 |
QString m_path;
|
|
| 86 |
std::string m_path;
|
|
| 91 | 87 |
std::ifstream *m_midiFile; |
| 92 | 88 |
size_t m_fileSize; |
| 93 |
QString m_error;
|
|
| 89 |
std::string m_error;
|
|
| 94 | 90 |
}; |
| 95 | 91 |
|
| 96 | 92 |
|
| Makefile | ||
|---|---|---|
| 1 |
|
|
| 2 |
all: midifile libmidireader.a |
|
| 3 |
|
|
| 4 |
LIB_OBJECTS := MIDIFileReader.o |
|
| 5 |
OBJECTS := $(LIB_OBJECTS) main.o |
|
| 6 |
|
|
| 7 |
midifile: $(OBJECTS) |
|
| 8 |
$(CXX) -o $@ $^ |
|
| 9 |
|
|
| 10 |
libmidireader.a: $(LIB_OBJECTS) |
|
| 11 |
$(AR) cr $@ $^ |
|
| 12 |
|
|
| 13 |
clean: |
|
| 14 |
rm $(OBJECTS) |
|
| 15 |
|
|
| main.cpp | ||
|---|---|---|
| 40 | 40 |
return 1; |
| 41 | 41 |
} |
| 42 | 42 |
|
| 43 |
QString filename = argv[1];
|
|
| 43 |
std::string filename = argv[1];
|
|
| 44 | 44 |
|
| 45 | 45 |
MIDIFileReader fr(filename); |
| 46 | 46 |
if (!fr.isOK()) {
|
| 47 |
std::cerr << "Error: " << fr.getError().toStdString() << std::endl;
|
|
| 47 |
std::cerr << "Error: " << fr.getError().c_str() << std::endl;
|
|
| 48 | 48 |
return 1; |
| 49 | 49 |
} |
| 50 | 50 |
|
| ... | ... | |
| 70 | 70 |
|
| 71 | 71 |
for (MIDIComposition::const_iterator i = c.begin(); i != c.end(); ++i) {
|
| 72 | 72 |
|
| 73 |
cout << "Start of track: " << i.key()+1 << endl;
|
|
| 73 |
cout << "Start of track: " << i->first+1 << endl;
|
|
| 74 | 74 |
|
| 75 |
for (MIDITrack::const_iterator j = i->begin(); j != i->end(); ++j) {
|
|
| 75 |
for (MIDITrack::const_iterator j = i->second.begin(); j != i->second.end(); ++j) {
|
|
| 76 | 76 |
|
| 77 | 77 |
unsigned int t = j->getTime(); |
| 78 | 78 |
int ch = j->getChannelNumber(); |
| midifile.pro | ||
|---|---|---|
| 1 |
|
|
| 2 |
TEMPLATE = app |
|
| 3 |
TARGET = |
|
| 4 |
QT -= gui |
|
| 5 |
DEPENDPATH += . |
|
| 6 |
INCLUDEPATH += . |
|
| 7 |
|
|
| 8 |
HEADERS += MIDIEvent.h MIDIComposition.h MIDIFileReader.h |
|
| 9 |
SOURCES += MIDIFileReader.cpp main.cpp |
|
Also available in: Unified diff