addd more DEBUG_TRACE for DiskIO; set DiskIOProcess::_need_butler more appropriately...
[ardour.git] / libs / ardour / disk_writer.cc
1 /*
2     Copyright (C) 2009-2016 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "pbd/i18n.h"
21
22 #include "ardour/analyser.h"
23 #include "ardour/audioengine.h"
24 #include "ardour/audiofilesource.h"
25 #include "ardour/audio_buffer.h"
26 #include "ardour/audioplaylist.h"
27 #include "ardour/audioregion.h"
28 #include "ardour/butler.h"
29 #include "ardour/debug.h"
30 #include "ardour/disk_writer.h"
31 #include "ardour/midi_playlist.h"
32 #include "ardour/midi_source.h"
33 #include "ardour/midi_track.h"
34 #include "ardour/port.h"
35 #include "ardour/region_factory.h"
36 #include "ardour/session.h"
37 #include "ardour/smf_source.h"
38
39 using namespace ARDOUR;
40 using namespace PBD;
41 using namespace std;
42
43 ARDOUR::framecnt_t DiskWriter::_chunk_frames = DiskWriter::default_chunk_frames ();
44 PBD::Signal0<void> DiskWriter::Overrun;
45
46 DiskWriter::DiskWriter (Session& s, string const & str, DiskIOProcessor::Flag f)
47         : DiskIOProcessor (s, str, f)
48         , capture_start_frame (0)
49         , capture_captured (0)
50         , was_recording (false)
51         , adjust_capture_position (0)
52         , _capture_offset (0)
53         , first_recordable_frame (max_framepos)
54         , last_recordable_frame (max_framepos)
55         , last_possibly_recording (0)
56         , _alignment_style (ExistingMaterial)
57         , _alignment_choice (Automatic)
58         , _num_captured_loops (0)
59         , _accumulated_capture_offset (0)
60         , _gui_feed_buffer(AudioEngine::instance()->raw_buffer_size (DataType::MIDI))
61 {
62         DiskIOProcessor::init ();
63 }
64
65 framecnt_t
66 DiskWriter::default_chunk_frames ()
67 {
68         return 65536;
69 }
70
71 bool
72 DiskWriter::set_write_source_name (string const & str)
73 {
74         _write_source_name = str;
75         return true;
76 }
77
78 void
79 DiskWriter::check_record_status (framepos_t transport_frame, bool can_record)
80 {
81         int possibly_recording;
82         int rolling;
83         int change;
84         const int transport_rolling = 0x4;
85         const int track_rec_enabled = 0x2;
86         const int global_rec_enabled = 0x1;
87         const int fully_rec_enabled = (transport_rolling|track_rec_enabled|global_rec_enabled);
88
89         /* merge together the 3 factors that affect record status, and compute
90          * what has changed.
91          */
92
93         rolling = _session.transport_speed() != 0.0f;
94         possibly_recording = (rolling << 2) | ((int)record_enabled() << 1) | (int)can_record;
95         change = possibly_recording ^ last_possibly_recording;
96
97         if (possibly_recording == last_possibly_recording) {
98                 return;
99         }
100
101         const framecnt_t existing_material_offset = _session.worst_playback_latency();
102
103         if (possibly_recording == fully_rec_enabled) {
104
105                 if (last_possibly_recording == fully_rec_enabled) {
106                         return;
107                 }
108
109                 capture_start_frame = _session.transport_frame();
110                 first_recordable_frame = capture_start_frame + _capture_offset;
111                 last_recordable_frame = max_framepos;
112
113                 DEBUG_TRACE (DEBUG::CaptureAlignment, string_compose ("%1: @ %7 (%9) FRF = %2 CSF = %4 CO = %5, EMO = %6 RD = %8 WOL %10 WTL %11\n",
114                                                                       name(), first_recordable_frame, last_recordable_frame, capture_start_frame,
115                                                                       _capture_offset,
116                                                                       existing_material_offset,
117                                                                       transport_frame,
118                                                                       _session.transport_frame(),
119                                                                       _session.worst_output_latency(),
120                                                                       _session.worst_track_latency()));
121
122
123                 if (_alignment_style == ExistingMaterial) {
124                         first_recordable_frame += existing_material_offset;
125                         DEBUG_TRACE (DEBUG::CaptureAlignment, string_compose ("\tshift FRF by EMO %1\n",
126                                                                               first_recordable_frame));
127                 }
128
129                 prepare_record_status (capture_start_frame);
130
131         } else {
132
133                 if (last_possibly_recording == fully_rec_enabled) {
134
135                         /* we were recording last time */
136
137                         if (change & transport_rolling) {
138
139                                 /* transport-change (stopped rolling): last_recordable_frame was set in ::prepare_to_stop(). We
140                                  * had to set it there because we likely rolled past the stopping point to declick out,
141                                  * and then backed up.
142                                  */
143
144                         } else {
145                                 /* punch out */
146
147                                 last_recordable_frame = _session.transport_frame() + _capture_offset;
148
149                                 if (_alignment_style == ExistingMaterial) {
150                                         last_recordable_frame += existing_material_offset;
151                                 }
152                         }
153                 }
154         }
155
156         last_possibly_recording = possibly_recording;
157 }
158
159 void
160 DiskWriter::calculate_record_range (Evoral::OverlapType ot, framepos_t transport_frame, framecnt_t nframes,
161                                     framecnt_t & rec_nframes, framecnt_t & rec_offset)
162 {
163         switch (ot) {
164         case Evoral::OverlapNone:
165                 rec_nframes = 0;
166                 break;
167
168         case Evoral::OverlapInternal:
169                 /*     ----------    recrange
170                  *       |---|       transrange
171                  */
172                 rec_nframes = nframes;
173                 rec_offset = 0;
174                 break;
175
176         case Evoral::OverlapStart:
177                 /*    |--------|    recrange
178                  *  -----|          transrange
179                  */
180                 rec_nframes = transport_frame + nframes - first_recordable_frame;
181                 if (rec_nframes) {
182                         rec_offset = first_recordable_frame - transport_frame;
183                 }
184                 break;
185
186         case Evoral::OverlapEnd:
187                 /*    |--------|    recrange
188                  *       |--------  transrange
189                  */
190                 rec_nframes = last_recordable_frame - transport_frame;
191                 rec_offset = 0;
192                 break;
193
194         case Evoral::OverlapExternal:
195                 /*    |--------|    recrange
196                  *  --------------  transrange
197                  */
198                 rec_nframes = last_recordable_frame - first_recordable_frame;
199                 rec_offset = first_recordable_frame - transport_frame;
200                 break;
201         }
202
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_frame, nframes,
205                                                               first_recordable_frame, last_recordable_frame, rec_nframes, rec_offset));
206 }
207
208 void
209 DiskWriter::prepare_to_stop (framepos_t transport_frame, framepos_t audible_frame)
210 {
211         switch (_alignment_style) {
212         case ExistingMaterial:
213                 last_recordable_frame = transport_frame + _capture_offset;
214                 DEBUG_TRACE (DEBUG::CaptureAlignment, string_compose("%1: prepare to stop sets last recordable frame to %2 + %3 = %4\n", _name, transport_frame, _capture_offset, last_recordable_frame));
215                 break;
216
217         case CaptureTime:
218                 last_recordable_frame = audible_frame; // note that capture_offset is zero
219                 /* we may already have captured audio before the last_recordable_frame (audible frame),
220                    so deal with this.
221                 */
222                 if (last_recordable_frame > capture_start_frame) {
223                         capture_captured = min (capture_captured, last_recordable_frame - capture_start_frame);
224                 }
225                 DEBUG_TRACE (DEBUG::CaptureAlignment, string_compose("%1: prepare to stop sets last recordable frame to audible frame @ %2\n", _name, audible_frame));
226                 break;
227         }
228
229 }
230
231 void
232 DiskWriter::engage_record_enable ()
233 {
234         g_atomic_int_set (&_record_enabled, 1);
235 }
236
237 void
238 DiskWriter::disengage_record_enable ()
239 {
240         g_atomic_int_set (&_record_enabled, 0);
241 }
242
243 void
244 DiskWriter::engage_record_safe ()
245 {
246         g_atomic_int_set (&_record_safe, 1);
247 }
248
249 void
250 DiskWriter::disengage_record_safe ()
251 {
252         g_atomic_int_set (&_record_safe, 0);
253 }
254
255 /** Get the start position (in session frames) of the nth capture in the current pass */
256 ARDOUR::framepos_t
257 DiskWriter::get_capture_start_frame (uint32_t n) const
258 {
259         Glib::Threads::Mutex::Lock lm (capture_info_lock);
260
261         if (capture_info.size() > n) {
262                 /* this is a completed capture */
263                 return capture_info[n]->start;
264         } else {
265                 /* this is the currently in-progress capture */
266                 return capture_start_frame;
267         }
268 }
269
270 ARDOUR::framecnt_t
271 DiskWriter::get_captured_frames (uint32_t n) const
272 {
273         Glib::Threads::Mutex::Lock lm (capture_info_lock);
274
275         if (capture_info.size() > n) {
276                 /* this is a completed capture */
277                 return capture_info[n]->frames;
278         } else {
279                 /* this is the currently in-progress capture */
280                 return capture_captured;
281         }
282 }
283
284 void
285 DiskWriter::set_input_latency (framecnt_t l)
286 {
287         _input_latency = l;
288 }
289
290 void
291 DiskWriter::set_capture_offset ()
292 {
293         switch (_alignment_style) {
294         case ExistingMaterial:
295                 _capture_offset = _input_latency;
296                 break;
297
298         case CaptureTime:
299         default:
300                 _capture_offset = 0;
301                 break;
302         }
303
304         DEBUG_TRACE (DEBUG::CaptureAlignment, string_compose ("%1: using IO latency, capture offset set to %2 with style = %3\n", name(), _capture_offset, enum_2_string (_alignment_style)));
305 }
306
307
308 void
309 DiskWriter::set_align_style (AlignStyle a, bool force)
310 {
311         if (record_enabled() && _session.actively_recording()) {
312                 return;
313         }
314
315         if ((a != _alignment_style) || force) {
316                 _alignment_style = a;
317                 set_capture_offset ();
318                 AlignmentStyleChanged ();
319         }
320 }
321
322 void
323 DiskWriter::set_align_style_from_io ()
324 {
325         bool have_physical = false;
326
327         if (_alignment_choice != Automatic) {
328                 return;
329         }
330
331         if (!_route) {
332                 return;
333         }
334
335         boost::shared_ptr<IO> input = _route->input ();
336
337         if (input) {
338                 uint32_t n = 0;
339                 vector<string> connections;
340                 boost::shared_ptr<ChannelList> c = channels.reader();
341
342                 for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan, ++n) {
343
344                         if ((input->nth (n).get()) && (input->nth (n)->get_connections (connections) == 0)) {
345                                 if (AudioEngine::instance()->port_is_physical (connections[0])) {
346                                         have_physical = true;
347                                         break;
348                                 }
349                         }
350
351                         connections.clear ();
352                 }
353         }
354
355 #ifdef MIXBUS
356         // compensate for latency when bouncing from master or mixbus.
357         // we need to use "ExistingMaterial" to pick up the master bus' latency
358         // see also Route::direct_feeds_according_to_reality
359         IOVector ios;
360         ios.push_back (_io);
361         if (_session.master_out() && ios.fed_by (_session.master_out()->output())) {
362                 have_physical = true;
363         }
364         for (uint32_t n = 0; n < NUM_MIXBUSES && !have_physical; ++n) {
365                 if (_session.get_mixbus (n) && ios.fed_by (_session.get_mixbus(n)->output())) {
366                         have_physical = true;
367                 }
368         }
369 #endif
370
371         if (have_physical) {
372                 set_align_style (ExistingMaterial);
373         } else {
374                 set_align_style (CaptureTime);
375         }
376 }
377
378 void
379 DiskWriter::set_align_choice (AlignChoice a, bool force)
380 {
381         if (record_enabled() && _session.actively_recording()) {
382                 return;
383         }
384
385         if ((a != _alignment_choice) || force) {
386                 _alignment_choice = a;
387
388                 switch (_alignment_choice) {
389                         case Automatic:
390                                 set_align_style_from_io ();
391                                 break;
392                         case UseExistingMaterial:
393                                 set_align_style (ExistingMaterial);
394                                 break;
395                         case UseCaptureTime:
396                                 set_align_style (CaptureTime);
397                                 break;
398                 }
399         }
400 }
401
402 XMLNode&
403 DiskWriter::state (bool full)
404 {
405         XMLNode& node (DiskIOProcessor::state (full));
406         node.add_property (X_("capture-alignment"), enum_2_string (_alignment_choice));
407         node.add_property (X_("record-safe"), (_record_safe ? X_("yes" : "no")));
408         return node;
409 }
410
411 int
412 DiskWriter::set_state (const XMLNode& node, int version)
413 {
414         XMLProperty const * prop;
415
416         if (DiskIOProcessor::set_state (node, version)) {
417                 return -1;
418         }
419
420         if ((prop = node.property (X_("capture-alignment"))) != 0) {
421                 set_align_choice (AlignChoice (string_2_enum (prop->value(), _alignment_choice)), true);
422         } else {
423                 set_align_choice (Automatic, true);
424         }
425
426
427         if ((prop = node.property ("record-safe")) != 0) {
428             _record_safe = PBD::string_is_affirmative (prop->value()) ? 1 : 0;
429         }
430
431         return 0;
432 }
433
434 void
435 DiskWriter::non_realtime_locate (framepos_t position)
436 {
437         if (_midi_write_source) {
438                 _midi_write_source->set_timeline_position (position);
439         }
440
441         DiskIOProcessor::non_realtime_locate (position);
442 }
443
444
445 void
446 DiskWriter::prepare_record_status(framepos_t capture_start_frame)
447 {
448         if (recordable() && destructive()) {
449                 boost::shared_ptr<ChannelList> c = channels.reader();
450                 for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
451
452                         RingBufferNPT<CaptureTransition>::rw_vector transitions;
453                         (*chan)->capture_transition_buf->get_write_vector (&transitions);
454
455                         if (transitions.len[0] > 0) {
456                                 transitions.buf[0]->type = CaptureStart;
457                                 transitions.buf[0]->capture_val = capture_start_frame;
458                                 (*chan)->capture_transition_buf->increment_write_ptr(1);
459                         } else {
460                                 // bad!
461                                 fatal << X_("programming error: capture_transition_buf is full on rec start!  inconceivable!")
462                                         << endmsg;
463                         }
464                 }
465         }
466 }
467
468
469 /** Do some record stuff [not described in this comment!]
470  *
471  *  Also:
472  *    - Setup playback_distance with the nframes, or nframes adjusted
473  *      for current varispeed, if appropriate.
474  *    - Setup current_playback_buffer in each ChannelInfo to point to data
475  *      that someone can read playback_distance worth of data from.
476  */
477 void
478 DiskWriter::run (BufferSet& bufs, framepos_t start_frame, framepos_t end_frame,
479                  double speed, pframes_t nframes, bool result_required)
480 {
481         uint32_t n;
482         boost::shared_ptr<ChannelList> c = channels.reader();
483         ChannelList::iterator chan;
484         framecnt_t rec_offset = 0;
485         framecnt_t rec_nframes = 0;
486         bool nominally_recording;
487         bool re = record_enabled ();
488         bool can_record = _session.actively_recording ();
489
490         _need_butler = false;
491
492         check_record_status (start_frame, can_record);
493
494         if (nframes == 0) {
495                 return;
496         }
497
498         nominally_recording = (can_record && re);
499
500         // Safeguard against situations where process() goes haywire when autopunching
501         // and last_recordable_frame < first_recordable_frame
502
503         if (last_recordable_frame < first_recordable_frame) {
504                 last_recordable_frame = max_framepos;
505         }
506
507         const Location* const loop_loc    = loop_location;
508         framepos_t            loop_start  = 0;
509         framepos_t            loop_end    = 0;
510         framepos_t            loop_length = 0;
511
512         if (loop_loc) {
513                 get_location_times (loop_loc, &loop_start, &loop_end, &loop_length);
514         }
515
516         adjust_capture_position = 0;
517
518         if (nominally_recording || (re && was_recording && _session.get_record_enabled() && (_session.config.get_punch_in() || _session.preroll_record_punch_enabled()))) {
519
520                 Evoral::OverlapType ot = Evoral::coverage (first_recordable_frame, last_recordable_frame, start_frame, end_frame);
521                 // XXX should this be transport_frame + nframes - 1 ? coverage() expects its parameter ranges to include their end points
522                 // XXX also, first_recordable_frame & last_recordable_frame may both be == max_framepos: coverage() will return OverlapNone in that case. Is thak OK?
523                 calculate_record_range (ot, start_frame, nframes, rec_nframes, rec_offset);
524
525                 DEBUG_TRACE (DEBUG::CaptureAlignment, string_compose ("%1: this time record %2 of %3 frames, offset %4\n", _name, rec_nframes, nframes, rec_offset));
526
527                 if (rec_nframes && !was_recording) {
528                         capture_captured = 0;
529
530                         if (loop_loc) {
531                                 /* Loop recording, so pretend the capture started at the loop
532                                    start rgardless of what time it is now, so the source starts
533                                    at the loop start and can handle time wrapping around.
534                                    Otherwise, start the source right now as usual.
535                                 */
536                                 capture_captured    = start_frame - loop_start;
537                                 capture_start_frame = loop_start;
538                         }
539
540                         _midi_write_source->mark_write_starting_now (capture_start_frame, capture_captured, loop_length);
541
542                         g_atomic_int_set(const_cast<gint*> (&_frames_pending_write), 0);
543                         g_atomic_int_set(const_cast<gint*> (&_num_captured_loops), 0);
544
545                         was_recording = true;
546
547                 }
548
549                 /* For audio: not writing frames to the capture ringbuffer offsets
550                  * the recording. For midi: we need to keep track of the record range
551                  * and subtract the accumulated difference from the event time.
552                  */
553                 if (rec_nframes) {
554                         _accumulated_capture_offset += rec_offset;
555                 } else {
556                         _accumulated_capture_offset += nframes;
557                 }
558
559         }
560
561         if (can_record && !_last_capture_sources.empty()) {
562                 _last_capture_sources.clear ();
563         }
564
565         if (rec_nframes) {
566
567                 /* AUDIO */
568
569                 const size_t n_buffers = bufs.count().n_audio();
570
571                 for (n = 0; chan != c->end(); ++chan, ++n) {
572
573                         ChannelInfo* chaninfo (*chan);
574                         AudioBuffer& buf (bufs.get_audio (n%n_buffers));
575
576                         chaninfo->buf->get_write_vector (&chaninfo->rw_vector);
577
578                         if (rec_nframes <= (framecnt_t) chaninfo->rw_vector.len[0]) {
579
580                                 Sample *incoming = buf.data (rec_offset);
581                                 memcpy (chaninfo->rw_vector.buf[0], incoming, sizeof (Sample) * rec_nframes);
582
583                         } else {
584
585                                 framecnt_t total = chaninfo->rw_vector.len[0] + chaninfo->rw_vector.len[1];
586
587                                 if (rec_nframes > total) {
588                                         DEBUG_TRACE (DEBUG::Butler, string_compose ("%1 overrun in %2, rec_nframes = %3 total space = %4\n",
589                                                                                     DEBUG_THREAD_SELF, name(), rec_nframes, total));
590                                         Overrun ();
591                                         return;
592                                 }
593
594                                 Sample *incoming = buf.data (rec_offset);
595                                 framecnt_t first = chaninfo->rw_vector.len[0];
596
597                                 memcpy (chaninfo->rw_vector.buf[0], incoming, sizeof (Sample) * first);
598                                 memcpy (chaninfo->rw_vector.buf[1], incoming + first, sizeof (Sample) * (rec_nframes - first));
599                         }
600
601                         chaninfo->buf->increment_write_ptr (rec_nframes);
602
603                 }
604
605                 /* MIDI */
606
607                 // Pump entire port buffer into the ring buffer (TODO: split cycles?)
608                 MidiBuffer& buf    = bufs.get_midi (0);
609                 boost::shared_ptr<MidiTrack> mt = boost::dynamic_pointer_cast<MidiTrack>(_route);
610                 MidiChannelFilter* filter = mt ? &mt->capture_filter() : 0;
611
612                 for (MidiBuffer::iterator i = buf.begin(); i != buf.end(); ++i) {
613                         Evoral::Event<MidiBuffer::TimeType> ev(*i, false);
614                         if (ev.time() + rec_offset > rec_nframes) {
615                                 break;
616                         }
617 #ifndef NDEBUG
618                         if (DEBUG_ENABLED(DEBUG::MidiIO)) {
619                                 const uint8_t* __data = ev.buffer();
620                                 DEBUG_STR_DECL(a);
621                                 DEBUG_STR_APPEND(a, string_compose ("mididiskstream %1 capture event @ %2 + %3 sz %4 ", this, ev.time(), start_frame, ev.size()));
622                                 for (size_t i=0; i < ev.size(); ++i) {
623                                         DEBUG_STR_APPEND(a,hex);
624                                         DEBUG_STR_APPEND(a,"0x");
625                                         DEBUG_STR_APPEND(a,(int)__data[i]);
626                                         DEBUG_STR_APPEND(a,' ');
627                                 }
628                                 DEBUG_STR_APPEND(a,'\n');
629                                 DEBUG_TRACE (DEBUG::MidiIO, DEBUG_STR(a).str());
630                         }
631 #endif
632                         /* Write events to the capture buffer in frames from session start,
633                            but ignoring looping so event time progresses monotonically.
634                            The source knows the loop length so it knows exactly where the
635                            event occurs in the series of recorded loops and can implement
636                            any desirable behaviour.  We don't want to send event with
637                            transport time here since that way the source can not
638                            reconstruct their actual time; future clever MIDI looping should
639                            probably be implemented in the source instead of here.
640                         */
641                         const framecnt_t loop_offset = _num_captured_loops * loop_length;
642                         const framepos_t event_time = start_frame + loop_offset - _accumulated_capture_offset + ev.time();
643                         if (event_time < 0 || event_time < first_recordable_frame) {
644                                 /* Event out of range, skip */
645                                 continue;
646                         }
647
648                         if (!filter || !filter->filter(ev.buffer(), ev.size())) {
649                                 _midi_buf->write (event_time, ev.event_type(), ev.size(), ev.buffer());
650                         }
651                 }
652                 g_atomic_int_add(const_cast<gint*>(&_frames_pending_write), nframes);
653
654                 if (buf.size() != 0) {
655                         Glib::Threads::Mutex::Lock lm (_gui_feed_buffer_mutex, Glib::Threads::TRY_LOCK);
656
657                         if (lm.locked ()) {
658                                 /* Copy this data into our GUI feed buffer and tell the GUI
659                                    that it can read it if it likes.
660                                 */
661                                 _gui_feed_buffer.clear ();
662
663                                 for (MidiBuffer::iterator i = buf.begin(); i != buf.end(); ++i) {
664                                         /* This may fail if buf is larger than _gui_feed_buffer, but it's not really
665                                            the end of the world if it does.
666                                         */
667                                         _gui_feed_buffer.push_back ((*i).time() + start_frame, (*i).size(), (*i).buffer());
668                                 }
669                         }
670
671                         DataRecorded (_midi_write_source); /* EMIT SIGNAL */
672                 }
673
674                 capture_captured += rec_nframes;
675                 DEBUG_TRACE (DEBUG::CaptureAlignment, string_compose ("%1 now captured %2 (by %3)\n", name(), capture_captured, rec_nframes));
676
677         } else {
678
679                 /* not recording this time, but perhaps we were before .. */
680
681                 if (was_recording) {
682                         finish_capture (c);
683                         _accumulated_capture_offset = 0;
684                 }
685         }
686
687         /* AUDIO BUTLER REQUIRED CODE */
688
689         if (_playlists[DataType::AUDIO] && !c->empty()) {
690                 if (((framecnt_t) c->front()->buf->read_space() >= _chunk_frames)) {
691                         _need_butler = true;
692                 }
693         }
694
695         /* MIDI BUTLER REQUIRED CODE */
696
697         if (_playlists[DataType::MIDI] && (_midi_buf->read_space() < _midi_buf->bufsize() / 2)) {
698                 _need_butler = true;
699         }
700
701         DEBUG_TRACE (DEBUG::Butler, string_compose ("%1 writer run, needs butler = %2\n", name(), _need_butler));
702 }
703
704 void
705 DiskWriter::finish_capture (boost::shared_ptr<ChannelList> c)
706 {
707         was_recording = false;
708         first_recordable_frame = max_framepos;
709         last_recordable_frame = max_framepos;
710
711         if (capture_captured == 0) {
712                 return;
713         }
714
715         if (recordable() && destructive()) {
716                 for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
717
718                         RingBufferNPT<CaptureTransition>::rw_vector transvec;
719                         (*chan)->capture_transition_buf->get_write_vector(&transvec);
720
721                         if (transvec.len[0] > 0) {
722                                 transvec.buf[0]->type = CaptureEnd;
723                                 transvec.buf[0]->capture_val = capture_captured;
724                                 (*chan)->capture_transition_buf->increment_write_ptr(1);
725                         }
726                         else {
727                                 // bad!
728                                 fatal << string_compose (_("programmer error: %1"), X_("capture_transition_buf is full when stopping record!  inconceivable!")) << endmsg;
729                         }
730                 }
731         }
732
733
734         CaptureInfo* ci = new CaptureInfo;
735
736         ci->start =  capture_start_frame;
737         ci->frames = capture_captured;
738
739         /* XXX theoretical race condition here. Need atomic exchange ?
740            However, the circumstances when this is called right
741            now (either on record-disable or transport_stopped)
742            mean that no actual race exists. I think ...
743            We now have a capture_info_lock, but it is only to be used
744            to synchronize in the transport_stop and the capture info
745            accessors, so that invalidation will not occur (both non-realtime).
746         */
747
748         DEBUG_TRACE (DEBUG::CaptureAlignment, string_compose ("Finish capture, add new CI, %1 + %2\n", ci->start, ci->frames));
749
750         capture_info.push_back (ci);
751         capture_captured = 0;
752
753         /* now we've finished a capture, reset first_recordable_frame for next time */
754         first_recordable_frame = max_framepos;
755 }
756
757 void
758 DiskWriter::set_record_enabled (bool yn)
759 {
760         if (!recordable() || !_session.record_enabling_legal() || record_safe ()) {
761                 return;
762         }
763
764         /* can't rec-enable in destructive mode if transport is before start */
765
766         if (destructive() && yn && _session.transport_frame() < _session.current_start_frame()) {
767                 return;
768         }
769
770         /* yes, i know that this not proof against race conditions, but its
771            good enough. i think.
772         */
773
774         if (record_enabled() != yn) {
775                 if (yn) {
776                         engage_record_enable ();
777                 } else {
778                         disengage_record_enable ();
779                 }
780
781                 RecordEnableChanged (); /* EMIT SIGNAL */
782         }
783 }
784
785 void
786 DiskWriter::set_record_safe (bool yn)
787 {
788         if (!recordable() || !_session.record_enabling_legal() || channels.reader()->empty()) {
789                 return;
790         }
791
792         /* can't rec-safe in destructive mode if transport is before start ????
793          REQUIRES REVIEW */
794
795         if (destructive() && yn && _session.transport_frame() < _session.current_start_frame()) {
796                 return;
797         }
798
799         /* yes, i know that this not proof against race conditions, but its
800          good enough. i think.
801          */
802
803         if (record_safe () != yn) {
804                 if (yn) {
805                         engage_record_safe ();
806                 } else {
807                         disengage_record_safe ();
808                 }
809
810                 RecordSafeChanged (); /* EMIT SIGNAL */
811         }
812 }
813
814 bool
815 DiskWriter::prep_record_enable ()
816 {
817         if (!recordable() || !_session.record_enabling_legal() || channels.reader()->empty() || record_safe ()) { // REQUIRES REVIEW "|| record_safe ()"
818                 return false;
819         }
820
821         /* can't rec-enable in destructive mode if transport is before start */
822
823         if (destructive() && _session.transport_frame() < _session.current_start_frame()) {
824                 return false;
825         }
826
827         boost::shared_ptr<ChannelList> c = channels.reader();
828
829         capturing_sources.clear ();
830
831         for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
832                 capturing_sources.push_back ((*chan)->write_source);
833                 Source::Lock lock((*chan)->write_source->mutex());
834                 (*chan)->write_source->mark_streaming_write_started (lock);
835         }
836
837         return true;
838 }
839
840 bool
841 DiskWriter::prep_record_disable ()
842 {
843         capturing_sources.clear ();
844         return true;
845 }
846
847 float
848 DiskWriter::buffer_load () const
849 {
850         boost::shared_ptr<ChannelList> c = channels.reader();
851
852         if (c->empty ()) {
853                 return 1.0;
854         }
855
856         return (float) ((double) c->front()->buf->write_space()/
857                         (double) c->front()->buf->bufsize());
858 }
859
860 void
861 DiskWriter::set_note_mode (NoteMode m)
862 {
863         _note_mode = m;
864
865         boost::shared_ptr<MidiPlaylist> mp = boost::dynamic_pointer_cast<MidiPlaylist> (_playlists[DataType::MIDI]);
866
867         if (mp) {
868                 mp->set_note_mode (m);
869         }
870
871         if (_midi_write_source && _midi_write_source->model())
872                 _midi_write_source->model()->set_note_mode(m);
873 }
874
875 int
876 DiskWriter::seek (framepos_t frame, bool complete_refill)
877 {
878         uint32_t n;
879         ChannelList::iterator chan;
880         boost::shared_ptr<ChannelList> c = channels.reader();
881
882         for (n = 0, chan = c->begin(); chan != c->end(); ++chan, ++n) {
883                 (*chan)->buf->reset ();
884         }
885
886         _midi_buf->reset ();
887         g_atomic_int_set(&_frames_read_from_ringbuffer, 0);
888         g_atomic_int_set(&_frames_written_to_ringbuffer, 0);
889
890         /* can't rec-enable in destructive mode if transport is before start */
891
892         if (destructive() && record_enabled() && frame < _session.current_start_frame()) {
893                 disengage_record_enable ();
894         }
895
896         playback_sample = frame;
897         file_frame = frame;
898
899         return 0;
900 }
901
902 int
903 DiskWriter::do_flush (RunContext ctxt, bool force_flush)
904 {
905         uint32_t to_write;
906         int32_t ret = 0;
907         RingBufferNPT<Sample>::rw_vector vector;
908         RingBufferNPT<CaptureTransition>::rw_vector transvec;
909         framecnt_t total;
910
911         transvec.buf[0] = 0;
912         transvec.buf[1] = 0;
913         vector.buf[0] = 0;
914         vector.buf[1] = 0;
915
916         boost::shared_ptr<ChannelList> c = channels.reader();
917         for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
918
919                 (*chan)->buf->get_read_vector (&vector);
920
921                 total = vector.len[0] + vector.len[1];
922
923                 if (total == 0 || (total < _chunk_frames && !force_flush && was_recording)) {
924                         goto out;
925                 }
926
927                 /* if there are 2+ chunks of disk i/o possible for
928                    this track, let the caller know so that it can arrange
929                    for us to be called again, ASAP.
930
931                    if we are forcing a flush, then if there is* any* extra
932                    work, let the caller know.
933
934                    if we are no longer recording and there is any extra work,
935                    let the caller know too.
936                 */
937
938                 if (total >= 2 * _chunk_frames || ((force_flush || !was_recording) && total > _chunk_frames)) {
939                         ret = 1;
940                 }
941
942                 to_write = min (_chunk_frames, (framecnt_t) vector.len[0]);
943
944                 // check the transition buffer when recording destructive
945                 // important that we get this after the capture buf
946
947                 if (destructive()) {
948                         (*chan)->capture_transition_buf->get_read_vector(&transvec);
949                         size_t transcount = transvec.len[0] + transvec.len[1];
950                         size_t ti;
951
952                         for (ti=0; ti < transcount; ++ti) {
953                                 CaptureTransition & captrans = (ti < transvec.len[0]) ? transvec.buf[0][ti] : transvec.buf[1][ti-transvec.len[0]];
954
955                                 if (captrans.type == CaptureStart) {
956                                         // by definition, the first data we got above represents the given capture pos
957
958                                         (*chan)->write_source->mark_capture_start (captrans.capture_val);
959                                         (*chan)->curr_capture_cnt = 0;
960
961                                 } else if (captrans.type == CaptureEnd) {
962
963                                         // capture end, the capture_val represents total frames in capture
964
965                                         if (captrans.capture_val <= (*chan)->curr_capture_cnt + to_write) {
966
967                                                 // shorten to make the write a perfect fit
968                                                 uint32_t nto_write = (captrans.capture_val - (*chan)->curr_capture_cnt);
969
970                                                 if (nto_write < to_write) {
971                                                         ret = 1; // should we?
972                                                 }
973                                                 to_write = nto_write;
974
975                                                 (*chan)->write_source->mark_capture_end ();
976
977                                                 // increment past this transition, but go no further
978                                                 ++ti;
979                                                 break;
980                                         }
981                                         else {
982                                                 // actually ends just beyond this chunk, so force more work
983                                                 ret = 1;
984                                                 break;
985                                         }
986                                 }
987                         }
988
989                         if (ti > 0) {
990                                 (*chan)->capture_transition_buf->increment_read_ptr(ti);
991                         }
992                 }
993
994                 if ((!(*chan)->write_source) || (*chan)->write_source->write (vector.buf[0], to_write) != to_write) {
995                         error << string_compose(_("AudioDiskstream %1: cannot write to disk"), id()) << endmsg;
996                         return -1;
997                 }
998
999                 (*chan)->buf->increment_read_ptr (to_write);
1000                 (*chan)->curr_capture_cnt += to_write;
1001
1002                 if ((to_write == vector.len[0]) && (total > to_write) && (to_write < _chunk_frames) && !destructive()) {
1003
1004                         /* we wrote all of vector.len[0] but it wasn't an entire
1005                            disk_write_chunk_frames of data, so arrange for some part
1006                            of vector.len[1] to be flushed to disk as well.
1007                         */
1008
1009                         to_write = min ((framecnt_t)(_chunk_frames - to_write), (framecnt_t) vector.len[1]);
1010
1011                         DEBUG_TRACE (DEBUG::Butler, string_compose ("%1 additional write of %2\n", name(), to_write));
1012
1013                         if ((*chan)->write_source->write (vector.buf[1], to_write) != to_write) {
1014                                 error << string_compose(_("AudioDiskstream %1: cannot write to disk"), id()) << endmsg;
1015                                 return -1;
1016                         }
1017
1018                         (*chan)->buf->increment_read_ptr (to_write);
1019                         (*chan)->curr_capture_cnt += to_write;
1020                 }
1021         }
1022
1023         /* MIDI*/
1024
1025   out:
1026         return ret;
1027
1028 }
1029
1030 void
1031 DiskWriter::reset_write_sources (bool mark_write_complete, bool /*force*/)
1032 {
1033         ChannelList::iterator chan;
1034         boost::shared_ptr<ChannelList> c = channels.reader();
1035         uint32_t n;
1036
1037         if (!_session.writable() || !recordable()) {
1038                 return;
1039         }
1040
1041         capturing_sources.clear ();
1042
1043         for (chan = c->begin(), n = 0; chan != c->end(); ++chan, ++n) {
1044
1045                 if (!destructive()) {
1046
1047                         if ((*chan)->write_source) {
1048
1049                                 if (mark_write_complete) {
1050                                         Source::Lock lock((*chan)->write_source->mutex());
1051                                         (*chan)->write_source->mark_streaming_write_completed (lock);
1052                                         (*chan)->write_source->done_with_peakfile_writes ();
1053                                 }
1054
1055                                 if ((*chan)->write_source->removable()) {
1056                                         (*chan)->write_source->mark_for_remove ();
1057                                         (*chan)->write_source->drop_references ();
1058                                 }
1059
1060                                 (*chan)->write_source.reset ();
1061                         }
1062
1063                         use_new_write_source (DataType::AUDIO, n);
1064
1065                         if (record_enabled()) {
1066                                 capturing_sources.push_back ((*chan)->write_source);
1067                         }
1068
1069                 } else {
1070
1071                         if ((*chan)->write_source == 0) {
1072                                 use_new_write_source (DataType::AUDIO, n);
1073                         }
1074                 }
1075         }
1076
1077         if (_midi_write_source) {
1078                 if (mark_write_complete) {
1079                         Source::Lock lm(_midi_write_source->mutex());
1080                         _midi_write_source->mark_streaming_write_completed (lm);
1081                 }
1082
1083                 use_new_write_source (DataType::MIDI);
1084
1085                 if (destructive() && !c->empty ()) {
1086
1087                         /* we now have all our write sources set up, so create the
1088                            playlist's single region.
1089                         */
1090
1091                         if (_playlists[DataType::MIDI]->empty()) {
1092                                 setup_destructive_playlist ();
1093                         }
1094                 }
1095         }
1096 }
1097
1098 int
1099 DiskWriter::use_new_write_source (DataType dt, uint32_t n)
1100 {
1101         if (dt == DataType::MIDI) {
1102
1103                 _accumulated_capture_offset = 0;
1104                 _midi_write_source.reset();
1105
1106                 try {
1107                         _midi_write_source = boost::dynamic_pointer_cast<SMFSource>(
1108                                 _session.create_midi_source_for_session (write_source_name ()));
1109
1110                         if (!_midi_write_source) {
1111                                 throw failed_constructor();
1112                         }
1113                 }
1114
1115                 catch (failed_constructor &err) {
1116                         error << string_compose (_("%1:%2 new capture file not initialized correctly"), _name, n) << endmsg;
1117                         _midi_write_source.reset();
1118                         return -1;
1119                 }
1120         } else {
1121                 boost::shared_ptr<ChannelList> c = channels.reader();
1122
1123                 if (!recordable()) {
1124                         return 1;
1125                 }
1126
1127                 if (n >= c->size()) {
1128                         error << string_compose (_("AudioDiskstream: channel %1 out of range"), n) << endmsg;
1129                         return -1;
1130                 }
1131
1132                 ChannelInfo* chan = (*c)[n];
1133
1134                 try {
1135                         if ((chan->write_source = _session.create_audio_source_for_session (
1136                                      c->size(), write_source_name(), n, destructive())) == 0) {
1137                                 throw failed_constructor();
1138                         }
1139                 }
1140
1141                 catch (failed_constructor &err) {
1142                         error << string_compose (_("%1:%2 new capture file not initialized correctly"), _name, n) << endmsg;
1143                         chan->write_source.reset ();
1144                         return -1;
1145                 }
1146
1147                 /* do not remove destructive files even if they are empty */
1148
1149                 chan->write_source->set_allow_remove_if_empty (!destructive());
1150         }
1151
1152         return 0;
1153 }
1154
1155 void
1156 DiskWriter::transport_stopped_wallclock (struct tm& when, time_t twhen, bool abort_capture)
1157 {
1158         uint32_t buffer_position;
1159         bool more_work = true;
1160         int err = 0;
1161         boost::shared_ptr<AudioRegion> region;
1162         framecnt_t total_capture;
1163         SourceList srcs;
1164         SourceList::iterator src;
1165         ChannelList::iterator chan;
1166         vector<CaptureInfo*>::iterator ci;
1167         boost::shared_ptr<ChannelList> c = channels.reader();
1168         uint32_t n = 0;
1169         bool mark_write_completed = false;
1170
1171         finish_capture (c);
1172
1173         boost::shared_ptr<AudioPlaylist> pl = boost::dynamic_pointer_cast<AudioPlaylist> (_playlists[DataType::AUDIO]);
1174
1175         /* butler is already stopped, but there may be work to do
1176            to flush remaining data to disk.
1177         */
1178
1179         while (more_work && !err) {
1180                 switch (do_flush (TransportContext, true)) {
1181                 case 0:
1182                         more_work = false;
1183                         break;
1184                 case 1:
1185                         break;
1186                 case -1:
1187                         error << string_compose(_("AudioDiskstream \"%1\": cannot flush captured data to disk!"), _name) << endmsg;
1188                         err++;
1189                 }
1190         }
1191
1192         /* XXX is there anything we can do if err != 0 ? */
1193         Glib::Threads::Mutex::Lock lm (capture_info_lock);
1194
1195         if (capture_info.empty()) {
1196                 return;
1197         }
1198
1199         if (abort_capture) {
1200
1201                 if (destructive()) {
1202                         goto outout;
1203                 }
1204
1205                 for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
1206
1207                         if ((*chan)->write_source) {
1208
1209                                 (*chan)->write_source->mark_for_remove ();
1210                                 (*chan)->write_source->drop_references ();
1211                                 (*chan)->write_source.reset ();
1212                         }
1213
1214                         /* new source set up in "out" below */
1215                 }
1216
1217                 goto out;
1218         }
1219
1220         for (total_capture = 0, ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1221                 total_capture += (*ci)->frames;
1222         }
1223
1224         /* figure out the name for this take */
1225
1226         for (n = 0, chan = c->begin(); chan != c->end(); ++chan, ++n) {
1227
1228                 boost::shared_ptr<AudioFileSource> s = (*chan)->write_source;
1229
1230                 if (s) {
1231                         srcs.push_back (s);
1232                         s->update_header (capture_info.front()->start, when, twhen);
1233                         s->set_captured_for (_name.val());
1234                         s->mark_immutable ();
1235
1236                         if (Config->get_auto_analyse_audio()) {
1237                                 Analyser::queue_source_for_analysis (s, true);
1238                         }
1239
1240                         DEBUG_TRACE (DEBUG::CaptureAlignment, string_compose ("newly captured source %1 length %2\n", s->path(), s->length (0)));
1241                 }
1242         }
1243
1244         if (!pl) {
1245                 goto midi;
1246         }
1247
1248         /* destructive tracks have a single, never changing region */
1249
1250         if (destructive()) {
1251
1252                 /* send a signal that any UI can pick up to do the right thing. there is
1253                    a small problem here in that a UI may need the peak data to be ready
1254                    for the data that was recorded and this isn't interlocked with that
1255                    process. this problem is deferred to the UI.
1256                  */
1257
1258                 pl->LayeringChanged(); // XXX this may not get the UI to do the right thing
1259
1260         } else {
1261
1262                 string whole_file_region_name;
1263                 whole_file_region_name = region_name_from_path (c->front()->write_source->name(), true);
1264
1265                 /* Register a new region with the Session that
1266                    describes the entire source. Do this first
1267                    so that any sub-regions will obviously be
1268                    children of this one (later!)
1269                 */
1270
1271                 try {
1272                         PropertyList plist;
1273
1274                         plist.add (Properties::start, c->front()->write_source->last_capture_start_frame());
1275                         plist.add (Properties::length, total_capture);
1276                         plist.add (Properties::name, whole_file_region_name);
1277                         boost::shared_ptr<Region> rx (RegionFactory::create (srcs, plist));
1278                         rx->set_automatic (true);
1279                         rx->set_whole_file (true);
1280
1281                         region = boost::dynamic_pointer_cast<AudioRegion> (rx);
1282                         region->special_set_position (capture_info.front()->start);
1283                 }
1284
1285
1286                 catch (failed_constructor& err) {
1287                         error << string_compose(_("%1: could not create region for complete audio file"), _name) << endmsg;
1288                         /* XXX what now? */
1289                 }
1290
1291                 _last_capture_sources.insert (_last_capture_sources.end(), srcs.begin(), srcs.end());
1292
1293                 pl->clear_changes ();
1294                 pl->set_capture_insertion_in_progress (true);
1295                 pl->freeze ();
1296
1297                 const framepos_t preroll_off = _session.preroll_record_trim_len ();
1298                 for (buffer_position = c->front()->write_source->last_capture_start_frame(), ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1299
1300                         string region_name;
1301
1302                         RegionFactory::region_name (region_name, whole_file_region_name, false);
1303
1304                         DEBUG_TRACE (DEBUG::CaptureAlignment, string_compose ("%1 capture bufpos %5 start @ %2 length %3 add new region %4\n",
1305                                                                               _name, (*ci)->start, (*ci)->frames, region_name, buffer_position));
1306
1307                         try {
1308
1309                                 PropertyList plist;
1310
1311                                 plist.add (Properties::start, buffer_position);
1312                                 plist.add (Properties::length, (*ci)->frames);
1313                                 plist.add (Properties::name, region_name);
1314
1315                                 boost::shared_ptr<Region> rx (RegionFactory::create (srcs, plist));
1316                                 region = boost::dynamic_pointer_cast<AudioRegion> (rx);
1317                                 if (preroll_off > 0) {
1318                                         region->trim_front (buffer_position + preroll_off);
1319                                 }
1320                         }
1321
1322                         catch (failed_constructor& err) {
1323                                 error << _("AudioDiskstream: could not create region for captured audio!") << endmsg;
1324                                 continue; /* XXX is this OK? */
1325                         }
1326
1327                         i_am_the_modifier++;
1328
1329                         pl->add_region (region, (*ci)->start + preroll_off, 1, non_layered());
1330                         pl->set_layer (region, DBL_MAX);
1331                         i_am_the_modifier--;
1332
1333                         buffer_position += (*ci)->frames;
1334                 }
1335
1336                 pl->thaw ();
1337                 pl->set_capture_insertion_in_progress (false);
1338                 _session.add_command (new StatefulDiffCommand (pl));
1339         }
1340
1341         mark_write_completed = true;
1342
1343   out:
1344         reset_write_sources (mark_write_completed);
1345
1346   outout:
1347
1348         for (ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1349                 delete *ci;
1350         }
1351
1352         capture_info.clear ();
1353         capture_start_frame = 0;
1354
1355   midi:
1356         return;
1357 }
1358
1359 #if 0 // MIDI PART
1360 void
1361 DiskWriter::transport_stopped_wallclock (struct tm& /*when*/, time_t /*twhen*/, bool abort_capture)
1362 {
1363         bool more_work = true;
1364         int err = 0;
1365         boost::shared_ptr<MidiRegion> region;
1366         MidiRegion::SourceList srcs;
1367         MidiRegion::SourceList::iterator src;
1368         vector<CaptureInfo*>::iterator ci;
1369
1370         finish_capture ();
1371
1372         /* butler is already stopped, but there may be work to do
1373            to flush remaining data to disk.
1374            */
1375
1376         while (more_work && !err) {
1377                 switch (do_flush (TransportContext, true)) {
1378                 case 0:
1379                         more_work = false;
1380                         break;
1381                 case 1:
1382                         break;
1383                 case -1:
1384                         error << string_compose(_("MidiDiskstream \"%1\": cannot flush captured data to disk!"), _name) << endmsg;
1385                         err++;
1386                 }
1387         }
1388
1389         /* XXX is there anything we can do if err != 0 ? */
1390         Glib::Threads::Mutex::Lock lm (capture_info_lock);
1391
1392         if (capture_info.empty()) {
1393                 goto no_capture_stuff_to_do;
1394         }
1395
1396         if (abort_capture) {
1397
1398                 if (_write_source) {
1399                         _write_source->mark_for_remove ();
1400                         _write_source->drop_references ();
1401                         _write_source.reset();
1402                 }
1403
1404                 /* new source set up in "out" below */
1405
1406         } else {
1407
1408                 framecnt_t total_capture = 0;
1409                 for (ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1410                         total_capture += (*ci)->frames;
1411                 }
1412
1413                 if (_write_source->length (capture_info.front()->start) != 0) {
1414
1415                         /* phew, we have data */
1416
1417                         Source::Lock source_lock(_write_source->mutex());
1418
1419                         /* figure out the name for this take */
1420
1421                         srcs.push_back (_write_source);
1422
1423                         _write_source->set_timeline_position (capture_info.front()->start);
1424                         _write_source->set_captured_for (_name);
1425
1426                         /* set length in beats to entire capture length */
1427
1428                         BeatsFramesConverter converter (_session.tempo_map(), capture_info.front()->start);
1429                         const Evoral::Beats total_capture_beats = converter.from (total_capture);
1430                         _write_source->set_length_beats (total_capture_beats);
1431
1432                         /* flush to disk: this step differs from the audio path,
1433                            where all the data is already on disk.
1434                         */
1435
1436                         _write_source->mark_midi_streaming_write_completed (source_lock, Evoral::Sequence<Evoral::Beats>::ResolveStuckNotes, total_capture_beats);
1437
1438                         /* we will want to be able to keep (over)writing the source
1439                            but we don't want it to be removable. this also differs
1440                            from the audio situation, where the source at this point
1441                            must be considered immutable. luckily, we can rely on
1442                            MidiSource::mark_streaming_write_completed() to have
1443                            already done the necessary work for that.
1444                         */
1445
1446                         string whole_file_region_name;
1447                         whole_file_region_name = region_name_from_path (_write_source->name(), true);
1448
1449                         /* Register a new region with the Session that
1450                            describes the entire source. Do this first
1451                            so that any sub-regions will obviously be
1452                            children of this one (later!)
1453                         */
1454
1455                         try {
1456                                 PropertyList plist;
1457
1458                                 plist.add (Properties::name, whole_file_region_name);
1459                                 plist.add (Properties::whole_file, true);
1460                                 plist.add (Properties::automatic, true);
1461                                 plist.add (Properties::start, 0);
1462                                 plist.add (Properties::length, total_capture);
1463                                 plist.add (Properties::layer, 0);
1464
1465                                 boost::shared_ptr<Region> rx (RegionFactory::create (srcs, plist));
1466
1467                                 region = boost::dynamic_pointer_cast<MidiRegion> (rx);
1468                                 region->special_set_position (capture_info.front()->start);
1469                         }
1470
1471
1472                         catch (failed_constructor& err) {
1473                                 error << string_compose(_("%1: could not create region for complete midi file"), _name) << endmsg;
1474                                 /* XXX what now? */
1475                         }
1476
1477                         _last_capture_sources.insert (_last_capture_sources.end(), srcs.begin(), srcs.end());
1478
1479                         _playlist->clear_changes ();
1480                         _playlist->freeze ();
1481
1482                         /* Session frame time of the initial capture in this pass, which is where the source starts */
1483                         framepos_t initial_capture = 0;
1484                         if (!capture_info.empty()) {
1485                                 initial_capture = capture_info.front()->start;
1486                         }
1487
1488                         const framepos_t preroll_off = _session.preroll_record_trim_len ();
1489                         for (ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1490
1491                                 string region_name;
1492
1493                                 RegionFactory::region_name (region_name, _write_source->name(), false);
1494
1495                                 DEBUG_TRACE (DEBUG::CaptureAlignment, string_compose ("%1 capture start @ %2 length %3 add new region %4\n",
1496                                                         _name, (*ci)->start, (*ci)->frames, region_name));
1497
1498
1499                                 // cerr << _name << ": based on ci of " << (*ci)->start << " for " << (*ci)->frames << " add a region\n";
1500
1501                                 try {
1502                                         PropertyList plist;
1503
1504                                         /* start of this region is the offset between the start of its capture and the start of the whole pass */
1505                                         plist.add (Properties::start, (*ci)->start - initial_capture);
1506                                         plist.add (Properties::length, (*ci)->frames);
1507                                         plist.add (Properties::length_beats, converter.from((*ci)->frames).to_double());
1508                                         plist.add (Properties::name, region_name);
1509
1510                                         boost::shared_ptr<Region> rx (RegionFactory::create (srcs, plist));
1511                                         region = boost::dynamic_pointer_cast<MidiRegion> (rx);
1512                                         if (preroll_off > 0) {
1513                                                 region->trim_front ((*ci)->start - initial_capture + preroll_off);
1514                                         }
1515                                 }
1516
1517                                 catch (failed_constructor& err) {
1518                                         error << _("MidiDiskstream: could not create region for captured midi!") << endmsg;
1519                                         continue; /* XXX is this OK? */
1520                                 }
1521
1522                                 // cerr << "add new region, buffer position = " << buffer_position << " @ " << (*ci)->start << endl;
1523
1524                                 i_am_the_modifier++;
1525                                 _playlist->add_region (region, (*ci)->start + preroll_off);
1526                                 i_am_the_modifier--;
1527                         }
1528
1529                         _playlist->thaw ();
1530                         _session.add_command (new StatefulDiffCommand(_playlist));
1531
1532                 } else {
1533
1534                         /* No data was recorded, so this capture will
1535                            effectively be aborted; do the same as we
1536                            do for an explicit abort.
1537                         */
1538
1539                         if (_write_source) {
1540                                 _write_source->mark_for_remove ();
1541                                 _write_source->drop_references ();
1542                                 _write_source.reset();
1543                         }
1544                 }
1545
1546         }
1547
1548         use_new_write_source (0);
1549
1550         for (ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1551                 delete *ci;
1552         }
1553
1554         capture_info.clear ();
1555         capture_start_frame = 0;
1556
1557   no_capture_stuff_to_do:
1558
1559         reset_tracker ();
1560 }
1561 #endif
1562
1563 void
1564 DiskWriter::transport_looped (framepos_t transport_frame)
1565 {
1566         if (was_recording) {
1567                 // all we need to do is finish this capture, with modified capture length
1568                 boost::shared_ptr<ChannelList> c = channels.reader();
1569
1570                 finish_capture (c);
1571
1572                 // the next region will start recording via the normal mechanism
1573                 // we'll set the start position to the current transport pos
1574                 // no latency adjustment or capture offset needs to be made, as that already happened the first time
1575                 capture_start_frame = transport_frame;
1576                 first_recordable_frame = transport_frame; // mild lie
1577                 last_recordable_frame = max_framepos;
1578                 was_recording = true;
1579
1580                 if (recordable() && destructive()) {
1581                         for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
1582
1583                                 RingBufferNPT<CaptureTransition>::rw_vector transvec;
1584                                 (*chan)->capture_transition_buf->get_write_vector(&transvec);
1585
1586                                 if (transvec.len[0] > 0) {
1587                                         transvec.buf[0]->type = CaptureStart;
1588                                         transvec.buf[0]->capture_val = capture_start_frame;
1589                                         (*chan)->capture_transition_buf->increment_write_ptr(1);
1590                                 }
1591                                 else {
1592                                         // bad!
1593                                         fatal << X_("programming error: capture_transition_buf is full on rec loop!  inconceivable!")
1594                                               << endmsg;
1595                                 }
1596                         }
1597                 }
1598
1599         }
1600
1601         /* Here we only keep track of the number of captured loops so monotonic
1602            event times can be delivered to the write source in process().  Trying
1603            to be clever here is a world of trouble, it is better to simply record
1604            the input in a straightforward non-destructive way.  In the future when
1605            we want to implement more clever MIDI looping modes it should be done in
1606            the Source and/or entirely after the capture is finished.
1607         */
1608         if (was_recording) {
1609                 g_atomic_int_add(const_cast<gint*> (&_num_captured_loops), 1);
1610         }
1611 }
1612
1613 void
1614 DiskWriter::setup_destructive_playlist ()
1615 {
1616         SourceList srcs;
1617         boost::shared_ptr<ChannelList> c = channels.reader();
1618
1619         for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
1620                 srcs.push_back ((*chan)->write_source);
1621         }
1622
1623         /* a single full-sized region */
1624
1625         assert (!srcs.empty ());
1626
1627         PropertyList plist;
1628         plist.add (Properties::name, _name.val());
1629         plist.add (Properties::start, 0);
1630         plist.add (Properties::length, max_framepos - srcs.front()->natural_position());
1631
1632         boost::shared_ptr<Region> region (RegionFactory::create (srcs, plist));
1633         _playlists[DataType::AUDIO]->add_region (region, srcs.front()->natural_position());
1634
1635         /* apply region properties and update write sources */
1636         use_destructive_playlist();
1637 }
1638
1639 void
1640 DiskWriter::use_destructive_playlist ()
1641 {
1642         /* this is called from the XML-based constructor or ::set_destructive. when called,
1643            we already have a playlist and a region, but we need to
1644            set up our sources for write. we use the sources associated
1645            with the (presumed single, full-extent) region.
1646         */
1647
1648         boost::shared_ptr<Region> rp;
1649         {
1650                 const RegionList& rl (_playlists[DataType::AUDIO]->region_list_property().rlist());
1651                 if (rl.size() > 0) {
1652                         /* this can happen when dragging a region onto a tape track */
1653                         assert((rl.size() == 1));
1654                         rp = rl.front();
1655                 }
1656         }
1657
1658         if (!rp) {
1659                 reset_write_sources (false, true);
1660                 return;
1661         }
1662
1663         boost::shared_ptr<AudioRegion> region = boost::dynamic_pointer_cast<AudioRegion> (rp);
1664
1665         if (region == 0) {
1666                 throw failed_constructor();
1667         }
1668
1669         /* be sure to stretch the region out to the maximum length (non-musical)*/
1670
1671         region->set_length (max_framepos - region->position(), 0);
1672
1673         uint32_t n;
1674         ChannelList::iterator chan;
1675         boost::shared_ptr<ChannelList> c = channels.reader();
1676
1677         for (n = 0, chan = c->begin(); chan != c->end(); ++chan, ++n) {
1678                 (*chan)->write_source = boost::dynamic_pointer_cast<AudioFileSource>(region->source (n));
1679                 assert((*chan)->write_source);
1680                 (*chan)->write_source->set_allow_remove_if_empty (false);
1681
1682                 /* this might be false if we switched modes, so force it */
1683
1684 #ifdef XXX_OLD_DESTRUCTIVE_API_XXX
1685                 (*chan)->write_source->set_destructive (true);
1686 #else
1687                 // should be set when creating the source or loading the state
1688                 assert ((*chan)->write_source->destructive());
1689 #endif
1690         }
1691
1692         /* the source list will never be reset for a destructive track */
1693 }
1694
1695 void
1696 DiskWriter::adjust_buffering ()
1697 {
1698         boost::shared_ptr<ChannelList> c = channels.reader();
1699
1700         for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
1701                 (*chan)->resize (_session.butler()->audio_diskstream_capture_buffer_size());
1702         }
1703 }