+void
+DiskReader::reset_tracker ()
+{
+ _midi_buf->reset_tracker ();
+
+ boost::shared_ptr<MidiPlaylist> mp (midi_playlist());
+
+ if (mp) {
+ mp->reset_note_trackers ();
+ }
+}
+
+void
+DiskReader::resolve_tracker (Evoral::EventSink<samplepos_t>& buffer, samplepos_t time)
+{
+ _midi_buf->resolve_tracker(buffer, time);
+
+ boost::shared_ptr<MidiPlaylist> mp (midi_playlist());
+
+ if (mp) {
+ mp->reset_note_trackers ();
+ }
+}
+
+/** Writes playback events from playback_sample for nframes to dst, translating time stamps
+ * so that an event at playback_sample has time = 0
+ */
+void
+DiskReader::get_midi_playback (MidiBuffer& dst, samplepos_t start_sample, samplepos_t end_sample, MonitorState ms, BufferSet& scratch_bufs, double speed, samplecnt_t disk_samples_to_consume)
+{
+ MidiBuffer* target;
+ samplepos_t nframes = end_sample - start_sample;
+
+ if ((ms & MonitoringInput) == 0) {
+ /* Route::process_output_buffers() clears the buffer as-needed */
+ target = &dst;
+ } else {
+ target = &scratch_bufs.get_midi (0);
+ }
+
+ if (ms & MonitoringDisk) {
+ /* disk data needed */
+
+ Location* loc = _loop_location;
+
+ DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose (
+ "%1 MDS pre-read read %8 offset = %9 @ %4..%5 from %2 write to %3, LOOPED ? %6 .. %7\n", _name,
+ _midi_buf->get_read_ptr(), _midi_buf->get_write_ptr(), start_sample, end_sample,
+ (loc ? loc->start() : -1), (loc ? loc->end() : -1), nframes, Port::port_offset()));
+
+ //cerr << "======== PRE ========\n";
+ //_midi_buf->dump (cerr);
+ //cerr << "----------------\n";
+
+ size_t events_read = 0;
+
+ if (loc) {
+ samplepos_t effective_start;
+
+ Evoral::Range<samplepos_t> loop_range (loc->start(), loc->end() - 1);
+ effective_start = loop_range.squish (start_sample);
+
+ DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose ("looped, effective start adjusted to %1\n", effective_start));
+
+ if (effective_start == loc->start()) {
+ /* We need to turn off notes that may extend
+ beyond the loop end.
+ */
+
+ _midi_buf->resolve_tracker (*target, 0);
+ }
+
+ /* for split-cycles we need to offset the events */
+
+ if (loc->end() >= effective_start && loc->end() < effective_start + nframes) {
+
+ /* end of loop is within the range we are reading, so
+ split the read in two, and lie about the location
+ for the 2nd read
+ */
+
+ samplecnt_t first, second;
+
+ first = loc->end() - effective_start;
+ second = nframes - first;
+
+ DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose ("loop read for eff %1 end %2: %3 and %4, cycle offset %5\n",
+ effective_start, loc->end(), first, second));
+
+ if (first) {
+ DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose ("loop read #1, from %1 for %2\n",
+ effective_start, first));
+ events_read = _midi_buf->read (*target, effective_start, first);
+ }
+
+ if (second) {
+ DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose ("loop read #2, from %1 for %2\n",
+ loc->start(), second));
+ events_read += _midi_buf->read (*target, loc->start(), second);
+ }
+
+ } else {
+ DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose ("loop read #3, adjusted start as %1 for %2\n",
+ effective_start, nframes));
+ events_read = _midi_buf->read (*target, effective_start, effective_start + nframes);
+ }
+ } else {
+ const size_t n_skipped = _midi_buf->skip_to (start_sample);
+ if (n_skipped > 0) {
+ warning << string_compose(_("MidiDiskstream %1: skipped %2 events, possible underflow"), id(), n_skipped) << endmsg;
+ }
+ DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose ("playback buffer read, from %1 to %2 (%3)", start_sample, end_sample, nframes));
+ events_read = _midi_buf->read (*target, start_sample, end_sample, Port::port_offset ());
+ }
+
+ DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose (
+ "%1 MDS events read %2 range %3 .. %4 rspace %5 wspace %6 r@%7 w@%8\n",
+ _name, events_read, playback_sample, playback_sample + nframes,
+ _midi_buf->read_space(), _midi_buf->write_space(),
+ _midi_buf->get_read_ptr(), _midi_buf->get_write_ptr()));
+ }
+
+ g_atomic_int_add (&_samples_read_from_ringbuffer, nframes);
+
+ /* vari-speed */
+ if (speed != 0.0 && fabsf (speed) != 1.0f) {
+ for (MidiBuffer::iterator i = target->begin(); i != target->end(); ++i) {
+ MidiBuffer::TimeType *tme = i.timeptr();
+ // XXX need to subtract port offsets before scaling
+ // also we must only scale events read from disk
+ // and not existing input data in the buffer.
+ *tme = (*tme) * nframes / disk_samples_to_consume;
+ }
+ }
+
+ if (ms & MonitoringInput) {
+ dst.merge_from (*target, nframes);
+ }
+
+#if 0
+ if (!target->empty ()) {
+ cerr << "======== MIDI OUT ========\n";
+ for (MidiBuffer::iterator i = target->begin(); i != target->end(); ++i) {
+ const Evoral::Event<MidiBuffer::TimeType> ev (*i, false);
+ cerr << "MIDI EVENT (from disk) @ " << ev.time();
+ for (size_t xx = 0; xx < ev.size(); ++xx) {
+ cerr << ' ' << hex << (int) ev.buffer()[xx];
+ }
+ cerr << dec << endl;
+ }
+ cerr << "----------------\n";
+ }
+#endif
+#if 0
+ cerr << "======== MIDI Disk Buffer ========\n";
+ _midi_buf->dump (cerr);
+ cerr << "----------------\n";
+#endif
+}
+
+/** @a start is set to the new sample position (TIME) read up to */
+int
+DiskReader::midi_read (samplepos_t& start, samplecnt_t dur, bool reversed)
+{
+ samplecnt_t this_read = 0;
+ samplepos_t loop_end = 0;
+ samplepos_t loop_start = 0;
+ samplecnt_t loop_length = 0;
+ Location* loc = _loop_location;
+ samplepos_t effective_start = start;
+ Evoral::Range<samplepos_t>* loop_range (0);
+
+ DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose ("MDS::midi_read @ %1 cnt %2\n", start, dur));
+
+ boost::shared_ptr<MidiTrack> mt = boost::dynamic_pointer_cast<MidiTrack>(_route);
+ MidiChannelFilter* filter = mt ? &mt->playback_filter() : 0;
+ sampleoffset_t loop_offset = 0;
+
+ if (!reversed && loc) {
+ get_location_times (loc, &loop_start, &loop_end, &loop_length);
+ }
+
+ while (dur) {
+
+ /* take any loop into account. we can't read past the end of the loop. */
+
+ if (loc && !reversed) {
+
+ if (!loop_range) {
+ loop_range = new Evoral::Range<samplepos_t> (loop_start, loop_end-1); // inclusive semantics require -1
+ }
+
+ /* if we are (seamlessly) looping, ensure that the first sample we read is at the correct
+ position within the loop.
+ */
+
+ effective_start = loop_range->squish (effective_start);
+
+ if ((loop_end - effective_start) <= dur) {
+ /* too close to end of loop to read "dur", so
+ shorten it.
+ */
+ this_read = loop_end - effective_start;
+ } else {
+ this_read = dur;
+ }
+
+ } else {
+ this_read = dur;
+ }
+
+ if (this_read == 0) {
+ break;
+ }
+
+ this_read = min (dur,this_read);
+
+ DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose ("MDS ::read at %1 for %2 loffset %3\n", effective_start, this_read, loop_offset));
+
+ if (midi_playlist()->read (*_midi_buf, effective_start, this_read, loop_range, 0, filter) != this_read) {
+ error << string_compose(
+ _("MidiDiskstream %1: cannot read %2 from playlist at sample %3"),
+ id(), this_read, start) << endmsg;
+ return -1;
+ }
+
+ g_atomic_int_add (&_samples_written_to_ringbuffer, this_read);
+
+ if (reversed) {
+
+ // Swap note ons with note offs here. etc?
+ // Fully reversing MIDI requires look-ahead (well, behind) to find previous
+ // CC values etc. hard.
+
+ } else {
+
+ /* adjust passed-by-reference argument (note: this is
+ monotonic and does not reflect looping.
+ */
+ start += this_read;
+
+ /* similarly adjust effective_start, but this may be
+ readjusted for seamless looping as we continue around
+ the loop.
+ */
+ effective_start += this_read;
+ }
+
+ dur -= this_read;
+ }
+
+ return 0;
+}
+
+int
+DiskReader::refill_midi ()
+{
+ if (!_playlists[DataType::MIDI]) {
+ return 0;
+ }
+
+ const size_t write_space = _midi_buf->write_space();
+ const bool reversed = _session.transport_speed() < 0.0f;
+
+ DEBUG_TRACE (DEBUG::DiskIO, string_compose ("MIDI refill, write space = %1 file sample = %2\n", write_space, file_sample[DataType::MIDI]));
+
+ /* no space to write */
+ if (write_space == 0) {
+ return 0;
+ }
+
+ if (reversed) {
+ return 0;
+ }
+
+ /* at end: nothing to do */
+
+ samplepos_t ffm = file_sample[DataType::MIDI];
+
+ if (ffm == max_samplepos) {
+ return 0;
+ }
+
+ int ret = 0;
+ const uint32_t samples_read = g_atomic_int_get (&_samples_read_from_ringbuffer);
+ const uint32_t samples_written = g_atomic_int_get (&_samples_written_to_ringbuffer);
+
+ if ((samples_read < samples_written) && (samples_written - samples_read) >= midi_readahead) {
+ return 0;
+ }
+
+ samplecnt_t to_read = midi_readahead - ((samplecnt_t)samples_written - (samplecnt_t)samples_read);
+
+ to_read = min (to_read, (samplecnt_t) (max_samplepos - ffm));
+ to_read = min (to_read, (samplecnt_t) write_space);
+
+ if (midi_read (ffm, to_read, reversed)) {
+ ret = -1;
+ }
+
+ file_sample[DataType::MIDI] = ffm;
+
+ return ret;
+}
+
+void
+DiskReader::set_no_disk_output (bool yn)
+{
+ /* this MUST be called as part of the process call tree, before any
+ disk readers are invoked. We use it when the session needs the
+ transport (and thus effective read position for DiskReaders) to keep
+ advancing as part of syncing up with a transport master, but we
+ don't want any actual disk output yet because we are still not
+ synced.
+ */
+ _no_disk_output = yn;
+}