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