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 "evoral/Range.h"
27
28 #include "ardour/amp.h"
29 #include "ardour/audio_buffer.h"
30 #include "ardour/audioengine.h"
31 #include "ardour/audioplaylist.h"
32 #include "ardour/butler.h"
33 #include "ardour/debug.h"
34 #include "ardour/disk_reader.h"
35 #include "ardour/midi_playlist.h"
36 #include "ardour/midi_ring_buffer.h"
37 #include "ardour/midi_track.h"
38 #include "ardour/pannable.h"
39 #include "ardour/playlist.h"
40 #include "ardour/playlist_factory.h"
41 #include "ardour/session.h"
42 #include "ardour/session_playlists.h"
43
44 #include "pbd/i18n.h"
45
46 using namespace ARDOUR;
47 using namespace PBD;
48 using namespace std;
49
50 ARDOUR::samplecnt_t DiskReader::_chunk_samples = default_chunk_samples ();
51 PBD::Signal0<void> DiskReader::Underrun;
52 Sample* DiskReader::_sum_buffer = 0;
53 Sample* DiskReader::_mixdown_buffer = 0;
54 gain_t* DiskReader::_gain_buffer = 0;
55 GATOMIC_QUAL gint DiskReader::_no_disk_output (0);
56 DiskReader::Declicker DiskReader::loop_declick_in;
57 DiskReader::Declicker DiskReader::loop_declick_out;
58 samplecnt_t DiskReader::loop_fade_length (0);
59
DiskReader(Session & s,Track & t,string const & str,DiskIOProcessor::Flag f)60 DiskReader::DiskReader (Session& s, Track& t, string const& str, DiskIOProcessor::Flag f)
61 : DiskIOProcessor (s, t, X_("player:") + str, f)
62 , overwrite_sample (0)
63 , run_must_resolve (false)
64 , _declick_amp (s.nominal_sample_rate ())
65 , _declick_offs (0)
66 , _declick_enabled (false)
67 , last_refill_loop_start (0)
68 {
69 file_sample[DataType::AUDIO] = 0;
70 file_sample[DataType::MIDI] = 0;
71 g_atomic_int_set (&_pending_overwrite, 0);
72 }
73
~DiskReader()74 DiskReader::~DiskReader ()
75 {
76 DEBUG_TRACE (DEBUG::Destruction, string_compose ("DiskReader %1 @ %2 deleted\n", _name, this));
77 }
78
79 std::string
display_name() const80 DiskReader::display_name () const
81 {
82 return std::string (_("Player"));
83 }
84
85 void
resize(samplecnt_t bufsize)86 DiskReader::ReaderChannelInfo::resize (samplecnt_t bufsize)
87 {
88 delete rbuf;
89 rbuf = 0;
90
91 rbuf = new PlaybackBuffer<Sample> (bufsize);
92 /* touch memory to lock it */
93 memset (rbuf->buffer (), 0, sizeof (Sample) * rbuf->bufsize ());
94 initialized = false;
95 }
96
97 void
resize_preloop(samplecnt_t bufsize)98 DiskReader::ReaderChannelInfo::resize_preloop (samplecnt_t bufsize)
99 {
100 if (bufsize == 0) {
101 return;
102 }
103
104 if (bufsize > pre_loop_buffer_size) {
105 delete[] pre_loop_buffer;
106 pre_loop_buffer = new Sample[bufsize];
107 pre_loop_buffer_size = bufsize;
108 }
109 }
110
111 int
add_channel_to(boost::shared_ptr<ChannelList> c,uint32_t how_many)112 DiskReader::add_channel_to (boost::shared_ptr<ChannelList> c, uint32_t how_many)
113 {
114 while (how_many--) {
115 c->push_back (new ReaderChannelInfo (_session.butler ()->audio_playback_buffer_size (), loop_fade_length));
116 DEBUG_TRACE (DEBUG::DiskIO, string_compose ("%1: new reader channel, write space = %2 read = %3\n",
117 name (),
118 c->back ()->rbuf->write_space (),
119 c->back ()->rbuf->read_space ()));
120 }
121
122 return 0;
123 }
124
125 void
allocate_working_buffers()126 DiskReader::allocate_working_buffers ()
127 {
128 /* with varifill buffer refilling, we compute the read size in bytes (to optimize
129 for disk i/o bandwidth) and then convert back into samples. These buffers
130 need to reflect the maximum size we could use, which is 4MB reads, or 2M samples
131 using 16 bit samples.
132 */
133 _sum_buffer = new Sample[2 * 1048576];
134 _mixdown_buffer = new Sample[2 * 1048576];
135 _gain_buffer = new gain_t[2 * 1048576];
136 }
137
138 void
free_working_buffers()139 DiskReader::free_working_buffers ()
140 {
141 delete[] _sum_buffer;
142 delete[] _mixdown_buffer;
143 delete[] _gain_buffer;
144 _sum_buffer = 0;
145 _mixdown_buffer = 0;
146 _gain_buffer = 0;
147 }
148
149 samplecnt_t
default_chunk_samples()150 DiskReader::default_chunk_samples ()
151 {
152 return 65536;
153 }
154
155 bool
set_name(string const & str)156 DiskReader::set_name (string const& str)
157 {
158 string my_name = X_("player:");
159 my_name += str;
160
161 if (_name != my_name) {
162 SessionObject::set_name (my_name);
163 }
164
165 return true;
166 }
167
168 XMLNode&
state()169 DiskReader::state ()
170 {
171 XMLNode& node (DiskIOProcessor::state ());
172 node.set_property (X_("type"), X_("diskreader"));
173 return node;
174 }
175
176 int
set_state(const XMLNode & node,int version)177 DiskReader::set_state (const XMLNode& node, int version)
178 {
179 if (DiskIOProcessor::set_state (node, version)) {
180 return -1;
181 }
182
183 return 0;
184 }
185
186 void
realtime_handle_transport_stopped()187 DiskReader::realtime_handle_transport_stopped ()
188 {
189 /* can't do the resolve here because we don't have a place to put the
190 * note resolving data. Defer to
191 * MidiTrack::realtime_handle_transport_stopped() which will call
192 * ::resolve_tracker() and put the output in its _immediate_events store.
193 */
194 }
195
196 void
realtime_locate(bool for_loop_end)197 DiskReader::realtime_locate (bool for_loop_end)
198 {
199 if (!for_loop_end) {
200 MidiTrack* mt = dynamic_cast<MidiTrack*> (&_track);
201 _tracker.resolve_notes (mt->immediate_events (), 0);
202 }
203 }
204
205 float
buffer_load() const206 DiskReader::buffer_load () const
207 {
208 /* Note: for MIDI it's not trivial to differentiate the following two cases:
209 *
210 * 1. The playback buffer is empty because the system has run out of time to fill it.
211 * 2. The playback buffer is empty because there is no more data on the playlist.
212 *
213 * If we use a simple buffer load computation, we will report that the MIDI diskstream
214 * cannot keep up when #2 happens, when in fact it can. Since MIDI data rates
215 * are so low compared to audio, just use the audio value here.
216 */
217
218 boost::shared_ptr<ChannelList> c = channels.reader ();
219
220 if (c->empty ()) {
221 /* no channels, so no buffers, so completely full and ready to playback, sir! */
222 return 1.0;
223 }
224
225 PBD::PlaybackBuffer<Sample>* b = c->front ()->rbuf;
226 return (float)((double)b->read_space () / (double)b->bufsize ());
227 }
228
229 void
adjust_buffering()230 DiskReader::adjust_buffering ()
231 {
232 boost::shared_ptr<ChannelList> c = channels.reader ();
233
234 for (ChannelList::iterator chan = c->begin (); chan != c->end (); ++chan) {
235 (*chan)->resize (_session.butler ()->audio_playback_buffer_size ());
236 }
237 }
238
239 void
playlist_modified()240 DiskReader::playlist_modified ()
241 {
242 _session.request_overwrite_buffer (_track.shared_ptr (), PlaylistModified);
243 }
244
245 int
use_playlist(DataType dt,boost::shared_ptr<Playlist> playlist)246 DiskReader::use_playlist (DataType dt, boost::shared_ptr<Playlist> playlist)
247 {
248 bool prior_playlist = false;
249
250 if (_playlists[dt]) {
251 prior_playlist = true;
252 }
253
254 if (DiskIOProcessor::use_playlist (dt, playlist)) {
255 return -1;
256 }
257
258 /* don't do this if we've already asked for it *or* if we are setting up
259 * the diskstream for the very first time - the input changed handling will
260 * take care of the buffer refill. */
261
262 if (!(g_atomic_int_get (&_pending_overwrite) & PlaylistChanged) || prior_playlist) {
263 _session.request_overwrite_buffer (_track.shared_ptr (), PlaylistChanged);
264 }
265
266 return 0;
267 }
268
269 void
run(BufferSet & bufs,samplepos_t start_sample,samplepos_t end_sample,double speed,pframes_t nframes,bool result_required)270 DiskReader::run (BufferSet& bufs, samplepos_t start_sample, samplepos_t end_sample, double speed, pframes_t nframes, bool result_required)
271 {
272 uint32_t n;
273 boost::shared_ptr<ChannelList> c = channels.reader ();
274 ChannelList::iterator chan;
275 sampleoffset_t disk_samples_to_consume;
276 MonitorState ms = _track.monitoring_state ();
277 const bool midi_only = (c->empty() || !_playlists[DataType::AUDIO]);
278 bool no_disk_output = g_atomic_int_get (&_no_disk_output) != 0;
279
280 if (_active) {
281 if (!_pending_active) {
282 _active = false;
283 return;
284 }
285 } else {
286 if (_pending_active) {
287 _active = true;
288 } else {
289 return;
290 }
291 }
292
293 const gain_t target_gain = ((speed == 0.0) || ((ms & MonitoringDisk) == 0)) ? 0.0 : 1.0;
294 bool declick_out = (_declick_amp.gain () != target_gain) && target_gain == 0.0;
295
296 if (declick_out && _declick_amp.gain () == GAIN_COEFF_UNITY) {
297 /* beginning a de-click, set de-click reason */
298 if (speed == 0) {
299 _declick_enabled = _session.cfg ()->get_use_transport_fades ();
300 } else {
301 _declick_enabled = _session.cfg ()->get_use_monitor_fades ();
302 }
303 } else if (_declick_amp.gain () == GAIN_COEFF_ZERO && speed == 0) {
304 /* fade in */
305 _declick_enabled = _session.cfg ()->get_use_transport_fades ();
306 }
307
308 if (!_declick_enabled || (_session.exporting () && !_session.realtime_export ())) {
309 /* no transport fades or exporting - no declick out logic */
310
311 if (!midi_only) {
312 _declick_amp.set_gain (target_gain);
313 declick_out = false;
314 }
315
316 } else {
317 /* using transport fades and not exporting - declick login in effect */
318
319 if (ms == MonitoringDisk) {
320 /* Only monitoring from disk, so if we've finished a
321 * declick (for stop/locate), do not accidentally pass
322 * any data from disk to our outputs.
323 */
324
325 if ((target_gain == 0.0) && (_declick_amp.gain () == target_gain)) {
326 /* we were heading for zero (declick out for
327 * stop), and we've reached there. Done. */
328 return;
329 }
330 }
331 }
332
333 BufferSet& scratch_bufs (_session.get_scratch_buffers (bufs.count ()));
334 const bool still_locating = _session.global_locate_pending ();
335
336 assert (speed == -1 || speed == 0 || speed == 1);
337
338 if (speed == 0) {
339 disk_samples_to_consume = 0;
340 } else {
341 disk_samples_to_consume = nframes;
342 }
343
344 if (midi_only) {
345 /* do nothing with audio */
346 goto midi;
347 }
348
349 if (declick_out) {
350 /* fade-out */
351
352 // printf ("DR fade-out speed=%.1f gain=%.3f off=%ld start=%ld playpos=%ld (%s)\n", speed, _declick_amp.gain (), _declick_offs, start_sample, playback_sample, owner()->name().c_str());
353
354 ms = MonitorState (ms | MonitoringDisk);
355 assert (result_required);
356 result_required = true;
357 disk_samples_to_consume = 0; // non-committing read
358 } else {
359 _declick_offs = 0;
360 }
361
362 if (!result_required || ((ms & MonitoringDisk) == 0) || still_locating || no_disk_output) {
363 /* no need for actual disk data, just advance read pointer */
364
365 if (!still_locating || no_disk_output) {
366 for (ChannelList::iterator chan = c->begin (); chan != c->end (); ++chan) {
367 assert ((*chan)->rbuf);
368 (*chan)->rbuf->increment_read_ptr (disk_samples_to_consume);
369 }
370 }
371
372 /* if monitoring disk but locating put silence in the buffers */
373
374 if ((no_disk_output || still_locating) && (ms == MonitoringDisk)) {
375 bufs.silence (nframes, 0);
376 }
377
378 } else {
379 /* we need audio data from disk */
380
381 size_t n_buffers = bufs.count ().n_audio ();
382 size_t n_chans = c->size ();
383 gain_t scaling;
384
385 if (n_chans > n_buffers) {
386 scaling = ((float)n_buffers) / n_chans;
387 } else {
388 scaling = 1.0;
389 }
390
391 const float initial_declick_gain = _declick_amp.gain ();
392 const sampleoffset_t declick_offs = _declick_offs;
393
394 for (n = 0, chan = c->begin (); chan != c->end (); ++chan, ++n) {
395 ReaderChannelInfo* chaninfo = dynamic_cast<ReaderChannelInfo*> (*chan);
396 AudioBuffer& output (bufs.get_audio (n % n_buffers));
397
398 AudioBuffer& disk_buf ((ms & MonitoringInput) ? scratch_bufs.get_audio (n) : output);
399
400 if (start_sample != playback_sample && target_gain != 0) {
401 samplepos_t ss = start_sample;
402 Location* loc = _loop_location;
403 if (loc) {
404 Evoral::Range<samplepos_t> loop_range (loc->start (), loc->end () - 1);
405 ss = loop_range.squish (playback_sample);
406 }
407 if (ss != playback_sample) {
408 if (can_internal_playback_seek (ss - playback_sample)) {
409 internal_playback_seek (ss - playback_sample);
410 } else {
411 disk_samples_to_consume = 0; /* will force an underrun below */
412 }
413 }
414 }
415
416 /* reset _declick_amp to the correct gain before processing this channel. */
417 _declick_amp.set_gain (initial_declick_gain);
418
419 if (!declick_out) {
420 const samplecnt_t available = chaninfo->rbuf->read (disk_buf.data (), disk_samples_to_consume);
421
422 if (available == 0 && !chaninfo->initialized) {
423 disk_buf.silence (disk_samples_to_consume);
424 } else if (disk_samples_to_consume > available) {
425 cerr << "underrun for " << _name << " Available samples: " << available << " required: " << disk_samples_to_consume << endl;
426 DEBUG_TRACE (DEBUG::Butler, string_compose ("%1 underrun in %2, total space = %3 vs %4\n", DEBUG_THREAD_SELF, name (), available, disk_samples_to_consume));
427 Underrun ();
428 return;
429 }
430
431 } else if (_declick_amp.gain () != target_gain) {
432 assert (target_gain == 0);
433
434 /* note that this is a non-committing read: it
435 * retrieves data from the ringbuffer but does not
436 * advance the read pointer. As a result,
437 * subsequent calls (as we declick) need to
438 * pass in an offset describing where to read
439 * from. We maintain _declick_offs across calls
440 * to ::run()
441 */
442
443 const samplecnt_t total = chaninfo->rbuf->read (disk_buf.data (), nframes, false, declick_offs);
444
445 if (n == 0) {
446 _declick_offs += total;
447 }
448 }
449
450 _declick_amp.apply_gain (disk_buf, nframes, target_gain);
451
452 /* _declick_amp is now left with the correct gain after processing nframes */
453
454 Amp::apply_simple_gain (disk_buf, nframes, scaling);
455
456 if (ms & MonitoringInput) {
457 /* mix the disk signal into the input signal (already in bufs) */
458 mix_buffers_no_gain (output.data (), disk_buf.data (), nframes);
459 }
460 }
461 }
462
463 midi:
464
465 /* MIDI data handling */
466
467 const bool no_playlist_modification_pending = !(pending_overwrite () & PlaylistModified);
468
469 if (bufs.count ().n_midi ()) {
470 MidiBuffer& dst (bufs.get_midi (0));
471
472 if (run_must_resolve) {
473 resolve_tracker (dst, 0);
474 run_must_resolve = false;
475 }
476
477 if (!no_disk_output && !declick_in_progress () && (ms & MonitoringDisk) && !still_locating && no_playlist_modification_pending && speed) {
478 get_midi_playback (dst, start_sample, end_sample, ms, scratch_bufs, speed, disk_samples_to_consume);
479 }
480 }
481
482 /* decide if we need the butler */
483
484 if (!still_locating && no_playlist_modification_pending) {
485 bool butler_required = false;
486
487 if (speed < 0.0) {
488 playback_sample -= disk_samples_to_consume;
489 } else {
490 playback_sample += disk_samples_to_consume;
491 }
492
493 Location* loc = _loop_location;
494 if (loc) {
495 Evoral::Range<samplepos_t> loop_range (loc->start (), loc->end () - 1);
496 playback_sample = loop_range.squish (playback_sample);
497 }
498
499 if (_playlists[DataType::AUDIO]) {
500 if (!c->empty ()) {
501 if (_slaved) {
502 if (c->front ()->rbuf->write_space () >= c->front ()->rbuf->bufsize () / 2) {
503 DEBUG_TRACE (DEBUG::Butler, string_compose ("%1: slaved, write space = %2 of %3\n", name (), c->front ()->rbuf->write_space (), c->front ()->rbuf->bufsize ()));
504 butler_required = true;
505 }
506 } else {
507 if ((samplecnt_t)c->front ()->rbuf->write_space () >= _chunk_samples) {
508 DEBUG_TRACE (DEBUG::Butler, string_compose ("%1: write space = %2 of %3\n", name (), c->front ()->rbuf->write_space (),
509 _chunk_samples));
510 butler_required = true;
511 }
512 }
513 }
514 }
515
516 /* All of MIDI is in RAM, no need to call the butler unless we
517 * have to overwrite buffers because of a playlist change.
518 */
519
520 _need_butler = butler_required;
521 }
522
523 if (_need_butler) {
524 DEBUG_TRACE (DEBUG::Butler, string_compose ("%1 reader run, needs butler = %2\n", name (), _need_butler));
525 }
526 }
527
528 bool
declick_in_progress() const529 DiskReader::declick_in_progress () const
530 {
531 if (!_declick_enabled || (_session.exporting () && !_session.realtime_export ())) {
532 return false;
533 }
534 return _declick_amp.gain () != 0; // declick-out
535 }
536
537 void
configuration_changed()538 DiskReader::configuration_changed ()
539 {
540 boost::shared_ptr<ChannelList> c = channels.reader ();
541 if (!c->empty ()) {
542 ReaderChannelInfo* chaninfo = dynamic_cast<ReaderChannelInfo*> (c->front ());
543 if (!chaninfo->initialized) {
544 seek (_session.transport_sample(), true);
545 return;
546 }
547 }
548 _session.request_overwrite_buffer (_track.shared_ptr (), LoopDisabled);
549 }
550
551 bool
pending_overwrite() const552 DiskReader::pending_overwrite () const
553 {
554 return g_atomic_int_get (&_pending_overwrite) != 0;
555 }
556
557 void
set_pending_overwrite(OverwriteReason why)558 DiskReader::set_pending_overwrite (OverwriteReason why)
559 {
560 boost::shared_ptr<ChannelList> c = channels.reader ();
561
562 /* called from audio thread, so we can use the read ptr and playback sample as we wish */
563
564 if (!c->empty ()) {
565
566 if (c->size () > 1) {
567 /* Align newly added buffers.
568 *
569 * overwrite_sample and file_sample[] are are maintained
570 * per DiskReader, not per channel.
571 * ::refill_audio() and ::overwrite_existing_audio() expect
572 * that read-pointers and fill_level of all buffers are in sync.
573 */
574 ChannelList::iterator chan = c->begin ();
575 for (++chan; chan != c->end (); ++chan) {
576 ReaderChannelInfo* chaninfo = dynamic_cast<ReaderChannelInfo*> (*chan);
577 if (!chaninfo->initialized) {
578 (*chan)->rbuf->align_to (*(c->front ()->rbuf));
579 }
580 }
581 }
582
583 const samplecnt_t reserved_size = c->front ()->rbuf->reserved_size ();
584 const samplecnt_t bufsize = c->front ()->rbuf->bufsize ();
585
586 overwrite_offset = c->front ()->rbuf->read_ptr ();
587 overwrite_sample = playback_sample - reserved_size;
588
589 if (overwrite_offset > reserved_size) {
590 /*
591 * |----------------------------------------------------------------------|
592 * ^ ^
593 * RRRRRRRRRRRRRRRRoverwrite_offset (old read_ptr)
594 * |<- second ->|<------------------ first chunk ------------------------>|
595 *
596 * Fill the the end of the buffer ("first chunk"), above
597 */
598
599 overwrite_offset -= reserved_size;
600
601 } else {
602 /*
603 * |----------------------------------------------------------------------|
604 * RRRRRRRRE^ RRRRRRRRR
605 * overwrite_offset (old read_ptr)
606 * |< second chunk >|<first>|
607 *
608 * Fill the end of the buffer ("R1R1R1" aka "first" above)
609 */
610
611 overwrite_offset = bufsize - (reserved_size - overwrite_offset);
612 }
613 }
614
615 if (why & (LoopChanged | PlaylistModified | PlaylistChanged)) {
616 run_must_resolve = true;
617 }
618
619 while (true) {
620 OverwriteReason current = OverwriteReason (g_atomic_int_get (&_pending_overwrite));
621 OverwriteReason next = OverwriteReason (current | why);
622 if (g_atomic_int_compare_and_exchange (&_pending_overwrite, current, next)) {
623 break;
624 }
625 }
626 }
627
628 bool
overwrite_existing_audio()629 DiskReader::overwrite_existing_audio ()
630 {
631 /* This is a tricky and/or clever little method. Let's try to describe
632 * precisely what it does.
633 *
634 * Our goal is to completely overwrite the playback buffers for each
635 * audio channel with new data. The wrinkle is that we want to preserve
636 * the EXACT mapping between a given timeline position and buffer
637 * offset that existed when we requested an overwrite. That is, if the
638 * Nth position in the buffer contained the sample corresponding to
639 * timeline position T, then once this is complete that condition
640 * should still hold. The actual value of the sample (and even whether it
641 * corresponds to any actual material on disk - it may just be silence)
642 * may change, but this buffer_offset<->timeline_position mapping must
643 * remain constant.
644 *
645 * Why do this? There are many reasons. A trivial example is that the
646 * region gain level for one region has been changed, and the user
647 * should be able to hear the result.
648 *
649 * In ::set_pending_overwrite() (above) we stored a sample and a buffer
650 * offset. These corresponded to the next sample to be played and the
651 * buffer position holding that sample. We were able to determine this
652 * pair atomically because ::set_pending_overwrite() is called from
653 * within process context, and thus neither playback_sample nor the
654 * buffer read ptr can change while it runs. We computed the earliest
655 * sample/timeline position in the buffer (at the start of the reserved
656 * zone, if any) and its corresponding buffer offset.
657 *
658 * Here, we will refill the buffer, starting with the sample and buffer
659 * offset computed by ::set_pending_overwrite(). Typically this will
660 * take two reads from the playlist, because our read will be "split"
661 * by the end of the buffer (i.e. we fill from some mid-buffer point to
662 * the end, then fill from the start to the mid-buffer point, as is
663 * common with ring buffers).
664 *
665 * Note that the process thread may indeed access the buffer while we
666 * are doing this. There is a strong likelihood of colliding read/write
667 * between this thread (the butler) and a process thread. But we don't
668 * care: we know that the samples being read/written will correspond to
669 * the same timeline position, and that the user has just done
670 * something forcing us to update the value(s). Given that a Sample is
671 * currently (and likely forever) a floating point value, and that on
672 * many/most architectures, a store for a floating point value is
673 * non-atomic, there is some chance of the process read reading a
674 * sample value while it is being written. This could theoretically
675 * cause a brief glitch, but no more or less than any other
676 * "discontinuity" in the sample's value will.
677 *
678 * It goes without saying that this relies on being serialized within
679 * the butler thread with respect any other buffer write operation
680 * (e.g. via ::refill()). It should also be noted that it has no effect
681 * at all on the write-related members of the playback buffer - we
682 * simply replace the contents of the buffer.
683 */
684
685 boost::shared_ptr<ChannelList> c = channels.reader ();
686
687 if (c->empty ()) {
688 return true;
689 }
690
691 const bool reversed = !_session.transport_will_roll_forwards ();
692
693 sampleoffset_t chunk1_offset;
694 samplecnt_t chunk1_cnt;
695 samplecnt_t chunk2_cnt;
696
697 const samplecnt_t to_overwrite = c->front()->rbuf->overwritable_at (overwrite_offset);
698
699 chunk1_offset = overwrite_offset;
700 chunk1_cnt = min (c->front()->rbuf->bufsize() - overwrite_offset, to_overwrite);
701
702 /* note: because we are overwriting buffer contents but not moving the
703 * write/read pointers, we actually want to fill all the way to the
704 * write pointer (the value returned by PlaybackBuffer::overwritable_at().
705 *
706 * This differs from what happens during ::refill_audio() where we are
707 * careful not to allow the read pointer to catch the write pointer
708 * (that indicates an empty buffer)
709 */
710
711 if (chunk1_cnt == to_overwrite) {
712 chunk2_cnt = 0;
713 } else {
714 chunk2_cnt = to_overwrite - chunk1_cnt;
715 }
716
717 boost::scoped_array<Sample> mixdown_buffer (new Sample[to_overwrite]);
718 boost::scoped_array<float> gain_buffer (new float[to_overwrite]);
719 uint32_t n = 0;
720 bool ret = true;
721 samplepos_t start;
722
723 for (ChannelList::iterator chan = c->begin (); chan != c->end (); ++chan, ++n) {
724
725 Sample* buf = (*chan)->rbuf->buffer ();
726 ReaderChannelInfo* rci = dynamic_cast<ReaderChannelInfo*> (*chan);
727
728 /* Note that @param start is passed by reference and will be
729 * updated by the ::audio_read() call
730 */
731
732 start = overwrite_sample;
733
734 if (chunk1_cnt) {
735 if (audio_read (buf + chunk1_offset, mixdown_buffer.get (), gain_buffer.get (), start, chunk1_cnt, rci, n, reversed) != chunk1_cnt) {
736 error << string_compose (_("DiskReader %1: when overwriting(1), cannot read %2 from playlist at sample %3"), id (), chunk1_cnt, overwrite_sample) << endmsg;
737 ret = false;
738 continue;
739 }
740
741 }
742
743 if (chunk2_cnt) {
744 if (audio_read (buf, mixdown_buffer.get (), gain_buffer.get (), start, chunk2_cnt, rci, n, reversed) != chunk2_cnt) {
745 error << string_compose (_("DiskReader %1: when overwriting(2), cannot read %2 from playlist at sample %3"), id (), chunk2_cnt, overwrite_sample) << endmsg;
746 ret = false;
747 }
748 }
749
750 if (!rci->initialized) {
751 DEBUG_TRACE (DEBUG::DiskIO, string_compose ("Init ReaderChannel '%1' overwriting at: %2, avail: %3\n", name (), overwrite_sample, (*chan)->rbuf->read_space ()));
752 if ((*chan)->rbuf->read_space () > 0) {
753 rci->initialized = true;
754 }
755 }
756 }
757
758 file_sample[DataType::AUDIO] = start;
759
760 return ret;
761 }
762
763 bool
overwrite_existing_midi()764 DiskReader::overwrite_existing_midi ()
765 {
766 RTMidiBuffer* mbuf = rt_midibuffer ();
767
768 if (mbuf) {
769 MidiTrack* mt = dynamic_cast<MidiTrack*> (&_track);
770 MidiChannelFilter* filter = mt ? &mt->playback_filter () : 0;
771
772 PBD::Timing minsert;
773 minsert.start ();
774
775 midi_playlist ()->render (filter);
776
777 minsert.update ();
778 assert (midi_playlist ()->rendered ());
779 cerr << "Reading " << name () << " took " << minsert.elapsed () << " microseconds, final size = " << midi_playlist ()->rendered ()->size () << endl;
780 }
781
782 return true;
783 }
784
785 bool
overwrite_existing_buffers()786 DiskReader::overwrite_existing_buffers ()
787 {
788 /* called from butler thread */
789
790 DEBUG_TRACE (DEBUG::DiskIO, string_compose ("%1 overwriting existing buffers at %2 (because %3%4%5\n", owner ()->name (), overwrite_sample, std::hex, g_atomic_int_get (&_pending_overwrite), std::dec));
791
792 bool ret = true;
793
794 if (g_atomic_int_get (&_pending_overwrite) & (PlaylistModified | LoopDisabled | LoopChanged | PlaylistChanged)) {
795 if (_playlists[DataType::AUDIO] && !overwrite_existing_audio ()) {
796 ret = false;
797 }
798 }
799
800 if (g_atomic_int_get (&_pending_overwrite) & (PlaylistModified | PlaylistChanged)) {
801 if (_playlists[DataType::MIDI] && !overwrite_existing_midi ()) {
802 ret = false;
803 }
804 }
805
806 g_atomic_int_set (&_pending_overwrite, 0);
807
808 return ret;
809 }
810
811 int
seek(samplepos_t sample,bool complete_refill)812 DiskReader::seek (samplepos_t sample, bool complete_refill)
813 {
814 /* called via non_realtime_locate() from butler thread */
815
816 int ret = -1;
817
818 const bool read_reversed = !_session.transport_will_roll_forwards ();
819 const bool read_loop = (bool)_loop_location;
820
821 boost::shared_ptr<ChannelList> c = channels.reader ();
822
823 if (c->empty ()) {
824 return 0;
825 }
826
827 /* There are two possible shortcuts we can take that will completely
828 * skip reading from disk. However, they are invalid if we need to read
829 * data in the opposite direction than we did last time, or if our need
830 * for looped data has chaned since the last read. Both of these change
831 * the semantics of a read from disk, even if the position we are
832 * reading from is the same.
833 */
834
835 if ((_last_read_reversed.value_or (read_reversed) == read_reversed) && (_last_read_loop.value_or (read_loop) == read_loop)) {
836 if (sample == playback_sample && !complete_refill) {
837 return 0;
838 }
839
840 if (abs (sample - playback_sample) < (c->front ()->rbuf->reserved_size () / 6)) {
841 /* we're close enough. Note: this is a heuristic */
842 return 0;
843 }
844 }
845
846 g_atomic_int_set (&_pending_overwrite, 0);
847
848 DEBUG_TRACE (DEBUG::DiskIO, string_compose ("DiskReader::seek %1 %2 -> %3 refill=%4\n", owner ()->name ().c_str (), playback_sample, sample, complete_refill));
849
850 const samplecnt_t distance = sample - playback_sample;
851 if (!complete_refill && can_internal_playback_seek (distance)) {
852 internal_playback_seek (distance);
853 return 0;
854 }
855
856 for (ChannelList::iterator chan = c->begin (); chan != c->end (); ++chan) {
857 (*chan)->rbuf->reset ();
858 assert ((*chan)->rbuf->reserved_size () == 0);
859 }
860
861 /* move the intended read target, so that after the refill is done,
862 * the intended read target is "reservation" from the start of the
863 * playback buffer. Then increment the read ptr, so that we can
864 * potentially do an internal seek backwards of up "reservation"
865 * samples.
866 */
867
868 samplecnt_t shift = sample > c->front ()->rbuf->reservation_size () ? c->front ()->rbuf->reservation_size () : sample;
869
870 if (read_reversed) {
871 /* reading in reverse, so start at a later sample, and read
872 * "backwards" from there. */
873 shift = -shift;
874 }
875
876 /* start the read at an earlier position (or later if reversed) */
877
878 sample -= shift;
879
880 playback_sample = sample;
881 file_sample[DataType::AUDIO] = sample;
882 file_sample[DataType::MIDI] = sample;
883
884 if (complete_refill) {
885 /* call _do_refill() to refill the entire buffer, using
886 * the largest reads possible. */
887 while ((ret = do_refill_with_alloc (false, read_reversed)) > 0)
888 ;
889 } else {
890 /* call _do_refill() to refill just one chunk, and then return. */
891 ret = do_refill_with_alloc (true, read_reversed);
892 }
893
894 if (shift) {
895 /* now tell everyone where we really are, leaving the
896 * "reserved" data represented by "shift" available in the
897 * buffer for backwards-internal-seek
898 */
899
900 playback_sample += shift;
901
902 /* we always move the read-ptr forwards, since even when in
903 * reverse, the data is placed in the buffer in normal read
904 * (increment) order.
905 */
906
907 shift = abs (shift);
908
909 for (ChannelList::iterator chan = c->begin (); chan != c->end (); ++chan) {
910 (*chan)->rbuf->increment_read_ptr (shift);
911 }
912 }
913
914 return ret;
915 }
916
917 bool
can_internal_playback_seek(sampleoffset_t distance)918 DiskReader::can_internal_playback_seek (sampleoffset_t distance)
919 {
920 /* 1. Audio */
921
922 ChannelList::iterator chan;
923 boost::shared_ptr<ChannelList> c = channels.reader ();
924
925 for (chan = c->begin (); chan != c->end (); ++chan) {
926 if (!(*chan)->rbuf->can_seek (distance)) {
927 return false;
928 }
929 }
930
931 /* 2. MIDI can always seek any distance */
932
933 return true;
934 }
935
936 void
internal_playback_seek(sampleoffset_t distance)937 DiskReader::internal_playback_seek (sampleoffset_t distance)
938 {
939 if (distance == 0) {
940 return;
941 }
942
943 sampleoffset_t off = distance;
944
945 ChannelList::iterator chan;
946 boost::shared_ptr<ChannelList> c = channels.reader ();
947 for (chan = c->begin (); chan != c->end (); ++chan) {
948 if (distance < 0) {
949 off = 0 - (sampleoffset_t) (*chan)->rbuf->decrement_read_ptr (::llabs (distance));
950 } else {
951 off = (*chan)->rbuf->increment_read_ptr (distance);
952 }
953 }
954
955 playback_sample += off;
956 }
957
958 static void
swap_by_ptr(Sample * first,Sample * last)959 swap_by_ptr (Sample* first, Sample* last)
960 {
961 while (first < last) {
962 Sample tmp = *first;
963 *first++ = *last;
964 *last-- = tmp;
965 }
966 }
967
968 /** Read some data for 1 channel from our playlist into a buffer.
969 *
970 * @param sum_buf sample-containing buffer to write to. Must be contiguous.
971 * @param mixdown_buffer sample-containing buffer that will be used to mix layers
972 * @param gain_buffer ptr to a buffer used to hold any necessary gain (automation) data
973 * @param start Session sample to start reading from; updated to where we end up
974 * after the read. Global timeline position.
975 * @param cnt Count of samples to read.
976 * @param rci ptr to ReaderChannelInfo for the channel we're reading
977 * @param channel the number of the channel we're reading (0..N)
978 * @param reversed true if we are running backwards, otherwise false.
979 */
980
981 samplecnt_t
audio_read(Sample * sum_buffer,Sample * mixdown_buffer,float * gain_buffer,samplepos_t & start,samplecnt_t cnt,ReaderChannelInfo * rci,int channel,bool reversed)982 DiskReader::audio_read (Sample* sum_buffer,
983 Sample* mixdown_buffer,
984 float* gain_buffer,
985 samplepos_t& start,
986 samplecnt_t cnt,
987 ReaderChannelInfo* rci,
988 int channel,
989 bool reversed)
990 {
991 samplecnt_t this_read = 0;
992 bool reloop = false;
993 samplepos_t loop_end = 0;
994 samplepos_t loop_start = 0;
995 Location* loc = 0;
996 const samplecnt_t rcnt = cnt;
997
998 /* XXX we don't currently play loops in reverse. not sure why */
999
1000 if (!reversed) {
1001
1002 /* Make the use of a Location atomic for this read operation.
1003
1004 Note: Locations don't get deleted, so all we care about
1005 when I say "atomic" is that we are always pointing to
1006 the same one and using a start/length values obtained
1007 just once.
1008 */
1009
1010 if ((loc = _loop_location) != 0) {
1011 loop_start = loc->start ();
1012 loop_end = loc->end ();
1013
1014 /* Evoral::Range has inclusive range semantics. Ugh. Hence the -1 */
1015 const Evoral::Range<samplepos_t> loop_range (loop_start, loop_end - 1);
1016 start = loop_range.squish (start);
1017 }
1018
1019 } else {
1020
1021 start -= cnt;
1022 start = max (samplepos_t (0), start);
1023 }
1024
1025 /* We need this while loop in case we hit a loop boundary, in which case our read from
1026 * the playlist must be split into more than one section. */
1027
1028 while (cnt) {
1029 /* take any loop into account. we can't read past the end of the loop. */
1030
1031 if (loc && (loop_end - start < cnt)) {
1032 this_read = loop_end - start;
1033 reloop = true;
1034 } else {
1035 reloop = false;
1036 this_read = cnt;
1037 }
1038
1039 if (this_read == 0) {
1040 break;
1041 }
1042
1043 this_read = min (cnt, this_read);
1044
1045 /* note that the mixdown and gain buffers are purely for the
1046 * internal use of the playlist, and cannot be considered
1047 * useful after the return from AudioPlayback::read()
1048 */
1049
1050 if (audio_playlist ()->read (sum_buffer, mixdown_buffer, gain_buffer, start, this_read, channel) != this_read) {
1051 error << string_compose (_("DiskReader %1: cannot read %2 from playlist at sample %3"), id (), this_read, start) << endmsg;
1052 return 0;
1053 }
1054
1055 if (loc) {
1056 /* Looping: do something (maybe) about the loop boundaries */
1057
1058 switch (Config->get_loop_fade_choice ()) {
1059 case NoLoopFade:
1060 break;
1061 case BothLoopFade:
1062 loop_declick_in.run (sum_buffer, start, start + this_read);
1063 loop_declick_out.run (sum_buffer, start, start + this_read);
1064 break;
1065 case EndLoopFade:
1066 loop_declick_out.run (sum_buffer, start, start + this_read);
1067 break;
1068 case XFadeLoop:
1069 if (last_refill_loop_start != loc->start() || rci->pre_loop_buffer == 0) {
1070 setup_preloop_buffer ();
1071 last_refill_loop_start = loc->start();
1072 }
1073 maybe_xfade_loop (sum_buffer, start, start + this_read, rci);
1074 break;
1075 }
1076 }
1077
1078 if (reversed) {
1079 swap_by_ptr (sum_buffer, sum_buffer + this_read - 1);
1080
1081 } else {
1082 /* if we read to the end of the loop, go back to the beginning */
1083
1084 if (reloop) {
1085 start = loop_start;
1086 } else {
1087 start += this_read;
1088 }
1089 }
1090
1091 cnt -= this_read;
1092 sum_buffer += this_read;
1093 }
1094
1095 _last_read_reversed = reversed;
1096 _last_read_loop = (bool)loc;
1097
1098 return rcnt;
1099 }
1100
1101 int
do_refill()1102 DiskReader::do_refill ()
1103 {
1104 const bool reversed = !_session.transport_will_roll_forwards ();
1105 return refill (_sum_buffer, _mixdown_buffer, _gain_buffer, 0, reversed);
1106 }
1107
1108 int
do_refill_with_alloc(bool partial_fill,bool reversed)1109 DiskReader::do_refill_with_alloc (bool partial_fill, bool reversed)
1110 {
1111 /* We limit disk reads to at most 4MB chunks, which with floating point
1112 * samples would be 1M samples. But we might use 16 or 14 bit samples,
1113 * in which case 4MB is more samples than that. Therefore size this for
1114 * the smallest sample value .. 4MB = 2M samples (16 bit).
1115 */
1116
1117 boost::scoped_array<Sample> sum_buf (new Sample[2 * 1048576]);
1118 boost::scoped_array<Sample> mix_buf (new Sample[2 * 1048576]);
1119 boost::scoped_array<float> gain_buf (new float[2 * 1048576]);
1120
1121 return refill_audio (sum_buf.get (), mix_buf.get (), gain_buf.get (), (partial_fill ? _chunk_samples : 0), reversed);
1122 }
1123
1124 int
refill(Sample * sum_buffer,Sample * mixdown_buffer,float * gain_buffer,samplecnt_t fill_level,bool reversed)1125 DiskReader::refill (Sample* sum_buffer, Sample* mixdown_buffer, float* gain_buffer, samplecnt_t fill_level, bool reversed)
1126 {
1127 /* NOTE: Audio refill MUST come first so that in contexts where ONLY it
1128 * is called, _last_read_reversed is set correctly.
1129 */
1130
1131 if (refill_audio (sum_buffer, mixdown_buffer, gain_buffer, fill_level, reversed)) {
1132 return -1;
1133 }
1134
1135 if (rt_midibuffer () && (reversed != rt_midibuffer ()->reversed ())) {
1136 rt_midibuffer ()->reverse ();
1137 }
1138
1139 return 0;
1140 }
1141
1142 /** Get some more data from disk and put it in our channels' bufs,
1143 * if there is suitable space in them.
1144 *
1145 * If fill_level is non-zero, then we will refill the buffer so that there is
1146 * still at least fill_level samples of space left to be filled. This is used
1147 * after locates so that we do not need to wait to fill the entire buffer.
1148 *
1149 */
1150
1151 int
refill_audio(Sample * sum_buffer,Sample * mixdown_buffer,float * gain_buffer,samplecnt_t fill_level,bool reversed)1152 DiskReader::refill_audio (Sample* sum_buffer, Sample* mixdown_buffer, float* gain_buffer, samplecnt_t fill_level, bool reversed)
1153 {
1154 /* do not read from disk while session is marked as Loading, to avoid
1155 useless redundant I/O.
1156 */
1157
1158 if (_session.loading ()) {
1159 return 0;
1160 }
1161
1162 int32_t ret = 0;
1163 samplecnt_t zero_fill;
1164 uint32_t chan_n;
1165 ChannelList::iterator i;
1166 boost::shared_ptr<ChannelList> c = channels.reader ();
1167
1168 _last_read_reversed = reversed;
1169
1170 if (c->empty ()) {
1171 return 0;
1172 }
1173
1174 assert (mixdown_buffer);
1175 assert (gain_buffer);
1176
1177 samplecnt_t total_space = c->front ()->rbuf->write_space ();
1178
1179 if (total_space == 0) {
1180 DEBUG_TRACE (DEBUG::DiskIO, string_compose ("%1: no space to refill\n", name ()));
1181 /* nowhere to write to */
1182 return 0;
1183 }
1184
1185 if (fill_level) {
1186 if (fill_level < total_space) {
1187 total_space -= fill_level;
1188 } else {
1189 /* we can't do anything with it */
1190 fill_level = 0;
1191 }
1192 }
1193
1194 /* if we're running close to normal speed and there isn't enough
1195 * space to do disk_read_chunk_samples of I/O, then don't bother.
1196 *
1197 * at higher speeds, just do it because the sync between butler
1198 * and audio thread may not be good enough.
1199 *
1200 * Note: it is a design assumption that disk_read_chunk_samples is smaller
1201 * than the playback buffer size, so this check should never trip when
1202 * the playback buffer is empty.
1203 */
1204
1205 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 ()));
1206 if ((total_space < _chunk_samples) && fabs (_session.transport_speed ()) < 2.0f) {
1207 return 0;
1208 }
1209
1210 /* when slaved, don't try to get too close to the read pointer. this
1211 * leaves space for the buffer reversal to have something useful to
1212 * work with.
1213 */
1214
1215 if (_slaved && total_space < (samplecnt_t) (c->front ()->rbuf->bufsize () / 2)) {
1216 DEBUG_TRACE (DEBUG::DiskIO, string_compose ("%1: not enough to refill while slaved\n", this));
1217 return 0;
1218 }
1219
1220 samplepos_t fsa = file_sample[DataType::AUDIO];
1221
1222 if (reversed) {
1223 if (fsa == 0) {
1224 /* at start: nothing to do but fill with silence */
1225 for (chan_n = 0, i = c->begin (); i != c->end (); ++i, ++chan_n) {
1226 ChannelInfo* chan (*i);
1227 chan->rbuf->write_zero (chan->rbuf->write_space ());
1228 }
1229 return 0;
1230 }
1231
1232 if (fsa < total_space) {
1233 /* too close to the start: read what we can, and then zero fill the rest */
1234 zero_fill = total_space - fsa;
1235 total_space = fsa;
1236 } else {
1237 zero_fill = 0;
1238 }
1239
1240 } else {
1241 if (fsa == max_samplepos) {
1242 /* at end: nothing to do but fill with silence */
1243 for (chan_n = 0, i = c->begin (); i != c->end (); ++i, ++chan_n) {
1244 ChannelInfo* chan (*i);
1245 chan->rbuf->write_zero (chan->rbuf->write_space ());
1246 }
1247 return 0;
1248 }
1249
1250 if (fsa > max_samplepos - total_space) {
1251 /* to close to the end: read what we can, and zero fill the rest */
1252 zero_fill = total_space - (max_samplepos - fsa);
1253 total_space = max_samplepos - fsa;
1254
1255 } else {
1256 zero_fill = 0;
1257 }
1258 }
1259
1260 /* total_space is in samples. We want to optimize read sizes in various sizes using bytes */
1261 const size_t bits_per_sample = format_data_width (_session.config.get_native_file_data_format ());
1262 size_t total_bytes = total_space * bits_per_sample / 8;
1263
1264 /* chunk size range is 256kB to 4MB. Bigger is faster in terms of MB/sec, but bigger chunk size always takes longer */
1265 size_t byte_size_for_read = max ((size_t) (256 * 1024), min ((size_t) (4 * 1048576), total_bytes));
1266
1267 /* find nearest (lower) multiple of 16384 */
1268
1269 byte_size_for_read = (byte_size_for_read / 16384) * 16384;
1270
1271 /* now back to samples */
1272 samplecnt_t samples_to_read = byte_size_for_read / (bits_per_sample / 8);
1273
1274 DEBUG_TRACE (DEBUG::DiskIO, string_compose ("%1: will refill %2 channels with %3 samples\n", name (), c->size (), total_space));
1275
1276 samplepos_t file_sample_tmp = fsa;
1277
1278 #if 0
1279 int64_t before = g_get_monotonic_time ();
1280 int64_t elapsed;
1281 #endif
1282
1283 for (chan_n = 0, i = c->begin (); i != c->end (); ++i, ++chan_n) {
1284 ChannelInfo* chan (*i);
1285
1286 /* we want all channels to read from the same position, but
1287 * audio_read() will increment its position argument. So
1288 * reinitialize this for every channel.
1289 */
1290
1291 file_sample_tmp = fsa;
1292 samplecnt_t ts = total_space;
1293
1294 samplecnt_t to_read = min (ts, (samplecnt_t)chan->rbuf->write_space ());
1295 to_read = min (to_read, samples_to_read);
1296 assert (to_read >= 0);
1297
1298 // cerr << owner()->name() << " to-read: " << to_read << endl;
1299
1300 if (to_read) {
1301 ReaderChannelInfo* rci = dynamic_cast<ReaderChannelInfo*> (chan);
1302
1303 if (!_playlists[DataType::AUDIO]) {
1304 chan->rbuf->write_zero (to_read);
1305
1306 } else {
1307 samplecnt_t nread;
1308 if ((nread = audio_read (sum_buffer, mixdown_buffer, gain_buffer, file_sample_tmp, to_read, rci, chan_n, reversed)) != to_read) {
1309 error << string_compose (_("DiskReader %1: when refilling, cannot read %2 from playlist at sample %3"), name (), to_read, fsa) << endmsg;
1310 ret = -1;
1311 goto out;
1312 }
1313
1314 if (chan->rbuf->write (sum_buffer, nread) != nread) {
1315 error << string_compose (_("DiskReader %1: when refilling, cannot write %2 into buffer"), name (), nread) << endmsg;
1316 ret = -1;
1317 }
1318 }
1319 if (!rci->initialized) {
1320 DEBUG_TRACE (DEBUG::DiskIO, string_compose (" -- Init ReaderChannel '%1' read: %2 samples, at: %4, avail: %5\n", name (), to_read, file_sample_tmp , rci->rbuf->read_space ()));
1321 rci->initialized = true;
1322 }
1323 }
1324
1325 if (zero_fill) {
1326 /* not sure if action is needed,
1327 * we'll later hit the "to close to the end" case
1328 */
1329 //chan->rbuf->write_zero (zero_fill);
1330 }
1331 }
1332
1333 #if 0
1334 elapsed = g_get_monotonic_time () - before;
1335 cerr << '\t' << name() << ": bandwidth = " << (byte_size_for_read / 1048576.0) / (elapsed/1000000.0) << "MB/sec\n";
1336 #endif
1337
1338 file_sample[DataType::AUDIO] = file_sample_tmp;
1339 assert (file_sample[DataType::AUDIO] >= 0);
1340
1341 ret = ((total_space - samples_to_read) > _chunk_samples);
1342
1343 out:
1344 return ret;
1345 }
1346
1347 void
playlist_ranges_moved(list<Evoral::RangeMove<samplepos_t>> const & movements_samples,bool from_undo_or_shift)1348 DiskReader::playlist_ranges_moved (list<Evoral::RangeMove<samplepos_t> > const& movements_samples, bool from_undo_or_shift)
1349 {
1350 /* If we're coming from an undo, it will have handled
1351 * automation undo (it must, since automation-follows-regions
1352 * can lose automation data). Hence we can do nothing here.
1353 *
1354 * Likewise when shifting regions (insert/remove time)
1355 * automation is taken care of separately (busses with
1356 * automation have no disk-reader).
1357 */
1358
1359 if (from_undo_or_shift) {
1360 return;
1361 }
1362
1363 if (Config->get_automation_follows_regions () == false) {
1364 return;
1365 }
1366
1367 list<Evoral::RangeMove<double> > movements;
1368
1369 for (list<Evoral::RangeMove<samplepos_t> >::const_iterator i = movements_samples.begin ();
1370 i != movements_samples.end ();
1371 ++i) {
1372 movements.push_back (Evoral::RangeMove<double> (i->from, i->length, i->to));
1373 }
1374
1375 /* move panner automation */
1376 boost::shared_ptr<Pannable> pannable = _track.pannable ();
1377 Evoral::ControlSet::Controls& c (pannable->controls ());
1378
1379 for (Evoral::ControlSet::Controls::iterator ci = c.begin (); ci != c.end (); ++ci) {
1380 boost::shared_ptr<AutomationControl> ac = boost::dynamic_pointer_cast<AutomationControl> (ci->second);
1381 if (!ac) {
1382 continue;
1383 }
1384 boost::shared_ptr<AutomationList> alist = ac->alist ();
1385 if (!alist->size ()) {
1386 continue;
1387 }
1388 XMLNode& before = alist->get_state ();
1389 bool const things_moved = alist->move_ranges (movements);
1390 if (things_moved) {
1391 _session.add_command (new MementoCommand<AutomationList> (
1392 *alist.get (), &before, &alist->get_state ()));
1393 }
1394 }
1395 /* move processor automation */
1396 _track.foreach_processor (boost::bind (&DiskReader::move_processor_automation, this, _1, movements_samples));
1397 }
1398
1399 void
move_processor_automation(boost::weak_ptr<Processor> p,list<Evoral::RangeMove<samplepos_t>> const & movements_samples)1400 DiskReader::move_processor_automation (boost::weak_ptr<Processor> p, list<Evoral::RangeMove<samplepos_t> > const& movements_samples)
1401 {
1402 boost::shared_ptr<Processor> processor (p.lock ());
1403 if (!processor) {
1404 return;
1405 }
1406
1407 list<Evoral::RangeMove<double> > movements;
1408 for (list<Evoral::RangeMove<samplepos_t> >::const_iterator i = movements_samples.begin (); i != movements_samples.end (); ++i) {
1409 movements.push_back (Evoral::RangeMove<double> (i->from, i->length, i->to));
1410 }
1411
1412 set<Evoral::Parameter> const a = processor->what_can_be_automated ();
1413
1414 for (set<Evoral::Parameter>::const_iterator i = a.begin (); i != a.end (); ++i) {
1415 boost::shared_ptr<AutomationList> al = processor->automation_control (*i)->alist ();
1416 if (!al->size ()) {
1417 continue;
1418 }
1419 XMLNode& before = al->get_state ();
1420 bool const things_moved = al->move_ranges (movements);
1421 if (things_moved) {
1422 _session.add_command (
1423 new MementoCommand<AutomationList> (
1424 *al.get (), &before, &al->get_state ()));
1425 }
1426 }
1427 }
1428
1429 void
reset_tracker()1430 DiskReader::reset_tracker ()
1431 {
1432 _tracker.reset ();
1433 }
1434
1435 void
resolve_tracker(Evoral::EventSink<samplepos_t> & buffer,samplepos_t time)1436 DiskReader::resolve_tracker (Evoral::EventSink<samplepos_t>& buffer, samplepos_t time)
1437 {
1438 _tracker.resolve_notes (buffer, time);
1439 }
1440
1441 /** Writes playback events from playback_sample for nframes to dst, translating time stamps
1442 * so that an event at playback_sample has time = 0
1443 */
1444 void
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)1445 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)
1446 {
1447 RTMidiBuffer* rtmb = rt_midibuffer ();
1448
1449 if (!rtmb || (rtmb->size () == 0)) {
1450 /* no data to read, so do nothing */
1451 return;
1452 }
1453
1454 MidiBuffer* target;
1455
1456 if (ms & MonitoringInput) {
1457 /* data from disk needs to be *merged* not written into the
1458 * dst, because it may contain input data that we want to
1459 * monitor. Since RTMidiBuffer currently (Oct 2019) has no
1460 * suitable method, put the disk data into a scratch buffer and
1461 * then merge later.
1462 */
1463
1464 target = &scratch_bufs.get_midi (0);
1465 } else {
1466 /* No need to preserve the contents of the input buffer. But
1467 * Route::process_output_buffers() clears the buffer as-needed
1468 * so know we do not need to clear it.
1469 */
1470 target = &dst;
1471 }
1472
1473 if (!g_atomic_int_get (&_no_disk_output)) {
1474 const samplecnt_t nframes = abs (end_sample - start_sample);
1475
1476 if (ms & MonitoringDisk) {
1477 /* disk data needed */
1478
1479 Location* loc = _loop_location;
1480
1481 if (loc) {
1482 /* Evoral::Range has inclusive range semantics. Ugh. Hence the -1 */
1483 const Evoral::Range<samplepos_t> loop_range (loc->start (), loc->end () - 1);
1484 samplepos_t effective_start = start_sample;
1485 samplecnt_t cnt = nframes;
1486 sampleoffset_t offset = 0;
1487
1488 DEBUG_TRACE (DEBUG::MidiDiskIO, string_compose ("LOOP read, loop is %1..%2 range is %3..%4 nf %5\n", loc->start (), loc->end (), start_sample, end_sample, nframes));
1489
1490 do {
1491 samplepos_t effective_end;
1492
1493 effective_start = loop_range.squish (effective_start);
1494 effective_end = min (effective_start + cnt, loc->end ());
1495 assert (effective_end > effective_start);
1496
1497 const samplecnt_t this_read = effective_end - effective_start;
1498
1499 DEBUG_TRACE (DEBUG::MidiDiskIO, string_compose ("playback buffer LOOP read, from %1 to %2 (%3)\n", effective_start, effective_end, this_read));
1500
1501 size_t events_read = rtmb->read (*target, effective_start, effective_end, _tracker, offset);
1502 cnt -= this_read;
1503 effective_start += this_read;
1504 offset += this_read;
1505
1506 DEBUG_TRACE (DEBUG::MidiDiskIO, string_compose ("%1 MDS events LOOP read %2 cnt now %3\n", _name, events_read, cnt));
1507
1508 if (cnt) {
1509 /* We re going to have to read across the loop end. Resolve any notes the extend across the loop end.
1510 * Time is relative to start_sample.
1511 */
1512 DEBUG_TRACE (DEBUG::MidiDiskIO, string_compose ("read crosses loop end, resolve @ %1\n", effective_end - start_sample));
1513 _tracker.resolve_notes (*target, effective_end - start_sample);
1514 }
1515
1516 } while (cnt);
1517
1518 } else {
1519 DEBUG_TRACE (DEBUG::MidiDiskIO, string_compose ("playback buffer read, from %1 to %2 (%3)\n", start_sample, end_sample, nframes));
1520 size_t events_read = rtmb->read (*target, start_sample, end_sample, _tracker);
1521 DEBUG_TRACE (DEBUG::MidiDiskIO, string_compose ("%1 MDS events read %2 range %3 .. %4\n", _name, events_read, playback_sample, playback_sample + nframes));
1522 }
1523 }
1524
1525 if (ms & MonitoringInput) {
1526 /* merges data from disk (in "target", which is a scratch
1527 * buffer in this case) into the actual destination buffer
1528 * (which holds existing input data).
1529 */
1530 dst.merge_from (*target, nframes);
1531 }
1532 }
1533
1534 #if 0
1535 if (!target->empty ()) {
1536 cerr << "======== MIDI OUT ========\n";
1537 for (MidiBuffer::iterator i = target->begin(); i != target->end(); ++i) {
1538 const Evoral::Event<MidiBuffer::TimeType> ev (*i, false);
1539 cerr << "MIDI EVENT (from disk) @ " << ev.time();
1540 for (size_t xx = 0; xx < ev.size(); ++xx) {
1541 cerr << ' ' << hex << (int) ev.buffer()[xx];
1542 }
1543 cerr << dec << endl;
1544 }
1545 cerr << "----------------\n";
1546 }
1547 #endif
1548 }
1549 void
inc_no_disk_output()1550 DiskReader::inc_no_disk_output ()
1551 {
1552 g_atomic_int_inc (&_no_disk_output);
1553 }
1554
1555 void
dec_no_disk_output()1556 DiskReader::dec_no_disk_output ()
1557 {
1558 /* this is called unconditionally when things happen that ought to end
1559 * a period of "no disk output". It's OK for that to happen when there
1560 * was no corresponding call to ::inc_no_disk_output(), but we must
1561 * stop the value from becoming negative.
1562 */
1563
1564 do {
1565 gint v = g_atomic_int_get (&_no_disk_output);
1566 if (v > 0) {
1567 if (g_atomic_int_compare_and_exchange (&_no_disk_output, v, v - 1)) {
1568 break;
1569 }
1570 } else {
1571 break;
1572 }
1573 } while (true);
1574 }
1575
1576 /* min gain difference for de-click and loop-fadess
1577 * (-60dB difference to target)
1578 */
1579 #define GAIN_COEFF_DELTA (1e-5)
1580
DeclickAmp(samplecnt_t sample_rate)1581 DiskReader::DeclickAmp::DeclickAmp (samplecnt_t sample_rate)
1582 {
1583 _a = 800.f / (gain_t)sample_rate; // ~ 1/50Hz to fade by 40dB
1584 _l = -log1p (_a);
1585 _g = 0;
1586 }
1587
1588 void
apply_gain(AudioBuffer & buf,samplecnt_t n_samples,const float target,sampleoffset_t buffer_offset)1589 DiskReader::DeclickAmp::apply_gain (AudioBuffer& buf, samplecnt_t n_samples, const float target, sampleoffset_t buffer_offset)
1590 {
1591 if (n_samples == 0) {
1592 return;
1593 }
1594 float g = _g;
1595
1596 if (g == target) {
1597 assert (buffer_offset == 0);
1598 Amp::apply_simple_gain (buf, n_samples, target, 0);
1599 return;
1600 }
1601
1602 const float a = _a;
1603 Sample* const buffer = buf.data ();
1604
1605 const int max_nproc = 4;
1606 uint32_t remain = n_samples;
1607 uint32_t offset = buffer_offset;
1608
1609 while (remain > 0) {
1610 uint32_t n_proc = remain > max_nproc ? max_nproc : remain;
1611 for (uint32_t i = 0; i < n_proc; ++i) {
1612 buffer[offset + i] *= g;
1613 }
1614 #if 1
1615 g += a * (target - g);
1616 #else /* accurate exponential fade */
1617 if (n_proc == max_nproc) {
1618 g += a * (target - g);
1619 } else {
1620 g = target - (target - g) * expf (_l * n_proc / max_nproc);
1621 }
1622 #endif
1623 remain -= n_proc;
1624 offset += n_proc;
1625 }
1626
1627 if (fabsf (g - target) < GAIN_COEFF_DELTA) {
1628 _g = target;
1629 } else {
1630 _g = g;
1631 }
1632 }
1633
Declicker()1634 DiskReader::Declicker::Declicker ()
1635 : fade_start (0)
1636 , fade_end (0)
1637 , fade_length (0)
1638 , vec (0)
1639 {
1640 }
1641
~Declicker()1642 DiskReader::Declicker::~Declicker ()
1643 {
1644 delete[] vec;
1645 }
1646
1647 void
alloc(samplecnt_t sr,bool fadein,bool linear)1648 DiskReader::Declicker::alloc (samplecnt_t sr, bool fadein, bool linear)
1649 {
1650 delete[] vec;
1651 vec = new Sample[loop_fade_length];
1652
1653 if (linear) {
1654 if (fadein) {
1655 for (samplecnt_t n = 0; n < loop_fade_length; ++n) {
1656 vec[n] = n / (float) loop_fade_length;
1657 }
1658 } else {
1659 for (samplecnt_t n = 0; n < loop_fade_length; ++n) {
1660 vec[n] = 1.f - n / (float) loop_fade_length;
1661 }
1662 }
1663 fade_length = loop_fade_length - 1;
1664 return;
1665 }
1666
1667 /* Exponential fade */
1668
1669 const float a = 390.f / sr; // ~ 1/100Hz for 40dB
1670
1671 /* build a psuedo-exponential (linear-volume) shape for the fade */
1672
1673 samplecnt_t n;
1674
1675 if (fadein) {
1676 gain_t g = 0.0;
1677 for (n = 0; (n < loop_fade_length) && ((1.f - g) > GAIN_COEFF_DELTA); ++n) {
1678 vec[n] = g;
1679 g += a * (1.0 - g);
1680 }
1681 } else {
1682 gain_t g = 1.0;
1683 for (n = 0; (n < loop_fade_length) && (g > GAIN_COEFF_DELTA); ++n) {
1684 vec[n] = g;
1685 g += a * -g;
1686 }
1687 }
1688
1689 assert (n > 0 && n <= loop_fade_length);
1690
1691 fade_length = n - 1;
1692
1693 /* Fill remaining fader-buffer with the target value.
1694 *
1695 * This is needed for loop x-fade. Due to float precision near 1.0, fade-in length
1696 * is can be one or two samples shorter than fade-out length (depending on sample-rate).
1697 * Summing the fade-in and fade-out curve over the complete fade-range (fade-out,
1698 * as done by DiskReader::maybe_xfade_loop) must yield 1.0 +/- GAIN_COEFF_DELTA.
1699 */
1700 for (; n < loop_fade_length; ++n) {
1701 vec[n] = fadein ? 1.f : 0.f;
1702 }
1703 }
1704
1705 void
reset(samplepos_t loop_start,samplepos_t loop_end,bool fadein,samplecnt_t sr)1706 DiskReader::Declicker::reset (samplepos_t loop_start, samplepos_t loop_end, bool fadein, samplecnt_t sr)
1707 {
1708 if (loop_start == loop_end) {
1709 fade_start = 0;
1710 fade_end = 0;
1711 return;
1712 }
1713
1714 /* adjust the position of the fade (this is absolute (global) timeline units) */
1715
1716 if (fadein) {
1717 fade_start = loop_start;
1718 fade_end = loop_start + fade_length;
1719 } else {
1720 fade_start = loop_end - fade_length;
1721 fade_end = loop_end;
1722 }
1723 }
1724
1725 void
run(Sample * buf,samplepos_t read_start,samplepos_t read_end)1726 DiskReader::Declicker::run (Sample* buf, samplepos_t read_start, samplepos_t read_end)
1727 {
1728 samplecnt_t n; /* how many samples to process */
1729 sampleoffset_t bo; /* offset into buffer */
1730 sampleoffset_t vo; /* offset into gain vector */
1731
1732 if (fade_start == fade_end) {
1733 return;
1734 }
1735
1736 /* Determine how the read range overlaps with the fade range, so we can determine
1737 * which part of the fade gain vector to apply to which part of the buffer.
1738 *
1739 * see also DiskReader::maybe_xfade_loop()
1740 */
1741
1742 switch (Evoral::coverage (fade_start, fade_end, read_start, read_end)) {
1743 case Evoral::OverlapInternal:
1744 /* note: start and end points cannot coincide (see evoral/Range.h)
1745 *
1746 * read range is entirely within fade range
1747 */
1748 bo = 0;
1749 vo = read_start - fade_start;
1750 n = read_end - read_start;
1751 break;
1752
1753 case Evoral::OverlapExternal:
1754 /* read range extends on either side of fade range
1755 *
1756 * External allows coincidental start & end points, so check for that
1757 */
1758 if (fade_start == read_start && fade_end == read_end) {
1759 /* fade entire read ... this is SO unlikely ! */
1760 bo = 0;
1761 vo = 0;
1762 n = fade_end - fade_start;
1763 } else {
1764 bo = fade_start - read_start;
1765 vo = 0;
1766 n = fade_end - fade_start;
1767 }
1768 break;
1769
1770 case Evoral::OverlapStart:
1771 /* read range starts before and ends within fade or at same end as fade */
1772 n = fade_end - read_start;
1773 vo = 0;
1774 bo = fade_start - read_start;
1775 break;
1776
1777 case Evoral::OverlapEnd:
1778 /* read range starts within fade range, but possibly at it's end, so check */
1779 if (read_start == fade_end) {
1780 /* nothing to do */
1781 return;
1782 }
1783 bo = 0;
1784 vo = read_start - fade_start;
1785 n = fade_end - read_start;
1786 break;
1787
1788 case Evoral::OverlapNone:
1789 /* no overlap ... nothing to do */
1790 return;
1791 }
1792
1793 Sample* b = &buf[bo];
1794 gain_t* g = &vec[vo];
1795
1796 for (sampleoffset_t i = 0; i < n; ++i) {
1797 b[i] *= g[i];
1798 }
1799 }
1800
1801 void
maybe_xfade_loop(Sample * buf,samplepos_t read_start,samplepos_t read_end,ReaderChannelInfo * chan)1802 DiskReader::maybe_xfade_loop (Sample* buf, samplepos_t read_start, samplepos_t read_end, ReaderChannelInfo* chan)
1803 {
1804 samplecnt_t n; /* how many samples to process */
1805 sampleoffset_t bo; /* offset into buffer */
1806 sampleoffset_t vo; /* offset into gain vector */
1807
1808 const samplepos_t fade_start = loop_declick_out.fade_start;
1809 const samplepos_t fade_end = loop_declick_out.fade_end;
1810
1811 if (fade_start == fade_end) {
1812 return;
1813 }
1814
1815 /* Determine how the read range overlaps with the fade range, so we can determine
1816 * which part of the fade gain vector to apply to which part of the buffer.
1817 *
1818 * see also DiskReader::Declicker::run()
1819 */
1820
1821 switch (Evoral::coverage (fade_start, fade_end, read_start, read_end)) {
1822 case Evoral::OverlapInternal:
1823 /* note: start and end points cannot coincide (see evoral/Range.h)
1824 *
1825 * read range is entirely within fade range
1826 */
1827 bo = 0;
1828 vo = read_start - fade_start;
1829 n = read_end - read_start;
1830 break;
1831
1832 case Evoral::OverlapExternal:
1833 /* read range extends on either side of fade range
1834 *
1835 * External allows coincidental start & end points, so check for that
1836 */
1837 if (fade_start == read_start && fade_end == read_end) {
1838 /* fade entire read ... this is SO unlikely ! */
1839 bo = 0;
1840 vo = 0;
1841 n = fade_end - fade_start;
1842 } else {
1843 bo = fade_start - read_start;
1844 vo = 0;
1845 n = fade_end - fade_start;
1846 }
1847 break;
1848
1849 case Evoral::OverlapStart:
1850 /* read range starts before and ends within fade or at same end as fade */
1851 n = read_end - fade_start;
1852 vo = 0;
1853 bo = fade_start - read_start;
1854 break;
1855
1856 case Evoral::OverlapEnd:
1857 /* read range starts within fade range, but possibly at it's end, so check */
1858 if (read_start == fade_end) {
1859 /* nothing to do */
1860 return;
1861 }
1862 bo = 0;
1863 vo = read_start - fade_start;
1864 n = fade_end - read_start;
1865 break;
1866
1867 case Evoral::OverlapNone:
1868 /* no overlap ... nothing to do */
1869 return;
1870 }
1871
1872 Sample* b = &buf[bo]; /* data to be faded out */
1873 Sample* sbuf = &chan->pre_loop_buffer[vo]; /* pre-loop (maybe silence) to be faded in */
1874 gain_t* og = &loop_declick_out.vec[vo]; /* fade out gain vector */
1875 gain_t* ig = &loop_declick_in.vec[vo]; /* fade in gain vector */
1876
1877 for (sampleoffset_t i = 0; i < n; ++i) {
1878 b[i] = (b[i] * og[i]) + (sbuf[i] * ig[i]);
1879 }
1880 }
1881
1882 RTMidiBuffer*
rt_midibuffer()1883 DiskReader::rt_midibuffer ()
1884 {
1885 boost::shared_ptr<Playlist> pl = _playlists[DataType::MIDI];
1886
1887 if (!pl) {
1888 return 0;
1889 }
1890
1891 boost::shared_ptr<MidiPlaylist> mpl = boost::dynamic_pointer_cast<MidiPlaylist> (pl);
1892
1893 if (!mpl) {
1894 /* error, but whatever ... */
1895 return 0;
1896 }
1897
1898 return mpl->rendered ();
1899 }
1900
1901 void
alloc_loop_declick(samplecnt_t sr)1902 DiskReader::alloc_loop_declick (samplecnt_t sr)
1903 {
1904 loop_fade_length = lrintf (ceil (-log (GAIN_COEFF_DELTA / 2.) / (390. / sr)));
1905 loop_declick_in.alloc (sr, true, Config->get_loop_fade_choice () == XFadeLoop);
1906 loop_declick_out.alloc (sr, false, Config->get_loop_fade_choice () == XFadeLoop);
1907 }
1908
1909 #undef GAIN_COEFF_DELTA
1910
1911 void
reset_loop_declick(Location * loc,samplecnt_t sr)1912 DiskReader::reset_loop_declick (Location* loc, samplecnt_t sr)
1913 {
1914 if (loc) {
1915 loop_declick_in.reset (loc->start (), loc->end (), true, sr);
1916 loop_declick_out.reset (loc->start (), loc->end (), false, sr);
1917 } else {
1918 loop_declick_in.reset (0, 0, true, sr);
1919 loop_declick_out.reset (0, 0, false, sr);
1920 }
1921 }
1922
1923 void
set_loop(Location * loc)1924 DiskReader::set_loop (Location* loc)
1925 {
1926 Processor::set_loop (loc);
1927
1928 if (!loc) {
1929 return;
1930 }
1931 }
1932
1933 void
setup_preloop_buffer()1934 DiskReader::setup_preloop_buffer ()
1935 {
1936 if (!_loop_location) {
1937 return;
1938 }
1939
1940 boost::shared_ptr<ChannelList> c = channels.reader ();
1941
1942 if (c->empty () || !_playlists[DataType::AUDIO]) {
1943 return;
1944 }
1945
1946 Location* loc = _loop_location;
1947 boost::scoped_array<Sample> mix_buf (new Sample[loop_fade_length]);
1948 boost::scoped_array<Sample> gain_buf (new Sample[loop_fade_length]);
1949
1950 uint32_t channel = 0;
1951
1952 for (ChannelList::iterator chan = c->begin (); chan != c->end (); ++chan, ++channel) {
1953 ReaderChannelInfo* rci = dynamic_cast<ReaderChannelInfo*> (*chan);
1954
1955 rci->resize_preloop (loop_fade_length);
1956
1957 if (loc->start () > loop_fade_length) {
1958 audio_playlist ()->read (rci->pre_loop_buffer, mix_buf.get (), gain_buf.get (), loc->start () - loop_declick_out.fade_length, loop_declick_out.fade_length, channel);
1959 } else {
1960 memset (rci->pre_loop_buffer, 0, sizeof (Sample) * loop_fade_length);
1961 }
1962 }
1963 }
1964