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