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