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