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 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
70 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
76 Session::no_roll (jack_nframes_t nframes, jack_nframes_t offset)
78 jack_nframes_t end_frame = _transport_frame + nframes;
80 bool declick = get_transport_declick_required();
81 boost::shared_ptr<RouteList> r = routes.reader ();
84 _click_io->silence (nframes, offset);
87 if (g_atomic_int_get (&processing_prohibited)) {
88 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
89 (*i)->silence (nframes, offset);
94 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
100 (*i)->set_pending_declick (declick);
102 if ((*i)->no_roll (nframes, _transport_frame, end_frame, offset, non_realtime_work_pending(),
103 actively_recording(), declick)) {
104 error << string_compose(_("Session: error in no roll for %1"), (*i)->name()) << endmsg;
114 Session::process_routes (jack_nframes_t nframes, jack_nframes_t offset)
117 int declick = get_transport_declick_required();
118 bool rec_monitors = get_rec_monitors_input();
119 boost::shared_ptr<RouteList> r = routes.reader ();
121 if (transport_sub_state & StopPendingCapture) {
122 /* force a declick out */
126 record_active = actively_recording(); // || (get_record_enabled() && get_punch_in());
128 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
132 if ((*i)->hidden()) {
136 (*i)->set_pending_declick (declick);
138 if ((ret = (*i)->roll (nframes, _transport_frame, _transport_frame + nframes, offset, declick, record_active, rec_monitors)) < 0) {
140 /* we have to do this here. Route::roll() for an AudioTrack will have called AudioDiskstream::process(),
141 and the DS will expect AudioDiskstream::commit() to be called. but we're aborting from that
142 call path, so make sure we release any outstanding locks here before we return failure.
145 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
146 for (DiskstreamList::iterator ids = dsl->begin(); ids != dsl->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();
164 boost::shared_ptr<RouteList> r = routes.reader ();
166 if (transport_sub_state & StopPendingCapture) {
167 /* force a declick out */
171 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
175 if ((*i)->hidden()) {
179 if ((ret = (*i)->silent_roll (nframes, _transport_frame, _transport_frame + nframes, offset, record_active, rec_monitors)) < 0) {
181 /* we have to do this here. Route::roll() for an AudioTrack will have called AudioDiskstream::process(),
182 and the DS will expect AudioDiskstream::commit() to be called. but we're aborting from that
183 call path, so make sure we release any outstanding locks here before we return failure.
186 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
187 for (DiskstreamList::iterator ids = dsl->begin(); ids != dsl->end(); ++ids) {
200 Session::commit_diskstreams (jack_nframes_t nframes, bool &needs_butler)
206 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
207 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
209 if ((*i)->hidden()) {
213 /* force all diskstreams not handled by a Route to call do their stuff.
216 if ((dret = (*i)->process (_transport_frame, nframes, 0, actively_recording(), get_rec_monitors_input())) == 0) {
217 if ((*i)->commit (nframes)) {
221 } else if (dret < 0) {
225 pworst = min (pworst, (*i)->playback_buffer_load());
226 cworst = min (cworst, (*i)->capture_buffer_load());
229 uint32_t pmin = g_atomic_int_get (&_playback_load);
230 uint32_t pminold = g_atomic_int_get (&_playback_load_min);
231 uint32_t cmin = g_atomic_int_get (&_capture_load);
232 uint32_t cminold = g_atomic_int_get (&_capture_load_min);
234 g_atomic_int_set (&_playback_load, (uint32_t) floor (pworst * 100.0f));
235 g_atomic_int_set (&_capture_load, (uint32_t) floor (cworst * 100.0f));
236 g_atomic_int_set (&_playback_load_min, min (pmin, pminold));
237 g_atomic_int_set (&_capture_load_min, min (cmin, cminold));
239 if (actively_recording()) {
245 Session::process_with_events (jack_nframes_t nframes)
248 jack_nframes_t this_nframes;
249 jack_nframes_t end_frame;
250 jack_nframes_t offset;
251 bool session_needs_butler = false;
252 jack_nframes_t stop_limit;
255 /* make sure the auditioner is silent */
258 auditioner->silence (nframes, 0);
261 /* handle any pending events */
263 while (pending_events.read (&ev, 1) == 1) {
267 /* if we are not in the middle of a state change,
268 and there are immediate events queued up,
272 while (!non_realtime_work_pending() && !immediate_events.empty()) {
273 Event *ev = immediate_events.front ();
274 immediate_events.pop_front ();
278 if (!process_can_proceed()) {
279 no_roll (nframes, 0);
283 if (events.empty() || next_event == events.end()) {
284 process_without_events (nframes);
288 end_frame = _transport_frame + nframes;
292 Events::iterator the_next_one;
294 if (post_transport_work & (PostTransportLocate|PostTransportStop)) {
295 no_roll (nframes, 0);
299 if (!_exporting && _slave) {
300 if (!follow_slave (nframes, 0)) {
305 if (_transport_speed == 0) {
306 no_roll (nframes, 0);
310 if (actively_recording()) {
311 stop_limit = max_frames;
314 if (Config->get_stop_at_session_end()) {
315 stop_limit = current_end_frame();
317 stop_limit = max_frames;
321 if (maybe_stop (stop_limit)) {
322 no_roll (nframes, 0);
326 this_event = *next_event;
327 the_next_one = next_event;
334 if (this_event == 0 || this_event->action_frame > end_frame || this_event->action_frame < _transport_frame) {
336 this_nframes = nframes;
340 /* compute nframes to next event */
342 if (this_event->action_frame < end_frame) {
343 this_nframes = nframes - (end_frame - this_event->action_frame);
345 this_nframes = nframes;
351 click (_transport_frame, nframes, offset);
353 /* now process frames between now and the first event in this block */
354 prepare_diskstreams ();
356 if (process_routes (this_nframes, offset)) {
357 no_roll (nframes, 0);
361 commit_diskstreams (this_nframes, session_needs_butler);
363 nframes -= this_nframes;
364 offset += this_nframes;
366 frames_moved = (jack_nframes_t) floor (_transport_speed * this_nframes);
368 if (frames_moved < 0) {
369 decrement_transport_position (-frames_moved);
371 increment_transport_position (frames_moved);
374 maybe_stop (stop_limit);
375 check_declick_out ();
378 /* now handle this event and all others scheduled for the same time */
380 while (this_event && this_event->action_frame == _transport_frame) {
381 process_event (this_event);
383 if (the_next_one == events.end()) {
386 this_event = *the_next_one;
391 /* if an event left our state changing, do the right thing */
393 if (non_realtime_work_pending()) {
394 no_roll (nframes, offset);
398 /* this is necessary to handle the case of seamless looping */
399 /* not sure if it will work in conjuction with varispeed */
400 end_frame = _transport_frame + nframes;
406 } /* implicit release of route lock */
409 if (session_needs_butler) {
413 if (!_engine.freewheeling() && send_mtc) {
414 send_midi_time_code_in_another_thread ();
421 Session::reset_slave_state ()
423 average_slave_delta = 1800;
424 delta_accumulator_cnt = 0;
425 have_first_delta_accumulator = false;
426 slave_state = Stopped;
430 Session::transport_locked () const
434 if (!locate_pending() && ((_slave_type == None) || (sl && sl->ok() && sl->locked()))) {
442 Session::follow_slave (jack_nframes_t nframes, jack_nframes_t offset)
445 jack_nframes_t slave_transport_frame;
446 jack_nframes_t this_delta;
452 set_slave_source (None, 0);
456 _slave->speed_and_position (slave_speed, slave_transport_frame);
458 if (!_slave->locked()) {
462 if (slave_transport_frame > _transport_frame) {
463 this_delta = slave_transport_frame - _transport_frame;
466 this_delta = _transport_frame - slave_transport_frame;
470 if ((starting = _slave->starting())) {
475 cerr << "delta = " << (int) (dir * this_delta)
476 << " speed = " << slave_speed
477 << " ts = " << _transport_speed
478 << " M@"<< slave_transport_frame << " S@" << _transport_frame
479 << " avgdelta = " << average_slave_delta
483 if (Config->get_timecode_source_is_synced()) {
485 /* if the TC source is synced, then we assume that its
486 speed is binary: 0.0 or 1.0
489 if (slave_speed != 0.0f) {
495 /* TC source is able to drift relative to us (slave)
496 so we need to keep track of the drift and adjust
497 our speed to remain locked.
500 if (delta_accumulator_cnt >= delta_accumulator_size) {
501 have_first_delta_accumulator = true;
502 delta_accumulator_cnt = 0;
505 if (delta_accumulator_cnt != 0 || this_delta < _current_frame_rate) {
506 delta_accumulator[delta_accumulator_cnt++] = dir*this_delta;
509 if (have_first_delta_accumulator) {
510 average_slave_delta = 0;
511 for (int i = 0; i < delta_accumulator_size; ++i) {
512 average_slave_delta += delta_accumulator[i];
514 average_slave_delta /= delta_accumulator_size;
515 if (average_slave_delta < 0) {
517 average_slave_delta = -average_slave_delta;
521 // cerr << "avgdelta = " << average_slave_delta*average_dir << endl;
525 if (slave_speed != 0.0f) {
527 /* slave is running */
529 switch (slave_state) {
531 if (_slave->requires_seekahead()) {
532 slave_wait_end = slave_transport_frame + _current_frame_rate;
533 locate (slave_wait_end, false, false);
534 slave_state = Waiting;
539 slave_state = Running;
541 Location* al = _locations.auto_loop_location();
543 if (al && auto_loop && (slave_transport_frame < al->start() || slave_transport_frame > al->end())) {
545 request_auto_loop(false);
548 if (slave_transport_frame != _transport_frame) {
549 locate (slave_transport_frame, false, false);
562 if (slave_state == Waiting) {
564 // cerr << "waiting at " << slave_transport_frame << endl;
566 if (slave_transport_frame >= slave_wait_end) {
567 // cerr << "\tstart at " << _transport_frame << endl;
569 slave_state = Running;
572 jack_nframes_t frame_delta = slave_transport_frame - _transport_frame;
574 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
576 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
577 if (!(*i)->can_internal_playback_seek (frame_delta)) {
584 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
585 (*i)->internal_playback_seek (frame_delta);
587 _transport_frame += frame_delta;
590 // cerr << "cannot micro-seek\n";
594 memset (delta_accumulator, 0, sizeof (jack_nframes_t) * delta_accumulator_size);
595 average_slave_delta = 0;
600 if (slave_state == Running && _transport_speed == 0.0f) {
602 // cerr << "slave starts transport\n";
609 /* slave has stopped */
611 if (_transport_speed != 0.0f) {
613 // cerr << "slave stops transport: " << slave_speed << " frame: " << slave_transport_frame
614 // << " tf = " << _transport_frame
617 if (_slave_type == JACK) {
618 last_stop_frame = _transport_frame;
624 if (slave_transport_frame != _transport_frame) {
625 // cerr << "slave stopped, move to " << slave_transport_frame << endl;
626 force_locate (slave_transport_frame, false);
629 slave_state = Stopped;
632 if (slave_state == Running && !Config->get_timecode_source_is_synced()) {
635 if (_transport_speed != 0.0f) {
638 note that average_dir is +1 or -1
641 const float adjust_seconds = 1.0f;
644 //if (average_slave_delta == 0) {
648 // delta = average_slave_delta;
649 // delta *= average_dir;
652 float adjusted_speed = slave_speed +
653 (delta / (adjust_seconds * _current_frame_rate));
655 // cerr << "adjust using " << delta
656 // << " towards " << adjusted_speed
657 // << " ratio = " << adjusted_speed / slave_speed
658 // << " current = " << _transport_speed
659 // << " slave @ " << slave_speed
662 request_transport_speed (adjusted_speed);
665 if ((jack_nframes_t) average_slave_delta > _slave->resolution()) {
666 // cerr << "not locked\n";
673 if (!starting && !non_realtime_work_pending()) {
674 /* speed is set, we're locked, and good to go */
680 if (slave_speed && _transport_speed) {
682 /* something isn't right, but we should move with the master
688 prepare_diskstreams ();
689 silent_process_routes (nframes, offset);
690 commit_diskstreams (nframes, need_butler);
696 int32_t frames_moved = (int32_t) floor (_transport_speed * nframes);
698 if (frames_moved < 0) {
699 decrement_transport_position (-frames_moved);
701 increment_transport_position (frames_moved);
704 jack_nframes_t stop_limit;
706 if (actively_recording()) {
707 stop_limit = max_frames;
709 if (Config->get_stop_at_session_end()) {
710 stop_limit = current_end_frame();
712 stop_limit = max_frames;
716 maybe_stop (stop_limit);
720 /* don't move at all */
721 no_roll (nframes, 0);
726 Session::process_without_events (jack_nframes_t nframes)
728 bool session_needs_butler = false;
729 jack_nframes_t stop_limit;
731 jack_nframes_t offset = 0;
734 if (post_transport_work & (PostTransportLocate|PostTransportStop)) {
735 no_roll (nframes, 0);
739 if (!_exporting && _slave) {
740 if (!follow_slave (nframes, 0)) {
745 if (_transport_speed == 0) {
746 no_roll (nframes, 0);
750 if (actively_recording()) {
751 stop_limit = max_frames;
753 if (Config->get_stop_at_session_end()) {
754 stop_limit = current_end_frame();
756 stop_limit = max_frames;
760 if (maybe_stop (stop_limit)) {
761 no_roll (nframes, 0);
765 if (maybe_sync_start (nframes, offset)) {
769 click (_transport_frame, nframes, offset);
771 prepare_diskstreams ();
773 frames_moved = (long) floor (_transport_speed * nframes);
775 if (process_routes (nframes, offset)) {
776 no_roll (nframes, offset);
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;
845 Session::maybe_sync_start (jack_nframes_t& nframes, jack_nframes_t& offset)
847 jack_nframes_t sync_offset;
849 if (!waiting_for_sync_offset) {
853 if (_engine.get_sync_offset (sync_offset) && sync_offset < nframes) {
855 no_roll (sync_offset, 0);
856 nframes -= sync_offset;
857 offset += sync_offset;
858 waiting_for_sync_offset = false;
865 no_roll (nframes, 0);