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/audio_diskstream.h"
32 #include "ardour/audioengine.h"
33 #include "ardour/auditioner.h"
34 #include "ardour/butler.h"
35 #include "ardour/debug.h"
36 #include "ardour/process_thread.h"
37 #include "ardour/session.h"
38 #include "ardour/slave.h"
39 #include "ardour/timestamps.h"
41 #include "midi++/manager.h"
45 using namespace ARDOUR;
49 /** Called by the audio engine when there is work to be done with JACK.
50 * @param nframes Number of frames to process.
53 Session::process (nframes_t nframes)
55 MIDI::Manager::instance()->cycle_start(nframes);
59 if (processing_blocked()) {
64 if (non_realtime_work_pending()) {
65 if (!_butler->transport_work_requested ()) {
70 _engine.main_thread()->get_buffers ();
72 (this->*process_function) (nframes);
74 _engine.main_thread()->drop_buffers ();
76 // the ticker is for sending time information like MidiClock
77 nframes_t transport_frames = transport_frame();
78 BBT_Time transport_bbt;
79 bbt_time(transport_frames, transport_bbt);
80 Timecode::Time transport_timecode;
81 timecode_time(transport_frames, transport_timecode);
82 tick (transport_frames, transport_bbt, transport_timecode); /* EMIT SIGNAL */
84 SendFeedback (); /* EMIT SIGNAL */
86 MIDI::Manager::instance()->cycle_end();
90 Session::fail_roll (nframes_t nframes)
92 return no_roll (nframes);
96 Session::no_roll (nframes_t nframes)
98 nframes_t end_frame = _transport_frame + nframes; // FIXME: varispeed + no_roll ??
100 bool declick = get_transport_declick_required();
101 boost::shared_ptr<RouteList> r = routes.reader ();
104 _click_io->silence (nframes);
107 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
109 if ((*i)->is_hidden()) {
113 (*i)->set_pending_declick (declick);
115 if ((*i)->no_roll (nframes, _transport_frame, end_frame, non_realtime_work_pending(),
116 actively_recording(), declick)) {
117 error << string_compose(_("Session: error in no roll for %1"), (*i)->name()) << endmsg;
127 Session::process_routes (nframes_t nframes, bool& need_butler)
130 int declick = get_transport_declick_required();
131 bool rec_monitors = get_rec_monitors_input();
132 boost::shared_ptr<RouteList> r = routes.reader ();
134 if (transport_sub_state & StopPendingCapture) {
135 /* force a declick out */
139 record_active = actively_recording(); // || (get_record_enabled() && get_punch_in());
141 const nframes_t start_frame = _transport_frame;
142 const nframes_t end_frame = _transport_frame + (nframes_t)floor(nframes * _transport_speed);
144 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
148 if ((*i)->is_hidden()) {
152 (*i)->set_pending_declick (declick);
154 if ((ret = (*i)->roll (nframes, start_frame, end_frame, declick, record_active, rec_monitors, need_butler)) < 0) {
164 Session::silent_process_routes (nframes_t nframes, bool& need_butler)
166 bool record_active = actively_recording();
167 int declick = get_transport_declick_required();
168 bool rec_monitors = get_rec_monitors_input();
169 boost::shared_ptr<RouteList> r = routes.reader ();
171 if (transport_sub_state & StopPendingCapture) {
172 /* force a declick out */
176 const nframes_t start_frame = _transport_frame;
177 const nframes_t end_frame = _transport_frame + lrintf(nframes * _transport_speed);
179 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
183 if ((*i)->is_hidden()) {
187 if ((ret = (*i)->silent_roll (nframes, start_frame, end_frame, record_active, rec_monitors, need_butler)) < 0) {
197 Session::get_diskstream_statistics ()
202 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
203 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
205 if ((*i)->hidden()) {
209 pworst = min (pworst, (*i)->playback_buffer_load());
210 cworst = min (cworst, (*i)->capture_buffer_load());
213 uint32_t pmin = g_atomic_int_get (&_playback_load);
214 uint32_t pminold = g_atomic_int_get (&_playback_load_min);
215 uint32_t cmin = g_atomic_int_get (&_capture_load);
216 uint32_t cminold = g_atomic_int_get (&_capture_load_min);
218 g_atomic_int_set (&_playback_load, (uint32_t) floor (pworst * 100.0f));
219 g_atomic_int_set (&_capture_load, (uint32_t) floor (cworst * 100.0f));
220 g_atomic_int_set (&_playback_load_min, min (pmin, pminold));
221 g_atomic_int_set (&_capture_load_min, min (cmin, cminold));
223 if (actively_recording()) {
228 /** Process callback used when the auditioner is not active */
230 Session::process_with_events (nframes_t nframes)
233 nframes_t this_nframes;
235 bool session_needs_butler = false;
236 nframes_t stop_limit;
239 /* make sure the auditioner is silent */
242 auditioner->silence (nframes);
245 /* handle any pending events */
247 while (pending_events.read (&ev, 1) == 1) {
251 /* if we are not in the middle of a state change,
252 and there are immediate events queued up,
256 while (!non_realtime_work_pending() && !immediate_events.empty()) {
257 SessionEvent *ev = immediate_events.front ();
258 immediate_events.pop_front ();
262 /* Events caused a transport change, send an MTC Full Frame (Timecode) message.
263 * This is sent whether rolling or not, to give slaves an idea of ardour time
264 * on locates (and allow slow slaves to position and prepare for rolling)
266 if (_send_timecode_update) {
267 send_full_time_code(nframes);
268 deliver_mmc (MIDI::MachineControl::cmdLocate, _transport_frame);
271 if (!process_can_proceed()) {
276 if (events.empty() || next_event == events.end()) {
277 process_without_events (nframes);
281 if (_transport_speed == 1.0) {
282 frames_moved = (long) nframes;
284 interpolation.set_target_speed (fabs(_target_transport_speed));
285 interpolation.set_speed (fabs(_transport_speed));
286 frames_moved = (long) interpolation.interpolate (0, nframes, 0, 0);
289 end_frame = _transport_frame + (nframes_t)frames_moved;
292 SessionEvent* this_event;
293 Events::iterator the_next_one;
295 if (!process_can_proceed()) {
300 if (!_exporting && _slave) {
301 if (!follow_slave (nframes)) {
306 if (_transport_speed == 0) {
312 send_midi_time_code_for_cycle (nframes);
315 if (actively_recording()) {
316 stop_limit = max_frames;
319 if (Config->get_stop_at_session_end()) {
320 stop_limit = current_end_frame();
322 stop_limit = max_frames;
326 if (maybe_stop (stop_limit)) {
331 this_event = *next_event;
332 the_next_one = next_event;
335 /* yes folks, here it is, the actual loop where we really truly
341 this_nframes = nframes; /* real (jack) time relative */
342 frames_moved = (long) floor (_transport_speed * nframes); /* transport relative */
344 /* running an event, position transport precisely to its time */
345 if (this_event && this_event->action_frame <= end_frame && this_event->action_frame >= _transport_frame) {
346 /* this isn't quite right for reverse play */
347 frames_moved = (long) (this_event->action_frame - _transport_frame);
348 this_nframes = (nframes_t) abs( floor(frames_moved / _transport_speed) );
353 click (_transport_frame, this_nframes);
355 if (process_routes (this_nframes, session_needs_butler)) {
360 nframes -= this_nframes;
362 if (frames_moved < 0) {
363 decrement_transport_position (-frames_moved);
365 increment_transport_position (frames_moved);
368 maybe_stop (stop_limit);
369 check_declick_out ();
372 _engine.split_cycle (this_nframes);
374 /* now handle this event and all others scheduled for the same time */
376 while (this_event && this_event->action_frame == _transport_frame) {
377 process_event (this_event);
379 if (the_next_one == events.end()) {
382 this_event = *the_next_one;
387 /* if an event left our state changing, do the right thing */
389 if (nframes && non_realtime_work_pending()) {
394 /* this is necessary to handle the case of seamless looping */
395 end_frame = _transport_frame + (nframes_t) floor (nframes * _transport_speed);
401 } /* implicit release of route lock */
403 if (session_needs_butler) {
409 Session::reset_slave_state ()
411 average_slave_delta = 1800;
412 delta_accumulator_cnt = 0;
413 have_first_delta_accumulator = false;
414 _slave_state = Stopped;
418 Session::transport_locked () const
422 if (!locate_pending() && (!config.get_external_sync() || (sl && sl->ok() && sl->locked()))) {
430 Session::follow_slave (nframes_t nframes)
433 nframes64_t slave_transport_frame;
434 nframes_t this_delta;
439 config.set_external_sync (false);
443 _slave->speed_and_position (slave_speed, slave_transport_frame);
445 DEBUG_TRACE (DEBUG::Slave, string_compose ("Slave position %1 speed %2\n", slave_transport_frame, slave_speed));
447 if (!_slave->locked()) {
448 DEBUG_TRACE (DEBUG::Slave, "slave not locked\n");
452 if (slave_transport_frame > _transport_frame) {
453 this_delta = slave_transport_frame - _transport_frame;
456 this_delta = _transport_frame - slave_transport_frame;
460 if (_slave->starting()) {
464 if (_slave->is_always_synced() || config.get_timecode_source_is_synced()) {
466 /* if the TC source is synced, then we assume that its
467 speed is binary: 0.0 or 1.0
470 if (slave_speed != 0.0f) {
476 /* if we are chasing and the average delta between us and the
477 master gets too big, we want to switch to silent
478 motion. so keep track of that here.
481 if (_slave_state == Running) {
482 calculate_moving_average_of_slave_delta(dir, this_delta);
486 track_slave_state (slave_speed, slave_transport_frame, this_delta);
488 DEBUG_TRACE (DEBUG::Slave, string_compose ("slave state %1 @ %2 speed %3 cur delta %4 avg delta %5\n",
489 _slave_state, slave_transport_frame, slave_speed, this_delta, average_slave_delta));
492 if (_slave_state == Running && !_slave->is_always_synced() && !config.get_timecode_source_is_synced()) {
494 if (_transport_speed != 0.0f) {
497 note that average_dir is +1 or -1
502 if (average_slave_delta == 0) {
506 delta = average_slave_delta;
507 delta *= average_dir;
511 if (slave_speed != 0.0) {
512 DEBUG_TRACE (DEBUG::Slave, string_compose ("delta = %1 speed = %2 ts = %3 M@%4 S@%5 avgdelta %6\n",
513 (int) (dir * this_delta),
517 slave_transport_frame,
518 average_slave_delta));
522 if (_slave->give_slave_full_control_over_transport_speed()) {
523 set_transport_speed (slave_speed, false, false);
524 //std::cout << "set speed = " << slave_speed << "\n";
526 float adjusted_speed = slave_speed + (1.5 * (delta / float(_current_frame_rate)));
527 request_transport_speed (adjusted_speed);
528 DEBUG_TRACE (DEBUG::Slave, string_compose ("adjust using %1 towards %2 ratio %3 current %4 slave @ %5\n",
529 delta, adjusted_speed, adjusted_speed/slave_speed, _transport_speed,
534 if ((nframes_t) abs(average_slave_delta) > _slave->resolution()) {
535 cerr << "average slave delta greater than slave resolution (" << _slave->resolution() << "), going to silent motion\n";
543 if (_slave_state == Running && !non_realtime_work_pending()) {
544 /* speed is set, we're locked, and good to go */
549 DEBUG_TRACE (DEBUG::Slave, "silent motion\n")
550 follow_slave_silently (nframes, slave_speed);
553 /* don't move at all */
554 DEBUG_TRACE (DEBUG::Slave, "no roll\n")
560 Session::calculate_moving_average_of_slave_delta(int dir, nframes_t this_delta)
562 if (delta_accumulator_cnt >= delta_accumulator_size) {
563 have_first_delta_accumulator = true;
564 delta_accumulator_cnt = 0;
567 if (delta_accumulator_cnt != 0 || this_delta < _current_frame_rate) {
568 delta_accumulator[delta_accumulator_cnt++] = long(dir) * long(this_delta);
571 if (have_first_delta_accumulator) {
572 average_slave_delta = 0L;
573 for (int i = 0; i < delta_accumulator_size; ++i) {
574 average_slave_delta += delta_accumulator[i];
576 average_slave_delta /= long(delta_accumulator_size);
577 if (average_slave_delta < 0L) {
579 average_slave_delta = abs(average_slave_delta);
587 Session::track_slave_state (float slave_speed, nframes_t slave_transport_frame, nframes_t this_delta)
589 if (slave_speed != 0.0f) {
591 /* slave is running */
593 switch (_slave_state) {
595 if (_slave->requires_seekahead()) {
596 slave_wait_end = slave_transport_frame + _slave->seekahead_distance ();
597 DEBUG_TRACE (DEBUG::Slave, string_compose ("slave stopped, but running, requires seekahead to %1\n", slave_wait_end));
598 /* we can call locate() here because we are in process context */
599 locate (slave_wait_end, false, false);
600 _slave_state = Waiting;
604 _slave_state = Running;
606 Location* al = _locations.auto_loop_location();
608 if (al && play_loop && (slave_transport_frame < al->start() || slave_transport_frame > al->end())) {
610 request_play_loop(false);
613 if (slave_transport_frame != _transport_frame) {
614 locate (slave_transport_frame, false, false);
624 if (_slave_state == Waiting) {
626 DEBUG_TRACE (DEBUG::Slave, string_compose ("slave waiting at %1\n", slave_transport_frame));
628 if (slave_transport_frame >= slave_wait_end) {
630 DEBUG_TRACE (DEBUG::Slave, string_compose ("slave start at %1 vs %2\n", slave_transport_frame, _transport_frame));
632 _slave_state = Running;
634 /* now perform a "micro-seek" within the disk buffers to realign ourselves
635 precisely with the master.
640 nframes_t frame_delta = slave_transport_frame - _transport_frame;
642 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
644 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
645 if (!(*i)->can_internal_playback_seek (frame_delta)) {
652 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
653 (*i)->internal_playback_seek (frame_delta);
655 _transport_frame += frame_delta;
658 cerr << "cannot micro-seek\n";
662 memset (delta_accumulator, 0, sizeof (long) * delta_accumulator_size);
663 average_slave_delta = 0L;
668 if (_slave_state == Running && _transport_speed == 0.0f) {
669 DEBUG_TRACE (DEBUG::Slave, "slave starts transport\n");
673 } else { // slave_speed is 0
675 /* slave has stopped */
677 if (_transport_speed != 0.0f) {
678 DEBUG_TRACE (DEBUG::Slave, string_compose ("slave stops transport: %1 frame %2 tf %3\n", slave_speed, slave_transport_frame, _transport_frame));
682 if (slave_transport_frame != _transport_frame) {
683 DEBUG_TRACE (DEBUG::Slave, string_compose ("slave stopped, move to %1\n", slave_transport_frame));
684 force_locate (slave_transport_frame, false);
687 _slave_state = Stopped;
692 Session::follow_slave_silently (nframes_t nframes, float slave_speed)
694 if (slave_speed && _transport_speed) {
696 /* something isn't right, but we should move with the master
702 silent_process_routes (nframes, need_butler);
708 int32_t frames_moved = (int32_t) floor (_transport_speed * nframes);
710 if (frames_moved < 0) {
711 decrement_transport_position (-frames_moved);
713 increment_transport_position (frames_moved);
716 nframes_t stop_limit;
718 if (actively_recording()) {
719 stop_limit = max_frames;
721 if (Config->get_stop_at_session_end()) {
722 stop_limit = current_end_frame();
724 stop_limit = max_frames;
728 maybe_stop (stop_limit);
733 Session::process_without_events (nframes_t nframes)
735 bool session_needs_butler = false;
736 nframes_t stop_limit;
739 if (!process_can_proceed()) {
744 if (!_exporting && _slave) {
745 if (!follow_slave (nframes)) {
750 if (_transport_speed == 0) {
756 send_midi_time_code_for_cycle (nframes);
759 if (actively_recording()) {
760 stop_limit = max_frames;
762 if (Config->get_stop_at_session_end()) {
763 stop_limit = current_end_frame();
765 stop_limit = max_frames;
769 if (maybe_stop (stop_limit)) {
774 if (maybe_sync_start (nframes)) {
778 click (_transport_frame, nframes);
780 if (_transport_speed == 1.0) {
781 frames_moved = (long) nframes;
783 interpolation.set_target_speed (fabs(_target_transport_speed));
784 interpolation.set_speed (fabs(_transport_speed));
785 frames_moved = (long) interpolation.interpolate (0, nframes, 0, 0);
788 if (process_routes (nframes, session_needs_butler)) {
793 if (frames_moved < 0) {
794 decrement_transport_position (-frames_moved);
796 increment_transport_position (frames_moved);
799 maybe_stop (stop_limit);
800 check_declick_out ();
802 if (session_needs_butler) {
807 /** Process callback used when the auditioner is active.
808 * @param nframes number of frames to process.
811 Session::process_audition (nframes_t nframes)
814 boost::shared_ptr<RouteList> r = routes.reader ();
816 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
817 if (!(*i)->is_hidden()) {
818 (*i)->silence (nframes);
822 /* run the auditioner, and if it says we need butler service, ask for it */
824 if (auditioner->play_audition (nframes) > 0) {
828 /* if using a monitor section, run it because otherwise we don't hear anything */
830 if (auditioner->needs_monitor()) {
831 _monitor_out->passthru (_transport_frame, _transport_frame + nframes, nframes, false);
834 /* handle pending events */
836 while (pending_events.read (&ev, 1) == 1) {
840 /* if we are not in the middle of a state change,
841 and there are immediate events queued up,
845 while (!non_realtime_work_pending() && !immediate_events.empty()) {
846 SessionEvent *ev = immediate_events.front ();
847 immediate_events.pop_front ();
851 if (!auditioner->auditioning()) {
852 /* auditioner no longer active, so go back to the normal process callback */
853 process_function = &Session::process_with_events;
858 Session::maybe_sync_start (nframes_t& nframes)
860 nframes_t sync_offset;
862 if (!waiting_for_sync_offset) {
866 if (_engine.get_sync_offset (sync_offset) && sync_offset < nframes) {
868 /* generate silence up to the sync point, then
869 adjust nframes + offset to reflect whatever
873 no_roll (sync_offset);
874 nframes -= sync_offset;
875 Port::increment_port_offset (sync_offset);
876 waiting_for_sync_offset = false;
879 return true; // done, nothing left to process
884 /* sync offset point is not within this process()
885 cycle, so just generate silence. and don't bother
886 with any fancy stuff here, just the minimal silence.
891 if (Config->get_locate_while_waiting_for_sync()) {
892 if (micro_locate (nframes)) {
893 /* XXX ERROR !!! XXX */
897 return true; // done, nothing left to process
904 Session::queue_event (SessionEvent* ev)
906 if (_state_of_the_state & Deletion) {
908 } else if (_state_of_the_state & Loading) {
911 pending_events.write (&ev, 1);
916 Session::set_next_event ()
918 if (events.empty()) {
919 next_event = events.end();
923 if (next_event == events.end()) {
924 next_event = events.begin();
927 if ((*next_event)->action_frame > _transport_frame) {
928 next_event = events.begin();
931 for (; next_event != events.end(); ++next_event) {
932 if ((*next_event)->action_frame >= _transport_frame) {
939 Session::process_event (SessionEvent* ev)
944 /* if we're in the middle of a state change (i.e. waiting
945 for the butler thread to complete the non-realtime
946 part of the change), we'll just have to queue this
947 event for a time when the change is complete.
950 if (non_realtime_work_pending()) {
952 /* except locates, which we have the capability to handle */
954 if (ev->type != SessionEvent::Locate) {
955 immediate_events.insert (immediate_events.end(), ev);
961 DEBUG_TRACE (DEBUG::SessionEvents, string_compose ("Processing event: %1 @ %2\n", enum_2_string (ev->type), _transport_frame));
964 case SessionEvent::SetLoop:
965 set_play_loop (ev->yes_or_no);
968 case SessionEvent::AutoLoop:
970 start_locate (ev->target_frame, true, false, Config->get_seamless_loop());
976 case SessionEvent::Locate:
978 // cerr << "forced locate to " << ev->target_frame << endl;
979 locate (ev->target_frame, false, true, false);
981 // cerr << "soft locate to " << ev->target_frame << endl;
982 start_locate (ev->target_frame, false, true, false);
984 _send_timecode_update = true;
987 case SessionEvent::LocateRoll:
989 // cerr << "forced locate to+roll " << ev->target_frame << endl;
990 locate (ev->target_frame, true, true, false);
992 // cerr << "soft locate to+roll " << ev->target_frame << endl;
993 start_locate (ev->target_frame, true, true, false);
995 _send_timecode_update = true;
998 case SessionEvent::LocateRollLocate:
999 // locate is handled by ::request_roll_at_and_return()
1000 _requested_return_frame = ev->target_frame;
1001 request_locate (ev->target2_frame, true);
1005 case SessionEvent::SetTransportSpeed:
1006 set_transport_speed (ev->speed, ev->yes_or_no, ev->second_yes_or_no);
1009 case SessionEvent::PunchIn:
1010 // cerr << "PunchIN at " << transport_frame() << endl;
1011 if (config.get_punch_in() && record_status() == Enabled) {
1018 case SessionEvent::PunchOut:
1019 // cerr << "PunchOUT at " << transport_frame() << endl;
1020 if (config.get_punch_out()) {
1021 step_back_from_record ();
1027 case SessionEvent::StopOnce:
1028 if (!non_realtime_work_pending()) {
1029 stop_transport (ev->yes_or_no);
1030 _clear_event_type (SessionEvent::StopOnce);
1036 case SessionEvent::RangeStop:
1037 if (!non_realtime_work_pending()) {
1038 stop_transport (ev->yes_or_no);
1044 case SessionEvent::RangeLocate:
1045 start_locate (ev->target_frame, true, true, false);
1050 case SessionEvent::Overwrite:
1051 overwrite_some_buffers (static_cast<Diskstream*>(ev->ptr));
1054 case SessionEvent::SetDiskstreamSpeed:
1055 set_diskstream_speed (static_cast<Diskstream*> (ev->ptr), ev->speed);
1058 case SessionEvent::SetSyncSource:
1059 use_sync_source (ev->slave);
1062 case SessionEvent::Audition:
1063 set_audition (ev->region);
1064 // drop reference to region
1065 ev->region.reset ();
1068 case SessionEvent::InputConfigurationChange:
1069 add_post_transport_work (PostTransportInputChange);
1070 _butler->schedule_transport_work ();
1073 case SessionEvent::SetPlayAudioRange:
1074 set_play_range (ev->audio_range, (ev->speed == 1.0f));
1077 case SessionEvent::RealTimeOperation:
1079 del = false; // other side of RT request needs to clean up
1083 fatal << string_compose(_("Programming error: illegal event type in process_event (%1)"), ev->type) << endmsg;
1089 del = del && !_remove_event (ev);