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/audio_diskstream.h"
31 #include "ardour/audioengine.h"
32 #include "ardour/auditioner.h"
33 #include "ardour/butler.h"
34 #include "ardour/cycle_timer.h"
35 #include "ardour/cycles.h"
36 #include "ardour/session.h"
37 #include "ardour/slave.h"
38 #include "ardour/timestamps.h"
40 #include "midi++/manager.h"
44 using namespace ARDOUR;
48 /** Called by the audio engine when there is work to be done with JACK.
49 * @param nframes Number of frames to process.
52 Session::process (nframes_t nframes)
54 // This is no more the appropriate place to call cycle
55 // start. cycle_start needs to be called at the Route::roll()
56 // where the signals which we want to mixdown have been calculated.
58 MIDI::Manager::instance()->cycle_start(nframes);
62 if (processing_blocked()) {
67 if (non_realtime_work_pending()) {
68 if (!_butler->transport_work_requested ()) {
73 (this->*process_function) (nframes);
75 // the ticker is for sending time information like MidiClock
76 nframes_t transport_frames = transport_frame();
77 BBT_Time transport_bbt;
78 bbt_time(transport_frames, transport_bbt);
79 SMPTE::Time transport_smpte;
80 smpte_time(transport_frames, transport_smpte);
81 tick (transport_frames, transport_bbt, transport_smpte); /* EMIT SIGNAL */
83 SendFeedback (); /* EMIT SIGNAL */
85 MIDI::Manager::instance()->cycle_end();
89 Session::prepare_diskstreams ()
91 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
92 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
98 Session::fail_roll (nframes_t nframes)
100 return no_roll (nframes);
104 Session::no_roll (nframes_t nframes)
106 nframes_t end_frame = _transport_frame + nframes; // FIXME: varispeed + no_roll ??
108 bool declick = get_transport_declick_required();
109 boost::shared_ptr<RouteList> r = routes.reader ();
112 _click_io->silence (nframes);
115 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
117 if ((*i)->is_hidden()) {
121 (*i)->set_pending_declick (declick);
123 if ((*i)->no_roll (nframes, _transport_frame, end_frame, non_realtime_work_pending(),
124 actively_recording(), declick)) {
125 error << string_compose(_("Session: error in no roll for %1"), (*i)->name()) << endmsg;
135 Session::process_routes (nframes_t nframes)
138 int declick = get_transport_declick_required();
139 bool rec_monitors = get_rec_monitors_input();
140 boost::shared_ptr<RouteList> r = routes.reader ();
142 if (transport_sub_state & StopPendingCapture) {
143 /* force a declick out */
147 record_active = actively_recording(); // || (get_record_enabled() && get_punch_in());
149 const nframes_t start_frame = _transport_frame;
150 const nframes_t end_frame = _transport_frame + (nframes_t)floor(nframes * _transport_speed);
152 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
156 if ((*i)->is_hidden()) {
160 (*i)->set_pending_declick (declick);
162 if ((ret = (*i)->roll (nframes, start_frame, end_frame, declick, record_active, rec_monitors)) < 0) {
164 /* we have to do this here. Route::roll() for an AudioTrack will have called AudioDiskstream::process(),
165 and the DS will expect AudioDiskstream::commit() to be called. but we're aborting from that
166 call path, so make sure we release any outstanding locks here before we return failure.
169 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
170 for (DiskstreamList::iterator ids = dsl->begin(); ids != dsl->end(); ++ids) {
183 Session::silent_process_routes (nframes_t nframes)
185 bool record_active = actively_recording();
186 int declick = get_transport_declick_required();
187 bool rec_monitors = get_rec_monitors_input();
188 boost::shared_ptr<RouteList> r = routes.reader ();
190 if (transport_sub_state & StopPendingCapture) {
191 /* force a declick out */
195 const nframes_t start_frame = _transport_frame;
196 const nframes_t end_frame = _transport_frame + lrintf(nframes * _transport_speed);
198 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
202 if ((*i)->is_hidden()) {
206 if ((ret = (*i)->silent_roll (nframes, start_frame, end_frame, record_active, rec_monitors)) < 0) {
208 /* we have to do this here. Route::roll() for an AudioTrack will have called AudioDiskstream::process(),
209 and the DS will expect AudioDiskstream::commit() to be called. but we're aborting from that
210 call path, so make sure we release any outstanding locks here before we return failure.
213 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
214 for (DiskstreamList::iterator ids = dsl->begin(); ids != dsl->end(); ++ids) {
227 Session::commit_diskstreams (nframes_t nframes, bool &needs_butler)
233 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
234 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
236 if ((*i)->hidden()) {
240 /* force all diskstreams not handled by a Route to call do their stuff.
241 Note: the diskstreams that were handled by a route will just return zero
242 from this call, because they know they were processed. So in fact, this
243 also runs commit() for every diskstream.
246 if ((dret = (*i)->process (_transport_frame, nframes, actively_recording(), get_rec_monitors_input())) == 0) {
247 if ((*i)->commit (nframes)) {
251 } else if (dret < 0) {
255 pworst = min (pworst, (*i)->playback_buffer_load());
256 cworst = min (cworst, (*i)->capture_buffer_load());
259 uint32_t pmin = g_atomic_int_get (&_playback_load);
260 uint32_t pminold = g_atomic_int_get (&_playback_load_min);
261 uint32_t cmin = g_atomic_int_get (&_capture_load);
262 uint32_t cminold = g_atomic_int_get (&_capture_load_min);
264 g_atomic_int_set (&_playback_load, (uint32_t) floor (pworst * 100.0f));
265 g_atomic_int_set (&_capture_load, (uint32_t) floor (cworst * 100.0f));
266 g_atomic_int_set (&_playback_load_min, min (pmin, pminold));
267 g_atomic_int_set (&_capture_load_min, min (cmin, cminold));
269 if (actively_recording()) {
274 /** Process callback used when the auditioner is not active */
276 Session::process_with_events (nframes_t nframes)
279 nframes_t this_nframes;
281 bool session_needs_butler = false;
282 nframes_t stop_limit;
285 /* make sure the auditioner is silent */
288 auditioner->silence (nframes);
291 /* handle any pending events */
293 while (pending_events.read (&ev, 1) == 1) {
297 /* if we are not in the middle of a state change,
298 and there are immediate events queued up,
302 while (!non_realtime_work_pending() && !immediate_events.empty()) {
303 Event *ev = immediate_events.front ();
304 immediate_events.pop_front ();
308 /* Events caused a transport change, send an MTC Full Frame (SMPTE) message.
309 * This is sent whether rolling or not, to give slaves an idea of ardour time
310 * on locates (and allow slow slaves to position and prepare for rolling)
312 if (_send_smpte_update) {
313 send_full_time_code(nframes);
316 if (!process_can_proceed()) {
321 if (events.empty() || next_event == events.end()) {
322 process_without_events (nframes);
326 if (_transport_speed == 1.0) {
327 frames_moved = (long) nframes;
329 interpolation.set_target_speed (fabs(_target_transport_speed));
330 interpolation.set_speed (fabs(_transport_speed));
331 frames_moved = (long) interpolation.interpolate (0, nframes, 0, 0);
334 end_frame = _transport_frame + (nframes_t)frames_moved;
338 Events::iterator the_next_one;
340 if (!process_can_proceed()) {
345 if (!_exporting && _slave) {
346 if (!follow_slave (nframes)) {
351 if (_transport_speed == 0) {
357 send_midi_time_code_for_cycle (nframes);
360 if (actively_recording()) {
361 stop_limit = max_frames;
364 if (Config->get_stop_at_session_end()) {
365 stop_limit = current_end_frame();
367 stop_limit = max_frames;
371 if (maybe_stop (stop_limit)) {
376 this_event = *next_event;
377 the_next_one = next_event;
380 /* yes folks, here it is, the actual loop where we really truly
386 this_nframes = nframes; /* real (jack) time relative */
387 frames_moved = (long) floor (_transport_speed * nframes); /* transport relative */
389 /* running an event, position transport precisely to its time */
390 if (this_event && this_event->action_frame <= end_frame && this_event->action_frame >= _transport_frame) {
391 /* this isn't quite right for reverse play */
392 frames_moved = (long) (this_event->action_frame - _transport_frame);
393 this_nframes = (nframes_t) abs( floor(frames_moved / _transport_speed) );
398 click (_transport_frame, this_nframes);
400 /* now process frames between now and the first event in this block */
401 prepare_diskstreams ();
403 if (process_routes (this_nframes)) {
408 commit_diskstreams (this_nframes, session_needs_butler);
410 nframes -= this_nframes;
412 if (frames_moved < 0) {
413 decrement_transport_position (-frames_moved);
415 increment_transport_position (frames_moved);
418 maybe_stop (stop_limit);
419 check_declick_out ();
422 _engine.split_cycle (this_nframes);
424 /* now handle this event and all others scheduled for the same time */
426 while (this_event && this_event->action_frame == _transport_frame) {
427 process_event (this_event);
429 if (the_next_one == events.end()) {
432 this_event = *the_next_one;
437 /* if an event left our state changing, do the right thing */
439 if (nframes && non_realtime_work_pending()) {
444 /* this is necessary to handle the case of seamless looping */
445 end_frame = _transport_frame + (nframes_t) floor (nframes * _transport_speed);
451 } /* implicit release of route lock */
453 if (session_needs_butler) {
459 Session::reset_slave_state ()
461 average_slave_delta = 1800;
462 delta_accumulator_cnt = 0;
463 have_first_delta_accumulator = false;
464 slave_state = Stopped;
468 Session::transport_locked () const
472 if (!locate_pending() && ((Config->get_slave_source() == None) || (sl && sl->ok() && sl->locked()))) {
480 Session::follow_slave (nframes_t nframes)
483 nframes_t slave_transport_frame;
484 nframes_t this_delta;
490 Config->set_slave_source (None);
494 _slave->speed_and_position (slave_speed, slave_transport_frame);
496 if (!_slave->locked()) {
500 if (slave_transport_frame > _transport_frame) {
501 this_delta = slave_transport_frame - _transport_frame;
504 this_delta = _transport_frame - slave_transport_frame;
508 if ((starting = _slave->starting())) {
512 if (_slave->is_always_synced() || config.get_timecode_source_is_synced()) {
514 /* if the TC source is synced, then we assume that its
515 speed is binary: 0.0 or 1.0
518 if (slave_speed != 0.0f) {
524 /* TC source is able to drift relative to us (slave)
525 so we need to keep track of the drift and adjust
526 our speed to remain locked.
529 calculate_moving_average_of_slave_delta(dir, this_delta);
532 track_slave_state(slave_speed, slave_transport_frame, this_delta, starting);
534 if (slave_state == Running && !_slave->is_always_synced() && !config.get_timecode_source_is_synced()) {
536 if (_transport_speed != 0.0f) {
539 note that average_dir is +1 or -1
544 #ifdef USE_MOVING_AVERAGE_OF_SLAVE
545 if (average_slave_delta == 0) {
549 delta = average_slave_delta;
550 delta *= average_dir;
557 float adjusted_speed = slave_speed + (delta / float(_current_frame_rate));
559 if (_slave->give_slave_full_control_over_transport_speed()) {
560 request_transport_speed(slave_speed);
562 request_transport_speed(adjusted_speed);
564 cerr << "adjust using " << delta
565 << " towards " << adjusted_speed
566 << " ratio = " << adjusted_speed / slave_speed
567 << " current = " << _transport_speed
568 << " slave @ " << slave_speed
573 if (abs(average_slave_delta) > (long) _slave->resolution()) {
574 cerr << "average slave delta greater than slave resolution, going to silent motion\n";
581 if (slave_speed != 0.0)
582 cerr << "delta = " << (int) (dir * this_delta)
583 << " speed = " << slave_speed
584 << " ts = " << _transport_speed
585 << " M@ "<< slave_transport_frame << " S@ " << _transport_frame
586 << " avgdelta = " << average_slave_delta
590 if (!starting && !non_realtime_work_pending()) {
591 /* speed is set, we're locked, and good to go */
597 cerr << "reached silent_motion:" <<endl;
600 follow_slave_silently (nframes, slave_speed);
603 /* don't move at all */
605 cerr << "reached no_roll:" <<endl;
612 Session::calculate_moving_average_of_slave_delta(int dir, nframes_t this_delta)
614 if (delta_accumulator_cnt >= delta_accumulator_size) {
615 have_first_delta_accumulator = true;
616 delta_accumulator_cnt = 0;
619 if (delta_accumulator_cnt != 0 || this_delta < _current_frame_rate) {
620 delta_accumulator[delta_accumulator_cnt++] = long(dir) * long(this_delta);
623 if (have_first_delta_accumulator) {
624 average_slave_delta = 0L;
625 for (int i = 0; i < delta_accumulator_size; ++i) {
626 average_slave_delta += delta_accumulator[i];
628 average_slave_delta /= long(delta_accumulator_size);
629 if (average_slave_delta < 0L) {
631 average_slave_delta = abs(average_slave_delta);
639 Session::track_slave_state(
641 nframes_t slave_transport_frame,
642 nframes_t this_delta,
645 if (slave_speed != 0.0f) {
647 /* slave is running */
649 switch (slave_state) {
651 if (_slave->requires_seekahead()) {
652 slave_wait_end = slave_transport_frame + _current_frame_rate;
653 locate (slave_wait_end, false, false);
654 slave_state = Waiting;
659 slave_state = Running;
661 Location* al = _locations.auto_loop_location();
663 if (al && play_loop && (slave_transport_frame < al->start() || slave_transport_frame > al->end())) {
665 request_play_loop(false);
668 if (slave_transport_frame != _transport_frame) {
669 locate (slave_transport_frame, false, false);
682 if (slave_state == Waiting) {
685 cerr << "waiting at " << slave_transport_frame << endl;
687 if (slave_transport_frame >= slave_wait_end) {
689 cerr << "\tstart at " << _transport_frame << endl;
691 slave_state = Running;
694 nframes_t frame_delta = slave_transport_frame - _transport_frame;
696 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
698 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
699 if (!(*i)->can_internal_playback_seek (frame_delta)) {
706 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
707 (*i)->internal_playback_seek (frame_delta);
709 _transport_frame += frame_delta;
712 cerr << "cannot micro-seek\n";
716 memset (delta_accumulator, 0, sizeof (long) * delta_accumulator_size);
717 average_slave_delta = 0L;
722 if (slave_state == Running && _transport_speed == 0.0f) {
725 cerr << "slave starts transport\n";
730 } else { // slave_speed is 0
732 /* slave has stopped */
734 if (_transport_speed != 0.0f) {
737 cerr << "slave stops transport: " << slave_speed << " frame: " << slave_transport_frame
738 << " tf = " << _transport_frame << endl;
744 if (slave_transport_frame != _transport_frame) {
746 cerr << "slave stopped, move to " << slave_transport_frame << endl;
748 force_locate (slave_transport_frame, false);
751 slave_state = Stopped;
756 Session::follow_slave_silently (nframes_t nframes, float slave_speed)
758 if (slave_speed && _transport_speed) {
760 /* something isn't right, but we should move with the master
766 prepare_diskstreams ();
767 silent_process_routes (nframes);
768 commit_diskstreams (nframes, need_butler);
774 int32_t frames_moved = (int32_t) floor (_transport_speed * nframes);
776 if (frames_moved < 0) {
777 decrement_transport_position (-frames_moved);
779 increment_transport_position (frames_moved);
782 nframes_t stop_limit;
784 if (actively_recording()) {
785 stop_limit = max_frames;
787 if (Config->get_stop_at_session_end()) {
788 stop_limit = current_end_frame();
790 stop_limit = max_frames;
794 maybe_stop (stop_limit);
799 Session::process_without_events (nframes_t nframes)
801 bool session_needs_butler = false;
802 nframes_t stop_limit;
805 if (!process_can_proceed()) {
810 if (!_exporting && _slave) {
811 if (!follow_slave (nframes)) {
816 if (_transport_speed == 0) {
822 send_midi_time_code_for_cycle (nframes);
825 if (actively_recording()) {
826 stop_limit = max_frames;
828 if (Config->get_stop_at_session_end()) {
829 stop_limit = current_end_frame();
831 stop_limit = max_frames;
835 if (maybe_stop (stop_limit)) {
840 if (maybe_sync_start (nframes)) {
844 click (_transport_frame, nframes);
846 prepare_diskstreams ();
848 if (_transport_speed == 1.0) {
849 frames_moved = (long) nframes;
851 interpolation.set_target_speed (fabs(_target_transport_speed));
852 interpolation.set_speed (fabs(_transport_speed));
853 frames_moved = (long) interpolation.interpolate (0, nframes, 0, 0);
856 if (process_routes (nframes)) {
861 commit_diskstreams (nframes, session_needs_butler);
863 if (frames_moved < 0) {
864 decrement_transport_position (-frames_moved);
866 increment_transport_position (frames_moved);
869 maybe_stop (stop_limit);
870 check_declick_out ();
872 if (session_needs_butler) {
877 /** Process callback used when the auditioner is active.
878 * @param nframes number of frames to process.
881 Session::process_audition (nframes_t nframes)
884 boost::shared_ptr<RouteList> r = routes.reader ();
886 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
887 if (!(*i)->is_hidden()) {
888 (*i)->silence (nframes);
892 /* run the auditioner, and if it says we need butler service, ask for it */
894 if (auditioner->play_audition (nframes) > 0) {
898 /* handle pending events */
900 while (pending_events.read (&ev, 1) == 1) {
904 /* if we are not in the middle of a state change,
905 and there are immediate events queued up,
909 while (!non_realtime_work_pending() && !immediate_events.empty()) {
910 Event *ev = immediate_events.front ();
911 immediate_events.pop_front ();
915 if (!auditioner->active()) {
916 /* auditioner no longer active, so go back to the normal process callback */
917 process_function = &Session::process_with_events;
922 Session::maybe_sync_start (nframes_t& nframes)
924 nframes_t sync_offset;
926 if (!waiting_for_sync_offset) {
930 if (_engine.get_sync_offset (sync_offset) && sync_offset < nframes) {
932 /* generate silence up to the sync point, then
933 adjust nframes + offset to reflect whatever
937 no_roll (sync_offset);
938 nframes -= sync_offset;
939 Port::increment_port_offset (sync_offset);
940 waiting_for_sync_offset = false;
943 return true; // done, nothing left to process
948 /* sync offset point is not within this process()
949 cycle, so just generate silence. and don't bother
950 with any fancy stuff here, just the minimal silence.
955 if (Config->get_locate_while_waiting_for_sync()) {
956 if (micro_locate (nframes)) {
957 /* XXX ERROR !!! XXX */
961 return true; // done, nothing left to process