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