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