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