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();
90 cerr << "[DR] no_roll\n";
93 _click_io->silence (nframes, offset);
96 /* XXX we're supposed to have the route_lock while doing this.
97 this is really bad ...
100 if (atomic_read (&processing_prohibited)) {
101 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
102 (*i)->silence (nframes, offset);
107 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
109 if ((*i)->hidden()) {
113 (*i)->set_pending_declick (declick);
115 if ((*i)->no_roll (nframes, _transport_frame, end_frame, offset, 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 (jack_nframes_t nframes, jack_nframes_t offset)
130 int declick = get_transport_declick_required();
131 bool rec_monitors = get_rec_monitors_input();
133 if (transport_sub_state & StopPendingCapture) {
134 /* force a declick out */
138 record_active = actively_recording(); // || (get_record_enabled() && get_punch_in());
140 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
144 if ((*i)->hidden()) {
148 (*i)->set_pending_declick (declick);
150 if ((ret = (*i)->roll (nframes, _transport_frame, _transport_frame + nframes, offset, declick, record_active, rec_monitors)) < 0) {
152 /* we have to do this here. Route::roll() for an AudioTrack will have called DiskStream::process(),
153 and the DS will expect DiskStream::commit() to be called. but we're aborting from that
154 call path, so make sure we release any outstanding locks here before we return failure.
157 for (DiskStreamList::iterator ids = diskstreams.begin(); ids != diskstreams.end(); ++ids) {
170 Session::silent_process_routes (jack_nframes_t nframes, jack_nframes_t offset)
172 bool record_active = actively_recording();
173 int declick = get_transport_declick_required();
174 bool rec_monitors = get_rec_monitors_input();
176 if (transport_sub_state & StopPendingCapture) {
177 /* force a declick out */
181 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
185 if ((*i)->hidden()) {
189 if ((ret = (*i)->silent_roll (nframes, _transport_frame, _transport_frame + nframes, offset, record_active, rec_monitors)) < 0) {
191 /* we have to do this here. Route::roll() for an AudioTrack will have called DiskStream::process(),
192 and the DS will expect DiskStream::commit() to be called. but we're aborting from that
193 call path, so make sure we release any outstanding locks here before we return failure.
196 for (DiskStreamList::iterator ids = diskstreams.begin(); ids != diskstreams.end(); ++ids) {
209 Session::commit_diskstreams (jack_nframes_t nframes, bool &needs_butler)
215 for (DiskStreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
217 if ((*i)->hidden()) {
221 /* force all diskstreams not handled by a Route to call do their stuff.
224 if ((dret = (*i)->process (_transport_frame, nframes, 0, actively_recording(), get_rec_monitors_input())) == 0) {
225 if ((*i)->commit (nframes)) {
229 } else if (dret < 0) {
233 pworst = min (pworst, (*i)->playback_buffer_load());
234 cworst = min (cworst, (*i)->capture_buffer_load());
237 uint32_t pmin = atomic_read (&_playback_load);
238 uint32_t pminold = atomic_read (&_playback_load_min);
239 uint32_t cmin = atomic_read (&_capture_load);
240 uint32_t cminold = atomic_read (&_capture_load_min);
242 atomic_set (&_playback_load, (uint32_t) floor (pworst * 100.0f));
243 atomic_set (&_capture_load, (uint32_t) floor (cworst * 100.0f));
244 atomic_set (&_playback_load_min, min (pmin, pminold));
245 atomic_set (&_capture_load_min, min (cmin, cminold));
247 if (actively_recording()) {
254 Session::process_with_events (jack_nframes_t nframes)
257 jack_nframes_t this_nframes;
258 jack_nframes_t end_frame;
259 jack_nframes_t offset;
260 jack_nframes_t stop_limit;
262 bool session_needs_butler = false;
264 cerr << "[DR] with events" << endl;
267 auditioner->silence (nframes, 0);
270 while (pending_events.read (&ev, 1) == 1) {
274 /* if we are not in the middle of a state change,
275 and there are immediate events queued up,
279 while (!non_realtime_work_pending() && !immediate_events.empty()) {
280 Event *ev = immediate_events.front ();
281 immediate_events.pop_front ();
285 /* Events caused a transport change and we need to send MTC
286 * [DR] FIXME: best place for this? */
287 if (_send_smpte_update) {
288 cerr << "[DR] TIME CHANGE\n" << endl;
289 send_full_time_code(nframes);
292 if (!process_can_proceed()) {
293 no_roll (nframes, 0);
297 if (events.empty() || next_event == events.end()) {
298 process_without_events (nframes);
302 end_frame = _transport_frame + nframes;
305 TentativeRWLockMonitor rm (route_lock, false, __LINE__, __FILE__);
306 TentativeRWLockMonitor dsm (diskstream_lock, false, __LINE__, __FILE__);
309 Events::iterator the_next_one;
311 if (!rm.locked() || !dsm.locked() || (post_transport_work & (PostTransportLocate|PostTransportStop))) {
312 no_roll (nframes, 0);
316 if (!_exporting && _slave) {
317 if (!follow_slave (nframes, 0)) {
322 if (_transport_speed == 0) {
323 no_roll (nframes, 0);
327 if (actively_recording()) {
328 stop_limit = max_frames;
331 if (Config->get_stop_at_session_end()) {
332 stop_limit = current_end_frame();
334 stop_limit = max_frames;
338 if (maybe_stop (stop_limit)) {
339 no_roll (nframes, 0);
343 this_event = *next_event;
344 the_next_one = next_event;
351 if (this_event == 0 || this_event->action_frame > end_frame || this_event->action_frame < _transport_frame) {
353 this_nframes = nframes;
357 /* compute nframes to next event */
359 if (this_event->action_frame < end_frame) {
360 this_nframes = nframes - (end_frame - this_event->action_frame);
362 this_nframes = nframes;
368 click (_transport_frame, nframes, offset);
370 /* now process frames between now and the first event in this block */
371 prepare_diskstreams ();
373 if (process_routes (this_nframes, offset)) {
374 no_roll (nframes, 0);
378 commit_diskstreams (this_nframes, session_needs_butler);
380 nframes -= this_nframes;
381 offset += this_nframes;
383 frames_moved = (jack_nframes_t) floor (_transport_speed * this_nframes);
385 if (frames_moved < 0) {
386 decrement_transport_position (-frames_moved);
388 increment_transport_position (frames_moved);
391 maybe_stop (stop_limit);
392 check_declick_out ();
395 /* now handle this event and all others scheduled for the same time */
397 while (this_event && this_event->action_frame == _transport_frame) {
398 process_event (this_event);
400 if (the_next_one == events.end()) {
403 this_event = *the_next_one;
408 /* if an event left our state changing, do the right thing */
410 if (non_realtime_work_pending()) {
411 no_roll (nframes, offset);
415 /* this is necessary to handle the case of seamless looping */
416 /* not sure if it will work in conjuction with varispeed */
417 end_frame = _transport_frame + nframes;
423 } /* implicit release of route lock */
425 if (session_needs_butler)
428 send_midi_time_code_for_cycle(nframes);
432 Session::reset_slave_state ()
434 average_slave_delta = 1800;
435 delta_accumulator_cnt = 0;
436 have_first_delta_accumulator = false;
437 slave_state = Stopped;
441 Session::transport_locked () const
445 if (!locate_pending() && ((_slave_type == None) || (sl && sl->ok() && sl->locked()))) {
453 Session::follow_slave (jack_nframes_t nframes, jack_nframes_t offset)
456 jack_nframes_t slave_transport_frame;
457 jack_nframes_t this_delta;
463 set_slave_source (None, 0);
467 _slave->speed_and_position (slave_speed, slave_transport_frame);
469 if (!_slave->locked()) {
473 if (slave_transport_frame > _transport_frame) {
474 this_delta = slave_transport_frame - _transport_frame;
477 this_delta = _transport_frame - slave_transport_frame;
481 if ((starting = _slave->starting())) {
486 cerr << "delta = " << (int) (dir * this_delta)
487 << " speed = " << slave_speed
488 << " ts = " << _transport_speed
489 << " M@"<< slave_transport_frame << " S@" << _transport_frame
490 << " avgdelta = " << average_slave_delta
494 if (Config->get_timecode_source_is_synced()) {
496 /* if the TC source is synced, then we assume that its
497 speed is binary: 0.0 or 1.0
500 if (slave_speed != 0.0f) {
506 /* TC source is able to drift relative to us (slave)
507 so we need to keep track of the drift and adjust
508 our speed to remain locked.
511 if (delta_accumulator_cnt >= delta_accumulator_size) {
512 have_first_delta_accumulator = true;
513 delta_accumulator_cnt = 0;
516 if (delta_accumulator_cnt != 0 || this_delta < _current_frame_rate) {
517 delta_accumulator[delta_accumulator_cnt++] = dir*this_delta;
520 if (have_first_delta_accumulator) {
521 average_slave_delta = 0;
522 for (int i = 0; i < delta_accumulator_size; ++i) {
523 average_slave_delta += delta_accumulator[i];
525 average_slave_delta /= delta_accumulator_size;
526 if (average_slave_delta < 0) {
528 average_slave_delta = -average_slave_delta;
532 // cerr << "avgdelta = " << average_slave_delta*average_dir << endl;
536 if (slave_speed != 0.0f) {
538 /* slave is running */
540 switch (slave_state) {
542 if (_slave->requires_seekahead()) {
543 slave_wait_end = slave_transport_frame + _current_frame_rate;
544 locate (slave_wait_end, false, false);
545 slave_state = Waiting;
550 slave_state = Running;
552 Location* al = _locations.auto_loop_location();
554 if (al && auto_loop && (slave_transport_frame < al->start() || slave_transport_frame > al->end())) {
556 request_auto_loop(false);
559 if (slave_transport_frame != _transport_frame) {
560 locate (slave_transport_frame, false, false);
573 if (slave_state == Waiting) {
575 // cerr << "waiting at " << slave_transport_frame << endl;
576 TentativeRWLockMonitor dsm (diskstream_lock, false, __LINE__, __FILE__);
578 if (dsm.locked() && slave_transport_frame >= slave_wait_end) {
579 // cerr << "\tstart at " << _transport_frame << endl;
581 slave_state = Running;
584 jack_nframes_t frame_delta = slave_transport_frame - _transport_frame;
586 for (DiskStreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
587 if (!(*i)->can_internal_playback_seek (frame_delta)) {
594 for (DiskStreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
595 (*i)->internal_playback_seek (frame_delta);
597 _transport_frame += frame_delta;
600 // cerr << "cannot micro-seek\n";
604 memset (delta_accumulator, 0, sizeof (jack_nframes_t) * delta_accumulator_size);
605 average_slave_delta = 0;
610 if (slave_state == Running && _transport_speed == 0.0f) {
612 // cerr << "slave starts transport\n";
619 /* slave has stopped */
621 if (_transport_speed != 0.0f) {
623 // cerr << "slave stops transport: " << slave_speed << " frame: " << slave_transport_frame
624 // << " tf = " << _transport_frame
627 if (_slave_type == JACK) {
628 last_stop_frame = _transport_frame;
634 if (slave_transport_frame != _transport_frame) {
635 // cerr << "slave stopped, move to " << slave_transport_frame << endl;
636 force_locate (slave_transport_frame, false);
639 slave_state = Stopped;
642 if (slave_state == Running && !Config->get_timecode_source_is_synced()) {
645 if (_transport_speed != 0.0f) {
648 note that average_dir is +1 or -1
651 const float adjust_seconds = 1.0f;
654 //if (average_slave_delta == 0) {
658 // delta = average_slave_delta;
659 // delta *= average_dir;
662 float adjusted_speed = slave_speed +
663 (delta / (adjust_seconds * _current_frame_rate));
665 // cerr << "adjust using " << delta
666 // << " towards " << adjusted_speed
667 // << " ratio = " << adjusted_speed / slave_speed
668 // << " current = " << _transport_speed
669 // << " slave @ " << slave_speed
672 request_transport_speed (adjusted_speed);
675 if ((jack_nframes_t) average_slave_delta > _slave->resolution()) {
676 // cerr << "not locked\n";
683 if (!starting && !non_realtime_work_pending()) {
684 /* speed is set, we're locked, and good to go */
690 if (slave_speed && _transport_speed) {
692 /* something isn't right, but we should move with the master
698 TentativeRWLockMonitor dsm (diskstream_lock, false, __LINE__, __FILE__);
704 prepare_diskstreams ();
705 silent_process_routes (nframes, offset);
706 commit_diskstreams (nframes, need_butler);
712 jack_nframes_t frames_moved = (long) floor (_transport_speed * nframes);
714 if (frames_moved < 0) {
715 decrement_transport_position (-frames_moved);
717 increment_transport_position (frames_moved);
720 jack_nframes_t stop_limit;
722 if (actively_recording()) {
723 stop_limit = max_frames;
725 if (Config->get_stop_at_session_end()) {
726 stop_limit = current_end_frame();
728 stop_limit = max_frames;
732 maybe_stop (stop_limit);
736 /* don't move at all */
737 no_roll (nframes, 0);
742 Session::process_without_events (jack_nframes_t nframes)
744 cerr << "[DR] without events" << endl;
746 bool session_needs_butler = false;
747 jack_nframes_t stop_limit;
751 TentativeRWLockMonitor rm (route_lock, false, __LINE__, __FILE__);
752 TentativeRWLockMonitor dsm (diskstream_lock, false, __LINE__, __FILE__);
754 if (!rm.locked() || !dsm.locked() || (post_transport_work & (PostTransportLocate|PostTransportStop))) {
755 no_roll (nframes, 0);
759 if (!_exporting && _slave) {
760 if (!follow_slave (nframes, 0)) {
765 if (_transport_speed == 0) {
766 no_roll (nframes, 0);
770 if (actively_recording()) {
771 stop_limit = max_frames;
773 if (Config->get_stop_at_session_end()) {
774 stop_limit = current_end_frame();
776 stop_limit = max_frames;
780 if (maybe_stop (stop_limit)) {
781 no_roll (nframes, 0);
785 click (_transport_frame, nframes, 0);
787 prepare_diskstreams ();
789 frames_moved = (long) floor (_transport_speed * nframes);
791 if (process_routes (nframes, 0)) {
792 no_roll (nframes, 0);
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 send_midi_time_code_for_cycle(nframes);
811 if (session_needs_butler)
816 Session::process_audition (jack_nframes_t nframes)
818 TentativeRWLockMonitor rm (route_lock, false, __LINE__, __FILE__);
822 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
823 if (!(*i)->hidden()) {
824 (*i)->silence (nframes, 0);
829 if (auditioner->play_audition (nframes) > 0) {
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;