- assert(_end_iter._is_end);
- assert( ! _end_iter._locked);
-}
-
-/** Read events in frame range \a start .. \a start+cnt into \a dst,
- * adding \a stamp_offset to each event's timestamp.
- * \return number of events written to \a dst
- */
-size_t
-MidiModel::read(MidiRingBuffer& dst, nframes_t start, nframes_t nframes, nframes_t stamp_offset, nframes_t negative_stamp_offset) const
-{
- cerr << this << " MM::read @ " << start << " frames: " << nframes << " -> " << stamp_offset << endl;
- cerr << this << " MM # notes: " << n_notes() << endl;
-
- size_t read_events = 0;
-
- if (start != _next_read) {
- _read_iter = const_iterator(*this, (double)start);
- cerr << "Repositioning iterator from " << _next_read << " to " << start << endl;
- } else {
- cerr << "Using cached iterator at " << _next_read << endl;
- }
-
- _next_read = start + nframes;
-
- while (_read_iter != end() && _read_iter->time() < start + nframes) {
- assert(_read_iter->size() > 0);
- dst.write(_read_iter->time() + stamp_offset - negative_stamp_offset, _read_iter->size(), _read_iter->buffer());
- cerr << this << " MM::read event @ " << _read_iter->time()
- << " type: " << hex << int(_read_iter->type()) << dec
- << " note: " << int(_read_iter->note())
- << " velocity: " << int(_read_iter->velocity())
- << endl;
- ++_read_iter;
- ++read_events;
- }
-
- return read_events;
-}
-
-
-bool
-MidiModel::control_to_midi_event(MIDI::Event& ev, const MidiControlIterator& iter) const
-{
- if (iter.first->parameter().type() == MidiCCAutomation) {
- if (ev.size() < 3)
- ev.set_buffer((Byte*)malloc(3), true);
-
- assert(iter.first);
- assert(iter.first->parameter().channel() < 16);
- assert(iter.first->parameter().id() <= INT8_MAX);
- assert(iter.second.second <= INT8_MAX);
- ev.buffer()[0] = MIDI_CMD_CONTROL + iter.first->parameter().channel();
- ev.buffer()[1] = (Byte)iter.first->parameter().id();
- ev.buffer()[2] = (Byte)iter.second.second;
- ev.time() = iter.second.first; // x
- ev.size() = 3;
- return true;
- } else {
- return false;
- }
-}
-
-
-/** Begin a write of events to the model.
- *
- * If \a mode is Sustained, complete notes with duration are constructed as note
- * on/off events are received. Otherwise (Percussive), only note on events are
- * stored; note off events are discarded entirely and all contained notes will
- * have duration 0.
- */
-void
-MidiModel::start_write()
-{
- //cerr << "MM " << this << " START WRITE, MODE = " << enum_2_string(_note_mode) << endl;
- write_lock();
- _writing = true;
- for (int i = 0; i < 16; ++i)
- _write_notes[i].clear();
- write_unlock();
-}
-
-
-
-/** Finish a write of events to the model.
- *
- * If \a delete_stuck is true and the current mode is Sustained, note on events
- * that were never resolved with a corresonding note off will be deleted.
- * Otherwise they will remain as notes with duration 0.
- */
-void
-MidiModel::end_write(bool delete_stuck)
-{
- write_lock();
- assert(_writing);
-
- //cerr << "MM " << this << " END WRITE: " << _notes.size() << " NOTES\n";
-
- if (_note_mode == Sustained && delete_stuck) {
- for (Notes::iterator n = _notes.begin(); n != _notes.end() ; ) {
- if ((*n)->duration() == 0) {
- cerr << "WARNING: Stuck note lost: " << (*n)->note() << endl;
- n = _notes.erase(n);
- } else {
- ++n;
- }
- }
- }
-
- for (int i = 0; i < 16; ++i) {
- if (!_write_notes[i].empty()) {
- cerr << "WARNING: MidiModel::end_write: Channel " << i << " has "
- << _write_notes[i].size() << " stuck notes" << endl;
- }
- _write_notes[i].clear();
- }
-
- _writing = false;
- write_unlock();
-}
-
-
-/** Append \a in_event to model. NOT realtime safe.
- *
- * Timestamps of events in \a buf are expected to be relative to
- * the start of this model (t=0) and MUST be monotonically increasing
- * and MUST be >= the latest event currently in the model.
- */
-void
-MidiModel::append(const MIDI::Event& ev)
-{
- write_lock();
-
- assert(_notes.empty() || ev.time() >= _notes.back()->time());
- assert(_writing);
-
- if (ev.is_note_on())
- append_note_on_unlocked(ev.channel(), ev.time(), ev.note(), ev.velocity());
- else if (ev.is_note_off())
- append_note_off_unlocked(ev.channel(), ev.time(), ev.note());
- else if (ev.is_cc())
- append_cc_unlocked(ev.channel(), ev.time(), ev.cc_number(), ev.cc_value());
- else
- printf("MM Unknown event type %X\n", ev.type());
-
- write_unlock();
-}
-
-
-void
-MidiModel::append_note_on_unlocked(uint8_t chan, double time, uint8_t note_num, uint8_t velocity)
-{
- /*cerr << "MidiModel " << this << " chan " << (int)chan <<
- " note " << (int)note_num << " on @ " << time << endl;*/
-
- assert(chan < 16);
- assert(_writing);
-
- _notes.push_back(boost::shared_ptr<Note>(new Note(chan, time, 0, note_num, velocity)));
- if (_note_mode == Sustained) {
- //cerr << "MM Sustained: Appending active note on " << (unsigned)(uint8_t)note_num << endl;
- _write_notes[chan].push_back(_notes.size() - 1);
- }/* else {
- cerr << "MM Percussive: NOT appending active note on" << endl;
- }*/
-}
-
-
-void
-MidiModel::append_note_off_unlocked(uint8_t chan, double time, uint8_t note_num)
-{
- /*cerr << "MidiModel " << this << " chan " << (int)chan <<
- " note " << (int)note_num << " off @ " << time << endl;*/
-
- assert(chan < 16);
- assert(_writing);
-
- if (_note_mode == Percussive) {
- cerr << "MidiModel Ignoring note off (percussive mode)" << endl;
- return;
- }
-
- /* FIXME: make _write_notes fixed size (127 noted) for speed */
-
- /* FIXME: note off velocity for that one guy out there who actually has
- * keys that send it */
-
- bool resolved = false;
-
- for (WriteNotes::iterator n = _write_notes[chan].begin(); n != _write_notes[chan].end(); ++n) {
- Note& note = *_notes[*n].get();
- //cerr << (unsigned)(uint8_t)note.note() << " ? " << (unsigned)note_num << endl;
- if (note.note() == note_num) {
- assert(time >= note.time());
- note.set_duration(time - note.time());
- _write_notes[chan].erase(n);
- //cerr << "MM resolved note, duration: " << note.duration() << endl;
- resolved = true;
- break;
- }
- }
-
- if (!resolved)
- cerr << "MidiModel " << this << " spurious note off chan " << (int)chan
- << ", note " << (int)note_num << " @ " << time << endl;
-}
-
-
-void
-MidiModel::append_cc_unlocked(uint8_t chan, double time, uint8_t number, uint8_t value)
-{
- /*cerr << "MidiModel " << this << " chan " << (int)chan <<
- " CC " << (int)number << " = " << (int)value << " @ " << time << endl;*/
-
- assert(chan < 16);
- assert(_writing);
-
- Parameter param(MidiCCAutomation, number, chan);
-
- boost::shared_ptr<AutomationControl> control = Automatable::control(param, true);
- control->list()->fast_simple_add(time, (double)value);
-}
-
-
-void
-MidiModel::add_note_unlocked(const boost::shared_ptr<Note> note)
-{
- //cerr << "MidiModel " << this << " add note " << (int)note.note() << " @ " << note.time() << endl;
- Notes::iterator i = upper_bound(_notes.begin(), _notes.end(), note, note_time_comparator);
- _notes.insert(i, note);
-}
-
-
-void
-MidiModel::remove_note_unlocked(const boost::shared_ptr<const Note> note)
-{
- //cerr << "MidiModel " << this << " remove note " << (int)note.note() << " @ " << note.time() << endl;
- for(Notes::iterator n = _notes.begin(); n != _notes.end(); ++n) {
- if(**n == *note) {
- _notes.erase(n);
- }
- }
-
-}
-
-/** Slow! for debugging only. */
-#ifndef NDEBUG
-bool
-MidiModel::is_sorted() const
-{
- bool t = 0;
- for (Notes::const_iterator n = _notes.begin(); n != _notes.end(); ++n)
- if ((*n)->time() < t)
- return false;
- else
- t = (*n)->time();
-
- return true;