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