2 Copyright (C) 1999-2003 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "libardour-config.h"
30 #include "pbd/error.h"
31 #include "pbd/enumwriter.h"
32 #include "pbd/pthread_utils.h"
33 #include "pbd/memento_command.h"
35 #include "midi++/mmc.h"
36 #include "midi++/port.h"
37 #include "midi++/manager.h"
39 #include "ardour/ardour.h"
40 #include "ardour/audioengine.h"
41 #include "ardour/auditioner.h"
42 #include "ardour/butler.h"
43 #include "ardour/debug.h"
44 #include "ardour/location.h"
45 #include "ardour/session.h"
46 #include "ardour/slave.h"
47 #include "ardour/operations.h"
52 using namespace ARDOUR;
56 Session::add_post_transport_work (PostTransportWork ptw)
58 PostTransportWork oldval;
59 PostTransportWork newval;
63 oldval = (PostTransportWork) g_atomic_int_get (&_post_transport_work);
64 newval = PostTransportWork (oldval | ptw);
65 if (g_atomic_int_compare_and_exchange (&_post_transport_work, oldval, newval)) {
71 error << "Could not set post transport work! Crazy thread madness, call the programmers" << endmsg;
75 Session::request_input_change_handling ()
77 if (!(_state_of_the_state & (InitialConnecting|Deletion))) {
78 SessionEvent* ev = new SessionEvent (SessionEvent::InputConfigurationChange, SessionEvent::Add, SessionEvent::Immediate, 0, 0.0);
84 Session::request_sync_source (Slave* new_slave)
86 SessionEvent* ev = new SessionEvent (SessionEvent::SetSyncSource, SessionEvent::Add, SessionEvent::Immediate, 0, 0.0);
89 seamless = Config->get_seamless_loop ();
91 if (dynamic_cast<JACK_Slave*>(new_slave)) {
92 /* JACK cannot support seamless looping at present */
93 Config->set_seamless_loop (false);
95 /* reset to whatever the value was before we last switched slaves */
96 Config->set_seamless_loop (_was_seamless);
99 /* save value of seamless from before the switch */
100 _was_seamless = seamless;
102 ev->slave = new_slave;
107 Session::request_transport_speed (double speed)
109 SessionEvent* ev = new SessionEvent (SessionEvent::SetTransportSpeed, SessionEvent::Add, SessionEvent::Immediate, 0, speed);
110 DEBUG_TRACE (DEBUG::Transport, string_compose ("Request transport speed = %1\n", speed));
114 /** Request a new transport speed, but if the speed parameter is exactly zero then use
115 * a very small +ve value to prevent the transport actually stopping. This method should
116 * be used by callers who are varying transport speed but don't ever want to stop it.
119 Session::request_transport_speed_nonzero (double speed)
125 request_transport_speed (speed);
129 Session::request_track_speed (Track* tr, double speed)
131 SessionEvent* ev = new SessionEvent (SessionEvent::SetTrackSpeed, SessionEvent::Add, SessionEvent::Immediate, 0, speed);
137 Session::request_stop (bool abort, bool clear_state)
139 SessionEvent* ev = new SessionEvent (SessionEvent::SetTransportSpeed, SessionEvent::Add, SessionEvent::Immediate, 0, 0.0, abort, clear_state);
140 DEBUG_TRACE (DEBUG::Transport, string_compose ("Request transport stop, abort = %1, clear state = %2\n", abort, clear_state));
145 Session::request_locate (framepos_t target_frame, bool with_roll)
147 SessionEvent *ev = new SessionEvent (with_roll ? SessionEvent::LocateRoll : SessionEvent::Locate, SessionEvent::Add, SessionEvent::Immediate, target_frame, 0, false);
148 DEBUG_TRACE (DEBUG::Transport, string_compose ("Request locate to %1\n", target_frame));
153 Session::force_locate (framepos_t target_frame, bool with_roll)
155 SessionEvent *ev = new SessionEvent (with_roll ? SessionEvent::LocateRoll : SessionEvent::Locate, SessionEvent::Add, SessionEvent::Immediate, target_frame, 0, true);
156 DEBUG_TRACE (DEBUG::Transport, string_compose ("Request forced locate to %1\n", target_frame));
161 Session::request_play_loop (bool yn, bool leave_rolling)
164 Location *location = _locations->auto_loop_location();
166 if (location == 0 && yn) {
167 error << _("Cannot loop - no loop range defined")
172 ev = new SessionEvent (SessionEvent::SetLoop, SessionEvent::Add, SessionEvent::Immediate, 0, (leave_rolling ? 1.0 : 0.0), yn);
173 DEBUG_TRACE (DEBUG::Transport, string_compose ("Request set loop = %1, leave rolling ? %2\n", yn, leave_rolling));
176 if (!leave_rolling && !yn && Config->get_seamless_loop() && transport_rolling()) {
177 // request an immediate locate to refresh the tracks
178 // after disabling looping
179 request_locate (_transport_frame-1, false);
184 Session::request_play_range (list<AudioRange>* range, bool leave_rolling)
186 SessionEvent* ev = new SessionEvent (SessionEvent::SetPlayAudioRange, SessionEvent::Add, SessionEvent::Immediate, 0, (leave_rolling ? 1.0 : 0.0));
188 ev->audio_range = *range;
190 ev->audio_range.clear ();
192 DEBUG_TRACE (DEBUG::Transport, string_compose ("Request play range, leave rolling ? %1\n", leave_rolling));
197 Session::realtime_stop (bool abort, bool clear_state)
199 DEBUG_TRACE (DEBUG::Transport, "realtime stop\n");
200 PostTransportWork todo = PostTransportWork (0);
202 /* assume that when we start, we'll be moving forwards */
204 if (_transport_speed < 0.0f) {
205 todo = (PostTransportWork (todo | PostTransportStop | PostTransportReverse));
207 todo = PostTransportWork (todo | PostTransportStop);
212 boost::shared_ptr<RouteList> r = routes.reader ();
214 for (RouteList::iterator i = r->begin (); i != r->end(); ++i) {
215 (*i)->realtime_handle_transport_stopped ();
218 if (actively_recording()) {
220 /* move the transport position back to where the
221 request for a stop was noticed. we rolled
222 past that point to pick up delayed input (and/or to delick)
225 if (worst_playback_latency() > current_block_size) {
226 /* we rolled past the stop point to pick up data that had
227 not yet arrived. move back to where the stop occured.
229 decrement_transport_position (current_block_size + (worst_playback_latency() - current_block_size));
231 decrement_transport_position (current_block_size);
234 /* the duration change is not guaranteed to have happened, but is likely */
236 todo = PostTransportWork (todo | PostTransportDuration);
240 todo = PostTransportWork (todo | PostTransportAbort);
244 todo = PostTransportWork (todo | PostTransportClearSubstate);
248 add_post_transport_work (todo);
251 _clear_event_type (SessionEvent::StopOnce);
252 _clear_event_type (SessionEvent::RangeStop);
253 _clear_event_type (SessionEvent::RangeLocate);
255 /* if we're going to clear loop state, then force disabling record BUT only if we're not doing latched rec-enable */
256 disable_record (true, (!Config->get_latched_record_enable() && clear_state));
258 reset_slave_state ();
260 _transport_speed = 0;
261 _target_transport_speed = 0;
263 g_atomic_int_set (&_playback_load, 100);
264 g_atomic_int_set (&_capture_load, 100);
266 if (config.get_use_video_sync()) {
267 waiting_for_sync_offset = true;
270 transport_sub_state = 0;
274 Session::butler_transport_work ()
278 PostTransportWork ptw;
279 boost::shared_ptr<RouteList> r = routes.reader ();
281 int on_entry = g_atomic_int_get (&_butler->should_do_transport_work);
283 ptw = post_transport_work();
285 DEBUG_TRACE (DEBUG::Transport, string_compose ("Butler transport work, todo = %1\n", enum_2_string (ptw)));
287 if (ptw & PostTransportAdjustPlaybackBuffering) {
288 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
289 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
291 tr->adjust_playback_buffering ();
292 /* and refill those buffers ... */
293 tr->non_realtime_locate (_transport_frame);
299 if (ptw & PostTransportAdjustCaptureBuffering) {
300 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
301 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
303 tr->adjust_capture_buffering ();
308 if (ptw & PostTransportCurveRealloc) {
309 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
310 (*i)->curve_reallocate();
314 if (ptw & PostTransportInputChange) {
315 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
316 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
318 tr->non_realtime_input_change ();
323 if (ptw & PostTransportSpeed) {
324 non_realtime_set_speed ();
327 if (ptw & PostTransportReverse) {
330 cumulative_rf_motion = 0;
333 /* don't seek if locate will take care of that in non_realtime_stop() */
335 if (!(ptw & PostTransportLocate)) {
337 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
338 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
339 if (tr && !tr->hidden()) {
340 tr->non_realtime_locate (_transport_frame);
342 if (on_entry != g_atomic_int_get (&_butler->should_do_transport_work)) {
343 /* new request, stop seeking, and start again */
344 g_atomic_int_dec_and_test (&_butler->should_do_transport_work);
351 if (ptw & PostTransportLocate) {
352 non_realtime_locate ();
355 if (ptw & PostTransportStop) {
356 non_realtime_stop (ptw & PostTransportAbort, on_entry, finished);
358 g_atomic_int_dec_and_test (&_butler->should_do_transport_work);
363 if (ptw & PostTransportOverWrite) {
364 non_realtime_overwrite (on_entry, finished);
366 g_atomic_int_dec_and_test (&_butler->should_do_transport_work);
371 if (ptw & PostTransportAudition) {
372 non_realtime_set_audition ();
375 g_atomic_int_dec_and_test (&_butler->should_do_transport_work);
377 DEBUG_TRACE (DEBUG::Transport, X_("Butler transport work all done\n"));
381 Session::non_realtime_set_speed ()
383 boost::shared_ptr<RouteList> rl = routes.reader();
384 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
385 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
387 tr->non_realtime_set_speed ();
393 Session::non_realtime_overwrite (int on_entry, bool& finished)
395 boost::shared_ptr<RouteList> rl = routes.reader();
396 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
397 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
398 if (tr && tr->pending_overwrite ()) {
399 tr->overwrite_existing_buffers ();
401 if (on_entry != g_atomic_int_get (&_butler->should_do_transport_work)) {
410 Session::non_realtime_locate ()
412 boost::shared_ptr<RouteList> rl = routes.reader();
413 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
414 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
416 tr->non_realtime_locate (_transport_frame);
423 Session::non_realtime_stop (bool abort, int on_entry, bool& finished)
429 PostTransportWork ptw = post_transport_work();
434 boost::shared_ptr<RouteList> rl = routes.reader();
435 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
436 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
437 if (tr && tr->get_captured_frames () != 0) {
443 /* stop and locate are merged here because they share a lot of common stuff */
446 now = localtime (&xnow);
449 auditioner->cancel_audition ();
453 cumulative_rf_motion = 0;
457 begin_reversible_command (Operations::capture);
458 _have_captured = true;
461 DEBUG_TRACE (DEBUG::Transport, X_("Butler PTW: DS stop\n"));
463 if (abort && did_record) {
464 /* no reason to save the session file when we remove sources
466 _state_of_the_state = StateOfTheState (_state_of_the_state|InCleanup);
469 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
470 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
472 tr->transport_stopped_wallclock (*now, xnow, abort);
476 if (abort && did_record) {
477 _state_of_the_state = StateOfTheState (_state_of_the_state & ~InCleanup);
480 boost::shared_ptr<RouteList> r = routes.reader ();
482 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
483 if (!(*i)->is_hidden()) {
484 (*i)->set_pending_declick (0);
489 commit_reversible_command ();
492 if (_engine.running()) {
493 PostTransportWork ptw = post_transport_work ();
494 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
495 (*i)->nonrealtime_handle_transport_stopped (abort, (ptw & PostTransportLocate), (!(ptw & PostTransportLocate) || pending_locate_flush));
497 update_latency_compensation ();
500 bool const auto_return_enabled =
501 (!config.get_external_sync() && config.get_auto_return());
503 if (auto_return_enabled ||
504 (ptw & PostTransportLocate) ||
505 (_requested_return_frame >= 0) ||
508 if (pending_locate_flush) {
509 flush_all_inserts ();
512 if ((auto_return_enabled || synced_to_jack() || _requested_return_frame >= 0) &&
513 !(ptw & PostTransportLocate)) {
515 /* no explicit locate queued */
517 bool do_locate = false;
519 if (_requested_return_frame >= 0) {
521 /* explicit return request pre-queued in event list. overrides everything else */
523 cerr << "explicit auto-return to " << _requested_return_frame << endl;
525 _transport_frame = _requested_return_frame;
529 if (config.get_auto_return()) {
533 /* don't try to handle loop play when synced to JACK */
535 if (!synced_to_jack()) {
537 Location *location = _locations->auto_loop_location();
540 _transport_frame = location->start();
542 _transport_frame = _last_roll_location;
547 } else if (_play_range) {
549 /* return to start of range */
551 if (!current_audio_range.empty()) {
552 _transport_frame = current_audio_range.front().start;
558 /* regular auto-return */
560 _transport_frame = _last_roll_location;
566 _requested_return_frame = -1;
569 _engine.transport_locate (_transport_frame);
575 /* do this before seeking, because otherwise the tracks will do the wrong thing in seamless loop mode.
578 if (ptw & PostTransportClearSubstate) {
583 /* this for() block can be put inside the previous if() and has the effect of ... ??? what */
585 DEBUG_TRACE (DEBUG::Transport, X_("Butler PTW: locate\n"));
586 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
587 DEBUG_TRACE (DEBUG::Transport, string_compose ("Butler PTW: locate on %1\n", (*i)->name()));
588 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
589 if (tr && !tr->hidden()) {
590 tr->non_realtime_locate (_transport_frame);
593 if (on_entry != g_atomic_int_get (&_butler->should_do_transport_work)) {
595 /* we will be back */
602 send_full_time_code (_transport_frame);
604 if (!dynamic_cast<MTC_Slave*>(_slave)) {
605 MIDI::Manager::instance()->mmc()->send (MIDI::MachineControlCommand (MIDI::MachineControl::cmdStop));
606 send_mmc_locate (_transport_frame);
609 if ((ptw & PostTransportLocate) && get_record_enabled()) {
610 /* capture start has been changed, so save pending state */
611 save_state ("", true);
615 /* always try to get rid of this */
617 remove_pending_capture_state ();
619 /* save the current state of things if appropriate */
621 if (did_record && !saved) {
622 save_state (_current_snapshot_name);
625 if (ptw & PostTransportStop) {
630 PositionChanged (_transport_frame); /* EMIT SIGNAL */
631 TransportStateChange (); /* EMIT SIGNAL */
633 /* and start it up again if relevant */
635 if ((ptw & PostTransportLocate) && !config.get_external_sync() && pending_locate_roll) {
636 request_transport_speed (1.0);
637 pending_locate_roll = false;
642 Session::check_declick_out ()
644 bool locate_required = transport_sub_state & PendingLocate;
646 /* this is called after a process() iteration. if PendingDeclickOut was set,
647 it means that we were waiting to declick the output (which has just been
648 done) before doing something else. this is where we do that "something else".
650 note: called from the audio thread.
653 if (transport_sub_state & PendingDeclickOut) {
655 if (locate_required) {
656 start_locate (pending_locate_frame, pending_locate_roll, pending_locate_flush);
657 transport_sub_state &= ~(PendingDeclickOut|PendingLocate);
659 stop_transport (pending_abort);
660 transport_sub_state &= ~(PendingDeclickOut|PendingLocate);
666 Session::unset_play_loop ()
669 clear_events (SessionEvent::AutoLoop);
671 // set all tracks to NOT use internal looping
672 boost::shared_ptr<RouteList> rl = routes.reader ();
673 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
674 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
675 if (tr && !tr->hidden()) {
682 Session::set_play_loop (bool yn)
684 /* Called from event-handling context */
688 if (yn == play_loop || (actively_recording() && yn) || (loc = _locations->auto_loop_location()) == 0) {
689 /* nothing to do, or can't change loop status while recording */
693 if (yn && Config->get_seamless_loop() && synced_to_jack()) {
694 warning << string_compose (_("Seamless looping cannot be supported while %1 is using JACK transport.\n"
695 "Recommend changing the configured options"), PROGRAM_NAME)
708 if (Config->get_seamless_loop()) {
709 // set all tracks to use internal looping
710 boost::shared_ptr<RouteList> rl = routes.reader ();
711 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
712 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
713 if (tr && !tr->hidden()) {
719 // set all tracks to NOT use internal looping
720 boost::shared_ptr<RouteList> rl = routes.reader ();
721 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
722 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
723 if (tr && !tr->hidden()) {
729 /* put the loop event into the event list */
731 SessionEvent* event = new SessionEvent (SessionEvent::AutoLoop, SessionEvent::Replace, loc->end(), loc->start(), 0.0f);
734 /* locate to start of loop and roll. If doing seamless loop, force a
735 locate+buffer refill even if we are positioned there already.
738 start_locate (loc->start(), true, true, false, Config->get_seamless_loop());
746 TransportStateChange ();
749 Session::flush_all_inserts ()
751 boost::shared_ptr<RouteList> r = routes.reader ();
753 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
754 (*i)->flush_processors ();
759 Session::start_locate (framepos_t target_frame, bool with_roll, bool with_flush, bool with_loop, bool force)
761 if (synced_to_jack()) {
766 _slave->speed_and_position (sp, pos);
768 if (target_frame != pos) {
770 /* tell JACK to change transport position, and we will
771 follow along later in ::follow_slave()
774 _engine.transport_locate (target_frame);
776 if (sp != 1.0f && with_roll) {
777 _engine.transport_start ();
783 locate (target_frame, with_roll, with_flush, with_loop, force);
788 Session::micro_locate (framecnt_t distance)
790 boost::shared_ptr<RouteList> rl = routes.reader();
791 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
792 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
793 if (tr && !tr->can_internal_playback_seek (distance)) {
798 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
799 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
801 tr->internal_playback_seek (distance);
805 _transport_frame += distance;
809 /** @param with_mmc true to send a MMC locate command when the locate is done */
811 Session::locate (framepos_t target_frame, bool with_roll, bool with_flush, bool with_loop, bool force, bool with_mmc)
813 if (actively_recording() && !with_loop) {
817 if (!force && _transport_frame == target_frame && !loop_changing && !with_loop) {
819 set_transport_speed (1.0, false);
821 loop_changing = false;
822 Located (); /* EMIT SIGNAL */
826 if (_transport_speed) {
827 /* schedule a declick. we'll be called again when its done */
829 if (!(transport_sub_state & PendingDeclickOut)) {
830 transport_sub_state |= (PendingDeclickOut|PendingLocate);
831 pending_locate_frame = target_frame;
832 pending_locate_roll = with_roll;
833 pending_locate_flush = with_flush;
838 // Update Timecode time
839 // [DR] FIXME: find out exactly where this should go below
840 _transport_frame = target_frame;
841 timecode_time(_transport_frame, transmitting_timecode_time);
842 outbound_mtc_timecode_frame = _transport_frame;
843 next_quarter_frame_to_send = 0;
845 if (transport_rolling() && (!auto_play_legal || !config.get_auto_play()) && !with_roll && !(synced_to_jack() && play_loop)) {
846 realtime_stop (false, true); // XXX paul - check if the 2nd arg is really correct
849 if (force || !with_loop || loop_changing) {
851 PostTransportWork todo = PostTransportLocate;
854 todo = PostTransportWork (todo | PostTransportRoll);
857 add_post_transport_work (todo);
858 _butler->schedule_transport_work ();
862 /* this is functionally what clear_clicks() does but with a tentative lock */
864 Glib::RWLock::WriterLock clickm (click_lock, Glib::TRY_LOCK);
866 if (clickm.locked()) {
868 for (Clicks::iterator i = clicks.begin(); i != clicks.end(); ++i) {
877 /* switch from input if we're going to roll */
878 if (Config->get_monitoring_model() == HardwareMonitoring) {
879 set_track_monitor_input_status (!config.get_auto_input());
882 /* otherwise we're going to stop, so do the opposite */
883 if (Config->get_monitoring_model() == HardwareMonitoring) {
884 set_track_monitor_input_status (true);
888 /* cancel looped playback if transport pos outside of loop range */
890 Location* al = _locations->auto_loop_location();
892 if (al && (_transport_frame < al->start() || _transport_frame > al->end())) {
893 // cancel looping directly, this is called from event handling context
894 set_play_loop (false);
896 else if (al && _transport_frame == al->start()) {
898 // this is only necessary for seamless looping
900 boost::shared_ptr<RouteList> rl = routes.reader();
901 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
902 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
903 if (tr && tr->record_enabled ()) {
904 // tell it we've looped, so it can deal with the record state
905 tr->transport_looped(_transport_frame);
910 TransportLooped(); // EMIT SIGNAL
914 loop_changing = false;
916 _send_timecode_update = true;
919 send_mmc_locate (_transport_frame);
922 Located (); /* EMIT SIGNAL */
925 /** Set the transport speed.
926 * @param speed New speed
930 Session::set_transport_speed (double speed, bool abort, bool clear_state)
932 DEBUG_TRACE (DEBUG::Transport, string_compose ("Set transport speed to %1, abort = %2 clear_state = %3, current = %4\n", speed, abort, clear_state, _transport_speed));
934 if (_transport_speed == speed) {
938 _target_transport_speed = fabs(speed);
940 /* 8.0 max speed is somewhat arbitrary but based on guestimates regarding disk i/o capability
941 and user needs. We really need CD-style "skip" playback for ffwd and rewind.
945 speed = min (8.0, speed);
946 } else if (speed < 0) {
947 speed = max (-8.0, speed);
950 if (transport_rolling() && speed == 0.0) {
952 /* we are rolling and we want to stop */
954 if (Config->get_monitoring_model() == HardwareMonitoring) {
955 set_track_monitor_input_status (true);
958 if (synced_to_jack ()) {
960 /* do this here because our response to the slave won't
966 _engine.transport_stop ();
968 stop_transport (abort);
973 } else if (transport_stopped() && speed == 1.0) {
975 /* we are stopped and we want to start rolling at speed 1 */
977 if (Config->get_monitoring_model() == HardwareMonitoring && config.get_auto_input()) {
978 set_track_monitor_input_status (false);
981 if (synced_to_jack()) {
982 _engine.transport_start ();
989 if ((synced_to_jack()) && speed != 0.0 && speed != 1.0) {
990 warning << string_compose (_("Global varispeed cannot be supported while %1 is connected to JACK transport control"),
996 if (actively_recording()) {
1000 if (speed > 0.0 && _transport_frame == current_end_frame()) {
1004 if (speed < 0.0 && _transport_frame == 0) {
1010 /* if we are reversing relative to the current speed, or relative to the speed
1011 before the last stop, then we have to do extra work.
1014 PostTransportWork todo = PostTransportWork (0);
1016 if ((_transport_speed && speed * _transport_speed < 0.0) || (_last_transport_speed * speed < 0.0) || (_last_transport_speed == 0.0f && speed < 0.0f)) {
1017 todo = PostTransportWork (todo | PostTransportReverse);
1018 _last_roll_or_reversal_location = _transport_frame;
1021 _last_transport_speed = _transport_speed;
1022 _transport_speed = speed;
1024 boost::shared_ptr<RouteList> rl = routes.reader();
1025 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
1026 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
1027 if (tr && tr->realtime_set_speed (tr->speed(), true)) {
1028 todo = PostTransportWork (todo | PostTransportSpeed);
1033 add_post_transport_work (todo);
1034 _butler->schedule_transport_work ();
1040 /** Stop the transport. */
1042 Session::stop_transport (bool abort, bool clear_state)
1044 if (_transport_speed == 0.0f) {
1048 if (actively_recording() && !(transport_sub_state & StopPendingCapture) && worst_playback_latency() > current_block_size) {
1050 boost::shared_ptr<RouteList> rl = routes.reader();
1051 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
1052 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
1054 tr->prepare_to_stop (_transport_frame);
1058 /* we need to capture the audio that has still not yet been received by the system
1059 at the time the stop is requested, so we have to roll past that time.
1061 we want to declick before stopping, so schedule the autostop for one
1062 block before the actual end. we'll declick in the subsequent block,
1063 and then we'll really be stopped.
1066 SessionEvent *ev = new SessionEvent (SessionEvent::StopOnce, SessionEvent::Replace,
1067 _transport_frame + _worst_input_latency - current_block_size,
1071 transport_sub_state |= StopPendingCapture;
1072 pending_abort = abort;
1077 if ((transport_sub_state & PendingDeclickOut) == 0) {
1079 if (!(transport_sub_state & StopPendingCapture)) {
1080 boost::shared_ptr<RouteList> rl = routes.reader();
1081 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
1082 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
1084 tr->prepare_to_stop (_transport_frame);
1089 transport_sub_state |= PendingDeclickOut;
1090 /* we'll be called again after the declick */
1091 pending_abort = abort;
1095 realtime_stop (abort, clear_state);
1096 _butler->schedule_transport_work ();
1100 Session::start_transport ()
1102 _last_roll_location = _transport_frame;
1103 _last_roll_or_reversal_location = _transport_frame;
1105 have_looped = false;
1107 /* if record status is Enabled, move it to Recording. if its
1108 already Recording, move it to Disabled.
1111 switch (record_status()) {
1113 if (!config.get_punch_in()) {
1120 disable_record (false);
1128 transport_sub_state |= PendingDeclickIn;
1130 _transport_speed = 1.0;
1131 _target_transport_speed = 1.0;
1133 boost::shared_ptr<RouteList> rl = routes.reader();
1134 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
1135 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
1137 tr->realtime_set_speed (tr->speed(), true);
1139 (*i)->automation_snapshot (_transport_frame, true);
1142 Timecode::Time time;
1143 timecode_time_subframes (_transport_frame, time);
1144 if (!dynamic_cast<MTC_Slave*>(_slave)) {
1145 MIDI::Manager::instance()->mmc()->send (MIDI::MachineControlCommand (MIDI::MachineControl::cmdDeferredPlay));
1148 TransportStateChange (); /* EMIT SIGNAL */
1151 /** Do any transport work in the audio thread that needs to be done after the
1152 * transport thread is finished. Audio thread, realtime safe.
1155 Session::post_transport ()
1157 PostTransportWork ptw = post_transport_work ();
1159 if (ptw & PostTransportAudition) {
1160 if (auditioner && auditioner->auditioning()) {
1161 process_function = &Session::process_audition;
1163 process_function = &Session::process_with_events;
1167 if (ptw & PostTransportStop) {
1169 transport_sub_state = 0;
1172 if (ptw & PostTransportLocate) {
1174 if (((!config.get_external_sync() && (auto_play_legal && config.get_auto_play())) && !_exporting) || (ptw & PostTransportRoll)) {
1178 transport_sub_state = 0;
1183 /* XXX is this really safe? shouldn't we just be unsetting the bits that we actually
1186 set_post_transport_work (PostTransportWork (0));
1190 Session::reset_rf_scale (framecnt_t motion)
1192 cumulative_rf_motion += motion;
1194 if (cumulative_rf_motion < 4 * _current_frame_rate) {
1196 } else if (cumulative_rf_motion < 8 * _current_frame_rate) {
1198 } else if (cumulative_rf_motion < 16 * _current_frame_rate) {
1210 Session::use_sync_source (Slave* new_slave)
1212 /* Runs in process() context */
1214 bool non_rt_required = false;
1216 /* XXX this deletion is problematic because we're in RT context */
1221 boost::shared_ptr<RouteList> rl = routes.reader();
1222 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
1223 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
1224 if (tr && !tr->hidden()) {
1225 if (tr->realtime_set_speed (tr->speed(), true)) {
1226 non_rt_required = true;
1228 tr->set_slaved (_slave != 0);
1232 if (non_rt_required) {
1233 add_post_transport_work (PostTransportSpeed);
1234 _butler->schedule_transport_work ();
1241 Session::drop_sync_source ()
1243 request_sync_source (0);
1247 Session::switch_to_sync_source (SyncSource src)
1251 DEBUG_TRACE (DEBUG::Slave, string_compose ("Setting up sync source %1\n", enum_2_string (src)));
1255 if (_slave && dynamic_cast<MTC_Slave*>(_slave)) {
1260 new_slave = new MTC_Slave (*this, *MIDI::Manager::instance()->mtc_input_port());
1263 catch (failed_constructor& err) {
1269 if (_slave && dynamic_cast<MIDIClock_Slave*>(_slave)) {
1274 new_slave = new MIDIClock_Slave (*this, *MIDI::Manager::instance()->midi_clock_input_port(), 24);
1277 catch (failed_constructor& err) {
1283 if (_slave && dynamic_cast<JACK_Slave*>(_slave)) {
1287 if (config.get_video_pullup() != 0.0f) {
1291 new_slave = new JACK_Slave (_engine.jack());
1299 request_sync_source (new_slave);
1303 Session::reverse_track_buffers ()
1305 add_post_transport_work (PostTransportReverse);
1306 _butler->schedule_transport_work ();
1310 Session::set_track_speed (Track* track, double speed)
1312 if (track->realtime_set_speed (speed, false)) {
1313 add_post_transport_work (PostTransportSpeed);
1314 _butler->schedule_transport_work ();
1320 Session::unset_play_range ()
1322 _play_range = false;
1323 _clear_event_type (SessionEvent::RangeStop);
1324 _clear_event_type (SessionEvent::RangeLocate);
1328 Session::set_play_range (list<AudioRange>& range, bool leave_rolling)
1332 /* Called from event-processing context */
1334 unset_play_range ();
1336 if (range.empty()) {
1337 /* _play_range set to false in unset_play_range()
1339 if (!leave_rolling) {
1340 /* stop transport */
1341 SessionEvent* ev = new SessionEvent (SessionEvent::SetTransportSpeed, SessionEvent::Add, SessionEvent::Immediate, 0, 0.0f, false);
1349 /* cancel loop play */
1352 list<AudioRange>::size_type sz = range.size();
1356 list<AudioRange>::iterator i = range.begin();
1357 list<AudioRange>::iterator next;
1359 while (i != range.end()) {
1364 /* locating/stopping is subject to delays for declicking.
1367 framepos_t requested_frame = i->end;
1369 if (requested_frame > current_block_size) {
1370 requested_frame -= current_block_size;
1372 requested_frame = 0;
1375 if (next == range.end()) {
1376 ev = new SessionEvent (SessionEvent::RangeStop, SessionEvent::Add, requested_frame, 0, 0.0f);
1378 ev = new SessionEvent (SessionEvent::RangeLocate, SessionEvent::Add, requested_frame, (*next).start, 0.0f);
1386 } else if (sz == 1) {
1388 ev = new SessionEvent (SessionEvent::RangeStop, SessionEvent::Add, range.front().end, 0, 0.0f);
1393 /* save range so we can do auto-return etc. */
1395 current_audio_range = range;
1397 /* now start rolling at the right place */
1399 ev = new SessionEvent (SessionEvent::LocateRoll, SessionEvent::Add, SessionEvent::Immediate, range.front().start, 0.0f, false);
1402 TransportStateChange ();
1406 Session::request_bounded_roll (framepos_t start, framepos_t end)
1408 AudioRange ar (start, end, 0);
1409 list<AudioRange> lar;
1412 request_play_range (&lar, true);
1415 Session::request_roll_at_and_return (framepos_t start, framepos_t return_to)
1417 SessionEvent *ev = new SessionEvent (SessionEvent::LocateRollLocate, SessionEvent::Add, SessionEvent::Immediate, return_to, 1.0);
1418 ev->target2_frame = start;
1423 Session::engine_halted ()
1427 /* there will be no more calls to process(), so
1428 we'd better clean up for ourselves, right now.
1430 but first, make sure the butler is out of
1434 g_atomic_int_set (&_butler->should_do_transport_work, 0);
1435 set_post_transport_work (PostTransportWork (0));
1438 realtime_stop (false, true);
1439 non_realtime_stop (false, 0, ignored);
1440 transport_sub_state = 0;
1442 TransportStateChange (); /* EMIT SIGNAL */
1447 Session::xrun_recovery ()
1449 Xrun (_transport_frame); //EMIT SIGNAL
1451 if (Config->get_stop_recording_on_xrun() && actively_recording()) {
1453 /* it didn't actually halt, but we need
1454 to handle things in the same way.
1462 Session::route_processors_changed (RouteProcessorChange c)
1464 if (c.type == RouteProcessorChange::MeterPointChange) {
1468 update_latency_compensation ();
1473 Session::allow_auto_play (bool yn)
1475 auto_play_legal = yn;
1479 Session::reset_jack_connection (jack_client_t* jack)
1483 if (_slave && ((js = dynamic_cast<JACK_Slave*> (_slave)) != 0)) {
1484 js->reset_client (jack);
1489 Session::maybe_stop (framepos_t limit)
1491 if ((_transport_speed > 0.0f && _transport_frame >= limit) || (_transport_speed < 0.0f && _transport_frame == 0)) {
1492 if (synced_to_jack () && config.get_jack_time_master ()) {
1493 _engine.transport_stop ();
1494 } else if (!synced_to_jack ()) {
1503 Session::send_mmc_locate (framepos_t t)
1505 Timecode::Time time;
1506 timecode_time_subframes (t, time);
1507 MIDI::Manager::instance()->mmc()->send (MIDI::MachineControlCommand (time));
1510 /** Ask the transport to not send timecode until further notice. The suspension
1511 * will come into effect some finite time after this call, and timecode_transmission_suspended()
1512 * should be checked by the caller to find out when.
1515 Session::request_suspend_timecode_transmission ()
1517 SessionEvent* ev = new SessionEvent (SessionEvent::SetTimecodeTransmission, SessionEvent::Add, SessionEvent::Immediate, 0, 0, false);
1522 Session::request_resume_timecode_transmission ()
1524 SessionEvent* ev = new SessionEvent (SessionEvent::SetTimecodeTransmission, SessionEvent::Add, SessionEvent::Immediate, 0, 0, true);
1529 Session::timecode_transmission_suspended () const
1531 return g_atomic_int_get (&_suspend_timecode_transmission) == 1;