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>
41 #include <midi++/manager.h>
45 using namespace ARDOUR;
46 //using namespace sigc;
50 Session::process (jack_nframes_t nframes)
52 //cerr << "CYCLE START " << _transport_frame << "-------------------" << endl;
54 MIDI::Manager::instance()->cycle_start(nframes);
56 if (synced_to_jack() && waiting_to_start) {
57 if ( _engine.transport_state() == AudioEngine::TransportRolling) {
58 actually_start_transport ();
62 if (non_realtime_work_pending()) {
63 if (atomic_read (&butler_should_do_transport_work) == 0) {
68 (this->*process_function) (nframes);
70 MIDI::Manager::instance()->cycle_end();
72 //cerr << "CYCLE END " << _transport_frame << "-----------------------" << endl;
76 Session::prepare_diskstreams ()
78 for (DiskStreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
84 Session::no_roll (jack_nframes_t nframes, jack_nframes_t offset)
86 jack_nframes_t end_frame = _transport_frame + nframes;
88 bool declick = get_transport_declick_required();
91 _click_io->silence (nframes, offset);
94 /* XXX we're supposed to have the route_lock while doing this.
95 this is really bad ...
98 if (atomic_read (&processing_prohibited)) {
99 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
100 (*i)->silence (nframes, offset);
105 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
107 if ((*i)->hidden()) {
111 (*i)->set_pending_declick (declick);
113 if ((*i)->no_roll (nframes, _transport_frame, end_frame, offset, non_realtime_work_pending(),
114 actively_recording(), declick)) {
115 error << string_compose(_("Session: error in no roll for %1"), (*i)->name()) << endmsg;
125 Session::process_routes (jack_nframes_t nframes, jack_nframes_t offset)
128 int declick = get_transport_declick_required();
129 bool rec_monitors = get_rec_monitors_input();
131 if (transport_sub_state & StopPendingCapture) {
132 /* force a declick out */
136 record_active = actively_recording(); // || (get_record_enabled() && get_punch_in());
138 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
142 if ((*i)->hidden()) {
146 (*i)->set_pending_declick (declick);
148 if ((ret = (*i)->roll (nframes, _transport_frame, _transport_frame + nframes, offset, declick, record_active, rec_monitors)) < 0) {
150 /* we have to do this here. Route::roll() for an AudioTrack will have called DiskStream::process(),
151 and the DS will expect DiskStream::commit() to be called. but we're aborting from that
152 call path, so make sure we release any outstanding locks here before we return failure.
155 for (DiskStreamList::iterator ids = diskstreams.begin(); ids != diskstreams.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();
174 if (transport_sub_state & StopPendingCapture) {
175 /* force a declick out */
179 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
183 if ((*i)->hidden()) {
187 if ((ret = (*i)->silent_roll (nframes, _transport_frame, _transport_frame + nframes, offset, record_active, rec_monitors)) < 0) {
189 /* we have to do this here. Route::roll() for an AudioTrack will have called DiskStream::process(),
190 and the DS will expect DiskStream::commit() to be called. but we're aborting from that
191 call path, so make sure we release any outstanding locks here before we return failure.
194 for (DiskStreamList::iterator ids = diskstreams.begin(); ids != diskstreams.end(); ++ids) {
207 Session::commit_diskstreams (jack_nframes_t nframes, bool &needs_butler)
213 for (DiskStreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
215 if ((*i)->hidden()) {
219 /* force all diskstreams not handled by a Route to call do their stuff.
222 if ((dret = (*i)->process (_transport_frame, nframes, 0, actively_recording(), get_rec_monitors_input())) == 0) {
223 if ((*i)->commit (nframes)) {
227 } else if (dret < 0) {
231 pworst = min (pworst, (*i)->playback_buffer_load());
232 cworst = min (cworst, (*i)->capture_buffer_load());
235 uint32_t pmin = atomic_read (&_playback_load);
236 uint32_t pminold = atomic_read (&_playback_load_min);
237 uint32_t cmin = atomic_read (&_capture_load);
238 uint32_t cminold = atomic_read (&_capture_load_min);
240 atomic_set (&_playback_load, (uint32_t) floor (pworst * 100.0f));
241 atomic_set (&_capture_load, (uint32_t) floor (cworst * 100.0f));
242 atomic_set (&_playback_load_min, min (pmin, pminold));
243 atomic_set (&_capture_load_min, min (cmin, cminold));
245 if (actively_recording()) {
252 Session::process_with_events (jack_nframes_t nframes)
255 jack_nframes_t this_nframes;
256 jack_nframes_t end_frame;
257 jack_nframes_t offset;
258 jack_nframes_t stop_limit;
260 bool session_needs_butler = false;
263 auditioner->silence (nframes, 0);
266 while (pending_events.read (&ev, 1) == 1) {
270 /* if we are not in the middle of a state change,
271 and there are immediate events queued up,
275 while (!non_realtime_work_pending() && !immediate_events.empty()) {
276 Event *ev = immediate_events.front ();
277 immediate_events.pop_front ();
281 /* Events caused a transport change and we need to send MTC
282 * [DR] FIXME: best place for this? */
283 if (_send_smpte_update) {
284 cerr << "[DR] TIME CHANGE\n" << endl;
285 send_full_time_code(nframes);
288 if (!process_can_proceed()) {
289 no_roll (nframes, 0);
293 if (events.empty() || next_event == events.end()) {
294 process_without_events (nframes);
298 end_frame = _transport_frame + nframes;
301 TentativeRWLockMonitor rm (route_lock, false, __LINE__, __FILE__);
302 TentativeRWLockMonitor dsm (diskstream_lock, false, __LINE__, __FILE__);
305 Events::iterator the_next_one;
307 if (!rm.locked() || !dsm.locked() || (post_transport_work & (PostTransportLocate|PostTransportStop))) {
308 no_roll (nframes, 0);
312 if (!_exporting && _slave) {
313 if (!follow_slave (nframes, 0)) {
318 if (_transport_speed == 0) {
319 no_roll (nframes, 0);
323 if (actively_recording()) {
324 stop_limit = max_frames;
327 if (Config->get_stop_at_session_end()) {
328 stop_limit = current_end_frame();
330 stop_limit = max_frames;
334 if (maybe_stop (stop_limit)) {
335 no_roll (nframes, 0);
339 this_event = *next_event;
340 the_next_one = next_event;
347 if (this_event == 0 || this_event->action_frame > end_frame || this_event->action_frame < _transport_frame) {
349 this_nframes = nframes;
353 /* compute nframes to next event */
355 if (this_event->action_frame < end_frame) {
356 this_nframes = nframes - (end_frame - this_event->action_frame);
358 this_nframes = nframes;
364 click (_transport_frame, nframes, offset);
366 /* now process frames between now and the first event in this block */
367 prepare_diskstreams ();
369 if (process_routes (this_nframes, offset)) {
370 no_roll (nframes, 0);
374 commit_diskstreams (this_nframes, session_needs_butler);
376 nframes -= this_nframes;
377 offset += this_nframes;
379 frames_moved = (jack_nframes_t) floor (_transport_speed * this_nframes);
381 if (frames_moved < 0) {
382 decrement_transport_position (-frames_moved);
384 increment_transport_position (frames_moved);
387 maybe_stop (stop_limit);
388 check_declick_out ();
391 /* now handle this event and all others scheduled for the same time */
393 while (this_event && this_event->action_frame == _transport_frame) {
394 process_event (this_event);
396 if (the_next_one == events.end()) {
399 this_event = *the_next_one;
404 /* if an event left our state changing, do the right thing */
406 if (non_realtime_work_pending()) {
407 no_roll (nframes, offset);
411 /* this is necessary to handle the case of seamless looping */
412 /* not sure if it will work in conjuction with varispeed */
413 end_frame = _transport_frame + nframes;
419 } /* implicit release of route lock */
421 if (session_needs_butler)
424 send_midi_time_code_for_cycle(nframes);
428 Session::reset_slave_state ()
430 average_slave_delta = 1800;
431 delta_accumulator_cnt = 0;
432 have_first_delta_accumulator = false;
433 slave_state = Stopped;
437 Session::transport_locked () const
441 if (!locate_pending() && ((_slave_type == None) || (sl && sl->ok() && sl->locked()))) {
449 Session::follow_slave (jack_nframes_t nframes, jack_nframes_t offset)
452 jack_nframes_t slave_transport_frame;
453 jack_nframes_t this_delta;
459 set_slave_source (None, 0);
463 _slave->speed_and_position (slave_speed, slave_transport_frame);
465 if (!_slave->locked()) {
469 if (slave_transport_frame > _transport_frame) {
470 this_delta = slave_transport_frame - _transport_frame;
473 this_delta = _transport_frame - slave_transport_frame;
477 if ((starting = _slave->starting())) {
482 cerr << "delta = " << (int) (dir * this_delta)
483 << " speed = " << slave_speed
484 << " ts = " << _transport_speed
485 << " M@"<< slave_transport_frame << " S@" << _transport_frame
486 << " avgdelta = " << average_slave_delta
490 if (Config->get_timecode_source_is_synced()) {
492 /* if the TC source is synced, then we assume that its
493 speed is binary: 0.0 or 1.0
496 if (slave_speed != 0.0f) {
502 /* TC source is able to drift relative to us (slave)
503 so we need to keep track of the drift and adjust
504 our speed to remain locked.
507 if (delta_accumulator_cnt >= delta_accumulator_size) {
508 have_first_delta_accumulator = true;
509 delta_accumulator_cnt = 0;
512 if (delta_accumulator_cnt != 0 || this_delta < _current_frame_rate) {
513 delta_accumulator[delta_accumulator_cnt++] = dir*this_delta;
516 if (have_first_delta_accumulator) {
517 average_slave_delta = 0;
518 for (int i = 0; i < delta_accumulator_size; ++i) {
519 average_slave_delta += delta_accumulator[i];
521 average_slave_delta /= delta_accumulator_size;
522 if (average_slave_delta < 0) {
524 average_slave_delta = -average_slave_delta;
528 // cerr << "avgdelta = " << average_slave_delta*average_dir << endl;
532 if (slave_speed != 0.0f) {
534 /* slave is running */
536 switch (slave_state) {
538 if (_slave->requires_seekahead()) {
539 slave_wait_end = slave_transport_frame + _current_frame_rate;
540 locate (slave_wait_end, false, false);
541 slave_state = Waiting;
546 slave_state = Running;
548 Location* al = _locations.auto_loop_location();
550 if (al && auto_loop && (slave_transport_frame < al->start() || slave_transport_frame > al->end())) {
552 request_auto_loop(false);
555 if (slave_transport_frame != _transport_frame) {
556 locate (slave_transport_frame, false, false);
569 if (slave_state == Waiting) {
571 // cerr << "waiting at " << slave_transport_frame << endl;
572 TentativeRWLockMonitor dsm (diskstream_lock, false, __LINE__, __FILE__);
574 if (dsm.locked() && slave_transport_frame >= slave_wait_end) {
575 // cerr << "\tstart at " << _transport_frame << endl;
577 slave_state = Running;
580 jack_nframes_t frame_delta = slave_transport_frame - _transport_frame;
582 for (DiskStreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
583 if (!(*i)->can_internal_playback_seek (frame_delta)) {
590 for (DiskStreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
591 (*i)->internal_playback_seek (frame_delta);
593 _transport_frame += frame_delta;
596 // cerr << "cannot micro-seek\n";
600 memset (delta_accumulator, 0, sizeof (jack_nframes_t) * delta_accumulator_size);
601 average_slave_delta = 0;
606 if (slave_state == Running && _transport_speed == 0.0f) {
608 // cerr << "slave starts transport\n";
615 /* slave has stopped */
617 if (_transport_speed != 0.0f) {
619 // cerr << "slave stops transport: " << slave_speed << " frame: " << slave_transport_frame
620 // << " tf = " << _transport_frame
623 if (_slave_type == JACK) {
624 last_stop_frame = _transport_frame;
630 if (slave_transport_frame != _transport_frame) {
631 // cerr << "slave stopped, move to " << slave_transport_frame << endl;
632 force_locate (slave_transport_frame, false);
635 slave_state = Stopped;
638 if (slave_state == Running && !Config->get_timecode_source_is_synced()) {
641 if (_transport_speed != 0.0f) {
644 note that average_dir is +1 or -1
647 const float adjust_seconds = 1.0f;
650 //if (average_slave_delta == 0) {
654 // delta = average_slave_delta;
655 // delta *= average_dir;
658 float adjusted_speed = slave_speed +
659 (delta / (adjust_seconds * _current_frame_rate));
661 // cerr << "adjust using " << delta
662 // << " towards " << adjusted_speed
663 // << " ratio = " << adjusted_speed / slave_speed
664 // << " current = " << _transport_speed
665 // << " slave @ " << slave_speed
668 request_transport_speed (adjusted_speed);
671 if ((jack_nframes_t) average_slave_delta > _slave->resolution()) {
672 // cerr << "not locked\n";
679 if (!starting && !non_realtime_work_pending()) {
680 /* speed is set, we're locked, and good to go */
686 if (slave_speed && _transport_speed) {
688 /* something isn't right, but we should move with the master
694 TentativeRWLockMonitor dsm (diskstream_lock, false, __LINE__, __FILE__);
700 prepare_diskstreams ();
701 silent_process_routes (nframes, offset);
702 commit_diskstreams (nframes, need_butler);
708 jack_nframes_t frames_moved = (long) floor (_transport_speed * nframes);
710 if (frames_moved < 0) {
711 decrement_transport_position (-frames_moved);
713 increment_transport_position (frames_moved);
716 jack_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);
732 /* don't move at all */
733 no_roll (nframes, 0);
738 Session::process_without_events (jack_nframes_t nframes)
740 bool session_needs_butler = false;
741 jack_nframes_t stop_limit;
745 TentativeRWLockMonitor rm (route_lock, false, __LINE__, __FILE__);
746 TentativeRWLockMonitor dsm (diskstream_lock, false, __LINE__, __FILE__);
748 if (!rm.locked() || !dsm.locked() || (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 if (actively_recording()) {
765 stop_limit = max_frames;
767 if (Config->get_stop_at_session_end()) {
768 stop_limit = current_end_frame();
770 stop_limit = max_frames;
774 if (maybe_stop (stop_limit)) {
775 no_roll (nframes, 0);
779 click (_transport_frame, nframes, 0);
781 prepare_diskstreams ();
783 frames_moved = (long) floor (_transport_speed * nframes);
785 if (process_routes (nframes, 0)) {
786 no_roll (nframes, 0);
790 commit_diskstreams (nframes, session_needs_butler);
792 if (frames_moved < 0) {
793 decrement_transport_position (-frames_moved);
795 increment_transport_position (frames_moved);
798 maybe_stop (stop_limit);
799 check_declick_out ();
801 } /* implicit release of route lock */
803 send_midi_time_code_for_cycle(nframes);
805 if (session_needs_butler)
810 Session::process_audition (jack_nframes_t nframes)
812 TentativeRWLockMonitor rm (route_lock, false, __LINE__, __FILE__);
816 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
817 if (!(*i)->hidden()) {
818 (*i)->silence (nframes, 0);
823 if (auditioner->play_audition (nframes) > 0) {
827 while (pending_events.read (&ev, 1) == 1) {
831 /* if we are not in the middle of a state change,
832 and there are immediate events queued up,
836 while (!non_realtime_work_pending() && !immediate_events.empty()) {
837 Event *ev = immediate_events.front ();
838 immediate_events.pop_front ();
842 if (!auditioner->active()) {
843 process_function = &Session::process_with_events;