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 "ardour/analyser.h"
21 #include "ardour/audioengine.h"
22 #include "ardour/audiofilesource.h"
23 #include "ardour/audio_buffer.h"
24 #include "ardour/audioplaylist.h"
25 #include "ardour/audioregion.h"
26 #include "ardour/butler.h"
27 #include "ardour/debug.h"
28 #include "ardour/disk_writer.h"
29 #include "ardour/midi_playlist.h"
30 #include "ardour/midi_source.h"
31 #include "ardour/midi_track.h"
32 #include "ardour/port.h"
33 #include "ardour/region_factory.h"
34 #include "ardour/session.h"
35 #include "ardour/smf_source.h"
39 using namespace ARDOUR;
43 ARDOUR::samplecnt_t DiskWriter::_chunk_samples = DiskWriter::default_chunk_samples ();
44 PBD::Signal0<void> DiskWriter::Overrun;
46 DiskWriter::DiskWriter (Session& s, string const & str, DiskIOProcessor::Flag f)
47 : DiskIOProcessor (s, str, f)
50 , capture_start_sample (0)
51 , capture_captured (0)
52 , was_recording (false)
53 , first_recordable_sample (max_samplepos)
54 , last_recordable_sample (max_samplepos)
55 , last_possibly_recording (0)
56 , _alignment_style (ExistingMaterial)
57 , _num_captured_loops (0)
58 , _accumulated_capture_offset (0)
59 , _gui_feed_buffer(AudioEngine::instance()->raw_buffer_size (DataType::MIDI))
61 DiskIOProcessor::init ();
64 DiskWriter::~DiskWriter ()
66 DEBUG_TRACE (DEBUG::Destruction, string_compose ("DiskWriter %1 @ %2 deleted\n", _name, this));
68 boost::shared_ptr<ChannelList> c = channels.reader();
70 for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
71 (*chan)->write_source.reset ();
76 DiskWriter::default_chunk_samples ()
82 DiskWriter::set_write_source_name (string const & str)
84 _write_source_name = str;
89 DiskWriter::check_record_status (samplepos_t transport_sample, double speed, bool can_record)
91 int possibly_recording;
92 const int transport_rolling = 0x4;
93 const int track_rec_enabled = 0x2;
94 const int global_rec_enabled = 0x1;
95 const int fully_rec_enabled = (transport_rolling |track_rec_enabled | global_rec_enabled);
97 /* merge together the 3 factors that affect record status, and compute what has changed. */
99 possibly_recording = (speed != 0.0f ? 4 : 0) | (record_enabled() ? 2 : 0) | (can_record ? 1 : 0);
101 if (possibly_recording == last_possibly_recording) {
105 if (possibly_recording == fully_rec_enabled) {
107 if (last_possibly_recording == fully_rec_enabled) {
112 if (_session.config.get_punch_in () && 0 != (loc = _session.locations()->auto_punch_location ())) {
113 capture_start_sample = loc->start ();
115 capture_start_sample = _session.transport_sample ();
118 first_recordable_sample = capture_start_sample;
120 if (_alignment_style == ExistingMaterial) {
121 first_recordable_sample += _capture_offset + _playback_offset;
124 if (_session.config.get_punch_out () && 0 != (loc = _session.locations()->auto_punch_location ())) {
125 /* this freezes the punch-out point when starting to record.
127 * We should allow to move it or at least allow to disable punch-out
130 last_recordable_sample = loc->end ();
131 if (_alignment_style == ExistingMaterial) {
132 last_recordable_sample += _capture_offset + _playback_offset;
135 last_recordable_sample = max_samplepos;
138 DEBUG_TRACE (DEBUG::CaptureAlignment, string_compose ("%1: @ %2 (STS: %3) CS:%4 FRS: %5 IL: %7, OL: %8 CO: %r9 PO: %10 WOL: %11 WIL: %12\n",
141 _session.transport_sample(),
142 capture_start_sample,
143 first_recordable_sample,
144 last_recordable_sample,
149 _session.worst_output_latency(),
150 _session.worst_input_latency()));
153 prepare_record_status (capture_start_sample);
157 last_possibly_recording = possibly_recording;
161 DiskWriter::calculate_record_range (Evoral::OverlapType ot, samplepos_t transport_sample, samplecnt_t nframes, samplecnt_t & rec_nframes, samplecnt_t & rec_offset)
164 case Evoral::OverlapNone:
168 case Evoral::OverlapInternal:
169 /* ---------- recrange
172 rec_nframes = nframes;
176 case Evoral::OverlapStart:
177 /* |--------| recrange
180 rec_nframes = transport_sample + nframes - first_recordable_sample;
182 rec_offset = first_recordable_sample - transport_sample;
186 case Evoral::OverlapEnd:
187 /* |--------| recrange
188 * |-------- transrange
190 rec_nframes = last_recordable_sample - transport_sample;
194 case Evoral::OverlapExternal:
195 /* |--------| recrange
196 * -------------- transrange
198 rec_nframes = last_recordable_sample - first_recordable_sample;
199 rec_offset = first_recordable_sample - transport_sample;
203 DEBUG_TRACE (DEBUG::CaptureAlignment, string_compose ("%1 rec? %2 @ %3 (for %4) FRF %5 LRF %6 : rf %7 @ %8\n",
204 _name, enum_2_string (ot), transport_sample, nframes,
205 first_recordable_sample, last_recordable_sample, rec_nframes, rec_offset));
209 DiskWriter::engage_record_enable ()
211 g_atomic_int_set (&_record_enabled, 1);
215 DiskWriter::disengage_record_enable ()
217 g_atomic_int_set (&_record_enabled, 0);
221 DiskWriter::engage_record_safe ()
223 g_atomic_int_set (&_record_safe, 1);
227 DiskWriter::disengage_record_safe ()
229 g_atomic_int_set (&_record_safe, 0);
232 /** Get the start position (in session samples) of the nth capture in the current pass */
234 DiskWriter::get_capture_start_sample (uint32_t n) const
236 Glib::Threads::Mutex::Lock lm (capture_info_lock);
238 if (capture_info.size() > n) {
239 /* this is a completed capture */
240 return capture_info[n]->start;
242 /* this is the currently in-progress capture */
243 return capture_start_sample;
248 DiskWriter::get_captured_samples (uint32_t n) const
250 Glib::Threads::Mutex::Lock lm (capture_info_lock);
252 if (capture_info.size() > n) {
253 /* this is a completed capture */
254 return capture_info[n]->samples;
256 /* this is the currently in-progress capture */
257 return capture_captured;
262 DiskWriter::set_align_style (AlignStyle a, bool force)
264 if (record_enabled() && _session.actively_recording()) {
268 if ((a != _alignment_style) || force) {
269 _alignment_style = a;
270 AlignmentStyleChanged ();
275 DiskWriter::state (bool full)
277 XMLNode& node (DiskIOProcessor::state (full));
278 node.set_property (X_("type"), X_("diskwriter"));
279 node.set_property (X_("record-safe"), (_record_safe ? X_("yes" : "no")));
284 DiskWriter::set_state (const XMLNode& node, int version)
286 if (DiskIOProcessor::set_state (node, version)) {
290 if (!node.get_property (X_("record-safe"), _record_safe)) {
291 _record_safe = false;
294 reset_write_sources (false, true);
300 DiskWriter::non_realtime_locate (samplepos_t position)
302 if (_midi_write_source) {
303 _midi_write_source->set_timeline_position (position);
306 DiskIOProcessor::non_realtime_locate (position);
311 DiskWriter::prepare_record_status (samplepos_t capture_start_sample)
313 if (recordable() && destructive()) {
314 boost::shared_ptr<ChannelList> c = channels.reader ();
315 for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
317 RingBufferNPT<CaptureTransition>::rw_vector transitions;
318 (*chan)->capture_transition_buf->get_write_vector (&transitions);
320 if (transitions.len[0] > 0) {
321 transitions.buf[0]->type = CaptureStart;
322 transitions.buf[0]->capture_val = capture_start_sample;
323 (*chan)->capture_transition_buf->increment_write_ptr(1);
326 fatal << X_("programming error: capture_transition_buf is full on rec start! inconceivable!")
334 /** Do some record stuff [not described in this comment!]
337 * - Setup playback_distance with the nframes, or nframes adjusted
338 * for current varispeed, if appropriate.
339 * - Setup current_playback_buffer in each ChannelInfo to point to data
340 * that someone can read playback_distance worth of data from.
343 DiskWriter::run (BufferSet& bufs, samplepos_t start_sample, samplepos_t end_sample,
344 double speed, pframes_t nframes, bool result_required)
346 if (!_active && !_pending_active) {
349 _active = _pending_active;
352 boost::shared_ptr<ChannelList> c = channels.reader();
353 ChannelList::iterator chan;
355 samplecnt_t rec_offset = 0;
356 samplecnt_t rec_nframes = 0;
357 bool nominally_recording;
359 bool re = record_enabled ();
360 bool punch_in = _session.config.get_punch_in () && _session.locations()->auto_punch_location ();
361 bool can_record = _session.actively_recording ();
362 can_record |= _session.get_record_enabled () && punch_in && _session.transport_sample () <= _session.locations()->auto_punch_location ()->start ();
364 _need_butler = false;
367 if (speed != 0 && re) {
368 DEBUG_TRACE (DEBUG::CaptureAlignment, string_compose ("%1: run() start: %2 end: %3 NF: %4\n", _name, start_sample, end_sample, nframes));
372 check_record_status (start_sample, speed, can_record);
378 nominally_recording = (can_record && re);
380 // Safeguard against situations where process() goes haywire when autopunching
381 // and last_recordable_sample < first_recordable_sample
383 if (last_recordable_sample < first_recordable_sample) {
384 last_recordable_sample = max_samplepos;
387 const Location* const loop_loc = loop_location;
388 samplepos_t loop_start = 0;
389 samplepos_t loop_end = 0;
390 samplepos_t loop_length = 0;
393 get_location_times (loop_loc, &loop_start, &loop_end, &loop_length);
396 if (nominally_recording || (re && was_recording && _session.get_record_enabled() && punch_in)) {
398 Evoral::OverlapType ot = Evoral::coverage (first_recordable_sample, last_recordable_sample, start_sample, end_sample);
399 // XXX should this be transport_sample + nframes - 1 ? coverage() expects its parameter ranges to include their end points
400 // XXX also, first_recordable_sample & last_recordable_sample may both be == max_samplepos: coverage() will return OverlapNone in that case. Is thak OK?
401 calculate_record_range (ot, start_sample, nframes, rec_nframes, rec_offset);
403 DEBUG_TRACE (DEBUG::CaptureAlignment, string_compose ("%1: this time record %2 of %3 samples, offset %4\n", _name, rec_nframes, nframes, rec_offset));
405 if (rec_nframes && !was_recording) {
406 capture_captured = 0;
409 /* Loop recording, so pretend the capture started at the loop
410 start rgardless of what time it is now, so the source starts
411 at the loop start and can handle time wrapping around.
412 Otherwise, start the source right now as usual.
414 capture_captured = start_sample - loop_start;
415 capture_start_sample = loop_start;
418 if (_midi_write_source) {
419 _midi_write_source->mark_write_starting_now (capture_start_sample, capture_captured, loop_length);
422 g_atomic_int_set (const_cast<gint*> (&_samples_pending_write), 0);
423 g_atomic_int_set (const_cast<gint*> (&_num_captured_loops), 0);
425 was_recording = true;
429 /* For audio: not writing samples to the capture ringbuffer offsets
430 * the recording. For midi: we need to keep track of the record range
431 * and subtract the accumulated difference from the event time.
434 _accumulated_capture_offset += rec_offset;
436 _accumulated_capture_offset += nframes;
441 if (can_record && !_last_capture_sources.empty ()) {
442 _last_capture_sources.clear ();
449 const size_t n_buffers = bufs.count().n_audio();
451 for (chan = c->begin(), n = 0; chan != c->end(); ++chan, ++n) {
453 ChannelInfo* chaninfo (*chan);
454 AudioBuffer& buf (bufs.get_audio (n%n_buffers));
456 chaninfo->buf->get_write_vector (&chaninfo->rw_vector);
458 if (rec_nframes <= (samplecnt_t) chaninfo->rw_vector.len[0]) {
460 Sample *incoming = buf.data (rec_offset);
461 memcpy (chaninfo->rw_vector.buf[0], incoming, sizeof (Sample) * rec_nframes);
465 samplecnt_t total = chaninfo->rw_vector.len[0] + chaninfo->rw_vector.len[1];
467 if (rec_nframes > total) {
468 DEBUG_TRACE (DEBUG::Butler, string_compose ("%1 overrun in %2, rec_nframes = %3 total space = %4\n",
469 DEBUG_THREAD_SELF, name(), rec_nframes, total));
474 Sample *incoming = buf.data (rec_offset);
475 samplecnt_t first = chaninfo->rw_vector.len[0];
477 memcpy (chaninfo->rw_vector.buf[0], incoming, sizeof (Sample) * first);
478 memcpy (chaninfo->rw_vector.buf[1], incoming + first, sizeof (Sample) * (rec_nframes - first));
481 chaninfo->buf->increment_write_ptr (rec_nframes);
487 // Pump entire port buffer into the ring buffer (TODO: split cycles?)
488 MidiBuffer& buf = bufs.get_midi (0);
489 boost::shared_ptr<MidiTrack> mt = boost::dynamic_pointer_cast<MidiTrack>(_route);
490 MidiChannelFilter* filter = mt ? &mt->capture_filter() : 0;
492 for (MidiBuffer::iterator i = buf.begin(); i != buf.end(); ++i) {
493 Evoral::Event<MidiBuffer::TimeType> ev (*i, false);
494 if (ev.time() + rec_offset > rec_nframes) {
498 if (DEBUG_ENABLED(DEBUG::MidiIO)) {
499 const uint8_t* __data = ev.buffer();
501 DEBUG_STR_APPEND(a, string_compose ("mididiskstream %1 capture event @ %2 + %3 sz %4 ", this, ev.time(), start_sample, ev.size()));
502 for (size_t i=0; i < ev.size(); ++i) {
503 DEBUG_STR_APPEND(a,hex);
504 DEBUG_STR_APPEND(a,"0x");
505 DEBUG_STR_APPEND(a,(int)__data[i]);
506 DEBUG_STR_APPEND(a,' ');
508 DEBUG_STR_APPEND(a,'\n');
509 DEBUG_TRACE (DEBUG::MidiIO, DEBUG_STR(a).str());
512 /* Write events to the capture buffer in samples from session start,
513 but ignoring looping so event time progresses monotonically.
514 The source knows the loop length so it knows exactly where the
515 event occurs in the series of recorded loops and can implement
516 any desirable behaviour. We don't want to send event with
517 transport time here since that way the source can not
518 reconstruct their actual time; future clever MIDI looping should
519 probably be implemented in the source instead of here.
521 const samplecnt_t loop_offset = _num_captured_loops * loop_length;
522 const samplepos_t event_time = start_sample + loop_offset - _accumulated_capture_offset + ev.time();
523 if (event_time < 0 || event_time < first_recordable_sample) {
524 /* Event out of range, skip */
528 if (!filter || !filter->filter(ev.buffer(), ev.size())) {
529 _midi_buf->write (event_time, ev.event_type(), ev.size(), ev.buffer());
532 g_atomic_int_add (const_cast<gint*>(&_samples_pending_write), nframes);
534 if (buf.size() != 0) {
535 Glib::Threads::Mutex::Lock lm (_gui_feed_buffer_mutex, Glib::Threads::TRY_LOCK);
538 /* Copy this data into our GUI feed buffer and tell the GUI
539 that it can read it if it likes.
541 _gui_feed_buffer.clear ();
543 for (MidiBuffer::iterator i = buf.begin(); i != buf.end(); ++i) {
544 /* This may fail if buf is larger than _gui_feed_buffer, but it's not really
545 the end of the world if it does.
547 _gui_feed_buffer.push_back ((*i).time() + start_sample, (*i).size(), (*i).buffer());
551 DataRecorded (_midi_write_source); /* EMIT SIGNAL */
554 capture_captured += rec_nframes;
555 DEBUG_TRACE (DEBUG::CaptureAlignment, string_compose ("%1 now captured %2 (by %3)\n", name(), capture_captured, rec_nframes));
559 /* not recording this time, but perhaps we were before .. */
563 _accumulated_capture_offset = 0;
567 /* AUDIO BUTLER REQUIRED CODE */
569 if (_playlists[DataType::AUDIO] && !c->empty()) {
570 if (((samplecnt_t) c->front()->buf->read_space() >= _chunk_samples)) {
575 /* MIDI BUTLER REQUIRED CODE */
577 if (_playlists[DataType::MIDI] && (_midi_buf->read_space() < _midi_buf->bufsize() / 2)) {
581 // DEBUG_TRACE (DEBUG::Butler, string_compose ("%1 writer run, needs butler = %2\n", name(), _need_butler));
585 DiskWriter::finish_capture (boost::shared_ptr<ChannelList> c)
587 was_recording = false;
588 first_recordable_sample = max_samplepos;
589 last_recordable_sample = max_samplepos;
591 if (capture_captured == 0) {
595 if (recordable() && destructive()) {
596 for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
598 RingBufferNPT<CaptureTransition>::rw_vector transvec;
599 (*chan)->capture_transition_buf->get_write_vector(&transvec);
601 if (transvec.len[0] > 0) {
602 transvec.buf[0]->type = CaptureEnd;
603 transvec.buf[0]->capture_val = capture_captured;
604 (*chan)->capture_transition_buf->increment_write_ptr(1);
608 fatal << string_compose (_("programmer error: %1"), X_("capture_transition_buf is full when stopping record! inconceivable!")) << endmsg;
614 CaptureInfo* ci = new CaptureInfo;
616 ci->start = capture_start_sample;
617 ci->samples = capture_captured;
619 DEBUG_TRACE (DEBUG::CaptureAlignment, string_compose ("Finish capture, add new CI, %1 + %2\n", ci->start, ci->samples));
621 /* XXX theoretical race condition here. Need atomic exchange ?
622 However, the circumstances when this is called right
623 now (either on record-disable or transport_stopped)
624 mean that no actual race exists. I think ...
625 We now have a capture_info_lock, but it is only to be used
626 to synchronize in the transport_stop and the capture info
627 accessors, so that invalidation will not occur (both non-realtime).
630 capture_info.push_back (ci);
631 capture_captured = 0;
633 /* now we've finished a capture, reset first_recordable_sample for next time */
634 first_recordable_sample = max_samplepos;
638 DiskWriter::set_record_enabled (bool yn)
640 if (!recordable() || !_session.record_enabling_legal() || record_safe ()) {
644 /* can't rec-enable in destructive mode if transport is before start */
646 if (destructive() && yn && _session.transport_sample() < _session.current_start_sample()) {
650 /* yes, i know that this not proof against race conditions, but its
651 good enough. i think.
654 if (record_enabled() != yn) {
656 engage_record_enable ();
658 disengage_record_enable ();
661 RecordEnableChanged (); /* EMIT SIGNAL */
666 DiskWriter::set_record_safe (bool yn)
668 if (!recordable() || !_session.record_enabling_legal() || channels.reader()->empty()) {
672 /* can't rec-safe in destructive mode if transport is before start ????
675 if (destructive() && yn && _session.transport_sample() < _session.current_start_sample()) {
679 /* yes, i know that this not proof against race conditions, but its
680 good enough. i think.
683 if (record_safe () != yn) {
685 engage_record_safe ();
687 disengage_record_safe ();
690 RecordSafeChanged (); /* EMIT SIGNAL */
695 DiskWriter::prep_record_enable ()
697 if (!recordable() || !_session.record_enabling_legal() || channels.reader()->empty() || record_safe ()) { // REQUIRES REVIEW "|| record_safe ()"
701 /* can't rec-enable in destructive mode if transport is before start */
703 if (destructive() && _session.transport_sample() < _session.current_start_sample()) {
707 boost::shared_ptr<ChannelList> c = channels.reader();
709 capturing_sources.clear ();
711 for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
712 capturing_sources.push_back ((*chan)->write_source);
713 Source::Lock lock((*chan)->write_source->mutex());
714 (*chan)->write_source->mark_streaming_write_started (lock);
721 DiskWriter::prep_record_disable ()
723 capturing_sources.clear ();
728 DiskWriter::buffer_load () const
730 boost::shared_ptr<ChannelList> c = channels.reader();
736 return (float) ((double) c->front()->buf->write_space()/
737 (double) c->front()->buf->bufsize());
741 DiskWriter::set_note_mode (NoteMode m)
745 boost::shared_ptr<MidiPlaylist> mp = boost::dynamic_pointer_cast<MidiPlaylist> (_playlists[DataType::MIDI]);
748 mp->set_note_mode (m);
751 if (_midi_write_source && _midi_write_source->model())
752 _midi_write_source->model()->set_note_mode(m);
756 DiskWriter::seek (samplepos_t sample, bool complete_refill)
759 ChannelList::iterator chan;
760 boost::shared_ptr<ChannelList> c = channels.reader();
762 for (n = 0, chan = c->begin(); chan != c->end(); ++chan, ++n) {
763 (*chan)->buf->reset ();
767 g_atomic_int_set(&_samples_read_from_ringbuffer, 0);
768 g_atomic_int_set(&_samples_written_to_ringbuffer, 0);
770 /* can't rec-enable in destructive mode if transport is before start */
772 if (destructive() && record_enabled() && sample < _session.current_start_sample()) {
773 disengage_record_enable ();
776 playback_sample = sample;
782 DiskWriter::do_flush (RunContext ctxt, bool force_flush)
786 RingBufferNPT<Sample>::rw_vector vector;
787 RingBufferNPT<CaptureTransition>::rw_vector transvec;
795 boost::shared_ptr<ChannelList> c = channels.reader();
796 for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
798 (*chan)->buf->get_read_vector (&vector);
800 total = vector.len[0] + vector.len[1];
802 if (total == 0 || (total < _chunk_samples && !force_flush && was_recording)) {
806 /* if there are 2+ chunks of disk i/o possible for
807 this track, let the caller know so that it can arrange
808 for us to be called again, ASAP.
810 if we are forcing a flush, then if there is* any* extra
811 work, let the caller know.
813 if we are no longer recording and there is any extra work,
814 let the caller know too.
817 if (total >= 2 * _chunk_samples || ((force_flush || !was_recording) && total > _chunk_samples)) {
821 to_write = min (_chunk_samples, (samplecnt_t) vector.len[0]);
823 // check the transition buffer when recording destructive
824 // important that we get this after the capture buf
827 (*chan)->capture_transition_buf->get_read_vector(&transvec);
828 size_t transcount = transvec.len[0] + transvec.len[1];
831 for (ti=0; ti < transcount; ++ti) {
832 CaptureTransition & captrans = (ti < transvec.len[0]) ? transvec.buf[0][ti] : transvec.buf[1][ti-transvec.len[0]];
834 if (captrans.type == CaptureStart) {
835 // by definition, the first data we got above represents the given capture pos
837 (*chan)->write_source->mark_capture_start (captrans.capture_val);
838 (*chan)->curr_capture_cnt = 0;
840 } else if (captrans.type == CaptureEnd) {
842 // capture end, the capture_val represents total samples in capture
844 if (captrans.capture_val <= (*chan)->curr_capture_cnt + to_write) {
846 // shorten to make the write a perfect fit
847 uint32_t nto_write = (captrans.capture_val - (*chan)->curr_capture_cnt);
849 if (nto_write < to_write) {
850 ret = 1; // should we?
852 to_write = nto_write;
854 (*chan)->write_source->mark_capture_end ();
856 // increment past this transition, but go no further
861 // actually ends just beyond this chunk, so force more work
869 (*chan)->capture_transition_buf->increment_read_ptr(ti);
873 if ((!(*chan)->write_source) || (*chan)->write_source->write (vector.buf[0], to_write) != to_write) {
874 error << string_compose(_("AudioDiskstream %1: cannot write to disk"), id()) << endmsg;
878 (*chan)->buf->increment_read_ptr (to_write);
879 (*chan)->curr_capture_cnt += to_write;
881 if ((to_write == vector.len[0]) && (total > to_write) && (to_write < _chunk_samples) && !destructive()) {
883 /* we wrote all of vector.len[0] but it wasn't an entire
884 disk_write_chunk_samples of data, so arrange for some part
885 of vector.len[1] to be flushed to disk as well.
888 to_write = min ((samplecnt_t)(_chunk_samples - to_write), (samplecnt_t) vector.len[1]);
890 DEBUG_TRACE (DEBUG::Butler, string_compose ("%1 additional write of %2\n", name(), to_write));
892 if ((*chan)->write_source->write (vector.buf[1], to_write) != to_write) {
893 error << string_compose(_("AudioDiskstream %1: cannot write to disk"), id()) << endmsg;
897 (*chan)->buf->increment_read_ptr (to_write);
898 (*chan)->curr_capture_cnt += to_write;
904 if (_midi_write_source) {
906 const samplecnt_t total = g_atomic_int_get(const_cast<gint*> (&_samples_pending_write));
909 _midi_buf->read_space() == 0 ||
910 (!force_flush && (total < _chunk_samples) && was_recording)) {
914 /* if there are 2+ chunks of disk i/o possible for
915 this track), let the caller know so that it can arrange
916 for us to be called again, ASAP.
918 if we are forcing a flush, then if there is* any* extra
919 work, let the caller know.
921 if we are no longer recording and there is any extra work,
922 let the caller know too.
925 if (total >= 2 * _chunk_samples || ((force_flush || !was_recording) && total > _chunk_samples)) {
930 /* push out everything we have, right now */
931 to_write = UINT32_MAX;
933 to_write = _chunk_samples;
936 if (record_enabled() && ((total > _chunk_samples) || force_flush)) {
937 Source::Lock lm(_midi_write_source->mutex());
938 if (_midi_write_source->midi_write (lm, *_midi_buf, get_capture_start_sample (0), to_write) != to_write) {
939 error << string_compose(_("MidiDiskstream %1: cannot write to disk"), id()) << endmsg;
942 g_atomic_int_add(const_cast<gint*> (&_samples_pending_write), -to_write);
952 DiskWriter::reset_write_sources (bool mark_write_complete, bool /*force*/)
954 ChannelList::iterator chan;
955 boost::shared_ptr<ChannelList> c = channels.reader();
958 if (!_session.writable() || !recordable()) {
962 capturing_sources.clear ();
964 for (chan = c->begin(), n = 0; chan != c->end(); ++chan, ++n) {
966 if (!destructive()) {
968 if ((*chan)->write_source) {
970 if (mark_write_complete) {
971 Source::Lock lock((*chan)->write_source->mutex());
972 (*chan)->write_source->mark_streaming_write_completed (lock);
973 (*chan)->write_source->done_with_peakfile_writes ();
976 if ((*chan)->write_source->removable()) {
977 (*chan)->write_source->mark_for_remove ();
978 (*chan)->write_source->drop_references ();
981 (*chan)->write_source.reset ();
984 use_new_write_source (DataType::AUDIO, n);
986 if (record_enabled()) {
987 capturing_sources.push_back ((*chan)->write_source);
992 if ((*chan)->write_source == 0) {
993 use_new_write_source (DataType::AUDIO, n);
998 if (_midi_write_source) {
999 if (mark_write_complete) {
1000 Source::Lock lm(_midi_write_source->mutex());
1001 _midi_write_source->mark_streaming_write_completed (lm);
1005 if (_playlists[DataType::MIDI]) {
1006 use_new_write_source (DataType::MIDI);
1009 if (destructive() && !c->empty ()) {
1011 /* we now have all our write sources set up, so create the
1012 playlist's single region.
1015 if (_playlists[DataType::MIDI]->empty()) {
1016 setup_destructive_playlist ();
1022 DiskWriter::use_new_write_source (DataType dt, uint32_t n)
1024 if (dt == DataType::MIDI) {
1026 _accumulated_capture_offset = 0;
1027 _midi_write_source.reset();
1030 _midi_write_source = boost::dynamic_pointer_cast<SMFSource>(
1031 _session.create_midi_source_for_session (write_source_name ()));
1033 if (!_midi_write_source) {
1034 throw failed_constructor();
1038 catch (failed_constructor &err) {
1039 error << string_compose (_("%1:%2 new capture file not initialized correctly"), _name, n) << endmsg;
1040 _midi_write_source.reset();
1044 boost::shared_ptr<ChannelList> c = channels.reader();
1046 if (!recordable()) {
1050 if (n >= c->size()) {
1051 error << string_compose (_("AudioDiskstream: channel %1 out of range"), n) << endmsg;
1055 ChannelInfo* chan = (*c)[n];
1058 if ((chan->write_source = _session.create_audio_source_for_session (
1059 c->size(), write_source_name(), n, destructive())) == 0) {
1060 throw failed_constructor();
1064 catch (failed_constructor &err) {
1065 error << string_compose (_("%1:%2 new capture file not initialized correctly"), _name, n) << endmsg;
1066 chan->write_source.reset ();
1070 /* do not remove destructive files even if they are empty */
1072 chan->write_source->set_allow_remove_if_empty (!destructive());
1079 DiskWriter::transport_stopped_wallclock (struct tm& when, time_t twhen, bool abort_capture)
1081 bool more_work = true;
1083 samplecnt_t total_capture;
1084 SourceList audio_srcs;
1085 SourceList midi_srcs;
1086 ChannelList::iterator chan;
1087 vector<CaptureInfo*>::iterator ci;
1088 boost::shared_ptr<ChannelList> c = channels.reader();
1090 bool mark_write_completed = false;
1095 /* butler is already stopped, but there may be work to do
1096 to flush remaining data to disk.
1099 while (more_work && !err) {
1100 switch (do_flush (TransportContext, true)) {
1107 error << string_compose(_("AudioDiskstream \"%1\": cannot flush captured data to disk!"), _name) << endmsg;
1112 /* XXX is there anything we can do if err != 0 ? */
1113 Glib::Threads::Mutex::Lock lm (capture_info_lock);
1115 if (capture_info.empty()) {
1119 if (abort_capture) {
1121 if (destructive()) {
1125 for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
1127 if ((*chan)->write_source) {
1129 (*chan)->write_source->mark_for_remove ();
1130 (*chan)->write_source->drop_references ();
1131 (*chan)->write_source.reset ();
1134 /* new source set up in "out" below */
1137 if (_midi_write_source) {
1138 _midi_write_source->mark_for_remove ();
1139 _midi_write_source->drop_references ();
1140 _midi_write_source.reset();
1146 for (total_capture = 0, ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1147 total_capture += (*ci)->samples;
1150 /* figure out the name for this take */
1152 for (n = 0, chan = c->begin(); chan != c->end(); ++chan, ++n) {
1154 boost::shared_ptr<AudioFileSource> as = (*chan)->write_source;
1157 audio_srcs.push_back (as);
1158 as->update_header (capture_info.front()->start, when, twhen);
1159 as->set_captured_for (_name.val());
1160 as->mark_immutable ();
1162 if (Config->get_auto_analyse_audio()) {
1163 Analyser::queue_source_for_analysis (as, true);
1166 DEBUG_TRACE (DEBUG::CaptureAlignment, string_compose ("newly captured source %1 length %2\n", as->path(), as->length (0)));
1169 if (_midi_write_source) {
1170 midi_srcs.push_back (_midi_write_source);
1177 if (_midi_write_source) {
1179 if (_midi_write_source->length (capture_info.front()->start) == 0) {
1180 /* No data was recorded, so this capture will
1181 effectively be aborted; do the same as we
1182 do for an explicit abort.
1184 if (_midi_write_source) {
1185 _midi_write_source->mark_for_remove ();
1186 _midi_write_source->drop_references ();
1187 _midi_write_source.reset();
1193 /* phew, we have data */
1195 Source::Lock source_lock(_midi_write_source->mutex());
1197 /* figure out the name for this take */
1199 midi_srcs.push_back (_midi_write_source);
1201 _midi_write_source->set_timeline_position (capture_info.front()->start);
1202 _midi_write_source->set_captured_for (_name);
1204 /* set length in beats to entire capture length */
1206 BeatsSamplesConverter converter (_session.tempo_map(), capture_info.front()->start);
1207 const Temporal::Beats total_capture_beats = converter.from (total_capture);
1208 _midi_write_source->set_length_beats (total_capture_beats);
1210 /* flush to disk: this step differs from the audio path,
1211 where all the data is already on disk.
1214 _midi_write_source->mark_midi_streaming_write_completed (source_lock, Evoral::Sequence<Temporal::Beats>::ResolveStuckNotes, total_capture_beats);
1217 _last_capture_sources.insert (_last_capture_sources.end(), audio_srcs.begin(), audio_srcs.end());
1218 _last_capture_sources.insert (_last_capture_sources.end(), midi_srcs.begin(), midi_srcs.end());
1222 _route->use_captured_sources (audio_srcs, capture_info);
1223 _route->use_captured_sources (midi_srcs, capture_info);
1226 mark_write_completed = true;
1229 reset_write_sources (mark_write_completed);
1233 for (ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1237 capture_info.clear ();
1238 capture_start_sample = 0;
1242 DiskWriter::transport_looped (samplepos_t transport_sample)
1244 if (was_recording) {
1245 // all we need to do is finish this capture, with modified capture length
1246 boost::shared_ptr<ChannelList> c = channels.reader();
1250 // the next region will start recording via the normal mechanism
1251 // we'll set the start position to the current transport pos
1252 // no latency adjustment or capture offset needs to be made, as that already happened the first time
1253 capture_start_sample = transport_sample;
1254 first_recordable_sample = transport_sample; // mild lie
1255 last_recordable_sample = max_samplepos;
1256 was_recording = true;
1258 if (recordable() && destructive()) {
1259 for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
1261 RingBufferNPT<CaptureTransition>::rw_vector transvec;
1262 (*chan)->capture_transition_buf->get_write_vector(&transvec);
1264 if (transvec.len[0] > 0) {
1265 transvec.buf[0]->type = CaptureStart;
1266 transvec.buf[0]->capture_val = capture_start_sample;
1267 (*chan)->capture_transition_buf->increment_write_ptr(1);
1271 fatal << X_("programming error: capture_transition_buf is full on rec loop! inconceivable!")
1279 /* Here we only keep track of the number of captured loops so monotonic
1280 event times can be delivered to the write source in process(). Trying
1281 to be clever here is a world of trouble, it is better to simply record
1282 the input in a straightforward non-destructive way. In the future when
1283 we want to implement more clever MIDI looping modes it should be done in
1284 the Source and/or entirely after the capture is finished.
1286 if (was_recording) {
1287 g_atomic_int_add(const_cast<gint*> (&_num_captured_loops), 1);
1292 DiskWriter::setup_destructive_playlist ()
1295 boost::shared_ptr<ChannelList> c = channels.reader();
1297 for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
1298 srcs.push_back ((*chan)->write_source);
1301 /* a single full-sized region */
1303 assert (!srcs.empty ());
1306 plist.add (Properties::name, _name.val());
1307 plist.add (Properties::start, 0);
1308 plist.add (Properties::length, max_samplepos - srcs.front()->natural_position());
1310 boost::shared_ptr<Region> region (RegionFactory::create (srcs, plist));
1311 _playlists[DataType::AUDIO]->add_region (region, srcs.front()->natural_position());
1313 /* apply region properties and update write sources */
1314 use_destructive_playlist();
1318 DiskWriter::use_destructive_playlist ()
1320 /* this is called from the XML-based constructor or ::set_destructive. when called,
1321 we already have a playlist and a region, but we need to
1322 set up our sources for write. we use the sources associated
1323 with the (presumed single, full-extent) region.
1326 boost::shared_ptr<Region> rp;
1328 const RegionList& rl (_playlists[DataType::AUDIO]->region_list_property().rlist());
1329 if (rl.size() > 0) {
1330 /* this can happen when dragging a region onto a tape track */
1331 assert((rl.size() == 1));
1337 reset_write_sources (false, true);
1341 boost::shared_ptr<AudioRegion> region = boost::dynamic_pointer_cast<AudioRegion> (rp);
1344 throw failed_constructor();
1347 /* be sure to stretch the region out to the maximum length (non-musical)*/
1349 region->set_length (max_samplepos - region->position(), 0);
1352 ChannelList::iterator chan;
1353 boost::shared_ptr<ChannelList> c = channels.reader();
1355 for (n = 0, chan = c->begin(); chan != c->end(); ++chan, ++n) {
1356 (*chan)->write_source = boost::dynamic_pointer_cast<AudioFileSource>(region->source (n));
1357 assert((*chan)->write_source);
1358 (*chan)->write_source->set_allow_remove_if_empty (false);
1360 /* this might be false if we switched modes, so force it */
1362 #ifdef XXX_OLD_DESTRUCTIVE_API_XXX
1363 (*chan)->write_source->set_destructive (true);
1365 // should be set when creating the source or loading the state
1366 assert ((*chan)->write_source->destructive());
1370 /* the source list will never be reset for a destructive track */
1374 DiskWriter::adjust_buffering ()
1376 boost::shared_ptr<ChannelList> c = channels.reader();
1378 for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
1379 (*chan)->resize (_session.butler()->audio_diskstream_capture_buffer_size());
1384 DiskWriter::realtime_handle_transport_stopped ()
1386 realtime_speed_change ();
1390 DiskWriter::set_name (string const & str)
1392 string my_name = X_("recorder:");
1395 if (_name != my_name) {
1396 SessionObject::set_name (my_name);
1403 DiskWriter::steal_write_source_name ()
1405 if (_playlists[DataType::MIDI]) {
1406 string our_old_name = _midi_write_source->name();
1408 /* this will bump the name of the current write source to the next one
1409 * (e.g. "MIDI 1-1" gets renamed to "MIDI 1-2"), thus leaving the
1410 * current write source name (e.g. "MIDI 1-1" available). See the
1411 * comments in Session::create_midi_source_by_stealing_name() about why
1416 string new_path = _session.new_midi_source_path (name());
1418 if (_midi_write_source->rename (new_path)) {
1425 return our_old_name;
1428 return std::string();
1432 DiskWriter::configure_io (ChanCount in, ChanCount out)
1434 if (!DiskIOProcessor::configure_io (in, out)) {
1438 reset_write_sources (false, true);