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