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