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