2 * Copyright (C) 1999-2019 Paul Davis <paul@linuxaudiosystems.com>
3 * Copyright (C) 2006-2007 Jesse Chappell <jesse@essej.net>
4 * Copyright (C) 2006-2014 David Robillard <d@drobilla.net>
5 * Copyright (C) 2007-2012 Carl Hetherington <carl@carlh.net>
6 * Copyright (C) 2008-2009 Hans Baier <hansfbaier@googlemail.com>
7 * Copyright (C) 2012-2019 Robin Gareus <robin@gareus.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 #include <boost/algorithm/string/erase.hpp>
32 #include "pbd/error.h"
33 #include "pbd/enumwriter.h"
35 #include <glibmm/threads.h>
37 #include "ardour/audioengine.h"
38 #include "ardour/auditioner.h"
39 #include "ardour/butler.h"
40 #include "ardour/cycle_timer.h"
41 #include "ardour/debug.h"
42 #include "ardour/disk_reader.h"
43 #include "ardour/graph.h"
44 #include "ardour/port.h"
45 #include "ardour/process_thread.h"
46 #include "ardour/scene_changer.h"
47 #include "ardour/session.h"
48 #include "ardour/transport_fsm.h"
49 #include "ardour/transport_master.h"
50 #include "ardour/transport_master_manager.h"
51 #include "ardour/ticker.h"
52 #include "ardour/types.h"
53 #include "ardour/vca.h"
54 #include "ardour/vca_manager.h"
56 #include "midi++/mmc.h"
58 using namespace ARDOUR;
62 #define TFSM_EVENT(evtype) { _transport_fsm->enqueue (new TransportFSM::Event (evtype)); }
63 #define TFSM_STOP(abort,clear) { _transport_fsm->enqueue (new TransportFSM::Event (TransportFSM::StopTransport,abort,clear)); }
64 #define TFSM_LOCATE(target,roll,flush,loop,force) { _transport_fsm->enqueue (new TransportFSM::Event (TransportFSM::Locate,target,roll,flush,loop,force)); }
67 /** Called by the audio engine when there is work to be done with JACK.
68 * @param nframes Number of samples to process.
72 Session::process (pframes_t nframes)
74 samplepos_t transport_at_start = _transport_sample;
78 if (processing_blocked()) {
83 if (non_realtime_work_pending()) {
84 DEBUG_TRACE (DEBUG::Butler, string_compose ("non-realtime work pending: %1 (%2%3%4)\n", enum_2_string (post_transport_work()), std::hex, post_transport_work(), std::dec));
85 if (!_butler->transport_work_requested ()) {
86 DEBUG_TRACE (DEBUG::Butler, string_compose ("done, waiting? %1\n", _transport_fsm->waiting_for_butler()));
87 butler_completed_transport_work ();
89 DEBUG_TRACE (DEBUG::Butler, "doesn't seem to have finished yet (from view of RT thread)\n");
93 _engine.main_thread()->get_buffers ();
95 (this->*process_function) (nframes);
97 /* realtime-safe meter-position and processor-order changes
99 * ideally this would be done in
100 * Route::process_output_buffers() but various functions
101 * callig it hold a _processor_lock reader-lock
103 boost::shared_ptr<RouteList> r = routes.reader ();
104 bool one_or_more_routes_declicking = false;
105 for (RouteList::const_iterator i = r->begin(); i != r->end(); ++i) {
106 if ((*i)->apply_processor_changes_rt()) {
107 _rt_emit_pending = true;
109 if ((*i)->declick_in_progress()) {
110 one_or_more_routes_declicking = true;
114 if (_rt_emit_pending) {
115 if (!_rt_thread_active) {
116 emit_route_signals ();
118 if (pthread_mutex_trylock (&_rt_emit_mutex) == 0) {
119 pthread_cond_signal (&_rt_emit_cond);
120 pthread_mutex_unlock (&_rt_emit_mutex);
121 _rt_emit_pending = false;
125 /* We are checking two things here:
127 * 1) whether or not all tracks have finished a declick out.
128 * 2) is the transport FSM waiting to be told this
131 if (!one_or_more_routes_declicking && declick_in_progress()) {
132 /* end of the declick has been reached by all routes */
133 TFSM_EVENT (TransportFSM::DeclickDone);
136 _engine.main_thread()->drop_buffers ();
138 /* deliver MIDI clock. Note that we need to use the transport sample
139 * position at the start of process(), not the value at the end of
140 * it. We may already have ticked() because of a transport state
141 * change, for example.
145 if (!_silent && !_engine.freewheeling() && Config->get_send_midi_clock() && (transport_speed() == 1.0f || transport_speed() == 0.0f) && midi_clock->has_midi_port()) {
146 midi_clock->tick (transport_at_start, nframes);
149 _scene_changer->run (transport_at_start, transport_at_start + nframes);
152 /* don't bother with a message */
155 SendFeedback (); /* EMIT SIGNAL */
159 Session::fail_roll (pframes_t nframes)
161 return no_roll (nframes);
165 Session::no_roll (pframes_t nframes)
169 samplepos_t end_sample = _transport_sample + nframes; // FIXME: varispeed + no_roll ??
171 boost::shared_ptr<RouteList> r = routes.reader ();
174 _click_io->silence (nframes);
177 ltc_tx_send_time_code_for_cycle (_transport_sample, end_sample, _target_transport_speed, _transport_speed, nframes);
179 VCAList v = _vca_manager->vcas ();
180 for (VCAList::const_iterator i = v.begin(); i != v.end(); ++i) {
181 (*i)->automation_run (_transport_sample, nframes);
184 _global_locate_pending = locate_pending ();
186 if (_process_graph) {
187 DEBUG_TRACE(DEBUG::ProcessThreads,"calling graph/no-roll\n");
188 _process_graph->routes_no_roll( nframes, _transport_sample, end_sample, non_realtime_work_pending());
190 PT_TIMING_CHECK (10);
191 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
193 if ((*i)->is_auditioner()) {
197 if ((*i)->no_roll (nframes, _transport_sample, end_sample, non_realtime_work_pending())) {
198 error << string_compose(_("Session: error in no roll for %1"), (*i)->name()) << endmsg;
203 PT_TIMING_CHECK (11);
210 /** @param need_butler to be set to true by this method if it needs the butler,
211 * otherwise it must be left alone.
214 Session::process_routes (pframes_t nframes, bool& need_butler)
216 boost::shared_ptr<RouteList> r = routes.reader ();
218 const samplepos_t start_sample = _transport_sample;
219 const samplepos_t end_sample = _transport_sample + floor (nframes * _transport_speed);
221 VCAList v = _vca_manager->vcas ();
222 for (VCAList::const_iterator i = v.begin(); i != v.end(); ++i) {
223 (*i)->automation_run (start_sample, nframes);
226 _global_locate_pending = locate_pending();
228 if (_process_graph) {
229 DEBUG_TRACE(DEBUG::ProcessThreads,"calling graph/process-routes\n");
230 if (_process_graph->process_routes (nframes, start_sample, end_sample, need_butler) < 0) {
236 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
240 if ((*i)->is_auditioner()) {
246 if ((ret = (*i)->roll (nframes, start_sample, end_sample, b)) < 0) {
247 TFSM_STOP (false, false);
252 DEBUG_TRACE (DEBUG::Butler, string_compose ("%1 rolled and needs butler\n", (*i)->name()));
262 Session::get_track_statistics ()
267 boost::shared_ptr<RouteList> rl = routes.reader();
268 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
270 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
272 if (!tr || tr->is_private_route()) {
276 pworst = min (pworst, tr->playback_buffer_load());
277 cworst = min (cworst, tr->capture_buffer_load());
280 g_atomic_int_set (&_playback_load, (uint32_t) floor (pworst * 100.0f));
281 g_atomic_int_set (&_capture_load, (uint32_t) floor (cworst * 100.0f));
283 if (actively_recording()) {
289 Session::compute_audible_delta (samplepos_t& pos_and_delta) const
291 if (_transport_speed == 0.0 || _count_in_samples > 0 || _remaining_latency_preroll > 0) {
292 /* cannot compute audible delta, because the session is
293 generating silence that does not correspond to the timeline,
294 but is instead filling playback buffers to manage latency
297 DEBUG_TRACE (DEBUG::Slave, string_compose ("still adjusting for latency (%1) and/or count-in (%2) or stopped %1\n", _remaining_latency_preroll, _count_in_samples, _transport_speed));
301 pos_and_delta -= _transport_sample;
305 /** Process callback used when the auditioner is not active */
307 Session::process_with_events (pframes_t nframes)
312 pframes_t this_nframes;
313 samplepos_t end_sample;
314 bool session_needs_butler = false;
315 samplecnt_t samples_moved;
317 /* make sure the auditioner is silent */
320 auditioner->silence (nframes);
323 /* handle any pending events */
325 while (pending_events.read (&ev, 1) == 1) {
329 /* if we are not in the middle of a state change,
330 and there are immediate events queued up,
334 while (!non_realtime_work_pending() && !immediate_events.empty()) {
335 SessionEvent *ev = immediate_events.front ();
336 immediate_events.pop_front ();
339 /* only count-in when going to roll at speed 1.0 */
340 if (_transport_speed != 1.0 && _count_in_samples > 0) {
341 _count_in_samples = 0;
343 if (_transport_speed == 0.0) {
344 _remaining_latency_preroll = 0;
347 assert (_count_in_samples == 0 || _remaining_latency_preroll == 0 || _count_in_samples == _remaining_latency_preroll);
349 // DEBUG_TRACE (DEBUG::Transport, string_compose ("Running count in/latency preroll of %1 & %2\n", _count_in_samples, _remaining_latency_preroll));
351 while (_count_in_samples > 0 || _remaining_latency_preroll > 0) {
354 if (_remaining_latency_preroll > 0) {
355 ns = std::min ((samplecnt_t)nframes, _remaining_latency_preroll);
357 ns = std::min ((samplecnt_t)nframes, _count_in_samples);
360 boost::shared_ptr<RouteList> r = routes.reader ();
361 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
362 samplecnt_t route_offset = (*i)->playback_latency ();
363 if (_remaining_latency_preroll > route_offset + ns) {
364 /* route will no-roll for complete pre-roll cycle */
367 if (_remaining_latency_preroll > route_offset) {
368 /* route may need partial no-roll and partial roll from
369 * (_transport_sample - _remaining_latency_preroll) .. +ns.
370 * shorten and split the cycle.
372 ns = std::min (ns, (_remaining_latency_preroll - route_offset));
376 if (_count_in_samples > 0) {
377 run_click (_transport_sample - _count_in_samples, ns);
378 assert (_count_in_samples >= ns);
379 _count_in_samples -= ns;
382 if (_remaining_latency_preroll > 0) {
383 if (_count_in_samples == 0) {
384 click (_transport_sample - _remaining_latency_preroll, ns);
386 if (process_routes (ns, session_needs_butler)) {
393 if (_remaining_latency_preroll > 0) {
394 assert (_remaining_latency_preroll >= ns);
395 _remaining_latency_preroll -= ns;
400 /* process events.. */
401 if (!events.empty() && next_event != events.end()) {
402 SessionEvent* this_event = *next_event;
403 Events::iterator the_next_one = next_event;
406 while (this_event && this_event->action_sample == _transport_sample) {
407 process_event (this_event);
408 if (the_next_one == events.end()) {
411 this_event = *the_next_one;
421 _engine.split_cycle (ns);
425 /* Decide on what to do with quarter-frame MTC during this cycle */
427 bool const was_sending_qf_mtc = _send_qf_mtc;
428 double const tolerance = Config->get_mtc_qf_speed_tolerance() / 100.0;
430 if (_transport_speed != 0) {
432 Config->get_send_mtc () &&
433 _transport_speed >= (1 - tolerance) &&
434 _transport_speed <= (1 + tolerance)
437 if (_send_qf_mtc && !was_sending_qf_mtc) {
438 /* we will re-start quarter-frame MTC this cycle, so send a full update to set things up */
439 _send_timecode_update = true;
442 if (Config->get_send_mtc() && !_send_qf_mtc && _pframes_since_last_mtc > (sample_rate () / 4)) {
443 /* we're sending MTC, but we're not sending QF MTC at the moment, and it's been
444 a quarter of a second since we sent anything at all, so send a full MTC update
447 _send_timecode_update = true;
450 _pframes_since_last_mtc += nframes;
453 /* Events caused a transport change (or we re-started sending
454 * MTC), so send an MTC Full Frame (Timecode) message. This
455 * is sent whether rolling or not, to give slaves an idea of
456 * ardour time on locates (and allow slow slaves to position
457 * and prepare for rolling)
459 if (_send_timecode_update) {
460 send_full_time_code (_transport_sample, nframes);
463 if (!process_can_proceed()) {
468 if (events.empty() || next_event == events.end()) {
469 try_run_lua (nframes); // also during export ?? ->move to process_without_events()
470 /* lua scripts may inject events */
471 while (_n_lua_scripts > 0 && pending_events.read (&ev, 1) == 1) {
474 if (events.empty() || next_event == events.end()) {
475 process_without_events (nframes);
480 assert (_transport_speed == 0 || _transport_speed == 1.0 || _transport_speed == -1.0);
482 samples_moved = (samplecnt_t) nframes * _transport_speed;
483 // DEBUG_TRACE (DEBUG::Transport, string_compose ("plan to move transport by %1 (%2 @ %3)\n", samples_moved, nframes, _transport_speed));
485 end_sample = _transport_sample + samples_moved;
488 SessionEvent* this_event;
489 Events::iterator the_next_one;
491 if (!process_can_proceed()) {
496 if (!_exporting && config.get_external_sync()) {
497 if (!follow_transport_master (nframes)) {
498 ltc_tx_send_time_code_for_cycle (_transport_sample, end_sample, _target_transport_speed, _transport_speed, nframes);
503 if (_transport_speed == 0) {
508 if (!_exporting && !timecode_transmission_suspended()) {
509 send_midi_time_code_for_cycle (_transport_sample, end_sample, nframes);
512 ltc_tx_send_time_code_for_cycle (_transport_sample, end_sample, _target_transport_speed, _transport_speed, nframes);
514 samplepos_t stop_limit = compute_stop_limit ();
516 if (maybe_stop (stop_limit)) {
521 this_event = *next_event;
522 the_next_one = next_event;
525 /* yes folks, here it is, the actual loop where we really truly
531 this_nframes = nframes; /* real (jack) time relative */
532 samples_moved = (samplecnt_t) floor (_transport_speed * nframes); /* transport relative */
533 // DEBUG_TRACE (DEBUG::Transport, string_compose ("sub-loop plan to move transport by %1 (%2 @ %3)\n", samples_moved, nframes, _transport_speed));
535 /* running an event, position transport precisely to its time */
536 if (this_event && this_event->action_sample <= end_sample && this_event->action_sample >= _transport_sample) {
537 /* this isn't quite right for reverse play */
538 samples_moved = (samplecnt_t) (this_event->action_sample - _transport_sample);
539 // DEBUG_TRACE (DEBUG::Transport, string_compose ("sub-loop2 (for %4)plan to move transport by %1 (%2 @ %3)\n", samples_moved, nframes, _transport_speed, enum_2_string (this_event->type)));
540 this_nframes = abs (floor(samples_moved / _transport_speed));
543 try_run_lua (this_nframes);
547 click (_transport_sample, this_nframes);
549 if (process_routes (this_nframes, session_needs_butler)) {
554 get_track_statistics ();
556 nframes -= this_nframes;
558 if (samples_moved < 0) {
559 decrement_transport_position (-samples_moved);
560 } else if (samples_moved) {
561 increment_transport_position (samples_moved);
563 DEBUG_TRACE (DEBUG::Transport, "no transport motion\n");
566 maybe_stop (stop_limit);
570 _engine.split_cycle (this_nframes);
573 /* now handle this event and all others scheduled for the same time */
575 while (this_event && this_event->action_sample == _transport_sample) {
576 process_event (this_event);
578 if (the_next_one == events.end()) {
581 this_event = *the_next_one;
586 /* if an event left our state changing, do the right thing */
588 if (nframes && non_realtime_work_pending()) {
593 /* this is necessary to handle the case of seamless looping */
594 end_sample = _transport_sample + floor (nframes * _transport_speed);
599 } /* implicit release of route lock */
601 if (session_needs_butler) {
602 DEBUG_TRACE (DEBUG::Butler, "p-with-events: session needs butler, call it\n");
608 Session::transport_locked () const
610 if (!locate_pending() && (!config.get_external_sync() || (transport_master()->ok() && transport_master()->locked()))) {
618 Session::process_without_events (pframes_t nframes)
620 bool session_needs_butler = false;
621 samplecnt_t samples_moved;
623 if (!process_can_proceed()) {
628 if (!_exporting && config.get_external_sync()) {
629 if (!follow_transport_master (nframes)) {
630 ltc_tx_send_time_code_for_cycle (_transport_sample, _transport_sample, 0, 0 , nframes);
635 assert (_transport_speed == 0 || _transport_speed == 1.0 || _transport_speed == -1.0);
637 if (_transport_speed == 0) {
641 samples_moved = (samplecnt_t) nframes * _transport_speed;
644 if (!_exporting && !timecode_transmission_suspended()) {
645 send_midi_time_code_for_cycle (_transport_sample, _transport_sample + samples_moved, nframes);
648 ltc_tx_send_time_code_for_cycle (_transport_sample, _transport_sample + samples_moved, _target_transport_speed, _transport_speed, nframes);
650 samplepos_t const stop_limit = compute_stop_limit ();
652 if (maybe_stop (stop_limit)) {
657 if (maybe_sync_start (nframes)) {
661 click (_transport_sample, nframes);
663 if (process_routes (nframes, session_needs_butler)) {
668 get_track_statistics ();
670 if (samples_moved < 0) {
671 decrement_transport_position (-samples_moved);
672 //DEBUG_TRACE (DEBUG::Transport, string_compose ("DEcrement transport by %1 to %2\n", samples_moved, _transport_sample));
673 } else if (samples_moved) {
674 increment_transport_position (samples_moved);
675 //DEBUG_TRACE (DEBUG::Transport, string_compose ("INcrement transport by %1 to %2\n", samples_moved, _transport_sample));
677 DEBUG_TRACE (DEBUG::Transport, "no transport motion\n");
680 maybe_stop (stop_limit);
682 if (session_needs_butler) {
683 DEBUG_TRACE (DEBUG::Butler, "p-without-events: session needs butler, call it\n");
688 /** Process callback used when the auditioner is active.
689 * @param nframes number of samples to process.
692 Session::process_audition (pframes_t nframes)
695 boost::shared_ptr<RouteList> r = routes.reader ();
697 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
698 if (!(*i)->is_auditioner()) {
699 (*i)->silence (nframes);
703 /* run the auditioner, and if it says we need butler service, ask for it */
705 if (auditioner->play_audition (nframes) > 0) {
706 DEBUG_TRACE (DEBUG::Butler, "auditioner needs butler, call it\n");
710 /* if using a monitor section, run it because otherwise we don't hear anything */
712 if (_monitor_out && auditioner->needs_monitor()) {
713 _monitor_out->monitor_run (_transport_sample, _transport_sample + nframes, nframes);
716 /* handle pending events */
718 while (pending_events.read (&ev, 1) == 1) {
722 /* if we are not in the middle of a state change,
723 and there are immediate events queued up,
727 while (!non_realtime_work_pending() && !immediate_events.empty()) {
728 SessionEvent *ev = immediate_events.front ();
729 immediate_events.pop_front ();
733 if (!auditioner->auditioning()) {
734 /* auditioner no longer active, so go back to the normal process callback */
735 process_function = &Session::process_with_events;
740 Session::maybe_sync_start (pframes_t & nframes)
742 pframes_t sync_offset;
744 if (!waiting_for_sync_offset) {
748 if (_engine.get_sync_offset (sync_offset) && sync_offset < nframes) {
750 /* generate silence up to the sync point, then
751 adjust nframes + offset to reflect whatever
755 no_roll (sync_offset);
756 nframes -= sync_offset;
757 Port::increment_global_port_buffer_offset (sync_offset);
758 waiting_for_sync_offset = false;
761 return true; // done, nothing left to process
766 /* sync offset point is not within this process()
767 cycle, so just generate silence. and don't bother
768 with any fancy stuff here, just the minimal silence.
773 if (Config->get_locate_while_waiting_for_sync()) {
774 DEBUG_TRACE (DEBUG::Transport, "micro-locate while waiting for sync\n");
775 if (micro_locate (nframes)) {
776 /* XXX ERROR !!! XXX */
780 return true; // done, nothing left to process
787 Session::queue_event (SessionEvent* ev)
789 if (deletion_in_progress ()) {
791 } else if (loading ()) {
794 Glib::Threads::Mutex::Lock lm (rb_write_lock);
795 pending_events.write (&ev, 1);
800 Session::set_next_event ()
802 if (events.empty()) {
803 next_event = events.end();
807 if (next_event == events.end()) {
808 next_event = events.begin();
811 if ((*next_event)->action_sample > _transport_sample) {
812 next_event = events.begin();
815 for (; next_event != events.end(); ++next_event) {
816 if ((*next_event)->action_sample >= _transport_sample) {
823 Session::process_event (SessionEvent* ev)
828 /* if we're in the middle of a state change (i.e. waiting
829 for the butler thread to complete the non-realtime
830 part of the change), we'll just have to queue this
831 event for a time when the change is complete.
834 if (non_realtime_work_pending()) {
836 /* except locates, which we have the capability to handle */
838 if (ev->type != SessionEvent::Locate) {
839 immediate_events.insert (immediate_events.end(), ev);
845 DEBUG_TRACE (DEBUG::SessionEvents, string_compose ("Processing event: %1 @ %2\n", enum_2_string (ev->type), _transport_sample));
848 case SessionEvent::SetLoop:
849 set_play_loop (ev->yes_or_no, ev->speed);
852 case SessionEvent::AutoLoop:
854 /* roll after locate, do not flush, set "for loop end" true
856 TFSM_LOCATE (ev->target_sample, true, false, true, false);
862 case SessionEvent::Locate:
863 /* args: do not roll after locate, do flush, not with loop, force */
864 TFSM_LOCATE (ev->target_sample, false, true, false, ev->yes_or_no);
865 _send_timecode_update = true;
868 case SessionEvent::LocateRoll:
869 /* args: roll after locate, do flush, not with loop, force */
870 TFSM_LOCATE (ev->target_sample, true, true, false, ev->yes_or_no);
871 _send_timecode_update = true;
874 case SessionEvent::Skip:
875 if (Config->get_skip_playback()) {
876 TFSM_LOCATE (ev->target_sample, true, true, false, false);
877 _send_timecode_update = true;
883 case SessionEvent::LocateRollLocate:
884 // locate is handled by ::request_roll_at_and_return()
885 _requested_return_sample = ev->target_sample;
886 request_locate (ev->target2_sample, true);
890 case SessionEvent::SetTransportSpeed:
891 set_transport_speed (ev->speed, ev->target_sample, ev->yes_or_no, ev->second_yes_or_no, ev->third_yes_or_no);
894 case SessionEvent::SetTransportMaster:
895 TransportMasterManager::instance().set_current (ev->transport_master);
898 case SessionEvent::PunchIn:
899 // cerr << "PunchIN at " << transport_sample() << endl;
900 if (config.get_punch_in() && record_status() == Enabled) {
907 case SessionEvent::PunchOut:
908 // cerr << "PunchOUT at " << transport_sample() << endl;
909 if (config.get_punch_out()) {
910 step_back_from_record ();
916 case SessionEvent::RangeStop:
917 TFSM_STOP (ev->yes_or_no, false);
922 case SessionEvent::RangeLocate:
923 /* args: roll after locate, do flush, not with loop */
924 TFSM_LOCATE (ev->target_sample, true, true, false, false);
929 case SessionEvent::Overwrite:
930 overwrite_some_buffers (ev->track);
933 case SessionEvent::Audition:
934 set_audition (ev->region);
935 // drop reference to region
939 case SessionEvent::SetPlayAudioRange:
940 set_play_range (ev->audio_range, (ev->speed == 1.0f));
943 case SessionEvent::CancelPlayAudioRange:
947 case SessionEvent::RealTimeOperation:
949 del = false; // other side of RT request needs to clean up
952 case SessionEvent::AdjustPlaybackBuffering:
953 schedule_playback_buffering_adjustment ();
956 case SessionEvent::AdjustCaptureBuffering:
957 schedule_capture_buffering_adjustment ();
960 case SessionEvent::SetTimecodeTransmission:
961 g_atomic_int_set (&_suspend_timecode_transmission, ev->yes_or_no ? 0 : 1);
965 fatal << string_compose(_("Programming error: illegal event type in process_event (%1)"), ev->type) << endmsg;
966 abort(); /*NOTREACHED*/
971 del = del && !_remove_event (ev);
980 Session::compute_stop_limit () const
982 if (!Config->get_stop_at_session_end ()) {
983 return max_samplepos;
986 if (config.get_external_sync()) {
987 return max_samplepos;
990 bool const punching_in = (config.get_punch_in () && _locations->auto_punch_location());
991 bool const punching_out = (config.get_punch_out () && _locations->auto_punch_location());
993 if (actively_recording ()) {
994 /* permanently recording */
995 return max_samplepos;
996 } else if (punching_in && !punching_out) {
997 /* punching in but never out */
998 return max_samplepos;
999 } else if (punching_in && punching_out && _locations->auto_punch_location()->end() > current_end_sample()) {
1000 /* punching in and punching out after session end */
1001 return max_samplepos;
1004 return current_end_sample ();
1009 /* dedicated thread for signal emission.
1011 * while sending cross-thread signals from the process thread
1012 * is fine in general, PBD::Signal's use of boost::function and
1013 * boost:bind can produce a vast overhead which is not
1014 * acceptable for low latency.
1016 * This works around the issue by moving the boost overhead
1017 * out of the RT thread. The overall load is probably higher but
1018 * the realtime thread remains unaffected.
1022 Session::emit_route_signals ()
1024 // TODO use RAII to allow using these signals in other places
1025 BatchUpdateStart(); /* EMIT SIGNAL */
1026 boost::shared_ptr<RouteList> r = routes.reader ();
1027 for (RouteList::const_iterator ci = r->begin(); ci != r->end(); ++ci) {
1028 (*ci)->emit_pending_signals ();
1030 BatchUpdateEnd(); /* EMIT SIGNAL */
1034 Session::emit_thread_start ()
1036 if (_rt_thread_active) {
1039 _rt_thread_active = true;
1041 if (pthread_create (&_rt_emit_thread, NULL, emit_thread, this)) {
1042 _rt_thread_active = false;
1047 Session::emit_thread_terminate ()
1049 if (!_rt_thread_active) {
1052 _rt_thread_active = false;
1054 if (pthread_mutex_lock (&_rt_emit_mutex) == 0) {
1055 pthread_cond_signal (&_rt_emit_cond);
1056 pthread_mutex_unlock (&_rt_emit_mutex);
1060 pthread_join (_rt_emit_thread, &status);
1064 Session::emit_thread (void *arg)
1066 Session *s = static_cast<Session *>(arg);
1067 s->emit_thread_run ();
1073 Session::emit_thread_run ()
1075 pthread_mutex_lock (&_rt_emit_mutex);
1076 while (_rt_thread_active) {
1077 emit_route_signals();
1078 pthread_cond_wait (&_rt_emit_cond, &_rt_emit_mutex);
1080 pthread_mutex_unlock (&_rt_emit_mutex);
1084 Session::follow_transport_master (pframes_t nframes)
1086 TransportMasterManager& tmm (TransportMasterManager::instance());
1089 samplepos_t slave_transport_sample;
1090 sampleoffset_t delta;
1092 if (tmm.master_invalid_this_cycle()) {
1093 DEBUG_TRACE (DEBUG::Slave, "session told not to use the transport master this cycle\n");
1097 slave_speed = tmm.get_current_speed_in_process_context();
1098 slave_transport_sample = tmm.get_current_position_in_process_context ();
1099 delta = _transport_sample - slave_transport_sample;
1101 DEBUG_TRACE (DEBUG::Slave, string_compose ("session at %1, master at %2, delta: %3 res: %4\n", _transport_sample, slave_transport_sample, delta, tmm.current()->resolution()));
1103 /* This is a heuristic rather than a strictly provable rule. The idea
1104 * is that if we're "far away" from the master, we should locate to its
1105 * current position, and then varispeed to sync with it.
1107 * On the other hand, if we're close to it, just varispeed.
1110 if (!actively_recording() && abs (delta) > (5 * current_block_size)) {
1111 DiskReader::inc_no_disk_output ();
1112 if (!_transport_fsm->locating()) {
1113 DEBUG_TRACE (DEBUG::Slave, string_compose ("request locate to master position %1\n", slave_transport_sample));
1114 TFSM_LOCATE (slave_transport_sample, true, true, false, false);
1119 if (slave_speed != 0.0) {
1120 if (_transport_speed == 0.0f) {
1121 DEBUG_TRACE (DEBUG::Slave, string_compose ("slave starts transport: %1 sample %2 tf %3\n", slave_speed, slave_transport_sample, _transport_sample));
1122 TFSM_EVENT (TransportFSM::StartTransport);
1125 if (_transport_speed != 0.0f) {
1126 DEBUG_TRACE (DEBUG::Slave, string_compose ("slave stops transport: %1 sample %2 tf %3\n", slave_speed, slave_transport_sample, _transport_sample));
1127 TFSM_STOP (false, false);
1131 /* This is the second part of the "we're not synced yet" code. If we're
1132 * close, but not within the resolution of the master, silence disk
1133 * output but continue to varispeed to get in sync.
1136 if (!actively_recording() && abs (delta) > tmm.current()->resolution()) {
1137 /* just varispeed to chase the master, and be silent till we're synced */
1138 DiskReader::inc_no_disk_output ();
1142 /* speed is set, we're locked, and good to go */
1143 DiskReader::dec_no_disk_output ();
1147 /* don't move at all */
1148 DEBUG_TRACE (DEBUG::Slave, "no roll\n")
1154 Session::reset_slave_state ()
1156 transport_master_tracking_state = Stopped;
1157 DiskReader::dec_no_disk_output ();