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