Revision 5:7fde3cc109dc MIDIFileReader.cpp
| 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; |
Also available in: Unified diff