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