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"
33 #include "pbd/stacktrace.h"
35 #include "midi++/mmc.h"
36 #include "midi++/port.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/profile.h"
45 #include "ardour/scene_changer.h"
46 #include "ardour/session.h"
47 #include "ardour/slave.h"
48 #include "ardour/operations.h"
53 using namespace ARDOUR;
57 Session::add_post_transport_work (PostTransportWork ptw)
59 PostTransportWork oldval;
60 PostTransportWork newval;
64 oldval = (PostTransportWork) g_atomic_int_get (&_post_transport_work);
65 newval = PostTransportWork (oldval | ptw);
66 if (g_atomic_int_compare_and_exchange (&_post_transport_work, oldval, newval)) {
72 error << "Could not set post transport work! Crazy thread madness, call the programmers" << endmsg;
76 Session::request_input_change_handling ()
78 if (!(_state_of_the_state & (InitialConnecting|Deletion))) {
79 SessionEvent* ev = new SessionEvent (SessionEvent::InputConfigurationChange, SessionEvent::Add, SessionEvent::Immediate, 0, 0.0);
85 Session::request_sync_source (Slave* new_slave)
87 SessionEvent* ev = new SessionEvent (SessionEvent::SetSyncSource, SessionEvent::Add, SessionEvent::Immediate, 0, 0.0);
90 seamless = Config->get_seamless_loop ();
92 if (dynamic_cast<Engine_Slave*>(new_slave)) {
93 /* JACK cannot support seamless looping at present */
94 Config->set_seamless_loop (false);
96 /* reset to whatever the value was before we last switched slaves */
97 Config->set_seamless_loop (_was_seamless);
100 /* save value of seamless from before the switch */
101 _was_seamless = seamless;
103 ev->slave = new_slave;
104 DEBUG_TRACE (DEBUG::Slave, "sent request for new slave\n");
109 Session::request_transport_speed (double speed, bool as_default)
111 SessionEvent* ev = new SessionEvent (SessionEvent::SetTransportSpeed, SessionEvent::Add, SessionEvent::Immediate, 0, speed);
112 ev->third_yes_or_no = true;
113 DEBUG_TRACE (DEBUG::Transport, string_compose ("Request transport speed = %1 as default = %2\n", speed, as_default));
117 /** Request a new transport speed, but if the speed parameter is exactly zero then use
118 * a very small +ve value to prevent the transport actually stopping. This method should
119 * be used by callers who are varying transport speed but don't ever want to stop it.
122 Session::request_transport_speed_nonzero (double speed, bool as_default)
128 request_transport_speed (speed, as_default);
132 Session::request_track_speed (Track* tr, double speed)
134 SessionEvent* ev = new SessionEvent (SessionEvent::SetTrackSpeed, SessionEvent::Add, SessionEvent::Immediate, 0, speed);
140 Session::request_stop (bool abort, bool clear_state)
142 SessionEvent* ev = new SessionEvent (SessionEvent::SetTransportSpeed, SessionEvent::Add, SessionEvent::Immediate, 0, 0.0, abort, clear_state);
143 DEBUG_TRACE (DEBUG::Transport, string_compose ("Request transport stop, abort = %1, clear state = %2\n", abort, clear_state));
148 Session::request_locate (framepos_t target_frame, bool with_roll)
150 SessionEvent *ev = new SessionEvent (with_roll ? SessionEvent::LocateRoll : SessionEvent::Locate, SessionEvent::Add, SessionEvent::Immediate, target_frame, 0, false);
151 DEBUG_TRACE (DEBUG::Transport, string_compose ("Request locate to %1\n", target_frame));
156 Session::force_locate (framepos_t target_frame, bool with_roll)
158 SessionEvent *ev = new SessionEvent (with_roll ? SessionEvent::LocateRoll : SessionEvent::Locate, SessionEvent::Add, SessionEvent::Immediate, target_frame, 0, true);
159 DEBUG_TRACE (DEBUG::Transport, string_compose ("Request forced locate to %1\n", target_frame));
164 Session::request_play_loop (bool yn, bool change_transport_roll)
167 Location *location = _locations->auto_loop_location();
170 if (location == 0 && yn) {
171 error << _("Cannot loop - no loop range defined")
176 if (change_transport_roll) {
177 if (transport_rolling()) {
178 /* start looping at current speed */
179 target_speed = transport_speed ();
181 /* currently stopped */
183 /* start looping at normal speed */
190 /* leave the speed alone */
191 target_speed = transport_speed ();
194 ev = new SessionEvent (SessionEvent::SetLoop, SessionEvent::Add, SessionEvent::Immediate, 0, target_speed, yn);
195 DEBUG_TRACE (DEBUG::Transport, string_compose ("Request set loop = %1, change roll state ? %2\n", yn, change_transport_roll));
199 if (!change_transport_roll) {
200 if (!transport_rolling()) {
201 /* we're not changing transport state, but we do want
202 to set up position for the new loop. Don't
203 do this if we're rolling already.
205 request_locate (location->start(), false);
209 if (!change_transport_roll && Config->get_seamless_loop() && transport_rolling()) {
210 // request an immediate locate to refresh the tracks
211 // after disabling looping
212 request_locate (_transport_frame-1, false);
218 Session::request_play_range (list<AudioRange>* range, bool leave_rolling)
220 SessionEvent* ev = new SessionEvent (SessionEvent::SetPlayAudioRange, SessionEvent::Add, SessionEvent::Immediate, 0, (leave_rolling ? 1.0 : 0.0));
222 ev->audio_range = *range;
224 ev->audio_range.clear ();
226 DEBUG_TRACE (DEBUG::Transport, string_compose ("Request play range, leave rolling ? %1\n", leave_rolling));
231 Session::realtime_stop (bool abort, bool clear_state)
233 DEBUG_TRACE (DEBUG::Transport, string_compose ("realtime stop @ %1\n", _transport_frame));
234 PostTransportWork todo = PostTransportWork (0);
236 /* assume that when we start, we'll be moving forwards */
238 if (_transport_speed < 0.0f) {
239 todo = (PostTransportWork (todo | PostTransportStop | PostTransportReverse));
240 _default_transport_speed = 1.0;
242 todo = PostTransportWork (todo | PostTransportStop);
247 boost::shared_ptr<RouteList> r = routes.reader ();
249 for (RouteList::iterator i = r->begin (); i != r->end(); ++i) {
250 (*i)->realtime_handle_transport_stopped ();
253 if (actively_recording()) {
255 /* move the transport position back to where the
256 request for a stop was noticed. we rolled
257 past that point to pick up delayed input (and/or to delick)
260 if (worst_playback_latency() > current_block_size) {
261 /* we rolled past the stop point to pick up data that had
262 not yet arrived. move back to where the stop occured.
264 decrement_transport_position (current_block_size + (worst_input_latency() - current_block_size));
266 decrement_transport_position (current_block_size);
269 /* the duration change is not guaranteed to have happened, but is likely */
271 todo = PostTransportWork (todo | PostTransportDuration);
275 todo = PostTransportWork (todo | PostTransportAbort);
279 todo = PostTransportWork (todo | PostTransportClearSubstate);
283 add_post_transport_work (todo);
286 _clear_event_type (SessionEvent::StopOnce);
287 _clear_event_type (SessionEvent::RangeStop);
288 _clear_event_type (SessionEvent::RangeLocate);
290 /* if we're going to clear loop state, then force disabling record BUT only if we're not doing latched rec-enable */
291 disable_record (true, (!Config->get_latched_record_enable() && clear_state));
293 reset_slave_state ();
295 _transport_speed = 0;
296 _target_transport_speed = 0;
298 g_atomic_int_set (&_playback_load, 100);
299 g_atomic_int_set (&_capture_load, 100);
301 if (config.get_use_video_sync()) {
302 waiting_for_sync_offset = true;
305 transport_sub_state = 0;
309 Session::realtime_locate ()
311 boost::shared_ptr<RouteList> r = routes.reader ();
312 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
313 (*i)->realtime_locate ();
318 Session::butler_transport_work ()
322 PostTransportWork ptw;
323 boost::shared_ptr<RouteList> r = routes.reader ();
325 int on_entry = g_atomic_int_get (&_butler->should_do_transport_work);
327 ptw = post_transport_work();
329 DEBUG_TRACE (DEBUG::Transport, string_compose ("Butler transport work, todo = %1\n", enum_2_string (ptw)));
331 if (ptw & PostTransportAdjustPlaybackBuffering) {
332 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
333 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
335 tr->adjust_playback_buffering ();
336 /* and refill those buffers ... */
338 (*i)->non_realtime_locate (_transport_frame);
343 if (ptw & PostTransportAdjustCaptureBuffering) {
344 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
345 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
347 tr->adjust_capture_buffering ();
352 if (ptw & PostTransportCurveRealloc) {
353 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
354 (*i)->curve_reallocate();
358 if (ptw & PostTransportInputChange) {
359 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
360 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
362 tr->non_realtime_input_change ();
367 if (ptw & PostTransportSpeed) {
368 non_realtime_set_speed ();
371 if (ptw & PostTransportReverse) {
374 cumulative_rf_motion = 0;
377 /* don't seek if locate will take care of that in non_realtime_stop() */
379 if (!(ptw & PostTransportLocate)) {
381 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
382 (*i)->non_realtime_locate (_transport_frame);
384 if (on_entry != g_atomic_int_get (&_butler->should_do_transport_work)) {
385 /* new request, stop seeking, and start again */
386 g_atomic_int_dec_and_test (&_butler->should_do_transport_work);
393 if (ptw & PostTransportLocate) {
394 non_realtime_locate ();
397 if (ptw & PostTransportStop) {
398 non_realtime_stop (ptw & PostTransportAbort, on_entry, finished);
400 g_atomic_int_dec_and_test (&_butler->should_do_transport_work);
405 if (ptw & PostTransportOverWrite) {
406 non_realtime_overwrite (on_entry, finished);
408 g_atomic_int_dec_and_test (&_butler->should_do_transport_work);
413 if (ptw & PostTransportAudition) {
414 non_realtime_set_audition ();
417 g_atomic_int_dec_and_test (&_butler->should_do_transport_work);
419 DEBUG_TRACE (DEBUG::Transport, X_("Butler transport work all done\n"));
420 DEBUG_TRACE (DEBUG::Transport, X_(string_compose ("Frame %1\n", _transport_frame)));
424 Session::non_realtime_set_speed ()
426 boost::shared_ptr<RouteList> rl = routes.reader();
427 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
428 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
430 tr->non_realtime_set_speed ();
436 Session::non_realtime_overwrite (int on_entry, bool& finished)
438 boost::shared_ptr<RouteList> rl = routes.reader();
439 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
440 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
441 if (tr && tr->pending_overwrite ()) {
442 tr->overwrite_existing_buffers ();
444 if (on_entry != g_atomic_int_get (&_butler->should_do_transport_work)) {
453 Session::non_realtime_locate ()
455 boost::shared_ptr<RouteList> rl = routes.reader();
456 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
457 (*i)->non_realtime_locate (_transport_frame);
460 _scene_changer->locate (_transport_frame);
462 /* XXX: it would be nice to generate the new clicks here (in the non-RT thread)
463 rather than clearing them so that the RT thread has to spend time constructing
464 them (in Session::click).
471 Session::non_realtime_stop (bool abort, int on_entry, bool& finished)
477 PostTransportWork ptw = post_transport_work();
482 boost::shared_ptr<RouteList> rl = routes.reader();
483 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
484 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
485 if (tr && tr->get_captured_frames () != 0) {
491 /* stop and locate are merged here because they share a lot of common stuff */
494 now = localtime (&xnow);
497 auditioner->cancel_audition ();
500 cumulative_rf_motion = 0;
504 begin_reversible_command (Operations::capture);
505 _have_captured = true;
508 DEBUG_TRACE (DEBUG::Transport, X_("Butler PTW: DS stop\n"));
510 if (abort && did_record) {
511 /* no reason to save the session file when we remove sources
513 _state_of_the_state = StateOfTheState (_state_of_the_state|InCleanup);
516 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
517 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
519 tr->transport_stopped_wallclock (*now, xnow, abort);
523 if (abort && did_record) {
524 _state_of_the_state = StateOfTheState (_state_of_the_state & ~InCleanup);
527 boost::shared_ptr<RouteList> r = routes.reader ();
529 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
530 if (!(*i)->is_auditioner()) {
531 (*i)->set_pending_declick (0);
536 commit_reversible_command ();
537 /* increase take name */
538 if (config.get_track_name_take () && !config.get_take_name ().empty()) {
539 string newname = config.get_take_name();
540 config.set_take_name(bump_name_number (newname));
544 if (_engine.running()) {
545 PostTransportWork ptw = post_transport_work ();
546 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
547 (*i)->nonrealtime_handle_transport_stopped (abort, (ptw & PostTransportLocate), (!(ptw & PostTransportLocate) || pending_locate_flush));
549 update_latency_compensation ();
552 bool const auto_return_enabled =
553 (!config.get_external_sync() && config.get_auto_return());
555 if (auto_return_enabled ||
556 (ptw & PostTransportLocate) ||
557 (_requested_return_frame >= 0) ||
558 synced_to_engine()) {
560 if (pending_locate_flush) {
561 flush_all_inserts ();
564 if ((auto_return_enabled || synced_to_engine() || _requested_return_frame >= 0) &&
565 !(ptw & PostTransportLocate)) {
567 /* no explicit locate queued */
569 bool do_locate = false;
571 if (_requested_return_frame >= 0) {
573 /* explicit return request pre-queued in event list. overrides everything else */
575 _transport_frame = _requested_return_frame;
579 if (config.get_auto_return()) {
583 /* don't try to handle loop play when synced to JACK */
585 if (!synced_to_engine()) {
587 Location *location = _locations->auto_loop_location();
590 _transport_frame = location->start();
592 _transport_frame = _last_roll_location;
597 } else if (_play_range) {
599 /* return to start of range */
601 if (!current_audio_range.empty()) {
602 _transport_frame = current_audio_range.front().start;
608 /* regular auto-return */
610 _transport_frame = _last_roll_location;
616 _requested_return_frame = -1;
619 _engine.transport_locate (_transport_frame);
627 /* do this before seeking, because otherwise the tracks will do the wrong thing in seamless loop mode.
630 if (ptw & PostTransportClearSubstate) {
632 if (!Config->get_loop_is_mode()) {
637 /* this for() block can be put inside the previous if() and has the effect of ... ??? what */
639 DEBUG_TRACE (DEBUG::Transport, X_("Butler PTW: locate\n"));
640 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
641 DEBUG_TRACE (DEBUG::Transport, string_compose ("Butler PTW: locate on %1\n", (*i)->name()));
642 (*i)->non_realtime_locate (_transport_frame);
644 if (on_entry != g_atomic_int_get (&_butler->should_do_transport_work)) {
646 /* we will be back */
653 /* don't bother with this stuff if we're disconnected from the engine,
654 because there will be no process callbacks to deliver stuff from
657 if (_engine.connected() && !_engine.freewheeling()) {
658 // need to queue this in the next RT cycle
659 _send_timecode_update = true;
661 if (!dynamic_cast<MTC_Slave*>(_slave)) {
662 _mmc->send (MIDI::MachineControlCommand (MIDI::MachineControl::cmdStop));
664 /* This (::non_realtime_stop()) gets called by main
665 process thread, which will lead to confusion
666 when calling AsyncMIDIPort::write().
668 Something must be done. XXX
670 send_mmc_locate (_transport_frame);
674 if ((ptw & PostTransportLocate) && get_record_enabled()) {
675 /* This is scheduled by realtime_stop(), which is also done
676 * when a slave requests /locate/ for an initial sync.
677 * We can't hold up the slave for long with a save() here,
678 * without breaking its initial sync cycle.
680 * save state only if there's no slave or if it's not yet locked.
682 if (!_slave || !_slave->locked()) {
683 DEBUG_TRACE (DEBUG::Transport, X_("Butler PTW: pending save\n"));
684 /* capture start has been changed, so save pending state */
685 save_state ("", true);
690 /* always try to get rid of this */
692 remove_pending_capture_state ();
694 /* save the current state of things if appropriate */
696 if (did_record && !saved) {
697 save_state (_current_snapshot_name);
700 if (ptw & PostTransportStop) {
702 if (!Config->get_loop_is_mode()) {
707 PositionChanged (_transport_frame); /* EMIT SIGNAL */
708 DEBUG_TRACE (DEBUG::Transport, string_compose ("send TSC with speed = %1\n", _transport_speed));
709 TransportStateChange (); /* EMIT SIGNAL */
711 /* and start it up again if relevant */
713 if ((ptw & PostTransportLocate) && !config.get_external_sync() && pending_locate_roll) {
714 request_transport_speed (1.0);
717 /* Even if we didn't do a pending locate roll this time, we don't want it hanging
718 around for next time.
720 pending_locate_roll = false;
724 Session::check_declick_out ()
726 bool locate_required = transport_sub_state & PendingLocate;
728 /* this is called after a process() iteration. if PendingDeclickOut was set,
729 it means that we were waiting to declick the output (which has just been
730 done) before maybe doing something else. this is where we do that "something else".
732 note: called from the audio thread.
735 if (transport_sub_state & PendingDeclickOut) {
737 if (locate_required) {
738 start_locate (pending_locate_frame, pending_locate_roll, pending_locate_flush);
739 transport_sub_state &= ~(PendingDeclickOut|PendingLocate);
741 stop_transport (pending_abort);
742 transport_sub_state &= ~(PendingDeclickOut|PendingLocate);
745 } else if (transport_sub_state & PendingLoopDeclickOut) {
746 /* Nothing else to do here; we've declicked, and the loop event will be along shortly */
747 transport_sub_state &= ~PendingLoopDeclickOut;
752 Session::unset_play_loop ()
755 clear_events (SessionEvent::AutoLoop);
756 clear_events (SessionEvent::AutoLoopDeclick);
758 // set all tracks to NOT use internal looping
759 boost::shared_ptr<RouteList> rl = routes.reader ();
760 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
761 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
762 if (tr && !tr->hidden()) {
769 Session::set_play_loop (bool yn, double speed)
771 /* Called from event-handling context */
775 if (yn == play_loop || (actively_recording() && yn) || (loc = _locations->auto_loop_location()) == 0) {
776 /* nothing to do, or can't change loop status while recording */
780 if (yn && Config->get_seamless_loop() && synced_to_engine()) {
781 warning << string_compose (
782 _("Seamless looping cannot be supported while %1 is using JACK transport.\n"
783 "Recommend changing the configured options"), PROGRAM_NAME)
796 if (Config->get_seamless_loop()) {
797 // set all tracks to use internal looping
798 boost::shared_ptr<RouteList> rl = routes.reader ();
799 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
800 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
801 if (tr && !tr->hidden()) {
807 // set all tracks to NOT use internal looping
808 boost::shared_ptr<RouteList> rl = routes.reader ();
809 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
810 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
811 if (tr && !tr->hidden()) {
817 /* Put the delick and loop events in into the event list. The declick event will
818 cause a de-clicking fade-out just before the end of the loop, and it will also result
819 in a fade-in when the loop restarts. The AutoLoop event will peform the actual loop.
824 auto_loop_declick_range (loc, dcp, dcl);
825 merge_event (new SessionEvent (SessionEvent::AutoLoopDeclick, SessionEvent::Replace, dcp, dcl, 0.0f));
826 merge_event (new SessionEvent (SessionEvent::AutoLoop, SessionEvent::Replace, loc->end(), loc->start(), 0.0f));
828 /* if requested to roll, locate to start of loop and
829 * roll but ONLY if we're not already rolling.
831 args: positition, roll=true, flush=true, with_loop=false, force buffer refill if seamless looping
834 if (Config->get_loop_is_mode()) {
835 /* loop IS a transport mode: if already
836 rolling, do not locate to loop start.
838 if (!transport_rolling() && (speed != 0.0)) {
839 start_locate (loc->start(), true, true, false, Config->get_seamless_loop());
843 start_locate (loc->start(), true, true, false, Config->get_seamless_loop());
853 DEBUG_TRACE (DEBUG::Transport, string_compose ("send TSC2 with speed = %1\n", _transport_speed));
854 TransportStateChange ();
857 Session::flush_all_inserts ()
859 boost::shared_ptr<RouteList> r = routes.reader ();
861 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
862 (*i)->flush_processors ();
867 Session::start_locate (framepos_t target_frame, bool with_roll, bool with_flush, bool with_loop, bool force)
869 if (synced_to_engine()) {
874 _slave->speed_and_position (sp, pos);
876 if (target_frame != pos) {
878 if (config.get_jack_time_master()) {
879 /* actually locate now, since otherwise jack_timebase_callback
880 will use the incorrect _transport_frame and report an old
881 and incorrect time to Jack transport
883 locate (target_frame, with_roll, with_flush, with_loop, force);
886 /* tell JACK to change transport position, and we will
887 follow along later in ::follow_slave()
890 _engine.transport_locate (target_frame);
892 if (sp != 1.0f && with_roll) {
893 _engine.transport_start ();
899 locate (target_frame, with_roll, with_flush, with_loop, force);
904 Session::micro_locate (framecnt_t distance)
906 boost::shared_ptr<RouteList> rl = routes.reader();
907 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
908 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
909 if (tr && !tr->can_internal_playback_seek (distance)) {
914 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
915 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
917 tr->internal_playback_seek (distance);
921 _transport_frame += distance;
925 /** @param with_mmc true to send a MMC locate command when the locate is done */
927 Session::locate (framepos_t target_frame, bool with_roll, bool with_flush, bool for_seamless_loop, bool force, bool with_mmc)
929 /* Locates for seamless looping are fairly different from other
930 * locates. They assume that the diskstream buffers for each track
931 * already have the correct data in them, and thus there is no need to
932 * actually tell the tracks to locate. What does need to be done,
933 * though, is all the housekeeping that is associated with non-linear
934 * changes in the value of _transport_frame.
937 if (actively_recording() && !for_seamless_loop) {
941 if (!force && _transport_frame == target_frame && !loop_changing && !for_seamless_loop) {
943 set_transport_speed (1.0, false);
945 loop_changing = false;
946 Located (); /* EMIT SIGNAL */
950 if (_transport_speed && !for_seamless_loop) {
951 /* Schedule a declick. We'll be called again when its done.
952 We only do it this way for ordinary locates, not those
953 due to **seamless** loops.
956 if (!(transport_sub_state & PendingDeclickOut)) {
957 transport_sub_state |= (PendingDeclickOut|PendingLocate);
958 pending_locate_frame = target_frame;
959 pending_locate_roll = with_roll;
960 pending_locate_flush = with_flush;
965 // Update Timecode time
966 // [DR] FIXME: find out exactly where this should go below
967 _transport_frame = target_frame;
968 _last_roll_or_reversal_location = target_frame;
969 timecode_time(_transport_frame, transmitting_timecode_time);
970 outbound_mtc_timecode_frame = _transport_frame;
971 next_quarter_frame_to_send = 0;
973 /* do "stopped" stuff if:
976 * no autoplay in effect AND
977 * we're not going to keep rolling after the locate AND
978 * !(playing a loop with JACK sync)
982 bool transport_was_stopped = !transport_rolling();
984 if (transport_was_stopped && (!auto_play_legal || !config.get_auto_play()) && !with_roll && !(synced_to_engine() && play_loop)) {
985 realtime_stop (false, true); // XXX paul - check if the 2nd arg is really correct
986 transport_was_stopped = true;
988 /* otherwise tell the world that we located */
992 if (force || !for_seamless_loop || loop_changing) {
994 PostTransportWork todo = PostTransportLocate;
996 if (with_roll && transport_was_stopped) {
997 todo = PostTransportWork (todo | PostTransportRoll);
1000 add_post_transport_work (todo);
1001 _butler->schedule_transport_work ();
1005 /* this is functionally what clear_clicks() does but with a tentative lock */
1007 Glib::Threads::RWLock::WriterLock clickm (click_lock, Glib::Threads::TRY_LOCK);
1009 if (clickm.locked()) {
1011 for (Clicks::iterator i = clicks.begin(); i != clicks.end(); ++i) {
1020 /* switch from input if we're going to roll */
1021 if (Config->get_monitoring_model() == HardwareMonitoring) {
1022 set_track_monitor_input_status (!config.get_auto_input());
1025 /* otherwise we're going to stop, so do the opposite */
1026 if (Config->get_monitoring_model() == HardwareMonitoring) {
1027 set_track_monitor_input_status (true);
1031 /* cancel looped playback if transport pos outside of loop range */
1034 Location* al = _locations->auto_loop_location();
1037 if (_transport_frame < al->start() || _transport_frame > al->end()) {
1039 // located outside the loop: cancel looping directly, this is called from event handling context
1041 if (!Config->get_loop_is_mode()) {
1042 set_play_loop (false, _transport_speed);
1045 } else if (_transport_frame == al->start()) {
1047 // located to start of loop - this is looping, basically
1049 if (for_seamless_loop) {
1051 // this is only necessary for seamless looping
1053 boost::shared_ptr<RouteList> rl = routes.reader();
1055 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
1056 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
1058 if (tr && tr->record_enabled ()) {
1059 // tell it we've looped, so it can deal with the record state
1060 tr->transport_looped (_transport_frame);
1066 TransportLooped(); // EMIT SIGNAL
1071 loop_changing = false;
1073 _send_timecode_update = true;
1076 send_mmc_locate (_transport_frame);
1079 _last_roll_location = _last_roll_or_reversal_location = _transport_frame;
1080 Located (); /* EMIT SIGNAL */
1083 /** Set the transport speed.
1084 * Called from the process thread.
1085 * @param speed New speed
1088 Session::set_transport_speed (double speed, bool abort, bool clear_state, bool as_default)
1090 DEBUG_TRACE (DEBUG::Transport, string_compose ("@ %5 Set transport speed to %1, abort = %2 clear_state = %3, current = %4 as_default %6\n",
1091 speed, abort, clear_state, _transport_speed, _transport_frame, as_default));
1093 if (_transport_speed == speed) {
1094 if (as_default && speed == 0.0) { // => reset default transport speed. hacky or what?
1095 _default_transport_speed = 1.0;
1100 if (actively_recording() && speed != 1.0 && speed != 0.0) {
1101 /* no varispeed during recording */
1102 DEBUG_TRACE (DEBUG::Transport, string_compose ("No varispeed during recording cur_speed %1, frame %2\n",
1103 _transport_speed, _transport_frame));
1107 _target_transport_speed = fabs(speed);
1109 /* 8.0 max speed is somewhat arbitrary but based on guestimates regarding disk i/o capability
1110 and user needs. We really need CD-style "skip" playback for ffwd and rewind.
1114 speed = min (8.0, speed);
1115 } else if (speed < 0) {
1116 speed = max (-8.0, speed);
1119 if (transport_rolling() && speed == 0.0) {
1121 /* we are rolling and we want to stop */
1123 if (Config->get_monitoring_model() == HardwareMonitoring) {
1124 set_track_monitor_input_status (true);
1127 if (synced_to_engine ()) {
1129 /* do this here because our response to the slave won't
1132 _play_range = false;
1135 _engine.transport_stop ();
1137 stop_transport (abort);
1140 if (!Config->get_loop_is_mode()) {
1144 } else if (transport_stopped() && speed == 1.0) {
1146 /* we are stopped and we want to start rolling at speed 1 */
1148 if (Config->get_loop_is_mode() && play_loop) {
1150 Location *location = _locations->auto_loop_location();
1152 if (location != 0) {
1153 if (_transport_frame != location->start()) {
1154 /* jump to start and then roll from there */
1155 request_locate (location->start(), true);
1161 if (Config->get_monitoring_model() == HardwareMonitoring && config.get_auto_input()) {
1162 set_track_monitor_input_status (false);
1165 if (synced_to_engine()) {
1166 _engine.transport_start ();
1173 /* not zero, not 1.0 ... varispeed */
1175 if ((synced_to_engine()) && speed != 0.0 && speed != 1.0) {
1176 warning << string_compose (
1177 _("Global varispeed cannot be supported while %1 is connected to JACK transport control"),
1183 if (actively_recording()) {
1187 if (speed > 0.0 && _transport_frame == current_end_frame()) {
1191 if (speed < 0.0 && _transport_frame == 0) {
1197 /* if we are reversing relative to the current speed, or relative to the speed
1198 before the last stop, then we have to do extra work.
1201 PostTransportWork todo = PostTransportWork (0);
1203 if ((_transport_speed && speed * _transport_speed < 0.0) || (_last_transport_speed * speed < 0.0) || (_last_transport_speed == 0.0f && speed < 0.0f)) {
1204 todo = PostTransportWork (todo | PostTransportReverse);
1205 _last_roll_or_reversal_location = _transport_frame;
1208 _last_transport_speed = _transport_speed;
1209 _transport_speed = speed;
1212 _default_transport_speed = speed;
1215 boost::shared_ptr<RouteList> rl = routes.reader();
1216 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
1217 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
1218 if (tr && tr->realtime_set_speed (tr->speed(), true)) {
1219 todo = PostTransportWork (todo | PostTransportSpeed);
1224 add_post_transport_work (todo);
1225 _butler->schedule_transport_work ();
1228 DEBUG_TRACE (DEBUG::Transport, string_compose ("send TSC3 with speed = %1\n", _transport_speed));
1229 TransportStateChange (); /* EMIT SIGNAL */
1234 /** Stop the transport. */
1236 Session::stop_transport (bool abort, bool clear_state)
1238 if (_transport_speed == 0.0f) {
1242 if (actively_recording() && !(transport_sub_state & StopPendingCapture) && worst_input_latency() > current_block_size) {
1244 boost::shared_ptr<RouteList> rl = routes.reader();
1245 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
1246 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
1248 tr->prepare_to_stop (_transport_frame);
1252 /* we need to capture the audio that has still not yet been received by the system
1253 at the time the stop is requested, so we have to roll past that time.
1255 we want to declick before stopping, so schedule the autostop for one
1256 block before the actual end. we'll declick in the subsequent block,
1257 and then we'll really be stopped.
1260 DEBUG_TRACE (DEBUG::Transport, string_compose ("stop transport requested @ %1, scheduled for + %2 - %3 = %4, abort = %5\n",
1261 _transport_frame, _worst_input_latency, current_block_size,
1262 _transport_frame - _worst_input_latency - current_block_size,
1265 SessionEvent *ev = new SessionEvent (SessionEvent::StopOnce, SessionEvent::Replace,
1266 _transport_frame + _worst_input_latency - current_block_size,
1270 transport_sub_state |= StopPendingCapture;
1271 pending_abort = abort;
1275 if ((transport_sub_state & PendingDeclickOut) == 0) {
1277 if (!(transport_sub_state & StopPendingCapture)) {
1278 boost::shared_ptr<RouteList> rl = routes.reader();
1279 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
1280 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
1282 tr->prepare_to_stop (_transport_frame);
1287 transport_sub_state |= PendingDeclickOut;
1288 /* we'll be called again after the declick */
1289 pending_abort = abort;
1293 realtime_stop (abort, clear_state);
1294 _butler->schedule_transport_work ();
1297 /** Called from the process thread */
1299 Session::start_transport ()
1301 DEBUG_TRACE (DEBUG::Transport, "start_transport\n");
1303 _last_roll_location = _transport_frame;
1304 _last_roll_or_reversal_location = _transport_frame;
1306 have_looped = false;
1308 /* if record status is Enabled, move it to Recording. if its
1309 already Recording, move it to Disabled.
1312 switch (record_status()) {
1314 if (!config.get_punch_in()) {
1321 disable_record (false);
1329 transport_sub_state |= PendingDeclickIn;
1331 _transport_speed = _default_transport_speed;
1332 _target_transport_speed = _transport_speed;
1334 boost::shared_ptr<RouteList> rl = routes.reader();
1335 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
1336 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
1338 tr->realtime_set_speed (tr->speed(), true);
1342 if (!_engine.freewheeling()) {
1343 Timecode::Time time;
1344 timecode_time_subframes (_transport_frame, time);
1345 if (!dynamic_cast<MTC_Slave*>(_slave)) {
1346 _mmc->send (MIDI::MachineControlCommand (MIDI::MachineControl::cmdDeferredPlay));
1350 DEBUG_TRACE (DEBUG::Transport, string_compose ("send TSC4 with speed = %1\n", _transport_speed));
1351 TransportStateChange (); /* EMIT SIGNAL */
1354 /** Do any transport work in the audio thread that needs to be done after the
1355 * transport thread is finished. Audio thread, realtime safe.
1358 Session::post_transport ()
1360 PostTransportWork ptw = post_transport_work ();
1362 if (ptw & PostTransportAudition) {
1363 if (auditioner && auditioner->auditioning()) {
1364 process_function = &Session::process_audition;
1366 process_function = &Session::process_with_events;
1370 if (ptw & PostTransportStop) {
1372 transport_sub_state = 0;
1375 if (ptw & PostTransportLocate) {
1377 if (((!config.get_external_sync() && (auto_play_legal && config.get_auto_play())) && !_exporting) || (ptw & PostTransportRoll)) {
1380 transport_sub_state = 0;
1385 /* XXX is this really safe? shouldn't we just be unsetting the bits that we actually
1388 set_post_transport_work (PostTransportWork (0));
1392 Session::reset_rf_scale (framecnt_t motion)
1394 cumulative_rf_motion += motion;
1396 if (cumulative_rf_motion < 4 * _current_frame_rate) {
1398 } else if (cumulative_rf_motion < 8 * _current_frame_rate) {
1400 } else if (cumulative_rf_motion < 16 * _current_frame_rate) {
1412 Session::use_sync_source (Slave* new_slave)
1414 /* Runs in process() context */
1416 bool non_rt_required = false;
1418 /* XXX this deletion is problematic because we're in RT context */
1423 DEBUG_TRACE (DEBUG::Slave, string_compose ("set new slave to %1\n", _slave));
1425 // need to queue this for next process() cycle
1426 _send_timecode_update = true;
1428 boost::shared_ptr<RouteList> rl = routes.reader();
1429 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
1430 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
1431 if (tr && !tr->hidden()) {
1432 if (tr->realtime_set_speed (tr->speed(), true)) {
1433 non_rt_required = true;
1435 tr->set_slaved (_slave != 0);
1439 if (non_rt_required) {
1440 add_post_transport_work (PostTransportSpeed);
1441 _butler->schedule_transport_work ();
1448 Session::drop_sync_source ()
1450 request_sync_source (0);
1454 Session::switch_to_sync_source (SyncSource src)
1458 DEBUG_TRACE (DEBUG::Slave, string_compose ("Setting up sync source %1\n", enum_2_string (src)));
1462 if (_slave && dynamic_cast<MTC_Slave*>(_slave)) {
1467 new_slave = new MTC_Slave (*this, *_midi_ports->mtc_input_port());
1470 catch (failed_constructor& err) {
1476 if (_slave && dynamic_cast<LTC_Slave*>(_slave)) {
1481 new_slave = new LTC_Slave (*this);
1484 catch (failed_constructor& err) {
1491 if (_slave && dynamic_cast<MIDIClock_Slave*>(_slave)) {
1496 new_slave = new MIDIClock_Slave (*this, *_midi_ports->midi_clock_input_port(), 24);
1499 catch (failed_constructor& err) {
1505 if (_slave && dynamic_cast<Engine_Slave*>(_slave)) {
1509 if (config.get_video_pullup() != 0.0f) {
1513 new_slave = new Engine_Slave (*AudioEngine::instance());
1521 request_sync_source (new_slave);
1525 Session::set_track_speed (Track* track, double speed)
1527 if (track->realtime_set_speed (speed, false)) {
1528 add_post_transport_work (PostTransportSpeed);
1529 _butler->schedule_transport_work ();
1535 Session::unset_play_range ()
1537 _play_range = false;
1538 _clear_event_type (SessionEvent::RangeStop);
1539 _clear_event_type (SessionEvent::RangeLocate);
1543 Session::set_play_range (list<AudioRange>& range, bool leave_rolling)
1547 /* Called from event-processing context */
1549 unset_play_range ();
1551 if (range.empty()) {
1552 /* _play_range set to false in unset_play_range()
1554 if (!leave_rolling) {
1555 /* stop transport */
1556 SessionEvent* ev = new SessionEvent (SessionEvent::SetTransportSpeed, SessionEvent::Add, SessionEvent::Immediate, 0, 0.0f, false);
1564 /* cancel loop play */
1567 list<AudioRange>::size_type sz = range.size();
1571 list<AudioRange>::iterator i = range.begin();
1572 list<AudioRange>::iterator next;
1574 while (i != range.end()) {
1579 /* locating/stopping is subject to delays for declicking.
1582 framepos_t requested_frame = i->end;
1584 if (requested_frame > current_block_size) {
1585 requested_frame -= current_block_size;
1587 requested_frame = 0;
1590 if (next == range.end()) {
1591 ev = new SessionEvent (SessionEvent::RangeStop, SessionEvent::Add, requested_frame, 0, 0.0f);
1593 ev = new SessionEvent (SessionEvent::RangeLocate, SessionEvent::Add, requested_frame, (*next).start, 0.0f);
1601 } else if (sz == 1) {
1603 ev = new SessionEvent (SessionEvent::RangeStop, SessionEvent::Add, range.front().end, 0, 0.0f);
1608 /* save range so we can do auto-return etc. */
1610 current_audio_range = range;
1612 /* now start rolling at the right place */
1614 ev = new SessionEvent (SessionEvent::LocateRoll, SessionEvent::Add, SessionEvent::Immediate, range.front().start, 0.0f, false);
1617 DEBUG_TRACE (DEBUG::Transport, string_compose ("send TSC5 with speed = %1\n", _transport_speed));
1618 TransportStateChange ();
1622 Session::request_bounded_roll (framepos_t start, framepos_t end)
1624 AudioRange ar (start, end, 0);
1625 list<AudioRange> lar;
1628 request_play_range (&lar, true);
1631 Session::request_roll_at_and_return (framepos_t start, framepos_t return_to)
1633 SessionEvent *ev = new SessionEvent (SessionEvent::LocateRollLocate, SessionEvent::Add, SessionEvent::Immediate, return_to, 1.0);
1634 ev->target2_frame = start;
1639 Session::engine_halted ()
1643 /* there will be no more calls to process(), so
1644 we'd better clean up for ourselves, right now.
1646 but first, make sure the butler is out of
1651 g_atomic_int_set (&_butler->should_do_transport_work, 0);
1652 set_post_transport_work (PostTransportWork (0));
1656 realtime_stop (false, true);
1657 non_realtime_stop (false, 0, ignored);
1658 transport_sub_state = 0;
1660 DEBUG_TRACE (DEBUG::Transport, string_compose ("send TSC6 with speed = %1\n", _transport_speed));
1661 TransportStateChange (); /* EMIT SIGNAL */
1666 Session::xrun_recovery ()
1668 Xrun (_transport_frame); /* EMIT SIGNAL */
1670 if (Config->get_stop_recording_on_xrun() && actively_recording()) {
1672 /* it didn't actually halt, but we need
1673 to handle things in the same way.
1681 Session::route_processors_changed (RouteProcessorChange c)
1683 if (ignore_route_processor_changes) {
1687 if (c.type == RouteProcessorChange::MeterPointChange) {
1691 update_latency_compensation ();
1698 Session::allow_auto_play (bool yn)
1700 auto_play_legal = yn;
1704 Session::maybe_stop (framepos_t limit)
1706 if ((_transport_speed > 0.0f && _transport_frame >= limit) || (_transport_speed < 0.0f && _transport_frame == 0)) {
1707 if (synced_to_engine () && config.get_jack_time_master ()) {
1708 _engine.transport_stop ();
1709 } else if (!synced_to_engine ()) {
1718 Session::send_mmc_locate (framepos_t t)
1720 if (!_engine.freewheeling()) {
1721 Timecode::Time time;
1722 timecode_time_subframes (t, time);
1723 _mmc->send (MIDI::MachineControlCommand (time));
1727 /** Ask the transport to not send timecode until further notice. The suspension
1728 * will come into effect some finite time after this call, and timecode_transmission_suspended()
1729 * should be checked by the caller to find out when.
1732 Session::request_suspend_timecode_transmission ()
1734 SessionEvent* ev = new SessionEvent (SessionEvent::SetTimecodeTransmission, SessionEvent::Add, SessionEvent::Immediate, 0, 0, false);
1739 Session::request_resume_timecode_transmission ()
1741 SessionEvent* ev = new SessionEvent (SessionEvent::SetTimecodeTransmission, SessionEvent::Add, SessionEvent::Immediate, 0, 0, true);
1746 Session::timecode_transmission_suspended () const
1748 return g_atomic_int_get (&_suspend_timecode_transmission) == 1;