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