NO-OP/Optimize Bounce: Lookup data-type only once
[ardour.git] / libs / ardour / disk_reader.cc
index c5ae34d4f29681b9c2a2316e365003d5b97caf2d..cef82639fe1db27f64897fd842841e7743d4ed8b 100644 (file)
@@ -23,6 +23,7 @@
 #include "pbd/memento_command.h"
 #include "pbd/playback_buffer.h"
 
+#include "ardour/amp.h"
 #include "ardour/audioengine.h"
 #include "ardour/audioplaylist.h"
 #include "ardour/audio_buffer.h"
@@ -55,23 +56,18 @@ bool DiskReader::_no_disk_output = false;
 DiskReader::DiskReader (Session& s, string const & str, DiskIOProcessor::Flag f)
        : DiskIOProcessor (s, str, f)
        , overwrite_sample (0)
-       , _pending_overwrite (false)
        , overwrite_queued (false)
-       , _declick_gain (0)
+       , _declick_amp (s.nominal_sample_rate ())
+       , _declick_offs (0)
 {
        file_sample[DataType::AUDIO] = 0;
        file_sample[DataType::MIDI] = 0;
+       g_atomic_int_set (&_pending_overwrite, 0);
 }
 
 DiskReader::~DiskReader ()
 {
        DEBUG_TRACE (DEBUG::Destruction, string_compose ("DiskReader %1 @ %2 deleted\n", _name, this));
-
-       for (uint32_t n = 0; n < DataType::num_types; ++n) {
-               if (_playlists[n]) {
-                       _playlists[n]->release ();
-               }
-       }
 }
 
 void
@@ -266,7 +262,13 @@ DiskReader::run (BufferSet& bufs, samplepos_t start_sample, samplepos_t end_samp
                }
        }
 
-       if ((speed == 0.0) && (ms == MonitoringDisk)) {
+       const gain_t target_gain = (speed == 0.0 || ((ms & MonitoringDisk) == 0)) ? 0.0 : 1.0;
+
+       if (!_session.cfg ()->get_use_transport_fades ()) {
+               _declick_amp.set_gain (target_gain);
+       }
+
+       if ((speed == 0.0) && (ms == MonitoringDisk) && _declick_amp.gain () == target_gain) {
                /* no channels, or stopped. Don't accidentally pass any data
                 * from disk into our outputs (e.g. via interpolation)
                 */
@@ -274,7 +276,7 @@ DiskReader::run (BufferSet& bufs, samplepos_t start_sample, samplepos_t end_samp
        }
 
        BufferSet& scratch_bufs (_session.get_scratch_buffers (bufs.count()));
-       const bool still_locating = _session.global_locate_pending();
+       const bool still_locating = _session.global_locate_pending() || pending_overwrite ();
 
        if (c->empty()) {
                /* do nothing with audio */
@@ -289,6 +291,19 @@ DiskReader::run (BufferSet& bufs, samplepos_t start_sample, samplepos_t end_samp
                disk_samples_to_consume = nframes;
        }
 
+       if (_declick_amp.gain () != target_gain && target_gain == 0) {
+               /* fade-out */
+#if 0
+               printf ("DR fade-out speed=%.1f gain=%.3f off=%ld start=%ld playpos=%ld (%s)\n",
+                               speed, _declick_amp.gain (), _declick_offs, start_sample, playback_sample, owner()->name().c_str());
+#endif
+               ms = MonitorState (ms | MonitoringDisk);
+               assert (result_required);
+               result_required = true;
+       } else {
+               _declick_offs = 0;
+       }
+
        if (!result_required || ((ms & MonitoringDisk) == 0) || still_locating || _no_disk_output) {
 
                /* no need for actual disk data, just advance read pointer and return */
@@ -322,20 +337,14 @@ DiskReader::run (BufferSet& bufs, samplepos_t start_sample, samplepos_t end_samp
                for (n = 0, chan = c->begin(); chan != c->end(); ++chan, ++n) {
 
                        ChannelInfo* chaninfo (*chan);
-                       AudioBuffer& output (bufs.get_audio (n%n_buffers));
-                       Sample* disk_signal = 0; /* assignment not really needed but it keeps the compiler quiet and helps track bugs */
+                       AudioBuffer& output (bufs.get_audio (n % n_buffers));
 
-                       if (ms & MonitoringInput) {
-                               /* put disk stream in scratch buffer, blend at end */
-                               disk_signal = scratch_bufs.get_audio(n).data ();
-                       } else {
-                               /* no input stream needed, just overwrite buffers */
-                               disk_signal = output.data ();
-                       }
+                       AudioBuffer& disk_buf ((ms & MonitoringInput) ? scratch_bufs.get_audio(n) : output);
 
-                       if (start_sample != playback_sample) {
+                       if (start_sample != playback_sample && target_gain != 0) {
+#ifndef NDEBUG
                                cerr << owner()->name() << " playback @ " << start_sample << " not aligned with " << playback_sample << " jump " << (start_sample - playback_sample) << endl;
-
+#endif
                                if (can_internal_playback_seek (start_sample - playback_sample)) {
                                        internal_playback_seek (start_sample - playback_sample);
                                } else {
@@ -346,8 +355,7 @@ DiskReader::run (BufferSet& bufs, samplepos_t start_sample, samplepos_t end_samp
                        }
 
                        if (speed != 0.0) {
-                               assert (disk_signal);
-                               const samplecnt_t total = chaninfo->rbuf->read (disk_signal, disk_samples_to_consume);
+                               const samplecnt_t total = chaninfo->rbuf->read (disk_buf.data(), disk_samples_to_consume);
                                if (disk_samples_to_consume > total) {
                                        cerr << _name << " Need " << disk_samples_to_consume << " total = " << total << endl;
                                        cerr << "underrun for " << _name << endl;
@@ -356,15 +364,19 @@ DiskReader::run (BufferSet& bufs, samplepos_t start_sample, samplepos_t end_samp
                                        Underrun ();
                                        return;
                                }
+                       } else if (_declick_amp.gain () != target_gain) {
+                               assert (target_gain == 0);
+                               const samplecnt_t total = chaninfo->rbuf->read (disk_buf.data(), nframes, false, _declick_offs);
+                               _declick_offs += total;
                        }
 
-                       if (scaling != 1.0f && speed != 0.0) {
-                               apply_gain_to_buffer (disk_signal, disk_samples_to_consume, scaling);
-                       }
+                       _declick_amp.apply_gain (disk_buf, nframes, target_gain);
+
+                       Amp::apply_simple_gain (disk_buf, nframes, scaling);
 
                        if (ms & MonitoringInput) {
                                /* mix the disk signal into the input signal (already in bufs) */
-                               mix_buffers_no_gain (output.data(), disk_signal, disk_samples_to_consume);
+                               mix_buffers_no_gain (output.data(), disk_buf.data(), nframes);
                        }
                }
        }
@@ -469,32 +481,44 @@ DiskReader::run (BufferSet& bufs, samplepos_t start_sample, samplepos_t end_samp
        // DEBUG_TRACE (DEBUG::Butler, string_compose ("%1 reader run, needs butler = %2\n", name(), _need_butler));
 }
 
+bool
+DiskReader::declick_in_progress () const {
+       /* TODO use an atomic-get.
+        * this may be called from the butler thread
+        */
+       return _declick_amp.gain() != 0; // declick-out
+}
+
+bool
+DiskReader::pending_overwrite () const {
+       return g_atomic_int_get (&_pending_overwrite) != 0;
+}
+
 void
-DiskReader::set_pending_overwrite (bool yn)
+DiskReader::set_pending_overwrite ()
 {
        /* called from audio thread, so we can use the read ptr and playback sample as we wish */
 
-       _pending_overwrite = yn;
-
+       assert (!pending_overwrite ());
        overwrite_sample = playback_sample;
 
        boost::shared_ptr<ChannelList> c = channels.reader ();
        for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
                (*chan)->rbuf->read_flush ();
        }
+       g_atomic_int_set (&_pending_overwrite, 1);
 }
 
-int
+bool
 DiskReader::overwrite_existing_buffers ()
 {
-       int ret = -1;
-
-       boost::shared_ptr<ChannelList> c = channels.reader();
-
+       /* called from butler thread */
+       assert (pending_overwrite ());
        overwrite_queued = false;
 
        DEBUG_TRACE (DEBUG::DiskIO, string_compose ("%1 overwriting existing buffers at %2\n", overwrite_sample));
 
+       boost::shared_ptr<ChannelList> c = channels.reader();
        if (!c->empty ()) {
                /* AUDIO */
 
@@ -502,6 +526,7 @@ DiskReader::overwrite_existing_buffers ()
 
                /* assume all are the same size */
                samplecnt_t size = c->front()->rbuf->write_space ();
+               assert (size > 0);
 
                boost::scoped_array<Sample> sum_buffer (new Sample[size]);
                boost::scoped_array<Sample> mixdown_buffer (new Sample[size]);
@@ -526,8 +551,6 @@ DiskReader::overwrite_existing_buffers ()
                                goto midi;
                        }
                }
-
-               ret = 0;
        }
 
   midi:
@@ -554,21 +577,40 @@ DiskReader::overwrite_existing_buffers ()
                file_sample[DataType::MIDI] = overwrite_sample; // overwrite_sample was adjusted by ::midi_read() to the new position
        }
 
-       _pending_overwrite = false;
+       g_atomic_int_set (&_pending_overwrite, 0);
 
-       return ret;
+       return true;
 }
 
 int
 DiskReader::seek (samplepos_t sample, bool complete_refill)
 {
+       /* called via non_realtime_locate() from butler thread */
+
        uint32_t n;
        int ret = -1;
        ChannelList::iterator chan;
        boost::shared_ptr<ChannelList> c = channels.reader();
 
+#ifndef NDEBUG
+       if (_declick_amp.gain() != 0) {
+               /* this should not happen. new transport should postponse seeking
+                * until de-click is complete */
+               printf ("LOCATE WITHOUT DECLICK (gain=%f) at %ld seek-to %ld\n", _declick_amp.gain (), playback_sample, sample);
+               //return -1;
+       }
+       if (sample == playback_sample && !complete_refill) {
+               return 0; // XXX double-check this
+       }
+#endif
+
+       g_atomic_int_set (&_pending_overwrite, 0);
+
        //sample = std::max ((samplecnt_t)0, sample -_session.worst_output_latency ());
 
+       //printf ("DiskReader::seek %s %ld -> %ld refill=%d\n", owner()->name().c_str(), playback_sample, sample, complete_refill);
+       // TODO: check if we can micro-locate
+
        for (n = 0, chan = c->begin(); chan != c->end(); ++chan, ++n) {
                (*chan)->rbuf->reset ();
        }
@@ -603,7 +645,6 @@ DiskReader::seek (samplepos_t sample, bool complete_refill)
                ret = do_refill_with_alloc (true);
        }
 
-
        return ret;
 }
 
@@ -646,7 +687,7 @@ DiskReader::internal_playback_seek (sampleoffset_t distance)
        boost::shared_ptr<ChannelList> c = channels.reader();
        for (chan = c->begin(); chan != c->end(); ++chan) {
                if (distance < 0) {
-                       off = 0 - (sampleoffset_t) (*chan)->rbuf->decrement_read_ptr (llabs (distance));
+                       off = 0 - (sampleoffset_t) (*chan)->rbuf->decrement_read_ptr (::llabs (distance));
                } else {
                        off = (*chan)->rbuf->increment_read_ptr (distance);
                }
@@ -1110,7 +1151,7 @@ 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 = llabs (end_sample - start_sample);
+       samplepos_t nframes = ::llabs (end_sample - start_sample);
 
        assert (_midi_buf);
 
@@ -1205,17 +1246,6 @@ DiskReader::get_midi_playback (MidiBuffer& dst, samplepos_t start_sample, sample
 
        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);
        }
@@ -1400,3 +1430,54 @@ DiskReader::set_no_disk_output (bool yn)
        */
        _no_disk_output = yn;
 }
+
+DiskReader::DeclickAmp::DeclickAmp (samplecnt_t sample_rate)
+{
+       _a = 4550.f / (gain_t)sample_rate;
+       _l = -log1p (_a);
+       _g = 0;
+}
+
+void
+DiskReader::DeclickAmp::apply_gain (AudioBuffer& buf, samplecnt_t n_samples, const float target)
+{
+       if (n_samples == 0) {
+               return;
+       }
+       float g = _g;
+
+       if (g == target) {
+               Amp::apply_simple_gain (buf, n_samples, target, 0);
+               return;
+       }
+
+       const float a = _a;
+       Sample* const buffer = buf.data ();
+
+#define MAX_NPROC 16
+       uint32_t remain = n_samples;
+       uint32_t offset = 0;
+       while (remain > 0) {
+               uint32_t n_proc = remain > MAX_NPROC ? MAX_NPROC : remain;
+               for (uint32_t i = 0; i < n_proc; ++i) {
+                       buffer[offset + i] *= g;
+               }
+#if 1
+               g += a * (target - g);
+#else /* accurate exponential fade */
+               if (n_proc == MAX_NPROC) {
+                       g += a * (target - g);
+               } else {
+                       g = target - (target - g) * expf (_l * n_proc / MAX_NPROC);
+               }
+#endif
+               remain -= n_proc;
+               offset += n_proc;
+       }
+
+       if (fabsf (g - target) < /* GAIN_COEFF_DELTA */ 1e-5) {
+               _g = target;
+       } else {
+               _g = g;
+       }
+}