fix monitoring so that MIDI tracks don't work the same way as audio (basically, they...
[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         , _rec_enable_control (new RecEnableControllable(*this))
47 {
48         _freeze_record.state = NoFreeze;
49         _declickable = true;
50 }
51
52 Track::~Track ()
53 {
54         DEBUG_TRACE (DEBUG::Destruction, string_compose ("track %1 destructor\n", _name));
55 }
56
57 int
58 Track::init ()
59 {
60         if (Route::init ()) {
61                 return -1;
62         }
63
64         return 0;
65 }
66 XMLNode&
67 Track::get_state ()
68 {
69         return state (true);
70 }
71
72
73
74 XMLNode&
75 Track::get_template ()
76 {
77         return state (false);
78 }
79
80 void
81 Track::toggle_monitor_input ()
82 {
83         for (PortSet::iterator i = _input->ports().begin(); i != _input->ports().end(); ++i) {
84                 i->ensure_monitor_input(!i->monitoring_input());
85         }
86 }
87
88 ARDOUR::framecnt_t
89 Track::update_total_latency ()
90 {
91         framecnt_t old = _output->effective_latency();
92         framecnt_t own_latency = _output->user_latency();
93
94         for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
95                 if ((*i)->active ()) {
96                         own_latency += (*i)->signal_latency ();
97                 }
98         }
99
100 #undef DEBUG_LATENCY
101 #ifdef DEBUG_LATENCY
102         cerr << _name << ": internal redirect (final) latency = " << own_latency << endl;
103 #endif
104
105         _output->set_port_latency (own_latency);
106
107         if (old != own_latency) {
108                 _output->set_latency_delay (own_latency);
109                 signal_latency_changed (); /* EMIT SIGNAL */
110         }
111
112         return _output->effective_latency();
113 }
114
115 Track::FreezeRecord::~FreezeRecord ()
116 {
117         for (vector<FreezeRecordProcessorInfo*>::iterator i = processor_info.begin(); i != processor_info.end(); ++i) {
118                 delete *i;
119         }
120 }
121
122 Track::FreezeState
123 Track::freeze_state() const
124 {
125         return _freeze_record.state;
126 }
127
128 Track::RecEnableControllable::RecEnableControllable (Track& s)
129         : Controllable (X_("recenable")), track (s)
130 {
131 }
132
133 void
134 Track::RecEnableControllable::set_value (double val)
135 {
136         bool bval = ((val >= 0.5) ? true: false);
137         track.set_record_enabled (bval, this);
138 }
139
140 double
141 Track::RecEnableControllable::get_value (void) const
142 {
143         if (track.record_enabled()) { return 1.0; }
144         return 0.0;
145 }
146
147 bool
148 Track::record_enabled () const
149 {
150         return _diskstream && _diskstream->record_enabled ();
151 }
152
153 bool
154 Track::can_record()
155 {
156         bool will_record = true;
157         for (PortSet::iterator i = _input->ports().begin(); i != _input->ports().end() && will_record; ++i) {
158                 if (!i->connected())
159                         will_record = false;
160         }
161
162         return will_record;
163 }
164
165 void
166 Track::set_record_enabled (bool yn, void *src)
167 {
168         if (!_session.writable()) {
169                 return;
170         }
171
172         if (_freeze_record.state == Frozen) {
173                 return;
174         }
175
176         if (_route_group && src != _route_group && _route_group->is_active() && _route_group->is_recenable()) {
177                 _route_group->apply (&Track::set_record_enabled, yn, _route_group);
178                 return;
179         }
180
181         /* keep track of the meter point as it was before we rec-enabled */
182         if (!_diskstream->record_enabled()) {
183                 _saved_meter_point = _meter_point;
184         }
185
186         _diskstream->set_record_enabled (yn);
187
188         if (_diskstream->record_enabled()) {
189                 if (_meter_point != MeterCustom) {
190                         set_meter_point (MeterInput);
191                 }
192         } else {
193                 set_meter_point (_saved_meter_point);
194         }
195
196         _rec_enable_control->Changed ();
197 }
198
199
200 bool
201 Track::set_name (const string& str)
202 {
203         bool ret;
204
205         if (record_enabled() && _session.actively_recording()) {
206                 /* this messes things up if done while recording */
207                 return false;
208         }
209
210         if (_diskstream->set_name (str)) {
211                 return false;
212         }
213
214         /* save state so that the statefile fully reflects any filename changes */
215
216         if ((ret = Route::set_name (str)) == 0) {
217                 _session.save_state ("");
218         }
219
220         return ret;
221 }
222
223 void
224 Track::set_latency_delay (framecnt_t longest_session_latency)
225 {
226         Route::set_latency_delay (longest_session_latency);
227         _diskstream->set_roll_delay (_roll_delay);
228 }
229
230 void
231 Track::zero_diskstream_id_in_xml (XMLNode& node)
232 {
233         if (node.property ("diskstream-id")) {
234                 node.add_property ("diskstream-id", "0");
235         }
236 }
237
238 int
239 Track::no_roll (pframes_t nframes, framepos_t start_frame, framepos_t end_frame,
240                 bool session_state_changing, bool can_record, bool /*rec_monitors_input*/)
241 {
242         Glib::RWLock::ReaderLock lm (_processor_lock, Glib::TRY_LOCK);
243         if (!lm.locked()) {
244                 return 0;
245         }
246
247         if (n_outputs().n_total() == 0) {
248                 return 0;
249         }
250
251         if (!_active) {
252                 silence (nframes);
253                 return 0;
254         }
255
256         if (session_state_changing) {
257                 if (_session.transport_speed() != 0.0f) {
258                         /* we're rolling but some state is changing (e.g. our diskstream contents)
259                            so we cannot use them. Be silent till this is over. Don't declick.
260
261                            XXX note the absurdity of ::no_roll() being called when we ARE rolling!
262                         */
263                         passthru_silence (start_frame, end_frame, nframes, 0);
264                         return 0;
265                 }
266                 /* we're really not rolling, so we're either delivery silence or actually
267                    monitoring, both of which are safe to do while session_state_changing is true.
268                 */
269         }
270
271         _diskstream->check_record_status (start_frame, can_record);
272
273         bool be_silent;
274
275         if (_have_internal_generator) {
276                 /* since the instrument has no input streams,
277                    there is no reason to send any signal
278                    into the route.
279                 */
280                 be_silent = true;
281         } else {
282                 be_silent = send_silence ();
283         }
284
285         _amp->apply_gain_automation(false);
286
287         if (be_silent) {
288
289                 /* if we're sending silence, but we want the meters to show levels for the signal,
290                    meter right here.
291                 */
292
293                 if (_have_internal_generator) {
294                         passthru_silence (start_frame, end_frame, nframes, 0);
295                 } else {
296                         if (_meter_point == MeterInput) {
297                                 _input->process_input (_meter, start_frame, end_frame, nframes);
298                         }
299                         passthru_silence (start_frame, end_frame, nframes, 0);
300                 }
301
302         } else {
303
304                 /* we're sending signal, but we may still want to meter the input.
305                  */
306
307                 passthru (start_frame, end_frame, nframes, false);
308         }
309
310         _main_outs->flush_buffers (nframes, end_frame - start_frame - 1);
311
312         return 0;
313 }
314
315 int
316 Track::silent_roll (pframes_t nframes, framepos_t /*start_frame*/, framepos_t /*end_frame*/,
317                     bool can_record, bool rec_monitors_input, bool& need_butler)
318 {
319         Glib::RWLock::ReaderLock lm (_processor_lock, Glib::TRY_LOCK);
320         if (!lm.locked()) {
321                 return 0;
322         }
323
324         if (n_outputs().n_total() == 0 && _processors.empty()) {
325                 return 0;
326         }
327
328         if (!_active) {
329                 silence (nframes);
330                 return 0;
331         }
332
333         _silent = true;
334         _amp->apply_gain_automation(false);
335
336         silence (nframes);
337
338         return _diskstream->process (_session.transport_frame(), nframes, can_record, rec_monitors_input, need_butler);
339 }
340
341 void
342 Track::set_diskstream (boost::shared_ptr<Diskstream> ds)
343 {
344         _diskstream = ds;
345         
346         ds->PlaylistChanged.connect_same_thread (*this, boost::bind (&Track::diskstream_playlist_changed, this));
347         diskstream_playlist_changed ();
348         ds->RecordEnableChanged.connect_same_thread (*this, boost::bind (&Track::diskstream_record_enable_changed, this));
349         ds->SpeedChanged.connect_same_thread (*this, boost::bind (&Track::diskstream_speed_changed, this));
350         ds->AlignmentStyleChanged.connect_same_thread (*this, boost::bind (&Track::diskstream_alignment_style_changed, this));
351 }
352
353 void
354 Track::diskstream_playlist_changed ()
355 {
356         PlaylistChanged (); /* EMIT SIGNAL */
357 }
358
359 void
360 Track::diskstream_record_enable_changed ()
361 {
362         RecordEnableChanged (); /* EMIT SIGNAL */
363 }
364
365 void
366 Track::diskstream_speed_changed ()
367 {
368         SpeedChanged (); /* EMIT SIGNAL */
369 }
370
371 void
372 Track::diskstream_alignment_style_changed ()
373 {
374         AlignmentStyleChanged (); /* EMIT SIGNAL */
375 }
376
377 boost::shared_ptr<Playlist>
378 Track::playlist ()
379 {
380         return _diskstream->playlist ();
381 }
382
383 void
384 Track::monitor_input (bool m)
385 {
386         _diskstream->monitor_input (m);
387 }
388
389 bool
390 Track::destructive () const
391 {
392         return _diskstream->destructive ();
393 }
394
395 list<boost::shared_ptr<Source> > &
396 Track::last_capture_sources ()
397 {
398         return _diskstream->last_capture_sources ();
399 }
400
401 void
402 Track::set_capture_offset ()
403 {
404         _diskstream->set_capture_offset ();
405 }
406
407 list<boost::shared_ptr<Source> > 
408 Track::steal_write_sources()
409 {
410         return _diskstream->steal_write_sources ();
411 }
412
413 void
414 Track::reset_write_sources (bool r, bool force)
415 {
416         _diskstream->reset_write_sources (r, force);
417 }
418
419 float
420 Track::playback_buffer_load () const
421 {
422         return _diskstream->playback_buffer_load ();
423 }
424
425 float
426 Track::capture_buffer_load () const
427 {
428         return _diskstream->capture_buffer_load ();
429 }
430
431 int
432 Track::do_refill ()
433 {
434         return _diskstream->do_refill ();
435 }
436
437 int
438 Track::do_flush (RunContext c, bool force)
439 {
440         return _diskstream->do_flush (c, force);
441 }
442
443 void
444 Track::set_pending_overwrite (bool o)
445 {
446         _diskstream->set_pending_overwrite (o);
447 }
448
449 int
450 Track::seek (framepos_t p, bool complete_refill)
451 {
452         return _diskstream->seek (p, complete_refill);
453 }
454
455 bool
456 Track::hidden () const
457 {
458         return _diskstream->hidden ();
459 }
460
461 int
462 Track::can_internal_playback_seek (framepos_t p)
463 {
464         return _diskstream->can_internal_playback_seek (p);
465 }
466
467 int
468 Track::internal_playback_seek (framepos_t p)
469 {
470         return _diskstream->internal_playback_seek (p);
471 }
472
473 void
474 Track::non_realtime_input_change ()
475 {
476         _diskstream->non_realtime_input_change ();
477 }
478
479 void
480 Track::non_realtime_locate (framepos_t p)
481 {
482         _diskstream->non_realtime_locate (p);
483 }
484
485 void
486 Track::non_realtime_set_speed ()
487 {
488         _diskstream->non_realtime_set_speed ();
489 }
490
491 int
492 Track::overwrite_existing_buffers ()
493 {
494         return _diskstream->overwrite_existing_buffers ();
495 }
496
497 framecnt_t
498 Track::get_captured_frames (uint32_t n) const
499 {
500         return _diskstream->get_captured_frames (n);
501 }
502
503 int
504 Track::set_loop (Location* l)
505 {
506         return _diskstream->set_loop (l);
507 }
508
509 void
510 Track::transport_looped (framepos_t p)
511 {
512         _diskstream->transport_looped (p);
513 }
514
515 bool
516 Track::realtime_set_speed (double s, bool g)
517 {
518         return _diskstream->realtime_set_speed (s, g);
519 }
520
521 void
522 Track::transport_stopped_wallclock (struct tm & n, time_t t, bool g)
523 {
524         _diskstream->transport_stopped_wallclock (n, t, g);
525 }
526
527 bool
528 Track::pending_overwrite () const
529 {
530         return _diskstream->pending_overwrite ();
531 }
532
533 double
534 Track::speed () const
535 {
536         return _diskstream->speed ();
537 }
538
539 void
540 Track::prepare_to_stop (framepos_t p)
541 {
542         _diskstream->prepare_to_stop (p);
543 }
544
545 void
546 Track::set_slaved (bool s)
547 {
548         _diskstream->set_slaved (s);
549 }
550
551 ChanCount
552 Track::n_channels ()
553 {
554         return _diskstream->n_channels ();
555 }
556
557 framepos_t
558 Track::get_capture_start_frame (uint32_t n) const
559 {
560         return _diskstream->get_capture_start_frame (n);
561 }
562
563 AlignStyle
564 Track::alignment_style () const
565 {
566         return _diskstream->alignment_style ();
567 }
568
569 framepos_t
570 Track::current_capture_start () const
571 {
572         return _diskstream->current_capture_start ();
573 }
574
575 framepos_t
576 Track::current_capture_end () const
577 {
578         return _diskstream->current_capture_end ();
579 }
580
581 void
582 Track::playlist_modified ()
583 {
584         _diskstream->playlist_modified ();
585 }
586
587 int
588 Track::use_playlist (boost::shared_ptr<Playlist> p)
589 {
590         return _diskstream->use_playlist (p);
591 }
592
593 int
594 Track::use_copy_playlist ()
595 {
596         return _diskstream->use_copy_playlist ();
597 }
598
599 int
600 Track::use_new_playlist ()
601 {
602         return _diskstream->use_new_playlist ();
603 }
604
605 uint32_t
606 Track::read_data_count () const
607 {
608         return _diskstream->read_data_count ();
609 }
610
611 void
612 Track::set_align_style (AlignStyle s)
613 {
614         _diskstream->set_align_style (s);
615 }
616
617 uint32_t
618 Track::write_data_count () const
619 {
620         return _diskstream->write_data_count ();
621 }
622
623 PBD::ID const &
624 Track::diskstream_id () const
625 {
626         return _diskstream->id ();
627 }
628
629 void
630 Track::set_block_size (pframes_t n)
631 {
632         Route::set_block_size (n);
633         _diskstream->set_block_size (n);
634 }
635
636 void 
637 Track::adjust_playback_buffering ()
638 {
639         if (_diskstream) {
640                 _diskstream->adjust_playback_buffering ();
641         }
642 }
643
644 void 
645 Track::adjust_capture_buffering ()
646 {
647         if (_diskstream) {
648                 _diskstream->adjust_capture_buffering ();
649         }
650 }
651
652 bool
653 Track::send_silence () const
654 {
655         /*
656           ADATs work in a strange way..
657           they monitor input always when stopped.and auto-input is engaged.
658
659           Other machines switch to input on stop if the track is record enabled,
660           regardless of the auto input setting (auto input only changes the
661           monitoring state when the transport is rolling)
662         */
663
664         bool send_silence;
665
666         if (!Config->get_tape_machine_mode()) {
667                 /*
668                   ADATs work in a strange way..
669                   they monitor input always when stopped.and auto-input is engaged.
670                 */
671                 if ((Config->get_monitoring_model() == SoftwareMonitoring)
672                     && (_session.config.get_auto_input () || _diskstream->record_enabled())) {
673                         send_silence = false;
674                 } else {
675                         send_silence = true;
676                 }
677         } else {
678                 /*
679                   Other machines switch to input on stop if the track is record enabled,
680                   regardless of the auto input setting (auto input only changes the
681                   monitoring state when the transport is rolling)
682                 */
683                 if ((Config->get_monitoring_model() == SoftwareMonitoring)
684                     && _diskstream->record_enabled()) {
685                         send_silence = false;
686                 } else {
687                         send_silence = true;
688                 }
689         }
690
691         return send_silence;
692 }