Remove disk-reader local MIDI vari-speed
[ardour.git] / libs / ardour / disk_reader.cc
1 /*
2  * Copyright (C) 2009-2018 Paul Davis
3  * Copyright (C) 2019 Robin Gareus <robin@gareus.org>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18  */
19
20 #include <boost/smart_ptr/scoped_array.hpp>
21
22 #include "pbd/enumwriter.h"
23 #include "pbd/memento_command.h"
24 #include "pbd/playback_buffer.h"
25
26 #include "ardour/amp.h"
27 #include "ardour/audioengine.h"
28 #include "ardour/audioplaylist.h"
29 #include "ardour/audio_buffer.h"
30 #include "ardour/butler.h"
31 #include "ardour/debug.h"
32 #include "ardour/disk_reader.h"
33 #include "ardour/midi_ring_buffer.h"
34 #include "ardour/midi_playlist.h"
35 #include "ardour/midi_track.h"
36 #include "ardour/pannable.h"
37 #include "ardour/playlist.h"
38 #include "ardour/playlist_factory.h"
39 #include "ardour/session.h"
40 #include "ardour/session_playlists.h"
41
42 #include "pbd/i18n.h"
43
44 using namespace ARDOUR;
45 using namespace PBD;
46 using namespace std;
47
48 ARDOUR::samplecnt_t DiskReader::_chunk_samples = default_chunk_samples ();
49 PBD::Signal0<void> DiskReader::Underrun;
50 Sample* DiskReader::_sum_buffer = 0;
51 Sample* DiskReader::_mixdown_buffer = 0;
52 gain_t* DiskReader::_gain_buffer = 0;
53 samplecnt_t DiskReader::midi_readahead = 4096;
54 bool DiskReader::_no_disk_output = false;
55
56 DiskReader::DiskReader (Session& s, string const & str, DiskIOProcessor::Flag f)
57         : DiskIOProcessor (s, str, f)
58         , overwrite_sample (0)
59         , _pending_overwrite (false)
60         , overwrite_queued (false)
61         , _declick_amp (s.nominal_sample_rate ())
62         , _declick_offs (0)
63 {
64         file_sample[DataType::AUDIO] = 0;
65         file_sample[DataType::MIDI] = 0;
66 }
67
68 DiskReader::~DiskReader ()
69 {
70         DEBUG_TRACE (DEBUG::Destruction, string_compose ("DiskReader %1 @ %2 deleted\n", _name, this));
71
72         for (uint32_t n = 0; n < DataType::num_types; ++n) {
73                 if (_playlists[n]) {
74                         _playlists[n]->release ();
75                 }
76         }
77 }
78
79 void
80 DiskReader::ReaderChannelInfo::resize (samplecnt_t bufsize)
81 {
82         delete rbuf;
83         /* touch memory to lock it */
84         rbuf = new PlaybackBuffer<Sample> (bufsize);
85         memset (rbuf->buffer(), 0, sizeof (Sample) * rbuf->bufsize());
86 }
87
88 int
89 DiskReader::add_channel_to (boost::shared_ptr<ChannelList> c, uint32_t how_many)
90 {
91         while (how_many--) {
92                 c->push_back (new ReaderChannelInfo (_session.butler()->audio_diskstream_playback_buffer_size()));
93                 DEBUG_TRACE (DEBUG::DiskIO, string_compose ("%1: new reader channel, write space = %2 read = %3\n",
94                                                             name(),
95                                                             c->back()->rbuf->write_space(),
96                                                             c->back()->rbuf->read_space()));
97         }
98
99         return 0;
100 }
101
102 void
103 DiskReader::allocate_working_buffers()
104 {
105         /* with varifill buffer refilling, we compute the read size in bytes (to optimize
106            for disk i/o bandwidth) and then convert back into samples. These buffers
107            need to reflect the maximum size we could use, which is 4MB reads, or 2M samples
108            using 16 bit samples.
109         */
110         _sum_buffer           = new Sample[2*1048576];
111         _mixdown_buffer       = new Sample[2*1048576];
112         _gain_buffer          = new gain_t[2*1048576];
113 }
114
115 void
116 DiskReader::free_working_buffers()
117 {
118         delete [] _sum_buffer;
119         delete [] _mixdown_buffer;
120         delete [] _gain_buffer;
121         _sum_buffer     = 0;
122         _mixdown_buffer = 0;
123         _gain_buffer    = 0;
124 }
125
126 samplecnt_t
127 DiskReader::default_chunk_samples()
128 {
129         return 65536;
130 }
131
132 bool
133 DiskReader::set_name (string const & str)
134 {
135         string my_name = X_("player:");
136         my_name += str;
137
138         if (_name != my_name) {
139                 SessionObject::set_name (my_name);
140         }
141
142         return true;
143 }
144
145 XMLNode&
146 DiskReader::state ()
147 {
148         XMLNode& node (DiskIOProcessor::state ());
149         node.set_property(X_("type"), X_("diskreader"));
150         return node;
151 }
152
153 int
154 DiskReader::set_state (const XMLNode& node, int version)
155 {
156         if (DiskIOProcessor::set_state (node, version)) {
157                 return -1;
158         }
159
160         return 0;
161 }
162
163 void
164 DiskReader::realtime_handle_transport_stopped ()
165 {
166 }
167
168 void
169 DiskReader::realtime_locate ()
170 {
171 }
172
173 float
174 DiskReader::buffer_load () const
175 {
176         /* Note: for MIDI it's not trivial to differentiate the following two cases:
177
178            1.  The playback buffer is empty because the system has run out of time to fill it.
179            2.  The playback buffer is empty because there is no more data on the playlist.
180
181            If we use a simple buffer load computation, we will report that the MIDI diskstream
182            cannot keep up when #2 happens, when in fact it can.  Since MIDI data rates
183            are so low compared to audio, just use the audio value here.
184         */
185
186         boost::shared_ptr<ChannelList> c = channels.reader();
187
188         if (c->empty ()) {
189                 /* no channels, so no buffers, so completely full and ready to playback, sir! */
190                 return 1.0;
191         }
192
193         PBD::PlaybackBuffer<Sample>* b = c->front()->rbuf;
194         return (float) ((double) b->read_space() / (double) b->bufsize());
195 }
196
197 void
198 DiskReader::adjust_buffering ()
199 {
200         boost::shared_ptr<ChannelList> c = channels.reader();
201
202         for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
203                 (*chan)->resize (_session.butler()->audio_diskstream_playback_buffer_size());
204         }
205 }
206
207 void
208 DiskReader::playlist_changed (const PropertyChange&)
209 {
210         playlist_modified ();
211 }
212
213 void
214 DiskReader::playlist_modified ()
215 {
216         if (!i_am_the_modifier && !overwrite_queued) {
217                 _session.request_overwrite_buffer (_route);
218                 overwrite_queued = true;
219         }
220 }
221
222 int
223 DiskReader::use_playlist (DataType dt, boost::shared_ptr<Playlist> playlist)
224 {
225         bool prior_playlist = false;
226
227         if (_playlists[dt]) {
228                 prior_playlist = true;
229         }
230
231         if (DiskIOProcessor::use_playlist (dt, playlist)) {
232                 return -1;
233         }
234
235         /* don't do this if we've already asked for it *or* if we are setting up
236            the diskstream for the very first time - the input changed handling will
237            take care of the buffer refill.
238         */
239
240         if (!overwrite_queued && (prior_playlist || _session.loading())) {
241                 _session.request_overwrite_buffer (_route);
242                 overwrite_queued = true;
243         }
244
245         return 0;
246 }
247
248 void
249 DiskReader::run (BufferSet& bufs, samplepos_t start_sample, samplepos_t end_sample,
250                  double speed, pframes_t nframes, bool result_required)
251 {
252         uint32_t n;
253         boost::shared_ptr<ChannelList> c = channels.reader();
254         ChannelList::iterator chan;
255         sampleoffset_t disk_samples_to_consume;
256         MonitorState ms = _route->monitoring_state ();
257
258         if (_active) {
259                 if (!_pending_active) {
260                         _active = false;
261                         return;
262                 }
263         } else {
264                 if (_pending_active) {
265                         _active = true;
266                 } else {
267                         return;
268                 }
269         }
270
271         if ((speed == 0.0) && (ms == MonitoringDisk)) {
272                 /* no channels, or stopped. Don't accidentally pass any data
273                  * from disk into our outputs (e.g. via interpolation)
274                  */
275                 return;
276         }
277
278         BufferSet& scratch_bufs (_session.get_scratch_buffers (bufs.count()));
279         const bool still_locating = _session.global_locate_pending();
280
281         if (c->empty()) {
282                 /* do nothing with audio */
283                 goto midi;
284         }
285
286         assert (speed == -1 || speed == 0 || speed == 1);
287
288         if (speed == 0) {
289                 disk_samples_to_consume = 0;
290         } else {
291                 disk_samples_to_consume = nframes;
292         }
293
294         if (!result_required || ((ms & MonitoringDisk) == 0) || still_locating || _no_disk_output) {
295
296                 /* no need for actual disk data, just advance read pointer and return */
297
298                 if (!still_locating || _no_disk_output) {
299                         for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
300                                 (*chan)->rbuf->increment_read_ptr (disk_samples_to_consume);
301                         }
302                 }
303
304                 /* if monitoring disk but locating put silence in the buffers */
305
306                 if ((_no_disk_output || still_locating) && (ms == MonitoringDisk)) {
307                         bufs.silence (nframes, 0);
308                 }
309
310         } else {
311
312                 /* we need audio data from disk */
313
314                 size_t n_buffers = bufs.count().n_audio();
315                 size_t n_chans = c->size();
316                 gain_t scaling;
317
318                 if (n_chans > n_buffers) {
319                         scaling = ((float) n_buffers) / n_chans;
320                 } else {
321                         scaling = 1.0;
322                 }
323
324                 for (n = 0, chan = c->begin(); chan != c->end(); ++chan, ++n) {
325
326                         ChannelInfo* chaninfo (*chan);
327                         AudioBuffer& output (bufs.get_audio (n%n_buffers));
328
329                         AudioBuffer& disk_buf ((ms & MonitoringInput) ? scratch_bufs.get_audio(n) : output);
330
331                         if (start_sample != playback_sample) {
332                                 cerr << owner()->name() << " playback @ " << start_sample << " not aligned with " << playback_sample << " jump " << (start_sample - playback_sample) << endl;
333
334                                 if (can_internal_playback_seek (start_sample - playback_sample)) {
335                                         internal_playback_seek (start_sample - playback_sample);
336                                 } else {
337                                         cerr << owner()->name() << " playback not possible: ss = " << start_sample << " ps = " << playback_sample << endl;
338                                         abort (); // XXX -- now what?
339                                         goto midi;
340                                 }
341                         }
342
343                         if (speed != 0.0) {
344                                 const samplecnt_t total = chaninfo->rbuf->read (disk_buf.data(), disk_samples_to_consume);
345                                 if (disk_samples_to_consume > total) {
346                                         cerr << _name << " Need " << disk_samples_to_consume << " total = " << total << endl;
347                                         cerr << "underrun for " << _name << endl;
348                                         DEBUG_TRACE (DEBUG::Butler, string_compose ("%1 underrun in %2, total space = %3\n",
349                                                                                     DEBUG_THREAD_SELF, name(), total));
350                                         Underrun ();
351                                         return;
352                                 }
353                         }
354
355                         if (scaling != 1.0f && speed != 0.0) {
356                                 Amp::apply_simple_gain (disk_buf, disk_samples_to_consume, scaling);
357                         }
358
359                         if (ms & MonitoringInput) {
360                                 /* mix the disk signal into the input signal (already in bufs) */
361                                 mix_buffers_no_gain (output.data(), disk_buf.data(), disk_samples_to_consume);
362                         }
363                 }
364         }
365
366         /* MIDI data handling */
367
368   midi:
369         if (/*!_session.declick_out_pending() && */ bufs.count().n_midi() && _midi_buf) {
370                 MidiBuffer* dst;
371
372                 if (_no_disk_output) {
373                         dst = &scratch_bufs.get_midi(0);
374                 } else {
375                         dst = &bufs.get_midi (0);
376                 }
377
378                 if ((ms & MonitoringDisk) && !still_locating) {
379                         get_midi_playback (*dst, start_sample, end_sample, ms, scratch_bufs, speed, disk_samples_to_consume);
380                 }
381         }
382
383         if (!still_locating) {
384
385                 bool butler_required = false;
386
387                 if (speed < 0.0) {
388                         playback_sample -= disk_samples_to_consume;
389                 } else {
390                         playback_sample += disk_samples_to_consume;
391                 }
392
393                 if (_playlists[DataType::AUDIO]) {
394                         if (!c->empty()) {
395                                 if (_slaved) {
396                                         if (c->front()->rbuf->write_space() >= c->front()->rbuf->bufsize() / 2) {
397                                                 DEBUG_TRACE (DEBUG::Butler, string_compose ("%1: slaved, write space = %2 of %3\n", name(), c->front()->rbuf->write_space(), c->front()->rbuf->bufsize()));
398                                                 butler_required = true;
399                                         }
400                                 } else {
401                                         if ((samplecnt_t) c->front()->rbuf->write_space() >= _chunk_samples) {
402                                                 DEBUG_TRACE (DEBUG::Butler, string_compose ("%1: write space = %2 of %3\n", name(), c->front()->rbuf->write_space(),
403                                                                                             _chunk_samples));
404                                                 butler_required = true;
405                                         }
406                                 }
407                         }
408                 }
409
410                 if (_playlists[DataType::MIDI]) {
411                         /* MIDI butler needed part */
412
413                         uint32_t samples_read = g_atomic_int_get(const_cast<gint*>(&_samples_read_from_ringbuffer));
414                         uint32_t samples_written = g_atomic_int_get(const_cast<gint*>(&_samples_written_to_ringbuffer));
415
416                         /*
417                           cerr << name() << " MDS written: " << samples_written << " - read: " << samples_read <<
418                           " = " << samples_written - samples_read
419                           << " + " << disk_samples_to_consume << " < " << midi_readahead << " = " << need_butler << ")" << endl;
420                         */
421
422                         /* samples_read will generally be less than samples_written, but
423                          * immediately after an overwrite, we can end up having read some data
424                          * before we've written any. we don't need to trip an assert() on this,
425                          * but we do need to check so that the decision on whether or not we
426                          * need the butler is done correctly.
427                          */
428
429                         /* furthermore..
430                          *
431                          * Doing heavy GUI operations[1] can stall also the butler.
432                          * The RT-thread meanwhile will happily continue and
433                          * â€˜samples_read’ (from buffer to output) will become larger
434                          * than â€˜samples_written’ (from disk to buffer).
435                          *
436                          * The disk-stream is now behind..
437                          *
438                          * In those cases the butler needs to be summed to refill the buffer (done now)
439                          * AND we need to skip (samples_read - samples_written). ie remove old events
440                          * before playback_sample from the rinbuffer.
441                          *
442                          * [1] one way to do so is described at #6170.
443                          * For me just popping up the context-menu on a MIDI-track header
444                          * of a track with a large (think beethoven :) midi-region also did the
445                          * trick. The playhead stalls for 2 or 3 sec, until the context-menu shows.
446                          *
447                          * In both cases the root cause is that redrawing MIDI regions on the GUI is still very slow
448                          * and can stall
449                          */
450                         if (samples_read <= samples_written) {
451                                 if ((samples_written - samples_read) + disk_samples_to_consume < midi_readahead) {
452                                         butler_required = true;
453                                 }
454                         } else {
455                                 butler_required = true;
456                         }
457
458                 }
459
460                 _need_butler = butler_required;
461         }
462
463         // DEBUG_TRACE (DEBUG::Butler, string_compose ("%1 reader run, needs butler = %2\n", name(), _need_butler));
464 }
465
466 void
467 DiskReader::set_pending_overwrite (bool yn)
468 {
469         /* called from audio thread, so we can use the read ptr and playback sample as we wish */
470
471         _pending_overwrite = yn;
472
473         overwrite_sample = playback_sample;
474
475         boost::shared_ptr<ChannelList> c = channels.reader ();
476         for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
477                 (*chan)->rbuf->read_flush ();
478         }
479 }
480
481 int
482 DiskReader::overwrite_existing_buffers ()
483 {
484         int ret = -1;
485
486         boost::shared_ptr<ChannelList> c = channels.reader();
487
488         overwrite_queued = false;
489
490         DEBUG_TRACE (DEBUG::DiskIO, string_compose ("%1 overwriting existing buffers at %2\n", overwrite_sample));
491
492         if (!c->empty ()) {
493                 /* AUDIO */
494
495                 const bool reversed = _session.transport_speed() < 0.0f;
496
497                 /* assume all are the same size */
498                 samplecnt_t size = c->front()->rbuf->write_space ();
499
500                 boost::scoped_array<Sample> sum_buffer (new Sample[size]);
501                 boost::scoped_array<Sample> mixdown_buffer (new Sample[size]);
502                 boost::scoped_array<float> gain_buffer (new float[size]);
503
504                 /* reduce size so that we can fill the buffer correctly (ringbuffers
505                  * can only handle size-1, otherwise they appear to be empty)
506                  */
507                 size--;
508
509                 uint32_t n=0;
510
511                 for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan, ++n) {
512
513                         samplepos_t start = overwrite_sample;
514                         samplecnt_t to_read = size;
515
516                         cerr << owner()->name() << " over-read: " << to_read << endl;
517
518                         if (audio_read ((*chan)->rbuf, sum_buffer.get(), mixdown_buffer.get(), gain_buffer.get(), start, to_read, n, reversed)) {
519                                 error << string_compose(_("DiskReader %1: when refilling, cannot read %2 from playlist at sample %3"), id(), size, overwrite_sample) << endmsg;
520                                 goto midi;
521                         }
522                 }
523
524                 ret = 0;
525         }
526
527   midi:
528
529         if (_midi_buf && _playlists[DataType::MIDI]) {
530
531                 /* Clear the playback buffer contents.  This is safe as long as the butler
532                    thread is suspended, which it should be.
533                 */
534                 _midi_buf->reset ();
535                 _midi_buf->reset_tracker ();
536
537                 g_atomic_int_set (&_samples_read_from_ringbuffer, 0);
538                 g_atomic_int_set (&_samples_written_to_ringbuffer, 0);
539
540                 /* Resolve all currently active notes in the playlist.  This is more
541                    aggressive than it needs to be: ideally we would only resolve what is
542                    absolutely necessary, but this seems difficult and/or impossible without
543                    having the old data or knowing what change caused the overwrite.
544                 */
545                 midi_playlist()->resolve_note_trackers (*_midi_buf, overwrite_sample);
546
547                 midi_read (overwrite_sample, _chunk_samples, false);
548                 file_sample[DataType::MIDI] = overwrite_sample; // overwrite_sample was adjusted by ::midi_read() to the new position
549         }
550
551         _pending_overwrite = false;
552
553         return ret;
554 }
555
556 int
557 DiskReader::seek (samplepos_t sample, bool complete_refill)
558 {
559         uint32_t n;
560         int ret = -1;
561         ChannelList::iterator chan;
562         boost::shared_ptr<ChannelList> c = channels.reader();
563
564         //sample = std::max ((samplecnt_t)0, sample -_session.worst_output_latency ());
565
566         for (n = 0, chan = c->begin(); chan != c->end(); ++chan, ++n) {
567                 (*chan)->rbuf->reset ();
568         }
569
570         if (g_atomic_int_get (&_samples_read_from_ringbuffer) == 0) {
571                 /* we haven't read anything since the last seek,
572                    so flush all note trackers to prevent
573                    wierdness
574                 */
575                 reset_tracker ();
576         }
577
578         if (_midi_buf) {
579                 _midi_buf->reset();
580         }
581         g_atomic_int_set(&_samples_read_from_ringbuffer, 0);
582         g_atomic_int_set(&_samples_written_to_ringbuffer, 0);
583
584         playback_sample = sample;
585         file_sample[DataType::AUDIO] = sample;
586         file_sample[DataType::MIDI] = sample;
587
588         if (complete_refill) {
589                 /* call _do_refill() to refill the entire buffer, using
590                    the largest reads possible.
591                 */
592                 while ((ret = do_refill_with_alloc (false)) > 0) ;
593         } else {
594                 /* call _do_refill() to refill just one chunk, and then
595                    return.
596                 */
597                 ret = do_refill_with_alloc (true);
598         }
599
600
601         return ret;
602 }
603
604 bool
605 DiskReader::can_internal_playback_seek (sampleoffset_t distance)
606 {
607         /* 1. Audio */
608
609         ChannelList::iterator chan;
610         boost::shared_ptr<ChannelList> c = channels.reader();
611
612         for (chan = c->begin(); chan != c->end(); ++chan) {
613                 if (!(*chan)->rbuf->can_seek (distance)) {
614                         return false;
615                 }
616         }
617
618         if (distance < 0) {
619                 return true; // XXX TODO un-seek MIDI
620         }
621
622         /* 2. MIDI */
623
624         uint32_t samples_read    = g_atomic_int_get(&_samples_read_from_ringbuffer);
625         uint32_t samples_written = g_atomic_int_get(&_samples_written_to_ringbuffer);
626
627         return ((samples_written - samples_read) < distance);
628 }
629
630 void
631 DiskReader::internal_playback_seek (sampleoffset_t distance)
632 {
633         if (distance == 0) {
634                 return;
635         }
636
637         sampleoffset_t off = distance;
638
639         ChannelList::iterator chan;
640         boost::shared_ptr<ChannelList> c = channels.reader();
641         for (chan = c->begin(); chan != c->end(); ++chan) {
642                 if (distance < 0) {
643                         off = 0 - (sampleoffset_t) (*chan)->rbuf->decrement_read_ptr (llabs (distance));
644                 } else {
645                         off = (*chan)->rbuf->increment_read_ptr (distance);
646                 }
647         }
648
649         playback_sample += off;
650 }
651
652 static
653 void swap_by_ptr (Sample *first, Sample *last)
654 {
655         while (first < last) {
656                 Sample tmp = *first;
657                 *first++ = *last;
658                 *last-- = tmp;
659         }
660 }
661
662 /** Read some data for 1 channel from our playlist into a buffer.
663  *  @param buf Buffer to write to.
664  *  @param start Session sample to start reading from; updated to where we end up
665  *         after the read.
666  *  @param cnt Count of samples to read.
667  *  @param reversed true if we are running backwards, otherwise false.
668  */
669 int
670 DiskReader::audio_read (PBD::PlaybackBuffer<Sample>*rb,
671                         Sample* sum_buffer,
672                         Sample* mixdown_buffer,
673                         float* gain_buffer,
674                         samplepos_t& start, samplecnt_t cnt,
675                         int channel, bool reversed)
676 {
677         samplecnt_t this_read = 0;
678         bool reloop = false;
679         samplepos_t loop_end = 0;
680         samplepos_t loop_start = 0;
681         Location *loc = 0;
682
683         if (!_playlists[DataType::AUDIO]) {
684                 rb->write_zero (cnt);
685                 return 0;
686         }
687
688         /* XXX we don't currently play loops in reverse. not sure why */
689
690         if (!reversed) {
691
692                 samplecnt_t loop_length = 0;
693
694                 /* Make the use of a Location atomic for this read operation.
695
696                    Note: Locations don't get deleted, so all we care about
697                    when I say "atomic" is that we are always pointing to
698                    the same one and using a start/length values obtained
699                    just once.
700                 */
701
702                 if ((loc = _loop_location) != 0) {
703                         loop_start = loc->start();
704                         loop_end = loc->end();
705                         loop_length = loop_end - loop_start;
706                 }
707
708                 /* if we are looping, ensure that the first sample we read is at the correct
709                    position within the loop.
710                 */
711
712                 if (loc && start >= loop_end) {
713                         start = loop_start + ((start - loop_start) % loop_length);
714                 }
715
716         }
717
718         if (reversed) {
719                 start -= cnt;
720         }
721
722         /* We need this while loop in case we hit a loop boundary, in which case our read from
723            the playlist must be split into more than one section.
724         */
725
726         while (cnt) {
727
728                 /* take any loop into account. we can't read past the end of the loop. */
729
730                 if (loc && (loop_end - start < cnt)) {
731                         this_read = loop_end - start;
732                         reloop = true;
733                 } else {
734                         reloop = false;
735                         this_read = cnt;
736                 }
737
738                 if (this_read == 0) {
739                         break;
740                 }
741
742                 this_read = min (cnt, this_read);
743
744                 if (audio_playlist()->read (sum_buffer, mixdown_buffer, gain_buffer, start, this_read, channel) != this_read) {
745                         error << string_compose(_("DiskReader %1: cannot read %2 from playlist at sample %3"), id(), this_read, start) << endmsg;
746                         return -1;
747                 }
748
749                 if (reversed) {
750
751                         swap_by_ptr (sum_buffer, sum_buffer + this_read - 1);
752
753                 } else {
754
755                         /* if we read to the end of the loop, go back to the beginning */
756
757                         if (reloop) {
758                                 start = loop_start;
759                         } else {
760                                 start += this_read;
761                         }
762                 }
763
764                 if (rb->write (sum_buffer, this_read) != this_read) {
765                         cerr << owner()->name() << " Ringbuffer Write overrun" << endl;
766                 }
767
768                 cnt -= this_read;
769         }
770
771         return 0;
772 }
773
774 int
775 DiskReader::_do_refill_with_alloc (bool partial_fill)
776 {
777         /* We limit disk reads to at most 4MB chunks, which with floating point
778            samples would be 1M samples. But we might use 16 or 14 bit samples,
779            in which case 4MB is more samples than that. Therefore size this for
780            the smallest sample value .. 4MB = 2M samples (16 bit).
781         */
782
783         {
784                 boost::scoped_array<Sample> sum_buf (new Sample[2*1048576]);
785                 boost::scoped_array<Sample> mix_buf (new Sample[2*1048576]);
786                 boost::scoped_array<float>  gain_buf (new float[2*1048576]);
787
788                 int ret = refill_audio (sum_buf.get(), mix_buf.get(), gain_buf.get(), (partial_fill ? _chunk_samples : 0));
789
790                 if (ret) {
791                         return ret;
792                 }
793         }
794
795         return refill_midi ();
796 }
797
798 int
799 DiskReader::refill (Sample* sum_buffer, Sample* mixdown_buffer, float* gain_buffer, samplecnt_t fill_level)
800 {
801         int ret = refill_audio (sum_buffer, mixdown_buffer, gain_buffer, fill_level);
802
803         if (ret) {
804                 return ret;
805         }
806
807         return refill_midi ();
808 }
809
810
811 /** Get some more data from disk and put it in our channels' bufs,
812  *  if there is suitable space in them.
813  *
814  * If fill_level is non-zero, then we will refill the buffer so that there is
815  * still at least fill_level samples of space left to be filled. This is used
816  * after locates so that we do not need to wait to fill the entire buffer.
817  *
818  */
819
820 int
821 DiskReader::refill_audio (Sample* sum_buffer, Sample* mixdown_buffer, float* gain_buffer, samplecnt_t fill_level)
822 {
823         /* do not read from disk while session is marked as Loading, to avoid
824            useless redundant I/O.
825         */
826
827         if (_session.loading()) {
828                 return 0;
829         }
830
831         int32_t ret = 0;
832         bool const reversed = _session.transport_speed() < 0.0f;
833         samplecnt_t zero_fill;
834         uint32_t chan_n;
835         ChannelList::iterator i;
836         boost::shared_ptr<ChannelList> c = channels.reader();
837
838         if (c->empty()) {
839                 return 0;
840         }
841
842         assert(mixdown_buffer);
843         assert(gain_buffer);
844
845         samplecnt_t total_space = c->front()->rbuf->write_space();
846
847         if (total_space == 0) {
848                 DEBUG_TRACE (DEBUG::DiskIO, string_compose ("%1: no space to refill\n", name()));
849                 /* nowhere to write to */
850                 return 0;
851         }
852
853         if (fill_level) {
854                 if (fill_level < total_space) {
855                         total_space -= fill_level;
856                 } else {
857                         /* we can't do anything with it */
858                         fill_level = 0;
859                 }
860         }
861
862         /* if we're running close to normal speed and there isn't enough
863            space to do disk_read_chunk_samples of I/O, then don't bother.
864
865            at higher speeds, just do it because the sync between butler
866            and audio thread may not be good enough.
867
868            Note: it is a design assumption that disk_read_chunk_samples is smaller
869            than the playback buffer size, so this check should never trip when
870            the playback buffer is empty.
871         */
872
873         DEBUG_TRACE (DEBUG::DiskIO, string_compose ("%1: space to refill %2 vs. chunk %3 (speed = %4)\n", name(), total_space, _chunk_samples, _session.transport_speed()));
874         if ((total_space < _chunk_samples) && fabs (_session.transport_speed()) < 2.0f) {
875                 return 0;
876         }
877
878         /* when slaved, don't try to get too close to the read pointer. this
879            leaves space for the buffer reversal to have something useful to
880            work with.
881         */
882
883         if (_slaved && total_space < (samplecnt_t) (c->front()->rbuf->bufsize() / 2)) {
884                 DEBUG_TRACE (DEBUG::DiskIO, string_compose ("%1: not enough to refill while slaved\n", this));
885                 return 0;
886         }
887
888         samplepos_t ffa = file_sample[DataType::AUDIO];
889
890         if (reversed) {
891
892                 if (ffa == 0) {
893                         /* at start: nothing to do but fill with silence */
894                         for (chan_n = 0, i = c->begin(); i != c->end(); ++i, ++chan_n) {
895                                 ChannelInfo* chan (*i);
896                                 chan->rbuf->write_zero (chan->rbuf->write_space ());
897                         }
898                         return 0;
899                 }
900
901                 if (ffa < total_space) {
902                         /* too close to the start: read what we can, and then zero fill the rest */
903                         zero_fill = total_space - ffa;
904                         total_space = ffa;
905                 } else {
906                         zero_fill = 0;
907                 }
908
909         } else {
910
911                 if (ffa == max_samplepos) {
912                         /* at end: nothing to do but fill with silence */
913                         for (chan_n = 0, i = c->begin(); i != c->end(); ++i, ++chan_n) {
914                                 ChannelInfo* chan (*i);
915                                 chan->rbuf->write_zero (chan->rbuf->write_space ());
916                         }
917                         return 0;
918                 }
919
920                 if (ffa > max_samplepos - total_space) {
921                         /* to close to the end: read what we can, and zero fill the rest */
922                         zero_fill = total_space - (max_samplepos - ffa);
923                         total_space = max_samplepos - ffa;
924
925                 } else {
926                         zero_fill = 0;
927                 }
928         }
929
930         /* total_space is in samples. We want to optimize read sizes in various sizes using bytes */
931         const size_t bits_per_sample = format_data_width (_session.config.get_native_file_data_format());
932         size_t total_bytes = total_space * bits_per_sample / 8;
933
934         /* chunk size range is 256kB to 4MB. Bigger is faster in terms of MB/sec, but bigger chunk size always takes longer */
935         size_t byte_size_for_read = max ((size_t) (256 * 1024), min ((size_t) (4 * 1048576), total_bytes));
936
937         /* find nearest (lower) multiple of 16384 */
938
939         byte_size_for_read = (byte_size_for_read / 16384) * 16384;
940
941         /* now back to samples */
942         samplecnt_t samples_to_read = byte_size_for_read / (bits_per_sample / 8);
943
944         DEBUG_TRACE (DEBUG::DiskIO, string_compose ("%1: will refill %2 channels with %3 samples\n", name(), c->size(), total_space));
945
946         samplepos_t file_sample_tmp = ffa;
947
948         for (chan_n = 0, i = c->begin(); i != c->end(); ++i, ++chan_n) {
949                 ChannelInfo* chan (*i);
950                 file_sample_tmp = ffa;
951                 samplecnt_t ts = total_space;
952
953                 samplecnt_t to_read = min (ts, (samplecnt_t) chan->rbuf->write_space ());
954                 to_read = min (to_read, samples_to_read);
955                 assert (to_read >= 0);
956
957                 // cerr << owner()->name() << " to-read: " << to_read << endl;
958
959                 if (to_read) {
960                         if (audio_read (chan->rbuf, sum_buffer, mixdown_buffer, gain_buffer, file_sample_tmp, to_read, chan_n, reversed)) {
961                                 error << string_compose(_("DiskReader %1: when refilling, cannot read %2 from playlist at sample %3"), id(), to_read, ffa) << endmsg;
962                                 ret = -1;
963                                 goto out;
964                         }
965                 }
966
967                 if (zero_fill) {
968                         /* not sure if action is needed,
969                          * we'll later hit the "to close to the end" case
970                          */
971                         //chan->rbuf->write_zero (zero_fill);
972                 }
973         }
974
975         // elapsed = g_get_monotonic_time () - before;
976         // cerr << '\t' << name() << ": bandwidth = " << (byte_size_for_read / 1048576.0) / (elapsed/1000000.0) << "MB/sec\n";
977
978         file_sample[DataType::AUDIO] = file_sample_tmp;
979         assert (file_sample[DataType::AUDIO] >= 0);
980
981         ret = ((total_space - samples_to_read) > _chunk_samples);
982
983   out:
984         return ret;
985 }
986
987 void
988 DiskReader::playlist_ranges_moved (list< Evoral::RangeMove<samplepos_t> > const & movements_samples, bool from_undo_or_shift)
989 {
990         /* If we're coming from an undo, it will have handled
991          * automation undo (it must, since automation-follows-regions
992          * can lose automation data).  Hence we can do nothing here.
993          *
994          * Likewise when shifting regions (insert/remove time)
995          * automation is taken care of separately (busses with
996          * automation have no disk-reader).
997          */
998
999         if (from_undo_or_shift) {
1000                 return;
1001         }
1002
1003         if (!_route || Config->get_automation_follows_regions () == false) {
1004                 return;
1005         }
1006
1007         list< Evoral::RangeMove<double> > movements;
1008
1009         for (list< Evoral::RangeMove<samplepos_t> >::const_iterator i = movements_samples.begin();
1010              i != movements_samples.end();
1011              ++i) {
1012
1013                 movements.push_back(Evoral::RangeMove<double>(i->from, i->length, i->to));
1014         }
1015
1016         /* move panner automation */
1017         boost::shared_ptr<Pannable> pannable = _route->pannable();
1018         Evoral::ControlSet::Controls& c (pannable->controls());
1019
1020         for (Evoral::ControlSet::Controls::iterator ci = c.begin(); ci != c.end(); ++ci) {
1021                 boost::shared_ptr<AutomationControl> ac = boost::dynamic_pointer_cast<AutomationControl>(ci->second);
1022                 if (!ac) {
1023                         continue;
1024                 }
1025                 boost::shared_ptr<AutomationList> alist = ac->alist();
1026                 if (!alist->size()) {
1027                         continue;
1028                 }
1029                 XMLNode & before = alist->get_state ();
1030                 bool const things_moved = alist->move_ranges (movements);
1031                 if (things_moved) {
1032                         _session.add_command (new MementoCommand<AutomationList> (
1033                                                       *alist.get(), &before, &alist->get_state ()));
1034                 }
1035         }
1036         /* move processor automation */
1037         _route->foreach_processor (boost::bind (&DiskReader::move_processor_automation, this, _1, movements_samples));
1038 }
1039
1040 void
1041 DiskReader::move_processor_automation (boost::weak_ptr<Processor> p, list< Evoral::RangeMove<samplepos_t> > const & movements_samples)
1042 {
1043         boost::shared_ptr<Processor> processor (p.lock ());
1044         if (!processor) {
1045                 return;
1046         }
1047
1048         list< Evoral::RangeMove<double> > movements;
1049         for (list< Evoral::RangeMove<samplepos_t> >::const_iterator i = movements_samples.begin(); i != movements_samples.end(); ++i) {
1050                 movements.push_back(Evoral::RangeMove<double>(i->from, i->length, i->to));
1051         }
1052
1053         set<Evoral::Parameter> const a = processor->what_can_be_automated ();
1054
1055         for (set<Evoral::Parameter>::const_iterator i = a.begin (); i != a.end (); ++i) {
1056                 boost::shared_ptr<AutomationList> al = processor->automation_control(*i)->alist();
1057                 if (!al->size()) {
1058                         continue;
1059                 }
1060                 XMLNode & before = al->get_state ();
1061                 bool const things_moved = al->move_ranges (movements);
1062                 if (things_moved) {
1063                         _session.add_command (
1064                                 new MementoCommand<AutomationList> (
1065                                         *al.get(), &before, &al->get_state ()
1066                                         )
1067                                 );
1068                 }
1069         }
1070 }
1071
1072 void
1073 DiskReader::reset_tracker ()
1074 {
1075         if (_midi_buf) {
1076                 _midi_buf->reset_tracker ();
1077         }
1078
1079         boost::shared_ptr<MidiPlaylist> mp (midi_playlist());
1080
1081         if (mp) {
1082                 mp->reset_note_trackers ();
1083         }
1084 }
1085
1086 void
1087 DiskReader::resolve_tracker (Evoral::EventSink<samplepos_t>& buffer, samplepos_t time)
1088 {
1089         if (_midi_buf) {
1090                 _midi_buf->resolve_tracker(buffer, time);
1091         }
1092
1093         boost::shared_ptr<MidiPlaylist> mp (midi_playlist());
1094
1095         if (mp) {
1096                 mp->reset_note_trackers ();
1097         }
1098 }
1099
1100 /** Writes playback events from playback_sample for nframes to dst, translating time stamps
1101  *  so that an event at playback_sample has time = 0
1102  */
1103 void
1104 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)
1105 {
1106         MidiBuffer* target;
1107         samplepos_t nframes = llabs (end_sample - start_sample);
1108
1109         assert (_midi_buf);
1110
1111         if ((ms & MonitoringInput) == 0) {
1112                 /* Route::process_output_buffers() clears the buffer as-needed */
1113                 target = &dst;
1114         } else {
1115                 target = &scratch_bufs.get_midi (0);
1116         }
1117
1118         if (ms & MonitoringDisk) {
1119                 /* disk data needed */
1120
1121                 Location* loc = _loop_location;
1122
1123                 DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose (
1124                                      "%1 MDS pre-read read %8 offset = %9 @ %4..%5 from %2 write to %3, LOOPED ? %6 .. %7\n", _name,
1125                                      _midi_buf->get_read_ptr(), _midi_buf->get_write_ptr(), start_sample, end_sample,
1126                                      (loc ? loc->start() : -1), (loc ? loc->end() : -1), nframes, Port::port_offset()));
1127
1128                 //cerr << "======== PRE ========\n";
1129                 //_midi_buf->dump (cerr);
1130                 //cerr << "----------------\n";
1131
1132                 size_t events_read = 0;
1133
1134                 if (loc) {
1135                         samplepos_t effective_start;
1136
1137                         Evoral::Range<samplepos_t> loop_range (loc->start(), loc->end() - 1);
1138                         effective_start = loop_range.squish (start_sample);
1139
1140                         DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose ("looped, effective start adjusted to %1\n", effective_start));
1141
1142                         if (effective_start == loc->start()) {
1143                                 /* We need to turn off notes that may extend
1144                                    beyond the loop end.
1145                                 */
1146
1147                                 _midi_buf->resolve_tracker (*target, 0);
1148                         }
1149
1150                         /* for split-cycles we need to offset the events */
1151
1152                         if (loc->end() >= effective_start && loc->end() < effective_start + nframes) {
1153
1154                                 /* end of loop is within the range we are reading, so
1155                                    split the read in two, and lie about the location
1156                                    for the 2nd read
1157                                 */
1158
1159                                 samplecnt_t first, second;
1160
1161                                 first = loc->end() - effective_start;
1162                                 second = nframes - first;
1163
1164                                 DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose ("loop read for eff %1 end %2: %3 and %4, cycle offset %5\n",
1165                                                                                       effective_start, loc->end(), first, second));
1166
1167                                 if (first) {
1168                                         DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose ("loop read #1, from %1 for %2\n",
1169                                                                                               effective_start, first));
1170                                         events_read = _midi_buf->read (*target, effective_start, first);
1171                                 }
1172
1173                                 if (second) {
1174                                         DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose ("loop read #2, from %1 for %2\n",
1175                                                                                               loc->start(), second));
1176                                         events_read += _midi_buf->read (*target, loc->start(), second);
1177                                 }
1178
1179                         } else {
1180                                 DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose ("loop read #3, adjusted start as %1 for %2\n",
1181                                                                                       effective_start, nframes));
1182                                 events_read = _midi_buf->read (*target, effective_start, effective_start + nframes);
1183                         }
1184                 } else {
1185                         const size_t n_skipped = _midi_buf->skip_to (start_sample);
1186                         if (n_skipped > 0) {
1187                                 warning << string_compose(_("MidiDiskstream %1: skipped %2 events, possible underflow"), id(), n_skipped) << endmsg;
1188                         }
1189                         DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose ("playback buffer read, from %1 to %2 (%3)", start_sample, end_sample, nframes));
1190                         events_read = _midi_buf->read (*target, start_sample, end_sample, Port::port_offset ());
1191                 }
1192
1193                 DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose (
1194                                      "%1 MDS events read %2 range %3 .. %4 rspace %5 wspace %6 r@%7 w@%8\n",
1195                                      _name, events_read, playback_sample, playback_sample + nframes,
1196                                      _midi_buf->read_space(), _midi_buf->write_space(),
1197                                      _midi_buf->get_read_ptr(), _midi_buf->get_write_ptr()));
1198         }
1199
1200         g_atomic_int_add (&_samples_read_from_ringbuffer, nframes);
1201
1202         if (ms & MonitoringInput) {
1203                 dst.merge_from (*target, nframes);
1204         }
1205
1206 #if 0
1207         if (!target->empty ()) {
1208                 cerr << "======== MIDI OUT ========\n";
1209                 for (MidiBuffer::iterator i = target->begin(); i != target->end(); ++i) {
1210                         const Evoral::Event<MidiBuffer::TimeType> ev (*i, false);
1211                         cerr << "MIDI EVENT (from disk) @ " << ev.time();
1212                         for (size_t xx = 0; xx < ev.size(); ++xx) {
1213                                 cerr << ' ' << hex << (int) ev.buffer()[xx];
1214                         }
1215                         cerr << dec << endl;
1216                 }
1217                 cerr << "----------------\n";
1218         }
1219 #endif
1220 #if 0
1221         cerr << "======== MIDI Disk Buffer ========\n";
1222         _midi_buf->dump (cerr);
1223         cerr << "----------------\n";
1224 #endif
1225 }
1226
1227 /** @a start is set to the new sample position (TIME) read up to */
1228 int
1229 DiskReader::midi_read (samplepos_t& start, samplecnt_t dur, bool reversed)
1230 {
1231         samplecnt_t this_read   = 0;
1232         samplepos_t loop_end    = 0;
1233         samplepos_t loop_start  = 0;
1234         samplecnt_t loop_length = 0;
1235         Location*  loc         = _loop_location;
1236         samplepos_t effective_start = start;
1237         Evoral::Range<samplepos_t>*  loop_range (0);
1238
1239         assert(_midi_buf);
1240
1241         DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose ("MDS::midi_read @ %1 cnt %2\n", start, dur));
1242
1243         boost::shared_ptr<MidiTrack> mt = boost::dynamic_pointer_cast<MidiTrack>(_route);
1244         MidiChannelFilter* filter = mt ? &mt->playback_filter() : 0;
1245         sampleoffset_t loop_offset = 0;
1246
1247         if (!reversed && loc) {
1248                 get_location_times (loc, &loop_start, &loop_end, &loop_length);
1249         }
1250
1251         while (dur) {
1252
1253                 /* take any loop into account. we can't read past the end of the loop. */
1254
1255                 if (loc && !reversed) {
1256
1257                         if (!loop_range) {
1258                                 loop_range = new Evoral::Range<samplepos_t> (loop_start, loop_end-1); // inclusive semantics require -1
1259                         }
1260
1261                         /* if we are (seamlessly) looping, ensure that the first sample we read is at the correct
1262                            position within the loop.
1263                         */
1264
1265                         effective_start = loop_range->squish (effective_start);
1266
1267                         if ((loop_end - effective_start) <= dur) {
1268                                 /* too close to end of loop to read "dur", so
1269                                    shorten it.
1270                                 */
1271                                 this_read = loop_end - effective_start;
1272                         } else {
1273                                 this_read = dur;
1274                         }
1275
1276                 } else {
1277                         this_read = dur;
1278                 }
1279
1280                 if (this_read == 0) {
1281                         break;
1282                 }
1283
1284                 this_read = min (dur,this_read);
1285
1286                 DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose ("MDS ::read at %1 for %2 loffset %3\n", effective_start, this_read, loop_offset));
1287
1288                 if (midi_playlist()->read (*_midi_buf, effective_start, this_read, loop_range, 0, filter) != this_read) {
1289                         error << string_compose(
1290                                         _("MidiDiskstream %1: cannot read %2 from playlist at sample %3"),
1291                                         id(), this_read, start) << endmsg;
1292                         return -1;
1293                 }
1294
1295                 g_atomic_int_add (&_samples_written_to_ringbuffer, this_read);
1296
1297                 if (reversed) {
1298
1299                         // Swap note ons with note offs here.  etc?
1300                         // Fully reversing MIDI requires look-ahead (well, behind) to find previous
1301                         // CC values etc.  hard.
1302
1303                 } else {
1304
1305                         /* adjust passed-by-reference argument (note: this is
1306                            monotonic and does not reflect looping.
1307                         */
1308                         start += this_read;
1309
1310                         /* similarly adjust effective_start, but this may be
1311                            readjusted for seamless looping as we continue around
1312                            the loop.
1313                         */
1314                         effective_start += this_read;
1315                 }
1316
1317                 dur -= this_read;
1318         }
1319
1320         return 0;
1321 }
1322
1323 int
1324 DiskReader::refill_midi ()
1325 {
1326         if (!_playlists[DataType::MIDI] || !_midi_buf) {
1327                 return 0;
1328         }
1329
1330         const size_t  write_space = _midi_buf->write_space();
1331         const bool reversed    = _session.transport_speed() < 0.0f;
1332
1333         DEBUG_TRACE (DEBUG::DiskIO, string_compose ("MIDI refill, write space = %1 file sample = %2\n", write_space, file_sample[DataType::MIDI]));
1334
1335         /* no space to write */
1336         if (write_space == 0) {
1337                 return 0;
1338         }
1339
1340         if (reversed) {
1341                 return 0;
1342         }
1343
1344         /* at end: nothing to do */
1345
1346         samplepos_t ffm = file_sample[DataType::MIDI];
1347
1348         if (ffm == max_samplepos) {
1349                 return 0;
1350         }
1351
1352         int ret = 0;
1353         const uint32_t samples_read = g_atomic_int_get (&_samples_read_from_ringbuffer);
1354         const uint32_t samples_written = g_atomic_int_get (&_samples_written_to_ringbuffer);
1355
1356         if ((samples_read < samples_written) && (samples_written - samples_read) >= midi_readahead) {
1357                 return 0;
1358         }
1359
1360         samplecnt_t to_read = midi_readahead - ((samplecnt_t)samples_written - (samplecnt_t)samples_read);
1361
1362         to_read = min (to_read, (samplecnt_t) (max_samplepos - ffm));
1363         to_read = min (to_read, (samplecnt_t) write_space);
1364
1365         if (midi_read (ffm, to_read, reversed)) {
1366                 ret = -1;
1367         }
1368
1369         file_sample[DataType::MIDI] = ffm;
1370
1371         return ret;
1372 }
1373
1374 void
1375 DiskReader::set_no_disk_output (bool yn)
1376 {
1377         /* this MUST be called as part of the process call tree, before any
1378            disk readers are invoked. We use it when the session needs the
1379            transport (and thus effective read position for DiskReaders) to keep
1380            advancing as part of syncing up with a transport master, but we
1381            don't want any actual disk output yet because we are still not
1382            synced.
1383         */
1384         _no_disk_output = yn;
1385 }
1386
1387 DiskReader::DeclickAmp::DeclickAmp (samplecnt_t sample_rate)
1388 {
1389         _a = 2200.f / (gain_t)sample_rate;
1390         _l = -log1p (_a);
1391         _g = 0;
1392 }
1393
1394 void
1395 DiskReader::DeclickAmp::apply_gain (AudioBuffer& buf, samplecnt_t n_samples, const float target)
1396 {
1397         if (n_samples == 0) {
1398                 return;
1399         }
1400         float g = _g;
1401
1402         if (g == target) {
1403                 Amp::apply_simple_gain (buf, n_samples, target, 0);
1404                 return;
1405         }
1406
1407         const float a = _a;
1408         Sample* const buffer = buf.data ();
1409
1410 #define MAX_NPROC 16
1411         uint32_t remain = n_samples;
1412         uint32_t offset = 0;
1413         while (remain > 0) {
1414                 uint32_t n_proc = remain > MAX_NPROC ? MAX_NPROC : remain;
1415                 for (uint32_t i = 0; i < n_proc; ++i) {
1416                         buffer[offset + i] *= g;
1417                 }
1418 #if 1
1419                 g += a * (target - g);
1420 #else /* accurate exponential fade */
1421                 if (n_proc == MAX_NPROC) {
1422                         g += a * (target - g);
1423                 } else {
1424                         g = target - (target - g) * expf (_l * n_proc / MAX_NPROC);
1425                 }
1426 #endif
1427                 remain -= n_proc;
1428                 offset += n_proc;
1429         }
1430
1431         if (fabsf (g - target) < /* GAIN_COEFF_DELTA */ 1e-5) {
1432                 _g = target;
1433         } else {
1434                 _g = g;
1435         }
1436 }