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