fixes for endemic (compiler?) issues with virtual inheritance of sigc::trackable...
[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                 } else if (child->name() == X_("recenable")) {
251                         _rec_enable_control.set_state (*child);
252                         _session.add_controllable (&_rec_enable_control);
253                 }
254         }
255
256         pending_state = const_cast<XMLNode*> (&node);
257
258         _session.StateReady.connect (mem_fun (*this, &AudioTrack::set_state_part_two));
259
260         return 0;
261 }
262
263 XMLNode& 
264 AudioTrack::state(bool full_state)
265 {
266         XMLNode& root (Route::state(full_state));
267         XMLNode* freeze_node;
268         char buf[64];
269
270         if (_freeze_record.playlist) {
271                 XMLNode* inode;
272
273                 freeze_node = new XMLNode (X_("freeze-info"));
274                 freeze_node->add_property ("playlist", _freeze_record.playlist->name());
275                 snprintf (buf, sizeof (buf), "%d", (int) _freeze_record.state);
276                 freeze_node->add_property ("state", buf);
277
278                 for (vector<FreezeRecordInsertInfo*>::iterator i = _freeze_record.insert_info.begin(); i != _freeze_record.insert_info.end(); ++i) {
279                         inode = new XMLNode (X_("insert"));
280                         (*i)->id.print (buf, sizeof (buf));
281                         inode->add_property (X_("id"), buf);
282                         inode->add_child_copy ((*i)->state);
283                 
284                         freeze_node->add_child_nocopy (*inode);
285                 }
286
287                 root.add_child_nocopy (*freeze_node);
288         }
289
290         /* Alignment: act as a proxy for the diskstream */
291         
292         XMLNode* align_node = new XMLNode (X_("alignment"));
293         switch (_diskstream->alignment_style()) {
294         case ExistingMaterial:
295                 snprintf (buf, sizeof (buf), X_("existing"));
296                 break;
297         case CaptureTime:
298                 snprintf (buf, sizeof (buf), X_("capture"));
299                 break;
300         }
301         align_node->add_property (X_("style"), buf);
302         root.add_child_nocopy (*align_node);
303
304         XMLNode* remote_control_node = new XMLNode (X_("remote_control"));
305         snprintf (buf, sizeof (buf), "%d", _remote_control_id);
306         remote_control_node->add_property (X_("id"), buf);
307         root.add_child_nocopy (*remote_control_node);
308
309         switch (_mode) {
310         case Normal:
311                 root.add_property (X_("mode"), X_("normal"));
312                 break;
313         case Destructive:
314                 root.add_property (X_("mode"), X_("destructive"));
315                 break;
316         }
317
318         /* we don't return diskstream state because we don't
319            own the diskstream exclusively. control of the diskstream
320            state is ceded to the Session, even if we create the
321            diskstream.
322         */
323
324         _diskstream->id().print (buf, sizeof (buf));
325         root.add_property ("diskstream-id", buf);
326
327         root.add_child_nocopy (_rec_enable_control.get_state());
328
329         return root;
330 }
331
332 void
333 AudioTrack::set_state_part_two ()
334 {
335         XMLNode* fnode;
336         XMLProperty* prop;
337         LocaleGuard lg (X_("POSIX"));
338
339         /* This is called after all session state has been restored but before
340            have been made ports and connections are established.
341         */
342
343         if (pending_state == 0) {
344                 return;
345         }
346
347         if ((fnode = find_named_node (*pending_state, X_("freeze-info"))) != 0) {
348
349                 
350                 _freeze_record.have_mementos = false;
351                 _freeze_record.state = Frozen;
352                 
353                 for (vector<FreezeRecordInsertInfo*>::iterator i = _freeze_record.insert_info.begin(); i != _freeze_record.insert_info.end(); ++i) {
354                         delete *i;
355                 }
356                 _freeze_record.insert_info.clear ();
357                 
358                 if ((prop = fnode->property (X_("playlist"))) != 0) {
359                         Playlist* pl = _session.playlist_by_name (prop->value());
360                         if (pl) {
361                                 _freeze_record.playlist = dynamic_cast<AudioPlaylist*> (pl);
362                         } else {
363                                 _freeze_record.playlist = 0;
364                                 _freeze_record.state = NoFreeze;
365                         return;
366                         }
367                 }
368                 
369                 if ((prop = fnode->property (X_("state"))) != 0) {
370                         _freeze_record.state = (FreezeState) atoi (prop->value().c_str());
371                 }
372                 
373                 XMLNodeConstIterator citer;
374                 XMLNodeList clist = fnode->children();
375                 
376                 for (citer = clist.begin(); citer != clist.end(); ++citer) {
377                         if ((*citer)->name() != X_("insert")) {
378                                 continue;
379                         }
380                         
381                         if ((prop = (*citer)->property (X_("id"))) == 0) {
382                                 continue;
383                         }
384                         
385                         FreezeRecordInsertInfo* frii = new FreezeRecordInsertInfo (*((*citer)->children().front()),
386                                                                                    boost::shared_ptr<Insert>());
387                         frii->id = prop->value ();
388                         _freeze_record.insert_info.push_back (frii);
389                 }
390         }
391
392         /* Alignment: act as a proxy for the diskstream */
393
394         if ((fnode = find_named_node (*pending_state, X_("alignment"))) != 0) {
395
396                 if ((prop = fnode->property (X_("style"))) != 0) {
397                         if (prop->value() == "existing") {
398                                 _diskstream->set_persistent_align_style (ExistingMaterial);
399                         } else if (prop->value() == "capture") {
400                                 _diskstream->set_persistent_align_style (CaptureTime);
401                         }
402                 }
403         }
404         return;
405 }       
406
407 uint32_t
408 AudioTrack::n_process_buffers ()
409 {
410         return max ((uint32_t) _diskstream->n_channels(), redirect_max_outs);
411 }
412
413 void
414 AudioTrack::passthru_silence (nframes_t start_frame, nframes_t end_frame, nframes_t nframes, nframes_t offset, int declick, bool meter)
415 {
416         uint32_t nbufs = n_process_buffers ();
417         process_output_buffers (_session.get_silent_buffers (nbufs), nbufs, start_frame, end_frame, nframes, offset, true, declick, meter);
418 }
419
420 int 
421 AudioTrack::no_roll (nframes_t nframes, nframes_t start_frame, nframes_t end_frame, nframes_t offset, 
422                      bool session_state_changing, bool can_record, bool rec_monitors_input)
423 {
424         if (n_outputs() == 0) {
425                 return 0;
426         }
427
428         if (!_active) {
429                 silence (nframes, offset);
430                 return 0;
431         }
432
433         if (session_state_changing) {
434
435                 /* XXX is this safe to do against transport state changes? */
436
437                 passthru_silence (start_frame, end_frame, nframes, offset, 0, false);
438                 return 0;
439         }
440
441         audio_diskstream()->check_record_status (start_frame, nframes, can_record);
442
443         bool send_silence;
444         
445         if (_have_internal_generator) {
446                 /* since the instrument has no input streams,
447                    there is no reason to send any signal
448                    into the route.
449                 */
450                 send_silence = true;
451         } else {
452
453                 if (Config->get_auto_input()) {
454                         if (Config->get_monitoring_model() == SoftwareMonitoring) {
455                                 send_silence = false;
456                         } else {
457                                 send_silence = true;
458                         }
459                 } else {
460                         if (_diskstream->record_enabled()) {
461                                 if (Config->get_monitoring_model() == SoftwareMonitoring) {
462                                         send_silence = false;
463                                 } else {
464                                         send_silence = true;
465                                 }
466                         } else {
467                                 send_silence = true;
468                         }
469                 }
470         }
471
472         apply_gain_automation = false;
473
474         if (send_silence) {
475                 
476                 /* if we're sending silence, but we want the meters to show levels for the signal,
477                    meter right here.
478                 */
479                 
480                 if (_have_internal_generator) {
481                         passthru_silence (start_frame, end_frame, nframes, offset, 0, true);
482                 } else {
483                         if (_meter_point == MeterInput) {
484                                 just_meter_input (start_frame, end_frame, nframes, offset);
485                         }
486                         passthru_silence (start_frame, end_frame, nframes, offset, 0, false);
487                 }
488
489         } else {
490         
491                 /* we're sending signal, but we may still want to meter the input. 
492                  */
493
494                 passthru (start_frame, end_frame, nframes, offset, 0, (_meter_point == MeterInput));
495         }
496
497         return 0;
498 }
499
500 int
501 AudioTrack::roll (nframes_t nframes, nframes_t start_frame, nframes_t end_frame, nframes_t offset, int declick,
502                   bool can_record, bool rec_monitors_input)
503 {
504         int dret;
505         Sample* b;
506         Sample* tmpb;
507         nframes_t transport_frame;
508         boost::shared_ptr<AudioDiskstream> diskstream = audio_diskstream();
509         
510         if (n_outputs() == 0 && _redirects.empty()) {
511                 return 0;
512         }
513
514         if (!_active) {
515                 silence (nframes, offset);
516                 return 0;
517         }
518
519         transport_frame = _session.transport_frame();
520
521         if ((nframes = check_initial_delay (nframes, offset, transport_frame)) == 0) {
522                 /* need to do this so that the diskstream sets its
523                    playback distance to zero, thus causing diskstream::commit
524                    to do nothing.
525                 */
526                 return diskstream->process (transport_frame, 0, 0, can_record, rec_monitors_input);
527         } 
528
529         _silent = false;
530         apply_gain_automation = false;
531
532         if ((dret = diskstream->process (transport_frame, nframes, offset, can_record, rec_monitors_input)) != 0) {
533                 
534                 silence (nframes, offset);
535
536                 return dret;
537         }
538
539         /* special condition applies */
540         
541         if (_meter_point == MeterInput) {
542                 just_meter_input (start_frame, end_frame, nframes, offset);
543         }
544
545         if (diskstream->record_enabled() && !can_record && !Config->get_auto_input()) {
546
547                 /* not actually recording, but we want to hear the input material anyway,
548                    at least potentially (depending on monitoring options)
549                  */
550
551                 passthru (start_frame, end_frame, nframes, offset, 0, true);
552
553         } else if ((b = diskstream->playback_buffer(0)) != 0) {
554
555                 /*
556                   XXX is it true that the earlier test on n_outputs()
557                   means that we can avoid checking it again here? i think
558                   so, because changing the i/o configuration of an IO
559                   requires holding the AudioEngine lock, which we hold
560                   while in the process() tree.
561                 */
562
563                 
564                 /* copy the diskstream data to all output buffers */
565                 
566                 vector<Sample*>& bufs = _session.get_passthru_buffers ();
567                 uint32_t limit = n_process_buffers ();
568                 
569                 uint32_t n;
570                 uint32_t i;
571
572
573                 for (i = 0, n = 1; i < limit; ++i, ++n) {
574                         memcpy (bufs[i], b, sizeof (Sample) * nframes); 
575                         if (n < diskstream->n_channels()) {
576                                 tmpb = diskstream->playback_buffer(n);
577                                 if (tmpb!=0) {
578                                         b = tmpb;
579                                 }
580                         }
581                 }
582
583                 /* don't waste time with automation if we're recording or we've just stopped (yes it can happen) */
584
585                 if (!diskstream->record_enabled() && _session.transport_rolling()) {
586                         Glib::Mutex::Lock am (automation_lock, Glib::TRY_LOCK);
587                         
588                         if (am.locked() && gain_automation_playback()) {
589                                 apply_gain_automation = _gain_automation_curve.rt_safe_get_vector (start_frame, end_frame, _session.gain_automation_buffer(), nframes);
590                         }
591                 }
592
593                 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));
594                 
595         } else {
596                 /* problem with the diskstream; just be quiet for a bit */
597                 silence (nframes, offset);
598         }
599
600         return 0;
601 }
602
603 int
604 AudioTrack::silent_roll (nframes_t nframes, nframes_t start_frame, nframes_t end_frame, nframes_t offset, 
605                          bool can_record, bool rec_monitors_input)
606 {
607         if (n_outputs() == 0 && _redirects.empty()) {
608                 return 0;
609         }
610
611         if (!_active) {
612                 silence (nframes, offset);
613                 return 0;
614         }
615
616         _silent = true;
617         apply_gain_automation = false;
618
619         silence (nframes, offset);
620
621         return audio_diskstream()->process (_session.transport_frame() + offset, nframes, offset, can_record, rec_monitors_input);
622 }
623
624 int
625 AudioTrack::export_stuff (vector<Sample*>& buffers, uint32_t nbufs, nframes_t start, nframes_t nframes)
626 {
627         gain_t  gain_automation[nframes];
628         gain_t  gain_buffer[nframes];
629         float   mix_buffer[nframes];
630         RedirectList::iterator i;
631         bool post_fader_work = false;
632         gain_t this_gain = _gain;
633         vector<Sample*>::iterator bi;
634         Sample * b;
635         boost::shared_ptr<AudioDiskstream> diskstream = audio_diskstream();
636         
637         Glib::RWLock::ReaderLock rlock (redirect_lock);
638
639         // FIXME
640         AudioPlaylist* const apl = dynamic_cast<AudioPlaylist*>(diskstream->playlist());
641         assert(apl);
642
643         if (apl->read (buffers[0], mix_buffer, gain_buffer, start, nframes) != nframes) {
644                 return -1;
645         }
646
647         uint32_t n=1;
648         bi = buffers.begin();
649         b = buffers[0];
650         ++bi;
651         for (; bi != buffers.end(); ++bi, ++n) {
652                 if (n < diskstream->n_channels()) {
653                         if (apl->read ((*bi), mix_buffer, gain_buffer, start, nframes, n) != nframes) {
654                                 return -1;
655                         }
656                         b = (*bi);
657                 }
658                 else {
659                         /* duplicate last across remaining buffers */
660                         memcpy ((*bi), b, sizeof (Sample) * nframes); 
661                 }
662         }
663
664
665         /* note: only run inserts during export. other layers in the machinery
666            will already have checked that there are no external port inserts.
667         */
668         
669         for (i = _redirects.begin(); i != _redirects.end(); ++i) {
670                 boost::shared_ptr<Insert> insert;
671                 
672                 if ((insert = boost::dynamic_pointer_cast<Insert>(*i)) != 0) {
673                         switch (insert->placement()) {
674                         case PreFader:
675                                 insert->run (buffers, nbufs, nframes, 0);
676                                 break;
677                         case PostFader:
678                                 post_fader_work = true;
679                                 break;
680                         }
681                 }
682         }
683         
684         if (_gain_automation_curve.automation_state() == Play) {
685                 
686                 _gain_automation_curve.get_vector (start, start + nframes, gain_automation, nframes);
687
688                 for (bi = buffers.begin(); bi != buffers.end(); ++bi) {
689                         Sample *b = *bi;
690                         for (nframes_t n = 0; n < nframes; ++n) {
691                                 b[n] *= gain_automation[n];
692                         }
693                 }
694
695         } else {
696
697                 for (bi = buffers.begin(); bi != buffers.end(); ++bi) {
698                         Sample *b = *bi;
699                         for (nframes_t n = 0; n < nframes; ++n) {
700                                 b[n] *= this_gain;
701                         }
702                 }
703         }
704
705         if (post_fader_work) {
706
707                 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
708                         boost::shared_ptr<PluginInsert> insert;
709                         
710                         if ((insert = boost::dynamic_pointer_cast<PluginInsert>(*i)) != 0) {
711                                 switch ((*i)->placement()) {
712                                 case PreFader:
713                                         break;
714                                 case PostFader:
715                                         insert->run (buffers, nbufs, nframes, 0);
716                                         break;
717                                 }
718                         }
719                 }
720         } 
721
722         return 0;
723 }
724
725 void
726 AudioTrack::bounce (InterThreadInfo& itt)
727 {
728         vector<boost::shared_ptr<AudioSource> > srcs;
729         _session.write_one_audio_track (*this, 0, _session.current_end_frame(), false, srcs, itt);
730 }
731
732
733 void
734 AudioTrack::bounce_range (nframes_t start, nframes_t end, InterThreadInfo& itt)
735 {
736         vector<boost::shared_ptr<AudioSource> > srcs;
737         _session.write_one_audio_track (*this, start, end, false, srcs, itt);
738 }
739
740 void
741 AudioTrack::freeze (InterThreadInfo& itt)
742 {
743         vector<boost::shared_ptr<AudioSource> > srcs;
744         string new_playlist_name;
745         Playlist* new_playlist;
746         string dir;
747         string region_name;
748         boost::shared_ptr<AudioDiskstream> diskstream = audio_diskstream();
749         
750         if ((_freeze_record.playlist = dynamic_cast<AudioPlaylist*>(diskstream->playlist())) == 0) {
751                 return;
752         }
753
754         uint32_t n = 1;
755
756         while (n < (UINT_MAX-1)) {
757          
758                 string candidate;
759                 
760                 candidate = string_compose ("<F%2>%1", _freeze_record.playlist->name(), n);
761
762                 if (_session.playlist_by_name (candidate) == 0) {
763                         new_playlist_name = candidate;
764                         break;
765                 }
766
767                 ++n;
768
769         } 
770
771         if (n == (UINT_MAX-1)) {
772           error << string_compose (X_("There are too many frozen versions of playlist \"%1\""
773                             " to create another one"), _freeze_record.playlist->name())
774                << endmsg;
775                 return;
776         }
777
778         if (_session.write_one_audio_track (*this, 0, _session.current_end_frame(), true, srcs, itt)) {
779                 return;
780         }
781
782         _freeze_record.insert_info.clear ();
783         _freeze_record.have_mementos = true;
784
785         {
786                 Glib::RWLock::ReaderLock lm (redirect_lock);
787                 
788                 for (RedirectList::iterator r = _redirects.begin(); r != _redirects.end(); ++r) {
789                         
790                         boost::shared_ptr<Insert> insert;
791
792                         if ((insert = boost::dynamic_pointer_cast<Insert>(*r)) != 0) {
793                                 
794                                 FreezeRecordInsertInfo* frii  = new FreezeRecordInsertInfo ((*r)->get_state(), insert);
795                                 
796                                 frii->id = insert->id();
797 #ifdef STATE_MANAGER
798                                 frii->memento = (*r)->get_memento();
799 #endif                          
800                                 _freeze_record.insert_info.push_back (frii);
801                                 
802                                 /* now deactivate the insert */
803                                 
804                                 insert->set_active (false, this);
805                         }
806                 }
807         }
808
809         new_playlist = new AudioPlaylist (_session, new_playlist_name, false);
810         region_name = new_playlist_name;
811
812         /* create a new region from all filesources, keep it private */
813
814         boost::shared_ptr<Region> region (RegionFactory::create (srcs, 0, srcs[0]->length(), 
815                                                                  region_name, 0, 
816                                                                  (AudioRegion::Flag) (AudioRegion::WholeFile|AudioRegion::DefaultFlags),
817                                                                  false));
818
819         new_playlist->set_orig_diskstream_id (diskstream->id());
820         new_playlist->add_region (region, 0);
821         new_playlist->set_frozen (true);
822         region->set_locked (true);
823
824         diskstream->use_playlist (dynamic_cast<AudioPlaylist*>(new_playlist));
825         diskstream->set_record_enabled (false);
826
827         _freeze_record.state = Frozen;
828         FreezeChange(); /* EMIT SIGNAL */
829 }
830
831 void
832 AudioTrack::unfreeze ()
833 {
834         if (_freeze_record.playlist) {
835                 audio_diskstream()->use_playlist (_freeze_record.playlist);
836
837                 if (_freeze_record.have_mementos) {
838
839                         for (vector<FreezeRecordInsertInfo*>::iterator i = _freeze_record.insert_info.begin(); i != _freeze_record.insert_info.end(); ++i) {
840                                 (*i)->memento ();
841                         }
842
843                 } else {
844
845                         Glib::RWLock::ReaderLock lm (redirect_lock); // should this be a write lock? jlc
846                         for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
847                                 for (vector<FreezeRecordInsertInfo*>::iterator ii = _freeze_record.insert_info.begin(); ii != _freeze_record.insert_info.end(); ++ii) {
848                                         if ((*ii)->id == (*i)->id()) {
849                                                 (*i)->set_state (((*ii)->state));
850                                                 break;
851                                         }
852                                 }
853                         }
854                 }
855                 
856                 _freeze_record.playlist = 0;
857         }
858
859         _freeze_record.state = UnFrozen;
860         FreezeChange (); /* EMIT SIGNAL */
861 }
862