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