2 Copyright (C) 1999-2002 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.
25 #include "pbd/error.h"
26 #include "pbd/enumwriter.h"
28 #include <glibmm/threads.h>
30 #include "ardour/audioengine.h"
31 #include "ardour/auditioner.h"
32 #include "ardour/butler.h"
33 #include "ardour/cycle_timer.h"
34 #include "ardour/debug.h"
35 #include "ardour/disk_reader.h"
36 #include "ardour/graph.h"
37 #include "ardour/port.h"
38 #include "ardour/process_thread.h"
39 #include "ardour/scene_changer.h"
40 #include "ardour/session.h"
41 #include "ardour/slave.h"
42 #include "ardour/ticker.h"
43 #include "ardour/types.h"
44 #include "ardour/vca.h"
45 #include "ardour/vca_manager.h"
47 #include "midi++/mmc.h"
51 using namespace ARDOUR;
55 /** Called by the audio engine when there is work to be done with JACK.
56 * @param nframes Number of samples to process.
60 Session::process (pframes_t nframes)
62 samplepos_t transport_at_start = _transport_sample;
66 if (processing_blocked()) {
71 if (non_realtime_work_pending()) {
72 if (!_butler->transport_work_requested ()) {
77 _engine.main_thread()->get_buffers ();
79 (this->*process_function) (nframes);
81 /* realtime-safe meter-position and processor-order changes
83 * ideally this would be done in
84 * Route::process_output_buffers() but various functions
85 * callig it hold a _processor_lock reader-lock
87 boost::shared_ptr<RouteList> r = routes.reader ();
88 for (RouteList::const_iterator i = r->begin(); i != r->end(); ++i) {
89 if ((*i)->apply_processor_changes_rt()) {
90 _rt_emit_pending = true;
93 if (_rt_emit_pending) {
94 if (!_rt_thread_active) {
95 emit_route_signals ();
97 if (pthread_mutex_trylock (&_rt_emit_mutex) == 0) {
98 pthread_cond_signal (&_rt_emit_cond);
99 pthread_mutex_unlock (&_rt_emit_mutex);
100 _rt_emit_pending = false;
104 _engine.main_thread()->drop_buffers ();
106 /* deliver MIDI clock. Note that we need to use the transport sample
107 * position at the start of process(), not the value at the end of
108 * it. We may already have ticked() because of a transport state
109 * change, for example.
113 if (!_silent && !_engine.freewheeling() && Config->get_send_midi_clock() && (transport_speed() == 1.0f || transport_speed() == 0.0f) && midi_clock->has_midi_port()) {
114 midi_clock->tick (transport_at_start, nframes);
117 _scene_changer->run (transport_at_start, transport_at_start + nframes);
120 /* don't bother with a message */
123 SendFeedback (); /* EMIT SIGNAL */
127 Session::fail_roll (pframes_t nframes)
129 return no_roll (nframes);
133 Session::no_roll (pframes_t nframes)
137 samplepos_t end_sample = _transport_sample + nframes; // FIXME: varispeed + no_roll ??
139 int declick = (config.get_use_transport_fades() ? get_transport_declick_required() : false);
140 boost::shared_ptr<RouteList> r = routes.reader ();
143 _click_io->silence (nframes);
146 ltc_tx_send_time_code_for_cycle (_transport_sample, end_sample, _target_transport_speed, _transport_speed, nframes);
148 VCAList v = _vca_manager->vcas ();
149 for (VCAList::const_iterator i = v.begin(); i != v.end(); ++i) {
150 (*i)->automation_run (_transport_sample, nframes);
153 if (_process_graph) {
154 DEBUG_TRACE(DEBUG::ProcessThreads,"calling graph/no-roll\n");
155 _process_graph->routes_no_roll( nframes, _transport_sample, end_sample, non_realtime_work_pending(), declick);
157 PT_TIMING_CHECK (10);
158 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
160 if ((*i)->is_auditioner()) {
164 (*i)->set_pending_declick (declick);
166 if ((*i)->no_roll (nframes, _transport_sample, end_sample, non_realtime_work_pending())) {
167 error << string_compose(_("Session: error in no roll for %1"), (*i)->name()) << endmsg;
172 PT_TIMING_CHECK (11);
179 /** @param need_butler to be set to true by this method if it needs the butler,
180 * otherwise it must be left alone.
183 Session::process_routes (pframes_t nframes, bool& need_butler)
185 int declick = (config.get_use_transport_fades() ? get_transport_declick_required() : false);
186 boost::shared_ptr<RouteList> r = routes.reader ();
188 const samplepos_t start_sample = _transport_sample;
189 const samplepos_t end_sample = _transport_sample + floor (nframes * _transport_speed);
191 VCAList v = _vca_manager->vcas ();
192 for (VCAList::const_iterator i = v.begin(); i != v.end(); ++i) {
193 (*i)->automation_run (start_sample, nframes);
196 _global_locate_pending = locate_pending ();
198 if (_process_graph) {
199 DEBUG_TRACE(DEBUG::ProcessThreads,"calling graph/process-routes\n");
200 if (_process_graph->process_routes (nframes, start_sample, end_sample, declick, need_butler) < 0) {
206 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
210 if ((*i)->is_auditioner()) {
214 (*i)->set_pending_declick (declick);
218 if ((ret = (*i)->roll (nframes, start_sample, end_sample, declick, b)) < 0) {
224 DEBUG_TRACE (DEBUG::Butler, string_compose ("%1 rolled and needs butler\n", (*i)->name()));
234 Session::get_track_statistics ()
239 boost::shared_ptr<RouteList> rl = routes.reader();
240 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
242 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
244 if (!tr || tr->is_private_route()) {
248 pworst = min (pworst, tr->playback_buffer_load());
249 cworst = min (cworst, tr->capture_buffer_load());
252 g_atomic_int_set (&_playback_load, (uint32_t) floor (pworst * 100.0f));
253 g_atomic_int_set (&_capture_load, (uint32_t) floor (cworst * 100.0f));
255 if (actively_recording()) {
260 /** Process callback used when the auditioner is not active */
262 Session::process_with_events (pframes_t nframes)
267 pframes_t this_nframes;
268 samplepos_t end_sample;
269 bool session_needs_butler = false;
270 samplecnt_t samples_moved;
272 /* make sure the auditioner is silent */
275 auditioner->silence (nframes);
278 /* handle any pending events */
280 while (pending_events.read (&ev, 1) == 1) {
284 /* if we are not in the middle of a state change,
285 and there are immediate events queued up,
289 while (!non_realtime_work_pending() && !immediate_events.empty()) {
290 SessionEvent *ev = immediate_events.front ();
291 immediate_events.pop_front ();
295 /* only count-in when going to roll at speed 1.0 */
296 if (_transport_speed != 1.0 && _count_in_samples > 0) {
297 _count_in_samples = 0;
299 if (_transport_speed == 0.0) {
300 _remaining_latency_preroll = 0;
303 assert (_count_in_samples == 0 || _remaining_latency_preroll == 0 || _count_in_samples == _remaining_latency_preroll);
305 if (_count_in_samples > 0 || _remaining_latency_preroll > 0) {
308 if (_remaining_latency_preroll > 0) {
309 ns = std::min ((samplecnt_t)nframes, _remaining_latency_preroll);
311 ns = std::min ((samplecnt_t)nframes, _count_in_samples);
314 if (_count_in_samples > 0) {
315 run_click (_transport_sample - _count_in_samples, ns);
316 assert (_count_in_samples >= ns);
317 _count_in_samples -= ns;
320 if (_remaining_latency_preroll > 0) {
321 if (_count_in_samples == 0) {
322 click (_transport_sample - _remaining_latency_preroll, ns);
324 if (process_routes (ns, session_needs_butler)) {
331 if (_remaining_latency_preroll > 0) {
332 assert (_remaining_latency_preroll >= ns);
333 _remaining_latency_preroll -= ns;
338 /* process events.. */
339 if (!events.empty() && next_event != events.end()) {
340 SessionEvent* this_event = *next_event;
341 Events::iterator the_next_one = next_event;
344 while (this_event && this_event->action_sample == _transport_sample) {
345 process_event (this_event);
346 if (the_next_one == events.end()) {
349 this_event = *the_next_one;
356 check_declick_out ();
361 _engine.split_cycle (ns);
365 /* Decide on what to do with quarter-frame MTC during this cycle */
367 bool const was_sending_qf_mtc = _send_qf_mtc;
368 double const tolerance = Config->get_mtc_qf_speed_tolerance() / 100.0;
370 if (_transport_speed != 0) {
372 Config->get_send_mtc () &&
373 _transport_speed >= (1 - tolerance) &&
374 _transport_speed <= (1 + tolerance)
377 if (_send_qf_mtc && !was_sending_qf_mtc) {
378 /* we will re-start quarter-frame MTC this cycle, so send a full update to set things up */
379 _send_timecode_update = true;
382 if (Config->get_send_mtc() && !_send_qf_mtc && _pframes_since_last_mtc > (sample_rate () / 4)) {
383 /* we're sending MTC, but we're not sending QF MTC at the moment, and it's been
384 a quarter of a second since we sent anything at all, so send a full MTC update
387 _send_timecode_update = true;
390 _pframes_since_last_mtc += nframes;
393 /* Events caused a transport change (or we re-started sending
394 * MTC), so send an MTC Full Frame (Timecode) message. This
395 * is sent whether rolling or not, to give slaves an idea of
396 * ardour time on locates (and allow slow slaves to position
397 * and prepare for rolling)
399 if (_send_timecode_update) {
400 send_full_time_code (_transport_sample, nframes);
403 if (!process_can_proceed()) {
408 if (events.empty() || next_event == events.end()) {
409 try_run_lua (nframes); // also during export ?? ->move to process_without_events()
410 /* lua scripts may inject events */
411 while (_n_lua_scripts > 0 && pending_events.read (&ev, 1) == 1) {
414 if (events.empty() || next_event == events.end()) {
415 process_without_events (nframes);
420 if (_transport_speed == 1.0) {
421 samples_moved = (samplecnt_t) nframes;
423 interpolation.set_target_speed (_target_transport_speed);
424 interpolation.set_speed (_transport_speed);
425 samples_moved = (samplecnt_t) interpolation.interpolate (0, nframes, 0, 0);
428 end_sample = _transport_sample + samples_moved;
431 SessionEvent* this_event;
432 Events::iterator the_next_one;
434 if (!process_can_proceed()) {
439 if (!_exporting && _slave) {
440 if (!follow_slave (nframes)) {
445 if (_transport_speed == 0) {
450 if (!_exporting && !timecode_transmission_suspended()) {
451 send_midi_time_code_for_cycle (_transport_sample, end_sample, nframes);
454 ltc_tx_send_time_code_for_cycle (_transport_sample, end_sample, _target_transport_speed, _transport_speed, nframes);
456 samplepos_t stop_limit = compute_stop_limit ();
458 if (maybe_stop (stop_limit)) {
463 this_event = *next_event;
464 the_next_one = next_event;
467 /* yes folks, here it is, the actual loop where we really truly
473 this_nframes = nframes; /* real (jack) time relative */
474 samples_moved = (samplecnt_t) floor (_transport_speed * nframes); /* transport relative */
476 /* running an event, position transport precisely to its time */
477 if (this_event && this_event->action_sample <= end_sample && this_event->action_sample >= _transport_sample) {
478 /* this isn't quite right for reverse play */
479 samples_moved = (samplecnt_t) (this_event->action_sample - _transport_sample);
480 this_nframes = abs (floor(samples_moved / _transport_speed));
483 try_run_lua (this_nframes);
487 click (_transport_sample, this_nframes);
489 if (process_routes (this_nframes, session_needs_butler)) {
494 get_track_statistics ();
496 nframes -= this_nframes;
498 if (samples_moved < 0) {
499 decrement_transport_position (-samples_moved);
500 } else if (samples_moved) {
501 increment_transport_position (samples_moved);
504 maybe_stop (stop_limit);
505 check_declick_out ();
509 _engine.split_cycle (this_nframes);
512 /* now handle this event and all others scheduled for the same time */
514 while (this_event && this_event->action_sample == _transport_sample) {
515 process_event (this_event);
517 if (the_next_one == events.end()) {
520 this_event = *the_next_one;
525 /* if an event left our state changing, do the right thing */
527 if (nframes && non_realtime_work_pending()) {
532 /* this is necessary to handle the case of seamless looping */
533 end_sample = _transport_sample + floor (nframes * _transport_speed);
538 } /* implicit release of route lock */
540 if (session_needs_butler) {
541 DEBUG_TRACE (DEBUG::Butler, "p-with-events: session needs butler, call it\n");
547 Session::reset_slave_state ()
549 average_slave_delta = 1800;
550 delta_accumulator_cnt = 0;
551 have_first_delta_accumulator = false;
552 _slave_state = Stopped;
553 DiskReader::set_no_disk_output (false);
557 Session::transport_locked () const
561 if (!locate_pending() && (!config.get_external_sync() || (sl && sl->ok() && sl->locked()))) {
569 Session::follow_slave (pframes_t nframes)
572 samplepos_t slave_transport_sample;
573 samplecnt_t this_delta;
578 config.set_external_sync (false);
582 _slave->speed_and_position (slave_speed, slave_transport_sample);
584 DEBUG_TRACE (DEBUG::Slave, string_compose ("Slave position %1 speed %2\n", slave_transport_sample, slave_speed));
586 if (!_slave->locked()) {
587 DEBUG_TRACE (DEBUG::Slave, "slave not locked\n");
591 if (slave_transport_sample > _transport_sample) {
592 this_delta = slave_transport_sample - _transport_sample;
595 this_delta = _transport_sample - slave_transport_sample;
599 if (_slave->starting()) {
603 if (_slave->is_always_synced() ||
604 (Config->get_timecode_source_is_synced() && (dynamic_cast<TimecodeSlave*>(_slave)) != 0)
607 /* if the TC source is synced, then we assume that its
608 speed is binary: 0.0 or 1.0
611 if (slave_speed != 0.0f) {
617 /* if we are chasing and the average delta between us and the
618 master gets too big, we want to switch to silent
619 motion. so keep track of that here.
622 if (_slave_state == Running) {
623 calculate_moving_average_of_slave_delta(dir, abs(this_delta));
627 track_slave_state (slave_speed, slave_transport_sample, this_delta);
629 DEBUG_TRACE (DEBUG::Slave, string_compose ("slave state %1 @ %2 speed %3 cur delta %4 avg delta %5\n",
630 _slave_state, slave_transport_sample, slave_speed, this_delta, average_slave_delta));
633 if (_slave_state == Running && !_slave->is_always_synced() && !(Config->get_timecode_source_is_synced() && (dynamic_cast<TimecodeSlave*>(_slave)) != 0)) {
635 /* may need to varispeed to sync with slave */
637 if (_transport_speed != 0.0f) {
640 note that average_dir is +1 or -1
645 if (average_slave_delta == 0) {
649 delta = average_slave_delta;
650 delta *= average_dir;
654 if (slave_speed != 0.0) {
655 DEBUG_TRACE (DEBUG::Slave, string_compose ("delta = %1 speed = %2 ts = %3 M@%4 S@%5 avgdelta %6\n",
656 (int) (dir * this_delta),
660 slave_transport_sample,
661 average_slave_delta));
665 if (_slave->give_slave_full_control_over_transport_speed()) {
666 set_transport_speed (slave_speed, 0, false, false);
667 //std::cout << "set speed = " << slave_speed << "\n";
669 float adjusted_speed = slave_speed + (1.5 * (delta / float(_current_sample_rate)));
670 request_transport_speed (adjusted_speed);
671 DEBUG_TRACE (DEBUG::Slave, string_compose ("adjust using %1 towards %2 ratio %3 current %4 slave @ %5\n",
672 delta, adjusted_speed, adjusted_speed/slave_speed, _transport_speed,
676 if (!actively_recording() && (samplecnt_t) average_slave_delta > _slave->resolution()) {
677 DEBUG_TRACE (DEBUG::Slave, string_compose ("average slave delta %1 greater than slave resolution %2 => no disk output\n", average_slave_delta, _slave->resolution()));
678 /* run routes as normal, but no disk output */
679 DiskReader::set_no_disk_output (true);
683 if (!have_first_delta_accumulator) {
684 DEBUG_TRACE (DEBUG::Slave, "waiting for first slave delta accumulator to be ready, no disk output\n");
685 /* run routes as normal, but no disk output */
686 DiskReader::set_no_disk_output (true);
693 if (!have_first_delta_accumulator) {
694 DEBUG_TRACE (DEBUG::Slave, "still waiting to compute slave delta, no disk output\n");
695 DiskReader::set_no_disk_output (true);
697 DiskReader::set_no_disk_output (false);
700 if ((_slave_state == Running) && (0 == (post_transport_work () & ~PostTransportSpeed))) {
701 /* speed is set, we're locked, and good to go */
706 /* don't move at all */
707 DEBUG_TRACE (DEBUG::Slave, "no roll\n")
713 Session::calculate_moving_average_of_slave_delta (int dir, samplecnt_t this_delta)
715 if (delta_accumulator_cnt >= delta_accumulator_size) {
716 have_first_delta_accumulator = true;
717 delta_accumulator_cnt = 0;
720 if (delta_accumulator_cnt != 0 || this_delta < _current_sample_rate) {
721 delta_accumulator[delta_accumulator_cnt++] = (samplecnt_t) dir * (samplecnt_t) this_delta;
724 if (have_first_delta_accumulator) {
725 average_slave_delta = 0L;
726 for (int i = 0; i < delta_accumulator_size; ++i) {
727 average_slave_delta += delta_accumulator[i];
729 average_slave_delta /= (int32_t) delta_accumulator_size;
730 if (average_slave_delta < 0L) {
732 average_slave_delta = average_slave_delta;
740 Session::track_slave_state (float slave_speed, samplepos_t slave_transport_sample, samplecnt_t /*this_delta*/)
742 if (slave_speed != 0.0f) {
744 /* slave is running */
746 switch (_slave_state) {
748 if (_slave->requires_seekahead()) {
749 slave_wait_end = slave_transport_sample + _slave->seekahead_distance ();
750 DEBUG_TRACE (DEBUG::Slave, string_compose ("slave stopped, but running, requires seekahead to %1\n", slave_wait_end));
751 /* we can call locate() here because we are in process context */
752 locate (slave_wait_end, false, false);
753 _slave_state = Waiting;
757 DEBUG_TRACE (DEBUG::Slave, string_compose ("slave stopped -> running at %1\n", slave_transport_sample));
759 memset (delta_accumulator, 0, sizeof (int32_t) * delta_accumulator_size);
760 average_slave_delta = 0L;
762 Location* al = _locations->auto_loop_location();
764 if (al && play_loop && (slave_transport_sample < al->start() || slave_transport_sample > al->end())) {
766 request_play_loop(false);
769 if (slave_transport_sample != _transport_sample) {
770 DEBUG_TRACE (DEBUG::Slave, string_compose ("require locate to run. eng: %1 -> sl: %2\n", _transport_sample, slave_transport_sample));
771 locate (slave_transport_sample, false, false);
773 _slave_state = Running;
782 if (_slave_state == Waiting) {
784 DEBUG_TRACE (DEBUG::Slave, string_compose ("slave waiting at %1\n", slave_transport_sample));
786 if (slave_transport_sample >= slave_wait_end) {
788 DEBUG_TRACE (DEBUG::Slave, string_compose ("slave start at %1 vs %2\n", slave_transport_sample, _transport_sample));
790 _slave_state = Running;
792 /* now perform a "micro-seek" within the disk buffers to realign ourselves
793 precisely with the master.
798 samplecnt_t sample_delta = slave_transport_sample - _transport_sample;
800 boost::shared_ptr<RouteList> rl = routes.reader();
801 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
802 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
803 if (tr && !tr->can_internal_playback_seek (sample_delta)) {
810 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
811 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
813 tr->internal_playback_seek (sample_delta);
816 _transport_sample += sample_delta;
819 cerr << "cannot micro-seek\n";
825 if (_slave_state == Running && _transport_speed == 0.0f) {
826 DEBUG_TRACE (DEBUG::Slave, "slave starts transport\n");
830 } else { // slave_speed is 0
832 /* slave has stopped */
834 if (_transport_speed != 0.0f) {
835 DEBUG_TRACE (DEBUG::Slave, string_compose ("slave stops transport: %1 sample %2 tf %3\n", slave_speed, slave_transport_sample, _transport_sample));
839 if (slave_transport_sample != _transport_sample) {
840 DEBUG_TRACE (DEBUG::Slave, string_compose ("slave stopped, move to %1\n", slave_transport_sample));
841 force_locate (slave_transport_sample, false);
849 Session::process_without_events (pframes_t nframes)
851 bool session_needs_butler = false;
852 samplecnt_t samples_moved;
854 if (!process_can_proceed()) {
859 if (!_exporting && _slave) {
860 if (!follow_slave (nframes)) {
861 ltc_tx_send_time_code_for_cycle (_transport_sample, _transport_sample, 0, 0 , nframes);
866 if (_transport_speed == 0) {
871 if (_transport_speed == 1.0) {
872 samples_moved = (samplecnt_t) nframes;
874 interpolation.set_target_speed (_target_transport_speed);
875 interpolation.set_speed (_transport_speed);
876 samples_moved = (samplecnt_t) interpolation.interpolate (0, nframes, 0, 0);
879 if (!_exporting && !timecode_transmission_suspended()) {
880 send_midi_time_code_for_cycle (_transport_sample, _transport_sample + samples_moved, nframes);
883 ltc_tx_send_time_code_for_cycle (_transport_sample, _transport_sample + samples_moved, _target_transport_speed, _transport_speed, nframes);
885 samplepos_t const stop_limit = compute_stop_limit ();
887 if (maybe_stop (stop_limit)) {
892 if (maybe_sync_start (nframes)) {
896 click (_transport_sample, nframes);
898 if (process_routes (nframes, session_needs_butler)) {
903 get_track_statistics ();
905 if (samples_moved < 0) {
906 decrement_transport_position (-samples_moved);
907 } else if (samples_moved) {
908 increment_transport_position (samples_moved);
911 maybe_stop (stop_limit);
912 check_declick_out ();
914 if (session_needs_butler) {
915 DEBUG_TRACE (DEBUG::Butler, "p-without-events: session needs butler, call it\n");
920 /** Process callback used when the auditioner is active.
921 * @param nframes number of samples to process.
924 Session::process_audition (pframes_t nframes)
927 boost::shared_ptr<RouteList> r = routes.reader ();
929 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
930 if (!(*i)->is_auditioner()) {
931 (*i)->silence (nframes);
935 /* run the auditioner, and if it says we need butler service, ask for it */
937 if (auditioner->play_audition (nframes) > 0) {
938 DEBUG_TRACE (DEBUG::Butler, "auditioner needs butler, call it\n");
942 /* if using a monitor section, run it because otherwise we don't hear anything */
944 if (_monitor_out && auditioner->needs_monitor()) {
945 _monitor_out->monitor_run (_transport_sample, _transport_sample + nframes, nframes, false);
948 /* handle pending events */
950 while (pending_events.read (&ev, 1) == 1) {
954 /* if we are not in the middle of a state change,
955 and there are immediate events queued up,
959 while (!non_realtime_work_pending() && !immediate_events.empty()) {
960 SessionEvent *ev = immediate_events.front ();
961 immediate_events.pop_front ();
965 if (!auditioner->auditioning()) {
966 /* auditioner no longer active, so go back to the normal process callback */
967 process_function = &Session::process_with_events;
972 Session::maybe_sync_start (pframes_t & nframes)
974 pframes_t sync_offset;
976 if (!waiting_for_sync_offset) {
980 if (_engine.get_sync_offset (sync_offset) && sync_offset < nframes) {
982 /* generate silence up to the sync point, then
983 adjust nframes + offset to reflect whatever
987 no_roll (sync_offset);
988 nframes -= sync_offset;
989 Port::increment_global_port_buffer_offset (sync_offset);
990 waiting_for_sync_offset = false;
993 return true; // done, nothing left to process
998 /* sync offset point is not within this process()
999 cycle, so just generate silence. and don't bother
1000 with any fancy stuff here, just the minimal silence.
1005 if (Config->get_locate_while_waiting_for_sync()) {
1006 if (micro_locate (nframes)) {
1007 /* XXX ERROR !!! XXX */
1011 return true; // done, nothing left to process
1018 Session::queue_event (SessionEvent* ev)
1020 if (_state_of_the_state & Deletion) {
1022 } else if (_state_of_the_state & Loading) {
1025 Glib::Threads::Mutex::Lock lm (rb_write_lock);
1026 pending_events.write (&ev, 1);
1031 Session::set_next_event ()
1033 if (events.empty()) {
1034 next_event = events.end();
1038 if (next_event == events.end()) {
1039 next_event = events.begin();
1042 if ((*next_event)->action_sample > _transport_sample) {
1043 next_event = events.begin();
1046 for (; next_event != events.end(); ++next_event) {
1047 if ((*next_event)->action_sample >= _transport_sample) {
1054 Session::process_event (SessionEvent* ev)
1059 /* if we're in the middle of a state change (i.e. waiting
1060 for the butler thread to complete the non-realtime
1061 part of the change), we'll just have to queue this
1062 event for a time when the change is complete.
1065 if (non_realtime_work_pending()) {
1067 /* except locates, which we have the capability to handle */
1069 if (ev->type != SessionEvent::Locate) {
1070 immediate_events.insert (immediate_events.end(), ev);
1076 DEBUG_TRACE (DEBUG::SessionEvents, string_compose ("Processing event: %1 @ %2\n", enum_2_string (ev->type), _transport_sample));
1079 case SessionEvent::SetLoop:
1080 set_play_loop (ev->yes_or_no, ev->speed);
1083 case SessionEvent::AutoLoop:
1085 /* roll after locate, do not flush, set "with loop"
1086 true only if we are seamless looping
1088 start_locate (ev->target_sample, true, false, Config->get_seamless_loop());
1094 case SessionEvent::AutoLoopDeclick:
1096 /* Request a declick fade-out and a fade-in; the fade-out will happen
1097 at the end of the loop, and the fade-in at the start.
1099 transport_sub_state |= (PendingLoopDeclickOut | PendingLoopDeclickIn);
1105 case SessionEvent::Locate:
1106 if (ev->yes_or_no) { /* force locate */
1107 /* args: do not roll after locate, do flush, not with loop */
1108 locate (ev->target_sample, false, true, false);
1110 /* args: do not roll after locate, do flush, not with loop */
1111 start_locate (ev->target_sample, false, true, false);
1113 _send_timecode_update = true;
1116 case SessionEvent::LocateRoll:
1117 if (ev->yes_or_no) {
1118 /* args: roll after locate, do flush, not with loop */
1119 locate (ev->target_sample, true, true, false);
1121 /* args: roll after locate, do flush, not with loop */
1122 start_locate (ev->target_sample, true, true, false);
1124 _send_timecode_update = true;
1127 case SessionEvent::Skip:
1128 if (Config->get_skip_playback()) {
1129 start_locate (ev->target_sample, true, true, false);
1130 _send_timecode_update = true;
1136 case SessionEvent::LocateRollLocate:
1137 // locate is handled by ::request_roll_at_and_return()
1138 _requested_return_sample = ev->target_sample;
1139 request_locate (ev->target2_sample, true);
1143 case SessionEvent::SetTransportSpeed:
1144 set_transport_speed (ev->speed, ev->target_sample, ev->yes_or_no, ev->second_yes_or_no, ev->third_yes_or_no);
1147 case SessionEvent::PunchIn:
1148 // cerr << "PunchIN at " << transport_sample() << endl;
1149 if (config.get_punch_in() && record_status() == Enabled) {
1156 case SessionEvent::PunchOut:
1157 // cerr << "PunchOUT at " << transport_sample() << endl;
1158 if (config.get_punch_out()) {
1159 step_back_from_record ();
1165 case SessionEvent::StopOnce:
1166 if (!non_realtime_work_pending()) {
1167 _clear_event_type (SessionEvent::StopOnce);
1168 stop_transport (ev->yes_or_no);
1174 case SessionEvent::RangeStop:
1175 if (!non_realtime_work_pending()) {
1176 stop_transport (ev->yes_or_no);
1182 case SessionEvent::RangeLocate:
1183 /* args: roll after locate, do flush, not with loop */
1184 start_locate (ev->target_sample, true, true, false);
1189 case SessionEvent::Overwrite:
1190 overwrite_some_buffers (static_cast<Track*>(ev->ptr));
1193 case SessionEvent::SetSyncSource:
1194 DEBUG_TRACE (DEBUG::Slave, "seen request for new slave\n");
1195 use_sync_source (ev->slave);
1198 case SessionEvent::Audition:
1199 set_audition (ev->region);
1200 // drop reference to region
1201 ev->region.reset ();
1204 case SessionEvent::SetPlayAudioRange:
1205 set_play_range (ev->audio_range, (ev->speed == 1.0f));
1208 case SessionEvent::CancelPlayAudioRange:
1212 case SessionEvent::RealTimeOperation:
1214 del = false; // other side of RT request needs to clean up
1217 case SessionEvent::AdjustPlaybackBuffering:
1218 schedule_playback_buffering_adjustment ();
1221 case SessionEvent::AdjustCaptureBuffering:
1222 schedule_capture_buffering_adjustment ();
1225 case SessionEvent::SetTimecodeTransmission:
1226 g_atomic_int_set (&_suspend_timecode_transmission, ev->yes_or_no ? 0 : 1);
1230 fatal << string_compose(_("Programming error: illegal event type in process_event (%1)"), ev->type) << endmsg;
1231 abort(); /*NOTREACHED*/
1236 del = del && !_remove_event (ev);
1245 Session::compute_stop_limit () const
1247 if (!Config->get_stop_at_session_end ()) {
1248 return max_samplepos;
1252 return max_samplepos;
1255 bool const punching_in = (config.get_punch_in () && _locations->auto_punch_location());
1256 bool const punching_out = (config.get_punch_out () && _locations->auto_punch_location());
1258 if (actively_recording ()) {
1259 /* permanently recording */
1260 return max_samplepos;
1261 } else if (punching_in && !punching_out) {
1262 /* punching in but never out */
1263 return max_samplepos;
1264 } else if (punching_in && punching_out && _locations->auto_punch_location()->end() > current_end_sample()) {
1265 /* punching in and punching out after session end */
1266 return max_samplepos;
1269 return current_end_sample ();
1274 /* dedicated thread for signal emission.
1276 * while sending cross-thread signals from the process thread
1277 * is fine in general, PBD::Signal's use of boost::function and
1278 * boost:bind can produce a vast overhead which is not
1279 * acceptable for low latency.
1281 * This works around the issue by moving the boost overhead
1282 * out of the RT thread. The overall load is probably higher but
1283 * the realtime thread remains unaffected.
1287 Session::emit_route_signals ()
1289 // TODO use RAII to allow using these signals in other places
1290 BatchUpdateStart(); /* EMIT SIGNAL */
1291 boost::shared_ptr<RouteList> r = routes.reader ();
1292 for (RouteList::const_iterator ci = r->begin(); ci != r->end(); ++ci) {
1293 (*ci)->emit_pending_signals ();
1295 BatchUpdateEnd(); /* EMIT SIGNAL */
1299 Session::emit_thread_start ()
1301 if (_rt_thread_active) {
1304 _rt_thread_active = true;
1306 if (pthread_create (&_rt_emit_thread, NULL, emit_thread, this)) {
1307 _rt_thread_active = false;
1312 Session::emit_thread_terminate ()
1314 if (!_rt_thread_active) {
1317 _rt_thread_active = false;
1319 if (pthread_mutex_lock (&_rt_emit_mutex) == 0) {
1320 pthread_cond_signal (&_rt_emit_cond);
1321 pthread_mutex_unlock (&_rt_emit_mutex);
1325 pthread_join (_rt_emit_thread, &status);
1329 Session::emit_thread (void *arg)
1331 Session *s = static_cast<Session *>(arg);
1332 s->emit_thread_run ();
1338 Session::emit_thread_run ()
1340 pthread_mutex_lock (&_rt_emit_mutex);
1341 while (_rt_thread_active) {
1342 emit_route_signals();
1343 pthread_cond_wait (&_rt_emit_cond, &_rt_emit_mutex);
1345 pthread_mutex_unlock (&_rt_emit_mutex);