28 #include <glibmm/threads.h>
34 #include "ardour/debug.h"
44 #include "midi++/mmc.h"
59 framepos_t transport_at_start = _transport_frame;
63 if (processing_blocked()) {
68 if (non_realtime_work_pending()) {
69 if (!_butler->transport_work_requested ()) {
74 _engine.main_thread()->get_buffers ();
76 (this->*process_function) (nframes);
85 for (RouteList::const_iterator i = r->begin(); i != r->end(); ++i) {
86 if ((*i)->apply_processor_changes_rt()) {
87 _rt_emit_pending =
true;
90 if (_rt_emit_pending) {
91 if (!_rt_thread_active) {
92 emit_route_signals ();
94 if (pthread_mutex_trylock (&_rt_emit_mutex) == 0) {
95 pthread_cond_signal (&_rt_emit_cond);
96 pthread_mutex_unlock (&_rt_emit_mutex);
97 _rt_emit_pending =
false;
101 _engine.main_thread()->drop_buffers ();
110 if (!_silent && !_engine.freewheeling() &&
Config->get_send_midi_clock() && (transport_speed() == 1.0f || transport_speed() == 0.0f) && midi_clock->has_midi_port()) {
111 midi_clock->tick (transport_at_start, nframes);
114 _scene_changer->run (transport_at_start, transport_at_start + nframes);
126 return no_roll (nframes);
134 framepos_t end_frame = _transport_frame + nframes;
136 int declick = get_transport_declick_required();
140 _click_io->silence (nframes);
143 ltc_tx_send_time_code_for_cycle (_transport_frame, end_frame, _target_transport_speed, _transport_speed, nframes);
145 if (_process_graph) {
147 _process_graph->routes_no_roll( nframes, _transport_frame, end_frame, non_realtime_work_pending(), declick);
150 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
152 if ((*i)->is_auditioner()) {
156 (*i)->set_pending_declick (declick);
158 if ((*i)->no_roll (nframes, _transport_frame, end_frame, non_realtime_work_pending())) {
177 int declick = get_transport_declick_required();
180 const framepos_t start_frame = _transport_frame;
181 const framepos_t end_frame = _transport_frame + floor (nframes * _transport_speed);
183 if (_process_graph) {
185 if (_process_graph->process_routes (nframes, start_frame, end_frame, declick, need_butler) < 0) {
191 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
195 if ((*i)->is_auditioner()) {
199 (*i)->set_pending_declick (declick);
203 if ((ret = (*i)->roll (nframes, start_frame, end_frame, declick, b)) < 0) {
225 const framepos_t start_frame = _transport_frame;
226 const framepos_t end_frame = _transport_frame + lrintf(nframes * _transport_speed);
228 if (_process_graph) {
229 _process_graph->silent_process_routes (nframes, start_frame, end_frame, need_butler);
231 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
235 if ((*i)->is_auditioner()) {
241 if ((ret = (*i)->silent_roll (nframes, start_frame, end_frame, b)) < 0) {
262 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
266 if (!tr || tr->
hidden()) {
274 g_atomic_int_set (&_playback_load, (uint32_t) floor (pworst * 100.0
f));
275 g_atomic_int_set (&_capture_load, (uint32_t) floor (cworst * 100.0f));
277 if (actively_recording()) {
291 bool session_needs_butler =
false;
297 auditioner->silence (nframes);
302 while (pending_events.read (&ev, 1) == 1) {
311 while (!non_realtime_work_pending() && !immediate_events.empty()) {
313 immediate_events.pop_front ();
319 bool const was_sending_qf_mtc = _send_qf_mtc;
320 double const tolerance =
Config->get_mtc_qf_speed_tolerance() / 100.0;
322 if (_transport_speed != 0) {
324 Config->get_send_mtc () &&
325 _transport_speed >= (1 - tolerance) &&
326 _transport_speed <= (1 + tolerance)
329 if (_send_qf_mtc && !was_sending_qf_mtc) {
331 _send_timecode_update =
true;
334 if (
Config->get_send_mtc() && !_send_qf_mtc && _pframes_since_last_mtc > (frame_rate () / 4)) {
339 _send_timecode_update =
true;
342 _pframes_since_last_mtc += nframes;
351 if (_send_timecode_update) {
352 send_full_time_code (_transport_frame, nframes);
355 if (!process_can_proceed()) {
360 if (events.empty() || next_event == events.end()) {
361 process_without_events (nframes);
365 if (_transport_speed == 1.0) {
368 interpolation.set_target_speed (_target_transport_speed);
369 interpolation.set_speed (_transport_speed);
370 frames_moved = (
framecnt_t) interpolation.interpolate (0, nframes, 0, 0);
373 end_frame = _transport_frame + frames_moved;
377 Events::iterator the_next_one;
379 if (!process_can_proceed()) {
384 if (!_exporting && _slave) {
385 if (!follow_slave (nframes)) {
390 if (_transport_speed == 0) {
395 if (!_exporting && !timecode_transmission_suspended()) {
396 send_midi_time_code_for_cycle (_transport_frame, end_frame, nframes);
399 ltc_tx_send_time_code_for_cycle (_transport_frame, end_frame, _target_transport_speed, _transport_speed, nframes);
401 framepos_t stop_limit = compute_stop_limit ();
403 if (maybe_stop (stop_limit)) {
408 this_event = *next_event;
409 the_next_one = next_event;
418 this_nframes = nframes;
419 frames_moved = (
framecnt_t) floor (_transport_speed * nframes);
425 this_nframes = abs (floor(frames_moved / _transport_speed));
430 click (_transport_frame, this_nframes);
432 if (process_routes (this_nframes, session_needs_butler)) {
437 get_track_statistics ();
439 nframes -= this_nframes;
441 if (frames_moved < 0) {
442 decrement_transport_position (-frames_moved);
444 increment_transport_position (frames_moved);
447 maybe_stop (stop_limit);
448 check_declick_out ();
452 _engine.split_cycle (this_nframes);
457 while (this_event && this_event->
action_frame == _transport_frame) {
458 process_event (this_event);
460 if (the_next_one == events.end()) {
463 this_event = *the_next_one;
470 if (nframes && non_realtime_work_pending()) {
476 end_frame = _transport_frame + floor (nframes * _transport_speed);
483 if (session_needs_butler) {
491 average_slave_delta = 1800;
492 delta_accumulator_cnt = 0;
493 have_first_delta_accumulator =
false;
494 _slave_state = Stopped;
502 if (!locate_pending() && (!config.get_external_sync() || (sl && sl->
ok() && sl->
locked()))) {
519 config.set_external_sync (
false);
523 _slave->speed_and_position (slave_speed, slave_transport_frame);
527 if (!_slave->locked()) {
532 if (slave_transport_frame > _transport_frame) {
533 this_delta = slave_transport_frame - _transport_frame;
536 this_delta = _transport_frame - slave_transport_frame;
540 if (_slave->starting()) {
544 if (_slave->is_always_synced() ||
552 if (slave_speed != 0.0
f) {
563 if (_slave_state == Running) {
564 calculate_moving_average_of_slave_delta(dir, this_delta);
568 track_slave_state (slave_speed, slave_transport_frame, this_delta);
571 _slave_state, slave_transport_frame, slave_speed, this_delta, average_slave_delta));
574 if (_slave_state == Running && !_slave->is_always_synced() &&
578 if (_transport_speed != 0.0
f) {
586 if (average_slave_delta == 0) {
590 delta = average_slave_delta;
591 delta *= average_dir;
595 if (slave_speed != 0.0) {
597 (
int) (dir * this_delta),
601 slave_transport_frame,
602 average_slave_delta));
606 if (_slave->give_slave_full_control_over_transport_speed()) {
607 set_transport_speed (slave_speed, 0,
false,
false);
610 float adjusted_speed = slave_speed + (1.5 * (delta / float(_current_frame_rate)));
611 request_transport_speed (adjusted_speed);
613 delta, adjusted_speed, adjusted_speed/slave_speed, _transport_speed,
618 if (!actively_recording() && (
framecnt_t) abs(average_slave_delta) > _slave->resolution()) {
619 cerr <<
"average slave delta greater than slave resolution (" << _slave->resolution() <<
"), going to silent motion\n";
627 if (_slave_state == Running && !non_realtime_work_pending()) {
634 follow_slave_silently (nframes, slave_speed);
646 if (delta_accumulator_cnt >= delta_accumulator_size) {
647 have_first_delta_accumulator =
true;
648 delta_accumulator_cnt = 0;
651 if (delta_accumulator_cnt != 0 || this_delta < _current_frame_rate) {
655 if (have_first_delta_accumulator) {
656 average_slave_delta = 0L;
657 for (
int i = 0; i < delta_accumulator_size; ++i) {
658 average_slave_delta += delta_accumulator[i];
660 average_slave_delta /= (int32_t) delta_accumulator_size;
661 if (average_slave_delta < 0L) {
663 average_slave_delta = abs(average_slave_delta);
673 if (slave_speed != 0.0
f) {
677 switch (_slave_state) {
679 if (_slave->requires_seekahead()) {
680 slave_wait_end = slave_transport_frame + _slave->seekahead_distance ();
683 locate (slave_wait_end,
false,
false);
684 _slave_state = Waiting;
690 memset (delta_accumulator, 0,
sizeof (int32_t) * delta_accumulator_size);
691 average_slave_delta = 0L;
693 Location* al = _locations->auto_loop_location();
695 if (al && play_loop && (slave_transport_frame < al->
start() || slave_transport_frame > al->
end())) {
697 request_play_loop(
false);
700 if (slave_transport_frame != _transport_frame) {
702 locate (slave_transport_frame,
false,
false);
704 _slave_state = Running;
713 if (_slave_state == Waiting) {
717 if (slave_transport_frame >= slave_wait_end) {
721 _slave_state = Running;
729 framecnt_t frame_delta = slave_transport_frame - _transport_frame;
732 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
741 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
747 _transport_frame += frame_delta;
750 cerr <<
"cannot micro-seek\n";
756 if (_slave_state == Running && _transport_speed == 0.0
f) {
765 if (_transport_speed != 0.0
f) {
770 if (slave_transport_frame != _transport_frame) {
772 force_locate (slave_transport_frame,
false);
782 if (slave_speed && _transport_speed) {
788 bool need_butler =
false;
790 silent_process_routes (nframes, need_butler);
792 get_track_statistics ();
798 int32_t frames_moved = (int32_t) floor (_transport_speed * nframes);
800 if (frames_moved < 0) {
801 decrement_transport_position (-frames_moved);
803 increment_transport_position (frames_moved);
806 framepos_t const stop_limit = compute_stop_limit ();
807 maybe_stop (stop_limit);
814 bool session_needs_butler =
false;
817 if (!process_can_proceed()) {
822 if (!_exporting && _slave) {
823 if (!follow_slave (nframes)) {
824 ltc_tx_send_time_code_for_cycle (_transport_frame, _transport_frame, 0, 0 , nframes);
829 if (_transport_speed == 0) {
834 if (_transport_speed == 1.0) {
837 interpolation.set_target_speed (_target_transport_speed);
838 interpolation.set_speed (_transport_speed);
839 frames_moved = (
framecnt_t) interpolation.interpolate (0, nframes, 0, 0);
842 if (!_exporting && !timecode_transmission_suspended()) {
843 send_midi_time_code_for_cycle (_transport_frame, _transport_frame + frames_moved, nframes);
846 ltc_tx_send_time_code_for_cycle (_transport_frame, _transport_frame + frames_moved, _target_transport_speed, _transport_speed, nframes);
848 framepos_t const stop_limit = compute_stop_limit ();
850 if (maybe_stop (stop_limit)) {
855 if (maybe_sync_start (nframes)) {
859 click (_transport_frame, nframes);
861 if (process_routes (nframes, session_needs_butler)) {
866 get_track_statistics ();
868 if (frames_moved < 0) {
869 decrement_transport_position (-frames_moved);
871 increment_transport_position (frames_moved);
874 maybe_stop (stop_limit);
875 check_declick_out ();
877 if (session_needs_butler) {
891 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
892 if (!(*i)->is_auditioner()) {
893 (*i)->silence (nframes);
899 if (auditioner->play_audition (nframes) > 0) {
905 if (_monitor_out && auditioner->needs_monitor()) {
906 _monitor_out->monitor_run (_transport_frame, _transport_frame + nframes, nframes,
false);
911 while (pending_events.read (&ev, 1) == 1) {
920 while (!non_realtime_work_pending() && !immediate_events.empty()) {
922 immediate_events.pop_front ();
926 if (!auditioner->auditioning()) {
937 if (!waiting_for_sync_offset) {
941 if (_engine.get_sync_offset (sync_offset) && sync_offset < nframes) {
948 no_roll (sync_offset);
949 nframes -= sync_offset;
951 waiting_for_sync_offset =
false;
966 if (
Config->get_locate_while_waiting_for_sync()) {
967 if (micro_locate (nframes)) {
981 if (_state_of_the_state & Deletion) {
983 }
else if (_state_of_the_state & Loading) {
986 pending_events.write (&ev, 1);
993 if (events.empty()) {
994 next_event = events.end();
998 if (next_event == events.end()) {
999 next_event = events.begin();
1002 if ((*next_event)->action_frame > _transport_frame) {
1003 next_event = events.begin();
1006 for (; next_event != events.end(); ++next_event) {
1007 if ((*next_event)->action_frame >= _transport_frame) {
1025 if (non_realtime_work_pending()) {
1030 immediate_events.insert (immediate_events.end(), ev);
1059 transport_sub_state |= (PendingLoopDeclickOut | PendingLoopDeclickIn);
1073 _send_timecode_update =
true;
1084 _send_timecode_update =
true;
1088 if (
Config->get_skip_playback()) {
1090 _send_timecode_update =
true;
1109 if (config.get_punch_in() && record_status() == Enabled) {
1118 if (config.get_punch_out()) {
1119 step_back_from_record ();
1126 if (!non_realtime_work_pending()) {
1135 if (!non_realtime_work_pending()) {
1150 overwrite_some_buffers (static_cast<Track*>(ev->
ptr));
1154 set_track_speed (static_cast<Track*> (ev->
ptr), ev->
speed);
1159 use_sync_source (ev->
slave);
1163 set_audition (ev->
region);
1169 add_post_transport_work (PostTransportInputChange);
1170 _butler->schedule_transport_work ();
1187 schedule_playback_buffering_adjustment ();
1191 schedule_capture_buffering_adjustment ();
1195 g_atomic_int_set (&_suspend_timecode_transmission, ev->
yes_or_no ? 0 : 1);
1205 del = del && !_remove_event (ev);
1216 if (!
Config->get_stop_at_session_end ()) {
1225 bool const punching_in = (config.get_punch_in () && _locations->auto_punch_location());
1226 bool const punching_out = (config.get_punch_out () && _locations->auto_punch_location());
1228 if (actively_recording ()) {
1231 }
else if (punching_in && !punching_out) {
1234 }
else if (punching_in && punching_out && _locations->auto_punch_location()->end() > current_end_frame()) {
1239 return current_end_frame ();
1262 for (RouteList::const_iterator ci = r->begin(); ci != r->end(); ++ci) {
1263 (*ci)->emit_pending_signals ();
1271 if (_rt_thread_active) {
1274 _rt_thread_active =
true;
1276 if (
pthread_create (&_rt_emit_thread, NULL, emit_thread,
this)) {
1277 _rt_thread_active =
false;
1284 if (!_rt_thread_active) {
1287 _rt_thread_active =
false;
1289 if (pthread_mutex_lock (&_rt_emit_mutex) == 0) {
1290 pthread_cond_signal (&_rt_emit_cond);
1291 pthread_mutex_unlock (&_rt_emit_mutex);
1295 pthread_join (_rt_emit_thread, &status);
1310 pthread_mutex_lock (&_rt_emit_mutex);
1311 while (_rt_thread_active) {
1312 emit_route_signals();
1313 pthread_cond_wait (&_rt_emit_cond, &_rt_emit_mutex);
1315 pthread_mutex_unlock (&_rt_emit_mutex);
static void increment_global_port_buffer_offset(pframes_t n)
void emit_route_signals()
void get_track_statistics()
LIBPBD_API Transmitter fatal
bool maybe_sync_start(pframes_t &)
void calculate_moving_average_of_slave_delta(int dir, framecnt_t this_delta)
void track_slave_state(float slave_speed, framepos_t slave_transport_frame, framecnt_t this_delta)
int process_routes(pframes_t, bool &need_butler)
shared_ptr< T > dynamic_pointer_cast(shared_ptr< U > const &r)
LIBARDOUR_API uint64_t ProcessThreads
static void * emit_thread(void *)
float capture_buffer_load() const
void emit_thread_terminate()
void process_event(SessionEvent *)
LIBPBD_API Transmitter error
std::ostream & endmsg(std::ostream &ostr)
#define PT_TIMING_CHECK(x)
LIBARDOUR_API PBD::PropertyDescriptor< framepos_t > start
std::list< AudioRange > audio_range
bool transport_locked() const
LIBARDOUR_API uint64_t SessionEvents
void process_without_events(pframes_t)
LIBARDOUR_API RCConfiguration * Config
void process_with_events(pframes_t)
int internal_playback_seek(framecnt_t)
void process_audition(pframes_t)
The Slave interface can be used to sync ARDOURs tempo to an external source like MTC, MIDI Clock, etc.
void process(pframes_t nframes)
virtual bool ok() const =0
virtual bool locked() const =0
int fail_roll(pframes_t nframes)
#define DEBUG_TRACE(bits, str)
int silent_process_routes(pframes_t, bool &need_butler)
LIBARDOUR_API uint64_t Slave
void queue_event(SessionEvent *)
bool follow_slave(pframes_t)
boost::shared_ptr< Region > region
framepos_t compute_stop_limit() const
int can_internal_playback_seek(framecnt_t)
void follow_slave_silently(pframes_t nframes, float slave_speed)
float playback_buffer_load() const
static const framepos_t max_framepos
int no_roll(pframes_t nframes)
int pthread_create(pthread_t *__restrict thread, __const pthread_attr_t *__restrict attr, void *(*start_routine)(void *), void *__restrict arg)
std::string string_compose(const std::string &fmt, const T1 &o1)