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