Merged with trunk R879
[ardour.git] / libs / ardour / session_process.cc
1 /*
2     Copyright (C) 1999-2002 Paul Davis 
3
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.
8
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.
13
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.
17
18     $Id$
19 */
20
21 #include <cmath>
22 #include <cerrno>
23 #include <algorithm>
24 #include <unistd.h>
25
26 #include <pbd/error.h>
27
28 #include <glibmm/thread.h>
29
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>
39
40 #include <midi++/manager.h>
41
42 #include "i18n.h"
43
44 using namespace ARDOUR;
45 using namespace PBD;
46 using namespace std;
47
48 void
49 Session::process (jack_nframes_t nframes)
50 {
51         MIDI::Manager::instance()->cycle_start(nframes);
52
53         if (synced_to_jack() && waiting_to_start) {
54                 if ( _engine.transport_state() == AudioEngine::TransportRolling) {
55                         actually_start_transport ();
56                 }
57         }
58
59         if (non_realtime_work_pending()) {
60                 if (g_atomic_int_get (&butler_should_do_transport_work) == 0) {
61                         post_transport ();
62                 } 
63         } 
64         
65         (this->*process_function) (nframes);
66         
67         MIDI::Manager::instance()->cycle_end();
68
69         SendFeedback (); /* EMIT SIGNAL */
70 }
71
72 void
73 Session::prepare_diskstreams ()
74 {
75         boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
76         for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
77                 (*i)->prepare ();
78         }
79 }
80
81 int
82 Session::no_roll (jack_nframes_t nframes, jack_nframes_t offset)
83 {
84         jack_nframes_t end_frame = _transport_frame + nframes; // FIXME: varispeed + no_roll ??
85         int ret = 0;
86         bool declick = get_transport_declick_required();
87         boost::shared_ptr<RouteList> r = routes.reader ();
88
89         if (_click_io) {
90                 _click_io->silence (nframes, offset);
91         }
92
93         if (g_atomic_int_get (&processing_prohibited)) {
94                 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
95                         (*i)->silence (nframes, offset);
96                 }
97                 return 0;
98         }
99
100         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
101                 
102                 if ((*i)->hidden()) {
103                         continue;
104                 }
105                 
106                 (*i)->set_pending_declick (declick);
107                 
108                 if ((*i)->no_roll (nframes, _transport_frame, end_frame, offset, non_realtime_work_pending(), 
109                                    actively_recording(), declick)) {
110                         error << string_compose(_("Session: error in no roll for %1"), (*i)->name()) << endmsg;
111                         ret = -1;
112                         break;
113                 }
114         }
115
116         return ret;
117 }
118
119 int
120 Session::process_routes (jack_nframes_t nframes, jack_nframes_t offset)
121 {
122         bool record_active;
123         int  declick = get_transport_declick_required();
124         bool rec_monitors = get_rec_monitors_input();
125         boost::shared_ptr<RouteList> r = routes.reader ();
126
127         if (transport_sub_state & StopPendingCapture) {
128                 /* force a declick out */
129                 declick = -1;
130         }
131
132         record_active = actively_recording(); // || (get_record_enabled() && get_punch_in());
133
134         const jack_nframes_t start_frame = _transport_frame;
135         const jack_nframes_t end_frame = _transport_frame + (jack_nframes_t)floor(nframes * _transport_speed);
136
137         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
138
139                 int ret;
140
141                 if ((*i)->hidden()) {
142                         continue;
143                 }
144
145                 (*i)->set_pending_declick (declick);
146
147                 if ((ret = (*i)->roll (nframes, start_frame, end_frame, offset, declick, record_active, rec_monitors)) < 0) {
148
149                         /* we have to do this here. Route::roll() for an AudioTrack will have called AudioDiskstream::process(),
150                            and the DS will expect AudioDiskstream::commit() to be called. but we're aborting from that
151                            call path, so make sure we release any outstanding locks here before we return failure.
152                         */
153
154                         boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
155                         for (DiskstreamList::iterator ids = dsl->begin(); ids != dsl->end(); ++ids) {
156                                 (*ids)->recover ();
157                         }
158
159                         stop_transport ();
160                         return -1;
161                 } 
162         }
163
164         return 0;
165 }
166
167 int
168 Session::silent_process_routes (jack_nframes_t nframes, jack_nframes_t offset)
169 {
170         bool record_active = actively_recording();
171         int  declick = get_transport_declick_required();
172         bool rec_monitors = get_rec_monitors_input();
173         boost::shared_ptr<RouteList> r = routes.reader ();
174
175         if (transport_sub_state & StopPendingCapture) {
176                 /* force a declick out */
177                 declick = -1;
178         }
179         
180         const jack_nframes_t start_frame = _transport_frame;
181         const jack_nframes_t end_frame = _transport_frame + lrintf(nframes * _transport_speed);
182
183         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
184
185                 int ret;
186
187                 if ((*i)->hidden()) {
188                         continue;
189                 }
190
191                 if ((ret = (*i)->silent_roll (nframes, start_frame, end_frame, offset, record_active, rec_monitors)) < 0) {
192                         
193                         /* we have to do this here. Route::roll() for an AudioTrack will have called AudioDiskstream::process(),
194                            and the DS will expect AudioDiskstream::commit() to be called. but we're aborting from that
195                            call path, so make sure we release any outstanding locks here before we return failure.
196                         */
197
198                         boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
199                         for (DiskstreamList::iterator ids = dsl->begin(); ids != dsl->end(); ++ids) {
200                                 (*ids)->recover ();
201                         }
202
203                         stop_transport ();
204                         return -1;
205                 } 
206         }
207
208         return 0;
209 }
210
211 void
212 Session::commit_diskstreams (jack_nframes_t nframes, bool &needs_butler)
213 {
214         int dret;
215         float pworst = 1.0f;
216         float cworst = 1.0f;
217
218         boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
219         for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
220
221                 if ((*i)->hidden()) {
222                         continue;
223                 }
224                 
225                 /* force all diskstreams not handled by a Route to call do their stuff.
226                  */
227
228                 if ((dret = (*i)->process (_transport_frame, nframes, 0, actively_recording(), get_rec_monitors_input())) == 0) {
229                         if ((*i)->commit (nframes)) {
230                                 needs_butler = true;
231                         }
232
233                 } else if (dret < 0) {
234                         (*i)->recover();
235                 }
236                 
237                 pworst = min (pworst, (*i)->playback_buffer_load());
238                 cworst = min (cworst, (*i)->capture_buffer_load());
239         }
240
241         uint32_t pmin = g_atomic_int_get (&_playback_load);
242         uint32_t pminold = g_atomic_int_get (&_playback_load_min);
243         uint32_t cmin = g_atomic_int_get (&_capture_load);
244         uint32_t cminold = g_atomic_int_get (&_capture_load_min);
245
246         g_atomic_int_set (&_playback_load, (uint32_t) floor (pworst * 100.0f));
247         g_atomic_int_set (&_capture_load, (uint32_t) floor (cworst * 100.0f));
248         g_atomic_int_set (&_playback_load_min, min (pmin, pminold));
249         g_atomic_int_set (&_capture_load_min, min (cmin, cminold));
250
251         if (actively_recording()) {
252                 set_dirty();
253         }
254 }
255
256
257 void
258 Session::process_with_events (jack_nframes_t nframes)
259 {
260         Event*         ev;
261         jack_nframes_t this_nframes;
262         jack_nframes_t end_frame;
263         jack_nframes_t offset;
264         jack_nframes_t stop_limit;
265         long           frames_moved;
266         bool           session_needs_butler = false;
267
268         /* make sure the auditioner is silent */
269
270         if (auditioner) {
271                 auditioner->silence (nframes, 0);
272         }
273
274         /* handle any pending events */
275
276         while (pending_events.read (&ev, 1) == 1) {
277                 merge_event (ev);
278         }
279
280         /* if we are not in the middle of a state change,
281            and there are immediate events queued up,
282            process them. 
283         */
284
285         while (!non_realtime_work_pending() && !immediate_events.empty()) {
286                 Event *ev = immediate_events.front ();
287                 immediate_events.pop_front ();
288                 process_event (ev);
289         }
290
291         /* Events caused a transport change, send an MTC Full Frame (SMPTE) message.
292          * This is sent whether rolling or not, to give slaves an idea of ardour time
293          * on locates (and allow slow slaves to position and prepare for rolling)
294          */
295         if (_send_smpte_update) {
296                 send_full_time_code(nframes);
297         }
298
299         if (!process_can_proceed()) {
300                 no_roll (nframes, 0);
301                 return;
302         }
303
304         if (events.empty() || next_event == events.end()) {
305                 process_without_events (nframes);
306                 return;
307         }
308
309         end_frame = _transport_frame + nframes;
310
311         {
312                 Event* this_event;
313                 Events::iterator the_next_one;
314                 
315                 if (post_transport_work & (PostTransportLocate|PostTransportStop)) {
316                         no_roll (nframes, 0);
317                         return;
318                 }
319                 
320                 if (!_exporting && _slave) {
321                         if (!follow_slave (nframes, 0)) {
322                                 return;
323                         }
324                 } 
325
326                 if (_transport_speed == 0) {
327                         no_roll (nframes, 0);
328                         return;
329                 }
330         
331                 send_midi_time_code_for_cycle(nframes);
332
333                 if (actively_recording()) {
334                         stop_limit = max_frames;
335                 } else {
336
337                         if (Config->get_stop_at_session_end()) {
338                                 stop_limit = current_end_frame();
339                         } else {
340                                 stop_limit = max_frames;
341                         }
342                 }
343
344                 if (maybe_stop (stop_limit)) {
345                         no_roll (nframes, 0);
346                         return;
347                 } 
348
349                 this_event = *next_event;
350                 the_next_one = next_event;
351                 ++the_next_one;
352
353                 offset = 0;
354
355                 while (nframes) {
356
357                         if (this_event == 0 || this_event->action_frame > end_frame || this_event->action_frame < _transport_frame) {
358
359                                 this_nframes = nframes;
360                                 
361                         } else {
362                                 
363                                 /* compute nframes to next event */
364
365                                 if (this_event->action_frame < end_frame) {
366                                         this_nframes = nframes - (end_frame - this_event->action_frame);
367                                 } else {
368                                         this_nframes = nframes;
369                                 }
370                         }
371
372                         if (this_nframes) {
373                                 
374                                 click (_transport_frame, nframes, offset);
375                                 
376                                 /* now process frames between now and the first event in this block */
377                                 prepare_diskstreams ();
378
379                                 if (process_routes (this_nframes, offset)) {
380                                         no_roll (nframes, 0);
381                                         return;
382                                 }
383                                 
384                                 commit_diskstreams (this_nframes, session_needs_butler);
385
386                                 nframes -= this_nframes;
387                                 offset += this_nframes;
388                                 
389                                 frames_moved = (jack_nframes_t) floor (_transport_speed * this_nframes);
390                         
391                                 if (frames_moved < 0) {
392                                         decrement_transport_position (-frames_moved);
393                                 } else {
394                                         increment_transport_position (frames_moved);
395                                 }
396
397                                 maybe_stop (stop_limit);
398                                 check_declick_out ();
399                         }
400
401                         /* now handle this event and all others scheduled for the same time */
402                         
403                         while (this_event && this_event->action_frame == _transport_frame) {
404                                 process_event (this_event);
405
406                                 if (the_next_one == events.end()) {
407                                         this_event = 0;
408                                 } else {
409                                         this_event = *the_next_one;
410                                         ++the_next_one;
411                                 }
412                         } 
413
414                         /* if an event left our state changing, do the right thing */
415
416                         if (non_realtime_work_pending()) {
417                                 no_roll (nframes, offset);
418                                 break;
419                         }
420
421                         /* this is necessary to handle the case of seamless looping */
422                         /* not sure if it will work in conjuction with varispeed */
423                         end_frame = _transport_frame + nframes;
424                         
425                 }
426
427                 set_next_event ();
428
429         } /* implicit release of route lock */
430
431         if (session_needs_butler)
432                 summon_butler ();
433 }
434
435 void
436 Session::reset_slave_state ()
437 {
438         average_slave_delta = 1800;
439         delta_accumulator_cnt = 0;
440         have_first_delta_accumulator = false;
441         slave_state = Stopped;
442 }
443
444 bool
445 Session::transport_locked () const
446 {
447         Slave* sl = _slave;
448
449         if (!locate_pending() && ((_slave_type == None) || (sl && sl->ok() && sl->locked()))) {
450                 return true;
451         }
452
453         return false;
454 }
455
456 bool
457 Session::follow_slave (jack_nframes_t nframes, jack_nframes_t offset)
458 {
459         float slave_speed;
460         jack_nframes_t slave_transport_frame;
461         jack_nframes_t this_delta;
462         int dir;
463         bool starting;
464
465         if (!_slave->ok()) {
466                 stop_transport ();
467                 set_slave_source (None, 0);
468                 goto noroll;
469         }
470         
471         _slave->speed_and_position (slave_speed, slave_transport_frame);
472
473         if (!_slave->locked()) {
474                 goto noroll;
475         }
476
477         if (slave_transport_frame > _transport_frame) {
478                 this_delta = slave_transport_frame - _transport_frame;
479                 dir = 1;
480         } else {
481                 this_delta = _transport_frame - slave_transport_frame;
482                 dir = -1;
483         }
484
485         if ((starting = _slave->starting())) {
486                 slave_speed = 0.0f;
487         }
488
489 #if 0
490         cerr << "delta = " << (int) (dir * this_delta)
491              << " speed = " << slave_speed 
492              << " ts = " << _transport_speed 
493              << " M@"<< slave_transport_frame << " S@" << _transport_frame 
494              << " avgdelta = " << average_slave_delta 
495              << endl;
496 #endif  
497
498         if (Config->get_timecode_source_is_synced()) {
499
500                 /* if the TC source is synced, then we assume that its 
501                    speed is binary: 0.0 or 1.0
502                 */
503
504                 if (slave_speed != 0.0f) {
505                         slave_speed = 1.0f;
506                 } 
507
508         } else {
509
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.
513                 */
514
515                 if (delta_accumulator_cnt >= delta_accumulator_size) {
516                         have_first_delta_accumulator = true;
517                         delta_accumulator_cnt = 0;
518                 }
519
520                 if (delta_accumulator_cnt != 0 || this_delta < _current_frame_rate) {
521                         delta_accumulator[delta_accumulator_cnt++] = dir*this_delta;
522                 }
523                 
524                 if (have_first_delta_accumulator) {
525                         average_slave_delta = 0;
526                         for (int i = 0; i < delta_accumulator_size; ++i) {
527                                 average_slave_delta += delta_accumulator[i];
528                         }
529                         average_slave_delta /= delta_accumulator_size;
530                         if (average_slave_delta < 0) {
531                                 average_dir = -1;
532                                 average_slave_delta = -average_slave_delta;
533                         } else {
534                                 average_dir = 1;
535                         }
536                         // cerr << "avgdelta = " << average_slave_delta*average_dir << endl;
537                 }
538         }
539
540         if (slave_speed != 0.0f) {
541
542                 /* slave is running */
543
544                 switch (slave_state) {
545                 case Stopped:
546                         if (_slave->requires_seekahead()) {
547                                 slave_wait_end = slave_transport_frame + _current_frame_rate;
548                                 locate (slave_wait_end, false, false);
549                                 slave_state = Waiting;
550                                 starting = true;
551
552                         } else {
553
554                                 slave_state = Running;
555
556                                 Location* al = _locations.auto_loop_location();
557
558                                 if (al && auto_loop && (slave_transport_frame < al->start() || slave_transport_frame > al->end())) {
559                                         // cancel looping
560                                         request_auto_loop(false);
561                                 }
562
563                                 if (slave_transport_frame != _transport_frame) {
564                                         locate (slave_transport_frame, false, false);
565                                 }
566                         }
567                         break;
568
569                 case Waiting:
570                         break;
571
572                 default:
573                         break;
574
575                 }
576
577                 if (slave_state == Waiting) {
578
579                         // cerr << "waiting at " << slave_transport_frame << endl;
580                         if (slave_transport_frame >= slave_wait_end) {
581                                 // cerr << "\tstart at " << _transport_frame << endl;
582
583                                 slave_state = Running;
584
585                                 bool ok = true;
586                                 jack_nframes_t frame_delta = slave_transport_frame - _transport_frame;
587
588                                 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
589                                 
590                                 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
591                                         if (!(*i)->can_internal_playback_seek (frame_delta)) {
592                                                 ok = false;
593                                                 break;
594                                         }
595                                 }
596
597                                 if (ok) {
598                                         for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
599                                                 (*i)->internal_playback_seek (frame_delta);
600                                         }
601                                         _transport_frame += frame_delta;
602                                        
603                                 } else {
604                                         // cerr << "cannot micro-seek\n";
605                                         /* XXX what? */
606                                 }
607
608                                 memset (delta_accumulator, 0, sizeof (jack_nframes_t) * delta_accumulator_size);
609                                 average_slave_delta = 0;
610                                 this_delta = 0;
611                         }
612                 }
613                 
614                 if (slave_state == Running && _transport_speed == 0.0f) {
615                         
616                         // cerr << "slave starts transport\n";
617                         
618                         start_transport ();
619                 } 
620
621         } else {
622
623                 /* slave has stopped */
624
625                 if (_transport_speed != 0.0f) {
626
627                         // cerr << "slave stops transport: " << slave_speed << " frame: " << slave_transport_frame 
628                         // << " tf = " << _transport_frame
629                         // << endl;
630                         
631                         if (_slave_type == JACK) {
632                                 last_stop_frame = _transport_frame;
633                         }
634
635                         stop_transport();
636                 }
637
638                 if (slave_transport_frame != _transport_frame) {
639                         // cerr << "slave stopped, move to " << slave_transport_frame << endl;
640                         force_locate (slave_transport_frame, false);
641                 }
642
643                 slave_state = Stopped;
644         }
645
646         if (slave_state == Running && !Config->get_timecode_source_is_synced()) {
647
648
649                 if (_transport_speed != 0.0f) {
650                         
651                         /* 
652                            note that average_dir is +1 or -1 
653                         */
654                         
655                         const float adjust_seconds = 1.0f;
656                         float delta;
657
658                         //if (average_slave_delta == 0) {
659                                 delta = this_delta;
660                                 delta *= dir;
661 //                      } else {
662 //                              delta = average_slave_delta;
663 //                              delta *= average_dir;
664 //                      }
665
666                         float adjusted_speed = slave_speed +
667                                 (delta / (adjust_seconds * _current_frame_rate));
668                         
669                         // cerr << "adjust using " << delta
670                         // << " towards " << adjusted_speed
671                         // << " ratio = " << adjusted_speed / slave_speed
672                         // << " current = " << _transport_speed
673                         // << " slave @ " << slave_speed
674                         // << endl;
675                         
676                         request_transport_speed (adjusted_speed);
677                         
678 #if 1
679                         if ((jack_nframes_t) average_slave_delta > _slave->resolution()) {
680                                 // cerr << "not locked\n";
681                                 goto silent_motion;
682                         }
683 #endif
684                 }
685         } 
686
687         if (!starting && !non_realtime_work_pending()) {
688                 /* speed is set, we're locked, and good to go */
689                 return true;
690         }
691
692   silent_motion:
693
694         if (slave_speed && _transport_speed) {
695
696                 /* something isn't right, but we should move with the master
697                    for now.
698                 */
699
700                 bool need_butler;
701                 
702                 prepare_diskstreams ();
703                 silent_process_routes (nframes, offset);
704                 commit_diskstreams (nframes, need_butler);
705
706                 if (need_butler) {
707                         summon_butler ();
708                 }
709                 
710                 int32_t frames_moved = (int32_t) floor (_transport_speed * nframes);
711                 
712                 if (frames_moved < 0) {
713                         decrement_transport_position (-frames_moved);
714                 } else {
715                         increment_transport_position (frames_moved);
716                 }
717                 
718                 jack_nframes_t stop_limit;
719                 
720                 if (actively_recording()) {
721                         stop_limit = max_frames;
722                 } else {
723                         if (Config->get_stop_at_session_end()) {
724                                 stop_limit = current_end_frame();
725                         } else {
726                                 stop_limit = max_frames;
727                         }
728                 }
729
730                 maybe_stop (stop_limit);
731         }
732
733   noroll:
734         /* don't move at all */
735         no_roll (nframes, 0);
736         return false;
737 }
738
739 void
740 Session::process_without_events (jack_nframes_t nframes)
741 {
742         bool session_needs_butler = false;
743         jack_nframes_t stop_limit;
744         long frames_moved;
745         jack_nframes_t offset = 0;
746
747         {
748                 if (post_transport_work & (PostTransportLocate|PostTransportStop)) {
749                         no_roll (nframes, 0);
750                         return;
751                 }
752
753                 if (!_exporting && _slave) {
754                         if (!follow_slave (nframes, 0)) {
755                                 return;
756                         }
757                 } 
758
759                 if (_transport_speed == 0) {
760                         no_roll (nframes, 0);
761                         return;
762                 }
763         
764                 send_midi_time_code_for_cycle(nframes);
765                 
766                 if (actively_recording()) {
767                         stop_limit = max_frames;
768                 } else {
769                         if (Config->get_stop_at_session_end()) {
770                                 stop_limit = current_end_frame();
771                         } else {
772                                 stop_limit = max_frames;
773                         }
774                 }
775                 
776                 if (maybe_stop (stop_limit)) {
777                         no_roll (nframes, 0);
778                         return;
779                 } 
780
781                 if (maybe_sync_start (nframes, offset)) {
782                         return;
783                 }
784
785                 click (_transport_frame, nframes, offset);
786
787                 prepare_diskstreams ();
788         
789                 frames_moved = (long) floor (_transport_speed * nframes);
790
791                 if (process_routes (nframes, offset)) {
792                         no_roll (nframes, offset);
793                         return;
794                 }
795
796                 commit_diskstreams (nframes, session_needs_butler);
797
798                 if (frames_moved < 0) {
799                         decrement_transport_position (-frames_moved);
800                 } else {
801                         increment_transport_position (frames_moved);
802                 }
803                 
804                 maybe_stop (stop_limit);
805                 check_declick_out ();
806
807         } /* implicit release of route lock */
808
809         if (session_needs_butler)
810                 summon_butler ();
811 }
812
813 void
814 Session::process_audition (jack_nframes_t nframes)
815 {
816         Event* ev;
817         boost::shared_ptr<RouteList> r = routes.reader ();
818
819         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
820                 if (!(*i)->hidden()) {
821                         (*i)->silence (nframes, 0);
822                 }
823         }
824
825         /* run the auditioner, and if it says we need butler service, ask for it */
826         
827         if (auditioner->play_audition (nframes) > 0) {
828                 summon_butler ();
829         } 
830
831         /* handle pending events */
832
833         while (pending_events.read (&ev, 1) == 1) {
834                 merge_event (ev);
835         }
836
837         /* if we are not in the middle of a state change,
838            and there are immediate events queued up,
839            process them. 
840         */
841
842         while (!non_realtime_work_pending() && !immediate_events.empty()) {
843                 Event *ev = immediate_events.front ();
844                 immediate_events.pop_front ();
845                 process_event (ev);
846         }
847
848         if (!auditioner->active()) {
849                 process_function = &Session::process_with_events;
850         }
851 }
852
853 bool
854 Session::maybe_sync_start (jack_nframes_t& nframes, jack_nframes_t& offset)
855 {
856         jack_nframes_t sync_offset;
857         
858         if (!waiting_for_sync_offset) {
859                 return false;
860         }
861
862         if (_engine.get_sync_offset (sync_offset) && sync_offset < nframes) {
863
864                 no_roll (sync_offset, 0);
865                 nframes -= sync_offset;
866                 offset += sync_offset;
867                 waiting_for_sync_offset = false;
868                 
869                 if (nframes == 0) {
870                         return true; // done
871                 }
872                 
873         } else {
874                 no_roll (nframes, 0);
875                 return true; // done
876         }
877
878         return false;
879 }
880