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