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 // This is no more the appriopriate place to call cycle
54 // start. cycle_start needs to be called at the Route::roll()
55 // where the signals which we want to mixdown have been calculated.
57 MIDI::Manager::instance()->cycle_start(nframes);
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 bool session_needs_butler = false;
270 nframes_t stop_limit;
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()) {
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 (!process_can_proceed()) {
325 if (!_exporting && _slave) {
326 if (!follow_slave (nframes, 0)) {
331 if (_transport_speed == 0) {
332 no_roll (nframes, 0);
337 send_midi_time_code_for_cycle (nframes);
340 if (actively_recording()) {
341 stop_limit = max_frames;
344 if (Config->get_stop_at_session_end()) {
345 stop_limit = current_end_frame();
347 stop_limit = max_frames;
351 if (maybe_stop (stop_limit)) {
352 no_roll (nframes, 0);
356 this_event = *next_event;
357 the_next_one = next_event;
362 /* yes folks, here it is, the actual loop where we really truly
363 process some audio */
366 this_nframes = nframes; /* real (jack) time relative */
367 frames_moved = (long) floor (_transport_speed * nframes); /* transport relative */
369 /* running an event, position transport precisely to its time */
370 if (this_event && this_event->action_frame <= end_frame && this_event->action_frame >= _transport_frame) {
371 /* this isn't quite right for reverse play */
372 frames_moved = (long) (this_event->action_frame - _transport_frame);
373 this_nframes = (nframes_t) abs( floor(frames_moved / _transport_speed) );
378 click (_transport_frame, nframes, offset);
380 /* now process frames between now and the first event in this block */
381 prepare_diskstreams ();
383 if (process_routes (this_nframes, offset)) {
384 no_roll (nframes, 0);
388 commit_diskstreams (this_nframes, session_needs_butler);
390 nframes -= this_nframes;
391 offset += this_nframes;
393 if (frames_moved < 0) {
394 decrement_transport_position (-frames_moved);
396 increment_transport_position (frames_moved);
399 maybe_stop (stop_limit);
400 check_declick_out ();
403 /* now handle this event and all others scheduled for the same time */
405 while (this_event && this_event->action_frame == _transport_frame) {
406 process_event (this_event);
408 if (the_next_one == events.end()) {
411 this_event = *the_next_one;
416 /* if an event left our state changing, do the right thing */
418 if (non_realtime_work_pending()) {
419 no_roll (nframes, offset);
423 /* this is necessary to handle the case of seamless looping */
424 end_frame = _transport_frame + (nframes_t) floor (nframes * _transport_speed);
430 } /* implicit release of route lock */
432 if (session_needs_butler)
437 Session::reset_slave_state ()
439 average_slave_delta = 1800;
440 delta_accumulator_cnt = 0;
441 have_first_delta_accumulator = false;
442 slave_state = Stopped;
446 Session::transport_locked () const
450 if (!locate_pending() && ((Config->get_slave_source() == None) || (sl && sl->ok() && sl->locked()))) {
458 Session::follow_slave (nframes_t nframes, nframes_t offset)
461 nframes_t slave_transport_frame;
462 nframes_t this_delta;
468 Config->set_slave_source (None);
472 _slave->speed_and_position (slave_speed, slave_transport_frame);
474 if (!_slave->locked()) {
478 if (slave_transport_frame > _transport_frame) {
479 this_delta = slave_transport_frame - _transport_frame;
482 this_delta = _transport_frame - slave_transport_frame;
486 if ((starting = _slave->starting())) {
491 cerr << "delta = " << (int) (dir * this_delta)
492 << " speed = " << slave_speed
493 << " ts = " << _transport_speed
494 << " M@ "<< slave_transport_frame << " S@ " << _transport_frame
495 << " avgdelta = " << average_slave_delta
499 if (_slave->is_always_synced() || Config->get_timecode_source_is_synced()) {
501 /* if the TC source is synced, then we assume that its
502 speed is binary: 0.0 or 1.0
505 if (slave_speed != 0.0f) {
511 /* TC source is able to drift relative to us (slave)
512 so we need to keep track of the drift and adjust
513 our speed to remain locked.
516 if (delta_accumulator_cnt >= delta_accumulator_size) {
517 have_first_delta_accumulator = true;
518 delta_accumulator_cnt = 0;
521 if (delta_accumulator_cnt != 0 || this_delta < _current_frame_rate) {
522 delta_accumulator[delta_accumulator_cnt++] = long(dir) * long(this_delta);
525 if (have_first_delta_accumulator) {
526 average_slave_delta = 0L;
527 for (int i = 0; i < delta_accumulator_size; ++i) {
528 average_slave_delta += delta_accumulator[i];
530 average_slave_delta /= long(delta_accumulator_size);
531 if (average_slave_delta < 0L) {
533 average_slave_delta = abs(average_slave_delta);
537 // cerr << "avgdelta = " << average_slave_delta*average_dir << endl;
541 if (slave_speed != 0.0f) {
543 /* slave is running */
545 switch (slave_state) {
547 if (_slave->requires_seekahead()) {
548 slave_wait_end = slave_transport_frame + _current_frame_rate;
549 locate (slave_wait_end, false, false);
550 slave_state = Waiting;
555 slave_state = Running;
557 Location* al = _locations.auto_loop_location();
559 if (al && play_loop && (slave_transport_frame < al->start() || slave_transport_frame > al->end())) {
561 request_play_loop(false);
564 if (slave_transport_frame != _transport_frame) {
565 locate (slave_transport_frame, false, false);
578 if (slave_state == Waiting) {
580 // cerr << "waiting at " << slave_transport_frame << endl;
581 if (slave_transport_frame >= slave_wait_end) {
582 // cerr << "\tstart at " << _transport_frame << endl;
584 slave_state = Running;
587 nframes_t frame_delta = slave_transport_frame - _transport_frame;
589 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
591 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
592 if (!(*i)->can_internal_playback_seek (frame_delta)) {
599 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
600 (*i)->internal_playback_seek (frame_delta);
602 _transport_frame += frame_delta;
605 // cerr << "cannot micro-seek\n";
609 memset (delta_accumulator, 0, sizeof (long) * delta_accumulator_size);
610 average_slave_delta = 0L;
615 if (slave_state == Running && _transport_speed == 0.0f) {
617 // cerr << "slave starts transport\n";
624 /* slave has stopped */
626 if (_transport_speed != 0.0f) {
628 // cerr << "slave stops transport: " << slave_speed << " frame: " << slave_transport_frame
629 // << " tf = " << _transport_frame
632 if (Config->get_slave_source() == JACK) {
633 last_stop_frame = _transport_frame;
639 if (slave_transport_frame != _transport_frame) {
640 // cerr << "slave stopped, move to " << slave_transport_frame << endl;
641 force_locate (slave_transport_frame, false);
644 slave_state = Stopped;
647 if (slave_state == Running && !_slave->is_always_synced() && !Config->get_timecode_source_is_synced()) {
650 if (_transport_speed != 0.0f) {
653 note that average_dir is +1 or -1
656 const float adjust_seconds = 1.0f;
659 //if (average_slave_delta == 0) {
663 // delta = average_slave_delta;
664 // delta *= average_dir;
667 float adjusted_speed = slave_speed +
668 (delta / (adjust_seconds * _current_frame_rate));
671 cerr << "adjust using " << delta
672 << " towards " << adjusted_speed
673 << " ratio = " << adjusted_speed / slave_speed
674 << " current = " << _transport_speed
675 << " slave @ " << slave_speed
678 request_transport_speed (adjusted_speed);
681 if (abs(average_slave_delta) > (long) _slave->resolution()) {
682 cerr << "average slave delta greater than slave resolution, going to silent motion\n";
689 if (!starting && !non_realtime_work_pending()) {
690 /* speed is set, we're locked, and good to go */
696 if (slave_speed && _transport_speed) {
698 /* something isn't right, but we should move with the master
704 prepare_diskstreams ();
705 silent_process_routes (nframes, offset);
706 commit_diskstreams (nframes, need_butler);
712 int32_t frames_moved = (int32_t) floor (_transport_speed * nframes);
714 if (frames_moved < 0) {
715 decrement_transport_position (-frames_moved);
717 increment_transport_position (frames_moved);
720 nframes_t stop_limit;
722 if (actively_recording()) {
723 stop_limit = max_frames;
725 if (Config->get_stop_at_session_end()) {
726 stop_limit = current_end_frame();
728 stop_limit = max_frames;
732 maybe_stop (stop_limit);
736 /* don't move at all */
737 cerr << "********* noroll" << endl;
738 no_roll (nframes, 0);
743 Session::process_without_events (nframes_t nframes)
745 bool session_needs_butler = false;
746 nframes_t stop_limit;
748 nframes_t offset = 0;
750 if (!process_can_proceed()) {
755 if (!_exporting && _slave) {
756 if (!follow_slave (nframes, 0)) {
761 if (_transport_speed == 0) {
762 no_roll (nframes, 0);
767 send_midi_time_code_for_cycle (nframes);
770 if (actively_recording()) {
771 stop_limit = max_frames;
773 if (Config->get_stop_at_session_end()) {
774 stop_limit = current_end_frame();
776 stop_limit = max_frames;
780 if (maybe_stop (stop_limit)) {
781 no_roll (nframes, 0);
785 if (maybe_sync_start (nframes, offset)) {
789 click (_transport_frame, nframes, offset);
791 prepare_diskstreams ();
793 frames_moved = (long) floor (_transport_speed * nframes);
795 if (process_routes (nframes, offset)) {
796 no_roll (nframes, offset);
800 commit_diskstreams (nframes, session_needs_butler);
802 if (frames_moved < 0) {
803 decrement_transport_position (-frames_moved);
805 increment_transport_position (frames_moved);
808 maybe_stop (stop_limit);
809 check_declick_out ();
811 if (session_needs_butler)
815 /** Process callback used when the auditioner is active.
816 * @param nframes number of frames to process.
819 Session::process_audition (nframes_t nframes)
822 boost::shared_ptr<RouteList> r = routes.reader ();
824 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
825 if (!(*i)->is_hidden()) {
826 (*i)->silence (nframes, 0);
830 /* run the auditioner, and if it says we need butler service, ask for it */
832 if (auditioner->play_audition (nframes) > 0) {
836 /* handle pending events */
838 while (pending_events.read (&ev, 1) == 1) {
842 /* if we are not in the middle of a state change,
843 and there are immediate events queued up,
847 while (!non_realtime_work_pending() && !immediate_events.empty()) {
848 Event *ev = immediate_events.front ();
849 immediate_events.pop_front ();
853 if (!auditioner->active()) {
854 /* auditioner no longer active, so go back to the normal process callback */
855 process_function = &Session::process_with_events;
860 Session::maybe_sync_start (nframes_t& nframes, nframes_t& offset)
862 nframes_t sync_offset;
864 if (!waiting_for_sync_offset) {
868 if (_engine.get_sync_offset (sync_offset) && sync_offset < nframes) {
870 /* generate silence up to the sync point, then
871 adjust nframes + offset to reflect whatever
875 no_roll (sync_offset, 0);
876 nframes -= sync_offset;
877 offset += sync_offset;
878 waiting_for_sync_offset = false;
881 return true; // done, nothing left to process
886 /* sync offset point is not within this process()
887 cycle, so just generate silence. and don't bother
888 with any fancy stuff here, just the minimal silence.
891 g_atomic_int_inc (&processing_prohibited);
892 no_roll (nframes, 0);
893 g_atomic_int_dec_and_test (&processing_prohibited);
895 if (Config->get_locate_while_waiting_for_sync()) {
896 if (micro_locate (nframes)) {
897 /* XXX ERROR !!! XXX */
901 return true; // done, nothing left to process