2 Copyright (C) 2009-2016 Paul Davis
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.
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.
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.
20 #include <boost/smart_ptr/scoped_ptr.hpp>
22 #include "pbd/enumwriter.h"
23 #include "pbd/memento_command.h"
25 #include "ardour/audioengine.h"
26 #include "ardour/audioplaylist.h"
27 #include "ardour/audio_buffer.h"
28 #include "ardour/butler.h"
29 #include "ardour/debug.h"
30 #include "ardour/disk_reader.h"
31 #include "ardour/midi_ring_buffer.h"
32 #include "ardour/midi_playlist.h"
33 #include "ardour/midi_track.h"
34 #include "ardour/pannable.h"
35 #include "ardour/playlist.h"
36 #include "ardour/playlist_factory.h"
37 #include "ardour/session.h"
38 #include "ardour/session_playlists.h"
42 using namespace ARDOUR;
46 ARDOUR::samplecnt_t DiskReader::_chunk_samples = default_chunk_samples ();
47 PBD::Signal0<void> DiskReader::Underrun;
48 Sample* DiskReader::_mixdown_buffer = 0;
49 gain_t* DiskReader::_gain_buffer = 0;
50 samplecnt_t DiskReader::midi_readahead = 4096;
51 bool DiskReader::_no_disk_output = false;
53 DiskReader::DiskReader (Session& s, string const & str, DiskIOProcessor::Flag f)
54 : DiskIOProcessor (s, str, f)
55 , overwrite_sample (0)
56 , overwrite_offset (0)
57 , _pending_overwrite (false)
58 , overwrite_queued (false)
60 file_sample[DataType::AUDIO] = 0;
61 file_sample[DataType::MIDI] = 0;
64 DiskReader::~DiskReader ()
66 DEBUG_TRACE (DEBUG::Destruction, string_compose ("DiskReader %1 @ %2 deleted\n", _name, this));
68 for (uint32_t n = 0; n < DataType::num_types; ++n) {
70 _playlists[n]->release ();
75 RCUWriter<ChannelList> writer (channels);
76 boost::shared_ptr<ChannelList> c = writer.get_copy();
78 for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
91 DiskReader::allocate_working_buffers()
93 /* with varifill buffer refilling, we compute the read size in bytes (to optimize
94 for disk i/o bandwidth) and then convert back into samples. These buffers
95 need to reflect the maximum size we could use, which is 4MB reads, or 2M samples
98 _mixdown_buffer = new Sample[2*1048576];
99 _gain_buffer = new gain_t[2*1048576];
103 DiskReader::free_working_buffers()
105 delete [] _mixdown_buffer;
106 delete [] _gain_buffer;
112 DiskReader::default_chunk_samples()
118 DiskReader::set_name (string const & str)
120 string my_name = X_("player:");
123 if (_name != my_name) {
124 SessionObject::set_name (my_name);
133 XMLNode& node (DiskIOProcessor::state ());
134 node.set_property(X_("type"), X_("diskreader"));
139 DiskReader::set_state (const XMLNode& node, int version)
141 if (DiskIOProcessor::set_state (node, version)) {
149 DiskReader::realtime_handle_transport_stopped ()
154 DiskReader::realtime_locate ()
159 DiskReader::buffer_load () const
161 /* Note: for MIDI it's not trivial to differentiate the following two cases:
163 1. The playback buffer is empty because the system has run out of time to fill it.
164 2. The playback buffer is empty because there is no more data on the playlist.
166 If we use a simple buffer load computation, we will report that the MIDI diskstream
167 cannot keep up when #2 happens, when in fact it can. Since MIDI data rates
168 are so low compared to audio, just use the audio value here.
171 boost::shared_ptr<ChannelList> c = channels.reader();
174 /* no channels, so no buffers, so completely full and ready to playback, sir! */
178 PBD::RingBufferNPT<Sample> * b = c->front()->buf;
179 return (float) ((double) b->read_space() / (double) b->bufsize());
183 DiskReader::adjust_buffering ()
185 boost::shared_ptr<ChannelList> c = channels.reader();
187 for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
188 (*chan)->resize (_session.butler()->audio_diskstream_playback_buffer_size());
193 DiskReader::playlist_changed (const PropertyChange&)
195 playlist_modified ();
199 DiskReader::playlist_modified ()
201 if (!i_am_the_modifier && !overwrite_queued) {
202 _session.request_overwrite_buffer (_route);
203 overwrite_queued = true;
208 DiskReader::use_playlist (DataType dt, boost::shared_ptr<Playlist> playlist)
210 bool prior_playlist = false;
212 if (_playlists[dt]) {
213 prior_playlist = true;
216 if (DiskIOProcessor::use_playlist (dt, playlist)) {
220 /* don't do this if we've already asked for it *or* if we are setting up
221 the diskstream for the very first time - the input changed handling will
222 take care of the buffer refill.
225 if (!overwrite_queued && (prior_playlist || _session.loading())) {
226 _session.request_overwrite_buffer (_route);
227 overwrite_queued = true;
234 DiskReader::run (BufferSet& bufs, samplepos_t start_sample, samplepos_t end_sample,
235 double speed, pframes_t nframes, bool result_required)
238 boost::shared_ptr<ChannelList> c = channels.reader();
239 ChannelList::iterator chan;
240 sampleoffset_t disk_samples_to_consume;
241 MonitorState ms = _route->monitoring_state ();
244 if (!_pending_active) {
249 if (_pending_active) {
256 if ((speed == 0.0) && (ms == MonitoringDisk)) {
257 /* no channels, or stopped. Don't accidentally pass any data
258 * from disk into our outputs (e.g. via interpolation)
263 BufferSet& scratch_bufs (_session.get_scratch_buffers (bufs.count()));
264 const bool still_locating = _session.global_locate_pending();
267 /* do nothing with audio */
271 if (speed != 1.0f && speed != -1.0f) {
272 interpolation.set_speed (speed);
273 disk_samples_to_consume = interpolation.distance (nframes);
275 disk_samples_to_consume = -disk_samples_to_consume;
278 disk_samples_to_consume = nframes;
282 if (!result_required || ((ms & MonitoringDisk) == 0) || still_locating || _no_disk_output) {
284 /* no need for actual disk data, just advance read pointer and return */
286 if (!still_locating || _no_disk_output) {
287 for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
288 (*chan)->buf->increment_read_ptr (disk_samples_to_consume);
292 /* if monitoring disk but locating put silence in the buffers */
294 if ((_no_disk_output || still_locating) && (ms == MonitoringDisk)) {
295 bufs.silence (nframes, 0);
300 /* we need audio data from disk */
302 size_t n_buffers = bufs.count().n_audio();
303 size_t n_chans = c->size();
306 if (n_chans > n_buffers) {
307 scaling = ((float) n_buffers)/n_chans;
312 for (n = 0, chan = c->begin(); chan != c->end(); ++chan, ++n) {
314 ChannelInfo* chaninfo (*chan);
315 AudioBuffer& output (bufs.get_audio (n%n_buffers));
316 Sample* disk_signal = 0; /* assignment not really needed but it keeps the compiler quiet and helps track bugs */
318 if (ms & MonitoringInput) {
319 /* put disk stream in scratch buffer, blend at end */
320 disk_signal = scratch_bufs.get_audio(n).data ();
322 /* no input stream needed, just overwrite buffers */
323 disk_signal = output.data ();
327 if (start_sample < playback_sample) {
328 cerr << owner()->name() << " SS = " << start_sample << " PS = " << playback_sample << endl;
331 } else if (speed < 0) {
332 if (playback_sample < start_sample) {
333 cerr << owner()->name() << " SS = " << start_sample << " PS = " << playback_sample << " REVERSE" << endl;
338 if ((speed > 0) && (start_sample != playback_sample)) {
339 cerr << owner()->name() << " playback not aligned, jump ahead " << (start_sample - playback_sample) << endl;
341 if (can_internal_playback_seek (start_sample - playback_sample)) {
342 internal_playback_seek (start_sample - playback_sample);
344 cerr << owner()->name() << " playback not possible: ss = " << start_sample << " ps = " << playback_sample << endl;
349 chaninfo->buf->get_read_vector (&(*chan)->rw_vector);
351 if (disk_samples_to_consume <= (samplecnt_t) chaninfo->rw_vector.len[0]) {
353 if (fabsf (speed) != 1.0f) {
354 samplecnt_t ocnt = nframes;
355 samplecnt_t icnt = chaninfo->rw_vector.len[0];
356 (void) interpolation.interpolate (n, icnt, chaninfo->rw_vector.buf[0], ocnt, disk_signal);
357 } else if (speed != 0.0) {
358 memcpy (disk_signal, chaninfo->rw_vector.buf[0], sizeof (Sample) * disk_samples_to_consume);
363 const samplecnt_t total = chaninfo->rw_vector.len[0] + chaninfo->rw_vector.len[1];
365 if (disk_samples_to_consume <= total) {
367 if (fabsf (speed) != 1.0f) {
368 samplecnt_t ocnt = nframes;
369 interpolation.interpolate (n, chaninfo->rw_vector.len[0], chaninfo->rw_vector.buf[0], ocnt, disk_signal);
371 if (ocnt < nframes) {
373 ocnt = nframes - ocnt;
374 interpolation.interpolate (n, chaninfo->rw_vector.len[1], chaninfo->rw_vector.buf[1], ocnt, disk_signal);
377 } else if (speed != 0.0) {
380 chaninfo->rw_vector.buf[0],
381 chaninfo->rw_vector.len[0] * sizeof (Sample));
382 memcpy (disk_signal + chaninfo->rw_vector.len[0],
383 chaninfo->rw_vector.buf[1],
384 (disk_samples_to_consume - chaninfo->rw_vector.len[0]) * sizeof (Sample));
389 cerr << _name << " Need " << disk_samples_to_consume << " total = " << total << endl;
390 cerr << "underrun for " << _name << endl;
391 DEBUG_TRACE (DEBUG::Butler, string_compose ("%1 underrun in %2, total space = %3\n",
392 DEBUG_THREAD_SELF, name(), total));
399 if (scaling != 1.0f && speed != 0.0) {
400 apply_gain_to_buffer (disk_signal, disk_samples_to_consume, scaling);
403 chaninfo->buf->increment_read_ptr (disk_samples_to_consume);
405 if (ms & MonitoringInput) {
406 /* mix the disk signal into the input signal (already in bufs) */
407 mix_buffers_no_gain (output.data(), disk_signal, disk_samples_to_consume);
412 /* MIDI data handling */
415 if (!_session.declick_out_pending() && bufs.count().n_midi()) {
418 if (_no_disk_output) {
419 dst = &scratch_bufs.get_midi(0);
421 dst = &bufs.get_midi (0);
424 if ((ms & MonitoringDisk) && !still_locating) {
425 get_midi_playback (*dst, start_sample, end_sample, ms, scratch_bufs, speed, disk_samples_to_consume);
429 if (!still_locating) {
431 bool butler_required = false;
434 playback_sample -= disk_samples_to_consume;
436 playback_sample += disk_samples_to_consume;
439 if (_playlists[DataType::AUDIO]) {
442 if (c->front()->buf->write_space() >= c->front()->buf->bufsize() / 2) {
443 DEBUG_TRACE (DEBUG::Butler, string_compose ("%1: slaved, write space = %2 of %3\n", name(), c->front()->buf->write_space(),
444 c->front()->buf->bufsize()));
445 butler_required = true;
448 if ((samplecnt_t) c->front()->buf->write_space() >= _chunk_samples) {
449 DEBUG_TRACE (DEBUG::Butler, string_compose ("%1: write space = %2 of %3\n", name(), c->front()->buf->write_space(),
451 butler_required = true;
457 if (_playlists[DataType::MIDI]) {
458 /* MIDI butler needed part */
460 uint32_t samples_read = g_atomic_int_get(const_cast<gint*>(&_samples_read_from_ringbuffer));
461 uint32_t samples_written = g_atomic_int_get(const_cast<gint*>(&_samples_written_to_ringbuffer));
464 cerr << name() << " MDS written: " << samples_written << " - read: " << samples_read <<
465 " = " << samples_written - samples_read
466 << " + " << disk_samples_to_consume << " < " << midi_readahead << " = " << need_butler << ")" << endl;
469 /* samples_read will generally be less than samples_written, but
470 * immediately after an overwrite, we can end up having read some data
471 * before we've written any. we don't need to trip an assert() on this,
472 * but we do need to check so that the decision on whether or not we
473 * need the butler is done correctly.
478 * Doing heavy GUI operations[1] can stall also the butler.
479 * The RT-thread meanwhile will happily continue and
480 * ‘samples_read’ (from buffer to output) will become larger
481 * than ‘samples_written’ (from disk to buffer).
483 * The disk-stream is now behind..
485 * In those cases the butler needs to be summed to refill the buffer (done now)
486 * AND we need to skip (samples_read - samples_written). ie remove old events
487 * before playback_sample from the rinbuffer.
489 * [1] one way to do so is described at #6170.
490 * For me just popping up the context-menu on a MIDI-track header
491 * of a track with a large (think beethoven :) midi-region also did the
492 * trick. The playhead stalls for 2 or 3 sec, until the context-menu shows.
494 * In both cases the root cause is that redrawing MIDI regions on the GUI is still very slow
497 if (samples_read <= samples_written) {
498 if ((samples_written - samples_read) + disk_samples_to_consume < midi_readahead) {
499 butler_required = true;
502 butler_required = true;
507 _need_butler = butler_required;
510 // DEBUG_TRACE (DEBUG::Butler, string_compose ("%1 reader run, needs butler = %2\n", name(), _need_butler));
514 DiskReader::set_pending_overwrite (bool yn)
516 /* called from audio thread, so we can use the read ptr and playback sample as we wish */
518 _pending_overwrite = yn;
520 overwrite_sample = playback_sample;
522 boost::shared_ptr<ChannelList> c = channels.reader ();
524 overwrite_offset = c->front()->buf->get_read_ptr();
529 DiskReader::overwrite_existing_buffers ()
533 boost::shared_ptr<ChannelList> c = channels.reader();
535 overwrite_queued = false;
537 DEBUG_TRACE (DEBUG::DiskIO, string_compose ("%1 overwriting existing buffers at %2\n", overwrite_sample));
543 const bool reversed = _session.transport_speed() < 0.0f;
545 /* assume all are the same size */
546 samplecnt_t size = c->front()->buf->bufsize();
548 boost::scoped_ptr<Sample> mixdown_buffer (new Sample[size]);
549 boost::scoped_ptr<float> gain_buffer (new float[size]);
551 /* reduce size so that we can fill the buffer correctly (ringbuffers
552 can only handle size-1, otherwise they appear to be empty)
559 for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan, ++n) {
561 start = overwrite_sample;
562 samplecnt_t cnt = size;
564 /* to fill the buffer without resetting the playback sample, we need to
565 do it one or two chunks (normally two).
567 |----------------------------------------------------------------------|
571 |<- second chunk->||<----------------- first chunk ------------------>|
575 samplecnt_t to_read = size - overwrite_offset;
577 if (audio_read ((*chan)->buf->buffer() + overwrite_offset, mixdown_buffer.get(), gain_buffer.get(), start, to_read, n, reversed)) {
578 error << string_compose(_("DiskReader %1: when refilling, cannot read %2 from playlist at sample %3"),
579 id(), size, playback_sample) << endmsg;
587 if (audio_read ((*chan)->buf->buffer(), mixdown_buffer.get(), gain_buffer.get(), start, cnt, n, reversed)) {
588 error << string_compose(_("DiskReader %1: when refilling, cannot read %2 from playlist at sample %3"),
589 id(), size, playback_sample) << endmsg;
601 if (_midi_buf && _playlists[DataType::MIDI]) {
603 /* Clear the playback buffer contents. This is safe as long as the butler
604 thread is suspended, which it should be.
607 _midi_buf->reset_tracker ();
609 g_atomic_int_set (&_samples_read_from_ringbuffer, 0);
610 g_atomic_int_set (&_samples_written_to_ringbuffer, 0);
612 /* Resolve all currently active notes in the playlist. This is more
613 aggressive than it needs to be: ideally we would only resolve what is
614 absolutely necessary, but this seems difficult and/or impossible without
615 having the old data or knowing what change caused the overwrite.
617 midi_playlist()->resolve_note_trackers (*_midi_buf, overwrite_sample);
619 midi_read (overwrite_sample, _chunk_samples, false);
620 file_sample[DataType::MIDI] = overwrite_sample; // overwrite_sample was adjusted by ::midi_read() to the new position
623 _pending_overwrite = false;
629 DiskReader::seek (samplepos_t sample, bool complete_refill)
633 ChannelList::iterator chan;
634 boost::shared_ptr<ChannelList> c = channels.reader();
636 //sample = std::max ((samplecnt_t)0, sample -_session.worst_output_latency ());
638 for (n = 0, chan = c->begin(); chan != c->end(); ++chan, ++n) {
639 (*chan)->buf->reset ();
642 if (g_atomic_int_get (&_samples_read_from_ringbuffer) == 0) {
643 /* we haven't read anything since the last seek,
644 so flush all note trackers to prevent
651 g_atomic_int_set(&_samples_read_from_ringbuffer, 0);
652 g_atomic_int_set(&_samples_written_to_ringbuffer, 0);
654 playback_sample = sample;
655 file_sample[DataType::AUDIO] = sample;
656 file_sample[DataType::MIDI] = sample;
658 if (complete_refill) {
659 /* call _do_refill() to refill the entire buffer, using
660 the largest reads possible.
662 while ((ret = do_refill_with_alloc (false)) > 0) ;
664 /* call _do_refill() to refill just one chunk, and then
667 ret = do_refill_with_alloc (true);
675 DiskReader::can_internal_playback_seek (samplecnt_t distance)
679 ChannelList::iterator chan;
680 boost::shared_ptr<ChannelList> c = channels.reader();
682 for (chan = c->begin(); chan != c->end(); ++chan) {
683 if ((*chan)->buf->read_space() < (size_t) distance) {
690 uint32_t samples_read = g_atomic_int_get(&_samples_read_from_ringbuffer);
691 uint32_t samples_written = g_atomic_int_get(&_samples_written_to_ringbuffer);
693 return ((samples_written - samples_read) < distance);
697 DiskReader::internal_playback_seek (samplecnt_t distance)
699 ChannelList::iterator chan;
700 boost::shared_ptr<ChannelList> c = channels.reader();
702 for (chan = c->begin(); chan != c->end(); ++chan) {
703 (*chan)->buf->increment_read_ptr (::llabs(distance));
706 playback_sample += distance;
712 void swap_by_ptr (Sample *first, Sample *last)
714 while (first < last) {
721 /** Read some data for 1 channel from our playlist into a buffer.
722 * @param buf Buffer to write to.
723 * @param start Session sample to start reading from; updated to where we end up
725 * @param cnt Count of samples to read.
726 * @param reversed true if we are running backwards, otherwise false.
729 DiskReader::audio_read (Sample* buf, Sample* mixdown_buffer, float* gain_buffer,
730 samplepos_t& start, samplecnt_t cnt,
731 int channel, bool reversed)
733 samplecnt_t this_read = 0;
735 samplepos_t loop_end = 0;
736 samplepos_t loop_start = 0;
737 samplecnt_t offset = 0;
740 if (!_playlists[DataType::AUDIO]) {
741 memset (buf, 0, sizeof (Sample) * cnt);
745 /* XXX we don't currently play loops in reverse. not sure why */
749 samplecnt_t loop_length = 0;
751 /* Make the use of a Location atomic for this read operation.
753 Note: Locations don't get deleted, so all we care about
754 when I say "atomic" is that we are always pointing to
755 the same one and using a start/length values obtained
759 if ((loc = _loop_location) != 0) {
760 loop_start = loc->start();
761 loop_end = loc->end();
762 loop_length = loop_end - loop_start;
765 /* if we are looping, ensure that the first sample we read is at the correct
766 position within the loop.
769 if (loc && start >= loop_end) {
770 start = loop_start + ((start - loop_start) % loop_length);
779 /* We need this while loop in case we hit a loop boundary, in which case our read from
780 the playlist must be split into more than one section.
785 /* take any loop into account. we can't read past the end of the loop. */
787 if (loc && (loop_end - start < cnt)) {
788 this_read = loop_end - start;
795 if (this_read == 0) {
799 this_read = min(cnt,this_read);
801 if (audio_playlist()->read (buf+offset, mixdown_buffer, gain_buffer, start, this_read, channel) != this_read) {
802 error << string_compose(_("DiskReader %1: cannot read %2 from playlist at sample %3"), id(), this_read,
809 swap_by_ptr (buf, buf + this_read - 1);
813 /* if we read to the end of the loop, go back to the beginning */
830 DiskReader::_do_refill_with_alloc (bool partial_fill)
832 /* We limit disk reads to at most 4MB chunks, which with floating point
833 samples would be 1M samples. But we might use 16 or 14 bit samples,
834 in which case 4MB is more samples than that. Therefore size this for
835 the smallest sample value .. 4MB = 2M samples (16 bit).
839 boost::scoped_ptr<Sample> mix_buf (new Sample[2*1048576]);
840 boost::scoped_ptr<float> gain_buf (new float[2*1048576]);
842 int ret = refill_audio (mix_buf.get(), gain_buf.get(), (partial_fill ? _chunk_samples : 0));
849 return refill_midi ();
853 DiskReader::refill (Sample* mixdown_buffer, float* gain_buffer, samplecnt_t fill_level)
855 int ret = refill_audio (mixdown_buffer, gain_buffer, fill_level);
861 return refill_midi ();
865 /** Get some more data from disk and put it in our channels' bufs,
866 * if there is suitable space in them.
868 * If fill_level is non-zero, then we will refill the buffer so that there is
869 * still at least fill_level samples of space left to be filled. This is used
870 * after locates so that we do not need to wait to fill the entire buffer.
875 DiskReader::refill_audio (Sample* mixdown_buffer, float* gain_buffer, samplecnt_t fill_level)
877 /* do not read from disk while session is marked as Loading, to avoid
878 useless redundant I/O.
881 if (_session.loading()) {
887 RingBufferNPT<Sample>::rw_vector vector;
888 bool const reversed = _session.transport_speed() < 0.0f;
889 samplecnt_t total_space;
890 samplecnt_t zero_fill;
892 ChannelList::iterator i;
893 boost::shared_ptr<ChannelList> c = channels.reader();
900 assert(mixdown_buffer);
908 c->front()->buf->get_write_vector (&vector);
910 if ((total_space = vector.len[0] + vector.len[1]) == 0) {
911 DEBUG_TRACE (DEBUG::DiskIO, string_compose ("%1: no space to refill\n", name()));
912 /* nowhere to write to */
917 if (fill_level < total_space) {
918 total_space -= fill_level;
920 /* we can't do anything with it */
925 /* if we're running close to normal speed and there isn't enough
926 space to do disk_read_chunk_samples of I/O, then don't bother.
928 at higher speeds, just do it because the sync between butler
929 and audio thread may not be good enough.
931 Note: it is a design assumption that disk_read_chunk_samples is smaller
932 than the playback buffer size, so this check should never trip when
933 the playback buffer is empty.
936 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()));
937 if ((total_space < _chunk_samples) && fabs (_session.transport_speed()) < 2.0f) {
941 /* when slaved, don't try to get too close to the read pointer. this
942 leaves space for the buffer reversal to have something useful to
946 if (_slaved && total_space < (samplecnt_t) (c->front()->buf->bufsize() / 2)) {
947 DEBUG_TRACE (DEBUG::DiskIO, string_compose ("%1: not enough to refill while slaved\n", this));
951 samplepos_t ffa = file_sample[DataType::AUDIO];
957 /* at start: nothing to do but fill with silence */
959 for (chan_n = 0, i = c->begin(); i != c->end(); ++i, ++chan_n) {
961 ChannelInfo* chan (*i);
962 chan->buf->get_write_vector (&vector);
963 memset (vector.buf[0], 0, sizeof(Sample) * vector.len[0]);
965 memset (vector.buf[1], 0, sizeof(Sample) * vector.len[1]);
967 chan->buf->increment_write_ptr (vector.len[0] + vector.len[1]);
972 if (ffa < total_space) {
974 /* too close to the start: read what we can,
975 and then zero fill the rest
978 zero_fill = total_space - ffa;
988 if (ffa == max_samplepos) {
990 /* at end: nothing to do but fill with silence */
992 for (chan_n = 0, i = c->begin(); i != c->end(); ++i, ++chan_n) {
994 ChannelInfo* chan (*i);
995 chan->buf->get_write_vector (&vector);
996 memset (vector.buf[0], 0, sizeof(Sample) * vector.len[0]);
998 memset (vector.buf[1], 0, sizeof(Sample) * vector.len[1]);
1000 chan->buf->increment_write_ptr (vector.len[0] + vector.len[1]);
1005 if (ffa > max_samplepos - total_space) {
1007 /* to close to the end: read what we can, and zero fill the rest */
1009 zero_fill = total_space - (max_samplepos - ffa);
1010 total_space = max_samplepos - ffa;
1017 samplepos_t file_sample_tmp = 0;
1019 /* total_space is in samples. We want to optimize read sizes in various sizes using bytes */
1021 const size_t bits_per_sample = format_data_width (_session.config.get_native_file_data_format());
1022 size_t total_bytes = total_space * bits_per_sample / 8;
1024 /* chunk size range is 256kB to 4MB. Bigger is faster in terms of MB/sec, but bigger chunk size always takes longer
1026 size_t byte_size_for_read = max ((size_t) (256 * 1024), min ((size_t) (4 * 1048576), total_bytes));
1028 /* find nearest (lower) multiple of 16384 */
1030 byte_size_for_read = (byte_size_for_read / 16384) * 16384;
1032 /* now back to samples */
1034 samplecnt_t samples_to_read = byte_size_for_read / (bits_per_sample / 8);
1036 DEBUG_TRACE (DEBUG::DiskIO, string_compose ("%1: will refill %2 channels with %3 samples\n", name(), c->size(), total_space));
1038 // uint64_t before = g_get_monotonic_time ();
1039 // uint64_t elapsed;
1041 for (chan_n = 0, i = c->begin(); i != c->end(); ++i, ++chan_n) {
1043 ChannelInfo* chan (*i);
1046 samplecnt_t len1, len2;
1048 chan->buf->get_write_vector (&vector);
1050 if ((samplecnt_t) vector.len[0] > samples_to_read) {
1052 /* we're not going to fill the first chunk, so certainly do not bother with the
1053 other part. it won't be connected with the part we do fill, as in:
1055 .... => writable space
1056 ++++ => readable space
1057 ^^^^ => 1 x disk_read_chunk_samples that would be filled
1059 |......|+++++++++++++|...............................|
1064 So, just pretend that the buf1 part isn't there.
1074 file_sample_tmp = ffa;
1076 buf1 = vector.buf[0];
1077 len1 = vector.len[0];
1078 buf2 = vector.buf[1];
1079 len2 = vector.len[1];
1081 to_read = min (ts, len1);
1082 to_read = min (to_read, (samplecnt_t) samples_to_read);
1084 assert (to_read >= 0);
1088 if (audio_read (buf1, mixdown_buffer, gain_buffer, file_sample_tmp, to_read, chan_n, reversed)) {
1092 chan->buf->increment_write_ptr (to_read);
1096 to_read = min (ts, len2);
1100 /* we read all of vector.len[0], but it wasn't the
1101 entire samples_to_read of data, so read some or
1102 all of vector.len[1] as well.
1105 if (audio_read (buf2, mixdown_buffer, gain_buffer, file_sample_tmp, to_read, chan_n, reversed)) {
1110 chan->buf->increment_write_ptr (to_read);
1114 /* XXX: do something */
1119 // elapsed = g_get_monotonic_time () - before;
1120 // cerr << '\t' << name() << ": bandwidth = " << (byte_size_for_read / 1048576.0) / (elapsed/1000000.0) << "MB/sec\n";
1122 file_sample[DataType::AUDIO] = file_sample_tmp;
1123 assert (file_sample[DataType::AUDIO] >= 0);
1125 ret = ((total_space - samples_to_read) > _chunk_samples);
1127 c->front()->buf->get_write_vector (&vector);
1134 DiskReader::playlist_ranges_moved (list< Evoral::RangeMove<samplepos_t> > const & movements_samples, bool from_undo)
1136 /* If we're coming from an undo, it will have handled
1137 automation undo (it must, since automation-follows-regions
1138 can lose automation data). Hence we can do nothing here.
1145 if (!_route || Config->get_automation_follows_regions () == false) {
1149 list< Evoral::RangeMove<double> > movements;
1151 for (list< Evoral::RangeMove<samplepos_t> >::const_iterator i = movements_samples.begin();
1152 i != movements_samples.end();
1155 movements.push_back(Evoral::RangeMove<double>(i->from, i->length, i->to));
1158 /* move panner automation */
1159 boost::shared_ptr<Pannable> pannable = _route->pannable();
1160 Evoral::ControlSet::Controls& c (pannable->controls());
1162 for (Evoral::ControlSet::Controls::iterator ci = c.begin(); ci != c.end(); ++ci) {
1163 boost::shared_ptr<AutomationControl> ac = boost::dynamic_pointer_cast<AutomationControl>(ci->second);
1167 boost::shared_ptr<AutomationList> alist = ac->alist();
1168 if (!alist->size()) {
1171 XMLNode & before = alist->get_state ();
1172 bool const things_moved = alist->move_ranges (movements);
1174 _session.add_command (new MementoCommand<AutomationList> (
1175 *alist.get(), &before, &alist->get_state ()));
1178 /* move processor automation */
1179 _route->foreach_processor (boost::bind (&DiskReader::move_processor_automation, this, _1, movements_samples));
1183 DiskReader::move_processor_automation (boost::weak_ptr<Processor> p, list< Evoral::RangeMove<samplepos_t> > const & movements_samples)
1185 boost::shared_ptr<Processor> processor (p.lock ());
1190 list< Evoral::RangeMove<double> > movements;
1191 for (list< Evoral::RangeMove<samplepos_t> >::const_iterator i = movements_samples.begin(); i != movements_samples.end(); ++i) {
1192 movements.push_back(Evoral::RangeMove<double>(i->from, i->length, i->to));
1195 set<Evoral::Parameter> const a = processor->what_can_be_automated ();
1197 for (set<Evoral::Parameter>::const_iterator i = a.begin (); i != a.end (); ++i) {
1198 boost::shared_ptr<AutomationList> al = processor->automation_control(*i)->alist();
1202 XMLNode & before = al->get_state ();
1203 bool const things_moved = al->move_ranges (movements);
1205 _session.add_command (
1206 new MementoCommand<AutomationList> (
1207 *al.get(), &before, &al->get_state ()
1215 DiskReader::reset_tracker ()
1217 _midi_buf->reset_tracker ();
1219 boost::shared_ptr<MidiPlaylist> mp (midi_playlist());
1222 mp->reset_note_trackers ();
1227 DiskReader::resolve_tracker (Evoral::EventSink<samplepos_t>& buffer, samplepos_t time)
1229 _midi_buf->resolve_tracker(buffer, time);
1231 boost::shared_ptr<MidiPlaylist> mp (midi_playlist());
1234 mp->reset_note_trackers ();
1238 /** Writes playback events from playback_sample for nframes to dst, translating time stamps
1239 * so that an event at playback_sample has time = 0
1242 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)
1245 samplepos_t nframes = end_sample - start_sample;
1247 if ((ms & MonitoringInput) == 0) {
1248 /* Route::process_output_buffers() clears the buffer as-needed */
1251 target = &scratch_bufs.get_midi (0);
1254 if (ms & MonitoringDisk) {
1255 /* disk data needed */
1257 Location* loc = _loop_location;
1259 DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose (
1260 "%1 MDS pre-read read %8 offset = %9 @ %4..%5 from %2 write to %3, LOOPED ? %6 .. %7\n", _name,
1261 _midi_buf->get_read_ptr(), _midi_buf->get_write_ptr(), start_sample, end_sample,
1262 (loc ? loc->start() : -1), (loc ? loc->end() : -1), nframes, Port::port_offset()));
1264 //cerr << "======== PRE ========\n";
1265 //_midi_buf->dump (cerr);
1266 //cerr << "----------------\n";
1268 size_t events_read = 0;
1271 samplepos_t effective_start;
1273 Evoral::Range<samplepos_t> loop_range (loc->start(), loc->end() - 1);
1274 effective_start = loop_range.squish (start_sample);
1276 DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose ("looped, effective start adjusted to %1\n", effective_start));
1278 if (effective_start == loc->start()) {
1279 /* We need to turn off notes that may extend
1280 beyond the loop end.
1283 _midi_buf->resolve_tracker (*target, 0);
1286 /* for split-cycles we need to offset the events */
1288 if (loc->end() >= effective_start && loc->end() < effective_start + nframes) {
1290 /* end of loop is within the range we are reading, so
1291 split the read in two, and lie about the location
1295 samplecnt_t first, second;
1297 first = loc->end() - effective_start;
1298 second = nframes - first;
1300 DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose ("loop read for eff %1 end %2: %3 and %4, cycle offset %5\n",
1301 effective_start, loc->end(), first, second));
1304 DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose ("loop read #1, from %1 for %2\n",
1305 effective_start, first));
1306 events_read = _midi_buf->read (*target, effective_start, first);
1310 DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose ("loop read #2, from %1 for %2\n",
1311 loc->start(), second));
1312 events_read += _midi_buf->read (*target, loc->start(), second);
1316 DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose ("loop read #3, adjusted start as %1 for %2\n",
1317 effective_start, nframes));
1318 events_read = _midi_buf->read (*target, effective_start, effective_start + nframes);
1321 const size_t n_skipped = _midi_buf->skip_to (start_sample);
1322 if (n_skipped > 0) {
1323 warning << string_compose(_("MidiDiskstream %1: skipped %2 events, possible underflow"), id(), n_skipped) << endmsg;
1325 DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose ("playback buffer read, from %1 to %2 (%3)", start_sample, end_sample, nframes));
1326 events_read = _midi_buf->read (*target, start_sample, end_sample, Port::port_offset ());
1329 DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose (
1330 "%1 MDS events read %2 range %3 .. %4 rspace %5 wspace %6 r@%7 w@%8\n",
1331 _name, events_read, playback_sample, playback_sample + nframes,
1332 _midi_buf->read_space(), _midi_buf->write_space(),
1333 _midi_buf->get_read_ptr(), _midi_buf->get_write_ptr()));
1336 g_atomic_int_add (&_samples_read_from_ringbuffer, nframes);
1339 if (speed != 0.0 && fabsf (speed) != 1.0f) {
1340 for (MidiBuffer::iterator i = target->begin(); i != target->end(); ++i) {
1341 MidiBuffer::TimeType *tme = i.timeptr();
1342 // XXX need to subtract port offsets before scaling
1343 // also we must only scale events read from disk
1344 // and not existing input data in the buffer.
1345 *tme = (*tme) * nframes / disk_samples_to_consume;
1349 if (ms & MonitoringInput) {
1350 dst.merge_from (*target, nframes);
1354 if (!target->empty ()) {
1355 cerr << "======== MIDI OUT ========\n";
1356 for (MidiBuffer::iterator i = target->begin(); i != target->end(); ++i) {
1357 const Evoral::Event<MidiBuffer::TimeType> ev (*i, false);
1358 cerr << "MIDI EVENT (from disk) @ " << ev.time();
1359 for (size_t xx = 0; xx < ev.size(); ++xx) {
1360 cerr << ' ' << hex << (int) ev.buffer()[xx];
1362 cerr << dec << endl;
1364 cerr << "----------------\n";
1368 cerr << "======== MIDI Disk Buffer ========\n";
1369 _midi_buf->dump (cerr);
1370 cerr << "----------------\n";
1374 /** @a start is set to the new sample position (TIME) read up to */
1376 DiskReader::midi_read (samplepos_t& start, samplecnt_t dur, bool reversed)
1378 samplecnt_t this_read = 0;
1379 samplepos_t loop_end = 0;
1380 samplepos_t loop_start = 0;
1381 samplecnt_t loop_length = 0;
1382 Location* loc = _loop_location;
1383 samplepos_t effective_start = start;
1384 Evoral::Range<samplepos_t>* loop_range (0);
1386 DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose ("MDS::midi_read @ %1 cnt %2\n", start, dur));
1388 boost::shared_ptr<MidiTrack> mt = boost::dynamic_pointer_cast<MidiTrack>(_route);
1389 MidiChannelFilter* filter = mt ? &mt->playback_filter() : 0;
1390 sampleoffset_t loop_offset = 0;
1392 if (!reversed && loc) {
1393 get_location_times (loc, &loop_start, &loop_end, &loop_length);
1398 /* take any loop into account. we can't read past the end of the loop. */
1400 if (loc && !reversed) {
1403 loop_range = new Evoral::Range<samplepos_t> (loop_start, loop_end-1); // inclusive semantics require -1
1406 /* if we are (seamlessly) looping, ensure that the first sample we read is at the correct
1407 position within the loop.
1410 effective_start = loop_range->squish (effective_start);
1412 if ((loop_end - effective_start) <= dur) {
1413 /* too close to end of loop to read "dur", so
1416 this_read = loop_end - effective_start;
1425 if (this_read == 0) {
1429 this_read = min (dur,this_read);
1431 DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose ("MDS ::read at %1 for %2 loffset %3\n", effective_start, this_read, loop_offset));
1433 if (midi_playlist()->read (*_midi_buf, effective_start, this_read, loop_range, 0, filter) != this_read) {
1434 error << string_compose(
1435 _("MidiDiskstream %1: cannot read %2 from playlist at sample %3"),
1436 id(), this_read, start) << endmsg;
1440 g_atomic_int_add (&_samples_written_to_ringbuffer, this_read);
1444 // Swap note ons with note offs here. etc?
1445 // Fully reversing MIDI requires look-ahead (well, behind) to find previous
1446 // CC values etc. hard.
1450 /* adjust passed-by-reference argument (note: this is
1451 monotonic and does not reflect looping.
1455 /* similarly adjust effective_start, but this may be
1456 readjusted for seamless looping as we continue around
1459 effective_start += this_read;
1469 DiskReader::refill_midi ()
1471 if (!_playlists[DataType::MIDI]) {
1475 const size_t write_space = _midi_buf->write_space();
1476 const bool reversed = _session.transport_speed() < 0.0f;
1478 DEBUG_TRACE (DEBUG::DiskIO, string_compose ("MIDI refill, write space = %1 file sample = %2\n", write_space, file_sample[DataType::MIDI]));
1480 /* no space to write */
1481 if (write_space == 0) {
1489 /* at end: nothing to do */
1491 samplepos_t ffm = file_sample[DataType::MIDI];
1493 if (ffm == max_samplepos) {
1498 const uint32_t samples_read = g_atomic_int_get (&_samples_read_from_ringbuffer);
1499 const uint32_t samples_written = g_atomic_int_get (&_samples_written_to_ringbuffer);
1501 if ((samples_read < samples_written) && (samples_written - samples_read) >= midi_readahead) {
1505 samplecnt_t to_read = midi_readahead - ((samplecnt_t)samples_written - (samplecnt_t)samples_read);
1507 to_read = min (to_read, (samplecnt_t) (max_samplepos - ffm));
1508 to_read = min (to_read, (samplecnt_t) write_space);
1510 if (midi_read (ffm, to_read, reversed)) {
1514 file_sample[DataType::MIDI] = ffm;
1520 DiskReader::set_no_disk_output (bool yn)
1522 /* this MUST be called as part of the process call tree, before any
1523 disk readers are invoked. We use it when the session needs the
1524 transport (and thus effective read position for DiskReaders) to keep
1525 advancing as part of syncing up with a transport master, but we
1526 don't want any actual disk output yet because we are still not
1529 _no_disk_output = yn;