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