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