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;
47 /** Called by the audio engine when there is work to be done with JACK.
48 * @param nframes Number of frames to process.
51 Session::process (nframes_t nframes)
53 MIDI::Manager::instance()->cycle_start(nframes);
55 if (synced_to_jack() && waiting_to_start) {
56 if ( _engine.transport_state() == AudioEngine::TransportRolling) {
57 actually_start_transport ();
61 if (non_realtime_work_pending()) {
62 if (!transport_work_requested ()) {
67 (this->*process_function) (nframes);
69 MIDI::Manager::instance()->cycle_end();
71 SendFeedback (); /* EMIT SIGNAL */
75 Session::prepare_diskstreams ()
77 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
78 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
84 Session::no_roll (nframes_t nframes, nframes_t offset)
86 nframes_t end_frame = _transport_frame + nframes; // FIXME: varispeed + no_roll ??
88 bool declick = get_transport_declick_required();
89 boost::shared_ptr<RouteList> r = routes.reader ();
92 _click_io->silence (nframes, offset);
95 if (g_atomic_int_get (&processing_prohibited)) {
96 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
97 (*i)->silence (nframes, offset);
102 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
104 if ((*i)->is_hidden()) {
108 (*i)->set_pending_declick (declick);
110 if ((*i)->no_roll (nframes, _transport_frame, end_frame, offset, non_realtime_work_pending(),
111 actively_recording(), declick)) {
112 error << string_compose(_("Session: error in no roll for %1"), (*i)->name()) << endmsg;
122 Session::process_routes (nframes_t nframes, nframes_t offset)
125 int declick = get_transport_declick_required();
126 bool rec_monitors = get_rec_monitors_input();
127 boost::shared_ptr<RouteList> r = routes.reader ();
129 if (transport_sub_state & StopPendingCapture) {
130 /* force a declick out */
134 record_active = actively_recording(); // || (get_record_enabled() && get_punch_in());
136 const nframes_t start_frame = _transport_frame;
137 const nframes_t end_frame = _transport_frame + (nframes_t)floor(nframes * _transport_speed);
139 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
143 if ((*i)->is_hidden()) {
147 (*i)->set_pending_declick (declick);
149 if ((ret = (*i)->roll (nframes, start_frame, end_frame, offset, declick, record_active, rec_monitors)) < 0) {
151 /* we have to do this here. Route::roll() for an AudioTrack will have called AudioDiskstream::process(),
152 and the DS will expect AudioDiskstream::commit() to be called. but we're aborting from that
153 call path, so make sure we release any outstanding locks here before we return failure.
156 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
157 for (DiskstreamList::iterator ids = dsl->begin(); ids != dsl->end(); ++ids) {
170 Session::silent_process_routes (nframes_t nframes, nframes_t offset)
172 bool record_active = actively_recording();
173 int declick = get_transport_declick_required();
174 bool rec_monitors = get_rec_monitors_input();
175 boost::shared_ptr<RouteList> r = routes.reader ();
177 if (transport_sub_state & StopPendingCapture) {
178 /* force a declick out */
182 const nframes_t start_frame = _transport_frame;
183 const nframes_t end_frame = _transport_frame + lrintf(nframes * _transport_speed);
185 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
189 if ((*i)->is_hidden()) {
193 if ((ret = (*i)->silent_roll (nframes, start_frame, end_frame, offset, record_active, rec_monitors)) < 0) {
195 /* we have to do this here. Route::roll() for an AudioTrack will have called AudioDiskstream::process(),
196 and the DS will expect AudioDiskstream::commit() to be called. but we're aborting from that
197 call path, so make sure we release any outstanding locks here before we return failure.
200 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
201 for (DiskstreamList::iterator ids = dsl->begin(); ids != dsl->end(); ++ids) {
214 Session::commit_diskstreams (nframes_t nframes, bool &needs_butler)
220 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
221 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
223 if ((*i)->hidden()) {
227 /* force all diskstreams not handled by a Route to call do their stuff.
228 Note: the diskstreams that were handled by a route will just return zero
229 from this call, because they know they were processed. So in fact, this
230 also runs commit() for every diskstream.
233 if ((dret = (*i)->process (_transport_frame, nframes, 0, actively_recording(), get_rec_monitors_input())) == 0) {
234 if ((*i)->commit (nframes)) {
238 } else if (dret < 0) {
242 pworst = min (pworst, (*i)->playback_buffer_load());
243 cworst = min (cworst, (*i)->capture_buffer_load());
246 uint32_t pmin = g_atomic_int_get (&_playback_load);
247 uint32_t pminold = g_atomic_int_get (&_playback_load_min);
248 uint32_t cmin = g_atomic_int_get (&_capture_load);
249 uint32_t cminold = g_atomic_int_get (&_capture_load_min);
251 g_atomic_int_set (&_playback_load, (uint32_t) floor (pworst * 100.0f));
252 g_atomic_int_set (&_capture_load, (uint32_t) floor (cworst * 100.0f));
253 g_atomic_int_set (&_playback_load_min, min (pmin, pminold));
254 g_atomic_int_set (&_capture_load_min, min (cmin, cminold));
256 if (actively_recording()) {
261 /** Process callback used when the auditioner is not active */
263 Session::process_with_events (nframes_t nframes)
266 nframes_t this_nframes;
269 nframes_t stop_limit;
271 bool session_needs_butler = false;
273 /* make sure the auditioner is silent */
276 auditioner->silence (nframes, 0);
279 /* handle any pending events */
281 while (pending_events.read (&ev, 1) == 1) {
285 /* if we are not in the middle of a state change,
286 and there are immediate events queued up,
290 while (!non_realtime_work_pending() && !immediate_events.empty()) {
291 Event *ev = immediate_events.front ();
292 immediate_events.pop_front ();
296 /* Events caused a transport change, send an MTC Full Frame (SMPTE) message.
297 * This is sent whether rolling or not, to give slaves an idea of ardour time
298 * on locates (and allow slow slaves to position and prepare for rolling)
300 if (_send_smpte_update) {
301 send_full_time_code(nframes);
304 if (!process_can_proceed()) {
305 no_roll (nframes, 0);
309 if (events.empty() || next_event == events.end()) {
310 process_without_events (nframes);
314 end_frame = _transport_frame + (nframes_t)abs(floor(nframes * _transport_speed));
318 Events::iterator the_next_one;
320 if (post_transport_work & (PostTransportLocate|PostTransportStop)) {
321 no_roll (nframes, 0);
325 if (!_exporting && _slave) {
326 if (!follow_slave (nframes, 0)) {
331 if (_transport_speed == 0) {
332 no_roll (nframes, 0);
336 send_midi_time_code_for_cycle(nframes);
338 if (actively_recording()) {
339 stop_limit = max_frames;
342 if (Config->get_stop_at_session_end()) {
343 stop_limit = current_end_frame();
345 stop_limit = max_frames;
349 if (maybe_stop (stop_limit)) {
350 no_roll (nframes, 0);
354 this_event = *next_event;
355 the_next_one = next_event;
360 /* yes folks, here it is, the actual loop where we really truly
361 process some audio */
364 this_nframes = nframes; /* real (jack) time relative */
365 frames_moved = (long) floor (_transport_speed * nframes); /* transport relative */
367 /* running an event, position transport precisely to its time */
368 if (this_event && this_event->action_frame <= end_frame && this_event->action_frame >= _transport_frame) {
369 /* this isn't quite right for reverse play */
370 frames_moved = (long) (this_event->action_frame - _transport_frame);
371 this_nframes = (nframes_t) abs( floor(frames_moved / _transport_speed) );
376 click (_transport_frame, nframes, offset);
378 /* now process frames between now and the first event in this block */
379 prepare_diskstreams ();
381 if (process_routes (this_nframes, offset)) {
382 no_roll (nframes, 0);
386 commit_diskstreams (this_nframes, session_needs_butler);
388 nframes -= this_nframes;
389 offset += this_nframes;
391 if (frames_moved < 0) {
392 decrement_transport_position (-frames_moved);
394 increment_transport_position (frames_moved);
397 maybe_stop (stop_limit);
398 check_declick_out ();
401 /* now handle this event and all others scheduled for the same time */
403 while (this_event && this_event->action_frame == _transport_frame) {
404 process_event (this_event);
406 if (the_next_one == events.end()) {
409 this_event = *the_next_one;
414 /* if an event left our state changing, do the right thing */
416 if (non_realtime_work_pending()) {
417 no_roll (nframes, offset);
421 /* this is necessary to handle the case of seamless looping */
422 end_frame = _transport_frame + (nframes_t) floor (nframes * _transport_speed);
428 } /* implicit release of route lock */
430 if (session_needs_butler)
435 Session::reset_slave_state ()
437 average_slave_delta = 1800;
438 delta_accumulator_cnt = 0;
439 have_first_delta_accumulator = false;
440 slave_state = Stopped;
444 Session::transport_locked () const
448 if (!locate_pending() && ((Config->get_slave_source() == None) || (sl && sl->ok() && sl->locked()))) {
456 Session::follow_slave (nframes_t nframes, nframes_t offset)
459 nframes_t slave_transport_frame;
460 nframes_t this_delta;
466 Config->set_slave_source (None);
470 _slave->speed_and_position (slave_speed, slave_transport_frame);
472 if (!_slave->locked()) {
476 if (slave_transport_frame > _transport_frame) {
477 this_delta = slave_transport_frame - _transport_frame;
480 this_delta = _transport_frame - slave_transport_frame;
484 if ((starting = _slave->starting())) {
489 cerr << "delta = " << (int) (dir * this_delta)
490 << " speed = " << slave_speed
491 << " ts = " << _transport_speed
492 << " M@ "<< slave_transport_frame << " S@ " << _transport_frame
493 << " avgdelta = " << average_slave_delta
497 if (Config->get_timecode_source_is_synced()) {
499 /* if the TC source is synced, then we assume that its
500 speed is binary: 0.0 or 1.0
503 if (slave_speed != 0.0f) {
509 /* TC source is able to drift relative to us (slave)
510 so we need to keep track of the drift and adjust
511 our speed to remain locked.
514 if (delta_accumulator_cnt >= delta_accumulator_size) {
515 have_first_delta_accumulator = true;
516 delta_accumulator_cnt = 0;
519 if (delta_accumulator_cnt != 0 || this_delta < _current_frame_rate) {
520 delta_accumulator[delta_accumulator_cnt++] = dir*this_delta;
523 if (have_first_delta_accumulator) {
524 average_slave_delta = 0;
525 for (int i = 0; i < delta_accumulator_size; ++i) {
526 average_slave_delta += delta_accumulator[i];
528 average_slave_delta /= delta_accumulator_size;
529 if (average_slave_delta < 0) {
531 average_slave_delta = -average_slave_delta;
535 // cerr << "avgdelta = " << average_slave_delta*average_dir << endl;
539 if (slave_speed != 0.0f) {
541 /* slave is running */
543 switch (slave_state) {
545 if (_slave->requires_seekahead()) {
546 slave_wait_end = slave_transport_frame + _current_frame_rate;
547 locate (slave_wait_end, false, false);
548 slave_state = Waiting;
553 slave_state = Running;
555 Location* al = _locations.auto_loop_location();
557 if (al && play_loop && (slave_transport_frame < al->start() || slave_transport_frame > al->end())) {
559 request_play_loop(false);
562 if (slave_transport_frame != _transport_frame) {
563 locate (slave_transport_frame, false, false);
576 if (slave_state == Waiting) {
578 // cerr << "waiting at " << slave_transport_frame << endl;
579 if (slave_transport_frame >= slave_wait_end) {
580 // cerr << "\tstart at " << _transport_frame << endl;
582 slave_state = Running;
585 nframes_t frame_delta = slave_transport_frame - _transport_frame;
587 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
589 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
590 if (!(*i)->can_internal_playback_seek (frame_delta)) {
597 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
598 (*i)->internal_playback_seek (frame_delta);
600 _transport_frame += frame_delta;
603 // cerr << "cannot micro-seek\n";
607 memset (delta_accumulator, 0, sizeof (nframes_t) * delta_accumulator_size);
608 average_slave_delta = 0;
613 if (slave_state == Running && _transport_speed == 0.0f) {
615 // cerr << "slave starts transport\n";
622 /* slave has stopped */
624 if (_transport_speed != 0.0f) {
626 // cerr << "slave stops transport: " << slave_speed << " frame: " << slave_transport_frame
627 // << " tf = " << _transport_frame
630 if (Config->get_slave_source() == JACK) {
631 last_stop_frame = _transport_frame;
637 if (slave_transport_frame != _transport_frame) {
638 // cerr << "slave stopped, move to " << slave_transport_frame << endl;
639 force_locate (slave_transport_frame, false);
642 slave_state = Stopped;
645 if (slave_state == Running && !Config->get_timecode_source_is_synced()) {
648 if (_transport_speed != 0.0f) {
651 note that average_dir is +1 or -1
654 const float adjust_seconds = 1.0f;
657 //if (average_slave_delta == 0) {
661 // delta = average_slave_delta;
662 // delta *= average_dir;
665 float adjusted_speed = slave_speed +
666 (delta / (adjust_seconds * _current_frame_rate));
668 // cerr << "adjust using " << delta
669 // << " towards " << adjusted_speed
670 // << " ratio = " << adjusted_speed / slave_speed
671 // << " current = " << _transport_speed
672 // << " slave @ " << slave_speed
675 request_transport_speed (adjusted_speed);
678 if ((nframes_t) average_slave_delta > _slave->resolution()) {
679 // cerr << "not locked\n";
686 if (!starting && !non_realtime_work_pending()) {
687 /* speed is set, we're locked, and good to go */
693 if (slave_speed && _transport_speed) {
695 /* something isn't right, but we should move with the master
701 prepare_diskstreams ();
702 silent_process_routes (nframes, offset);
703 commit_diskstreams (nframes, need_butler);
709 int32_t frames_moved = (int32_t) floor (_transport_speed * nframes);
711 if (frames_moved < 0) {
712 decrement_transport_position (-frames_moved);
714 increment_transport_position (frames_moved);
717 nframes_t stop_limit;
719 if (actively_recording()) {
720 stop_limit = max_frames;
722 if (Config->get_stop_at_session_end()) {
723 stop_limit = current_end_frame();
725 stop_limit = max_frames;
729 maybe_stop (stop_limit);
733 /* don't move at all */
734 no_roll (nframes, 0);
739 Session::process_without_events (nframes_t nframes)
741 bool session_needs_butler = false;
742 nframes_t stop_limit;
744 nframes_t offset = 0;
747 if (post_transport_work & (PostTransportLocate|PostTransportStop)) {
748 no_roll (nframes, 0);
752 if (!_exporting && _slave) {
753 if (!follow_slave (nframes, 0)) {
758 if (_transport_speed == 0) {
759 no_roll (nframes, 0);
763 send_midi_time_code_for_cycle(nframes);
765 if (actively_recording()) {
766 stop_limit = max_frames;
768 if (Config->get_stop_at_session_end()) {
769 stop_limit = current_end_frame();
771 stop_limit = max_frames;
775 if (maybe_stop (stop_limit)) {
776 no_roll (nframes, 0);
780 if (maybe_sync_start (nframes, offset)) {
784 click (_transport_frame, nframes, offset);
786 prepare_diskstreams ();
788 frames_moved = (long) floor (_transport_speed * nframes);
790 if (process_routes (nframes, offset)) {
791 no_roll (nframes, offset);
795 commit_diskstreams (nframes, session_needs_butler);
797 if (frames_moved < 0) {
798 decrement_transport_position (-frames_moved);
800 increment_transport_position (frames_moved);
803 maybe_stop (stop_limit);
804 check_declick_out ();
806 } /* implicit release of route lock */
808 if (session_needs_butler)
812 /** Process callback used when the auditioner is active.
813 * @param nframes number of frames to process.
816 Session::process_audition (nframes_t nframes)
819 boost::shared_ptr<RouteList> r = routes.reader ();
821 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
822 if (!(*i)->is_hidden()) {
823 (*i)->silence (nframes, 0);
827 /* run the auditioner, and if it says we need butler service, ask for it */
829 if (auditioner->play_audition (nframes) > 0) {
833 /* handle pending events */
835 while (pending_events.read (&ev, 1) == 1) {
839 /* if we are not in the middle of a state change,
840 and there are immediate events queued up,
844 while (!non_realtime_work_pending() && !immediate_events.empty()) {
845 Event *ev = immediate_events.front ();
846 immediate_events.pop_front ();
850 if (!auditioner->active()) {
851 /* auditioner no longer active, so go back to the normal process callback */
852 process_function = &Session::process_with_events;
857 Session::maybe_sync_start (nframes_t& nframes, nframes_t& offset)
859 nframes_t sync_offset;
861 if (!waiting_for_sync_offset) {
865 if (_engine.get_sync_offset (sync_offset) && sync_offset < nframes) {
867 no_roll (sync_offset, 0);
868 nframes -= sync_offset;
869 offset += sync_offset;
870 waiting_for_sync_offset = false;
877 no_roll (nframes, 0);