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 appropriate 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 // the ticker is for sending time information like MidiClock
70 nframes_t transport_frames = transport_frame();
71 BBT_Time transport_bbt;
72 bbt_time(transport_frames, transport_bbt);
73 SMPTE::Time transport_smpte;
74 smpte_time(transport_frames, transport_smpte);
75 tick (transport_frames, transport_bbt, transport_smpte); /* EMIT SIGNAL */
77 SendFeedback (); /* EMIT SIGNAL */
79 MIDI::Manager::instance()->cycle_end();
83 Session::prepare_diskstreams ()
85 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
86 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
92 Session::no_roll (nframes_t nframes, nframes_t offset)
94 nframes_t end_frame = _transport_frame + nframes; // FIXME: varispeed + no_roll ??
96 bool declick = get_transport_declick_required();
97 boost::shared_ptr<RouteList> r = routes.reader ();
100 _click_io->silence (nframes, offset);
103 if (g_atomic_int_get (&processing_prohibited)) {
104 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
105 (*i)->silence (nframes, offset);
110 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
112 if ((*i)->is_hidden()) {
116 (*i)->set_pending_declick (declick);
118 if ((*i)->no_roll (nframes, _transport_frame, end_frame, offset, non_realtime_work_pending(),
119 actively_recording(), declick)) {
120 error << string_compose(_("Session: error in no roll for %1"), (*i)->name()) << endmsg;
130 Session::process_routes (nframes_t nframes, nframes_t offset)
133 int declick = get_transport_declick_required();
134 bool rec_monitors = get_rec_monitors_input();
135 boost::shared_ptr<RouteList> r = routes.reader ();
137 if (transport_sub_state & StopPendingCapture) {
138 /* force a declick out */
142 record_active = actively_recording(); // || (get_record_enabled() && get_punch_in());
144 const nframes_t start_frame = _transport_frame;
145 const nframes_t end_frame = _transport_frame + (nframes_t)floor(nframes * _transport_speed);
147 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
151 if ((*i)->is_hidden()) {
155 (*i)->set_pending_declick (declick);
157 if ((ret = (*i)->roll (nframes, start_frame, end_frame, offset, declick, record_active, rec_monitors)) < 0) {
159 /* we have to do this here. Route::roll() for an AudioTrack will have called AudioDiskstream::process(),
160 and the DS will expect AudioDiskstream::commit() to be called. but we're aborting from that
161 call path, so make sure we release any outstanding locks here before we return failure.
164 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
165 for (DiskstreamList::iterator ids = dsl->begin(); ids != dsl->end(); ++ids) {
178 Session::silent_process_routes (nframes_t nframes, nframes_t offset)
180 bool record_active = actively_recording();
181 int declick = get_transport_declick_required();
182 bool rec_monitors = get_rec_monitors_input();
183 boost::shared_ptr<RouteList> r = routes.reader ();
185 if (transport_sub_state & StopPendingCapture) {
186 /* force a declick out */
190 const nframes_t start_frame = _transport_frame;
191 const nframes_t end_frame = _transport_frame + lrintf(nframes * _transport_speed);
193 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
197 if ((*i)->is_hidden()) {
201 if ((ret = (*i)->silent_roll (nframes, start_frame, end_frame, offset, record_active, rec_monitors)) < 0) {
203 /* we have to do this here. Route::roll() for an AudioTrack will have called AudioDiskstream::process(),
204 and the DS will expect AudioDiskstream::commit() to be called. but we're aborting from that
205 call path, so make sure we release any outstanding locks here before we return failure.
208 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
209 for (DiskstreamList::iterator ids = dsl->begin(); ids != dsl->end(); ++ids) {
222 Session::commit_diskstreams (nframes_t nframes, bool &needs_butler)
228 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
229 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
231 if ((*i)->hidden()) {
235 /* force all diskstreams not handled by a Route to call do their stuff.
236 Note: the diskstreams that were handled by a route will just return zero
237 from this call, because they know they were processed. So in fact, this
238 also runs commit() for every diskstream.
241 if ((dret = (*i)->process (_transport_frame, nframes, 0, actively_recording(), get_rec_monitors_input())) == 0) {
242 if ((*i)->commit (nframes)) {
246 } else if (dret < 0) {
250 pworst = min (pworst, (*i)->playback_buffer_load());
251 cworst = min (cworst, (*i)->capture_buffer_load());
254 uint32_t pmin = g_atomic_int_get (&_playback_load);
255 uint32_t pminold = g_atomic_int_get (&_playback_load_min);
256 uint32_t cmin = g_atomic_int_get (&_capture_load);
257 uint32_t cminold = g_atomic_int_get (&_capture_load_min);
259 g_atomic_int_set (&_playback_load, (uint32_t) floor (pworst * 100.0f));
260 g_atomic_int_set (&_capture_load, (uint32_t) floor (cworst * 100.0f));
261 g_atomic_int_set (&_playback_load_min, min (pmin, pminold));
262 g_atomic_int_set (&_capture_load_min, min (cmin, cminold));
264 if (actively_recording()) {
269 /** Process callback used when the auditioner is not active */
271 Session::process_with_events (nframes_t nframes)
274 nframes_t this_nframes;
277 bool session_needs_butler = false;
278 nframes_t stop_limit;
281 /* make sure the auditioner is silent */
284 auditioner->silence (nframes, 0);
287 /* handle any pending events */
289 while (pending_events.read (&ev, 1) == 1) {
293 /* if we are not in the middle of a state change,
294 and there are immediate events queued up,
298 while (!non_realtime_work_pending() && !immediate_events.empty()) {
299 Event *ev = immediate_events.front ();
300 immediate_events.pop_front ();
304 /* Events caused a transport change, send an MTC Full Frame (SMPTE) message.
305 * This is sent whether rolling or not, to give slaves an idea of ardour time
306 * on locates (and allow slow slaves to position and prepare for rolling)
308 if (_send_smpte_update) {
309 send_full_time_code(nframes);
312 if (!process_can_proceed()) {
317 if (events.empty() || next_event == events.end()) {
318 process_without_events (nframes);
322 end_frame = _transport_frame + (nframes_t)abs(floor(nframes * _transport_speed));
326 Events::iterator the_next_one;
328 if (!process_can_proceed()) {
333 if (!_exporting && _slave) {
334 if (!follow_slave (nframes, 0)) {
339 if (_transport_speed == 0) {
340 no_roll (nframes, 0);
345 send_midi_time_code_for_cycle (nframes);
348 if (actively_recording()) {
349 stop_limit = max_frames;
352 if (Config->get_stop_at_session_end()) {
353 stop_limit = current_end_frame();
355 stop_limit = max_frames;
359 if (maybe_stop (stop_limit)) {
360 no_roll (nframes, 0);
364 this_event = *next_event;
365 the_next_one = next_event;
370 /* yes folks, here it is, the actual loop where we really truly
371 process some audio */
374 this_nframes = nframes; /* real (jack) time relative */
375 frames_moved = (long) floor (_transport_speed * nframes); /* transport relative */
377 /* running an event, position transport precisely to its time */
378 if (this_event && this_event->action_frame <= end_frame && this_event->action_frame >= _transport_frame) {
379 /* this isn't quite right for reverse play */
380 frames_moved = (long) (this_event->action_frame - _transport_frame);
381 this_nframes = (nframes_t) abs( floor(frames_moved / _transport_speed) );
386 click (_transport_frame, nframes, offset);
388 /* now process frames between now and the first event in this block */
389 prepare_diskstreams ();
391 if (process_routes (this_nframes, offset)) {
392 no_roll (nframes, 0);
396 commit_diskstreams (this_nframes, session_needs_butler);
398 nframes -= this_nframes;
399 offset += this_nframes;
401 if (frames_moved < 0) {
402 decrement_transport_position (-frames_moved);
404 increment_transport_position (frames_moved);
407 maybe_stop (stop_limit);
408 check_declick_out ();
411 /* now handle this event and all others scheduled for the same time */
413 while (this_event && this_event->action_frame == _transport_frame) {
414 process_event (this_event);
416 if (the_next_one == events.end()) {
419 this_event = *the_next_one;
424 /* if an event left our state changing, do the right thing */
426 if (non_realtime_work_pending()) {
427 no_roll (nframes, offset);
431 /* this is necessary to handle the case of seamless looping */
432 end_frame = _transport_frame + (nframes_t) floor (nframes * _transport_speed);
438 } /* implicit release of route lock */
440 if (session_needs_butler)
445 Session::reset_slave_state ()
447 average_slave_delta = 1800;
448 delta_accumulator_cnt = 0;
449 have_first_delta_accumulator = false;
450 slave_state = Stopped;
454 Session::transport_locked () const
458 if (!locate_pending() && ((Config->get_slave_source() == None) || (sl && sl->ok() && sl->locked()))) {
466 Session::follow_slave (nframes_t nframes, nframes_t offset)
469 nframes_t slave_transport_frame;
470 nframes_t this_delta;
476 Config->set_slave_source (None);
480 _slave->speed_and_position (slave_speed, slave_transport_frame);
482 if (!_slave->locked()) {
486 if (slave_transport_frame > _transport_frame) {
487 this_delta = slave_transport_frame - _transport_frame;
490 this_delta = _transport_frame - slave_transport_frame;
494 if ((starting = _slave->starting())) {
498 if (_slave->is_always_synced() || Config->get_timecode_source_is_synced()) {
500 /* if the TC source is synced, then we assume that its
501 speed is binary: 0.0 or 1.0
504 if (slave_speed != 0.0f) {
510 /* TC source is able to drift relative to us (slave)
511 so we need to keep track of the drift and adjust
512 our speed to remain locked.
515 calculate_moving_average_of_slave_delta(dir, this_delta);
518 track_slave_state(slave_speed, slave_transport_frame, this_delta, starting);
520 if (slave_state == Running && !_slave->is_always_synced() && !Config->get_timecode_source_is_synced()) {
522 if (_transport_speed != 0.0f) {
525 note that average_dir is +1 or -1
530 #ifdef USE_MOVING_AVERAGE_OF_SLAVE
531 if (average_slave_delta == 0) {
535 delta = average_slave_delta;
536 delta *= average_dir;
543 float adjusted_speed = slave_speed + (delta / float(_current_frame_rate));
545 if (_slave->give_slave_full_control_over_transport_speed()) {
546 request_transport_speed(slave_speed);
548 request_transport_speed(adjusted_speed);
550 cerr << "adjust using " << delta
551 << " towards " << adjusted_speed
552 << " ratio = " << adjusted_speed / slave_speed
553 << " current = " << _transport_speed
554 << " slave @ " << slave_speed
559 if (abs(average_slave_delta) > (long) _slave->resolution()) {
560 cerr << "average slave delta greater than slave resolution, going to silent motion\n";
567 if (slave_speed != 0.0)
568 cerr << "delta = " << (int) (dir * this_delta)
569 << " speed = " << slave_speed
570 << " ts = " << _transport_speed
571 << " M@ "<< slave_transport_frame << " S@ " << _transport_frame
572 << " avgdelta = " << average_slave_delta
576 if (!starting && !non_realtime_work_pending()) {
577 /* speed is set, we're locked, and good to go */
583 cerr << "reached silent_motion:" <<endl;
586 follow_slave_silently(nframes, offset, slave_speed);
589 /* don't move at all */
591 cerr << "reached no_roll:" <<endl;
593 no_roll (nframes, 0);
598 Session::calculate_moving_average_of_slave_delta(int dir, nframes_t this_delta)
600 if (delta_accumulator_cnt >= delta_accumulator_size) {
601 have_first_delta_accumulator = true;
602 delta_accumulator_cnt = 0;
605 if (delta_accumulator_cnt != 0 || this_delta < _current_frame_rate) {
606 delta_accumulator[delta_accumulator_cnt++] = long(dir) * long(this_delta);
609 if (have_first_delta_accumulator) {
610 average_slave_delta = 0L;
611 for (int i = 0; i < delta_accumulator_size; ++i) {
612 average_slave_delta += delta_accumulator[i];
614 average_slave_delta /= long(delta_accumulator_size);
615 if (average_slave_delta < 0L) {
617 average_slave_delta = abs(average_slave_delta);
625 Session::track_slave_state(
627 nframes_t slave_transport_frame,
628 nframes_t this_delta,
631 if (slave_speed != 0.0f) {
633 /* slave is running */
635 switch (slave_state) {
637 if (_slave->requires_seekahead()) {
638 slave_wait_end = slave_transport_frame + _current_frame_rate;
639 locate (slave_wait_end, false, false);
640 slave_state = Waiting;
645 slave_state = Running;
647 Location* al = _locations.auto_loop_location();
649 if (al && play_loop && (slave_transport_frame < al->start() || slave_transport_frame > al->end())) {
651 request_play_loop(false);
654 if (slave_transport_frame != _transport_frame) {
655 locate (slave_transport_frame, false, false);
668 if (slave_state == Waiting) {
671 cerr << "waiting at " << slave_transport_frame << endl;
673 if (slave_transport_frame >= slave_wait_end) {
675 cerr << "\tstart at " << _transport_frame << endl;
677 slave_state = Running;
680 nframes_t frame_delta = slave_transport_frame - _transport_frame;
682 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
684 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
685 if (!(*i)->can_internal_playback_seek (frame_delta)) {
692 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
693 (*i)->internal_playback_seek (frame_delta);
695 _transport_frame += frame_delta;
698 cerr << "cannot micro-seek\n";
702 memset (delta_accumulator, 0, sizeof (long) * delta_accumulator_size);
703 average_slave_delta = 0L;
708 if (slave_state == Running && _transport_speed == 0.0f) {
711 cerr << "slave starts transport\n";
716 } else { // slave_speed is 0
718 /* slave has stopped */
720 if (_transport_speed != 0.0f) {
723 cerr << "slave stops transport: " << slave_speed << " frame: " << slave_transport_frame
724 << " tf = " << _transport_frame << endl;
727 if (Config->get_slave_source() == JACK) {
728 last_stop_frame = _transport_frame;
734 if (slave_transport_frame != _transport_frame) {
736 cerr << "slave stopped, move to " << slave_transport_frame << endl;
738 force_locate (slave_transport_frame, false);
741 slave_state = Stopped;
746 Session::follow_slave_silently(nframes_t nframes, nframes_t offset, float slave_speed)
748 if (slave_speed && _transport_speed) {
750 /* something isn't right, but we should move with the master
756 prepare_diskstreams ();
757 silent_process_routes (nframes, offset);
758 commit_diskstreams (nframes, need_butler);
764 int32_t frames_moved = (int32_t) floor (_transport_speed * nframes);
766 if (frames_moved < 0) {
767 decrement_transport_position (-frames_moved);
769 increment_transport_position (frames_moved);
772 nframes_t stop_limit;
774 if (actively_recording()) {
775 stop_limit = max_frames;
777 if (Config->get_stop_at_session_end()) {
778 stop_limit = current_end_frame();
780 stop_limit = max_frames;
784 maybe_stop (stop_limit);
789 Session::process_without_events (nframes_t nframes)
791 bool session_needs_butler = false;
792 nframes_t stop_limit;
794 nframes_t offset = 0;
796 if (!process_can_proceed()) {
801 if (!_exporting && _slave) {
802 if (!follow_slave (nframes, 0)) {
807 if (_transport_speed == 0) {
808 no_roll (nframes, 0);
813 send_midi_time_code_for_cycle (nframes);
816 if (actively_recording()) {
817 stop_limit = max_frames;
819 if (Config->get_stop_at_session_end()) {
820 stop_limit = current_end_frame();
822 stop_limit = max_frames;
826 if (maybe_stop (stop_limit)) {
827 no_roll (nframes, 0);
831 if (maybe_sync_start (nframes, offset)) {
835 click (_transport_frame, nframes, offset);
837 prepare_diskstreams ();
839 frames_moved = (long) floor (_transport_speed * nframes);
841 if (process_routes (nframes, offset)) {
842 no_roll (nframes, offset);
846 commit_diskstreams (nframes, session_needs_butler);
848 if (frames_moved < 0) {
849 decrement_transport_position (-frames_moved);
851 increment_transport_position (frames_moved);
854 maybe_stop (stop_limit);
855 check_declick_out ();
857 if (session_needs_butler)
861 /** Process callback used when the auditioner is active.
862 * @param nframes number of frames to process.
865 Session::process_audition (nframes_t nframes)
868 boost::shared_ptr<RouteList> r = routes.reader ();
870 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
871 if (!(*i)->is_hidden()) {
872 (*i)->silence (nframes, 0);
876 /* run the auditioner, and if it says we need butler service, ask for it */
878 if (auditioner->play_audition (nframes) > 0) {
882 /* handle pending events */
884 while (pending_events.read (&ev, 1) == 1) {
888 /* if we are not in the middle of a state change,
889 and there are immediate events queued up,
893 while (!non_realtime_work_pending() && !immediate_events.empty()) {
894 Event *ev = immediate_events.front ();
895 immediate_events.pop_front ();
899 if (!auditioner->active()) {
900 /* auditioner no longer active, so go back to the normal process callback */
901 process_function = &Session::process_with_events;
906 Session::maybe_sync_start (nframes_t& nframes, nframes_t& offset)
908 nframes_t sync_offset;
910 if (!waiting_for_sync_offset) {
914 if (_engine.get_sync_offset (sync_offset) && sync_offset < nframes) {
916 /* generate silence up to the sync point, then
917 adjust nframes + offset to reflect whatever
921 no_roll (sync_offset, 0);
922 nframes -= sync_offset;
923 offset += sync_offset;
924 waiting_for_sync_offset = false;
927 return true; // done, nothing left to process
932 /* sync offset point is not within this process()
933 cycle, so just generate silence. and don't bother
934 with any fancy stuff here, just the minimal silence.
937 g_atomic_int_inc (&processing_prohibited);
938 no_roll (nframes, 0);
939 g_atomic_int_dec_and_test (&processing_prohibited);
941 if (Config->get_locate_while_waiting_for_sync()) {
942 if (micro_locate (nframes)) {
943 /* XXX ERROR !!! XXX */
947 return true; // done, nothing left to process