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"
43 #include "ardour/operations.h"
48 using namespace ARDOUR;
52 Session::add_post_transport_work (PostTransportWork ptw)
54 PostTransportWork oldval;
55 PostTransportWork newval;
59 oldval = (PostTransportWork) g_atomic_int_get (&_post_transport_work);
60 newval = PostTransportWork (oldval | ptw);
61 if (g_atomic_int_compare_and_exchange (&_post_transport_work, oldval, newval)) {
67 error << "Could not set post transport work! Crazy thread madness, call the programmers" << endmsg;
71 Session::request_input_change_handling ()
73 if (!(_state_of_the_state & (InitialConnecting|Deletion))) {
74 SessionEvent* ev = new SessionEvent (SessionEvent::InputConfigurationChange, SessionEvent::Add, SessionEvent::Immediate, 0, 0.0);
80 Session::request_sync_source (Slave* new_slave)
82 SessionEvent* ev = new SessionEvent (SessionEvent::SetSyncSource, SessionEvent::Add, SessionEvent::Immediate, 0, 0.0);
85 seamless = Config->get_seamless_loop ();
87 if (dynamic_cast<JACK_Slave*>(new_slave)) {
88 /* JACK cannot support seamless looping at present */
89 Config->set_seamless_loop (false);
91 /* reset to whatever the value was before we last switched slaves */
92 Config->set_seamless_loop (_was_seamless);
95 /* save value of seamless from before the switch */
96 _was_seamless = seamless;
98 ev->slave = new_slave;
103 Session::request_transport_speed (double speed)
105 SessionEvent* ev = new SessionEvent (SessionEvent::SetTransportSpeed, SessionEvent::Add, SessionEvent::Immediate, 0, speed);
106 DEBUG_TRACE (DEBUG::Transport, string_compose ("Request transport speed = %1\n", speed));
111 Session::request_track_speed (Track* tr, double speed)
113 SessionEvent* ev = new SessionEvent (SessionEvent::SetTrackSpeed, SessionEvent::Add, SessionEvent::Immediate, 0, speed);
119 Session::request_stop (bool abort, bool clear_state)
121 SessionEvent* ev = new SessionEvent (SessionEvent::SetTransportSpeed, SessionEvent::Add, SessionEvent::Immediate, 0, 0.0, abort, clear_state);
122 DEBUG_TRACE (DEBUG::Transport, string_compose ("Request transport stop, abort = %1, clear state = %2\n", abort, clear_state));
127 Session::request_locate (framepos_t target_frame, bool with_roll)
129 SessionEvent *ev = new SessionEvent (with_roll ? SessionEvent::LocateRoll : SessionEvent::Locate, SessionEvent::Add, SessionEvent::Immediate, target_frame, 0, false);
130 DEBUG_TRACE (DEBUG::Transport, string_compose ("Request locate to %1\n", target_frame));
135 Session::force_locate (framepos_t target_frame, bool with_roll)
137 SessionEvent *ev = new SessionEvent (with_roll ? SessionEvent::LocateRoll : SessionEvent::Locate, SessionEvent::Add, SessionEvent::Immediate, target_frame, 0, true);
138 DEBUG_TRACE (DEBUG::Transport, string_compose ("Request forced locate to %1\n", target_frame));
143 Session::request_play_loop (bool yn, bool leave_rolling)
146 Location *location = _locations->auto_loop_location();
148 if (location == 0 && yn) {
149 error << _("Cannot loop - no loop range defined")
154 ev = new SessionEvent (SessionEvent::SetLoop, SessionEvent::Add, SessionEvent::Immediate, 0, (leave_rolling ? 1.0 : 0.0), yn);
155 DEBUG_TRACE (DEBUG::Transport, string_compose ("Request set loop = %1, leave rolling ? %2\n", yn, leave_rolling));
158 if (!leave_rolling && !yn && Config->get_seamless_loop() && transport_rolling()) {
159 // request an immediate locate to refresh the tracks
160 // after disabling looping
161 request_locate (_transport_frame-1, false);
166 Session::request_play_range (list<AudioRange>* range, bool leave_rolling)
168 SessionEvent* ev = new SessionEvent (SessionEvent::SetPlayAudioRange, SessionEvent::Add, SessionEvent::Immediate, 0, (leave_rolling ? 1.0 : 0.0));
170 ev->audio_range = *range;
172 ev->audio_range.clear ();
174 DEBUG_TRACE (DEBUG::Transport, string_compose ("Request play range, leave rolling ? %1\n", leave_rolling));
179 Session::realtime_stop (bool abort, bool clear_state)
181 DEBUG_TRACE (DEBUG::Transport, "realtime stop\n");
182 PostTransportWork todo = PostTransportWork (0);
184 /* assume that when we start, we'll be moving forwards */
186 if (_transport_speed < 0.0f) {
187 todo = (PostTransportWork (todo | PostTransportStop | PostTransportReverse));
189 todo = PostTransportWork (todo | PostTransportStop);
194 boost::shared_ptr<RouteList> r = routes.reader ();
196 for (RouteList::iterator i = r->begin (); i != r->end(); ++i) {
197 (*i)->realtime_handle_transport_stopped ();
200 if (actively_recording()) {
202 /* move the transport position back to where the
203 request for a stop was noticed. we rolled
204 past that point to pick up delayed input (and/or to delick)
207 if (_worst_output_latency > current_block_size) {
208 /* we rolled past the stop point to pick up data that had
209 not yet arrived. move back to where the stop occured.
211 decrement_transport_position (current_block_size + (_worst_output_latency - current_block_size));
213 decrement_transport_position (current_block_size);
216 /* the duration change is not guaranteed to have happened, but is likely */
218 todo = PostTransportWork (todo | PostTransportDuration);
222 todo = PostTransportWork (todo | PostTransportAbort);
226 todo = PostTransportWork (todo | PostTransportClearSubstate);
230 add_post_transport_work (todo);
233 _clear_event_type (SessionEvent::StopOnce);
234 _clear_event_type (SessionEvent::RangeStop);
235 _clear_event_type (SessionEvent::RangeLocate);
237 /* if we're going to clear loop state, then force disabling record BUT only if we're not doing latched rec-enable */
238 disable_record (true, (!Config->get_latched_record_enable() && clear_state));
240 reset_slave_state ();
242 _transport_speed = 0;
243 _target_transport_speed = 0;
245 g_atomic_int_set (&_playback_load, 100);
246 g_atomic_int_set (&_capture_load, 100);
248 if (config.get_use_video_sync()) {
249 waiting_for_sync_offset = true;
252 transport_sub_state = 0;
256 Session::butler_transport_work ()
260 PostTransportWork ptw;
261 boost::shared_ptr<RouteList> r = routes.reader ();
263 int on_entry = g_atomic_int_get (&_butler->should_do_transport_work);
265 ptw = post_transport_work();
267 DEBUG_TRACE (DEBUG::Transport, string_compose ("Butler transport work, todo = %1\n", enum_2_string (ptw)));
269 if (ptw & PostTransportAdjustPlaybackBuffering) {
270 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
271 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
273 tr->adjust_playback_buffering ();
274 /* and refill those buffers ... */
275 tr->non_realtime_locate (_transport_frame);
281 if (ptw & PostTransportAdjustCaptureBuffering) {
282 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
283 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
285 tr->adjust_capture_buffering ();
290 if (ptw & PostTransportCurveRealloc) {
291 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
292 (*i)->curve_reallocate();
296 if (ptw & PostTransportInputChange) {
297 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
298 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
300 tr->non_realtime_input_change ();
305 if (ptw & PostTransportSpeed) {
306 non_realtime_set_speed ();
309 if (ptw & PostTransportReverse) {
312 cumulative_rf_motion = 0;
315 /* don't seek if locate will take care of that in non_realtime_stop() */
317 if (!(ptw & PostTransportLocate)) {
319 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
320 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
321 if (tr && !tr->hidden()) {
322 tr->non_realtime_locate (_transport_frame);
324 if (on_entry != g_atomic_int_get (&_butler->should_do_transport_work)) {
325 /* new request, stop seeking, and start again */
326 g_atomic_int_dec_and_test (&_butler->should_do_transport_work);
333 if (ptw & PostTransportLocate) {
334 non_realtime_locate ();
337 if (ptw & PostTransportStop) {
338 non_realtime_stop (ptw & PostTransportAbort, on_entry, finished);
340 g_atomic_int_dec_and_test (&_butler->should_do_transport_work);
345 if (ptw & PostTransportOverWrite) {
346 non_realtime_overwrite (on_entry, finished);
348 g_atomic_int_dec_and_test (&_butler->should_do_transport_work);
353 if (ptw & PostTransportAudition) {
354 non_realtime_set_audition ();
357 g_atomic_int_dec_and_test (&_butler->should_do_transport_work);
359 DEBUG_TRACE (DEBUG::Transport, X_("Butler transport work all done\n"));
363 Session::non_realtime_set_speed ()
365 boost::shared_ptr<RouteList> rl = routes.reader();
366 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
367 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
369 tr->non_realtime_set_speed ();
375 Session::non_realtime_overwrite (int on_entry, bool& finished)
377 boost::shared_ptr<RouteList> rl = routes.reader();
378 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
379 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
380 if (tr && tr->pending_overwrite ()) {
381 tr->overwrite_existing_buffers ();
383 if (on_entry != g_atomic_int_get (&_butler->should_do_transport_work)) {
392 Session::non_realtime_locate ()
394 boost::shared_ptr<RouteList> rl = routes.reader();
395 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
396 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
398 tr->non_realtime_locate (_transport_frame);
405 Session::non_realtime_stop (bool abort, int on_entry, bool& finished)
411 PostTransportWork ptw = post_transport_work();
416 boost::shared_ptr<RouteList> rl = routes.reader();
417 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
418 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
419 if (tr && tr->get_captured_frames () != 0) {
425 /* stop and locate are merged here because they share a lot of common stuff */
428 now = localtime (&xnow);
431 auditioner->cancel_audition ();
435 cumulative_rf_motion = 0;
439 begin_reversible_command (Operations::capture);
440 _have_captured = true;
443 DEBUG_TRACE (DEBUG::Transport, X_("Butler PTW: DS stop\n"));
445 if (abort && did_record) {
446 /* no reason to save the session file when we remove sources
448 _state_of_the_state = StateOfTheState (_state_of_the_state|InCleanup);
451 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
452 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
454 tr->transport_stopped_wallclock (*now, xnow, abort);
458 if (abort && did_record) {
459 _state_of_the_state = StateOfTheState (_state_of_the_state & ~InCleanup);
462 boost::shared_ptr<RouteList> r = routes.reader ();
464 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
465 if (!(*i)->is_hidden()) {
466 (*i)->set_pending_declick (0);
471 commit_reversible_command ();
474 if (_engine.running()) {
475 update_latency_compensation (true, abort);
478 bool const auto_return_enabled =
479 (!config.get_external_sync() && config.get_auto_return());
481 if (auto_return_enabled ||
482 (ptw & PostTransportLocate) ||
483 (_requested_return_frame >= 0) ||
486 if (pending_locate_flush) {
487 flush_all_inserts ();
490 if ((auto_return_enabled || synced_to_jack() || _requested_return_frame >= 0) &&
491 !(ptw & PostTransportLocate)) {
493 /* no explicit locate queued */
495 bool do_locate = false;
497 if (_requested_return_frame >= 0) {
499 /* explicit return request pre-queued in event list. overrides everything else */
501 cerr << "explicit auto-return to " << _requested_return_frame << endl;
503 _transport_frame = _requested_return_frame;
507 if (config.get_auto_return()) {
511 /* don't try to handle loop play when synced to JACK */
513 if (!synced_to_jack()) {
515 Location *location = _locations->auto_loop_location();
518 _transport_frame = location->start();
520 _transport_frame = _last_roll_location;
525 } else if (_play_range) {
527 /* return to start of range */
529 if (!current_audio_range.empty()) {
530 _transport_frame = current_audio_range.front().start;
536 /* regular auto-return */
538 _transport_frame = _last_roll_location;
544 _requested_return_frame = -1;
547 _engine.transport_locate (_transport_frame);
553 /* do this before seeking, because otherwise the tracks will do the wrong thing in seamless loop mode.
556 if (ptw & PostTransportClearSubstate) {
561 /* this for() block can be put inside the previous if() and has the effect of ... ??? what */
563 DEBUG_TRACE (DEBUG::Transport, X_("Butler PTW: locate\n"));
564 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
565 DEBUG_TRACE (DEBUG::Transport, string_compose ("Butler PTW: locate on %1\n", (*i)->name()));
566 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
567 if (tr && !tr->hidden()) {
568 tr->non_realtime_locate (_transport_frame);
571 if (on_entry != g_atomic_int_get (&_butler->should_do_transport_work)) {
573 /* we will be back */
580 send_full_time_code (_transport_frame);
582 if (!dynamic_cast<MTC_Slave*>(_slave)) {
583 MIDI::Manager::instance()->mmc()->send (MIDI::MachineControlCommand (MIDI::MachineControl::cmdStop));
584 send_mmc_locate (_transport_frame);
587 if ((ptw & PostTransportLocate) && get_record_enabled()) {
588 /* capture start has been changed, so save pending state */
589 save_state ("", true);
593 /* always try to get rid of this */
595 remove_pending_capture_state ();
597 /* save the current state of things if appropriate */
599 if (did_record && !saved) {
600 save_state (_current_snapshot_name);
603 if (ptw & PostTransportStop) {
608 PositionChanged (_transport_frame); /* EMIT SIGNAL */
609 TransportStateChange (); /* EMIT SIGNAL */
611 /* and start it up again if relevant */
613 if ((ptw & PostTransportLocate) && !config.get_external_sync() && pending_locate_roll) {
614 request_transport_speed (1.0);
615 pending_locate_roll = false;
620 Session::check_declick_out ()
622 bool locate_required = transport_sub_state & PendingLocate;
624 /* this is called after a process() iteration. if PendingDeclickOut was set,
625 it means that we were waiting to declick the output (which has just been
626 done) before doing something else. this is where we do that "something else".
628 note: called from the audio thread.
631 if (transport_sub_state & PendingDeclickOut) {
633 if (locate_required) {
634 start_locate (pending_locate_frame, pending_locate_roll, pending_locate_flush);
635 transport_sub_state &= ~(PendingDeclickOut|PendingLocate);
637 stop_transport (pending_abort);
638 transport_sub_state &= ~(PendingDeclickOut|PendingLocate);
644 Session::unset_play_loop ()
647 clear_events (SessionEvent::AutoLoop);
649 // set all tracks to NOT use internal looping
650 boost::shared_ptr<RouteList> rl = routes.reader ();
651 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
652 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
653 if (tr && !tr->hidden()) {
660 Session::set_play_loop (bool yn)
662 /* Called from event-handling context */
666 if (yn == play_loop || (actively_recording() && yn) || (loc = _locations->auto_loop_location()) == 0) {
667 /* nothing to do, or can't change loop status while recording */
671 if (yn && Config->get_seamless_loop() && synced_to_jack()) {
672 warning << string_compose (_("Seamless looping cannot be supported while %1 is using JACK transport.\n"
673 "Recommend changing the configured options"), PROGRAM_NAME)
686 if (Config->get_seamless_loop()) {
687 // set all tracks to use internal looping
688 boost::shared_ptr<RouteList> rl = routes.reader ();
689 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
690 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
691 if (tr && !tr->hidden()) {
697 // set all tracks to NOT use internal looping
698 boost::shared_ptr<RouteList> rl = routes.reader ();
699 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
700 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
701 if (tr && !tr->hidden()) {
707 /* put the loop event into the event list */
709 SessionEvent* event = new SessionEvent (SessionEvent::AutoLoop, SessionEvent::Replace, loc->end(), loc->start(), 0.0f);
712 /* locate to start of loop and roll. If doing seamless loop, force a
713 locate+buffer refill even if we are positioned there already.
716 start_locate (loc->start(), true, true, false, Config->get_seamless_loop());
724 TransportStateChange ();
727 Session::flush_all_inserts ()
729 boost::shared_ptr<RouteList> r = routes.reader ();
731 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
732 (*i)->flush_processors ();
737 Session::start_locate (framepos_t target_frame, bool with_roll, bool with_flush, bool with_loop, bool force)
739 if (synced_to_jack()) {
744 _slave->speed_and_position (sp, pos);
746 if (target_frame != pos) {
748 /* tell JACK to change transport position, and we will
749 follow along later in ::follow_slave()
752 _engine.transport_locate (target_frame);
754 if (sp != 1.0f && with_roll) {
755 _engine.transport_start ();
761 locate (target_frame, with_roll, with_flush, with_loop, force);
766 Session::micro_locate (framecnt_t distance)
768 boost::shared_ptr<RouteList> rl = routes.reader();
769 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
770 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
771 if (tr && !tr->can_internal_playback_seek (distance)) {
776 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
777 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
779 tr->internal_playback_seek (distance);
783 _transport_frame += distance;
787 /** @param with_mmc true to send a MMC locate command when the locate is done */
789 Session::locate (framepos_t target_frame, bool with_roll, bool with_flush, bool with_loop, bool force, bool with_mmc)
791 if (actively_recording() && !with_loop) {
795 if (!force && _transport_frame == target_frame && !loop_changing && !with_loop) {
797 set_transport_speed (1.0, false);
799 loop_changing = false;
800 Located (); /* EMIT SIGNAL */
804 // Update Timecode time
805 // [DR] FIXME: find out exactly where this should go below
806 _transport_frame = target_frame;
807 timecode_time(_transport_frame, transmitting_timecode_time);
808 outbound_mtc_timecode_frame = _transport_frame;
809 next_quarter_frame_to_send = 0;
811 if (_transport_speed && (!with_loop || loop_changing)) {
812 /* schedule a declick. we'll be called again when its done */
814 if (!(transport_sub_state & PendingDeclickOut)) {
815 transport_sub_state |= (PendingDeclickOut|PendingLocate);
816 pending_locate_frame = target_frame;
817 pending_locate_roll = with_roll;
818 pending_locate_flush = with_flush;
823 if (transport_rolling() && (!auto_play_legal || !config.get_auto_play()) && !with_roll && !(synced_to_jack() && play_loop)) {
824 realtime_stop (false, true); // XXX paul - check if the 2nd arg is really correct
827 if (force || !with_loop || loop_changing) {
829 PostTransportWork todo = PostTransportLocate;
832 todo = PostTransportWork (todo | PostTransportRoll);
835 add_post_transport_work (todo);
836 _butler->schedule_transport_work ();
840 /* this is functionally what clear_clicks() does but with a tentative lock */
842 Glib::RWLock::WriterLock clickm (click_lock, Glib::TRY_LOCK);
844 if (clickm.locked()) {
846 for (Clicks::iterator i = clicks.begin(); i != clicks.end(); ++i) {
855 /* switch from input if we're going to roll */
856 if (Config->get_monitoring_model() == HardwareMonitoring) {
858 boost::shared_ptr<RouteList> rl = routes.reader();
859 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
860 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
861 if (tr && tr->record_enabled ()) {
862 //cerr << "switching from input" << __FILE__ << __LINE__ << endl << endl;
863 tr->monitor_input (!config.get_auto_input());
868 /* otherwise we're going to stop, so do the opposite */
869 if (Config->get_monitoring_model() == HardwareMonitoring) {
871 boost::shared_ptr<RouteList> rl = routes.reader();
872 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
873 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
874 if (tr && tr->record_enabled ()) {
875 //cerr << "switching to input" << __FILE__ << __LINE__ << endl << endl;
876 tr->monitor_input (true);
882 /* cancel looped playback if transport pos outside of loop range */
884 Location* al = _locations->auto_loop_location();
886 if (al && (_transport_frame < al->start() || _transport_frame > al->end())) {
887 // cancel looping directly, this is called from event handling context
888 set_play_loop (false);
890 else if (al && _transport_frame == al->start()) {
892 // this is only necessary for seamless looping
894 boost::shared_ptr<RouteList> rl = routes.reader();
895 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
896 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
897 if (tr && tr->record_enabled ()) {
898 // tell it we've looped, so it can deal with the record state
899 tr->transport_looped(_transport_frame);
904 TransportLooped(); // EMIT SIGNAL
908 loop_changing = false;
910 _send_timecode_update = true;
913 send_mmc_locate (_transport_frame);
916 Located (); /* EMIT SIGNAL */
919 /** Set the transport speed.
920 * @param speed New speed
924 Session::set_transport_speed (double speed, bool abort, bool clear_state)
926 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));
928 if (_transport_speed == speed) {
932 _target_transport_speed = fabs(speed);
934 /* 8.0 max speed is somewhat arbitrary but based on guestimates regarding disk i/o capability
935 and user needs. We really need CD-style "skip" playback for ffwd and rewind.
939 speed = min (8.0, speed);
940 } else if (speed < 0) {
941 speed = max (-8.0, speed);
944 if (transport_rolling() && speed == 0.0) {
946 /* we are rolling and we want to stop */
948 if (Config->get_monitoring_model() == HardwareMonitoring)
950 boost::shared_ptr<RouteList> rl = routes.reader();
952 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
953 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
954 if (tr && tr->record_enabled ()) {
955 //cerr << "switching to input" << __FILE__ << __LINE__ << endl << endl;
956 tr->monitor_input (true);
961 if (synced_to_jack ()) {
963 /* do this here because our response to the slave won't
969 _engine.transport_stop ();
971 stop_transport (abort);
976 } else if (transport_stopped() && speed == 1.0) {
978 /* we are stopped and we want to start rolling at speed 1 */
980 if (Config->get_monitoring_model() == HardwareMonitoring) {
982 boost::shared_ptr<RouteList> rl = routes.reader();
983 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
984 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
985 if (config.get_auto_input() && tr && tr->record_enabled ()) {
986 //cerr << "switching from input" << __FILE__ << __LINE__ << endl << endl;
987 tr->monitor_input (false);
992 if (synced_to_jack()) {
993 _engine.transport_start ();
1000 if ((synced_to_jack()) && speed != 0.0 && speed != 1.0) {
1001 warning << string_compose (_("Global varispeed cannot be supported while %1 is connected to JACK transport control"),
1007 if (actively_recording()) {
1011 if (speed > 0.0 && _transport_frame == current_end_frame()) {
1015 if (speed < 0.0 && _transport_frame == 0) {
1021 /* if we are reversing relative to the current speed, or relative to the speed
1022 before the last stop, then we have to do extra work.
1025 PostTransportWork todo = PostTransportWork (0);
1027 if ((_transport_speed && speed * _transport_speed < 0.0) || (_last_transport_speed * speed < 0.0) || (_last_transport_speed == 0.0f && speed < 0.0f)) {
1028 todo = PostTransportWork (todo | PostTransportReverse);
1029 _last_roll_or_reversal_location = _transport_frame;
1032 _last_transport_speed = _transport_speed;
1033 _transport_speed = speed;
1035 boost::shared_ptr<RouteList> rl = routes.reader();
1036 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
1037 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
1038 if (tr && tr->realtime_set_speed (tr->speed(), true)) {
1039 todo = PostTransportWork (todo | PostTransportSpeed);
1044 add_post_transport_work (todo);
1045 _butler->schedule_transport_work ();
1051 /** Stop the transport. */
1053 Session::stop_transport (bool abort, bool clear_state)
1055 if (_transport_speed == 0.0f) {
1059 if (actively_recording() && !(transport_sub_state & StopPendingCapture) && _worst_output_latency > current_block_size) {
1061 boost::shared_ptr<RouteList> rl = routes.reader();
1062 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
1063 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
1065 tr->prepare_to_stop (_transport_frame);
1069 /* we need to capture the audio that has still not yet been received by the system
1070 at the time the stop is requested, so we have to roll past that time.
1072 we want to declick before stopping, so schedule the autostop for one
1073 block before the actual end. we'll declick in the subsequent block,
1074 and then we'll really be stopped.
1077 SessionEvent *ev = new SessionEvent (SessionEvent::StopOnce, SessionEvent::Replace,
1078 _transport_frame + _worst_output_latency - current_block_size,
1082 transport_sub_state |= StopPendingCapture;
1083 pending_abort = abort;
1088 if ((transport_sub_state & PendingDeclickOut) == 0) {
1090 if (!(transport_sub_state & StopPendingCapture)) {
1091 boost::shared_ptr<RouteList> rl = routes.reader();
1092 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
1093 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
1095 tr->prepare_to_stop (_transport_frame);
1100 transport_sub_state |= PendingDeclickOut;
1101 /* we'll be called again after the declick */
1102 pending_abort = abort;
1106 realtime_stop (abort, clear_state);
1107 _butler->schedule_transport_work ();
1111 Session::start_transport ()
1113 _last_roll_location = _transport_frame;
1114 _last_roll_or_reversal_location = _transport_frame;
1116 have_looped = false;
1118 /* if record status is Enabled, move it to Recording. if its
1119 already Recording, move it to Disabled.
1122 switch (record_status()) {
1124 if (!config.get_punch_in()) {
1131 disable_record (false);
1139 transport_sub_state |= PendingDeclickIn;
1141 _transport_speed = 1.0;
1142 _target_transport_speed = 1.0;
1144 boost::shared_ptr<RouteList> rl = routes.reader();
1145 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
1146 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
1148 tr->realtime_set_speed (tr->speed(), true);
1150 (*i)->automation_snapshot (_transport_frame, true);
1153 Timecode::Time time;
1154 timecode_time_subframes (_transport_frame, time);
1155 if (!dynamic_cast<MTC_Slave*>(_slave)) {
1156 MIDI::Manager::instance()->mmc()->send (MIDI::MachineControlCommand (MIDI::MachineControl::cmdDeferredPlay));
1159 TransportStateChange (); /* EMIT SIGNAL */
1162 /** Do any transport work in the audio thread that needs to be done after the
1163 * transport thread is finished. Audio thread, realtime safe.
1166 Session::post_transport ()
1168 PostTransportWork ptw = post_transport_work ();
1170 if (ptw & PostTransportAudition) {
1171 if (auditioner && auditioner->auditioning()) {
1172 process_function = &Session::process_audition;
1174 process_function = &Session::process_with_events;
1178 if (ptw & PostTransportStop) {
1180 transport_sub_state = 0;
1183 if (ptw & PostTransportLocate) {
1185 if (((!config.get_external_sync() && (auto_play_legal && config.get_auto_play())) && !_exporting) || (ptw & PostTransportRoll)) {
1189 transport_sub_state = 0;
1194 /* XXX is this really safe? shouldn't we just be unsetting the bits that we actually
1197 set_post_transport_work (PostTransportWork (0));
1201 Session::reset_rf_scale (framecnt_t motion)
1203 cumulative_rf_motion += motion;
1205 if (cumulative_rf_motion < 4 * _current_frame_rate) {
1207 } else if (cumulative_rf_motion < 8 * _current_frame_rate) {
1209 } else if (cumulative_rf_motion < 16 * _current_frame_rate) {
1221 Session::use_sync_source (Slave* new_slave)
1223 /* Runs in process() context */
1225 bool non_rt_required = false;
1227 /* XXX this deletion is problematic because we're in RT context */
1232 boost::shared_ptr<RouteList> rl = routes.reader();
1233 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
1234 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
1235 if (tr && !tr->hidden()) {
1236 if (tr->realtime_set_speed (tr->speed(), true)) {
1237 non_rt_required = true;
1239 tr->set_slaved (_slave != 0);
1243 if (non_rt_required) {
1244 add_post_transport_work (PostTransportSpeed);
1245 _butler->schedule_transport_work ();
1252 Session::drop_sync_source ()
1254 request_sync_source (0);
1258 Session::switch_to_sync_source (SyncSource src)
1262 DEBUG_TRACE (DEBUG::Slave, string_compose ("Setting up sync source %1\n", enum_2_string (src)));
1266 if (_slave && dynamic_cast<MTC_Slave*>(_slave)) {
1271 new_slave = new MTC_Slave (*this, *MIDI::Manager::instance()->mtc_input_port());
1274 catch (failed_constructor& err) {
1280 if (_slave && dynamic_cast<MIDIClock_Slave*>(_slave)) {
1285 new_slave = new MIDIClock_Slave (*this, *MIDI::Manager::instance()->midi_clock_input_port(), 24);
1288 catch (failed_constructor& err) {
1294 if (_slave && dynamic_cast<JACK_Slave*>(_slave)) {
1298 if (config.get_video_pullup() != 0.0f) {
1302 new_slave = new JACK_Slave (_engine.jack());
1310 request_sync_source (new_slave);
1314 Session::reverse_track_buffers ()
1316 add_post_transport_work (PostTransportReverse);
1317 _butler->schedule_transport_work ();
1321 Session::set_track_speed (Track* track, double speed)
1323 if (track->realtime_set_speed (speed, false)) {
1324 add_post_transport_work (PostTransportSpeed);
1325 _butler->schedule_transport_work ();
1331 Session::unset_play_range ()
1333 _play_range = false;
1334 _clear_event_type (SessionEvent::RangeStop);
1335 _clear_event_type (SessionEvent::RangeLocate);
1339 Session::set_play_range (list<AudioRange>& range, bool leave_rolling)
1343 /* Called from event-processing context */
1345 unset_play_range ();
1347 if (range.empty()) {
1348 /* _play_range set to false in unset_play_range()
1350 if (!leave_rolling) {
1351 /* stop transport */
1352 SessionEvent* ev = new SessionEvent (SessionEvent::SetTransportSpeed, SessionEvent::Add, SessionEvent::Immediate, 0, 0.0f, false);
1360 /* cancel loop play */
1363 list<AudioRange>::size_type sz = range.size();
1367 list<AudioRange>::iterator i = range.begin();
1368 list<AudioRange>::iterator next;
1370 while (i != range.end()) {
1375 /* locating/stopping is subject to delays for declicking.
1378 framepos_t requested_frame = i->end;
1380 if (requested_frame > current_block_size) {
1381 requested_frame -= current_block_size;
1383 requested_frame = 0;
1386 if (next == range.end()) {
1387 ev = new SessionEvent (SessionEvent::RangeStop, SessionEvent::Add, requested_frame, 0, 0.0f);
1389 ev = new SessionEvent (SessionEvent::RangeLocate, SessionEvent::Add, requested_frame, (*next).start, 0.0f);
1397 } else if (sz == 1) {
1399 ev = new SessionEvent (SessionEvent::RangeStop, SessionEvent::Add, range.front().end, 0, 0.0f);
1404 /* save range so we can do auto-return etc. */
1406 current_audio_range = range;
1408 /* now start rolling at the right place */
1410 ev = new SessionEvent (SessionEvent::LocateRoll, SessionEvent::Add, SessionEvent::Immediate, range.front().start, 0.0f, false);
1413 TransportStateChange ();
1417 Session::request_bounded_roll (framepos_t start, framepos_t end)
1419 AudioRange ar (start, end, 0);
1420 list<AudioRange> lar;
1423 request_play_range (&lar, true);
1426 Session::request_roll_at_and_return (framepos_t start, framepos_t return_to)
1428 SessionEvent *ev = new SessionEvent (SessionEvent::LocateRollLocate, SessionEvent::Add, SessionEvent::Immediate, return_to, 1.0);
1429 ev->target2_frame = start;
1434 Session::engine_halted ()
1438 /* there will be no more calls to process(), so
1439 we'd better clean up for ourselves, right now.
1441 but first, make sure the butler is out of
1445 g_atomic_int_set (&_butler->should_do_transport_work, 0);
1446 set_post_transport_work (PostTransportWork (0));
1449 realtime_stop (false, true);
1450 non_realtime_stop (false, 0, ignored);
1451 transport_sub_state = 0;
1453 TransportStateChange (); /* EMIT SIGNAL */
1458 Session::xrun_recovery ()
1460 Xrun (_transport_frame); //EMIT SIGNAL
1462 if (Config->get_stop_recording_on_xrun() && actively_recording()) {
1464 /* it didn't actually halt, but we need
1465 to handle things in the same way.
1473 Session::route_processors_changed (RouteProcessorChange c)
1475 if (c.type == RouteProcessorChange::MeterPointChange) {
1479 update_latency_compensation (false, false);
1484 Session::update_latency_compensation (bool with_stop, bool abort)
1486 bool update_jack = false;
1487 PostTransportWork ptw;
1489 if (_state_of_the_state & Deletion) {
1493 _worst_track_latency = 0;
1494 ptw = post_transport_work();
1496 #undef DEBUG_LATENCY
1497 #ifdef DEBUG_LATENCY
1498 cerr << "\n---------------------------------\nUPDATE LATENCY\n";
1501 boost::shared_ptr<RouteList> r = routes.reader ();
1503 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
1506 (*i)->nonrealtime_handle_transport_stopped (abort, (ptw & PostTransportLocate), (!(ptw & PostTransportLocate) || pending_locate_flush));
1509 framecnt_t old_latency = (*i)->output()->signal_latency ();
1510 framecnt_t track_latency = (*i)->update_total_latency ();
1512 if (old_latency != track_latency) {
1513 (*i)->input()->update_port_total_latencies ();
1514 (*i)->output()->update_port_total_latencies ();
1518 if (!(*i)->is_hidden() && ((*i)->active())) {
1519 _worst_track_latency = max (_worst_track_latency, track_latency);
1524 _engine.update_total_latencies ();
1527 #ifdef DEBUG_LATENCY
1528 cerr << "\tworst was " << _worst_track_latency << endl;
1531 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
1532 (*i)->set_latency_delay (_worst_track_latency);
1535 set_worst_io_latencies ();
1537 /* reflect any changes in latencies into capture offsets
1540 boost::shared_ptr<RouteList> rl = routes.reader();
1541 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
1542 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
1544 tr->set_capture_offset ();
1550 Session::allow_auto_play (bool yn)
1552 auto_play_legal = yn;
1556 Session::reset_jack_connection (jack_client_t* jack)
1560 if (_slave && ((js = dynamic_cast<JACK_Slave*> (_slave)) != 0)) {
1561 js->reset_client (jack);
1566 Session::maybe_stop (framepos_t limit)
1568 if ((_transport_speed > 0.0f && _transport_frame >= limit) || (_transport_speed < 0.0f && _transport_frame == 0)) {
1569 if (synced_to_jack () && config.get_jack_time_master ()) {
1570 _engine.transport_stop ();
1571 } else if (!synced_to_jack ()) {
1580 Session::send_mmc_locate (framepos_t t)
1582 Timecode::Time time;
1583 timecode_time_subframes (t, time);
1584 MIDI::Manager::instance()->mmc()->send (MIDI::MachineControlCommand (time));
1587 /** Ask the transport to not send timecode until further notice. The suspension
1588 * will come into effect some finite time after this call, and timecode_transmission_suspended()
1589 * should be checked by the caller to find out when.
1592 Session::request_suspend_timecode_transmission ()
1594 SessionEvent* ev = new SessionEvent (SessionEvent::SetTimecodeTransmission, SessionEvent::Add, SessionEvent::Immediate, 0, 0, false);
1599 Session::request_resume_timecode_transmission ()
1601 SessionEvent* ev = new SessionEvent (SessionEvent::SetTimecodeTransmission, SessionEvent::Add, SessionEvent::Immediate, 0, 0, true);
1606 Session::timecode_transmission_suspended () const
1608 return g_atomic_int_get (&_suspend_timecode_transmission) == 1;