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