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;
102 DEBUG_TRACE (DEBUG::Slave, "sent request for new slave\n");
107 Session::request_transport_speed (double speed, bool as_default)
109 SessionEvent* ev = new SessionEvent (SessionEvent::SetTransportSpeed, SessionEvent::Add, SessionEvent::Immediate, 0, speed);
110 ev->third_yes_or_no = true;
111 DEBUG_TRACE (DEBUG::Transport, string_compose ("Request transport speed = %1 as default = %2\n", speed, as_default));
115 /** Request a new transport speed, but if the speed parameter is exactly zero then use
116 * a very small +ve value to prevent the transport actually stopping. This method should
117 * be used by callers who are varying transport speed but don't ever want to stop it.
120 Session::request_transport_speed_nonzero (double speed, bool as_default)
126 request_transport_speed (speed, as_default);
130 Session::request_track_speed (Track* tr, double speed)
132 SessionEvent* ev = new SessionEvent (SessionEvent::SetTrackSpeed, SessionEvent::Add, SessionEvent::Immediate, 0, speed);
138 Session::request_stop (bool abort, bool clear_state)
140 SessionEvent* ev = new SessionEvent (SessionEvent::SetTransportSpeed, SessionEvent::Add, SessionEvent::Immediate, 0, 0.0, abort, clear_state);
141 DEBUG_TRACE (DEBUG::Transport, string_compose ("Request transport stop, abort = %1, clear state = %2\n", abort, clear_state));
146 Session::request_locate (framepos_t target_frame, bool with_roll)
148 SessionEvent *ev = new SessionEvent (with_roll ? SessionEvent::LocateRoll : SessionEvent::Locate, SessionEvent::Add, SessionEvent::Immediate, target_frame, 0, false);
149 DEBUG_TRACE (DEBUG::Transport, string_compose ("Request locate to %1\n", target_frame));
154 Session::force_locate (framepos_t target_frame, bool with_roll)
156 SessionEvent *ev = new SessionEvent (with_roll ? SessionEvent::LocateRoll : SessionEvent::Locate, SessionEvent::Add, SessionEvent::Immediate, target_frame, 0, true);
157 DEBUG_TRACE (DEBUG::Transport, string_compose ("Request forced locate to %1\n", target_frame));
162 Session::request_play_loop (bool yn, bool leave_rolling)
165 Location *location = _locations->auto_loop_location();
167 if (location == 0 && yn) {
168 error << _("Cannot loop - no loop range defined")
173 ev = new SessionEvent (SessionEvent::SetLoop, SessionEvent::Add, SessionEvent::Immediate, 0, (leave_rolling ? 1.0 : 0.0), yn);
174 DEBUG_TRACE (DEBUG::Transport, string_compose ("Request set loop = %1, leave rolling ? %2\n", yn, leave_rolling));
177 if (!leave_rolling && !yn && Config->get_seamless_loop() && transport_rolling()) {
178 // request an immediate locate to refresh the tracks
179 // after disabling looping
180 request_locate (_transport_frame-1, false);
185 Session::request_play_range (list<AudioRange>* range, bool leave_rolling)
187 SessionEvent* ev = new SessionEvent (SessionEvent::SetPlayAudioRange, SessionEvent::Add, SessionEvent::Immediate, 0, (leave_rolling ? 1.0 : 0.0));
189 ev->audio_range = *range;
191 ev->audio_range.clear ();
193 DEBUG_TRACE (DEBUG::Transport, string_compose ("Request play range, leave rolling ? %1\n", leave_rolling));
198 Session::realtime_stop (bool abort, bool clear_state)
200 DEBUG_TRACE (DEBUG::Transport, string_compose ("realtime stop @ %1\n", _transport_frame));
201 PostTransportWork todo = PostTransportWork (0);
203 /* assume that when we start, we'll be moving forwards */
205 if (_transport_speed < 0.0f) {
206 todo = (PostTransportWork (todo | PostTransportStop | PostTransportReverse));
208 todo = PostTransportWork (todo | PostTransportStop);
213 boost::shared_ptr<RouteList> r = routes.reader ();
215 for (RouteList::iterator i = r->begin (); i != r->end(); ++i) {
216 (*i)->realtime_handle_transport_stopped ();
219 if (actively_recording()) {
221 /* move the transport position back to where the
222 request for a stop was noticed. we rolled
223 past that point to pick up delayed input (and/or to delick)
226 if (worst_playback_latency() > current_block_size) {
227 /* we rolled past the stop point to pick up data that had
228 not yet arrived. move back to where the stop occured.
230 decrement_transport_position (current_block_size + (worst_input_latency() - current_block_size));
232 decrement_transport_position (current_block_size);
235 /* the duration change is not guaranteed to have happened, but is likely */
237 todo = PostTransportWork (todo | PostTransportDuration);
241 todo = PostTransportWork (todo | PostTransportAbort);
245 todo = PostTransportWork (todo | PostTransportClearSubstate);
249 add_post_transport_work (todo);
252 _clear_event_type (SessionEvent::StopOnce);
253 _clear_event_type (SessionEvent::RangeStop);
254 _clear_event_type (SessionEvent::RangeLocate);
256 /* if we're going to clear loop state, then force disabling record BUT only if we're not doing latched rec-enable */
257 disable_record (true, (!Config->get_latched_record_enable() && clear_state));
259 reset_slave_state ();
261 _transport_speed = 0;
262 _target_transport_speed = 0;
264 g_atomic_int_set (&_playback_load, 100);
265 g_atomic_int_set (&_capture_load, 100);
267 if (config.get_use_video_sync()) {
268 waiting_for_sync_offset = true;
271 transport_sub_state = 0;
275 Session::realtime_locate ()
277 boost::shared_ptr<RouteList> r = routes.reader ();
278 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
279 (*i)->realtime_locate ();
284 Session::butler_transport_work ()
288 PostTransportWork ptw;
289 boost::shared_ptr<RouteList> r = routes.reader ();
291 int on_entry = g_atomic_int_get (&_butler->should_do_transport_work);
293 ptw = post_transport_work();
295 DEBUG_TRACE (DEBUG::Transport, string_compose ("Butler transport work, todo = %1\n", enum_2_string (ptw)));
297 if (ptw & PostTransportAdjustPlaybackBuffering) {
298 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
299 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
301 tr->adjust_playback_buffering ();
302 /* and refill those buffers ... */
304 (*i)->non_realtime_locate (_transport_frame);
309 if (ptw & PostTransportAdjustCaptureBuffering) {
310 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
311 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
313 tr->adjust_capture_buffering ();
318 if (ptw & PostTransportCurveRealloc) {
319 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
320 (*i)->curve_reallocate();
324 if (ptw & PostTransportInputChange) {
325 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
326 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
328 tr->non_realtime_input_change ();
333 if (ptw & PostTransportSpeed) {
334 non_realtime_set_speed ();
337 if (ptw & PostTransportReverse) {
340 cumulative_rf_motion = 0;
343 /* don't seek if locate will take care of that in non_realtime_stop() */
345 if (!(ptw & PostTransportLocate)) {
347 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
348 (*i)->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 (*i)->non_realtime_locate (_transport_frame);
425 /* XXX: it would be nice to generate the new clicks here (in the non-RT thread)
426 rather than clearing them so that the RT thread has to spend time constructing
427 them (in Session::click).
434 Session::non_realtime_stop (bool abort, int on_entry, bool& finished)
440 PostTransportWork ptw = post_transport_work();
445 boost::shared_ptr<RouteList> rl = routes.reader();
446 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
447 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
448 if (tr && tr->get_captured_frames () != 0) {
454 /* stop and locate are merged here because they share a lot of common stuff */
457 now = localtime (&xnow);
460 auditioner->cancel_audition ();
463 cumulative_rf_motion = 0;
467 begin_reversible_command (Operations::capture);
468 _have_captured = true;
471 DEBUG_TRACE (DEBUG::Transport, X_("Butler PTW: DS stop\n"));
473 if (abort && did_record) {
474 /* no reason to save the session file when we remove sources
476 _state_of_the_state = StateOfTheState (_state_of_the_state|InCleanup);
479 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
480 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
482 tr->transport_stopped_wallclock (*now, xnow, abort);
486 if (abort && did_record) {
487 _state_of_the_state = StateOfTheState (_state_of_the_state & ~InCleanup);
490 boost::shared_ptr<RouteList> r = routes.reader ();
492 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
493 if (!(*i)->is_hidden()) {
494 (*i)->set_pending_declick (0);
499 commit_reversible_command ();
502 if (_engine.running()) {
503 PostTransportWork ptw = post_transport_work ();
504 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
505 (*i)->nonrealtime_handle_transport_stopped (abort, (ptw & PostTransportLocate), (!(ptw & PostTransportLocate) || pending_locate_flush));
507 update_latency_compensation ();
510 bool const auto_return_enabled =
511 (!config.get_external_sync() && config.get_auto_return());
513 if (auto_return_enabled ||
514 (ptw & PostTransportLocate) ||
515 (_requested_return_frame >= 0) ||
518 if (pending_locate_flush) {
519 flush_all_inserts ();
522 if ((auto_return_enabled || synced_to_jack() || _requested_return_frame >= 0) &&
523 !(ptw & PostTransportLocate)) {
525 /* no explicit locate queued */
527 bool do_locate = false;
529 if (_requested_return_frame >= 0) {
531 /* explicit return request pre-queued in event list. overrides everything else */
533 cerr << "explicit auto-return to " << _requested_return_frame << endl;
535 _transport_frame = _requested_return_frame;
539 if (config.get_auto_return()) {
543 /* don't try to handle loop play when synced to JACK */
545 if (!synced_to_jack()) {
547 Location *location = _locations->auto_loop_location();
550 _transport_frame = location->start();
552 _transport_frame = _last_roll_location;
557 } else if (_play_range) {
559 /* return to start of range */
561 if (!current_audio_range.empty()) {
562 _transport_frame = current_audio_range.front().start;
568 /* regular auto-return */
570 _transport_frame = _last_roll_location;
576 _requested_return_frame = -1;
579 _engine.transport_locate (_transport_frame);
587 /* do this before seeking, because otherwise the tracks will do the wrong thing in seamless loop mode.
590 if (ptw & PostTransportClearSubstate) {
595 /* this for() block can be put inside the previous if() and has the effect of ... ??? what */
597 DEBUG_TRACE (DEBUG::Transport, X_("Butler PTW: locate\n"));
598 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
599 DEBUG_TRACE (DEBUG::Transport, string_compose ("Butler PTW: locate on %1\n", (*i)->name()));
600 (*i)->non_realtime_locate (_transport_frame);
602 if (on_entry != g_atomic_int_get (&_butler->should_do_transport_work)) {
604 /* we will be back */
611 if (!_engine.freewheeling()) {
612 send_full_time_code (_transport_frame);
614 if (!dynamic_cast<MTC_Slave*>(_slave)) {
615 MIDI::Manager::instance()->mmc()->send (MIDI::MachineControlCommand (MIDI::MachineControl::cmdStop));
616 send_mmc_locate (_transport_frame);
620 if ((ptw & PostTransportLocate) && get_record_enabled()) {
621 /* capture start has been changed, so save pending state */
622 save_state ("", true);
626 /* always try to get rid of this */
628 remove_pending_capture_state ();
630 /* save the current state of things if appropriate */
632 if (did_record && !saved) {
633 save_state (_current_snapshot_name);
636 if (ptw & PostTransportStop) {
641 PositionChanged (_transport_frame); /* EMIT SIGNAL */
642 DEBUG_TRACE (DEBUG::Transport, string_compose ("send TSC with speed = %1\n", _transport_speed));
643 TransportStateChange (); /* EMIT SIGNAL */
645 /* and start it up again if relevant */
647 if ((ptw & PostTransportLocate) && !config.get_external_sync() && pending_locate_roll) {
648 request_transport_speed (1.0);
651 /* Even if we didn't do a pending locate roll this time, we don't want it hanging
652 around for next time.
654 pending_locate_roll = false;
658 Session::check_declick_out ()
660 bool locate_required = transport_sub_state & PendingLocate;
662 /* this is called after a process() iteration. if PendingDeclickOut was set,
663 it means that we were waiting to declick the output (which has just been
664 done) before maybe doing something else. this is where we do that "something else".
666 note: called from the audio thread.
669 if (transport_sub_state & PendingDeclickOut) {
671 if (locate_required) {
672 start_locate (pending_locate_frame, pending_locate_roll, pending_locate_flush);
673 transport_sub_state &= ~(PendingDeclickOut|PendingLocate);
675 stop_transport (pending_abort);
676 transport_sub_state &= ~(PendingDeclickOut|PendingLocate);
679 } else if (transport_sub_state & PendingLoopDeclickOut) {
680 /* Nothing else to do here; we've declicked, and the loop event will be along shortly */
681 transport_sub_state &= ~PendingLoopDeclickOut;
686 Session::unset_play_loop ()
689 clear_events (SessionEvent::AutoLoop);
690 clear_events (SessionEvent::AutoLoopDeclick);
692 // set all tracks to NOT use internal looping
693 boost::shared_ptr<RouteList> rl = routes.reader ();
694 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
695 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
696 if (tr && !tr->hidden()) {
703 Session::set_play_loop (bool yn)
705 /* Called from event-handling context */
709 if (yn == play_loop || (actively_recording() && yn) || (loc = _locations->auto_loop_location()) == 0) {
710 /* nothing to do, or can't change loop status while recording */
714 if (yn && Config->get_seamless_loop() && synced_to_jack()) {
715 warning << string_compose (
716 _("Seamless looping cannot be supported while %1 is using JACK transport.\n"
717 "Recommend changing the configured options"), PROGRAM_NAME)
730 if (Config->get_seamless_loop()) {
731 // set all tracks to use internal looping
732 boost::shared_ptr<RouteList> rl = routes.reader ();
733 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
734 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
735 if (tr && !tr->hidden()) {
741 // set all tracks to NOT use internal looping
742 boost::shared_ptr<RouteList> rl = routes.reader ();
743 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
744 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
745 if (tr && !tr->hidden()) {
751 /* Put the delick and loop events in into the event list. The declick event will
752 cause a de-clicking fade-out just before the end of the loop, and it will also result
753 in a fade-in when the loop restarts. The AutoLoop event will peform the actual loop.
758 auto_loop_declick_range (loc, dcp, dcl);
759 merge_event (new SessionEvent (SessionEvent::AutoLoopDeclick, SessionEvent::Replace, dcp, dcl, 0.0f));
760 merge_event (new SessionEvent (SessionEvent::AutoLoop, SessionEvent::Replace, loc->end(), loc->start(), 0.0f));
762 /* locate to start of loop and roll.
764 args: positition, roll=true, flush=true, with_loop=false, force buffer refill if seamless looping
767 start_locate (loc->start(), true, true, false, Config->get_seamless_loop());
775 DEBUG_TRACE (DEBUG::Transport, string_compose ("send TSC2 with speed = %1\n", _transport_speed));
776 TransportStateChange ();
779 Session::flush_all_inserts ()
781 boost::shared_ptr<RouteList> r = routes.reader ();
783 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
784 (*i)->flush_processors ();
789 Session::start_locate (framepos_t target_frame, bool with_roll, bool with_flush, bool with_loop, bool force)
791 if (synced_to_jack()) {
796 _slave->speed_and_position (sp, pos);
798 if (target_frame != pos) {
800 if (config.get_jack_time_master()) {
801 /* actually locate now, since otherwise jack_timebase_callback
802 will use the incorrect _transport_frame and report an old
803 and incorrect time to Jack transport
805 locate (target_frame, with_roll, with_flush, with_loop, force);
808 /* tell JACK to change transport position, and we will
809 follow along later in ::follow_slave()
812 _engine.transport_locate (target_frame);
814 if (sp != 1.0f && with_roll) {
815 _engine.transport_start ();
821 locate (target_frame, with_roll, with_flush, with_loop, force);
826 Session::micro_locate (framecnt_t distance)
828 boost::shared_ptr<RouteList> rl = routes.reader();
829 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
830 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
831 if (tr && !tr->can_internal_playback_seek (distance)) {
836 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
837 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
839 tr->internal_playback_seek (distance);
843 _transport_frame += distance;
847 /** @param with_mmc true to send a MMC locate command when the locate is done */
849 Session::locate (framepos_t target_frame, bool with_roll, bool with_flush, bool for_seamless_loop, bool force, bool with_mmc)
851 /* Locates for seamless looping are fairly different from other
852 * locates. They assume that the diskstream buffers for each track
853 * already have the correct data in them, and thus there is no need to
854 * actually tell the tracks to locate. What does need to be done,
855 * though, is all the housekeeping that is associated with non-linear
856 * changes in the value of _transport_frame.
859 if (actively_recording() && !for_seamless_loop) {
863 if (!force && _transport_frame == target_frame && !loop_changing && !for_seamless_loop) {
865 set_transport_speed (1.0, false);
867 loop_changing = false;
868 Located (); /* EMIT SIGNAL */
872 if (_transport_speed && !for_seamless_loop) {
873 /* Schedule a declick. We'll be called again when its done.
874 We only do it this way for ordinary locates, not those
875 due to **seamless** loops.
878 if (!(transport_sub_state & PendingDeclickOut)) {
879 transport_sub_state |= (PendingDeclickOut|PendingLocate);
880 pending_locate_frame = target_frame;
881 pending_locate_roll = with_roll;
882 pending_locate_flush = with_flush;
887 // Update Timecode time
888 // [DR] FIXME: find out exactly where this should go below
889 _transport_frame = target_frame;
890 _last_roll_or_reversal_location = target_frame;
891 timecode_time(_transport_frame, transmitting_timecode_time);
892 outbound_mtc_timecode_frame = _transport_frame;
893 next_quarter_frame_to_send = 0;
895 /* do "stopped" stuff if:
898 * no autoplay in effect AND
899 * we're not going to keep rolling after the locate AND
900 * !(playing a loop with JACK sync)
904 bool transport_was_stopped = !transport_rolling();
906 if (transport_was_stopped && (!auto_play_legal || !config.get_auto_play()) && !with_roll && !(synced_to_jack() && play_loop)) {
907 realtime_stop (false, true); // XXX paul - check if the 2nd arg is really correct
908 transport_was_stopped = true;
910 /* otherwise tell the world that we located */
914 if (force || !for_seamless_loop || loop_changing) {
916 PostTransportWork todo = PostTransportLocate;
918 if (with_roll && transport_was_stopped) {
919 todo = PostTransportWork (todo | PostTransportRoll);
922 add_post_transport_work (todo);
923 _butler->schedule_transport_work ();
927 /* this is functionally what clear_clicks() does but with a tentative lock */
929 Glib::Threads::RWLock::WriterLock clickm (click_lock, Glib::Threads::TRY_LOCK);
931 if (clickm.locked()) {
933 for (Clicks::iterator i = clicks.begin(); i != clicks.end(); ++i) {
942 /* switch from input if we're going to roll */
943 if (Config->get_monitoring_model() == HardwareMonitoring) {
944 set_track_monitor_input_status (!config.get_auto_input());
947 /* otherwise we're going to stop, so do the opposite */
948 if (Config->get_monitoring_model() == HardwareMonitoring) {
949 set_track_monitor_input_status (true);
953 /* cancel looped playback if transport pos outside of loop range */
956 Location* al = _locations->auto_loop_location();
959 if (_transport_frame < al->start() || _transport_frame > al->end()) {
961 // located outside the loop: cancel looping directly, this is called from event handling context
963 set_play_loop (false);
965 } else if (_transport_frame == al->start()) {
967 // located to start of loop - this is looping, basically
969 if (for_seamless_loop) {
971 // this is only necessary for seamless looping
973 boost::shared_ptr<RouteList> rl = routes.reader();
975 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
976 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
978 if (tr && tr->record_enabled ()) {
979 // tell it we've looped, so it can deal with the record state
980 tr->transport_looped (_transport_frame);
986 TransportLooped(); // EMIT SIGNAL
991 loop_changing = false;
993 _send_timecode_update = true;
996 send_mmc_locate (_transport_frame);
999 Located (); /* EMIT SIGNAL */
1002 /** Set the transport speed.
1003 * Called from the process thread.
1004 * @param speed New speed
1007 Session::set_transport_speed (double speed, bool abort, bool clear_state, bool as_default)
1009 DEBUG_TRACE (DEBUG::Transport, string_compose ("@ %5 Set transport speed to %1, abort = %2 clear_state = %3, current = %4 as_default %6\n",
1010 speed, abort, clear_state, _transport_speed, _transport_frame, as_default));
1012 if (_transport_speed == speed) {
1013 if (as_default && speed == 0.0) { // => reset default transport speed. hacky or what?
1014 _default_transport_speed = 1.0;
1019 if (actively_recording() && speed != 1.0 && speed != 0.0) {
1020 /* no varispeed during recording */
1021 DEBUG_TRACE (DEBUG::Transport, string_compose ("No varispeed during recording cur_speed %1, frame %2\n",
1022 _transport_speed, _transport_frame));
1026 _target_transport_speed = fabs(speed);
1028 /* 8.0 max speed is somewhat arbitrary but based on guestimates regarding disk i/o capability
1029 and user needs. We really need CD-style "skip" playback for ffwd and rewind.
1033 speed = min (8.0, speed);
1034 } else if (speed < 0) {
1035 speed = max (-8.0, speed);
1038 if (transport_rolling() && speed == 0.0) {
1040 /* we are rolling and we want to stop */
1042 if (Config->get_monitoring_model() == HardwareMonitoring) {
1043 set_track_monitor_input_status (true);
1046 if (synced_to_jack ()) {
1048 /* do this here because our response to the slave won't
1051 _play_range = false;
1054 _engine.transport_stop ();
1056 stop_transport (abort);
1061 } else if (transport_stopped() && speed == 1.0) {
1063 /* we are stopped and we want to start rolling at speed 1 */
1065 if (Config->get_monitoring_model() == HardwareMonitoring && config.get_auto_input()) {
1066 set_track_monitor_input_status (false);
1069 if (synced_to_jack()) {
1070 _engine.transport_start ();
1077 /* not zero, not 1.0 ... varispeed */
1079 if ((synced_to_jack()) && speed != 0.0 && speed != 1.0) {
1080 warning << string_compose (
1081 _("Global varispeed cannot be supported while %1 is connected to JACK transport control"),
1087 if (actively_recording()) {
1091 if (speed > 0.0 && _transport_frame == current_end_frame()) {
1095 if (speed < 0.0 && _transport_frame == 0) {
1101 /* if we are reversing relative to the current speed, or relative to the speed
1102 before the last stop, then we have to do extra work.
1105 PostTransportWork todo = PostTransportWork (0);
1107 if ((_transport_speed && speed * _transport_speed < 0.0) || (_last_transport_speed * speed < 0.0) || (_last_transport_speed == 0.0f && speed < 0.0f)) {
1108 todo = PostTransportWork (todo | PostTransportReverse);
1109 _last_roll_or_reversal_location = _transport_frame;
1112 _last_transport_speed = _transport_speed;
1113 _transport_speed = speed;
1116 _default_transport_speed = speed;
1119 boost::shared_ptr<RouteList> rl = routes.reader();
1120 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
1121 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
1122 if (tr && tr->realtime_set_speed (tr->speed(), true)) {
1123 todo = PostTransportWork (todo | PostTransportSpeed);
1128 add_post_transport_work (todo);
1129 _butler->schedule_transport_work ();
1132 DEBUG_TRACE (DEBUG::Transport, string_compose ("send TSC3 with speed = %1\n", _transport_speed));
1133 TransportStateChange (); /* EMIT SIGNAL */
1138 /** Stop the transport. */
1140 Session::stop_transport (bool abort, bool clear_state)
1142 if (_transport_speed == 0.0f) {
1146 if (actively_recording() && !(transport_sub_state & StopPendingCapture) && worst_input_latency() > current_block_size) {
1148 boost::shared_ptr<RouteList> rl = routes.reader();
1149 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
1150 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
1152 tr->prepare_to_stop (_transport_frame);
1156 /* we need to capture the audio that has still not yet been received by the system
1157 at the time the stop is requested, so we have to roll past that time.
1159 we want to declick before stopping, so schedule the autostop for one
1160 block before the actual end. we'll declick in the subsequent block,
1161 and then we'll really be stopped.
1164 DEBUG_TRACE (DEBUG::Transport, string_compose ("stop transport requested @ %1, scheduled for + %2 - %3 = %4, abort = %5\n",
1165 _transport_frame, _worst_input_latency, current_block_size,
1166 _transport_frame - _worst_input_latency - current_block_size,
1169 SessionEvent *ev = new SessionEvent (SessionEvent::StopOnce, SessionEvent::Replace,
1170 _transport_frame + _worst_input_latency - current_block_size,
1174 transport_sub_state |= StopPendingCapture;
1175 pending_abort = abort;
1179 if ((transport_sub_state & PendingDeclickOut) == 0) {
1181 if (!(transport_sub_state & StopPendingCapture)) {
1182 boost::shared_ptr<RouteList> rl = routes.reader();
1183 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
1184 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
1186 tr->prepare_to_stop (_transport_frame);
1191 transport_sub_state |= PendingDeclickOut;
1192 /* we'll be called again after the declick */
1193 pending_abort = abort;
1197 realtime_stop (abort, clear_state);
1198 _butler->schedule_transport_work ();
1201 /** Called from the process thread */
1203 Session::start_transport ()
1205 DEBUG_TRACE (DEBUG::Transport, "start_transport\n");
1207 _last_roll_location = _transport_frame;
1208 _last_roll_or_reversal_location = _transport_frame;
1210 have_looped = false;
1212 /* if record status is Enabled, move it to Recording. if its
1213 already Recording, move it to Disabled.
1216 switch (record_status()) {
1218 if (!config.get_punch_in()) {
1225 disable_record (false);
1233 transport_sub_state |= PendingDeclickIn;
1235 _transport_speed = _default_transport_speed;
1236 _target_transport_speed = _transport_speed;
1238 boost::shared_ptr<RouteList> rl = routes.reader();
1239 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
1240 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
1242 tr->realtime_set_speed (tr->speed(), true);
1246 if (!_engine.freewheeling()) {
1247 Timecode::Time time;
1248 timecode_time_subframes (_transport_frame, time);
1249 if (!dynamic_cast<MTC_Slave*>(_slave)) {
1250 MIDI::Manager::instance()->mmc()->send (MIDI::MachineControlCommand (MIDI::MachineControl::cmdDeferredPlay));
1254 DEBUG_TRACE (DEBUG::Transport, string_compose ("send TSC4 with speed = %1\n", _transport_speed));
1255 TransportStateChange (); /* EMIT SIGNAL */
1258 /** Do any transport work in the audio thread that needs to be done after the
1259 * transport thread is finished. Audio thread, realtime safe.
1262 Session::post_transport ()
1264 PostTransportWork ptw = post_transport_work ();
1266 if (ptw & PostTransportAudition) {
1267 if (auditioner && auditioner->auditioning()) {
1268 process_function = &Session::process_audition;
1270 process_function = &Session::process_with_events;
1274 if (ptw & PostTransportStop) {
1276 transport_sub_state = 0;
1279 if (ptw & PostTransportLocate) {
1281 if (((!config.get_external_sync() && (auto_play_legal && config.get_auto_play())) && !_exporting) || (ptw & PostTransportRoll)) {
1284 transport_sub_state = 0;
1289 /* XXX is this really safe? shouldn't we just be unsetting the bits that we actually
1292 set_post_transport_work (PostTransportWork (0));
1296 Session::reset_rf_scale (framecnt_t motion)
1298 cumulative_rf_motion += motion;
1300 if (cumulative_rf_motion < 4 * _current_frame_rate) {
1302 } else if (cumulative_rf_motion < 8 * _current_frame_rate) {
1304 } else if (cumulative_rf_motion < 16 * _current_frame_rate) {
1316 Session::use_sync_source (Slave* new_slave)
1318 /* Runs in process() context */
1320 bool non_rt_required = false;
1322 /* XXX this deletion is problematic because we're in RT context */
1327 DEBUG_TRACE (DEBUG::Slave, string_compose ("set new slave to %1\n", _slave));
1329 send_full_time_code (_transport_frame);
1331 boost::shared_ptr<RouteList> rl = routes.reader();
1332 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
1333 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
1334 if (tr && !tr->hidden()) {
1335 if (tr->realtime_set_speed (tr->speed(), true)) {
1336 non_rt_required = true;
1338 tr->set_slaved (_slave != 0);
1342 if (non_rt_required) {
1343 add_post_transport_work (PostTransportSpeed);
1344 _butler->schedule_transport_work ();
1351 Session::drop_sync_source ()
1353 request_sync_source (0);
1357 Session::switch_to_sync_source (SyncSource src)
1361 DEBUG_TRACE (DEBUG::Slave, string_compose ("Setting up sync source %1\n", enum_2_string (src)));
1365 if (_slave && dynamic_cast<MTC_Slave*>(_slave)) {
1370 new_slave = new MTC_Slave (*this, *MIDI::Manager::instance()->mtc_input_port());
1373 catch (failed_constructor& err) {
1380 if (_slave && dynamic_cast<LTC_Slave*>(_slave)) {
1385 new_slave = new LTC_Slave (*this);
1388 catch (failed_constructor& err) {
1397 if (_slave && dynamic_cast<MIDIClock_Slave*>(_slave)) {
1402 new_slave = new MIDIClock_Slave (*this, *MIDI::Manager::instance()->midi_clock_input_port(), 24);
1405 catch (failed_constructor& err) {
1411 if (_slave && dynamic_cast<JACK_Slave*>(_slave)) {
1415 if (config.get_video_pullup() != 0.0f) {
1419 new_slave = new JACK_Slave (_engine.jack());
1427 request_sync_source (new_slave);
1431 Session::set_track_speed (Track* track, double speed)
1433 if (track->realtime_set_speed (speed, false)) {
1434 add_post_transport_work (PostTransportSpeed);
1435 _butler->schedule_transport_work ();
1441 Session::unset_play_range ()
1443 _play_range = false;
1444 _clear_event_type (SessionEvent::RangeStop);
1445 _clear_event_type (SessionEvent::RangeLocate);
1449 Session::set_play_range (list<AudioRange>& range, bool leave_rolling)
1453 /* Called from event-processing context */
1455 unset_play_range ();
1457 if (range.empty()) {
1458 /* _play_range set to false in unset_play_range()
1460 if (!leave_rolling) {
1461 /* stop transport */
1462 SessionEvent* ev = new SessionEvent (SessionEvent::SetTransportSpeed, SessionEvent::Add, SessionEvent::Immediate, 0, 0.0f, false);
1470 /* cancel loop play */
1473 list<AudioRange>::size_type sz = range.size();
1477 list<AudioRange>::iterator i = range.begin();
1478 list<AudioRange>::iterator next;
1480 while (i != range.end()) {
1485 /* locating/stopping is subject to delays for declicking.
1488 framepos_t requested_frame = i->end;
1490 if (requested_frame > current_block_size) {
1491 requested_frame -= current_block_size;
1493 requested_frame = 0;
1496 if (next == range.end()) {
1497 ev = new SessionEvent (SessionEvent::RangeStop, SessionEvent::Add, requested_frame, 0, 0.0f);
1499 ev = new SessionEvent (SessionEvent::RangeLocate, SessionEvent::Add, requested_frame, (*next).start, 0.0f);
1507 } else if (sz == 1) {
1509 ev = new SessionEvent (SessionEvent::RangeStop, SessionEvent::Add, range.front().end, 0, 0.0f);
1514 /* save range so we can do auto-return etc. */
1516 current_audio_range = range;
1518 /* now start rolling at the right place */
1520 ev = new SessionEvent (SessionEvent::LocateRoll, SessionEvent::Add, SessionEvent::Immediate, range.front().start, 0.0f, false);
1523 DEBUG_TRACE (DEBUG::Transport, string_compose ("send TSC5 with speed = %1\n", _transport_speed));
1524 TransportStateChange ();
1528 Session::request_bounded_roll (framepos_t start, framepos_t end)
1530 AudioRange ar (start, end, 0);
1531 list<AudioRange> lar;
1534 request_play_range (&lar, true);
1537 Session::request_roll_at_and_return (framepos_t start, framepos_t return_to)
1539 SessionEvent *ev = new SessionEvent (SessionEvent::LocateRollLocate, SessionEvent::Add, SessionEvent::Immediate, return_to, 1.0);
1540 ev->target2_frame = start;
1545 Session::engine_halted ()
1549 /* there will be no more calls to process(), so
1550 we'd better clean up for ourselves, right now.
1552 but first, make sure the butler is out of
1557 g_atomic_int_set (&_butler->should_do_transport_work, 0);
1558 set_post_transport_work (PostTransportWork (0));
1562 realtime_stop (false, true);
1563 non_realtime_stop (false, 0, ignored);
1564 transport_sub_state = 0;
1566 DEBUG_TRACE (DEBUG::Transport, string_compose ("send TSC6 with speed = %1\n", _transport_speed));
1567 TransportStateChange (); /* EMIT SIGNAL */
1572 Session::xrun_recovery ()
1574 Xrun (_transport_frame); /* EMIT SIGNAL */
1576 if (Config->get_stop_recording_on_xrun() && actively_recording()) {
1578 /* it didn't actually halt, but we need
1579 to handle things in the same way.
1587 Session::route_processors_changed (RouteProcessorChange c)
1589 if (ignore_route_processor_changes) {
1593 if (c.type == RouteProcessorChange::MeterPointChange) {
1597 update_latency_compensation ();
1604 Session::allow_auto_play (bool yn)
1606 auto_play_legal = yn;
1610 Session::reset_jack_connection (jack_client_t* jack)
1614 if (_slave && ((js = dynamic_cast<JACK_Slave*> (_slave)) != 0)) {
1615 js->reset_client (jack);
1620 Session::maybe_stop (framepos_t limit)
1622 if ((_transport_speed > 0.0f && _transport_frame >= limit) || (_transport_speed < 0.0f && _transport_frame == 0)) {
1623 if (synced_to_jack () && config.get_jack_time_master ()) {
1624 _engine.transport_stop ();
1625 } else if (!synced_to_jack ()) {
1634 Session::send_mmc_locate (framepos_t t)
1636 if (!_engine.freewheeling()) {
1637 Timecode::Time time;
1638 timecode_time_subframes (t, time);
1639 MIDI::Manager::instance()->mmc()->send (MIDI::MachineControlCommand (time));
1643 /** Ask the transport to not send timecode until further notice. The suspension
1644 * will come into effect some finite time after this call, and timecode_transmission_suspended()
1645 * should be checked by the caller to find out when.
1648 Session::request_suspend_timecode_transmission ()
1650 SessionEvent* ev = new SessionEvent (SessionEvent::SetTimecodeTransmission, SessionEvent::Add, SessionEvent::Immediate, 0, 0, false);
1655 Session::request_resume_timecode_transmission ()
1657 SessionEvent* ev = new SessionEvent (SessionEvent::SetTimecodeTransmission, SessionEvent::Add, SessionEvent::Immediate, 0, 0, true);
1662 Session::timecode_transmission_suspended () const
1664 return g_atomic_int_get (&_suspend_timecode_transmission) == 1;