2 Copyright (C) 1999-2003 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include "libardour-config.h"
29 #include "pbd/error.h"
30 #include "pbd/enumwriter.h"
31 #include "pbd/pthread_utils.h"
32 #include "pbd/memento_command.h"
34 #include "midi++/mmc.h"
35 #include "midi++/port.h"
36 #include "midi++/manager.h"
38 #include "ardour/audioengine.h"
39 #include "ardour/auditioner.h"
40 #include "ardour/butler.h"
41 #include "ardour/click.h"
42 #include "ardour/debug.h"
43 #include "ardour/location.h"
44 #include "ardour/session.h"
45 #include "ardour/slave.h"
46 #include "ardour/operations.h"
51 using namespace ARDOUR;
55 Session::add_post_transport_work (PostTransportWork ptw)
57 PostTransportWork oldval;
58 PostTransportWork newval;
62 oldval = (PostTransportWork) g_atomic_int_get (&_post_transport_work);
63 newval = PostTransportWork (oldval | ptw);
64 if (g_atomic_int_compare_and_exchange (&_post_transport_work, oldval, newval)) {
70 error << "Could not set post transport work! Crazy thread madness, call the programmers" << endmsg;
74 Session::request_input_change_handling ()
76 if (!(_state_of_the_state & (InitialConnecting|Deletion))) {
77 SessionEvent* ev = new SessionEvent (SessionEvent::InputConfigurationChange, SessionEvent::Add, SessionEvent::Immediate, 0, 0.0);
83 Session::request_sync_source (Slave* new_slave)
85 SessionEvent* ev = new SessionEvent (SessionEvent::SetSyncSource, SessionEvent::Add, SessionEvent::Immediate, 0, 0.0);
88 seamless = Config->get_seamless_loop ();
90 if (dynamic_cast<JACK_Slave*>(new_slave)) {
91 /* JACK cannot support seamless looping at present */
92 Config->set_seamless_loop (false);
94 /* reset to whatever the value was before we last switched slaves */
95 Config->set_seamless_loop (_was_seamless);
98 /* save value of seamless from before the switch */
99 _was_seamless = seamless;
101 ev->slave = new_slave;
106 Session::request_transport_speed (double speed)
108 SessionEvent* ev = new SessionEvent (SessionEvent::SetTransportSpeed, SessionEvent::Add, SessionEvent::Immediate, 0, speed);
109 DEBUG_TRACE (DEBUG::Transport, string_compose ("Request transport speed = %1\n", speed));
113 /** Request a new transport speed, but if the speed parameter is exactly zero then use
114 * a very small +ve value to prevent the transport actually stopping. This method should
115 * be used by callers who are varying transport speed but don't ever want to stop it.
118 Session::request_transport_speed_nonzero (double speed)
124 request_transport_speed (speed);
128 Session::request_track_speed (Track* tr, double speed)
130 SessionEvent* ev = new SessionEvent (SessionEvent::SetTrackSpeed, SessionEvent::Add, SessionEvent::Immediate, 0, speed);
136 Session::request_stop (bool abort, bool clear_state)
138 SessionEvent* ev = new SessionEvent (SessionEvent::SetTransportSpeed, SessionEvent::Add, SessionEvent::Immediate, 0, 0.0, abort, clear_state);
139 DEBUG_TRACE (DEBUG::Transport, string_compose ("Request transport stop, abort = %1, clear state = %2\n", abort, clear_state));
144 Session::request_locate (framepos_t target_frame, bool with_roll)
146 SessionEvent *ev = new SessionEvent (with_roll ? SessionEvent::LocateRoll : SessionEvent::Locate, SessionEvent::Add, SessionEvent::Immediate, target_frame, 0, false);
147 DEBUG_TRACE (DEBUG::Transport, string_compose ("Request locate to %1\n", target_frame));
152 Session::force_locate (framepos_t target_frame, bool with_roll)
154 SessionEvent *ev = new SessionEvent (with_roll ? SessionEvent::LocateRoll : SessionEvent::Locate, SessionEvent::Add, SessionEvent::Immediate, target_frame, 0, true);
155 DEBUG_TRACE (DEBUG::Transport, string_compose ("Request forced locate to %1\n", target_frame));
160 Session::request_play_loop (bool yn, bool leave_rolling)
163 Location *location = _locations->auto_loop_location();
165 if (location == 0 && yn) {
166 error << _("Cannot loop - no loop range defined")
171 ev = new SessionEvent (SessionEvent::SetLoop, SessionEvent::Add, SessionEvent::Immediate, 0, (leave_rolling ? 1.0 : 0.0), yn);
172 DEBUG_TRACE (DEBUG::Transport, string_compose ("Request set loop = %1, leave rolling ? %2\n", yn, leave_rolling));
175 if (!leave_rolling && !yn && Config->get_seamless_loop() && transport_rolling()) {
176 // request an immediate locate to refresh the tracks
177 // after disabling looping
178 request_locate (_transport_frame-1, false);
183 Session::request_play_range (list<AudioRange>* range, bool leave_rolling)
185 SessionEvent* ev = new SessionEvent (SessionEvent::SetPlayAudioRange, SessionEvent::Add, SessionEvent::Immediate, 0, (leave_rolling ? 1.0 : 0.0));
187 ev->audio_range = *range;
189 ev->audio_range.clear ();
191 DEBUG_TRACE (DEBUG::Transport, string_compose ("Request play range, leave rolling ? %1\n", leave_rolling));
196 Session::realtime_stop (bool abort, bool clear_state)
198 DEBUG_TRACE (DEBUG::Transport, string_compose ("realtime stop @ %1\n", _transport_frame));
199 PostTransportWork todo = PostTransportWork (0);
201 /* assume that when we start, we'll be moving forwards */
203 if (_transport_speed < 0.0f) {
204 todo = (PostTransportWork (todo | PostTransportStop | PostTransportReverse));
206 todo = PostTransportWork (todo | PostTransportStop);
211 boost::shared_ptr<RouteList> r = routes.reader ();
213 for (RouteList::iterator i = r->begin (); i != r->end(); ++i) {
214 (*i)->realtime_handle_transport_stopped ();
217 if (actively_recording()) {
219 /* move the transport position back to where the
220 request for a stop was noticed. we rolled
221 past that point to pick up delayed input (and/or to delick)
224 if (worst_playback_latency() > current_block_size) {
225 /* we rolled past the stop point to pick up data that had
226 not yet arrived. move back to where the stop occured.
228 decrement_transport_position (current_block_size + (worst_input_latency() - current_block_size));
230 decrement_transport_position (current_block_size);
233 /* the duration change is not guaranteed to have happened, but is likely */
235 todo = PostTransportWork (todo | PostTransportDuration);
239 todo = PostTransportWork (todo | PostTransportAbort);
243 todo = PostTransportWork (todo | PostTransportClearSubstate);
247 add_post_transport_work (todo);
250 _clear_event_type (SessionEvent::StopOnce);
251 _clear_event_type (SessionEvent::RangeStop);
252 _clear_event_type (SessionEvent::RangeLocate);
254 /* if we're going to clear loop state, then force disabling record BUT only if we're not doing latched rec-enable */
255 disable_record (true, (!Config->get_latched_record_enable() && clear_state));
257 reset_slave_state ();
259 _transport_speed = 0;
260 _target_transport_speed = 0;
262 g_atomic_int_set (&_playback_load, 100);
263 g_atomic_int_set (&_capture_load, 100);
265 if (config.get_use_video_sync()) {
266 waiting_for_sync_offset = true;
269 transport_sub_state = 0;
273 Session::realtime_locate ()
275 boost::shared_ptr<RouteList> r = routes.reader ();
276 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
277 (*i)->realtime_locate ();
282 Session::butler_transport_work ()
286 PostTransportWork ptw;
287 boost::shared_ptr<RouteList> r = routes.reader ();
289 int on_entry = g_atomic_int_get (&_butler->should_do_transport_work);
291 ptw = post_transport_work();
293 DEBUG_TRACE (DEBUG::Transport, string_compose ("Butler transport work, todo = %1\n", enum_2_string (ptw)));
295 if (ptw & PostTransportAdjustPlaybackBuffering) {
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->adjust_playback_buffering ();
300 /* and refill those buffers ... */
301 tr->non_realtime_locate (_transport_frame);
307 if (ptw & PostTransportAdjustCaptureBuffering) {
308 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
309 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
311 tr->adjust_capture_buffering ();
316 if (ptw & PostTransportCurveRealloc) {
317 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
318 (*i)->curve_reallocate();
322 if (ptw & PostTransportInputChange) {
323 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
324 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
326 tr->non_realtime_input_change ();
331 if (ptw & PostTransportSpeed) {
332 non_realtime_set_speed ();
335 if (ptw & PostTransportReverse) {
338 cumulative_rf_motion = 0;
341 /* don't seek if locate will take care of that in non_realtime_stop() */
343 if (!(ptw & PostTransportLocate)) {
345 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
346 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
347 if (tr && !tr->hidden()) {
348 tr->non_realtime_locate (_transport_frame);
350 if (on_entry != g_atomic_int_get (&_butler->should_do_transport_work)) {
351 /* new request, stop seeking, and start again */
352 g_atomic_int_dec_and_test (&_butler->should_do_transport_work);
359 if (ptw & PostTransportLocate) {
360 non_realtime_locate ();
363 if (ptw & PostTransportStop) {
364 non_realtime_stop (ptw & PostTransportAbort, on_entry, finished);
366 g_atomic_int_dec_and_test (&_butler->should_do_transport_work);
371 if (ptw & PostTransportOverWrite) {
372 non_realtime_overwrite (on_entry, finished);
374 g_atomic_int_dec_and_test (&_butler->should_do_transport_work);
379 if (ptw & PostTransportAudition) {
380 non_realtime_set_audition ();
383 g_atomic_int_dec_and_test (&_butler->should_do_transport_work);
385 DEBUG_TRACE (DEBUG::Transport, X_("Butler transport work all done\n"));
389 Session::non_realtime_set_speed ()
391 boost::shared_ptr<RouteList> rl = routes.reader();
392 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
393 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
395 tr->non_realtime_set_speed ();
401 Session::non_realtime_overwrite (int on_entry, bool& finished)
403 boost::shared_ptr<RouteList> rl = routes.reader();
404 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
405 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
406 if (tr && tr->pending_overwrite ()) {
407 tr->overwrite_existing_buffers ();
409 if (on_entry != g_atomic_int_get (&_butler->should_do_transport_work)) {
418 Session::non_realtime_locate ()
420 boost::shared_ptr<RouteList> rl = routes.reader();
421 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
422 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
424 tr->non_realtime_locate (_transport_frame);
428 /* XXX: it would be nice to generate the new clicks here (in the non-RT thread)
429 rather than clearing them so that the RT thread has to spend time constructing
430 them (in Session::click).
437 Session::non_realtime_stop (bool abort, int on_entry, bool& finished)
443 PostTransportWork ptw = post_transport_work();
448 boost::shared_ptr<RouteList> rl = routes.reader();
449 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
450 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
451 if (tr && tr->get_captured_frames () != 0) {
457 /* stop and locate are merged here because they share a lot of common stuff */
460 now = localtime (&xnow);
463 auditioner->cancel_audition ();
466 cumulative_rf_motion = 0;
470 begin_reversible_command (Operations::capture);
471 _have_captured = true;
474 DEBUG_TRACE (DEBUG::Transport, X_("Butler PTW: DS stop\n"));
476 if (abort && did_record) {
477 /* no reason to save the session file when we remove sources
479 _state_of_the_state = StateOfTheState (_state_of_the_state|InCleanup);
482 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
483 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
485 tr->transport_stopped_wallclock (*now, xnow, abort);
489 if (abort && did_record) {
490 _state_of_the_state = StateOfTheState (_state_of_the_state & ~InCleanup);
493 boost::shared_ptr<RouteList> r = routes.reader ();
495 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
496 if (!(*i)->is_hidden()) {
497 (*i)->set_pending_declick (0);
502 commit_reversible_command ();
505 if (_engine.running()) {
506 PostTransportWork ptw = post_transport_work ();
507 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
508 (*i)->nonrealtime_handle_transport_stopped (abort, (ptw & PostTransportLocate), (!(ptw & PostTransportLocate) || pending_locate_flush));
510 update_latency_compensation ();
513 bool const auto_return_enabled =
514 (!config.get_external_sync() && config.get_auto_return());
516 if (auto_return_enabled ||
517 (ptw & PostTransportLocate) ||
518 (_requested_return_frame >= 0) ||
521 if (pending_locate_flush) {
522 flush_all_inserts ();
525 if ((auto_return_enabled || synced_to_jack() || _requested_return_frame >= 0) &&
526 !(ptw & PostTransportLocate)) {
528 /* no explicit locate queued */
530 bool do_locate = false;
532 if (_requested_return_frame >= 0) {
534 /* explicit return request pre-queued in event list. overrides everything else */
536 cerr << "explicit auto-return to " << _requested_return_frame << endl;
538 _transport_frame = _requested_return_frame;
542 if (config.get_auto_return()) {
546 /* don't try to handle loop play when synced to JACK */
548 if (!synced_to_jack()) {
550 Location *location = _locations->auto_loop_location();
553 _transport_frame = location->start();
555 _transport_frame = _last_roll_location;
560 } else if (_play_range) {
562 /* return to start of range */
564 if (!current_audio_range.empty()) {
565 _transport_frame = current_audio_range.front().start;
571 /* regular auto-return */
573 _transport_frame = _last_roll_location;
579 _requested_return_frame = -1;
582 _engine.transport_locate (_transport_frame);
590 /* do this before seeking, because otherwise the tracks will do the wrong thing in seamless loop mode.
593 if (ptw & PostTransportClearSubstate) {
598 /* this for() block can be put inside the previous if() and has the effect of ... ??? what */
600 DEBUG_TRACE (DEBUG::Transport, X_("Butler PTW: locate\n"));
601 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
602 DEBUG_TRACE (DEBUG::Transport, string_compose ("Butler PTW: locate on %1\n", (*i)->name()));
603 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
604 if (tr && !tr->hidden()) {
605 tr->non_realtime_locate (_transport_frame);
608 if (on_entry != g_atomic_int_get (&_butler->should_do_transport_work)) {
610 /* we will be back */
617 if (!_engine.freewheeling()) {
618 send_full_time_code (_transport_frame);
620 if (!dynamic_cast<MTC_Slave*>(_slave)) {
621 MIDI::Manager::instance()->mmc()->send (MIDI::MachineControlCommand (MIDI::MachineControl::cmdStop));
622 send_mmc_locate (_transport_frame);
626 if ((ptw & PostTransportLocate) && get_record_enabled()) {
627 /* capture start has been changed, so save pending state */
628 save_state ("", true);
632 /* always try to get rid of this */
634 remove_pending_capture_state ();
636 /* save the current state of things if appropriate */
638 if (did_record && !saved) {
639 save_state (_current_snapshot_name);
642 if (ptw & PostTransportStop) {
647 PositionChanged (_transport_frame); /* EMIT SIGNAL */
648 TransportStateChange (); /* EMIT SIGNAL */
650 /* and start it up again if relevant */
652 if ((ptw & PostTransportLocate) && !config.get_external_sync() && pending_locate_roll) {
653 request_transport_speed (1.0);
654 pending_locate_roll = false;
659 Session::check_declick_out ()
661 bool locate_required = transport_sub_state & PendingLocate;
663 /* this is called after a process() iteration. if PendingDeclickOut was set,
664 it means that we were waiting to declick the output (which has just been
665 done) before doing something else. this is where we do that "something else".
667 note: called from the audio thread.
670 if (transport_sub_state & PendingDeclickOut) {
672 if (locate_required) {
673 start_locate (pending_locate_frame, pending_locate_roll, pending_locate_flush);
674 transport_sub_state &= ~(PendingDeclickOut|PendingLocate);
676 stop_transport (pending_abort);
677 transport_sub_state &= ~(PendingDeclickOut|PendingLocate);
683 Session::unset_play_loop ()
686 clear_events (SessionEvent::AutoLoop);
688 // set all tracks to NOT use internal looping
689 boost::shared_ptr<RouteList> rl = routes.reader ();
690 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
691 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
692 if (tr && !tr->hidden()) {
699 Session::set_play_loop (bool yn)
701 /* Called from event-handling context */
705 if (yn == play_loop || (actively_recording() && yn) || (loc = _locations->auto_loop_location()) == 0) {
706 /* nothing to do, or can't change loop status while recording */
710 if (yn && Config->get_seamless_loop() && synced_to_jack()) {
711 warning << string_compose (
712 _("Seamless looping cannot be supported while %1 is using JACK transport.\n"
713 "Recommend changing the configured options"), PROGRAM_NAME)
726 if (Config->get_seamless_loop()) {
727 // set all tracks to use internal looping
728 boost::shared_ptr<RouteList> rl = routes.reader ();
729 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
730 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
731 if (tr && !tr->hidden()) {
737 // set all tracks to NOT use internal looping
738 boost::shared_ptr<RouteList> rl = routes.reader ();
739 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
740 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
741 if (tr && !tr->hidden()) {
747 /* put the loop event into the event list */
749 SessionEvent* event = new SessionEvent (SessionEvent::AutoLoop, SessionEvent::Replace, loc->end(), loc->start(), 0.0f);
752 /* locate to start of loop and roll. If doing seamless loop, force a
753 locate+buffer refill even if we are positioned there already.
756 start_locate (loc->start(), true, true, false, Config->get_seamless_loop());
764 TransportStateChange ();
767 Session::flush_all_inserts ()
769 boost::shared_ptr<RouteList> r = routes.reader ();
771 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
772 (*i)->flush_processors ();
777 Session::start_locate (framepos_t target_frame, bool with_roll, bool with_flush, bool with_loop, bool force)
779 if (synced_to_jack()) {
784 _slave->speed_and_position (sp, pos);
786 if (target_frame != pos) {
788 /* tell JACK to change transport position, and we will
789 follow along later in ::follow_slave()
792 _engine.transport_locate (target_frame);
794 if (sp != 1.0f && with_roll) {
795 _engine.transport_start ();
801 locate (target_frame, with_roll, with_flush, with_loop, force);
806 Session::micro_locate (framecnt_t distance)
808 boost::shared_ptr<RouteList> rl = routes.reader();
809 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
810 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
811 if (tr && !tr->can_internal_playback_seek (distance)) {
816 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
817 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
819 tr->internal_playback_seek (distance);
823 _transport_frame += distance;
827 /** @param with_mmc true to send a MMC locate command when the locate is done */
829 Session::locate (framepos_t target_frame, bool with_roll, bool with_flush, bool with_loop, bool force, bool with_mmc)
831 if (actively_recording() && !with_loop) {
835 if (!force && _transport_frame == target_frame && !loop_changing && !with_loop) {
837 set_transport_speed (1.0, false);
839 loop_changing = false;
840 Located (); /* EMIT SIGNAL */
844 if (_transport_speed) {
845 /* schedule a declick. we'll be called again when its done */
847 if (!(transport_sub_state & PendingDeclickOut)) {
848 transport_sub_state |= (PendingDeclickOut|PendingLocate);
849 pending_locate_frame = target_frame;
850 pending_locate_roll = with_roll;
851 pending_locate_flush = with_flush;
856 // Update Timecode time
857 // [DR] FIXME: find out exactly where this should go below
858 _transport_frame = target_frame;
859 timecode_time(_transport_frame, transmitting_timecode_time);
860 outbound_mtc_timecode_frame = _transport_frame;
861 next_quarter_frame_to_send = 0;
863 /* do "stopped" stuff if:
866 * no autoplay in effect AND
867 * we're not going to keep rolling after the locate AND
868 * !(playing a loop with JACK sync)
872 if (transport_rolling() && (!auto_play_legal || !config.get_auto_play()) && !with_roll && !(synced_to_jack() && play_loop)) {
873 realtime_stop (false, true); // XXX paul - check if the 2nd arg is really correct
875 /* otherwise tell the world that we located */
879 if (force || !with_loop || loop_changing) {
881 PostTransportWork todo = PostTransportLocate;
884 todo = PostTransportWork (todo | PostTransportRoll);
887 add_post_transport_work (todo);
888 _butler->schedule_transport_work ();
892 /* this is functionally what clear_clicks() does but with a tentative lock */
894 Glib::RWLock::WriterLock clickm (click_lock, Glib::TRY_LOCK);
896 if (clickm.locked()) {
898 for (Clicks::iterator i = clicks.begin(); i != clicks.end(); ++i) {
907 /* switch from input if we're going to roll */
908 if (Config->get_monitoring_model() == HardwareMonitoring) {
909 set_track_monitor_input_status (!config.get_auto_input());
912 /* otherwise we're going to stop, so do the opposite */
913 if (Config->get_monitoring_model() == HardwareMonitoring) {
914 set_track_monitor_input_status (true);
918 /* cancel looped playback if transport pos outside of loop range */
920 Location* al = _locations->auto_loop_location();
922 if (al && (_transport_frame < al->start() || _transport_frame > al->end())) {
923 // cancel looping directly, this is called from event handling context
924 set_play_loop (false);
926 else if (al && _transport_frame == al->start()) {
928 // this is only necessary for seamless looping
930 boost::shared_ptr<RouteList> rl = routes.reader();
931 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
932 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
933 if (tr && tr->record_enabled ()) {
934 // tell it we've looped, so it can deal with the record state
935 tr->transport_looped(_transport_frame);
940 TransportLooped(); // EMIT SIGNAL
944 loop_changing = false;
946 _send_timecode_update = true;
949 send_mmc_locate (_transport_frame);
952 Located (); /* EMIT SIGNAL */
955 /** Set the transport speed.
956 * Called from the process thread.
957 * @param speed New speed
960 Session::set_transport_speed (double speed, bool abort, bool clear_state)
962 DEBUG_TRACE (DEBUG::Transport, string_compose ("@ %5 Set transport speed to %1, abort = %2 clear_state = %3, current = %4\n",
963 speed, abort, clear_state, _transport_speed, _transport_frame));
965 if (_transport_speed == speed) {
969 if (actively_recording() && speed != 1.0 && speed != 0.0) {
970 /* no varispeed during recording */
974 _target_transport_speed = fabs(speed);
976 /* 8.0 max speed is somewhat arbitrary but based on guestimates regarding disk i/o capability
977 and user needs. We really need CD-style "skip" playback for ffwd and rewind.
981 speed = min (8.0, speed);
982 } else if (speed < 0) {
983 speed = max (-8.0, speed);
986 if (transport_rolling() && speed == 0.0) {
988 /* we are rolling and we want to stop */
990 if (Config->get_monitoring_model() == HardwareMonitoring) {
991 set_track_monitor_input_status (true);
994 if (synced_to_jack ()) {
996 /* do this here because our response to the slave won't
1002 _engine.transport_stop ();
1004 stop_transport (abort);
1009 } else if (transport_stopped() && speed == 1.0) {
1011 /* we are stopped and we want to start rolling at speed 1 */
1013 if (Config->get_monitoring_model() == HardwareMonitoring && config.get_auto_input()) {
1014 set_track_monitor_input_status (false);
1017 if (synced_to_jack()) {
1018 _engine.transport_start ();
1025 /* not zero, not 1.0 ... varispeed */
1027 if ((synced_to_jack()) && speed != 0.0 && speed != 1.0) {
1028 warning << string_compose (
1029 _("Global varispeed cannot be supported while %1 is connected to JACK transport control"),
1035 if (actively_recording()) {
1039 if (speed > 0.0 && _transport_frame == current_end_frame()) {
1043 if (speed < 0.0 && _transport_frame == 0) {
1049 /* if we are reversing relative to the current speed, or relative to the speed
1050 before the last stop, then we have to do extra work.
1053 PostTransportWork todo = PostTransportWork (0);
1055 if ((_transport_speed && speed * _transport_speed < 0.0) || (_last_transport_speed * speed < 0.0) || (_last_transport_speed == 0.0f && speed < 0.0f)) {
1056 todo = PostTransportWork (todo | PostTransportReverse);
1057 _last_roll_or_reversal_location = _transport_frame;
1060 _last_transport_speed = _transport_speed;
1061 _transport_speed = speed;
1063 boost::shared_ptr<RouteList> rl = routes.reader();
1064 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
1065 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
1066 if (tr && tr->realtime_set_speed (tr->speed(), true)) {
1067 todo = PostTransportWork (todo | PostTransportSpeed);
1072 add_post_transport_work (todo);
1073 _butler->schedule_transport_work ();
1076 TransportStateChange (); /* EMIT SIGNAL */
1081 /** Stop the transport. */
1083 Session::stop_transport (bool abort, bool clear_state)
1085 if (_transport_speed == 0.0f) {
1089 if (actively_recording() && !(transport_sub_state & StopPendingCapture) && worst_input_latency() > current_block_size) {
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);
1099 /* we need to capture the audio that has still not yet been received by the system
1100 at the time the stop is requested, so we have to roll past that time.
1102 we want to declick before stopping, so schedule the autostop for one
1103 block before the actual end. we'll declick in the subsequent block,
1104 and then we'll really be stopped.
1107 DEBUG_TRACE (DEBUG::Transport, string_compose ("stop transport requested @ %1, scheduled for + %2 - %3 = %4, abort = %5\n",
1108 _transport_frame, _worst_input_latency, current_block_size,
1109 _transport_frame - _worst_input_latency - current_block_size,
1112 SessionEvent *ev = new SessionEvent (SessionEvent::StopOnce, SessionEvent::Replace,
1113 _transport_frame + _worst_input_latency - current_block_size,
1117 transport_sub_state |= StopPendingCapture;
1118 pending_abort = abort;
1122 if ((transport_sub_state & PendingDeclickOut) == 0) {
1124 if (!(transport_sub_state & StopPendingCapture)) {
1125 boost::shared_ptr<RouteList> rl = routes.reader();
1126 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
1127 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
1129 tr->prepare_to_stop (_transport_frame);
1134 transport_sub_state |= PendingDeclickOut;
1135 /* we'll be called again after the declick */
1136 pending_abort = abort;
1140 realtime_stop (abort, clear_state);
1141 _butler->schedule_transport_work ();
1144 /** Called from the process thread */
1146 Session::start_transport ()
1148 DEBUG_TRACE (DEBUG::Transport, "start_transport\n");
1150 _last_roll_location = _transport_frame;
1151 _last_roll_or_reversal_location = _transport_frame;
1153 have_looped = false;
1155 /* if record status is Enabled, move it to Recording. if its
1156 already Recording, move it to Disabled.
1159 switch (record_status()) {
1161 if (!config.get_punch_in()) {
1168 disable_record (false);
1176 transport_sub_state |= PendingDeclickIn;
1178 _transport_speed = 1.0;
1179 _target_transport_speed = 1.0;
1181 boost::shared_ptr<RouteList> rl = routes.reader();
1182 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
1183 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
1185 tr->realtime_set_speed (tr->speed(), true);
1187 (*i)->automation_snapshot (_transport_frame, true);
1190 if (!_engine.freewheeling()) {
1191 Timecode::Time time;
1192 timecode_time_subframes (_transport_frame, time);
1193 if (!dynamic_cast<MTC_Slave*>(_slave)) {
1194 MIDI::Manager::instance()->mmc()->send (MIDI::MachineControlCommand (MIDI::MachineControl::cmdDeferredPlay));
1198 TransportStateChange (); /* EMIT SIGNAL */
1201 /** Do any transport work in the audio thread that needs to be done after the
1202 * transport thread is finished. Audio thread, realtime safe.
1205 Session::post_transport ()
1207 PostTransportWork ptw = post_transport_work ();
1209 if (ptw & PostTransportAudition) {
1210 if (auditioner && auditioner->auditioning()) {
1211 process_function = &Session::process_audition;
1213 process_function = &Session::process_with_events;
1217 if (ptw & PostTransportStop) {
1219 transport_sub_state = 0;
1222 if (ptw & PostTransportLocate) {
1224 if (((!config.get_external_sync() && (auto_play_legal && config.get_auto_play())) && !_exporting) || (ptw & PostTransportRoll)) {
1228 transport_sub_state = 0;
1233 /* XXX is this really safe? shouldn't we just be unsetting the bits that we actually
1236 set_post_transport_work (PostTransportWork (0));
1240 Session::reset_rf_scale (framecnt_t motion)
1242 cumulative_rf_motion += motion;
1244 if (cumulative_rf_motion < 4 * _current_frame_rate) {
1246 } else if (cumulative_rf_motion < 8 * _current_frame_rate) {
1248 } else if (cumulative_rf_motion < 16 * _current_frame_rate) {
1260 Session::use_sync_source (Slave* new_slave)
1262 /* Runs in process() context */
1264 bool non_rt_required = false;
1266 /* XXX this deletion is problematic because we're in RT context */
1271 send_full_time_code (_transport_frame);
1273 boost::shared_ptr<RouteList> rl = routes.reader();
1274 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
1275 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
1276 if (tr && !tr->hidden()) {
1277 if (tr->realtime_set_speed (tr->speed(), true)) {
1278 non_rt_required = true;
1280 tr->set_slaved (_slave != 0);
1284 if (non_rt_required) {
1285 add_post_transport_work (PostTransportSpeed);
1286 _butler->schedule_transport_work ();
1293 Session::drop_sync_source ()
1295 request_sync_source (0);
1299 Session::switch_to_sync_source (SyncSource src)
1303 DEBUG_TRACE (DEBUG::Slave, string_compose ("Setting up sync source %1\n", enum_2_string (src)));
1307 if (_slave && dynamic_cast<MTC_Slave*>(_slave)) {
1312 new_slave = new MTC_Slave (*this, *MIDI::Manager::instance()->mtc_input_port());
1315 catch (failed_constructor& err) {
1321 if (_slave && dynamic_cast<MIDIClock_Slave*>(_slave)) {
1326 new_slave = new MIDIClock_Slave (*this, *MIDI::Manager::instance()->midi_clock_input_port(), 24);
1329 catch (failed_constructor& err) {
1335 if (_slave && dynamic_cast<JACK_Slave*>(_slave)) {
1339 if (config.get_video_pullup() != 0.0f) {
1343 new_slave = new JACK_Slave (_engine.jack());
1351 request_sync_source (new_slave);
1355 Session::set_track_speed (Track* track, double speed)
1357 if (track->realtime_set_speed (speed, false)) {
1358 add_post_transport_work (PostTransportSpeed);
1359 _butler->schedule_transport_work ();
1365 Session::unset_play_range ()
1367 _play_range = false;
1368 _clear_event_type (SessionEvent::RangeStop);
1369 _clear_event_type (SessionEvent::RangeLocate);
1373 Session::set_play_range (list<AudioRange>& range, bool leave_rolling)
1377 /* Called from event-processing context */
1379 unset_play_range ();
1381 if (range.empty()) {
1382 /* _play_range set to false in unset_play_range()
1384 if (!leave_rolling) {
1385 /* stop transport */
1386 SessionEvent* ev = new SessionEvent (SessionEvent::SetTransportSpeed, SessionEvent::Add, SessionEvent::Immediate, 0, 0.0f, false);
1394 /* cancel loop play */
1397 list<AudioRange>::size_type sz = range.size();
1401 list<AudioRange>::iterator i = range.begin();
1402 list<AudioRange>::iterator next;
1404 while (i != range.end()) {
1409 /* locating/stopping is subject to delays for declicking.
1412 framepos_t requested_frame = i->end;
1414 if (requested_frame > current_block_size) {
1415 requested_frame -= current_block_size;
1417 requested_frame = 0;
1420 if (next == range.end()) {
1421 ev = new SessionEvent (SessionEvent::RangeStop, SessionEvent::Add, requested_frame, 0, 0.0f);
1423 ev = new SessionEvent (SessionEvent::RangeLocate, SessionEvent::Add, requested_frame, (*next).start, 0.0f);
1431 } else if (sz == 1) {
1433 ev = new SessionEvent (SessionEvent::RangeStop, SessionEvent::Add, range.front().end, 0, 0.0f);
1438 /* save range so we can do auto-return etc. */
1440 current_audio_range = range;
1442 /* now start rolling at the right place */
1444 ev = new SessionEvent (SessionEvent::LocateRoll, SessionEvent::Add, SessionEvent::Immediate, range.front().start, 0.0f, false);
1447 TransportStateChange ();
1451 Session::request_bounded_roll (framepos_t start, framepos_t end)
1453 AudioRange ar (start, end, 0);
1454 list<AudioRange> lar;
1457 request_play_range (&lar, true);
1460 Session::request_roll_at_and_return (framepos_t start, framepos_t return_to)
1462 SessionEvent *ev = new SessionEvent (SessionEvent::LocateRollLocate, SessionEvent::Add, SessionEvent::Immediate, return_to, 1.0);
1463 ev->target2_frame = start;
1468 Session::engine_halted ()
1472 /* there will be no more calls to process(), so
1473 we'd better clean up for ourselves, right now.
1475 but first, make sure the butler is out of
1479 g_atomic_int_set (&_butler->should_do_transport_work, 0);
1480 set_post_transport_work (PostTransportWork (0));
1483 realtime_stop (false, true);
1484 non_realtime_stop (false, 0, ignored);
1485 transport_sub_state = 0;
1487 TransportStateChange (); /* EMIT SIGNAL */
1492 Session::xrun_recovery ()
1494 Xrun (_transport_frame); /* EMIT SIGNAL */
1496 if (Config->get_stop_recording_on_xrun() && actively_recording()) {
1498 /* it didn't actually halt, but we need
1499 to handle things in the same way.
1507 Session::route_processors_changed (RouteProcessorChange c)
1509 if (ignore_route_processor_changes) {
1513 if (c.type == RouteProcessorChange::MeterPointChange) {
1517 update_latency_compensation ();
1524 Session::allow_auto_play (bool yn)
1526 auto_play_legal = yn;
1530 Session::reset_jack_connection (jack_client_t* jack)
1534 if (_slave && ((js = dynamic_cast<JACK_Slave*> (_slave)) != 0)) {
1535 js->reset_client (jack);
1540 Session::maybe_stop (framepos_t limit)
1542 if ((_transport_speed > 0.0f && _transport_frame >= limit) || (_transport_speed < 0.0f && _transport_frame == 0)) {
1543 if (synced_to_jack () && config.get_jack_time_master ()) {
1544 _engine.transport_stop ();
1545 } else if (!synced_to_jack ()) {
1554 Session::send_mmc_locate (framepos_t t)
1556 if (!_engine.freewheeling()) {
1557 Timecode::Time time;
1558 timecode_time_subframes (t, time);
1559 MIDI::Manager::instance()->mmc()->send (MIDI::MachineControlCommand (time));
1563 /** Ask the transport to not send timecode until further notice. The suspension
1564 * will come into effect some finite time after this call, and timecode_transmission_suspended()
1565 * should be checked by the caller to find out when.
1568 Session::request_suspend_timecode_transmission ()
1570 SessionEvent* ev = new SessionEvent (SessionEvent::SetTimecodeTransmission, SessionEvent::Add, SessionEvent::Immediate, 0, 0, false);
1575 Session::request_resume_timecode_transmission ()
1577 SessionEvent* ev = new SessionEvent (SessionEvent::SetTimecodeTransmission, SessionEvent::Add, SessionEvent::Immediate, 0, 0, true);
1582 Session::timecode_transmission_suspended () const
1584 return g_atomic_int_get (&_suspend_timecode_transmission) == 1;