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)
50 if (processing_blocked()) {
55 if (non_realtime_work_pending()) {
56 if (!transport_work_requested ()) {
61 (this->*process_function) (nframes);
64 Glib::Mutex::Lock lm (midi_lock, Glib::TRY_LOCK);
65 SendFeedback (); /* EMIT SIGNAL */
70 Session::prepare_diskstreams ()
72 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
73 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
79 Session::no_roll (nframes_t nframes, nframes_t offset)
81 nframes_t end_frame = _transport_frame + nframes;
83 bool declick = get_transport_declick_required();
84 boost::shared_ptr<RouteList> r = routes.reader ();
87 _click_io->silence (nframes, offset);
90 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
96 (*i)->set_pending_declick (declick);
98 if ((*i)->no_roll (nframes, _transport_frame, end_frame, offset, non_realtime_work_pending(),
99 actively_recording(), declick)) {
100 error << string_compose(_("Session: error in no roll for %1"), (*i)->name()) << endmsg;
110 Session::process_routes (nframes_t nframes, nframes_t offset)
113 int declick = get_transport_declick_required();
114 bool rec_monitors = get_rec_monitors_input();
115 boost::shared_ptr<RouteList> r = routes.reader ();
117 if (transport_sub_state & StopPendingCapture) {
118 /* force a declick out */
122 record_active = actively_recording(); // || (get_record_enabled() && get_punch_in());
124 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
128 if ((*i)->hidden()) {
132 (*i)->set_pending_declick (declick);
134 if ((ret = (*i)->roll (nframes, _transport_frame, _transport_frame + nframes, offset, declick, record_active, rec_monitors)) < 0) {
136 /* we have to do this here. Route::roll() for an AudioTrack will have called AudioDiskstream::process(),
137 and the DS will expect AudioDiskstream::commit() to be called. but we're aborting from that
138 call path, so make sure we release any outstanding locks here before we return failure.
141 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
142 for (DiskstreamList::iterator ids = dsl->begin(); ids != dsl->end(); ++ids) {
155 Session::silent_process_routes (nframes_t nframes, nframes_t offset)
157 bool record_active = actively_recording();
158 int declick = get_transport_declick_required();
159 bool rec_monitors = get_rec_monitors_input();
160 boost::shared_ptr<RouteList> r = routes.reader ();
162 if (transport_sub_state & StopPendingCapture) {
163 /* force a declick out */
167 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
171 if ((*i)->hidden()) {
175 if ((ret = (*i)->silent_roll (nframes, _transport_frame, _transport_frame + nframes, offset, record_active, rec_monitors)) < 0) {
177 /* we have to do this here. Route::roll() for an AudioTrack will have called AudioDiskstream::process(),
178 and the DS will expect AudioDiskstream::commit() to be called. but we're aborting from that
179 call path, so make sure we release any outstanding locks here before we return failure.
182 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
183 for (DiskstreamList::iterator ids = dsl->begin(); ids != dsl->end(); ++ids) {
196 Session::commit_diskstreams (nframes_t nframes, bool &needs_butler)
202 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
203 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
205 if ((*i)->hidden()) {
209 /* force all diskstreams not handled by a Route to call do their stuff.
210 Note: the diskstreams that were handled by a route will just return zero
211 from this call, because they know they were processed. So in fact, this
212 also runs commit() for every diskstream.
215 if ((dret = (*i)->process (_transport_frame, nframes, 0, actively_recording(), get_rec_monitors_input())) == 0) {
216 if ((*i)->commit (nframes)) {
220 } else if (dret < 0) {
224 pworst = min (pworst, (*i)->playback_buffer_load());
225 cworst = min (cworst, (*i)->capture_buffer_load());
228 uint32_t pmin = g_atomic_int_get (&_playback_load);
229 uint32_t pminold = g_atomic_int_get (&_playback_load_min);
230 uint32_t cmin = g_atomic_int_get (&_capture_load);
231 uint32_t cminold = g_atomic_int_get (&_capture_load_min);
233 g_atomic_int_set (&_playback_load, (uint32_t) floor (pworst * 100.0f));
234 g_atomic_int_set (&_capture_load, (uint32_t) floor (cworst * 100.0f));
235 g_atomic_int_set (&_playback_load_min, min (pmin, pminold));
236 g_atomic_int_set (&_capture_load_min, min (cmin, cminold));
238 if (actively_recording()) {
244 Session::process_with_events (nframes_t nframes)
247 nframes_t this_nframes;
250 bool session_needs_butler = false;
251 nframes_t stop_limit;
254 /* make sure the auditioner is silent */
257 auditioner->silence (nframes, 0);
260 /* handle any pending events */
262 while (pending_events.read (&ev, 1) == 1) {
266 /* if we are not in the middle of a state change,
267 and there are immediate events queued up,
271 while (!non_realtime_work_pending() && !immediate_events.empty()) {
272 Event *ev = immediate_events.front ();
273 immediate_events.pop_front ();
277 if (!process_can_proceed()) {
282 if (events.empty() || next_event == events.end()) {
283 process_without_events (nframes);
287 end_frame = _transport_frame + (nframes_t)abs(floor(nframes * _transport_speed));
291 Events::iterator the_next_one;
293 if (!process_can_proceed()) {
298 if (!_exporting && _slave) {
299 if (!follow_slave (nframes, 0)) {
304 if (_transport_speed == 0) {
305 no_roll (nframes, 0);
309 if (actively_recording()) {
310 stop_limit = max_frames;
313 if (Config->get_stop_at_session_end()) {
314 stop_limit = current_end_frame();
316 stop_limit = max_frames;
320 if (maybe_stop (stop_limit)) {
321 no_roll (nframes, 0);
325 this_event = *next_event;
326 the_next_one = next_event;
333 this_nframes = nframes; /* real (jack) time relative */
334 frames_moved = (long) floor (_transport_speed * nframes); /* transport relative */
336 /* running an event, position transport precisely to its time */
337 if (this_event && this_event->action_frame <= end_frame && this_event->action_frame >= _transport_frame) {
338 /* this isn't quite right for reverse play */
339 frames_moved = (long) (this_event->action_frame - _transport_frame);
340 this_nframes = (nframes_t) abs( floor(frames_moved / _transport_speed) );
345 click (_transport_frame, nframes, offset);
347 /* now process frames between now and the first event in this block */
348 prepare_diskstreams ();
350 if (process_routes (this_nframes, offset)) {
351 no_roll (nframes, 0);
355 commit_diskstreams (this_nframes, session_needs_butler);
357 nframes -= this_nframes;
358 offset += this_nframes;
360 if (frames_moved < 0) {
361 decrement_transport_position (-frames_moved);
363 increment_transport_position (frames_moved);
366 maybe_stop (stop_limit);
367 check_declick_out ();
370 /* now handle this event and all others scheduled for the same time */
372 while (this_event && this_event->action_frame == _transport_frame) {
373 process_event (this_event);
375 if (the_next_one == events.end()) {
378 this_event = *the_next_one;
383 /* if an event left our state changing, do the right thing */
385 if (nframes && non_realtime_work_pending()) {
386 no_roll (nframes, offset);
390 /* this is necessary to handle the case of seamless looping */
391 end_frame = _transport_frame + (nframes_t) floor (nframes * _transport_speed);
397 } /* implicit release of route lock */
400 if (session_needs_butler) {
404 if (!_engine.freewheeling() && session_send_mtc) {
405 send_midi_time_code_in_another_thread ();
412 Session::reset_slave_state ()
414 average_slave_delta = 1800;
415 delta_accumulator_cnt = 0;
416 have_first_delta_accumulator = false;
417 slave_state = Stopped;
421 Session::transport_locked () const
425 if (!locate_pending() && ((Config->get_slave_source() == None) || (sl && sl->ok() && sl->locked()))) {
433 Session::follow_slave (nframes_t nframes, nframes_t offset)
436 nframes_t slave_transport_frame;
437 nframes_t this_delta;
443 Config->set_slave_source (None);
447 _slave->speed_and_position (slave_speed, slave_transport_frame);
449 if (!_slave->locked()) {
450 // cerr << "Slave not locked, not rolling\n";
454 if (slave_transport_frame > _transport_frame) {
455 this_delta = slave_transport_frame - _transport_frame;
458 this_delta = _transport_frame - slave_transport_frame;
462 if ((starting = _slave->starting())) {
467 cerr << "delta = " << (int) (dir * this_delta)
468 << " speed = " << slave_speed
469 << " ts = " << _transport_speed
470 << " M@ "<< slave_transport_frame << " S@ " << _transport_frame
471 << " avgdelta = " << average_slave_delta
475 if (_slave->is_always_synced() || Config->get_timecode_source_is_synced()) {
477 /* if the TC source is synced, then we assume that its
478 speed is binary: 0.0 or 1.0
481 if (slave_speed != 0.0f) {
487 /* TC source is able to drift relative to us (slave)
488 so we need to keep track of the drift and adjust
489 our speed to remain locked.
492 if (delta_accumulator_cnt >= delta_accumulator_size) {
493 have_first_delta_accumulator = true;
494 delta_accumulator_cnt = 0;
497 if (delta_accumulator_cnt != 0 || this_delta < _current_frame_rate) {
498 delta_accumulator[delta_accumulator_cnt++] = dir*this_delta;
501 if (have_first_delta_accumulator) {
502 average_slave_delta = 0;
503 for (int i = 0; i < delta_accumulator_size; ++i) {
504 average_slave_delta += delta_accumulator[i];
506 average_slave_delta /= delta_accumulator_size;
507 if (average_slave_delta < 0) {
509 average_slave_delta = -average_slave_delta;
513 // cerr << "avgdelta = " << average_slave_delta*average_dir << endl;
517 if (slave_speed != 0.0f) {
519 /* slave is running */
521 switch (slave_state) {
523 if (_slave->requires_seekahead()) {
524 slave_wait_end = slave_transport_frame + _current_frame_rate;
525 locate (slave_wait_end, false, false);
526 slave_state = Waiting;
531 slave_state = Running;
533 Location* al = _locations.auto_loop_location();
535 if (al && play_loop && (slave_transport_frame < al->start() || slave_transport_frame > al->end())) {
537 request_play_loop(false);
540 if (slave_transport_frame != _transport_frame) {
541 locate (slave_transport_frame, false, false);
554 if (slave_state == Waiting) {
556 // cerr << "waiting at " << slave_transport_frame << endl;
558 if (slave_transport_frame >= slave_wait_end) {
559 // cerr << "\tstart at " << _transport_frame << endl;
561 slave_state = Running;
564 nframes_t frame_delta = slave_transport_frame - _transport_frame;
566 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
568 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
569 if (!(*i)->can_internal_playback_seek (frame_delta)) {
576 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
577 (*i)->internal_playback_seek (frame_delta);
579 _transport_frame += frame_delta;
582 // cerr << "cannot micro-seek\n";
586 memset (delta_accumulator, 0, sizeof (nframes_t) * delta_accumulator_size);
587 average_slave_delta = 0;
592 if (slave_state == Running && _transport_speed == 0.0f) {
594 // cerr << "slave starts transport\n";
601 /* slave has stopped */
603 if (_transport_speed != 0.0f) {
605 // cerr << "slave stops transport: " << slave_speed << " frame: " << slave_transport_frame
606 // << " tf = " << _transport_frame
609 if (Config->get_slave_source() == JACK) {
610 last_stop_frame = _transport_frame;
616 if (slave_transport_frame != _transport_frame) {
617 // cerr << "slave stopped, move to " << slave_transport_frame << endl;
618 force_locate (slave_transport_frame, false);
621 slave_state = Stopped;
624 if (slave_state == Running && !_slave->is_always_synced() && !Config->get_timecode_source_is_synced()) {
627 if (_transport_speed != 0.0f) {
630 note that average_dir is +1 or -1
633 const float adjust_seconds = 1.0f;
636 //if (average_slave_delta == 0) {
640 // delta = average_slave_delta;
641 // delta *= average_dir;
644 float adjusted_speed = slave_speed +
645 (delta / (adjust_seconds * _current_frame_rate));
648 cerr << "adjust using " << delta
649 << " towards " << adjusted_speed
650 << " ratio = " << adjusted_speed / slave_speed
651 << " current = " << _transport_speed
652 << " 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;
727 if (!process_can_proceed()) {
732 if (!_exporting && _slave) {
733 if (!follow_slave (nframes, 0)) {
738 if (_transport_speed == 0) {
739 no_roll (nframes, 0);
743 if (actively_recording()) {
744 stop_limit = max_frames;
746 if (Config->get_stop_at_session_end()) {
747 stop_limit = current_end_frame();
749 stop_limit = max_frames;
753 if (maybe_stop (stop_limit)) {
754 no_roll (nframes, 0);
758 if (maybe_sync_start (nframes, offset)) {
762 click (_transport_frame, nframes, offset);
764 prepare_diskstreams ();
766 frames_moved = (long) floor (_transport_speed * nframes);
768 if (process_routes (nframes, offset)) {
769 no_roll (nframes, offset);
773 commit_diskstreams (nframes, session_needs_butler);
775 if (frames_moved < 0) {
776 decrement_transport_position (-frames_moved);
778 increment_transport_position (frames_moved);
781 maybe_stop (stop_limit);
782 check_declick_out ();
784 if (session_needs_butler) {
788 if (!_engine.freewheeling() && session_send_mtc) {
789 send_midi_time_code_in_another_thread ();
796 Session::process_audition (nframes_t nframes)
799 boost::shared_ptr<RouteList> r = routes.reader ();
801 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
802 if (!(*i)->hidden()) {
803 (*i)->silence (nframes, 0);
807 /* run the auditioner, and if it says we need butler service, ask for it */
809 if (auditioner->play_audition (nframes) > 0) {
813 /* handle pending events */
815 while (pending_events.read (&ev, 1) == 1) {
819 /* if we are not in the middle of a state change,
820 and there are immediate events queued up,
824 while (!non_realtime_work_pending() && !immediate_events.empty()) {
825 Event *ev = immediate_events.front ();
826 immediate_events.pop_front ();
830 if (!auditioner->active()) {
831 process_function = &Session::process_with_events;
836 Session::maybe_sync_start (nframes_t& nframes, nframes_t& offset)
838 nframes_t sync_offset;
840 if (!waiting_for_sync_offset) {
844 if (_engine.get_sync_offset (sync_offset) && sync_offset < nframes) {
846 /* generate silence up to the sync point, then
847 adjust nframes + offset to reflect whatever
851 no_roll (sync_offset, 0);
852 nframes -= sync_offset;
853 offset += sync_offset;
854 waiting_for_sync_offset = false;
857 return true; // done, nothing left to process
862 /* sync offset point is not within this process()
863 cycle, so just generate silence. and don't bother
864 with any fancy stuff here, just the minimal silence.
869 if (Config->get_locate_while_waiting_for_sync()) {
870 if (micro_locate (nframes)) {
871 /* XXX ERROR !!! XXX */
875 return true; // done, nothing left to process