PBD::Signal<...>::connect() is already thread safe, so drop intermediate proxy/call_s...
[ardour.git] / libs / ardour / audio_diskstream.cc
1 /*
2     Copyright (C) 2000-2006 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 #include <cstdio>
20 #include <unistd.h>
21 #include <cmath>
22 #include <cerrno>
23 #include <cassert>
24 #include <string>
25 #include <climits>
26 #include <fcntl.h>
27 #include <cstdlib>
28 #include <ctime>
29
30 #include "pbd/gstdio_compat.h"
31 #include "pbd/error.h"
32 #include "pbd/xml++.h"
33 #include "pbd/memento_command.h"
34 #include "pbd/enumwriter.h"
35 #include "pbd/stateful_diff_command.h"
36
37 #include "ardour/analyser.h"
38 #include "ardour/audio_buffer.h"
39 #include "ardour/audio_diskstream.h"
40 #include "ardour/audio_port.h"
41 #include "ardour/audioengine.h"
42 #include "ardour/audiofilesource.h"
43 #include "ardour/audioplaylist.h"
44 #include "ardour/audioregion.h"
45 #include "ardour/butler.h"
46 #include "ardour/debug.h"
47 #include "ardour/io.h"
48 #include "ardour/playlist_factory.h"
49 #include "ardour/profile.h"
50 #include "ardour/region_factory.h"
51 #include "ardour/session.h"
52 #include "ardour/session_playlists.h"
53 #include "ardour/sndfile_helpers.h"
54 #include "ardour/source_factory.h"
55 #include "ardour/track.h"
56 #include "ardour/types.h"
57 #include "ardour/utils.h"
58
59 #include "pbd/i18n.h"
60 #include <locale.h>
61
62 using namespace std;
63 using namespace ARDOUR;
64 using namespace PBD;
65
66 Sample* AudioDiskstream::_mixdown_buffer       = 0;
67 gain_t* AudioDiskstream::_gain_buffer          = 0;
68
69 AudioDiskstream::AudioDiskstream (Session &sess, const string &name, Diskstream::Flag flag)
70         : Diskstream(sess, name, flag)
71         , channels (new ChannelList)
72 {
73         /* prevent any write sources from being created */
74
75         in_set_state = true;
76         use_new_playlist ();
77         in_set_state = false;
78
79         if (flag & Destructive) {
80                 use_destructive_playlist ();
81         }
82 }
83
84 AudioDiskstream::AudioDiskstream (Session& sess, const XMLNode& node)
85         : Diskstream(sess, node)
86         , channels (new ChannelList)
87 {
88         in_set_state = true;
89         init ();
90
91         if (set_state (node, Stateful::loading_state_version)) {
92                 in_set_state = false;
93                 throw failed_constructor();
94         }
95
96         in_set_state = false;
97
98         if (destructive()) {
99                 use_destructive_playlist ();
100         }
101 }
102
103 void
104 AudioDiskstream::init ()
105 {
106         /* there are no channels at this point, so these
107            two calls just get speed_buffer_size and wrap_buffer
108            size setup without duplicating their code.
109         */
110
111         set_block_size (_session.get_block_size());
112         allocate_temporary_buffers ();
113 }
114
115 AudioDiskstream::~AudioDiskstream ()
116 {
117         DEBUG_TRACE (DEBUG::Destruction, string_compose ("Audio Diskstream %1 destructor\n", _name));
118
119         {
120                 RCUWriter<ChannelList> writer (channels);
121                 boost::shared_ptr<ChannelList> c = writer.get_copy();
122
123                 for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
124                         delete *chan;
125                 }
126
127                 c->clear();
128         }
129
130         channels.flush ();
131 }
132
133 void
134 AudioDiskstream::allocate_working_buffers()
135 {
136         /* with varifill buffer refilling, we compute the read size in bytes (to optimize
137            for disk i/o bandwidth) and then convert back into samples. These buffers
138            need to reflect the maximum size we could use, which is 4MB reads, or 2M samples
139            using 16 bit samples.
140         */
141         _mixdown_buffer       = new Sample[2*1048576];
142         _gain_buffer          = new gain_t[2*1048576];
143 }
144
145 void
146 AudioDiskstream::free_working_buffers()
147 {
148         delete [] _mixdown_buffer;
149         delete [] _gain_buffer;
150         _mixdown_buffer       = 0;
151         _gain_buffer          = 0;
152 }
153
154 void
155 AudioDiskstream::non_realtime_input_change ()
156 {
157         bool need_write_sources = false;
158
159         {
160                 Glib::Threads::Mutex::Lock lm (state_lock);
161
162                 if (input_change_pending.type == IOChange::NoChange) {
163                         return;
164                 }
165
166                 boost::shared_ptr<ChannelList> cr = channels.reader();
167                 if (!cr->empty() && !cr->front()->write_source) {
168                         need_write_sources = true;
169                 }
170
171                 if (input_change_pending.type & IOChange::ConfigurationChanged) {
172                         RCUWriter<ChannelList> writer (channels);
173                         boost::shared_ptr<ChannelList> c = writer.get_copy();
174
175                         _n_channels.set(DataType::AUDIO, c->size());
176
177                         if (_io->n_ports().n_audio() > _n_channels.n_audio()) {
178                                 add_channel_to (c, _io->n_ports().n_audio() - _n_channels.n_audio());
179                         } else if (_io->n_ports().n_audio() < _n_channels.n_audio()) {
180                                 remove_channel_from (c, _n_channels.n_audio() - _io->n_ports().n_audio());
181                         }
182
183                         need_write_sources = true;
184                 }
185
186                 if (input_change_pending.type & IOChange::ConnectionsChanged) {
187                         get_input_sources ();
188                         set_capture_offset ();
189                         set_align_style_from_io ();
190                 }
191
192                 input_change_pending = IOChange::NoChange;
193
194                 /* implicit unlock */
195         }
196
197         if (need_write_sources) {
198                 reset_write_sources (false);
199         }
200
201         /* now refill channel buffers */
202
203         if (speed() != 1.0f || speed() != -1.0f) {
204                 seek ((framepos_t) (_session.transport_frame() * (double) speed()));
205         } else {
206                 seek (_session.transport_frame());
207         }
208 }
209
210 void
211 AudioDiskstream::non_realtime_locate (framepos_t location)
212 {
213         /* now refill channel buffers */
214
215         if (speed() != 1.0f || speed() != -1.0f) {
216                 seek ((framepos_t) (location * (double) speed()), true);
217         } else {
218                 seek (location, true);
219         }
220 }
221
222 void
223 AudioDiskstream::get_input_sources ()
224 {
225         boost::shared_ptr<ChannelList> c = channels.reader();
226
227         uint32_t n;
228         ChannelList::iterator chan;
229         uint32_t ni = _io->n_ports().n_audio();
230         vector<string> connections;
231
232         for (n = 0, chan = c->begin(); chan != c->end() && n < ni; ++chan, ++n) {
233
234                 connections.clear ();
235
236                 if ((_io->nth (n).get()) && (_io->nth (n)->get_connections (connections) == 0)) {
237                         if (!(*chan)->source.name.empty()) {
238                                 // _source->disable_metering ();
239                         }
240                         (*chan)->source.name = string();
241                 } else {
242                         (*chan)->source.name = connections[0];
243                 }
244         }
245 }
246
247 int
248 AudioDiskstream::find_and_use_playlist (const string& name)
249 {
250         boost::shared_ptr<AudioPlaylist> playlist;
251
252         if ((playlist = boost::dynamic_pointer_cast<AudioPlaylist> (_session.playlists->by_name (name))) == 0) {
253                 playlist = boost::dynamic_pointer_cast<AudioPlaylist> (PlaylistFactory::create (DataType::AUDIO, _session, name));
254         }
255
256         if (!playlist) {
257                 error << string_compose(_("AudioDiskstream: Playlist \"%1\" isn't an audio playlist"), name) << endmsg;
258                 return -1;
259         }
260
261         return use_playlist (playlist);
262 }
263
264 int
265 AudioDiskstream::use_playlist (boost::shared_ptr<Playlist> playlist)
266 {
267         assert(boost::dynamic_pointer_cast<AudioPlaylist>(playlist));
268
269         Diskstream::use_playlist(playlist);
270
271         return 0;
272 }
273
274 int
275 AudioDiskstream::use_new_playlist ()
276 {
277         string newname;
278         boost::shared_ptr<AudioPlaylist> playlist;
279
280         if (!in_set_state && destructive()) {
281                 return 0;
282         }
283
284         if (_playlist) {
285                 newname = Playlist::bump_name (_playlist->name(), _session);
286         } else {
287                 newname = Playlist::bump_name (_name, _session);
288         }
289
290         if ((playlist = boost::dynamic_pointer_cast<AudioPlaylist> (PlaylistFactory::create (DataType::AUDIO, _session, newname, hidden()))) != 0) {
291
292                 return use_playlist (playlist);
293
294         } else {
295                 return -1;
296         }
297 }
298
299 int
300 AudioDiskstream::use_copy_playlist ()
301 {
302         assert(audio_playlist());
303
304         if (destructive()) {
305                 return 0;
306         }
307
308         if (_playlist == 0) {
309                 error << string_compose(_("AudioDiskstream %1: there is no existing playlist to make a copy of!"), _name) << endmsg;
310                 return -1;
311         }
312
313         string newname;
314         boost::shared_ptr<AudioPlaylist> playlist;
315
316         newname = Playlist::bump_name (_playlist->name(), _session);
317
318         if ((playlist = boost::dynamic_pointer_cast<AudioPlaylist>(PlaylistFactory::create (audio_playlist(), newname))) != 0) {
319                 playlist->reset_shares();
320                 return use_playlist (playlist);
321         } else {
322                 return -1;
323         }
324 }
325
326 void
327 AudioDiskstream::setup_destructive_playlist ()
328 {
329         SourceList srcs;
330         boost::shared_ptr<ChannelList> c = channels.reader();
331
332         for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
333                 srcs.push_back ((*chan)->write_source);
334         }
335
336         /* a single full-sized region */
337
338         assert (!srcs.empty ());
339
340         PropertyList plist;
341         plist.add (Properties::name, _name.val());
342         plist.add (Properties::start, 0);
343         plist.add (Properties::length, max_framepos - srcs.front()->natural_position());
344
345         boost::shared_ptr<Region> region (RegionFactory::create (srcs, plist));
346         _playlist->add_region (region, srcs.front()->natural_position());
347
348         /* apply region properties and update write sources */
349         use_destructive_playlist();
350 }
351
352 void
353 AudioDiskstream::use_destructive_playlist ()
354 {
355         /* this is called from the XML-based constructor or ::set_destructive. when called,
356            we already have a playlist and a region, but we need to
357            set up our sources for write. we use the sources associated
358            with the (presumed single, full-extent) region.
359         */
360
361         boost::shared_ptr<Region> rp;
362         {
363                 const RegionList& rl (_playlist->region_list_property().rlist());
364                 if (rl.size() > 0) {
365                         /* this can happen when dragging a region onto a tape track */
366                         assert((rl.size() == 1));
367                         rp = rl.front();
368                 }
369         }
370
371         if (!rp) {
372                 reset_write_sources (false, true);
373                 return;
374         }
375
376         boost::shared_ptr<AudioRegion> region = boost::dynamic_pointer_cast<AudioRegion> (rp);
377
378         if (region == 0) {
379                 throw failed_constructor();
380         }
381
382         /* be sure to stretch the region out to the maximum length (non-musical)*/
383
384         region->set_length (max_framepos - region->position(), 0);
385
386         uint32_t n;
387         ChannelList::iterator chan;
388         boost::shared_ptr<ChannelList> c = channels.reader();
389
390         for (n = 0, chan = c->begin(); chan != c->end(); ++chan, ++n) {
391                 (*chan)->write_source = boost::dynamic_pointer_cast<AudioFileSource>(region->source (n));
392                 assert((*chan)->write_source);
393                 (*chan)->write_source->set_allow_remove_if_empty (false);
394
395                 /* this might be false if we switched modes, so force it */
396
397 #ifdef XXX_OLD_DESTRUCTIVE_API_XXX
398                 (*chan)->write_source->set_destructive (true);
399 #else
400                 // should be set when creating the source or loading the state
401                 assert ((*chan)->write_source->destructive());
402 #endif
403         }
404
405         /* the source list will never be reset for a destructive track */
406 }
407
408 void
409 AudioDiskstream::prepare_record_status(framepos_t capture_start_frame)
410 {
411         if (recordable() && destructive()) {
412                 boost::shared_ptr<ChannelList> c = channels.reader();
413                 for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
414
415                         RingBufferNPT<CaptureTransition>::rw_vector transitions;
416                         (*chan)->capture_transition_buf->get_write_vector (&transitions);
417
418                         if (transitions.len[0] > 0) {
419                                 transitions.buf[0]->type = CaptureStart;
420                                 transitions.buf[0]->capture_val = capture_start_frame;
421                                 (*chan)->capture_transition_buf->increment_write_ptr(1);
422                         } else {
423                                 // bad!
424                                 fatal << X_("programming error: capture_transition_buf is full on rec start!  inconceivable!")
425                                         << endmsg;
426                         }
427                 }
428         }
429 }
430
431
432 /** Do some record stuff [not described in this comment!]
433  *
434  *  Also:
435  *    - Setup playback_distance with the nframes, or nframes adjusted
436  *      for current varispeed, if appropriate.
437  *    - Setup current_playback_buffer in each ChannelInfo to point to data
438  *      that someone can read playback_distance worth of data from.
439  */
440 int
441 AudioDiskstream::process (BufferSet& bufs, framepos_t transport_frame, pframes_t nframes, framecnt_t& playback_distance, bool need_disk_signal)
442 {
443         uint32_t n;
444         boost::shared_ptr<ChannelList> c = channels.reader();
445         ChannelList::iterator chan;
446         framecnt_t rec_offset = 0;
447         framecnt_t rec_nframes = 0;
448         bool collect_playback = false;
449         bool can_record = _session.actively_recording ();
450
451         playback_distance = 0;
452
453         if (!_io || !_io->active()) {
454                 return 0;
455         }
456
457         check_record_status (transport_frame, can_record);
458
459         if (nframes == 0) {
460                 return 0;
461         }
462
463         Glib::Threads::Mutex::Lock sm (state_lock, Glib::Threads::TRY_LOCK);
464
465         if (!sm.locked()) {
466                 return 1;
467         }
468
469         adjust_capture_position = 0;
470
471         for (chan = c->begin(); chan != c->end(); ++chan) {
472                 (*chan)->current_capture_buffer = 0;
473                 (*chan)->current_playback_buffer = 0;
474         }
475
476         // Safeguard against situations where process() goes haywire when autopunching
477         // and last_recordable_frame < first_recordable_frame
478
479         if (last_recordable_frame < first_recordable_frame) {
480                 last_recordable_frame = max_framepos;
481         }
482
483         if (record_enabled()) {
484
485                 Evoral::OverlapType ot = Evoral::coverage (first_recordable_frame, last_recordable_frame, transport_frame, transport_frame + nframes);
486                 // XXX should this be transport_frame + nframes - 1 ? coverage() expects its parameter ranges to include their end points
487                 // XXX also, first_recordable_frame & last_recordable_frame may both be == max_framepos: coverage() will return OverlapNone in that case. Is thak OK?
488                 calculate_record_range (ot, transport_frame, nframes, rec_nframes, rec_offset);
489
490                 DEBUG_TRACE (DEBUG::CaptureAlignment, string_compose ("%1: this time record %2 of %3 frames, offset %4\n", _name, rec_nframes, nframes, rec_offset));
491
492                 if (rec_nframes && !was_recording) {
493                         capture_captured = 0;
494                         was_recording = true;
495                 }
496         }
497
498         if (can_record && !_last_capture_sources.empty()) {
499                 _last_capture_sources.clear ();
500         }
501
502         if (rec_nframes) {
503
504                 uint32_t limit = _io->n_ports ().n_audio();
505
506                 /* one or more ports could already have been removed from _io, but our
507                    channel setup hasn't yet been updated. prevent us from trying to
508                    use channels that correspond to missing ports. note that the
509                    process callback (from which this is called) is always atomic
510                    with respect to port removal/addition.
511                 */
512
513                 for (n = 0, chan = c->begin(); chan != c->end() && n < limit; ++chan, ++n) {
514
515                         ChannelInfo* chaninfo (*chan);
516
517                         chaninfo->capture_buf->get_write_vector (&chaninfo->capture_vector);
518
519                         if (rec_nframes <= (framecnt_t) chaninfo->capture_vector.len[0]) {
520
521                                 chaninfo->current_capture_buffer = chaninfo->capture_vector.buf[0];
522
523                                 /* note: grab the entire port buffer, but only copy what we were supposed to
524                                    for recording, and use rec_offset
525                                 */
526
527                                 boost::shared_ptr<AudioPort> const ap = _io->audio (n);
528                                 assert(ap);
529                                 assert(rec_nframes <= (framecnt_t) ap->get_audio_buffer(nframes).capacity());
530
531                                 Sample *buf = bufs.get_audio (n).data(rec_offset);
532                                 memcpy (chaninfo->current_capture_buffer, buf, sizeof (Sample) * rec_nframes);
533
534                         } else {
535
536                                 framecnt_t total = chaninfo->capture_vector.len[0] + chaninfo->capture_vector.len[1];
537
538                                 if (rec_nframes > total) {
539                                         DEBUG_TRACE (DEBUG::Butler, string_compose ("%1 overrun in %2, rec_nframes = %3 total space = %4\n",
540                                                                                     DEBUG_THREAD_SELF, name(), rec_nframes, total));
541                                         DiskOverrun ();
542                                         return -1;
543                                 }
544
545                                 boost::shared_ptr<AudioPort> const ap = _io->audio (n);
546                                 assert(ap);
547
548                                 Sample *buf = bufs.get_audio (n).data(rec_offset);
549                                 framecnt_t first = chaninfo->capture_vector.len[0];
550
551                                 memcpy (chaninfo->capture_wrap_buffer, buf, sizeof (Sample) * first);
552                                 memcpy (chaninfo->capture_vector.buf[0], buf, sizeof (Sample) * first);
553                                 memcpy (chaninfo->capture_wrap_buffer+first, buf + first, sizeof (Sample) * (rec_nframes - first));
554                                 memcpy (chaninfo->capture_vector.buf[1], buf + first, sizeof (Sample) * (rec_nframes - first));
555
556                                 chaninfo->current_capture_buffer = chaninfo->capture_wrap_buffer;
557                         }
558                 }
559
560         } else {
561
562                 if (was_recording) {
563                         finish_capture (c);
564                 }
565
566         }
567
568         if (rec_nframes) {
569
570                 /* data will be written to disk */
571
572                 if (rec_nframes == nframes && rec_offset == 0) {
573
574                         for (chan = c->begin(); chan != c->end(); ++chan) {
575                                 (*chan)->current_playback_buffer = (*chan)->current_capture_buffer;
576                         }
577
578                         playback_distance = nframes;
579
580                 } else {
581
582
583                         /* we can't use the capture buffer as the playback buffer, because
584                            we recorded only a part of the current process' cycle data
585                            for capture.
586                         */
587
588                         collect_playback = true;
589                 }
590
591                 adjust_capture_position = rec_nframes;
592
593         } else if (can_record && record_enabled()) {
594
595                 /* can't do actual capture yet - waiting for latency effects to finish before we start*/
596
597                 for (chan = c->begin(); chan != c->end(); ++chan) {
598                         (*chan)->current_playback_buffer = (*chan)->current_capture_buffer;
599                 }
600
601                 playback_distance = nframes;
602
603         } else {
604
605                 collect_playback = true;
606         }
607
608         if ((_track->monitoring_state () & MonitoringDisk) || collect_playback) {
609
610                 /* we're doing playback */
611
612                 framecnt_t necessary_samples;
613
614                 /* no varispeed playback if we're recording, because the output .... TBD */
615
616                 if (rec_nframes == 0 && _actual_speed != 1.0) {
617                         necessary_samples = (framecnt_t) ceil ((nframes * fabs (_actual_speed))) + 2;
618                 } else {
619                         necessary_samples = nframes;
620                 }
621
622                 for (chan = c->begin(); chan != c->end(); ++chan) {
623                         (*chan)->playback_buf->get_read_vector (&(*chan)->playback_vector);
624                 }
625
626                 n = 0;
627
628                 /* Setup current_playback_buffer in each ChannelInfo to point to data that someone
629                    can read necessary_samples (== nframes at a transport speed of 1) worth of data
630                    from right now.
631                 */
632
633                 for (chan = c->begin(); chan != c->end(); ++chan, ++n) {
634
635                         ChannelInfo* chaninfo (*chan);
636
637                         if (necessary_samples <= (framecnt_t) chaninfo->playback_vector.len[0]) {
638                                 /* There are enough samples in the first part of the ringbuffer */
639                                 chaninfo->current_playback_buffer = chaninfo->playback_vector.buf[0];
640
641                         } else {
642                                 framecnt_t total = chaninfo->playback_vector.len[0] + chaninfo->playback_vector.len[1];
643
644                                 if (necessary_samples > total) {
645                                         cerr << _name << " Need " << necessary_samples << " total = " << total << endl;
646                                         cerr << "underrun for " << _name << endl;
647                                         DEBUG_TRACE (DEBUG::Butler, string_compose ("%1 underrun in %2, rec_nframes = %3 total space = %4\n",
648                                                                                     DEBUG_THREAD_SELF, name(), rec_nframes, total));
649                                         DiskUnderrun ();
650                                         return -1;
651
652                                 } else {
653
654                                         /* We have enough samples, but not in one lump.  Coalesce the two parts
655                                            into one in playback_wrap_buffer in our ChannelInfo, and specify that
656                                            as our current_playback_buffer.
657                                         */
658
659                                         assert(wrap_buffer_size >= necessary_samples);
660
661                                         /* Copy buf[0] from playback_buf */
662                                         memcpy ((char *) chaninfo->playback_wrap_buffer,
663                                                         chaninfo->playback_vector.buf[0],
664                                                         chaninfo->playback_vector.len[0] * sizeof (Sample));
665
666                                         /* Copy buf[1] from playback_buf */
667                                         memcpy (chaninfo->playback_wrap_buffer + chaninfo->playback_vector.len[0],
668                                                         chaninfo->playback_vector.buf[1],
669                                                         (necessary_samples - chaninfo->playback_vector.len[0])
670                                                                         * sizeof (Sample));
671
672                                         chaninfo->current_playback_buffer = chaninfo->playback_wrap_buffer;
673                                 }
674                         }
675                 }
676
677                 if (rec_nframes == 0 && _actual_speed != 1.0f && _actual_speed != -1.0f) {
678
679                         interpolation.set_speed (_target_speed);
680
681                         int channel = 0;
682                         for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan, ++channel) {
683                                 ChannelInfo* chaninfo (*chan);
684
685                                 playback_distance = interpolation.interpolate (
686                                         channel, nframes, chaninfo->current_playback_buffer, chaninfo->speed_buffer);
687
688                                 chaninfo->current_playback_buffer = chaninfo->speed_buffer;
689                         }
690
691                 } else {
692                         playback_distance = nframes;
693                 }
694
695                 _speed = _target_speed;
696         }
697
698         if (need_disk_signal) {
699
700                 /* copy data over to buffer set */
701
702                 size_t n_buffers = bufs.count().n_audio();
703                 size_t n_chans = c->size();
704                 gain_t scaling = 1.0f;
705
706                 if (n_chans > n_buffers) {
707                         scaling = ((float) n_buffers)/n_chans;
708                 }
709
710                 for (n = 0, chan = c->begin(); chan != c->end(); ++chan, ++n) {
711
712                         AudioBuffer& buf (bufs.get_audio (n%n_buffers));
713                         ChannelInfo* chaninfo (*chan);
714
715                         if (n < n_chans) {
716                                 if (scaling != 1.0f) {
717                                         buf.read_from_with_gain (chaninfo->current_playback_buffer, nframes, scaling);
718                                 } else {
719                                         buf.read_from (chaninfo->current_playback_buffer, nframes);
720                                 }
721                         } else {
722                                 if (scaling != 1.0f) {
723                                         buf.accumulate_with_gain_from (chaninfo->current_playback_buffer, nframes, scaling);
724                                 } else {
725                                         buf.accumulate_from (chaninfo->current_playback_buffer, nframes);
726                                 }
727                         }
728                 }
729
730                 /* leave the MIDI count alone */
731                 ChanCount cnt (DataType::AUDIO, n_chans);
732                 cnt.set (DataType::MIDI, bufs.count().n_midi());
733                 bufs.set_count (cnt);
734
735                 /* extra buffers will already be silent, so leave them alone */
736         }
737
738         return 0;
739 }
740
741 frameoffset_t
742 AudioDiskstream::calculate_playback_distance (pframes_t nframes)
743 {
744         frameoffset_t playback_distance = nframes;
745
746         if (record_enabled()) {
747                 playback_distance = nframes;
748         } else if (_actual_speed != 1.0f && _actual_speed != -1.0f) {
749                 interpolation.set_speed (_target_speed);
750                 boost::shared_ptr<ChannelList> c = channels.reader();
751                 int channel = 0;
752                 for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan, ++channel) {
753                         playback_distance = interpolation.interpolate (channel, nframes, NULL, NULL);
754                 }
755         } else {
756                 playback_distance = nframes;
757         }
758
759         if (_actual_speed < 0.0) {
760                 return -playback_distance;
761         } else {
762                 return playback_distance;
763         }
764 }
765
766 /** Update various things including playback_sample, read pointer on each channel's playback_buf
767  *  and write pointer on each channel's capture_buf.  Also wout whether the butler is needed.
768  *  @return true if the butler is required.
769  */
770 bool
771 AudioDiskstream::commit (framecnt_t playback_distance)
772 {
773         bool need_butler = false;
774
775         if (!_io || !_io->active()) {
776                 return false;
777         }
778
779         if (_actual_speed < 0.0) {
780                 playback_sample -= playback_distance;
781         } else {
782                 playback_sample += playback_distance;
783         }
784
785         boost::shared_ptr<ChannelList> c = channels.reader();
786         for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
787
788                 (*chan)->playback_buf->increment_read_ptr (playback_distance);
789
790                 if (adjust_capture_position) {
791                         (*chan)->capture_buf->increment_write_ptr (adjust_capture_position);
792                 }
793         }
794
795         if (adjust_capture_position != 0) {
796                 capture_captured += adjust_capture_position;
797                 DEBUG_TRACE (DEBUG::CaptureAlignment, string_compose ("%1 now captured %2 (by %3)\n", name(), capture_captured, adjust_capture_position));
798                 adjust_capture_position = 0;
799         }
800
801         if (c->empty()) {
802                 return false;
803         }
804
805         if (_slaved) {
806                 if (_io && _io->active()) {
807                         need_butler = c->front()->playback_buf->write_space() >= c->front()->playback_buf->bufsize() / 2;
808                 } else {
809                         need_butler = false;
810                 }
811         } else {
812                 if (_io && _io->active()) {
813                         need_butler = ((framecnt_t) c->front()->playback_buf->write_space() >= disk_read_chunk_frames)
814                                 || ((framecnt_t) c->front()->capture_buf->read_space() >= disk_write_chunk_frames);
815                 } else {
816                         need_butler = ((framecnt_t) c->front()->capture_buf->read_space() >= disk_write_chunk_frames);
817                 }
818         }
819
820         return need_butler;
821 }
822
823 void
824 AudioDiskstream::set_pending_overwrite (bool yn)
825 {
826         /* called from audio thread, so we can use the read ptr and playback sample as we wish */
827
828         _pending_overwrite = yn;
829
830         overwrite_frame = playback_sample;
831
832         boost::shared_ptr<ChannelList> c = channels.reader ();
833         if (!c->empty ()) {
834                 overwrite_offset = c->front()->playback_buf->get_read_ptr();
835         }
836 }
837
838 int
839 AudioDiskstream::overwrite_existing_buffers ()
840 {
841         boost::shared_ptr<ChannelList> c = channels.reader();
842         if (c->empty ()) {
843                 _pending_overwrite = false;
844                 return 0;
845         }
846
847         Sample* mixdown_buffer;
848         float* gain_buffer;
849         int ret = -1;
850         bool reversed = (_visible_speed * _session.transport_speed()) < 0.0f;
851
852         overwrite_queued = false;
853
854         /* assume all are the same size */
855         framecnt_t size = c->front()->playback_buf->bufsize();
856
857         mixdown_buffer = new Sample[size];
858         gain_buffer = new float[size];
859
860         /* reduce size so that we can fill the buffer correctly (ringbuffers
861            can only handle size-1, otherwise they appear to be empty)
862         */
863         size--;
864
865         uint32_t n=0;
866         framepos_t start;
867
868         for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan, ++n) {
869
870                 start = overwrite_frame;
871                 framecnt_t cnt = size;
872
873                 /* to fill the buffer without resetting the playback sample, we need to
874                    do it one or two chunks (normally two).
875
876                    |----------------------------------------------------------------------|
877
878                                        ^
879                                        overwrite_offset
880                     |<- second chunk->||<----------------- first chunk ------------------>|
881
882                 */
883
884                 framecnt_t to_read = size - overwrite_offset;
885
886                 if (read ((*chan)->playback_buf->buffer() + overwrite_offset, mixdown_buffer, gain_buffer, start, to_read, n, reversed)) {
887                         error << string_compose(_("AudioDiskstream %1: when refilling, cannot read %2 from playlist at frame %3"),
888                                                 id(), size, playback_sample) << endmsg;
889                         goto out;
890                 }
891
892                 if (cnt > to_read) {
893
894                         cnt -= to_read;
895
896                         if (read ((*chan)->playback_buf->buffer(), mixdown_buffer, gain_buffer, start, cnt, n, reversed)) {
897                                 error << string_compose(_("AudioDiskstream %1: when refilling, cannot read %2 from playlist at frame %3"),
898                                                         id(), size, playback_sample) << endmsg;
899                                 goto out;
900                         }
901                 }
902         }
903
904         ret = 0;
905
906   out:
907         _pending_overwrite = false;
908         delete [] gain_buffer;
909         delete [] mixdown_buffer;
910         return ret;
911 }
912
913 int
914 AudioDiskstream::seek (framepos_t frame, bool complete_refill)
915 {
916         uint32_t n;
917         int ret = -1;
918         ChannelList::iterator chan;
919         boost::shared_ptr<ChannelList> c = channels.reader();
920
921         Glib::Threads::Mutex::Lock lm (state_lock);
922
923         for (n = 0, chan = c->begin(); chan != c->end(); ++chan, ++n) {
924                 (*chan)->playback_buf->reset ();
925                 (*chan)->capture_buf->reset ();
926         }
927
928         /* can't rec-enable in destructive mode if transport is before start */
929
930         if (destructive() && record_enabled() && frame < _session.current_start_frame()) {
931                 disengage_record_enable ();
932         }
933
934         playback_sample = frame;
935         file_frame = frame;
936
937         if (complete_refill) {
938                 /* call _do_refill() to refill the entire buffer, using
939                    the largest reads possible.
940                 */
941                 while ((ret = do_refill_with_alloc (false)) > 0) ;
942         } else {
943                 /* call _do_refill() to refill just one chunk, and then
944                    return.
945                 */
946                 ret = do_refill_with_alloc (true);
947         }
948
949         return ret;
950 }
951
952 int
953 AudioDiskstream::can_internal_playback_seek (framecnt_t distance)
954 {
955         ChannelList::iterator chan;
956         boost::shared_ptr<ChannelList> c = channels.reader();
957
958         for (chan = c->begin(); chan != c->end(); ++chan) {
959                 if ((*chan)->playback_buf->read_space() < (size_t) distance) {
960                         return false;
961                 }
962         }
963         return true;
964 }
965
966 int
967 AudioDiskstream::internal_playback_seek (framecnt_t distance)
968 {
969         ChannelList::iterator chan;
970         boost::shared_ptr<ChannelList> c = channels.reader();
971
972         for (chan = c->begin(); chan != c->end(); ++chan) {
973                 (*chan)->playback_buf->increment_read_ptr (::llabs(distance));
974         }
975
976         if (first_recordable_frame < max_framepos) {
977                 first_recordable_frame += distance;
978         }
979         playback_sample += distance;
980
981         return 0;
982 }
983
984 /** Read some data for 1 channel from our playlist into a buffer.
985  *  @param buf Buffer to write to.
986  *  @param start Session frame to start reading from; updated to where we end up
987  *         after the read.
988  *  @param cnt Count of samples to read.
989  *  @param reversed true if we are running backwards, otherwise false.
990  */
991 int
992 AudioDiskstream::read (Sample* buf, Sample* mixdown_buffer, float* gain_buffer,
993                        framepos_t& start, framecnt_t cnt,
994                        int channel, bool reversed)
995 {
996         framecnt_t this_read = 0;
997         bool reloop = false;
998         framepos_t loop_end = 0;
999         framepos_t loop_start = 0;
1000         framecnt_t offset = 0;
1001         Location *loc = 0;
1002
1003         /* XXX we don't currently play loops in reverse. not sure why */
1004
1005         if (!reversed) {
1006
1007                 framecnt_t loop_length = 0;
1008
1009                 /* Make the use of a Location atomic for this read operation.
1010
1011                    Note: Locations don't get deleted, so all we care about
1012                    when I say "atomic" is that we are always pointing to
1013                    the same one and using a start/length values obtained
1014                    just once.
1015                 */
1016
1017                 if ((loc = loop_location) != 0) {
1018                         loop_start = loc->start();
1019                         loop_end = loc->end();
1020                         loop_length = loop_end - loop_start;
1021                 }
1022
1023                 /* if we are looping, ensure that the first frame we read is at the correct
1024                    position within the loop.
1025                 */
1026
1027                 if (loc && start >= loop_end) {
1028                         start = loop_start + ((start - loop_start) % loop_length);
1029                 }
1030
1031         }
1032
1033         if (reversed) {
1034                 start -= cnt;
1035         }
1036
1037         /* We need this while loop in case we hit a loop boundary, in which case our read from
1038            the playlist must be split into more than one section.
1039         */
1040
1041         while (cnt) {
1042
1043                 /* take any loop into account. we can't read past the end of the loop. */
1044
1045                 if (loc && (loop_end - start < cnt)) {
1046                         this_read = loop_end - start;
1047                         reloop = true;
1048                 } else {
1049                         reloop = false;
1050                         this_read = cnt;
1051                 }
1052
1053                 if (this_read == 0) {
1054                         break;
1055                 }
1056
1057                 this_read = min(cnt,this_read);
1058
1059                 if (audio_playlist()->read (buf+offset, mixdown_buffer, gain_buffer, start, this_read, channel) != this_read) {
1060                         error << string_compose(_("AudioDiskstream %1: cannot read %2 from playlist at frame %3"), id(), this_read,
1061                                          start) << endmsg;
1062                         return -1;
1063                 }
1064
1065                 if (reversed) {
1066
1067                         swap_by_ptr (buf, buf + this_read - 1);
1068
1069                 } else {
1070
1071                         /* if we read to the end of the loop, go back to the beginning */
1072
1073                         if (reloop) {
1074                                 start = loop_start;
1075                         } else {
1076                                 start += this_read;
1077                         }
1078                 }
1079
1080                 cnt -= this_read;
1081                 offset += this_read;
1082         }
1083
1084         return 0;
1085 }
1086
1087 int
1088 AudioDiskstream::_do_refill_with_alloc (bool partial_fill)
1089 {
1090         /* We limit disk reads to at most 4MB chunks, which with floating point
1091            samples would be 1M samples. But we might use 16 or 14 bit samples,
1092            in which case 4MB is more samples than that. Therefore size this for
1093            the smallest sample value .. 4MB = 2M samples (16 bit).
1094         */
1095
1096         Sample* mix_buf  = new Sample[2*1048576];
1097         float*  gain_buf = new float[2*1048576];
1098
1099         int ret = _do_refill (mix_buf, gain_buf, (partial_fill ? disk_read_chunk_frames : 0));
1100
1101         delete [] mix_buf;
1102         delete [] gain_buf;
1103
1104         return ret;
1105 }
1106
1107 /** Get some more data from disk and put it in our channels' playback_bufs,
1108  *  if there is suitable space in them.
1109  *
1110  * If fill_level is non-zero, then we will refill the buffer so that there is
1111  * still at least fill_level samples of space left to be filled. This is used
1112  * after locates so that we do not need to wait to fill the entire buffer.
1113  *
1114  */
1115
1116 int
1117 AudioDiskstream::_do_refill (Sample* mixdown_buffer, float* gain_buffer, framecnt_t fill_level)
1118 {
1119         int32_t ret = 0;
1120         framecnt_t to_read;
1121         RingBufferNPT<Sample>::rw_vector vector;
1122         bool const reversed = (_visible_speed * _session.transport_speed()) < 0.0f;
1123         framecnt_t total_space;
1124         framecnt_t zero_fill;
1125         uint32_t chan_n;
1126         ChannelList::iterator i;
1127         boost::shared_ptr<ChannelList> c = channels.reader();
1128         framecnt_t ts;
1129
1130         /* do not read from disk while session is marked as Loading, to avoid
1131            useless redundant I/O.
1132         */
1133
1134         if (_session.state_of_the_state() & Session::Loading) {
1135                 return 0;
1136         }
1137
1138         if (c->empty()) {
1139                 return 0;
1140         }
1141
1142         assert(mixdown_buffer);
1143         assert(gain_buffer);
1144
1145         vector.buf[0] = 0;
1146         vector.len[0] = 0;
1147         vector.buf[1] = 0;
1148         vector.len[1] = 0;
1149
1150         c->front()->playback_buf->get_write_vector (&vector);
1151
1152         if ((total_space = vector.len[0] + vector.len[1]) == 0) {
1153                 /* nowhere to write to */
1154                 return 0;
1155         }
1156
1157         if (fill_level) {
1158                 if (fill_level < total_space) {
1159                         total_space -= fill_level;
1160                 } else {
1161                         /* we can't do anything with it */
1162                         fill_level = 0;
1163                 }
1164         }
1165
1166         /* if we're running close to normal speed and there isn't enough
1167            space to do disk_read_chunk_frames of I/O, then don't bother.
1168
1169            at higher speeds, just do it because the sync between butler
1170            and audio thread may not be good enough.
1171
1172            Note: it is a design assumption that disk_read_chunk_frames is smaller
1173            than the playback buffer size, so this check should never trip when
1174            the playback buffer is empty.
1175         */
1176
1177         if ((total_space < disk_read_chunk_frames) && fabs (_actual_speed) < 2.0f) {
1178                 return 0;
1179         }
1180
1181         /* when slaved, don't try to get too close to the read pointer. this
1182            leaves space for the buffer reversal to have something useful to
1183            work with.
1184         */
1185
1186         if (_slaved && total_space < (framecnt_t) (c->front()->playback_buf->bufsize() / 2)) {
1187                 return 0;
1188         }
1189
1190         if (reversed) {
1191
1192                 if (file_frame == 0) {
1193
1194                         /* at start: nothing to do but fill with silence */
1195
1196                         for (chan_n = 0, i = c->begin(); i != c->end(); ++i, ++chan_n) {
1197
1198                                 ChannelInfo* chan (*i);
1199                                 chan->playback_buf->get_write_vector (&vector);
1200                                 memset (vector.buf[0], 0, sizeof(Sample) * vector.len[0]);
1201                                 if (vector.len[1]) {
1202                                         memset (vector.buf[1], 0, sizeof(Sample) * vector.len[1]);
1203                                 }
1204                                 chan->playback_buf->increment_write_ptr (vector.len[0] + vector.len[1]);
1205                         }
1206                         return 0;
1207                 }
1208
1209                 if (file_frame < total_space) {
1210
1211                         /* too close to the start: read what we can,
1212                            and then zero fill the rest
1213                         */
1214
1215                         zero_fill = total_space - file_frame;
1216                         total_space = file_frame;
1217
1218                 } else {
1219
1220                         zero_fill = 0;
1221                 }
1222
1223         } else {
1224
1225                 if (file_frame == max_framepos) {
1226
1227                         /* at end: nothing to do but fill with silence */
1228
1229                         for (chan_n = 0, i = c->begin(); i != c->end(); ++i, ++chan_n) {
1230
1231                                 ChannelInfo* chan (*i);
1232                                 chan->playback_buf->get_write_vector (&vector);
1233                                 memset (vector.buf[0], 0, sizeof(Sample) * vector.len[0]);
1234                                 if (vector.len[1]) {
1235                                         memset (vector.buf[1], 0, sizeof(Sample) * vector.len[1]);
1236                                 }
1237                                 chan->playback_buf->increment_write_ptr (vector.len[0] + vector.len[1]);
1238                         }
1239                         return 0;
1240                 }
1241
1242                 if (file_frame > max_framepos - total_space) {
1243
1244                         /* to close to the end: read what we can, and zero fill the rest */
1245
1246                         zero_fill = total_space - (max_framepos - file_frame);
1247                         total_space = max_framepos - file_frame;
1248
1249                 } else {
1250                         zero_fill = 0;
1251                 }
1252         }
1253
1254         framepos_t file_frame_tmp = 0;
1255
1256         /* total_space is in samples. We want to optimize read sizes in various sizes using bytes */
1257
1258         const size_t bits_per_sample = format_data_width (_session.config.get_native_file_data_format());
1259         size_t total_bytes = total_space * bits_per_sample / 8;
1260
1261         /* chunk size range is 256kB to 4MB. Bigger is faster in terms of MB/sec, but bigger chunk size always takes longer
1262          */
1263         size_t byte_size_for_read = max ((size_t) (256 * 1024), min ((size_t) (4 * 1048576), total_bytes));
1264
1265         /* find nearest (lower) multiple of 16384 */
1266
1267         byte_size_for_read = (byte_size_for_read / 16384) * 16384;
1268
1269         /* now back to samples */
1270
1271         framecnt_t samples_to_read = byte_size_for_read / (bits_per_sample / 8);
1272
1273         //cerr << name() << " will read " << byte_size_for_read << " out of total bytes " << total_bytes << " in buffer of "
1274         // << c->front()->playback_buf->bufsize() * bits_per_sample / 8 << " bps = " << bits_per_sample << endl;
1275         // cerr << name () << " read samples = " << samples_to_read << " out of total space " << total_space << " in buffer of " << c->front()->playback_buf->bufsize() << " samples\n";
1276
1277         // uint64_t before = g_get_monotonic_time ();
1278         // uint64_t elapsed;
1279
1280         for (chan_n = 0, i = c->begin(); i != c->end(); ++i, ++chan_n) {
1281
1282                 ChannelInfo* chan (*i);
1283                 Sample* buf1;
1284                 Sample* buf2;
1285                 framecnt_t len1, len2;
1286
1287                 chan->playback_buf->get_write_vector (&vector);
1288
1289                 if ((framecnt_t) vector.len[0] > samples_to_read) {
1290
1291                         /* we're not going to fill the first chunk, so certainly do not bother with the
1292                            other part. it won't be connected with the part we do fill, as in:
1293
1294                            .... => writable space
1295                            ++++ => readable space
1296                            ^^^^ => 1 x disk_read_chunk_frames that would be filled
1297
1298                            |......|+++++++++++++|...............................|
1299                            buf1                buf0
1300                                                 ^^^^^^^^^^^^^^^
1301
1302
1303                            So, just pretend that the buf1 part isn't there.
1304
1305                         */
1306
1307                         vector.buf[1] = 0;
1308                         vector.len[1] = 0;
1309
1310                 }
1311
1312                 ts = total_space;
1313                 file_frame_tmp = file_frame;
1314
1315                 buf1 = vector.buf[0];
1316                 len1 = vector.len[0];
1317                 buf2 = vector.buf[1];
1318                 len2 = vector.len[1];
1319
1320                 to_read = min (ts, len1);
1321                 to_read = min (to_read, (framecnt_t) samples_to_read);
1322
1323                 assert (to_read >= 0);
1324
1325                 if (to_read) {
1326
1327                         if (read (buf1, mixdown_buffer, gain_buffer, file_frame_tmp, to_read, chan_n, reversed)) {
1328                                 ret = -1;
1329                                 goto out;
1330                         }
1331
1332                         chan->playback_buf->increment_write_ptr (to_read);
1333                         ts -= to_read;
1334                 }
1335
1336                 to_read = min (ts, len2);
1337
1338                 if (to_read) {
1339
1340                         /* we read all of vector.len[0], but it wasn't the
1341                            entire samples_to_read of data, so read some or
1342                            all of vector.len[1] as well.
1343                         */
1344
1345                         if (read (buf2, mixdown_buffer, gain_buffer, file_frame_tmp, to_read, chan_n, reversed)) {
1346                                 ret = -1;
1347                                 goto out;
1348                         }
1349
1350                         chan->playback_buf->increment_write_ptr (to_read);
1351                 }
1352
1353                 if (zero_fill) {
1354                         /* XXX: do something */
1355                 }
1356
1357         }
1358
1359         // elapsed = g_get_monotonic_time () - before;
1360         // cerr << "\tbandwidth = " << (byte_size_for_read / 1048576.0) / (elapsed/1000000.0) << "MB/sec\n";
1361
1362         file_frame = file_frame_tmp;
1363         assert (file_frame >= 0);
1364
1365         ret = ((total_space - samples_to_read) > disk_read_chunk_frames);
1366
1367         c->front()->playback_buf->get_write_vector (&vector);
1368
1369   out:
1370         return ret;
1371 }
1372
1373 /** Flush pending data to disk.
1374  *
1375  * Important note: this function will write *AT MOST* disk_write_chunk_frames
1376  * of data to disk. it will never write more than that.  If it writes that
1377  * much and there is more than that waiting to be written, it will return 1,
1378  * otherwise 0 on success or -1 on failure.
1379  *
1380  * If there is less than disk_write_chunk_frames to be written, no data will be
1381  * written at all unless @a force_flush is true.
1382  */
1383 int
1384 AudioDiskstream::do_flush (RunContext /*context*/, bool force_flush)
1385 {
1386         uint32_t to_write;
1387         int32_t ret = 0;
1388         RingBufferNPT<Sample>::rw_vector vector;
1389         RingBufferNPT<CaptureTransition>::rw_vector transvec;
1390         framecnt_t total;
1391
1392         transvec.buf[0] = 0;
1393         transvec.buf[1] = 0;
1394         vector.buf[0] = 0;
1395         vector.buf[1] = 0;
1396
1397         boost::shared_ptr<ChannelList> c = channels.reader();
1398         for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
1399
1400                 (*chan)->capture_buf->get_read_vector (&vector);
1401
1402                 total = vector.len[0] + vector.len[1];
1403
1404                 if (total == 0 || (total < disk_write_chunk_frames && !force_flush && was_recording)) {
1405                         goto out;
1406                 }
1407
1408                 /* if there are 2+ chunks of disk i/o possible for
1409                    this track, let the caller know so that it can arrange
1410                    for us to be called again, ASAP.
1411
1412                    if we are forcing a flush, then if there is* any* extra
1413                    work, let the caller know.
1414
1415                    if we are no longer recording and there is any extra work,
1416                    let the caller know too.
1417                 */
1418
1419                 if (total >= 2 * disk_write_chunk_frames || ((force_flush || !was_recording) && total > disk_write_chunk_frames)) {
1420                         ret = 1;
1421                 }
1422
1423                 to_write = min (disk_write_chunk_frames, (framecnt_t) vector.len[0]);
1424
1425                 // check the transition buffer when recording destructive
1426                 // important that we get this after the capture buf
1427
1428                 if (destructive()) {
1429                         (*chan)->capture_transition_buf->get_read_vector(&transvec);
1430                         size_t transcount = transvec.len[0] + transvec.len[1];
1431                         size_t ti;
1432
1433                         for (ti=0; ti < transcount; ++ti) {
1434                                 CaptureTransition & captrans = (ti < transvec.len[0]) ? transvec.buf[0][ti] : transvec.buf[1][ti-transvec.len[0]];
1435
1436                                 if (captrans.type == CaptureStart) {
1437                                         // by definition, the first data we got above represents the given capture pos
1438
1439                                         (*chan)->write_source->mark_capture_start (captrans.capture_val);
1440                                         (*chan)->curr_capture_cnt = 0;
1441
1442                                 } else if (captrans.type == CaptureEnd) {
1443
1444                                         // capture end, the capture_val represents total frames in capture
1445
1446                                         if (captrans.capture_val <= (*chan)->curr_capture_cnt + to_write) {
1447
1448                                                 // shorten to make the write a perfect fit
1449                                                 uint32_t nto_write = (captrans.capture_val - (*chan)->curr_capture_cnt);
1450
1451                                                 if (nto_write < to_write) {
1452                                                         ret = 1; // should we?
1453                                                 }
1454                                                 to_write = nto_write;
1455
1456                                                 (*chan)->write_source->mark_capture_end ();
1457
1458                                                 // increment past this transition, but go no further
1459                                                 ++ti;
1460                                                 break;
1461                                         }
1462                                         else {
1463                                                 // actually ends just beyond this chunk, so force more work
1464                                                 ret = 1;
1465                                                 break;
1466                                         }
1467                                 }
1468                         }
1469
1470                         if (ti > 0) {
1471                                 (*chan)->capture_transition_buf->increment_read_ptr(ti);
1472                         }
1473                 }
1474
1475                 if ((!(*chan)->write_source) || (*chan)->write_source->write (vector.buf[0], to_write) != to_write) {
1476                         error << string_compose(_("AudioDiskstream %1: cannot write to disk"), id()) << endmsg;
1477                         return -1;
1478                 }
1479
1480                 (*chan)->capture_buf->increment_read_ptr (to_write);
1481                 (*chan)->curr_capture_cnt += to_write;
1482
1483                 if ((to_write == vector.len[0]) && (total > to_write) && (to_write < disk_write_chunk_frames) && !destructive()) {
1484
1485                         /* we wrote all of vector.len[0] but it wasn't an entire
1486                            disk_write_chunk_frames of data, so arrange for some part
1487                            of vector.len[1] to be flushed to disk as well.
1488                         */
1489
1490                         to_write = min ((framecnt_t)(disk_write_chunk_frames - to_write), (framecnt_t) vector.len[1]);
1491
1492                         DEBUG_TRACE (DEBUG::Butler, string_compose ("%1 additional write of %2\n", name(), to_write));
1493
1494                         if ((*chan)->write_source->write (vector.buf[1], to_write) != to_write) {
1495                                 error << string_compose(_("AudioDiskstream %1: cannot write to disk"), id()) << endmsg;
1496                                 return -1;
1497                         }
1498
1499                         (*chan)->capture_buf->increment_read_ptr (to_write);
1500                         (*chan)->curr_capture_cnt += to_write;
1501                 }
1502         }
1503
1504   out:
1505         return ret;
1506 }
1507
1508 void
1509 AudioDiskstream::transport_stopped_wallclock (struct tm& when, time_t twhen, bool abort_capture)
1510 {
1511         uint32_t buffer_position;
1512         bool more_work = true;
1513         int err = 0;
1514         boost::shared_ptr<AudioRegion> region;
1515         framecnt_t total_capture;
1516         SourceList srcs;
1517         SourceList::iterator src;
1518         ChannelList::iterator chan;
1519         vector<CaptureInfo*>::iterator ci;
1520         boost::shared_ptr<ChannelList> c = channels.reader();
1521         uint32_t n = 0;
1522         bool mark_write_completed = false;
1523
1524         finish_capture (c);
1525
1526         /* butler is already stopped, but there may be work to do
1527            to flush remaining data to disk.
1528         */
1529
1530         while (more_work && !err) {
1531                 switch (do_flush (TransportContext, true)) {
1532                 case 0:
1533                         more_work = false;
1534                         break;
1535                 case 1:
1536                         break;
1537                 case -1:
1538                         error << string_compose(_("AudioDiskstream \"%1\": cannot flush captured data to disk!"), _name) << endmsg;
1539                         err++;
1540                 }
1541         }
1542
1543         /* XXX is there anything we can do if err != 0 ? */
1544         Glib::Threads::Mutex::Lock lm (capture_info_lock);
1545
1546         if (capture_info.empty()) {
1547                 return;
1548         }
1549
1550         if (abort_capture) {
1551
1552                 if (destructive()) {
1553                         goto outout;
1554                 }
1555
1556                 for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
1557
1558                         if ((*chan)->write_source) {
1559
1560                                 (*chan)->write_source->mark_for_remove ();
1561                                 (*chan)->write_source->drop_references ();
1562                                 (*chan)->write_source.reset ();
1563                         }
1564
1565                         /* new source set up in "out" below */
1566                 }
1567
1568                 goto out;
1569         }
1570
1571         for (total_capture = 0, ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1572                 total_capture += (*ci)->frames;
1573         }
1574
1575         /* figure out the name for this take */
1576
1577         for (n = 0, chan = c->begin(); chan != c->end(); ++chan, ++n) {
1578
1579                 boost::shared_ptr<AudioFileSource> s = (*chan)->write_source;
1580
1581                 if (s) {
1582                         srcs.push_back (s);
1583                         s->update_header (capture_info.front()->start, when, twhen);
1584                         s->set_captured_for (_name.val());
1585                         s->mark_immutable ();
1586
1587                         if (Config->get_auto_analyse_audio()) {
1588                                 Analyser::queue_source_for_analysis (s, true);
1589                         }
1590
1591                         DEBUG_TRACE (DEBUG::CaptureAlignment, string_compose ("newly captured source %1 length %2\n", s->path(), s->length (0)));
1592                 }
1593         }
1594
1595         /* destructive tracks have a single, never changing region */
1596
1597         if (destructive()) {
1598
1599                 /* send a signal that any UI can pick up to do the right thing. there is
1600                    a small problem here in that a UI may need the peak data to be ready
1601                    for the data that was recorded and this isn't interlocked with that
1602                    process. this problem is deferred to the UI.
1603                  */
1604
1605                 _playlist->LayeringChanged(); // XXX this may not get the UI to do the right thing
1606
1607         } else {
1608
1609                 string whole_file_region_name;
1610                 whole_file_region_name = region_name_from_path (c->front()->write_source->name(), true);
1611
1612                 /* Register a new region with the Session that
1613                    describes the entire source. Do this first
1614                    so that any sub-regions will obviously be
1615                    children of this one (later!)
1616                 */
1617
1618                 try {
1619                         PropertyList plist;
1620
1621                         plist.add (Properties::start, c->front()->write_source->last_capture_start_frame());
1622                         plist.add (Properties::length, total_capture);
1623                         plist.add (Properties::name, whole_file_region_name);
1624                         boost::shared_ptr<Region> rx (RegionFactory::create (srcs, plist));
1625                         rx->set_automatic (true);
1626                         rx->set_whole_file (true);
1627
1628                         region = boost::dynamic_pointer_cast<AudioRegion> (rx);
1629                         region->special_set_position (capture_info.front()->start);
1630                 }
1631
1632
1633                 catch (failed_constructor& err) {
1634                         error << string_compose(_("%1: could not create region for complete audio file"), _name) << endmsg;
1635                         /* XXX what now? */
1636                 }
1637
1638                 _last_capture_sources.insert (_last_capture_sources.end(), srcs.begin(), srcs.end());
1639
1640                 _playlist->clear_changes ();
1641                 _playlist->set_capture_insertion_in_progress (true);
1642                 _playlist->freeze ();
1643
1644                 const framepos_t preroll_off = _session.preroll_record_trim_len ();
1645                 for (buffer_position = c->front()->write_source->last_capture_start_frame(), ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1646
1647                         string region_name;
1648
1649                         RegionFactory::region_name (region_name, whole_file_region_name, false);
1650
1651                         DEBUG_TRACE (DEBUG::CaptureAlignment, string_compose ("%1 capture bufpos %5 start @ %2 length %3 add new region %4\n",
1652                                                                               _name, (*ci)->start, (*ci)->frames, region_name, buffer_position));
1653
1654                         try {
1655
1656                                 PropertyList plist;
1657
1658                                 plist.add (Properties::start, buffer_position);
1659                                 plist.add (Properties::length, (*ci)->frames);
1660                                 plist.add (Properties::name, region_name);
1661
1662                                 boost::shared_ptr<Region> rx (RegionFactory::create (srcs, plist));
1663                                 region = boost::dynamic_pointer_cast<AudioRegion> (rx);
1664                                 if (preroll_off > 0) {
1665                                         region->trim_front (buffer_position + preroll_off);
1666                                 }
1667                         }
1668
1669                         catch (failed_constructor& err) {
1670                                 error << _("AudioDiskstream: could not create region for captured audio!") << endmsg;
1671                                 continue; /* XXX is this OK? */
1672                         }
1673
1674                         i_am_the_modifier++;
1675
1676                         _playlist->add_region (region, (*ci)->start + preroll_off, 1, non_layered());
1677                         _playlist->set_layer (region, DBL_MAX);
1678                         i_am_the_modifier--;
1679
1680                         buffer_position += (*ci)->frames;
1681                 }
1682
1683                 _playlist->thaw ();
1684                 _playlist->set_capture_insertion_in_progress (false);
1685                 _session.add_command (new StatefulDiffCommand (_playlist));
1686         }
1687
1688         mark_write_completed = true;
1689
1690   out:
1691         reset_write_sources (mark_write_completed);
1692
1693   outout:
1694
1695         for (ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1696                 delete *ci;
1697         }
1698
1699         capture_info.clear ();
1700         capture_start_frame = 0;
1701 }
1702
1703 void
1704 AudioDiskstream::transport_looped (framepos_t transport_frame)
1705 {
1706         if (was_recording) {
1707                 // all we need to do is finish this capture, with modified capture length
1708                 boost::shared_ptr<ChannelList> c = channels.reader();
1709
1710                 finish_capture (c);
1711
1712                 // the next region will start recording via the normal mechanism
1713                 // we'll set the start position to the current transport pos
1714                 // no latency adjustment or capture offset needs to be made, as that already happened the first time
1715                 capture_start_frame = transport_frame;
1716                 first_recordable_frame = transport_frame; // mild lie
1717                 last_recordable_frame = max_framepos;
1718                 was_recording = true;
1719
1720                 if (recordable() && destructive()) {
1721                         for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
1722
1723                                 RingBufferNPT<CaptureTransition>::rw_vector transvec;
1724                                 (*chan)->capture_transition_buf->get_write_vector(&transvec);
1725
1726                                 if (transvec.len[0] > 0) {
1727                                         transvec.buf[0]->type = CaptureStart;
1728                                         transvec.buf[0]->capture_val = capture_start_frame;
1729                                         (*chan)->capture_transition_buf->increment_write_ptr(1);
1730                                 }
1731                                 else {
1732                                         // bad!
1733                                         fatal << X_("programming error: capture_transition_buf is full on rec loop!  inconceivable!")
1734                                               << endmsg;
1735                                 }
1736                         }
1737                 }
1738
1739         }
1740 }
1741
1742 void
1743 AudioDiskstream::finish_capture (boost::shared_ptr<ChannelList> c)
1744 {
1745         was_recording = false;
1746         first_recordable_frame = max_framepos;
1747         last_recordable_frame = max_framepos;
1748
1749         if (capture_captured == 0) {
1750                 return;
1751         }
1752
1753         if (recordable() && destructive()) {
1754                 for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
1755
1756                         RingBufferNPT<CaptureTransition>::rw_vector transvec;
1757                         (*chan)->capture_transition_buf->get_write_vector(&transvec);
1758
1759                         if (transvec.len[0] > 0) {
1760                                 transvec.buf[0]->type = CaptureEnd;
1761                                 transvec.buf[0]->capture_val = capture_captured;
1762                                 (*chan)->capture_transition_buf->increment_write_ptr(1);
1763                         }
1764                         else {
1765                                 // bad!
1766                                 fatal << string_compose (_("programmer error: %1"), X_("capture_transition_buf is full when stopping record!  inconceivable!")) << endmsg;
1767                         }
1768                 }
1769         }
1770
1771
1772         CaptureInfo* ci = new CaptureInfo;
1773
1774         ci->start =  capture_start_frame;
1775         ci->frames = capture_captured;
1776
1777         /* XXX theoretical race condition here. Need atomic exchange ?
1778            However, the circumstances when this is called right
1779            now (either on record-disable or transport_stopped)
1780            mean that no actual race exists. I think ...
1781            We now have a capture_info_lock, but it is only to be used
1782            to synchronize in the transport_stop and the capture info
1783            accessors, so that invalidation will not occur (both non-realtime).
1784         */
1785
1786         DEBUG_TRACE (DEBUG::CaptureAlignment, string_compose ("Finish capture, add new CI, %1 + %2\n", ci->start, ci->frames));
1787
1788         capture_info.push_back (ci);
1789         capture_captured = 0;
1790
1791         /* now we've finished a capture, reset first_recordable_frame for next time */
1792         first_recordable_frame = max_framepos;
1793 }
1794
1795 void
1796 AudioDiskstream::set_record_enabled (bool yn)
1797 {
1798         if (!recordable() || !_session.record_enabling_legal() || _io->n_ports().n_audio() == 0 || record_safe ()) {
1799                 return;
1800         }
1801
1802         /* can't rec-enable in destructive mode if transport is before start */
1803
1804         if (destructive() && yn && _session.transport_frame() < _session.current_start_frame()) {
1805                 return;
1806         }
1807
1808         /* yes, i know that this not proof against race conditions, but its
1809            good enough. i think.
1810         */
1811
1812         if (record_enabled() != yn) {
1813                 if (yn) {
1814                         engage_record_enable ();
1815                 } else {
1816                         disengage_record_enable ();
1817                 }
1818
1819                 RecordEnableChanged (); /* EMIT SIGNAL */
1820         }
1821 }
1822
1823 void
1824 AudioDiskstream::set_record_safe (bool yn)
1825 {
1826         if (!recordable() || !_session.record_enabling_legal() || _io->n_ports().n_audio() == 0) {
1827                 return;
1828         }
1829
1830         /* can't rec-safe in destructive mode if transport is before start ????
1831          REQUIRES REVIEW */
1832
1833         if (destructive() && yn && _session.transport_frame() < _session.current_start_frame()) {
1834                 return;
1835         }
1836
1837         /* yes, i know that this not proof against race conditions, but its
1838          good enough. i think.
1839          */
1840
1841         if (record_safe () != yn) {
1842                 if (yn) {
1843                         engage_record_safe ();
1844                 } else {
1845                         disengage_record_safe ();
1846                 }
1847
1848                 RecordSafeChanged (); /* EMIT SIGNAL */
1849         }
1850 }
1851
1852 bool
1853 AudioDiskstream::prep_record_enable ()
1854 {
1855         if (!recordable() || !_session.record_enabling_legal() || _io->n_ports().n_audio() == 0 || record_safe ()) { // REQUIRES REVIEW "|| record_safe ()"
1856                 return false;
1857         }
1858
1859         /* can't rec-enable in destructive mode if transport is before start */
1860
1861         if (destructive() && _session.transport_frame() < _session.current_start_frame()) {
1862                 return false;
1863         }
1864
1865         bool rolling = _session.transport_speed() != 0.0f;
1866         boost::shared_ptr<ChannelList> c = channels.reader();
1867
1868         capturing_sources.clear ();
1869
1870         if (Config->get_monitoring_model() == HardwareMonitoring) {
1871
1872                 for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
1873                         (*chan)->source.request_input_monitoring (!(_session.config.get_auto_input() && rolling));
1874                         capturing_sources.push_back ((*chan)->write_source);
1875                         Source::Lock lock((*chan)->write_source->mutex());
1876                         (*chan)->write_source->mark_streaming_write_started (lock);
1877                 }
1878
1879         } else {
1880                 for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
1881                         capturing_sources.push_back ((*chan)->write_source);
1882                         Source::Lock lock((*chan)->write_source->mutex());
1883                         (*chan)->write_source->mark_streaming_write_started (lock);
1884                 }
1885         }
1886
1887         return true;
1888 }
1889
1890 bool
1891 AudioDiskstream::prep_record_disable ()
1892 {
1893         boost::shared_ptr<ChannelList> c = channels.reader();
1894         if (Config->get_monitoring_model() == HardwareMonitoring) {
1895                 for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
1896                         (*chan)->source.request_input_monitoring (false);
1897                 }
1898         }
1899         capturing_sources.clear ();
1900
1901         return true;
1902 }
1903
1904 XMLNode&
1905 AudioDiskstream::get_state ()
1906 {
1907         XMLNode& node (Diskstream::get_state());
1908         char buf[64] = "";
1909         LocaleGuard lg;
1910
1911         boost::shared_ptr<ChannelList> c = channels.reader();
1912         snprintf (buf, sizeof(buf), "%u", (unsigned int) c->size());
1913         node.add_property ("channels", buf);
1914
1915         if (!capturing_sources.empty() && _session.get_record_enabled()) {
1916
1917                 XMLNode* cs_child = new XMLNode (X_("CapturingSources"));
1918                 XMLNode* cs_grandchild;
1919
1920                 for (vector<boost::shared_ptr<AudioFileSource> >::iterator i = capturing_sources.begin(); i != capturing_sources.end(); ++i) {
1921                         cs_grandchild = new XMLNode (X_("file"));
1922                         cs_grandchild->add_property (X_("path"), (*i)->path());
1923                         cs_child->add_child_nocopy (*cs_grandchild);
1924                 }
1925
1926                 /* store the location where capture will start */
1927
1928                 Location* pi;
1929
1930                 if (_session.preroll_record_punch_enabled ()) {
1931                         snprintf (buf, sizeof (buf), "%" PRId64, _session.preroll_record_punch_pos ());
1932                 } else if (_session.config.get_punch_in() && ((pi = _session.locations()->auto_punch_location()) != 0)) {
1933                         snprintf (buf, sizeof (buf), "%" PRId64, pi->start());
1934                 } else {
1935                         snprintf (buf, sizeof (buf), "%" PRId64, _session.transport_frame());
1936                 }
1937
1938                 cs_child->add_property (X_("at"), buf);
1939                 node.add_child_nocopy (*cs_child);
1940         }
1941
1942         return node;
1943 }
1944
1945 int
1946 AudioDiskstream::set_state (const XMLNode& node, int version)
1947 {
1948         XMLProperty const * prop;
1949         XMLNodeList nlist = node.children();
1950         XMLNodeIterator niter;
1951         uint32_t nchans = 1;
1952         XMLNode* capture_pending_node = 0;
1953         LocaleGuard lg;
1954
1955         /* prevent write sources from being created */
1956
1957         in_set_state = true;
1958
1959         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1960                 if ((*niter)->name() == IO::state_node_name) {
1961                         deprecated_io_node = new XMLNode (**niter);
1962                 }
1963
1964                 if ((*niter)->name() == X_("CapturingSources")) {
1965                         capture_pending_node = *niter;
1966                 }
1967         }
1968
1969         if (Diskstream::set_state (node, version)) {
1970                 return -1;
1971         }
1972
1973         if ((prop = node.property ("channels")) != 0) {
1974                 nchans = atoi (prop->value().c_str());
1975         }
1976
1977         // create necessary extra channels
1978         // we are always constructed with one and we always need one
1979
1980         _n_channels.set(DataType::AUDIO, channels.reader()->size());
1981
1982         if (nchans > _n_channels.n_audio()) {
1983
1984                 add_channel (nchans - _n_channels.n_audio());
1985                 IO::PortCountChanged(_n_channels);
1986
1987         } else if (nchans < _n_channels.n_audio()) {
1988
1989                 remove_channel (_n_channels.n_audio() - nchans);
1990         }
1991
1992
1993
1994         if (!destructive() && capture_pending_node) {
1995                 /* destructive streams have one and only one source per channel,
1996                    and so they never end up in pending capture in any useful
1997                    sense.
1998                 */
1999                 use_pending_capture_data (*capture_pending_node);
2000         }
2001
2002         in_set_state = false;
2003
2004         /* make sure this is clear before we do anything else */
2005
2006         capturing_sources.clear ();
2007
2008         /* write sources are handled when we handle the input set
2009            up of the IO that owns this DS (::non_realtime_input_change())
2010         */
2011
2012         return 0;
2013 }
2014
2015 int
2016 AudioDiskstream::use_new_write_source (uint32_t n)
2017 {
2018         boost::shared_ptr<ChannelList> c = channels.reader();
2019
2020         if (!recordable()) {
2021                 return 1;
2022         }
2023
2024         if (n >= c->size()) {
2025                 error << string_compose (_("AudioDiskstream: channel %1 out of range"), n) << endmsg;
2026                 return -1;
2027         }
2028
2029         ChannelInfo* chan = (*c)[n];
2030
2031         try {
2032                 if ((chan->write_source = _session.create_audio_source_for_session (
2033                              n_channels().n_audio(), write_source_name(), n, destructive())) == 0) {
2034                         throw failed_constructor();
2035                 }
2036         }
2037
2038         catch (failed_constructor &err) {
2039                 error << string_compose (_("%1:%2 new capture file not initialized correctly"), _name, n) << endmsg;
2040                 chan->write_source.reset ();
2041                 return -1;
2042         }
2043
2044         /* do not remove destructive files even if they are empty */
2045
2046         chan->write_source->set_allow_remove_if_empty (!destructive());
2047
2048         return 0;
2049 }
2050
2051 void
2052 AudioDiskstream::reset_write_sources (bool mark_write_complete, bool /*force*/)
2053 {
2054         ChannelList::iterator chan;
2055         boost::shared_ptr<ChannelList> c = channels.reader();
2056         uint32_t n;
2057
2058         if (!_session.writable() || !recordable()) {
2059                 return;
2060         }
2061
2062         capturing_sources.clear ();
2063
2064         for (chan = c->begin(), n = 0; chan != c->end(); ++chan, ++n) {
2065
2066                 if (!destructive()) {
2067
2068                         if ((*chan)->write_source) {
2069
2070                                 if (mark_write_complete) {
2071                                         Source::Lock lock((*chan)->write_source->mutex());
2072                                         (*chan)->write_source->mark_streaming_write_completed (lock);
2073                                         (*chan)->write_source->done_with_peakfile_writes ();
2074                                 }
2075
2076                                 if ((*chan)->write_source->removable()) {
2077                                         (*chan)->write_source->mark_for_remove ();
2078                                         (*chan)->write_source->drop_references ();
2079                                 }
2080
2081                                 (*chan)->write_source.reset ();
2082                         }
2083
2084                         use_new_write_source (n);
2085
2086                         if (record_enabled()) {
2087                                 capturing_sources.push_back ((*chan)->write_source);
2088                         }
2089
2090                 } else {
2091
2092                         if ((*chan)->write_source == 0) {
2093                                 use_new_write_source (n);
2094                         }
2095                 }
2096         }
2097
2098         if (destructive() && !c->empty ()) {
2099
2100                 /* we now have all our write sources set up, so create the
2101                    playlist's single region.
2102                 */
2103
2104                 if (_playlist->empty()) {
2105                         setup_destructive_playlist ();
2106                 }
2107         }
2108 }
2109
2110 void
2111 AudioDiskstream::set_block_size (pframes_t /*nframes*/)
2112 {
2113         if (_session.get_block_size() > speed_buffer_size) {
2114                 speed_buffer_size = _session.get_block_size();
2115                 boost::shared_ptr<ChannelList> c = channels.reader();
2116
2117                 for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
2118                         if ((*chan)->speed_buffer)
2119                                 delete [] (*chan)->speed_buffer;
2120                         (*chan)->speed_buffer = new Sample[speed_buffer_size];
2121                 }
2122         }
2123         allocate_temporary_buffers ();
2124 }
2125
2126 void
2127 AudioDiskstream::allocate_temporary_buffers ()
2128 {
2129         /* make sure the wrap buffer is at least large enough to deal
2130            with the speeds up to 1.2, to allow for micro-variation
2131            when slaving to MTC, Timecode etc.
2132         */
2133
2134         double const sp = max (fabs (_actual_speed), 1.2);
2135         framecnt_t required_wrap_size = (framecnt_t) ceil (_session.get_block_size() * sp) + 2;
2136
2137         if (required_wrap_size > wrap_buffer_size) {
2138
2139                 boost::shared_ptr<ChannelList> c = channels.reader();
2140
2141                 for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
2142                         if ((*chan)->playback_wrap_buffer) {
2143                                 delete [] (*chan)->playback_wrap_buffer;
2144                         }
2145                         (*chan)->playback_wrap_buffer = new Sample[required_wrap_size];
2146                         if ((*chan)->capture_wrap_buffer) {
2147                                 delete [] (*chan)->capture_wrap_buffer;
2148                         }
2149                         (*chan)->capture_wrap_buffer = new Sample[required_wrap_size];
2150                 }
2151
2152                 wrap_buffer_size = required_wrap_size;
2153         }
2154 }
2155
2156 void
2157 AudioDiskstream::request_input_monitoring (bool yn)
2158 {
2159         boost::shared_ptr<ChannelList> c = channels.reader();
2160
2161         for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
2162                 (*chan)->source.request_input_monitoring (yn);
2163         }
2164 }
2165
2166 void
2167 AudioDiskstream::set_align_style_from_io ()
2168 {
2169         bool have_physical = false;
2170
2171         if (_alignment_choice != Automatic) {
2172                 return;
2173         }
2174
2175         if (_io == 0) {
2176                 return;
2177         }
2178
2179         get_input_sources ();
2180
2181         boost::shared_ptr<ChannelList> c = channels.reader();
2182
2183         for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
2184                 if ((*chan)->source.is_physical ()) {
2185                         have_physical = true;
2186                         break;
2187                 }
2188         }
2189
2190 #ifdef MIXBUS
2191         // compensate for latency when bouncing from master or mixbus.
2192         // we need to use "ExistingMaterial" to pick up the master bus' latency
2193         // see also Route::direct_feeds_according_to_reality
2194         IOVector ios;
2195         ios.push_back (_io);
2196         if (_session.master_out() && ios.fed_by (_session.master_out()->output())) {
2197                 have_physical = true;
2198         }
2199         for (uint32_t n = 0; n < NUM_MIXBUSES && !have_physical; ++n) {
2200                 if (_session.get_mixbus (n) && ios.fed_by (_session.get_mixbus(n)->output())) {
2201                         have_physical = true;
2202                 }
2203         }
2204 #endif
2205
2206         if (have_physical) {
2207                 set_align_style (ExistingMaterial);
2208         } else {
2209                 set_align_style (CaptureTime);
2210         }
2211 }
2212
2213 int
2214 AudioDiskstream::add_channel_to (boost::shared_ptr<ChannelList> c, uint32_t how_many)
2215 {
2216         while (how_many--) {
2217                 c->push_back (new ChannelInfo(
2218                                       _session.butler()->audio_diskstream_playback_buffer_size(),
2219                                       _session.butler()->audio_diskstream_capture_buffer_size(),
2220                                       speed_buffer_size, wrap_buffer_size));
2221                 interpolation.add_channel_to (
2222                         _session.butler()->audio_diskstream_playback_buffer_size(),
2223                         speed_buffer_size);
2224         }
2225
2226         _n_channels.set(DataType::AUDIO, c->size());
2227
2228         return 0;
2229 }
2230
2231 int
2232 AudioDiskstream::add_channel (uint32_t how_many)
2233 {
2234         RCUWriter<ChannelList> writer (channels);
2235         boost::shared_ptr<ChannelList> c = writer.get_copy();
2236
2237         return add_channel_to (c, how_many);
2238 }
2239
2240 int
2241 AudioDiskstream::remove_channel_from (boost::shared_ptr<ChannelList> c, uint32_t how_many)
2242 {
2243         while (how_many-- && !c->empty()) {
2244                 delete c->back();
2245                 c->pop_back();
2246                 interpolation.remove_channel_from ();
2247         }
2248
2249         _n_channels.set(DataType::AUDIO, c->size());
2250
2251         return 0;
2252 }
2253
2254 int
2255 AudioDiskstream::remove_channel (uint32_t how_many)
2256 {
2257         RCUWriter<ChannelList> writer (channels);
2258         boost::shared_ptr<ChannelList> c = writer.get_copy();
2259
2260         return remove_channel_from (c, how_many);
2261 }
2262
2263 float
2264 AudioDiskstream::playback_buffer_load () const
2265 {
2266         boost::shared_ptr<ChannelList> c = channels.reader();
2267
2268         if (c->empty ()) {
2269                 return 1.0;
2270         }
2271
2272         return (float) ((double) c->front()->playback_buf->read_space()/
2273                            (double) c->front()->playback_buf->bufsize());
2274 }
2275
2276 float
2277 AudioDiskstream::capture_buffer_load () const
2278 {
2279         boost::shared_ptr<ChannelList> c = channels.reader();
2280
2281         if (c->empty ()) {
2282                 return 1.0;
2283         }
2284
2285         return (float) ((double) c->front()->capture_buf->write_space()/
2286                         (double) c->front()->capture_buf->bufsize());
2287 }
2288
2289 int
2290 AudioDiskstream::use_pending_capture_data (XMLNode& node)
2291 {
2292         XMLProperty const * prop;
2293         XMLNodeList nlist = node.children();
2294         XMLNodeIterator niter;
2295         boost::shared_ptr<AudioFileSource> fs;
2296         boost::shared_ptr<AudioFileSource> first_fs;
2297         SourceList pending_sources;
2298         framepos_t position;
2299
2300         if ((prop = node.property (X_("at"))) == 0) {
2301                 return -1;
2302         }
2303
2304         if (sscanf (prop->value().c_str(), "%" PRIu64, &position) != 1) {
2305                 return -1;
2306         }
2307
2308         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2309                 if ((*niter)->name() == X_("file")) {
2310
2311                         if ((prop = (*niter)->property (X_("path"))) == 0) {
2312                                 continue;
2313                         }
2314
2315                         // This protects sessions from errant CapturingSources in stored sessions
2316                         GStatBuf sbuf;
2317                         if (g_stat (prop->value().c_str(), &sbuf)) {
2318                                 continue;
2319                         }
2320
2321                         /* XXX as of June 2014, we always record to mono
2322                            files. Since this Source is being created as part of
2323                            crash recovery, we know that we need the first
2324                            channel (the final argument to the SourceFactory
2325                            call below). If we ever support non-mono files for
2326                            capture, this will need rethinking.
2327                         */
2328
2329                         try {
2330                                 fs = boost::dynamic_pointer_cast<AudioFileSource> (SourceFactory::createForRecovery (DataType::AUDIO, _session, prop->value(), 0));
2331                         }
2332
2333                         catch (failed_constructor& err) {
2334                                 error << string_compose (_("%1: cannot restore pending capture source file %2"),
2335                                                   _name, prop->value())
2336                                       << endmsg;
2337                                 return -1;
2338                         }
2339
2340                         pending_sources.push_back (fs);
2341
2342                         if (first_fs == 0) {
2343                                 first_fs = fs;
2344                         }
2345
2346                         fs->set_captured_for (_name.val());
2347                 }
2348         }
2349
2350         if (pending_sources.size() == 0) {
2351                 /* nothing can be done */
2352                 return 1;
2353         }
2354
2355         if (pending_sources.size() != _n_channels.n_audio()) {
2356                 error << string_compose (_("%1: incorrect number of pending sources listed - ignoring them all"), _name)
2357                       << endmsg;
2358                 return -1;
2359         }
2360
2361         try {
2362
2363                 boost::shared_ptr<AudioRegion> wf_region;
2364                 boost::shared_ptr<AudioRegion> region;
2365
2366                 /* First create the whole file region */
2367
2368                 PropertyList plist;
2369
2370                 plist.add (Properties::start, 0);
2371                 plist.add (Properties::length, first_fs->length (first_fs->timeline_position()));
2372                 plist.add (Properties::name, region_name_from_path (first_fs->name(), true));
2373
2374                 wf_region = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (pending_sources, plist));
2375
2376                 wf_region->set_automatic (true);
2377                 wf_region->set_whole_file (true);
2378                 wf_region->special_set_position (position);
2379
2380                 /* Now create a region that isn't the whole file for adding to
2381                  * the playlist */
2382
2383                 region = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (pending_sources, plist));
2384
2385                 _playlist->add_region (region, position);
2386         }
2387
2388         catch (failed_constructor& err) {
2389                 error << string_compose (
2390                                 _("%1: cannot create whole-file region from pending capture sources"),
2391                                 _name) << endmsg;
2392
2393                 return -1;
2394         }
2395
2396
2397         return 0;
2398 }
2399
2400 #ifdef XXX_OLD_DESTRUCTIVE_API_XXX
2401 int
2402 AudioDiskstream::set_non_layered (bool yn)
2403 {
2404         if (yn != non_layered()) {
2405
2406                 if (yn) {
2407                         _flags = Flag (_flags | NonLayered);
2408                 } else {
2409                         _flags = Flag (_flags & ~NonLayered);
2410                 }
2411         }
2412
2413         return 0;
2414 }
2415
2416 int
2417 AudioDiskstream::set_destructive (bool yn)
2418 {
2419         if (yn != destructive()) {
2420
2421                 if (yn) {
2422                         bool bounce_ignored;
2423                         /* requestor should already have checked this and
2424                            bounced if necessary and desired
2425                         */
2426                         if (!can_become_destructive (bounce_ignored)) {
2427                                 return -1;
2428                         }
2429                         _flags = Flag (_flags | Destructive);
2430                         use_destructive_playlist ();
2431                 } else {
2432                         _flags = Flag (_flags & ~Destructive);
2433                         reset_write_sources (true, true);
2434                 }
2435         }
2436
2437         return 0;
2438 }
2439
2440 bool
2441 AudioDiskstream::can_become_destructive (bool& requires_bounce) const
2442 {
2443         if (Profile->get_trx()) {
2444                 return false;
2445         }
2446
2447         if (!_playlist) {
2448                 requires_bounce = false;
2449                 return false;
2450         }
2451
2452         /* if no regions are present: easy */
2453
2454         if (_playlist->n_regions() == 0) {
2455                 requires_bounce = false;
2456                 return true;
2457         }
2458
2459         /* is there only one region ? */
2460
2461         if (_playlist->n_regions() != 1) {
2462                 requires_bounce = true;
2463                 return false;
2464         }
2465
2466         boost::shared_ptr<Region> first;
2467         {
2468                 const RegionList& rl (_playlist->region_list_property().rlist());
2469                 assert((rl.size() == 1));
2470                 first = rl.front();
2471
2472         }
2473
2474         if (!first) {
2475                 requires_bounce = false;
2476                 return true;
2477         }
2478
2479         /* do the source(s) for the region cover the session start position ? */
2480
2481         if (first->position() != _session.current_start_frame()) {
2482                 // what is the idea here?  why start() ??
2483                 if (first->start() > _session.current_start_frame()) {
2484                         requires_bounce = true;
2485                         return false;
2486                 }
2487         }
2488
2489         /* currently RouteTimeAxisView::set_track_mode does not
2490          * implement bounce. Existing regions cannot be converted.
2491          *
2492          * so let's make sure this region is already set up
2493          * as tape-track (spanning the complete range)
2494          */
2495         if (first->length() != max_framepos - first->position()) {
2496                 requires_bounce = true;
2497                 return false;
2498         }
2499
2500         /* is the source used by only 1 playlist ? */
2501
2502         boost::shared_ptr<AudioRegion> afirst = boost::dynamic_pointer_cast<AudioRegion> (first);
2503
2504         assert (afirst);
2505
2506         if (_session.playlists->source_use_count (afirst->source()) > 1) {
2507                 requires_bounce = true;
2508                 return false;
2509         }
2510
2511         requires_bounce = false;
2512         return true;
2513 }
2514 #endif
2515
2516 void
2517 AudioDiskstream::adjust_playback_buffering ()
2518 {
2519         boost::shared_ptr<ChannelList> c = channels.reader();
2520
2521         for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
2522                 (*chan)->resize_playback (_session.butler()->audio_diskstream_playback_buffer_size());
2523         }
2524 }
2525
2526 void
2527 AudioDiskstream::adjust_capture_buffering ()
2528 {
2529         boost::shared_ptr<ChannelList> c = channels.reader();
2530
2531         for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
2532                 (*chan)->resize_capture (_session.butler()->audio_diskstream_capture_buffer_size());
2533         }
2534 }
2535
2536 bool
2537 AudioDiskstream::ChannelSource::is_physical () const
2538 {
2539         if (name.empty()) {
2540                 return false;
2541         }
2542
2543         return AudioEngine::instance()->port_is_physical (name);
2544 }
2545
2546 void
2547 AudioDiskstream::ChannelSource::request_input_monitoring (bool yn) const
2548 {
2549         if (name.empty()) {
2550                 return;
2551         }
2552
2553         return AudioEngine::instance()->request_input_monitoring (name, yn);
2554 }
2555
2556 AudioDiskstream::ChannelInfo::ChannelInfo (framecnt_t playback_bufsize, framecnt_t capture_bufsize, framecnt_t speed_size, framecnt_t wrap_size)
2557 {
2558         current_capture_buffer = 0;
2559         current_playback_buffer = 0;
2560         curr_capture_cnt = 0;
2561
2562         speed_buffer = new Sample[speed_size];
2563         playback_wrap_buffer = new Sample[wrap_size];
2564         capture_wrap_buffer = new Sample[wrap_size];
2565
2566         playback_buf = new RingBufferNPT<Sample> (playback_bufsize);
2567         capture_buf = new RingBufferNPT<Sample> (capture_bufsize);
2568         capture_transition_buf = new RingBufferNPT<CaptureTransition> (256);
2569
2570         /* touch the ringbuffer buffers, which will cause
2571            them to be mapped into locked physical RAM if
2572            we're running with mlockall(). this doesn't do
2573            much if we're not.
2574         */
2575
2576         memset (playback_buf->buffer(), 0, sizeof (Sample) * playback_buf->bufsize());
2577         memset (capture_buf->buffer(), 0, sizeof (Sample) * capture_buf->bufsize());
2578         memset (capture_transition_buf->buffer(), 0, sizeof (CaptureTransition) * capture_transition_buf->bufsize());
2579 }
2580
2581 void
2582 AudioDiskstream::ChannelInfo::resize_playback (framecnt_t playback_bufsize)
2583 {
2584         delete playback_buf;
2585         playback_buf = new RingBufferNPT<Sample> (playback_bufsize);
2586         memset (playback_buf->buffer(), 0, sizeof (Sample) * playback_buf->bufsize());
2587 }
2588
2589 void
2590 AudioDiskstream::ChannelInfo::resize_capture (framecnt_t capture_bufsize)
2591 {
2592         delete capture_buf;
2593
2594         capture_buf = new RingBufferNPT<Sample> (capture_bufsize);
2595         memset (capture_buf->buffer(), 0, sizeof (Sample) * capture_buf->bufsize());
2596 }
2597
2598 AudioDiskstream::ChannelInfo::~ChannelInfo ()
2599 {
2600         if (write_source) {
2601                 if (write_source->removable()) {
2602                         /* this is a "stub" write source which exists in the
2603                            Session source list, but is removable. We must emit
2604                            a drop references call because it should not
2605                            continue to exist. If we do not do this, then the
2606                            Session retains a reference to it, it is not
2607                            deleted, and later attempts to create a new source
2608                            file will use wierd naming because it already
2609                            exists.
2610
2611                            XXX longer term TO-DO: do not add to session source
2612                            list until we write to the source.
2613                         */
2614                         write_source->drop_references ();
2615                 }
2616         }
2617
2618         write_source.reset ();
2619
2620         delete [] speed_buffer;
2621         speed_buffer = 0;
2622
2623         delete [] playback_wrap_buffer;
2624         playback_wrap_buffer = 0;
2625
2626         delete [] capture_wrap_buffer;
2627         capture_wrap_buffer = 0;
2628
2629         delete playback_buf;
2630         playback_buf = 0;
2631
2632         delete capture_buf;
2633         capture_buf = 0;
2634
2635         delete capture_transition_buf;
2636         capture_transition_buf = 0;
2637 }
2638
2639
2640 bool
2641 AudioDiskstream::set_name (string const & name)
2642 {
2643         if (_name == name) {
2644                 return true;
2645         }
2646         Diskstream::set_name (name);
2647
2648         /* get a new write source so that its name reflects the new diskstream name */
2649
2650         boost::shared_ptr<ChannelList> c = channels.reader();
2651         ChannelList::iterator i;
2652         int n = 0;
2653
2654         for (n = 0, i = c->begin(); i != c->end(); ++i, ++n) {
2655                 use_new_write_source (n);
2656         }
2657
2658         return true;
2659 }
2660
2661 bool
2662 AudioDiskstream::set_write_source_name (const std::string& str) {
2663         if (_write_source_name == str) {
2664                 return true;
2665         }
2666
2667         Diskstream::set_write_source_name (str);
2668
2669         if (_write_source_name == name()) {
2670                 return true;
2671         }
2672         boost::shared_ptr<ChannelList> c = channels.reader();
2673         ChannelList::iterator i;
2674         int n = 0;
2675
2676         for (n = 0, i = c->begin(); i != c->end(); ++i, ++n) {
2677                 use_new_write_source (n);
2678         }
2679         return true;
2680 }