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