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.
26 #include <pbd/error.h>
28 #include <glibmm/thread.h>
30 #include <ardour/ardour.h>
31 #include <ardour/session.h>
32 #include <ardour/timestamps.h>
33 #include <ardour/audio_diskstream.h>
34 #include <ardour/audioengine.h>
35 #include <ardour/slave.h>
36 #include <ardour/auditioner.h>
37 #include <ardour/cycles.h>
38 #include <ardour/cycle_timer.h>
40 #include <midi++/manager.h>
44 using namespace ARDOUR;
49 Session::process (jack_nframes_t nframes)
51 MIDI::Manager::instance()->cycle_start(nframes);
53 if (synced_to_jack() && waiting_to_start) {
54 if ( _engine.transport_state() == AudioEngine::TransportRolling) {
55 actually_start_transport ();
59 if (non_realtime_work_pending()) {
60 if (g_atomic_int_get (&butler_should_do_transport_work) == 0) {
65 (this->*process_function) (nframes);
67 MIDI::Manager::instance()->cycle_end();
69 SendFeedback (); /* EMIT SIGNAL */
73 Session::prepare_diskstreams ()
75 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
76 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
82 Session::no_roll (jack_nframes_t nframes, jack_nframes_t offset)
84 jack_nframes_t end_frame = _transport_frame + nframes; // FIXME: varispeed + no_roll ??
86 bool declick = get_transport_declick_required();
87 boost::shared_ptr<RouteList> r = routes.reader ();
90 _click_io->silence (nframes, offset);
93 if (g_atomic_int_get (&processing_prohibited)) {
94 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
95 (*i)->silence (nframes, offset);
100 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
102 if ((*i)->hidden()) {
106 (*i)->set_pending_declick (declick);
108 if ((*i)->no_roll (nframes, _transport_frame, end_frame, offset, non_realtime_work_pending(),
109 actively_recording(), declick)) {
110 error << string_compose(_("Session: error in no roll for %1"), (*i)->name()) << endmsg;
120 Session::process_routes (jack_nframes_t nframes, jack_nframes_t offset)
123 int declick = get_transport_declick_required();
124 bool rec_monitors = get_rec_monitors_input();
125 boost::shared_ptr<RouteList> r = routes.reader ();
127 if (transport_sub_state & StopPendingCapture) {
128 /* force a declick out */
132 record_active = actively_recording(); // || (get_record_enabled() && get_punch_in());
134 const jack_nframes_t start_frame = _transport_frame;
135 const jack_nframes_t end_frame = _transport_frame + (jack_nframes_t)floor(nframes * _transport_speed);
137 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
141 if ((*i)->hidden()) {
145 (*i)->set_pending_declick (declick);
147 if ((ret = (*i)->roll (nframes, start_frame, end_frame, offset, declick, record_active, rec_monitors)) < 0) {
149 /* we have to do this here. Route::roll() for an AudioTrack will have called AudioDiskstream::process(),
150 and the DS will expect AudioDiskstream::commit() to be called. but we're aborting from that
151 call path, so make sure we release any outstanding locks here before we return failure.
154 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
155 for (DiskstreamList::iterator ids = dsl->begin(); ids != dsl->end(); ++ids) {
168 Session::silent_process_routes (jack_nframes_t nframes, jack_nframes_t offset)
170 bool record_active = actively_recording();
171 int declick = get_transport_declick_required();
172 bool rec_monitors = get_rec_monitors_input();
173 boost::shared_ptr<RouteList> r = routes.reader ();
175 if (transport_sub_state & StopPendingCapture) {
176 /* force a declick out */
180 const jack_nframes_t start_frame = _transport_frame;
181 const jack_nframes_t end_frame = _transport_frame + lrintf(nframes * _transport_speed);
183 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
187 if ((*i)->hidden()) {
191 if ((ret = (*i)->silent_roll (nframes, start_frame, end_frame, offset, record_active, rec_monitors)) < 0) {
193 /* we have to do this here. Route::roll() for an AudioTrack will have called AudioDiskstream::process(),
194 and the DS will expect AudioDiskstream::commit() to be called. but we're aborting from that
195 call path, so make sure we release any outstanding locks here before we return failure.
198 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
199 for (DiskstreamList::iterator ids = dsl->begin(); ids != dsl->end(); ++ids) {
212 Session::commit_diskstreams (jack_nframes_t nframes, bool &needs_butler)
218 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
219 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
221 if ((*i)->hidden()) {
225 /* force all diskstreams not handled by a Route to call do their stuff.
228 if ((dret = (*i)->process (_transport_frame, nframes, 0, actively_recording(), get_rec_monitors_input())) == 0) {
229 if ((*i)->commit (nframes)) {
233 } else if (dret < 0) {
237 pworst = min (pworst, (*i)->playback_buffer_load());
238 cworst = min (cworst, (*i)->capture_buffer_load());
241 uint32_t pmin = g_atomic_int_get (&_playback_load);
242 uint32_t pminold = g_atomic_int_get (&_playback_load_min);
243 uint32_t cmin = g_atomic_int_get (&_capture_load);
244 uint32_t cminold = g_atomic_int_get (&_capture_load_min);
246 g_atomic_int_set (&_playback_load, (uint32_t) floor (pworst * 100.0f));
247 g_atomic_int_set (&_capture_load, (uint32_t) floor (cworst * 100.0f));
248 g_atomic_int_set (&_playback_load_min, min (pmin, pminold));
249 g_atomic_int_set (&_capture_load_min, min (cmin, cminold));
251 if (actively_recording()) {
258 Session::process_with_events (jack_nframes_t nframes)
261 jack_nframes_t this_nframes;
262 jack_nframes_t end_frame;
263 jack_nframes_t offset;
264 jack_nframes_t stop_limit;
266 bool session_needs_butler = false;
268 /* make sure the auditioner is silent */
271 auditioner->silence (nframes, 0);
274 /* handle any pending events */
276 while (pending_events.read (&ev, 1) == 1) {
280 /* if we are not in the middle of a state change,
281 and there are immediate events queued up,
285 while (!non_realtime_work_pending() && !immediate_events.empty()) {
286 Event *ev = immediate_events.front ();
287 immediate_events.pop_front ();
291 /* Events caused a transport change, send an MTC Full Frame (SMPTE) message.
292 * This is sent whether rolling or not, to give slaves an idea of ardour time
293 * on locates (and allow slow slaves to position and prepare for rolling)
295 if (_send_smpte_update) {
296 send_full_time_code(nframes);
299 if (!process_can_proceed()) {
300 no_roll (nframes, 0);
304 if (events.empty() || next_event == events.end()) {
305 process_without_events (nframes);
309 end_frame = _transport_frame + nframes;
313 Events::iterator the_next_one;
315 if (post_transport_work & (PostTransportLocate|PostTransportStop)) {
316 no_roll (nframes, 0);
320 if (!_exporting && _slave) {
321 if (!follow_slave (nframes, 0)) {
326 if (_transport_speed == 0) {
327 no_roll (nframes, 0);
331 send_midi_time_code_for_cycle(nframes);
333 if (actively_recording()) {
334 stop_limit = max_frames;
337 if (Config->get_stop_at_session_end()) {
338 stop_limit = current_end_frame();
340 stop_limit = max_frames;
344 if (maybe_stop (stop_limit)) {
345 no_roll (nframes, 0);
349 this_event = *next_event;
350 the_next_one = next_event;
357 if (this_event == 0 || this_event->action_frame > end_frame || this_event->action_frame < _transport_frame) {
359 this_nframes = nframes;
363 /* compute nframes to next event */
365 if (this_event->action_frame < end_frame) {
366 this_nframes = nframes - (end_frame - this_event->action_frame);
368 this_nframes = nframes;
374 click (_transport_frame, nframes, offset);
376 /* now process frames between now and the first event in this block */
377 prepare_diskstreams ();
379 if (process_routes (this_nframes, offset)) {
380 no_roll (nframes, 0);
384 commit_diskstreams (this_nframes, session_needs_butler);
386 nframes -= this_nframes;
387 offset += this_nframes;
389 frames_moved = (jack_nframes_t) floor (_transport_speed * this_nframes);
391 if (frames_moved < 0) {
392 decrement_transport_position (-frames_moved);
394 increment_transport_position (frames_moved);
397 maybe_stop (stop_limit);
398 check_declick_out ();
401 /* now handle this event and all others scheduled for the same time */
403 while (this_event && this_event->action_frame == _transport_frame) {
404 process_event (this_event);
406 if (the_next_one == events.end()) {
409 this_event = *the_next_one;
414 /* if an event left our state changing, do the right thing */
416 if (non_realtime_work_pending()) {
417 no_roll (nframes, offset);
421 /* this is necessary to handle the case of seamless looping */
422 /* not sure if it will work in conjuction with varispeed */
423 end_frame = _transport_frame + nframes;
429 } /* implicit release of route lock */
431 if (session_needs_butler)
436 Session::reset_slave_state ()
438 average_slave_delta = 1800;
439 delta_accumulator_cnt = 0;
440 have_first_delta_accumulator = false;
441 slave_state = Stopped;
445 Session::transport_locked () const
449 if (!locate_pending() && ((_slave_type == None) || (sl && sl->ok() && sl->locked()))) {
457 Session::follow_slave (jack_nframes_t nframes, jack_nframes_t offset)
460 jack_nframes_t slave_transport_frame;
461 jack_nframes_t this_delta;
467 set_slave_source (None, 0);
471 _slave->speed_and_position (slave_speed, slave_transport_frame);
473 if (!_slave->locked()) {
477 if (slave_transport_frame > _transport_frame) {
478 this_delta = slave_transport_frame - _transport_frame;
481 this_delta = _transport_frame - slave_transport_frame;
485 if ((starting = _slave->starting())) {
490 cerr << "delta = " << (int) (dir * this_delta)
491 << " speed = " << slave_speed
492 << " ts = " << _transport_speed
493 << " M@"<< slave_transport_frame << " S@" << _transport_frame
494 << " avgdelta = " << average_slave_delta
498 if (Config->get_timecode_source_is_synced()) {
500 /* if the TC source is synced, then we assume that its
501 speed is binary: 0.0 or 1.0
504 if (slave_speed != 0.0f) {
510 /* TC source is able to drift relative to us (slave)
511 so we need to keep track of the drift and adjust
512 our speed to remain locked.
515 if (delta_accumulator_cnt >= delta_accumulator_size) {
516 have_first_delta_accumulator = true;
517 delta_accumulator_cnt = 0;
520 if (delta_accumulator_cnt != 0 || this_delta < _current_frame_rate) {
521 delta_accumulator[delta_accumulator_cnt++] = dir*this_delta;
524 if (have_first_delta_accumulator) {
525 average_slave_delta = 0;
526 for (int i = 0; i < delta_accumulator_size; ++i) {
527 average_slave_delta += delta_accumulator[i];
529 average_slave_delta /= delta_accumulator_size;
530 if (average_slave_delta < 0) {
532 average_slave_delta = -average_slave_delta;
536 // cerr << "avgdelta = " << average_slave_delta*average_dir << endl;
540 if (slave_speed != 0.0f) {
542 /* slave is running */
544 switch (slave_state) {
546 if (_slave->requires_seekahead()) {
547 slave_wait_end = slave_transport_frame + _current_frame_rate;
548 locate (slave_wait_end, false, false);
549 slave_state = Waiting;
554 slave_state = Running;
556 Location* al = _locations.auto_loop_location();
558 if (al && auto_loop && (slave_transport_frame < al->start() || slave_transport_frame > al->end())) {
560 request_auto_loop(false);
563 if (slave_transport_frame != _transport_frame) {
564 locate (slave_transport_frame, false, false);
577 if (slave_state == Waiting) {
579 // cerr << "waiting at " << slave_transport_frame << endl;
580 if (slave_transport_frame >= slave_wait_end) {
581 // cerr << "\tstart at " << _transport_frame << endl;
583 slave_state = Running;
586 jack_nframes_t frame_delta = slave_transport_frame - _transport_frame;
588 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
590 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
591 if (!(*i)->can_internal_playback_seek (frame_delta)) {
598 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
599 (*i)->internal_playback_seek (frame_delta);
601 _transport_frame += frame_delta;
604 // cerr << "cannot micro-seek\n";
608 memset (delta_accumulator, 0, sizeof (jack_nframes_t) * delta_accumulator_size);
609 average_slave_delta = 0;
614 if (slave_state == Running && _transport_speed == 0.0f) {
616 // cerr << "slave starts transport\n";
623 /* slave has stopped */
625 if (_transport_speed != 0.0f) {
627 // cerr << "slave stops transport: " << slave_speed << " frame: " << slave_transport_frame
628 // << " tf = " << _transport_frame
631 if (_slave_type == JACK) {
632 last_stop_frame = _transport_frame;
638 if (slave_transport_frame != _transport_frame) {
639 // cerr << "slave stopped, move to " << slave_transport_frame << endl;
640 force_locate (slave_transport_frame, false);
643 slave_state = Stopped;
646 if (slave_state == Running && !Config->get_timecode_source_is_synced()) {
649 if (_transport_speed != 0.0f) {
652 note that average_dir is +1 or -1
655 const float adjust_seconds = 1.0f;
658 //if (average_slave_delta == 0) {
662 // delta = average_slave_delta;
663 // delta *= average_dir;
666 float adjusted_speed = slave_speed +
667 (delta / (adjust_seconds * _current_frame_rate));
669 // cerr << "adjust using " << delta
670 // << " towards " << adjusted_speed
671 // << " ratio = " << adjusted_speed / slave_speed
672 // << " current = " << _transport_speed
673 // << " slave @ " << slave_speed
676 request_transport_speed (adjusted_speed);
679 if ((jack_nframes_t) average_slave_delta > _slave->resolution()) {
680 // cerr << "not locked\n";
687 if (!starting && !non_realtime_work_pending()) {
688 /* speed is set, we're locked, and good to go */
694 if (slave_speed && _transport_speed) {
696 /* something isn't right, but we should move with the master
702 prepare_diskstreams ();
703 silent_process_routes (nframes, offset);
704 commit_diskstreams (nframes, need_butler);
710 int32_t frames_moved = (int32_t) floor (_transport_speed * nframes);
712 if (frames_moved < 0) {
713 decrement_transport_position (-frames_moved);
715 increment_transport_position (frames_moved);
718 jack_nframes_t stop_limit;
720 if (actively_recording()) {
721 stop_limit = max_frames;
723 if (Config->get_stop_at_session_end()) {
724 stop_limit = current_end_frame();
726 stop_limit = max_frames;
730 maybe_stop (stop_limit);
734 /* don't move at all */
735 no_roll (nframes, 0);
740 Session::process_without_events (jack_nframes_t nframes)
742 bool session_needs_butler = false;
743 jack_nframes_t stop_limit;
745 jack_nframes_t offset = 0;
748 if (post_transport_work & (PostTransportLocate|PostTransportStop)) {
749 no_roll (nframes, 0);
753 if (!_exporting && _slave) {
754 if (!follow_slave (nframes, 0)) {
759 if (_transport_speed == 0) {
760 no_roll (nframes, 0);
764 send_midi_time_code_for_cycle(nframes);
766 if (actively_recording()) {
767 stop_limit = max_frames;
769 if (Config->get_stop_at_session_end()) {
770 stop_limit = current_end_frame();
772 stop_limit = max_frames;
776 if (maybe_stop (stop_limit)) {
777 no_roll (nframes, 0);
781 if (maybe_sync_start (nframes, offset)) {
785 click (_transport_frame, nframes, offset);
787 prepare_diskstreams ();
789 frames_moved = (long) floor (_transport_speed * nframes);
791 if (process_routes (nframes, offset)) {
792 no_roll (nframes, offset);
796 commit_diskstreams (nframes, session_needs_butler);
798 if (frames_moved < 0) {
799 decrement_transport_position (-frames_moved);
801 increment_transport_position (frames_moved);
804 maybe_stop (stop_limit);
805 check_declick_out ();
807 } /* implicit release of route lock */
809 if (session_needs_butler)
814 Session::process_audition (jack_nframes_t nframes)
817 boost::shared_ptr<RouteList> r = routes.reader ();
819 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
820 if (!(*i)->hidden()) {
821 (*i)->silence (nframes, 0);
825 /* run the auditioner, and if it says we need butler service, ask for it */
827 if (auditioner->play_audition (nframes) > 0) {
831 /* handle pending events */
833 while (pending_events.read (&ev, 1) == 1) {
837 /* if we are not in the middle of a state change,
838 and there are immediate events queued up,
842 while (!non_realtime_work_pending() && !immediate_events.empty()) {
843 Event *ev = immediate_events.front ();
844 immediate_events.pop_front ();
848 if (!auditioner->active()) {
849 process_function = &Session::process_with_events;
854 Session::maybe_sync_start (jack_nframes_t& nframes, jack_nframes_t& offset)
856 jack_nframes_t sync_offset;
858 if (!waiting_for_sync_offset) {
862 if (_engine.get_sync_offset (sync_offset) && sync_offset < nframes) {
864 no_roll (sync_offset, 0);
865 nframes -= sync_offset;
866 offset += sync_offset;
867 waiting_for_sync_offset = false;
874 no_roll (nframes, 0);