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/thread.h>
30 #include "ardour/ardour.h"
31 #include "ardour/audioengine.h"
32 #include "ardour/auditioner.h"
33 #include "ardour/butler.h"
34 #include "ardour/debug.h"
35 #include "ardour/process_thread.h"
36 #include "ardour/session.h"
37 #include "ardour/slave.h"
38 #include "ardour/timestamps.h"
39 #include "ardour/graph.h"
40 #include "ardour/port.h"
42 #include "midi++/manager.h"
46 using namespace ARDOUR;
50 /** Called by the audio engine when there is work to be done with JACK.
51 * @param nframes Number of frames to process.
54 Session::process (nframes_t nframes)
56 MIDI::Manager::instance()->cycle_start(nframes);
60 if (processing_blocked()) {
65 if (non_realtime_work_pending()) {
66 if (!_butler->transport_work_requested ()) {
71 _engine.main_thread()->get_buffers ();
73 (this->*process_function) (nframes);
75 _engine.main_thread()->drop_buffers ();
77 // the ticker is for sending time information like MidiClock
78 nframes_t transport_frames = transport_frame();
79 BBT_Time transport_bbt;
80 bbt_time(transport_frames, transport_bbt);
81 Timecode::Time transport_timecode;
82 timecode_time(transport_frames, transport_timecode);
83 tick (transport_frames, transport_bbt, transport_timecode); /* EMIT SIGNAL */
85 SendFeedback (); /* EMIT SIGNAL */
87 MIDI::Manager::instance()->cycle_end();
91 Session::fail_roll (nframes_t nframes)
93 return no_roll (nframes);
97 Session::no_roll (nframes_t nframes)
99 nframes_t end_frame = _transport_frame + nframes; // FIXME: varispeed + no_roll ??
101 bool declick = get_transport_declick_required();
102 boost::shared_ptr<RouteList> r = routes.reader ();
105 _click_io->silence (nframes);
108 route_graph->routes_no_roll( nframes, _transport_frame, end_frame, non_realtime_work_pending(), actively_recording(), declick);
110 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
112 if ((*i)->is_hidden()) {
116 (*i)->set_pending_declick (declick);
118 if ((*i)->no_roll (nframes, _transport_frame, end_frame, non_realtime_work_pending(),
119 actively_recording(), declick)) {
120 error << string_compose(_("Session: error in no roll for %1"), (*i)->name()) << endmsg;
131 Session::process_routes (nframes_t nframes, bool& need_butler)
134 int declick = get_transport_declick_required();
135 bool rec_monitors = get_rec_monitors_input();
136 boost::shared_ptr<RouteList> r = routes.reader ();
138 if (transport_sub_state & StopPendingCapture) {
139 /* force a declick out */
143 record_active = actively_recording(); // || (get_record_enabled() && get_punch_in());
145 const nframes_t start_frame = _transport_frame;
146 const nframes_t end_frame = _transport_frame + (nframes_t)floor(nframes * _transport_speed);
148 route_graph->process_routes( nframes, start_frame, end_frame, declick, record_active, rec_monitors, need_butler);
150 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
154 if ((*i)->is_hidden()) {
158 (*i)->set_pending_declick (declick);
160 if ((ret = (*i)->roll (nframes, start_frame, end_frame, declick, record_active, rec_monitors, need_butler)) < 0) {
170 Session::silent_process_routes (nframes_t nframes, bool& need_butler)
172 bool record_active = actively_recording();
173 int declick = get_transport_declick_required();
174 bool rec_monitors = get_rec_monitors_input();
175 boost::shared_ptr<RouteList> r = routes.reader ();
177 if (transport_sub_state & StopPendingCapture) {
178 /* force a declick out */
182 const nframes_t start_frame = _transport_frame;
183 const nframes_t end_frame = _transport_frame + lrintf(nframes * _transport_speed);
185 route_graph->silent_process_routes( nframes, start_frame, end_frame, record_active, rec_monitors, need_butler);
187 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
191 if ((*i)->is_hidden()) {
195 if ((ret = (*i)->silent_roll (nframes, start_frame, end_frame, record_active, rec_monitors, need_butler)) < 0) {
205 Session::get_track_statistics ()
210 boost::shared_ptr<RouteList> rl = routes.reader();
211 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
213 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
215 if (!tr || tr->hidden()) {
219 pworst = min (pworst, tr->playback_buffer_load());
220 cworst = min (cworst, tr->capture_buffer_load());
223 g_atomic_int_set (&_playback_load, (uint32_t) floor (pworst * 100.0f));
224 g_atomic_int_set (&_capture_load, (uint32_t) floor (cworst * 100.0f));
226 if (actively_recording()) {
231 /** Process callback used when the auditioner is not active */
233 Session::process_with_events (nframes_t nframes)
236 nframes_t this_nframes;
238 bool session_needs_butler = false;
239 nframes_t stop_limit;
242 /* make sure the auditioner is silent */
245 auditioner->silence (nframes);
248 /* handle any pending events */
250 while (pending_events.read (&ev, 1) == 1) {
254 /* if we are not in the middle of a state change,
255 and there are immediate events queued up,
259 while (!non_realtime_work_pending() && !immediate_events.empty()) {
260 SessionEvent *ev = immediate_events.front ();
261 immediate_events.pop_front ();
265 /* Events caused a transport change, send an MTC Full Frame (Timecode) message.
266 * This is sent whether rolling or not, to give slaves an idea of ardour time
267 * on locates (and allow slow slaves to position and prepare for rolling)
269 if (_send_timecode_update) {
270 send_full_time_code(nframes);
273 if (!process_can_proceed()) {
278 if (events.empty() || next_event == events.end()) {
279 process_without_events (nframes);
283 if (_transport_speed == 1.0) {
284 frames_moved = (long) nframes;
286 interpolation.set_target_speed (fabs(_target_transport_speed));
287 interpolation.set_speed (fabs(_transport_speed));
288 frames_moved = (long) interpolation.interpolate (0, nframes, 0, 0);
291 end_frame = _transport_frame + (nframes_t)frames_moved;
294 SessionEvent* this_event;
295 Events::iterator the_next_one;
297 if (!process_can_proceed()) {
302 if (!_exporting && _slave) {
303 if (!follow_slave (nframes)) {
308 if (_transport_speed == 0) {
314 send_midi_time_code_for_cycle (nframes);
317 if (actively_recording()) {
318 stop_limit = max_frames;
321 if (Config->get_stop_at_session_end()) {
322 stop_limit = current_end_frame();
324 stop_limit = max_frames;
328 if (maybe_stop (stop_limit)) {
333 this_event = *next_event;
334 the_next_one = next_event;
337 /* yes folks, here it is, the actual loop where we really truly
343 this_nframes = nframes; /* real (jack) time relative */
344 frames_moved = (long) floor (_transport_speed * nframes); /* transport relative */
346 /* running an event, position transport precisely to its time */
347 if (this_event && this_event->action_frame <= end_frame && this_event->action_frame >= _transport_frame) {
348 /* this isn't quite right for reverse play */
349 frames_moved = (long) (this_event->action_frame - _transport_frame);
350 this_nframes = (nframes_t) abs( floor(frames_moved / _transport_speed) );
355 click (_transport_frame, this_nframes);
357 if (process_routes (this_nframes, session_needs_butler)) {
362 get_track_statistics ();
364 nframes -= this_nframes;
366 if (frames_moved < 0) {
367 decrement_transport_position (-frames_moved);
369 increment_transport_position (frames_moved);
372 maybe_stop (stop_limit);
373 check_declick_out ();
376 _engine.split_cycle (this_nframes);
378 /* now handle this event and all others scheduled for the same time */
380 while (this_event && this_event->action_frame == _transport_frame) {
381 process_event (this_event);
383 if (the_next_one == events.end()) {
386 this_event = *the_next_one;
391 /* if an event left our state changing, do the right thing */
393 if (nframes && non_realtime_work_pending()) {
398 /* this is necessary to handle the case of seamless looping */
399 end_frame = _transport_frame + (nframes_t) floor (nframes * _transport_speed);
405 } /* implicit release of route lock */
407 if (session_needs_butler) {
413 Session::reset_slave_state ()
415 average_slave_delta = 1800;
416 delta_accumulator_cnt = 0;
417 have_first_delta_accumulator = false;
418 _slave_state = Stopped;
422 Session::transport_locked () const
426 if (!locate_pending() && (!config.get_external_sync() || (sl && sl->ok() && sl->locked()))) {
434 Session::follow_slave (nframes_t nframes)
437 nframes64_t slave_transport_frame;
438 nframes_t this_delta;
443 config.set_external_sync (false);
447 _slave->speed_and_position (slave_speed, slave_transport_frame);
449 DEBUG_TRACE (DEBUG::Slave, string_compose ("Slave position %1 speed %2\n", slave_transport_frame, slave_speed));
451 if (!_slave->locked()) {
452 DEBUG_TRACE (DEBUG::Slave, "slave not locked\n");
456 if (slave_transport_frame > _transport_frame) {
457 this_delta = slave_transport_frame - _transport_frame;
460 this_delta = _transport_frame - slave_transport_frame;
464 if (_slave->starting()) {
468 if (_slave->is_always_synced() || config.get_timecode_source_is_synced()) {
470 /* if the TC source is synced, then we assume that its
471 speed is binary: 0.0 or 1.0
474 if (slave_speed != 0.0f) {
480 /* if we are chasing and the average delta between us and the
481 master gets too big, we want to switch to silent
482 motion. so keep track of that here.
485 if (_slave_state == Running) {
486 calculate_moving_average_of_slave_delta(dir, this_delta);
490 track_slave_state (slave_speed, slave_transport_frame, this_delta);
492 DEBUG_TRACE (DEBUG::Slave, string_compose ("slave state %1 @ %2 speed %3 cur delta %4 avg delta %5\n",
493 _slave_state, slave_transport_frame, slave_speed, this_delta, average_slave_delta));
496 if (_slave_state == Running && !_slave->is_always_synced() && !config.get_timecode_source_is_synced()) {
498 if (_transport_speed != 0.0f) {
501 note that average_dir is +1 or -1
506 if (average_slave_delta == 0) {
510 delta = average_slave_delta;
511 delta *= average_dir;
515 if (slave_speed != 0.0) {
516 DEBUG_TRACE (DEBUG::Slave, string_compose ("delta = %1 speed = %2 ts = %3 M@%4 S@%5 avgdelta %6\n",
517 (int) (dir * this_delta),
521 slave_transport_frame,
522 average_slave_delta));
526 if (_slave->give_slave_full_control_over_transport_speed()) {
527 set_transport_speed (slave_speed, false, false);
528 //std::cout << "set speed = " << slave_speed << "\n";
530 float adjusted_speed = slave_speed + (1.5 * (delta / float(_current_frame_rate)));
531 request_transport_speed (adjusted_speed);
532 DEBUG_TRACE (DEBUG::Slave, string_compose ("adjust using %1 towards %2 ratio %3 current %4 slave @ %5\n",
533 delta, adjusted_speed, adjusted_speed/slave_speed, _transport_speed,
538 if ((nframes_t) abs(average_slave_delta) > _slave->resolution()) {
539 cerr << "average slave delta greater than slave resolution (" << _slave->resolution() << "), going to silent motion\n";
547 if (_slave_state == Running && !non_realtime_work_pending()) {
548 /* speed is set, we're locked, and good to go */
553 DEBUG_TRACE (DEBUG::Slave, "silent motion\n")
554 follow_slave_silently (nframes, slave_speed);
557 /* don't move at all */
558 DEBUG_TRACE (DEBUG::Slave, "no roll\n")
564 Session::calculate_moving_average_of_slave_delta(int dir, nframes_t this_delta)
566 if (delta_accumulator_cnt >= delta_accumulator_size) {
567 have_first_delta_accumulator = true;
568 delta_accumulator_cnt = 0;
571 if (delta_accumulator_cnt != 0 || this_delta < _current_frame_rate) {
572 delta_accumulator[delta_accumulator_cnt++] = long(dir) * long(this_delta);
575 if (have_first_delta_accumulator) {
576 average_slave_delta = 0L;
577 for (int i = 0; i < delta_accumulator_size; ++i) {
578 average_slave_delta += delta_accumulator[i];
580 average_slave_delta /= long(delta_accumulator_size);
581 if (average_slave_delta < 0L) {
583 average_slave_delta = abs(average_slave_delta);
591 Session::track_slave_state (float slave_speed, nframes_t slave_transport_frame, nframes_t this_delta)
593 if (slave_speed != 0.0f) {
595 /* slave is running */
597 switch (_slave_state) {
599 if (_slave->requires_seekahead()) {
600 slave_wait_end = slave_transport_frame + _slave->seekahead_distance ();
601 DEBUG_TRACE (DEBUG::Slave, string_compose ("slave stopped, but running, requires seekahead to %1\n", slave_wait_end));
602 /* we can call locate() here because we are in process context */
603 locate (slave_wait_end, false, false);
604 _slave_state = Waiting;
608 _slave_state = Running;
610 Location* al = _locations.auto_loop_location();
612 if (al && play_loop && (slave_transport_frame < al->start() || slave_transport_frame > al->end())) {
614 request_play_loop(false);
617 if (slave_transport_frame != _transport_frame) {
618 locate (slave_transport_frame, false, false);
628 if (_slave_state == Waiting) {
630 DEBUG_TRACE (DEBUG::Slave, string_compose ("slave waiting at %1\n", slave_transport_frame));
632 if (slave_transport_frame >= slave_wait_end) {
634 DEBUG_TRACE (DEBUG::Slave, string_compose ("slave start at %1 vs %2\n", slave_transport_frame, _transport_frame));
636 _slave_state = Running;
638 /* now perform a "micro-seek" within the disk buffers to realign ourselves
639 precisely with the master.
644 nframes_t frame_delta = slave_transport_frame - _transport_frame;
646 boost::shared_ptr<RouteList> rl = routes.reader();
647 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
648 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
649 if (tr && !tr->can_internal_playback_seek (frame_delta)) {
656 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
657 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
659 tr->internal_playback_seek (frame_delta);
662 _transport_frame += frame_delta;
665 cerr << "cannot micro-seek\n";
669 memset (delta_accumulator, 0, sizeof (long) * delta_accumulator_size);
670 average_slave_delta = 0L;
675 if (_slave_state == Running && _transport_speed == 0.0f) {
676 DEBUG_TRACE (DEBUG::Slave, "slave starts transport\n");
680 } else { // slave_speed is 0
682 /* slave has stopped */
684 if (_transport_speed != 0.0f) {
685 DEBUG_TRACE (DEBUG::Slave, string_compose ("slave stops transport: %1 frame %2 tf %3\n", slave_speed, slave_transport_frame, _transport_frame));
689 if (slave_transport_frame != _transport_frame) {
690 DEBUG_TRACE (DEBUG::Slave, string_compose ("slave stopped, move to %1\n", slave_transport_frame));
691 force_locate (slave_transport_frame, false);
694 _slave_state = Stopped;
699 Session::follow_slave_silently (nframes_t nframes, float slave_speed)
701 if (slave_speed && _transport_speed) {
703 /* something isn't right, but we should move with the master
709 silent_process_routes (nframes, need_butler);
711 get_track_statistics ();
717 int32_t frames_moved = (int32_t) floor (_transport_speed * nframes);
719 if (frames_moved < 0) {
720 decrement_transport_position (-frames_moved);
722 increment_transport_position (frames_moved);
725 nframes_t stop_limit;
727 if (actively_recording()) {
728 stop_limit = max_frames;
730 if (Config->get_stop_at_session_end()) {
731 stop_limit = current_end_frame();
733 stop_limit = max_frames;
737 maybe_stop (stop_limit);
742 Session::process_without_events (nframes_t nframes)
744 bool session_needs_butler = false;
745 nframes_t stop_limit;
748 if (!process_can_proceed()) {
753 if (!_exporting && _slave) {
754 if (!follow_slave (nframes)) {
759 if (_transport_speed == 0) {
765 send_midi_time_code_for_cycle (nframes);
768 if (actively_recording()) {
769 stop_limit = max_frames;
771 if (Config->get_stop_at_session_end()) {
772 stop_limit = current_end_frame();
774 stop_limit = max_frames;
778 if (maybe_stop (stop_limit)) {
783 if (maybe_sync_start (nframes)) {
787 click (_transport_frame, nframes);
789 if (_transport_speed == 1.0) {
790 frames_moved = (long) nframes;
792 interpolation.set_target_speed (fabs(_target_transport_speed));
793 interpolation.set_speed (fabs(_transport_speed));
794 frames_moved = (long) interpolation.interpolate (0, nframes, 0, 0);
797 if (process_routes (nframes, session_needs_butler)) {
802 get_track_statistics ();
804 if (frames_moved < 0) {
805 decrement_transport_position (-frames_moved);
807 increment_transport_position (frames_moved);
810 maybe_stop (stop_limit);
811 check_declick_out ();
813 if (session_needs_butler) {
818 /** Process callback used when the auditioner is active.
819 * @param nframes number of frames to process.
822 Session::process_audition (nframes_t nframes)
825 boost::shared_ptr<RouteList> r = routes.reader ();
827 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
828 if (!(*i)->is_hidden()) {
829 (*i)->silence (nframes);
833 /* run the auditioner, and if it says we need butler service, ask for it */
835 if (auditioner->play_audition (nframes) > 0) {
839 /* if using a monitor section, run it because otherwise we don't hear anything */
841 if (auditioner->needs_monitor()) {
842 _monitor_out->passthru (_transport_frame, _transport_frame + nframes, nframes, false);
845 /* handle pending events */
847 while (pending_events.read (&ev, 1) == 1) {
851 /* if we are not in the middle of a state change,
852 and there are immediate events queued up,
856 while (!non_realtime_work_pending() && !immediate_events.empty()) {
857 SessionEvent *ev = immediate_events.front ();
858 immediate_events.pop_front ();
862 if (!auditioner->auditioning()) {
863 /* auditioner no longer active, so go back to the normal process callback */
864 process_function = &Session::process_with_events;
869 Session::maybe_sync_start (nframes_t& nframes)
871 nframes_t sync_offset;
873 if (!waiting_for_sync_offset) {
877 if (_engine.get_sync_offset (sync_offset) && sync_offset < nframes) {
879 /* generate silence up to the sync point, then
880 adjust nframes + offset to reflect whatever
884 no_roll (sync_offset);
885 nframes -= sync_offset;
886 Port::increment_port_offset (sync_offset);
887 waiting_for_sync_offset = false;
890 return true; // done, nothing left to process
895 /* sync offset point is not within this process()
896 cycle, so just generate silence. and don't bother
897 with any fancy stuff here, just the minimal silence.
902 if (Config->get_locate_while_waiting_for_sync()) {
903 if (micro_locate (nframes)) {
904 /* XXX ERROR !!! XXX */
908 return true; // done, nothing left to process
915 Session::queue_event (SessionEvent* ev)
917 if (_state_of_the_state & Deletion) {
919 } else if (_state_of_the_state & Loading) {
922 pending_events.write (&ev, 1);
927 Session::set_next_event ()
929 if (events.empty()) {
930 next_event = events.end();
934 if (next_event == events.end()) {
935 next_event = events.begin();
938 if ((*next_event)->action_frame > _transport_frame) {
939 next_event = events.begin();
942 for (; next_event != events.end(); ++next_event) {
943 if ((*next_event)->action_frame >= _transport_frame) {
950 Session::process_event (SessionEvent* ev)
955 /* if we're in the middle of a state change (i.e. waiting
956 for the butler thread to complete the non-realtime
957 part of the change), we'll just have to queue this
958 event for a time when the change is complete.
961 if (non_realtime_work_pending()) {
963 /* except locates, which we have the capability to handle */
965 if (ev->type != SessionEvent::Locate) {
966 immediate_events.insert (immediate_events.end(), ev);
972 DEBUG_TRACE (DEBUG::SessionEvents, string_compose ("Processing event: %1 @ %2\n", enum_2_string (ev->type), _transport_frame));
975 case SessionEvent::SetLoop:
976 set_play_loop (ev->yes_or_no);
979 case SessionEvent::AutoLoop:
981 start_locate (ev->target_frame, true, false, Config->get_seamless_loop());
987 case SessionEvent::Locate:
989 // cerr << "forced locate to " << ev->target_frame << endl;
990 locate (ev->target_frame, false, true, false);
992 // cerr << "soft locate to " << ev->target_frame << endl;
993 start_locate (ev->target_frame, false, true, false);
995 _send_timecode_update = true;
998 case SessionEvent::LocateRoll:
1000 // cerr << "forced locate to+roll " << ev->target_frame << endl;
1001 locate (ev->target_frame, true, true, false);
1003 // cerr << "soft locate to+roll " << ev->target_frame << endl;
1004 start_locate (ev->target_frame, true, true, false);
1006 _send_timecode_update = true;
1009 case SessionEvent::LocateRollLocate:
1010 // locate is handled by ::request_roll_at_and_return()
1011 _requested_return_frame = ev->target_frame;
1012 request_locate (ev->target2_frame, true);
1016 case SessionEvent::SetTransportSpeed:
1017 set_transport_speed (ev->speed, ev->yes_or_no, ev->second_yes_or_no);
1020 case SessionEvent::PunchIn:
1021 // cerr << "PunchIN at " << transport_frame() << endl;
1022 if (config.get_punch_in() && record_status() == Enabled) {
1029 case SessionEvent::PunchOut:
1030 // cerr << "PunchOUT at " << transport_frame() << endl;
1031 if (config.get_punch_out()) {
1032 step_back_from_record ();
1038 case SessionEvent::StopOnce:
1039 if (!non_realtime_work_pending()) {
1040 stop_transport (ev->yes_or_no);
1041 _clear_event_type (SessionEvent::StopOnce);
1047 case SessionEvent::RangeStop:
1048 if (!non_realtime_work_pending()) {
1049 stop_transport (ev->yes_or_no);
1055 case SessionEvent::RangeLocate:
1056 start_locate (ev->target_frame, true, true, false);
1061 case SessionEvent::Overwrite:
1062 overwrite_some_buffers (static_cast<Track*>(ev->ptr));
1065 case SessionEvent::SetTrackSpeed:
1066 set_track_speed (static_cast<Track*> (ev->ptr), ev->speed);
1069 case SessionEvent::SetSyncSource:
1070 use_sync_source (ev->slave);
1073 case SessionEvent::Audition:
1074 set_audition (ev->region);
1075 // drop reference to region
1076 ev->region.reset ();
1079 case SessionEvent::InputConfigurationChange:
1080 add_post_transport_work (PostTransportInputChange);
1081 _butler->schedule_transport_work ();
1084 case SessionEvent::SetPlayAudioRange:
1085 set_play_range (ev->audio_range, (ev->speed == 1.0f));
1088 case SessionEvent::RealTimeOperation:
1090 del = false; // other side of RT request needs to clean up
1093 case SessionEvent::AdjustPlaybackBuffering:
1094 schedule_playback_buffering_adjustment ();
1097 case SessionEvent::AdjustCaptureBuffering:
1098 schedule_capture_buffering_adjustment ();
1102 fatal << string_compose(_("Programming error: illegal event type in process_event (%1)"), ev->type) << endmsg;
1108 del = del && !_remove_event (ev);