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.
26 #include <pbd/error.h>
28 #include <glibmm/thread.h>
30 #include <ardour/ardour.h>
31 #include <ardour/session.h>
32 #include <ardour/timestamps.h>
33 #include <ardour/audio_diskstream.h>
34 #include <ardour/audioengine.h>
35 #include <ardour/slave.h>
36 #include <ardour/auditioner.h>
37 #include <ardour/cycles.h>
38 #include <ardour/cycle_timer.h>
40 #include <midi++/manager.h>
44 using namespace ARDOUR;
49 Session::process (jack_nframes_t nframes)
51 MIDI::Manager::instance()->cycle_start(nframes);
53 if (synced_to_jack() && waiting_to_start) {
54 if ( _engine.transport_state() == AudioEngine::TransportRolling) {
55 actually_start_transport ();
59 if (non_realtime_work_pending()) {
60 if (g_atomic_int_get (&butler_should_do_transport_work) == 0) {
65 (this->*process_function) (nframes);
67 MIDI::Manager::instance()->cycle_end();
69 SendFeedback (); /* EMIT SIGNAL */
73 Session::prepare_diskstreams ()
75 for (DiskstreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
81 Session::no_roll (jack_nframes_t nframes, jack_nframes_t offset)
83 jack_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 (jack_nframes_t nframes, jack_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 for (DiskstreamList::iterator ids = diskstreams.begin(); ids != diskstreams.end(); ++ids) {
166 Session::silent_process_routes (jack_nframes_t nframes, jack_nframes_t offset)
168 bool record_active = actively_recording();
169 int declick = get_transport_declick_required();
170 bool rec_monitors = get_rec_monitors_input();
171 boost::shared_ptr<RouteList> r = routes.reader ();
173 if (transport_sub_state & StopPendingCapture) {
174 /* force a declick out */
178 const jack_nframes_t start_frame = _transport_frame;
179 const jack_nframes_t end_frame = _transport_frame + lrintf(nframes * _transport_speed);
181 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
185 if ((*i)->hidden()) {
189 if ((ret = (*i)->silent_roll (nframes, start_frame, end_frame, offset, record_active, rec_monitors)) < 0) {
191 /* we have to do this here. Route::roll() for an AudioTrack will have called AudioDiskstream::process(),
192 and the DS will expect AudioDiskstream::commit() to be called. but we're aborting from that
193 call path, so make sure we release any outstanding locks here before we return failure.
196 for (DiskstreamList::iterator ids = diskstreams.begin(); ids != diskstreams.end(); ++ids) {
209 Session::commit_diskstreams (jack_nframes_t nframes, bool &needs_butler)
215 for (DiskstreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
217 if ((*i)->hidden()) {
221 /* force all diskstreams not handled by a Route to call do their stuff.
224 if ((dret = (*i)->process (_transport_frame, nframes, 0, actively_recording(), get_rec_monitors_input())) == 0) {
225 if ((*i)->commit (nframes)) {
229 } else if (dret < 0) {
233 pworst = min (pworst, (*i)->playback_buffer_load());
234 cworst = min (cworst, (*i)->capture_buffer_load());
237 uint32_t pmin = g_atomic_int_get (&_playback_load);
238 uint32_t pminold = g_atomic_int_get (&_playback_load_min);
239 uint32_t cmin = g_atomic_int_get (&_capture_load);
240 uint32_t cminold = g_atomic_int_get (&_capture_load_min);
242 g_atomic_int_set (&_playback_load, (uint32_t) floor (pworst * 100.0f));
243 g_atomic_int_set (&_capture_load, (uint32_t) floor (cworst * 100.0f));
244 g_atomic_int_set (&_playback_load_min, min (pmin, pminold));
245 g_atomic_int_set (&_capture_load_min, min (cmin, cminold));
247 if (actively_recording()) {
254 Session::process_with_events (jack_nframes_t nframes)
257 jack_nframes_t this_nframes;
258 jack_nframes_t end_frame;
259 jack_nframes_t offset;
260 jack_nframes_t stop_limit;
262 bool session_needs_butler = false;
264 /* make sure the auditioner is silent */
267 auditioner->silence (nframes, 0);
270 /* handle any pending events */
272 while (pending_events.read (&ev, 1) == 1) {
276 /* if we are not in the middle of a state change,
277 and there are immediate events queued up,
281 while (!non_realtime_work_pending() && !immediate_events.empty()) {
282 Event *ev = immediate_events.front ();
283 immediate_events.pop_front ();
287 /* Events caused a transport change, send an MTC Full Frame (SMPTE) message.
288 * This is sent whether rolling or not, to give slaves an idea of ardour time
289 * on locates (and allow slow slaves to position and prepare for rolling)
291 if (_send_smpte_update) {
292 send_full_time_code(nframes);
295 if (!process_can_proceed()) {
296 no_roll (nframes, 0);
300 if (events.empty() || next_event == events.end()) {
301 process_without_events (nframes);
305 end_frame = _transport_frame + nframes;
308 Glib::RWLock::ReaderLock dsm (diskstream_lock, Glib::TRY_LOCK);
311 Events::iterator the_next_one;
313 if (!dsm.locked() || (post_transport_work & (PostTransportLocate|PostTransportStop))) {
314 no_roll (nframes, 0);
318 if (!_exporting && _slave) {
319 if (!follow_slave (nframes, 0)) {
324 if (_transport_speed == 0) {
325 no_roll (nframes, 0);
329 send_midi_time_code_for_cycle(nframes);
331 if (actively_recording()) {
332 stop_limit = max_frames;
335 if (Config->get_stop_at_session_end()) {
336 stop_limit = current_end_frame();
338 stop_limit = max_frames;
342 if (maybe_stop (stop_limit)) {
343 no_roll (nframes, 0);
347 this_event = *next_event;
348 the_next_one = next_event;
355 if (this_event == 0 || this_event->action_frame > end_frame || this_event->action_frame < _transport_frame) {
357 this_nframes = nframes;
361 /* compute nframes to next event */
363 if (this_event->action_frame < end_frame) {
364 this_nframes = nframes - (end_frame - this_event->action_frame);
366 this_nframes = nframes;
372 click (_transport_frame, nframes, offset);
374 /* now process frames between now and the first event in this block */
375 prepare_diskstreams ();
377 if (process_routes (this_nframes, offset)) {
378 no_roll (nframes, 0);
382 commit_diskstreams (this_nframes, session_needs_butler);
384 nframes -= this_nframes;
385 offset += this_nframes;
387 frames_moved = (jack_nframes_t) floor (_transport_speed * this_nframes);
389 if (frames_moved < 0) {
390 decrement_transport_position (-frames_moved);
392 increment_transport_position (frames_moved);
395 maybe_stop (stop_limit);
396 check_declick_out ();
399 /* now handle this event and all others scheduled for the same time */
401 while (this_event && this_event->action_frame == _transport_frame) {
402 process_event (this_event);
404 if (the_next_one == events.end()) {
407 this_event = *the_next_one;
412 /* if an event left our state changing, do the right thing */
414 if (non_realtime_work_pending()) {
415 no_roll (nframes, offset);
419 /* this is necessary to handle the case of seamless looping */
420 /* not sure if it will work in conjuction with varispeed */
421 end_frame = _transport_frame + nframes;
427 } /* implicit release of route lock */
429 if (session_needs_butler)
434 Session::reset_slave_state ()
436 average_slave_delta = 1800;
437 delta_accumulator_cnt = 0;
438 have_first_delta_accumulator = false;
439 slave_state = Stopped;
443 Session::transport_locked () const
447 if (!locate_pending() && ((_slave_type == None) || (sl && sl->ok() && sl->locked()))) {
455 Session::follow_slave (jack_nframes_t nframes, jack_nframes_t offset)
458 jack_nframes_t slave_transport_frame;
459 jack_nframes_t this_delta;
465 set_slave_source (None, 0);
469 _slave->speed_and_position (slave_speed, slave_transport_frame);
471 if (!_slave->locked()) {
475 if (slave_transport_frame > _transport_frame) {
476 this_delta = slave_transport_frame - _transport_frame;
479 this_delta = _transport_frame - slave_transport_frame;
483 if ((starting = _slave->starting())) {
488 cerr << "delta = " << (int) (dir * this_delta)
489 << " speed = " << slave_speed
490 << " ts = " << _transport_speed
491 << " M@"<< slave_transport_frame << " S@" << _transport_frame
492 << " avgdelta = " << average_slave_delta
496 if (Config->get_timecode_source_is_synced()) {
498 /* if the TC source is synced, then we assume that its
499 speed is binary: 0.0 or 1.0
502 if (slave_speed != 0.0f) {
508 /* TC source is able to drift relative to us (slave)
509 so we need to keep track of the drift and adjust
510 our speed to remain locked.
513 if (delta_accumulator_cnt >= delta_accumulator_size) {
514 have_first_delta_accumulator = true;
515 delta_accumulator_cnt = 0;
518 if (delta_accumulator_cnt != 0 || this_delta < _current_frame_rate) {
519 delta_accumulator[delta_accumulator_cnt++] = dir*this_delta;
522 if (have_first_delta_accumulator) {
523 average_slave_delta = 0;
524 for (int i = 0; i < delta_accumulator_size; ++i) {
525 average_slave_delta += delta_accumulator[i];
527 average_slave_delta /= delta_accumulator_size;
528 if (average_slave_delta < 0) {
530 average_slave_delta = -average_slave_delta;
534 // cerr << "avgdelta = " << average_slave_delta*average_dir << endl;
538 if (slave_speed != 0.0f) {
540 /* slave is running */
542 switch (slave_state) {
544 if (_slave->requires_seekahead()) {
545 slave_wait_end = slave_transport_frame + _current_frame_rate;
546 locate (slave_wait_end, false, false);
547 slave_state = Waiting;
552 slave_state = Running;
554 Location* al = _locations.auto_loop_location();
556 if (al && auto_loop && (slave_transport_frame < al->start() || slave_transport_frame > al->end())) {
558 request_auto_loop(false);
561 if (slave_transport_frame != _transport_frame) {
562 locate (slave_transport_frame, false, false);
575 if (slave_state == Waiting) {
577 // cerr << "waiting at " << slave_transport_frame << endl;
578 Glib::RWLock::ReaderLock dsm (diskstream_lock, Glib::TRY_LOCK);
580 if (dsm.locked() && slave_transport_frame >= slave_wait_end) {
581 // cerr << "\tstart at " << _transport_frame << endl;
583 slave_state = Running;
586 jack_nframes_t frame_delta = slave_transport_frame - _transport_frame;
588 for (DiskstreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
589 if (!(*i)->can_internal_playback_seek (frame_delta)) {
596 for (DiskstreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
597 (*i)->internal_playback_seek (frame_delta);
599 _transport_frame += frame_delta;
602 // cerr << "cannot micro-seek\n";
606 memset (delta_accumulator, 0, sizeof (jack_nframes_t) * delta_accumulator_size);
607 average_slave_delta = 0;
612 if (slave_state == Running && _transport_speed == 0.0f) {
614 // cerr << "slave starts transport\n";
621 /* slave has stopped */
623 if (_transport_speed != 0.0f) {
625 // cerr << "slave stops transport: " << slave_speed << " frame: " << slave_transport_frame
626 // << " tf = " << _transport_frame
629 if (_slave_type == JACK) {
630 last_stop_frame = _transport_frame;
636 if (slave_transport_frame != _transport_frame) {
637 // cerr << "slave stopped, move to " << slave_transport_frame << endl;
638 force_locate (slave_transport_frame, false);
641 slave_state = Stopped;
644 if (slave_state == Running && !Config->get_timecode_source_is_synced()) {
647 if (_transport_speed != 0.0f) {
650 note that average_dir is +1 or -1
653 const float adjust_seconds = 1.0f;
656 //if (average_slave_delta == 0) {
660 // delta = average_slave_delta;
661 // delta *= average_dir;
664 float adjusted_speed = slave_speed +
665 (delta / (adjust_seconds * _current_frame_rate));
667 // cerr << "adjust using " << delta
668 // << " towards " << adjusted_speed
669 // << " ratio = " << adjusted_speed / slave_speed
670 // << " current = " << _transport_speed
671 // << " slave @ " << slave_speed
674 request_transport_speed (adjusted_speed);
677 if ((jack_nframes_t) average_slave_delta > _slave->resolution()) {
678 // cerr << "not locked\n";
685 if (!starting && !non_realtime_work_pending()) {
686 /* speed is set, we're locked, and good to go */
692 if (slave_speed && _transport_speed) {
694 /* something isn't right, but we should move with the master
700 Glib::RWLock::ReaderLock dsm (diskstream_lock, Glib::TRY_LOCK);
706 prepare_diskstreams ();
707 silent_process_routes (nframes, offset);
708 commit_diskstreams (nframes, need_butler);
714 int32_t frames_moved = (int32_t) floor (_transport_speed * nframes);
716 if (frames_moved < 0) {
717 decrement_transport_position (-frames_moved);
719 increment_transport_position (frames_moved);
722 jack_nframes_t stop_limit;
724 if (actively_recording()) {
725 stop_limit = max_frames;
727 if (Config->get_stop_at_session_end()) {
728 stop_limit = current_end_frame();
730 stop_limit = max_frames;
734 maybe_stop (stop_limit);
738 /* don't move at all */
739 no_roll (nframes, 0);
744 Session::process_without_events (jack_nframes_t nframes)
746 bool session_needs_butler = false;
747 jack_nframes_t stop_limit;
751 Glib::RWLock::ReaderLock dsm (diskstream_lock, Glib::TRY_LOCK);
753 if (!dsm.locked() || (post_transport_work & (PostTransportLocate|PostTransportStop))) {
754 no_roll (nframes, 0);
758 if (!_exporting && _slave) {
759 if (!follow_slave (nframes, 0)) {
764 if (_transport_speed == 0) {
765 no_roll (nframes, 0);
769 send_midi_time_code_for_cycle(nframes);
771 if (actively_recording()) {
772 stop_limit = max_frames;
774 if (Config->get_stop_at_session_end()) {
775 stop_limit = current_end_frame();
777 stop_limit = max_frames;
781 if (maybe_stop (stop_limit)) {
782 no_roll (nframes, 0);
786 click (_transport_frame, nframes, 0);
788 prepare_diskstreams ();
790 frames_moved = (long) floor (_transport_speed * nframes);
792 if (process_routes (nframes, 0)) {
793 no_roll (nframes, 0);
797 commit_diskstreams (nframes, session_needs_butler);
799 if (frames_moved < 0) {
800 decrement_transport_position (-frames_moved);
802 increment_transport_position (frames_moved);
805 maybe_stop (stop_limit);
806 check_declick_out ();
808 } /* implicit release of route lock */
810 if (session_needs_butler)
815 Session::process_audition (jack_nframes_t nframes)
818 boost::shared_ptr<RouteList> r = routes.reader ();
820 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
821 if (!(*i)->hidden()) {
822 (*i)->silence (nframes, 0);
826 /* run the auditioner, and if it says we need butler service, ask for it */
828 if (auditioner->play_audition (nframes) > 0) {
832 /* handle pending events */
834 while (pending_events.read (&ev, 1) == 1) {
838 /* if we are not in the middle of a state change,
839 and there are immediate events queued up,
843 while (!non_realtime_work_pending() && !immediate_events.empty()) {
844 Event *ev = immediate_events.front ();
845 immediate_events.pop_front ();
849 if (!auditioner->active()) {
850 process_function = &Session::process_with_events;