Fix comment.
[ardour.git] / libs / ardour / track.cc
1 /*
2     Copyright (C) 2006 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 #include "pbd/error.h"
19
20 #include "ardour/amp.h"
21 #include "ardour/audioplaylist.h"
22 #include "ardour/audioregion.h"
23 #include "ardour/audiosource.h"
24 #include "ardour/debug.h"
25 #include "ardour/delivery.h"
26 #include "ardour/diskstream.h"
27 #include "ardour/io_processor.h"
28 #include "ardour/meter.h"
29 #include "ardour/port.h"
30 #include "ardour/processor.h"
31 #include "ardour/route_group_specialized.h"
32 #include "ardour/session.h"
33 #include "ardour/track.h"
34 #include "ardour/utils.h"
35
36 #include "i18n.h"
37
38 using namespace std;
39 using namespace ARDOUR;
40 using namespace PBD;
41
42 Track::Track (Session& sess, string name, Route::Flag flag, TrackMode mode, DataType default_type)
43         : Route (sess, name, flag, default_type)
44         , _saved_meter_point (_meter_point)
45         , _mode (mode)
46         , _monitoring (MonitorAuto)
47         , _rec_enable_control (new RecEnableControllable(*this))
48 {
49         _freeze_record.state = NoFreeze;
50         _declickable = true;
51 }
52
53 Track::~Track ()
54 {
55         DEBUG_TRACE (DEBUG::Destruction, string_compose ("track %1 destructor\n", _name));
56 }
57
58 int
59 Track::init ()
60 {
61         if (Route::init ()) {
62                 return -1;
63         }
64
65         return 0;
66 }
67
68 XMLNode&
69 Track::get_state ()
70 {
71         return state (true);
72 }
73
74 XMLNode&
75 Track::state (bool full)
76 {
77         XMLNode& root (Route::state (full));
78         root.add_property (X_("monitoring"), enum_2_string (_monitoring));
79         root.add_property (X_("saved-meter-point"), enum_2_string (_saved_meter_point));
80         root.add_child_nocopy (_rec_enable_control->get_state());
81         root.add_child_nocopy (_diskstream->get_state ());
82         return root;
83 }       
84
85 int
86 Track::set_state (const XMLNode& node, int version)
87 {
88         return _set_state (node, version);
89 }
90
91 int
92 Track::_set_state (const XMLNode& node, int version)
93 {
94         if (Route::_set_state (node, version)) {
95                 return -1;
96         }
97
98         XMLNode* child;
99
100         if (version >= 3000) {
101                 if ((child = find_named_node (node, X_("Diskstream"))) != 0) {
102                         boost::shared_ptr<Diskstream> ds = diskstream_factory (*child);
103                         ds->do_refill_with_alloc ();
104                         set_diskstream (ds);
105                 }
106         }
107
108         /* set rec-enable control *AFTER* setting up diskstream, because it may
109            want to operate on the diskstream as it sets its own state
110         */
111
112         XMLNodeList nlist = node.children();
113         for (XMLNodeConstIterator niter = nlist.begin(); niter != nlist.end(); ++niter) {
114                 child = *niter;
115
116                 XMLProperty* prop;
117                 if (child->name() == Controllable::xml_node_name && (prop = child->property ("name")) != 0) {
118                         if (prop->value() == X_("recenable")) {
119                                 _rec_enable_control->set_state (*child, version);
120                         }
121                 }
122         }
123         
124         const XMLProperty* prop;
125
126         if ((prop = node.property (X_("monitoring"))) != 0) {
127                 _monitoring = MonitorChoice (string_2_enum (prop->value(), _monitoring));
128         } else {
129                 _monitoring = MonitorAuto;
130         }
131
132         if ((prop = node.property (X_("saved-meter-point"))) != 0) {
133                 _saved_meter_point = MeterPoint (string_2_enum (prop->value(), _saved_meter_point));
134         } else {
135                 _saved_meter_point = _meter_point;
136         }
137
138         return 0;
139 }
140
141 XMLNode&
142 Track::get_template ()
143 {
144         return state (false);
145 }
146
147 void
148 Track::toggle_monitor_input ()
149 {
150         for (PortSet::iterator i = _input->ports().begin(); i != _input->ports().end(); ++i) {
151                 i->ensure_monitor_input(!i->monitoring_input());
152         }
153 }
154
155 Track::FreezeRecord::~FreezeRecord ()
156 {
157         for (vector<FreezeRecordProcessorInfo*>::iterator i = processor_info.begin(); i != processor_info.end(); ++i) {
158                 delete *i;
159         }
160 }
161
162 Track::FreezeState
163 Track::freeze_state() const
164 {
165         return _freeze_record.state;
166 }
167
168 Track::RecEnableControllable::RecEnableControllable (Track& s)
169         : Controllable (X_("recenable")), track (s)
170 {
171 }
172
173 void
174 Track::RecEnableControllable::set_value (double val)
175 {
176         bool bval = ((val >= 0.5) ? true: false);
177         track.set_record_enabled (bval, this);
178 }
179
180 double
181 Track::RecEnableControllable::get_value (void) const
182 {
183         if (track.record_enabled()) { return 1.0; }
184         return 0.0;
185 }
186
187 bool
188 Track::record_enabled () const
189 {
190         return _diskstream && _diskstream->record_enabled ();
191 }
192
193 bool
194 Track::can_record()
195 {
196         bool will_record = true;
197         for (PortSet::iterator i = _input->ports().begin(); i != _input->ports().end() && will_record; ++i) {
198                 if (!i->connected())
199                         will_record = false;
200         }
201
202         return will_record;
203 }
204
205 void
206 Track::set_record_enabled (bool yn, void *src)
207 {
208         if (!_session.writable()) {
209                 return;
210         }
211
212         if (_freeze_record.state == Frozen) {
213                 return;
214         }
215
216         if (_route_group && src != _route_group && _route_group->is_active() && _route_group->is_recenable()) {
217                 _route_group->apply (&Track::set_record_enabled, yn, _route_group);
218                 return;
219         }
220
221         /* keep track of the meter point as it was before we rec-enabled */
222         if (!_diskstream->record_enabled()) {
223                 _saved_meter_point = _meter_point;
224         }
225
226         _diskstream->set_record_enabled (yn);
227
228         if (_diskstream->record_enabled()) {
229                 if (_meter_point != MeterCustom) {
230                         set_meter_point (MeterInput);
231                 }
232         } else {
233                 set_meter_point (_saved_meter_point);
234         }
235
236         _rec_enable_control->Changed ();
237 }
238
239
240 bool
241 Track::set_name (const string& str)
242 {
243         bool ret;
244
245         if (record_enabled() && _session.actively_recording()) {
246                 /* this messes things up if done while recording */
247                 return false;
248         }
249
250         _diskstream->set_name (str);
251
252         /* save state so that the statefile fully reflects any filename changes */
253
254         if ((ret = Route::set_name (str)) == 0) {
255                 _session.save_state ("");
256         }
257
258         return ret;
259 }
260
261 void
262 Track::set_latency_compensation (framecnt_t longest_session_latency)
263 {
264         Route::set_latency_compensation (longest_session_latency);
265         _diskstream->set_roll_delay (_roll_delay);
266 }
267
268 void
269 Track::zero_diskstream_id_in_xml (XMLNode& node)
270 {
271         if (node.property ("diskstream-id")) {
272                 node.add_property ("diskstream-id", "0");
273         }
274 }
275
276 int
277 Track::no_roll (pframes_t nframes, framepos_t start_frame, framepos_t end_frame, bool session_state_changing)
278 {
279         Glib::RWLock::ReaderLock lm (_processor_lock, Glib::TRY_LOCK);
280         if (!lm.locked()) {
281                 return 0;
282         }
283
284         bool can_record = _session.actively_recording ();
285
286         if (n_outputs().n_total() == 0) {
287                 return 0;
288         }
289
290         if (!_active) {
291                 silence (nframes);
292                 return 0;
293         }
294
295         if (session_state_changing) {
296                 if (_session.transport_speed() != 0.0f) {
297                         /* we're rolling but some state is changing (e.g. our diskstream contents)
298                            so we cannot use them. Be silent till this is over. Don't declick.
299
300                            XXX note the absurdity of ::no_roll() being called when we ARE rolling!
301                         */
302                         passthru_silence (start_frame, end_frame, nframes, 0);
303                         return 0;
304                 }
305                 /* we're really not rolling, so we're either delivery silence or actually
306                    monitoring, both of which are safe to do while session_state_changing is true.
307                 */
308         }
309
310         _diskstream->check_record_status (start_frame, can_record);
311
312         bool be_silent;
313
314         if (_have_internal_generator) {
315                 /* since the instrument has no input streams,
316                    there is no reason to send any signal
317                    into the route.
318                 */
319                 be_silent = true;
320         } else {
321                 be_silent = send_silence ();
322         }
323
324         _amp->apply_gain_automation(false);
325
326         if (be_silent) {
327
328                 /* if we're sending silence, but we want the meters to show levels for the signal,
329                    meter right here.
330                 */
331
332                 if (_have_internal_generator) {
333                         passthru_silence (start_frame, end_frame, nframes, 0);
334                 } else {
335                         if (_meter_point == MeterInput) {
336                                 _input->process_input (_meter, start_frame, end_frame, nframes);
337                         }
338                         passthru_silence (start_frame, end_frame, nframes, 0);
339                 }
340
341         } else {
342
343                 /* we're sending signal, but we may still want to meter the input.
344                  */
345
346                 passthru (start_frame, end_frame, nframes, false);
347         }
348
349         for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
350                 boost::shared_ptr<Delivery> d = boost::dynamic_pointer_cast<Delivery> (*i);
351                 if (d) {
352                         d->flush_buffers (nframes, end_frame - start_frame - 1);
353                 }
354         }
355
356         return 0;
357 }
358
359 int
360 Track::silent_roll (pframes_t nframes, framepos_t /*start_frame*/, framepos_t /*end_frame*/, bool& need_butler)
361 {
362         Glib::RWLock::ReaderLock lm (_processor_lock, Glib::TRY_LOCK);
363         if (!lm.locked()) {
364                 return 0;
365         }
366
367         if (n_outputs().n_total() == 0 && _processors.empty()) {
368                 return 0;
369         }
370
371         if (!_active) {
372                 silence (nframes);
373                 return 0;
374         }
375
376         _silent = true;
377         _amp->apply_gain_automation(false);
378
379         silence (nframes);
380
381         framecnt_t playback_distance;
382         int const dret = _diskstream->process (_session.transport_frame(), nframes, playback_distance);
383         need_butler = _diskstream->commit (playback_distance);
384         return dret;
385 }
386
387 void
388 Track::set_diskstream (boost::shared_ptr<Diskstream> ds)
389 {
390         _diskstream = ds;
391
392         ds->PlaylistChanged.connect_same_thread (*this, boost::bind (&Track::diskstream_playlist_changed, this));
393         diskstream_playlist_changed ();
394         ds->RecordEnableChanged.connect_same_thread (*this, boost::bind (&Track::diskstream_record_enable_changed, this));
395         ds->SpeedChanged.connect_same_thread (*this, boost::bind (&Track::diskstream_speed_changed, this));
396         ds->AlignmentStyleChanged.connect_same_thread (*this, boost::bind (&Track::diskstream_alignment_style_changed, this));
397 }
398
399 void
400 Track::diskstream_playlist_changed ()
401 {
402         PlaylistChanged (); /* EMIT SIGNAL */
403 }
404
405 void
406 Track::diskstream_record_enable_changed ()
407 {
408         RecordEnableChanged (); /* EMIT SIGNAL */
409 }
410
411 void
412 Track::diskstream_speed_changed ()
413 {
414         SpeedChanged (); /* EMIT SIGNAL */
415 }
416
417 void
418 Track::diskstream_alignment_style_changed ()
419 {
420         AlignmentStyleChanged (); /* EMIT SIGNAL */
421 }
422
423 boost::shared_ptr<Playlist>
424 Track::playlist ()
425 {
426         return _diskstream->playlist ();
427 }
428
429 void
430 Track::monitor_input (bool m)
431 {
432         _diskstream->monitor_input (m);
433 }
434
435 bool
436 Track::destructive () const
437 {
438         return _diskstream->destructive ();
439 }
440
441 list<boost::shared_ptr<Source> > &
442 Track::last_capture_sources ()
443 {
444         return _diskstream->last_capture_sources ();
445 }
446
447 void
448 Track::set_capture_offset ()
449 {
450         _diskstream->set_capture_offset ();
451 }
452
453 list<boost::shared_ptr<Source> >
454 Track::steal_write_sources()
455 {
456         return _diskstream->steal_write_sources ();
457 }
458
459 void
460 Track::reset_write_sources (bool r, bool force)
461 {
462         _diskstream->reset_write_sources (r, force);
463 }
464
465 float
466 Track::playback_buffer_load () const
467 {
468         return _diskstream->playback_buffer_load ();
469 }
470
471 float
472 Track::capture_buffer_load () const
473 {
474         return _diskstream->capture_buffer_load ();
475 }
476
477 int
478 Track::do_refill ()
479 {
480         return _diskstream->do_refill ();
481 }
482
483 int
484 Track::do_flush (RunContext c, bool force)
485 {
486         return _diskstream->do_flush (c, force);
487 }
488
489 void
490 Track::set_pending_overwrite (bool o)
491 {
492         _diskstream->set_pending_overwrite (o);
493 }
494
495 int
496 Track::seek (framepos_t p, bool complete_refill)
497 {
498         return _diskstream->seek (p, complete_refill);
499 }
500
501 bool
502 Track::hidden () const
503 {
504         return _diskstream->hidden ();
505 }
506
507 int
508 Track::can_internal_playback_seek (framepos_t p)
509 {
510         return _diskstream->can_internal_playback_seek (p);
511 }
512
513 int
514 Track::internal_playback_seek (framepos_t p)
515 {
516         return _diskstream->internal_playback_seek (p);
517 }
518
519 void
520 Track::non_realtime_input_change ()
521 {
522         _diskstream->non_realtime_input_change ();
523 }
524
525 void
526 Track::non_realtime_locate (framepos_t p)
527 {
528         _diskstream->non_realtime_locate (p);
529 }
530
531 void
532 Track::non_realtime_set_speed ()
533 {
534         _diskstream->non_realtime_set_speed ();
535 }
536
537 int
538 Track::overwrite_existing_buffers ()
539 {
540         return _diskstream->overwrite_existing_buffers ();
541 }
542
543 framecnt_t
544 Track::get_captured_frames (uint32_t n) const
545 {
546         return _diskstream->get_captured_frames (n);
547 }
548
549 int
550 Track::set_loop (Location* l)
551 {
552         return _diskstream->set_loop (l);
553 }
554
555 void
556 Track::transport_looped (framepos_t p)
557 {
558         _diskstream->transport_looped (p);
559 }
560
561 bool
562 Track::realtime_set_speed (double s, bool g)
563 {
564         return _diskstream->realtime_set_speed (s, g);
565 }
566
567 void
568 Track::transport_stopped_wallclock (struct tm & n, time_t t, bool g)
569 {
570         _diskstream->transport_stopped_wallclock (n, t, g);
571 }
572
573 bool
574 Track::pending_overwrite () const
575 {
576         return _diskstream->pending_overwrite ();
577 }
578
579 double
580 Track::speed () const
581 {
582         return _diskstream->speed ();
583 }
584
585 void
586 Track::prepare_to_stop (framepos_t p)
587 {
588         _diskstream->prepare_to_stop (p);
589 }
590
591 void
592 Track::set_slaved (bool s)
593 {
594         _diskstream->set_slaved (s);
595 }
596
597 ChanCount
598 Track::n_channels ()
599 {
600         return _diskstream->n_channels ();
601 }
602
603 framepos_t
604 Track::get_capture_start_frame (uint32_t n) const
605 {
606         return _diskstream->get_capture_start_frame (n);
607 }
608
609 AlignStyle
610 Track::alignment_style () const
611 {
612         return _diskstream->alignment_style ();
613 }
614
615 AlignChoice
616 Track::alignment_choice () const
617 {
618         return _diskstream->alignment_choice ();
619 }
620
621 framepos_t
622 Track::current_capture_start () const
623 {
624         return _diskstream->current_capture_start ();
625 }
626
627 framepos_t
628 Track::current_capture_end () const
629 {
630         return _diskstream->current_capture_end ();
631 }
632
633 void
634 Track::playlist_modified ()
635 {
636         _diskstream->playlist_modified ();
637 }
638
639 int
640 Track::use_playlist (boost::shared_ptr<Playlist> p)
641 {
642         return _diskstream->use_playlist (p);
643 }
644
645 int
646 Track::use_copy_playlist ()
647 {
648         return _diskstream->use_copy_playlist ();
649 }
650
651 int
652 Track::use_new_playlist ()
653 {
654         return _diskstream->use_new_playlist ();
655 }
656
657 void
658 Track::set_align_style (AlignStyle s, bool force)
659 {
660         _diskstream->set_align_style (s, force);
661 }
662
663 void
664 Track::set_align_choice (AlignChoice s, bool force)
665 {
666         _diskstream->set_align_choice (s, force);
667 }
668
669 PBD::ID const &
670 Track::diskstream_id () const
671 {
672         return _diskstream->id ();
673 }
674
675 void
676 Track::set_block_size (pframes_t n)
677 {
678         Route::set_block_size (n);
679         _diskstream->set_block_size (n);
680 }
681
682 void
683 Track::adjust_playback_buffering ()
684 {
685         if (_diskstream) {
686                 _diskstream->adjust_playback_buffering ();
687         }
688 }
689
690 void
691 Track::adjust_capture_buffering ()
692 {
693         if (_diskstream) {
694                 _diskstream->adjust_capture_buffering ();
695         }
696 }
697
698 bool
699 Track::send_silence () const
700 {
701         bool send_silence;
702
703         if (Config->get_tape_machine_mode()) {
704
705                 /* ADATs work in a strange way..
706                    they monitor input always when stopped.and auto-input is engaged.
707                 */
708                 
709                 if ((Config->get_monitoring_model() == SoftwareMonitoring)
710                     && ((_monitoring & MonitorInput) || (_diskstream->record_enabled()))) {
711                         send_silence = false;
712                 } else {
713                         send_silence = true;
714                 }
715                 
716                 
717         } else {
718                 
719                 /* Other machines switch to input on stop if the track is record enabled,
720                    regardless of the auto input setting (auto input only changes the
721                    monitoring state when the transport is rolling)
722                 */
723                 
724                 if ((Config->get_monitoring_model() == SoftwareMonitoring)
725                     && ((_monitoring & MonitorInput) || 
726                         (!(_monitoring & MonitorDisk) && (_session.config.get_auto_input () || _diskstream->record_enabled())))){
727
728                         DEBUG_TRACE (DEBUG::Monitor, 
729                                      string_compose ("%1: no roll, use silence = FALSE,  monitoring choice %2 recenable %3 sRA %4 autoinput %5\n",
730                                                      name(), enum_2_string (_monitoring), 
731                                                      _diskstream->record_enabled(), _session.actively_recording(),
732                                                      _session.config.get_auto_input()));
733
734                         send_silence = false;
735                 } else {
736                         DEBUG_TRACE (DEBUG::Monitor, 
737                                      string_compose ("%1: no roll, use silence = TRUE,  monitoring choice %2 recenable %3 sRA %4 autoinput %5\n",
738                                                      name(), enum_2_string (_monitoring), 
739                                                      _diskstream->record_enabled(), _session.actively_recording(),
740                                                      _session.config.get_auto_input()));
741                         send_silence = true;
742                 }
743         }
744
745         return send_silence;
746 }
747
748 MonitorState
749 Track::monitoring_state ()
750 {
751         MonitorState ms = MonitoringSilence;
752
753         if (_session.transport_rolling()) {
754                 
755                 /* roll case */
756                 
757                 if (_monitoring & MonitorInput) { // explicitly requested input monitoring
758                         
759                         ms = MonitoringInput;
760
761                 } else if (_monitoring & MonitorDisk) { // explicitly requested disk monitoring
762                         
763                         ms = MonitoringDisk;
764
765                 } else if (_diskstream->record_enabled() && _session.actively_recording()) { // Track actually recording
766                         
767                         ms = MonitoringInput;
768
769                 } else if (_diskstream->record_enabled() && !_session.actively_recording() && _session.config.get_auto_input()) { // Track armed but not recording, with auto input enabled
770                         
771                         ms = MonitoringInput;
772
773                 } else { // Every other state
774                         
775                         ms = MonitoringDisk; 
776
777                 }
778
779         } else {
780
781                 /* no-roll case */
782
783                 if (send_silence()) {
784                         
785                         ms = MonitoringSilence;
786                 } else {
787                         
788                         ms = MonitoringInput;
789                 }
790         }
791
792         return ms;
793 }
794
795 void
796 Track::maybe_declick (BufferSet& bufs, framecnt_t nframes, int declick)
797 {
798         /* never declick if there is an internal generator - we just want it to
799            keep generating sound without interruption.
800
801            ditto if we are monitoring inputs.
802         */
803
804         if (_have_internal_generator || monitoring_choice() == MonitorInput) {
805                 return;
806         }
807
808         if (!declick) {
809                 declick = _pending_declick;
810         }
811
812         if (declick != 0) {
813                 Amp::declick (bufs, nframes, declick);
814         }
815 }
816
817 framecnt_t
818 Track::check_initial_delay (framecnt_t nframes, framecnt_t& transport_frame)
819 {
820         if (_roll_delay > nframes) {
821
822                 _roll_delay -= nframes;
823                 silence_unlocked (nframes);
824                 /* transport frame is not legal for caller to use */
825                 return 0;
826
827         } else if (_roll_delay > 0) {
828
829                 nframes -= _roll_delay;
830                 silence_unlocked (_roll_delay);
831                 transport_frame += _roll_delay;
832
833                 /* shuffle all the port buffers for things that lead "out" of this Route
834                    to reflect that we just wrote _roll_delay frames of silence.
835                 */
836
837                 Glib::RWLock::ReaderLock lm (_processor_lock);
838                 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
839                         boost::shared_ptr<IOProcessor> iop = boost::dynamic_pointer_cast<IOProcessor> (*i);
840                         if (iop) {
841                                 iop->increment_port_buffer_offset (_roll_delay);
842                         }
843                 }
844                 _output->increment_port_buffer_offset (_roll_delay);
845
846                 _roll_delay = 0;
847
848         }
849
850         return nframes; 
851 }
852
853 void
854 Track::set_monitoring (MonitorChoice mc)
855 {
856         if (mc !=  _monitoring) {
857                 _monitoring = mc;
858                 MonitoringChanged (); /* EMIT SIGNAL */
859         }
860 }
861