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>
42 using namespace ARDOUR;
47 Session::process (jack_nframes_t nframes)
49 if (synced_to_jack() && waiting_to_start) {
50 if ( _engine.transport_state() == AudioEngine::TransportRolling) {
51 actually_start_transport ();
55 if (non_realtime_work_pending()) {
56 if (g_atomic_int_get (&butler_should_do_transport_work) == 0) {
61 (this->*process_function) (nframes);
65 Session::prepare_diskstreams ()
67 for (AudioDiskstreamList::iterator i = audio_diskstreams.begin(); i != audio_diskstreams.end(); ++i) {
73 Session::no_roll (jack_nframes_t nframes, jack_nframes_t offset)
75 jack_nframes_t end_frame = _transport_frame + nframes;
77 bool declick = get_transport_declick_required();
80 _click_io->silence (nframes, offset);
83 /* XXX we're supposed to have the route_lock while doing this.
84 this is really bad ...
87 if (g_atomic_int_get (&processing_prohibited)) {
88 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
89 (*i)->silence (nframes, offset);
94 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
100 (*i)->set_pending_declick (declick);
102 if ((*i)->no_roll (nframes, _transport_frame, end_frame, offset, non_realtime_work_pending(),
103 actively_recording(), declick)) {
104 error << string_compose(_("Session: error in no roll for %1"), (*i)->name()) << endmsg;
114 Session::process_routes (jack_nframes_t nframes, jack_nframes_t offset)
117 int declick = get_transport_declick_required();
118 bool rec_monitors = get_rec_monitors_input();
120 if (transport_sub_state & StopPendingCapture) {
121 /* force a declick out */
125 record_active = actively_recording(); // || (get_record_enabled() && get_punch_in());
127 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
131 if ((*i)->hidden()) {
135 (*i)->set_pending_declick (declick);
137 if ((ret = (*i)->roll (nframes, _transport_frame, _transport_frame + nframes, offset, declick, record_active, rec_monitors)) < 0) {
139 /* we have to do this here. Route::roll() for an AudioTrack will have called AudioDiskstream::process(),
140 and the DS will expect AudioDiskstream::commit() to be called. but we're aborting from that
141 call path, so make sure we release any outstanding locks here before we return failure.
144 for (AudioDiskstreamList::iterator ids = audio_diskstreams.begin(); ids != audio_diskstreams.end(); ++ids) {
157 Session::silent_process_routes (jack_nframes_t nframes, jack_nframes_t offset)
159 bool record_active = actively_recording();
160 int declick = get_transport_declick_required();
161 bool rec_monitors = get_rec_monitors_input();
163 if (transport_sub_state & StopPendingCapture) {
164 /* force a declick out */
168 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
172 if ((*i)->hidden()) {
176 if ((ret = (*i)->silent_roll (nframes, _transport_frame, _transport_frame + nframes, offset, record_active, rec_monitors)) < 0) {
178 /* we have to do this here. Route::roll() for an AudioTrack will have called AudioDiskstream::process(),
179 and the DS will expect AudioDiskstream::commit() to be called. but we're aborting from that
180 call path, so make sure we release any outstanding locks here before we return failure.
183 for (AudioDiskstreamList::iterator ids = audio_diskstreams.begin(); ids != audio_diskstreams.end(); ++ids) {
196 Session::commit_diskstreams (jack_nframes_t nframes, bool &needs_butler)
202 for (AudioDiskstreamList::iterator i = audio_diskstreams.begin(); i != audio_diskstreams.end(); ++i) {
204 if ((*i)->hidden()) {
208 /* force all diskstreams not handled by a Route to call do their stuff.
211 if ((dret = (*i)->process (_transport_frame, nframes, 0, actively_recording(), get_rec_monitors_input())) == 0) {
212 if ((*i)->commit (nframes)) {
216 } else if (dret < 0) {
220 pworst = min (pworst, (*i)->playback_buffer_load());
221 cworst = min (cworst, (*i)->capture_buffer_load());
224 uint32_t pmin = g_atomic_int_get (&_playback_load);
225 uint32_t pminold = g_atomic_int_get (&_playback_load_min);
226 uint32_t cmin = g_atomic_int_get (&_capture_load);
227 uint32_t cminold = g_atomic_int_get (&_capture_load_min);
229 g_atomic_int_set (&_playback_load, (uint32_t) floor (pworst * 100.0f));
230 g_atomic_int_set (&_capture_load, (uint32_t) floor (cworst * 100.0f));
231 g_atomic_int_set (&_playback_load_min, min (pmin, pminold));
232 g_atomic_int_set (&_capture_load_min, min (cmin, cminold));
234 if (actively_recording()) {
240 Session::process_with_events (jack_nframes_t nframes)
243 jack_nframes_t this_nframes;
244 jack_nframes_t end_frame;
245 jack_nframes_t offset;
246 bool session_needs_butler = false;
247 jack_nframes_t stop_limit;
251 auditioner->silence (nframes, 0);
254 while (pending_events.read (&ev, 1) == 1) {
258 /* if we are not in the middle of a state change,
259 and there are immediate events queued up,
263 while (!non_realtime_work_pending() && !immediate_events.empty()) {
264 Event *ev = immediate_events.front ();
265 immediate_events.pop_front ();
269 if (!process_can_proceed()) {
270 no_roll (nframes, 0);
274 if (events.empty() || next_event == events.end()) {
275 process_without_events (nframes);
279 end_frame = _transport_frame + nframes;
282 Glib::RWLock::ReaderLock rm (route_lock, Glib::TRY_LOCK);
283 Glib::RWLock::ReaderLock dsm (diskstream_lock, Glib::TRY_LOCK);
286 Events::iterator the_next_one;
288 if (!rm.locked() || !dsm.locked() || (post_transport_work & (PostTransportLocate|PostTransportStop))) {
289 no_roll (nframes, 0);
293 if (!_exporting && _slave) {
294 if (!follow_slave (nframes, 0)) {
299 if (_transport_speed == 0) {
300 no_roll (nframes, 0);
304 if (actively_recording()) {
305 stop_limit = max_frames;
308 if (Config->get_stop_at_session_end()) {
309 stop_limit = current_end_frame();
311 stop_limit = max_frames;
315 if (maybe_stop (stop_limit)) {
316 no_roll (nframes, 0);
320 this_event = *next_event;
321 the_next_one = next_event;
328 if (this_event == 0 || this_event->action_frame > end_frame || this_event->action_frame < _transport_frame) {
330 this_nframes = nframes;
334 /* compute nframes to next event */
336 if (this_event->action_frame < end_frame) {
337 this_nframes = nframes - (end_frame - this_event->action_frame);
339 this_nframes = nframes;
345 click (_transport_frame, nframes, offset);
347 /* now process frames between now and the first event in this block */
348 prepare_diskstreams ();
350 if (process_routes (this_nframes, offset)) {
351 no_roll (nframes, 0);
355 commit_diskstreams (this_nframes, session_needs_butler);
357 nframes -= this_nframes;
358 offset += this_nframes;
360 frames_moved = (jack_nframes_t) floor (_transport_speed * 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 /* now handle this event and all others scheduled for the same time */
374 while (this_event && this_event->action_frame == _transport_frame) {
375 process_event (this_event);
377 if (the_next_one == events.end()) {
380 this_event = *the_next_one;
385 /* if an event left our state changing, do the right thing */
387 if (non_realtime_work_pending()) {
388 no_roll (nframes, offset);
392 /* this is necessary to handle the case of seamless looping */
393 /* not sure if it will work in conjuction with varispeed */
394 end_frame = _transport_frame + nframes;
400 } /* implicit release of route lock */
403 if (session_needs_butler) {
407 if (!_engine.freewheeling() && send_mtc) {
408 send_midi_time_code_in_another_thread ();
415 Session::reset_slave_state ()
417 average_slave_delta = 1800;
418 delta_accumulator_cnt = 0;
419 have_first_delta_accumulator = false;
420 slave_state = Stopped;
424 Session::transport_locked () const
428 if (!locate_pending() && ((_slave_type == None) || (sl && sl->ok() && sl->locked()))) {
436 Session::follow_slave (jack_nframes_t nframes, jack_nframes_t offset)
439 jack_nframes_t slave_transport_frame;
440 jack_nframes_t this_delta;
446 set_slave_source (None, 0);
450 _slave->speed_and_position (slave_speed, slave_transport_frame);
452 if (!_slave->locked()) {
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 ((starting = _slave->starting())) {
469 cerr << "delta = " << (int) (dir * this_delta)
470 << " speed = " << slave_speed
471 << " ts = " << _transport_speed
472 << " M@"<< slave_transport_frame << " S@" << _transport_frame
473 << " avgdelta = " << average_slave_delta
477 if (Config->get_timecode_source_is_synced()) {
479 /* if the TC source is synced, then we assume that its
480 speed is binary: 0.0 or 1.0
483 if (slave_speed != 0.0f) {
489 /* TC source is able to drift relative to us (slave)
490 so we need to keep track of the drift and adjust
491 our speed to remain locked.
494 if (delta_accumulator_cnt >= delta_accumulator_size) {
495 have_first_delta_accumulator = true;
496 delta_accumulator_cnt = 0;
499 if (delta_accumulator_cnt != 0 || this_delta < _current_frame_rate) {
500 delta_accumulator[delta_accumulator_cnt++] = dir*this_delta;
503 if (have_first_delta_accumulator) {
504 average_slave_delta = 0;
505 for (int i = 0; i < delta_accumulator_size; ++i) {
506 average_slave_delta += delta_accumulator[i];
508 average_slave_delta /= delta_accumulator_size;
509 if (average_slave_delta < 0) {
511 average_slave_delta = -average_slave_delta;
515 // cerr << "avgdelta = " << average_slave_delta*average_dir << endl;
519 if (slave_speed != 0.0f) {
521 /* slave is running */
523 switch (slave_state) {
525 if (_slave->requires_seekahead()) {
526 slave_wait_end = slave_transport_frame + _current_frame_rate;
527 locate (slave_wait_end, false, false);
528 slave_state = Waiting;
533 slave_state = Running;
535 Location* al = _locations.auto_loop_location();
537 if (al && auto_loop && (slave_transport_frame < al->start() || slave_transport_frame > al->end())) {
539 request_auto_loop(false);
542 if (slave_transport_frame != _transport_frame) {
543 locate (slave_transport_frame, false, false);
556 if (slave_state == Waiting) {
558 // cerr << "waiting at " << slave_transport_frame << endl;
559 Glib::RWLock::ReaderLock dsm (diskstream_lock, Glib::TRY_LOCK);
561 if (dsm.locked() && slave_transport_frame >= slave_wait_end) {
562 // cerr << "\tstart at " << _transport_frame << endl;
564 slave_state = Running;
567 jack_nframes_t frame_delta = slave_transport_frame - _transport_frame;
569 for (AudioDiskstreamList::iterator i = audio_diskstreams.begin(); i != audio_diskstreams.end(); ++i) {
570 if (!(*i)->can_internal_playback_seek (frame_delta)) {
577 for (AudioDiskstreamList::iterator i = audio_diskstreams.begin(); i != audio_diskstreams.end(); ++i) {
578 (*i)->internal_playback_seek (frame_delta);
580 _transport_frame += frame_delta;
583 // cerr << "cannot micro-seek\n";
587 memset (delta_accumulator, 0, sizeof (jack_nframes_t) * delta_accumulator_size);
588 average_slave_delta = 0;
593 if (slave_state == Running && _transport_speed == 0.0f) {
595 // cerr << "slave starts transport\n";
602 /* slave has stopped */
604 if (_transport_speed != 0.0f) {
606 // cerr << "slave stops transport: " << slave_speed << " frame: " << slave_transport_frame
607 // << " tf = " << _transport_frame
610 if (_slave_type == JACK) {
611 last_stop_frame = _transport_frame;
617 if (slave_transport_frame != _transport_frame) {
618 // cerr << "slave stopped, move to " << slave_transport_frame << endl;
619 force_locate (slave_transport_frame, false);
622 slave_state = Stopped;
625 if (slave_state == Running && !Config->get_timecode_source_is_synced()) {
628 if (_transport_speed != 0.0f) {
631 note that average_dir is +1 or -1
634 const float adjust_seconds = 1.0f;
637 //if (average_slave_delta == 0) {
641 // delta = average_slave_delta;
642 // delta *= average_dir;
645 float adjusted_speed = slave_speed +
646 (delta / (adjust_seconds * _current_frame_rate));
648 // cerr << "adjust using " << delta
649 // << " towards " << adjusted_speed
650 // << " ratio = " << adjusted_speed / slave_speed
651 // << " current = " << _transport_speed
652 // << " slave @ " << slave_speed
655 request_transport_speed (adjusted_speed);
658 if ((jack_nframes_t) average_slave_delta > _slave->resolution()) {
659 // cerr << "not locked\n";
666 if (!starting && !non_realtime_work_pending()) {
667 /* speed is set, we're locked, and good to go */
673 if (slave_speed && _transport_speed) {
675 /* something isn't right, but we should move with the master
681 Glib::RWLock::ReaderLock dsm (diskstream_lock, Glib::TRY_LOCK);
687 prepare_diskstreams ();
688 silent_process_routes (nframes, offset);
689 commit_diskstreams (nframes, need_butler);
695 jack_nframes_t frames_moved = (long) floor (_transport_speed * nframes);
697 if (frames_moved < 0) {
698 decrement_transport_position (-frames_moved);
700 increment_transport_position (frames_moved);
703 jack_nframes_t stop_limit;
705 if (actively_recording()) {
706 stop_limit = max_frames;
708 if (Config->get_stop_at_session_end()) {
709 stop_limit = current_end_frame();
711 stop_limit = max_frames;
715 maybe_stop (stop_limit);
719 /* don't move at all */
720 no_roll (nframes, 0);
725 Session::process_without_events (jack_nframes_t nframes)
727 bool session_needs_butler = false;
728 jack_nframes_t stop_limit;
732 Glib::RWLock::ReaderLock rm (route_lock, Glib::TRY_LOCK);
733 Glib::RWLock::ReaderLock dsm (diskstream_lock, Glib::TRY_LOCK);
735 if (!rm.locked() || !dsm.locked() || (post_transport_work & (PostTransportLocate|PostTransportStop))) {
736 no_roll (nframes, 0);
740 if (!_exporting && _slave) {
741 if (!follow_slave (nframes, 0)) {
746 if (_transport_speed == 0) {
747 no_roll (nframes, 0);
751 if (actively_recording()) {
752 stop_limit = max_frames;
754 if (Config->get_stop_at_session_end()) {
755 stop_limit = current_end_frame();
757 stop_limit = max_frames;
761 if (maybe_stop (stop_limit)) {
762 no_roll (nframes, 0);
766 click (_transport_frame, nframes, 0);
768 prepare_diskstreams ();
770 frames_moved = (long) floor (_transport_speed * nframes);
772 if (process_routes (nframes, 0)) {
773 no_roll (nframes, 0);
777 commit_diskstreams (nframes, session_needs_butler);
779 if (frames_moved < 0) {
780 decrement_transport_position (-frames_moved);
782 increment_transport_position (frames_moved);
785 maybe_stop (stop_limit);
786 check_declick_out ();
788 } /* implicit release of route lock */
790 if (session_needs_butler) {
794 if (!_engine.freewheeling() && send_mtc) {
795 send_midi_time_code_in_another_thread ();
802 Session::process_audition (jack_nframes_t nframes)
804 Glib::RWLock::ReaderLock rm (route_lock, Glib::TRY_LOCK);
808 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
809 if (!(*i)->hidden()) {
810 (*i)->silence (nframes, 0);
815 if (auditioner->play_audition (nframes) > 0) {
819 while (pending_events.read (&ev, 1) == 1) {
823 /* if we are not in the middle of a state change,
824 and there are immediate events queued up,
828 while (!non_realtime_work_pending() && !immediate_events.empty()) {
829 Event *ev = immediate_events.front ();
830 immediate_events.pop_front ();
834 if (!auditioner->active()) {
835 process_function = &Session::process_with_events;