changeset 5:7fde3cc109dc midifile

Remove dependencies on Qt
author Chris Cannam
date Tue, 17 May 2011 10:38:16 +0100
parents a98a66b43882
children 467d1e3dc4cc
files MIDIComposition.h MIDIEvent.h MIDIFileReader.cpp MIDIFileReader.h Makefile main.cpp midifile.pro
diffstat 7 files changed, 58 insertions(+), 55 deletions(-) [+]
line wrap: on
line diff
--- a/MIDIComposition.h	Mon May 16 13:58:09 2011 +0100
+++ b/MIDIComposition.h	Tue May 17 10:38:16 2011 +0100
@@ -34,10 +34,10 @@
 #define _MIDI_COMPOSITION_H_
 
 #include "MIDIEvent.h"
-#include <QLinkedList>
-#include <QMap>
+#include <vector>
+#include <map>
 
-typedef QLinkedList<MIDIEvent> MIDITrack;
-typedef QMap<unsigned int, MIDITrack> MIDIComposition;
+typedef std::vector<MIDIEvent> MIDITrack;
+typedef std::map<unsigned int, MIDITrack> MIDIComposition;
 
 #endif
--- a/MIDIEvent.h	Mon May 16 13:58:09 2011 +0100
+++ b/MIDIEvent.h	Tue May 17 10:38:16 2011 +0100
@@ -33,7 +33,6 @@
 #ifndef _MIDI_EVENT_H_
 #define _MIDI_EVENT_H_
 
-#include <QString>
 #include <string>
 #include <iostream>
 
@@ -227,18 +226,17 @@
 class MIDIException : virtual public std::exception
 {
 public:
-    MIDIException(QString message) throw() : m_message(message) {
-        std::cerr << "WARNING: MIDI exception: "
-		  << message.toLocal8Bit().data() << std::endl;
+    MIDIException(std::string message) throw() : m_message(message) {
+        std::cerr << "WARNING: MIDI exception: " << message.c_str() << std::endl;
     }
     virtual ~MIDIException() throw() { }
 
     virtual const char *what() const throw() {
-	return m_message.toLocal8Bit().data();
+	return m_message.c_str();
     }
 
 protected:
-    QString m_message;
+    std::string m_message;
 };
 
 #endif
--- a/MIDIFileReader.cpp	Mon May 16 13:58:09 2011 +0100
+++ b/MIDIFileReader.cpp	Tue May 17 10:38:16 2011 +0100
@@ -39,9 +39,6 @@
 #include "MIDIFileReader.h"
 #include "MIDIEvent.h"
 
-#include <QString>
-#include <QVector>
-
 #include <sstream>
 
 using std::string;
@@ -56,8 +53,15 @@
 
 //#define DEBUG_MIDI_FILE_READER 1
 
+#define throw_exception(...) do { \
+        char message[128]; \
+        snprintf(message, 128, __VA_ARGS__); \
+        throw MIDIException(std::string(message)); \
+    } while (0)
+    
 
-MIDIFileReader::MIDIFileReader(QString path) :
+
+MIDIFileReader::MIDIFileReader(std::string path) :
     m_timingDivision(0),
     m_format(MIDI_FILE_BAD_FORMAT),
     m_numberOfTracks(0),
@@ -82,7 +86,7 @@
     return (m_error == "");
 }
 
-QString
+std::string
 MIDIFileReader::getError() const
 {
     return m_error;
@@ -92,7 +96,7 @@
 MIDIFileReader::midiBytesToLong(const string& bytes)
 {
     if (bytes.length() != 4) {
-	throw MIDIException(QObject::tr("Wrong length for long data in MIDI stream (%1, should be %2)").arg(bytes.length()).arg(4));
+	throw_exception("Wrong length for long data in MIDI stream (%d, should be %d)", (int)bytes.length(), 4);
     }
 
     long longRet = ((long)(((MIDIByte)bytes[0]) << 24)) |
@@ -107,7 +111,7 @@
 MIDIFileReader::midiBytesToInt(const string& bytes)
 {
     if (bytes.length() != 2) {
-	throw MIDIException(QObject::tr("Wrong length for int data in MIDI stream (%1, should be %2)").arg(bytes.length()).arg(2));
+	throw_exception("Wrong length for int data in MIDI stream (%d, should be %d)", (int)bytes.length(), 2);
     }
 
     int intRet = ((int)(((MIDIByte)bytes[0]) << 8)) |
@@ -124,15 +128,15 @@
 MIDIFileReader::getMIDIByte()
 {
     if (!m_midiFile) {
-	throw MIDIException(QObject::tr("getMIDIByte called but no MIDI file open"));
+	throw_exception("getMIDIByte called but no MIDI file open");
     }
 
     if (m_midiFile->eof()) {
-        throw MIDIException(QObject::tr("End of MIDI file encountered while reading"));
+        throw_exception("End of MIDI file encountered while reading");
     }
 
     if (m_decrementCount && m_trackByteCount <= 0) {
-        throw MIDIException(QObject::tr("Attempt to get more bytes than expected on Track"));
+        throw_exception("Attempt to get more bytes than expected on Track");
     }
 
     char byte;
@@ -141,7 +145,7 @@
 	return (MIDIByte)byte;
     }
 
-    throw MIDIException(QObject::tr("Attempt to read past MIDI file end"));
+    throw_exception("Attempt to read past MIDI file end");
 }
 
 
@@ -153,15 +157,15 @@
 MIDIFileReader::getMIDIBytes(unsigned long numberOfBytes)
 {
     if (!m_midiFile) {
-	throw MIDIException(QObject::tr("getMIDIBytes called but no MIDI file open"));
+	throw_exception("getMIDIBytes called but no MIDI file open");
     }
 
     if (m_midiFile->eof()) {
-        throw MIDIException(QObject::tr("End of MIDI file encountered while reading"));
+        throw_exception("End of MIDI file encountered while reading");
     }
 
     if (m_decrementCount && (numberOfBytes > (unsigned long)m_trackByteCount)) {
-        throw MIDIException(QObject::tr("Attempt to get more bytes than available on Track (%1, only have %2)").arg(numberOfBytes).arg(m_trackByteCount));
+        throw_exception("Attempt to get more bytes than available on Track (%lu, only have %ld)", numberOfBytes, m_trackByteCount);
     }
 
     string stringRet;
@@ -177,7 +181,7 @@
     //
     if (stringRet.length() < numberOfBytes) {
         stringRet = "";
-        throw MIDIException(QObject::tr("Attempt to read past MIDI file end"));
+        throw_exception("Attempt to read past MIDI file end");
     }
 
     // decrement the byte count
@@ -194,7 +198,7 @@
 MIDIFileReader::getNumberFromMIDIBytes(int firstByte)
 {
     if (!m_midiFile) {
-	throw MIDIException(QObject::tr("getNumberFromMIDIBytes called but no MIDI file open"));
+	throw_exception("getNumberFromMIDIBytes called but no MIDI file open");
     }
 
     long longRet = 0;
@@ -228,7 +232,7 @@
 MIDIFileReader::skipToNextTrack()
 {
     if (!m_midiFile) {
-	throw MIDIException(QObject::tr("skipToNextTrack called but no MIDI file open"));
+	throw_exception("skipToNextTrack called but no MIDI file open");
     }
 
     string buffer, buffer2;
@@ -265,8 +269,7 @@
 #endif
 
     // Open the file
-    m_midiFile = new ifstream(m_path.toLocal8Bit().data(),
-			      ios::in | ios::binary);
+    m_midiFile = new ifstream(m_path.c_str(), ios::in | ios::binary);
 
     if (!*m_midiFile) {
 	m_error = "File not found or not readable.";
@@ -429,7 +432,7 @@
 	    cerr << "WARNING: Invalid event code " << eventCode
 		 << " in MIDI file" << endl;
 #endif
-	    throw MIDIException(QObject::tr("Invalid event code %1 found").arg(int(eventCode)));
+	    throw_exception("Invalid event code %d found", int(eventCode));
 	}
 
         deltaTime = getNumberFromMIDIBytes();
@@ -444,7 +447,7 @@
         if (!(midiByte & MIDI_STATUS_BYTE_MASK)) {
 
 	    if (runningStatus < 0) {
-		throw MIDIException(QObject::tr("Running status used for first event in track"));
+		throw_exception("Running status used for first event in track");
 	    }
 
 	    eventCode = (MIDIByte)runningStatus;
--- a/MIDIFileReader.h	Mon May 16 13:58:09 2011 +0100
+++ b/MIDIFileReader.h	Tue May 17 10:38:16 2011 +0100
@@ -33,25 +33,21 @@
 #ifndef _MIDI_FILE_READER_H_
 #define _MIDI_FILE_READER_H_
 
-#include <QObject>
-#include <QList>
-#include <QMap>
-#include <QSet>
+#include "MIDIComposition.h"
 
+#include <set>
 #include <iostream>
 
-#include "MIDIComposition.h"
-
 typedef unsigned char MIDIByte;
 
 class MIDIFileReader
 {
 public:
-    MIDIFileReader(QString path);
+    MIDIFileReader(std::string path);
     virtual ~MIDIFileReader();
 
     virtual bool isOK() const;
-    virtual QString getError() const;
+    virtual std::string getError() const;
 
     virtual MIDIComposition load() const;
 
@@ -84,13 +80,13 @@
     long                   m_trackByteCount;
     bool                   m_decrementCount;
 
-    QMap<int, QString>     m_trackNames;
+    std::map<int, std::string> m_trackNames;
     MIDIComposition        m_midiComposition;
 
-    QString                m_path;
+    std::string            m_path;
     std::ifstream         *m_midiFile;
     size_t                 m_fileSize;
-    QString                m_error;
+    std::string            m_error;
 };
 
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Makefile	Tue May 17 10:38:16 2011 +0100
@@ -0,0 +1,15 @@
+
+all:		midifile libmidireader.a
+
+LIB_OBJECTS	:= MIDIFileReader.o
+OBJECTS		:= $(LIB_OBJECTS) main.o
+
+midifile:	$(OBJECTS)
+		$(CXX) -o $@ $^
+
+libmidireader.a:	$(LIB_OBJECTS)
+		$(AR) cr $@ $^
+
+clean:		
+		rm $(OBJECTS)
+
--- a/main.cpp	Mon May 16 13:58:09 2011 +0100
+++ b/main.cpp	Tue May 17 10:38:16 2011 +0100
@@ -40,11 +40,11 @@
 	return 1;
     }
 
-    QString filename = argv[1];
+    std::string filename = argv[1];
 
     MIDIFileReader fr(filename);
     if (!fr.isOK()) {
-	std::cerr << "Error: " << fr.getError().toStdString() << std::endl;
+	std::cerr << "Error: " << fr.getError().c_str() << std::endl;
 	return 1;
     }
 
@@ -70,9 +70,9 @@
 
     for (MIDIComposition::const_iterator i = c.begin(); i != c.end(); ++i) {
 	
-	cout << "Start of track: " << i.key()+1 << endl;
+	cout << "Start of track: " << i->first+1 << endl;
 
-	for (MIDITrack::const_iterator j = i->begin(); j != i->end(); ++j) {
+	for (MIDITrack::const_iterator j = i->second.begin(); j != i->second.end(); ++j) {
 
 	    unsigned int t = j->getTime();
 	    int ch = j->getChannelNumber();
--- a/midifile.pro	Mon May 16 13:58:09 2011 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,9 +0,0 @@
-
-TEMPLATE = app
-TARGET = 
-QT -= gui
-DEPENDPATH += .
-INCLUDEPATH += .
-
-HEADERS += MIDIEvent.h MIDIComposition.h MIDIFileReader.h
-SOURCES += MIDIFileReader.cpp main.cpp