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