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/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;
45 //using namespace sigc;
49 Session::process (jack_nframes_t nframes)
51 //cerr << "CYCLE START " << _transport_frame << "-------------------" << endl;
53 MIDI::Manager::instance()->cycle_start(nframes);
55 if (synced_to_jack() && waiting_to_start) {
56 if ( _engine.transport_state() == AudioEngine::TransportRolling) {
57 actually_start_transport ();
61 if (non_realtime_work_pending()) {
62 if (g_atomic_int_get (&butler_should_do_transport_work) == 0) {
67 (this->*process_function) (nframes);
69 MIDI::Manager::instance()->cycle_end();
71 //cerr << "CYCLE END " << _transport_frame << "-----------------------" << endl;
75 Session::prepare_diskstreams ()
77 for (DiskStreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
83 Session::no_roll (jack_nframes_t nframes, jack_nframes_t offset)
85 jack_nframes_t end_frame = _transport_frame + nframes;
87 bool declick = get_transport_declick_required();
90 _click_io->silence (nframes, offset);
93 /* XXX we're supposed to have the route_lock while doing this.
94 this is really bad ...
97 if (g_atomic_int_get (&processing_prohibited)) {
98 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
99 (*i)->silence (nframes, offset);
104 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
106 if ((*i)->hidden()) {
110 (*i)->set_pending_declick (declick);
112 if ((*i)->no_roll (nframes, _transport_frame, end_frame, offset, non_realtime_work_pending(),
113 actively_recording(), declick)) {
114 error << string_compose(_("Session: error in no roll for %1"), (*i)->name()) << endmsg;
124 Session::process_routes (jack_nframes_t nframes, jack_nframes_t offset)
127 int declick = get_transport_declick_required();
128 bool rec_monitors = get_rec_monitors_input();
130 if (transport_sub_state & StopPendingCapture) {
131 /* force a declick out */
135 record_active = actively_recording(); // || (get_record_enabled() && get_punch_in());
137 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
141 if ((*i)->hidden()) {
145 (*i)->set_pending_declick (declick);
147 if ((ret = (*i)->roll (nframes, _transport_frame, _transport_frame + nframes, offset, declick, record_active, rec_monitors)) < 0) {
149 /* we have to do this here. Route::roll() for an AudioTrack will have called DiskStream::process(),
150 and the DS will expect DiskStream::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 for (DiskStreamList::iterator ids = diskstreams.begin(); ids != diskstreams.end(); ++ids) {
167 Session::silent_process_routes (jack_nframes_t nframes, jack_nframes_t offset)
169 bool record_active = actively_recording();
170 int declick = get_transport_declick_required();
171 bool rec_monitors = get_rec_monitors_input();
173 if (transport_sub_state & StopPendingCapture) {
174 /* force a declick out */
178 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
182 if ((*i)->hidden()) {
186 if ((ret = (*i)->silent_roll (nframes, _transport_frame, _transport_frame + nframes, offset, record_active, rec_monitors)) < 0) {
188 /* we have to do this here. Route::roll() for an AudioTrack will have called DiskStream::process(),
189 and the DS will expect DiskStream::commit() to be called. but we're aborting from that
190 call path, so make sure we release any outstanding locks here before we return failure.
193 for (DiskStreamList::iterator ids = diskstreams.begin(); ids != diskstreams.end(); ++ids) {
206 Session::commit_diskstreams (jack_nframes_t nframes, bool &needs_butler)
212 for (DiskStreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
214 if ((*i)->hidden()) {
218 /* force all diskstreams not handled by a Route to call do their stuff.
221 if ((dret = (*i)->process (_transport_frame, nframes, 0, actively_recording(), get_rec_monitors_input())) == 0) {
222 if ((*i)->commit (nframes)) {
226 } else if (dret < 0) {
230 pworst = min (pworst, (*i)->playback_buffer_load());
231 cworst = min (cworst, (*i)->capture_buffer_load());
234 uint32_t pmin = g_atomic_int_get (&_playback_load);
235 uint32_t pminold = g_atomic_int_get (&_playback_load_min);
236 uint32_t cmin = g_atomic_int_get (&_capture_load);
237 uint32_t cminold = g_atomic_int_get (&_capture_load_min);
239 g_atomic_int_set (&_playback_load, (uint32_t) floor (pworst * 100.0f));
240 g_atomic_int_set (&_capture_load, (uint32_t) floor (cworst * 100.0f));
241 g_atomic_int_set (&_playback_load_min, min (pmin, pminold));
242 g_atomic_int_set (&_capture_load_min, min (cmin, cminold));
244 if (actively_recording()) {
251 Session::process_with_events (jack_nframes_t nframes)
254 jack_nframes_t this_nframes;
255 jack_nframes_t end_frame;
256 jack_nframes_t offset;
257 jack_nframes_t stop_limit;
259 bool session_needs_butler = false;
262 auditioner->silence (nframes, 0);
265 while (pending_events.read (&ev, 1) == 1) {
269 /* if we are not in the middle of a state change,
270 and there are immediate events queued up,
274 while (!non_realtime_work_pending() && !immediate_events.empty()) {
275 Event *ev = immediate_events.front ();
276 immediate_events.pop_front ();
280 /* Events caused a transport change, send an MTC Full Frame (SMPTE) message.
281 * This is sent whether rolling or not, to give slaves an idea of ardour time
282 * on locates (and allow slow slaves to position and prepare for rolling)
284 if (_send_smpte_update) {
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 Glib::RWLock::ReaderLock rm (route_lock, Glib::TRY_LOCK);
302 Glib::RWLock::ReaderLock dsm (diskstream_lock, Glib::TRY_LOCK);
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 send_midi_time_code_for_cycle(nframes);
325 if (actively_recording()) {
326 stop_limit = max_frames;
329 if (Config->get_stop_at_session_end()) {
330 stop_limit = current_end_frame();
332 stop_limit = max_frames;
336 if (maybe_stop (stop_limit)) {
337 no_roll (nframes, 0);
341 this_event = *next_event;
342 the_next_one = next_event;
349 if (this_event == 0 || this_event->action_frame > end_frame || this_event->action_frame < _transport_frame) {
351 this_nframes = nframes;
355 /* compute nframes to next event */
357 if (this_event->action_frame < end_frame) {
358 this_nframes = nframes - (end_frame - this_event->action_frame);
360 this_nframes = nframes;
366 click (_transport_frame, nframes, offset);
368 /* now process frames between now and the first event in this block */
369 prepare_diskstreams ();
371 if (process_routes (this_nframes, offset)) {
372 no_roll (nframes, 0);
376 commit_diskstreams (this_nframes, session_needs_butler);
378 nframes -= this_nframes;
379 offset += this_nframes;
381 frames_moved = (jack_nframes_t) floor (_transport_speed * this_nframes);
383 if (frames_moved < 0) {
384 decrement_transport_position (-frames_moved);
386 increment_transport_position (frames_moved);
389 maybe_stop (stop_limit);
390 check_declick_out ();
393 /* now handle this event and all others scheduled for the same time */
395 while (this_event && this_event->action_frame == _transport_frame) {
396 process_event (this_event);
398 if (the_next_one == events.end()) {
401 this_event = *the_next_one;
406 /* if an event left our state changing, do the right thing */
408 if (non_realtime_work_pending()) {
409 no_roll (nframes, offset);
413 /* this is necessary to handle the case of seamless looping */
414 /* not sure if it will work in conjuction with varispeed */
415 end_frame = _transport_frame + nframes;
421 } /* implicit release of route lock */
423 if (session_needs_butler)
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 Glib::RWLock::ReaderLock dsm (diskstream_lock, Glib::TRY_LOCK);
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 Glib::RWLock::ReaderLock dsm (diskstream_lock, Glib::TRY_LOCK);
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 Glib::RWLock::ReaderLock rm (route_lock, Glib::TRY_LOCK);
746 Glib::RWLock::ReaderLock dsm (diskstream_lock, Glib::TRY_LOCK);
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 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 click (_transport_frame, nframes, 0);
783 prepare_diskstreams ();
785 frames_moved = (long) floor (_transport_speed * nframes);
787 if (process_routes (nframes, 0)) {
788 no_roll (nframes, 0);
792 commit_diskstreams (nframes, session_needs_butler);
794 if (frames_moved < 0) {
795 decrement_transport_position (-frames_moved);
797 increment_transport_position (frames_moved);
800 maybe_stop (stop_limit);
801 check_declick_out ();
803 } /* implicit release of route lock */
805 if (session_needs_butler)
810 Session::process_audition (jack_nframes_t nframes)
812 Glib::RWLock::ReaderLock rm (route_lock, Glib::TRY_LOCK);
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;