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.
25 #include <pbd/error.h>
27 #include <glibmm/thread.h>
29 #include <ardour/ardour.h>
30 #include <ardour/session.h>
31 #include <ardour/timestamps.h>
32 #include <ardour/audio_diskstream.h>
33 #include <ardour/audioengine.h>
34 #include <ardour/slave.h>
35 #include <ardour/auditioner.h>
36 #include <ardour/cycles.h>
37 #include <ardour/cycle_timer.h>
41 using namespace ARDOUR;
46 Session::process (nframes_t nframes)
48 if (synced_to_jack() && waiting_to_start) {
49 if ( _engine.transport_state() == AudioEngine::TransportRolling) {
50 actually_start_transport ();
54 if (non_realtime_work_pending()) {
55 if (!transport_work_requested ()) {
60 (this->*process_function) (nframes);
62 SendFeedback (); /* EMIT SIGNAL */
66 Session::prepare_diskstreams ()
68 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
69 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
75 Session::no_roll (nframes_t nframes, nframes_t offset)
77 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 (nframes_t nframes, 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 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
145 for (DiskstreamList::iterator ids = dsl->begin(); ids != dsl->end(); ++ids) {
158 Session::silent_process_routes (nframes_t nframes, nframes_t offset)
160 bool record_active = actively_recording();
161 int declick = get_transport_declick_required();
162 bool rec_monitors = get_rec_monitors_input();
163 boost::shared_ptr<RouteList> r = routes.reader ();
165 if (transport_sub_state & StopPendingCapture) {
166 /* force a declick out */
170 for (RouteList::iterator i = r->begin(); i != r->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 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
186 for (DiskstreamList::iterator ids = dsl->begin(); ids != dsl->end(); ++ids) {
199 Session::commit_diskstreams (nframes_t nframes, bool &needs_butler)
205 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
206 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
208 if ((*i)->hidden()) {
212 /* force all diskstreams not handled by a Route to call do their stuff.
213 Note: the diskstreams that were handled by a route will just return zero
214 from this call, because they know they were processed. So in fact, this
215 also runs commit() for every diskstream.
218 if ((dret = (*i)->process (_transport_frame, nframes, 0, actively_recording(), get_rec_monitors_input())) == 0) {
219 if ((*i)->commit (nframes)) {
223 } else if (dret < 0) {
227 pworst = min (pworst, (*i)->playback_buffer_load());
228 cworst = min (cworst, (*i)->capture_buffer_load());
231 uint32_t pmin = g_atomic_int_get (&_playback_load);
232 uint32_t pminold = g_atomic_int_get (&_playback_load_min);
233 uint32_t cmin = g_atomic_int_get (&_capture_load);
234 uint32_t cminold = g_atomic_int_get (&_capture_load_min);
236 g_atomic_int_set (&_playback_load, (uint32_t) floor (pworst * 100.0f));
237 g_atomic_int_set (&_capture_load, (uint32_t) floor (cworst * 100.0f));
238 g_atomic_int_set (&_playback_load_min, min (pmin, pminold));
239 g_atomic_int_set (&_capture_load_min, min (cmin, cminold));
241 if (actively_recording()) {
247 Session::process_with_events (nframes_t nframes)
250 nframes_t this_nframes;
253 bool session_needs_butler = false;
254 nframes_t stop_limit;
257 /* make sure the auditioner is silent */
260 auditioner->silence (nframes, 0);
263 /* handle any pending events */
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 if (!process_can_proceed()) {
281 no_roll (nframes, 0);
285 if (events.empty() || next_event == events.end()) {
286 process_without_events (nframes);
290 end_frame = _transport_frame + (nframes_t)abs(floor(nframes * _transport_speed));
294 Events::iterator the_next_one;
296 if (post_transport_work & (PostTransportLocate|PostTransportStop)) {
297 no_roll (nframes, 0);
301 if (!_exporting && _slave) {
302 if (!follow_slave (nframes, 0)) {
307 if (_transport_speed == 0) {
308 no_roll (nframes, 0);
312 if (actively_recording()) {
313 stop_limit = max_frames;
316 if (Config->get_stop_at_session_end()) {
317 stop_limit = current_end_frame();
319 stop_limit = max_frames;
323 if (maybe_stop (stop_limit)) {
324 no_roll (nframes, 0);
328 this_event = *next_event;
329 the_next_one = next_event;
336 this_nframes = nframes; /* real (jack) time relative */
337 frames_moved = (long) floor (_transport_speed * nframes); /* transport relative */
339 /* running an event, position transport precisely to its time */
340 if (this_event && this_event->action_frame <= end_frame && this_event->action_frame >= _transport_frame) {
341 /* this isn't quite right for reverse play */
342 frames_moved = (long) (this_event->action_frame - _transport_frame);
343 this_nframes = (nframes_t) abs( floor(frames_moved / _transport_speed) );
348 click (_transport_frame, nframes, offset);
350 /* now process frames between now and the first event in this block */
351 prepare_diskstreams ();
353 if (process_routes (this_nframes, offset)) {
354 no_roll (nframes, 0);
358 commit_diskstreams (this_nframes, session_needs_butler);
360 nframes -= this_nframes;
361 offset += this_nframes;
363 if (frames_moved < 0) {
364 decrement_transport_position (-frames_moved);
366 increment_transport_position (frames_moved);
369 maybe_stop (stop_limit);
370 check_declick_out ();
373 /* now handle this event and all others scheduled for the same time */
375 while (this_event && this_event->action_frame == _transport_frame) {
376 process_event (this_event);
378 if (the_next_one == events.end()) {
381 this_event = *the_next_one;
386 /* if an event left our state changing, do the right thing */
388 if (non_realtime_work_pending()) {
389 no_roll (nframes, offset);
393 /* this is necessary to handle the case of seamless looping */
394 end_frame = _transport_frame + (nframes_t) floor (nframes * _transport_speed);
400 } /* implicit release of route lock */
403 if (session_needs_butler) {
407 if (!_engine.freewheeling() && session_send_mtc) {
408 send_midi_time_code_in_another_thread ();
415 Session::reset_slave_state ()
417 average_slave_delta = 1800;
418 delta_accumulator_cnt = 0;
419 have_first_delta_accumulator = false;
420 slave_state = Stopped;
424 Session::transport_locked () const
428 if (!locate_pending() && ((Config->get_slave_source() == None) || (sl && sl->ok() && sl->locked()))) {
436 Session::follow_slave (nframes_t nframes, nframes_t offset)
439 nframes_t slave_transport_frame;
440 nframes_t this_delta;
446 Config->set_slave_source (None);
450 _slave->speed_and_position (slave_speed, slave_transport_frame);
452 if (!_slave->locked()) {
456 if (slave_transport_frame > _transport_frame) {
457 this_delta = slave_transport_frame - _transport_frame;
460 this_delta = _transport_frame - slave_transport_frame;
464 if ((starting = _slave->starting())) {
469 cerr << "delta = " << (int) (dir * this_delta)
470 << " speed = " << slave_speed
471 << " ts = " << _transport_speed
472 << " M@"<< slave_transport_frame << " S@" << _transport_frame
473 << " avgdelta = " << average_slave_delta
477 if (Config->get_timecode_source_is_synced()) {
479 /* if the TC source is synced, then we assume that its
480 speed is binary: 0.0 or 1.0
483 if (slave_speed != 0.0f) {
489 /* TC source is able to drift relative to us (slave)
490 so we need to keep track of the drift and adjust
491 our speed to remain locked.
494 if (delta_accumulator_cnt >= delta_accumulator_size) {
495 have_first_delta_accumulator = true;
496 delta_accumulator_cnt = 0;
499 if (delta_accumulator_cnt != 0 || this_delta < _current_frame_rate) {
500 delta_accumulator[delta_accumulator_cnt++] = dir*this_delta;
503 if (have_first_delta_accumulator) {
504 average_slave_delta = 0;
505 for (int i = 0; i < delta_accumulator_size; ++i) {
506 average_slave_delta += delta_accumulator[i];
508 average_slave_delta /= delta_accumulator_size;
509 if (average_slave_delta < 0) {
511 average_slave_delta = -average_slave_delta;
515 // cerr << "avgdelta = " << average_slave_delta*average_dir << endl;
519 if (slave_speed != 0.0f) {
521 /* slave is running */
523 switch (slave_state) {
525 if (_slave->requires_seekahead()) {
526 slave_wait_end = slave_transport_frame + _current_frame_rate;
527 locate (slave_wait_end, false, false);
528 slave_state = Waiting;
533 slave_state = Running;
535 Location* al = _locations.auto_loop_location();
537 if (al && play_loop && (slave_transport_frame < al->start() || slave_transport_frame > al->end())) {
539 request_play_loop(false);
542 if (slave_transport_frame != _transport_frame) {
543 locate (slave_transport_frame, false, false);
556 if (slave_state == Waiting) {
558 // cerr << "waiting at " << slave_transport_frame << endl;
560 if (slave_transport_frame >= slave_wait_end) {
561 // cerr << "\tstart at " << _transport_frame << endl;
563 slave_state = Running;
566 nframes_t frame_delta = slave_transport_frame - _transport_frame;
568 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
570 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
571 if (!(*i)->can_internal_playback_seek (frame_delta)) {
578 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
579 (*i)->internal_playback_seek (frame_delta);
581 _transport_frame += frame_delta;
584 // cerr << "cannot micro-seek\n";
588 memset (delta_accumulator, 0, sizeof (nframes_t) * delta_accumulator_size);
589 average_slave_delta = 0;
594 if (slave_state == Running && _transport_speed == 0.0f) {
596 // cerr << "slave starts transport\n";
603 /* slave has stopped */
605 if (_transport_speed != 0.0f) {
607 // cerr << "slave stops transport: " << slave_speed << " frame: " << slave_transport_frame
608 // << " tf = " << _transport_frame
611 if (Config->get_slave_source() == JACK) {
612 last_stop_frame = _transport_frame;
618 if (slave_transport_frame != _transport_frame) {
619 // cerr << "slave stopped, move to " << slave_transport_frame << endl;
620 force_locate (slave_transport_frame, false);
623 slave_state = Stopped;
626 if (slave_state == Running && !Config->get_timecode_source_is_synced()) {
629 if (_transport_speed != 0.0f) {
632 note that average_dir is +1 or -1
635 const float adjust_seconds = 1.0f;
638 //if (average_slave_delta == 0) {
642 // delta = average_slave_delta;
643 // delta *= average_dir;
646 float adjusted_speed = slave_speed +
647 (delta / (adjust_seconds * _current_frame_rate));
649 // cerr << "adjust using " << delta
650 // << " towards " << adjusted_speed
651 // << " ratio = " << adjusted_speed / slave_speed
652 // << " current = " << _transport_speed
653 // << " slave @ " << slave_speed
656 request_transport_speed (adjusted_speed);
659 if ((nframes_t) average_slave_delta > _slave->resolution()) {
660 // cerr << "not locked\n";
667 if (!starting && !non_realtime_work_pending()) {
668 /* speed is set, we're locked, and good to go */
674 if (slave_speed && _transport_speed) {
676 /* something isn't right, but we should move with the master
682 prepare_diskstreams ();
683 silent_process_routes (nframes, offset);
684 commit_diskstreams (nframes, need_butler);
690 int32_t frames_moved = (int32_t) floor (_transport_speed * nframes);
692 if (frames_moved < 0) {
693 decrement_transport_position (-frames_moved);
695 increment_transport_position (frames_moved);
698 nframes_t stop_limit;
700 if (actively_recording()) {
701 stop_limit = max_frames;
703 if (Config->get_stop_at_session_end()) {
704 stop_limit = current_end_frame();
706 stop_limit = max_frames;
710 maybe_stop (stop_limit);
714 /* don't move at all */
715 no_roll (nframes, 0);
720 Session::process_without_events (nframes_t nframes)
722 bool session_needs_butler = false;
723 nframes_t stop_limit;
725 nframes_t offset = 0;
728 if (post_transport_work & (PostTransportLocate|PostTransportStop)) {
729 no_roll (nframes, 0);
733 if (!_exporting && _slave) {
734 if (!follow_slave (nframes, 0)) {
739 if (_transport_speed == 0) {
740 no_roll (nframes, 0);
744 if (actively_recording()) {
745 stop_limit = max_frames;
747 if (Config->get_stop_at_session_end()) {
748 stop_limit = current_end_frame();
750 stop_limit = max_frames;
754 if (maybe_stop (stop_limit)) {
755 no_roll (nframes, 0);
759 if (maybe_sync_start (nframes, offset)) {
763 click (_transport_frame, nframes, offset);
765 prepare_diskstreams ();
767 frames_moved = (long) floor (_transport_speed * nframes);
769 if (process_routes (nframes, offset)) {
770 no_roll (nframes, offset);
774 commit_diskstreams (nframes, session_needs_butler);
776 if (frames_moved < 0) {
777 decrement_transport_position (-frames_moved);
779 increment_transport_position (frames_moved);
782 maybe_stop (stop_limit);
783 check_declick_out ();
785 } /* implicit release of route lock */
787 if (session_needs_butler) {
791 if (!_engine.freewheeling() && session_send_mtc) {
792 send_midi_time_code_in_another_thread ();
799 Session::process_audition (nframes_t nframes)
802 boost::shared_ptr<RouteList> r = routes.reader ();
804 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
805 if (!(*i)->hidden()) {
806 (*i)->silence (nframes, 0);
810 /* run the auditioner, and if it says we need butler service, ask for it */
812 if (auditioner->play_audition (nframes) > 0) {
816 /* handle pending events */
818 while (pending_events.read (&ev, 1) == 1) {
822 /* if we are not in the middle of a state change,
823 and there are immediate events queued up,
827 while (!non_realtime_work_pending() && !immediate_events.empty()) {
828 Event *ev = immediate_events.front ();
829 immediate_events.pop_front ();
833 if (!auditioner->active()) {
834 process_function = &Session::process_with_events;
839 Session::maybe_sync_start (nframes_t& nframes, nframes_t& offset)
841 nframes_t sync_offset;
843 if (!waiting_for_sync_offset) {
847 if (_engine.get_sync_offset (sync_offset) && sync_offset < nframes) {
849 no_roll (sync_offset, 0);
850 nframes -= sync_offset;
851 offset += sync_offset;
852 waiting_for_sync_offset = false;
859 no_roll (nframes, 0);