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.
26 #include "pbd/error.h"
27 #include "pbd/enumwriter.h"
28 #include "pbd/pthread_utils.h"
29 #include "pbd/memento_command.h"
31 #include "midi++/mmc.h"
32 #include "midi++/port.h"
33 #include "midi++/manager.h"
35 #include "ardour/ardour.h"
36 #include "ardour/audioengine.h"
37 #include "ardour/auditioner.h"
38 #include "ardour/butler.h"
39 #include "ardour/debug.h"
40 #include "ardour/location.h"
41 #include "ardour/session.h"
42 #include "ardour/slave.h"
47 using namespace ARDOUR;
51 Session::add_post_transport_work (PostTransportWork ptw)
53 PostTransportWork oldval;
54 PostTransportWork newval;
58 oldval = (PostTransportWork) g_atomic_int_get (&_post_transport_work);
59 newval = PostTransportWork (oldval | ptw);
60 if (g_atomic_int_compare_and_exchange (&_post_transport_work, oldval, newval)) {
66 error << "Could not set post transport work! Crazy thread madness, call the programmers" << endmsg;
70 Session::request_input_change_handling ()
72 if (!(_state_of_the_state & (InitialConnecting|Deletion))) {
73 SessionEvent* ev = new SessionEvent (SessionEvent::InputConfigurationChange, SessionEvent::Add, SessionEvent::Immediate, 0, 0.0);
79 Session::request_sync_source (Slave* new_slave)
81 SessionEvent* ev = new SessionEvent (SessionEvent::SetSyncSource, SessionEvent::Add, SessionEvent::Immediate, 0, 0.0);
84 seamless = Config->get_seamless_loop ();
86 if (dynamic_cast<JACK_Slave*>(new_slave)) {
87 /* JACK cannot support seamless looping at present */
88 Config->set_seamless_loop (false);
90 /* reset to whatever the value was before we last switched slaves */
91 Config->set_seamless_loop (_was_seamless);
94 /* save value of seamless from before the switch */
95 _was_seamless = seamless;
97 ev->slave = new_slave;
102 Session::request_transport_speed (double speed)
104 SessionEvent* ev = new SessionEvent (SessionEvent::SetTransportSpeed, SessionEvent::Add, SessionEvent::Immediate, 0, speed);
105 DEBUG_TRACE (DEBUG::Transport, string_compose ("Request transport speed = %1\n", speed));
110 Session::request_track_speed (Track* tr, double speed)
112 SessionEvent* ev = new SessionEvent (SessionEvent::SetTrackSpeed, SessionEvent::Add, SessionEvent::Immediate, 0, speed);
118 Session::request_stop (bool abort, bool clear_state)
120 SessionEvent* ev = new SessionEvent (SessionEvent::SetTransportSpeed, SessionEvent::Add, SessionEvent::Immediate, 0, 0.0, abort, clear_state);
121 DEBUG_TRACE (DEBUG::Transport, string_compose ("Request transport stop, abort = %1, clear state = %2\n", abort, clear_state));
126 Session::request_locate (framepos_t target_frame, bool with_roll)
128 SessionEvent *ev = new SessionEvent (with_roll ? SessionEvent::LocateRoll : SessionEvent::Locate, SessionEvent::Add, SessionEvent::Immediate, target_frame, 0, false);
129 DEBUG_TRACE (DEBUG::Transport, string_compose ("Request locate to %1\n", target_frame));
134 Session::force_locate (framepos_t target_frame, bool with_roll)
136 SessionEvent *ev = new SessionEvent (with_roll ? SessionEvent::LocateRoll : SessionEvent::Locate, SessionEvent::Add, SessionEvent::Immediate, target_frame, 0, true);
137 DEBUG_TRACE (DEBUG::Transport, string_compose ("Request forced locate to %1\n", target_frame));
142 Session::request_play_loop (bool yn, bool leave_rolling)
145 Location *location = _locations->auto_loop_location();
147 if (location == 0 && yn) {
148 error << _("Cannot loop - no loop range defined")
153 ev = new SessionEvent (SessionEvent::SetLoop, SessionEvent::Add, SessionEvent::Immediate, 0, (leave_rolling ? 1.0 : 0.0), yn);
154 DEBUG_TRACE (DEBUG::Transport, string_compose ("Request set loop = %1, leave rolling ? %2\n", yn, leave_rolling));
157 if (!leave_rolling && !yn && Config->get_seamless_loop() && transport_rolling()) {
158 // request an immediate locate to refresh the tracks
159 // after disabling looping
160 request_locate (_transport_frame-1, false);
165 Session::request_play_range (list<AudioRange>* range, bool leave_rolling)
167 SessionEvent* ev = new SessionEvent (SessionEvent::SetPlayAudioRange, SessionEvent::Add, SessionEvent::Immediate, 0, (leave_rolling ? 1.0 : 0.0));
169 ev->audio_range = *range;
171 ev->audio_range.clear ();
173 DEBUG_TRACE (DEBUG::Transport, string_compose ("Request play range, leave rolling ? %1\n", leave_rolling));
178 Session::realtime_stop (bool abort, bool clear_state)
180 DEBUG_TRACE (DEBUG::Transport, "realtime stop\n");
181 PostTransportWork todo = PostTransportWork (0);
183 /* assume that when we start, we'll be moving forwards */
185 if (_transport_speed < 0.0f) {
186 todo = (PostTransportWork (todo | PostTransportStop | PostTransportReverse));
188 todo = PostTransportWork (todo | PostTransportStop);
193 boost::shared_ptr<RouteList> r = routes.reader ();
195 for (RouteList::iterator i = r->begin (); i != r->end(); ++i) {
196 (*i)->realtime_handle_transport_stopped ();
199 if (actively_recording()) {
201 /* move the transport position back to where the
202 request for a stop was noticed. we rolled
203 past that point to pick up delayed input (and/or to delick)
206 if (_worst_output_latency > current_block_size) {
207 /* we rolled past the stop point to pick up data that had
208 not yet arrived. move back to where the stop occured.
210 decrement_transport_position (current_block_size + (_worst_output_latency - current_block_size));
212 decrement_transport_position (current_block_size);
215 /* the duration change is not guaranteed to have happened, but is likely */
217 todo = PostTransportWork (todo | PostTransportDuration);
221 todo = PostTransportWork (todo | PostTransportAbort);
225 todo = PostTransportWork (todo | PostTransportClearSubstate);
229 add_post_transport_work (todo);
232 _clear_event_type (SessionEvent::StopOnce);
233 _clear_event_type (SessionEvent::RangeStop);
234 _clear_event_type (SessionEvent::RangeLocate);
236 /* if we're going to clear loop state, then force disabling record BUT only if we're not doing latched rec-enable */
237 disable_record (true, (!Config->get_latched_record_enable() && clear_state));
239 reset_slave_state ();
241 _transport_speed = 0;
242 _target_transport_speed = 0;
244 g_atomic_int_set (&_playback_load, 100);
245 g_atomic_int_set (&_capture_load, 100);
247 if (config.get_use_video_sync()) {
248 waiting_for_sync_offset = true;
251 transport_sub_state = 0;
255 Session::butler_transport_work ()
259 PostTransportWork ptw;
260 boost::shared_ptr<RouteList> r = routes.reader ();
262 int on_entry = g_atomic_int_get (&_butler->should_do_transport_work);
264 ptw = post_transport_work();
266 DEBUG_TRACE (DEBUG::Transport, string_compose ("Butler transport work, todo = %1\n", enum_2_string (ptw)));
268 if (ptw & PostTransportAdjustPlaybackBuffering) {
269 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
270 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
272 tr->adjust_playback_buffering ();
273 /* and refill those buffers ... */
274 tr->non_realtime_locate (_transport_frame);
280 if (ptw & PostTransportAdjustCaptureBuffering) {
281 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
282 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
284 tr->adjust_capture_buffering ();
289 if (ptw & PostTransportCurveRealloc) {
290 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
291 (*i)->curve_reallocate();
295 if (ptw & PostTransportInputChange) {
296 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
297 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
299 tr->non_realtime_input_change ();
304 if (ptw & PostTransportSpeed) {
305 non_realtime_set_speed ();
308 if (ptw & PostTransportReverse) {
311 cumulative_rf_motion = 0;
314 /* don't seek if locate will take care of that in non_realtime_stop() */
316 if (!(ptw & PostTransportLocate)) {
318 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
319 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
320 if (tr && !tr->hidden()) {
321 tr->non_realtime_locate (_transport_frame);
323 if (on_entry != g_atomic_int_get (&_butler->should_do_transport_work)) {
324 /* new request, stop seeking, and start again */
325 g_atomic_int_dec_and_test (&_butler->should_do_transport_work);
332 if (ptw & PostTransportLocate) {
333 non_realtime_locate ();
336 if (ptw & PostTransportStop) {
337 non_realtime_stop (ptw & PostTransportAbort, on_entry, finished);
339 g_atomic_int_dec_and_test (&_butler->should_do_transport_work);
344 if (ptw & PostTransportOverWrite) {
345 non_realtime_overwrite (on_entry, finished);
347 g_atomic_int_dec_and_test (&_butler->should_do_transport_work);
352 if (ptw & PostTransportAudition) {
353 non_realtime_set_audition ();
356 g_atomic_int_dec_and_test (&_butler->should_do_transport_work);
358 DEBUG_TRACE (DEBUG::Transport, X_("Butler transport work all done\n"));
362 Session::non_realtime_set_speed ()
364 boost::shared_ptr<RouteList> rl = routes.reader();
365 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
366 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
368 tr->non_realtime_set_speed ();
374 Session::non_realtime_overwrite (int on_entry, bool& finished)
376 boost::shared_ptr<RouteList> rl = routes.reader();
377 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
378 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
379 if (tr && tr->pending_overwrite ()) {
380 tr->overwrite_existing_buffers ();
382 if (on_entry != g_atomic_int_get (&_butler->should_do_transport_work)) {
391 Session::non_realtime_locate ()
393 boost::shared_ptr<RouteList> rl = routes.reader();
394 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
395 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
397 tr->non_realtime_locate (_transport_frame);
404 Session::non_realtime_stop (bool abort, int on_entry, bool& finished)
410 PostTransportWork ptw = post_transport_work();
415 boost::shared_ptr<RouteList> rl = routes.reader();
416 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
417 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
418 if (tr && tr->get_captured_frames () != 0) {
424 /* stop and locate are merged here because they share a lot of common stuff */
427 now = localtime (&xnow);
430 auditioner->cancel_audition ();
434 cumulative_rf_motion = 0;
438 begin_reversible_command ("capture");
439 _have_captured = true;
442 DEBUG_TRACE (DEBUG::Transport, X_("Butler PTW: DS stop\n"));
444 if (abort && did_record) {
445 /* no reason to save the session file when we remove sources
447 _state_of_the_state = StateOfTheState (_state_of_the_state|InCleanup);
450 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
451 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
453 tr->transport_stopped_wallclock (*now, xnow, abort);
457 if (abort && did_record) {
458 _state_of_the_state = StateOfTheState (_state_of_the_state & ~InCleanup);
461 boost::shared_ptr<RouteList> r = routes.reader ();
463 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
464 if (!(*i)->is_hidden()) {
465 (*i)->set_pending_declick (0);
470 commit_reversible_command ();
473 if (_engine.running()) {
474 update_latency_compensation (true, abort);
477 bool const auto_return_enabled =
478 (!config.get_external_sync() && config.get_auto_return());
480 if (auto_return_enabled ||
481 (ptw & PostTransportLocate) ||
482 (_requested_return_frame >= 0) ||
485 if (pending_locate_flush) {
486 flush_all_inserts ();
489 if ((auto_return_enabled || synced_to_jack() || _requested_return_frame >= 0) &&
490 !(ptw & PostTransportLocate)) {
492 /* no explicit locate queued */
494 bool do_locate = false;
496 if (_requested_return_frame >= 0) {
498 /* explicit return request pre-queued in event list. overrides everything else */
500 cerr << "explicit auto-return to " << _requested_return_frame << endl;
502 _transport_frame = _requested_return_frame;
506 if (config.get_auto_return()) {
510 /* don't try to handle loop play when synced to JACK */
512 if (!synced_to_jack()) {
514 Location *location = _locations->auto_loop_location();
517 _transport_frame = location->start();
519 _transport_frame = _last_roll_location;
524 } else if (_play_range) {
526 /* return to start of range */
528 if (!current_audio_range.empty()) {
529 _transport_frame = current_audio_range.front().start;
535 /* regular auto-return */
537 _transport_frame = _last_roll_location;
543 _requested_return_frame = -1;
546 _engine.transport_locate (_transport_frame);
552 /* do this before seeking, because otherwise the tracks will do the wrong thing in seamless loop mode.
555 if (ptw & PostTransportClearSubstate) {
560 /* this for() block can be put inside the previous if() and has the effect of ... ??? what */
562 DEBUG_TRACE (DEBUG::Transport, X_("Butler PTW: locate\n"));
563 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
564 DEBUG_TRACE (DEBUG::Transport, string_compose ("Butler PTW: locate on %1\n", (*i)->name()));
565 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
566 if (tr && !tr->hidden()) {
567 tr->non_realtime_locate (_transport_frame);
570 if (on_entry != g_atomic_int_get (&_butler->should_do_transport_work)) {
572 /* we will be back */
579 send_full_time_code (_transport_frame);
580 MIDI::Manager::instance()->mmc()->send (MIDI::MachineControlCommand (MIDI::MachineControl::cmdStop));
581 send_mmc_locate (_transport_frame);
583 if ((ptw & PostTransportLocate) && get_record_enabled()) {
584 /* capture start has been changed, so save pending state */
585 save_state ("", true);
589 /* always try to get rid of this */
591 remove_pending_capture_state ();
593 /* save the current state of things if appropriate */
595 if (did_record && !saved) {
596 save_state (_current_snapshot_name);
599 if (ptw & PostTransportStop) {
604 PositionChanged (_transport_frame); /* EMIT SIGNAL */
605 TransportStateChange (); /* EMIT SIGNAL */
607 /* and start it up again if relevant */
609 if ((ptw & PostTransportLocate) && !config.get_external_sync() && pending_locate_roll) {
610 request_transport_speed (1.0);
611 pending_locate_roll = false;
616 Session::check_declick_out ()
618 bool locate_required = transport_sub_state & PendingLocate;
620 /* this is called after a process() iteration. if PendingDeclickOut was set,
621 it means that we were waiting to declick the output (which has just been
622 done) before doing something else. this is where we do that "something else".
624 note: called from the audio thread.
627 if (transport_sub_state & PendingDeclickOut) {
629 if (locate_required) {
630 start_locate (pending_locate_frame, pending_locate_roll, pending_locate_flush);
631 transport_sub_state &= ~(PendingDeclickOut|PendingLocate);
633 stop_transport (pending_abort);
634 transport_sub_state &= ~(PendingDeclickOut|PendingLocate);
640 Session::unset_play_loop ()
643 clear_events (SessionEvent::AutoLoop);
645 // set all tracks to NOT use internal looping
646 boost::shared_ptr<RouteList> rl = routes.reader ();
647 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
648 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
649 if (tr && !tr->hidden()) {
656 Session::set_play_loop (bool yn)
658 /* Called from event-handling context */
662 if (yn == play_loop || (actively_recording() && yn) || (loc = _locations->auto_loop_location()) == 0) {
663 /* nothing to do, or can't change loop status while recording */
667 if (yn && Config->get_seamless_loop() && synced_to_jack()) {
668 warning << string_compose (_("Seamless looping cannot be supported while %1 is using JACK transport.\n"
669 "Recommend changing the configured options"), PROGRAM_NAME)
682 if (Config->get_seamless_loop()) {
683 // set all tracks to use internal looping
684 boost::shared_ptr<RouteList> rl = routes.reader ();
685 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
686 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
687 if (tr && !tr->hidden()) {
693 // set all tracks to NOT use internal looping
694 boost::shared_ptr<RouteList> rl = routes.reader ();
695 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
696 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
697 if (tr && !tr->hidden()) {
703 /* put the loop event into the event list */
705 SessionEvent* event = new SessionEvent (SessionEvent::AutoLoop, SessionEvent::Replace, loc->end(), loc->start(), 0.0f);
708 /* locate to start of loop and roll. If doing seamless loop, force a
709 locate+buffer refill even if we are positioned there already.
712 start_locate (loc->start(), true, true, false, Config->get_seamless_loop());
720 TransportStateChange ();
723 Session::flush_all_inserts ()
725 boost::shared_ptr<RouteList> r = routes.reader ();
727 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
728 (*i)->flush_processors ();
733 Session::start_locate (framepos_t target_frame, bool with_roll, bool with_flush, bool with_loop, bool force)
735 if (synced_to_jack()) {
740 _slave->speed_and_position (sp, pos);
742 if (target_frame != pos) {
744 /* tell JACK to change transport position, and we will
745 follow along later in ::follow_slave()
748 _engine.transport_locate (target_frame);
750 if (sp != 1.0f && with_roll) {
751 _engine.transport_start ();
757 locate (target_frame, with_roll, with_flush, with_loop, force);
762 Session::micro_locate (framecnt_t distance)
764 boost::shared_ptr<RouteList> rl = routes.reader();
765 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
766 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
767 if (tr && !tr->can_internal_playback_seek (distance)) {
772 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
773 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
775 tr->internal_playback_seek (distance);
779 _transport_frame += distance;
783 /** @param with_mmc true to send a MMC locate command when the locate is done */
785 Session::locate (framepos_t target_frame, bool with_roll, bool with_flush, bool with_loop, bool force, bool with_mmc)
787 if (actively_recording() && !with_loop) {
791 if (!force && _transport_frame == target_frame && !loop_changing && !with_loop) {
793 set_transport_speed (1.0, false);
795 loop_changing = false;
796 Located (); /* EMIT SIGNAL */
800 // Update Timecode time
801 // [DR] FIXME: find out exactly where this should go below
802 _transport_frame = target_frame;
803 timecode_time(_transport_frame, transmitting_timecode_time);
804 outbound_mtc_timecode_frame = _transport_frame;
805 next_quarter_frame_to_send = 0;
807 if (_transport_speed && (!with_loop || loop_changing)) {
808 /* schedule a declick. we'll be called again when its done */
810 if (!(transport_sub_state & PendingDeclickOut)) {
811 transport_sub_state |= (PendingDeclickOut|PendingLocate);
812 pending_locate_frame = target_frame;
813 pending_locate_roll = with_roll;
814 pending_locate_flush = with_flush;
819 if (transport_rolling() && (!auto_play_legal || !config.get_auto_play()) && !with_roll && !(synced_to_jack() && play_loop)) {
820 realtime_stop (false, true); // XXX paul - check if the 2nd arg is really correct
823 if (force || !with_loop || loop_changing) {
825 PostTransportWork todo = PostTransportLocate;
828 todo = PostTransportWork (todo | PostTransportRoll);
831 add_post_transport_work (todo);
832 _butler->schedule_transport_work ();
836 /* this is functionally what clear_clicks() does but with a tentative lock */
838 Glib::RWLock::WriterLock clickm (click_lock, Glib::TRY_LOCK);
840 if (clickm.locked()) {
842 for (Clicks::iterator i = clicks.begin(); i != clicks.end(); ++i) {
851 /* switch from input if we're going to roll */
852 if (Config->get_monitoring_model() == HardwareMonitoring) {
854 boost::shared_ptr<RouteList> rl = routes.reader();
855 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
856 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
857 if (tr && tr->record_enabled ()) {
858 //cerr << "switching from input" << __FILE__ << __LINE__ << endl << endl;
859 tr->monitor_input (!config.get_auto_input());
864 /* otherwise we're going to stop, so do the opposite */
865 if (Config->get_monitoring_model() == HardwareMonitoring) {
867 boost::shared_ptr<RouteList> rl = routes.reader();
868 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
869 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
870 if (tr && tr->record_enabled ()) {
871 //cerr << "switching to input" << __FILE__ << __LINE__ << endl << endl;
872 tr->monitor_input (true);
878 /* cancel looped playback if transport pos outside of loop range */
880 Location* al = _locations->auto_loop_location();
882 if (al && (_transport_frame < al->start() || _transport_frame > al->end())) {
883 // cancel looping directly, this is called from event handling context
884 set_play_loop (false);
886 else if (al && _transport_frame == al->start()) {
888 // this is only necessary for seamless looping
890 boost::shared_ptr<RouteList> rl = routes.reader();
891 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
892 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
893 if (tr && tr->record_enabled ()) {
894 // tell it we've looped, so it can deal with the record state
895 tr->transport_looped(_transport_frame);
900 TransportLooped(); // EMIT SIGNAL
904 loop_changing = false;
906 _send_timecode_update = true;
909 send_mmc_locate (_transport_frame);
912 Located (); /* EMIT SIGNAL */
915 /** Set the transport speed.
916 * @param speed New speed
920 Session::set_transport_speed (double speed, bool abort, bool clear_state)
922 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));
924 if (_transport_speed == speed) {
928 _target_transport_speed = fabs(speed);
930 /* 8.0 max speed is somewhat arbitrary but based on guestimates regarding disk i/o capability
931 and user needs. We really need CD-style "skip" playback for ffwd and rewind.
935 speed = min (8.0, speed);
936 } else if (speed < 0) {
937 speed = max (-8.0, speed);
940 if (transport_rolling() && speed == 0.0) {
942 /* we are rolling and we want to stop */
944 if (Config->get_monitoring_model() == HardwareMonitoring)
946 boost::shared_ptr<RouteList> rl = routes.reader();
947 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
948 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
949 if (tr && tr->record_enabled ()) {
950 //cerr << "switching to input" << __FILE__ << __LINE__ << endl << endl;
951 tr->monitor_input (true);
956 if (synced_to_jack ()) {
958 /* do this here because our response to the slave won't
964 _engine.transport_stop ();
966 stop_transport (abort);
969 } else if (transport_stopped() && speed == 1.0) {
971 /* we are stopped and we want to start rolling at speed 1 */
973 if (Config->get_monitoring_model() == HardwareMonitoring) {
975 boost::shared_ptr<RouteList> rl = routes.reader();
976 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
977 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
978 if (config.get_auto_input() && tr && tr->record_enabled ()) {
979 //cerr << "switching from input" << __FILE__ << __LINE__ << endl << endl;
980 tr->monitor_input (false);
985 if (synced_to_jack()) {
986 _engine.transport_start ();
993 if ((synced_to_jack()) && speed != 0.0 && speed != 1.0) {
994 warning << string_compose (_("Global varispeed cannot be supported while %1 is connected to JACK transport control"),
1000 if (actively_recording()) {
1004 if (speed > 0.0 && _transport_frame == current_end_frame()) {
1008 if (speed < 0.0 && _transport_frame == 0) {
1014 /* if we are reversing relative to the current speed, or relative to the speed
1015 before the last stop, then we have to do extra work.
1018 PostTransportWork todo = PostTransportWork (0);
1020 if ((_transport_speed && speed * _transport_speed < 0.0) || (_last_transport_speed * speed < 0.0) || (_last_transport_speed == 0.0f && speed < 0.0f)) {
1021 todo = PostTransportWork (todo | PostTransportReverse);
1022 _last_roll_or_reversal_location = _transport_frame;
1025 _last_transport_speed = _transport_speed;
1026 _transport_speed = speed;
1028 boost::shared_ptr<RouteList> rl = routes.reader();
1029 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
1030 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
1031 if (tr && tr->realtime_set_speed (tr->speed(), true)) {
1032 todo = PostTransportWork (todo | PostTransportSpeed);
1038 add_post_transport_work (todo);
1039 _butler->schedule_transport_work ();
1045 /** Stop the transport. */
1047 Session::stop_transport (bool abort, bool clear_state)
1049 if (_transport_speed == 0.0f) {
1053 if (actively_recording() && !(transport_sub_state & StopPendingCapture) && _worst_output_latency > current_block_size) {
1055 boost::shared_ptr<RouteList> rl = routes.reader();
1056 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
1057 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
1059 tr->prepare_to_stop (_transport_frame);
1063 /* we need to capture the audio that has still not yet been received by the system
1064 at the time the stop is requested, so we have to roll past that time.
1066 we want to declick before stopping, so schedule the autostop for one
1067 block before the actual end. we'll declick in the subsequent block,
1068 and then we'll really be stopped.
1071 SessionEvent *ev = new SessionEvent (SessionEvent::StopOnce, SessionEvent::Replace,
1072 _transport_frame + _worst_output_latency - current_block_size,
1076 transport_sub_state |= StopPendingCapture;
1077 pending_abort = abort;
1082 if ((transport_sub_state & PendingDeclickOut) == 0) {
1084 if (!(transport_sub_state & StopPendingCapture)) {
1085 boost::shared_ptr<RouteList> rl = routes.reader();
1086 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
1087 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
1089 tr->prepare_to_stop (_transport_frame);
1094 transport_sub_state |= PendingDeclickOut;
1095 /* we'll be called again after the declick */
1096 pending_abort = abort;
1100 realtime_stop (abort, clear_state);
1101 _butler->schedule_transport_work ();
1105 Session::start_transport ()
1107 _last_roll_location = _transport_frame;
1108 _last_roll_or_reversal_location = _transport_frame;
1110 have_looped = false;
1112 /* if record status is Enabled, move it to Recording. if its
1113 already Recording, move it to Disabled.
1116 switch (record_status()) {
1118 if (!config.get_punch_in()) {
1125 disable_record (false);
1133 transport_sub_state |= PendingDeclickIn;
1135 _transport_speed = 1.0;
1136 _target_transport_speed = 1.0;
1138 boost::shared_ptr<RouteList> rl = routes.reader();
1139 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
1140 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
1142 tr->realtime_set_speed (tr->speed(), true);
1144 (*i)->automation_snapshot (_transport_frame, true);
1147 Timecode::Time time;
1148 timecode_time_subframes (_transport_frame, time);
1149 MIDI::Manager::instance()->mmc()->send (MIDI::MachineControlCommand (MIDI::MachineControl::cmdDeferredPlay));
1151 TransportStateChange (); /* EMIT SIGNAL */
1154 /** Do any transport work in the audio thread that needs to be done after the
1155 * transport thread is finished. Audio thread, realtime safe.
1158 Session::post_transport ()
1160 PostTransportWork ptw = post_transport_work ();
1162 if (ptw & PostTransportAudition) {
1163 if (auditioner && auditioner->auditioning()) {
1164 process_function = &Session::process_audition;
1166 process_function = &Session::process_with_events;
1170 if (ptw & PostTransportStop) {
1172 transport_sub_state = 0;
1175 if (ptw & PostTransportLocate) {
1177 if (((!config.get_external_sync() && (auto_play_legal && config.get_auto_play())) && !_exporting) || (ptw & PostTransportRoll)) {
1181 transport_sub_state = 0;
1186 /* XXX is this really safe? shouldn't we just be unsetting the bits that we actually
1189 set_post_transport_work (PostTransportWork (0));
1193 Session::reset_rf_scale (framecnt_t motion)
1195 cumulative_rf_motion += motion;
1197 if (cumulative_rf_motion < 4 * _current_frame_rate) {
1199 } else if (cumulative_rf_motion < 8 * _current_frame_rate) {
1201 } else if (cumulative_rf_motion < 16 * _current_frame_rate) {
1213 Session::use_sync_source (Slave* new_slave)
1215 /* Runs in process() context */
1217 bool non_rt_required = false;
1219 /* XXX this deletion is problematic because we're in RT context */
1224 boost::shared_ptr<RouteList> rl = routes.reader();
1225 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
1226 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
1227 if (tr && !tr->hidden()) {
1228 if (tr->realtime_set_speed (tr->speed(), true)) {
1229 non_rt_required = true;
1231 tr->set_slaved (_slave != 0);
1235 if (non_rt_required) {
1236 add_post_transport_work (PostTransportSpeed);
1237 _butler->schedule_transport_work ();
1244 Session::drop_sync_source ()
1246 request_sync_source (0);
1250 Session::switch_to_sync_source (SyncSource src)
1254 DEBUG_TRACE (DEBUG::Slave, string_compose ("Setting up sync source %1\n", enum_2_string (src)));
1258 if (_slave && dynamic_cast<MTC_Slave*>(_slave)) {
1263 new_slave = new MTC_Slave (*this, *MIDI::Manager::instance()->mtc_input_port());
1266 catch (failed_constructor& err) {
1272 if (_slave && dynamic_cast<MIDIClock_Slave*>(_slave)) {
1277 new_slave = new MIDIClock_Slave (*this, *MIDI::Manager::instance()->midi_clock_input_port(), 24);
1280 catch (failed_constructor& err) {
1286 if (_slave && dynamic_cast<JACK_Slave*>(_slave)) {
1290 if (config.get_video_pullup() != 0.0f) {
1294 new_slave = new JACK_Slave (_engine.jack());
1302 request_sync_source (new_slave);
1306 Session::reverse_track_buffers ()
1308 add_post_transport_work (PostTransportReverse);
1309 _butler->schedule_transport_work ();
1313 Session::set_track_speed (Track* track, double speed)
1315 if (track->realtime_set_speed (speed, false)) {
1316 add_post_transport_work (PostTransportSpeed);
1317 _butler->schedule_transport_work ();
1323 Session::unset_play_range ()
1325 _play_range = false;
1326 _clear_event_type (SessionEvent::RangeStop);
1327 _clear_event_type (SessionEvent::RangeLocate);
1331 Session::set_play_range (list<AudioRange>& range, bool leave_rolling)
1335 /* Called from event-processing context */
1337 unset_play_range ();
1339 if (range.empty()) {
1340 /* _play_range set to false in unset_play_range()
1342 if (!leave_rolling) {
1343 /* stop transport */
1344 SessionEvent* ev = new SessionEvent (SessionEvent::SetTransportSpeed, SessionEvent::Add, SessionEvent::Immediate, 0, 0.0f, false);
1352 /* cancel loop play */
1355 list<AudioRange>::size_type sz = range.size();
1359 list<AudioRange>::iterator i = range.begin();
1360 list<AudioRange>::iterator next;
1362 while (i != range.end()) {
1367 /* locating/stopping is subject to delays for declicking.
1370 framepos_t requested_frame = i->end;
1372 if (requested_frame > current_block_size) {
1373 requested_frame -= current_block_size;
1375 requested_frame = 0;
1378 if (next == range.end()) {
1379 ev = new SessionEvent (SessionEvent::RangeStop, SessionEvent::Add, requested_frame, 0, 0.0f);
1381 ev = new SessionEvent (SessionEvent::RangeLocate, SessionEvent::Add, requested_frame, (*next).start, 0.0f);
1389 } else if (sz == 1) {
1391 ev = new SessionEvent (SessionEvent::RangeStop, SessionEvent::Add, range.front().end, 0, 0.0f);
1396 /* save range so we can do auto-return etc. */
1398 current_audio_range = range;
1400 /* now start rolling at the right place */
1402 ev = new SessionEvent (SessionEvent::LocateRoll, SessionEvent::Add, SessionEvent::Immediate, range.front().start, 0.0f, false);
1405 TransportStateChange ();
1409 Session::request_bounded_roll (framepos_t start, framepos_t end)
1411 AudioRange ar (start, end, 0);
1412 list<AudioRange> lar;
1415 request_play_range (&lar, true);
1418 Session::request_roll_at_and_return (framepos_t start, framepos_t return_to)
1420 SessionEvent *ev = new SessionEvent (SessionEvent::LocateRollLocate, SessionEvent::Add, SessionEvent::Immediate, return_to, 1.0);
1421 ev->target2_frame = start;
1426 Session::engine_halted ()
1430 /* there will be no more calls to process(), so
1431 we'd better clean up for ourselves, right now.
1433 but first, make sure the butler is out of
1437 g_atomic_int_set (&_butler->should_do_transport_work, 0);
1438 set_post_transport_work (PostTransportWork (0));
1441 realtime_stop (false, true);
1442 non_realtime_stop (false, 0, ignored);
1443 transport_sub_state = 0;
1445 TransportStateChange (); /* EMIT SIGNAL */
1450 Session::xrun_recovery ()
1452 Xrun (_transport_frame); //EMIT SIGNAL
1454 if (Config->get_stop_recording_on_xrun() && actively_recording()) {
1456 /* it didn't actually halt, but we need
1457 to handle things in the same way.
1465 Session::route_processors_changed (RouteProcessorChange c)
1467 if (c.type == RouteProcessorChange::MeterPointChange) {
1471 update_latency_compensation (false, false);
1476 Session::update_latency_compensation (bool with_stop, bool abort)
1478 bool update_jack = false;
1479 PostTransportWork ptw;
1481 if (_state_of_the_state & Deletion) {
1485 _worst_track_latency = 0;
1486 ptw = post_transport_work();
1488 #undef DEBUG_LATENCY
1489 #ifdef DEBUG_LATENCY
1490 cerr << "\n---------------------------------\nUPDATE LATENCY\n";
1493 boost::shared_ptr<RouteList> r = routes.reader ();
1495 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
1498 (*i)->nonrealtime_handle_transport_stopped (abort, (ptw & PostTransportLocate), (!(ptw & PostTransportLocate) || pending_locate_flush));
1501 framecnt_t old_latency = (*i)->output()->signal_latency ();
1502 framecnt_t track_latency = (*i)->update_total_latency ();
1504 if (old_latency != track_latency) {
1505 (*i)->input()->update_port_total_latencies ();
1506 (*i)->output()->update_port_total_latencies ();
1510 if (!(*i)->is_hidden() && ((*i)->active())) {
1511 _worst_track_latency = max (_worst_track_latency, track_latency);
1516 _engine.update_total_latencies ();
1519 #ifdef DEBUG_LATENCY
1520 cerr << "\tworst was " << _worst_track_latency << endl;
1523 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
1524 (*i)->set_latency_delay (_worst_track_latency);
1527 set_worst_io_latencies ();
1529 /* reflect any changes in latencies into capture offsets
1532 boost::shared_ptr<RouteList> rl = routes.reader();
1533 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
1534 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
1536 tr->set_capture_offset ();
1542 Session::allow_auto_play (bool yn)
1544 auto_play_legal = yn;
1548 Session::reset_jack_connection (jack_client_t* jack)
1552 if (_slave && ((js = dynamic_cast<JACK_Slave*> (_slave)) != 0)) {
1553 js->reset_client (jack);
1558 Session::maybe_stop (framepos_t limit)
1560 if ((_transport_speed > 0.0f && _transport_frame >= limit) || (_transport_speed < 0.0f && _transport_frame == 0)) {
1561 if (synced_to_jack () && config.get_jack_time_master ()) {
1562 _engine.transport_stop ();
1563 } else if (!synced_to_jack ()) {
1572 Session::send_mmc_locate (framepos_t t)
1574 Timecode::Time time;
1575 timecode_time_subframes (t, time);
1576 MIDI::Manager::instance()->mmc()->send (MIDI::MachineControlCommand (time));
1579 /** Ask the transport to not send timecode until further notice. The suspension
1580 * will come into effect some finite time after this call, and timecode_transmission_suspended()
1581 * should be checked by the caller to find out when.
1584 Session::request_suspend_timecode_transmission ()
1586 SessionEvent* ev = new SessionEvent (SessionEvent::SetTimecodeTransmission, SessionEvent::Add, SessionEvent::Immediate, 0, 0, false);
1591 Session::request_resume_timecode_transmission ()
1593 SessionEvent* ev = new SessionEvent (SessionEvent::SetTimecodeTransmission, SessionEvent::Add, SessionEvent::Immediate, 0, 0, true);
1598 Session::timecode_transmission_suspended () const
1600 return g_atomic_int_get (&_suspend_timecode_transmission) == 1;