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.
21 #include "libardour-config.h"
29 #include "pbd/error.h"
30 #include "pbd/enumwriter.h"
31 #include "pbd/pthread_utils.h"
32 #include "pbd/memento_command.h"
34 #include "midi++/mmc.h"
35 #include "midi++/port.h"
36 #include "midi++/manager.h"
38 #include "ardour/audioengine.h"
39 #include "ardour/auditioner.h"
40 #include "ardour/butler.h"
41 #include "ardour/click.h"
42 #include "ardour/debug.h"
43 #include "ardour/location.h"
44 #include "ardour/session.h"
45 #include "ardour/slave.h"
46 #include "ardour/operations.h"
51 using namespace ARDOUR;
55 Session::add_post_transport_work (PostTransportWork ptw)
57 PostTransportWork oldval;
58 PostTransportWork newval;
62 oldval = (PostTransportWork) g_atomic_int_get (&_post_transport_work);
63 newval = PostTransportWork (oldval | ptw);
64 if (g_atomic_int_compare_and_exchange (&_post_transport_work, oldval, newval)) {
70 error << "Could not set post transport work! Crazy thread madness, call the programmers" << endmsg;
74 Session::request_input_change_handling ()
76 if (!(_state_of_the_state & (InitialConnecting|Deletion))) {
77 SessionEvent* ev = new SessionEvent (SessionEvent::InputConfigurationChange, SessionEvent::Add, SessionEvent::Immediate, 0, 0.0);
83 Session::request_sync_source (Slave* new_slave)
85 SessionEvent* ev = new SessionEvent (SessionEvent::SetSyncSource, SessionEvent::Add, SessionEvent::Immediate, 0, 0.0);
88 seamless = Config->get_seamless_loop ();
90 if (dynamic_cast<JACK_Slave*>(new_slave)) {
91 /* JACK cannot support seamless looping at present */
92 Config->set_seamless_loop (false);
94 /* reset to whatever the value was before we last switched slaves */
95 Config->set_seamless_loop (_was_seamless);
98 /* save value of seamless from before the switch */
99 _was_seamless = seamless;
101 ev->slave = new_slave;
106 Session::request_transport_speed (double speed)
108 SessionEvent* ev = new SessionEvent (SessionEvent::SetTransportSpeed, SessionEvent::Add, SessionEvent::Immediate, 0, speed);
109 DEBUG_TRACE (DEBUG::Transport, string_compose ("Request transport speed = %1\n", speed));
113 /** Request a new transport speed, but if the speed parameter is exactly zero then use
114 * a very small +ve value to prevent the transport actually stopping. This method should
115 * be used by callers who are varying transport speed but don't ever want to stop it.
118 Session::request_transport_speed_nonzero (double speed)
124 request_transport_speed (speed);
128 Session::request_track_speed (Track* tr, double speed)
130 SessionEvent* ev = new SessionEvent (SessionEvent::SetTrackSpeed, SessionEvent::Add, SessionEvent::Immediate, 0, speed);
136 Session::request_stop (bool abort, bool clear_state)
138 SessionEvent* ev = new SessionEvent (SessionEvent::SetTransportSpeed, SessionEvent::Add, SessionEvent::Immediate, 0, 0.0, abort, clear_state);
139 DEBUG_TRACE (DEBUG::Transport, string_compose ("Request transport stop, abort = %1, clear state = %2\n", abort, clear_state));
144 Session::request_locate (framepos_t target_frame, bool with_roll)
146 SessionEvent *ev = new SessionEvent (with_roll ? SessionEvent::LocateRoll : SessionEvent::Locate, SessionEvent::Add, SessionEvent::Immediate, target_frame, 0, false);
147 DEBUG_TRACE (DEBUG::Transport, string_compose ("Request locate to %1\n", target_frame));
152 Session::force_locate (framepos_t target_frame, bool with_roll)
154 SessionEvent *ev = new SessionEvent (with_roll ? SessionEvent::LocateRoll : SessionEvent::Locate, SessionEvent::Add, SessionEvent::Immediate, target_frame, 0, true);
155 DEBUG_TRACE (DEBUG::Transport, string_compose ("Request forced locate to %1\n", target_frame));
160 Session::request_play_loop (bool yn, bool leave_rolling)
163 Location *location = _locations->auto_loop_location();
165 if (location == 0 && yn) {
166 error << _("Cannot loop - no loop range defined")
171 ev = new SessionEvent (SessionEvent::SetLoop, SessionEvent::Add, SessionEvent::Immediate, 0, (leave_rolling ? 1.0 : 0.0), yn);
172 DEBUG_TRACE (DEBUG::Transport, string_compose ("Request set loop = %1, leave rolling ? %2\n", yn, leave_rolling));
175 if (!leave_rolling && !yn && Config->get_seamless_loop() && transport_rolling()) {
176 // request an immediate locate to refresh the tracks
177 // after disabling looping
178 request_locate (_transport_frame-1, false);
183 Session::request_play_range (list<AudioRange>* range, bool leave_rolling)
185 SessionEvent* ev = new SessionEvent (SessionEvent::SetPlayAudioRange, SessionEvent::Add, SessionEvent::Immediate, 0, (leave_rolling ? 1.0 : 0.0));
187 ev->audio_range = *range;
189 ev->audio_range.clear ();
191 DEBUG_TRACE (DEBUG::Transport, string_compose ("Request play range, leave rolling ? %1\n", leave_rolling));
196 Session::realtime_stop (bool abort, bool clear_state)
198 DEBUG_TRACE (DEBUG::Transport, string_compose ("realtime stop @ %1\n", _transport_frame));
199 PostTransportWork todo = PostTransportWork (0);
201 /* assume that when we start, we'll be moving forwards */
203 if (_transport_speed < 0.0f) {
204 todo = (PostTransportWork (todo | PostTransportStop | PostTransportReverse));
206 todo = PostTransportWork (todo | PostTransportStop);
211 boost::shared_ptr<RouteList> r = routes.reader ();
213 for (RouteList::iterator i = r->begin (); i != r->end(); ++i) {
214 (*i)->realtime_handle_transport_stopped ();
217 if (actively_recording()) {
219 /* move the transport position back to where the
220 request for a stop was noticed. we rolled
221 past that point to pick up delayed input (and/or to delick)
224 if (worst_playback_latency() > current_block_size) {
225 /* we rolled past the stop point to pick up data that had
226 not yet arrived. move back to where the stop occured.
228 decrement_transport_position (current_block_size + (worst_input_latency() - current_block_size));
230 decrement_transport_position (current_block_size);
233 /* the duration change is not guaranteed to have happened, but is likely */
235 todo = PostTransportWork (todo | PostTransportDuration);
239 todo = PostTransportWork (todo | PostTransportAbort);
243 todo = PostTransportWork (todo | PostTransportClearSubstate);
247 add_post_transport_work (todo);
250 _clear_event_type (SessionEvent::StopOnce);
251 _clear_event_type (SessionEvent::RangeStop);
252 _clear_event_type (SessionEvent::RangeLocate);
254 /* if we're going to clear loop state, then force disabling record BUT only if we're not doing latched rec-enable */
255 disable_record (true, (!Config->get_latched_record_enable() && clear_state));
257 reset_slave_state ();
259 _transport_speed = 0;
260 _target_transport_speed = 0;
262 g_atomic_int_set (&_playback_load, 100);
263 g_atomic_int_set (&_capture_load, 100);
265 if (config.get_use_video_sync()) {
266 waiting_for_sync_offset = true;
269 transport_sub_state = 0;
273 Session::realtime_locate ()
275 boost::shared_ptr<RouteList> r = routes.reader ();
276 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
277 (*i)->realtime_locate ();
282 Session::butler_transport_work ()
286 PostTransportWork ptw;
287 boost::shared_ptr<RouteList> r = routes.reader ();
289 int on_entry = g_atomic_int_get (&_butler->should_do_transport_work);
291 ptw = post_transport_work();
293 DEBUG_TRACE (DEBUG::Transport, string_compose ("Butler transport work, todo = %1\n", enum_2_string (ptw)));
295 if (ptw & PostTransportAdjustPlaybackBuffering) {
296 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
297 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
299 tr->adjust_playback_buffering ();
300 /* and refill those buffers ... */
301 tr->non_realtime_locate (_transport_frame);
307 if (ptw & PostTransportAdjustCaptureBuffering) {
308 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
309 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
311 tr->adjust_capture_buffering ();
316 if (ptw & PostTransportCurveRealloc) {
317 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
318 (*i)->curve_reallocate();
322 if (ptw & PostTransportInputChange) {
323 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
324 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
326 tr->non_realtime_input_change ();
331 if (ptw & PostTransportSpeed) {
332 non_realtime_set_speed ();
335 if (ptw & PostTransportReverse) {
338 cumulative_rf_motion = 0;
341 /* don't seek if locate will take care of that in non_realtime_stop() */
343 if (!(ptw & PostTransportLocate)) {
345 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
346 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
347 if (tr && !tr->hidden()) {
348 tr->non_realtime_locate (_transport_frame);
350 if (on_entry != g_atomic_int_get (&_butler->should_do_transport_work)) {
351 /* new request, stop seeking, and start again */
352 g_atomic_int_dec_and_test (&_butler->should_do_transport_work);
359 if (ptw & PostTransportLocate) {
360 non_realtime_locate ();
363 if (ptw & PostTransportStop) {
364 non_realtime_stop (ptw & PostTransportAbort, on_entry, finished);
366 g_atomic_int_dec_and_test (&_butler->should_do_transport_work);
371 if (ptw & PostTransportOverWrite) {
372 non_realtime_overwrite (on_entry, finished);
374 g_atomic_int_dec_and_test (&_butler->should_do_transport_work);
379 if (ptw & PostTransportAudition) {
380 non_realtime_set_audition ();
383 g_atomic_int_dec_and_test (&_butler->should_do_transport_work);
385 DEBUG_TRACE (DEBUG::Transport, X_("Butler transport work all done\n"));
389 Session::non_realtime_set_speed ()
391 boost::shared_ptr<RouteList> rl = routes.reader();
392 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
393 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
395 tr->non_realtime_set_speed ();
401 Session::non_realtime_overwrite (int on_entry, bool& finished)
403 boost::shared_ptr<RouteList> rl = routes.reader();
404 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
405 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
406 if (tr && tr->pending_overwrite ()) {
407 tr->overwrite_existing_buffers ();
409 if (on_entry != g_atomic_int_get (&_butler->should_do_transport_work)) {
418 Session::non_realtime_locate ()
420 boost::shared_ptr<RouteList> rl = routes.reader();
421 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
422 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
424 tr->non_realtime_locate (_transport_frame);
428 /* XXX: it would be nice to generate the new clicks here (in the non-RT thread)
429 rather than clearing them so that the RT thread has to spend time constructing
430 them (in Session::click).
437 Session::non_realtime_stop (bool abort, int on_entry, bool& finished)
443 PostTransportWork ptw = post_transport_work();
448 boost::shared_ptr<RouteList> rl = routes.reader();
449 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
450 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
451 if (tr && tr->get_captured_frames () != 0) {
457 /* stop and locate are merged here because they share a lot of common stuff */
460 now = localtime (&xnow);
463 auditioner->cancel_audition ();
466 cumulative_rf_motion = 0;
470 begin_reversible_command (Operations::capture);
471 _have_captured = true;
474 DEBUG_TRACE (DEBUG::Transport, X_("Butler PTW: DS stop\n"));
476 if (abort && did_record) {
477 /* no reason to save the session file when we remove sources
479 _state_of_the_state = StateOfTheState (_state_of_the_state|InCleanup);
482 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
483 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
485 tr->transport_stopped_wallclock (*now, xnow, abort);
489 if (abort && did_record) {
490 _state_of_the_state = StateOfTheState (_state_of_the_state & ~InCleanup);
493 boost::shared_ptr<RouteList> r = routes.reader ();
495 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
496 if (!(*i)->is_hidden()) {
497 (*i)->set_pending_declick (0);
502 commit_reversible_command ();
505 if (_engine.running()) {
506 PostTransportWork ptw = post_transport_work ();
507 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
508 (*i)->nonrealtime_handle_transport_stopped (abort, (ptw & PostTransportLocate), (!(ptw & PostTransportLocate) || pending_locate_flush));
510 update_latency_compensation ();
513 bool const auto_return_enabled =
514 (!config.get_external_sync() && config.get_auto_return());
516 if (auto_return_enabled ||
517 (ptw & PostTransportLocate) ||
518 (_requested_return_frame >= 0) ||
521 if (pending_locate_flush) {
522 flush_all_inserts ();
525 if ((auto_return_enabled || synced_to_jack() || _requested_return_frame >= 0) &&
526 !(ptw & PostTransportLocate)) {
528 /* no explicit locate queued */
530 bool do_locate = false;
532 if (_requested_return_frame >= 0) {
534 /* explicit return request pre-queued in event list. overrides everything else */
536 cerr << "explicit auto-return to " << _requested_return_frame << endl;
538 _transport_frame = _requested_return_frame;
542 if (config.get_auto_return()) {
546 /* don't try to handle loop play when synced to JACK */
548 if (!synced_to_jack()) {
550 Location *location = _locations->auto_loop_location();
553 _transport_frame = location->start();
555 _transport_frame = _last_roll_location;
560 } else if (_play_range) {
562 /* return to start of range */
564 if (!current_audio_range.empty()) {
565 _transport_frame = current_audio_range.front().start;
571 /* regular auto-return */
573 _transport_frame = _last_roll_location;
579 _requested_return_frame = -1;
582 _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 if (!_engine.freewheeling()) {
618 send_full_time_code (_transport_frame);
620 if (!dynamic_cast<MTC_Slave*>(_slave)) {
621 MIDI::Manager::instance()->mmc()->send (MIDI::MachineControlCommand (MIDI::MachineControl::cmdStop));
622 send_mmc_locate (_transport_frame);
626 if ((ptw & PostTransportLocate) && get_record_enabled()) {
627 /* capture start has been changed, so save pending state */
628 save_state ("", true);
632 /* always try to get rid of this */
634 remove_pending_capture_state ();
636 /* save the current state of things if appropriate */
638 if (did_record && !saved) {
639 save_state (_current_snapshot_name);
642 if (ptw & PostTransportStop) {
647 PositionChanged (_transport_frame); /* EMIT SIGNAL */
648 TransportStateChange (); /* EMIT SIGNAL */
650 /* and start it up again if relevant */
652 if ((ptw & PostTransportLocate) && !config.get_external_sync() && pending_locate_roll) {
653 request_transport_speed (1.0);
654 pending_locate_roll = false;
659 Session::check_declick_out ()
661 bool locate_required = transport_sub_state & PendingLocate;
663 /* this is called after a process() iteration. if PendingDeclickOut was set,
664 it means that we were waiting to declick the output (which has just been
665 done) before maybe doing something else. this is where we do that "something else".
667 note: called from the audio thread.
670 if (transport_sub_state & PendingDeclickOut) {
672 if (locate_required) {
673 start_locate (pending_locate_frame, pending_locate_roll, pending_locate_flush);
674 transport_sub_state &= ~(PendingDeclickOut|PendingLocate);
676 stop_transport (pending_abort);
677 transport_sub_state &= ~(PendingDeclickOut|PendingLocate);
680 } else if (transport_sub_state & PendingLoopDeclickOut) {
681 /* Nothing else to do here; we've declicked, and the loop event will be along shortly */
682 transport_sub_state &= ~PendingLoopDeclickOut;
687 Session::unset_play_loop ()
690 clear_events (SessionEvent::AutoLoop);
691 clear_events (SessionEvent::AutoLoopDeclick);
693 // set all tracks to NOT use internal looping
694 boost::shared_ptr<RouteList> rl = routes.reader ();
695 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
696 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
697 if (tr && !tr->hidden()) {
704 Session::set_play_loop (bool yn)
706 /* Called from event-handling context */
710 if (yn == play_loop || (actively_recording() && yn) || (loc = _locations->auto_loop_location()) == 0) {
711 /* nothing to do, or can't change loop status while recording */
715 if (yn && Config->get_seamless_loop() && synced_to_jack()) {
716 warning << string_compose (
717 _("Seamless looping cannot be supported while %1 is using JACK transport.\n"
718 "Recommend changing the configured options"), PROGRAM_NAME)
731 if (Config->get_seamless_loop()) {
732 // set all tracks to use internal looping
733 boost::shared_ptr<RouteList> rl = routes.reader ();
734 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
735 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
736 if (tr && !tr->hidden()) {
742 // set all tracks to NOT use internal looping
743 boost::shared_ptr<RouteList> rl = routes.reader ();
744 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
745 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
746 if (tr && !tr->hidden()) {
752 /* Put the delick and loop events in into the event list. The declick event will
753 cause a de-clicking fade-out just before the end of the loop, and it will also result
754 in a fade-in when the loop restarts. The AutoLoop event will peform the actual loop.
759 auto_loop_declick_range (loc, dcp, dcl);
760 merge_event (new SessionEvent (SessionEvent::AutoLoopDeclick, SessionEvent::Replace, dcp, dcl, 0.0f));
761 merge_event (new SessionEvent (SessionEvent::AutoLoop, SessionEvent::Replace, loc->end(), loc->start(), 0.0f));
763 /* locate to start of loop and roll. If doing seamless loop, force a
764 locate+buffer refill even if we are positioned there already.
767 start_locate (loc->start(), true, true, false, Config->get_seamless_loop());
775 TransportStateChange ();
778 Session::flush_all_inserts ()
780 boost::shared_ptr<RouteList> r = routes.reader ();
782 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
783 (*i)->flush_processors ();
788 Session::start_locate (framepos_t target_frame, bool with_roll, bool with_flush, bool with_loop, bool force)
790 if (synced_to_jack()) {
795 _slave->speed_and_position (sp, pos);
797 if (target_frame != pos) {
799 /* tell JACK to change transport position, and we will
800 follow along later in ::follow_slave()
803 _engine.transport_locate (target_frame);
805 if (sp != 1.0f && with_roll) {
806 _engine.transport_start ();
812 locate (target_frame, with_roll, with_flush, with_loop, force);
817 Session::micro_locate (framecnt_t distance)
819 boost::shared_ptr<RouteList> rl = routes.reader();
820 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
821 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
822 if (tr && !tr->can_internal_playback_seek (distance)) {
827 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
828 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
830 tr->internal_playback_seek (distance);
834 _transport_frame += distance;
838 /** @param with_mmc true to send a MMC locate command when the locate is done */
840 Session::locate (framepos_t target_frame, bool with_roll, bool with_flush, bool with_loop, bool force, bool with_mmc)
842 if (actively_recording() && !with_loop) {
846 if (!force && _transport_frame == target_frame && !loop_changing && !with_loop) {
848 set_transport_speed (1.0, false);
850 loop_changing = false;
851 Located (); /* EMIT SIGNAL */
855 if (_transport_speed && !with_loop) {
856 /* Schedule a declick. We'll be called again when its done.
857 We only do it this way for ordinary locates, not those
861 if (!(transport_sub_state & PendingDeclickOut)) {
862 transport_sub_state |= (PendingDeclickOut|PendingLocate);
863 pending_locate_frame = target_frame;
864 pending_locate_roll = with_roll;
865 pending_locate_flush = with_flush;
870 // Update Timecode time
871 // [DR] FIXME: find out exactly where this should go below
872 _transport_frame = target_frame;
873 timecode_time(_transport_frame, transmitting_timecode_time);
874 outbound_mtc_timecode_frame = _transport_frame;
875 next_quarter_frame_to_send = 0;
877 /* do "stopped" stuff if:
880 * no autoplay in effect AND
881 * we're not going to keep rolling after the locate AND
882 * !(playing a loop with JACK sync)
886 if (transport_rolling() && (!auto_play_legal || !config.get_auto_play()) && !with_roll && !(synced_to_jack() && play_loop)) {
887 realtime_stop (false, true); // XXX paul - check if the 2nd arg is really correct
889 /* otherwise tell the world that we located */
893 if (force || !with_loop || loop_changing) {
895 PostTransportWork todo = PostTransportLocate;
898 todo = PostTransportWork (todo | PostTransportRoll);
901 add_post_transport_work (todo);
902 _butler->schedule_transport_work ();
906 /* this is functionally what clear_clicks() does but with a tentative lock */
908 Glib::RWLock::WriterLock clickm (click_lock, Glib::TRY_LOCK);
910 if (clickm.locked()) {
912 for (Clicks::iterator i = clicks.begin(); i != clicks.end(); ++i) {
921 /* switch from input if we're going to roll */
922 if (Config->get_monitoring_model() == HardwareMonitoring) {
923 set_track_monitor_input_status (!config.get_auto_input());
926 /* otherwise we're going to stop, so do the opposite */
927 if (Config->get_monitoring_model() == HardwareMonitoring) {
928 set_track_monitor_input_status (true);
932 /* cancel looped playback if transport pos outside of loop range */
934 Location* al = _locations->auto_loop_location();
936 if (al && (_transport_frame < al->start() || _transport_frame > al->end())) {
937 // cancel looping directly, this is called from event handling context
938 set_play_loop (false);
940 else if (al && _transport_frame == al->start()) {
942 // this is only necessary for seamless looping
944 boost::shared_ptr<RouteList> rl = routes.reader();
945 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
946 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
947 if (tr && tr->record_enabled ()) {
948 // tell it we've looped, so it can deal with the record state
949 tr->transport_looped(_transport_frame);
954 TransportLooped(); // EMIT SIGNAL
958 loop_changing = false;
960 _send_timecode_update = true;
963 send_mmc_locate (_transport_frame);
966 Located (); /* EMIT SIGNAL */
969 /** Set the transport speed.
970 * Called from the process thread.
971 * @param speed New speed
974 Session::set_transport_speed (double speed, bool abort, bool clear_state)
976 DEBUG_TRACE (DEBUG::Transport, string_compose ("@ %5 Set transport speed to %1, abort = %2 clear_state = %3, current = %4\n",
977 speed, abort, clear_state, _transport_speed, _transport_frame));
979 if (_transport_speed == speed) {
983 if (actively_recording() && speed != 1.0 && speed != 0.0) {
984 /* no varispeed during recording */
988 _target_transport_speed = fabs(speed);
990 /* 8.0 max speed is somewhat arbitrary but based on guestimates regarding disk i/o capability
991 and user needs. We really need CD-style "skip" playback for ffwd and rewind.
995 speed = min (8.0, speed);
996 } else if (speed < 0) {
997 speed = max (-8.0, speed);
1000 if (transport_rolling() && speed == 0.0) {
1002 /* we are rolling and we want to stop */
1004 if (Config->get_monitoring_model() == HardwareMonitoring) {
1005 set_track_monitor_input_status (true);
1008 if (synced_to_jack ()) {
1010 /* do this here because our response to the slave won't
1013 _play_range = false;
1016 _engine.transport_stop ();
1018 stop_transport (abort);
1023 } else if (transport_stopped() && speed == 1.0) {
1025 /* we are stopped and we want to start rolling at speed 1 */
1027 if (Config->get_monitoring_model() == HardwareMonitoring && config.get_auto_input()) {
1028 set_track_monitor_input_status (false);
1031 if (synced_to_jack()) {
1032 _engine.transport_start ();
1039 /* not zero, not 1.0 ... varispeed */
1041 if ((synced_to_jack()) && speed != 0.0 && speed != 1.0) {
1042 warning << string_compose (
1043 _("Global varispeed cannot be supported while %1 is connected to JACK transport control"),
1049 if (actively_recording()) {
1053 if (speed > 0.0 && _transport_frame == current_end_frame()) {
1057 if (speed < 0.0 && _transport_frame == 0) {
1063 /* if we are reversing relative to the current speed, or relative to the speed
1064 before the last stop, then we have to do extra work.
1067 PostTransportWork todo = PostTransportWork (0);
1069 if ((_transport_speed && speed * _transport_speed < 0.0) || (_last_transport_speed * speed < 0.0) || (_last_transport_speed == 0.0f && speed < 0.0f)) {
1070 todo = PostTransportWork (todo | PostTransportReverse);
1071 _last_roll_or_reversal_location = _transport_frame;
1074 _last_transport_speed = _transport_speed;
1075 _transport_speed = speed;
1077 boost::shared_ptr<RouteList> rl = routes.reader();
1078 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
1079 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
1080 if (tr && tr->realtime_set_speed (tr->speed(), true)) {
1081 todo = PostTransportWork (todo | PostTransportSpeed);
1086 add_post_transport_work (todo);
1087 _butler->schedule_transport_work ();
1090 TransportStateChange (); /* EMIT SIGNAL */
1095 /** Stop the transport. */
1097 Session::stop_transport (bool abort, bool clear_state)
1099 if (_transport_speed == 0.0f) {
1103 if (actively_recording() && !(transport_sub_state & StopPendingCapture) && worst_input_latency() > current_block_size) {
1105 boost::shared_ptr<RouteList> rl = routes.reader();
1106 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
1107 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
1109 tr->prepare_to_stop (_transport_frame);
1113 /* we need to capture the audio that has still not yet been received by the system
1114 at the time the stop is requested, so we have to roll past that time.
1116 we want to declick before stopping, so schedule the autostop for one
1117 block before the actual end. we'll declick in the subsequent block,
1118 and then we'll really be stopped.
1121 DEBUG_TRACE (DEBUG::Transport, string_compose ("stop transport requested @ %1, scheduled for + %2 - %3 = %4, abort = %5\n",
1122 _transport_frame, _worst_input_latency, current_block_size,
1123 _transport_frame - _worst_input_latency - current_block_size,
1126 SessionEvent *ev = new SessionEvent (SessionEvent::StopOnce, SessionEvent::Replace,
1127 _transport_frame + _worst_input_latency - current_block_size,
1131 transport_sub_state |= StopPendingCapture;
1132 pending_abort = abort;
1136 if ((transport_sub_state & PendingDeclickOut) == 0) {
1138 if (!(transport_sub_state & StopPendingCapture)) {
1139 boost::shared_ptr<RouteList> rl = routes.reader();
1140 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
1141 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
1143 tr->prepare_to_stop (_transport_frame);
1148 transport_sub_state |= PendingDeclickOut;
1149 /* we'll be called again after the declick */
1150 pending_abort = abort;
1154 realtime_stop (abort, clear_state);
1155 _butler->schedule_transport_work ();
1158 /** Called from the process thread */
1160 Session::start_transport ()
1162 DEBUG_TRACE (DEBUG::Transport, "start_transport\n");
1164 _last_roll_location = _transport_frame;
1165 _last_roll_or_reversal_location = _transport_frame;
1167 have_looped = false;
1169 /* if record status is Enabled, move it to Recording. if its
1170 already Recording, move it to Disabled.
1173 switch (record_status()) {
1175 if (!config.get_punch_in()) {
1182 disable_record (false);
1190 transport_sub_state |= PendingDeclickIn;
1192 _transport_speed = 1.0;
1193 _target_transport_speed = 1.0;
1195 boost::shared_ptr<RouteList> rl = routes.reader();
1196 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
1197 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
1199 tr->realtime_set_speed (tr->speed(), true);
1201 (*i)->automation_snapshot (_transport_frame, true);
1204 if (!_engine.freewheeling()) {
1205 Timecode::Time time;
1206 timecode_time_subframes (_transport_frame, time);
1207 if (!dynamic_cast<MTC_Slave*>(_slave)) {
1208 MIDI::Manager::instance()->mmc()->send (MIDI::MachineControlCommand (MIDI::MachineControl::cmdDeferredPlay));
1212 TransportStateChange (); /* EMIT SIGNAL */
1215 /** Do any transport work in the audio thread that needs to be done after the
1216 * transport thread is finished. Audio thread, realtime safe.
1219 Session::post_transport ()
1221 PostTransportWork ptw = post_transport_work ();
1223 if (ptw & PostTransportAudition) {
1224 if (auditioner && auditioner->auditioning()) {
1225 process_function = &Session::process_audition;
1227 process_function = &Session::process_with_events;
1231 if (ptw & PostTransportStop) {
1233 transport_sub_state = 0;
1236 if (ptw & PostTransportLocate) {
1238 if (((!config.get_external_sync() && (auto_play_legal && config.get_auto_play())) && !_exporting) || (ptw & PostTransportRoll)) {
1242 transport_sub_state = 0;
1247 /* XXX is this really safe? shouldn't we just be unsetting the bits that we actually
1250 set_post_transport_work (PostTransportWork (0));
1254 Session::reset_rf_scale (framecnt_t motion)
1256 cumulative_rf_motion += motion;
1258 if (cumulative_rf_motion < 4 * _current_frame_rate) {
1260 } else if (cumulative_rf_motion < 8 * _current_frame_rate) {
1262 } else if (cumulative_rf_motion < 16 * _current_frame_rate) {
1274 Session::use_sync_source (Slave* new_slave)
1276 /* Runs in process() context */
1278 bool non_rt_required = false;
1280 /* XXX this deletion is problematic because we're in RT context */
1285 send_full_time_code (_transport_frame);
1287 boost::shared_ptr<RouteList> rl = routes.reader();
1288 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
1289 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
1290 if (tr && !tr->hidden()) {
1291 if (tr->realtime_set_speed (tr->speed(), true)) {
1292 non_rt_required = true;
1294 tr->set_slaved (_slave != 0);
1298 if (non_rt_required) {
1299 add_post_transport_work (PostTransportSpeed);
1300 _butler->schedule_transport_work ();
1307 Session::drop_sync_source ()
1309 request_sync_source (0);
1313 Session::switch_to_sync_source (SyncSource src)
1317 DEBUG_TRACE (DEBUG::Slave, string_compose ("Setting up sync source %1\n", enum_2_string (src)));
1321 if (_slave && dynamic_cast<MTC_Slave*>(_slave)) {
1326 new_slave = new MTC_Slave (*this, *MIDI::Manager::instance()->mtc_input_port());
1329 catch (failed_constructor& err) {
1335 if (_slave && dynamic_cast<MIDIClock_Slave*>(_slave)) {
1340 new_slave = new MIDIClock_Slave (*this, *MIDI::Manager::instance()->midi_clock_input_port(), 24);
1343 catch (failed_constructor& err) {
1349 if (_slave && dynamic_cast<JACK_Slave*>(_slave)) {
1353 if (config.get_video_pullup() != 0.0f) {
1357 new_slave = new JACK_Slave (_engine.jack());
1365 request_sync_source (new_slave);
1369 Session::set_track_speed (Track* track, double speed)
1371 if (track->realtime_set_speed (speed, false)) {
1372 add_post_transport_work (PostTransportSpeed);
1373 _butler->schedule_transport_work ();
1379 Session::unset_play_range ()
1381 _play_range = false;
1382 _clear_event_type (SessionEvent::RangeStop);
1383 _clear_event_type (SessionEvent::RangeLocate);
1387 Session::set_play_range (list<AudioRange>& range, bool leave_rolling)
1391 /* Called from event-processing context */
1393 unset_play_range ();
1395 if (range.empty()) {
1396 /* _play_range set to false in unset_play_range()
1398 if (!leave_rolling) {
1399 /* stop transport */
1400 SessionEvent* ev = new SessionEvent (SessionEvent::SetTransportSpeed, SessionEvent::Add, SessionEvent::Immediate, 0, 0.0f, false);
1408 /* cancel loop play */
1411 list<AudioRange>::size_type sz = range.size();
1415 list<AudioRange>::iterator i = range.begin();
1416 list<AudioRange>::iterator next;
1418 while (i != range.end()) {
1423 /* locating/stopping is subject to delays for declicking.
1426 framepos_t requested_frame = i->end;
1428 if (requested_frame > current_block_size) {
1429 requested_frame -= current_block_size;
1431 requested_frame = 0;
1434 if (next == range.end()) {
1435 ev = new SessionEvent (SessionEvent::RangeStop, SessionEvent::Add, requested_frame, 0, 0.0f);
1437 ev = new SessionEvent (SessionEvent::RangeLocate, SessionEvent::Add, requested_frame, (*next).start, 0.0f);
1445 } else if (sz == 1) {
1447 ev = new SessionEvent (SessionEvent::RangeStop, SessionEvent::Add, range.front().end, 0, 0.0f);
1452 /* save range so we can do auto-return etc. */
1454 current_audio_range = range;
1456 /* now start rolling at the right place */
1458 ev = new SessionEvent (SessionEvent::LocateRoll, SessionEvent::Add, SessionEvent::Immediate, range.front().start, 0.0f, false);
1461 TransportStateChange ();
1465 Session::request_bounded_roll (framepos_t start, framepos_t end)
1467 AudioRange ar (start, end, 0);
1468 list<AudioRange> lar;
1471 request_play_range (&lar, true);
1474 Session::request_roll_at_and_return (framepos_t start, framepos_t return_to)
1476 SessionEvent *ev = new SessionEvent (SessionEvent::LocateRollLocate, SessionEvent::Add, SessionEvent::Immediate, return_to, 1.0);
1477 ev->target2_frame = start;
1482 Session::engine_halted ()
1486 /* there will be no more calls to process(), so
1487 we'd better clean up for ourselves, right now.
1489 but first, make sure the butler is out of
1494 g_atomic_int_set (&_butler->should_do_transport_work, 0);
1495 set_post_transport_work (PostTransportWork (0));
1499 realtime_stop (false, true);
1500 non_realtime_stop (false, 0, ignored);
1501 transport_sub_state = 0;
1503 TransportStateChange (); /* EMIT SIGNAL */
1508 Session::xrun_recovery ()
1510 Xrun (_transport_frame); /* EMIT SIGNAL */
1512 if (Config->get_stop_recording_on_xrun() && actively_recording()) {
1514 /* it didn't actually halt, but we need
1515 to handle things in the same way.
1523 Session::route_processors_changed (RouteProcessorChange c)
1525 if (ignore_route_processor_changes) {
1529 if (c.type == RouteProcessorChange::MeterPointChange) {
1533 update_latency_compensation ();
1540 Session::allow_auto_play (bool yn)
1542 auto_play_legal = yn;
1546 Session::reset_jack_connection (jack_client_t* jack)
1550 if (_slave && ((js = dynamic_cast<JACK_Slave*> (_slave)) != 0)) {
1551 js->reset_client (jack);
1556 Session::maybe_stop (framepos_t limit)
1558 if ((_transport_speed > 0.0f && _transport_frame >= limit) || (_transport_speed < 0.0f && _transport_frame == 0)) {
1559 if (synced_to_jack () && config.get_jack_time_master ()) {
1560 _engine.transport_stop ();
1561 } else if (!synced_to_jack ()) {
1570 Session::send_mmc_locate (framepos_t t)
1572 if (!_engine.freewheeling()) {
1573 Timecode::Time time;
1574 timecode_time_subframes (t, time);
1575 MIDI::Manager::instance()->mmc()->send (MIDI::MachineControlCommand (time));
1579 /** Ask the transport to not send timecode until further notice. The suspension
1580 * will come into effect some finite time after this call, and timecode_transmission_suspended()
1581 * should be checked by the caller to find out when.
1584 Session::request_suspend_timecode_transmission ()
1586 SessionEvent* ev = new SessionEvent (SessionEvent::SetTimecodeTransmission, SessionEvent::Add, SessionEvent::Immediate, 0, 0, false);
1591 Session::request_resume_timecode_transmission ()
1593 SessionEvent* ev = new SessionEvent (SessionEvent::SetTimecodeTransmission, SessionEvent::Add, SessionEvent::Immediate, 0, 0, true);
1598 Session::timecode_transmission_suspended () const
1600 return g_atomic_int_get (&_suspend_timecode_transmission) == 1;