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();
80 boost::shared_ptr<RouteList> r = routes.reader ();
83 _click_io->silence (nframes, offset);
86 if (g_atomic_int_get (&processing_prohibited)) {
87 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
88 (*i)->silence (nframes, offset);
93 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
99 (*i)->set_pending_declick (declick);
101 if ((*i)->no_roll (nframes, _transport_frame, end_frame, offset, non_realtime_work_pending(),
102 actively_recording(), declick)) {
103 error << string_compose(_("Session: error in no roll for %1"), (*i)->name()) << endmsg;
113 Session::process_routes (jack_nframes_t nframes, jack_nframes_t offset)
116 int declick = get_transport_declick_required();
117 bool rec_monitors = get_rec_monitors_input();
118 boost::shared_ptr<RouteList> r = routes.reader ();
120 if (transport_sub_state & StopPendingCapture) {
121 /* force a declick out */
125 record_active = actively_recording(); // || (get_record_enabled() && get_punch_in());
127 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
131 if ((*i)->hidden()) {
135 (*i)->set_pending_declick (declick);
137 if ((ret = (*i)->roll (nframes, _transport_frame, _transport_frame + nframes, offset, declick, record_active, rec_monitors)) < 0) {
139 /* we have to do this here. Route::roll() for an AudioTrack will have called AudioDiskstream::process(),
140 and the DS will expect AudioDiskstream::commit() to be called. but we're aborting from that
141 call path, so make sure we release any outstanding locks here before we return failure.
144 for (AudioDiskstreamList::iterator ids = audio_diskstreams.begin(); ids != audio_diskstreams.end(); ++ids) {
157 Session::silent_process_routes (jack_nframes_t nframes, jack_nframes_t offset)
159 bool record_active = actively_recording();
160 int declick = get_transport_declick_required();
161 bool rec_monitors = get_rec_monitors_input();
162 boost::shared_ptr<RouteList> r = routes.reader ();
164 if (transport_sub_state & StopPendingCapture) {
165 /* force a declick out */
169 for (RouteList::iterator i = r->begin(); i != r->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 AudioDiskstream::process(),
180 and the DS will expect AudioDiskstream::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 (AudioDiskstreamList::iterator ids = audio_diskstreams.begin(); ids != audio_diskstreams.end(); ++ids) {
197 Session::commit_diskstreams (jack_nframes_t nframes, bool &needs_butler)
203 for (AudioDiskstreamList::iterator i = audio_diskstreams.begin(); i != audio_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 = g_atomic_int_get (&_playback_load);
226 uint32_t pminold = g_atomic_int_get (&_playback_load_min);
227 uint32_t cmin = g_atomic_int_get (&_capture_load);
228 uint32_t cminold = g_atomic_int_get (&_capture_load_min);
230 g_atomic_int_set (&_playback_load, (uint32_t) floor (pworst * 100.0f));
231 g_atomic_int_set (&_capture_load, (uint32_t) floor (cworst * 100.0f));
232 g_atomic_int_set (&_playback_load_min, min (pmin, pminold));
233 g_atomic_int_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;
251 /* make sure the auditioner is silent */
254 auditioner->silence (nframes, 0);
257 /* handle any pending events */
259 while (pending_events.read (&ev, 1) == 1) {
263 /* if we are not in the middle of a state change,
264 and there are immediate events queued up,
268 while (!non_realtime_work_pending() && !immediate_events.empty()) {
269 Event *ev = immediate_events.front ();
270 immediate_events.pop_front ();
274 if (!process_can_proceed()) {
275 no_roll (nframes, 0);
279 if (events.empty() || next_event == events.end()) {
280 process_without_events (nframes);
284 end_frame = _transport_frame + nframes;
287 Glib::RWLock::ReaderLock dsm (diskstream_lock, Glib::TRY_LOCK);
290 Events::iterator the_next_one;
292 if (!dsm.locked() || (post_transport_work & (PostTransportLocate|PostTransportStop))) {
293 no_roll (nframes, 0);
297 if (!_exporting && _slave) {
298 if (!follow_slave (nframes, 0)) {
303 if (_transport_speed == 0) {
304 no_roll (nframes, 0);
308 if (actively_recording()) {
309 stop_limit = max_frames;
312 if (Config->get_stop_at_session_end()) {
313 stop_limit = current_end_frame();
315 stop_limit = max_frames;
319 if (maybe_stop (stop_limit)) {
320 no_roll (nframes, 0);
324 this_event = *next_event;
325 the_next_one = next_event;
332 if (this_event == 0 || this_event->action_frame > end_frame || this_event->action_frame < _transport_frame) {
334 this_nframes = nframes;
338 /* compute nframes to next event */
340 if (this_event->action_frame < end_frame) {
341 this_nframes = nframes - (end_frame - this_event->action_frame);
343 this_nframes = nframes;
349 click (_transport_frame, nframes, offset);
351 /* now process frames between now and the first event in this block */
352 prepare_diskstreams ();
354 if (process_routes (this_nframes, offset)) {
355 no_roll (nframes, 0);
359 commit_diskstreams (this_nframes, session_needs_butler);
361 nframes -= this_nframes;
362 offset += this_nframes;
364 frames_moved = (jack_nframes_t) floor (_transport_speed * this_nframes);
366 if (frames_moved < 0) {
367 decrement_transport_position (-frames_moved);
369 increment_transport_position (frames_moved);
372 maybe_stop (stop_limit);
373 check_declick_out ();
376 /* now handle this event and all others scheduled for the same time */
378 while (this_event && this_event->action_frame == _transport_frame) {
379 process_event (this_event);
381 if (the_next_one == events.end()) {
384 this_event = *the_next_one;
389 /* if an event left our state changing, do the right thing */
391 if (non_realtime_work_pending()) {
392 no_roll (nframes, offset);
396 /* this is necessary to handle the case of seamless looping */
397 /* not sure if it will work in conjuction with varispeed */
398 end_frame = _transport_frame + nframes;
404 } /* implicit release of route lock */
407 if (session_needs_butler) {
411 if (!_engine.freewheeling() && send_mtc) {
412 send_midi_time_code_in_another_thread ();
419 Session::reset_slave_state ()
421 average_slave_delta = 1800;
422 delta_accumulator_cnt = 0;
423 have_first_delta_accumulator = false;
424 slave_state = Stopped;
428 Session::transport_locked () const
432 if (!locate_pending() && ((_slave_type == None) || (sl && sl->ok() && sl->locked()))) {
440 Session::follow_slave (jack_nframes_t nframes, jack_nframes_t offset)
443 jack_nframes_t slave_transport_frame;
444 jack_nframes_t this_delta;
450 set_slave_source (None, 0);
454 _slave->speed_and_position (slave_speed, slave_transport_frame);
456 if (!_slave->locked()) {
460 if (slave_transport_frame > _transport_frame) {
461 this_delta = slave_transport_frame - _transport_frame;
464 this_delta = _transport_frame - slave_transport_frame;
468 if ((starting = _slave->starting())) {
473 cerr << "delta = " << (int) (dir * this_delta)
474 << " speed = " << slave_speed
475 << " ts = " << _transport_speed
476 << " M@"<< slave_transport_frame << " S@" << _transport_frame
477 << " avgdelta = " << average_slave_delta
481 if (Config->get_timecode_source_is_synced()) {
483 /* if the TC source is synced, then we assume that its
484 speed is binary: 0.0 or 1.0
487 if (slave_speed != 0.0f) {
493 /* TC source is able to drift relative to us (slave)
494 so we need to keep track of the drift and adjust
495 our speed to remain locked.
498 if (delta_accumulator_cnt >= delta_accumulator_size) {
499 have_first_delta_accumulator = true;
500 delta_accumulator_cnt = 0;
503 if (delta_accumulator_cnt != 0 || this_delta < _current_frame_rate) {
504 delta_accumulator[delta_accumulator_cnt++] = dir*this_delta;
507 if (have_first_delta_accumulator) {
508 average_slave_delta = 0;
509 for (int i = 0; i < delta_accumulator_size; ++i) {
510 average_slave_delta += delta_accumulator[i];
512 average_slave_delta /= delta_accumulator_size;
513 if (average_slave_delta < 0) {
515 average_slave_delta = -average_slave_delta;
519 // cerr << "avgdelta = " << average_slave_delta*average_dir << endl;
523 if (slave_speed != 0.0f) {
525 /* slave is running */
527 switch (slave_state) {
529 if (_slave->requires_seekahead()) {
530 slave_wait_end = slave_transport_frame + _current_frame_rate;
531 locate (slave_wait_end, false, false);
532 slave_state = Waiting;
537 slave_state = Running;
539 Location* al = _locations.auto_loop_location();
541 if (al && auto_loop && (slave_transport_frame < al->start() || slave_transport_frame > al->end())) {
543 request_auto_loop(false);
546 if (slave_transport_frame != _transport_frame) {
547 locate (slave_transport_frame, false, false);
560 if (slave_state == Waiting) {
562 // cerr << "waiting at " << slave_transport_frame << endl;
563 Glib::RWLock::ReaderLock dsm (diskstream_lock, Glib::TRY_LOCK);
565 if (dsm.locked() && slave_transport_frame >= slave_wait_end) {
566 // cerr << "\tstart at " << _transport_frame << endl;
568 slave_state = Running;
571 jack_nframes_t frame_delta = slave_transport_frame - _transport_frame;
573 for (AudioDiskstreamList::iterator i = audio_diskstreams.begin(); i != audio_diskstreams.end(); ++i) {
574 if (!(*i)->can_internal_playback_seek (frame_delta)) {
581 for (AudioDiskstreamList::iterator i = audio_diskstreams.begin(); i != audio_diskstreams.end(); ++i) {
582 (*i)->internal_playback_seek (frame_delta);
584 _transport_frame += frame_delta;
587 // cerr << "cannot micro-seek\n";
591 memset (delta_accumulator, 0, sizeof (jack_nframes_t) * delta_accumulator_size);
592 average_slave_delta = 0;
597 if (slave_state == Running && _transport_speed == 0.0f) {
599 // cerr << "slave starts transport\n";
606 /* slave has stopped */
608 if (_transport_speed != 0.0f) {
610 // cerr << "slave stops transport: " << slave_speed << " frame: " << slave_transport_frame
611 // << " tf = " << _transport_frame
614 if (_slave_type == JACK) {
615 last_stop_frame = _transport_frame;
621 if (slave_transport_frame != _transport_frame) {
622 // cerr << "slave stopped, move to " << slave_transport_frame << endl;
623 force_locate (slave_transport_frame, false);
626 slave_state = Stopped;
629 if (slave_state == Running && !Config->get_timecode_source_is_synced()) {
632 if (_transport_speed != 0.0f) {
635 note that average_dir is +1 or -1
638 const float adjust_seconds = 1.0f;
641 //if (average_slave_delta == 0) {
645 // delta = average_slave_delta;
646 // delta *= average_dir;
649 float adjusted_speed = slave_speed +
650 (delta / (adjust_seconds * _current_frame_rate));
652 // cerr << "adjust using " << delta
653 // << " towards " << adjusted_speed
654 // << " ratio = " << adjusted_speed / slave_speed
655 // << " current = " << _transport_speed
656 // << " slave @ " << slave_speed
659 request_transport_speed (adjusted_speed);
662 if ((jack_nframes_t) average_slave_delta > _slave->resolution()) {
663 // cerr << "not locked\n";
670 if (!starting && !non_realtime_work_pending()) {
671 /* speed is set, we're locked, and good to go */
677 if (slave_speed && _transport_speed) {
679 /* something isn't right, but we should move with the master
685 Glib::RWLock::ReaderLock dsm (diskstream_lock, Glib::TRY_LOCK);
691 prepare_diskstreams ();
692 silent_process_routes (nframes, offset);
693 commit_diskstreams (nframes, need_butler);
699 int32_t frames_moved = (int32_t) floor (_transport_speed * nframes);
701 if (frames_moved < 0) {
702 decrement_transport_position (-frames_moved);
704 increment_transport_position (frames_moved);
707 jack_nframes_t stop_limit;
709 if (actively_recording()) {
710 stop_limit = max_frames;
712 if (Config->get_stop_at_session_end()) {
713 stop_limit = current_end_frame();
715 stop_limit = max_frames;
719 maybe_stop (stop_limit);
723 /* don't move at all */
724 no_roll (nframes, 0);
729 Session::process_without_events (jack_nframes_t nframes)
731 bool session_needs_butler = false;
732 jack_nframes_t stop_limit;
736 Glib::RWLock::ReaderLock dsm (diskstream_lock, Glib::TRY_LOCK);
738 if (!dsm.locked() || (post_transport_work & (PostTransportLocate|PostTransportStop))) {
739 no_roll (nframes, 0);
743 if (!_exporting && _slave) {
744 if (!follow_slave (nframes, 0)) {
749 if (_transport_speed == 0) {
750 no_roll (nframes, 0);
754 if (actively_recording()) {
755 stop_limit = max_frames;
757 if (Config->get_stop_at_session_end()) {
758 stop_limit = current_end_frame();
760 stop_limit = max_frames;
764 if (maybe_stop (stop_limit)) {
765 no_roll (nframes, 0);
769 click (_transport_frame, nframes, 0);
771 prepare_diskstreams ();
773 frames_moved = (long) floor (_transport_speed * nframes);
775 if (process_routes (nframes, 0)) {
776 no_roll (nframes, 0);
780 commit_diskstreams (nframes, session_needs_butler);
782 if (frames_moved < 0) {
783 decrement_transport_position (-frames_moved);
785 increment_transport_position (frames_moved);
788 maybe_stop (stop_limit);
789 check_declick_out ();
791 } /* implicit release of route lock */
793 if (session_needs_butler) {
797 if (!_engine.freewheeling() && send_mtc) {
798 send_midi_time_code_in_another_thread ();
805 Session::process_audition (jack_nframes_t nframes)
808 boost::shared_ptr<RouteList> r = routes.reader ();
810 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
811 if (!(*i)->hidden()) {
812 (*i)->silence (nframes, 0);
816 /* run the auditioner, and if it says we need butler service, ask for it */
818 if (auditioner->play_audition (nframes) > 0) {
822 /* handle pending events */
824 while (pending_events.read (&ev, 1) == 1) {
828 /* if we are not in the middle of a state change,
829 and there are immediate events queued up,
833 while (!non_realtime_work_pending() && !immediate_events.empty()) {
834 Event *ev = immediate_events.front ();
835 immediate_events.pop_front ();
839 if (!auditioner->active()) {
840 process_function = &Session::process_with_events;