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