fixes for destructive track offsets of various kinds; move from jack_nframes_t -...
[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 #include <sys/stat.h>
31 #include <sys/mman.h>
32
33 #include <pbd/error.h>
34 #include <pbd/basename.h>
35 #include <glibmm/thread.h>
36 #include <pbd/xml++.h>
37 #include <pbd/memento_command.h>
38
39 #include <ardour/ardour.h>
40 #include <ardour/audioengine.h>
41 #include <ardour/audio_diskstream.h>
42 #include <ardour/utils.h>
43 #include <ardour/configuration.h>
44 #include <ardour/audiofilesource.h>
45 #include <ardour/destructive_filesource.h>
46 #include <ardour/send.h>
47 #include <ardour/region_factory.h>
48 #include <ardour/audioplaylist.h>
49 #include <ardour/cycle_timer.h>
50 #include <ardour/audioregion.h>
51 #include <ardour/source_factory.h>
52
53 #include "i18n.h"
54 #include <locale.h>
55
56 using namespace std;
57 using namespace ARDOUR;
58 using namespace PBD;
59
60 size_t  AudioDiskstream::_working_buffers_size = 0;
61 Sample* AudioDiskstream::_mixdown_buffer       = 0;
62 gain_t* AudioDiskstream::_gain_buffer          = 0;
63
64 AudioDiskstream::AudioDiskstream (Session &sess, const string &name, Diskstream::Flag flag)
65         : Diskstream(sess, name, flag)
66         , deprecated_io_node(NULL)
67 {
68         /* prevent any write sources from being created */
69
70         in_set_state = true;
71
72         init(flag);
73         use_new_playlist ();
74
75         in_set_state = false;
76 }
77         
78 AudioDiskstream::AudioDiskstream (Session& sess, const XMLNode& node)
79         : Diskstream(sess, node)
80         , deprecated_io_node(NULL)
81 {
82         in_set_state = true;
83         init (Recordable);
84
85         if (set_state (node)) {
86                 in_set_state = false;
87                 throw failed_constructor();
88         }
89
90         in_set_state = false;
91
92         if (destructive()) {
93                 use_destructive_playlist ();
94         }
95 }
96
97 void
98 AudioDiskstream::init_channel (ChannelInfo &chan)
99 {
100         chan.playback_wrap_buffer = 0;
101         chan.capture_wrap_buffer = 0;
102         chan.speed_buffer = 0;
103         chan.peak_power = 0.0f;
104         chan.source = 0;
105         chan.current_capture_buffer = 0;
106         chan.current_playback_buffer = 0;
107         chan.curr_capture_cnt = 0;
108         
109         chan.playback_buf = new RingBufferNPT<Sample> (_session.diskstream_buffer_size());
110         chan.capture_buf = new RingBufferNPT<Sample> (_session.diskstream_buffer_size());
111         chan.capture_transition_buf = new RingBufferNPT<CaptureTransition> (128);
112         
113         
114         /* touch the ringbuffer buffers, which will cause
115            them to be mapped into locked physical RAM if
116            we're running with mlockall(). this doesn't do
117            much if we're not.  
118         */
119         memset (chan.playback_buf->buffer(), 0, sizeof (Sample) * chan.playback_buf->bufsize());
120         memset (chan.capture_buf->buffer(), 0, sizeof (Sample) * chan.capture_buf->bufsize());
121         memset (chan.capture_transition_buf->buffer(), 0, sizeof (CaptureTransition) * chan.capture_transition_buf->bufsize());
122 }
123
124
125 void
126 AudioDiskstream::init (Diskstream::Flag f)
127 {
128         Diskstream::init(f);
129
130         /* there are no channels at this point, so these
131            two calls just get speed_buffer_size and wrap_buffer
132            size setup without duplicating their code.
133         */
134
135         set_block_size (_session.get_block_size());
136         allocate_temporary_buffers ();
137
138         add_channel ();
139         assert(_n_channels == 1);
140 }
141
142 void
143 AudioDiskstream::destroy_channel (ChannelInfo &chan)
144 {
145         if (chan.write_source) {
146                 chan.write_source.reset ();
147         }
148                 
149         if (chan.speed_buffer) {
150                 delete [] chan.speed_buffer;
151         }
152
153         if (chan.playback_wrap_buffer) {
154                 delete [] chan.playback_wrap_buffer;
155         }
156         if (chan.capture_wrap_buffer) {
157                 delete [] chan.capture_wrap_buffer;
158         }
159         
160         delete chan.playback_buf;
161         delete chan.capture_buf;
162         delete chan.capture_transition_buf;
163         
164         chan.playback_buf = 0;
165         chan.capture_buf = 0;
166 }
167
168 AudioDiskstream::~AudioDiskstream ()
169 {
170         Glib::Mutex::Lock lm (state_lock);
171
172         for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan)
173                 destroy_channel((*chan));
174         
175         channels.clear();
176 }
177
178 void
179 AudioDiskstream::allocate_working_buffers()
180 {
181         assert(disk_io_frames() > 0);
182
183         _working_buffers_size = disk_io_frames();
184         _mixdown_buffer       = new Sample[_working_buffers_size];
185         _gain_buffer          = new gain_t[_working_buffers_size];
186 }
187
188 void
189 AudioDiskstream::free_working_buffers()
190 {
191         delete [] _mixdown_buffer;
192         delete [] _gain_buffer;
193         _working_buffers_size = 0;
194         _mixdown_buffer       = 0;
195         _gain_buffer          = 0;
196 }
197
198 void
199 AudioDiskstream::non_realtime_input_change ()
200 {
201         { 
202                 Glib::Mutex::Lock lm (state_lock);
203
204                 if (input_change_pending == NoChange) {
205                         return;
206                 }
207
208                 if (input_change_pending & ConfigurationChanged) {
209
210                         if (_io->n_inputs() > _n_channels) {
211                                 
212                                 // we need to add new channel infos
213                                 
214                                 int diff = _io->n_inputs() - channels.size();
215                                 
216                                 for (int i = 0; i < diff; ++i) {
217                                         add_channel ();
218                                 }
219                                 
220                 } else if (_io->n_inputs() < _n_channels) {
221                                 
222                                 // we need to get rid of channels
223                                 
224                                 int diff = channels.size() - _io->n_inputs();
225                                 
226                                 for (int i = 0; i < diff; ++i) {
227                                         remove_channel ();
228                                 }
229                         }
230                 } 
231
232                 get_input_sources ();
233                 set_capture_offset ();
234                 
235                 if (first_input_change) {
236                         set_align_style (_persistent_alignment_style);
237                         first_input_change = false;
238                 } else {
239                         set_align_style_from_io ();
240                 }
241
242                 input_change_pending = NoChange;
243         }
244
245         /* reset capture files */
246
247         reset_write_sources (false);
248
249         /* now refill channel buffers */
250
251         if (speed() != 1.0f || speed() != -1.0f) {
252                 seek ((nframes_t) (_session.transport_frame() * (double) speed()));
253         } else {
254                 seek (_session.transport_frame());
255         }
256 }
257
258 void
259 AudioDiskstream::get_input_sources ()
260 {
261         uint32_t ni = _io->n_inputs();
262         
263         for (uint32_t n = 0; n < ni; ++n) {
264                 
265                 const char **connections = _io->input(n)->get_connections ();
266                 ChannelInfo& chan = channels[n];
267                 
268                 if (connections == 0 || connections[0] == 0) {
269                         
270                         if (chan.source) {
271                                 // _source->disable_metering ();
272                         }
273                         
274                         chan.source = 0;
275                         
276                 } else {
277                         chan.source = _session.engine().get_port_by_name (connections[0]);
278                 }
279                 
280                 if (connections) {
281                         free (connections);
282                 }
283         }
284 }               
285
286 int
287 AudioDiskstream::find_and_use_playlist (const string& name)
288 {
289         Playlist* pl;
290         AudioPlaylist* playlist;
291                 
292         if ((pl = _session.playlist_by_name (name)) == 0) {
293                 playlist = new AudioPlaylist(_session, name);
294                 pl = playlist;
295         }
296
297         if ((playlist = dynamic_cast<AudioPlaylist*> (pl)) == 0) {
298                 error << string_compose(_("AudioDiskstream: Playlist \"%1\" isn't an audio playlist"), name) << endmsg;
299                 return -1;
300         }
301
302         return use_playlist (playlist);
303 }
304
305 int
306 AudioDiskstream::use_playlist (Playlist* playlist)
307 {
308         assert(dynamic_cast<AudioPlaylist*>(playlist));
309
310         Diskstream::use_playlist(playlist);
311
312         return 0;
313 }
314
315 int
316 AudioDiskstream::use_new_playlist ()
317 {
318         string newname;
319         AudioPlaylist* playlist;
320
321         if (!in_set_state && destructive()) {
322                 return 0;
323         }
324
325         if (_playlist) {
326                 newname = Playlist::bump_name (_playlist->name(), _session);
327         } else {
328                 newname = Playlist::bump_name (_name, _session);
329         }
330
331         if ((playlist = new AudioPlaylist (_session, newname, hidden())) != 0) {
332                 playlist->set_orig_diskstream_id (id());
333                 return use_playlist (playlist);
334         } else { 
335                 return -1;
336         }
337 }
338
339 int
340 AudioDiskstream::use_copy_playlist ()
341 {
342         assert(audio_playlist());
343
344         if (destructive()) {
345                 return 0;
346         }
347
348         if (_playlist == 0) {
349                 error << string_compose(_("AudioDiskstream %1: there is no existing playlist to make a copy of!"), _name) << endmsg;
350                 return -1;
351         }
352
353         string newname;
354         AudioPlaylist* playlist;
355
356         newname = Playlist::bump_name (_playlist->name(), _session);
357         
358         if ((playlist  = new AudioPlaylist (*audio_playlist(), newname)) != 0) {
359                 playlist->set_orig_diskstream_id (id());
360                 return use_playlist (playlist);
361         } else { 
362                 return -1;
363         }
364 }
365
366 void
367 AudioDiskstream::setup_destructive_playlist ()
368 {
369         SourceList srcs;
370
371         for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
372                 srcs.push_back ((*chan).write_source);
373         }
374
375         /* a single full-sized region */
376
377         boost::shared_ptr<Region> region (RegionFactory::create (srcs, 0, max_frames - srcs.front()->natural_position(), _name));
378         _playlist->add_region (region, srcs.front()->natural_position());               
379 }
380
381 void
382 AudioDiskstream::use_destructive_playlist ()
383 {
384         /* this is called from the XML-based constructor. when its done,
385            we already have a playlist and a region, but we need to
386            set up our sources for write. we use the sources associated 
387            with the (presumed single, full-extent) region.
388         */
389
390         boost::shared_ptr<Region> rp = _playlist->find_next_region (_session.current_start_frame(), Start, 1);
391
392         if (!rp) {
393                 reset_write_sources (false, true);
394                 return;
395         }
396
397         boost::shared_ptr<AudioRegion> region = boost::dynamic_pointer_cast<AudioRegion> (rp);
398
399         if (region == 0) {
400                 throw failed_constructor();
401         }
402
403         uint32_t n;
404         ChannelList::iterator chan;
405
406         for (n = 0, chan = channels.begin(); chan != channels.end(); ++chan, ++n) {
407                 (*chan).write_source = boost::dynamic_pointer_cast<AudioFileSource>(region->source (n));
408                 assert((*chan).write_source);
409                 (*chan).write_source->set_allow_remove_if_empty (false);
410         }
411
412         /* the source list will never be reset for a destructive track */
413 }
414
415 void
416 AudioDiskstream::check_record_status (nframes_t transport_frame, nframes_t nframes, bool can_record)
417 {
418         int possibly_recording;
419         int rolling;
420         int change;
421         const int transport_rolling = 0x4;
422         const int track_rec_enabled = 0x2;
423         const int global_rec_enabled = 0x1;
424
425         /* merge together the 3 factors that affect record status, and compute
426            what has changed.
427         */
428
429         rolling = _session.transport_speed() != 0.0f;
430         possibly_recording = (rolling << 2) | (record_enabled() << 1) | can_record;
431         change = possibly_recording ^ last_possibly_recording;
432
433         if (possibly_recording == last_possibly_recording) {
434                 return;
435         }
436
437         /* change state */
438
439         /* if per-track or global rec-enable turned on while the other was already on, we've started recording */
440
441         if ((change & track_rec_enabled) && record_enabled() && (!(change & global_rec_enabled) && can_record) || 
442             ((change & global_rec_enabled) && can_record && (!(change & track_rec_enabled) && record_enabled()))) {
443                 
444                 /* starting to record: compute first+last frames */
445
446                 first_recordable_frame = transport_frame + _capture_offset;
447                 last_recordable_frame = max_frames;
448                 capture_start_frame = transport_frame;
449
450                 if (!(last_possibly_recording & transport_rolling) && (possibly_recording & transport_rolling)) {
451
452                         /* was stopped, now rolling (and recording) */
453
454                         if (_alignment_style == ExistingMaterial) {
455                                 first_recordable_frame += _session.worst_output_latency();
456                         } else {
457                                 first_recordable_frame += _roll_delay;
458                         }
459
460                 } else {
461
462                         /* was rolling, but record state changed */
463
464                         if (_alignment_style == ExistingMaterial) {
465
466                                 if (!Config->get_punch_in()) {
467
468                                         /* manual punch in happens at the correct transport frame
469                                            because the user hit a button. but to get alignment correct 
470                                            we have to back up the position of the new region to the 
471                                            appropriate spot given the roll delay.
472                                         */
473
474                                         capture_start_frame -= _roll_delay;
475
476                                         /* XXX paul notes (august 2005): i don't know why
477                                            this is needed.
478                                         */
479
480                                         first_recordable_frame += _capture_offset;
481
482                                 } else {
483
484                                         /* autopunch toggles recording at the precise
485                                            transport frame, and then the DS waits
486                                            to start recording for a time that depends
487                                            on the output latency.
488                                         */
489
490                                         first_recordable_frame += _session.worst_output_latency();
491                                 }
492
493                         } else {
494
495                                 if (Config->get_punch_in()) {
496                                         first_recordable_frame += _roll_delay;
497                                 } else {
498                                         capture_start_frame -= _roll_delay;
499                                 }
500                         }
501                         
502                 }
503
504                 if (_flags & Recordable) {
505                         for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
506                                 
507                                 RingBufferNPT<CaptureTransition>::rw_vector transvec;
508                                 (*chan).capture_transition_buf->get_write_vector(&transvec);
509                                 
510                                 if (transvec.len[0] > 0) {
511                                         transvec.buf[0]->type = CaptureStart;
512                                         transvec.buf[0]->capture_val = capture_start_frame;
513                                         (*chan).capture_transition_buf->increment_write_ptr(1);
514                                 }
515                                 else {
516                                         // bad!
517                                         fatal << X_("programming error: capture_transition_buf is full on rec start!  inconceivable!") 
518                                               << endmsg;
519                                 }
520                         }
521                 }
522
523         } else if (!record_enabled() || !can_record) {
524                 
525                 /* stop recording */
526
527                 last_recordable_frame = transport_frame + _capture_offset;
528                 
529                 if (_alignment_style == ExistingMaterial) {
530                         last_recordable_frame += _session.worst_output_latency();
531                 } else {
532                         last_recordable_frame += _roll_delay;
533                 }
534         }
535
536         last_possibly_recording = possibly_recording;
537 }
538
539 int
540 AudioDiskstream::process (nframes_t transport_frame, nframes_t nframes, nframes_t offset, bool can_record, bool rec_monitors_input)
541 {
542         uint32_t n;
543         ChannelList::iterator c;
544         int ret = -1;
545         nframes_t rec_offset = 0;
546         nframes_t rec_nframes = 0;
547         bool nominally_recording;
548         bool re = record_enabled ();
549         bool collect_playback = false;
550
551         /* if we've already processed the frames corresponding to this call,
552            just return. this allows multiple routes that are taking input
553            from this diskstream to call our ::process() method, but have
554            this stuff only happen once. more commonly, it allows both
555            the AudioTrack that is using this AudioDiskstream *and* the Session
556            to call process() without problems.
557         */
558
559         if (_processed) {
560                 return 0;
561         }
562
563         check_record_status (transport_frame, nframes, can_record);
564
565         nominally_recording = (can_record && re);
566
567         if (nframes == 0) {
568                 _processed = true;
569                 return 0;
570         }
571
572         /* This lock is held until the end of AudioDiskstream::commit, so these two functions
573            must always be called as a pair. The only exception is if this function
574            returns a non-zero value, in which case, ::commit should not be called.
575         */
576
577         // If we can't take the state lock return.
578         if (!state_lock.trylock()) {
579                 return 1;
580         }
581
582         adjust_capture_position = 0;
583
584         for (c = channels.begin(); c != channels.end(); ++c) {
585                 (*c).current_capture_buffer = 0;
586                 (*c).current_playback_buffer  = 0;
587         }
588
589         if (nominally_recording || (_session.get_record_enabled() && Config->get_punch_in())) {
590                 OverlapType ot;
591                 
592                 ot = coverage (first_recordable_frame, last_recordable_frame, transport_frame, transport_frame + nframes);
593
594                 switch (ot) {
595                 case OverlapNone:
596                         rec_nframes = 0;
597                         break;
598                         
599                 case OverlapInternal:
600                 /*     ----------    recrange
601                          |---|       transrange
602                 */
603                         rec_nframes = nframes;
604                         rec_offset = 0;
605                         break;
606                         
607                 case OverlapStart:
608                         /*    |--------|    recrange
609                             -----|          transrange
610                         */
611                         rec_nframes = transport_frame + nframes - first_recordable_frame;
612                         if (rec_nframes) {
613                                 rec_offset = first_recordable_frame - transport_frame;
614                         }
615                         break;
616                         
617                 case OverlapEnd:
618                         /*    |--------|    recrange
619                                  |--------  transrange
620                         */
621                         rec_nframes = last_recordable_frame - transport_frame;
622                         rec_offset = 0;
623                         break;
624                         
625                 case OverlapExternal:
626                         /*    |--------|    recrange
627                             --------------  transrange
628                         */
629                         rec_nframes = last_recordable_frame - last_recordable_frame;
630                         rec_offset = first_recordable_frame - transport_frame;
631                         break;
632                 }
633
634                 if (rec_nframes && !was_recording) {
635                         capture_captured = 0;
636                         was_recording = true;
637                 }
638         }
639
640
641         if (can_record && !_last_capture_regions.empty()) {
642                 _last_capture_regions.clear ();
643         }
644
645         if (nominally_recording || rec_nframes) {
646
647                 for (n = 0, c = channels.begin(); c != channels.end(); ++c, ++n) {
648                         
649                         ChannelInfo& chan (*c);
650                 
651                         chan.capture_buf->get_write_vector (&chan.capture_vector);
652
653                         if (rec_nframes <= chan.capture_vector.len[0]) {
654                                 
655                                 chan.current_capture_buffer = chan.capture_vector.buf[0];
656
657                                 /* note: grab the entire port buffer, but only copy what we were supposed to for recording, and use
658                                    rec_offset
659                                 */
660
661                                 memcpy (chan.current_capture_buffer, _io->input(n)->get_buffer (rec_nframes) + offset + rec_offset, sizeof (Sample) * rec_nframes);
662
663                         } else {
664
665                                 nframes_t total = chan.capture_vector.len[0] + chan.capture_vector.len[1];
666
667                                 if (rec_nframes > total) {
668                                         DiskOverrun ();
669                                         goto out;
670                                 }
671
672                                 Sample* buf = _io->input (n)->get_buffer (nframes) + offset;
673                                 nframes_t first = chan.capture_vector.len[0];
674
675                                 memcpy (chan.capture_wrap_buffer, buf, sizeof (Sample) * first);
676                                 memcpy (chan.capture_vector.buf[0], buf, sizeof (Sample) * first);
677                                 memcpy (chan.capture_wrap_buffer+first, buf + first, sizeof (Sample) * (rec_nframes - first));
678                                 memcpy (chan.capture_vector.buf[1], buf + first, sizeof (Sample) * (rec_nframes - first));
679                                 
680                                 chan.current_capture_buffer = chan.capture_wrap_buffer;
681                         }
682                 }
683
684         } else {
685
686                 if (was_recording) {
687                         finish_capture (rec_monitors_input);
688                 }
689
690         }
691         
692         if (rec_nframes) {
693                 
694                 /* data will be written to disk */
695
696                 if (rec_nframes == nframes && rec_offset == 0) {
697
698                         for (c = channels.begin(); c != channels.end(); ++c) {
699                                 (*c).current_playback_buffer = (*c).current_capture_buffer;
700                         }
701
702                         playback_distance = nframes;
703
704                 } else {
705
706
707                         /* we can't use the capture buffer as the playback buffer, because
708                            we recorded only a part of the current process' cycle data
709                            for capture.
710                         */
711
712                         collect_playback = true;
713                 }
714
715                 adjust_capture_position = rec_nframes;
716
717         } else if (nominally_recording) {
718
719                 /* can't do actual capture yet - waiting for latency effects to finish before we start*/
720
721                 for (c = channels.begin(); c != channels.end(); ++c) {
722                         (*c).current_playback_buffer = (*c).current_capture_buffer;
723                 }
724
725                 playback_distance = nframes;
726
727         } else {
728
729                 collect_playback = true;
730         }
731
732         if (collect_playback) {
733
734                 /* we're doing playback */
735
736                 nframes_t necessary_samples;
737
738                 /* no varispeed playback if we're recording, because the output .... TBD */
739
740                 if (rec_nframes == 0 && _actual_speed != 1.0f) {
741                         necessary_samples = (nframes_t) floor ((nframes * fabs (_actual_speed))) + 1;
742                 } else {
743                         necessary_samples = nframes;
744                 }
745                 
746                 for (c = channels.begin(); c != channels.end(); ++c) {
747                         (*c).playback_buf->get_read_vector (&(*c).playback_vector);
748                 }
749
750                 n = 0;                  
751
752                 for (c = channels.begin(); c != channels.end(); ++c, ++n) {
753                 
754                         ChannelInfo& chan (*c);
755
756                         if (necessary_samples <= chan.playback_vector.len[0]) {
757
758                                 chan.current_playback_buffer = chan.playback_vector.buf[0];
759
760                         } else {
761                                 nframes_t total = chan.playback_vector.len[0] + chan.playback_vector.len[1];
762                                 
763                                 if (necessary_samples > total) {
764                                         DiskUnderrun ();
765                                         goto out;
766                                         
767                                 } else {
768                                         
769                                         memcpy ((char *) chan.playback_wrap_buffer, chan.playback_vector.buf[0],
770                                                 chan.playback_vector.len[0] * sizeof (Sample));
771                                         memcpy (chan.playback_wrap_buffer + chan.playback_vector.len[0], chan.playback_vector.buf[1], 
772                                                 (necessary_samples - chan.playback_vector.len[0]) * sizeof (Sample));
773                                         
774                                         chan.current_playback_buffer = chan.playback_wrap_buffer;
775                                 }
776                         }
777                 } 
778
779                 if (rec_nframes == 0 && _actual_speed != 1.0f && _actual_speed != -1.0f) {
780                         
781                         uint64_t phase = last_phase;
782                         nframes_t i = 0;
783
784                         // Linearly interpolate into the alt buffer
785                         // using 40.24 fixp maths (swh)
786
787                         for (c = channels.begin(); c != channels.end(); ++c) {
788
789                                 float fr;
790                                 ChannelInfo& chan (*c);
791
792                                 i = 0;
793                                 phase = last_phase;
794
795                                 for (nframes_t outsample = 0; outsample < nframes; ++outsample) {
796                                         i = phase >> 24;
797                                         fr = (phase & 0xFFFFFF) / 16777216.0f;
798                                         chan.speed_buffer[outsample] = 
799                                                 chan.current_playback_buffer[i] * (1.0f - fr) +
800                                                 chan.current_playback_buffer[i+1] * fr;
801                                         phase += phi;
802                                 }
803                                 
804                                 chan.current_playback_buffer = chan.speed_buffer;
805                         }
806
807                         playback_distance = i + 1;
808                         last_phase = (phase & 0xFFFFFF);
809
810                 } else {
811                         playback_distance = nframes;
812                 }
813
814         }
815
816         ret = 0;
817
818   out:
819         _processed = true;
820
821         if (ret) {
822
823                 /* we're exiting with failure, so ::commit will not
824                    be called. unlock the state lock.
825                 */
826                 
827                 state_lock.unlock();
828         } 
829
830         return ret;
831 }
832
833 bool
834 AudioDiskstream::commit (nframes_t nframes)
835 {
836         bool need_butler = false;
837
838         if (_actual_speed < 0.0) {
839                 playback_sample -= playback_distance;
840         } else {
841                 playback_sample += playback_distance;
842         }
843
844         for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
845
846                 (*chan).playback_buf->increment_read_ptr (playback_distance);
847                 
848                 if (adjust_capture_position) {
849                         (*chan).capture_buf->increment_write_ptr (adjust_capture_position);
850                 }
851         }
852         
853         if (adjust_capture_position != 0) {
854                 capture_captured += adjust_capture_position;
855                 adjust_capture_position = 0;
856         }
857         
858         if (_slaved) {
859                 need_butler = channels[0].playback_buf->write_space() >= channels[0].playback_buf->bufsize() / 2;
860         } else {
861                 need_butler = channels[0].playback_buf->write_space() >= disk_io_chunk_frames
862                         || channels[0].capture_buf->read_space() >= disk_io_chunk_frames;
863         }
864
865         state_lock.unlock();
866
867         _processed = false;
868
869         return need_butler;
870 }
871
872 void
873 AudioDiskstream::set_pending_overwrite (bool yn)
874 {
875         /* called from audio thread, so we can use the read ptr and playback sample as we wish */
876         
877         pending_overwrite = yn;
878
879         overwrite_frame = playback_sample;
880         overwrite_offset = channels.front().playback_buf->get_read_ptr();
881 }
882
883 int
884 AudioDiskstream::overwrite_existing_buffers ()
885 {
886         Sample* mixdown_buffer;
887         float* gain_buffer;
888         int ret = -1;
889         bool reversed = (_visible_speed * _session.transport_speed()) < 0.0f;
890
891         overwrite_queued = false;
892
893         /* assume all are the same size */
894         nframes_t size = channels[0].playback_buf->bufsize();
895         
896         mixdown_buffer = new Sample[size];
897         gain_buffer = new float[size];
898         
899         /* reduce size so that we can fill the buffer correctly. */
900         size--;
901         
902         uint32_t n=0;
903         nframes_t start;
904
905         for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan, ++n) {
906
907                 start = overwrite_frame;
908                 nframes_t cnt = size;
909                 
910                 /* to fill the buffer without resetting the playback sample, we need to
911                    do it one or two chunks (normally two).
912
913                    |----------------------------------------------------------------------|
914
915                                        ^
916                                        overwrite_offset
917                     |<- second chunk->||<----------------- first chunk ------------------>|
918                    
919                 */
920                 
921                 nframes_t to_read = size - overwrite_offset;
922
923                 if (read ((*chan).playback_buf->buffer() + overwrite_offset, mixdown_buffer, gain_buffer, start, to_read, *chan, n, reversed)) {
924                         error << string_compose(_("AudioDiskstream %1: when refilling, cannot read %2 from playlist at frame %3"),
925                                          _id, size, playback_sample) << endmsg;
926                         goto out;
927                 }
928                         
929                 if (cnt > to_read) {
930
931                         cnt -= to_read;
932                 
933                         if (read ((*chan).playback_buf->buffer(), mixdown_buffer, gain_buffer,
934                                   start, cnt, *chan, n, reversed)) {
935                                 error << string_compose(_("AudioDiskstream %1: when refilling, cannot read %2 from playlist at frame %3"),
936                                                  _id, size, playback_sample) << endmsg;
937                                 goto out;
938                         }
939                 }
940         }
941
942         ret = 0;
943  
944   out:
945         pending_overwrite = false;
946         delete [] gain_buffer;
947         delete [] mixdown_buffer;
948         return ret;
949 }
950
951 int
952 AudioDiskstream::seek (nframes_t frame, bool complete_refill)
953 {
954         Glib::Mutex::Lock lm (state_lock);
955         uint32_t n;
956         int ret;
957         ChannelList::iterator chan;
958
959         for (n = 0, chan = channels.begin(); chan != channels.end(); ++chan, ++n) {
960                 (*chan).playback_buf->reset ();
961                 (*chan).capture_buf->reset ();
962         }
963         
964         /* can't rec-enable in destructive mode if transport is before start */
965
966         if (destructive() && record_enabled() && frame < _session.current_start_frame()) {
967                 disengage_record_enable ();
968         }
969
970         playback_sample = frame;
971         file_frame = frame;
972
973         if (complete_refill) {
974                 while ((ret = do_refill_with_alloc ()) > 0) ;
975         } else {
976                 ret = do_refill_with_alloc ();
977         }
978
979         return ret;
980 }
981
982 int
983 AudioDiskstream::can_internal_playback_seek (nframes_t distance)
984 {
985         ChannelList::iterator chan;
986
987         for (chan = channels.begin(); chan != channels.end(); ++chan) {
988                 if ((*chan).playback_buf->read_space() < distance) {
989                         return false;
990                 } 
991         }
992         return true;
993 }
994
995 int
996 AudioDiskstream::internal_playback_seek (nframes_t distance)
997 {
998         ChannelList::iterator chan;
999
1000         for (chan = channels.begin(); chan != channels.end(); ++chan) {
1001                 (*chan).playback_buf->increment_read_ptr (distance);
1002         }
1003
1004         first_recordable_frame += distance;
1005         playback_sample += distance;
1006         
1007         return 0;
1008 }
1009
1010 int
1011 AudioDiskstream::read (Sample* buf, Sample* mixdown_buffer, float* gain_buffer, nframes_t& start, nframes_t cnt, 
1012                   ChannelInfo& channel_info, int channel, bool reversed)
1013 {
1014         nframes_t this_read = 0;
1015         bool reloop = false;
1016         nframes_t loop_end = 0;
1017         nframes_t loop_start = 0;
1018         nframes_t loop_length = 0;
1019         nframes_t offset = 0;
1020         Location *loc = 0;
1021
1022         if (!reversed) {
1023                 /* Make the use of a Location atomic for this read operation.
1024                    
1025                    Note: Locations don't get deleted, so all we care about
1026                    when I say "atomic" is that we are always pointing to
1027                    the same one and using a start/length values obtained
1028                    just once.
1029                 */
1030                 
1031                 if ((loc = loop_location) != 0) {
1032                         loop_start = loc->start();
1033                         loop_end = loc->end();
1034                         loop_length = loop_end - loop_start;
1035                 }
1036                 
1037                 /* if we are looping, ensure that the first frame we read is at the correct
1038                    position within the loop.
1039                 */
1040                 
1041                 if (loc && start >= loop_end) {
1042                         //cerr << "start adjusted from " << start;
1043                         start = loop_start + ((start - loop_start) % loop_length);
1044                         //cerr << "to " << start << endl;
1045                 }
1046                 //cerr << "start is " << start << "  loopstart: " << loop_start << "  loopend: " << loop_end << endl;
1047         }
1048
1049         while (cnt) {
1050
1051                 /* take any loop into account. we can't read past the end of the loop. */
1052
1053                 if (loc && (loop_end - start < cnt)) {
1054                         this_read = loop_end - start;
1055                         //cerr << "reloop true: thisread: " << this_read << "  cnt: " << cnt << endl;
1056                         reloop = true;
1057                 } else {
1058                         reloop = false;
1059                         this_read = cnt;
1060                 }
1061
1062                 if (this_read == 0) {
1063                         break;
1064                 }
1065
1066                 this_read = min(cnt,this_read);
1067
1068                 if (audio_playlist()->read (buf+offset, mixdown_buffer, gain_buffer, start, this_read, channel) != this_read) {
1069                         error << string_compose(_("AudioDiskstream %1: cannot read %2 from playlist at frame %3"), _id, this_read, 
1070                                          start) << endmsg;
1071                         return -1;
1072                 }
1073
1074                 _read_data_count = _playlist->read_data_count();
1075                 
1076                 if (reversed) {
1077
1078                         /* don't adjust start, since caller has already done that
1079                          */
1080
1081                         swap_by_ptr (buf, buf + this_read - 1);
1082                         
1083                 } else {
1084                         
1085                         /* if we read to the end of the loop, go back to the beginning */
1086                         
1087                         if (reloop) {
1088                                 start = loop_start;
1089                         } else {
1090                                 start += this_read;
1091                         }
1092                 } 
1093
1094                 cnt -= this_read;
1095                 offset += this_read;
1096         }
1097
1098         return 0;
1099 }
1100
1101 int
1102 AudioDiskstream::do_refill_with_alloc()
1103 {
1104         Sample* mix_buf  = new Sample[disk_io_chunk_frames];
1105         float*  gain_buf = new float[disk_io_chunk_frames];
1106
1107         int ret = _do_refill(mix_buf, gain_buf);
1108         
1109         delete [] mix_buf;
1110         delete [] gain_buf;
1111
1112         return ret;
1113 }
1114
1115 int
1116 AudioDiskstream::_do_refill (Sample* mixdown_buffer, float* gain_buffer)
1117 {
1118         int32_t ret = 0;
1119         nframes_t to_read;
1120         RingBufferNPT<Sample>::rw_vector vector;
1121         bool reversed = (_visible_speed * _session.transport_speed()) < 0.0f;
1122         nframes_t total_space;
1123         nframes_t zero_fill;
1124         uint32_t chan_n;
1125         ChannelList::iterator i;
1126         nframes_t ts;
1127
1128         assert(mixdown_buffer);
1129         assert(gain_buffer);
1130
1131         channels.front().playback_buf->get_write_vector (&vector);
1132         
1133         if ((total_space = vector.len[0] + vector.len[1]) == 0) {
1134                 return 0;
1135         }
1136         
1137         /* if there are 2+ chunks of disk i/o possible for
1138            this track, let the caller know so that it can arrange
1139            for us to be called again, ASAP.
1140         */
1141         
1142         if (total_space >= (_slaved?3:2) * disk_io_chunk_frames) {
1143                 ret = 1;
1144         }
1145         
1146         /* if we're running close to normal speed and there isn't enough 
1147            space to do disk_io_chunk_frames of I/O, then don't bother.  
1148            
1149            at higher speeds, just do it because the sync between butler
1150            and audio thread may not be good enough.
1151         */
1152         
1153         if ((total_space < disk_io_chunk_frames) && fabs (_actual_speed) < 2.0f) {
1154                 return 0;
1155         }
1156         
1157         /* when slaved, don't try to get too close to the read pointer. this
1158            leaves space for the buffer reversal to have something useful to
1159            work with.
1160         */
1161         
1162         if (_slaved && total_space < (channels.front().playback_buf->bufsize() / 2)) {
1163                 return 0;
1164         }
1165
1166         total_space = min (disk_io_chunk_frames, total_space);
1167
1168         if (reversed) {
1169
1170                 if (file_frame == 0) {
1171
1172                         /* at start: nothing to do but fill with silence */
1173
1174                         for (chan_n = 0, i = channels.begin(); i != channels.end(); ++i, ++chan_n) {
1175                                         
1176                                 ChannelInfo& chan (*i);
1177                                 chan.playback_buf->get_write_vector (&vector);
1178                                 memset (vector.buf[0], 0, sizeof(Sample) * vector.len[0]);
1179                                 if (vector.len[1]) {
1180                                         memset (vector.buf[1], 0, sizeof(Sample) * vector.len[1]);
1181                                 }
1182                                 chan.playback_buf->increment_write_ptr (vector.len[0] + vector.len[1]);
1183                         }
1184                         return 0;
1185                 }
1186
1187                 if (file_frame < total_space) {
1188
1189                         /* too close to the start: read what we can, 
1190                            and then zero fill the rest 
1191                         */
1192
1193                         zero_fill = total_space - file_frame;
1194                         total_space = file_frame;
1195                         file_frame = 0;
1196
1197                 } else {
1198                         
1199                         /* move read position backwards because we are going
1200                            to reverse the data.
1201                         */
1202                         
1203                         file_frame -= total_space;
1204                         zero_fill = 0;
1205                 }
1206
1207         } else {
1208
1209                 if (file_frame == max_frames) {
1210
1211                         /* at end: nothing to do but fill with silence */
1212                         
1213                         for (chan_n = 0, i = channels.begin(); i != channels.end(); ++i, ++chan_n) {
1214                                         
1215                                 ChannelInfo& chan (*i);
1216                                 chan.playback_buf->get_write_vector (&vector);
1217                                 memset (vector.buf[0], 0, sizeof(Sample) * vector.len[0]);
1218                                 if (vector.len[1]) {
1219                                         memset (vector.buf[1], 0, sizeof(Sample) * vector.len[1]);
1220                                 }
1221                                 chan.playback_buf->increment_write_ptr (vector.len[0] + vector.len[1]);
1222                         }
1223                         return 0;
1224                 }
1225                 
1226                 if (file_frame > max_frames - total_space) {
1227
1228                         /* to close to the end: read what we can, and zero fill the rest */
1229
1230                         zero_fill = total_space - (max_frames - file_frame);
1231                         total_space = max_frames - file_frame;
1232
1233                 } else {
1234                         zero_fill = 0;
1235                 }
1236         }
1237         
1238         nframes_t file_frame_tmp = 0;
1239
1240         for (chan_n = 0, i = channels.begin(); i != channels.end(); ++i, ++chan_n) {
1241
1242                 ChannelInfo& chan (*i);
1243                 Sample* buf1;
1244                 Sample* buf2;
1245                 nframes_t len1, len2;
1246
1247                 chan.playback_buf->get_write_vector (&vector);
1248
1249                 ts = total_space;
1250                 file_frame_tmp = file_frame;
1251
1252                 if (reversed) {
1253                         buf1 = vector.buf[1];
1254                         len1 = vector.len[1];
1255                         buf2 = vector.buf[0];
1256                         len2 = vector.len[0];
1257                 } else {
1258                         buf1 = vector.buf[0];
1259                         len1 = vector.len[0];
1260                         buf2 = vector.buf[1];
1261                         len2 = vector.len[1];
1262                 }
1263
1264
1265                 to_read = min (ts, len1);
1266                 to_read = min (to_read, disk_io_chunk_frames);
1267
1268                 if (to_read) {
1269
1270                         if (read (buf1, mixdown_buffer, gain_buffer, file_frame_tmp, to_read, chan, chan_n, reversed)) {
1271                                 ret = -1;
1272                                 goto out;
1273                         }
1274                         
1275                         chan.playback_buf->increment_write_ptr (to_read);
1276                         ts -= to_read;
1277                 }
1278
1279                 to_read = min (ts, len2);
1280
1281                 if (to_read) {
1282
1283                         
1284                         /* we read all of vector.len[0], but it wasn't an entire disk_io_chunk_frames of data,
1285                            so read some or all of vector.len[1] as well.
1286                         */
1287
1288                         if (read (buf2, mixdown_buffer, gain_buffer, file_frame_tmp, to_read, chan, chan_n, reversed)) {
1289                                 ret = -1;
1290                                 goto out;
1291                         }
1292                 
1293                         chan.playback_buf->increment_write_ptr (to_read);
1294                 }
1295
1296                 if (zero_fill) {
1297                         /* do something */
1298                 }
1299
1300         }
1301         
1302         file_frame = file_frame_tmp;
1303
1304   out:
1305
1306         return ret;
1307 }       
1308
1309 /** Flush pending data to disk.
1310  *
1311  * Important note: this function will write *AT MOST* disk_io_chunk_frames
1312  * of data to disk. it will never write more than that.  If it writes that
1313  * much and there is more than that waiting to be written, it will return 1,
1314  * otherwise 0 on success or -1 on failure.
1315  * 
1316  * If there is less than disk_io_chunk_frames to be written, no data will be
1317  * written at all unless @a force_flush is true.
1318  */
1319 int
1320 AudioDiskstream::do_flush (Session::RunContext context, bool force_flush)
1321 {
1322         uint32_t to_write;
1323         int32_t ret = 0;
1324         RingBufferNPT<Sample>::rw_vector vector;
1325         RingBufferNPT<CaptureTransition>::rw_vector transvec;
1326         nframes_t total;
1327
1328         _write_data_count = 0;
1329
1330         for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1331         
1332                 (*chan).capture_buf->get_read_vector (&vector);
1333
1334                 total = vector.len[0] + vector.len[1];
1335
1336                 
1337                 if (total == 0 || (total < disk_io_chunk_frames && !force_flush && was_recording)) {
1338                         goto out;
1339                 }
1340
1341                 /* if there are 2+ chunks of disk i/o possible for
1342                    this track, let the caller know so that it can arrange
1343                    for us to be called again, ASAP.
1344                    
1345                    if we are forcing a flush, then if there is* any* extra
1346                    work, let the caller know.
1347
1348                    if we are no longer recording and there is any extra work,
1349                    let the caller know too.
1350                 */
1351
1352                 if (total >= 2 * disk_io_chunk_frames || ((force_flush || !was_recording) && total > disk_io_chunk_frames)) {
1353                         ret = 1;
1354                 } 
1355
1356                 to_write = min (disk_io_chunk_frames, (nframes_t) vector.len[0]);
1357                 
1358                 // check the transition buffer when recording destructive
1359                 // important that we get this after the capture buf
1360
1361                 if (destructive()) {
1362                         (*chan).capture_transition_buf->get_read_vector(&transvec);
1363                         size_t transcount = transvec.len[0] + transvec.len[1];
1364                         bool have_start = false;
1365                         size_t ti;
1366
1367                         for (ti=0; ti < transcount; ++ti) {
1368                                 CaptureTransition & captrans = (ti < transvec.len[0]) ? transvec.buf[0][ti] : transvec.buf[1][ti-transvec.len[0]];
1369                                 
1370                                 if (captrans.type == CaptureStart) {
1371                                         // by definition, the first data we got above represents the given capture pos
1372
1373                                         (*chan).write_source->mark_capture_start (captrans.capture_val);
1374                                         (*chan).curr_capture_cnt = 0;
1375
1376                                         have_start = true;
1377                                 }
1378                                 else if (captrans.type == CaptureEnd) {
1379
1380                                         // capture end, the capture_val represents total frames in capture
1381
1382                                         if (captrans.capture_val <= (*chan).curr_capture_cnt + to_write) {
1383
1384                                                 // shorten to make the write a perfect fit
1385                                                 uint32_t nto_write = (captrans.capture_val - (*chan).curr_capture_cnt); 
1386
1387                                                 if (nto_write < to_write) {
1388                                                         ret = 1; // should we?
1389                                                 }
1390                                                 to_write = nto_write;
1391
1392                                                 (*chan).write_source->mark_capture_end ();
1393                                                 
1394                                                 // increment past this transition, but go no further
1395                                                 ++ti;
1396                                                 break;
1397                                         }
1398                                         else {
1399                                                 // actually ends just beyond this chunk, so force more work
1400                                                 ret = 1;
1401                                                 break;
1402                                         }
1403                                 }
1404                         }
1405
1406                         if (ti > 0) {
1407                                 (*chan).capture_transition_buf->increment_read_ptr(ti);
1408                         }
1409                 }
1410
1411                 if ((!(*chan).write_source) || (*chan).write_source->write (vector.buf[0], to_write) != to_write) {
1412                         error << string_compose(_("AudioDiskstream %1: cannot write to disk"), _id) << endmsg;
1413                         return -1;
1414                 }
1415
1416                 (*chan).capture_buf->increment_read_ptr (to_write);
1417                 (*chan).curr_capture_cnt += to_write;
1418                 
1419                 if ((to_write == vector.len[0]) && (total > to_write) && (to_write < disk_io_chunk_frames) && !destructive()) {
1420                 
1421                         /* we wrote all of vector.len[0] but it wasn't an entire
1422                            disk_io_chunk_frames of data, so arrange for some part 
1423                            of vector.len[1] to be flushed to disk as well.
1424                         */
1425                 
1426                         to_write = min ((nframes_t)(disk_io_chunk_frames - to_write), (nframes_t) vector.len[1]);
1427                 
1428                         if ((*chan).write_source->write (vector.buf[1], to_write) != to_write) {
1429                                 error << string_compose(_("AudioDiskstream %1: cannot write to disk"), _id) << endmsg;
1430                                 return -1;
1431                         }
1432
1433                         _write_data_count += (*chan).write_source->write_data_count();
1434         
1435                         (*chan).capture_buf->increment_read_ptr (to_write);
1436                         (*chan).curr_capture_cnt += to_write;
1437                 }
1438         }
1439
1440   out:
1441         return ret;
1442 }
1443
1444 void
1445 AudioDiskstream::transport_stopped (struct tm& when, time_t twhen, bool abort_capture)
1446 {
1447         uint32_t buffer_position;
1448         bool more_work = true;
1449         int err = 0;
1450         boost::shared_ptr<AudioRegion> region;
1451         nframes_t total_capture;
1452         SourceList srcs;
1453         SourceList::iterator src;
1454         ChannelList::iterator chan;
1455         vector<CaptureInfo*>::iterator ci;
1456         uint32_t n = 0; 
1457         bool mark_write_completed = false;
1458
1459         finish_capture (true);
1460
1461         /* butler is already stopped, but there may be work to do 
1462            to flush remaining data to disk.
1463         */
1464
1465         while (more_work && !err) {
1466                 switch (do_flush (Session::TransportContext, true)) {
1467                 case 0:
1468                         more_work = false;
1469                         break;
1470                 case 1:
1471                         break;
1472                 case -1:
1473                         error << string_compose(_("AudioDiskstream \"%1\": cannot flush captured data to disk!"), _name) << endmsg;
1474                         err++;
1475                 }
1476         }
1477
1478         /* XXX is there anything we can do if err != 0 ? */
1479         Glib::Mutex::Lock lm (capture_info_lock);
1480         
1481         if (capture_info.empty()) {
1482                 return;
1483         }
1484
1485         if (abort_capture) {
1486                 
1487                 ChannelList::iterator chan;
1488                 
1489                 list<boost::shared_ptr<Source> >* deletion_list = new list<boost::shared_ptr<Source> >;
1490
1491                 for ( chan = channels.begin(); chan != channels.end(); ++chan) {
1492
1493                         if ((*chan).write_source) {
1494                                 
1495                                 (*chan).write_source->mark_for_remove ();
1496                                 
1497                                 deletion_list->push_back ((*chan).write_source);
1498
1499                                 (*chan).write_source.reset ();
1500                         }
1501                         
1502                         /* new source set up in "out" below */
1503                 }
1504                 
1505                 if (!deletion_list->empty()) {
1506                         DeleteSources (deletion_list);
1507                 } else {
1508                         delete deletion_list;
1509                 }
1510
1511                 goto out;
1512         } 
1513
1514         for (total_capture = 0, ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1515                 total_capture += (*ci)->frames;
1516         }
1517
1518         /* figure out the name for this take */
1519
1520         for (n = 0, chan = channels.begin(); chan != channels.end(); ++chan, ++n) {
1521
1522                 boost::shared_ptr<AudioFileSource> s = (*chan).write_source;
1523                 
1524                 if (s) {
1525                         srcs.push_back (s);
1526                         s->update_header (capture_info.front()->start, when, twhen);
1527                         s->set_captured_for (_name);
1528                         
1529                 }
1530         }
1531
1532         /* destructive tracks have a single, never changing region */
1533
1534         if (destructive()) {
1535
1536                 /* send a signal that any UI can pick up to do the right thing. there is 
1537                    a small problem here in that a UI may need the peak data to be ready
1538                    for the data that was recorded and this isn't interlocked with that
1539                    process. this problem is deferred to the UI.
1540                  */
1541                 
1542                 _playlist->Modified();
1543
1544         } else {
1545
1546                 /* Register a new region with the Session that
1547                    describes the entire source. Do this first
1548                    so that any sub-regions will obviously be
1549                    children of this one (later!)
1550                 */
1551                 
1552                 try {
1553                         boost::shared_ptr<Region> rx (RegionFactory::create (srcs, channels[0].write_source->last_capture_start_frame(), total_capture, 
1554                                                                              region_name_from_path (channels[0].write_source->name()), 
1555                                                                              0, AudioRegion::Flag (AudioRegion::DefaultFlags|AudioRegion::Automatic|AudioRegion::WholeFile)));
1556
1557                         region = boost::dynamic_pointer_cast<AudioRegion> (rx);
1558                         region->special_set_position (capture_info.front()->start);
1559                 }
1560                 
1561                 
1562                 catch (failed_constructor& err) {
1563                         error << string_compose(_("%1: could not create region for complete audio file"), _name) << endmsg;
1564                         /* XXX what now? */
1565                 }
1566                 
1567                 _last_capture_regions.push_back (region);
1568
1569                 // cerr << _name << ": there are " << capture_info.size() << " capture_info records\n";
1570                 
1571                 XMLNode &before = _playlist->get_state();
1572                 _playlist->freeze ();
1573                 
1574                 for (buffer_position = channels[0].write_source->last_capture_start_frame(), ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1575                         
1576                         string region_name;
1577                         _session.region_name (region_name, channels[0].write_source->name(), false);
1578                         
1579                         // cerr << _name << ": based on ci of " << (*ci)->start << " for " << (*ci)->frames << " add region " << region_name << endl;
1580                         
1581                         try {
1582                                 boost::shared_ptr<Region> rx (RegionFactory::create (srcs, buffer_position, (*ci)->frames, region_name));
1583                                 region = boost::dynamic_pointer_cast<AudioRegion> (rx);
1584                         }
1585                         
1586                         catch (failed_constructor& err) {
1587                                 error << _("AudioDiskstream: could not create region for captured audio!") << endmsg;
1588                                 continue; /* XXX is this OK? */
1589                         }
1590                         
1591                         _last_capture_regions.push_back (region);
1592                         
1593                         // cerr << "add new region, buffer position = " << buffer_position << " @ " << (*ci)->start << endl;
1594                         
1595                         i_am_the_modifier++;
1596                         _playlist->add_region (region, (*ci)->start);
1597                         i_am_the_modifier--;
1598                         
1599                         buffer_position += (*ci)->frames;
1600                 }
1601
1602                 _playlist->thaw ();
1603                 XMLNode &after = _playlist->get_state();
1604                 _session.add_command (new MementoCommand<Playlist>(*_playlist, &before, &after));
1605         }
1606
1607         mark_write_completed = true;
1608
1609         reset_write_sources (mark_write_completed);
1610
1611   out:
1612         for (ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1613                 delete *ci;
1614         }
1615
1616         capture_info.clear ();
1617         capture_start_frame = 0;
1618 }
1619
1620 void
1621 AudioDiskstream::finish_capture (bool rec_monitors_input)
1622 {
1623         was_recording = false;
1624         
1625         if (capture_captured == 0) {
1626                 return;
1627         }
1628
1629         if (recordable() && destructive()) {
1630                 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1631                         
1632                         RingBufferNPT<CaptureTransition>::rw_vector transvec;
1633                         (*chan).capture_transition_buf->get_write_vector(&transvec);
1634                         
1635                         
1636                         if (transvec.len[0] > 0) {
1637                                 transvec.buf[0]->type = CaptureEnd;
1638                                 transvec.buf[0]->capture_val = capture_captured;
1639                                 (*chan).capture_transition_buf->increment_write_ptr(1);
1640                         }
1641                         else {
1642                                 // bad!
1643                                 fatal << string_compose (_("programmer error: %1"), X_("capture_transition_buf is full when stopping record!  inconceivable!")) << endmsg;
1644                         }
1645                 }
1646         }
1647         
1648         
1649         CaptureInfo* ci = new CaptureInfo;
1650         
1651         ci->start =  capture_start_frame;
1652         ci->frames = capture_captured;
1653         
1654         /* XXX theoretical race condition here. Need atomic exchange ? 
1655            However, the circumstances when this is called right 
1656            now (either on record-disable or transport_stopped)
1657            mean that no actual race exists. I think ...
1658            We now have a capture_info_lock, but it is only to be used
1659            to synchronize in the transport_stop and the capture info
1660            accessors, so that invalidation will not occur (both non-realtime).
1661         */
1662
1663         // cerr << "Finish capture, add new CI, " << ci->start << '+' << ci->frames << endl;
1664
1665         capture_info.push_back (ci);
1666         capture_captured = 0;
1667 }
1668
1669 void
1670 AudioDiskstream::set_record_enabled (bool yn)
1671 {
1672         if (!recordable() || !_session.record_enabling_legal()) {
1673                 return;
1674         }
1675
1676         /* can't rec-enable in destructive mode if transport is before start */
1677
1678         if (destructive() && yn && _session.transport_frame() < _session.current_start_frame()) {
1679                 return;
1680         }
1681
1682         if (yn && channels[0].source == 0) {
1683
1684                 /* pick up connections not initiated *from* the IO object
1685                    we're associated with.
1686                 */
1687
1688                 get_input_sources ();
1689         }
1690
1691         /* yes, i know that this not proof against race conditions, but its
1692            good enough. i think.
1693         */
1694
1695         if (record_enabled() != yn) {
1696                 if (yn) {
1697                         engage_record_enable ();
1698                 } else {
1699                         disengage_record_enable ();
1700                 }
1701         }
1702 }
1703
1704 void
1705 AudioDiskstream::engage_record_enable ()
1706 {
1707     bool rolling = _session.transport_speed() != 0.0f;
1708
1709         g_atomic_int_set (&_record_enabled, 1);
1710         capturing_sources.clear ();
1711         if (Config->get_use_hardware_monitoring())  {
1712                 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1713                         if ((*chan).source) {
1714                                 (*chan).source->ensure_monitor_input (!(Config->get_auto_input() && rolling));
1715                         }
1716                         capturing_sources.push_back ((*chan).write_source);
1717                 }
1718         } else {
1719                 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1720                         capturing_sources.push_back ((*chan).write_source);
1721                 }
1722         }
1723
1724         RecordEnableChanged (); /* EMIT SIGNAL */
1725 }
1726
1727 void
1728 AudioDiskstream::disengage_record_enable ()
1729 {
1730         g_atomic_int_set (&_record_enabled, 0);
1731         if (Config->get_use_hardware_monitoring()) {
1732                 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1733                         if ((*chan).source) {
1734                                 (*chan).source->ensure_monitor_input (false);
1735                         }
1736                 }
1737         }
1738         capturing_sources.clear ();
1739         RecordEnableChanged (); /* EMIT SIGNAL */
1740 }
1741                 
1742
1743 XMLNode&
1744 AudioDiskstream::get_state ()
1745 {
1746         XMLNode* node = new XMLNode ("AudioDiskstream");
1747         char buf[64] = "";
1748         LocaleGuard lg (X_("POSIX"));
1749
1750         snprintf (buf, sizeof(buf), "0x%x", _flags);
1751         node->add_property ("flags", buf);
1752
1753         snprintf (buf, sizeof(buf), "%zd", channels.size());
1754         node->add_property ("channels", buf);
1755
1756         node->add_property ("playlist", _playlist->name());
1757         
1758         snprintf (buf, sizeof(buf), "%f", _visible_speed);
1759         node->add_property ("speed", buf);
1760
1761         node->add_property("name", _name);
1762         id().print (buf);
1763         node->add_property("id", buf);
1764
1765         if (!capturing_sources.empty() && _session.get_record_enabled()) {
1766
1767                 XMLNode* cs_child = new XMLNode (X_("CapturingSources"));
1768                 XMLNode* cs_grandchild;
1769
1770                 for (vector<boost::shared_ptr<AudioFileSource> >::iterator i = capturing_sources.begin(); i != capturing_sources.end(); ++i) {
1771                         cs_grandchild = new XMLNode (X_("file"));
1772                         cs_grandchild->add_property (X_("path"), (*i)->path());
1773                         cs_child->add_child_nocopy (*cs_grandchild);
1774                 }
1775
1776                 /* store the location where capture will start */
1777
1778                 Location* pi;
1779
1780                 if (Config->get_punch_in() && ((pi = _session.locations()->auto_punch_location()) != 0)) {
1781                         snprintf (buf, sizeof (buf), "%" PRIu32, pi->start());
1782                 } else {
1783                         snprintf (buf, sizeof (buf), "%" PRIu32, _session.transport_frame());
1784                 }
1785
1786                 cs_child->add_property (X_("at"), buf);
1787                 node->add_child_nocopy (*cs_child);
1788         }
1789
1790         if (_extra_xml) {
1791                 node->add_child_copy (*_extra_xml);
1792         }
1793
1794         return* node;
1795 }
1796
1797 int
1798 AudioDiskstream::set_state (const XMLNode& node)
1799 {
1800         const XMLProperty* prop;
1801         XMLNodeList nlist = node.children();
1802         XMLNodeIterator niter;
1803         uint32_t nchans = 1;
1804         XMLNode* capture_pending_node = 0;
1805         LocaleGuard lg (X_("POSIX"));
1806
1807         in_set_state = true;
1808
1809         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1810                 if ((*niter)->name() == IO::state_node_name) {
1811                         deprecated_io_node = new XMLNode (**niter);
1812                 }
1813
1814                 if ((*niter)->name() == X_("CapturingSources")) {
1815                         capture_pending_node = *niter;
1816                 }
1817         }
1818
1819         /* prevent write sources from being created */
1820         
1821         in_set_state = true;
1822         
1823         if ((prop = node.property ("name")) != 0) {
1824                 _name = prop->value();
1825         } 
1826
1827         if (deprecated_io_node) {
1828                 if ((prop = deprecated_io_node->property ("id")) != 0) {
1829                         _id = prop->value ();
1830                 }
1831         } else {
1832                 if ((prop = node.property ("id")) != 0) {
1833                         _id = prop->value ();
1834                 }
1835         }
1836
1837         if ((prop = node.property ("flags")) != 0) {
1838                 _flags = strtol (prop->value().c_str(), 0, 0);
1839         }
1840
1841         if ((prop = node.property ("channels")) != 0) {
1842                 nchans = atoi (prop->value().c_str());
1843         }
1844         
1845         // create necessary extra channels
1846         // we are always constructed with one and we always need one
1847
1848         if (nchans > _n_channels) {
1849
1850                 // we need to add new channel infos
1851                 //LockMonitor lm (state_lock, __LINE__, __FILE__);
1852
1853                 int diff = nchans - channels.size();
1854
1855                 for (int i=0; i < diff; ++i) {
1856                         add_channel ();
1857                 }
1858
1859         } else if (nchans < _n_channels) {
1860
1861                 // we need to get rid of channels
1862                 //LockMonitor lm (state_lock, __LINE__, __FILE__);
1863
1864                 int diff = channels.size() - nchans;
1865                 
1866                 for (int i = 0; i < diff; ++i) {
1867                         remove_channel ();
1868                 }
1869         }
1870
1871         if ((prop = node.property ("playlist")) == 0) {
1872                 return -1;
1873         }
1874
1875         {
1876                 bool had_playlist = (_playlist != 0);
1877         
1878                 if (find_and_use_playlist (prop->value())) {
1879                         return -1;
1880                 }
1881
1882                 if (!had_playlist) {
1883                         _playlist->set_orig_diskstream_id (_id);
1884                 }
1885                 
1886                 if (!destructive() && capture_pending_node) {
1887                         /* destructive streams have one and only one source per channel,
1888                            and so they never end up in pending capture in any useful
1889                            sense.
1890                         */
1891                         use_pending_capture_data (*capture_pending_node);
1892                 }
1893
1894         }
1895
1896         if ((prop = node.property ("speed")) != 0) {
1897                 double sp = atof (prop->value().c_str());
1898
1899                 if (realtime_set_speed (sp, false)) {
1900                         non_realtime_set_speed ();
1901                 }
1902         }
1903
1904         _n_channels = channels.size();
1905
1906         in_set_state = false;
1907
1908         /* make sure this is clear before we do anything else */
1909
1910         capturing_sources.clear ();
1911
1912         /* write sources are handled when we handle the input set 
1913            up of the IO that owns this DS (::non_realtime_input_change())
1914         */
1915                 
1916         in_set_state = false;
1917
1918         return 0;
1919 }
1920
1921 int
1922 AudioDiskstream::use_new_write_source (uint32_t n)
1923 {
1924         if (!recordable()) {
1925                 return 1;
1926         }
1927
1928         if (n >= channels.size()) {
1929                 error << string_compose (_("AudioDiskstream: channel %1 out of range"), n) << endmsg;
1930                 return -1;
1931         }
1932
1933         ChannelInfo &chan = channels[n];
1934         
1935         if (chan.write_source) {
1936
1937                 if (AudioFileSource::is_empty (_session, chan.write_source->path())) {
1938                         chan.write_source->mark_for_remove ();
1939                         chan.write_source.reset ();
1940                 } else {
1941                         chan.write_source.reset ();
1942                 }
1943         }
1944
1945         try {
1946                 if ((chan.write_source = _session.create_audio_source_for_session (*this, n, destructive())) == 0) {
1947                         throw failed_constructor();
1948                 }
1949         } 
1950
1951         catch (failed_constructor &err) {
1952                 error << string_compose (_("%1:%2 new capture file not initialized correctly"), _name, n) << endmsg;
1953                 chan.write_source.reset ();
1954                 return -1;
1955         }
1956
1957         /* do not remove destructive files even if they are empty */
1958
1959         chan.write_source->set_allow_remove_if_empty (!destructive());
1960
1961         return 0;
1962 }
1963
1964 void
1965 AudioDiskstream::reset_write_sources (bool mark_write_complete, bool force)
1966 {
1967         ChannelList::iterator chan;
1968         uint32_t n;
1969
1970         if (!recordable()) {
1971                 return;
1972         }
1973         
1974         capturing_sources.clear ();
1975         
1976         for (chan = channels.begin(), n = 0; chan != channels.end(); ++chan, ++n) {
1977                 if (!destructive()) {
1978
1979                         if ((*chan).write_source && mark_write_complete) {
1980                                 (*chan).write_source->mark_streaming_write_completed ();
1981                         }
1982                         use_new_write_source (n);
1983
1984                         if (record_enabled()) {
1985                                 capturing_sources.push_back ((*chan).write_source);
1986                         }
1987
1988                 } else {
1989                         if ((*chan).write_source == 0) {
1990                                 use_new_write_source (n);
1991                         }
1992                 }
1993         }
1994
1995         if (destructive()) {
1996
1997                 /* we now have all our write sources set up, so create the
1998                    playlist's single region.
1999                 */
2000
2001                 if (_playlist->empty()) {
2002                         setup_destructive_playlist ();
2003                 }
2004         }
2005 }
2006
2007 int
2008 AudioDiskstream::rename_write_sources ()
2009 {
2010         ChannelList::iterator chan;
2011         uint32_t n;
2012
2013         for (chan = channels.begin(), n = 0; chan != channels.end(); ++chan, ++n) {
2014                 if ((*chan).write_source != 0) {
2015                         (*chan).write_source->set_name (_name, destructive());
2016                         /* XXX what to do if one of them fails ? */
2017                 }
2018         }
2019
2020         return 0;
2021 }
2022
2023 void
2024 AudioDiskstream::set_block_size (nframes_t nframes)
2025 {
2026         if (_session.get_block_size() > speed_buffer_size) {
2027                 speed_buffer_size = _session.get_block_size();
2028
2029                 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
2030                         if ((*chan).speed_buffer) delete [] (*chan).speed_buffer;
2031                         (*chan).speed_buffer = new Sample[speed_buffer_size];
2032                 }
2033         }
2034         allocate_temporary_buffers ();
2035 }
2036
2037 void
2038 AudioDiskstream::allocate_temporary_buffers ()
2039 {
2040         /* make sure the wrap buffer is at least large enough to deal
2041            with the speeds up to 1.2, to allow for micro-variation
2042            when slaving to MTC, SMPTE etc.
2043         */
2044
2045         double sp = max (fabsf (_actual_speed), 1.2f);
2046         nframes_t required_wrap_size = (nframes_t) floor (_session.get_block_size() * sp) + 1;
2047
2048         if (required_wrap_size > wrap_buffer_size) {
2049
2050                 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
2051                         if ((*chan).playback_wrap_buffer) delete [] (*chan).playback_wrap_buffer;
2052                         (*chan).playback_wrap_buffer = new Sample[required_wrap_size];  
2053                         if ((*chan).capture_wrap_buffer) delete [] (*chan).capture_wrap_buffer;
2054                         (*chan).capture_wrap_buffer = new Sample[required_wrap_size];   
2055                 }
2056
2057                 wrap_buffer_size = required_wrap_size;
2058         }
2059 }
2060
2061 void
2062 AudioDiskstream::monitor_input (bool yn)
2063 {
2064         for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
2065                 
2066                 if ((*chan).source) {
2067                         (*chan).source->ensure_monitor_input (yn);
2068                 }
2069         }
2070 }
2071
2072 void
2073 AudioDiskstream::set_align_style_from_io ()
2074 {
2075         bool have_physical = false;
2076
2077         if (_io == 0) {
2078                 return;
2079         }
2080
2081         get_input_sources ();
2082         
2083         for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
2084                 if ((*chan).source && (*chan).source->flags() & JackPortIsPhysical) {
2085                         have_physical = true;
2086                         break;
2087                 }
2088         }
2089
2090         if (have_physical) {
2091                 set_align_style (ExistingMaterial);
2092         } else {
2093                 set_align_style (CaptureTime);
2094         }
2095 }
2096
2097 int
2098 AudioDiskstream::add_channel ()
2099 {
2100         /* XXX need to take lock??? */
2101
2102         ChannelInfo chan;
2103
2104         init_channel (chan);
2105
2106         chan.speed_buffer = new Sample[speed_buffer_size];
2107         chan.playback_wrap_buffer = new Sample[wrap_buffer_size];
2108         chan.capture_wrap_buffer = new Sample[wrap_buffer_size];
2109
2110         channels.push_back (chan);
2111
2112         _n_channels = channels.size();
2113
2114         return 0;
2115 }
2116
2117 int
2118 AudioDiskstream::remove_channel ()
2119 {
2120         if (channels.size() > 1) {
2121                 /* XXX need to take lock??? */
2122                 ChannelInfo & chan = channels.back();
2123                 destroy_channel (chan);
2124                 channels.pop_back();
2125
2126                 _n_channels = channels.size();
2127                 return 0;
2128         }
2129
2130         return -1;
2131 }
2132
2133 float
2134 AudioDiskstream::playback_buffer_load () const
2135 {
2136         return (float) ((double) channels.front().playback_buf->read_space()/
2137                         (double) channels.front().playback_buf->bufsize());
2138 }
2139
2140 float
2141 AudioDiskstream::capture_buffer_load () const
2142 {
2143         return (float) ((double) channels.front().capture_buf->write_space()/
2144                         (double) channels.front().capture_buf->bufsize());
2145 }
2146
2147 int
2148 AudioDiskstream::use_pending_capture_data (XMLNode& node)
2149 {
2150         const XMLProperty* prop;
2151         XMLNodeList nlist = node.children();
2152         XMLNodeIterator niter;
2153         boost::shared_ptr<AudioFileSource> fs;
2154         boost::shared_ptr<AudioFileSource> first_fs;
2155         SourceList pending_sources;
2156         nframes_t position;
2157
2158         if ((prop = node.property (X_("at"))) == 0) {
2159                 return -1;
2160         }
2161
2162         if (sscanf (prop->value().c_str(), "%" PRIu32, &position) != 1) {
2163                 return -1;
2164         }
2165
2166         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2167                 if ((*niter)->name() == X_("file")) {
2168
2169                         if ((prop = (*niter)->property (X_("path"))) == 0) {
2170                                 continue;
2171                         }
2172
2173                         try {
2174                                 fs = boost::dynamic_pointer_cast<AudioFileSource> (SourceFactory::createWritable (_session, prop->value(), false, _session.frame_rate()));
2175                         }
2176
2177                         catch (failed_constructor& err) {
2178                                 error << string_compose (_("%1: cannot restore pending capture source file %2"),
2179                                                   _name, prop->value())
2180                                       << endmsg;
2181                                 return -1;
2182                         }
2183
2184                         pending_sources.push_back (fs);
2185                         
2186                         if (first_fs == 0) {
2187                                 first_fs = fs;
2188                         }
2189
2190                         fs->set_captured_for (_name);
2191                 }
2192         }
2193
2194         if (pending_sources.size() == 0) {
2195                 /* nothing can be done */
2196                 return 1;
2197         }
2198
2199         if (pending_sources.size() != _n_channels) {
2200                 error << string_compose (_("%1: incorrect number of pending sources listed - ignoring them all"), _name)
2201                       << endmsg;
2202                 return -1;
2203         }
2204
2205         boost::shared_ptr<AudioRegion> region;
2206         
2207         try {
2208                 region = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (pending_sources, 0, first_fs->length(),
2209                                                                                           region_name_from_path (first_fs->name()), 
2210                                                                                           0, AudioRegion::Flag (AudioRegion::DefaultFlags|AudioRegion::Automatic|AudioRegion::WholeFile)));
2211                 region->special_set_position (0);
2212         }
2213
2214         catch (failed_constructor& err) {
2215                 error << string_compose (_("%1: cannot create whole-file region from pending capture sources"),
2216                                   _name)
2217                       << endmsg;
2218                 
2219                 return -1;
2220         }
2221
2222         try {
2223                 region = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (pending_sources, 0, first_fs->length(), region_name_from_path (first_fs->name())));
2224         }
2225
2226         catch (failed_constructor& err) {
2227                 error << string_compose (_("%1: cannot create region from pending capture sources"),
2228                                   _name)
2229                       << endmsg;
2230                 
2231                 return -1;
2232         }
2233
2234         _playlist->add_region (region, position);
2235
2236         return 0;
2237 }