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 <ardour/timestamps.h>
28 #include <pbd/error.h>
29 #include <pbd/atomic.h>
30 #include <pbd/lockmonitor.h>
32 #include <ardour/ardour.h>
33 #include <ardour/session.h>
34 #include <ardour/diskstream.h>
35 #include <ardour/audioengine.h>
36 #include <ardour/slave.h>
37 #include <ardour/auditioner.h>
38 #include <ardour/cycles.h>
39 #include <ardour/cycle_timer.h>
43 using namespace ARDOUR;
44 //using namespace sigc;
48 Session::process (jack_nframes_t nframes)
50 if (synced_to_jack() && waiting_to_start) {
51 if ( _engine.transport_state() == AudioEngine::TransportRolling) {
52 actually_start_transport ();
56 if (non_realtime_work_pending()) {
57 if (atomic_read (&butler_should_do_transport_work) == 0) {
62 (this->*process_function) (nframes);
66 Session::prepare_diskstreams ()
68 for (DiskStreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
74 Session::no_roll (jack_nframes_t nframes, jack_nframes_t offset)
76 jack_nframes_t end_frame = _transport_frame + nframes;
78 bool declick = get_transport_declick_required();
81 _click_io->silence (nframes, offset);
84 /* XXX we're supposed to have the route_lock while doing this.
85 this is really bad ...
88 if (atomic_read (&processing_prohibited)) {
89 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
90 (*i)->silence (nframes, offset);
95 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
101 (*i)->set_pending_declick (declick);
103 if ((*i)->no_roll (nframes, _transport_frame, end_frame, offset, non_realtime_work_pending(),
104 actively_recording(), declick)) {
105 error << string_compose(_("Session: error in no roll for %1"), (*i)->name()) << endmsg;
115 Session::process_routes (jack_nframes_t nframes, jack_nframes_t offset)
118 int declick = get_transport_declick_required();
119 bool rec_monitors = get_rec_monitors_input();
121 if (transport_sub_state & StopPendingCapture) {
122 /* force a declick out */
126 record_active = actively_recording(); // || (get_record_enabled() && get_punch_in());
128 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
132 if ((*i)->hidden()) {
136 (*i)->set_pending_declick (declick);
138 if ((ret = (*i)->roll (nframes, _transport_frame, _transport_frame + nframes, offset, declick, record_active, rec_monitors)) < 0) {
140 /* we have to do this here. Route::roll() for an AudioTrack will have called DiskStream::process(),
141 and the DS will expect DiskStream::commit() to be called. but we're aborting from that
142 call path, so make sure we release any outstanding locks here before we return failure.
145 for (DiskStreamList::iterator ids = diskstreams.begin(); ids != diskstreams.end(); ++ids) {
158 Session::silent_process_routes (jack_nframes_t nframes, jack_nframes_t offset)
160 bool record_active = actively_recording();
161 int declick = get_transport_declick_required();
162 bool rec_monitors = get_rec_monitors_input();
164 if (transport_sub_state & StopPendingCapture) {
165 /* force a declick out */
169 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
173 if ((*i)->hidden()) {
177 if ((ret = (*i)->silent_roll (nframes, _transport_frame, _transport_frame + nframes, offset, record_active, rec_monitors)) < 0) {
179 /* we have to do this here. Route::roll() for an AudioTrack will have called DiskStream::process(),
180 and the DS will expect DiskStream::commit() to be called. but we're aborting from that
181 call path, so make sure we release any outstanding locks here before we return failure.
184 for (DiskStreamList::iterator ids = diskstreams.begin(); ids != diskstreams.end(); ++ids) {
197 Session::commit_diskstreams (jack_nframes_t nframes, bool &needs_butler)
203 for (DiskStreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
205 if ((*i)->hidden()) {
209 /* force all diskstreams not handled by a Route to call do their stuff.
212 if ((dret = (*i)->process (_transport_frame, nframes, 0, actively_recording(), get_rec_monitors_input())) == 0) {
213 if ((*i)->commit (nframes)) {
217 } else if (dret < 0) {
221 pworst = min (pworst, (*i)->playback_buffer_load());
222 cworst = min (cworst, (*i)->capture_buffer_load());
225 uint32_t pmin = atomic_read (&_playback_load);
226 uint32_t pminold = atomic_read (&_playback_load_min);
227 uint32_t cmin = atomic_read (&_capture_load);
228 uint32_t cminold = atomic_read (&_capture_load_min);
230 atomic_set (&_playback_load, (uint32_t) floor (pworst * 100.0f));
231 atomic_set (&_capture_load, (uint32_t) floor (cworst * 100.0f));
232 atomic_set (&_playback_load_min, min (pmin, pminold));
233 atomic_set (&_capture_load_min, min (cmin, cminold));
235 if (actively_recording()) {
241 Session::process_with_events (jack_nframes_t nframes)
244 jack_nframes_t this_nframes;
245 jack_nframes_t end_frame;
246 jack_nframes_t offset;
247 bool session_needs_butler = false;
248 jack_nframes_t stop_limit;
252 auditioner->silence (nframes, 0);
255 while (pending_events.read (&ev, 1) == 1) {
259 /* if we are not in the middle of a state change,
260 and there are immediate events queued up,
264 while (!non_realtime_work_pending() && !immediate_events.empty()) {
265 Event *ev = immediate_events.front ();
266 immediate_events.pop_front ();
270 if (!process_can_proceed()) {
271 no_roll (nframes, 0);
275 if (events.empty() || next_event == events.end()) {
276 process_without_events (nframes);
280 end_frame = _transport_frame + nframes;
283 TentativeRWLockMonitor rm (route_lock, false, __LINE__, __FILE__);
284 TentativeRWLockMonitor dsm (diskstream_lock, false, __LINE__, __FILE__);
287 Events::iterator the_next_one;
289 if (!rm.locked() || !dsm.locked() || (post_transport_work & (PostTransportLocate|PostTransportStop))) {
290 no_roll (nframes, 0);
294 if (!_exporting && _slave) {
295 if (!follow_slave (nframes, 0)) {
300 if (_transport_speed == 0) {
301 no_roll (nframes, 0);
305 if (actively_recording()) {
306 stop_limit = max_frames;
309 if (Config->get_stop_at_session_end()) {
310 stop_limit = current_end_frame();
312 stop_limit = max_frames;
316 if (maybe_stop (stop_limit)) {
317 no_roll (nframes, 0);
321 this_event = *next_event;
322 the_next_one = next_event;
329 if (this_event == 0 || this_event->action_frame > end_frame || this_event->action_frame < _transport_frame) {
331 this_nframes = nframes;
335 /* compute nframes to next event */
337 if (this_event->action_frame < end_frame) {
338 this_nframes = nframes - (end_frame - this_event->action_frame);
340 this_nframes = nframes;
346 click (_transport_frame, nframes, offset);
348 /* now process frames between now and the first event in this block */
349 prepare_diskstreams ();
351 if (process_routes (this_nframes, offset)) {
352 no_roll (nframes, 0);
356 commit_diskstreams (this_nframes, session_needs_butler);
358 nframes -= this_nframes;
359 offset += this_nframes;
361 frames_moved = (jack_nframes_t) floor (_transport_speed * this_nframes);
363 if (frames_moved < 0) {
364 decrement_transport_position (-frames_moved);
366 increment_transport_position (frames_moved);
369 maybe_stop (stop_limit);
370 check_declick_out ();
373 /* now handle this event and all others scheduled for the same time */
375 while (this_event && this_event->action_frame == _transport_frame) {
376 process_event (this_event);
378 if (the_next_one == events.end()) {
381 this_event = *the_next_one;
386 /* if an event left our state changing, do the right thing */
388 if (non_realtime_work_pending()) {
389 no_roll (nframes, offset);
393 /* this is necessary to handle the case of seamless looping */
394 /* not sure if it will work in conjuction with varispeed */
395 end_frame = _transport_frame + nframes;
401 } /* implicit release of route lock */
404 if (session_needs_butler) {
408 if (!_engine.freewheeling() && send_mtc) {
409 send_midi_time_code_in_another_thread ();
416 Session::reset_slave_state ()
418 average_slave_delta = 1800;
419 delta_accumulator_cnt = 0;
420 have_first_delta_accumulator = false;
421 slave_state = Stopped;
425 Session::follow_slave (jack_nframes_t nframes, jack_nframes_t offset)
428 jack_nframes_t slave_transport_frame;
429 jack_nframes_t this_delta;
435 set_slave_source (None, 0);
439 _slave->speed_and_position (slave_speed, slave_transport_frame);
441 if (!_slave->locked()) {
445 if (slave_transport_frame > _transport_frame) {
446 this_delta = slave_transport_frame - _transport_frame;
449 this_delta = _transport_frame - slave_transport_frame;
453 if ((starting = _slave->starting())) {
458 cerr << "delta = " << (int) (dir * this_delta)
459 << " speed = " << slave_speed
460 << " ts = " << _transport_speed
461 << " M@"<< slave_transport_frame << " S@" << _transport_frame
462 << " avgdelta = " << average_slave_delta
466 if (Config->get_timecode_source_is_synced()) {
468 /* if the TC source is synced, then we assume that its
469 speed is binary: 0.0 or 1.0
472 if (slave_speed != 0.0f) {
478 /* TC source is able to drift relative to us (slave)
479 so we need to keep track of the drift and adjust
480 our speed to remain locked.
483 if (delta_accumulator_cnt >= delta_accumulator_size) {
484 have_first_delta_accumulator = true;
485 delta_accumulator_cnt = 0;
488 if (delta_accumulator_cnt != 0 || this_delta < _current_frame_rate) {
489 delta_accumulator[delta_accumulator_cnt++] = dir*this_delta;
492 if (have_first_delta_accumulator) {
493 average_slave_delta = 0;
494 for (int i = 0; i < delta_accumulator_size; ++i) {
495 average_slave_delta += delta_accumulator[i];
497 average_slave_delta /= delta_accumulator_size;
498 if (average_slave_delta < 0) {
500 average_slave_delta = -average_slave_delta;
504 // cerr << "avgdelta = " << average_slave_delta*average_dir << endl;
508 if (slave_speed != 0.0f) {
510 /* slave is running */
512 switch (slave_state) {
514 if (_slave->requires_seekahead()) {
515 slave_wait_end = slave_transport_frame + _current_frame_rate;
516 locate (slave_wait_end, false, false);
517 slave_state = Waiting;
522 slave_state = Running;
524 Location* al = _locations.auto_loop_location();
526 if (al && auto_loop && (slave_transport_frame < al->start() || slave_transport_frame > al->end())) {
528 request_auto_loop(false);
531 if (slave_transport_frame != _transport_frame) {
532 locate (slave_transport_frame, false, false);
545 if (slave_state == Waiting) {
547 // cerr << "waiting at " << slave_transport_frame << endl;
548 TentativeRWLockMonitor dsm (diskstream_lock, false, __LINE__, __FILE__);
550 if (dsm.locked() && slave_transport_frame >= slave_wait_end) {
551 // cerr << "\tstart at " << _transport_frame << endl;
553 slave_state = Running;
556 jack_nframes_t frame_delta = slave_transport_frame - _transport_frame;
558 for (DiskStreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
559 if (!(*i)->can_internal_playback_seek (frame_delta)) {
566 for (DiskStreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
567 (*i)->internal_playback_seek (frame_delta);
569 _transport_frame += frame_delta;
572 // cerr << "cannot micro-seek\n";
576 memset (delta_accumulator, 0, sizeof (jack_nframes_t) * delta_accumulator_size);
577 average_slave_delta = 0;
582 if (slave_state == Running && _transport_speed == 0.0f) {
584 // cerr << "slave starts transport\n";
591 /* slave has stopped */
593 if (_transport_speed != 0.0f) {
595 // cerr << "slave stops transport: " << slave_speed << " frame: " << slave_transport_frame
596 // << " tf = " << _transport_frame
599 if (_slave_type == JACK) {
600 last_stop_frame = _transport_frame;
606 if (slave_transport_frame != _transport_frame) {
607 // cerr << "slave stopped, move to " << slave_transport_frame << endl;
608 force_locate (slave_transport_frame, false);
611 slave_state = Stopped;
614 if (slave_state == Running && !Config->get_timecode_source_is_synced()) {
617 if (_transport_speed != 0.0f) {
620 note that average_dir is +1 or -1
623 const float adjust_seconds = 1.0f;
626 //if (average_slave_delta == 0) {
630 // delta = average_slave_delta;
631 // delta *= average_dir;
634 float adjusted_speed = slave_speed +
635 (delta / (adjust_seconds * _current_frame_rate));
637 // cerr << "adjust using " << delta
638 // << " towards " << adjusted_speed
639 // << " ratio = " << adjusted_speed / slave_speed
640 // << " current = " << _transport_speed
641 // << " slave @ " << slave_speed
644 request_transport_speed (adjusted_speed);
647 if ((jack_nframes_t) average_slave_delta > _slave->resolution()) {
648 // cerr << "not locked\n";
655 if (!starting && !non_realtime_work_pending()) {
656 /* speed is set, we're locked, and good to go */
662 if (slave_speed && _transport_speed) {
664 /* something isn't right, but we should move with the master
670 TentativeRWLockMonitor dsm (diskstream_lock, false, __LINE__, __FILE__);
676 prepare_diskstreams ();
677 silent_process_routes (nframes, offset);
678 commit_diskstreams (nframes, need_butler);
684 jack_nframes_t frames_moved = (long) floor (_transport_speed * nframes);
686 if (frames_moved < 0) {
687 decrement_transport_position (-frames_moved);
689 increment_transport_position (frames_moved);
692 jack_nframes_t stop_limit;
694 if (actively_recording()) {
695 stop_limit = max_frames;
697 if (Config->get_stop_at_session_end()) {
698 stop_limit = current_end_frame();
700 stop_limit = max_frames;
704 maybe_stop (stop_limit);
708 /* don't move at all */
709 no_roll (nframes, 0);
714 Session::process_without_events (jack_nframes_t nframes)
716 bool session_needs_butler = false;
717 jack_nframes_t stop_limit;
721 TentativeRWLockMonitor rm (route_lock, false, __LINE__, __FILE__);
722 TentativeRWLockMonitor dsm (diskstream_lock, false, __LINE__, __FILE__);
724 if (!rm.locked() || !dsm.locked() || (post_transport_work & (PostTransportLocate|PostTransportStop))) {
725 no_roll (nframes, 0);
729 if (!_exporting && _slave) {
730 if (!follow_slave (nframes, 0)) {
735 if (_transport_speed == 0) {
736 no_roll (nframes, 0);
740 if (actively_recording()) {
741 stop_limit = max_frames;
743 if (Config->get_stop_at_session_end()) {
744 stop_limit = current_end_frame();
746 stop_limit = max_frames;
750 if (maybe_stop (stop_limit)) {
751 no_roll (nframes, 0);
755 click (_transport_frame, nframes, 0);
757 prepare_diskstreams ();
759 frames_moved = (long) floor (_transport_speed * nframes);
761 if (process_routes (nframes, 0)) {
762 no_roll (nframes, 0);
766 commit_diskstreams (nframes, session_needs_butler);
768 if (frames_moved < 0) {
769 decrement_transport_position (-frames_moved);
771 increment_transport_position (frames_moved);
774 maybe_stop (stop_limit);
775 check_declick_out ();
777 } /* implicit release of route lock */
779 if (session_needs_butler) {
783 if (!_engine.freewheeling() && send_mtc) {
784 send_midi_time_code_in_another_thread ();
791 Session::process_audition (jack_nframes_t nframes)
793 TentativeRWLockMonitor rm (route_lock, false, __LINE__, __FILE__);
797 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
798 if (!(*i)->hidden()) {
799 (*i)->silence (nframes, 0);
804 if (auditioner->play_audition (nframes) > 0) {
808 while (pending_events.read (&ev, 1) == 1) {
812 /* if we are not in the middle of a state change,
813 and there are immediate events queued up,
817 while (!non_realtime_work_pending() && !immediate_events.empty()) {
818 Event *ev = immediate_events.front ();
819 immediate_events.pop_front ();
823 if (!auditioner->active()) {
824 process_function = &Session::process_with_events;