2 Copyright (C) 1999-2003 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "libardour-config.h"
30 #include "pbd/error.h"
31 #include "pbd/enumwriter.h"
32 #include "pbd/pthread_utils.h"
33 #include "pbd/memento_command.h"
35 #include "midi++/mmc.h"
36 #include "midi++/port.h"
37 #include "midi++/manager.h"
39 #include "ardour/ardour.h"
40 #include "ardour/audioengine.h"
41 #include "ardour/auditioner.h"
42 #include "ardour/butler.h"
43 #include "ardour/debug.h"
44 #include "ardour/location.h"
45 #include "ardour/session.h"
46 #include "ardour/slave.h"
47 #include "ardour/operations.h"
52 using namespace ARDOUR;
56 Session::add_post_transport_work (PostTransportWork ptw)
58 PostTransportWork oldval;
59 PostTransportWork newval;
63 oldval = (PostTransportWork) g_atomic_int_get (&_post_transport_work);
64 newval = PostTransportWork (oldval | ptw);
65 if (g_atomic_int_compare_and_exchange (&_post_transport_work, oldval, newval)) {
71 error << "Could not set post transport work! Crazy thread madness, call the programmers" << endmsg;
75 Session::request_input_change_handling ()
77 if (!(_state_of_the_state & (InitialConnecting|Deletion))) {
78 SessionEvent* ev = new SessionEvent (SessionEvent::InputConfigurationChange, SessionEvent::Add, SessionEvent::Immediate, 0, 0.0);
84 Session::request_sync_source (Slave* new_slave)
86 SessionEvent* ev = new SessionEvent (SessionEvent::SetSyncSource, SessionEvent::Add, SessionEvent::Immediate, 0, 0.0);
89 seamless = Config->get_seamless_loop ();
91 if (dynamic_cast<JACK_Slave*>(new_slave)) {
92 /* JACK cannot support seamless looping at present */
93 Config->set_seamless_loop (false);
95 /* reset to whatever the value was before we last switched slaves */
96 Config->set_seamless_loop (_was_seamless);
99 /* save value of seamless from before the switch */
100 _was_seamless = seamless;
102 ev->slave = new_slave;
107 Session::request_transport_speed (double speed)
109 SessionEvent* ev = new SessionEvent (SessionEvent::SetTransportSpeed, SessionEvent::Add, SessionEvent::Immediate, 0, speed);
110 DEBUG_TRACE (DEBUG::Transport, string_compose ("Request transport speed = %1\n", speed));
114 /** Request a new transport speed, but if the speed parameter is exactly zero then use
115 * a very small +ve value to prevent the transport actually stopping. This method should
116 * be used by callers who are varying transport speed but don't ever want to stop it.
119 Session::request_transport_speed_nonzero (double speed)
125 request_transport_speed (speed);
129 Session::request_track_speed (Track* tr, double speed)
131 SessionEvent* ev = new SessionEvent (SessionEvent::SetTrackSpeed, SessionEvent::Add, SessionEvent::Immediate, 0, speed);
137 Session::request_stop (bool abort, bool clear_state)
139 SessionEvent* ev = new SessionEvent (SessionEvent::SetTransportSpeed, SessionEvent::Add, SessionEvent::Immediate, 0, 0.0, abort, clear_state);
140 DEBUG_TRACE (DEBUG::Transport, string_compose ("Request transport stop, abort = %1, clear state = %2\n", abort, clear_state));
145 Session::request_locate (framepos_t target_frame, bool with_roll)
147 SessionEvent *ev = new SessionEvent (with_roll ? SessionEvent::LocateRoll : SessionEvent::Locate, SessionEvent::Add, SessionEvent::Immediate, target_frame, 0, false);
148 DEBUG_TRACE (DEBUG::Transport, string_compose ("Request locate to %1\n", target_frame));
153 Session::force_locate (framepos_t target_frame, bool with_roll)
155 SessionEvent *ev = new SessionEvent (with_roll ? SessionEvent::LocateRoll : SessionEvent::Locate, SessionEvent::Add, SessionEvent::Immediate, target_frame, 0, true);
156 DEBUG_TRACE (DEBUG::Transport, string_compose ("Request forced locate to %1\n", target_frame));
161 Session::request_play_loop (bool yn, bool leave_rolling)
164 Location *location = _locations->auto_loop_location();
166 if (location == 0 && yn) {
167 error << _("Cannot loop - no loop range defined")
172 ev = new SessionEvent (SessionEvent::SetLoop, SessionEvent::Add, SessionEvent::Immediate, 0, (leave_rolling ? 1.0 : 0.0), yn);
173 DEBUG_TRACE (DEBUG::Transport, string_compose ("Request set loop = %1, leave rolling ? %2\n", yn, leave_rolling));
176 if (!leave_rolling && !yn && Config->get_seamless_loop() && transport_rolling()) {
177 // request an immediate locate to refresh the tracks
178 // after disabling looping
179 request_locate (_transport_frame-1, false);
184 Session::request_play_range (list<AudioRange>* range, bool leave_rolling)
186 SessionEvent* ev = new SessionEvent (SessionEvent::SetPlayAudioRange, SessionEvent::Add, SessionEvent::Immediate, 0, (leave_rolling ? 1.0 : 0.0));
188 ev->audio_range = *range;
190 ev->audio_range.clear ();
192 DEBUG_TRACE (DEBUG::Transport, string_compose ("Request play range, leave rolling ? %1\n", leave_rolling));
197 Session::realtime_stop (bool abort, bool clear_state)
199 DEBUG_TRACE (DEBUG::Transport, string_compose ("realtime stop @ %1\n", _transport_frame));
200 PostTransportWork todo = PostTransportWork (0);
202 /* assume that when we start, we'll be moving forwards */
204 if (_transport_speed < 0.0f) {
205 todo = (PostTransportWork (todo | PostTransportStop | PostTransportReverse));
207 todo = PostTransportWork (todo | PostTransportStop);
212 boost::shared_ptr<RouteList> r = routes.reader ();
214 for (RouteList::iterator i = r->begin (); i != r->end(); ++i) {
215 (*i)->realtime_handle_transport_stopped ();
218 if (actively_recording()) {
220 /* move the transport position back to where the
221 request for a stop was noticed. we rolled
222 past that point to pick up delayed input (and/or to delick)
225 if (worst_playback_latency() > current_block_size) {
226 /* we rolled past the stop point to pick up data that had
227 not yet arrived. move back to where the stop occured.
229 decrement_transport_position (current_block_size + (worst_input_latency() - current_block_size));
231 decrement_transport_position (current_block_size);
234 /* the duration change is not guaranteed to have happened, but is likely */
236 todo = PostTransportWork (todo | PostTransportDuration);
240 todo = PostTransportWork (todo | PostTransportAbort);
244 todo = PostTransportWork (todo | PostTransportClearSubstate);
248 add_post_transport_work (todo);
251 _clear_event_type (SessionEvent::StopOnce);
252 _clear_event_type (SessionEvent::RangeStop);
253 _clear_event_type (SessionEvent::RangeLocate);
255 /* if we're going to clear loop state, then force disabling record BUT only if we're not doing latched rec-enable */
256 disable_record (true, (!Config->get_latched_record_enable() && clear_state));
258 reset_slave_state ();
260 _transport_speed = 0;
261 _target_transport_speed = 0;
263 g_atomic_int_set (&_playback_load, 100);
264 g_atomic_int_set (&_capture_load, 100);
266 if (config.get_use_video_sync()) {
267 waiting_for_sync_offset = true;
270 transport_sub_state = 0;
274 Session::realtime_locate ()
276 boost::shared_ptr<RouteList> r = routes.reader ();
277 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
278 (*i)->realtime_locate ();
283 Session::butler_transport_work ()
287 PostTransportWork ptw;
288 boost::shared_ptr<RouteList> r = routes.reader ();
290 int on_entry = g_atomic_int_get (&_butler->should_do_transport_work);
292 ptw = post_transport_work();
294 DEBUG_TRACE (DEBUG::Transport, string_compose ("Butler transport work, todo = %1\n", enum_2_string (ptw)));
296 if (ptw & PostTransportAdjustPlaybackBuffering) {
297 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
298 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
300 tr->adjust_playback_buffering ();
301 /* and refill those buffers ... */
302 tr->non_realtime_locate (_transport_frame);
308 if (ptw & PostTransportAdjustCaptureBuffering) {
309 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
310 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
312 tr->adjust_capture_buffering ();
317 if (ptw & PostTransportCurveRealloc) {
318 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
319 (*i)->curve_reallocate();
323 if (ptw & PostTransportInputChange) {
324 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
325 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
327 tr->non_realtime_input_change ();
332 if (ptw & PostTransportSpeed) {
333 non_realtime_set_speed ();
336 if (ptw & PostTransportReverse) {
339 cumulative_rf_motion = 0;
342 /* don't seek if locate will take care of that in non_realtime_stop() */
344 if (!(ptw & PostTransportLocate)) {
346 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
347 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
348 if (tr && !tr->hidden()) {
349 tr->non_realtime_locate (_transport_frame);
351 if (on_entry != g_atomic_int_get (&_butler->should_do_transport_work)) {
352 /* new request, stop seeking, and start again */
353 g_atomic_int_dec_and_test (&_butler->should_do_transport_work);
360 if (ptw & PostTransportLocate) {
361 non_realtime_locate ();
364 if (ptw & PostTransportStop) {
365 non_realtime_stop (ptw & PostTransportAbort, on_entry, finished);
367 g_atomic_int_dec_and_test (&_butler->should_do_transport_work);
372 if (ptw & PostTransportOverWrite) {
373 non_realtime_overwrite (on_entry, finished);
375 g_atomic_int_dec_and_test (&_butler->should_do_transport_work);
380 if (ptw & PostTransportAudition) {
381 non_realtime_set_audition ();
384 g_atomic_int_dec_and_test (&_butler->should_do_transport_work);
386 DEBUG_TRACE (DEBUG::Transport, X_("Butler transport work all done\n"));
390 Session::non_realtime_set_speed ()
392 boost::shared_ptr<RouteList> rl = routes.reader();
393 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
394 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
396 tr->non_realtime_set_speed ();
402 Session::non_realtime_overwrite (int on_entry, bool& finished)
404 boost::shared_ptr<RouteList> rl = routes.reader();
405 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
406 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
407 if (tr && tr->pending_overwrite ()) {
408 tr->overwrite_existing_buffers ();
410 if (on_entry != g_atomic_int_get (&_butler->should_do_transport_work)) {
419 Session::non_realtime_locate ()
421 boost::shared_ptr<RouteList> rl = routes.reader();
422 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
423 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
425 tr->non_realtime_locate (_transport_frame);
429 /* XXX: it would be nice to generate the new clicks here (in the non-RT thread)
430 rather than clearing them so that the RT thread has to spend time constructing
431 them (in Session::click).
438 Session::non_realtime_stop (bool abort, int on_entry, bool& finished)
444 PostTransportWork ptw = post_transport_work();
449 boost::shared_ptr<RouteList> rl = routes.reader();
450 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
451 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
452 if (tr && tr->get_captured_frames () != 0) {
458 /* stop and locate are merged here because they share a lot of common stuff */
461 now = localtime (&xnow);
464 auditioner->cancel_audition ();
468 cumulative_rf_motion = 0;
472 begin_reversible_command (Operations::capture);
473 _have_captured = true;
476 DEBUG_TRACE (DEBUG::Transport, X_("Butler PTW: DS stop\n"));
478 if (abort && did_record) {
479 /* no reason to save the session file when we remove sources
481 _state_of_the_state = StateOfTheState (_state_of_the_state|InCleanup);
484 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
485 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
487 tr->transport_stopped_wallclock (*now, xnow, abort);
491 if (abort && did_record) {
492 _state_of_the_state = StateOfTheState (_state_of_the_state & ~InCleanup);
495 boost::shared_ptr<RouteList> r = routes.reader ();
497 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
498 if (!(*i)->is_hidden()) {
499 (*i)->set_pending_declick (0);
504 commit_reversible_command ();
507 if (_engine.running()) {
508 PostTransportWork ptw = post_transport_work ();
509 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
510 (*i)->nonrealtime_handle_transport_stopped (abort, (ptw & PostTransportLocate), (!(ptw & PostTransportLocate) || pending_locate_flush));
512 update_latency_compensation ();
515 bool const auto_return_enabled =
516 (!config.get_external_sync() && config.get_auto_return());
518 if (auto_return_enabled ||
519 (ptw & PostTransportLocate) ||
520 (_requested_return_frame >= 0) ||
523 if (pending_locate_flush) {
524 flush_all_inserts ();
527 if ((auto_return_enabled || synced_to_jack() || _requested_return_frame >= 0) &&
528 !(ptw & PostTransportLocate)) {
530 /* no explicit locate queued */
532 bool do_locate = false;
534 if (_requested_return_frame >= 0) {
536 /* explicit return request pre-queued in event list. overrides everything else */
538 cerr << "explicit auto-return to " << _requested_return_frame << endl;
540 _transport_frame = _requested_return_frame;
544 if (config.get_auto_return()) {
548 /* don't try to handle loop play when synced to JACK */
550 if (!synced_to_jack()) {
552 Location *location = _locations->auto_loop_location();
555 _transport_frame = location->start();
557 _transport_frame = _last_roll_location;
562 } else if (_play_range) {
564 /* return to start of range */
566 if (!current_audio_range.empty()) {
567 _transport_frame = current_audio_range.front().start;
573 /* regular auto-return */
575 _transport_frame = _last_roll_location;
581 _requested_return_frame = -1;
584 _engine.transport_locate (_transport_frame);
590 /* do this before seeking, because otherwise the tracks will do the wrong thing in seamless loop mode.
593 if (ptw & PostTransportClearSubstate) {
598 /* this for() block can be put inside the previous if() and has the effect of ... ??? what */
600 DEBUG_TRACE (DEBUG::Transport, X_("Butler PTW: locate\n"));
601 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
602 DEBUG_TRACE (DEBUG::Transport, string_compose ("Butler PTW: locate on %1\n", (*i)->name()));
603 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
604 if (tr && !tr->hidden()) {
605 tr->non_realtime_locate (_transport_frame);
608 if (on_entry != g_atomic_int_get (&_butler->should_do_transport_work)) {
610 /* we will be back */
617 send_full_time_code (_transport_frame);
619 if (!dynamic_cast<MTC_Slave*>(_slave)) {
620 MIDI::Manager::instance()->mmc()->send (MIDI::MachineControlCommand (MIDI::MachineControl::cmdStop));
621 send_mmc_locate (_transport_frame);
624 if ((ptw & PostTransportLocate) && get_record_enabled()) {
625 /* capture start has been changed, so save pending state */
626 save_state ("", true);
630 /* always try to get rid of this */
632 remove_pending_capture_state ();
634 /* save the current state of things if appropriate */
636 if (did_record && !saved) {
637 save_state (_current_snapshot_name);
640 if (ptw & PostTransportStop) {
645 PositionChanged (_transport_frame); /* EMIT SIGNAL */
646 TransportStateChange (); /* EMIT SIGNAL */
648 /* and start it up again if relevant */
650 if ((ptw & PostTransportLocate) && !config.get_external_sync() && pending_locate_roll) {
651 request_transport_speed (1.0);
652 pending_locate_roll = false;
657 Session::check_declick_out ()
659 bool locate_required = transport_sub_state & PendingLocate;
661 /* this is called after a process() iteration. if PendingDeclickOut was set,
662 it means that we were waiting to declick the output (which has just been
663 done) before doing something else. this is where we do that "something else".
665 note: called from the audio thread.
668 if (transport_sub_state & PendingDeclickOut) {
670 if (locate_required) {
671 start_locate (pending_locate_frame, pending_locate_roll, pending_locate_flush);
672 transport_sub_state &= ~(PendingDeclickOut|PendingLocate);
674 stop_transport (pending_abort);
675 transport_sub_state &= ~(PendingDeclickOut|PendingLocate);
681 Session::unset_play_loop ()
684 clear_events (SessionEvent::AutoLoop);
686 // set all tracks to NOT use internal looping
687 boost::shared_ptr<RouteList> rl = routes.reader ();
688 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
689 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
690 if (tr && !tr->hidden()) {
697 Session::set_play_loop (bool yn)
699 /* Called from event-handling context */
703 if (yn == play_loop || (actively_recording() && yn) || (loc = _locations->auto_loop_location()) == 0) {
704 /* nothing to do, or can't change loop status while recording */
708 if (yn && Config->get_seamless_loop() && synced_to_jack()) {
709 warning << string_compose (
710 _("Seamless looping cannot be supported while %1 is using JACK transport.\n"
711 "Recommend changing the configured options"), PROGRAM_NAME)
724 if (Config->get_seamless_loop()) {
725 // set all tracks to use internal looping
726 boost::shared_ptr<RouteList> rl = routes.reader ();
727 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
728 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
729 if (tr && !tr->hidden()) {
735 // set all tracks to NOT use internal looping
736 boost::shared_ptr<RouteList> rl = routes.reader ();
737 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
738 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
739 if (tr && !tr->hidden()) {
745 /* put the loop event into the event list */
747 SessionEvent* event = new SessionEvent (SessionEvent::AutoLoop, SessionEvent::Replace, loc->end(), loc->start(), 0.0f);
750 /* locate to start of loop and roll. If doing seamless loop, force a
751 locate+buffer refill even if we are positioned there already.
754 start_locate (loc->start(), true, true, false, Config->get_seamless_loop());
762 TransportStateChange ();
765 Session::flush_all_inserts ()
767 boost::shared_ptr<RouteList> r = routes.reader ();
769 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
770 (*i)->flush_processors ();
775 Session::start_locate (framepos_t target_frame, bool with_roll, bool with_flush, bool with_loop, bool force)
777 if (synced_to_jack()) {
782 _slave->speed_and_position (sp, pos);
784 if (target_frame != pos) {
786 /* tell JACK to change transport position, and we will
787 follow along later in ::follow_slave()
790 _engine.transport_locate (target_frame);
792 if (sp != 1.0f && with_roll) {
793 _engine.transport_start ();
799 locate (target_frame, with_roll, with_flush, with_loop, force);
804 Session::micro_locate (framecnt_t distance)
806 boost::shared_ptr<RouteList> rl = routes.reader();
807 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
808 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
809 if (tr && !tr->can_internal_playback_seek (distance)) {
814 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
815 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
817 tr->internal_playback_seek (distance);
821 _transport_frame += distance;
825 /** @param with_mmc true to send a MMC locate command when the locate is done */
827 Session::locate (framepos_t target_frame, bool with_roll, bool with_flush, bool with_loop, bool force, bool with_mmc)
829 if (actively_recording() && !with_loop) {
833 if (!force && _transport_frame == target_frame && !loop_changing && !with_loop) {
835 set_transport_speed (1.0, false);
837 loop_changing = false;
838 Located (); /* EMIT SIGNAL */
842 if (_transport_speed) {
843 /* schedule a declick. we'll be called again when its done */
845 if (!(transport_sub_state & PendingDeclickOut)) {
846 transport_sub_state |= (PendingDeclickOut|PendingLocate);
847 pending_locate_frame = target_frame;
848 pending_locate_roll = with_roll;
849 pending_locate_flush = with_flush;
854 // Update Timecode time
855 // [DR] FIXME: find out exactly where this should go below
856 _transport_frame = target_frame;
857 timecode_time(_transport_frame, transmitting_timecode_time);
858 outbound_mtc_timecode_frame = _transport_frame;
859 next_quarter_frame_to_send = 0;
861 /* do "stopped" stuff if:
864 * no autoplay in effect AND
865 * we're not going to keep rolling after the locate AND
866 * !(playing a loop with JACK sync)
870 if (transport_rolling() && (!auto_play_legal || !config.get_auto_play()) && !with_roll && !(synced_to_jack() && play_loop)) {
871 realtime_stop (false, true); // XXX paul - check if the 2nd arg is really correct
873 /* otherwise tell the world that we located */
877 if (force || !with_loop || loop_changing) {
879 PostTransportWork todo = PostTransportLocate;
882 todo = PostTransportWork (todo | PostTransportRoll);
885 add_post_transport_work (todo);
886 _butler->schedule_transport_work ();
890 /* this is functionally what clear_clicks() does but with a tentative lock */
892 Glib::RWLock::WriterLock clickm (click_lock, Glib::TRY_LOCK);
894 if (clickm.locked()) {
896 for (Clicks::iterator i = clicks.begin(); i != clicks.end(); ++i) {
905 /* switch from input if we're going to roll */
906 if (Config->get_monitoring_model() == HardwareMonitoring) {
907 set_track_monitor_input_status (!config.get_auto_input());
910 /* otherwise we're going to stop, so do the opposite */
911 if (Config->get_monitoring_model() == HardwareMonitoring) {
912 set_track_monitor_input_status (true);
916 /* cancel looped playback if transport pos outside of loop range */
918 Location* al = _locations->auto_loop_location();
920 if (al && (_transport_frame < al->start() || _transport_frame > al->end())) {
921 // cancel looping directly, this is called from event handling context
922 set_play_loop (false);
924 else if (al && _transport_frame == al->start()) {
926 // this is only necessary for seamless looping
928 boost::shared_ptr<RouteList> rl = routes.reader();
929 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
930 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
931 if (tr && tr->record_enabled ()) {
932 // tell it we've looped, so it can deal with the record state
933 tr->transport_looped(_transport_frame);
938 TransportLooped(); // EMIT SIGNAL
942 loop_changing = false;
944 _send_timecode_update = true;
947 send_mmc_locate (_transport_frame);
950 Located (); /* EMIT SIGNAL */
953 /** Set the transport speed.
954 * Called from the process thread.
955 * @param speed New speed
958 Session::set_transport_speed (double speed, bool abort, bool clear_state)
960 DEBUG_TRACE (DEBUG::Transport, string_compose ("@ %5 Set transport speed to %1, abort = %2 clear_state = %3, current = %4\n",
961 speed, abort, clear_state, _transport_speed, _transport_frame));
963 if (_transport_speed == speed) {
967 if (actively_recording() && speed != 1.0 && speed != 0.0) {
968 /* no varispeed during recording */
972 _target_transport_speed = fabs(speed);
974 /* 8.0 max speed is somewhat arbitrary but based on guestimates regarding disk i/o capability
975 and user needs. We really need CD-style "skip" playback for ffwd and rewind.
979 speed = min (8.0, speed);
980 } else if (speed < 0) {
981 speed = max (-8.0, speed);
984 if (transport_rolling() && speed == 0.0) {
986 /* we are rolling and we want to stop */
988 if (Config->get_monitoring_model() == HardwareMonitoring) {
989 set_track_monitor_input_status (true);
992 if (synced_to_jack ()) {
994 /* do this here because our response to the slave won't
1000 _engine.transport_stop ();
1002 stop_transport (abort);
1007 } else if (transport_stopped() && speed == 1.0) {
1009 /* we are stopped and we want to start rolling at speed 1 */
1011 if (Config->get_monitoring_model() == HardwareMonitoring && config.get_auto_input()) {
1012 set_track_monitor_input_status (false);
1015 if (synced_to_jack()) {
1016 _engine.transport_start ();
1023 /* not zero, not 1.0 ... varispeed */
1025 if ((synced_to_jack()) && speed != 0.0 && speed != 1.0) {
1026 warning << string_compose (
1027 _("Global varispeed cannot be supported while %1 is connected to JACK transport control"),
1033 if (actively_recording()) {
1037 if (speed > 0.0 && _transport_frame == current_end_frame()) {
1041 if (speed < 0.0 && _transport_frame == 0) {
1047 /* if we are reversing relative to the current speed, or relative to the speed
1048 before the last stop, then we have to do extra work.
1051 PostTransportWork todo = PostTransportWork (0);
1053 if ((_transport_speed && speed * _transport_speed < 0.0) || (_last_transport_speed * speed < 0.0) || (_last_transport_speed == 0.0f && speed < 0.0f)) {
1054 todo = PostTransportWork (todo | PostTransportReverse);
1055 _last_roll_or_reversal_location = _transport_frame;
1058 _last_transport_speed = _transport_speed;
1059 _transport_speed = speed;
1061 boost::shared_ptr<RouteList> rl = routes.reader();
1062 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
1063 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
1064 if (tr && tr->realtime_set_speed (tr->speed(), true)) {
1065 todo = PostTransportWork (todo | PostTransportSpeed);
1070 add_post_transport_work (todo);
1071 _butler->schedule_transport_work ();
1074 TransportStateChange (); /* EMIT SIGNAL */
1079 /** Stop the transport. */
1081 Session::stop_transport (bool abort, bool clear_state)
1083 if (_transport_speed == 0.0f) {
1087 if (actively_recording() && !(transport_sub_state & StopPendingCapture) && worst_input_latency() > current_block_size) {
1089 boost::shared_ptr<RouteList> rl = routes.reader();
1090 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
1091 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
1093 tr->prepare_to_stop (_transport_frame);
1097 /* we need to capture the audio that has still not yet been received by the system
1098 at the time the stop is requested, so we have to roll past that time.
1100 we want to declick before stopping, so schedule the autostop for one
1101 block before the actual end. we'll declick in the subsequent block,
1102 and then we'll really be stopped.
1105 DEBUG_TRACE (DEBUG::Transport, string_compose ("stop transport requested @ %1, scheduled for + %2 - %3 = %4, abort = %5\n",
1106 _transport_frame, _worst_input_latency, current_block_size,
1107 _transport_frame - _worst_input_latency - current_block_size,
1110 SessionEvent *ev = new SessionEvent (SessionEvent::StopOnce, SessionEvent::Replace,
1111 _transport_frame + _worst_input_latency - current_block_size,
1115 transport_sub_state |= StopPendingCapture;
1116 pending_abort = abort;
1120 if ((transport_sub_state & PendingDeclickOut) == 0) {
1122 if (!(transport_sub_state & StopPendingCapture)) {
1123 boost::shared_ptr<RouteList> rl = routes.reader();
1124 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
1125 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
1127 tr->prepare_to_stop (_transport_frame);
1132 transport_sub_state |= PendingDeclickOut;
1133 /* we'll be called again after the declick */
1134 pending_abort = abort;
1138 realtime_stop (abort, clear_state);
1139 _butler->schedule_transport_work ();
1142 /** Called from the process thread */
1144 Session::start_transport ()
1146 DEBUG_TRACE (DEBUG::Transport, "start_transport\n");
1148 _last_roll_location = _transport_frame;
1149 _last_roll_or_reversal_location = _transport_frame;
1151 have_looped = false;
1153 /* if record status is Enabled, move it to Recording. if its
1154 already Recording, move it to Disabled.
1157 switch (record_status()) {
1159 if (!config.get_punch_in()) {
1166 disable_record (false);
1174 transport_sub_state |= PendingDeclickIn;
1176 _transport_speed = 1.0;
1177 _target_transport_speed = 1.0;
1179 boost::shared_ptr<RouteList> rl = routes.reader();
1180 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
1181 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
1183 tr->realtime_set_speed (tr->speed(), true);
1185 (*i)->automation_snapshot (_transport_frame, true);
1188 Timecode::Time time;
1189 timecode_time_subframes (_transport_frame, time);
1190 if (!dynamic_cast<MTC_Slave*>(_slave)) {
1191 MIDI::Manager::instance()->mmc()->send (MIDI::MachineControlCommand (MIDI::MachineControl::cmdDeferredPlay));
1194 TransportStateChange (); /* EMIT SIGNAL */
1197 /** Do any transport work in the audio thread that needs to be done after the
1198 * transport thread is finished. Audio thread, realtime safe.
1201 Session::post_transport ()
1203 PostTransportWork ptw = post_transport_work ();
1205 if (ptw & PostTransportAudition) {
1206 if (auditioner && auditioner->auditioning()) {
1207 process_function = &Session::process_audition;
1209 process_function = &Session::process_with_events;
1213 if (ptw & PostTransportStop) {
1215 transport_sub_state = 0;
1218 if (ptw & PostTransportLocate) {
1220 if (((!config.get_external_sync() && (auto_play_legal && config.get_auto_play())) && !_exporting) || (ptw & PostTransportRoll)) {
1224 transport_sub_state = 0;
1229 /* XXX is this really safe? shouldn't we just be unsetting the bits that we actually
1232 set_post_transport_work (PostTransportWork (0));
1236 Session::reset_rf_scale (framecnt_t motion)
1238 cumulative_rf_motion += motion;
1240 if (cumulative_rf_motion < 4 * _current_frame_rate) {
1242 } else if (cumulative_rf_motion < 8 * _current_frame_rate) {
1244 } else if (cumulative_rf_motion < 16 * _current_frame_rate) {
1256 Session::use_sync_source (Slave* new_slave)
1258 /* Runs in process() context */
1260 bool non_rt_required = false;
1262 /* XXX this deletion is problematic because we're in RT context */
1267 send_full_time_code (_transport_frame);
1269 boost::shared_ptr<RouteList> rl = routes.reader();
1270 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
1271 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
1272 if (tr && !tr->hidden()) {
1273 if (tr->realtime_set_speed (tr->speed(), true)) {
1274 non_rt_required = true;
1276 tr->set_slaved (_slave != 0);
1280 if (non_rt_required) {
1281 add_post_transport_work (PostTransportSpeed);
1282 _butler->schedule_transport_work ();
1289 Session::drop_sync_source ()
1291 request_sync_source (0);
1295 Session::switch_to_sync_source (SyncSource src)
1299 DEBUG_TRACE (DEBUG::Slave, string_compose ("Setting up sync source %1\n", enum_2_string (src)));
1303 if (_slave && dynamic_cast<MTC_Slave*>(_slave)) {
1308 new_slave = new MTC_Slave (*this, *MIDI::Manager::instance()->mtc_input_port());
1311 catch (failed_constructor& err) {
1317 if (_slave && dynamic_cast<MIDIClock_Slave*>(_slave)) {
1322 new_slave = new MIDIClock_Slave (*this, *MIDI::Manager::instance()->midi_clock_input_port(), 24);
1325 catch (failed_constructor& err) {
1331 if (_slave && dynamic_cast<JACK_Slave*>(_slave)) {
1335 if (config.get_video_pullup() != 0.0f) {
1339 new_slave = new JACK_Slave (_engine.jack());
1347 request_sync_source (new_slave);
1351 Session::reverse_track_buffers ()
1353 add_post_transport_work (PostTransportReverse);
1354 _butler->schedule_transport_work ();
1358 Session::set_track_speed (Track* track, double speed)
1360 if (track->realtime_set_speed (speed, false)) {
1361 add_post_transport_work (PostTransportSpeed);
1362 _butler->schedule_transport_work ();
1368 Session::unset_play_range ()
1370 _play_range = false;
1371 _clear_event_type (SessionEvent::RangeStop);
1372 _clear_event_type (SessionEvent::RangeLocate);
1376 Session::set_play_range (list<AudioRange>& range, bool leave_rolling)
1380 /* Called from event-processing context */
1382 unset_play_range ();
1384 if (range.empty()) {
1385 /* _play_range set to false in unset_play_range()
1387 if (!leave_rolling) {
1388 /* stop transport */
1389 SessionEvent* ev = new SessionEvent (SessionEvent::SetTransportSpeed, SessionEvent::Add, SessionEvent::Immediate, 0, 0.0f, false);
1397 /* cancel loop play */
1400 list<AudioRange>::size_type sz = range.size();
1404 list<AudioRange>::iterator i = range.begin();
1405 list<AudioRange>::iterator next;
1407 while (i != range.end()) {
1412 /* locating/stopping is subject to delays for declicking.
1415 framepos_t requested_frame = i->end;
1417 if (requested_frame > current_block_size) {
1418 requested_frame -= current_block_size;
1420 requested_frame = 0;
1423 if (next == range.end()) {
1424 ev = new SessionEvent (SessionEvent::RangeStop, SessionEvent::Add, requested_frame, 0, 0.0f);
1426 ev = new SessionEvent (SessionEvent::RangeLocate, SessionEvent::Add, requested_frame, (*next).start, 0.0f);
1434 } else if (sz == 1) {
1436 ev = new SessionEvent (SessionEvent::RangeStop, SessionEvent::Add, range.front().end, 0, 0.0f);
1441 /* save range so we can do auto-return etc. */
1443 current_audio_range = range;
1445 /* now start rolling at the right place */
1447 ev = new SessionEvent (SessionEvent::LocateRoll, SessionEvent::Add, SessionEvent::Immediate, range.front().start, 0.0f, false);
1450 TransportStateChange ();
1454 Session::request_bounded_roll (framepos_t start, framepos_t end)
1456 AudioRange ar (start, end, 0);
1457 list<AudioRange> lar;
1460 request_play_range (&lar, true);
1463 Session::request_roll_at_and_return (framepos_t start, framepos_t return_to)
1465 SessionEvent *ev = new SessionEvent (SessionEvent::LocateRollLocate, SessionEvent::Add, SessionEvent::Immediate, return_to, 1.0);
1466 ev->target2_frame = start;
1471 Session::engine_halted ()
1475 /* there will be no more calls to process(), so
1476 we'd better clean up for ourselves, right now.
1478 but first, make sure the butler is out of
1482 g_atomic_int_set (&_butler->should_do_transport_work, 0);
1483 set_post_transport_work (PostTransportWork (0));
1486 realtime_stop (false, true);
1487 non_realtime_stop (false, 0, ignored);
1488 transport_sub_state = 0;
1490 TransportStateChange (); /* EMIT SIGNAL */
1495 Session::xrun_recovery ()
1497 Xrun (_transport_frame); /* EMIT SIGNAL */
1499 if (Config->get_stop_recording_on_xrun() && actively_recording()) {
1501 /* it didn't actually halt, but we need
1502 to handle things in the same way.
1510 Session::route_processors_changed (RouteProcessorChange c)
1512 if (c.type == RouteProcessorChange::MeterPointChange) {
1516 update_latency_compensation ();
1523 Session::allow_auto_play (bool yn)
1525 auto_play_legal = yn;
1529 Session::reset_jack_connection (jack_client_t* jack)
1533 if (_slave && ((js = dynamic_cast<JACK_Slave*> (_slave)) != 0)) {
1534 js->reset_client (jack);
1539 Session::maybe_stop (framepos_t limit)
1541 if ((_transport_speed > 0.0f && _transport_frame >= limit) || (_transport_speed < 0.0f && _transport_frame == 0)) {
1542 if (synced_to_jack () && config.get_jack_time_master ()) {
1543 _engine.transport_stop ();
1544 } else if (!synced_to_jack ()) {
1553 Session::send_mmc_locate (framepos_t t)
1555 Timecode::Time time;
1556 timecode_time_subframes (t, time);
1557 MIDI::Manager::instance()->mmc()->send (MIDI::MachineControlCommand (time));
1560 /** Ask the transport to not send timecode until further notice. The suspension
1561 * will come into effect some finite time after this call, and timecode_transmission_suspended()
1562 * should be checked by the caller to find out when.
1565 Session::request_suspend_timecode_transmission ()
1567 SessionEvent* ev = new SessionEvent (SessionEvent::SetTimecodeTransmission, SessionEvent::Add, SessionEvent::Immediate, 0, 0, false);
1572 Session::request_resume_timecode_transmission ()
1574 SessionEvent* ev = new SessionEvent (SessionEvent::SetTimecodeTransmission, SessionEvent::Add, SessionEvent::Immediate, 0, 0, true);
1579 Session::timecode_transmission_suspended () const
1581 return g_atomic_int_get (&_suspend_timecode_transmission) == 1;