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 (non_realtime_work_pending()) {
51 if (!transport_work_requested ()) {
56 (this->*process_function) (nframes);
59 Glib::Mutex::Lock lm (midi_lock, Glib::TRY_LOCK);
60 SendFeedback (); /* EMIT SIGNAL */
65 Session::prepare_diskstreams ()
67 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
68 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
74 Session::no_roll (nframes_t nframes, nframes_t offset)
76 nframes_t end_frame = _transport_frame + nframes;
78 bool declick = get_transport_declick_required();
79 boost::shared_ptr<RouteList> r = routes.reader ();
82 _click_io->silence (nframes, offset);
85 if (g_atomic_int_get (&processing_prohibited)) {
86 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
87 (*i)->silence (nframes, offset);
92 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
98 (*i)->set_pending_declick (declick);
100 if ((*i)->no_roll (nframes, _transport_frame, end_frame, offset, non_realtime_work_pending(),
101 actively_recording(), declick)) {
102 error << string_compose(_("Session: error in no roll for %1"), (*i)->name()) << endmsg;
112 Session::process_routes (nframes_t nframes, nframes_t offset)
115 int declick = get_transport_declick_required();
116 bool rec_monitors = get_rec_monitors_input();
117 boost::shared_ptr<RouteList> r = routes.reader ();
119 if (transport_sub_state & StopPendingCapture) {
120 /* force a declick out */
124 record_active = actively_recording(); // || (get_record_enabled() && get_punch_in());
126 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
130 if ((*i)->hidden()) {
134 (*i)->set_pending_declick (declick);
136 if ((ret = (*i)->roll (nframes, _transport_frame, _transport_frame + nframes, offset, declick, record_active, rec_monitors)) < 0) {
138 /* we have to do this here. Route::roll() for an AudioTrack will have called AudioDiskstream::process(),
139 and the DS will expect AudioDiskstream::commit() to be called. but we're aborting from that
140 call path, so make sure we release any outstanding locks here before we return failure.
143 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
144 for (DiskstreamList::iterator ids = dsl->begin(); ids != dsl->end(); ++ids) {
157 Session::silent_process_routes (nframes_t nframes, 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 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
185 for (DiskstreamList::iterator ids = dsl->begin(); ids != dsl->end(); ++ids) {
198 Session::commit_diskstreams (nframes_t nframes, bool &needs_butler)
204 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
205 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
207 if ((*i)->hidden()) {
211 /* force all diskstreams not handled by a Route to call do their stuff.
212 Note: the diskstreams that were handled by a route will just return zero
213 from this call, because they know they were processed. So in fact, this
214 also runs commit() for every diskstream.
217 if ((dret = (*i)->process (_transport_frame, nframes, 0, actively_recording(), get_rec_monitors_input())) == 0) {
218 if ((*i)->commit (nframes)) {
222 } else if (dret < 0) {
226 pworst = min (pworst, (*i)->playback_buffer_load());
227 cworst = min (cworst, (*i)->capture_buffer_load());
230 uint32_t pmin = g_atomic_int_get (&_playback_load);
231 uint32_t pminold = g_atomic_int_get (&_playback_load_min);
232 uint32_t cmin = g_atomic_int_get (&_capture_load);
233 uint32_t cminold = g_atomic_int_get (&_capture_load_min);
235 g_atomic_int_set (&_playback_load, (uint32_t) floor (pworst * 100.0f));
236 g_atomic_int_set (&_capture_load, (uint32_t) floor (cworst * 100.0f));
237 g_atomic_int_set (&_playback_load_min, min (pmin, pminold));
238 g_atomic_int_set (&_capture_load_min, min (cmin, cminold));
240 if (actively_recording()) {
246 Session::process_with_events (nframes_t nframes)
249 nframes_t this_nframes;
252 bool session_needs_butler = false;
253 nframes_t stop_limit;
256 /* make sure the auditioner is silent */
259 auditioner->silence (nframes, 0);
262 /* handle any pending events */
264 while (pending_events.read (&ev, 1) == 1) {
268 /* if we are not in the middle of a state change,
269 and there are immediate events queued up,
273 while (!non_realtime_work_pending() && !immediate_events.empty()) {
274 Event *ev = immediate_events.front ();
275 immediate_events.pop_front ();
279 if (!process_can_proceed()) {
284 if (events.empty() || next_event == events.end()) {
285 process_without_events (nframes);
289 end_frame = _transport_frame + (nframes_t)abs(floor(nframes * _transport_speed));
293 Events::iterator the_next_one;
295 if (!process_can_proceed()) {
300 if (!_exporting && _slave) {
301 if (!follow_slave (nframes, 0)) {
306 if (_transport_speed == 0) {
307 no_roll (nframes, 0);
311 if (actively_recording()) {
312 stop_limit = max_frames;
315 if (Config->get_stop_at_session_end()) {
316 stop_limit = current_end_frame();
318 stop_limit = max_frames;
322 if (maybe_stop (stop_limit)) {
323 no_roll (nframes, 0);
327 this_event = *next_event;
328 the_next_one = next_event;
335 this_nframes = nframes; /* real (jack) time relative */
336 frames_moved = (long) floor (_transport_speed * nframes); /* transport relative */
338 /* running an event, position transport precisely to its time */
339 if (this_event && this_event->action_frame <= end_frame && this_event->action_frame >= _transport_frame) {
340 /* this isn't quite right for reverse play */
341 frames_moved = (long) (this_event->action_frame - _transport_frame);
342 this_nframes = (nframes_t) abs( floor(frames_moved / _transport_speed) );
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 if (frames_moved < 0) {
363 decrement_transport_position (-frames_moved);
365 increment_transport_position (frames_moved);
368 maybe_stop (stop_limit);
369 check_declick_out ();
372 /* now handle this event and all others scheduled for the same time */
374 while (this_event && this_event->action_frame == _transport_frame) {
375 process_event (this_event);
377 if (the_next_one == events.end()) {
380 this_event = *the_next_one;
385 /* if an event left our state changing, do the right thing */
387 if (non_realtime_work_pending()) {
388 no_roll (nframes, offset);
392 /* this is necessary to handle the case of seamless looping */
393 end_frame = _transport_frame + (nframes_t) floor (nframes * _transport_speed);
399 } /* implicit release of route lock */
402 if (session_needs_butler) {
406 if (!_engine.freewheeling() && session_send_mtc) {
407 send_midi_time_code_in_another_thread ();
414 Session::reset_slave_state ()
416 average_slave_delta = 1800;
417 delta_accumulator_cnt = 0;
418 have_first_delta_accumulator = false;
419 slave_state = Stopped;
423 Session::transport_locked () const
427 if (!locate_pending() && ((Config->get_slave_source() == None) || (sl && sl->ok() && sl->locked()))) {
435 Session::follow_slave (nframes_t nframes, nframes_t offset)
438 nframes_t slave_transport_frame;
439 nframes_t this_delta;
445 Config->set_slave_source (None);
449 _slave->speed_and_position (slave_speed, slave_transport_frame);
451 if (!_slave->locked()) {
452 // cerr << "Slave not locked, not rolling\n";
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 (_slave->is_always_synced() || 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 && !_slave->is_always_synced() && !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));
650 cerr << "adjust using " << delta
651 << " towards " << adjusted_speed
652 << " ratio = " << adjusted_speed / slave_speed
653 << " current = " << _transport_speed
654 << " slave @ " << slave_speed
658 request_transport_speed (adjusted_speed);
661 if ((nframes_t) average_slave_delta > _slave->resolution()) {
662 // cerr << "not locked\n";
669 if (!starting && !non_realtime_work_pending()) {
670 /* speed is set, we're locked, and good to go */
676 if (slave_speed && _transport_speed) {
678 /* something isn't right, but we should move with the master
684 prepare_diskstreams ();
685 silent_process_routes (nframes, offset);
686 commit_diskstreams (nframes, need_butler);
692 int32_t frames_moved = (int32_t) floor (_transport_speed * nframes);
694 if (frames_moved < 0) {
695 decrement_transport_position (-frames_moved);
697 increment_transport_position (frames_moved);
700 nframes_t stop_limit;
702 if (actively_recording()) {
703 stop_limit = max_frames;
705 if (Config->get_stop_at_session_end()) {
706 stop_limit = current_end_frame();
708 stop_limit = max_frames;
712 maybe_stop (stop_limit);
716 /* don't move at all */
717 no_roll (nframes, 0);
722 Session::process_without_events (nframes_t nframes)
724 bool session_needs_butler = false;
725 nframes_t stop_limit;
727 nframes_t offset = 0;
729 if (!process_can_proceed()) {
734 if (!_exporting && _slave) {
735 if (!follow_slave (nframes, 0)) {
740 if (_transport_speed == 0) {
741 no_roll (nframes, 0);
745 if (actively_recording()) {
746 stop_limit = max_frames;
748 if (Config->get_stop_at_session_end()) {
749 stop_limit = current_end_frame();
751 stop_limit = max_frames;
755 if (maybe_stop (stop_limit)) {
756 no_roll (nframes, 0);
760 if (maybe_sync_start (nframes, offset)) {
764 click (_transport_frame, nframes, offset);
766 prepare_diskstreams ();
768 frames_moved = (long) floor (_transport_speed * nframes);
770 if (process_routes (nframes, offset)) {
771 no_roll (nframes, offset);
775 commit_diskstreams (nframes, session_needs_butler);
777 if (frames_moved < 0) {
778 decrement_transport_position (-frames_moved);
780 increment_transport_position (frames_moved);
783 maybe_stop (stop_limit);
784 check_declick_out ();
786 if (session_needs_butler) {
790 if (!_engine.freewheeling() && session_send_mtc) {
791 send_midi_time_code_in_another_thread ();
798 Session::process_audition (nframes_t nframes)
801 boost::shared_ptr<RouteList> r = routes.reader ();
803 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
804 if (!(*i)->hidden()) {
805 (*i)->silence (nframes, 0);
809 /* run the auditioner, and if it says we need butler service, ask for it */
811 if (auditioner->play_audition (nframes) > 0) {
815 /* handle pending events */
817 while (pending_events.read (&ev, 1) == 1) {
821 /* if we are not in the middle of a state change,
822 and there are immediate events queued up,
826 while (!non_realtime_work_pending() && !immediate_events.empty()) {
827 Event *ev = immediate_events.front ();
828 immediate_events.pop_front ();
832 if (!auditioner->active()) {
833 process_function = &Session::process_with_events;
838 Session::maybe_sync_start (nframes_t& nframes, nframes_t& offset)
840 nframes_t sync_offset;
842 if (!waiting_for_sync_offset) {
846 if (_engine.get_sync_offset (sync_offset) && sync_offset < nframes) {
848 /* generate silence up to the sync point, then
849 adjust nframes + offset to reflect whatever
853 no_roll (sync_offset, 0);
854 nframes -= sync_offset;
855 offset += sync_offset;
856 waiting_for_sync_offset = false;
859 return true; // done, nothing left to process
864 /* sync offset point is not within this process()
865 cycle, so just generate silence. and don't bother
866 with any fancy stuff here, just the minimal silence.
869 g_atomic_int_inc (&processing_prohibited);
870 no_roll (nframes, 0);
871 g_atomic_int_dec_and_test (&processing_prohibited);
873 if (Config->get_locate_while_waiting_for_sync()) {
874 if (micro_locate (nframes)) {
875 /* XXX ERROR !!! XXX */
879 return true; // done, nothing left to process