2 * Copyright (C) 2017-2018 Robin Gareus <robin@gareus.org>
3 * Copyright (C) 2017 Paul Davis <paul@linuxaudiosystems.com>
4 * Copyright (C) 2018 Ben Loftis <ben@harrisonconsoles.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include "ardour/analyser.h"
22 #include "ardour/audioengine.h"
23 #include "ardour/audiofilesource.h"
24 #include "ardour/audio_buffer.h"
25 #include "ardour/audioplaylist.h"
26 #include "ardour/audioregion.h"
27 #include "ardour/butler.h"
28 #include "ardour/debug.h"
29 #include "ardour/disk_writer.h"
30 #include "ardour/midi_playlist.h"
31 #include "ardour/midi_source.h"
32 #include "ardour/midi_track.h"
33 #include "ardour/port.h"
34 #include "ardour/region_factory.h"
35 #include "ardour/session.h"
36 #include "ardour/smf_source.h"
40 using namespace ARDOUR;
44 ARDOUR::samplecnt_t DiskWriter::_chunk_samples = DiskWriter::default_chunk_samples ();
45 PBD::Signal0<void> DiskWriter::Overrun;
47 DiskWriter::DiskWriter (Session& s, string const & str, DiskIOProcessor::Flag f)
48 : DiskIOProcessor (s, str, f)
51 , _capture_start_sample (0)
52 , _capture_captured (0)
53 , _was_recording (false)
54 , _first_recordable_sample (max_samplepos)
55 , _last_recordable_sample (max_samplepos)
56 , _last_possibly_recording (0)
57 , _alignment_style (ExistingMaterial)
58 , _note_mode (Sustained)
59 , _samples_pending_write (0)
60 , _num_captured_loops (0)
61 , _accumulated_capture_offset (0)
62 , _gui_feed_buffer(AudioEngine::instance()->raw_buffer_size (DataType::MIDI))
64 DiskIOProcessor::init ();
67 DiskWriter::~DiskWriter ()
69 DEBUG_TRACE (DEBUG::Destruction, string_compose ("DiskWriter %1 @ %2 deleted\n", _name, this));
71 boost::shared_ptr<ChannelList> c = channels.reader();
73 for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
74 (*chan)->write_source.reset ();
79 DiskWriter::default_chunk_samples ()
85 DiskWriter::WriterChannelInfo::resize (samplecnt_t bufsize)
87 if (!capture_transition_buf) {
88 capture_transition_buf = new RingBufferNPT<CaptureTransition> (256);
91 wbuf = new RingBufferNPT<Sample> (bufsize);
92 /* touch memory to lock it */
93 memset (wbuf->buffer(), 0, sizeof (Sample) * wbuf->bufsize());
97 DiskWriter::add_channel_to (boost::shared_ptr<ChannelList> c, uint32_t how_many)
100 c->push_back (new WriterChannelInfo (_session.butler()->audio_capture_buffer_size()));
101 DEBUG_TRACE (DEBUG::DiskIO, string_compose ("%1: new writer channel, write space = %2 read = %3\n",
103 c->back()->wbuf->write_space(),
104 c->back()->wbuf->read_space()));
113 DiskWriter::set_write_source_name (string const & str)
115 _write_source_name = str;
120 DiskWriter::check_record_status (samplepos_t transport_sample, double speed, bool can_record)
122 int possibly_recording;
123 const int transport_rolling = 0x4;
124 const int track_rec_enabled = 0x2;
125 const int global_rec_enabled = 0x1;
126 const int fully_rec_enabled = (transport_rolling |track_rec_enabled | global_rec_enabled);
128 /* merge together the 3 factors that affect record status, and compute what has changed. */
130 possibly_recording = (speed != 0.0f ? 4 : 0) | (record_enabled() ? 2 : 0) | (can_record ? 1 : 0);
132 if (possibly_recording == _last_possibly_recording) {
136 if (possibly_recording == fully_rec_enabled) {
138 if (_last_possibly_recording == fully_rec_enabled) {
143 if (_session.config.get_punch_in () && 0 != (loc = _session.locations()->auto_punch_location ())) {
144 _capture_start_sample = loc->start ();
146 _capture_start_sample = _session.transport_sample ();
149 _first_recordable_sample = _capture_start_sample;
151 if (_alignment_style == ExistingMaterial) {
152 _first_recordable_sample += _capture_offset + _playback_offset;
155 if (_session.config.get_punch_out () && 0 != (loc = _session.locations()->auto_punch_location ())) {
156 /* this freezes the punch-out point when starting to record.
158 * We should allow to move it or at least allow to disable punch-out
161 _last_recordable_sample = loc->end ();
162 if (_alignment_style == ExistingMaterial) {
163 _last_recordable_sample += _capture_offset + _playback_offset;
166 _last_recordable_sample = max_samplepos;
169 DEBUG_TRACE (DEBUG::CaptureAlignment, string_compose ("%1: @ %2 (STS: %3) CS:%4 FRS: %5 IL: %7, OL: %8 CO: %9 PO: %10 WOL: %11 WIL: %12\n",
172 _session.transport_sample(),
173 _capture_start_sample,
174 _first_recordable_sample,
175 _last_recordable_sample,
180 _session.worst_output_latency(),
181 _session.worst_input_latency()));
184 prepare_record_status (_capture_start_sample);
188 _last_possibly_recording = possibly_recording;
192 DiskWriter::calculate_record_range (Evoral::OverlapType ot, samplepos_t transport_sample, samplecnt_t nframes, samplecnt_t & rec_nframes, samplecnt_t & rec_offset)
195 case Evoral::OverlapNone:
199 case Evoral::OverlapInternal:
200 /* ---------- recrange
203 rec_nframes = nframes;
207 case Evoral::OverlapStart:
208 /* |--------| recrange
211 rec_nframes = transport_sample + nframes - _first_recordable_sample;
213 rec_offset = _first_recordable_sample - transport_sample;
217 case Evoral::OverlapEnd:
218 /* |--------| recrange
219 * |-------- transrange
221 rec_nframes = _last_recordable_sample - transport_sample;
225 case Evoral::OverlapExternal:
226 /* |--------| recrange
227 * -------------- transrange
229 rec_nframes = _last_recordable_sample - _first_recordable_sample;
230 rec_offset = _first_recordable_sample - transport_sample;
234 DEBUG_TRACE (DEBUG::CaptureAlignment, string_compose ("%1 rec? %2 @ %3 (for %4) FRF %5 LRF %6 : rf %7 @ %8\n",
235 _name, enum_2_string (ot), transport_sample, nframes,
236 _first_recordable_sample, _last_recordable_sample, rec_nframes, rec_offset));
240 DiskWriter::engage_record_enable ()
242 g_atomic_int_set (&_record_enabled, 1);
246 DiskWriter::disengage_record_enable ()
248 g_atomic_int_set (&_record_enabled, 0);
252 DiskWriter::engage_record_safe ()
254 g_atomic_int_set (&_record_safe, 1);
258 DiskWriter::disengage_record_safe ()
260 g_atomic_int_set (&_record_safe, 0);
263 /** Get the start position (in session samples) of the nth capture in the current pass */
265 DiskWriter::get_capture_start_sample (uint32_t n) const
267 Glib::Threads::Mutex::Lock lm (capture_info_lock);
269 if (capture_info.size() > n) {
270 /* this is a completed capture */
271 return capture_info[n]->start;
273 /* this is the currently in-progress capture */
274 return _capture_start_sample;
279 DiskWriter::get_captured_samples (uint32_t n) const
281 Glib::Threads::Mutex::Lock lm (capture_info_lock);
283 if (capture_info.size() > n) {
284 /* this is a completed capture */
285 return capture_info[n]->samples;
287 /* this is the currently in-progress capture */
288 return _capture_captured;
293 DiskWriter::set_align_style (AlignStyle a, bool force)
295 if (record_enabled() && _session.actively_recording()) {
299 if ((a != _alignment_style) || force) {
300 _alignment_style = a;
301 AlignmentStyleChanged ();
308 XMLNode& node (DiskIOProcessor::state ());
309 node.set_property (X_("type"), X_("diskwriter"));
310 node.set_property (X_("record-safe"), (_record_safe ? X_("yes" : "no")));
315 DiskWriter::set_state (const XMLNode& node, int version)
317 if (DiskIOProcessor::set_state (node, version)) {
321 if (!node.get_property (X_("record-safe"), _record_safe)) {
322 _record_safe = false;
325 reset_write_sources (false, true);
331 DiskWriter::non_realtime_locate (samplepos_t position)
333 if (_midi_write_source) {
334 _midi_write_source->set_natural_position (position);
337 DiskIOProcessor::non_realtime_locate (position);
342 DiskWriter::prepare_record_status (samplepos_t _capture_start_sample)
344 if (recordable() && destructive()) {
345 boost::shared_ptr<ChannelList> c = channels.reader ();
346 for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
348 RingBufferNPT<CaptureTransition>::rw_vector transitions;
349 (*chan)->capture_transition_buf->get_write_vector (&transitions);
351 if (transitions.len[0] > 0) {
352 transitions.buf[0]->type = CaptureStart;
353 transitions.buf[0]->capture_val = _capture_start_sample;
354 (*chan)->capture_transition_buf->increment_write_ptr(1);
357 fatal << X_("programming error: capture_transition_buf is full on rec start! inconceivable!")
365 /** Do some record stuff [not described in this comment!]
368 * - Setup playback_distance with the nframes, or nframes adjusted
369 * for current varispeed, if appropriate.
370 * - Setup current_playback_buffer in each ChannelInfo to point to data
371 * that someone can read playback_distance worth of data from.
374 DiskWriter::run (BufferSet& bufs, samplepos_t start_sample, samplepos_t end_sample,
375 double speed, pframes_t nframes, bool result_required)
377 if (!_active && !_pending_active) {
380 _active = _pending_active;
383 boost::shared_ptr<ChannelList> c = channels.reader();
384 ChannelList::iterator chan;
386 samplecnt_t rec_offset = 0;
387 samplecnt_t rec_nframes = 0;
388 bool nominally_recording;
390 bool re = record_enabled ();
391 bool punch_in = _session.config.get_punch_in () && _session.locations()->auto_punch_location ();
392 bool can_record = _session.actively_recording ();
393 can_record |= speed != 0 && _session.get_record_enabled () && punch_in && _session.transport_sample () <= _session.locations()->auto_punch_location ()->start ();
395 _need_butler = false;
398 if (speed != 0 && re) {
399 DEBUG_TRACE (DEBUG::CaptureAlignment, string_compose ("%1: run() start: %2 end: %3 NF: %4\n", _name, start_sample, end_sample, nframes));
403 check_record_status (start_sample, speed, can_record);
409 nominally_recording = (can_record && re);
411 // Safeguard against situations where process() goes haywire when autopunching
412 // and last_recordable_sample < first_recordable_sample
414 if (_last_recordable_sample < _first_recordable_sample) {
415 _last_recordable_sample = max_samplepos;
418 const Location* const loop_loc = _loop_location;
419 samplepos_t loop_start = 0;
420 samplepos_t loop_end = 0;
421 samplepos_t loop_length = 0;
424 get_location_times (loop_loc, &loop_start, &loop_end, &loop_length);
427 if (nominally_recording || (re && _was_recording && _session.get_record_enabled() && punch_in)) {
429 Evoral::OverlapType ot = Evoral::coverage (_first_recordable_sample, _last_recordable_sample, start_sample, end_sample);
430 // XXX should this be transport_sample + nframes - 1 ? coverage() expects its parameter ranges to include their end points
431 // XXX also, first_recordable_sample & last_recordable_sample may both be == max_samplepos: coverage() will return OverlapNone in that case. Is thak OK?
432 calculate_record_range (ot, start_sample, nframes, rec_nframes, rec_offset);
434 DEBUG_TRACE (DEBUG::CaptureAlignment, string_compose ("%1: this time record %2 of %3 samples, offset %4\n", _name, rec_nframes, nframes, rec_offset));
436 if (rec_nframes && !_was_recording) {
437 _capture_captured = 0;
440 /* Loop recording, so pretend the capture started at the loop
441 start rgardless of what time it is now, so the source starts
442 at the loop start and can handle time wrapping around.
443 Otherwise, start the source right now as usual.
445 _capture_captured = start_sample - loop_start;
446 _capture_start_sample = loop_start;
449 if (_midi_write_source) {
450 _midi_write_source->mark_write_starting_now (_capture_start_sample, _capture_captured, loop_length);
453 g_atomic_int_set (const_cast<gint*> (&_samples_pending_write), 0);
454 g_atomic_int_set (const_cast<gint*> (&_num_captured_loops), 0);
456 _was_recording = true;
460 /* For audio: not writing samples to the capture ringbuffer offsets
461 * the recording. For midi: we need to keep track of the record range
462 * and subtract the accumulated difference from the event time.
465 _accumulated_capture_offset += rec_offset;
467 _accumulated_capture_offset += nframes;
472 if (can_record && !_last_capture_sources.empty ()) {
473 _last_capture_sources.clear ();
480 const size_t n_buffers = bufs.count().n_audio();
482 for (chan = c->begin(), n = 0; chan != c->end(); ++chan, ++n) {
484 ChannelInfo* chaninfo (*chan);
485 AudioBuffer& buf (bufs.get_audio (n%n_buffers));
487 chaninfo->wbuf->get_write_vector (&chaninfo->rw_vector);
489 if (rec_nframes <= (samplecnt_t) chaninfo->rw_vector.len[0]) {
491 Sample *incoming = buf.data (rec_offset);
492 memcpy (chaninfo->rw_vector.buf[0], incoming, sizeof (Sample) * rec_nframes);
496 samplecnt_t total = chaninfo->rw_vector.len[0] + chaninfo->rw_vector.len[1];
498 if (rec_nframes > total) {
499 DEBUG_TRACE (DEBUG::Butler, string_compose ("%1 overrun in %2, rec_nframes = %3 total space = %4\n",
500 DEBUG_THREAD_SELF, name(), rec_nframes, total));
505 Sample *incoming = buf.data (rec_offset);
506 samplecnt_t first = chaninfo->rw_vector.len[0];
508 memcpy (chaninfo->rw_vector.buf[0], incoming, sizeof (Sample) * first);
509 memcpy (chaninfo->rw_vector.buf[1], incoming + first, sizeof (Sample) * (rec_nframes - first));
512 chaninfo->wbuf->increment_write_ptr (rec_nframes);
520 // Pump entire port buffer into the ring buffer (TODO: split cycles?)
521 MidiBuffer& buf = bufs.get_midi (0);
522 boost::shared_ptr<MidiTrack> mt = boost::dynamic_pointer_cast<MidiTrack>(_track);
523 MidiChannelFilter* filter = mt ? &mt->capture_filter() : 0;
525 assert (buf.size() == 0 || _midi_buf);
527 for (MidiBuffer::iterator i = buf.begin(); i != buf.end(); ++i) {
528 Evoral::Event<MidiBuffer::TimeType> ev (*i, false);
529 if (ev.time() + rec_offset > rec_nframes) {
533 if (DEBUG_ENABLED(DEBUG::MidiIO)) {
534 const uint8_t* __data = ev.buffer();
536 DEBUG_STR_APPEND(a, string_compose ("mididiskstream %1 capture event @ %2 + %3 sz %4 ", this, ev.time(), start_sample, ev.size()));
537 for (size_t i=0; i < ev.size(); ++i) {
538 DEBUG_STR_APPEND(a,hex);
539 DEBUG_STR_APPEND(a,"0x");
540 DEBUG_STR_APPEND(a,(int)__data[i]);
541 DEBUG_STR_APPEND(a,' ');
543 DEBUG_STR_APPEND(a,'\n');
544 DEBUG_TRACE (DEBUG::MidiIO, DEBUG_STR(a).str());
547 /* Write events to the capture buffer in samples from session start,
548 but ignoring looping so event time progresses monotonically.
549 The source knows the loop length so it knows exactly where the
550 event occurs in the series of recorded loops and can implement
551 any desirable behaviour. We don't want to send event with
552 transport time here since that way the source can not
553 reconstruct their actual time; future clever MIDI looping should
554 probably be implemented in the source instead of here.
556 const samplecnt_t loop_offset = _num_captured_loops * loop_length;
557 const samplepos_t event_time = start_sample + loop_offset - _accumulated_capture_offset + ev.time();
558 if (event_time < 0 || event_time < _first_recordable_sample) {
559 /* Event out of range, skip */
563 bool skip_event = false;
565 /* skip injected immediate/out-of-band events */
566 MidiBuffer const& ieb (mt->immediate_event_buffer());
567 for (MidiBuffer::const_iterator j = ieb.begin(); j != ieb.end(); ++j) {
577 if (!filter || !filter->filter(ev.buffer(), ev.size())) {
578 _midi_buf->write (event_time, ev.event_type(), ev.size(), ev.buffer());
582 g_atomic_int_add (const_cast<gint*>(&_samples_pending_write), nframes);
584 if (buf.size() != 0) {
585 Glib::Threads::Mutex::Lock lm (_gui_feed_buffer_mutex, Glib::Threads::TRY_LOCK);
588 /* Copy this data into our GUI feed buffer and tell the GUI
589 that it can read it if it likes.
591 _gui_feed_buffer.clear ();
593 for (MidiBuffer::iterator i = buf.begin(); i != buf.end(); ++i) {
594 /* This may fail if buf is larger than _gui_feed_buffer, but it's not really
595 the end of the world if it does.
597 _gui_feed_buffer.push_back ((*i).time() + start_sample, (*i).size(), (*i).buffer());
601 DataRecorded (_midi_write_source); /* EMIT SIGNAL */
605 _capture_captured += rec_nframes;
606 DEBUG_TRACE (DEBUG::CaptureAlignment, string_compose ("%1 now captured %2 (by %3)\n", name(), _capture_captured, rec_nframes));
610 /* not recording this time, but perhaps we were before .. */
612 if (_was_recording) {
614 _accumulated_capture_offset = 0;
618 /* AUDIO BUTLER REQUIRED CODE */
620 if (_playlists[DataType::AUDIO] && !c->empty()) {
621 if (((samplecnt_t) c->front()->wbuf->read_space() >= _chunk_samples)) {
626 /* MIDI BUTLER REQUIRED CODE */
628 if (_playlists[DataType::MIDI] && _midi_buf && (_midi_buf->read_space() >= _midi_buf->bufsize() / 2)) {
632 // DEBUG_TRACE (DEBUG::Butler, string_compose ("%1 writer run, needs butler = %2\n", name(), _need_butler));
636 DiskWriter::finish_capture (boost::shared_ptr<ChannelList> c)
638 _was_recording = false;
639 _first_recordable_sample = max_samplepos;
640 _last_recordable_sample = max_samplepos;
642 if (_capture_captured == 0) {
646 if (recordable() && destructive()) {
647 for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
649 RingBufferNPT<CaptureTransition>::rw_vector transvec;
650 (*chan)->capture_transition_buf->get_write_vector(&transvec);
652 if (transvec.len[0] > 0) {
653 transvec.buf[0]->type = CaptureEnd;
654 transvec.buf[0]->capture_val = _capture_captured;
655 (*chan)->capture_transition_buf->increment_write_ptr(1);
659 fatal << string_compose (_("programmer error: %1"), X_("capture_transition_buf is full when stopping record! inconceivable!")) << endmsg;
665 CaptureInfo* ci = new CaptureInfo;
667 ci->start = _capture_start_sample;
668 ci->samples = _capture_captured;
670 DEBUG_TRACE (DEBUG::CaptureAlignment, string_compose ("Finish capture, add new CI, %1 + %2\n", ci->start, ci->samples));
672 /* XXX theoretical race condition here. Need atomic exchange ?
673 However, the circumstances when this is called right
674 now (either on record-disable or transport_stopped)
675 mean that no actual race exists. I think ...
676 We now have a capture_info_lock, but it is only to be used
677 to synchronize in the transport_stop and the capture info
678 accessors, so that invalidation will not occur (both non-realtime).
681 capture_info.push_back (ci);
682 _capture_captured = 0;
684 /* now we've finished a capture, reset first_recordable_sample for next time */
685 _first_recordable_sample = max_samplepos;
688 boost::shared_ptr<MidiBuffer>
689 DiskWriter::get_gui_feed_buffer () const
691 boost::shared_ptr<MidiBuffer> b (new MidiBuffer (AudioEngine::instance()->raw_buffer_size (DataType::MIDI)));
693 Glib::Threads::Mutex::Lock lm (_gui_feed_buffer_mutex);
694 b->copy (_gui_feed_buffer);
699 DiskWriter::set_record_enabled (bool yn)
701 if (!recordable() || !_session.record_enabling_legal() || record_safe ()) {
705 /* can't rec-enable in destructive mode if transport is before start */
707 if (destructive() && yn && _session.transport_sample() < _session.current_start_sample()) {
711 /* yes, i know that this not proof against race conditions, but its
712 good enough. i think.
715 if (record_enabled() != yn) {
717 engage_record_enable ();
719 disengage_record_enable ();
722 RecordEnableChanged (); /* EMIT SIGNAL */
727 DiskWriter::set_record_safe (bool yn)
729 if (!recordable() || !_session.record_enabling_legal() || channels.reader()->empty()) {
733 /* can't rec-safe in destructive mode if transport is before start ????
736 if (destructive() && yn && _session.transport_sample() < _session.current_start_sample()) {
740 /* yes, i know that this not proof against race conditions, but its
741 good enough. i think.
744 if (record_safe () != yn) {
746 engage_record_safe ();
748 disengage_record_safe ();
751 RecordSafeChanged (); /* EMIT SIGNAL */
756 DiskWriter::prep_record_enable ()
758 if (!recordable() || !_session.record_enabling_legal() || channels.reader()->empty() || record_safe ()) { // REQUIRES REVIEW "|| record_safe ()"
762 /* can't rec-enable in destructive mode if transport is before start */
764 if (destructive() && _session.transport_sample() < _session.current_start_sample()) {
768 boost::shared_ptr<ChannelList> c = channels.reader();
770 capturing_sources.clear ();
772 for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
773 capturing_sources.push_back ((*chan)->write_source);
774 Source::Lock lock((*chan)->write_source->mutex());
775 (*chan)->write_source->mark_streaming_write_started (lock);
782 DiskWriter::prep_record_disable ()
784 capturing_sources.clear ();
789 DiskWriter::buffer_load () const
791 boost::shared_ptr<ChannelList> c = channels.reader();
797 return (float) ((double) c->front()->wbuf->write_space()/
798 (double) c->front()->wbuf->bufsize());
802 DiskWriter::set_note_mode (NoteMode m)
806 boost::shared_ptr<MidiPlaylist> mp = boost::dynamic_pointer_cast<MidiPlaylist> (_playlists[DataType::MIDI]);
809 mp->set_note_mode (m);
812 if (_midi_write_source && _midi_write_source->model())
813 _midi_write_source->model()->set_note_mode(m);
817 DiskWriter::seek (samplepos_t sample, bool complete_refill)
820 ChannelList::iterator chan;
821 boost::shared_ptr<ChannelList> c = channels.reader();
823 for (n = 0, chan = c->begin(); chan != c->end(); ++chan, ++n) {
824 (*chan)->wbuf->reset ();
830 g_atomic_int_set(&_samples_read_from_ringbuffer, 0);
831 g_atomic_int_set(&_samples_written_to_ringbuffer, 0);
833 /* can't rec-enable in destructive mode if transport is before start */
835 if (destructive() && record_enabled() && sample < _session.current_start_sample()) {
836 disengage_record_enable ();
839 playback_sample = sample;
845 DiskWriter::do_flush (RunContext ctxt, bool force_flush)
849 RingBufferNPT<Sample>::rw_vector vector;
850 RingBufferNPT<CaptureTransition>::rw_vector transvec;
858 boost::shared_ptr<ChannelList> c = channels.reader();
859 for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
861 (*chan)->wbuf->get_read_vector (&vector);
863 total = vector.len[0] + vector.len[1];
865 if (total == 0 || (total < _chunk_samples && !force_flush && _was_recording)) {
869 /* if there are 2+ chunks of disk i/o possible for
870 this track, let the caller know so that it can arrange
871 for us to be called again, ASAP.
873 if we are forcing a flush, then if there is* any* extra
874 work, let the caller know.
876 if we are no longer recording and there is any extra work,
877 let the caller know too.
880 if (total >= 2 * _chunk_samples || ((force_flush || !_was_recording) && total > _chunk_samples)) {
884 to_write = min (_chunk_samples, (samplecnt_t) vector.len[0]);
886 // check the transition buffer when recording destructive
887 // important that we get this after the capture buf
890 (*chan)->capture_transition_buf->get_read_vector(&transvec);
891 size_t transcount = transvec.len[0] + transvec.len[1];
894 for (ti=0; ti < transcount; ++ti) {
895 CaptureTransition & captrans = (ti < transvec.len[0]) ? transvec.buf[0][ti] : transvec.buf[1][ti-transvec.len[0]];
897 if (captrans.type == CaptureStart) {
898 // by definition, the first data we got above represents the given capture pos
900 (*chan)->write_source->mark_capture_start (captrans.capture_val);
901 (*chan)->curr_capture_cnt = 0;
903 } else if (captrans.type == CaptureEnd) {
905 // capture end, the capture_val represents total samples in capture
907 if (captrans.capture_val <= (*chan)->curr_capture_cnt + to_write) {
909 // shorten to make the write a perfect fit
910 uint32_t nto_write = (captrans.capture_val - (*chan)->curr_capture_cnt);
912 if (nto_write < to_write) {
913 ret = 1; // should we?
915 to_write = nto_write;
917 (*chan)->write_source->mark_capture_end ();
919 // increment past this transition, but go no further
924 // actually ends just beyond this chunk, so force more work
932 (*chan)->capture_transition_buf->increment_read_ptr(ti);
936 if ((!(*chan)->write_source) || (*chan)->write_source->write (vector.buf[0], to_write) != to_write) {
937 error << string_compose(_("AudioDiskstream %1: cannot write to disk"), id()) << endmsg;
941 (*chan)->wbuf->increment_read_ptr (to_write);
942 (*chan)->curr_capture_cnt += to_write;
944 if ((to_write == vector.len[0]) && (total > to_write) && (to_write < _chunk_samples) && !destructive()) {
946 /* we wrote all of vector.len[0] but it wasn't an entire
947 disk_write_chunk_samples of data, so arrange for some part
948 of vector.len[1] to be flushed to disk as well.
951 to_write = min ((samplecnt_t)(_chunk_samples - to_write), (samplecnt_t) vector.len[1]);
953 DEBUG_TRACE (DEBUG::Butler, string_compose ("%1 additional write of %2\n", name(), to_write));
955 if ((*chan)->write_source->write (vector.buf[1], to_write) != to_write) {
956 error << string_compose(_("AudioDiskstream %1: cannot write to disk"), id()) << endmsg;
960 (*chan)->wbuf->increment_read_ptr (to_write);
961 (*chan)->curr_capture_cnt += to_write;
967 if (_midi_write_source && _midi_buf) {
969 const samplecnt_t total = g_atomic_int_get(const_cast<gint*> (&_samples_pending_write));
972 _midi_buf->read_space() == 0 ||
973 (!force_flush && (total < _chunk_samples) && _was_recording)) {
977 /* if there are 2+ chunks of disk i/o possible for
978 this track), let the caller know so that it can arrange
979 for us to be called again, ASAP.
981 if we are forcing a flush, then if there is* any* extra
982 work, let the caller know.
984 if we are no longer recording and there is any extra work,
985 let the caller know too.
988 if (total >= 2 * _chunk_samples || ((force_flush || !_was_recording) && total > _chunk_samples)) {
993 /* push out everything we have, right now */
996 to_write = _chunk_samples;
999 if (record_enabled() && ((total > _chunk_samples) || force_flush)) {
1000 Source::Lock lm(_midi_write_source->mutex());
1001 if (_midi_write_source->midi_write (lm, *_midi_buf, get_capture_start_sample (0), to_write) != to_write) {
1002 error << string_compose(_("MidiDiskstream %1: cannot write to disk"), id()) << endmsg;
1005 g_atomic_int_add(const_cast<gint*> (&_samples_pending_write), -to_write);
1015 DiskWriter::reset_write_sources (bool mark_write_complete, bool /*force*/)
1017 ChannelList::iterator chan;
1018 boost::shared_ptr<ChannelList> c = channels.reader();
1021 if (!_session.writable() || !recordable()) {
1025 capturing_sources.clear ();
1027 for (chan = c->begin(), n = 0; chan != c->end(); ++chan, ++n) {
1029 if (!destructive()) {
1031 if ((*chan)->write_source) {
1033 if (mark_write_complete) {
1034 Source::Lock lock((*chan)->write_source->mutex());
1035 (*chan)->write_source->mark_streaming_write_completed (lock);
1036 (*chan)->write_source->done_with_peakfile_writes ();
1039 if ((*chan)->write_source->removable()) {
1040 (*chan)->write_source->mark_for_remove ();
1041 (*chan)->write_source->drop_references ();
1044 (*chan)->write_source.reset ();
1047 use_new_write_source (DataType::AUDIO, n);
1049 if (record_enabled()) {
1050 capturing_sources.push_back ((*chan)->write_source);
1055 if ((*chan)->write_source == 0) {
1056 use_new_write_source (DataType::AUDIO, n);
1061 if (_midi_write_source) {
1062 if (mark_write_complete) {
1063 Source::Lock lm(_midi_write_source->mutex());
1064 _midi_write_source->mark_streaming_write_completed (lm);
1068 if (_playlists[DataType::MIDI]) {
1069 use_new_write_source (DataType::MIDI);
1072 if (destructive() && !c->empty ()) {
1074 /* we now have all our write sources set up, so create the
1075 playlist's single region.
1078 if (_playlists[DataType::MIDI]->empty()) {
1079 setup_destructive_playlist ();
1085 DiskWriter::use_new_write_source (DataType dt, uint32_t n)
1087 if (dt == DataType::MIDI) {
1089 _accumulated_capture_offset = 0;
1090 _midi_write_source.reset();
1093 _midi_write_source = boost::dynamic_pointer_cast<SMFSource>(
1094 _session.create_midi_source_for_session (write_source_name ()));
1096 if (!_midi_write_source) {
1097 throw failed_constructor();
1101 catch (failed_constructor &err) {
1102 error << string_compose (_("%1:%2 new capture file not initialized correctly"), _name, n) << endmsg;
1103 _midi_write_source.reset();
1107 boost::shared_ptr<ChannelList> c = channels.reader();
1109 if (!recordable()) {
1113 if (n >= c->size()) {
1114 error << string_compose (_("AudioDiskstream: channel %1 out of range"), n) << endmsg;
1118 ChannelInfo* chan = (*c)[n];
1121 if ((chan->write_source = _session.create_audio_source_for_session (
1122 c->size(), write_source_name(), n, destructive())) == 0) {
1123 throw failed_constructor();
1127 catch (failed_constructor &err) {
1128 error << string_compose (_("%1:%2 new capture file not initialized correctly"), _name, n) << endmsg;
1129 chan->write_source.reset ();
1133 /* do not remove destructive files even if they are empty */
1135 chan->write_source->set_allow_remove_if_empty (!destructive());
1142 DiskWriter::transport_stopped_wallclock (struct tm& when, time_t twhen, bool abort_capture)
1144 bool more_work = true;
1146 samplecnt_t total_capture;
1147 SourceList audio_srcs;
1148 SourceList midi_srcs;
1149 ChannelList::iterator chan;
1150 vector<CaptureInfo*>::iterator ci;
1151 boost::shared_ptr<ChannelList> c = channels.reader();
1153 bool mark_write_completed = false;
1158 /* butler is already stopped, but there may be work to do
1159 to flush remaining data to disk.
1162 while (more_work && !err) {
1163 switch (do_flush (TransportContext, true)) {
1170 error << string_compose(_("AudioDiskstream \"%1\": cannot flush captured data to disk!"), _name) << endmsg;
1175 /* XXX is there anything we can do if err != 0 ? */
1176 Glib::Threads::Mutex::Lock lm (capture_info_lock);
1178 if (capture_info.empty()) {
1182 if (abort_capture) {
1184 if (destructive()) {
1188 for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
1190 if ((*chan)->write_source) {
1192 (*chan)->write_source->mark_for_remove ();
1193 (*chan)->write_source->drop_references ();
1194 (*chan)->write_source.reset ();
1197 /* new source set up in "out" below */
1200 if (_midi_write_source) {
1201 _midi_write_source->mark_for_remove ();
1202 _midi_write_source->drop_references ();
1203 _midi_write_source.reset();
1209 for (total_capture = 0, ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1210 total_capture += (*ci)->samples;
1213 /* figure out the name for this take */
1215 for (n = 0, chan = c->begin(); chan != c->end(); ++chan, ++n) {
1217 boost::shared_ptr<AudioFileSource> as = (*chan)->write_source;
1220 audio_srcs.push_back (as);
1221 as->update_header (capture_info.front()->start, when, twhen);
1222 as->set_captured_for (_name.val());
1223 as->mark_immutable ();
1226 strftime (buf, sizeof(buf), "%F %H.%M.%S", &when);
1227 as->set_take_id ( buf );
1229 if (Config->get_auto_analyse_audio()) {
1230 Analyser::queue_source_for_analysis (as, true);
1233 DEBUG_TRACE (DEBUG::CaptureAlignment, string_compose ("newly captured source %1 length %2\n", as->path(), as->length (0)));
1236 if (_midi_write_source) {
1237 midi_srcs.push_back (_midi_write_source);
1240 (*chan)->write_source->stamp (twhen);
1242 /* "re-announce the source to the world */
1243 Source::SourcePropertyChanged ((*chan)->write_source);
1249 if (_midi_write_source) {
1251 if (_midi_write_source->length (capture_info.front()->start) == 0) {
1252 /* No data was recorded, so this capture will
1253 effectively be aborted; do the same as we
1254 do for an explicit abort.
1256 if (_midi_write_source) {
1257 _midi_write_source->mark_for_remove ();
1258 _midi_write_source->drop_references ();
1259 _midi_write_source.reset();
1265 /* phew, we have data */
1267 Source::Lock source_lock(_midi_write_source->mutex());
1269 /* figure out the name for this take */
1271 midi_srcs.push_back (_midi_write_source);
1273 _midi_write_source->set_natural_position (capture_info.front()->start);
1274 _midi_write_source->set_captured_for (_name);
1277 strftime (buf, sizeof(buf), "%F %H.%M.%S", &when);
1278 _midi_write_source->set_take_id ( buf );
1280 /* set length in beats to entire capture length */
1282 BeatsSamplesConverter converter (_session.tempo_map(), capture_info.front()->start);
1283 const Temporal::Beats total_capture_beats = converter.from (total_capture);
1284 _midi_write_source->set_length_beats (total_capture_beats);
1286 /* flush to disk: this step differs from the audio path,
1287 where all the data is already on disk.
1290 _midi_write_source->mark_midi_streaming_write_completed (source_lock, Evoral::Sequence<Temporal::Beats>::ResolveStuckNotes, total_capture_beats);
1293 _last_capture_sources.insert (_last_capture_sources.end(), audio_srcs.begin(), audio_srcs.end());
1294 _last_capture_sources.insert (_last_capture_sources.end(), midi_srcs.begin(), midi_srcs.end());
1298 _track->use_captured_sources (audio_srcs, capture_info);
1299 _track->use_captured_sources (midi_srcs, capture_info);
1302 mark_write_completed = true;
1305 reset_write_sources (mark_write_completed);
1309 for (ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1313 capture_info.clear ();
1314 _capture_start_sample = 0;
1318 DiskWriter::transport_looped (samplepos_t transport_sample)
1320 if (_was_recording) {
1321 // all we need to do is finish this capture, with modified capture length
1322 boost::shared_ptr<ChannelList> c = channels.reader();
1326 // the next region will start recording via the normal mechanism
1327 // we'll set the start position to the current transport pos
1328 // no latency adjustment or capture offset needs to be made, as that already happened the first time
1329 _capture_start_sample = transport_sample;
1330 _first_recordable_sample = transport_sample; // mild lie
1331 _last_recordable_sample = max_samplepos;
1332 _was_recording = true;
1334 if (recordable() && destructive()) {
1335 for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
1337 RingBufferNPT<CaptureTransition>::rw_vector transvec;
1338 (*chan)->capture_transition_buf->get_write_vector(&transvec);
1340 if (transvec.len[0] > 0) {
1341 transvec.buf[0]->type = CaptureStart;
1342 transvec.buf[0]->capture_val = _capture_start_sample;
1343 (*chan)->capture_transition_buf->increment_write_ptr(1);
1347 fatal << X_("programming error: capture_transition_buf is full on rec loop! inconceivable!")
1355 /* Here we only keep track of the number of captured loops so monotonic
1356 event times can be delivered to the write source in process(). Trying
1357 to be clever here is a world of trouble, it is better to simply record
1358 the input in a straightforward non-destructive way. In the future when
1359 we want to implement more clever MIDI looping modes it should be done in
1360 the Source and/or entirely after the capture is finished.
1362 if (_was_recording) {
1363 g_atomic_int_add(const_cast<gint*> (&_num_captured_loops), 1);
1368 DiskWriter::setup_destructive_playlist ()
1371 boost::shared_ptr<ChannelList> c = channels.reader();
1373 for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
1374 srcs.push_back ((*chan)->write_source);
1377 /* a single full-sized region */
1379 assert (!srcs.empty ());
1382 plist.add (Properties::name, _name.val());
1383 plist.add (Properties::start, 0);
1384 plist.add (Properties::length, max_samplepos - srcs.front()->natural_position());
1386 boost::shared_ptr<Region> region (RegionFactory::create (srcs, plist));
1387 _playlists[DataType::AUDIO]->add_region (region, srcs.front()->natural_position());
1389 /* apply region properties and update write sources */
1390 use_destructive_playlist();
1394 DiskWriter::use_destructive_playlist ()
1396 /* this is called from the XML-based constructor or ::set_destructive. when called,
1397 we already have a playlist and a region, but we need to
1398 set up our sources for write. we use the sources associated
1399 with the (presumed single, full-extent) region.
1402 boost::shared_ptr<Region> rp;
1404 const RegionList& rl (_playlists[DataType::AUDIO]->region_list_property().rlist());
1405 if (rl.size() > 0) {
1406 /* this can happen when dragging a region onto a tape track */
1407 assert((rl.size() == 1));
1413 reset_write_sources (false, true);
1417 boost::shared_ptr<AudioRegion> region = boost::dynamic_pointer_cast<AudioRegion> (rp);
1420 throw failed_constructor();
1423 /* be sure to stretch the region out to the maximum length (non-musical)*/
1425 region->set_length (max_samplepos - region->position(), 0);
1428 ChannelList::iterator chan;
1429 boost::shared_ptr<ChannelList> c = channels.reader();
1431 for (n = 0, chan = c->begin(); chan != c->end(); ++chan, ++n) {
1432 (*chan)->write_source = boost::dynamic_pointer_cast<AudioFileSource>(region->source (n));
1433 assert((*chan)->write_source);
1434 (*chan)->write_source->set_allow_remove_if_empty (false);
1436 // should be set when creating the source or loading the state
1437 assert ((*chan)->write_source->destructive());
1440 /* the source list will never be reset for a destructive track */
1444 DiskWriter::adjust_buffering ()
1446 boost::shared_ptr<ChannelList> c = channels.reader();
1448 for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
1449 (*chan)->resize (_session.butler()->audio_capture_buffer_size());
1454 DiskWriter::realtime_handle_transport_stopped ()
1459 DiskWriter::set_name (string const & str)
1461 string my_name = X_("recorder:");
1464 if (_name != my_name) {
1465 SessionObject::set_name (my_name);
1472 DiskWriter::steal_write_source_name ()
1474 if (_playlists[DataType::MIDI]) {
1475 string our_old_name = _midi_write_source->name();
1477 /* this will bump the name of the current write source to the next one
1478 * (e.g. "MIDI 1-1" gets renamed to "MIDI 1-2"), thus leaving the
1479 * current write source name (e.g. "MIDI 1-1" available). See the
1480 * comments in Session::create_midi_source_by_stealing_name() about why
1485 string new_path = _session.new_midi_source_path (name());
1487 if (_midi_write_source->rename (new_path)) {
1494 return our_old_name;
1497 return std::string();
1501 DiskWriter::configure_io (ChanCount in, ChanCount out)
1503 bool changed = false;
1505 boost::shared_ptr<ChannelList> c = channels.reader();
1506 if (in.n_audio() != c->size()) {
1509 if ((0 == in.n_midi ()) != (0 == _midi_buf)) {
1515 if (!DiskIOProcessor::configure_io (in, out)) {
1519 if (record_enabled() || changed) {
1520 reset_write_sources (false, true);