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);
63 SendFeedback (); /* EMIT SIGNAL */
67 Session::prepare_diskstreams ()
69 for (AudioDiskstreamList::iterator i = audio_diskstreams.begin(); i != audio_diskstreams.end(); ++i) {
75 Session::no_roll (jack_nframes_t nframes, jack_nframes_t offset)
77 jack_nframes_t end_frame = _transport_frame + nframes;
79 bool declick = get_transport_declick_required();
82 _click_io->silence (nframes, offset);
85 /* XXX we're supposed to have the route_lock while doing this.
86 this is really bad ...
89 if (g_atomic_int_get (&processing_prohibited)) {
90 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
91 (*i)->silence (nframes, offset);
96 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
102 (*i)->set_pending_declick (declick);
104 if ((*i)->no_roll (nframes, _transport_frame, end_frame, offset, non_realtime_work_pending(),
105 actively_recording(), declick)) {
106 error << string_compose(_("Session: error in no roll for %1"), (*i)->name()) << endmsg;
116 Session::process_routes (jack_nframes_t nframes, jack_nframes_t offset)
119 int declick = get_transport_declick_required();
120 bool rec_monitors = get_rec_monitors_input();
122 if (transport_sub_state & StopPendingCapture) {
123 /* force a declick out */
127 record_active = actively_recording(); // || (get_record_enabled() && get_punch_in());
129 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
133 if ((*i)->hidden()) {
137 (*i)->set_pending_declick (declick);
139 if ((ret = (*i)->roll (nframes, _transport_frame, _transport_frame + nframes, offset, declick, record_active, rec_monitors)) < 0) {
141 /* we have to do this here. Route::roll() for an AudioTrack will have called AudioDiskstream::process(),
142 and the DS will expect AudioDiskstream::commit() to be called. but we're aborting from that
143 call path, so make sure we release any outstanding locks here before we return failure.
146 for (AudioDiskstreamList::iterator ids = audio_diskstreams.begin(); ids != audio_diskstreams.end(); ++ids) {
159 Session::silent_process_routes (jack_nframes_t nframes, jack_nframes_t offset)
161 bool record_active = actively_recording();
162 int declick = get_transport_declick_required();
163 bool rec_monitors = get_rec_monitors_input();
165 if (transport_sub_state & StopPendingCapture) {
166 /* force a declick out */
170 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
174 if ((*i)->hidden()) {
178 if ((ret = (*i)->silent_roll (nframes, _transport_frame, _transport_frame + nframes, offset, record_active, rec_monitors)) < 0) {
180 /* we have to do this here. Route::roll() for an AudioTrack will have called AudioDiskstream::process(),
181 and the DS will expect AudioDiskstream::commit() to be called. but we're aborting from that
182 call path, so make sure we release any outstanding locks here before we return failure.
185 for (AudioDiskstreamList::iterator ids = audio_diskstreams.begin(); ids != audio_diskstreams.end(); ++ids) {
198 Session::commit_diskstreams (jack_nframes_t nframes, bool &needs_butler)
204 for (AudioDiskstreamList::iterator i = audio_diskstreams.begin(); i != audio_diskstreams.end(); ++i) {
206 if ((*i)->hidden()) {
210 /* force all diskstreams not handled by a Route to call do their stuff.
213 if ((dret = (*i)->process (_transport_frame, nframes, 0, actively_recording(), get_rec_monitors_input())) == 0) {
214 if ((*i)->commit (nframes)) {
218 } else if (dret < 0) {
222 pworst = min (pworst, (*i)->playback_buffer_load());
223 cworst = min (cworst, (*i)->capture_buffer_load());
226 uint32_t pmin = g_atomic_int_get (&_playback_load);
227 uint32_t pminold = g_atomic_int_get (&_playback_load_min);
228 uint32_t cmin = g_atomic_int_get (&_capture_load);
229 uint32_t cminold = g_atomic_int_get (&_capture_load_min);
231 g_atomic_int_set (&_playback_load, (uint32_t) floor (pworst * 100.0f));
232 g_atomic_int_set (&_capture_load, (uint32_t) floor (cworst * 100.0f));
233 g_atomic_int_set (&_playback_load_min, min (pmin, pminold));
234 g_atomic_int_set (&_capture_load_min, min (cmin, cminold));
236 if (actively_recording()) {
242 Session::process_with_events (jack_nframes_t nframes)
245 jack_nframes_t this_nframes;
246 jack_nframes_t end_frame;
247 jack_nframes_t offset;
248 bool session_needs_butler = false;
249 jack_nframes_t stop_limit;
253 auditioner->silence (nframes, 0);
256 while (pending_events.read (&ev, 1) == 1) {
260 /* if we are not in the middle of a state change,
261 and there are immediate events queued up,
265 while (!non_realtime_work_pending() && !immediate_events.empty()) {
266 Event *ev = immediate_events.front ();
267 immediate_events.pop_front ();
271 if (!process_can_proceed()) {
272 no_roll (nframes, 0);
276 if (events.empty() || next_event == events.end()) {
277 process_without_events (nframes);
281 end_frame = _transport_frame + nframes;
284 Glib::RWLock::ReaderLock rm (route_lock, Glib::TRY_LOCK);
285 Glib::RWLock::ReaderLock dsm (diskstream_lock, Glib::TRY_LOCK);
288 Events::iterator the_next_one;
290 if (!rm.locked() || !dsm.locked() || (post_transport_work & (PostTransportLocate|PostTransportStop))) {
291 no_roll (nframes, 0);
295 if (!_exporting && _slave) {
296 if (!follow_slave (nframes, 0)) {
301 if (_transport_speed == 0) {
302 no_roll (nframes, 0);
306 if (actively_recording()) {
307 stop_limit = max_frames;
310 if (Config->get_stop_at_session_end()) {
311 stop_limit = current_end_frame();
313 stop_limit = max_frames;
317 if (maybe_stop (stop_limit)) {
318 no_roll (nframes, 0);
322 this_event = *next_event;
323 the_next_one = next_event;
330 if (this_event == 0 || this_event->action_frame > end_frame || this_event->action_frame < _transport_frame) {
332 this_nframes = nframes;
336 /* compute nframes to next event */
338 if (this_event->action_frame < end_frame) {
339 this_nframes = nframes - (end_frame - this_event->action_frame);
341 this_nframes = nframes;
347 click (_transport_frame, nframes, offset);
349 /* now process frames between now and the first event in this block */
350 prepare_diskstreams ();
352 if (process_routes (this_nframes, offset)) {
353 no_roll (nframes, 0);
357 commit_diskstreams (this_nframes, session_needs_butler);
359 nframes -= this_nframes;
360 offset += this_nframes;
362 frames_moved = (jack_nframes_t) floor (_transport_speed * this_nframes);
364 if (frames_moved < 0) {
365 decrement_transport_position (-frames_moved);
367 increment_transport_position (frames_moved);
370 maybe_stop (stop_limit);
371 check_declick_out ();
374 /* now handle this event and all others scheduled for the same time */
376 while (this_event && this_event->action_frame == _transport_frame) {
377 process_event (this_event);
379 if (the_next_one == events.end()) {
382 this_event = *the_next_one;
387 /* if an event left our state changing, do the right thing */
389 if (non_realtime_work_pending()) {
390 no_roll (nframes, offset);
394 /* this is necessary to handle the case of seamless looping */
395 /* not sure if it will work in conjuction with varispeed */
396 end_frame = _transport_frame + nframes;
402 } /* implicit release of route lock */
405 if (session_needs_butler) {
409 if (!_engine.freewheeling() && send_mtc) {
410 send_midi_time_code_in_another_thread ();
417 Session::reset_slave_state ()
419 average_slave_delta = 1800;
420 delta_accumulator_cnt = 0;
421 have_first_delta_accumulator = false;
422 slave_state = Stopped;
426 Session::transport_locked () const
430 if (!locate_pending() && ((_slave_type == None) || (sl && sl->ok() && sl->locked()))) {
438 Session::follow_slave (jack_nframes_t nframes, jack_nframes_t offset)
441 jack_nframes_t slave_transport_frame;
442 jack_nframes_t this_delta;
448 set_slave_source (None, 0);
452 _slave->speed_and_position (slave_speed, slave_transport_frame);
454 if (!_slave->locked()) {
458 if (slave_transport_frame > _transport_frame) {
459 this_delta = slave_transport_frame - _transport_frame;
462 this_delta = _transport_frame - slave_transport_frame;
466 if ((starting = _slave->starting())) {
471 cerr << "delta = " << (int) (dir * this_delta)
472 << " speed = " << slave_speed
473 << " ts = " << _transport_speed
474 << " M@"<< slave_transport_frame << " S@" << _transport_frame
475 << " avgdelta = " << average_slave_delta
479 if (Config->get_timecode_source_is_synced()) {
481 /* if the TC source is synced, then we assume that its
482 speed is binary: 0.0 or 1.0
485 if (slave_speed != 0.0f) {
491 /* TC source is able to drift relative to us (slave)
492 so we need to keep track of the drift and adjust
493 our speed to remain locked.
496 if (delta_accumulator_cnt >= delta_accumulator_size) {
497 have_first_delta_accumulator = true;
498 delta_accumulator_cnt = 0;
501 if (delta_accumulator_cnt != 0 || this_delta < _current_frame_rate) {
502 delta_accumulator[delta_accumulator_cnt++] = dir*this_delta;
505 if (have_first_delta_accumulator) {
506 average_slave_delta = 0;
507 for (int i = 0; i < delta_accumulator_size; ++i) {
508 average_slave_delta += delta_accumulator[i];
510 average_slave_delta /= delta_accumulator_size;
511 if (average_slave_delta < 0) {
513 average_slave_delta = -average_slave_delta;
517 // cerr << "avgdelta = " << average_slave_delta*average_dir << endl;
521 if (slave_speed != 0.0f) {
523 /* slave is running */
525 switch (slave_state) {
527 if (_slave->requires_seekahead()) {
528 slave_wait_end = slave_transport_frame + _current_frame_rate;
529 locate (slave_wait_end, false, false);
530 slave_state = Waiting;
535 slave_state = Running;
537 Location* al = _locations.auto_loop_location();
539 if (al && auto_loop && (slave_transport_frame < al->start() || slave_transport_frame > al->end())) {
541 request_auto_loop(false);
544 if (slave_transport_frame != _transport_frame) {
545 locate (slave_transport_frame, false, false);
558 if (slave_state == Waiting) {
560 // cerr << "waiting at " << slave_transport_frame << endl;
561 Glib::RWLock::ReaderLock dsm (diskstream_lock, Glib::TRY_LOCK);
563 if (dsm.locked() && slave_transport_frame >= slave_wait_end) {
564 // cerr << "\tstart at " << _transport_frame << endl;
566 slave_state = Running;
569 jack_nframes_t frame_delta = slave_transport_frame - _transport_frame;
571 for (AudioDiskstreamList::iterator i = audio_diskstreams.begin(); i != audio_diskstreams.end(); ++i) {
572 if (!(*i)->can_internal_playback_seek (frame_delta)) {
579 for (AudioDiskstreamList::iterator i = audio_diskstreams.begin(); i != audio_diskstreams.end(); ++i) {
580 (*i)->internal_playback_seek (frame_delta);
582 _transport_frame += frame_delta;
585 // cerr << "cannot micro-seek\n";
589 memset (delta_accumulator, 0, sizeof (jack_nframes_t) * delta_accumulator_size);
590 average_slave_delta = 0;
595 if (slave_state == Running && _transport_speed == 0.0f) {
597 // cerr << "slave starts transport\n";
604 /* slave has stopped */
606 if (_transport_speed != 0.0f) {
608 // cerr << "slave stops transport: " << slave_speed << " frame: " << slave_transport_frame
609 // << " tf = " << _transport_frame
612 if (_slave_type == JACK) {
613 last_stop_frame = _transport_frame;
619 if (slave_transport_frame != _transport_frame) {
620 // cerr << "slave stopped, move to " << slave_transport_frame << endl;
621 force_locate (slave_transport_frame, false);
624 slave_state = Stopped;
627 if (slave_state == Running && !Config->get_timecode_source_is_synced()) {
630 if (_transport_speed != 0.0f) {
633 note that average_dir is +1 or -1
636 const float adjust_seconds = 1.0f;
639 //if (average_slave_delta == 0) {
643 // delta = average_slave_delta;
644 // delta *= average_dir;
647 float adjusted_speed = slave_speed +
648 (delta / (adjust_seconds * _current_frame_rate));
650 // cerr << "adjust using " << delta
651 // << " towards " << adjusted_speed
652 // << " ratio = " << adjusted_speed / slave_speed
653 // << " current = " << _transport_speed
654 // << " slave @ " << slave_speed
657 request_transport_speed (adjusted_speed);
660 if ((jack_nframes_t) average_slave_delta > _slave->resolution()) {
661 // cerr << "not locked\n";
668 if (!starting && !non_realtime_work_pending()) {
669 /* speed is set, we're locked, and good to go */
675 if (slave_speed && _transport_speed) {
677 /* something isn't right, but we should move with the master
683 Glib::RWLock::ReaderLock dsm (diskstream_lock, Glib::TRY_LOCK);
689 prepare_diskstreams ();
690 silent_process_routes (nframes, offset);
691 commit_diskstreams (nframes, need_butler);
697 jack_nframes_t frames_moved = (long) floor (_transport_speed * nframes);
699 if (frames_moved < 0) {
700 decrement_transport_position (-frames_moved);
702 increment_transport_position (frames_moved);
705 jack_nframes_t stop_limit;
707 if (actively_recording()) {
708 stop_limit = max_frames;
710 if (Config->get_stop_at_session_end()) {
711 stop_limit = current_end_frame();
713 stop_limit = max_frames;
717 maybe_stop (stop_limit);
721 /* don't move at all */
722 no_roll (nframes, 0);
727 Session::process_without_events (jack_nframes_t nframes)
729 bool session_needs_butler = false;
730 jack_nframes_t stop_limit;
734 Glib::RWLock::ReaderLock rm (route_lock, Glib::TRY_LOCK);
735 Glib::RWLock::ReaderLock dsm (diskstream_lock, Glib::TRY_LOCK);
737 if (!rm.locked() || !dsm.locked() || (post_transport_work & (PostTransportLocate|PostTransportStop))) {
738 no_roll (nframes, 0);
742 if (!_exporting && _slave) {
743 if (!follow_slave (nframes, 0)) {
748 if (_transport_speed == 0) {
749 no_roll (nframes, 0);
753 if (actively_recording()) {
754 stop_limit = max_frames;
756 if (Config->get_stop_at_session_end()) {
757 stop_limit = current_end_frame();
759 stop_limit = max_frames;
763 if (maybe_stop (stop_limit)) {
764 no_roll (nframes, 0);
768 click (_transport_frame, nframes, 0);
770 prepare_diskstreams ();
772 frames_moved = (long) floor (_transport_speed * nframes);
774 if (process_routes (nframes, 0)) {
775 no_roll (nframes, 0);
779 commit_diskstreams (nframes, session_needs_butler);
781 if (frames_moved < 0) {
782 decrement_transport_position (-frames_moved);
784 increment_transport_position (frames_moved);
787 maybe_stop (stop_limit);
788 check_declick_out ();
790 } /* implicit release of route lock */
792 if (session_needs_butler) {
796 if (!_engine.freewheeling() && send_mtc) {
797 send_midi_time_code_in_another_thread ();
804 Session::process_audition (jack_nframes_t nframes)
806 Glib::RWLock::ReaderLock rm (route_lock, Glib::TRY_LOCK);
810 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
811 if (!(*i)->hidden()) {
812 (*i)->silence (nframes, 0);
817 if (auditioner->play_audition (nframes) > 0) {
821 while (pending_events.read (&ev, 1) == 1) {
825 /* if we are not in the middle of a state change,
826 and there are immediate events queued up,
830 while (!non_realtime_work_pending() && !immediate_events.empty()) {
831 Event *ev = immediate_events.front ();
832 immediate_events.pop_front ();
836 if (!auditioner->active()) {
837 process_function = &Session::process_with_events;