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 jack_nframes_t start_frame = _transport_frame;
134 const jack_nframes_t end_frame = _transport_frame + (jack_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 jack_nframes_t start_frame = _transport_frame;
180 const jack_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.
227 if ((dret = (*i)->process (_transport_frame, nframes, 0, actively_recording(), get_rec_monitors_input())) == 0) {
228 if ((*i)->commit (nframes)) {
232 } else if (dret < 0) {
236 pworst = min (pworst, (*i)->playback_buffer_load());
237 cworst = min (cworst, (*i)->capture_buffer_load());
240 uint32_t pmin = g_atomic_int_get (&_playback_load);
241 uint32_t pminold = g_atomic_int_get (&_playback_load_min);
242 uint32_t cmin = g_atomic_int_get (&_capture_load);
243 uint32_t cminold = g_atomic_int_get (&_capture_load_min);
245 g_atomic_int_set (&_playback_load, (uint32_t) floor (pworst * 100.0f));
246 g_atomic_int_set (&_capture_load, (uint32_t) floor (cworst * 100.0f));
247 g_atomic_int_set (&_playback_load_min, min (pmin, pminold));
248 g_atomic_int_set (&_capture_load_min, min (cmin, cminold));
250 if (actively_recording()) {
257 Session::process_with_events (nframes_t nframes)
260 nframes_t this_nframes;
263 nframes_t stop_limit;
265 bool session_needs_butler = false;
267 /* make sure the auditioner is silent */
270 auditioner->silence (nframes, 0);
273 /* handle any pending events */
275 while (pending_events.read (&ev, 1) == 1) {
279 /* if we are not in the middle of a state change,
280 and there are immediate events queued up,
284 while (!non_realtime_work_pending() && !immediate_events.empty()) {
285 Event *ev = immediate_events.front ();
286 immediate_events.pop_front ();
290 /* Events caused a transport change, send an MTC Full Frame (SMPTE) message.
291 * This is sent whether rolling or not, to give slaves an idea of ardour time
292 * on locates (and allow slow slaves to position and prepare for rolling)
294 if (_send_smpte_update) {
295 send_full_time_code(nframes);
298 if (!process_can_proceed()) {
299 no_roll (nframes, 0);
303 if (events.empty() || next_event == events.end()) {
304 process_without_events (nframes);
308 end_frame = _transport_frame + (nframes_t)abs(floor(nframes * _transport_speed));
312 Events::iterator the_next_one;
314 if (post_transport_work & (PostTransportLocate|PostTransportStop)) {
315 no_roll (nframes, 0);
319 if (!_exporting && _slave) {
320 if (!follow_slave (nframes, 0)) {
325 if (_transport_speed == 0) {
326 no_roll (nframes, 0);
330 send_midi_time_code_for_cycle(nframes);
332 if (actively_recording()) {
333 stop_limit = max_frames;
336 if (Config->get_stop_at_session_end()) {
337 stop_limit = current_end_frame();
339 stop_limit = max_frames;
343 if (maybe_stop (stop_limit)) {
344 no_roll (nframes, 0);
348 this_event = *next_event;
349 the_next_one = next_event;
356 this_nframes = nframes; /* real (jack) time relative */
357 frames_moved = (long) floor (_transport_speed * nframes); /* transport relative */
359 /* running an event, position transport precisely to its time */
360 if (this_event && this_event->action_frame <= end_frame && this_event->action_frame >= _transport_frame) {
361 /* this isn't quite right for reverse play */
362 frames_moved = (long) (this_event->action_frame - _transport_frame);
363 this_nframes = (nframes_t) abs( floor(frames_moved / _transport_speed) );
368 click (_transport_frame, nframes, offset);
370 /* now process frames between now and the first event in this block */
371 prepare_diskstreams ();
373 if (process_routes (this_nframes, offset)) {
374 no_roll (nframes, 0);
378 commit_diskstreams (this_nframes, session_needs_butler);
380 nframes -= this_nframes;
381 offset += this_nframes;
383 if (frames_moved < 0) {
384 decrement_transport_position (-frames_moved);
386 increment_transport_position (frames_moved);
389 maybe_stop (stop_limit);
390 check_declick_out ();
393 /* now handle this event and all others scheduled for the same time */
395 while (this_event && this_event->action_frame == _transport_frame) {
396 process_event (this_event);
398 if (the_next_one == events.end()) {
401 this_event = *the_next_one;
406 /* if an event left our state changing, do the right thing */
408 if (non_realtime_work_pending()) {
409 no_roll (nframes, offset);
413 /* this is necessary to handle the case of seamless looping */
414 end_frame = _transport_frame + (nframes_t) floor (nframes * _transport_speed);
420 } /* implicit release of route lock */
422 if (session_needs_butler)
427 Session::reset_slave_state ()
429 average_slave_delta = 1800;
430 delta_accumulator_cnt = 0;
431 have_first_delta_accumulator = false;
432 slave_state = Stopped;
436 Session::transport_locked () const
440 if (!locate_pending() && ((Config->get_slave_source() == None) || (sl && sl->ok() && sl->locked()))) {
448 Session::follow_slave (nframes_t nframes, nframes_t offset)
451 nframes_t slave_transport_frame;
452 nframes_t this_delta;
458 Config->set_slave_source (None);
462 _slave->speed_and_position (slave_speed, slave_transport_frame);
464 if (!_slave->locked()) {
468 if (slave_transport_frame > _transport_frame) {
469 this_delta = slave_transport_frame - _transport_frame;
472 this_delta = _transport_frame - slave_transport_frame;
476 if ((starting = _slave->starting())) {
481 cerr << "delta = " << (int) (dir * this_delta)
482 << " speed = " << slave_speed
483 << " ts = " << _transport_speed
484 << " M@"<< slave_transport_frame << " S@" << _transport_frame
485 << " avgdelta = " << average_slave_delta
489 if (Config->get_timecode_source_is_synced()) {
491 /* if the TC source is synced, then we assume that its
492 speed is binary: 0.0 or 1.0
495 if (slave_speed != 0.0f) {
501 /* TC source is able to drift relative to us (slave)
502 so we need to keep track of the drift and adjust
503 our speed to remain locked.
506 if (delta_accumulator_cnt >= delta_accumulator_size) {
507 have_first_delta_accumulator = true;
508 delta_accumulator_cnt = 0;
511 if (delta_accumulator_cnt != 0 || this_delta < _current_frame_rate) {
512 delta_accumulator[delta_accumulator_cnt++] = dir*this_delta;
515 if (have_first_delta_accumulator) {
516 average_slave_delta = 0;
517 for (int i = 0; i < delta_accumulator_size; ++i) {
518 average_slave_delta += delta_accumulator[i];
520 average_slave_delta /= delta_accumulator_size;
521 if (average_slave_delta < 0) {
523 average_slave_delta = -average_slave_delta;
527 // cerr << "avgdelta = " << average_slave_delta*average_dir << endl;
531 if (slave_speed != 0.0f) {
533 /* slave is running */
535 switch (slave_state) {
537 if (_slave->requires_seekahead()) {
538 slave_wait_end = slave_transport_frame + _current_frame_rate;
539 locate (slave_wait_end, false, false);
540 slave_state = Waiting;
545 slave_state = Running;
547 Location* al = _locations.auto_loop_location();
549 if (al && play_loop && (slave_transport_frame < al->start() || slave_transport_frame > al->end())) {
551 request_play_loop(false);
554 if (slave_transport_frame != _transport_frame) {
555 locate (slave_transport_frame, false, false);
568 if (slave_state == Waiting) {
570 // cerr << "waiting at " << slave_transport_frame << endl;
571 if (slave_transport_frame >= slave_wait_end) {
572 // cerr << "\tstart at " << _transport_frame << endl;
574 slave_state = Running;
577 nframes_t frame_delta = slave_transport_frame - _transport_frame;
579 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
581 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
582 if (!(*i)->can_internal_playback_seek (frame_delta)) {
589 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
590 (*i)->internal_playback_seek (frame_delta);
592 _transport_frame += frame_delta;
595 // cerr << "cannot micro-seek\n";
599 memset (delta_accumulator, 0, sizeof (nframes_t) * delta_accumulator_size);
600 average_slave_delta = 0;
605 if (slave_state == Running && _transport_speed == 0.0f) {
607 // cerr << "slave starts transport\n";
614 /* slave has stopped */
616 if (_transport_speed != 0.0f) {
618 // cerr << "slave stops transport: " << slave_speed << " frame: " << slave_transport_frame
619 // << " tf = " << _transport_frame
622 if (Config->get_slave_source() == JACK) {
623 last_stop_frame = _transport_frame;
629 if (slave_transport_frame != _transport_frame) {
630 // cerr << "slave stopped, move to " << slave_transport_frame << endl;
631 force_locate (slave_transport_frame, false);
634 slave_state = Stopped;
637 if (slave_state == Running && !Config->get_timecode_source_is_synced()) {
640 if (_transport_speed != 0.0f) {
643 note that average_dir is +1 or -1
646 const float adjust_seconds = 1.0f;
649 //if (average_slave_delta == 0) {
653 // delta = average_slave_delta;
654 // delta *= average_dir;
657 float adjusted_speed = slave_speed +
658 (delta / (adjust_seconds * _current_frame_rate));
660 // cerr << "adjust using " << delta
661 // << " towards " << adjusted_speed
662 // << " ratio = " << adjusted_speed / slave_speed
663 // << " current = " << _transport_speed
664 // << " slave @ " << slave_speed
667 request_transport_speed (adjusted_speed);
670 if ((nframes_t) average_slave_delta > _slave->resolution()) {
671 // cerr << "not locked\n";
678 if (!starting && !non_realtime_work_pending()) {
679 /* speed is set, we're locked, and good to go */
685 if (slave_speed && _transport_speed) {
687 /* something isn't right, but we should move with the master
693 prepare_diskstreams ();
694 silent_process_routes (nframes, offset);
695 commit_diskstreams (nframes, need_butler);
701 int32_t frames_moved = (int32_t) floor (_transport_speed * nframes);
703 if (frames_moved < 0) {
704 decrement_transport_position (-frames_moved);
706 increment_transport_position (frames_moved);
709 nframes_t stop_limit;
711 if (actively_recording()) {
712 stop_limit = max_frames;
714 if (Config->get_stop_at_session_end()) {
715 stop_limit = current_end_frame();
717 stop_limit = max_frames;
721 maybe_stop (stop_limit);
725 /* don't move at all */
726 no_roll (nframes, 0);
731 Session::process_without_events (nframes_t nframes)
733 bool session_needs_butler = false;
734 nframes_t stop_limit;
736 nframes_t offset = 0;
739 if (post_transport_work & (PostTransportLocate|PostTransportStop)) {
740 no_roll (nframes, 0);
744 if (!_exporting && _slave) {
745 if (!follow_slave (nframes, 0)) {
750 if (_transport_speed == 0) {
751 no_roll (nframes, 0);
755 send_midi_time_code_for_cycle(nframes);
757 if (actively_recording()) {
758 stop_limit = max_frames;
760 if (Config->get_stop_at_session_end()) {
761 stop_limit = current_end_frame();
763 stop_limit = max_frames;
767 if (maybe_stop (stop_limit)) {
768 no_roll (nframes, 0);
772 if (maybe_sync_start (nframes, offset)) {
776 click (_transport_frame, nframes, offset);
778 prepare_diskstreams ();
780 frames_moved = (long) floor (_transport_speed * nframes);
782 if (process_routes (nframes, offset)) {
783 no_roll (nframes, offset);
787 commit_diskstreams (nframes, session_needs_butler);
789 if (frames_moved < 0) {
790 decrement_transport_position (-frames_moved);
792 increment_transport_position (frames_moved);
795 maybe_stop (stop_limit);
796 check_declick_out ();
798 } /* implicit release of route lock */
800 if (session_needs_butler)
805 Session::process_audition (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 (nframes_t& nframes, nframes_t& offset)
847 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);