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"
34 #include "ardour/ardour.h"
35 #include "ardour/audio_diskstream.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_diskstream_speed (Diskstream& ds, double speed)
112 SessionEvent* ev = new SessionEvent (SessionEvent::SetDiskstreamSpeed, 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 (nframes_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 (nframes64_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 diskstreams
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 // FIXME: where should this really be? [DR]
186 //send_full_time_code();
187 deliver_mmc (MIDI::MachineControl::cmdStop, 0);
188 deliver_mmc (MIDI::MachineControl::cmdLocate, _transport_frame);
190 if (_transport_speed < 0.0f) {
191 todo = (PostTransportWork (todo | PostTransportStop | PostTransportReverse));
193 todo = PostTransportWork (todo | PostTransportStop);
196 if (actively_recording()) {
198 /* move the transport position back to where the
199 request for a stop was noticed. we rolled
200 past that point to pick up delayed input (and/or to delick)
203 if (_worst_output_latency > current_block_size) {
204 /* we rolled past the stop point to pick up data that had
205 not yet arrived. move back to where the stop occured.
207 decrement_transport_position (current_block_size + (_worst_output_latency - current_block_size));
209 decrement_transport_position (current_block_size);
212 /* the duration change is not guaranteed to have happened, but is likely */
214 todo = PostTransportWork (todo | PostTransportDuration);
218 todo = PostTransportWork (todo | PostTransportAbort);
222 todo = PostTransportWork (todo | PostTransportClearSubstate);
226 add_post_transport_work (todo);
229 _clear_event_type (SessionEvent::StopOnce);
230 _clear_event_type (SessionEvent::RangeStop);
231 _clear_event_type (SessionEvent::RangeLocate);
233 disable_record (true);
235 reset_slave_state ();
237 _transport_speed = 0;
238 _target_transport_speed = 0;
240 if (config.get_use_video_sync()) {
241 waiting_for_sync_offset = true;
244 transport_sub_state = ((!config.get_external_sync()&& config.get_auto_return()) ? AutoReturning : 0);
248 Session::butler_transport_work ()
252 PostTransportWork ptw;
253 boost::shared_ptr<RouteList> r = routes.reader ();
254 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
256 int on_entry = g_atomic_int_get (&_butler->should_do_transport_work);
258 ptw = post_transport_work();
260 DEBUG_TRACE (DEBUG::Transport, string_compose ("Butler transport work, todo = %1\n", enum_2_string (ptw)));
262 if (ptw & PostTransportCurveRealloc) {
263 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
264 (*i)->curve_reallocate();
268 if (ptw & PostTransportInputChange) {
269 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
270 (*i)->non_realtime_input_change ();
274 if (ptw & PostTransportSpeed) {
275 non_realtime_set_speed ();
278 if (ptw & PostTransportReverse) {
281 cumulative_rf_motion = 0;
284 /* don't seek if locate will take care of that in non_realtime_stop() */
286 if (!(ptw & PostTransportLocate)) {
288 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
289 if (!(*i)->hidden()) {
290 (*i)->non_realtime_locate (_transport_frame);
292 if (on_entry != g_atomic_int_get (&_butler->should_do_transport_work)) {
293 /* new request, stop seeking, and start again */
294 g_atomic_int_dec_and_test (&_butler->should_do_transport_work);
301 if (ptw & PostTransportLocate) {
302 non_realtime_locate ();
305 if (ptw & PostTransportStop) {
306 non_realtime_stop (ptw & PostTransportAbort, on_entry, finished);
308 g_atomic_int_dec_and_test (&_butler->should_do_transport_work);
313 if (ptw & PostTransportOverWrite) {
314 non_realtime_overwrite (on_entry, finished);
316 g_atomic_int_dec_and_test (&_butler->should_do_transport_work);
321 if (ptw & PostTransportAudition) {
322 non_realtime_set_audition ();
325 g_atomic_int_dec_and_test (&_butler->should_do_transport_work);
327 DEBUG_TRACE (DEBUG::Transport, X_("Butler transport work all done\n"));
331 Session::non_realtime_set_speed ()
333 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
335 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
336 (*i)->non_realtime_set_speed ();
341 Session::non_realtime_overwrite (int on_entry, bool& finished)
343 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
345 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
346 if ((*i)->pending_overwrite) {
347 (*i)->overwrite_existing_buffers ();
349 if (on_entry != g_atomic_int_get (&_butler->should_do_transport_work)) {
358 Session::non_realtime_locate ()
360 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
362 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
363 (*i)->non_realtime_locate (_transport_frame);
369 Session::non_realtime_stop (bool abort, int on_entry, bool& finished)
375 PostTransportWork ptw = post_transport_work();
380 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
382 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
383 if ((*i)->get_captured_frames () != 0) {
389 /* stop and locate are merged here because they share a lot of common stuff */
392 now = localtime (&xnow);
395 auditioner->cancel_audition ();
399 cumulative_rf_motion = 0;
403 begin_reversible_command ("capture");
405 Location* loc = _locations.session_range_location();
406 bool change_end = false;
408 if (_transport_frame < loc->end()) {
410 /* stopped recording before current end */
412 if (config.get_end_marker_is_free()) {
414 /* first capture for this session, move end back to where we are */
419 } else if (_transport_frame > loc->end()) {
421 /* stopped recording after the current end, extend it */
427 XMLNode &before = loc->get_state();
428 loc->set_end(_transport_frame);
429 XMLNode &after = loc->get_state();
430 add_command (new MementoCommand<Location>(*loc, &before, &after));
433 config.set_end_marker_is_free (false);
434 _have_captured = true;
437 DEBUG_TRACE (DEBUG::Transport, X_("Butler PTW: DS stop\n"));
438 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
439 (*i)->transport_stopped (*now, xnow, abort);
442 boost::shared_ptr<RouteList> r = routes.reader ();
444 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
445 if (!(*i)->is_hidden()) {
446 (*i)->set_pending_declick (0);
451 commit_reversible_command ();
454 if (_engine.running()) {
455 update_latency_compensation (true, abort);
458 bool const auto_return_enabled =
459 (!config.get_external_sync() && config.get_auto_return());
461 if (auto_return_enabled ||
462 (ptw & PostTransportLocate) ||
463 (_requested_return_frame >= 0) ||
466 if (pending_locate_flush) {
467 flush_all_inserts ();
470 if ((auto_return_enabled || synced_to_jack() || _requested_return_frame >= 0) &&
471 !(ptw & PostTransportLocate)) {
473 /* no explicit locate queued */
475 bool do_locate = false;
477 if (_requested_return_frame >= 0) {
479 /* explicit return request pre-queued in event list. overrides everything else */
481 cerr << "explicit auto-return to " << _requested_return_frame << endl;
483 _transport_frame = _requested_return_frame;
487 if (config.get_auto_return()) {
491 /* don't try to handle loop play when synced to JACK */
493 if (!synced_to_jack()) {
495 Location *location = _locations.auto_loop_location();
498 _transport_frame = location->start();
500 _transport_frame = _last_roll_location;
505 } else if (_play_range) {
507 /* return to start of range */
509 if (!current_audio_range.empty()) {
510 _transport_frame = current_audio_range.front().start;
516 /* regular auto-return */
518 _transport_frame = _last_roll_location;
524 _requested_return_frame = -1;
527 _engine.transport_locate (_transport_frame);
533 /* do this before seeking, because otherwise the Diskstreams will do the wrong thing in seamless loop mode.
536 if (ptw & PostTransportClearSubstate) {
541 /* this for() block can be put inside the previous if() and has the effect of ... ??? what */
544 DEBUG_TRACE (DEBUG::Transport, X_("Butler PTW: locate\n"));
546 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
547 if (!(*i)->hidden()) {
548 DEBUG_TRACE (DEBUG::Transport, string_compose ("Butler PTW: locate on %1\n", (*i)->name()));
549 (*i)->non_realtime_locate (_transport_frame);
552 if (on_entry != g_atomic_int_get (&_butler->should_do_transport_work)) {
554 /* we will be back */
561 send_full_time_code (0);
562 deliver_mmc (MIDI::MachineControl::cmdStop, 0);
563 deliver_mmc (MIDI::MachineControl::cmdLocate, _transport_frame);
565 if ((ptw & PostTransportLocate) && get_record_enabled()) {
566 /* capture start has been changed, so save pending state */
567 save_state ("", true);
571 /* always try to get rid of this */
573 remove_pending_capture_state ();
575 /* save the current state of things if appropriate */
577 if (did_record && !saved) {
578 save_state (_current_snapshot_name);
581 if (ptw & PostTransportDuration) {
582 DurationChanged (); /* EMIT SIGNAL */
585 if (ptw & PostTransportStop) {
590 // can't cast away volatile so copy and emit that
591 nframes64_t tframe = _transport_frame;
592 PositionChanged (tframe); /* EMIT SIGNAL */
593 TransportStateChange (); /* EMIT SIGNAL */
595 /* and start it up again if relevant */
597 if ((ptw & PostTransportLocate) && !config.get_external_sync() && pending_locate_roll) {
598 request_transport_speed (1.0);
599 pending_locate_roll = false;
604 Session::check_declick_out ()
606 bool locate_required = transport_sub_state & PendingLocate;
608 /* this is called after a process() iteration. if PendingDeclickOut was set,
609 it means that we were waiting to declick the output (which has just been
610 done) before doing something else. this is where we do that "something else".
612 note: called from the audio thread.
615 if (transport_sub_state & PendingDeclickOut) {
617 if (locate_required) {
618 start_locate (pending_locate_frame, pending_locate_roll, pending_locate_flush);
619 transport_sub_state &= ~(PendingDeclickOut|PendingLocate);
621 stop_transport (pending_abort);
622 transport_sub_state &= ~(PendingDeclickOut|PendingLocate);
628 Session::unset_play_loop ()
631 clear_events (SessionEvent::AutoLoop);
633 // set all diskstreams to NOT use internal looping
634 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
635 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
636 if (!(*i)->hidden()) {
643 Session::set_play_loop (bool yn)
645 /* Called from event-handling context */
649 if (yn == play_loop || (actively_recording() && yn) || (loc = _locations.auto_loop_location()) == 0) {
650 /* nothing to do, or can't change loop status while recording */
656 if (yn && Config->get_seamless_loop() && synced_to_jack()) {
657 warning << string_compose (_("Seamless looping cannot be supported while %1 is using JACK transport.\n"
658 "Recommend changing the configured options"), PROGRAM_NAME)
671 if (Config->get_seamless_loop()) {
672 // set all diskstreams to use internal looping
673 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
674 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
675 if (!(*i)->hidden()) {
676 (*i)->set_loop (loc);
681 // set all diskstreams to NOT use internal looping
682 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
683 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
684 if (!(*i)->hidden()) {
690 /* put the loop event into the event list */
692 SessionEvent* event = new SessionEvent (SessionEvent::AutoLoop, SessionEvent::Replace, loc->end(), loc->start(), 0.0f);
695 /* locate to start of loop and roll. If doing seamless loop, force a
696 locate+buffer refill even if we are positioned there already.
699 start_locate (loc->start(), true, true, false, Config->get_seamless_loop());
707 TransportStateChange ();
710 Session::flush_all_inserts ()
712 boost::shared_ptr<RouteList> r = routes.reader ();
714 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
715 (*i)->flush_processors ();
720 Session::start_locate (nframes64_t target_frame, bool with_roll, bool with_flush, bool with_loop, bool force)
722 if (synced_to_jack()) {
727 _slave->speed_and_position (sp, pos);
729 if (target_frame != pos) {
731 /* tell JACK to change transport position, and we will
732 follow along later in ::follow_slave()
735 _engine.transport_locate (target_frame);
737 if (sp != 1.0f && with_roll) {
738 _engine.transport_start ();
744 locate (target_frame, with_roll, with_flush, with_loop, force);
749 Session::micro_locate (nframes_t distance)
751 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
753 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
754 if (!(*i)->can_internal_playback_seek (distance)) {
759 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
760 (*i)->internal_playback_seek (distance);
763 _transport_frame += distance;
768 Session::locate (nframes64_t target_frame, bool with_roll, bool with_flush, bool with_loop, bool force)
770 if (actively_recording() && !with_loop) {
774 if (!force && _transport_frame == target_frame && !loop_changing && !with_loop) {
776 set_transport_speed (1.0, false);
778 loop_changing = false;
782 // Update Timecode time
783 // [DR] FIXME: find out exactly where this should go below
784 _transport_frame = target_frame;
785 timecode_time(_transport_frame, transmitting_timecode_time);
786 outbound_mtc_timecode_frame = _transport_frame;
787 next_quarter_frame_to_send = 0;
789 if (_transport_speed && (!with_loop || loop_changing)) {
790 /* schedule a declick. we'll be called again when its done */
792 if (!(transport_sub_state & PendingDeclickOut)) {
793 transport_sub_state |= (PendingDeclickOut|PendingLocate);
794 pending_locate_frame = target_frame;
795 pending_locate_roll = with_roll;
796 pending_locate_flush = with_flush;
801 if (transport_rolling() && (!auto_play_legal || !config.get_auto_play()) && !with_roll && !(synced_to_jack() && play_loop)) {
802 realtime_stop (false, true); // XXX paul - check if the 2nd arg is really correct
805 if (force || !with_loop || loop_changing) {
807 PostTransportWork todo = PostTransportLocate;
810 todo = PostTransportWork (todo | PostTransportRoll);
813 add_post_transport_work (todo);
814 _butler->schedule_transport_work ();
818 /* this is functionally what clear_clicks() does but with a tentative lock */
820 Glib::RWLock::WriterLock clickm (click_lock, Glib::TRY_LOCK);
822 if (clickm.locked()) {
824 for (Clicks::iterator i = clicks.begin(); i != clicks.end(); ++i) {
833 /* switch from input if we're going to roll */
834 if (Config->get_monitoring_model() == HardwareMonitoring) {
836 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
838 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
839 if ((*i)->record_enabled ()) {
840 //cerr << "switching from input" << __FILE__ << __LINE__ << endl << endl;
841 (*i)->monitor_input (!config.get_auto_input());
846 /* otherwise we're going to stop, so do the opposite */
847 if (Config->get_monitoring_model() == HardwareMonitoring) {
848 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
850 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
851 if ((*i)->record_enabled ()) {
852 //cerr << "switching to input" << __FILE__ << __LINE__ << endl << endl;
853 (*i)->monitor_input (true);
859 /* cancel looped playback if transport pos outside of loop range */
861 Location* al = _locations.auto_loop_location();
863 if (al && (_transport_frame < al->start() || _transport_frame > al->end())) {
864 // cancel looping directly, this is called from event handling context
865 set_play_loop (false);
867 else if (al && _transport_frame == al->start()) {
869 // this is only necessary for seamless looping
871 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
873 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
874 if ((*i)->record_enabled ()) {
875 // tell it we've looped, so it can deal with the record state
876 (*i)->transport_looped(_transport_frame);
881 TransportLooped(); // EMIT SIGNAL
885 loop_changing = false;
887 _send_timecode_update = true;
889 Located (); /* EMIT SIGNAL */
892 /** Set the transport speed.
893 * @param speed New speed
897 Session::set_transport_speed (double speed, bool abort, bool clear_state)
899 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));
901 if (_transport_speed == speed) {
905 _target_transport_speed = fabs(speed);
907 /* 8.0 max speed is somewhat arbitrary but based on guestimates regarding disk i/o capability
908 and user needs. We really need CD-style "skip" playback for ffwd and rewind.
912 speed = min (8.0, speed);
913 } else if (speed < 0) {
914 speed = max (-8.0, speed);
917 if (transport_rolling() && speed == 0.0) {
919 /* we are rolling and we want to stop */
921 if (Config->get_monitoring_model() == HardwareMonitoring)
923 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
925 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
926 if ((*i)->record_enabled ()) {
927 //cerr << "switching to input" << __FILE__ << __LINE__ << endl << endl;
928 (*i)->monitor_input (true);
933 if (synced_to_jack ()) {
935 /* do this here because our response to the slave won't
941 _engine.transport_stop ();
943 stop_transport (abort);
946 } else if (transport_stopped() && speed == 1.0) {
948 /* we are stopped and we want to start rolling at speed 1 */
950 if (Config->get_monitoring_model() == HardwareMonitoring) {
952 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
954 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
955 if (config.get_auto_input() && (*i)->record_enabled ()) {
956 //cerr << "switching from input" << __FILE__ << __LINE__ << endl << endl;
957 (*i)->monitor_input (false);
962 if (synced_to_jack()) {
963 _engine.transport_start ();
970 if ((synced_to_jack()) && speed != 0.0 && speed != 1.0) {
971 warning << string_compose (_("Global varispeed cannot be supported while %1 is connected to JACK transport control"),
977 if (actively_recording()) {
981 if (speed > 0.0 && _transport_frame == current_end_frame()) {
985 if (speed < 0.0 && _transport_frame == 0) {
991 /* if we are reversing relative to the current speed, or relative to the speed
992 before the last stop, then we have to do extra work.
995 PostTransportWork todo = PostTransportWork (0);
997 if ((_transport_speed && speed * _transport_speed < 0.0) || (_last_transport_speed * speed < 0.0) || (_last_transport_speed == 0.0f && speed < 0.0f)) {
998 todo = PostTransportWork (todo | PostTransportReverse);
1001 _last_transport_speed = _transport_speed;
1002 _transport_speed = speed;
1004 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
1005 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
1006 if ((*i)->realtime_set_speed ((*i)->speed(), true)) {
1007 todo = PostTransportWork (todo | PostTransportSpeed);
1013 add_post_transport_work (todo);
1014 _butler->schedule_transport_work ();
1020 /** Stop the transport. */
1022 Session::stop_transport (bool abort, bool clear_state)
1024 if (_transport_speed == 0.0f) {
1028 if (actively_recording() && !(transport_sub_state & StopPendingCapture) && _worst_output_latency > current_block_size) {
1030 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
1032 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
1033 (*i)->prepare_to_stop (_transport_frame);
1036 /* we need to capture the audio that has still not yet been received by the system
1037 at the time the stop is requested, so we have to roll past that time.
1039 we want to declick before stopping, so schedule the autostop for one
1040 block before the actual end. we'll declick in the subsequent block,
1041 and then we'll really be stopped.
1044 SessionEvent *ev = new SessionEvent (SessionEvent::StopOnce, SessionEvent::Replace,
1045 _transport_frame + _worst_output_latency - current_block_size,
1049 transport_sub_state |= StopPendingCapture;
1050 pending_abort = abort;
1055 if ((transport_sub_state & PendingDeclickOut) == 0) {
1057 if (!(transport_sub_state & StopPendingCapture)) {
1058 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
1060 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
1061 (*i)->prepare_to_stop (_transport_frame);
1065 transport_sub_state |= PendingDeclickOut;
1066 /* we'll be called again after the declick */
1067 pending_abort = abort;
1071 realtime_stop (abort, clear_state);
1072 _butler->schedule_transport_work ();
1076 Session::start_transport ()
1078 _last_roll_location = _transport_frame;
1079 have_looped = false;
1081 /* if record status is Enabled, move it to Recording. if its
1082 already Recording, move it to Disabled.
1085 switch (record_status()) {
1087 if (!config.get_punch_in()) {
1094 disable_record (false);
1102 transport_sub_state |= PendingDeclickIn;
1104 _transport_speed = 1.0;
1105 _target_transport_speed = 1.0;
1107 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
1108 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
1109 (*i)->realtime_set_speed ((*i)->speed(), true);
1112 deliver_mmc(MIDI::MachineControl::cmdDeferredPlay, _transport_frame);
1114 TransportStateChange (); /* EMIT SIGNAL */
1117 /** Do any transport work in the audio thread that needs to be done after the
1118 * transport thread is finished. Audio thread, realtime safe.
1121 Session::post_transport ()
1123 PostTransportWork ptw = post_transport_work ();
1125 if (ptw & PostTransportAudition) {
1126 if (auditioner && auditioner->auditioning()) {
1127 process_function = &Session::process_audition;
1129 process_function = &Session::process_with_events;
1133 if (ptw & PostTransportStop) {
1135 transport_sub_state = 0;
1138 if (ptw & PostTransportLocate) {
1140 if (((!config.get_external_sync() && (auto_play_legal && config.get_auto_play())) && !_exporting) || (ptw & PostTransportRoll)) {
1144 transport_sub_state = 0;
1149 /* XXX is this really safe? shouldn't we just be unsetting the bits that we actually
1152 set_post_transport_work (PostTransportWork (0));
1156 Session::reset_rf_scale (nframes_t motion)
1158 cumulative_rf_motion += motion;
1160 if (cumulative_rf_motion < 4 * _current_frame_rate) {
1162 } else if (cumulative_rf_motion < 8 * _current_frame_rate) {
1164 } else if (cumulative_rf_motion < 16 * _current_frame_rate) {
1176 Session::use_sync_source (Slave* new_slave)
1178 /* Runs in process() context */
1180 bool non_rt_required = false;
1182 /* XXX this deletion is problematic because we're in RT context */
1187 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
1188 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
1189 if (!(*i)->hidden()) {
1190 if ((*i)->realtime_set_speed ((*i)->speed(), true)) {
1191 non_rt_required = true;
1193 (*i)->set_slaved (_slave != 0);
1197 if (non_rt_required) {
1198 add_post_transport_work (PostTransportSpeed);
1199 _butler->schedule_transport_work ();
1206 Session::drop_sync_source ()
1208 request_sync_source (0);
1212 Session::switch_to_sync_source (SyncSource src)
1216 DEBUG_TRACE (DEBUG::Slave, string_compose ("Setting up sync source %1\n", enum_2_string (src)));
1220 if (_slave && dynamic_cast<MTC_Slave*>(_slave)) {
1226 new_slave = new MTC_Slave (*this, *_mtc_port);
1229 catch (failed_constructor& err) {
1234 error << _("No MTC port defined: MTC slaving is impossible.") << endmsg;
1240 if (_slave && dynamic_cast<MIDIClock_Slave*>(_slave)) {
1244 if (_midi_clock_port) {
1246 new_slave = new MIDIClock_Slave (*this, *_midi_clock_port, 24);
1249 catch (failed_constructor& err) {
1254 error << _("No MIDI Clock port defined: MIDI Clock slaving is impossible.") << endmsg;
1260 if (_slave && dynamic_cast<JACK_Slave*>(_slave)) {
1264 new_slave = new JACK_Slave (_engine.jack());
1272 request_sync_source (new_slave);
1276 Session::reverse_diskstream_buffers ()
1278 add_post_transport_work (PostTransportReverse);
1279 _butler->schedule_transport_work ();
1283 Session::set_diskstream_speed (Diskstream* stream, double speed)
1285 if (stream->realtime_set_speed (speed, false)) {
1286 add_post_transport_work (PostTransportSpeed);
1287 _butler->schedule_transport_work ();
1293 Session::unset_play_range ()
1295 _play_range = false;
1296 _clear_event_type (SessionEvent::RangeStop);
1297 _clear_event_type (SessionEvent::RangeLocate);
1301 Session::set_play_range (list<AudioRange>& range, bool leave_rolling)
1305 /* Called from event-processing context */
1307 unset_play_range ();
1309 if (range.empty()) {
1310 /* _play_range set to false in unset_play_range()
1312 if (!leave_rolling) {
1313 /* stop transport */
1314 SessionEvent* ev = new SessionEvent (SessionEvent::SetTransportSpeed, SessionEvent::Add, SessionEvent::Immediate, 0, 0.0f, false);
1322 /* cancel loop play */
1325 list<AudioRange>::size_type sz = range.size();
1329 list<AudioRange>::iterator i = range.begin();
1330 list<AudioRange>::iterator next;
1332 while (i != range.end()) {
1337 /* locating/stopping is subject to delays for declicking.
1340 nframes_t requested_frame = (*i).end;
1342 if (requested_frame > current_block_size) {
1343 requested_frame -= current_block_size;
1345 requested_frame = 0;
1348 if (next == range.end()) {
1349 ev = new SessionEvent (SessionEvent::RangeStop, SessionEvent::Add, requested_frame, 0, 0.0f);
1351 ev = new SessionEvent (SessionEvent::RangeLocate, SessionEvent::Add, requested_frame, (*next).start, 0.0f);
1359 } else if (sz == 1) {
1361 ev = new SessionEvent (SessionEvent::RangeStop, SessionEvent::Add, range.front().end, 0, 0.0f);
1366 /* save range so we can do auto-return etc. */
1368 current_audio_range = range;
1370 /* now start rolling at the right place */
1372 ev = new SessionEvent (SessionEvent::LocateRoll, SessionEvent::Add, SessionEvent::Immediate, range.front().start, 0.0f, false);
1375 TransportStateChange ();
1379 Session::request_bounded_roll (nframes_t start, nframes_t end)
1381 AudioRange ar (start, end, 0);
1382 list<AudioRange> lar;
1385 request_play_range (&lar, true);
1388 Session::request_roll_at_and_return (nframes_t start, nframes_t return_to)
1390 SessionEvent *ev = new SessionEvent (SessionEvent::LocateRollLocate, SessionEvent::Add, SessionEvent::Immediate, return_to, 1.0);
1391 ev->target2_frame = start;
1396 Session::engine_halted ()
1400 /* there will be no more calls to process(), so
1401 we'd better clean up for ourselves, right now.
1403 but first, make sure the butler is out of
1407 g_atomic_int_set (&_butler->should_do_transport_work, 0);
1408 set_post_transport_work (PostTransportWork (0));
1411 realtime_stop (false, true);
1412 non_realtime_stop (false, 0, ignored);
1413 transport_sub_state = 0;
1415 TransportStateChange (); /* EMIT SIGNAL */
1420 Session::xrun_recovery ()
1422 // can't cast away volatile so copy and emit that
1423 nframes64_t tframe = _transport_frame;
1424 Xrun (tframe); //EMIT SIGNAL
1426 if (Config->get_stop_recording_on_xrun() && actively_recording()) {
1428 /* it didn't actually halt, but we need
1429 to handle things in the same way.
1437 Session::route_processors_changed (RouteProcessorChange c)
1439 if (c.type == RouteProcessorChange::MeterPointChange) {
1443 update_latency_compensation (false, false);
1448 Session::update_latency_compensation (bool with_stop, bool abort)
1450 bool update_jack = false;
1451 PostTransportWork ptw;
1453 if (_state_of_the_state & Deletion) {
1457 _worst_track_latency = 0;
1458 ptw = post_transport_work();
1460 #undef DEBUG_LATENCY
1461 #ifdef DEBUG_LATENCY
1462 cerr << "\n---------------------------------\nUPDATE LATENCY\n";
1465 boost::shared_ptr<RouteList> r = routes.reader ();
1467 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
1470 (*i)->handle_transport_stopped (abort, (ptw & PostTransportLocate), (!(ptw & PostTransportLocate) || pending_locate_flush));
1473 nframes_t old_latency = (*i)->output()->signal_latency ();
1474 nframes_t track_latency = (*i)->update_total_latency ();
1476 if (old_latency != track_latency) {
1477 (*i)->input()->update_port_total_latencies ();
1478 (*i)->output()->update_port_total_latencies ();
1482 if (!(*i)->is_hidden() && ((*i)->active())) {
1483 _worst_track_latency = max (_worst_track_latency, track_latency);
1488 _engine.update_total_latencies ();
1491 #ifdef DEBUG_LATENCY
1492 cerr << "\tworst was " << _worst_track_latency << endl;
1495 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
1496 (*i)->set_latency_delay (_worst_track_latency);
1499 set_worst_io_latencies ();
1501 /* reflect any changes in latencies into capture offsets
1504 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
1506 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
1507 (*i)->set_capture_offset ();
1512 Session::allow_auto_play (bool yn)
1514 auto_play_legal = yn;
1518 Session::reset_jack_connection (jack_client_t* jack)
1522 if (_slave && ((js = dynamic_cast<JACK_Slave*> (_slave)) != 0)) {
1523 js->reset_client (jack);
1528 Session::maybe_stop (nframes_t limit)
1530 if ((_transport_speed > 0.0f && _transport_frame >= limit) || (_transport_speed < 0.0f && _transport_frame == 0)) {
1531 if (synced_to_jack () && config.get_jack_time_master ()) {
1532 _engine.transport_stop ();
1533 } else if (!synced_to_jack ()) {