merge AudioDiskstream playback code into DiskReader
[ardour.git] / libs / ardour / disk_reader.cc
1 /*
2     Copyright (C) 2009-2016 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "pbd/i18n.h"
21
22 #include "ardour/audioplaylist.h"
23 #include "ardour/audio_buffer.h"
24 #include "ardour/butler.h"
25 #include "ardour/debug.h"
26 #include "ardour/disk_reader.h"
27 #include "ardour/playlist.h"
28 #include "ardour/playlist_factory.h"
29 #include "ardour/session.h"
30 #include "ardour/session_playlists.h"
31
32 using namespace ARDOUR;
33 using namespace PBD;
34 using namespace std;
35
36 ARDOUR::framecnt_t DiskReader::_chunk_frames = default_chunk_frames ();
37
38 DiskReader::DiskReader (Session& s, string const & str, DiskIOProcessor::Flag f)
39         : DiskIOProcessor (s, str, f)
40         , _roll_delay (0)
41         , overwrite_frame (0)
42         , overwrite_offset (0)
43         , _pending_overwrite (false)
44         , overwrite_queued (false)
45         , file_frame (0)
46         , playback_sample (0)
47         , _monitoring_choice (MonitorDisk)
48         , channels (new ChannelList)
49 {
50 }
51
52 DiskReader::~DiskReader ()
53 {
54         DEBUG_TRACE (DEBUG::Destruction, string_compose ("DiskReader %1 deleted\n", _name));
55
56         if (_playlist) {
57                 _playlist->release ();
58         }
59
60         {
61                 RCUWriter<ChannelList> writer (channels);
62                 boost::shared_ptr<ChannelList> c = writer.get_copy();
63
64                 for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
65                         delete *chan;
66                 }
67
68                 c->clear();
69         }
70
71         channels.flush ();
72 }
73
74 void
75 DiskReader::allocate_working_buffers()
76 {
77         /* with varifill buffer refilling, we compute the read size in bytes (to optimize
78            for disk i/o bandwidth) and then convert back into samples. These buffers
79            need to reflect the maximum size we could use, which is 4MB reads, or 2M samples
80            using 16 bit samples.
81         */
82         _mixdown_buffer       = new Sample[2*1048576];
83         _gain_buffer          = new gain_t[2*1048576];
84 }
85
86 void
87 DiskReader::free_working_buffers()
88 {
89         delete [] _mixdown_buffer;
90         delete [] _gain_buffer;
91         _mixdown_buffer       = 0;
92         _gain_buffer          = 0;
93 }
94
95 framecnt_t
96 DiskReader::default_chunk_frames()
97 {
98         return 65536;
99 }
100
101 bool
102 DiskReader::set_name (string const & str)
103 {
104         if (_name != str) {
105                 assert (_playlist);
106                 _playlist->set_name (str);
107                 SessionObject::set_name(str);
108         }
109
110         return true;
111 }
112
113 void
114 DiskReader::set_roll_delay (ARDOUR::framecnt_t nframes)
115 {
116         _roll_delay = nframes;
117 }
118
119 int
120 DiskReader::set_state (const XMLNode& node, int version)
121 {
122         XMLProperty const * prop;
123
124         if (DiskIOProcessor::set_state (node, version)) {
125                 return -1;
126         }
127
128         if ((prop = node.property ("playlist")) == 0) {
129                 return -1;
130         }
131
132         if (find_and_use_playlist (prop->value())) {
133                 return -1;
134         }
135
136         return 0;
137 }
138
139 /* Processor interface */
140
141 bool
142 DiskReader::configure_io (ChanCount in, ChanCount out)
143 {
144         return true;
145 }
146
147 bool
148 DiskReader::can_support_io_configuration (const ChanCount& in, ChanCount& out)
149 {
150         return true;
151 }
152
153 void
154 DiskReader::realtime_handle_transport_stopped ()
155 {
156 }
157
158 void
159 DiskReader::realtime_locate ()
160 {
161 }
162
163 int
164 DiskReader::add_channel_to (boost::shared_ptr<ChannelList> c, uint32_t how_many)
165 {
166         while (how_many--) {
167                 c->push_back (new ChannelInfo(
168                                       _session.butler()->audio_diskstream_playback_buffer_size(),
169                                       speed_buffer_size, wrap_buffer_size));
170                 interpolation.add_channel_to (
171                         _session.butler()->audio_diskstream_playback_buffer_size(),
172                         speed_buffer_size);
173         }
174
175         _n_channels.set (DataType::AUDIO, c->size());
176
177         return 0;
178 }
179
180 int
181 DiskReader::add_channel (uint32_t how_many)
182 {
183         RCUWriter<ChannelList> writer (channels);
184         boost::shared_ptr<ChannelList> c = writer.get_copy();
185
186         return add_channel_to (c, how_many);
187 }
188
189 int
190 DiskReader::remove_channel_from (boost::shared_ptr<ChannelList> c, uint32_t how_many)
191 {
192         while (how_many-- && !c->empty()) {
193                 delete c->back();
194                 c->pop_back();
195                 interpolation.remove_channel_from ();
196         }
197
198         _n_channels.set(DataType::AUDIO, c->size());
199
200         return 0;
201 }
202
203 int
204 DiskReader::remove_channel (uint32_t how_many)
205 {
206         RCUWriter<ChannelList> writer (channels);
207         boost::shared_ptr<ChannelList> c = writer.get_copy();
208
209         return remove_channel_from (c, how_many);
210 }
211
212 float
213 DiskReader::buffer_load () const
214 {
215         boost::shared_ptr<ChannelList> c = channels.reader();
216
217         if (c->empty ()) {
218                 return 1.0;
219         }
220
221         return (float) ((double) c->front()->buf->read_space()/
222                            (double) c->front()->buf->bufsize());
223 }
224
225 void
226 DiskReader::adjust_buffering ()
227 {
228         boost::shared_ptr<ChannelList> c = channels.reader();
229
230         for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
231                 (*chan)->resize (_session.butler()->audio_diskstream_playback_buffer_size());
232         }
233 }
234
235 DiskReader::ChannelInfo::ChannelInfo (framecnt_t bufsize, framecnt_t speed_size, framecnt_t wrap_size)
236 {
237         current_buffer = 0;
238
239         speed_buffer = new Sample[speed_size];
240         wrap_buffer = new Sample[wrap_size];
241
242         buf = new RingBufferNPT<Sample> (bufsize);
243
244         /* touch the ringbuffer buffer, which will cause
245            them to be mapped into locked physical RAM if
246            we're running with mlockall(). this doesn't do
247            much if we're not.
248         */
249
250         memset (buf->buffer(), 0, sizeof (Sample) * buf->bufsize());
251 }
252
253 void
254 DiskReader::ChannelInfo::resize (framecnt_t bufsize)
255 {
256         delete buf;
257         buf = new RingBufferNPT<Sample> (bufsize);
258         memset (buf->buffer(), 0, sizeof (Sample) * buf->bufsize());
259 }
260
261 DiskReader::ChannelInfo::~ChannelInfo ()
262 {
263         delete [] speed_buffer;
264         speed_buffer = 0;
265
266         delete [] wrap_buffer;
267         wrap_buffer = 0;
268
269         delete buf;
270         buf = 0;
271 }
272
273 int
274 DiskReader::set_block_size (pframes_t /*nframes*/)
275 {
276         if (_session.get_block_size() > speed_buffer_size) {
277                 speed_buffer_size = _session.get_block_size();
278                 boost::shared_ptr<ChannelList> c = channels.reader();
279
280                 for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
281                         delete [] (*chan)->speed_buffer;
282                         (*chan)->speed_buffer = new Sample[speed_buffer_size];
283                 }
284         }
285         allocate_temporary_buffers ();
286         return 0;
287 }
288
289 void
290 DiskReader::allocate_temporary_buffers ()
291 {
292         /* make sure the wrap buffer is at least large enough to deal
293            with the speeds up to 1.2, to allow for micro-variation
294            when slaving to MTC, Timecode etc.
295         */
296
297         double const sp = max (fabs (_actual_speed), 1.2);
298         framecnt_t required_wrap_size = (framecnt_t) ceil (_session.get_block_size() * sp) + 2;
299
300         if (required_wrap_size > wrap_buffer_size) {
301
302                 boost::shared_ptr<ChannelList> c = channels.reader();
303
304                 for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
305                         if ((*chan)->wrap_buffer) {
306                                 delete [] (*chan)->wrap_buffer;
307                         }
308                         (*chan)->wrap_buffer = new Sample[required_wrap_size];
309                 }
310
311                 wrap_buffer_size = required_wrap_size;
312         }
313 }
314
315
316 void
317 DiskReader::playlist_changed (const PropertyChange&)
318 {
319         playlist_modified ();
320 }
321
322 void
323 DiskReader::playlist_modified ()
324 {
325         if (!i_am_the_modifier && !overwrite_queued) {
326                 // !!!! _session.request_overwrite_buffer (this);
327                 overwrite_queued = true;
328         }
329 }
330
331 void
332 DiskReader::playlist_deleted (boost::weak_ptr<Playlist> wpl)
333 {
334         boost::shared_ptr<Playlist> pl (wpl.lock());
335
336         if (pl == _playlist) {
337
338                 /* this catches an ordering issue with session destruction. playlists
339                    are destroyed before disk readers. we have to invalidate any handles
340                    we have to the playlist.
341                 */
342
343                 if (_playlist) {
344                         _playlist.reset ();
345                 }
346         }
347 }
348
349 int
350 DiskReader::use_playlist (boost::shared_ptr<Playlist> playlist)
351 {
352         if (!playlist) {
353                 return 0;
354         }
355
356         bool prior_playlist = false;
357
358         {
359                 Glib::Threads::Mutex::Lock lm (state_lock);
360
361                 if (playlist == _playlist) {
362                         return 0;
363                 }
364
365                 playlist_connections.drop_connections ();
366
367                 if (_playlist) {
368                         _playlist->release();
369                         prior_playlist = true;
370                 }
371
372                 _playlist = playlist;
373                 _playlist->use();
374
375                 _playlist->ContentsChanged.connect_same_thread (playlist_connections, boost::bind (&DiskReader::playlist_modified, this));
376                 _playlist->LayeringChanged.connect_same_thread (playlist_connections, boost::bind (&DiskReader::playlist_modified, this));
377                 _playlist->DropReferences.connect_same_thread (playlist_connections, boost::bind (&DiskReader::playlist_deleted, this, boost::weak_ptr<Playlist>(_playlist)));
378                 _playlist->RangesMoved.connect_same_thread (playlist_connections, boost::bind (&DiskReader::playlist_ranges_moved, this, _1, _2));
379         }
380
381         /* don't do this if we've already asked for it *or* if we are setting up
382            the diskstream for the very first time - the input changed handling will
383            take care of the buffer refill.
384         */
385
386         if (!overwrite_queued && prior_playlist) {
387                 // !!! _session.request_overwrite_buffer (this);
388                 overwrite_queued = true;
389         }
390
391         PlaylistChanged (); /* EMIT SIGNAL */
392         _session.set_dirty ();
393
394         return 0;
395 }
396
397 int
398 DiskReader::find_and_use_playlist (const string& name)
399 {
400         boost::shared_ptr<AudioPlaylist> playlist;
401
402         if ((playlist = boost::dynamic_pointer_cast<AudioPlaylist> (_session.playlists->by_name (name))) == 0) {
403                 playlist = boost::dynamic_pointer_cast<AudioPlaylist> (PlaylistFactory::create (DataType::AUDIO, _session, name));
404         }
405
406         if (!playlist) {
407                 error << string_compose(_("DiskReader: Playlist \"%1\" isn't an audio playlist"), name) << endmsg;
408                 return -1;
409         }
410
411         return use_playlist (playlist);
412 }
413
414 int
415 DiskReader::use_new_playlist ()
416 {
417         string newname;
418         boost::shared_ptr<AudioPlaylist> playlist;
419
420         if (_playlist) {
421                 newname = Playlist::bump_name (_playlist->name(), _session);
422         } else {
423                 newname = Playlist::bump_name (_name, _session);
424         }
425
426         if ((playlist = boost::dynamic_pointer_cast<AudioPlaylist> (PlaylistFactory::create (DataType::AUDIO, _session, newname, hidden()))) != 0) {
427
428                 return use_playlist (playlist);
429
430         } else {
431                 return -1;
432         }
433 }
434
435 int
436 DiskReader::use_copy_playlist ()
437 {
438         assert(audio_playlist());
439
440         if (_playlist == 0) {
441                 error << string_compose(_("DiskReader %1: there is no existing playlist to make a copy of!"), _name) << endmsg;
442                 return -1;
443         }
444
445         string newname;
446         boost::shared_ptr<AudioPlaylist> playlist;
447
448         newname = Playlist::bump_name (_playlist->name(), _session);
449
450         if ((playlist = boost::dynamic_pointer_cast<AudioPlaylist>(PlaylistFactory::create (audio_playlist(), newname))) != 0) {
451                 playlist->reset_shares();
452                 return use_playlist (playlist);
453         } else {
454                 return -1;
455         }
456 }
457
458
459 /** Do some record stuff [not described in this comment!]
460  *
461  *  Also:
462  *    - Setup playback_distance with the nframes, or nframes adjusted
463  *      for current varispeed, if appropriate.
464  *    - Setup current_buffer in each ChannelInfo to point to data
465  *      that someone can read playback_distance worth of data from.
466  */
467 void
468 DiskReader::run (BufferSet& bufs, framepos_t start_frame, framepos_t end_frame,
469                  double speed, pframes_t nframes, bool result_required)
470 /*
471         int
472         DiskReader::process (BufferSet& bufs, framepos_t transport_frame, pframes_t nframes,
473         framecnt_t& playback_distance, bool need_disk_signal)
474 */
475 {
476         uint32_t n;
477         boost::shared_ptr<ChannelList> c = channels.reader();
478         ChannelList::iterator chan;
479         framecnt_t playback_distance = 0;
480
481         Glib::Threads::Mutex::Lock sm (state_lock, Glib::Threads::TRY_LOCK);
482
483         if (!sm.locked()) {
484                 return;
485         }
486
487         for (chan = c->begin(); chan != c->end(); ++chan) {
488                 (*chan)->current_buffer = 0;
489         }
490
491         if (result_required || _monitoring_choice == MonitorDisk || _monitoring_choice == MonitorCue) {
492
493                 /* we're doing playback */
494
495                 framecnt_t necessary_samples;
496
497                 if (_actual_speed != 1.0) {
498                         necessary_samples = (framecnt_t) ceil ((nframes * fabs (_actual_speed))) + 2;
499                 } else {
500                         necessary_samples = nframes;
501                 }
502
503                 for (chan = c->begin(); chan != c->end(); ++chan) {
504                         (*chan)->buf->get_read_vector (&(*chan)->read_vector);
505                 }
506
507                 n = 0;
508
509                 /* Setup current_buffer in each ChannelInfo to point to data that someone
510                    can read necessary_samples (== nframes at a transport speed of 1) worth of data
511                    from right now.
512                 */
513
514                 for (chan = c->begin(); chan != c->end(); ++chan, ++n) {
515
516                         ChannelInfo* chaninfo (*chan);
517
518                         if (necessary_samples <= (framecnt_t) chaninfo->read_vector.len[0]) {
519                                 /* There are enough samples in the first part of the ringbuffer */
520                                 chaninfo->current_buffer = chaninfo->read_vector.buf[0];
521
522                         } else {
523                                 framecnt_t total = chaninfo->read_vector.len[0] + chaninfo->read_vector.len[1];
524
525                                 if (necessary_samples > total) {
526                                         cerr << _name << " Need " << necessary_samples << " total = " << total << endl;
527                                         cerr << "underrun for " << _name << endl;
528                                         DEBUG_TRACE (DEBUG::Butler, string_compose ("%1 underrun in %2, total space = %3\n",
529                                                                                     DEBUG_THREAD_SELF, name(), total));
530                                         Underrun ();
531                                         return;
532
533                                 } else {
534
535                                         /* We have enough samples, but not in one lump.  Coalesce the two parts
536                                            into one in wrap_buffer in our ChannelInfo, and specify that
537                                            as our current_buffer.
538                                         */
539
540                                         assert(wrap_buffer_size >= necessary_samples);
541
542                                         /* Copy buf[0] from buf */
543                                         memcpy ((char *) chaninfo->wrap_buffer,
544                                                         chaninfo->read_vector.buf[0],
545                                                         chaninfo->read_vector.len[0] * sizeof (Sample));
546
547                                         /* Copy buf[1] from buf */
548                                         memcpy (chaninfo->wrap_buffer + chaninfo->read_vector.len[0],
549                                                         chaninfo->read_vector.buf[1],
550                                                         (necessary_samples - chaninfo->read_vector.len[0])
551                                                                         * sizeof (Sample));
552
553                                         chaninfo->current_buffer = chaninfo->wrap_buffer;
554                                 }
555                         }
556                 }
557
558                 if (_actual_speed != 1.0f && _actual_speed != -1.0f) {
559
560                         interpolation.set_speed (_target_speed);
561
562                         int channel = 0;
563                         for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan, ++channel) {
564                                 ChannelInfo* chaninfo (*chan);
565
566                                 playback_distance = interpolation.interpolate (
567                                         channel, nframes, chaninfo->current_buffer, chaninfo->speed_buffer);
568
569                                 chaninfo->current_buffer = chaninfo->speed_buffer;
570                         }
571
572                 } else {
573                         playback_distance = nframes;
574                 }
575
576                 _speed = _target_speed;
577         }
578
579         if (result_required || _monitoring_choice == MonitorDisk || _monitoring_choice == MonitorCue) {
580
581                 /* copy data over to buffer set */
582
583                 size_t n_buffers = bufs.count().n_audio();
584                 size_t n_chans = c->size();
585                 gain_t scaling = 1.0f;
586
587                 if (n_chans > n_buffers) {
588                         scaling = ((float) n_buffers)/n_chans;
589                 }
590
591                 for (n = 0, chan = c->begin(); chan != c->end(); ++chan, ++n) {
592
593                         AudioBuffer& buf (bufs.get_audio (n%n_buffers));
594                         ChannelInfo* chaninfo (*chan);
595
596                         if (n < n_chans) {
597                                 if (scaling != 1.0f) {
598                                         buf.read_from_with_gain (chaninfo->current_buffer, nframes, scaling);
599                                 } else {
600                                         buf.read_from (chaninfo->current_buffer, nframes);
601                                 }
602                         } else {
603                                 if (scaling != 1.0f) {
604                                         buf.accumulate_with_gain_from (chaninfo->current_buffer, nframes, scaling);
605                                 } else {
606                                         buf.accumulate_from (chaninfo->current_buffer, nframes);
607                                 }
608                         }
609                 }
610
611                 /* leave the MIDI count alone */
612                 ChanCount cnt (DataType::AUDIO, n_chans);
613                 cnt.set (DataType::MIDI, bufs.count().n_midi());
614                 bufs.set_count (cnt);
615
616                 /* extra buffers will already be silent, so leave them alone */
617         }
618
619         bool need_butler = false;
620
621         if (_actual_speed < 0.0) {
622                 playback_sample -= playback_distance;
623         } else {
624                 playback_sample += playback_distance;
625         }
626
627         for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
628                 (*chan)->buf->increment_read_ptr (playback_distance);
629         }
630
631         if (!c->empty()) {
632                 if (_slaved) {
633                         need_butler = c->front()->buf->write_space() >= c->front()->buf->bufsize() / 2;
634                 } else {
635                         need_butler = (framecnt_t) c->front()->buf->write_space() >= _chunk_frames;
636                 }
637         }
638
639         //return need_butler;
640 }
641
642 frameoffset_t
643 DiskReader::calculate_playback_distance (pframes_t nframes)
644 {
645         frameoffset_t playback_distance = nframes;
646
647         if (_actual_speed != 1.0f && _actual_speed != -1.0f) {
648                 interpolation.set_speed (_target_speed);
649                 boost::shared_ptr<ChannelList> c = channels.reader();
650                 int channel = 0;
651                 for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan, ++channel) {
652                         playback_distance = interpolation.interpolate (channel, nframes, NULL, NULL);
653                 }
654         } else {
655                 playback_distance = nframes;
656         }
657
658         if (_actual_speed < 0.0) {
659                 return -playback_distance;
660         } else {
661                 return playback_distance;
662         }
663 }
664
665 void
666 DiskReader::set_pending_overwrite (bool yn)
667 {
668         /* called from audio thread, so we can use the read ptr and playback sample as we wish */
669
670         _pending_overwrite = yn;
671
672         overwrite_frame = playback_sample;
673
674         boost::shared_ptr<ChannelList> c = channels.reader ();
675         if (!c->empty ()) {
676                 overwrite_offset = c->front()->buf->get_read_ptr();
677         }
678 }
679
680 int
681 DiskReader::overwrite_existing_buffers ()
682 {
683         boost::shared_ptr<ChannelList> c = channels.reader();
684         if (c->empty ()) {
685                 _pending_overwrite = false;
686                 return 0;
687         }
688
689         Sample* mixdown_buffer;
690         float* gain_buffer;
691         int ret = -1;
692         bool reversed = (_visible_speed * _session.transport_speed()) < 0.0f;
693
694         overwrite_queued = false;
695
696         /* assume all are the same size */
697         framecnt_t size = c->front()->buf->bufsize();
698
699         mixdown_buffer = new Sample[size];
700         gain_buffer = new float[size];
701
702         /* reduce size so that we can fill the buffer correctly (ringbuffers
703            can only handle size-1, otherwise they appear to be empty)
704         */
705         size--;
706
707         uint32_t n=0;
708         framepos_t start;
709
710         for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan, ++n) {
711
712                 start = overwrite_frame;
713                 framecnt_t cnt = size;
714
715                 /* to fill the buffer without resetting the playback sample, we need to
716                    do it one or two chunks (normally two).
717
718                    |----------------------------------------------------------------------|
719
720                                        ^
721                                        overwrite_offset
722                     |<- second chunk->||<----------------- first chunk ------------------>|
723
724                 */
725
726                 framecnt_t to_read = size - overwrite_offset;
727
728                 if (read ((*chan)->buf->buffer() + overwrite_offset, mixdown_buffer, gain_buffer, start, to_read, n, reversed)) {
729                         error << string_compose(_("DiskReader %1: when refilling, cannot read %2 from playlist at frame %3"),
730                                                 id(), size, playback_sample) << endmsg;
731                         goto out;
732                 }
733
734                 if (cnt > to_read) {
735
736                         cnt -= to_read;
737
738                         if (read ((*chan)->buf->buffer(), mixdown_buffer, gain_buffer, start, cnt, n, reversed)) {
739                                 error << string_compose(_("DiskReader %1: when refilling, cannot read %2 from playlist at frame %3"),
740                                                         id(), size, playback_sample) << endmsg;
741                                 goto out;
742                         }
743                 }
744         }
745
746         ret = 0;
747
748   out:
749         _pending_overwrite = false;
750         delete [] gain_buffer;
751         delete [] mixdown_buffer;
752         return ret;
753 }
754
755 void
756 DiskReader::non_realtime_locate (framepos_t location)
757 {
758         /* now refill channel buffers */
759
760         if (speed() != 1.0f || speed() != -1.0f) {
761                 seek ((framepos_t) (location * (double) speed()), true);
762         } else {
763                 seek (location, true);
764         }
765 }
766
767 int
768 DiskReader::seek (framepos_t frame, bool complete_refill)
769 {
770         uint32_t n;
771         int ret = -1;
772         ChannelList::iterator chan;
773         boost::shared_ptr<ChannelList> c = channels.reader();
774
775         Glib::Threads::Mutex::Lock lm (state_lock);
776
777         for (n = 0, chan = c->begin(); chan != c->end(); ++chan, ++n) {
778                 (*chan)->buf->reset ();
779         }
780
781         playback_sample = frame;
782         file_frame = frame;
783
784         if (complete_refill) {
785                 /* call _do_refill() to refill the entire buffer, using
786                    the largest reads possible.
787                 */
788                 while ((ret = do_refill_with_alloc (false)) > 0) ;
789         } else {
790                 /* call _do_refill() to refill just one chunk, and then
791                    return.
792                 */
793                 ret = do_refill_with_alloc (true);
794         }
795
796         return ret;
797 }
798
799 int
800 DiskReader::can_internal_playback_seek (framecnt_t distance)
801 {
802         ChannelList::iterator chan;
803         boost::shared_ptr<ChannelList> c = channels.reader();
804
805         for (chan = c->begin(); chan != c->end(); ++chan) {
806                 if ((*chan)->buf->read_space() < (size_t) distance) {
807                         return false;
808                 }
809         }
810         return true;
811 }
812
813 int
814 DiskReader::internal_playback_seek (framecnt_t distance)
815 {
816         ChannelList::iterator chan;
817         boost::shared_ptr<ChannelList> c = channels.reader();
818
819         for (chan = c->begin(); chan != c->end(); ++chan) {
820                 (*chan)->buf->increment_read_ptr (::llabs(distance));
821         }
822
823         playback_sample += distance;
824
825         return 0;
826 }
827
828 static
829 void swap_by_ptr (Sample *first, Sample *last)
830 {
831         while (first < last) {
832                 Sample tmp = *first;
833                 *first++ = *last;
834                 *last-- = tmp;
835         }
836 }
837
838 /** Read some data for 1 channel from our playlist into a buffer.
839  *  @param buf Buffer to write to.
840  *  @param start Session frame to start reading from; updated to where we end up
841  *         after the read.
842  *  @param cnt Count of samples to read.
843  *  @param reversed true if we are running backwards, otherwise false.
844  */
845 int
846 DiskReader::read (Sample* buf, Sample* mixdown_buffer, float* gain_buffer,
847                   framepos_t& start, framecnt_t cnt,
848                   int channel, bool reversed)
849 {
850         framecnt_t this_read = 0;
851         bool reloop = false;
852         framepos_t loop_end = 0;
853         framepos_t loop_start = 0;
854         framecnt_t offset = 0;
855         Location *loc = 0;
856
857         /* XXX we don't currently play loops in reverse. not sure why */
858
859         if (!reversed) {
860
861                 framecnt_t loop_length = 0;
862
863                 /* Make the use of a Location atomic for this read operation.
864
865                    Note: Locations don't get deleted, so all we care about
866                    when I say "atomic" is that we are always pointing to
867                    the same one and using a start/length values obtained
868                    just once.
869                 */
870
871                 if ((loc = loop_location) != 0) {
872                         loop_start = loc->start();
873                         loop_end = loc->end();
874                         loop_length = loop_end - loop_start;
875                 }
876
877                 /* if we are looping, ensure that the first frame we read is at the correct
878                    position within the loop.
879                 */
880
881                 if (loc && start >= loop_end) {
882                         start = loop_start + ((start - loop_start) % loop_length);
883                 }
884
885         }
886
887         if (reversed) {
888                 start -= cnt;
889         }
890
891         /* We need this while loop in case we hit a loop boundary, in which case our read from
892            the playlist must be split into more than one section.
893         */
894
895         while (cnt) {
896
897                 /* take any loop into account. we can't read past the end of the loop. */
898
899                 if (loc && (loop_end - start < cnt)) {
900                         this_read = loop_end - start;
901                         reloop = true;
902                 } else {
903                         reloop = false;
904                         this_read = cnt;
905                 }
906
907                 if (this_read == 0) {
908                         break;
909                 }
910
911                 this_read = min(cnt,this_read);
912
913                 if (audio_playlist()->read (buf+offset, mixdown_buffer, gain_buffer, start, this_read, channel) != this_read) {
914                         error << string_compose(_("DiskReader %1: cannot read %2 from playlist at frame %3"), id(), this_read,
915                                          start) << endmsg;
916                         return -1;
917                 }
918
919                 if (reversed) {
920
921                         swap_by_ptr (buf, buf + this_read - 1);
922
923                 } else {
924
925                         /* if we read to the end of the loop, go back to the beginning */
926
927                         if (reloop) {
928                                 start = loop_start;
929                         } else {
930                                 start += this_read;
931                         }
932                 }
933
934                 cnt -= this_read;
935                 offset += this_read;
936         }
937
938         return 0;
939 }
940
941 int
942 DiskReader::_do_refill_with_alloc (bool partial_fill)
943 {
944         /* We limit disk reads to at most 4MB chunks, which with floating point
945            samples would be 1M samples. But we might use 16 or 14 bit samples,
946            in which case 4MB is more samples than that. Therefore size this for
947            the smallest sample value .. 4MB = 2M samples (16 bit).
948         */
949
950         Sample* mix_buf  = new Sample[2*1048576];
951         float*  gain_buf = new float[2*1048576];
952
953         int ret = _do_refill (mix_buf, gain_buf, (partial_fill ? _chunk_frames : 0));
954
955         delete [] mix_buf;
956         delete [] gain_buf;
957
958         return ret;
959 }
960
961 /** Get some more data from disk and put it in our channels' bufs,
962  *  if there is suitable space in them.
963  *
964  * If fill_level is non-zero, then we will refill the buffer so that there is
965  * still at least fill_level samples of space left to be filled. This is used
966  * after locates so that we do not need to wait to fill the entire buffer.
967  *
968  */
969
970 int
971 DiskReader::_do_refill (Sample* mixdown_buffer, float* gain_buffer, framecnt_t fill_level)
972 {
973         if (_session.state_of_the_state() & Session::Loading) {
974                 return 0;
975         }
976
977         int32_t ret = 0;
978         framecnt_t to_read;
979         RingBufferNPT<Sample>::rw_vector vector;
980         bool const reversed = (_visible_speed * _session.transport_speed()) < 0.0f;
981         framecnt_t total_space;
982         framecnt_t zero_fill;
983         uint32_t chan_n;
984         ChannelList::iterator i;
985         boost::shared_ptr<ChannelList> c = channels.reader();
986         framecnt_t ts;
987
988         /* do not read from disk while session is marked as Loading, to avoid
989            useless redundant I/O.
990         */
991
992         if (c->empty()) {
993                 return 0;
994         }
995
996         assert(mixdown_buffer);
997         assert(gain_buffer);
998
999         vector.buf[0] = 0;
1000         vector.len[0] = 0;
1001         vector.buf[1] = 0;
1002         vector.len[1] = 0;
1003
1004         c->front()->buf->get_write_vector (&vector);
1005
1006         if ((total_space = vector.len[0] + vector.len[1]) == 0) {
1007                 /* nowhere to write to */
1008                 return 0;
1009         }
1010
1011         if (fill_level) {
1012                 if (fill_level < total_space) {
1013                         total_space -= fill_level;
1014                 } else {
1015                         /* we can't do anything with it */
1016                         fill_level = 0;
1017                 }
1018         }
1019
1020         /* if we're running close to normal speed and there isn't enough
1021            space to do disk_read_chunk_frames of I/O, then don't bother.
1022
1023            at higher speeds, just do it because the sync between butler
1024            and audio thread may not be good enough.
1025
1026            Note: it is a design assumption that disk_read_chunk_frames is smaller
1027            than the playback buffer size, so this check should never trip when
1028            the playback buffer is empty.
1029         */
1030
1031         if ((total_space < _chunk_frames) && fabs (_actual_speed) < 2.0f) {
1032                 return 0;
1033         }
1034
1035         /* when slaved, don't try to get too close to the read pointer. this
1036            leaves space for the buffer reversal to have something useful to
1037            work with.
1038         */
1039
1040         if (_slaved && total_space < (framecnt_t) (c->front()->buf->bufsize() / 2)) {
1041                 return 0;
1042         }
1043
1044         if (reversed) {
1045
1046                 if (file_frame == 0) {
1047
1048                         /* at start: nothing to do but fill with silence */
1049
1050                         for (chan_n = 0, i = c->begin(); i != c->end(); ++i, ++chan_n) {
1051
1052                                 ChannelInfo* chan (*i);
1053                                 chan->buf->get_write_vector (&vector);
1054                                 memset (vector.buf[0], 0, sizeof(Sample) * vector.len[0]);
1055                                 if (vector.len[1]) {
1056                                         memset (vector.buf[1], 0, sizeof(Sample) * vector.len[1]);
1057                                 }
1058                                 chan->buf->increment_write_ptr (vector.len[0] + vector.len[1]);
1059                         }
1060                         return 0;
1061                 }
1062
1063                 if (file_frame < total_space) {
1064
1065                         /* too close to the start: read what we can,
1066                            and then zero fill the rest
1067                         */
1068
1069                         zero_fill = total_space - file_frame;
1070                         total_space = file_frame;
1071
1072                 } else {
1073
1074                         zero_fill = 0;
1075                 }
1076
1077         } else {
1078
1079                 if (file_frame == max_framepos) {
1080
1081                         /* at end: nothing to do but fill with silence */
1082
1083                         for (chan_n = 0, i = c->begin(); i != c->end(); ++i, ++chan_n) {
1084
1085                                 ChannelInfo* chan (*i);
1086                                 chan->buf->get_write_vector (&vector);
1087                                 memset (vector.buf[0], 0, sizeof(Sample) * vector.len[0]);
1088                                 if (vector.len[1]) {
1089                                         memset (vector.buf[1], 0, sizeof(Sample) * vector.len[1]);
1090                                 }
1091                                 chan->buf->increment_write_ptr (vector.len[0] + vector.len[1]);
1092                         }
1093                         return 0;
1094                 }
1095
1096                 if (file_frame > max_framepos - total_space) {
1097
1098                         /* to close to the end: read what we can, and zero fill the rest */
1099
1100                         zero_fill = total_space - (max_framepos - file_frame);
1101                         total_space = max_framepos - file_frame;
1102
1103                 } else {
1104                         zero_fill = 0;
1105                 }
1106         }
1107
1108         framepos_t file_frame_tmp = 0;
1109
1110         /* total_space is in samples. We want to optimize read sizes in various sizes using bytes */
1111
1112         const size_t bits_per_sample = format_data_width (_session.config.get_native_file_data_format());
1113         size_t total_bytes = total_space * bits_per_sample / 8;
1114
1115         /* chunk size range is 256kB to 4MB. Bigger is faster in terms of MB/sec, but bigger chunk size always takes longer
1116          */
1117         size_t byte_size_for_read = max ((size_t) (256 * 1024), min ((size_t) (4 * 1048576), total_bytes));
1118
1119         /* find nearest (lower) multiple of 16384 */
1120
1121         byte_size_for_read = (byte_size_for_read / 16384) * 16384;
1122
1123         /* now back to samples */
1124
1125         framecnt_t samples_to_read = byte_size_for_read / (bits_per_sample / 8);
1126
1127         //cerr << name() << " will read " << byte_size_for_read << " out of total bytes " << total_bytes << " in buffer of "
1128         // << c->front()->buf->bufsize() * bits_per_sample / 8 << " bps = " << bits_per_sample << endl;
1129         // cerr << name () << " read samples = " << samples_to_read << " out of total space " << total_space << " in buffer of " << c->front()->buf->bufsize() << " samples\n";
1130
1131         // uint64_t before = g_get_monotonic_time ();
1132         // uint64_t elapsed;
1133
1134         for (chan_n = 0, i = c->begin(); i != c->end(); ++i, ++chan_n) {
1135
1136                 ChannelInfo* chan (*i);
1137                 Sample* buf1;
1138                 Sample* buf2;
1139                 framecnt_t len1, len2;
1140
1141                 chan->buf->get_write_vector (&vector);
1142
1143                 if ((framecnt_t) vector.len[0] > samples_to_read) {
1144
1145                         /* we're not going to fill the first chunk, so certainly do not bother with the
1146                            other part. it won't be connected with the part we do fill, as in:
1147
1148                            .... => writable space
1149                            ++++ => readable space
1150                            ^^^^ => 1 x disk_read_chunk_frames that would be filled
1151
1152                            |......|+++++++++++++|...............................|
1153                            buf1                buf0
1154                                                 ^^^^^^^^^^^^^^^
1155
1156
1157                            So, just pretend that the buf1 part isn't there.
1158
1159                         */
1160
1161                         vector.buf[1] = 0;
1162                         vector.len[1] = 0;
1163
1164                 }
1165
1166                 ts = total_space;
1167                 file_frame_tmp = file_frame;
1168
1169                 buf1 = vector.buf[0];
1170                 len1 = vector.len[0];
1171                 buf2 = vector.buf[1];
1172                 len2 = vector.len[1];
1173
1174                 to_read = min (ts, len1);
1175                 to_read = min (to_read, (framecnt_t) samples_to_read);
1176
1177                 assert (to_read >= 0);
1178
1179                 if (to_read) {
1180
1181                         if (read (buf1, mixdown_buffer, gain_buffer, file_frame_tmp, to_read, chan_n, reversed)) {
1182                                 ret = -1;
1183                                 goto out;
1184                         }
1185
1186                         chan->buf->increment_write_ptr (to_read);
1187                         ts -= to_read;
1188                 }
1189
1190                 to_read = min (ts, len2);
1191
1192                 if (to_read) {
1193
1194                         /* we read all of vector.len[0], but it wasn't the
1195                            entire samples_to_read of data, so read some or
1196                            all of vector.len[1] as well.
1197                         */
1198
1199                         if (read (buf2, mixdown_buffer, gain_buffer, file_frame_tmp, to_read, chan_n, reversed)) {
1200                                 ret = -1;
1201                                 goto out;
1202                         }
1203
1204                         chan->buf->increment_write_ptr (to_read);
1205                 }
1206
1207                 if (zero_fill) {
1208                         /* XXX: do something */
1209                 }
1210
1211         }
1212
1213         // elapsed = g_get_monotonic_time () - before;
1214         // cerr << "\tbandwidth = " << (byte_size_for_read / 1048576.0) / (elapsed/1000000.0) << "MB/sec\n";
1215
1216         file_frame = file_frame_tmp;
1217         assert (file_frame >= 0);
1218
1219         ret = ((total_space - samples_to_read) > _chunk_frames);
1220
1221         c->front()->buf->get_write_vector (&vector);
1222
1223   out:
1224         return ret;
1225 }