3 notable patches from lincoln (a) non-layered track mode (NOTE: this is broken for...
[ardour.git] / libs / ardour / audio_track.cc
1 /*
2     Copyright (C) 2002 Paul Davis 
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <sigc++/retype.h>
21 #include <sigc++/retype_return.h>
22 #include <sigc++/bind.h>
23
24 #include "pbd/error.h"
25 #include "pbd/enumwriter.h"
26
27 #include "evoral/Curve.hpp"
28
29 #include "ardour/audio_track.h"
30 #include "ardour/audio_diskstream.h"
31 #include "ardour/session.h"
32 #include "ardour/io_processor.h"
33 #include "ardour/audioregion.h"
34 #include "ardour/audiosource.h"
35 #include "ardour/region_factory.h"
36 #include "ardour/route_group_specialized.h"
37 #include "ardour/processor.h"
38 #include "ardour/plugin_insert.h"
39 #include "ardour/audioplaylist.h"
40 #include "ardour/playlist_factory.h"
41 #include "ardour/panner.h"
42 #include "ardour/utils.h"
43 #include "ardour/buffer_set.h"
44 #include "ardour/audio_buffer.h"
45 #include "i18n.h"
46
47 using namespace std;
48 using namespace ARDOUR;
49 using namespace PBD;
50
51 AudioTrack::AudioTrack (Session& sess, string name, Route::Flag flag, TrackMode mode)
52         : Track (sess, name, flag, mode)
53 {
54         use_new_diskstream ();
55 }
56
57 AudioTrack::AudioTrack (Session& sess, const XMLNode& node)
58         : Track (sess, node)
59 {
60         _set_state (node, false);
61 }
62
63 AudioTrack::~AudioTrack ()
64 {
65 }
66
67 void
68 AudioTrack::use_new_diskstream ()
69 {
70         AudioDiskstream::Flag dflags = AudioDiskstream::Flag (0);
71
72         if (_flags & Hidden) {
73                 dflags = AudioDiskstream::Flag (dflags | AudioDiskstream::Hidden);
74         } else {
75                 dflags = AudioDiskstream::Flag (dflags | AudioDiskstream::Recordable);
76         }
77
78         if (_mode == Destructive) {
79                 dflags = AudioDiskstream::Flag (dflags | AudioDiskstream::Destructive);
80         } else if (_mode == NonLayered){
81                 dflags = AudioDiskstream::Flag(dflags | AudioDiskstream::NonLayered);
82         }
83     
84
85         boost::shared_ptr<AudioDiskstream> ds (new AudioDiskstream (_session, name(), dflags));
86         
87         _session.add_diskstream (ds);
88
89         set_diskstream (boost::dynamic_pointer_cast<AudioDiskstream> (ds), this);
90 }
91
92 int
93 AudioTrack::set_mode (TrackMode m)
94 {
95         if (m != _mode) {
96
97                 if (_diskstream->set_destructive (m == Destructive)) {
98                         return -1;
99                 }
100                 
101                 _diskstream->set_non_layered (m == NonLayered);
102                 _mode = m;
103                 
104                 TrackModeChanged (); /* EMIT SIGNAL */
105         }
106
107         return 0;
108 }
109
110 bool
111 AudioTrack::can_use_mode (TrackMode m, bool& bounce_required)
112 {
113         switch (m) {
114         case NonLayered:
115         case Normal:
116                 bounce_required = false;
117                 return true;
118                 
119         case Destructive:
120         default:
121                 return _diskstream->can_become_destructive (bounce_required);
122         }
123 }
124
125 int
126 AudioTrack::deprecated_use_diskstream_connections ()
127 {
128         boost::shared_ptr<AudioDiskstream> diskstream = audio_diskstream();
129
130         if (diskstream->deprecated_io_node == 0) {
131                 return 0;
132         }
133
134         const XMLProperty* prop;
135         XMLNode& node (*diskstream->deprecated_io_node);
136
137         /* don't do this more than once. */
138
139         diskstream->deprecated_io_node = 0;
140
141         set_input_minimum (ChanCount::ZERO);
142         set_input_maximum (ChanCount::INFINITE);
143         set_output_minimum (ChanCount::ZERO);
144         set_output_maximum (ChanCount::INFINITE);
145         
146         if ((prop = node.property ("gain")) != 0) {
147                 set_gain (atof (prop->value().c_str()), this);
148                 _gain = _desired_gain;
149         }
150
151         if ((prop = node.property ("input-connection")) != 0) {
152                 boost::shared_ptr<Bundle> c = _session.bundle_by_name (prop->value());
153                 
154                 if (c == 0) {
155                         error << string_compose(_("Unknown bundle \"%1\" listed for input of %2"), prop->value(), _name) << endmsg;
156                         
157                         if ((c = _session.bundle_by_name (_("in 1"))) == 0) {
158                                 error << _("No input bundles available as a replacement")
159                                 << endmsg;
160                                 return -1;
161                         } else {
162                                 info << string_compose (_("Bundle %1 was not available - \"in 1\" used instead"), prop->value())
163                                << endmsg;
164                         }
165                 }
166
167                 connect_input_ports_to_bundle (c, this);
168
169         } else if ((prop = node.property ("inputs")) != 0) {
170                 if (set_inputs (prop->value())) {
171                         error << string_compose(_("improper input channel list in XML node (%1)"), prop->value()) << endmsg;
172                         return -1;
173                 }
174         }
175         
176         return 0;
177 }
178
179 int
180 AudioTrack::set_diskstream (boost::shared_ptr<AudioDiskstream> ds, void *src)
181 {
182         _diskstream = ds;
183         _diskstream->set_io (*this);
184         _diskstream->set_destructive (_mode == Destructive);
185         _diskstream->set_non_layered (_mode == NonLayered);
186
187         if (audio_diskstream()->deprecated_io_node) {
188
189                 if (!connecting_legal) {
190                         ConnectingLegal.connect (mem_fun (*this, &AudioTrack::deprecated_use_diskstream_connections));
191                 } else {
192                         deprecated_use_diskstream_connections ();
193                 }
194         }
195
196         _diskstream->set_record_enabled (false);
197         _diskstream->monitor_input (false);
198
199         ic_connection.disconnect();
200         ic_connection = input_changed.connect (mem_fun (*_diskstream, &Diskstream::handle_input_change));
201
202         DiskstreamChanged (); /* EMIT SIGNAL */
203
204         return 0;
205 }       
206
207 int 
208 AudioTrack::use_diskstream (string name)
209 {
210         boost::shared_ptr<AudioDiskstream> dstream;
211
212         if ((dstream = boost::dynamic_pointer_cast<AudioDiskstream>(_session.diskstream_by_name (name))) == 0) {
213                 error << string_compose(_("AudioTrack: audio diskstream \"%1\" not known by session"), name) << endmsg;
214                 return -1;
215         }
216         
217         return set_diskstream (dstream, this);
218 }
219
220 int 
221 AudioTrack::use_diskstream (const PBD::ID& id)
222 {
223         boost::shared_ptr<AudioDiskstream> dstream;
224
225         if ((dstream = boost::dynamic_pointer_cast<AudioDiskstream> (_session.diskstream_by_id (id))) == 0) {
226                 error << string_compose(_("AudioTrack: audio diskstream \"%1\" not known by session"), id) << endmsg;
227                 return -1;
228         }
229         
230         return set_diskstream (dstream, this);
231 }
232
233 boost::shared_ptr<AudioDiskstream>
234 AudioTrack::audio_diskstream() const
235 {
236         return boost::dynamic_pointer_cast<AudioDiskstream>(_diskstream);
237 }
238
239 int
240 AudioTrack::set_state (const XMLNode& node)
241 {
242         return _set_state (node, true);
243 }
244
245 int
246 AudioTrack::_set_state (const XMLNode& node, bool call_base)
247 {
248         const XMLProperty *prop;
249         XMLNodeConstIterator iter;
250
251         if (call_base) {
252                 if (Route::_set_state (node, call_base)) {
253                         return -1;
254                 }
255         }
256
257         if ((prop = node.property (X_("mode"))) != 0) {
258                 _mode = TrackMode (string_2_enum (prop->value(), _mode));
259         } else {
260                 _mode = Normal;
261         }
262
263         if ((prop = node.property ("diskstream-id")) == 0) {
264                 
265                 /* some old sessions use the diskstream name rather than the ID */
266
267                 if ((prop = node.property ("diskstream")) == 0) {
268                         fatal << _("programming error: AudioTrack given state without diskstream!") << endmsg;
269                         /*NOTREACHED*/
270                         return -1;
271                 }
272
273                 if (use_diskstream (prop->value())) {
274                         return -1;
275                 }
276
277         } else {
278                 
279                 PBD::ID id (prop->value());
280                 PBD::ID zero ("0");
281                 
282                 /* this wierd hack is used when creating tracks from a template. there isn't
283                    a particularly good time to interpose between setting the first part of
284                    the track state (notably Route::set_state() and the track mode), and the
285                    second part (diskstream stuff). So, we have a special ID for the diskstream
286                    that means "you should create a new diskstream here, not look for
287                    an old one.
288                 */
289                 
290                 if (id == zero) {
291                         use_new_diskstream ();
292                 } else if (use_diskstream (id)) {
293                         return -1;
294                 }
295         }
296
297
298         XMLNodeList nlist;
299         XMLNodeConstIterator niter;
300         XMLNode *child;
301
302         nlist = node.children();
303         for (niter = nlist.begin(); niter != nlist.end(); ++niter){
304                 child = *niter;
305
306                 if (child->name() == X_("recenable")) {
307                         _rec_enable_control->set_state (*child);
308                         _session.add_controllable (_rec_enable_control);
309                 }
310         }
311
312         pending_state = const_cast<XMLNode*> (&node);
313
314         if (_session.state_of_the_state() & Session::Loading) {
315                 _session.StateReady.connect (mem_fun (*this, &AudioTrack::set_state_part_two));
316         } else {
317                 set_state_part_two ();
318         }
319
320         return 0;
321 }
322
323 XMLNode& 
324 AudioTrack::state(bool full_state)
325 {
326         XMLNode& root (Route::state(full_state));
327         XMLNode* freeze_node;
328         char buf[64];
329
330         if (_freeze_record.playlist) {
331                 XMLNode* inode;
332
333                 freeze_node = new XMLNode (X_("freeze-info"));
334                 freeze_node->add_property ("playlist", _freeze_record.playlist->name());
335                 freeze_node->add_property ("state", enum_2_string (_freeze_record.state));
336
337                 for (vector<FreezeRecordProcessorInfo*>::iterator i = _freeze_record.processor_info.begin(); i != _freeze_record.processor_info.end(); ++i) {
338                         inode = new XMLNode (X_("processor"));
339                         (*i)->id.print (buf, sizeof (buf));
340                         inode->add_property (X_("id"), buf);
341                         inode->add_child_copy ((*i)->state);
342                 
343                         freeze_node->add_child_nocopy (*inode);
344                 }
345
346                 root.add_child_nocopy (*freeze_node);
347         }
348
349         /* Alignment: act as a proxy for the diskstream */
350         
351         XMLNode* align_node = new XMLNode (X_("Alignment"));
352         AlignStyle as = _diskstream->alignment_style ();
353         align_node->add_property (X_("style"), enum_2_string (as));
354         root.add_child_nocopy (*align_node);
355
356         root.add_property (X_("mode"), enum_2_string (_mode));
357
358         /* we don't return diskstream state because we don't
359            own the diskstream exclusively. control of the diskstream
360            state is ceded to the Session, even if we create the
361            diskstream.
362         */
363
364         _diskstream->id().print (buf, sizeof (buf));
365         root.add_property ("diskstream-id", buf);
366
367         root.add_child_nocopy (_rec_enable_control->get_state());
368
369         return root;
370 }
371
372 void
373 AudioTrack::set_state_part_two ()
374 {
375         XMLNode* fnode;
376         XMLProperty* prop;
377         LocaleGuard lg (X_("POSIX"));
378
379         /* This is called after all session state has been restored but before
380            have been made ports and connections are established.
381         */
382
383         if (pending_state == 0) {
384                 return;
385         }
386
387         if ((fnode = find_named_node (*pending_state, X_("freeze-info"))) != 0) {
388
389                 _freeze_record.state = Frozen;
390                 
391                 for (vector<FreezeRecordProcessorInfo*>::iterator i = _freeze_record.processor_info.begin(); i != _freeze_record.processor_info.end(); ++i) {
392                         delete *i;
393                 }
394                 _freeze_record.processor_info.clear ();
395                 
396                 if ((prop = fnode->property (X_("playlist"))) != 0) {
397                         boost::shared_ptr<Playlist> pl = _session.playlist_by_name (prop->value());
398                         if (pl) {
399                                 _freeze_record.playlist = boost::dynamic_pointer_cast<AudioPlaylist> (pl);
400                         } else {
401                                 _freeze_record.playlist.reset ();
402                                 _freeze_record.state = NoFreeze;
403                         return;
404                         }
405                 }
406                 
407                 if ((prop = fnode->property (X_("state"))) != 0) {
408                         _freeze_record.state = FreezeState (string_2_enum (prop->value(), _freeze_record.state));
409                 }
410                 
411                 XMLNodeConstIterator citer;
412                 XMLNodeList clist = fnode->children();
413                 
414                 for (citer = clist.begin(); citer != clist.end(); ++citer) {
415                         if ((*citer)->name() != X_("processor")) {
416                                 continue;
417                         }
418                         
419                         if ((prop = (*citer)->property (X_("id"))) == 0) {
420                                 continue;
421                         }
422                         
423                         FreezeRecordProcessorInfo* frii = new FreezeRecordProcessorInfo (*((*citer)->children().front()),
424                                                                                    boost::shared_ptr<Processor>());
425                         frii->id = prop->value ();
426                         _freeze_record.processor_info.push_back (frii);
427                 }
428         }
429
430         /* Alignment: act as a proxy for the diskstream */
431
432         if ((fnode = find_named_node (*pending_state, X_("Alignment"))) != 0) {
433
434                 if ((prop = fnode->property (X_("style"))) != 0) {
435
436                         /* fix for older sessions from before EnumWriter */
437
438                         string pstr;
439
440                         if (prop->value() == "capture") {
441                                 pstr = "CaptureTime";
442                         } else if (prop->value() == "existing") {
443                                 pstr = "ExistingMaterial";
444                         } else {
445                                 pstr = prop->value();
446                         }
447
448                         AlignStyle as = AlignStyle (string_2_enum (pstr, as));
449                         _diskstream->set_persistent_align_style (as);
450                 }
451         }
452         return;
453 }       
454
455 int 
456 AudioTrack::no_roll (nframes_t nframes, nframes_t start_frame, nframes_t end_frame, nframes_t offset, 
457                      bool session_state_changing, bool can_record, bool rec_monitors_input)
458 {
459         if (n_outputs().n_total() == 0) {
460                 return 0;
461         }
462
463         if (!_active) {
464                 silence (nframes, offset);
465                 return 0;
466         }
467
468         if (session_state_changing) {
469
470                 /* XXX is this safe to do against transport state changes? */
471
472                 passthru_silence (start_frame, end_frame, nframes, offset, 0, false);
473                 return 0;
474         }
475
476         audio_diskstream()->check_record_status (start_frame, nframes, can_record);
477
478         bool send_silence;
479         
480         if (_have_internal_generator) {
481                 /* since the instrument has no input streams,
482                    there is no reason to send any signal
483                    into the route.
484                 */
485                 send_silence = true;
486         } else {
487
488                 if (!Config->get_tape_machine_mode()) {
489                         /* 
490                            ADATs work in a strange way.. 
491                            they monitor input always when stopped.and auto-input is engaged. 
492                         */
493                         if ((Config->get_monitoring_model() == SoftwareMonitoring) && (Config->get_auto_input () || _diskstream->record_enabled())) {
494                                 send_silence = false;
495                         } else {
496                                 send_silence = true;
497                         }
498                 } else {
499                         /* 
500                            Other machines switch to input on stop if the track is record enabled,
501                            regardless of the auto input setting (auto input only changes the 
502                            monitoring state when the transport is rolling) 
503                         */
504                         if ((Config->get_monitoring_model() == SoftwareMonitoring) && _diskstream->record_enabled()) {
505                                 send_silence = false;
506                         } else {
507                                 send_silence = true;
508                         }
509                 }
510         }
511
512         apply_gain_automation = false;
513
514         if (send_silence) {
515                 
516                 /* if we're sending silence, but we want the meters to show levels for the signal,
517                    meter right here.
518                 */
519                 
520                 if (_have_internal_generator) {
521                         passthru_silence (start_frame, end_frame, nframes, offset, 0, true);
522                 } else {
523                         if (_meter_point == MeterInput) {
524                                 just_meter_input (start_frame, end_frame, nframes, offset);
525                         }
526                         passthru_silence (start_frame, end_frame, nframes, offset, 0, false);
527                 }
528
529         } else {
530         
531                 /* we're sending signal, but we may still want to meter the input. 
532                  */
533
534                 passthru (start_frame, end_frame, nframes, offset, 0, (_meter_point == MeterInput));
535         }
536
537         return 0;
538 }
539
540 int
541 AudioTrack::roll (nframes_t nframes, nframes_t start_frame, nframes_t end_frame, nframes_t offset, int declick,
542                   bool can_record, bool rec_monitors_input)
543 {
544         int dret;
545         Sample* b;
546         Sample* tmpb;
547         nframes_t transport_frame;
548         boost::shared_ptr<AudioDiskstream> diskstream = audio_diskstream();
549         
550         {
551                 Glib::RWLock::ReaderLock lm (_processor_lock, Glib::TRY_LOCK);
552                 if (lm.locked()) {
553                         // automation snapshot can also be called from the non-rt context
554                         // and it uses the redirect list, so we take the lock out here
555                         automation_snapshot (start_frame, false);
556                 }
557         }
558
559         
560         if (n_outputs().n_total() == 0 && _processors.empty()) {
561                 return 0;
562         }
563
564         if (!_active) {
565                 silence (nframes, offset);
566                 return 0;
567         }
568
569         transport_frame = _session.transport_frame();
570
571         prepare_inputs( nframes, offset );
572
573         if ((nframes = check_initial_delay (nframes, offset, transport_frame)) == 0) {
574                 /* need to do this so that the diskstream sets its
575                    playback distance to zero, thus causing diskstream::commit
576                    to do nothing.
577                 */
578                 return diskstream->process (transport_frame, 0, 0, can_record, rec_monitors_input);
579         } 
580
581         _silent = false;
582         apply_gain_automation = false;
583
584         if ((dret = diskstream->process (transport_frame, nframes, offset, can_record, rec_monitors_input)) != 0) {
585                 
586                 silence (nframes, offset);
587
588                 return dret;
589         }
590
591         /* special condition applies */
592         
593         if (_meter_point == MeterInput) {
594                 just_meter_input (start_frame, end_frame, nframes, offset);
595         }
596
597         if (diskstream->record_enabled() && !can_record && !Config->get_auto_input()) {
598
599                 /* not actually recording, but we want to hear the input material anyway,
600                    at least potentially (depending on monitoring options)
601                  */
602
603                 passthru (start_frame, end_frame, nframes, offset, 0, true);
604
605         } else if ((b = diskstream->playback_buffer(0)) != 0) {
606
607                 /*
608                   XXX is it true that the earlier test on n_outputs()
609                   means that we can avoid checking it again here? i think
610                   so, because changing the i/o configuration of an IO
611                   requires holding the AudioEngine lock, which we hold
612                   while in the process() tree.
613                 */
614
615                 
616                 /* copy the diskstream data to all output buffers */
617
618                 size_t limit = n_process_buffers().n_audio();
619                 BufferSet& bufs = _session.get_scratch_buffers ();
620                 const size_t blimit = bufs.count().n_audio();
621
622                 uint32_t n;
623                 uint32_t i;
624
625                 if (limit > blimit) {
626
627                         /* example case: auditioner configured for stereo output,
628                            but loaded with an 8 channel file. there are only
629                            2 passthrough buffers, but n_process_buffers() will
630                            return 8.
631                            
632                            arbitrary decision: map all channels in the diskstream
633                            to the outputs available.
634                         */
635
636                         float scaling = limit/blimit;
637                         
638                         for (i = 0, n = 1; i < blimit; ++i, ++n) {
639
640                                 /* first time through just copy a channel into 
641                                    the output buffer.
642                                 */
643                                 
644                                 Sample* bb = bufs.get_audio (i).data();
645
646                                 for (nframes_t xx = 0; xx < nframes; ++xx) {
647                                         bb[xx] = b[xx] * scaling;
648                                 }
649
650                                 if (n < diskstream->n_channels().n_audio()) {
651                                         tmpb = diskstream->playback_buffer(n);
652                                         if (tmpb!=0) {
653                                                 b = tmpb;
654                                         }
655                                 }
656                         }
657
658                         for (;i < limit; ++i, ++n) {
659                                 
660                                 /* for all remaining channels, sum with existing
661                                    data in the output buffers 
662                                 */
663                                 
664                                 bufs.get_audio (i%blimit).accumulate_with_gain_from (b, nframes, 0, scaling);
665                                 
666                                 if (n < diskstream->n_channels().n_audio()) {
667                                         tmpb = diskstream->playback_buffer(n);
668                                         if (tmpb!=0) {
669                                                 b = tmpb;
670                                         }
671                                 }
672                                 
673                         }
674
675                         limit = blimit;
676
677                 } else {
678                         for (i = 0, n = 1; i < blimit; ++i, ++n) {
679                                 memcpy (bufs.get_audio (i).data(), b, sizeof (Sample) * nframes); 
680                                 if (n < diskstream->n_channels().n_audio()) {
681                                         tmpb = diskstream->playback_buffer(n);
682                                         if (tmpb!=0) {
683                                                 b = tmpb;
684                                         }
685                                 }
686                         }
687                 }
688
689                 /* don't waste time with automation if we're recording or we've just stopped (yes it can happen) */
690
691                 if (!diskstream->record_enabled() && _session.transport_rolling()) {
692                         Glib::Mutex::Lock am (data().control_lock(), Glib::TRY_LOCK);
693                         
694                         if (am.locked() && gain_control()->automation_playback()) {
695                                 apply_gain_automation = gain_control()->list()->curve().rt_safe_get_vector (start_frame, end_frame, _session.gain_automation_buffer(), nframes);
696                         }
697                 }
698
699                 process_output_buffers (bufs, start_frame, end_frame, nframes, offset, (!_session.get_record_enabled() || !Config->get_do_not_record_plugins()), declick, (_meter_point != MeterInput));
700                 
701         } else {
702                 /* problem with the diskstream; just be quiet for a bit */
703                 silence (nframes, offset);
704         }
705
706         return 0;
707 }
708
709 int
710 AudioTrack::silent_roll (nframes_t nframes, nframes_t start_frame, nframes_t end_frame, nframes_t offset, 
711                          bool can_record, bool rec_monitors_input)
712 {
713         if (n_outputs().n_total() == 0 && _processors.empty()) {
714                 return 0;
715         }
716
717         if (!_active) {
718                 silence (nframes, offset);
719                 return 0;
720         }
721
722         _silent = true;
723         apply_gain_automation = false;
724
725         silence (nframes, offset);
726
727         return audio_diskstream()->process (_session.transport_frame() + offset, nframes, offset, can_record, rec_monitors_input);
728 }
729
730 int
731 AudioTrack::export_stuff (BufferSet& buffers, nframes_t start, nframes_t nframes, bool enable_processing)
732 {
733         gain_t  gain_automation[nframes];
734         gain_t  gain_buffer[nframes];
735         float   mix_buffer[nframes];
736         ProcessorList::iterator i;
737         bool post_fader_work = false;
738         gain_t this_gain = _gain;
739         boost::shared_ptr<AudioDiskstream> diskstream = audio_diskstream();
740         
741         Glib::RWLock::ReaderLock rlock (_processor_lock);
742
743         boost::shared_ptr<AudioPlaylist> apl = boost::dynamic_pointer_cast<AudioPlaylist>(diskstream->playlist());
744         assert(apl);
745
746         assert(buffers.get_audio(0).capacity() >= nframes);
747
748         if (apl->read (buffers.get_audio(0).data(), mix_buffer, gain_buffer, start, nframes) != nframes) {
749                 return -1;
750         }
751
752         assert(buffers.count().n_audio() >= 1);
753         uint32_t n=1;
754         Sample* b = buffers.get_audio(0).data();
755         BufferSet::audio_iterator bi = buffers.audio_begin();
756         ++bi;
757         for ( ; bi != buffers.audio_end(); ++bi, ++n) {
758                 if (n < diskstream->n_channels().n_audio()) {
759                         if (apl->read (bi->data(), mix_buffer, gain_buffer, start, nframes, n) != nframes) {
760                                 return -1;
761                         }
762                         b = bi->data();
763                 }
764                 else {
765                         /* duplicate last across remaining buffers */
766                         memcpy (bi->data(), b, sizeof (Sample) * nframes); 
767                 }
768         }
769
770         // If no processing is required, there's no need to go any further.
771         if (!enable_processing)
772                 return 0;
773
774         /* note: only run processors during export. other layers in the machinery
775            will already have checked that there are no external port processors.
776         */
777         
778         for (i = _processors.begin(); i != _processors.end(); ++i) {
779                 boost::shared_ptr<Processor> processor;
780                 
781                 if ((processor = boost::dynamic_pointer_cast<Processor>(*i)) != 0) {
782                         switch (processor->placement()) {
783                         case PreFader:
784                                 processor->run_in_place (buffers, start, start+nframes, nframes, 0);
785                                 break;
786                         case PostFader:
787                                 post_fader_work = true;
788                                 break;
789                         }
790                 }
791         }
792         
793         if (gain_control()->automation_state() == Play) {
794                 
795                 gain_control()->list()->curve().get_vector (start, start + nframes, gain_automation, nframes);
796
797                 for (BufferSet::audio_iterator bi = buffers.audio_begin(); bi != buffers.audio_end(); ++bi) {
798                         Sample *b = bi->data();
799                         for (nframes_t n = 0; n < nframes; ++n) {
800                                 b[n] *= gain_automation[n];
801                         }
802                 }
803
804         } else {
805
806                 for (BufferSet::audio_iterator bi = buffers.audio_begin(); bi != buffers.audio_end(); ++bi) {
807                         Sample *b = bi->data();
808                         for (nframes_t n = 0; n < nframes; ++n) {
809                                 b[n] *= this_gain;
810                         }
811                 }
812         }
813
814         if (post_fader_work) {
815
816                 for (i = _processors.begin(); i != _processors.end(); ++i) {
817                         boost::shared_ptr<PluginInsert> processor;
818                         
819                         if ((processor = boost::dynamic_pointer_cast<PluginInsert>(*i)) != 0) {
820                                 switch ((*i)->placement()) {
821                                 case PreFader:
822                                         break;
823                                 case PostFader:
824                                         processor->run_in_place (buffers, start, start+nframes, nframes, 0);
825                                         break;
826                                 }
827                         }
828                 }
829         } 
830
831         return 0;
832 }
833
834 boost::shared_ptr<Region>
835 AudioTrack::bounce (InterThreadInfo& itt)
836 {
837         vector<boost::shared_ptr<Source> > srcs;
838         return _session.write_one_track (*this, _session.current_start_frame(), _session.current_end_frame(), false, srcs, itt);
839 }
840
841 boost::shared_ptr<Region>
842 AudioTrack::bounce_range (nframes_t start, nframes_t end, InterThreadInfo& itt, bool enable_processing)
843 {
844         vector<boost::shared_ptr<Source> > srcs;
845         return _session.write_one_track (*this, start, end, false, srcs, itt, enable_processing);
846 }
847
848 void
849 AudioTrack::freeze (InterThreadInfo& itt)
850 {
851         vector<boost::shared_ptr<Source> > srcs;
852         string new_playlist_name;
853         boost::shared_ptr<Playlist> new_playlist;
854         string dir;
855         string region_name;
856         boost::shared_ptr<AudioDiskstream> diskstream = audio_diskstream();
857         
858         if ((_freeze_record.playlist = boost::dynamic_pointer_cast<AudioPlaylist>(diskstream->playlist())) == 0) {
859                 return;
860         }
861
862         uint32_t n = 1;
863
864         while (n < (UINT_MAX-1)) {
865          
866                 string candidate;
867                 
868                 candidate = string_compose ("<F%2>%1", _freeze_record.playlist->name(), n);
869
870                 if (_session.playlist_by_name (candidate) == 0) {
871                         new_playlist_name = candidate;
872                         break;
873                 }
874
875                 ++n;
876
877         } 
878
879         if (n == (UINT_MAX-1)) {
880           error << string_compose (X_("There are too many frozen versions of playlist \"%1\""
881                             " to create another one"), _freeze_record.playlist->name())
882                << endmsg;
883                 return;
884         }
885
886         boost::shared_ptr<Region> res;
887
888         if ((res = _session.write_one_track (*this, _session.current_start_frame(), _session.current_end_frame(), true, srcs, itt)) == 0) {
889                 return;
890         }
891
892         _freeze_record.processor_info.clear ();
893
894         {
895                 Glib::RWLock::ReaderLock lm (_processor_lock);
896                 
897                 for (ProcessorList::iterator r = _processors.begin(); r != _processors.end(); ++r) {
898                         
899                         boost::shared_ptr<Processor> processor;
900
901                         if ((processor = boost::dynamic_pointer_cast<Processor>(*r)) != 0) {
902                                 
903                                 FreezeRecordProcessorInfo* frii  = new FreezeRecordProcessorInfo ((*r)->get_state(), processor);
904                                 
905                                 frii->id = processor->id();
906
907                                 _freeze_record.processor_info.push_back (frii);
908                                 
909                                 /* now deactivate the processor */
910                                 
911                                 processor->deactivate ();
912                                 _session.set_dirty ();
913                         }
914                 }
915         }
916
917         new_playlist = PlaylistFactory::create (DataType::AUDIO, _session, new_playlist_name, false);
918
919         _freeze_record.gain = _gain;
920         _freeze_record.gain_automation_state = _gain_control->automation_state();
921         _freeze_record.pan_automation_state = _panner->automation_state();
922
923         region_name = new_playlist_name;
924
925         /* create a new region from all filesources, keep it private */
926
927         boost::shared_ptr<Region> region (RegionFactory::create (srcs, 0,
928                         srcs[0]->length(srcs[0]->timeline_position()), 
929                         region_name, 0,
930                         (Region::Flag) (Region::WholeFile|Region::DefaultFlags),
931                         false));
932
933         new_playlist->set_orig_diskstream_id (diskstream->id());
934         new_playlist->add_region (region, _session.current_start_frame());
935         new_playlist->set_frozen (true);
936         region->set_locked (true);
937
938         diskstream->use_playlist (boost::dynamic_pointer_cast<AudioPlaylist>(new_playlist));
939         diskstream->set_record_enabled (false);
940
941         /* reset stuff that has already been accounted for in the freeze process */
942         
943         set_gain (1.0, this);
944         _gain_control->set_automation_state (Off);
945         _panner->set_automation_state (Off);
946
947         _freeze_record.state = Frozen;
948         FreezeChange(); /* EMIT SIGNAL */
949 }
950
951 void
952 AudioTrack::unfreeze ()
953 {
954         if (_freeze_record.playlist) {
955                 audio_diskstream()->use_playlist (_freeze_record.playlist);
956
957                 {
958                         Glib::RWLock::ReaderLock lm (_processor_lock); // should this be a write lock? jlc
959                         for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
960                                 for (vector<FreezeRecordProcessorInfo*>::iterator ii = _freeze_record.processor_info.begin(); ii != _freeze_record.processor_info.end(); ++ii) {
961                                         if ((*ii)->id == (*i)->id()) {
962                                                 (*i)->set_state (((*ii)->state));
963                                                 break;
964                                         }
965                                 }
966                         }
967                 }
968                 
969                 _freeze_record.playlist.reset ();
970                 set_gain (_freeze_record.gain, this);
971                 _gain_control->set_automation_state (_freeze_record.gain_automation_state);
972                 _panner->set_automation_state (_freeze_record.pan_automation_state);
973         }
974
975         _freeze_record.state = UnFrozen;
976         FreezeChange (); /* EMIT SIGNAL */
977 }
978