show event state for debugging of NPAE\n
[ardour2.git] / libs / ardour / session_process.cc
blob4a8acb4ddad5213e41511c187cb8de76967d9cf4
1 /*
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.
20 #include <cmath>
21 #include <cerrno>
22 #include <algorithm>
23 #include <unistd.h>
25 #include <pbd/error.h>
26 #include <pbd/enumwriter.h>
28 #include <glibmm/thread.h>
30 #include <ardour/ardour.h>
31 #include <ardour/session.h>
32 #include <ardour/timestamps.h>
33 #include <ardour/audio_diskstream.h>
34 #include <ardour/audioengine.h>
35 #include <ardour/slave.h>
36 #include <ardour/auditioner.h>
37 #include <ardour/cycles.h>
38 #include <ardour/cycle_timer.h>
40 #include "i18n.h"
42 using namespace ARDOUR;
43 using namespace PBD;
44 using namespace std;
46 void
47 Session::process (nframes_t nframes)
49 _silent = false;
51 if (processing_blocked()) {
52 _silent = true;
53 return;
56 if (non_realtime_work_pending()) {
57 if (!transport_work_requested ()) {
58 post_transport ();
62 (this->*process_function) (nframes);
65 Glib::Mutex::Lock lm (midi_lock, Glib::TRY_LOCK);
66 SendFeedback (); /* EMIT SIGNAL */
70 void
71 Session::prepare_diskstreams ()
73 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
74 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
75 (*i)->prepare ();
79 int
80 Session::fail_roll (nframes_t nframes)
82 Port::set_port_offset (0);
83 return no_roll (nframes);
87 int
88 Session::no_roll (nframes_t nframes)
90 nframes_t end_frame = _transport_frame + nframes;
91 int ret = 0;
92 bool declick = get_transport_declick_required();
93 boost::shared_ptr<RouteList> r = routes.reader ();
95 if (_click_io) {
96 _click_io->silence (nframes);
99 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
101 if ((*i)->hidden()) {
102 continue;
105 (*i)->set_pending_declick (declick);
107 if ((*i)->no_roll (nframes, _transport_frame, end_frame, non_realtime_work_pending(),
108 actively_recording(), declick)) {
109 error << string_compose(_("Session: error in no roll for %1"), (*i)->name()) << endmsg;
110 ret = -1;
111 break;
115 return ret;
119 Session::process_routes (nframes_t nframes)
121 bool record_active;
122 int declick = get_transport_declick_required();
123 bool rec_monitors = get_rec_monitors_input();
124 boost::shared_ptr<RouteList> r = routes.reader ();
126 if (transport_sub_state & StopPendingCapture) {
127 /* force a declick out */
128 declick = -1;
131 record_active = actively_recording(); // || (get_record_enabled() && get_punch_in());
133 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
135 int ret;
137 if ((*i)->hidden()) {
138 continue;
141 (*i)->set_pending_declick (declick);
143 if ((ret = (*i)->roll (nframes, _transport_frame, _transport_frame + nframes, declick, record_active, rec_monitors)) < 0) {
145 /* we have to do this here. Route::roll() for an AudioTrack will have called AudioDiskstream::process(),
146 and the DS will expect AudioDiskstream::commit() to be called. but we're aborting from that
147 call path, so make sure we release any outstanding locks here before we return failure.
150 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
151 for (DiskstreamList::iterator ids = dsl->begin(); ids != dsl->end(); ++ids) {
152 (*ids)->recover ();
155 stop_transport ();
156 return -1;
160 return 0;
164 Session::silent_process_routes (nframes_t nframes)
166 bool record_active = actively_recording();
167 int declick = get_transport_declick_required();
168 bool rec_monitors = get_rec_monitors_input();
169 boost::shared_ptr<RouteList> r = routes.reader ();
171 if (transport_sub_state & StopPendingCapture) {
172 /* force a declick out */
173 declick = -1;
176 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
178 int ret;
180 if ((*i)->hidden()) {
181 continue;
184 if ((ret = (*i)->silent_roll (nframes, _transport_frame, _transport_frame + nframes, record_active, rec_monitors)) < 0) {
186 /* we have to do this here. Route::roll() for an AudioTrack will have called AudioDiskstream::process(),
187 and the DS will expect AudioDiskstream::commit() to be called. but we're aborting from that
188 call path, so make sure we release any outstanding locks here before we return failure.
191 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
192 for (DiskstreamList::iterator ids = dsl->begin(); ids != dsl->end(); ++ids) {
193 (*ids)->recover ();
196 stop_transport ();
197 return -1;
201 return 0;
204 void
205 Session::commit_diskstreams (nframes_t nframes, bool &needs_butler)
207 int dret;
208 float pworst = 1.0f;
209 float cworst = 1.0f;
211 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
212 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
214 if ((*i)->hidden()) {
215 continue;
218 /* force all diskstreams not handled by a Route to call do their stuff.
219 Note: the diskstreams that were handled by a route will just return zero
220 from this call, because they know they were processed. So in fact, this
221 also runs commit() for every diskstream.
224 if ((dret = (*i)->process (_transport_frame, nframes, actively_recording(), get_rec_monitors_input())) == 0) {
225 if ((*i)->commit (nframes)) {
226 needs_butler = true;
229 } else if (dret < 0) {
230 (*i)->recover();
233 pworst = min (pworst, (*i)->playback_buffer_load());
234 cworst = min (cworst, (*i)->capture_buffer_load());
237 uint32_t pmin = g_atomic_int_get (&_playback_load);
238 uint32_t pminold = g_atomic_int_get (&_playback_load_min);
239 uint32_t cmin = g_atomic_int_get (&_capture_load);
240 uint32_t cminold = g_atomic_int_get (&_capture_load_min);
242 g_atomic_int_set (&_playback_load, (uint32_t) floor (pworst * 100.0f));
243 g_atomic_int_set (&_capture_load, (uint32_t) floor (cworst * 100.0f));
244 g_atomic_int_set (&_playback_load_min, min (pmin, pminold));
245 g_atomic_int_set (&_capture_load_min, min (cmin, cminold));
247 if (actively_recording()) {
248 set_dirty();
252 void
253 Session::process_with_events (nframes_t nframes)
255 Event* ev;
256 nframes_t this_nframes;
257 nframes_t end_frame;
259 bool session_needs_butler = false;
260 nframes_t stop_limit;
261 long frames_moved;
263 cerr << "++PWE\n";
265 /* make sure the auditioner is silent */
267 if (auditioner) {
268 auditioner->silence (nframes);
271 /* handle any pending events */
273 while (pending_events.read (&ev, 1) == 1) {
274 cerr << "********** merge event action " << ev->action << " type " << enum_2_string (ev->type) << " to pending\n";
275 merge_event (ev);
278 /* if we are not in the middle of a state change,
279 and there are immediate events queued up,
280 process them.
283 while (!non_realtime_work_pending() && !immediate_events.empty()) {
284 Event *ev = immediate_events.front ();
285 immediate_events.pop_front ();
286 cerr << "******* process immediate effect event type " << ev->action << " type " << enum_2_string (ev->type) << endl;
287 process_event (ev);
290 dump_events ();
292 /* Events caused a transport change, send an MTC Full Frame (SMPTE) message.
293 * This is sent whether rolling or not, to give slaves an idea of ardour time
294 * on locates (and allow slow slaves to position and prepare for rolling)
296 if (_send_smpte_update) {
297 send_full_time_code ();
300 if (!process_can_proceed()) {
301 _silent = true;
302 cerr << "++PWE out 1\n";
303 return;
306 if (events.empty() || next_event == events.end()) {
307 process_without_events (nframes);
308 cerr << "++PWE out 2\n";
309 return;
312 end_frame = _transport_frame + (nframes_t)abs(floor(nframes * _transport_speed));
315 Event* this_event;
316 Events::iterator the_next_one;
318 if (!process_can_proceed()) {
319 _silent = true;
320 return;
323 if (!_exporting && _slave) {
324 if (!follow_slave (nframes)) {
325 return;
329 if (_transport_speed == 0) {
330 no_roll (nframes);
331 cerr << "++PWE out 3\n";
332 return;
335 if (actively_recording()) {
336 stop_limit = max_frames;
337 } else {
339 if (Config->get_stop_at_session_end()) {
340 stop_limit = current_end_frame();
341 } else {
342 stop_limit = max_frames;
346 if (maybe_stop (stop_limit)) {
347 no_roll (nframes);
348 cerr << "++PWE out 4\n";
349 return;
352 this_event = *next_event;
353 the_next_one = next_event;
354 ++the_next_one;
357 while (nframes) {
359 this_nframes = nframes; /* real (jack) time relative */
360 frames_moved = (long) floor (_transport_speed * nframes); /* transport relative */
362 /* running an event, position transport precisely to its time */
363 if (this_event && this_event->action_frame <= end_frame && this_event->action_frame >= _transport_frame) {
364 /* this isn't quite right for reverse play */
365 frames_moved = (long) (this_event->action_frame - _transport_frame);
366 this_nframes = (nframes_t) abs( floor(frames_moved / _transport_speed) );
369 if (this_nframes) {
371 click (_transport_frame, nframes);
373 /* now process frames between now and the first event in this block */
374 prepare_diskstreams ();
376 if (process_routes (this_nframes)) {
377 fail_roll (nframes);
378 cerr << "++PWE out 4\n";
379 return;
382 commit_diskstreams (this_nframes, session_needs_butler);
384 nframes -= this_nframes;
386 if (frames_moved < 0) {
387 decrement_transport_position (-frames_moved);
388 } else {
389 increment_transport_position (frames_moved);
392 maybe_stop (stop_limit);
393 check_declick_out ();
396 /* reset port offsets so that Port::get_buffer() will fetch the correct data */
398 Port::increment_port_offset (this_nframes);
400 /* now handle this event and all others scheduled for the same time */
402 while (this_event && this_event->action_frame == _transport_frame) {
403 process_event (this_event);
405 if (the_next_one == events.end()) {
406 this_event = 0;
407 } else {
408 this_event = *the_next_one;
409 ++the_next_one;
413 /* if an event left our state changing, do the right thing */
415 if (nframes && non_realtime_work_pending()) {
416 no_roll (nframes);
417 break;
420 /* this is necessary to handle the case of seamless looping */
421 end_frame = _transport_frame + (nframes_t) floor (nframes * _transport_speed);
424 set_next_event ();
426 } /* implicit release of route lock */
429 if (session_needs_butler) {
430 summon_butler ();
433 if (!_engine.freewheeling() && session_send_mtc) {
434 send_midi_time_code_in_another_thread ();
437 cerr << "++PWE out 5\n";
438 return;
441 void
442 Session::reset_slave_state ()
444 average_slave_delta = 1800;
445 delta_accumulator_cnt = 0;
446 have_first_delta_accumulator = false;
447 slave_state = Stopped;
450 bool
451 Session::transport_locked () const
453 Slave* sl = _slave;
455 if (!locate_pending() && ((Config->get_slave_source() == None) || (sl && sl->ok() && sl->locked()))) {
456 return true;
459 return false;
462 bool
463 Session::follow_slave (nframes_t nframes)
465 float slave_speed;
466 nframes_t slave_transport_frame;
467 nframes_t this_delta;
468 int dir;
469 bool starting;
471 if (!_slave->ok()) {
472 stop_transport ();
473 Config->set_slave_source (None);
474 goto noroll;
477 _slave->speed_and_position (slave_speed, slave_transport_frame);
479 if (!_slave->locked()) {
480 // cerr << "Slave not locked, not rolling\n";
481 goto noroll;
484 if (slave_transport_frame > _transport_frame) {
485 this_delta = slave_transport_frame - _transport_frame;
486 dir = 1;
487 } else {
488 this_delta = _transport_frame - slave_transport_frame;
489 dir = -1;
492 if ((starting = _slave->starting())) {
493 slave_speed = 0.0f;
496 #if 0
497 cerr << "delta = " << (int) (dir * this_delta)
498 << " speed = " << slave_speed
499 << " ts = " << _transport_speed
500 << " M@ "<< slave_transport_frame << " S@ " << _transport_frame
501 << " avgdelta = " << average_slave_delta
502 << endl;
503 #endif
505 if (_slave->is_always_synced() || Config->get_timecode_source_is_synced()) {
507 /* if the TC source is synced, then we assume that its
508 speed is binary: 0.0 or 1.0
511 if (slave_speed != 0.0f) {
512 slave_speed = 1.0f;
515 } else {
517 /* TC source is able to drift relative to us (slave)
518 so we need to keep track of the drift and adjust
519 our speed to remain locked.
522 if (delta_accumulator_cnt >= delta_accumulator_size) {
523 have_first_delta_accumulator = true;
524 delta_accumulator_cnt = 0;
527 if (delta_accumulator_cnt != 0 || this_delta < _current_frame_rate) {
528 delta_accumulator[delta_accumulator_cnt++] = dir*this_delta;
531 if (have_first_delta_accumulator) {
532 average_slave_delta = 0;
533 for (int i = 0; i < delta_accumulator_size; ++i) {
534 average_slave_delta += delta_accumulator[i];
536 average_slave_delta /= delta_accumulator_size;
537 if (average_slave_delta < 0) {
538 average_dir = -1;
539 average_slave_delta = -average_slave_delta;
540 } else {
541 average_dir = 1;
543 // cerr << "avgdelta = " << average_slave_delta*average_dir << endl;
547 if (slave_speed != 0.0f) {
549 /* slave is running */
551 switch (slave_state) {
552 case Stopped:
553 if (_slave->requires_seekahead()) {
554 slave_wait_end = slave_transport_frame + _current_frame_rate;
555 locate (slave_wait_end, false, false);
556 slave_state = Waiting;
557 starting = true;
559 } else {
561 slave_state = Running;
563 Location* al = _locations.auto_loop_location();
565 if (al && play_loop && (slave_transport_frame < al->start() || slave_transport_frame > al->end())) {
566 // master has moved outside the loop: stop looping
567 cerr << "Stop looping - master out of range\n";
568 request_play_loop(false);
571 if (slave_transport_frame != _transport_frame) {
572 locate (slave_transport_frame, false, false);
575 break;
577 case Waiting:
578 break;
580 default:
581 break;
585 if (slave_state == Waiting) {
587 // cerr << "waiting at " << slave_transport_frame << endl;
589 if (slave_transport_frame >= slave_wait_end) {
590 // cerr << "\tstart at " << _transport_frame << endl;
592 slave_state = Running;
594 bool ok = true;
595 nframes_t frame_delta = slave_transport_frame - _transport_frame;
597 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
599 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
600 if (!(*i)->can_internal_playback_seek (frame_delta)) {
601 ok = false;
602 break;
606 if (ok) {
607 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
608 (*i)->internal_playback_seek (frame_delta);
610 _transport_frame += frame_delta;
612 } else {
613 // cerr << "cannot micro-seek\n";
614 /* XXX what? */
617 memset (delta_accumulator, 0, sizeof (nframes_t) * delta_accumulator_size);
618 average_slave_delta = 0;
619 this_delta = 0;
623 if (slave_state == Running && _transport_speed == 0.0f) {
625 // cerr << "slave starts transport\n";
627 start_transport ();
630 } else {
632 /* slave has stopped */
634 if (_transport_speed != 0.0f) {
636 // cerr << "slave stops transport: " << slave_speed << " frame: " << slave_transport_frame
637 // << " tf = " << _transport_frame
638 // << endl;
640 if (Config->get_slave_source() == JACK) {
641 last_stop_frame = _transport_frame;
644 stop_transport();
647 if (slave_transport_frame != _transport_frame) {
648 // cerr << "slave stopped, move to " << slave_transport_frame << endl;
649 force_locate (slave_transport_frame, false);
652 slave_state = Stopped;
655 if (slave_state == Running && !_slave->is_always_synced() && !Config->get_timecode_source_is_synced()) {
658 if (_transport_speed != 0.0f) {
661 note that average_dir is +1 or -1
664 const float adjust_seconds = 1.0f;
665 float delta;
667 //if (average_slave_delta == 0) {
668 delta = this_delta;
669 delta *= dir;
670 // } else {
671 // delta = average_slave_delta;
672 // delta *= average_dir;
673 // }
675 float adjusted_speed = slave_speed +
676 (delta / (adjust_seconds * _current_frame_rate));
678 #if 0
679 cerr << "adjust using " << delta
680 << " towards " << adjusted_speed
681 << " ratio = " << adjusted_speed / slave_speed
682 << " current = " << _transport_speed
683 << " slave @ " << slave_speed
684 << endl;
685 #endif
687 request_transport_speed (adjusted_speed);
689 #if 1
690 if ((nframes_t) average_slave_delta > _slave->resolution()) {
691 // cerr << "not locked\n";
692 goto silent_motion;
694 #endif
698 if (!starting && !non_realtime_work_pending()) {
699 /* speed is set, we're locked, and good to go */
700 return true;
703 silent_motion:
705 if (slave_speed && _transport_speed) {
707 /* something isn't right, but we should move with the master
708 for now.
711 bool need_butler;
713 prepare_diskstreams ();
714 silent_process_routes (nframes);
715 commit_diskstreams (nframes, need_butler);
717 if (need_butler) {
718 summon_butler ();
721 int32_t frames_moved = (int32_t) floor (_transport_speed * nframes);
723 if (frames_moved < 0) {
724 decrement_transport_position (-frames_moved);
725 } else {
726 increment_transport_position (frames_moved);
729 nframes_t stop_limit;
731 if (actively_recording()) {
732 stop_limit = max_frames;
733 } else {
734 if (Config->get_stop_at_session_end()) {
735 stop_limit = current_end_frame();
736 } else {
737 stop_limit = max_frames;
741 maybe_stop (stop_limit);
744 noroll:
745 /* don't move at all */
746 no_roll (nframes);
747 return false;
750 void
751 Session::process_without_events (nframes_t nframes)
753 bool session_needs_butler = false;
754 nframes_t stop_limit;
755 long frames_moved;
757 cerr << "++PwE\n";
759 if (!process_can_proceed()) {
760 _silent = true;
761 return;
764 if (!_exporting && _slave) {
765 if (!follow_slave (nframes)) {
766 cerr << "++PwE out 1\n";
767 return;
771 if (_transport_speed == 0) {
772 fail_roll (nframes);
773 cerr << "++PwE out 2\n";
774 return;
777 if (actively_recording()) {
778 stop_limit = max_frames;
779 } else {
780 if (Config->get_stop_at_session_end()) {
781 stop_limit = current_end_frame();
782 } else {
783 stop_limit = max_frames;
787 if (maybe_stop (stop_limit)) {
788 no_roll (nframes);
789 cerr << "++PwE out 2\n";
790 return;
793 if (maybe_sync_start (nframes)) {
794 cerr << "++PwE out 3\n";
795 return;
798 click (_transport_frame, nframes);
800 prepare_diskstreams ();
802 frames_moved = (long) floor (_transport_speed * nframes);
804 if (process_routes (nframes)) {
805 fail_roll (nframes);
806 cerr << "++PwE out 4\n";
807 return;
810 commit_diskstreams (nframes, session_needs_butler);
812 if (frames_moved < 0) {
813 decrement_transport_position (-frames_moved);
814 } else {
815 increment_transport_position (frames_moved);
818 maybe_stop (stop_limit);
819 check_declick_out ();
821 if (session_needs_butler) {
822 summon_butler ();
825 if (!_engine.freewheeling() && session_send_mtc) {
826 send_midi_time_code_in_another_thread ();
829 cerr << "++PwE out 5\n";
830 return;
833 void
834 Session::process_audition (nframes_t nframes)
836 Event* ev;
837 boost::shared_ptr<RouteList> r = routes.reader ();
839 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
840 if (!(*i)->hidden()) {
841 (*i)->silence (nframes);
845 /* run the auditioner, and if it says we need butler service, ask for it */
847 if (auditioner->play_audition (nframes) > 0) {
848 summon_butler ();
851 /* handle pending events */
853 while (pending_events.read (&ev, 1) == 1) {
854 merge_event (ev);
857 /* if we are not in the middle of a state change,
858 and there are immediate events queued up,
859 process them.
862 while (!non_realtime_work_pending() && !immediate_events.empty()) {
863 Event *ev = immediate_events.front ();
864 immediate_events.pop_front ();
865 process_event (ev);
868 if (!auditioner->active()) {
869 cerr << "P1 pf = pwe\n";
870 process_function = &Session::process_with_events;
874 bool
875 Session::maybe_sync_start (nframes_t& nframes)
877 nframes_t sync_offset;
879 if (!waiting_for_sync_offset) {
880 return false;
883 if (_engine.get_sync_offset (sync_offset) && sync_offset < nframes) {
885 /* generate silence up to the sync point, then
886 adjust nframes + offset to reflect whatever
887 is left to do.
890 no_roll (sync_offset);
891 nframes -= sync_offset;
892 Port::increment_port_offset (sync_offset);
893 waiting_for_sync_offset = false;
895 if (nframes == 0) {
896 return true; // done, nothing left to process
899 } else {
901 /* sync offset point is not within this process()
902 cycle, so just generate silence. and don't bother
903 with any fancy stuff here, just the minimal silence.
906 _silent = true;
908 if (Config->get_locate_while_waiting_for_sync()) {
909 if (micro_locate (nframes)) {
910 /* XXX ERROR !!! XXX */
914 return true; // done, nothing left to process
917 return false;