More non-rt calls to VCA - fixes VCA automation writing
[ardour.git] / libs / ardour / session_transport.cc
1 /*
2     Copyright (C) 1999-2003 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 */
19
20 #ifdef WAF_BUILD
21 #include "libardour-config.h"
22 #endif
23
24 #include <cmath>
25 #include <cerrno>
26 #include <unistd.h>
27
28 #include "pbd/undo.h"
29 #include "pbd/error.h"
30 #include "pbd/enumwriter.h"
31 #include "pbd/pthread_utils.h"
32 #include "pbd/memento_command.h"
33 #include "pbd/stacktrace.h"
34
35 #include "midi++/mmc.h"
36 #include "midi++/port.h"
37
38 #include "ardour/audioengine.h"
39 #include "ardour/auditioner.h"
40 #include "ardour/automation_watch.h"
41 #include "ardour/butler.h"
42 #include "ardour/click.h"
43 #include "ardour/debug.h"
44 #include "ardour/location.h"
45 #include "ardour/profile.h"
46 #include "ardour/scene_changer.h"
47 #include "ardour/session.h"
48 #include "ardour/slave.h"
49 #include "ardour/tempo.h"
50 #include "ardour/operations.h"
51 #include "ardour/vca.h"
52 #include "ardour/vca_manager.h"
53
54 #include "pbd/i18n.h"
55
56 using namespace std;
57 using namespace ARDOUR;
58 using namespace PBD;
59
60 void
61 Session::add_post_transport_work (PostTransportWork ptw)
62 {
63         PostTransportWork oldval;
64         PostTransportWork newval;
65         int tries = 0;
66
67         while (tries < 8) {
68                 oldval = (PostTransportWork) g_atomic_int_get (&_post_transport_work);
69                 newval = PostTransportWork (oldval | ptw);
70                 if (g_atomic_int_compare_and_exchange (&_post_transport_work, oldval, newval)) {
71                         /* success */
72                         return;
73                 }
74         }
75
76         error << "Could not set post transport work! Crazy thread madness, call the programmers" << endmsg;
77 }
78
79 void
80 Session::request_input_change_handling ()
81 {
82         if (!(_state_of_the_state & (InitialConnecting|Deletion))) {
83                 SessionEvent* ev = new SessionEvent (SessionEvent::InputConfigurationChange, SessionEvent::Add, SessionEvent::Immediate, 0, 0.0);
84                 queue_event (ev);
85         }
86 }
87
88 void
89 Session::request_sync_source (Slave* new_slave)
90 {
91         SessionEvent* ev = new SessionEvent (SessionEvent::SetSyncSource, SessionEvent::Add, SessionEvent::Immediate, 0, 0.0);
92         bool seamless;
93
94         seamless = Config->get_seamless_loop ();
95
96         if (dynamic_cast<Engine_Slave*>(new_slave)) {
97                 /* JACK cannot support seamless looping at present */
98                 Config->set_seamless_loop (false);
99         } else {
100                 /* reset to whatever the value was before we last switched slaves */
101                 Config->set_seamless_loop (_was_seamless);
102         }
103
104         /* save value of seamless from before the switch */
105         _was_seamless = seamless;
106
107         ev->slave = new_slave;
108         DEBUG_TRACE (DEBUG::Slave, "sent request for new slave\n");
109         queue_event (ev);
110 }
111
112 void
113 Session::request_transport_speed (double speed, bool as_default)
114 {
115         SessionEvent* ev = new SessionEvent (SessionEvent::SetTransportSpeed, SessionEvent::Add, SessionEvent::Immediate, 0, speed);
116         ev->third_yes_or_no = as_default; // as_default
117         DEBUG_TRACE (DEBUG::Transport, string_compose ("Request transport speed = %1 as default = %2\n", speed, as_default));
118         queue_event (ev);
119 }
120
121 /** Request a new transport speed, but if the speed parameter is exactly zero then use
122  *  a very small +ve value to prevent the transport actually stopping.  This method should
123  *  be used by callers who are varying transport speed but don't ever want to stop it.
124  */
125 void
126 Session::request_transport_speed_nonzero (double speed, bool as_default)
127 {
128         if (speed == 0) {
129                 speed = DBL_EPSILON;
130         }
131
132         request_transport_speed (speed, as_default);
133 }
134
135 void
136 Session::request_track_speed (Track* tr, double speed)
137 {
138         SessionEvent* ev = new SessionEvent (SessionEvent::SetTrackSpeed, SessionEvent::Add, SessionEvent::Immediate, 0, speed);
139         ev->set_ptr (tr);
140         queue_event (ev);
141 }
142
143 void
144 Session::request_stop (bool abort, bool clear_state)
145 {
146         SessionEvent* ev = new SessionEvent (SessionEvent::SetTransportSpeed, SessionEvent::Add, SessionEvent::Immediate, audible_frame(), 0.0, abort, clear_state);
147         DEBUG_TRACE (DEBUG::Transport, string_compose ("Request transport stop, audible %3 transport %4 abort = %1, clear state = %2\n", abort, clear_state, audible_frame(), _transport_frame));
148         queue_event (ev);
149 }
150
151 void
152 Session::request_locate (framepos_t target_frame, bool with_roll)
153 {
154         SessionEvent *ev = new SessionEvent (with_roll ? SessionEvent::LocateRoll : SessionEvent::Locate, SessionEvent::Add, SessionEvent::Immediate, target_frame, 0, false);
155         DEBUG_TRACE (DEBUG::Transport, string_compose ("Request locate to %1\n", target_frame));
156         queue_event (ev);
157 }
158
159 void
160 Session::force_locate (framepos_t target_frame, bool with_roll)
161 {
162         SessionEvent *ev = new SessionEvent (with_roll ? SessionEvent::LocateRoll : SessionEvent::Locate, SessionEvent::Add, SessionEvent::Immediate, target_frame, 0, true);
163         DEBUG_TRACE (DEBUG::Transport, string_compose ("Request forced locate to %1\n", target_frame));
164         queue_event (ev);
165 }
166
167 void
168 Session::unset_preroll_record_punch ()
169 {
170         if (_preroll_record_punch_pos >= 0) {
171                 remove_event (_preroll_record_punch_pos, SessionEvent::RecordStart);
172         }
173         _preroll_record_punch_pos = -1;
174 }
175
176 void
177 Session::unset_preroll_record_trim ()
178 {
179         _preroll_record_trim_len = 0;
180 }
181
182 void
183 Session::request_preroll_record_punch (framepos_t rec_in, framecnt_t preroll)
184 {
185         if (actively_recording ()) {
186                 return;
187         }
188         unset_preroll_record_punch ();
189         unset_preroll_record_trim ();
190         framepos_t start = std::max ((framepos_t)0, rec_in - preroll);
191
192         _preroll_record_punch_pos = rec_in;
193         if (_preroll_record_punch_pos >= 0) {
194                 replace_event (SessionEvent::RecordStart, _preroll_record_punch_pos);
195                 config.set_punch_in (false);
196                 config.set_punch_out (false);
197         }
198         maybe_enable_record ();
199         request_locate (start, true);
200         set_requested_return_frame (rec_in);
201 }
202
203 void
204 Session::request_preroll_record_trim (framepos_t rec_in, framecnt_t preroll)
205 {
206         if (actively_recording ()) {
207                 return;
208         }
209         unset_preroll_record_punch ();
210         unset_preroll_record_trim ();
211
212         config.set_punch_in (false);
213         config.set_punch_out (false);
214
215         framepos_t pos = std::max ((framepos_t)0, rec_in - preroll);
216         _preroll_record_trim_len = preroll;
217         maybe_enable_record ();
218         request_locate (pos, true);
219         set_requested_return_frame (rec_in);
220 }
221
222 void
223 Session::request_count_in_record ()
224 {
225         if (actively_recording ()) {
226                 return;
227         }
228         if (transport_rolling()) {
229                 return;
230         }
231         maybe_enable_record ();
232         _count_in_once = true;
233         request_transport_speed (1.0, true);
234 }
235
236 void
237 Session::request_play_loop (bool yn, bool change_transport_roll)
238 {
239         if (_slave && yn) {
240                 // don't attempt to loop when not using Internal Transport
241                 // see also gtk2_ardour/ardour_ui_options.cc parameter_changed()
242                 return;
243         }
244
245         SessionEvent* ev;
246         Location *location = _locations->auto_loop_location();
247         double target_speed;
248
249         if (location == 0 && yn) {
250                 error << _("Cannot loop - no loop range defined")
251                       << endmsg;
252                 return;
253         }
254
255         if (change_transport_roll) {
256                 if (transport_rolling()) {
257                         /* start looping at current speed */
258                         target_speed = transport_speed ();
259                 } else {
260                         /* currently stopped */
261                         if (yn) {
262                                 /* start looping at normal speed */
263                                 target_speed = 1.0;
264                         } else {
265                                 target_speed = 0.0;
266                         }
267                 }
268         } else {
269                 /* leave the speed alone */
270                 target_speed = transport_speed ();
271         }
272
273         ev = new SessionEvent (SessionEvent::SetLoop, SessionEvent::Add, SessionEvent::Immediate, 0, target_speed, yn);
274         DEBUG_TRACE (DEBUG::Transport, string_compose ("Request set loop = %1, change roll state ? %2\n", yn, change_transport_roll));
275         queue_event (ev);
276
277         if (yn) {
278                 if (!change_transport_roll) {
279                         if (!transport_rolling()) {
280                                 /* we're not changing transport state, but we do want
281                                    to set up position for the new loop. Don't
282                                    do this if we're rolling already.
283                                 */
284                                 request_locate (location->start(), false);
285                         }
286                 }
287         } else {
288                 if (!change_transport_roll && Config->get_seamless_loop() && transport_rolling()) {
289                         // request an immediate locate to refresh the tracks
290                         // after disabling looping
291                         request_locate (_transport_frame-1, false);
292                 }
293         }
294 }
295
296 void
297 Session::request_play_range (list<AudioRange>* range, bool leave_rolling)
298 {
299         SessionEvent* ev = new SessionEvent (SessionEvent::SetPlayAudioRange, SessionEvent::Add, SessionEvent::Immediate, 0, (leave_rolling ? 1.0 : 0.0));
300         if (range) {
301                 ev->audio_range = *range;
302         } else {
303                 ev->audio_range.clear ();
304         }
305         DEBUG_TRACE (DEBUG::Transport, string_compose ("Request play range, leave rolling ? %1\n", leave_rolling));
306         queue_event (ev);
307 }
308
309 void
310 Session::request_cancel_play_range ()
311 {
312         SessionEvent* ev = new SessionEvent (SessionEvent::CancelPlayAudioRange, SessionEvent::Add, SessionEvent::Immediate, 0, 0);
313         queue_event (ev);
314 }
315
316
317 void
318 Session::realtime_stop (bool abort, bool clear_state)
319 {
320         DEBUG_TRACE (DEBUG::Transport, string_compose ("realtime stop @ %1\n", _transport_frame));
321         PostTransportWork todo = PostTransportWork (0);
322
323         /* assume that when we start, we'll be moving forwards */
324
325         if (_transport_speed < 0.0f) {
326                 todo = (PostTransportWork (todo | PostTransportStop | PostTransportReverse));
327                 _default_transport_speed = 1.0;
328         } else {
329                 todo = PostTransportWork (todo | PostTransportStop);
330         }
331
332         /* call routes */
333
334         boost::shared_ptr<RouteList> r = routes.reader ();
335
336         for (RouteList::iterator i = r->begin (); i != r->end(); ++i) {
337                 (*i)->realtime_handle_transport_stopped ();
338         }
339
340         DEBUG_TRACE (DEBUG::Transport, string_compose ("stop complete, auto-return scheduled for return to %1\n", _requested_return_frame));
341
342         /* the duration change is not guaranteed to have happened, but is likely */
343
344         todo = PostTransportWork (todo | PostTransportDuration);
345
346         if (abort) {
347                 todo = PostTransportWork (todo | PostTransportAbort);
348         }
349
350         if (clear_state) {
351                 todo = PostTransportWork (todo | PostTransportClearSubstate);
352         }
353
354         if (todo) {
355                 add_post_transport_work (todo);
356         }
357
358         _clear_event_type (SessionEvent::StopOnce);
359         _clear_event_type (SessionEvent::RangeStop);
360         _clear_event_type (SessionEvent::RangeLocate);
361
362         /* if we're going to clear loop state, then force disabling record BUT only if we're not doing latched rec-enable */
363         disable_record (true, (!Config->get_latched_record_enable() && clear_state));
364
365         if (clear_state && !Config->get_loop_is_mode()) {
366                 unset_play_loop ();
367         }
368
369         reset_slave_state ();
370
371         _transport_speed = 0;
372         _target_transport_speed = 0;
373
374         g_atomic_int_set (&_playback_load, 100);
375         g_atomic_int_set (&_capture_load, 100);
376
377         if (config.get_use_video_sync()) {
378                 waiting_for_sync_offset = true;
379         }
380
381         transport_sub_state = 0;
382 }
383
384 void
385 Session::realtime_locate ()
386 {
387         boost::shared_ptr<RouteList> r = routes.reader ();
388         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
389                 (*i)->realtime_locate ();
390         }
391 }
392
393 void
394 Session::butler_transport_work ()
395 {
396         /* Note: this function executes in the butler thread context */
397
398   restart:
399         bool finished;
400         PostTransportWork ptw;
401         boost::shared_ptr<RouteList> r = routes.reader ();
402         uint64_t before;
403
404         int on_entry = g_atomic_int_get (&_butler->should_do_transport_work);
405         finished = true;
406         ptw = post_transport_work();
407
408         DEBUG_TRACE (DEBUG::Transport, string_compose ("Butler transport work, todo = %1 at %2\n", enum_2_string (ptw), (before = g_get_monotonic_time())));
409
410
411         if (ptw & PostTransportLocate) {
412
413                 if (get_play_loop() && !Config->get_seamless_loop()) {
414
415                         DEBUG_TRACE (DEBUG::Butler, "flush loop recording fragment to disk\n");
416
417                         /* this locate might be happening while we are
418                          * loop recording.
419                          *
420                          * Non-seamless looping will require a locate (below) that
421                          * will reset capture buffers and throw away data.
422                          *
423                          * Rather than first find all tracks and see if they
424                          * have outstanding data, just do a flush anyway. It
425                          * may be cheaper this way anyway, and is certainly
426                          * more accurate.
427                          */
428
429                         bool more_disk_io_to_do = false;
430                         uint32_t errors = 0;
431
432                         do {
433                                 more_disk_io_to_do = _butler->flush_tracks_to_disk_after_locate (r, errors);
434
435                                 if (errors) {
436                                         break;
437                                 }
438
439                                 if (more_disk_io_to_do) {
440                                         continue;
441                                 }
442
443                         } while (false);
444
445                 }
446         }
447
448         if (ptw & PostTransportAdjustPlaybackBuffering) {
449                 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
450                         boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
451                         if (tr) {
452                                 tr->adjust_playback_buffering ();
453                                 /* and refill those buffers ... */
454                         }
455                         (*i)->non_realtime_locate (_transport_frame);
456                 }
457                 VCAList v = _vca_manager->vcas ();
458                 for (VCAList::const_iterator i = v.begin(); i != v.end(); ++i) {
459                         (*i)->transport_located (_transport_frame);
460                 }
461         }
462
463         if (ptw & PostTransportAdjustCaptureBuffering) {
464                 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
465                         boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
466                         if (tr) {
467                                 tr->adjust_capture_buffering ();
468                         }
469                 }
470         }
471
472         if (ptw & PostTransportCurveRealloc) {
473                 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
474                         (*i)->curve_reallocate();
475                 }
476         }
477
478         if (ptw & PostTransportInputChange) {
479                 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
480                         boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
481                         if (tr) {
482                                 tr->non_realtime_input_change ();
483                         }
484                 }
485         }
486
487         if (ptw & PostTransportSpeed) {
488                 non_realtime_set_speed ();
489         }
490
491         if (ptw & PostTransportReverse) {
492
493                 clear_clicks();
494                 cumulative_rf_motion = 0;
495                 reset_rf_scale (0);
496
497                 /* don't seek if locate will take care of that in non_realtime_stop() */
498
499                 if (!(ptw & PostTransportLocate)) {
500                         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
501                                 (*i)->non_realtime_locate (_transport_frame);
502
503                                 if (on_entry != g_atomic_int_get (&_butler->should_do_transport_work)) {
504                                         /* new request, stop seeking, and start again */
505                                         g_atomic_int_dec_and_test (&_butler->should_do_transport_work);
506                                         goto restart;
507                                 }
508                         }
509                         VCAList v = _vca_manager->vcas ();
510                         for (VCAList::const_iterator i = v.begin(); i != v.end(); ++i) {
511                                 (*i)->transport_located (_transport_frame);
512                         }
513                 }
514         }
515
516         if (ptw & PostTransportLocate) {
517                 DEBUG_TRACE (DEBUG::Transport, "nonrealtime locate invoked from BTW\n");
518                 non_realtime_locate ();
519         }
520
521         if (ptw & PostTransportStop) {
522                 non_realtime_stop (ptw & PostTransportAbort, on_entry, finished);
523                 if (!finished) {
524                         g_atomic_int_dec_and_test (&_butler->should_do_transport_work);
525                         goto restart;
526                 }
527         }
528
529         if (ptw & PostTransportOverWrite) {
530                 non_realtime_overwrite (on_entry, finished);
531                 if (!finished) {
532                         g_atomic_int_dec_and_test (&_butler->should_do_transport_work);
533                         goto restart;
534                 }
535         }
536
537         if (ptw & PostTransportAudition) {
538                 non_realtime_set_audition ();
539         }
540
541         g_atomic_int_dec_and_test (&_butler->should_do_transport_work);
542
543         DEBUG_TRACE (DEBUG::Transport, string_compose (X_("Butler transport work all done after %1 usecs\n"), g_get_monotonic_time() - before));
544         DEBUG_TRACE (DEBUG::Transport, X_(string_compose ("Frame %1\n", _transport_frame)));
545 }
546
547 void
548 Session::non_realtime_set_speed ()
549 {
550         boost::shared_ptr<RouteList> rl = routes.reader();
551         for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
552                 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
553                 if (tr) {
554                         tr->non_realtime_set_speed ();
555                 }
556         }
557 }
558
559 void
560 Session::non_realtime_overwrite (int on_entry, bool& finished)
561 {
562         boost::shared_ptr<RouteList> rl = routes.reader();
563         for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
564                 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
565                 if (tr && tr->pending_overwrite ()) {
566                         tr->overwrite_existing_buffers ();
567                 }
568                 if (on_entry != g_atomic_int_get (&_butler->should_do_transport_work)) {
569                         finished = false;
570                         return;
571                 }
572         }
573 }
574
575
576 void
577 Session::non_realtime_locate ()
578 {
579         DEBUG_TRACE (DEBUG::Transport, string_compose ("locate tracks to %1\n", _transport_frame));
580
581         if (Config->get_loop_is_mode() && get_play_loop()) {
582
583                 Location *loc  = _locations->auto_loop_location();
584
585                 if (!loc || (_transport_frame < loc->start() || _transport_frame >= loc->end())) {
586                         /* jumped out of loop range: stop tracks from looping,
587                            but leave loop (mode) enabled.
588                          */
589                         set_track_loop (false);
590
591                 } else if (loc && Config->get_seamless_loop() &&
592                    ((loc->start() <= _transport_frame) ||
593                    (loc->end() > _transport_frame) ) ) {
594
595                         /* jumping to start of loop. This  might have been done before but it is
596                          * idempotent and cheap. Doing it here ensures that when we start playback
597                          * outside the loop we still flip tracks into the magic seamless mode
598                          * when needed.
599                          */
600                         set_track_loop (true);
601
602                 } else if (loc) {
603                         set_track_loop (false);
604                 }
605
606         } else {
607
608                 /* no more looping .. should have been noticed elsewhere */
609         }
610
611
612         {
613                 boost::shared_ptr<RouteList> rl = routes.reader();
614                 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
615                         (*i)->non_realtime_locate (_transport_frame);
616                 }
617         }
618         {
619                 VCAList v = _vca_manager->vcas ();
620                 for (VCAList::const_iterator i = v.begin(); i != v.end(); ++i) {
621                         (*i)->transport_located (_transport_frame);
622                 }
623         }
624
625         _scene_changer->locate (_transport_frame);
626
627         /* XXX: it would be nice to generate the new clicks here (in the non-RT thread)
628            rather than clearing them so that the RT thread has to spend time constructing
629            them (in Session::click).
630          */
631         clear_clicks ();
632 }
633
634 #ifdef USE_TRACKS_CODE_FEATURES
635 bool
636 Session::select_playhead_priority_target (framepos_t& jump_to)
637 {
638         jump_to = -1;
639
640         AutoReturnTarget autoreturn = Config->get_auto_return_target_list ();
641
642         if (!autoreturn) {
643                 return false;
644         }
645
646         if (Profile->get_trx() && transport_rolling() ) {
647                 // We're playing, so do nothing.
648                 // Next stop will put us where we need to be.
649                 return false;
650         }
651
652         /* Note that the order of checking each AutoReturnTarget flag defines
653            the priority each flag.
654
655            Ardour/Mixbus: Last Locate
656                           Range Selection
657                           Loop Range
658                           Region Selection
659
660            Tracks:        Range Selection
661                           Loop Range
662                           Region Selection
663                           Last Locate
664         */
665
666         if (autoreturn & RangeSelectionStart) {
667                 if (!_range_selection.empty()) {
668                         jump_to = _range_selection.from;
669                 } else {
670                         if (transport_rolling ()) {
671                                 /* Range selection no longer exists, but we're playing,
672                                    so do nothing. Next stop will put us where
673                                    we need to be.
674                                 */
675                                 return false;
676                         }
677                 }
678         }
679
680         if (jump_to < 0 && (autoreturn & Loop) && get_play_loop()) {
681                 /* don't try to handle loop play when synced to JACK */
682
683                 if (!synced_to_engine()) {
684                         Location *location = _locations->auto_loop_location();
685
686                         if (location) {
687                                 jump_to = location->start();
688
689                                 if (Config->get_seamless_loop()) {
690                                         /* need to get track buffers reloaded */
691                                         set_track_loop (true);
692                                 }
693                         }
694                 }
695         }
696
697         if (jump_to < 0 && (autoreturn & RegionSelectionStart)) {
698                 if (!_object_selection.empty()) {
699                         jump_to = _object_selection.from;
700                 }
701         }
702
703         if (jump_to < 0 && (autoreturn & LastLocate)) {
704                 jump_to = _last_roll_location;
705         }
706
707         return jump_to >= 0;
708 }
709 #else
710
711 bool
712 Session::select_playhead_priority_target (framepos_t& jump_to)
713 {
714         if (config.get_external_sync() || !config.get_auto_return()) {
715                 return false;
716         }
717
718         jump_to = _last_roll_location;
719         return jump_to >= 0;
720 }
721
722 #endif
723
724 void
725 Session::follow_playhead_priority ()
726 {
727         framepos_t target;
728
729         if (select_playhead_priority_target (target)) {
730                 request_locate (target);
731         }
732 }
733
734 void
735 Session::non_realtime_stop (bool abort, int on_entry, bool& finished)
736 {
737         struct tm* now;
738         time_t     xnow;
739         bool       did_record;
740         bool       saved;
741         PostTransportWork ptw = post_transport_work();
742
743         did_record = false;
744         saved = false;
745
746         boost::shared_ptr<RouteList> rl = routes.reader();
747         for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
748                 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
749                 if (tr && tr->get_captured_frames () != 0) {
750                         did_record = true;
751                         break;
752                 }
753         }
754
755         /* stop and locate are merged here because they share a lot of common stuff */
756
757         time (&xnow);
758         now = localtime (&xnow);
759
760         if (auditioner) {
761                 auditioner->cancel_audition ();
762         }
763
764         cumulative_rf_motion = 0;
765         reset_rf_scale (0);
766
767         if (did_record) {
768                 begin_reversible_command (Operations::capture);
769                 _have_captured = true;
770         }
771
772         DEBUG_TRACE (DEBUG::Transport, X_("Butler PTW: DS stop\n"));
773
774         if (abort && did_record) {
775                 /* no reason to save the session file when we remove sources
776                  */
777                 _state_of_the_state = StateOfTheState (_state_of_the_state|InCleanup);
778         }
779
780         for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
781                 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
782                 if (tr) {
783                         tr->transport_stopped_wallclock (*now, xnow, abort);
784                 }
785         }
786
787         if (abort && did_record) {
788                 _state_of_the_state = StateOfTheState (_state_of_the_state & ~InCleanup);
789         }
790
791         boost::shared_ptr<RouteList> r = routes.reader ();
792
793         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
794                 if (!(*i)->is_auditioner()) {
795                         (*i)->set_pending_declick (0);
796                 }
797         }
798
799         if (did_record) {
800                 commit_reversible_command ();
801                 /* increase take name */
802                 if (config.get_track_name_take () && !config.get_take_name ().empty()) {
803                         string newname = config.get_take_name();
804                         config.set_take_name(bump_name_number (newname));
805                 }
806         }
807
808         if (_engine.running()) {
809                 PostTransportWork ptw = post_transport_work ();
810
811                 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
812                         (*i)->nonrealtime_handle_transport_stopped (abort, (ptw & PostTransportLocate), (!(ptw & PostTransportLocate) || pending_locate_flush));
813                 }
814                 VCAList v = _vca_manager->vcas ();
815                 for (VCAList::const_iterator i = v.begin(); i != v.end(); ++i) {
816                         (*i)->transport_stopped (_transport_frame);
817                 }
818
819                 update_latency_compensation ();
820         }
821
822         bool const auto_return_enabled = (!config.get_external_sync() && (Config->get_auto_return_target_list() || abort));
823
824         if (auto_return_enabled ||
825             (ptw & PostTransportLocate) ||
826             (_requested_return_frame >= 0) ||
827             synced_to_engine()) {
828
829                 if (pending_locate_flush) {
830                         flush_all_inserts ();
831                 }
832
833                 // rg: what is the logic behind this case?
834                 // _requested_return_frame should be ignored when synced_to_engine/slaved.
835                 // currently worked around in MTC_Slave by forcing _requested_return_frame to -1
836                 // 2016-01-10
837                 if ((auto_return_enabled || synced_to_engine() || _requested_return_frame >= 0) &&
838                     !(ptw & PostTransportLocate)) {
839
840                         /* no explicit locate queued */
841
842                         bool do_locate = false;
843
844                         if (_requested_return_frame >= 0) {
845
846                                 /* explicit return request pre-queued in event list. overrides everything else */
847
848                                 _transport_frame = _requested_return_frame;
849                                 do_locate = true;
850
851                         } else {
852                                 framepos_t jump_to;
853
854                                 if (select_playhead_priority_target (jump_to)) {
855
856                                         _transport_frame = jump_to;
857                                         do_locate = true;
858
859                                 } else if (abort) {
860
861                                         _transport_frame = _last_roll_location;
862                                         do_locate = true;
863                                 }
864                         }
865
866                         _requested_return_frame = -1;
867
868                         if (do_locate) {
869                                 _engine.transport_locate (_transport_frame);
870                         }
871                 }
872
873         }
874
875         clear_clicks();
876         unset_preroll_record_trim ();
877
878         /* do this before seeking, because otherwise the tracks will do the wrong thing in seamless loop mode.
879         */
880
881         if (ptw & PostTransportClearSubstate) {
882                 unset_play_range ();
883                 if (!Config->get_loop_is_mode()) {
884                         unset_play_loop ();
885                 }
886         }
887
888         /* this for() block can be put inside the previous if() and has the effect of ... ??? what */
889
890         {
891                 DEBUG_TRACE (DEBUG::Transport, X_("Butler PTW: locate\n"));
892                 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
893                         DEBUG_TRACE (DEBUG::Transport, string_compose ("Butler PTW: locate on %1\n", (*i)->name()));
894                         (*i)->non_realtime_locate (_transport_frame);
895
896                         if (on_entry != g_atomic_int_get (&_butler->should_do_transport_work)) {
897                                 finished = false;
898                                 /* we will be back */
899                                 return;
900                         }
901                 }
902         }
903
904         {
905                 VCAList v = _vca_manager->vcas ();
906                 for (VCAList::const_iterator i = v.begin(); i != v.end(); ++i) {
907                         (*i)->transport_located (_transport_frame);
908                 }
909         }
910
911         have_looped = false;
912
913         /* don't bother with this stuff if we're disconnected from the engine,
914            because there will be no process callbacks to deliver stuff from
915         */
916
917         if (_engine.connected() && !_engine.freewheeling()) {
918                 // need to queue this in the next RT cycle
919                 _send_timecode_update = true;
920
921                 if (!dynamic_cast<MTC_Slave*>(_slave)) {
922                         send_immediate_mmc (MIDI::MachineControlCommand (MIDI::MachineControl::cmdStop));
923
924                         /* This (::non_realtime_stop()) gets called by main
925                            process thread, which will lead to confusion
926                            when calling AsyncMIDIPort::write().
927
928                            Something must be done. XXX
929                         */
930                         send_mmc_locate (_transport_frame);
931                 }
932         }
933
934         if ((ptw & PostTransportLocate) && get_record_enabled()) {
935                 /* This is scheduled by realtime_stop(), which is also done
936                  * when a slave requests /locate/ for an initial sync.
937                  * We can't hold up the slave for long with a save() here,
938                  * without breaking its initial sync cycle.
939                  *
940                  * save state only if there's no slave or if it's not yet locked.
941                  */
942                 if (!_slave || !_slave->locked()) {
943                         DEBUG_TRACE (DEBUG::Transport, X_("Butler PTW: requests save\n"));
944                         SaveSessionRequested (_current_snapshot_name);
945                         saved = true;
946                 }
947         }
948
949         /* always try to get rid of this */
950
951         remove_pending_capture_state ();
952
953         /* save the current state of things if appropriate */
954
955         if (did_record && !saved) {
956                 SaveSessionRequested (_current_snapshot_name);
957         }
958
959         if (ptw & PostTransportStop) {
960                 unset_play_range ();
961                 if (!Config->get_loop_is_mode()) {
962                         unset_play_loop ();
963                 }
964         }
965
966         PositionChanged (_transport_frame); /* EMIT SIGNAL */
967         DEBUG_TRACE (DEBUG::Transport, string_compose ("send TSC with speed = %1\n", _transport_speed));
968         TransportStateChange (); /* EMIT SIGNAL */
969         AutomationWatch::instance().transport_stop_automation_watches (_transport_frame);
970
971         /* and start it up again if relevant */
972
973         if ((ptw & PostTransportLocate) && !config.get_external_sync() && pending_locate_roll) {
974                 request_transport_speed (1.0);
975         }
976
977         /* Even if we didn't do a pending locate roll this time, we don't want it hanging
978            around for next time.
979         */
980         pending_locate_roll = false;
981 }
982
983 void
984 Session::check_declick_out ()
985 {
986         bool locate_required = transport_sub_state & PendingLocate;
987
988         /* this is called after a process() iteration. if PendingDeclickOut was set,
989            it means that we were waiting to declick the output (which has just been
990            done) before maybe doing something else. this is where we do that "something else".
991
992            note: called from the audio thread.
993         */
994
995         if (transport_sub_state & PendingDeclickOut) {
996
997                 if (locate_required) {
998                         start_locate (pending_locate_frame, pending_locate_roll, pending_locate_flush);
999                         transport_sub_state &= ~(PendingDeclickOut|PendingLocate);
1000                 } else {
1001                         if (!(transport_sub_state & StopPendingCapture)) {
1002                                 stop_transport (pending_abort);
1003                                 transport_sub_state &= ~(PendingDeclickOut|PendingLocate);
1004                         }
1005                 }
1006
1007         } else if (transport_sub_state & PendingLoopDeclickOut) {
1008                 /* Nothing else to do here; we've declicked, and the loop event will be along shortly */
1009                 transport_sub_state &= ~PendingLoopDeclickOut;
1010         }
1011 }
1012
1013 void
1014 Session::unset_play_loop ()
1015 {
1016         if (play_loop) {
1017                 play_loop = false;
1018                 clear_events (SessionEvent::AutoLoop);
1019                 clear_events (SessionEvent::AutoLoopDeclick);
1020                 set_track_loop (false);
1021
1022
1023                 if (Config->get_seamless_loop()) {
1024                         /* likely need to flush track buffers: this will locate us to wherever we are */
1025                         add_post_transport_work (PostTransportLocate);
1026                         _butler->schedule_transport_work ();
1027                 }
1028         }
1029 }
1030
1031 void
1032 Session::set_track_loop (bool yn)
1033 {
1034         Location* loc = _locations->auto_loop_location ();
1035
1036         if (!loc) {
1037                 yn = false;
1038         }
1039
1040         boost::shared_ptr<RouteList> rl = routes.reader ();
1041
1042         for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
1043                 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
1044                 if (tr && !tr->hidden()) {
1045                         tr->set_loop (yn ? loc : 0);
1046                 }
1047         }
1048 }
1049
1050 void
1051 Session::set_play_loop (bool yn, double speed)
1052 {
1053         /* Called from event-handling context */
1054
1055         Location *loc;
1056
1057         if (yn == play_loop || (actively_recording() && yn) || (loc = _locations->auto_loop_location()) == 0) {
1058                 /* nothing to do, or can't change loop status while recording */
1059                 return;
1060         }
1061
1062         if (yn && Config->get_seamless_loop() && synced_to_engine()) {
1063                 warning << string_compose (
1064                         _("Seamless looping cannot be supported while %1 is using JACK transport.\n"
1065                           "Recommend changing the configured options"), PROGRAM_NAME)
1066                         << endmsg;
1067                 return;
1068         }
1069
1070         if (yn) {
1071
1072                 play_loop = true;
1073                 have_looped = false;
1074
1075                 if (loc) {
1076
1077                         unset_play_range ();
1078
1079                         if (Config->get_seamless_loop()) {
1080                                 if (!Config->get_loop_is_mode()) {
1081                                         /* set all tracks to use internal looping */
1082                                         set_track_loop (true);
1083                                 } else {
1084                                         /* we will do this in the locate to the start OR when we hit the end
1085                                          * of the loop for the first time
1086                                          */
1087                                 }
1088                         } else {
1089                                 /* set all tracks to NOT use internal looping */
1090                                 set_track_loop (false);
1091                         }
1092
1093                         /* Put the delick and loop events in into the event list.  The declick event will
1094                            cause a de-clicking fade-out just before the end of the loop, and it will also result
1095                            in a fade-in when the loop restarts.  The AutoLoop event will peform the actual loop.
1096                         */
1097
1098                         framepos_t dcp;
1099                         framecnt_t dcl;
1100                         auto_loop_declick_range (loc, dcp, dcl);
1101                         merge_event (new SessionEvent (SessionEvent::AutoLoopDeclick, SessionEvent::Replace, dcp, dcl, 0.0f));
1102                         merge_event (new SessionEvent (SessionEvent::AutoLoop, SessionEvent::Replace, loc->end(), loc->start(), 0.0f));
1103
1104                         /* if requested to roll, locate to start of loop and
1105                          * roll but ONLY if we're not already rolling.
1106
1107                            args: positition, roll=true, flush=true, with_loop=false, force buffer refill if seamless looping
1108                         */
1109
1110                         if (Config->get_loop_is_mode()) {
1111                                 /* loop IS a transport mode: if already
1112                                    rolling, do not locate to loop start.
1113                                 */
1114                                 if (!transport_rolling() && (speed != 0.0)) {
1115                                         start_locate (loc->start(), true, true, false, true);
1116                                 }
1117                         } else {
1118                                 if (speed != 0.0) {
1119                                         start_locate (loc->start(), true, true, false, true);
1120                                 }
1121                         }
1122                 }
1123
1124         } else {
1125
1126                 unset_play_loop ();
1127         }
1128
1129         DEBUG_TRACE (DEBUG::Transport, string_compose ("send TSC2 with speed = %1\n", _transport_speed));
1130         TransportStateChange ();
1131 }
1132 void
1133 Session::flush_all_inserts ()
1134 {
1135         boost::shared_ptr<RouteList> r = routes.reader ();
1136
1137         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
1138                 (*i)->flush_processors ();
1139         }
1140 }
1141
1142 void
1143 Session::start_locate (framepos_t target_frame, bool with_roll, bool with_flush, bool for_loop_enabled, bool force)
1144 {
1145         if (target_frame < 0) {
1146                 error << _("Locate called for negative sample position - ignored") << endmsg;
1147                 return;
1148         }
1149
1150         if (synced_to_engine()) {
1151
1152                 double sp;
1153                 framepos_t pos;
1154
1155                 _slave->speed_and_position (sp, pos);
1156
1157                 if (target_frame != pos) {
1158
1159                         if (config.get_jack_time_master()) {
1160                                 /* actually locate now, since otherwise jack_timebase_callback
1161                                    will use the incorrect _transport_frame and report an old
1162                                    and incorrect time to Jack transport
1163                                 */
1164                                 locate (target_frame, with_roll, with_flush, for_loop_enabled, force);
1165                         }
1166
1167                         /* tell JACK to change transport position, and we will
1168                            follow along later in ::follow_slave()
1169                         */
1170
1171                         _engine.transport_locate (target_frame);
1172
1173                         if (sp != 1.0f && with_roll) {
1174                                 _engine.transport_start ();
1175                         }
1176
1177                 }
1178
1179         } else {
1180                 locate (target_frame, with_roll, with_flush, for_loop_enabled, force);
1181         }
1182 }
1183
1184 int
1185 Session::micro_locate (framecnt_t distance)
1186 {
1187         boost::shared_ptr<RouteList> rl = routes.reader();
1188         for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
1189                 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
1190                 if (tr && !tr->can_internal_playback_seek (distance)) {
1191                         return -1;
1192                 }
1193         }
1194
1195         for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
1196                 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
1197                 if (tr) {
1198                         tr->internal_playback_seek (distance);
1199                 }
1200         }
1201
1202         _transport_frame += distance;
1203         return 0;
1204 }
1205
1206 /** @param with_mmc true to send a MMC locate command when the locate is done */
1207 void
1208 Session::locate (framepos_t target_frame, bool with_roll, bool with_flush, bool for_loop_enabled, bool force, bool with_mmc)
1209 {
1210         bool need_butler = false;
1211
1212         /* Locates for seamless looping are fairly different from other
1213          * locates. They assume that the diskstream buffers for each track
1214          * already have the correct data in them, and thus there is no need to
1215          * actually tell the tracks to locate. What does need to be done,
1216          * though, is all the housekeeping that is associated with non-linear
1217          * changes in the value of _transport_frame.
1218          */
1219
1220         DEBUG_TRACE (DEBUG::Transport, string_compose ("rt-locate to %1, roll %2 flush %3 loop-enabled %4 force %5 mmc %6\n",
1221                                                        target_frame, with_roll, with_flush, for_loop_enabled, force, with_mmc));
1222
1223         if (!force && _transport_frame == target_frame && !loop_changing && !for_loop_enabled) {
1224
1225                 /* already at the desired position. Not forced to locate,
1226                    the loop isn't changing, so unless we're told to
1227                    start rolling also, there's nothing to do but
1228                    tell the world where we are (again).
1229                 */
1230
1231                 if (with_roll) {
1232                         set_transport_speed (1.0, 0, false);
1233                 }
1234                 loop_changing = false;
1235                 Located (); /* EMIT SIGNAL */
1236                 return;
1237         }
1238
1239         if (_transport_speed && !(for_loop_enabled && Config->get_seamless_loop())) {
1240                 /* Schedule a declick.  We'll be called again when its done.
1241                    We only do it this way for ordinary locates, not those
1242                    due to **seamless** loops.
1243                 */
1244
1245                 if (!(transport_sub_state & PendingDeclickOut)) {
1246                         transport_sub_state |= (PendingDeclickOut|PendingLocate);
1247                         pending_locate_frame = target_frame;
1248                         pending_locate_roll = with_roll;
1249                         pending_locate_flush = with_flush;
1250                         return;
1251                 }
1252         }
1253
1254         // Update Timecode time
1255         _transport_frame = target_frame;
1256         _last_roll_or_reversal_location = target_frame;
1257         timecode_time(_transport_frame, transmitting_timecode_time);
1258
1259         /* do "stopped" stuff if:
1260          *
1261          * we are rolling AND
1262          *    no autoplay in effect AND
1263          *       we're not going to keep rolling after the locate AND
1264          *           !(playing a loop with JACK sync)
1265          *
1266          */
1267
1268         bool transport_was_stopped = !transport_rolling();
1269
1270         if (!transport_was_stopped && (!auto_play_legal || !config.get_auto_play()) && !with_roll && !(synced_to_engine() && play_loop) &&
1271             (!Profile->get_trx() || !(config.get_external_sync() && !synced_to_engine()))) {
1272                 realtime_stop (false, true); // XXX paul - check if the 2nd arg is really correct
1273                 transport_was_stopped = true;
1274         } else {
1275                 /* otherwise tell the world that we located */
1276                 realtime_locate ();
1277         }
1278
1279         if (force || !for_loop_enabled || loop_changing) {
1280
1281                 PostTransportWork todo = PostTransportLocate;
1282
1283                 if (with_roll && transport_was_stopped) {
1284                         todo = PostTransportWork (todo | PostTransportRoll);
1285                 }
1286
1287                 add_post_transport_work (todo);
1288                 need_butler = true;
1289
1290         } else {
1291
1292                 /* this is functionally what clear_clicks() does but with a tentative lock */
1293
1294                 Glib::Threads::RWLock::WriterLock clickm (click_lock, Glib::Threads::TRY_LOCK);
1295
1296                 if (clickm.locked()) {
1297
1298                         for (Clicks::iterator i = clicks.begin(); i != clicks.end(); ++i) {
1299                                 delete *i;
1300                         }
1301
1302                         clicks.clear ();
1303                 }
1304         }
1305
1306         if (with_roll) {
1307                 /* switch from input if we're going to roll */
1308                 if (Config->get_monitoring_model() == HardwareMonitoring) {
1309                         set_track_monitor_input_status (!config.get_auto_input());
1310                 }
1311         } else {
1312                 /* otherwise we're going to stop, so do the opposite */
1313                 if (Config->get_monitoring_model() == HardwareMonitoring) {
1314                         set_track_monitor_input_status (true);
1315                 }
1316         }
1317
1318         /* cancel looped playback if transport pos outside of loop range */
1319         if (play_loop) {
1320
1321                 Location* al = _locations->auto_loop_location();
1322
1323                 if (al) {
1324                         if (_transport_frame < al->start() || _transport_frame >= al->end()) {
1325
1326                                 // located outside the loop: cancel looping directly, this is called from event handling context
1327
1328                                 have_looped = false;
1329
1330                                 if (!Config->get_loop_is_mode()) {
1331                                         set_play_loop (false, _transport_speed);
1332                                 } else {
1333                                         if (Config->get_seamless_loop()) {
1334                                                 /* this will make the non_realtime_locate() in the butler
1335                                                    which then causes seek() in tracks actually do the right
1336                                                    thing.
1337                                                 */
1338                                                 set_track_loop (false);
1339                                         }
1340                                 }
1341
1342                         } else if (_transport_frame == al->start()) {
1343
1344                                 // located to start of loop - this is looping, basically
1345
1346                                 if (!have_looped) {
1347                                         /* first time */
1348                                         if (_last_roll_location != al->start()) {
1349                                                 /* didn't start at loop start - playback must have
1350                                                  * started before loop since we've now hit the loop
1351                                                  * end.
1352                                                  */
1353                                                 add_post_transport_work (PostTransportLocate);
1354                                                 need_butler = true;
1355                                         }
1356
1357                                 }
1358
1359                                 boost::shared_ptr<RouteList> rl = routes.reader();
1360
1361                                 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
1362                                         boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
1363
1364                                         if (tr && tr->rec_enable_control()->get_value()) {
1365                                                 // tell it we've looped, so it can deal with the record state
1366                                                 tr->transport_looped (_transport_frame);
1367                                         }
1368                                 }
1369
1370                                 have_looped = true;
1371                                 TransportLooped(); // EMIT SIGNAL
1372                         }
1373                 }
1374         }
1375
1376         if (need_butler) {
1377                 _butler->schedule_transport_work ();
1378         }
1379
1380         loop_changing = false;
1381
1382         _send_timecode_update = true;
1383
1384         if (with_mmc) {
1385                 send_mmc_locate (_transport_frame);
1386         }
1387
1388         _last_roll_location = _last_roll_or_reversal_location =  _transport_frame;
1389         if (!synced_to_engine () || _transport_frame == _engine.transport_frame ()) {
1390                 Located (); /* EMIT SIGNAL */
1391         }
1392 }
1393
1394 /** Set the transport speed.
1395  *  Called from the process thread.
1396  *  @param speed New speed
1397  */
1398 void
1399 Session::set_transport_speed (double speed, framepos_t destination_frame, bool abort, bool clear_state, bool as_default)
1400 {
1401         DEBUG_TRACE (DEBUG::Transport, string_compose ("@ %5 Set transport speed to %1, abort = %2 clear_state = %3, current = %4 as_default %6\n",
1402                                                        speed, abort, clear_state, _transport_speed, _transport_frame, as_default));
1403
1404         if (_transport_speed == speed) {
1405                 if (as_default && speed == 0.0) { // => reset default transport speed. hacky or what?
1406                         _default_transport_speed = 1.0;
1407                 }
1408                 return;
1409         }
1410
1411         if (actively_recording() && speed != 1.0 && speed != 0.0) {
1412                 /* no varispeed during recording */
1413                 DEBUG_TRACE (DEBUG::Transport, string_compose ("No varispeed during recording cur_speed %1, frame %2\n",
1414                                                        _transport_speed, _transport_frame));
1415                 return;
1416         }
1417
1418         _target_transport_speed = fabs(speed);
1419
1420         /* 8.0 max speed is somewhat arbitrary but based on guestimates regarding disk i/o capability
1421            and user needs. We really need CD-style "skip" playback for ffwd and rewind.
1422         */
1423
1424         if (speed > 0) {
1425                 speed = min (8.0, speed);
1426         } else if (speed < 0) {
1427                 speed = max (-8.0, speed);
1428         }
1429
1430         if (transport_rolling() && speed == 0.0) {
1431
1432                 /* we are rolling and we want to stop */
1433
1434                 if (Config->get_monitoring_model() == HardwareMonitoring) {
1435                         set_track_monitor_input_status (true);
1436                 }
1437
1438                 if (synced_to_engine ()) {
1439                         if (clear_state) {
1440                                 /* do this here because our response to the slave won't
1441                                    take care of it.
1442                                 */
1443                                 _play_range = false;
1444                                 _count_in_once = false;
1445                                 unset_play_loop ();
1446                         }
1447                         _engine.transport_stop ();
1448                 } else {
1449                         bool const auto_return_enabled = (!config.get_external_sync() && (Config->get_auto_return_target_list() || abort));
1450
1451                         if (!auto_return_enabled) {
1452                                 _requested_return_frame = destination_frame;
1453                         }
1454
1455                         stop_transport (abort);
1456                 }
1457
1458         } else if (transport_stopped() && speed == 1.0) {
1459                 if (as_default) {
1460                         _default_transport_speed = speed;
1461                 }
1462                 /* we are stopped and we want to start rolling at speed 1 */
1463
1464                 if (Config->get_loop_is_mode() && play_loop) {
1465
1466                         Location *location = _locations->auto_loop_location();
1467
1468                         if (location != 0) {
1469                                 if (_transport_frame != location->start()) {
1470
1471                                         if (Config->get_seamless_loop()) {
1472                                                 /* force tracks to do their thing */
1473                                                 set_track_loop (true);
1474                                         }
1475
1476                                         /* jump to start and then roll from there */
1477
1478                                         request_locate (location->start(), true);
1479                                         return;
1480                                 }
1481                         }
1482                 }
1483
1484                 if (Config->get_monitoring_model() == HardwareMonitoring && config.get_auto_input()) {
1485                         set_track_monitor_input_status (false);
1486                 }
1487
1488                 if (synced_to_engine()) {
1489                         _engine.transport_start ();
1490                         _count_in_once = false;
1491                 } else {
1492                         start_transport ();
1493                 }
1494
1495         } else {
1496
1497                 /* not zero, not 1.0 ... varispeed */
1498
1499                 if ((synced_to_engine()) && speed != 0.0 && speed != 1.0) {
1500                         warning << string_compose (
1501                                 _("Global varispeed cannot be supported while %1 is connected to JACK transport control"),
1502                                 PROGRAM_NAME)
1503                                 << endmsg;
1504                         return;
1505                 }
1506
1507                 if (actively_recording()) {
1508                         return;
1509                 }
1510
1511                 if (speed > 0.0 && _transport_frame == current_end_frame()) {
1512                         return;
1513                 }
1514
1515                 if (speed < 0.0 && _transport_frame == 0) {
1516                         return;
1517                 }
1518
1519                 clear_clicks ();
1520
1521                 /* if we are reversing relative to the current speed, or relative to the speed
1522                    before the last stop, then we have to do extra work.
1523                 */
1524
1525                 PostTransportWork todo = PostTransportWork (0);
1526
1527                 if ((_transport_speed && speed * _transport_speed < 0.0) || (_last_transport_speed * speed < 0.0) || (_last_transport_speed == 0.0 && speed < 0.0)) {
1528                         todo = PostTransportWork (todo | PostTransportReverse);
1529                         _last_roll_or_reversal_location = _transport_frame;
1530                 }
1531
1532                 _last_transport_speed = _transport_speed;
1533                 _transport_speed = speed;
1534
1535                 if (as_default) {
1536                         _default_transport_speed = speed;
1537                 }
1538
1539                 boost::shared_ptr<RouteList> rl = routes.reader();
1540                 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
1541                         boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
1542                         if (tr && tr->realtime_set_speed (tr->speed(), true)) {
1543                                 todo = PostTransportWork (todo | PostTransportSpeed);
1544                         }
1545                 }
1546
1547                 if (todo) {
1548                         add_post_transport_work (todo);
1549                         _butler->schedule_transport_work ();
1550                 }
1551
1552                 DEBUG_TRACE (DEBUG::Transport, string_compose ("send TSC3 with speed = %1\n", _transport_speed));
1553
1554                 /* throttle signal emissions.
1555                  * when slaved [_last]_transport_speed
1556                  * usually changes every cycle (tiny amounts due to DLL).
1557                  * Emitting a signal every cycle is overkill and unwarranted.
1558                  *
1559                  * Using _last_transport_speed is not acceptable,
1560                  * since it allows for large changes over a long period
1561                  * of time. Hence we introduce a dedicated variable to keep track
1562                  *
1563                  * The 0.2% dead-zone is somewhat arbitrary. Main use-case
1564                  * for TransportStateChange() here is the ShuttleControl display.
1565                  */
1566                 if (fabs (_signalled_varispeed - speed) > .002
1567                     // still, signal hard changes to 1.0 and 0.0:
1568                     || ( speed == 1.0 && _signalled_varispeed != 1.0)
1569                     || ( speed == 0.0 && _signalled_varispeed != 0.0)
1570                    )
1571                 {
1572                         TransportStateChange (); /* EMIT SIGNAL */
1573                         _signalled_varispeed = speed;
1574                 }
1575         }
1576 }
1577
1578
1579 /** Stop the transport.  */
1580 void
1581 Session::stop_transport (bool abort, bool clear_state)
1582 {
1583         _count_in_once = false;
1584         if (_transport_speed == 0.0f) {
1585                 return;
1586         }
1587
1588         DEBUG_TRACE (DEBUG::Transport, string_compose ("stop_transport, declick required? %1\n", get_transport_declick_required()));
1589
1590         if (!get_transport_declick_required()) {
1591
1592                 /* stop has not yet been scheduled */
1593
1594                 boost::shared_ptr<RouteList> rl = routes.reader();
1595                 framepos_t stop_target = audible_frame();
1596
1597                 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
1598                         boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
1599                         if (tr) {
1600                                 tr->prepare_to_stop (_transport_frame, stop_target);
1601                         }
1602                 }
1603
1604                 SubState new_bits;
1605
1606                 if (actively_recording() &&                           /* we are recording */
1607                     worst_input_latency() > current_block_size) {     /* input latency exceeds block size, so simple 1 cycle delay before stop is not enough */
1608
1609                         /* we need to capture the audio that is still somewhere in the pipeline between
1610                            wherever it was generated and the process callback. This means that even though
1611                            the user (or something else)  has asked us to stop, we have to roll
1612                            past this point and then reset the playhead/transport location to
1613                            the position at which the stop was requested.
1614
1615                            we still need playback to "stop" now, however, which is why we schedule
1616                            a declick below.
1617                         */
1618
1619                         DEBUG_TRACE (DEBUG::Transport, string_compose ("stop transport requested @ %1, scheduled for + %2 = %3, abort = %4\n",
1620                                                                        _transport_frame, _worst_input_latency,
1621                                                                        _transport_frame + _worst_input_latency,
1622                                                                        abort));
1623
1624                         SessionEvent *ev = new SessionEvent (SessionEvent::StopOnce, SessionEvent::Replace,
1625                                                              _transport_frame + _worst_input_latency,
1626                                                              0, 0, abort);
1627
1628                         merge_event (ev);
1629
1630                         /* request a declick at the start of the next process cycle() so that playback ceases.
1631                            It will remain silent until we actually stop (at the StopOnce event somewhere in
1632                            the future). The extra flag (StopPendingCapture) is set to ensure that check_declick_out()
1633                            does not stop the transport too early.
1634                          */
1635                         new_bits = SubState (PendingDeclickOut|StopPendingCapture);
1636
1637                 } else {
1638
1639                         /* Not recording, schedule a declick in the next process() cycle and then stop at its end */
1640
1641                         new_bits = PendingDeclickOut;
1642                         DEBUG_TRACE (DEBUG::Transport, string_compose ("stop scheduled for next process cycle @ %1\n", _transport_frame));
1643                 }
1644
1645                 /* we'll be called again after the declick */
1646                 transport_sub_state = SubState (transport_sub_state|new_bits);
1647                 pending_abort = abort;
1648
1649                 return;
1650
1651         } else {
1652
1653                 DEBUG_TRACE (DEBUG::Transport, "time to actually stop\n");
1654
1655                 /* declick was scheduled, but we've been called again, which means it is really time to stop
1656
1657                    XXX: we should probably split this off into its own method and call it explicitly.
1658                 */
1659
1660                 realtime_stop (abort, clear_state);
1661                 _butler->schedule_transport_work ();
1662         }
1663 }
1664
1665 /** Called from the process thread */
1666 void
1667 Session::start_transport ()
1668 {
1669         DEBUG_TRACE (DEBUG::Transport, "start_transport\n");
1670
1671         _last_roll_location = _transport_frame;
1672         _last_roll_or_reversal_location = _transport_frame;
1673
1674         have_looped = false;
1675
1676         /* if record status is Enabled, move it to Recording. if its
1677            already Recording, move it to Disabled.
1678         */
1679
1680         switch (record_status()) {
1681         case Enabled:
1682                 if (!config.get_punch_in() && !preroll_record_punch_enabled()) {
1683                         enable_record ();
1684                 }
1685                 break;
1686
1687         case Recording:
1688                 if (!play_loop) {
1689                         disable_record (false);
1690                 }
1691                 break;
1692
1693         default:
1694                 break;
1695         }
1696
1697         transport_sub_state |= PendingDeclickIn;
1698
1699         _transport_speed = _default_transport_speed;
1700         _target_transport_speed = _transport_speed;
1701
1702         boost::shared_ptr<RouteList> rl = routes.reader();
1703         for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
1704                 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
1705                 if (tr) {
1706                         tr->realtime_set_speed (tr->speed(), true);
1707                 }
1708         }
1709
1710         if (!_engine.freewheeling()) {
1711                 Timecode::Time time;
1712                 timecode_time_subframes (_transport_frame, time);
1713                 if (!dynamic_cast<MTC_Slave*>(_slave)) {
1714                         send_immediate_mmc (MIDI::MachineControlCommand (MIDI::MachineControl::cmdDeferredPlay));
1715                 }
1716
1717                 if (actively_recording() && click_data && (config.get_count_in () || _count_in_once)) {
1718                         _count_in_once = false;
1719                         /* calculate count-in duration (in audio samples)
1720                          * - use [fixed] tempo/meter at _transport_frame
1721                          * - calc duration of 1 bar + time-to-beat before or at transport_frame
1722                          */
1723                         const Tempo& tempo = _tempo_map->tempo_at_frame (_transport_frame);
1724                         const Meter& meter = _tempo_map->meter_at_frame (_transport_frame);
1725
1726                         const double num = meter.divisions_per_bar ();
1727                         const double den = meter.note_divisor ();
1728                         const double barbeat = _tempo_map->exact_qn_at_frame (_transport_frame, 0) * den / (4. * num);
1729                         const double bar_fract = fmod (barbeat, 1.0); // fraction of bar elapsed.
1730
1731                         _count_in_samples = meter.frames_per_bar (tempo, _current_frame_rate);
1732
1733                         double dt = _count_in_samples / num;
1734                         if (bar_fract == 0) {
1735                                 /* at bar boundary, count-in 2 bars before start. */
1736                                 _count_in_samples *= 2;
1737                         } else {
1738                                 /* beats left after full bar until roll position */
1739                                 _count_in_samples *= 1. + bar_fract;
1740                         }
1741
1742                         int clickbeat = 0;
1743                         framepos_t cf = _transport_frame - _count_in_samples;
1744                         while (cf < _transport_frame) {
1745                                 add_click (cf - _worst_track_latency, clickbeat == 0);
1746                                 cf += dt;
1747                                 clickbeat = fmod (clickbeat + 1, num);
1748                         }
1749                 }
1750         }
1751
1752         DEBUG_TRACE (DEBUG::Transport, string_compose ("send TSC4 with speed = %1\n", _transport_speed));
1753         TransportStateChange (); /* EMIT SIGNAL */
1754 }
1755
1756 /** Do any transport work in the audio thread that needs to be done after the
1757  * transport thread is finished.  Audio thread, realtime safe.
1758  */
1759 void
1760 Session::post_transport ()
1761 {
1762         PostTransportWork ptw = post_transport_work ();
1763
1764         if (ptw & PostTransportAudition) {
1765                 if (auditioner && auditioner->auditioning()) {
1766                         process_function = &Session::process_audition;
1767                 } else {
1768                         process_function = &Session::process_with_events;
1769                 }
1770         }
1771
1772         if (ptw & PostTransportStop) {
1773
1774                 transport_sub_state = 0;
1775         }
1776
1777         if (ptw & PostTransportLocate) {
1778
1779                 if (((!config.get_external_sync() && (auto_play_legal && config.get_auto_play())) && !_exporting) || (ptw & PostTransportRoll)) {
1780                         _count_in_once = false;
1781                         start_transport ();
1782                 } else {
1783                         transport_sub_state = 0;
1784                 }
1785         }
1786
1787         set_next_event ();
1788         /* XXX is this really safe? shouldn't we just be unsetting the bits that we actually
1789            know were handled ?
1790         */
1791         set_post_transport_work (PostTransportWork (0));
1792 }
1793
1794 void
1795 Session::reset_rf_scale (framecnt_t motion)
1796 {
1797         cumulative_rf_motion += motion;
1798
1799         if (cumulative_rf_motion < 4 * _current_frame_rate) {
1800                 rf_scale = 1;
1801         } else if (cumulative_rf_motion < 8 * _current_frame_rate) {
1802                 rf_scale = 4;
1803         } else if (cumulative_rf_motion < 16 * _current_frame_rate) {
1804                 rf_scale = 10;
1805         } else {
1806                 rf_scale = 100;
1807         }
1808
1809         if (motion != 0) {
1810                 set_dirty();
1811         }
1812 }
1813
1814 void
1815 Session::mtc_status_changed (bool yn)
1816 {
1817         g_atomic_int_set (&_mtc_active, yn);
1818         MTCSyncStateChanged( yn );
1819 }
1820
1821 void
1822 Session::ltc_status_changed (bool yn)
1823 {
1824         g_atomic_int_set (&_ltc_active, yn);
1825         LTCSyncStateChanged( yn );
1826 }
1827
1828 void
1829 Session::use_sync_source (Slave* new_slave)
1830 {
1831         /* Runs in process() context */
1832
1833         bool non_rt_required = false;
1834
1835         /* XXX this deletion is problematic because we're in RT context */
1836
1837         delete _slave;
1838         _slave = new_slave;
1839
1840         MTC_Slave* mtc_slave = dynamic_cast<MTC_Slave*>(_slave);
1841         if (mtc_slave) {
1842                 mtc_slave->ActiveChanged.connect_same_thread (mtc_status_connection, boost::bind (&Session::mtc_status_changed, this, _1));
1843                 MTCSyncStateChanged(mtc_slave->locked() );
1844         } else {
1845                 if (g_atomic_int_get (&_mtc_active) ){
1846                         g_atomic_int_set (&_mtc_active, 0);
1847                         MTCSyncStateChanged( false );
1848                 }
1849                 mtc_status_connection.disconnect ();
1850         }
1851
1852         LTC_Slave* ltc_slave = dynamic_cast<LTC_Slave*> (_slave);
1853         if (ltc_slave) {
1854                 ltc_slave->ActiveChanged.connect_same_thread (ltc_status_connection, boost::bind (&Session::ltc_status_changed, this, _1));
1855                 LTCSyncStateChanged (ltc_slave->locked() );
1856         } else {
1857                 if (g_atomic_int_get (&_ltc_active) ){
1858                         g_atomic_int_set (&_ltc_active, 0);
1859                         LTCSyncStateChanged( false );
1860                 }
1861                 ltc_status_connection.disconnect ();
1862         }
1863
1864         DEBUG_TRACE (DEBUG::Slave, string_compose ("set new slave to %1\n", _slave));
1865
1866         // need to queue this for next process() cycle
1867         _send_timecode_update = true;
1868
1869         boost::shared_ptr<RouteList> rl = routes.reader();
1870         for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
1871                 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
1872                 if (tr && !tr->hidden()) {
1873                         if (tr->realtime_set_speed (tr->speed(), true)) {
1874                                 non_rt_required = true;
1875                         }
1876                         tr->set_slaved (_slave != 0);
1877                 }
1878         }
1879
1880         if (non_rt_required) {
1881                 add_post_transport_work (PostTransportSpeed);
1882                 _butler->schedule_transport_work ();
1883         }
1884
1885         set_dirty();
1886 }
1887
1888 void
1889 Session::drop_sync_source ()
1890 {
1891         request_sync_source (0);
1892 }
1893
1894 void
1895 Session::switch_to_sync_source (SyncSource src)
1896 {
1897         Slave* new_slave;
1898
1899         DEBUG_TRACE (DEBUG::Slave, string_compose ("Setting up sync source %1\n", enum_2_string (src)));
1900
1901         switch (src) {
1902         case MTC:
1903                 if (_slave && dynamic_cast<MTC_Slave*>(_slave)) {
1904                         return;
1905                 }
1906
1907                 try {
1908                         new_slave = new MTC_Slave (*this, *_midi_ports->mtc_input_port());
1909                 }
1910
1911                 catch (failed_constructor& err) {
1912                         return;
1913                 }
1914                 break;
1915
1916         case LTC:
1917                 if (_slave && dynamic_cast<LTC_Slave*>(_slave)) {
1918                         return;
1919                 }
1920
1921                 try {
1922                         new_slave = new LTC_Slave (*this);
1923                 }
1924
1925                 catch (failed_constructor& err) {
1926                         return;
1927                 }
1928
1929                 break;
1930
1931         case MIDIClock:
1932                 if (_slave && dynamic_cast<MIDIClock_Slave*>(_slave)) {
1933                         return;
1934                 }
1935
1936                 try {
1937                         new_slave = new MIDIClock_Slave (*this, *_midi_ports->midi_clock_input_port(), 24);
1938                 }
1939
1940                 catch (failed_constructor& err) {
1941                         return;
1942                 }
1943                 break;
1944
1945         case Engine:
1946                 if (_slave && dynamic_cast<Engine_Slave*>(_slave)) {
1947                         return;
1948                 }
1949
1950                 if (config.get_video_pullup() != 0.0f) {
1951                         return;
1952                 }
1953
1954                 new_slave = new Engine_Slave (*AudioEngine::instance());
1955                 break;
1956
1957         default:
1958                 new_slave = 0;
1959                 break;
1960         };
1961
1962         request_sync_source (new_slave);
1963 }
1964
1965 void
1966 Session::set_track_speed (Track* track, double speed)
1967 {
1968         if (track->realtime_set_speed (speed, false)) {
1969                 add_post_transport_work (PostTransportSpeed);
1970                 _butler->schedule_transport_work ();
1971                 set_dirty ();
1972         }
1973 }
1974
1975 void
1976 Session::unset_play_range ()
1977 {
1978         _play_range = false;
1979         _clear_event_type (SessionEvent::RangeStop);
1980         _clear_event_type (SessionEvent::RangeLocate);
1981 }
1982
1983 void
1984 Session::set_play_range (list<AudioRange>& range, bool leave_rolling)
1985 {
1986         SessionEvent* ev;
1987
1988         /* Called from event-processing context */
1989
1990         unset_play_range ();
1991
1992         if (range.empty()) {
1993                 /* _play_range set to false in unset_play_range()
1994                  */
1995                 if (!leave_rolling) {
1996                         /* stop transport */
1997                         SessionEvent* ev = new SessionEvent (SessionEvent::SetTransportSpeed, SessionEvent::Add, SessionEvent::Immediate, 0, 0.0f, false);
1998                         merge_event (ev);
1999                 }
2000                 return;
2001         }
2002
2003         _play_range = true;
2004
2005         /* cancel loop play */
2006         unset_play_loop ();
2007
2008         list<AudioRange>::size_type sz = range.size();
2009
2010         if (sz > 1) {
2011
2012                 list<AudioRange>::iterator i = range.begin();
2013                 list<AudioRange>::iterator next;
2014
2015                 while (i != range.end()) {
2016
2017                         next = i;
2018                         ++next;
2019
2020                         /* locating/stopping is subject to delays for declicking.
2021                          */
2022
2023                         framepos_t requested_frame = i->end;
2024
2025                         if (requested_frame > current_block_size) {
2026                                 requested_frame -= current_block_size;
2027                         } else {
2028                                 requested_frame = 0;
2029                         }
2030
2031                         if (next == range.end()) {
2032                                 ev = new SessionEvent (SessionEvent::RangeStop, SessionEvent::Add, requested_frame, 0, 0.0f);
2033                         } else {
2034                                 ev = new SessionEvent (SessionEvent::RangeLocate, SessionEvent::Add, requested_frame, (*next).start, 0.0f);
2035                         }
2036
2037                         merge_event (ev);
2038
2039                         i = next;
2040                 }
2041
2042         } else if (sz == 1) {
2043
2044                 ev = new SessionEvent (SessionEvent::RangeStop, SessionEvent::Add, range.front().end, 0, 0.0f);
2045                 merge_event (ev);
2046
2047         }
2048
2049         /* save range so we can do auto-return etc. */
2050
2051         current_audio_range = range;
2052
2053         /* now start rolling at the right place */
2054
2055         ev = new SessionEvent (SessionEvent::LocateRoll, SessionEvent::Add, SessionEvent::Immediate, range.front().start, 0.0f, false);
2056         merge_event (ev);
2057
2058         DEBUG_TRACE (DEBUG::Transport, string_compose ("send TSC5 with speed = %1\n", _transport_speed));
2059         TransportStateChange ();
2060 }
2061
2062 void
2063 Session::request_bounded_roll (framepos_t start, framepos_t end)
2064 {
2065         AudioRange ar (start, end, 0);
2066         list<AudioRange> lar;
2067
2068         lar.push_back (ar);
2069         request_play_range (&lar, true);
2070 }
2071
2072 void
2073 Session::set_requested_return_frame (framepos_t return_to)
2074 {
2075         _requested_return_frame = return_to;
2076 }
2077
2078 void
2079 Session::request_roll_at_and_return (framepos_t start, framepos_t return_to)
2080 {
2081         SessionEvent *ev = new SessionEvent (SessionEvent::LocateRollLocate, SessionEvent::Add, SessionEvent::Immediate, return_to, 1.0);
2082         ev->target2_frame = start;
2083         queue_event (ev);
2084 }
2085
2086 void
2087 Session::engine_halted ()
2088 {
2089         bool ignored;
2090
2091         /* there will be no more calls to process(), so
2092            we'd better clean up for ourselves, right now.
2093
2094            but first, make sure the butler is out of
2095            the picture.
2096         */
2097
2098         if (_butler) {
2099                 _butler->stop ();
2100         }
2101
2102         realtime_stop (false, true);
2103         non_realtime_stop (false, 0, ignored);
2104         transport_sub_state = 0;
2105
2106         DEBUG_TRACE (DEBUG::Transport, string_compose ("send TSC6 with speed = %1\n", _transport_speed));
2107         TransportStateChange (); /* EMIT SIGNAL */
2108 }
2109
2110
2111 void
2112 Session::xrun_recovery ()
2113 {
2114         ++_xrun_count;
2115
2116         Xrun (_transport_frame); /* EMIT SIGNAL */
2117
2118         if (Config->get_stop_recording_on_xrun() && actively_recording()) {
2119
2120                 /* it didn't actually halt, but we need
2121                    to handle things in the same way.
2122                 */
2123
2124                 engine_halted();
2125         }
2126 }
2127
2128 void
2129 Session::route_processors_changed (RouteProcessorChange c)
2130 {
2131         if (g_atomic_int_get (&_ignore_route_processor_changes) > 0) {
2132                 return;
2133         }
2134
2135         if (c.type == RouteProcessorChange::MeterPointChange) {
2136                 set_dirty ();
2137                 return;
2138         }
2139
2140         if (c.type == RouteProcessorChange::RealTimeChange) {
2141                 set_dirty ();
2142                 return;
2143         }
2144
2145         update_latency_compensation ();
2146         resort_routes ();
2147
2148         set_dirty ();
2149 }
2150
2151 void
2152 Session::allow_auto_play (bool yn)
2153 {
2154         auto_play_legal = yn;
2155 }
2156
2157 bool
2158 Session::maybe_stop (framepos_t limit)
2159 {
2160         if ((_transport_speed > 0.0f && _transport_frame >= limit) || (_transport_speed < 0.0f && _transport_frame == 0)) {
2161                 if (synced_to_engine () && config.get_jack_time_master ()) {
2162                         _engine.transport_stop ();
2163                 } else if (!synced_to_engine ()) {
2164                         stop_transport ();
2165                 }
2166                 return true;
2167         }
2168         return false;
2169 }
2170
2171 void
2172 Session::send_mmc_locate (framepos_t t)
2173 {
2174         if (t < 0) {
2175                 return;
2176         }
2177
2178         if (!_engine.freewheeling()) {
2179                 Timecode::Time time;
2180                 timecode_time_subframes (t, time);
2181                 send_immediate_mmc (MIDI::MachineControlCommand (time));
2182         }
2183 }
2184
2185 /** Ask the transport to not send timecode until further notice.  The suspension
2186  *  will come into effect some finite time after this call, and timecode_transmission_suspended()
2187  *  should be checked by the caller to find out when.
2188  */
2189 void
2190 Session::request_suspend_timecode_transmission ()
2191 {
2192         SessionEvent* ev = new SessionEvent (SessionEvent::SetTimecodeTransmission, SessionEvent::Add, SessionEvent::Immediate, 0, 0, false);
2193         queue_event (ev);
2194 }
2195
2196 void
2197 Session::request_resume_timecode_transmission ()
2198 {
2199         SessionEvent* ev = new SessionEvent (SessionEvent::SetTimecodeTransmission, SessionEvent::Add, SessionEvent::Immediate, 0, 0, true);
2200         queue_event (ev);
2201 }
2202
2203 bool
2204 Session::timecode_transmission_suspended () const
2205 {
2206         return g_atomic_int_get (&_suspend_timecode_transmission) == 1;
2207 }