- LaterNoteEndComparator cmp;
- ActiveNotes active_notes(cmp);
-
- EventTimeComparator comp;
- typedef std::priority_queue<
- const MIDI::Event*,
- std::deque<const MIDI::Event*>,
- EventTimeComparator> MidiEvents;
-
- MidiEvents events(comp);
-
- /* Why sort manually, when a priority queue does the job for us,
- * (I am probably wrong here, but I needed that to test program
- * change code quickly) ???
- * */
- // Foreach note
- for (Notes::const_iterator n = _notes.begin(); n != _notes.end(); ++n) {
-
- // Write any pending note offs earlier than this note on
- while ( !active_notes.empty() ) {
- const boost::shared_ptr<const Note> earliest_off =
- active_notes.top();
- const MIDI::Event& off_ev = earliest_off->off_event();
- if (off_ev.time() <= (*n)->time()) {
- events.push(&off_ev);
- active_notes.pop();
- } else {
- break;
- }
- }
-
- // Write this note on
- events.push(&(*n)->on_event());
- if ((*n)->duration() > 0)
- active_notes.push(*n);
- }
-
- // Write any trailing note offs
- while ( !active_notes.empty() ) {
- events.push(&active_notes.top()->off_event());
- active_notes.pop();
- }
-
- while (!events.empty()) {
- source->append_event_unlocked(Frames, *events.top());
- //cerr << "MidiModel::write_to appending event with time:" << dec << int(events.top()->time()) << hex << " buffer: 0x" << int(events.top()->buffer()[0]) << " 0x" << int(events.top()->buffer()[1]) << " 0x" << int(events.top()->buffer()[2]) << endl;
- events.pop();
- }
-
- _edited = false;