-#ifndef NDEBUG
- DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose (
- "%1 MDS pre-read read %4..%5 from %2 write to %3\n", _name,
- _playback_buf->get_read_ptr(), _playback_buf->get_write_ptr(), start, end));
-// cerr << "================\n";
-// _playback_buf->dump (cerr);
-// cerr << "----------------\n";
+ if (loc) {
+ framepos_t effective_start;
+
+ if (playback_sample >= loc->end()) {
+ effective_start = loc->start() + ((playback_sample - loc->end()) % loc->length());
+ } else {
+ effective_start = playback_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.
+ */
+
+ _playback_buf->resolve_tracker (dst, split_cycle_offset);
+ }
+
+ _playback_buf->skip_to (effective_start);
+
+ /* 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
+ */
+ framecnt_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\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 = _playback_buf->read (dst, effective_start, first, split_cycle_offset);
+ }
+
+ if (second) {
+ DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose ("loop read #2, from %1 for %2\n",
+ loc->start(), second));
+ events_read += _playback_buf->read (dst, loc->start(), second, split_cycle_offset);
+ }
+
+ } else {
+ DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose ("loop read #3, adjusted start as %1 for %2\n",
+ effective_start, nframes));
+ events_read = _playback_buf->read (dst, effective_start, effective_start + nframes, split_cycle_offset);
+ }
+ } else {
+ _playback_buf->skip_to (playback_sample);
+ events_read = _playback_buf->read (dst, playback_sample, playback_sample + nframes, split_cycle_offset);
+ }