NO-OP/Optimize Bounce: Lookup data-type only once
[ardour.git] / libs / ardour / disk_reader.cc
index 70c35318cb6cde74845f54c7ba9dedd7e933be06..cef82639fe1db27f64897fd842841e7743d4ed8b 100644 (file)
@@ -56,24 +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_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
@@ -268,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)
                 */
@@ -276,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 */
@@ -291,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 */
@@ -324,13 +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));
+                       AudioBuffer& output (bufs.get_audio (n % n_buffers));
 
                        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 {
@@ -350,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) {
-                               Amp::apply_simple_gain (disk_buf, 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_buf.data(), disk_samples_to_consume);
+                               mix_buffers_no_gain (output.data(), disk_buf.data(), nframes);
                        }
                }
        }
@@ -463,31 +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 ()
 {
        /* called from audio thread, so we can use the read ptr and playback sample as we wish */
 
-       assert (!_pending_overwrite);
-
-       _pending_overwrite = true;
+       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);
 }
 
 bool
 DiskReader::overwrite_existing_buffers ()
 {
-       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 */
 
@@ -495,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]);
@@ -545,7 +577,7 @@ 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 true;
 }
@@ -553,13 +585,32 @@ DiskReader::overwrite_existing_buffers ()
 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 ();
        }
@@ -594,7 +645,6 @@ DiskReader::seek (samplepos_t sample, bool complete_refill)
                ret = do_refill_with_alloc (true);
        }
 
-
        return ret;
 }
 
@@ -637,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);
                }
@@ -1101,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);
 
@@ -1383,7 +1433,7 @@ DiskReader::set_no_disk_output (bool yn)
 
 DiskReader::DeclickAmp::DeclickAmp (samplecnt_t sample_rate)
 {
-       _a = 2200.f / (gain_t)sample_rate;
+       _a = 4550.f / (gain_t)sample_rate;
        _l = -log1p (_a);
        _g = 0;
 }