Chris@148: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@148: Chris@148: /* Chris@148: Sonic Visualiser Chris@148: An audio file viewer and annotation editor. Chris@148: Centre for Digital Music, Queen Mary, University of London. Chris@148: Chris@148: This program is free software; you can redistribute it and/or Chris@148: modify it under the terms of the GNU General Public License as Chris@148: published by the Free Software Foundation; either version 2 of the Chris@148: License, or (at your option) any later version. See the file Chris@148: COPYING included with this distribution for more information. Chris@148: */ Chris@148: Chris@148: Chris@148: /* Chris@148: This is a modified version of a source file from the Chris@148: Rosegarden MIDI and audio sequencer and notation editor. Chris@148: This file copyright 2000-2006 Richard Bown and Chris Cannam. Chris@148: */ Chris@148: Chris@148: Chris@148: #include Chris@148: #include Chris@148: #include Chris@148: #include Chris@148: #include Chris@148: Chris@148: #include "MIDIFileReader.h" Chris@148: Chris@560: #include "data/midi/MIDIEvent.h" Chris@301: Chris@150: #include "model/Model.h" Chris@148: #include "base/Pitch.h" Chris@148: #include "base/RealTime.h" Chris@148: #include "model/NoteModel.h" Chris@148: Chris@148: #include Chris@1030: #include Chris@148: Chris@148: #include Chris@148: Chris@843: #include "base/Debug.h" Chris@843: Chris@148: using std::string; Chris@148: using std::ifstream; Chris@148: using std::stringstream; Chris@148: using std::ends; Chris@148: using std::ios; Chris@148: using std::vector; Chris@148: using std::map; Chris@148: using std::set; Chris@148: Chris@301: using namespace MIDIConstants; Chris@301: Chris@1363: //#define MIDI_DEBUG 1 Chris@148: Chris@148: Chris@148: MIDIFileReader::MIDIFileReader(QString path, Chris@392: MIDIFileImportPreferenceAcquirer *acquirer, Chris@1491: sv_samplerate_t mainModelSampleRate, Chris@1491: ProgressReporter *) : // we don't actually report progress Chris@613: m_smpte(false), Chris@148: m_timingDivision(0), Chris@613: m_fps(0), Chris@613: m_subframes(0), Chris@148: m_format(MIDI_FILE_BAD_FORMAT), Chris@148: m_numberOfTracks(0), Chris@148: m_trackByteCount(0), Chris@148: m_decrementCount(false), Chris@148: m_path(path), Chris@1582: m_midiFile(nullptr), Chris@148: m_fileSize(0), Chris@392: m_mainModelSampleRate(mainModelSampleRate), Chris@392: m_acquirer(acquirer) Chris@148: { Chris@148: if (parseFile()) { Chris@1429: m_error = ""; Chris@148: } Chris@148: } Chris@148: Chris@148: MIDIFileReader::~MIDIFileReader() Chris@148: { Chris@148: for (MIDIComposition::iterator i = m_midiComposition.begin(); Chris@1429: i != m_midiComposition.end(); ++i) { Chris@1429: Chris@1429: for (MIDITrack::iterator j = i->second.begin(); Chris@1429: j != i->second.end(); ++j) { Chris@1429: delete *j; Chris@1429: } Chris@148: Chris@1429: i->second.clear(); Chris@148: } Chris@148: Chris@148: m_midiComposition.clear(); Chris@148: } Chris@148: Chris@148: bool Chris@148: MIDIFileReader::isOK() const Chris@148: { Chris@148: return (m_error == ""); Chris@148: } Chris@148: Chris@148: QString Chris@148: MIDIFileReader::getError() const Chris@148: { Chris@148: return m_error; Chris@148: } Chris@148: Chris@148: long Chris@148: MIDIFileReader::midiBytesToLong(const string& bytes) Chris@148: { Chris@148: if (bytes.length() != 4) { Chris@1429: throw MIDIException(tr("Wrong length for long data in MIDI stream (%1, should be %2)").arg(bytes.length()).arg(4)); Chris@148: } Chris@148: Chris@148: long longRet = ((long)(((MIDIByte)bytes[0]) << 24)) | Chris@148: ((long)(((MIDIByte)bytes[1]) << 16)) | Chris@148: ((long)(((MIDIByte)bytes[2]) << 8)) | Chris@148: ((long)((MIDIByte)(bytes[3]))); Chris@148: Chris@148: return longRet; Chris@148: } Chris@148: Chris@148: int Chris@148: MIDIFileReader::midiBytesToInt(const string& bytes) Chris@148: { Chris@148: if (bytes.length() != 2) { Chris@1429: throw MIDIException(tr("Wrong length for int data in MIDI stream (%1, should be %2)").arg(bytes.length()).arg(2)); Chris@148: } Chris@148: Chris@148: int intRet = ((int)(((MIDIByte)bytes[0]) << 8)) | Chris@148: ((int)(((MIDIByte)bytes[1]))); Chris@148: return(intRet); Chris@148: } Chris@148: Chris@148: Chris@148: // Gets a single byte from the MIDI byte stream. For each track Chris@148: // section we can read only a specified number of bytes held in Chris@148: // m_trackByteCount. Chris@148: // Chris@301: MIDIByte Chris@148: MIDIFileReader::getMIDIByte() Chris@148: { Chris@148: if (!m_midiFile) { Chris@1429: throw MIDIException(tr("getMIDIByte called but no MIDI file open")); Chris@148: } Chris@148: Chris@148: if (m_midiFile->eof()) { Chris@148: throw MIDIException(tr("End of MIDI file encountered while reading")); Chris@148: } Chris@148: Chris@148: if (m_decrementCount && m_trackByteCount <= 0) { Chris@148: throw MIDIException(tr("Attempt to get more bytes than expected on Track")); Chris@148: } Chris@148: Chris@148: char byte; Chris@148: if (m_midiFile->read(&byte, 1)) { Chris@1429: --m_trackByteCount; Chris@1429: return (MIDIByte)byte; Chris@148: } Chris@148: Chris@148: throw MIDIException(tr("Attempt to read past MIDI file end")); Chris@148: } Chris@148: Chris@148: Chris@148: // Gets a specified number of bytes from the MIDI byte stream. For Chris@148: // each track section we can read only a specified number of bytes Chris@148: // held in m_trackByteCount. Chris@148: // Chris@148: string Chris@148: MIDIFileReader::getMIDIBytes(unsigned long numberOfBytes) Chris@148: { Chris@148: if (!m_midiFile) { Chris@1429: throw MIDIException(tr("getMIDIBytes called but no MIDI file open")); Chris@148: } Chris@148: Chris@148: if (m_midiFile->eof()) { Chris@148: throw MIDIException(tr("End of MIDI file encountered while reading")); Chris@148: } Chris@148: Chris@148: if (m_decrementCount && (numberOfBytes > (unsigned long)m_trackByteCount)) { Chris@148: throw MIDIException(tr("Attempt to get more bytes than available on Track (%1, only have %2)").arg(numberOfBytes).arg(m_trackByteCount)); Chris@148: } Chris@148: Chris@148: string stringRet; Chris@148: char fileMIDIByte; Chris@148: Chris@148: while (stringRet.length() < numberOfBytes && Chris@148: m_midiFile->read(&fileMIDIByte, 1)) { Chris@148: stringRet += fileMIDIByte; Chris@148: } Chris@148: Chris@148: // if we've reached the end of file without fulfilling the Chris@148: // quota then panic as our parsing has performed incorrectly Chris@148: // Chris@148: if (stringRet.length() < numberOfBytes) { Chris@148: stringRet = ""; Chris@148: throw MIDIException(tr("Attempt to read past MIDI file end")); Chris@148: } Chris@148: Chris@148: // decrement the byte count Chris@148: if (m_decrementCount) Chris@148: m_trackByteCount -= stringRet.length(); Chris@148: Chris@148: return stringRet; Chris@148: } Chris@148: Chris@148: Chris@148: // Get a long number of variable length from the MIDI byte stream. Chris@148: // Chris@148: long Chris@148: MIDIFileReader::getNumberFromMIDIBytes(int firstByte) Chris@148: { Chris@148: if (!m_midiFile) { Chris@1429: throw MIDIException(tr("getNumberFromMIDIBytes called but no MIDI file open")); Chris@148: } Chris@148: Chris@148: long longRet = 0; Chris@148: MIDIByte midiByte; Chris@148: Chris@148: if (firstByte >= 0) { Chris@1429: midiByte = (MIDIByte)firstByte; Chris@148: } else if (m_midiFile->eof()) { Chris@1429: return longRet; Chris@148: } else { Chris@1429: midiByte = getMIDIByte(); Chris@148: } Chris@148: Chris@148: longRet = midiByte; Chris@148: if (midiByte & 0x80) { Chris@1429: longRet &= 0x7F; Chris@1429: do { Chris@1429: midiByte = getMIDIByte(); Chris@1429: longRet = (longRet << 7) + (midiByte & 0x7F); Chris@1429: } while (!m_midiFile->eof() && (midiByte & 0x80)); Chris@148: } Chris@148: Chris@148: return longRet; Chris@148: } Chris@148: Chris@148: Chris@148: // Seek to the next track in the midi file and set the number Chris@148: // of bytes to be read in the counter m_trackByteCount. Chris@148: // Chris@148: bool Chris@148: MIDIFileReader::skipToNextTrack() Chris@148: { Chris@148: if (!m_midiFile) { Chris@1429: throw MIDIException(tr("skipToNextTrack called but no MIDI file open")); Chris@148: } Chris@148: Chris@148: string buffer, buffer2; Chris@148: m_trackByteCount = -1; Chris@148: m_decrementCount = false; Chris@148: Chris@148: while (!m_midiFile->eof() && (m_decrementCount == false)) { Chris@148: buffer = getMIDIBytes(4); Chris@1429: if (buffer.compare(0, 4, MIDI_TRACK_HEADER) == 0) { Chris@1429: m_trackByteCount = midiBytesToLong(getMIDIBytes(4)); Chris@1429: m_decrementCount = true; Chris@1429: } Chris@148: } Chris@148: Chris@148: if (m_trackByteCount == -1) { // we haven't found a track Chris@148: return false; Chris@148: } else { Chris@148: return true; Chris@148: } Chris@148: } Chris@148: Chris@148: Chris@148: // Read in a MIDI file. The parsing process throws exceptions back up Chris@148: // here if we run into trouble which we can then pass back out to Chris@148: // whoever called us using a nice bool. Chris@148: // Chris@148: bool Chris@148: MIDIFileReader::parseFile() Chris@148: { Chris@148: m_error = ""; Chris@148: Chris@148: #ifdef MIDI_DEBUG Chris@690: SVDEBUG << "MIDIFileReader::open() : fileName = " << m_fileName.c_str() << endl; Chris@148: #endif Chris@148: Chris@148: // Open the file Chris@148: m_midiFile = new ifstream(m_path.toLocal8Bit().data(), Chris@1429: ios::in | ios::binary); Chris@148: Chris@148: if (!*m_midiFile) { Chris@1429: m_error = "File not found or not readable."; Chris@1429: m_format = MIDI_FILE_BAD_FORMAT; Chris@1429: delete m_midiFile; Chris@1582: m_midiFile = nullptr; Chris@1429: return false; Chris@148: } Chris@148: Chris@148: bool retval = false; Chris@148: Chris@148: try { Chris@148: Chris@1429: // Set file size so we can count it off Chris@1429: // Chris@1429: m_midiFile->seekg(0, ios::end); Chris@1038: std::streamoff off = m_midiFile->tellg(); Chris@1429: m_fileSize = 0; Chris@1038: if (off > 0) m_fileSize = off; Chris@1429: m_midiFile->seekg(0, ios::beg); Chris@148: Chris@1429: // Parse the MIDI header first. The first 14 bytes of the file. Chris@1429: if (!parseHeader(getMIDIBytes(14))) { Chris@1429: m_format = MIDI_FILE_BAD_FORMAT; Chris@1429: m_error = "Not a MIDI file."; Chris@1429: goto done; Chris@1429: } Chris@148: Chris@1429: unsigned int i = 0; Chris@148: Chris@1429: for (unsigned int j = 0; j < m_numberOfTracks; ++j) { Chris@148: Chris@148: #ifdef MIDI_DEBUG Chris@1429: SVDEBUG << "Parsing Track " << j << endl; Chris@148: #endif Chris@148: Chris@1429: if (!skipToNextTrack()) { Chris@148: #ifdef MIDI_DEBUG Chris@1429: SVDEBUG << "Couldn't find Track " << j << endl; Chris@148: #endif Chris@1429: m_error = "File corrupted or in non-standard format?"; Chris@1429: m_format = MIDI_FILE_BAD_FORMAT; Chris@1429: goto done; Chris@1429: } Chris@148: Chris@148: #ifdef MIDI_DEBUG Chris@1429: SVDEBUG << "Track has " << m_trackByteCount << " bytes" << endl; Chris@148: #endif Chris@148: Chris@1429: // Run through the events taking them into our internal Chris@1429: // representation. Chris@1429: if (!parseTrack(i)) { Chris@148: #ifdef MIDI_DEBUG Chris@1429: SVDEBUG << "Track " << j << " parsing failed" << endl; Chris@148: #endif Chris@1429: m_error = "File corrupted or in non-standard format?"; Chris@1429: m_format = MIDI_FILE_BAD_FORMAT; Chris@1429: goto done; Chris@1429: } Chris@148: Chris@1429: ++i; // j is the source track number, i the destination Chris@1429: } Chris@1429: Chris@1429: m_numberOfTracks = i; Chris@1429: retval = true; Chris@148: Chris@1465: } catch (const MIDIException &e) { Chris@148: Chris@690: SVDEBUG << "MIDIFileReader::open() - caught exception - " << e.what() << endl; Chris@1429: m_error = e.what(); Chris@148: } Chris@148: Chris@148: done: Chris@148: m_midiFile->close(); Chris@148: delete m_midiFile; Chris@148: Chris@148: for (unsigned int track = 0; track < m_numberOfTracks; ++track) { Chris@148: Chris@148: // Convert the deltaTime to an absolute time since the track Chris@148: // start. The addTime method returns the sum of the current Chris@148: // MIDI Event delta time plus the argument. Chris@148: Chris@1429: unsigned long acc = 0; Chris@148: Chris@148: for (MIDITrack::iterator i = m_midiComposition[track].begin(); Chris@148: i != m_midiComposition[track].end(); ++i) { Chris@148: acc = (*i)->addTime(acc); Chris@148: } Chris@148: Chris@148: if (consolidateNoteOffEvents(track)) { // returns true if some notes exist Chris@1429: m_loadableTracks.insert(track); Chris@1429: } Chris@148: } Chris@148: Chris@148: for (unsigned int track = 0; track < m_numberOfTracks; ++track) { Chris@148: updateTempoMap(track); Chris@148: } Chris@148: Chris@148: calculateTempoTimestamps(); Chris@148: Chris@148: return retval; Chris@148: } Chris@148: Chris@148: // Parse and ensure the MIDI Header is legitimate Chris@148: // Chris@148: bool Chris@148: MIDIFileReader::parseHeader(const string &midiHeader) Chris@148: { Chris@148: if (midiHeader.size() < 14) { Chris@148: #ifdef MIDI_DEBUG Chris@690: SVDEBUG << "MIDIFileReader::parseHeader() - file header undersized" << endl; Chris@148: #endif Chris@148: return false; Chris@148: } Chris@148: Chris@148: if (midiHeader.compare(0, 4, MIDI_FILE_HEADER) != 0) { Chris@148: #ifdef MIDI_DEBUG Chris@1429: SVDEBUG << "MIDIFileReader::parseHeader()" Chris@1429: << "- file header not found or malformed" Chris@1429: << endl; Chris@148: #endif Chris@1429: return false; Chris@148: } Chris@148: Chris@148: if (midiBytesToLong(midiHeader.substr(4,4)) != 6L) { Chris@148: #ifdef MIDI_DEBUG Chris@690: SVDEBUG << "MIDIFileReader::parseHeader()" Chris@1429: << " - header length incorrect" Chris@1429: << endl; Chris@148: #endif Chris@148: return false; Chris@148: } Chris@148: Chris@148: m_format = (MIDIFileFormatType) midiBytesToInt(midiHeader.substr(8,2)); Chris@148: m_numberOfTracks = midiBytesToInt(midiHeader.substr(10,2)); Chris@148: m_timingDivision = midiBytesToInt(midiHeader.substr(12,2)); Chris@148: Chris@613: if (m_timingDivision >= 32768) { Chris@613: m_smpte = true; Chris@613: m_fps = 256 - (m_timingDivision >> 8); Chris@613: m_subframes = (m_timingDivision & 0xff); Chris@613: } else { Chris@613: m_smpte = false; Chris@148: } Chris@148: Chris@148: return true; Chris@148: } Chris@148: Chris@148: // Extract the contents from a MIDI file track and places it into Chris@148: // our local map of MIDI events. Chris@148: // Chris@148: bool Chris@148: MIDIFileReader::parseTrack(unsigned int &lastTrackNum) Chris@148: { Chris@148: MIDIByte midiByte, metaEventCode, data1, data2; Chris@148: MIDIByte eventCode = 0x80; Chris@148: string metaMessage; Chris@1038: long messageLength; Chris@1038: long deltaTime; Chris@1038: long accumulatedTime = 0; Chris@148: Chris@148: // The trackNum passed in to this method is the default track for Chris@148: // all events provided they're all on the same channel. If we find Chris@148: // events on more than one channel, we increment trackNum and record Chris@148: // the mapping from channel to trackNum in this channelTrackMap. Chris@148: // We then return the new trackNum by reference so the calling Chris@148: // method knows we've got more tracks than expected. Chris@148: Chris@148: // This would be a vector but we need -1 to indicate Chris@148: // "not yet used" Chris@148: vector channelTrackMap(16, -1); Chris@148: Chris@148: // This is used to store the last absolute time found on each track, Chris@148: // allowing us to modify delta-times correctly when separating events Chris@148: // out from one to multiple tracks Chris@148: // Chris@148: map trackTimeMap; Chris@148: Chris@148: // Meta-events don't have a channel, so we place them in a fixed Chris@148: // track number instead Chris@148: unsigned int metaTrack = lastTrackNum; Chris@148: Chris@148: // Remember the last non-meta status byte (-1 if we haven't seen one) Chris@148: int runningStatus = -1; Chris@148: Chris@148: bool firstTrack = true; Chris@148: Chris@148: while (!m_midiFile->eof() && (m_trackByteCount > 0)) { Chris@148: Chris@1429: if (eventCode < 0x80) { Chris@148: #ifdef MIDI_DEBUG Chris@1429: SVDEBUG << "WARNING: Invalid event code " << eventCode Chris@1429: << " in MIDI file" << endl; Chris@148: #endif Chris@1429: throw MIDIException(tr("Invalid event code %1 found").arg(int(eventCode))); Chris@1429: } Chris@148: Chris@148: deltaTime = getNumberFromMIDIBytes(); Chris@148: Chris@148: #ifdef MIDI_DEBUG Chris@1429: SVDEBUG << "read delta time " << deltaTime << endl; Chris@148: #endif Chris@148: Chris@148: // Get a single byte Chris@148: midiByte = getMIDIByte(); Chris@148: Chris@148: if (!(midiByte & MIDI_STATUS_BYTE_MASK)) { Chris@148: Chris@1429: if (runningStatus < 0) { Chris@1429: throw MIDIException(tr("Running status used for first event in track")); Chris@1429: } Chris@148: Chris@1429: eventCode = (MIDIByte)runningStatus; Chris@1429: data1 = midiByte; Chris@148: Chris@148: #ifdef MIDI_DEBUG Chris@1429: SVDEBUG << "using running status (byte " << int(midiByte) << " found)" << endl; Chris@148: #endif Chris@148: } else { Chris@148: #ifdef MIDI_DEBUG Chris@1429: SVDEBUG << "have new event code " << int(midiByte) << endl; Chris@148: #endif Chris@148: eventCode = midiByte; Chris@1429: data1 = getMIDIByte(); Chris@1429: } Chris@148: Chris@148: if (eventCode == MIDI_FILE_META_EVENT) { Chris@148: Chris@1429: metaEventCode = data1; Chris@148: messageLength = getNumberFromMIDIBytes(); Chris@148: Chris@148: //#ifdef MIDI_DEBUG Chris@1429: SVDEBUG << "Meta event of type " << int(metaEventCode) << " and " << messageLength << " bytes found, putting on track " << metaTrack << endl; Chris@148: //#endif Chris@148: metaMessage = getMIDIBytes(messageLength); Chris@148: Chris@1429: long gap = accumulatedTime - trackTimeMap[metaTrack]; Chris@1429: accumulatedTime += deltaTime; Chris@1429: deltaTime += gap; Chris@1429: trackTimeMap[metaTrack] = accumulatedTime; Chris@148: Chris@148: MIDIEvent *e = new MIDIEvent(deltaTime, Chris@148: MIDI_FILE_META_EVENT, Chris@148: metaEventCode, Chris@148: metaMessage); Chris@148: Chris@1429: m_midiComposition[metaTrack].push_back(e); Chris@148: Chris@1429: if (metaEventCode == MIDI_TRACK_NAME) { Chris@1429: m_trackNames[metaTrack] = metaMessage.c_str(); Chris@1429: } Chris@148: Chris@148: } else { // non-meta events Chris@148: Chris@1429: runningStatus = eventCode; Chris@148: Chris@148: MIDIEvent *midiEvent; Chris@148: Chris@1429: int channel = (eventCode & MIDI_CHANNEL_NUM_MASK); Chris@1429: if (channelTrackMap[channel] == -1) { Chris@1429: if (!firstTrack) ++lastTrackNum; Chris@1429: else firstTrack = false; Chris@1429: channelTrackMap[channel] = lastTrackNum; Chris@1429: } Chris@148: Chris@1429: unsigned int trackNum = channelTrackMap[channel]; Chris@1429: Chris@1429: // accumulatedTime is abs time of last event on any track; Chris@1429: // trackTimeMap[trackNum] is that of last event on this track Chris@1429: Chris@1429: long gap = accumulatedTime - trackTimeMap[trackNum]; Chris@1429: accumulatedTime += deltaTime; Chris@1429: deltaTime += gap; Chris@1429: trackTimeMap[trackNum] = accumulatedTime; Chris@148: Chris@148: switch (eventCode & MIDI_MESSAGE_TYPE_MASK) { Chris@148: Chris@148: case MIDI_NOTE_ON: Chris@148: case MIDI_NOTE_OFF: Chris@148: case MIDI_POLY_AFTERTOUCH: Chris@148: case MIDI_CTRL_CHANGE: Chris@148: data2 = getMIDIByte(); Chris@148: Chris@148: // create and store our event Chris@148: midiEvent = new MIDIEvent(deltaTime, eventCode, data1, data2); Chris@148: Chris@148: /* Chris@1429: SVDEBUG << "MIDI event for channel " << channel << " (track " Chris@1429: << trackNum << ")" << endl; Chris@1429: midiEvent->print(); Chris@148: */ Chris@148: Chris@148: Chris@148: m_midiComposition[trackNum].push_back(midiEvent); Chris@148: Chris@1429: if (midiEvent->getChannelNumber() == MIDI_PERCUSSION_CHANNEL) { Chris@1429: m_percussionTracks.insert(trackNum); Chris@1429: } Chris@148: Chris@148: break; Chris@148: Chris@148: case MIDI_PITCH_BEND: Chris@148: data2 = getMIDIByte(); Chris@148: Chris@148: // create and store our event Chris@148: midiEvent = new MIDIEvent(deltaTime, eventCode, data1, data2); Chris@148: m_midiComposition[trackNum].push_back(midiEvent); Chris@148: break; Chris@148: Chris@148: case MIDI_PROG_CHANGE: Chris@148: case MIDI_CHNL_AFTERTOUCH: Chris@148: // create and store our event Chris@148: midiEvent = new MIDIEvent(deltaTime, eventCode, data1); Chris@148: m_midiComposition[trackNum].push_back(midiEvent); Chris@148: break; Chris@148: Chris@148: case MIDI_SYSTEM_EXCLUSIVE: Chris@148: messageLength = getNumberFromMIDIBytes(data1); Chris@148: Chris@148: #ifdef MIDI_DEBUG Chris@1429: SVDEBUG << "SysEx of " << messageLength << " bytes found" << endl; Chris@148: #endif Chris@148: Chris@148: metaMessage= getMIDIBytes(messageLength); Chris@148: Chris@148: if (MIDIByte(metaMessage[metaMessage.length() - 1]) != Chris@148: MIDI_END_OF_EXCLUSIVE) Chris@148: { Chris@148: #ifdef MIDI_DEBUG Chris@690: SVDEBUG << "MIDIFileReader::parseTrack() - " Chris@148: << "malformed or unsupported SysEx type" Chris@148: << endl; Chris@148: #endif Chris@148: continue; Chris@148: } Chris@148: Chris@148: // chop off the EOX Chris@148: // length fixed by Pedro Lopez-Cabanillas (20030523) Chris@148: // Chris@148: metaMessage = metaMessage.substr(0, metaMessage.length()-1); Chris@148: Chris@148: midiEvent = new MIDIEvent(deltaTime, Chris@148: MIDI_SYSTEM_EXCLUSIVE, Chris@148: metaMessage); Chris@148: m_midiComposition[trackNum].push_back(midiEvent); Chris@148: break; Chris@148: Chris@148: default: Chris@148: #ifdef MIDI_DEBUG Chris@690: SVDEBUG << "MIDIFileReader::parseTrack()" Chris@148: << " - Unsupported MIDI Event Code: " Chris@148: << (int)eventCode << endl; Chris@148: #endif Chris@148: break; Chris@148: } Chris@148: } Chris@148: } Chris@148: Chris@148: if (lastTrackNum > metaTrack) { Chris@1429: for (unsigned int track = metaTrack + 1; track <= lastTrackNum; ++track) { Chris@1429: m_trackNames[track] = QString("%1 <%2>") Chris@1429: .arg(m_trackNames[metaTrack]).arg(track - metaTrack + 1); Chris@1429: } Chris@148: } Chris@148: Chris@148: return true; Chris@148: } Chris@148: Chris@148: // Delete dead NOTE OFF and NOTE ON/Zero Velocity Events after Chris@148: // reading them and modifying their relevant NOTE ONs. Return true Chris@148: // if there are some notes in this track. Chris@148: // Chris@148: bool Chris@148: MIDIFileReader::consolidateNoteOffEvents(unsigned int track) Chris@148: { Chris@148: bool notesOnTrack = false; Chris@148: bool noteOffFound; Chris@148: Chris@148: for (MIDITrack::iterator i = m_midiComposition[track].begin(); Chris@1429: i != m_midiComposition[track].end(); i++) { Chris@148: Chris@148: if ((*i)->getMessageType() == MIDI_NOTE_ON && (*i)->getVelocity() > 0) { Chris@148: Chris@1429: notesOnTrack = true; Chris@148: noteOffFound = false; Chris@148: Chris@148: for (MIDITrack::iterator j = i; Chris@1429: j != m_midiComposition[track].end(); j++) { Chris@148: Chris@148: if (((*j)->getChannelNumber() == (*i)->getChannelNumber()) && Chris@1429: ((*j)->getPitch() == (*i)->getPitch()) && Chris@148: ((*j)->getMessageType() == MIDI_NOTE_OFF || Chris@148: ((*j)->getMessageType() == MIDI_NOTE_ON && Chris@148: (*j)->getVelocity() == 0x00))) { Chris@148: Chris@148: (*i)->setDuration((*j)->getTime() - (*i)->getTime()); Chris@148: Chris@148: delete *j; Chris@148: m_midiComposition[track].erase(j); Chris@148: Chris@148: noteOffFound = true; Chris@148: break; Chris@148: } Chris@148: } Chris@148: Chris@148: // If no matching NOTE OFF has been found then set Chris@148: // Event duration to length of track Chris@148: // Chris@148: if (!noteOffFound) { Chris@1429: MIDITrack::iterator j = m_midiComposition[track].end(); Chris@1429: --j; Chris@613: (*i)->setDuration((*j)->getTime() - (*i)->getTime()); Chris@1429: } Chris@148: } Chris@148: } Chris@148: Chris@148: return notesOnTrack; Chris@148: } Chris@148: Chris@148: // Add any tempo events found in the given track to the global tempo map. Chris@148: // Chris@148: void Chris@148: MIDIFileReader::updateTempoMap(unsigned int track) Chris@148: { Chris@1363: SVDEBUG << "updateTempoMap for track " << track << " (" << m_midiComposition[track].size() << " events)" << endl; Chris@148: Chris@148: for (MIDITrack::iterator i = m_midiComposition[track].begin(); Chris@1429: i != m_midiComposition[track].end(); ++i) { Chris@148: Chris@148: if ((*i)->isMeta() && Chris@1429: (*i)->getMetaEventCode() == MIDI_SET_TEMPO) { Chris@148: Chris@1429: MIDIByte m0 = (*i)->getMetaMessage()[0]; Chris@1429: MIDIByte m1 = (*i)->getMetaMessage()[1]; Chris@1429: MIDIByte m2 = (*i)->getMetaMessage()[2]; Chris@1429: Chris@1429: long tempo = (((m0 << 8) + m1) << 8) + m2; Chris@148: Chris@1429: SVDEBUG << "updateTempoMap: have tempo, it's " << tempo << " at " << (*i)->getTime() << endl; Chris@148: Chris@1429: if (tempo != 0) { Chris@1429: double qpm = 60000000.0 / double(tempo); Chris@1429: m_tempoMap[(*i)->getTime()] = Chris@1429: TempoChange(RealTime::zeroTime, qpm); Chris@1429: } Chris@148: } Chris@148: } Chris@148: } Chris@148: Chris@148: void Chris@148: MIDIFileReader::calculateTempoTimestamps() Chris@148: { Chris@148: unsigned long lastMIDITime = 0; Chris@148: RealTime lastRealTime = RealTime::zeroTime; Chris@148: double tempo = 120.0; Chris@148: int td = m_timingDivision; Chris@148: if (td == 0) td = 96; Chris@148: Chris@148: for (TempoMap::iterator i = m_tempoMap.begin(); i != m_tempoMap.end(); ++i) { Chris@1429: Chris@1429: unsigned long mtime = i->first; Chris@1429: unsigned long melapsed = mtime - lastMIDITime; Chris@1429: double quarters = double(melapsed) / double(td); Chris@1429: double seconds = (60.0 * quarters) / tempo; Chris@148: Chris@1429: RealTime t = lastRealTime + RealTime::fromSeconds(seconds); Chris@148: Chris@1429: i->second.first = t; Chris@148: Chris@1429: lastRealTime = t; Chris@1429: lastMIDITime = mtime; Chris@1429: tempo = i->second.second; Chris@148: } Chris@148: } Chris@148: Chris@148: RealTime Chris@148: MIDIFileReader::getTimeForMIDITime(unsigned long midiTime) const Chris@148: { Chris@148: unsigned long tempoMIDITime = 0; Chris@148: RealTime tempoRealTime = RealTime::zeroTime; Chris@148: double tempo = 120.0; Chris@148: Chris@148: TempoMap::const_iterator i = m_tempoMap.lower_bound(midiTime); Chris@148: if (i != m_tempoMap.begin()) { Chris@1429: --i; Chris@1429: tempoMIDITime = i->first; Chris@1429: tempoRealTime = i->second.first; Chris@1429: tempo = i->second.second; Chris@148: } Chris@148: Chris@148: int td = m_timingDivision; Chris@148: if (td == 0) td = 96; Chris@148: Chris@148: unsigned long melapsed = midiTime - tempoMIDITime; Chris@148: double quarters = double(melapsed) / double(td); Chris@148: double seconds = (60.0 * quarters) / tempo; Chris@148: Chris@148: /* Chris@690: SVDEBUG << "MIDIFileReader::getTimeForMIDITime(" << midiTime << ")" Chris@1429: << endl; Chris@690: SVDEBUG << "timing division = " << td << endl; Chris@1363: SVDEBUG << "nearest tempo event (of " << m_tempoMap.size() << ") is at " << tempoMIDITime << " (" Chris@1429: << tempoRealTime << ")" << endl; Chris@1363: SVDEBUG << "quarters since then = " << quarters << endl; Chris@1363: SVDEBUG << "tempo = " << tempo << " quarters per minute" << endl; Chris@1363: SVDEBUG << "seconds since then = " << seconds << endl; Chris@690: SVDEBUG << "resulting time = " << (tempoRealTime + RealTime::fromSeconds(seconds)) << endl; Chris@148: */ Chris@148: Chris@148: return tempoRealTime + RealTime::fromSeconds(seconds); Chris@148: } Chris@148: Chris@148: Model * Chris@148: MIDIFileReader::load() const Chris@148: { Chris@1582: if (!isOK()) return nullptr; Chris@148: Chris@148: if (m_loadableTracks.empty()) { Chris@392: if (m_acquirer) { Chris@392: m_acquirer->showError Chris@392: (tr("MIDI file \"%1\" has no notes in any track").arg(m_path)); Chris@392: } Chris@1582: return nullptr; Chris@148: } Chris@148: Chris@148: std::set tracksToLoad; Chris@148: Chris@148: if (m_loadableTracks.size() == 1) { Chris@148: Chris@1429: tracksToLoad.insert(*m_loadableTracks.begin()); Chris@148: Chris@148: } else { Chris@148: Chris@392: QStringList displayNames; Chris@148: Chris@1429: for (set::iterator i = m_loadableTracks.begin(); Chris@1429: i != m_loadableTracks.end(); ++i) { Chris@148: Chris@1429: unsigned int trackNo = *i; Chris@1429: QString label; Chris@148: Chris@1429: QString perc; Chris@1429: if (m_percussionTracks.find(trackNo) != m_percussionTracks.end()) { Chris@1429: perc = tr(" - uses GM percussion channel"); Chris@1429: } Chris@148: Chris@1429: if (m_trackNames.find(trackNo) != m_trackNames.end()) { Chris@1429: label = tr("Track %1 (%2)%3") Chris@1429: .arg(trackNo).arg(m_trackNames.find(trackNo)->second) Chris@1429: .arg(perc); Chris@1429: } else { Chris@1429: label = tr("Track %1 (untitled)%3").arg(trackNo).arg(perc); Chris@1429: } Chris@392: Chris@392: displayNames << label; Chris@1429: } Chris@148: Chris@392: QString singleTrack; Chris@148: Chris@392: bool haveSomePercussion = Chris@392: (!m_percussionTracks.empty() && Chris@392: (m_percussionTracks.size() < m_loadableTracks.size())); Chris@148: Chris@392: MIDIFileImportPreferenceAcquirer::TrackPreference pref; Chris@392: Chris@392: if (m_acquirer) { Chris@392: pref = m_acquirer->getTrackImportPreference(displayNames, Chris@392: haveSomePercussion, Chris@392: singleTrack); Chris@392: } else { Chris@392: pref = MIDIFileImportPreferenceAcquirer::MergeAllTracks; Chris@392: } Chris@392: Chris@1582: if (pref == MIDIFileImportPreferenceAcquirer::ImportNothing) return nullptr; Chris@392: Chris@392: if (pref == MIDIFileImportPreferenceAcquirer::MergeAllTracks || Chris@392: pref == MIDIFileImportPreferenceAcquirer::MergeAllNonPercussionTracks) { Chris@392: Chris@392: for (set::iterator i = m_loadableTracks.begin(); Chris@392: i != m_loadableTracks.end(); ++i) { Chris@392: Chris@1429: if (pref == MIDIFileImportPreferenceAcquirer::MergeAllTracks || Chris@1429: m_percussionTracks.find(*i) == m_percussionTracks.end()) { Chris@392: Chris@1429: tracksToLoad.insert(*i); Chris@1429: } Chris@1429: } Chris@148: Chris@1429: } else { Chris@1429: Chris@1429: int j = 0; Chris@148: Chris@1429: for (set::iterator i = m_loadableTracks.begin(); Chris@1429: i != m_loadableTracks.end(); ++i) { Chris@1429: Chris@1429: if (singleTrack == displayNames[j]) { Chris@1429: tracksToLoad.insert(*i); Chris@1429: break; Chris@1429: } Chris@1429: Chris@1429: ++j; Chris@1429: } Chris@1429: } Chris@148: } Chris@148: Chris@1582: if (tracksToLoad.empty()) return nullptr; Chris@148: Chris@1038: int n = int(tracksToLoad.size()), count = 0; Chris@1582: Model *model = nullptr; Chris@148: Chris@148: for (std::set::iterator i = tracksToLoad.begin(); Chris@1429: i != tracksToLoad.end(); ++i) { Chris@148: Chris@1429: int minProgress = (100 * count) / n; Chris@1429: int progressAmount = 100 / n; Chris@148: Chris@1429: model = loadTrack(*i, model, minProgress, progressAmount); Chris@148: Chris@1429: ++count; Chris@148: } Chris@148: Chris@148: if (dynamic_cast(model)) { Chris@1429: dynamic_cast(model)->setCompletion(100); Chris@148: } Chris@148: Chris@148: return model; Chris@148: } Chris@148: Chris@148: Model * Chris@148: MIDIFileReader::loadTrack(unsigned int trackToLoad, Chris@1429: Model *existingModel, Chris@1429: int minProgress, Chris@1429: int progressAmount) const Chris@148: { Chris@148: if (m_midiComposition.find(trackToLoad) == m_midiComposition.end()) { Chris@1582: return nullptr; Chris@148: } Chris@148: Chris@1582: NoteModel *model = nullptr; Chris@148: Chris@148: if (existingModel) { Chris@1429: model = dynamic_cast(existingModel); Chris@1429: if (!model) { Chris@1429: SVDEBUG << "WARNING: MIDIFileReader::loadTrack: Existing model given, but it isn't a NoteModel -- ignoring it" << endl; Chris@1429: } Chris@148: } Chris@148: Chris@148: if (!model) { Chris@1429: model = new NoteModel(m_mainModelSampleRate, 1, 0.0, 0.0, false); Chris@1429: model->setValueQuantization(1.0); Chris@1030: model->setObjectName(QFileInfo(m_path).fileName()); Chris@148: } Chris@148: Chris@148: const MIDITrack &track = m_midiComposition.find(trackToLoad)->second; Chris@148: Chris@1038: int totalEvents = int(track.size()); Chris@929: int count = 0; Chris@148: Chris@148: bool sharpKey = true; Chris@148: Chris@148: for (MIDITrack::const_iterator i = track.begin(); i != track.end(); ++i) { Chris@148: Chris@613: RealTime rt; Chris@613: unsigned long midiTime = (*i)->getTime(); Chris@613: Chris@613: if (m_smpte) { Chris@613: rt = RealTime::frame2RealTime(midiTime, m_fps * m_subframes); Chris@613: } else { Chris@613: rt = getTimeForMIDITime(midiTime); Chris@613: } Chris@148: Chris@1429: // We ignore most of these event types for now, though in Chris@1429: // theory some of the text ones could usefully be incorporated Chris@148: Chris@1429: if ((*i)->isMeta()) { Chris@148: Chris@1429: switch((*i)->getMetaEventCode()) { Chris@148: Chris@1429: case MIDI_KEY_SIGNATURE: Chris@1429: // minorKey = (int((*i)->getMetaMessage()[1]) != 0); Chris@1429: sharpKey = (int((*i)->getMetaMessage()[0]) >= 0); Chris@1429: break; Chris@148: Chris@1429: case MIDI_TEXT_EVENT: Chris@1429: case MIDI_LYRIC: Chris@1429: case MIDI_TEXT_MARKER: Chris@1429: case MIDI_COPYRIGHT_NOTICE: Chris@1429: case MIDI_TRACK_NAME: Chris@1429: // The text events that we could potentially use Chris@1429: break; Chris@148: Chris@1429: case MIDI_SET_TEMPO: Chris@1429: // Already dealt with in a separate pass previously Chris@1429: break; Chris@148: Chris@1429: case MIDI_TIME_SIGNATURE: Chris@1429: // Not yet! Chris@1429: break; Chris@148: Chris@1429: case MIDI_SEQUENCE_NUMBER: Chris@1429: case MIDI_CHANNEL_PREFIX_OR_PORT: Chris@1429: case MIDI_INSTRUMENT_NAME: Chris@1429: case MIDI_CUE_POINT: Chris@1429: case MIDI_CHANNEL_PREFIX: Chris@1429: case MIDI_SEQUENCER_SPECIFIC: Chris@1429: case MIDI_SMPTE_OFFSET: Chris@1429: default: Chris@1429: break; Chris@1429: } Chris@148: Chris@1429: } else { Chris@148: Chris@1429: switch ((*i)->getMessageType()) { Chris@148: Chris@1429: case MIDI_NOTE_ON: Chris@148: Chris@148: if ((*i)->getVelocity() == 0) break; // effective note-off Chris@1429: else { Chris@1429: RealTime endRT; Chris@613: unsigned long endMidiTime = (*i)->getTime() + (*i)->getDuration(); Chris@613: if (m_smpte) { Chris@613: endRT = RealTime::frame2RealTime(endMidiTime, m_fps * m_subframes); Chris@613: } else { Chris@613: endRT = getTimeForMIDITime(endMidiTime); Chris@613: } Chris@148: Chris@1429: long startFrame = RealTime::realTime2Frame Chris@1429: (rt, model->getSampleRate()); Chris@148: Chris@1429: long endFrame = RealTime::realTime2Frame Chris@1429: (endRT, model->getSampleRate()); Chris@148: Chris@1429: QString pitchLabel = Pitch::getPitchLabel((*i)->getPitch(), Chris@1429: 0, Chris@1429: !sharpKey); Chris@148: Chris@1429: QString noteLabel = tr("%1 - vel %2") Chris@1429: .arg(pitchLabel).arg(int((*i)->getVelocity())); Chris@148: Chris@340: float level = float((*i)->getVelocity()) / 128.f; Chris@340: Chris@1429: Note note(startFrame, (*i)->getPitch(), Chris@1429: endFrame - startFrame, level, noteLabel); Chris@148: Chris@1429: // SVDEBUG << "Adding note " << startFrame << "," << (endFrame-startFrame) << " : " << int((*i)->getPitch()) << endl; Chris@148: Chris@1429: model->addPoint(note); Chris@1429: break; Chris@1429: } Chris@148: Chris@148: case MIDI_PITCH_BEND: Chris@1429: // I guess we could make some use of this... Chris@148: break; Chris@148: Chris@148: case MIDI_NOTE_OFF: Chris@148: case MIDI_PROG_CHANGE: Chris@148: case MIDI_CTRL_CHANGE: Chris@148: case MIDI_SYSTEM_EXCLUSIVE: Chris@148: case MIDI_POLY_AFTERTOUCH: Chris@148: case MIDI_CHNL_AFTERTOUCH: Chris@148: break; Chris@148: Chris@148: default: Chris@148: break; Chris@148: } Chris@1429: } Chris@148: Chris@1429: model->setCompletion(minProgress + Chris@1429: (count * progressAmount) / totalEvents); Chris@1429: ++count; Chris@148: } Chris@148: Chris@148: return model; Chris@148: } Chris@148: Chris@148: