void MidiTrack::closeTrack() { long lastTick = 0; if (mEvents.size() > 0) { MidiEvent * last = *(--mEvents.end()); lastTick = last->getTick() + 1; } insertEvent(new EndOfTrack(lastTick, 0)); }
long MidiTrack::getLengthInTicks() { if (mEvents.size() == 0) { return 0; } // get the last note MidiEvent * E = *(--mEvents.end()); return E->getTick(); }
void MidiTrack::insertEvent(MidiEvent * newEvent) { if (newEvent == NULL) { return; } if (mClosed) { cerr << "Error: Cannot add an event to a closed track."; return; } MidiEvent * prev = NULL, *next = NULL; std::vector<MidiEvent*>::iterator it; for (it = mEvents.begin(); it != mEvents.end(); it++) { next = *it; if (next->getTick() > newEvent->getTick()) { break; } prev = next; next = NULL; } mEvents.insert(it, newEvent); mSizeNeedsRecalculating = true; // Set its delta time based on the previous event (or itself if no previous event exists) if (prev != NULL) { newEvent->setDelta(newEvent->getTick() - prev->getTick()); } else { newEvent->setDelta(newEvent->getTick()); } // Update the next event's delta time relative to the new event. if (next != NULL) { next->setDelta(next->getTick() - newEvent->getTick()); } mSize += newEvent->getSize(); if (newEvent->getType() == MetaEvent::END_OF_TRACK) { if (next != NULL) { cerr << "Attempting to insert EndOfTrack before an existing event. Use closeTrack() when finished with MidiTrack."; return; } mClosed = true; } }
bool MidiTrack::removeEvent(MidiEvent * E) { bool isRemoved = false; MidiEvent * prev = NULL, *curr = NULL, *next = NULL; std::vector<MidiEvent*>::iterator it; for (it = mEvents.begin(); it != mEvents.end(); it++) { next = *it; if (E == curr) { mEvents.erase(--it); isRemoved = true; delete curr; break; } prev = curr; curr = next; next = NULL; } if (next == NULL) { // Either the event was not found in the track, // or this is the last event in the track. // Either way, we won't need to update any delta times return isRemoved; } if (!isRemoved) { return false; } if (prev != NULL) { next->setDelta(next->getTick() - prev->getTick()); } else { next->setDelta(next->getTick()); } return true; }