Load audition synth on demand
[ardour.git] / libs / ardour / disk_reader.cc
index 8f53cf805d1c0bc0b08c9bcded0192c21cdbf007..4c62b03de25212ffa2cc0b65de9bb2115ec6d585 100644 (file)
@@ -17,8 +17,9 @@
 
 */
 
+#include <boost/smart_ptr/scoped_array.hpp>
+
 #include "pbd/enumwriter.h"
-#include "pbd/i18n.h"
 #include "pbd/memento_command.h"
 
 #include "ardour/audioengine.h"
@@ -36,6 +37,8 @@
 #include "ardour/session.h"
 #include "ardour/session_playlists.h"
 
+#include "pbd/i18n.h"
+
 using namespace ARDOUR;
 using namespace PBD;
 using namespace std;
@@ -49,12 +52,11 @@ bool DiskReader::_no_disk_output = false;
 
 DiskReader::DiskReader (Session& s, string const & str, DiskIOProcessor::Flag f)
        : DiskIOProcessor (s, str, f)
-       , _roll_delay (0)
        , overwrite_sample (0)
-        , overwrite_offset (0)
-        , _pending_overwrite (false)
-        , overwrite_queued (false)
-       , _gui_feed_buffer (AudioEngine::instance()->raw_buffer_size (DataType::MIDI))
+       , overwrite_offset (0)
+       , _pending_overwrite (false)
+       , overwrite_queued (false)
+       , _declick_gain (0)
 {
        file_sample[DataType::AUDIO] = 0;
        file_sample[DataType::MIDI] = 0;
@@ -69,21 +71,30 @@ DiskReader::~DiskReader ()
                        _playlists[n]->release ();
                }
        }
+       delete _midi_buf;
+}
 
-       {
-               RCUWriter<ChannelList> writer (channels);
-               boost::shared_ptr<ChannelList> c = writer.get_copy();
-
-               for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
-                       delete *chan;
-               }
+void
+DiskReader::ReaderChannelInfo::resize (samplecnt_t bufsize)
+{
+       delete rbuf;
+       /* touch memory to lock it */
+       rbuf = new RingBufferNPT<Sample> (bufsize);
+       memset (rbuf->buffer(), 0, sizeof (Sample) * rbuf->bufsize());
+}
 
-               c->clear();
+int
+DiskReader::add_channel_to (boost::shared_ptr<ChannelList> c, uint32_t how_many)
+{
+       while (how_many--) {
+               c->push_back (new ReaderChannelInfo (_session.butler()->audio_diskstream_playback_buffer_size()));
+               DEBUG_TRACE (DEBUG::DiskIO, string_compose ("%1: new reader channel, write space = %2 read = %3\n",
+                                                           name(),
+                                                           c->back()->rbuf->write_space(),
+                                                           c->back()->rbuf->read_space()));
        }
 
-       channels.flush ();
-
-       delete _midi_buf;
+       return 0;
 }
 
 void
@@ -126,17 +137,10 @@ DiskReader::set_name (string const & str)
        return true;
 }
 
-void
-DiskReader::set_roll_delay (ARDOUR::samplecnt_t nframes)
-{
-       /* Must be called from process context or with process lock held */
-       _roll_delay = nframes;
-}
-
 XMLNode&
-DiskReader::state (bool full)
+DiskReader::state ()
 {
-       XMLNode& node (DiskIOProcessor::state (full));
+       XMLNode& node (DiskIOProcessor::state ());
        node.set_property(X_("type"), X_("diskreader"));
        return node;
 }
@@ -154,7 +158,6 @@ DiskReader::set_state (const XMLNode& node, int version)
 void
 DiskReader::realtime_handle_transport_stopped ()
 {
-       realtime_speed_change ();
 }
 
 void
@@ -182,7 +185,7 @@ DiskReader::buffer_load () const
                return 1.0;
        }
 
-       PBD::RingBufferNPT<Sample> * b = c->front()->buf;
+       PBD::RingBufferNPT<Sample>* b = c->front()->rbuf;
        return (float) ((double) b->read_space() / (double) b->bufsize());
 }
 
@@ -260,65 +263,44 @@ DiskReader::run (BufferSet& bufs, samplepos_t start_sample, samplepos_t end_samp
                }
        }
 
-       if (speed == 0.0 && (ms == MonitoringDisk)) {
-               /* stopped. Don't accidentally pass any data from disk
-                * into our outputs (e.g. via interpolation)
+       if ((speed == 0.0) && (ms == MonitoringDisk)) {
+               /* no channels, or stopped. Don't accidentally pass any data
+                * from disk into our outputs (e.g. via interpolation)
                 */
-               bufs.silence (nframes, 0);
                return;
        }
 
-       if (speed != 1.0f && speed != -1.0f) {
-               interpolation.set_speed (speed);
-               midi_interpolation.set_speed (speed);
-               disk_samples_to_consume = midi_interpolation.distance (nframes);
-               if (speed < 0.0) {
-                       disk_samples_to_consume = -disk_samples_to_consume;
-               }
-       } else {
-               disk_samples_to_consume = nframes;
-       }
+       BufferSet& scratch_bufs (_session.get_scratch_buffers (bufs.count()));
+       const bool still_locating = _session.global_locate_pending();
 
-       bool roll_delayed = false;
-       samplecnt_t roll_delay_offset = 0;
+       if (c->empty()) {
+               /* do nothing with audio */
+               goto midi;
+       }
 
-       if (speed != 0.0) {
-               if (_roll_delay > disk_samples_to_consume) {
-                       /* still waiting for _roll_delay to end */
-                       _roll_delay -= disk_samples_to_consume;
-                       /* we could set disk_samples_to_consume to zero here, but it
-                          won't be used anyway.
-                       */
-                       roll_delayed = true;
+       assert (speed == -1 || speed == 0 || speed == 1);
 
-               } else if (_roll_delay > 0) {
-                       /* roll delay will end during this call to ::run(), but
-                        * there's some silence needed in the signal-from-disk first
-                        */
-                       roll_delay_offset = _roll_delay;
-                       bufs.silence (_roll_delay, 0);
-                       disk_samples_to_consume -= _roll_delay;
-                       start_sample += _roll_delay;
-                       _roll_delay = 0;
-               }
+       if (speed < 0) {
+               disk_samples_to_consume = -nframes;
+       } else if (speed > 0) {
+               disk_samples_to_consume = nframes;
+       } else {
+               disk_samples_to_consume = 0;
        }
 
-       BufferSet& scratch_bufs (_session.get_scratch_buffers (bufs.count()));
-       const bool still_locating = _session.global_locate_pending();
-
-       if (!result_required || ((ms & MonitoringDisk) == 0) || still_locating || _no_disk_output || roll_delayed) {
+       if (!result_required || ((ms & MonitoringDisk) == 0) || still_locating || _no_disk_output) {
 
                /* no need for actual disk data, just advance read pointer and return */
 
-               if (!roll_delayed && (!still_locating || _no_disk_output)) {
+               if (!still_locating || _no_disk_output) {
                        for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
-                               (*chan)->buf->increment_read_ptr (disk_samples_to_consume);
+                               (*chan)->rbuf->increment_read_ptr (disk_samples_to_consume);
                        }
                }
 
                /* if monitoring disk but locating put silence in the buffers */
 
-               if ((roll_delayed || _no_disk_output || still_locating) && (ms == MonitoringDisk)) {
+               if ((_no_disk_output || still_locating) && (ms == MonitoringDisk)) {
                        bufs.silence (nframes, 0);
                }
 
@@ -350,22 +332,22 @@ DiskReader::run (BufferSet& bufs, samplepos_t start_sample, samplepos_t end_samp
                                disk_signal = output.data ();
                        }
 
-                       /* if we skipped some number of samples at the start
-                          because of the _roll_delay being non-zero but small
-                          enough that we will process some data from disk,
-                          advance where we're going to write that data to,
-                          thus skipping over the silence that was written
-                          there.
-                       */
-                       disk_signal += roll_delay_offset;
-
-                       if (start_sample < playback_sample) {
-                               cerr << owner()->name() << " SS = " << start_sample << " PS = " << playback_sample << endl;
-                               abort ();
+                       if (speed > 0) {
+                               if (start_sample < playback_sample) {
+                                       cerr << owner()->name() << " SS = " << start_sample << " PS = " << playback_sample << endl;
+                                       abort ();
+                               }
+                       } else if (speed < 0) {
+                               if (playback_sample < start_sample) {
+                                       cerr << owner()->name() << " SS = " << start_sample << " PS = " << playback_sample << " REVERSE" << endl;
+                                       abort ();
+                               }
                        }
 
-                       if (start_sample != playback_sample) {
-                               cerr << owner()->name() << " playback not aligned, jump ahead " << (start_sample - playback_sample) << endl;
+                       if ((speed > 0) && (start_sample != playback_sample)) {
+                               stringstream str;
+                               str << owner()->name() << " playback @ " << start_sample << " not aligned with " << playback_sample << " jump ahead " << (start_sample - playback_sample) << endl;
+                               cerr << str.str();
 
                                if (can_internal_playback_seek (start_sample - playback_sample)) {
                                        internal_playback_seek (start_sample - playback_sample);
@@ -375,16 +357,11 @@ DiskReader::run (BufferSet& bufs, samplepos_t start_sample, samplepos_t end_samp
                                }
                        }
 
-                       chaninfo->buf->get_read_vector (&(*chan)->rw_vector);
+                       chaninfo->rbuf->get_read_vector (&(*chan)->rw_vector);
 
                        if (disk_samples_to_consume <= (samplecnt_t) chaninfo->rw_vector.len[0]) {
 
-                               if (fabsf (speed) != 1.0f) {
-                                       (void) interpolation.interpolate (
-                                               n, disk_samples_to_consume,
-                                               chaninfo->rw_vector.buf[0],
-                                               disk_signal);
-                               } else if (speed != 0.0) {
+                               if (speed != 0.0) {
                                        memcpy (disk_signal, chaninfo->rw_vector.buf[0], sizeof (Sample) * disk_samples_to_consume);
                                }
 
@@ -394,22 +371,11 @@ DiskReader::run (BufferSet& bufs, samplepos_t start_sample, samplepos_t end_samp
 
                                if (disk_samples_to_consume <= total) {
 
-                                       /* We have enough samples, but not in one lump.
-                                        */
-
-                                       if (fabsf (speed) != 1.0f) {
-                                               interpolation.interpolate (n, chaninfo->rw_vector.len[0],
-                                                                          chaninfo->rw_vector.buf[0],
-                                                                          disk_signal);
-                                               disk_signal += chaninfo->rw_vector.len[0];
-                                               interpolation.interpolate (n, disk_samples_to_consume - chaninfo->rw_vector.len[0],
-                                                                          chaninfo->rw_vector.buf[1],
-                                                                          disk_signal);
-                                       } else if (speed != 0.0) {
-                                               memcpy (disk_signal,
+                                               if (speed != 0.0) {
+                                                       memcpy (disk_signal,
                                                        chaninfo->rw_vector.buf[0],
                                                        chaninfo->rw_vector.len[0] * sizeof (Sample));
-                                               memcpy (disk_signal + chaninfo->rw_vector.len[0],
+                                                       memcpy (disk_signal + chaninfo->rw_vector.len[0],
                                                        chaninfo->rw_vector.buf[1],
                                                        (disk_samples_to_consume - chaninfo->rw_vector.len[0]) * sizeof (Sample));
                                        }
@@ -430,9 +396,7 @@ DiskReader::run (BufferSet& bufs, samplepos_t start_sample, samplepos_t end_samp
                                apply_gain_to_buffer (disk_signal, disk_samples_to_consume, scaling);
                        }
 
-                       chaninfo->buf->increment_read_ptr (disk_samples_to_consume);
-
-                 monitor_mix:
+                       chaninfo->rbuf->increment_read_ptr (disk_samples_to_consume);
 
                        if (ms & MonitoringInput) {
                                /* mix the disk signal into the input signal (already in bufs) */
@@ -444,7 +408,7 @@ DiskReader::run (BufferSet& bufs, samplepos_t start_sample, samplepos_t end_samp
        /* MIDI data handling */
 
   midi:
-       if (!_session.declick_out_pending() && bufs.count().n_midi()) {
+       if (/*!_session.declick_out_pending() && */ bufs.count().n_midi()) {
                MidiBuffer* dst;
 
                if (_no_disk_output) {
@@ -454,11 +418,11 @@ DiskReader::run (BufferSet& bufs, samplepos_t start_sample, samplepos_t end_samp
                }
 
                if ((ms & MonitoringDisk) && !still_locating) {
-                       get_midi_playback (*dst, disk_samples_to_consume, ms, scratch_bufs, speed, disk_samples_to_consume);
+                       get_midi_playback (*dst, start_sample, end_sample, ms, scratch_bufs, speed, disk_samples_to_consume);
                }
        }
 
-       if (!still_locating && !roll_delayed) {
+       if (!still_locating) {
 
                bool butler_required = false;
 
@@ -471,14 +435,13 @@ DiskReader::run (BufferSet& bufs, samplepos_t start_sample, samplepos_t end_samp
                if (_playlists[DataType::AUDIO]) {
                        if (!c->empty()) {
                                if (_slaved) {
-                                       if (c->front()->buf->write_space() >= c->front()->buf->bufsize() / 2) {
-                                               DEBUG_TRACE (DEBUG::Butler, string_compose ("%1: slaved, write space = %2 of %3\n", name(), c->front()->buf->write_space(),
-                                                                                           c->front()->buf->bufsize()));
+                                       if (c->front()->rbuf->write_space() >= c->front()->rbuf->bufsize() / 2) {
+                                               DEBUG_TRACE (DEBUG::Butler, string_compose ("%1: slaved, write space = %2 of %3\n", name(), c->front()->rbuf->write_space(), c->front()->rbuf->bufsize()));
                                                butler_required = true;
                                        }
                                } else {
-                                       if ((samplecnt_t) c->front()->buf->write_space() >= _chunk_samples) {
-                                               DEBUG_TRACE (DEBUG::Butler, string_compose ("%1: write space = %2 of %3\n", name(), c->front()->buf->write_space(),
+                                       if ((samplecnt_t) c->front()->rbuf->write_space() >= _chunk_samples) {
+                                               DEBUG_TRACE (DEBUG::Butler, string_compose ("%1: write space = %2 of %3\n", name(), c->front()->rbuf->write_space(),
                                                                                            _chunk_samples));
                                                butler_required = true;
                                        }
@@ -553,7 +516,7 @@ DiskReader::set_pending_overwrite (bool yn)
 
        boost::shared_ptr<ChannelList> c = channels.reader ();
        if (!c->empty ()) {
-               overwrite_offset = c->front()->buf->get_read_ptr();
+               overwrite_offset = c->front()->rbuf->get_read_ptr();
        }
 }
 
@@ -575,10 +538,10 @@ DiskReader::overwrite_existing_buffers ()
                const bool reversed = _session.transport_speed() < 0.0f;
 
                /* assume all are the same size */
-               samplecnt_t size = c->front()->buf->bufsize();
+               samplecnt_t size = c->front()->rbuf->bufsize();
 
-               std::auto_ptr<Sample> mixdown_buffer (new Sample[size]);
-               std::auto_ptr<float> gain_buffer (new float[size]);
+               boost::scoped_array<Sample> mixdown_buffer (new Sample[size]);
+               boost::scoped_array<float> gain_buffer (new float[size]);
 
                /* reduce size so that we can fill the buffer correctly (ringbuffers
                   can only handle size-1, otherwise they appear to be empty)
@@ -606,7 +569,7 @@ DiskReader::overwrite_existing_buffers ()
 
                        samplecnt_t to_read = size - overwrite_offset;
 
-                       if (audio_read ((*chan)->buf->buffer() + overwrite_offset, mixdown_buffer.get(), gain_buffer.get(), start, to_read, n, reversed)) {
+                       if (audio_read ((*chan)->rbuf->buffer() + overwrite_offset, mixdown_buffer.get(), gain_buffer.get(), start, to_read, n, reversed)) {
                                error << string_compose(_("DiskReader %1: when refilling, cannot read %2 from playlist at sample %3"),
                                                        id(), size, playback_sample) << endmsg;
                                goto midi;
@@ -616,7 +579,7 @@ DiskReader::overwrite_existing_buffers ()
 
                                cnt -= to_read;
 
-                               if (audio_read ((*chan)->buf->buffer(), mixdown_buffer.get(), gain_buffer.get(), start, cnt, n, reversed)) {
+                               if (audio_read ((*chan)->rbuf->buffer(), mixdown_buffer.get(), gain_buffer.get(), start, cnt, n, reversed)) {
                                        error << string_compose(_("DiskReader %1: when refilling, cannot read %2 from playlist at sample %3"),
                                                                id(), size, playback_sample) << endmsg;
                                        goto midi;
@@ -665,8 +628,10 @@ DiskReader::seek (samplepos_t sample, bool complete_refill)
        ChannelList::iterator chan;
        boost::shared_ptr<ChannelList> c = channels.reader();
 
+       //sample = std::max ((samplecnt_t)0, sample -_session.worst_output_latency ());
+
        for (n = 0, chan = c->begin(); chan != c->end(); ++chan, ++n) {
-               (*chan)->buf->reset ();
+               (*chan)->rbuf->reset ();
        }
 
        if (g_atomic_int_get (&_samples_read_from_ringbuffer) == 0) {
@@ -710,7 +675,7 @@ DiskReader::can_internal_playback_seek (samplecnt_t distance)
        boost::shared_ptr<ChannelList> c = channels.reader();
 
        for (chan = c->begin(); chan != c->end(); ++chan) {
-               if ((*chan)->buf->read_space() < (size_t) distance) {
+               if ((*chan)->rbuf->read_space() < (size_t) distance) {
                        return false;
                }
        }
@@ -730,7 +695,7 @@ DiskReader::internal_playback_seek (samplecnt_t distance)
        boost::shared_ptr<ChannelList> c = channels.reader();
 
        for (chan = c->begin(); chan != c->end(); ++chan) {
-               (*chan)->buf->increment_read_ptr (::llabs(distance));
+               (*chan)->rbuf->increment_read_ptr (::llabs(distance));
        }
 
        playback_sample += distance;
@@ -786,7 +751,7 @@ DiskReader::audio_read (Sample* buf, Sample* mixdown_buffer, float* gain_buffer,
                   just once.
                */
 
-               if ((loc = loop_location) != 0) {
+               if ((loc = _loop_location) != 0) {
                        loop_start = loc->start();
                        loop_end = loc->end();
                        loop_length = loop_end - loop_start;
@@ -866,8 +831,8 @@ DiskReader::_do_refill_with_alloc (bool partial_fill)
        */
 
        {
-               std::auto_ptr<Sample> mix_buf (new Sample[2*1048576]);
-               std::auto_ptr<float>  gain_buf (new float[2*1048576]);
+               boost::scoped_array<Sample> mix_buf (new Sample[2*1048576]);
+               boost::scoped_array<float>  gain_buf (new float[2*1048576]);
 
                int ret = refill_audio (mix_buf.get(), gain_buf.get(), (partial_fill ? _chunk_samples : 0));
 
@@ -935,7 +900,7 @@ DiskReader::refill_audio (Sample* mixdown_buffer, float* gain_buffer, samplecnt_
        vector.buf[1] = 0;
        vector.len[1] = 0;
 
-       c->front()->buf->get_write_vector (&vector);
+       c->front()->rbuf->get_write_vector (&vector);
 
        if ((total_space = vector.len[0] + vector.len[1]) == 0) {
                DEBUG_TRACE (DEBUG::DiskIO, string_compose ("%1: no space to refill\n", name()));
@@ -973,7 +938,7 @@ DiskReader::refill_audio (Sample* mixdown_buffer, float* gain_buffer, samplecnt_
           work with.
        */
 
-       if (_slaved && total_space < (samplecnt_t) (c->front()->buf->bufsize() / 2)) {
+       if (_slaved && total_space < (samplecnt_t) (c->front()->rbuf->bufsize() / 2)) {
                DEBUG_TRACE (DEBUG::DiskIO, string_compose ("%1: not enough to refill while slaved\n", this));
                return 0;
        }
@@ -989,12 +954,12 @@ DiskReader::refill_audio (Sample* mixdown_buffer, float* gain_buffer, samplecnt_
                        for (chan_n = 0, i = c->begin(); i != c->end(); ++i, ++chan_n) {
 
                                ChannelInfo* chan (*i);
-                               chan->buf->get_write_vector (&vector);
+                               chan->rbuf->get_write_vector (&vector);
                                memset (vector.buf[0], 0, sizeof(Sample) * vector.len[0]);
                                if (vector.len[1]) {
                                        memset (vector.buf[1], 0, sizeof(Sample) * vector.len[1]);
                                }
-                               chan->buf->increment_write_ptr (vector.len[0] + vector.len[1]);
+                               chan->rbuf->increment_write_ptr (vector.len[0] + vector.len[1]);
                        }
                        return 0;
                }
@@ -1022,12 +987,12 @@ DiskReader::refill_audio (Sample* mixdown_buffer, float* gain_buffer, samplecnt_
                        for (chan_n = 0, i = c->begin(); i != c->end(); ++i, ++chan_n) {
 
                                ChannelInfo* chan (*i);
-                               chan->buf->get_write_vector (&vector);
+                               chan->rbuf->get_write_vector (&vector);
                                memset (vector.buf[0], 0, sizeof(Sample) * vector.len[0]);
                                if (vector.len[1]) {
                                        memset (vector.buf[1], 0, sizeof(Sample) * vector.len[1]);
                                }
-                               chan->buf->increment_write_ptr (vector.len[0] + vector.len[1]);
+                               chan->rbuf->increment_write_ptr (vector.len[0] + vector.len[1]);
                        }
                        return 0;
                }
@@ -1075,7 +1040,7 @@ DiskReader::refill_audio (Sample* mixdown_buffer, float* gain_buffer, samplecnt_
                Sample* buf2;
                samplecnt_t len1, len2;
 
-               chan->buf->get_write_vector (&vector);
+               chan->rbuf->get_write_vector (&vector);
 
                if ((samplecnt_t) vector.len[0] > samples_to_read) {
 
@@ -1119,7 +1084,7 @@ DiskReader::refill_audio (Sample* mixdown_buffer, float* gain_buffer, samplecnt_
                                ret = -1;
                                goto out;
                        }
-                       chan->buf->increment_write_ptr (to_read);
+                       chan->rbuf->increment_write_ptr (to_read);
                        ts -= to_read;
                }
 
@@ -1137,7 +1102,7 @@ DiskReader::refill_audio (Sample* mixdown_buffer, float* gain_buffer, samplecnt_
                                goto out;
                        }
 
-                       chan->buf->increment_write_ptr (to_read);
+                       chan->rbuf->increment_write_ptr (to_read);
                }
 
                if (zero_fill) {
@@ -1154,7 +1119,7 @@ DiskReader::refill_audio (Sample* mixdown_buffer, float* gain_buffer, samplecnt_
 
        ret = ((total_space - samples_to_read) > _chunk_samples);
 
-       c->front()->buf->get_write_vector (&vector);
+       c->front()->rbuf->get_write_vector (&vector);
 
   out:
        return ret;
@@ -1241,16 +1206,6 @@ DiskReader::move_processor_automation (boost::weak_ptr<Processor> p, list< Evora
        }
 }
 
-boost::shared_ptr<MidiBuffer>
-DiskReader::get_gui_feed_buffer () const
-{
-       boost::shared_ptr<MidiBuffer> b (new MidiBuffer (AudioEngine::instance()->raw_buffer_size (DataType::MIDI)));
-
-       Glib::Threads::Mutex::Lock lm (_gui_feed_buffer_mutex);
-       b->copy (_gui_feed_buffer);
-       return b;
-}
-
 void
 DiskReader::reset_tracker ()
 {
@@ -1279,25 +1234,26 @@ DiskReader::resolve_tracker (Evoral::EventSink<samplepos_t>& buffer, samplepos_t
  *  so that an event at playback_sample has time = 0
  */
 void
-DiskReader::get_midi_playback (MidiBuffer& dst, samplecnt_t nframes, MonitorState ms, BufferSet& scratch_bufs, double speed, samplecnt_t disk_samples_to_consume)
+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) {
-               dst.clear();
+               /* Route::process_output_buffers() clears the buffer as-needed */
                target = &dst;
        } else {
                target = &scratch_bufs.get_midi (0);
        }
 
        if (ms & MonitoringDisk) {
-               /* no disk data needed */
+               /* disk data needed */
 
-               Location* loc = loop_location;
+               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(), playback_sample, playback_sample + nframes,
+                                    _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";
@@ -1310,7 +1266,7 @@ DiskReader::get_midi_playback (MidiBuffer& dst, samplecnt_t nframes, MonitorStat
                        samplepos_t effective_start;
 
                        Evoral::Range<samplepos_t> loop_range (loc->start(), loc->end() - 1);
-                       effective_start = loop_range.squish (playback_sample);
+                       effective_start = loop_range.squish (start_sample);
 
                        DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose ("looped, effective start adjusted to %1\n", effective_start));
 
@@ -1357,12 +1313,12 @@ DiskReader::get_midi_playback (MidiBuffer& dst, samplecnt_t nframes, MonitorStat
                                events_read = _midi_buf->read (*target, effective_start, effective_start + nframes);
                        }
                } else {
-                       const size_t n_skipped = _midi_buf->skip_to (playback_sample);
+                       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)", playback_sample, playback_sample + nframes, nframes));
-                       events_read = _midi_buf->read (*target, playback_sample, playback_sample + nframes);
+                       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 (
@@ -1375,10 +1331,12 @@ DiskReader::get_midi_playback (MidiBuffer& dst, samplecnt_t nframes, MonitorStat
        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;
                }
        }
@@ -1387,9 +1345,25 @@ DiskReader::get_midi_playback (MidiBuffer& dst, samplecnt_t nframes, MonitorStat
                dst.merge_from (*target, nframes);
        }
 
-       //cerr << "======== POST ========\n";
-       //_midi_buf->dump (cerr);
-       //cerr << "----------------\n";
+#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 */
@@ -1400,7 +1374,7 @@ DiskReader::midi_read (samplepos_t& start, samplecnt_t dur, bool reversed)
        samplepos_t loop_end    = 0;
        samplepos_t loop_start  = 0;
        samplecnt_t loop_length = 0;
-       Location*  loc         = loop_location;
+       Location*  loc         = _loop_location;
        samplepos_t effective_start = start;
        Evoral::Range<samplepos_t>*  loop_range (0);