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