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>
39 #include <midi++/manager.h>
43 using namespace ARDOUR;
48 Session::process (nframes_t nframes)
50 MIDI::Manager::instance()->cycle_start(nframes);
52 if (synced_to_jack() && waiting_to_start) {
53 if ( _engine.transport_state() == AudioEngine::TransportRolling) {
54 actually_start_transport ();
58 if (non_realtime_work_pending()) {
59 if (!transport_work_requested ()) {
64 (this->*process_function) (nframes);
66 MIDI::Manager::instance()->cycle_end();
68 SendFeedback (); /* EMIT SIGNAL */
72 Session::prepare_diskstreams ()
74 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
75 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
81 Session::no_roll (nframes_t nframes, nframes_t offset)
83 nframes_t end_frame = _transport_frame + nframes; // FIXME: varispeed + no_roll ??
85 bool declick = get_transport_declick_required();
86 boost::shared_ptr<RouteList> r = routes.reader ();
89 _click_io->silence (nframes, offset);
92 if (g_atomic_int_get (&processing_prohibited)) {
93 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
94 (*i)->silence (nframes, offset);
99 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
101 if ((*i)->hidden()) {
105 (*i)->set_pending_declick (declick);
107 if ((*i)->no_roll (nframes, _transport_frame, end_frame, offset, non_realtime_work_pending(),
108 actively_recording(), declick)) {
109 error << string_compose(_("Session: error in no roll for %1"), (*i)->name()) << endmsg;
119 Session::process_routes (nframes_t nframes, nframes_t offset)
122 int declick = get_transport_declick_required();
123 bool rec_monitors = get_rec_monitors_input();
124 boost::shared_ptr<RouteList> r = routes.reader ();
126 if (transport_sub_state & StopPendingCapture) {
127 /* force a declick out */
131 record_active = actively_recording(); // || (get_record_enabled() && get_punch_in());
133 const nframes_t start_frame = _transport_frame;
134 const nframes_t end_frame = _transport_frame + (nframes_t)floor(nframes * _transport_speed);
136 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
140 if ((*i)->hidden()) {
144 (*i)->set_pending_declick (declick);
146 if ((ret = (*i)->roll (nframes, start_frame, end_frame, offset, declick, record_active, rec_monitors)) < 0) {
148 /* we have to do this here. Route::roll() for an AudioTrack will have called AudioDiskstream::process(),
149 and the DS will expect AudioDiskstream::commit() to be called. but we're aborting from that
150 call path, so make sure we release any outstanding locks here before we return failure.
153 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
154 for (DiskstreamList::iterator ids = dsl->begin(); ids != dsl->end(); ++ids) {
167 Session::silent_process_routes (nframes_t nframes, nframes_t offset)
169 bool record_active = actively_recording();
170 int declick = get_transport_declick_required();
171 bool rec_monitors = get_rec_monitors_input();
172 boost::shared_ptr<RouteList> r = routes.reader ();
174 if (transport_sub_state & StopPendingCapture) {
175 /* force a declick out */
179 const nframes_t start_frame = _transport_frame;
180 const nframes_t end_frame = _transport_frame + lrintf(nframes * _transport_speed);
182 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
186 if ((*i)->hidden()) {
190 if ((ret = (*i)->silent_roll (nframes, start_frame, end_frame, offset, record_active, rec_monitors)) < 0) {
192 /* we have to do this here. Route::roll() for an AudioTrack will have called AudioDiskstream::process(),
193 and the DS will expect AudioDiskstream::commit() to be called. but we're aborting from that
194 call path, so make sure we release any outstanding locks here before we return failure.
197 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
198 for (DiskstreamList::iterator ids = dsl->begin(); ids != dsl->end(); ++ids) {
211 Session::commit_diskstreams (nframes_t nframes, bool &needs_butler)
217 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
218 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
220 if ((*i)->hidden()) {
224 /* force all diskstreams not handled by a Route to call do their stuff.
225 Note: the diskstreams that were handled by a route will just return zero
226 from this call, because they know they were processed. So in fact, this
227 also runs commit() for every diskstream.
230 if ((dret = (*i)->process (_transport_frame, nframes, 0, actively_recording(), get_rec_monitors_input())) == 0) {
231 if ((*i)->commit (nframes)) {
235 } else if (dret < 0) {
239 pworst = min (pworst, (*i)->playback_buffer_load());
240 cworst = min (cworst, (*i)->capture_buffer_load());
243 uint32_t pmin = g_atomic_int_get (&_playback_load);
244 uint32_t pminold = g_atomic_int_get (&_playback_load_min);
245 uint32_t cmin = g_atomic_int_get (&_capture_load);
246 uint32_t cminold = g_atomic_int_get (&_capture_load_min);
248 g_atomic_int_set (&_playback_load, (uint32_t) floor (pworst * 100.0f));
249 g_atomic_int_set (&_capture_load, (uint32_t) floor (cworst * 100.0f));
250 g_atomic_int_set (&_playback_load_min, min (pmin, pminold));
251 g_atomic_int_set (&_capture_load_min, min (cmin, cminold));
253 if (actively_recording()) {
260 Session::process_with_events (nframes_t nframes)
263 nframes_t this_nframes;
266 nframes_t stop_limit;
268 bool session_needs_butler = false;
270 /* make sure the auditioner is silent */
273 auditioner->silence (nframes, 0);
276 /* handle any pending events */
278 while (pending_events.read (&ev, 1) == 1) {
282 /* if we are not in the middle of a state change,
283 and there are immediate events queued up,
287 while (!non_realtime_work_pending() && !immediate_events.empty()) {
288 Event *ev = immediate_events.front ();
289 immediate_events.pop_front ();
293 /* Events caused a transport change, send an MTC Full Frame (SMPTE) message.
294 * This is sent whether rolling or not, to give slaves an idea of ardour time
295 * on locates (and allow slow slaves to position and prepare for rolling)
297 if (_send_smpte_update) {
298 send_full_time_code(nframes);
301 if (!process_can_proceed()) {
302 no_roll (nframes, 0);
306 if (events.empty() || next_event == events.end()) {
307 process_without_events (nframes);
311 end_frame = _transport_frame + (nframes_t)abs(floor(nframes * _transport_speed));
315 Events::iterator the_next_one;
317 if (post_transport_work & (PostTransportLocate|PostTransportStop)) {
318 no_roll (nframes, 0);
322 if (!_exporting && _slave) {
323 if (!follow_slave (nframes, 0)) {
328 if (_transport_speed == 0) {
329 no_roll (nframes, 0);
333 send_midi_time_code_for_cycle(nframes);
335 if (actively_recording()) {
336 stop_limit = max_frames;
339 if (Config->get_stop_at_session_end()) {
340 stop_limit = current_end_frame();
342 stop_limit = max_frames;
346 if (maybe_stop (stop_limit)) {
347 no_roll (nframes, 0);
351 this_event = *next_event;
352 the_next_one = next_event;
359 this_nframes = nframes; /* real (jack) time relative */
360 frames_moved = (long) floor (_transport_speed * nframes); /* transport relative */
362 /* running an event, position transport precisely to its time */
363 if (this_event && this_event->action_frame <= end_frame && this_event->action_frame >= _transport_frame) {
364 /* this isn't quite right for reverse play */
365 frames_moved = (long) (this_event->action_frame - _transport_frame);
366 this_nframes = (nframes_t) abs( floor(frames_moved / _transport_speed) );
371 click (_transport_frame, nframes, offset);
373 /* now process frames between now and the first event in this block */
374 prepare_diskstreams ();
376 if (process_routes (this_nframes, offset)) {
377 no_roll (nframes, 0);
381 commit_diskstreams (this_nframes, session_needs_butler);
383 nframes -= this_nframes;
384 offset += this_nframes;
386 if (frames_moved < 0) {
387 decrement_transport_position (-frames_moved);
389 increment_transport_position (frames_moved);
392 maybe_stop (stop_limit);
393 check_declick_out ();
396 /* now handle this event and all others scheduled for the same time */
398 while (this_event && this_event->action_frame == _transport_frame) {
399 process_event (this_event);
401 if (the_next_one == events.end()) {
404 this_event = *the_next_one;
409 /* if an event left our state changing, do the right thing */
411 if (non_realtime_work_pending()) {
412 no_roll (nframes, offset);
416 /* this is necessary to handle the case of seamless looping */
417 end_frame = _transport_frame + (nframes_t) floor (nframes * _transport_speed);
423 } /* implicit release of route lock */
425 if (session_needs_butler)
430 Session::reset_slave_state ()
432 average_slave_delta = 1800;
433 delta_accumulator_cnt = 0;
434 have_first_delta_accumulator = false;
435 slave_state = Stopped;
439 Session::transport_locked () const
443 if (!locate_pending() && ((Config->get_slave_source() == None) || (sl && sl->ok() && sl->locked()))) {
451 Session::follow_slave (nframes_t nframes, nframes_t offset)
454 nframes_t slave_transport_frame;
455 nframes_t this_delta;
461 Config->set_slave_source (None);
465 _slave->speed_and_position (slave_speed, slave_transport_frame);
467 if (!_slave->locked()) {
471 if (slave_transport_frame > _transport_frame) {
472 this_delta = slave_transport_frame - _transport_frame;
475 this_delta = _transport_frame - slave_transport_frame;
479 if ((starting = _slave->starting())) {
484 cerr << "delta = " << (int) (dir * this_delta)
485 << " speed = " << slave_speed
486 << " ts = " << _transport_speed
487 << " M@"<< slave_transport_frame << " S@" << _transport_frame
488 << " avgdelta = " << average_slave_delta
492 if (Config->get_timecode_source_is_synced()) {
494 /* if the TC source is synced, then we assume that its
495 speed is binary: 0.0 or 1.0
498 if (slave_speed != 0.0f) {
504 /* TC source is able to drift relative to us (slave)
505 so we need to keep track of the drift and adjust
506 our speed to remain locked.
509 if (delta_accumulator_cnt >= delta_accumulator_size) {
510 have_first_delta_accumulator = true;
511 delta_accumulator_cnt = 0;
514 if (delta_accumulator_cnt != 0 || this_delta < _current_frame_rate) {
515 delta_accumulator[delta_accumulator_cnt++] = dir*this_delta;
518 if (have_first_delta_accumulator) {
519 average_slave_delta = 0;
520 for (int i = 0; i < delta_accumulator_size; ++i) {
521 average_slave_delta += delta_accumulator[i];
523 average_slave_delta /= delta_accumulator_size;
524 if (average_slave_delta < 0) {
526 average_slave_delta = -average_slave_delta;
530 // cerr << "avgdelta = " << average_slave_delta*average_dir << endl;
534 if (slave_speed != 0.0f) {
536 /* slave is running */
538 switch (slave_state) {
540 if (_slave->requires_seekahead()) {
541 slave_wait_end = slave_transport_frame + _current_frame_rate;
542 locate (slave_wait_end, false, false);
543 slave_state = Waiting;
548 slave_state = Running;
550 Location* al = _locations.auto_loop_location();
552 if (al && play_loop && (slave_transport_frame < al->start() || slave_transport_frame > al->end())) {
554 request_play_loop(false);
557 if (slave_transport_frame != _transport_frame) {
558 locate (slave_transport_frame, false, false);
571 if (slave_state == Waiting) {
573 // cerr << "waiting at " << slave_transport_frame << endl;
574 if (slave_transport_frame >= slave_wait_end) {
575 // cerr << "\tstart at " << _transport_frame << endl;
577 slave_state = Running;
580 nframes_t frame_delta = slave_transport_frame - _transport_frame;
582 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
584 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
585 if (!(*i)->can_internal_playback_seek (frame_delta)) {
592 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
593 (*i)->internal_playback_seek (frame_delta);
595 _transport_frame += frame_delta;
598 // cerr << "cannot micro-seek\n";
602 memset (delta_accumulator, 0, sizeof (nframes_t) * delta_accumulator_size);
603 average_slave_delta = 0;
608 if (slave_state == Running && _transport_speed == 0.0f) {
610 // cerr << "slave starts transport\n";
617 /* slave has stopped */
619 if (_transport_speed != 0.0f) {
621 // cerr << "slave stops transport: " << slave_speed << " frame: " << slave_transport_frame
622 // << " tf = " << _transport_frame
625 if (Config->get_slave_source() == JACK) {
626 last_stop_frame = _transport_frame;
632 if (slave_transport_frame != _transport_frame) {
633 // cerr << "slave stopped, move to " << slave_transport_frame << endl;
634 force_locate (slave_transport_frame, false);
637 slave_state = Stopped;
640 if (slave_state == Running && !Config->get_timecode_source_is_synced()) {
643 if (_transport_speed != 0.0f) {
646 note that average_dir is +1 or -1
649 const float adjust_seconds = 1.0f;
652 //if (average_slave_delta == 0) {
656 // delta = average_slave_delta;
657 // delta *= average_dir;
660 float adjusted_speed = slave_speed +
661 (delta / (adjust_seconds * _current_frame_rate));
663 // cerr << "adjust using " << delta
664 // << " towards " << adjusted_speed
665 // << " ratio = " << adjusted_speed / slave_speed
666 // << " current = " << _transport_speed
667 // << " slave @ " << slave_speed
670 request_transport_speed (adjusted_speed);
673 if ((nframes_t) average_slave_delta > _slave->resolution()) {
674 // cerr << "not locked\n";
681 if (!starting && !non_realtime_work_pending()) {
682 /* speed is set, we're locked, and good to go */
688 if (slave_speed && _transport_speed) {
690 /* something isn't right, but we should move with the master
696 prepare_diskstreams ();
697 silent_process_routes (nframes, offset);
698 commit_diskstreams (nframes, need_butler);
704 int32_t frames_moved = (int32_t) floor (_transport_speed * nframes);
706 if (frames_moved < 0) {
707 decrement_transport_position (-frames_moved);
709 increment_transport_position (frames_moved);
712 nframes_t stop_limit;
714 if (actively_recording()) {
715 stop_limit = max_frames;
717 if (Config->get_stop_at_session_end()) {
718 stop_limit = current_end_frame();
720 stop_limit = max_frames;
724 maybe_stop (stop_limit);
728 /* don't move at all */
729 no_roll (nframes, 0);
734 Session::process_without_events (nframes_t nframes)
736 bool session_needs_butler = false;
737 nframes_t stop_limit;
739 nframes_t offset = 0;
742 if (post_transport_work & (PostTransportLocate|PostTransportStop)) {
743 no_roll (nframes, 0);
747 if (!_exporting && _slave) {
748 if (!follow_slave (nframes, 0)) {
753 if (_transport_speed == 0) {
754 no_roll (nframes, 0);
758 send_midi_time_code_for_cycle(nframes);
760 if (actively_recording()) {
761 stop_limit = max_frames;
763 if (Config->get_stop_at_session_end()) {
764 stop_limit = current_end_frame();
766 stop_limit = max_frames;
770 if (maybe_stop (stop_limit)) {
771 no_roll (nframes, 0);
775 if (maybe_sync_start (nframes, offset)) {
779 click (_transport_frame, nframes, offset);
781 prepare_diskstreams ();
783 frames_moved = (long) floor (_transport_speed * nframes);
785 if (process_routes (nframes, offset)) {
786 no_roll (nframes, offset);
790 commit_diskstreams (nframes, session_needs_butler);
792 if (frames_moved < 0) {
793 decrement_transport_position (-frames_moved);
795 increment_transport_position (frames_moved);
798 maybe_stop (stop_limit);
799 check_declick_out ();
801 } /* implicit release of route lock */
803 if (session_needs_butler)
808 Session::process_audition (nframes_t nframes)
811 boost::shared_ptr<RouteList> r = routes.reader ();
813 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
814 if (!(*i)->hidden()) {
815 (*i)->silence (nframes, 0);
819 /* run the auditioner, and if it says we need butler service, ask for it */
821 if (auditioner->play_audition (nframes) > 0) {
825 /* handle pending events */
827 while (pending_events.read (&ev, 1) == 1) {
831 /* if we are not in the middle of a state change,
832 and there are immediate events queued up,
836 while (!non_realtime_work_pending() && !immediate_events.empty()) {
837 Event *ev = immediate_events.front ();
838 immediate_events.pop_front ();
842 if (!auditioner->active()) {
843 process_function = &Session::process_with_events;
848 Session::maybe_sync_start (nframes_t& nframes, nframes_t& offset)
850 nframes_t sync_offset;
852 if (!waiting_for_sync_offset) {
856 if (_engine.get_sync_offset (sync_offset) && sync_offset < nframes) {
858 no_roll (sync_offset, 0);
859 nframes -= sync_offset;
860 offset += sync_offset;
861 waiting_for_sync_offset = false;
868 no_roll (nframes, 0);