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 (processing_blocked()) {
66 if (non_realtime_work_pending()) {
67 if (!transport_work_requested ()) {
72 (this->*process_function) (nframes);
74 // the ticker is for sending time information like MidiClock
75 nframes_t transport_frames = transport_frame();
76 BBT_Time transport_bbt;
77 bbt_time(transport_frames, transport_bbt);
78 SMPTE::Time transport_smpte;
79 smpte_time(transport_frames, transport_smpte);
80 tick (transport_frames, transport_bbt, transport_smpte); /* EMIT SIGNAL */
82 SendFeedback (); /* EMIT SIGNAL */
84 MIDI::Manager::instance()->cycle_end();
88 Session::prepare_diskstreams ()
90 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
91 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
97 Session::fail_roll (nframes_t nframes)
99 return no_roll (nframes);
103 Session::no_roll (nframes_t nframes)
105 nframes_t end_frame = _transport_frame + nframes; // FIXME: varispeed + no_roll ??
107 bool declick = get_transport_declick_required();
108 boost::shared_ptr<RouteList> r = routes.reader ();
111 _click_io->silence (nframes);
114 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
116 if ((*i)->is_hidden()) {
120 (*i)->set_pending_declick (declick);
122 if ((*i)->no_roll (nframes, _transport_frame, end_frame, non_realtime_work_pending(),
123 actively_recording(), declick)) {
124 error << string_compose(_("Session: error in no roll for %1"), (*i)->name()) << endmsg;
134 Session::process_routes (nframes_t nframes)
137 int declick = get_transport_declick_required();
138 bool rec_monitors = get_rec_monitors_input();
139 boost::shared_ptr<RouteList> r = routes.reader ();
141 if (transport_sub_state & StopPendingCapture) {
142 /* force a declick out */
146 record_active = actively_recording(); // || (get_record_enabled() && get_punch_in());
148 const nframes_t start_frame = _transport_frame;
149 const nframes_t end_frame = _transport_frame + (nframes_t)floor(nframes * _transport_speed);
151 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
155 if ((*i)->is_hidden()) {
159 (*i)->set_pending_declick (declick);
161 if ((ret = (*i)->roll (nframes, start_frame, end_frame, declick, record_active, rec_monitors)) < 0) {
163 /* we have to do this here. Route::roll() for an AudioTrack will have called AudioDiskstream::process(),
164 and the DS will expect AudioDiskstream::commit() to be called. but we're aborting from that
165 call path, so make sure we release any outstanding locks here before we return failure.
168 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
169 for (DiskstreamList::iterator ids = dsl->begin(); ids != dsl->end(); ++ids) {
182 Session::silent_process_routes (nframes_t nframes)
184 bool record_active = actively_recording();
185 int declick = get_transport_declick_required();
186 bool rec_monitors = get_rec_monitors_input();
187 boost::shared_ptr<RouteList> r = routes.reader ();
189 if (transport_sub_state & StopPendingCapture) {
190 /* force a declick out */
194 const nframes_t start_frame = _transport_frame;
195 const nframes_t end_frame = _transport_frame + lrintf(nframes * _transport_speed);
197 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
201 if ((*i)->is_hidden()) {
205 if ((ret = (*i)->silent_roll (nframes, start_frame, end_frame, record_active, rec_monitors)) < 0) {
207 /* we have to do this here. Route::roll() for an AudioTrack will have called AudioDiskstream::process(),
208 and the DS will expect AudioDiskstream::commit() to be called. but we're aborting from that
209 call path, so make sure we release any outstanding locks here before we return failure.
212 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
213 for (DiskstreamList::iterator ids = dsl->begin(); ids != dsl->end(); ++ids) {
226 Session::commit_diskstreams (nframes_t nframes, bool &needs_butler)
232 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
233 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
235 if ((*i)->hidden()) {
239 /* force all diskstreams not handled by a Route to call do their stuff.
240 Note: the diskstreams that were handled by a route will just return zero
241 from this call, because they know they were processed. So in fact, this
242 also runs commit() for every diskstream.
245 if ((dret = (*i)->process (_transport_frame, nframes, actively_recording(), get_rec_monitors_input())) == 0) {
246 if ((*i)->commit (nframes)) {
250 } else if (dret < 0) {
254 pworst = min (pworst, (*i)->playback_buffer_load());
255 cworst = min (cworst, (*i)->capture_buffer_load());
258 uint32_t pmin = g_atomic_int_get (&_playback_load);
259 uint32_t pminold = g_atomic_int_get (&_playback_load_min);
260 uint32_t cmin = g_atomic_int_get (&_capture_load);
261 uint32_t cminold = g_atomic_int_get (&_capture_load_min);
263 g_atomic_int_set (&_playback_load, (uint32_t) floor (pworst * 100.0f));
264 g_atomic_int_set (&_capture_load, (uint32_t) floor (cworst * 100.0f));
265 g_atomic_int_set (&_playback_load_min, min (pmin, pminold));
266 g_atomic_int_set (&_capture_load_min, min (cmin, cminold));
268 if (actively_recording()) {
273 /** Process callback used when the auditioner is not active */
275 Session::process_with_events (nframes_t nframes)
278 nframes_t this_nframes;
280 bool session_needs_butler = false;
281 nframes_t stop_limit;
284 /* make sure the auditioner is silent */
287 auditioner->silence (nframes);
290 /* handle any pending events */
292 while (pending_events.read (&ev, 1) == 1) {
296 /* if we are not in the middle of a state change,
297 and there are immediate events queued up,
301 while (!non_realtime_work_pending() && !immediate_events.empty()) {
302 Event *ev = immediate_events.front ();
303 immediate_events.pop_front ();
307 /* Events caused a transport change, send an MTC Full Frame (SMPTE) message.
308 * This is sent whether rolling or not, to give slaves an idea of ardour time
309 * on locates (and allow slow slaves to position and prepare for rolling)
311 if (_send_smpte_update) {
312 send_full_time_code(nframes);
315 if (!process_can_proceed()) {
320 if (events.empty() || next_event == events.end()) {
321 process_without_events (nframes);
325 if (_transport_speed == 1.0) {
326 frames_moved = (long) nframes;
328 interpolation.set_target_speed (fabs(_target_transport_speed));
329 interpolation.set_speed (fabs(_transport_speed));
330 frames_moved = (long) interpolation.interpolate (0, nframes, 0, 0);
333 end_frame = _transport_frame + (nframes_t)frames_moved;
337 Events::iterator the_next_one;
339 if (!process_can_proceed()) {
344 if (!_exporting && _slave) {
345 if (!follow_slave (nframes)) {
350 if (_transport_speed == 0) {
356 send_midi_time_code_for_cycle (nframes);
359 if (actively_recording()) {
360 stop_limit = max_frames;
363 if (Config->get_stop_at_session_end()) {
364 stop_limit = current_end_frame();
366 stop_limit = max_frames;
370 if (maybe_stop (stop_limit)) {
375 this_event = *next_event;
376 the_next_one = next_event;
379 /* yes folks, here it is, the actual loop where we really truly
385 this_nframes = nframes; /* real (jack) time relative */
386 frames_moved = (long) floor (_transport_speed * nframes); /* transport relative */
388 /* running an event, position transport precisely to its time */
389 if (this_event && this_event->action_frame <= end_frame && this_event->action_frame >= _transport_frame) {
390 /* this isn't quite right for reverse play */
391 frames_moved = (long) (this_event->action_frame - _transport_frame);
392 this_nframes = (nframes_t) abs( floor(frames_moved / _transport_speed) );
397 click (_transport_frame, this_nframes);
399 /* now process frames between now and the first event in this block */
400 prepare_diskstreams ();
402 if (process_routes (this_nframes)) {
407 commit_diskstreams (this_nframes, session_needs_butler);
409 nframes -= this_nframes;
411 if (frames_moved < 0) {
412 decrement_transport_position (-frames_moved);
414 increment_transport_position (frames_moved);
417 maybe_stop (stop_limit);
418 check_declick_out ();
421 _engine.split_cycle (this_nframes);
423 /* now handle this event and all others scheduled for the same time */
425 while (this_event && this_event->action_frame == _transport_frame) {
426 process_event (this_event);
428 if (the_next_one == events.end()) {
431 this_event = *the_next_one;
436 /* if an event left our state changing, do the right thing */
438 if (nframes && non_realtime_work_pending()) {
443 /* this is necessary to handle the case of seamless looping */
444 end_frame = _transport_frame + (nframes_t) floor (nframes * _transport_speed);
450 } /* implicit release of route lock */
452 if (session_needs_butler)
457 Session::reset_slave_state ()
459 average_slave_delta = 1800;
460 delta_accumulator_cnt = 0;
461 have_first_delta_accumulator = false;
462 slave_state = Stopped;
466 Session::transport_locked () const
470 if (!locate_pending() && ((Config->get_slave_source() == None) || (sl && sl->ok() && sl->locked()))) {
478 Session::follow_slave (nframes_t nframes)
481 nframes_t slave_transport_frame;
482 nframes_t this_delta;
488 Config->set_slave_source (None);
492 _slave->speed_and_position (slave_speed, slave_transport_frame);
494 if (!_slave->locked()) {
498 if (slave_transport_frame > _transport_frame) {
499 this_delta = slave_transport_frame - _transport_frame;
502 this_delta = _transport_frame - slave_transport_frame;
506 if ((starting = _slave->starting())) {
510 if (_slave->is_always_synced() || config.get_timecode_source_is_synced()) {
512 /* if the TC source is synced, then we assume that its
513 speed is binary: 0.0 or 1.0
516 if (slave_speed != 0.0f) {
522 /* TC source is able to drift relative to us (slave)
523 so we need to keep track of the drift and adjust
524 our speed to remain locked.
527 calculate_moving_average_of_slave_delta(dir, this_delta);
530 track_slave_state(slave_speed, slave_transport_frame, this_delta, starting);
532 if (slave_state == Running && !_slave->is_always_synced() && !config.get_timecode_source_is_synced()) {
534 if (_transport_speed != 0.0f) {
537 note that average_dir is +1 or -1
542 #ifdef USE_MOVING_AVERAGE_OF_SLAVE
543 if (average_slave_delta == 0) {
547 delta = average_slave_delta;
548 delta *= average_dir;
555 float adjusted_speed = slave_speed + (delta / float(_current_frame_rate));
557 if (_slave->give_slave_full_control_over_transport_speed()) {
558 request_transport_speed(slave_speed);
560 request_transport_speed(adjusted_speed);
562 cerr << "adjust using " << delta
563 << " towards " << adjusted_speed
564 << " ratio = " << adjusted_speed / slave_speed
565 << " current = " << _transport_speed
566 << " slave @ " << slave_speed
571 if (abs(average_slave_delta) > (long) _slave->resolution()) {
572 cerr << "average slave delta greater than slave resolution, going to silent motion\n";
579 if (slave_speed != 0.0)
580 cerr << "delta = " << (int) (dir * this_delta)
581 << " speed = " << slave_speed
582 << " ts = " << _transport_speed
583 << " M@ "<< slave_transport_frame << " S@ " << _transport_frame
584 << " avgdelta = " << average_slave_delta
588 if (!starting && !non_realtime_work_pending()) {
589 /* speed is set, we're locked, and good to go */
595 cerr << "reached silent_motion:" <<endl;
598 follow_slave_silently (nframes, slave_speed);
601 /* don't move at all */
603 cerr << "reached no_roll:" <<endl;
610 Session::calculate_moving_average_of_slave_delta(int dir, nframes_t this_delta)
612 if (delta_accumulator_cnt >= delta_accumulator_size) {
613 have_first_delta_accumulator = true;
614 delta_accumulator_cnt = 0;
617 if (delta_accumulator_cnt != 0 || this_delta < _current_frame_rate) {
618 delta_accumulator[delta_accumulator_cnt++] = long(dir) * long(this_delta);
621 if (have_first_delta_accumulator) {
622 average_slave_delta = 0L;
623 for (int i = 0; i < delta_accumulator_size; ++i) {
624 average_slave_delta += delta_accumulator[i];
626 average_slave_delta /= long(delta_accumulator_size);
627 if (average_slave_delta < 0L) {
629 average_slave_delta = abs(average_slave_delta);
637 Session::track_slave_state(
639 nframes_t slave_transport_frame,
640 nframes_t this_delta,
643 if (slave_speed != 0.0f) {
645 /* slave is running */
647 switch (slave_state) {
649 if (_slave->requires_seekahead()) {
650 slave_wait_end = slave_transport_frame + _current_frame_rate;
651 locate (slave_wait_end, false, false);
652 slave_state = Waiting;
657 slave_state = Running;
659 Location* al = _locations.auto_loop_location();
661 if (al && play_loop && (slave_transport_frame < al->start() || slave_transport_frame > al->end())) {
663 request_play_loop(false);
666 if (slave_transport_frame != _transport_frame) {
667 locate (slave_transport_frame, false, false);
680 if (slave_state == Waiting) {
683 cerr << "waiting at " << slave_transport_frame << endl;
685 if (slave_transport_frame >= slave_wait_end) {
687 cerr << "\tstart at " << _transport_frame << endl;
689 slave_state = Running;
692 nframes_t frame_delta = slave_transport_frame - _transport_frame;
694 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
696 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
697 if (!(*i)->can_internal_playback_seek (frame_delta)) {
704 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
705 (*i)->internal_playback_seek (frame_delta);
707 _transport_frame += frame_delta;
710 cerr << "cannot micro-seek\n";
714 memset (delta_accumulator, 0, sizeof (long) * delta_accumulator_size);
715 average_slave_delta = 0L;
720 if (slave_state == Running && _transport_speed == 0.0f) {
723 cerr << "slave starts transport\n";
728 } else { // slave_speed is 0
730 /* slave has stopped */
732 if (_transport_speed != 0.0f) {
735 cerr << "slave stops transport: " << slave_speed << " frame: " << slave_transport_frame
736 << " tf = " << _transport_frame << endl;
742 if (slave_transport_frame != _transport_frame) {
744 cerr << "slave stopped, move to " << slave_transport_frame << endl;
746 force_locate (slave_transport_frame, false);
749 slave_state = Stopped;
754 Session::follow_slave_silently (nframes_t nframes, float slave_speed)
756 if (slave_speed && _transport_speed) {
758 /* something isn't right, but we should move with the master
764 prepare_diskstreams ();
765 silent_process_routes (nframes);
766 commit_diskstreams (nframes, need_butler);
772 int32_t frames_moved = (int32_t) floor (_transport_speed * nframes);
774 if (frames_moved < 0) {
775 decrement_transport_position (-frames_moved);
777 increment_transport_position (frames_moved);
780 nframes_t stop_limit;
782 if (actively_recording()) {
783 stop_limit = max_frames;
785 if (Config->get_stop_at_session_end()) {
786 stop_limit = current_end_frame();
788 stop_limit = max_frames;
792 maybe_stop (stop_limit);
797 Session::process_without_events (nframes_t nframes)
799 bool session_needs_butler = false;
800 nframes_t stop_limit;
803 if (!process_can_proceed()) {
808 if (!_exporting && _slave) {
809 if (!follow_slave (nframes)) {
814 if (_transport_speed == 0) {
820 send_midi_time_code_for_cycle (nframes);
823 if (actively_recording()) {
824 stop_limit = max_frames;
826 if (Config->get_stop_at_session_end()) {
827 stop_limit = current_end_frame();
829 stop_limit = max_frames;
833 if (maybe_stop (stop_limit)) {
838 if (maybe_sync_start (nframes)) {
842 click (_transport_frame, nframes);
844 prepare_diskstreams ();
846 if (_transport_speed == 1.0) {
847 frames_moved = (long) nframes;
849 interpolation.set_target_speed (fabs(_target_transport_speed));
850 interpolation.set_speed (fabs(_transport_speed));
851 frames_moved = (long) interpolation.interpolate (0, nframes, 0, 0);
854 if (process_routes (nframes)) {
859 commit_diskstreams (nframes, session_needs_butler);
861 if (frames_moved < 0) {
862 decrement_transport_position (-frames_moved);
864 increment_transport_position (frames_moved);
867 maybe_stop (stop_limit);
868 check_declick_out ();
870 if (session_needs_butler)
874 /** Process callback used when the auditioner is active.
875 * @param nframes number of frames to process.
878 Session::process_audition (nframes_t nframes)
881 boost::shared_ptr<RouteList> r = routes.reader ();
883 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
884 if (!(*i)->is_hidden()) {
885 (*i)->silence (nframes);
889 /* run the auditioner, and if it says we need butler service, ask for it */
891 if (auditioner->play_audition (nframes) > 0) {
895 /* handle pending events */
897 while (pending_events.read (&ev, 1) == 1) {
901 /* if we are not in the middle of a state change,
902 and there are immediate events queued up,
906 while (!non_realtime_work_pending() && !immediate_events.empty()) {
907 Event *ev = immediate_events.front ();
908 immediate_events.pop_front ();
912 if (!auditioner->active()) {
913 /* auditioner no longer active, so go back to the normal process callback */
914 process_function = &Session::process_with_events;
919 Session::maybe_sync_start (nframes_t& nframes)
921 nframes_t sync_offset;
923 if (!waiting_for_sync_offset) {
927 if (_engine.get_sync_offset (sync_offset) && sync_offset < nframes) {
929 /* generate silence up to the sync point, then
930 adjust nframes + offset to reflect whatever
934 no_roll (sync_offset);
935 nframes -= sync_offset;
936 Port::increment_port_offset (sync_offset);
937 waiting_for_sync_offset = false;
940 return true; // done, nothing left to process
945 /* sync offset point is not within this process()
946 cycle, so just generate silence. and don't bother
947 with any fancy stuff here, just the minimal silence.
952 if (Config->get_locate_while_waiting_for_sync()) {
953 if (micro_locate (nframes)) {
954 /* XXX ERROR !!! XXX */
958 return true; // done, nothing left to process