60cb6ecee1f634022a00a86a82effcecdf6c474c
[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 #include "pbd/enumwriter.h"
27
28 #include <glibmm/thread.h>
29
30 #include "ardour/ardour.h"
31 #include "ardour/audioengine.h"
32 #include "ardour/auditioner.h"
33 #include "ardour/butler.h"
34 #include "ardour/debug.h"
35 #include "ardour/process_thread.h"
36 #include "ardour/session.h"
37 #include "ardour/slave.h"
38 #include "ardour/timestamps.h"
39 #include "ardour/graph.h"
40 #include "ardour/audio_port.h"
41 #include "ardour/tempo.h"
42 #include "ardour/ticker.h"
43 #include "ardour/cycle_timer.h"
44
45 #include "midi++/manager.h"
46 #include "midi++/mmc.h"
47
48 #include "i18n.h"
49
50 using namespace ARDOUR;
51 using namespace PBD;
52 using namespace std;
53
54 /** Called by the audio engine when there is work to be done with JACK.
55  * @param nframes Number of frames to process.
56  */
57
58 void
59 Session::process (pframes_t nframes)
60 {
61         framepos_t transport_at_start = _transport_frame;
62
63         MIDI::Manager::instance()->cycle_start(nframes);
64
65         _silent = false;
66
67         if (processing_blocked()) {
68                 _silent = true;
69                 return;
70         }
71
72         if (non_realtime_work_pending()) {
73                 if (!_butler->transport_work_requested ()) {
74                         post_transport ();
75                 }
76         }
77
78         _engine.main_thread()->get_buffers ();
79
80         (this->*process_function) (nframes);
81
82         _engine.main_thread()->drop_buffers ();
83
84         /* deliver MIDI clock. Note that we need to use the transport frame
85          * position at the start of process(), not the value at the end of
86          * it. We may already have ticked() because of a transport state
87          * change, for example.
88          */
89
90         try {
91                 if (!_engine.freewheeling() && Config->get_send_midi_clock() && transport_speed() == 1.0f && midi_clock->has_midi_port()) {
92                         midi_clock->tick (transport_at_start);
93                 }
94         } catch (...) {
95                 /* don't bother with a message */
96         }
97
98         SendFeedback (); /* EMIT SIGNAL */
99
100         MIDI::Manager::instance()->cycle_end();
101 }
102
103 int
104 Session::fail_roll (pframes_t nframes)
105 {
106         return no_roll (nframes);
107 }
108
109 int
110 Session::no_roll (pframes_t nframes)
111 {
112         PT_TIMING_CHECK (4);
113         
114         framepos_t end_frame = _transport_frame + nframes; // FIXME: varispeed + no_roll ??
115         int ret = 0;
116         bool declick = get_transport_declick_required();
117         boost::shared_ptr<RouteList> r = routes.reader ();
118
119         if (_click_io) {
120                 _click_io->silence (nframes);
121         }
122
123         if (_process_graph) {
124                 DEBUG_TRACE(DEBUG::ProcessThreads,"calling graph/no-roll\n");
125                 _process_graph->routes_no_roll( nframes, _transport_frame, end_frame, non_realtime_work_pending(), declick);
126         } else {
127                 PT_TIMING_CHECK (10);
128                 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
129
130                         if ((*i)->is_hidden()) {
131                                 continue;
132                         }
133
134                         (*i)->set_pending_declick (declick);
135
136                         if ((*i)->no_roll (nframes, _transport_frame, end_frame, non_realtime_work_pending())) {
137                                 error << string_compose(_("Session: error in no roll for %1"), (*i)->name()) << endmsg;
138                                 ret = -1;
139                                 break;
140                         }
141                 }
142                 PT_TIMING_CHECK (11);
143         }
144
145         PT_TIMING_CHECK (5);
146         return ret;
147 }
148
149 /** @param need_butler to be set to true by this method if it needs the butler,
150  *  otherwise it must be left alone.
151  */
152 int
153 Session::process_routes (pframes_t nframes, bool& need_butler)
154 {
155         int  declick = get_transport_declick_required();
156         boost::shared_ptr<RouteList> r = routes.reader ();
157
158         if (transport_sub_state & StopPendingCapture) {
159                 /* force a declick out */
160                 declick = -1;
161         }
162
163         const framepos_t start_frame = _transport_frame;
164         const framepos_t end_frame = _transport_frame + floor (nframes * _transport_speed);
165
166         if (_process_graph) {
167                 DEBUG_TRACE(DEBUG::ProcessThreads,"calling graph/process-routes\n");
168                 _process_graph->process_routes (nframes, start_frame, end_frame, declick, need_butler);
169         } else {
170
171                 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
172
173                         int ret;
174
175                         if ((*i)->is_hidden()) {
176                                 continue;
177                         }
178
179                         (*i)->set_pending_declick (declick);
180
181                         bool b = false;
182
183                         if ((ret = (*i)->roll (nframes, start_frame, end_frame, declick, b)) < 0) {
184                                 stop_transport ();
185                                 return -1;
186                         }
187
188                         if (b) {
189                                 need_butler = true;
190                         }
191                 }
192         }
193
194         return 0;
195 }
196
197 /** @param need_butler to be set to true by this method if it needs the butler,
198  *  otherwise it must be left alone.
199  */
200 int
201 Session::silent_process_routes (pframes_t nframes, bool& need_butler)
202 {
203         boost::shared_ptr<RouteList> r = routes.reader ();
204
205         const framepos_t start_frame = _transport_frame;
206         const framepos_t end_frame = _transport_frame + lrintf(nframes * _transport_speed);
207
208         if (_process_graph) {
209                 _process_graph->silent_process_routes (nframes, start_frame, end_frame, need_butler);
210         } else {
211                 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
212
213                         int ret;
214
215                         if ((*i)->is_hidden()) {
216                                 continue;
217                         }
218
219                         bool b = false;
220
221                         if ((ret = (*i)->silent_roll (nframes, start_frame, end_frame, b)) < 0) {
222                                 stop_transport ();
223                                 return -1;
224                         }
225
226                         if (b) {
227                                 need_butler = true;
228                         }
229                 }
230         }
231
232         return 0;
233 }
234
235 void
236 Session::get_track_statistics ()
237 {
238         float pworst = 1.0f;
239         float cworst = 1.0f;
240
241         boost::shared_ptr<RouteList> rl = routes.reader();
242         for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
243
244                 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
245
246                 if (!tr || tr->hidden()) {
247                         continue;
248                 }
249
250                 pworst = min (pworst, tr->playback_buffer_load());
251                 cworst = min (cworst, tr->capture_buffer_load());
252         }
253
254         g_atomic_int_set (&_playback_load, (uint32_t) floor (pworst * 100.0f));
255         g_atomic_int_set (&_capture_load, (uint32_t) floor (cworst * 100.0f));
256
257         if (actively_recording()) {
258                 set_dirty();
259         }
260 }
261
262 /** Process callback used when the auditioner is not active */
263 void
264 Session::process_with_events (pframes_t nframes)
265 {
266         PT_TIMING_CHECK (3);
267         
268         SessionEvent*  ev;
269         pframes_t      this_nframes;
270         framepos_t     end_frame;
271         bool           session_needs_butler = false;
272         framecnt_t     frames_moved;
273
274         /* make sure the auditioner is silent */
275
276         if (auditioner) {
277                 auditioner->silence (nframes);
278         }
279
280         /* handle any pending events */
281
282         while (pending_events.read (&ev, 1) == 1) {
283                 merge_event (ev);
284         }
285
286         /* if we are not in the middle of a state change,
287            and there are immediate events queued up,
288            process them.
289         */
290
291         while (!non_realtime_work_pending() && !immediate_events.empty()) {
292                 SessionEvent *ev = immediate_events.front ();
293                 immediate_events.pop_front ();
294                 process_event (ev);
295         }
296
297         /* Decide on what to do with quarter-frame MTC during this cycle */
298
299         bool const was_sending_qf_mtc = _send_qf_mtc;
300         double const tolerance = Config->get_mtc_qf_speed_tolerance() / 100.0;
301
302         if (_transport_speed != 0) {
303                 _send_qf_mtc = (
304                         Config->get_send_mtc () &&
305                         _transport_speed >= (1 - tolerance) &&
306                         _transport_speed <= (1 + tolerance)
307                         );
308
309                 if (_send_qf_mtc && !was_sending_qf_mtc) {
310                         /* we will re-start quarter-frame MTC this cycle, so send a full update to set things up */
311                         _send_timecode_update = true;
312                 }
313
314                 if (Config->get_send_mtc() && !_send_qf_mtc && _pframes_since_last_mtc > (frame_rate () / 4)) {
315                         /* we're sending MTC, but we're not sending QF MTC at the moment, and it's been
316                            a quarter of a second since we sent anything at all, so send a full MTC update
317                            this cycle.
318                         */
319                         _send_timecode_update = true;
320                 }
321
322                 _pframes_since_last_mtc += nframes;
323         }
324
325         /* Events caused a transport change (or we re-started sending
326          * MTC), so send an MTC Full Frame (Timecode) message.  This
327          * is sent whether rolling or not, to give slaves an idea of
328          * ardour time on locates (and allow slow slaves to position
329          * and prepare for rolling)
330          */
331         if (_send_timecode_update) {
332                 send_full_time_code (_transport_frame);
333         }
334
335         if (!process_can_proceed()) {
336                 _silent = true;
337                 return;
338         }
339
340         if (events.empty() || next_event == events.end()) {
341                 process_without_events (nframes);
342                 return;
343         }
344
345         if (_transport_speed == 1.0) {
346                 frames_moved = (framecnt_t) nframes;
347         } else {
348                 interpolation.set_target_speed (fabs(_target_transport_speed));
349                 interpolation.set_speed (fabs(_transport_speed));
350                 frames_moved = (framecnt_t) interpolation.interpolate (0, nframes, 0, 0);
351         }
352
353         end_frame = _transport_frame + frames_moved;
354
355         {
356                 SessionEvent* this_event;
357                 Events::iterator the_next_one;
358
359                 if (!process_can_proceed()) {
360                         _silent = true;
361                         return;
362                 }
363
364                 if (!_exporting && _slave) {
365                         if (!follow_slave (nframes)) {
366                                 return;
367                         }
368                 }
369
370                 if (_transport_speed == 0) {
371                         no_roll (nframes);
372                         return;
373                 }
374
375                 if (!_exporting && !timecode_transmission_suspended()) {
376                         send_midi_time_code_for_cycle (_transport_frame, end_frame, nframes);
377                 }
378
379                 framepos_t stop_limit = compute_stop_limit ();
380
381                 if (maybe_stop (stop_limit)) {
382                         no_roll (nframes);
383                         return;
384                 }
385
386                 this_event = *next_event;
387                 the_next_one = next_event;
388                 ++the_next_one;
389
390                 /* yes folks, here it is, the actual loop where we really truly
391                    process some audio
392                 */
393
394                 while (nframes) {
395
396                         this_nframes = nframes; /* real (jack) time relative */
397                         frames_moved = (framecnt_t) floor (_transport_speed * nframes); /* transport relative */
398
399                         /* running an event, position transport precisely to its time */
400                         if (this_event && this_event->action_frame <= end_frame && this_event->action_frame >= _transport_frame) {
401                                 /* this isn't quite right for reverse play */
402                                 frames_moved = (framecnt_t) (this_event->action_frame - _transport_frame);
403                                 this_nframes = abs (floor(frames_moved / _transport_speed));
404                         }
405
406                         if (this_nframes) {
407
408                                 click (_transport_frame, this_nframes);
409
410                                 if (process_routes (this_nframes, session_needs_butler)) {
411                                         fail_roll (nframes);
412                                         return;
413                                 }
414
415                                 get_track_statistics ();
416
417                                 nframes -= this_nframes;
418
419                                 if (frames_moved < 0) {
420                                         decrement_transport_position (-frames_moved);
421                                 } else {
422                                         increment_transport_position (frames_moved);
423                                 }
424
425                                 maybe_stop (stop_limit);
426                                 check_declick_out ();
427                         }
428
429                         _engine.split_cycle (this_nframes);
430
431                         /* now handle this event and all others scheduled for the same time */
432
433                         while (this_event && this_event->action_frame == _transport_frame) {
434                                 process_event (this_event);
435
436                                 if (the_next_one == events.end()) {
437                                         this_event = 0;
438                                 } else {
439                                         this_event = *the_next_one;
440                                         ++the_next_one;
441                                 }
442                         }
443
444                         /* if an event left our state changing, do the right thing */
445
446                         if (nframes && non_realtime_work_pending()) {
447                                 no_roll (nframes);
448                                 break;
449                         }
450
451                         /* this is necessary to handle the case of seamless looping */
452                         end_frame = _transport_frame + floor (nframes * _transport_speed);
453                 }
454
455                 set_next_event ();
456
457         } /* implicit release of route lock */
458
459         if (session_needs_butler) {
460                 _butler->summon ();
461         }
462 }
463
464 void
465 Session::reset_slave_state ()
466 {
467         average_slave_delta = 1800;
468         delta_accumulator_cnt = 0;
469         have_first_delta_accumulator = false;
470         _slave_state = Stopped;
471 }
472
473 bool
474 Session::transport_locked () const
475 {
476         Slave* sl = _slave;
477
478         if (!locate_pending() && (!config.get_external_sync() || (sl && sl->ok() && sl->locked()))) {
479                 return true;
480         }
481
482         return false;
483 }
484
485 bool
486 Session::follow_slave (pframes_t nframes)
487 {
488         double slave_speed;
489         framepos_t slave_transport_frame;
490         framecnt_t this_delta;
491         int dir;
492
493         if (!_slave->ok()) {
494                 stop_transport ();
495                 config.set_external_sync (false);
496                 goto noroll;
497         }
498
499         _slave->speed_and_position (slave_speed, slave_transport_frame);
500
501         DEBUG_TRACE (DEBUG::Slave, string_compose ("Slave position %1 speed %2\n", slave_transport_frame, slave_speed));
502
503         if (!_slave->locked()) {
504                 DEBUG_TRACE (DEBUG::Slave, "slave not locked\n");
505                 goto noroll;
506         }
507
508         if (slave_transport_frame > _transport_frame) {
509                 this_delta = slave_transport_frame - _transport_frame;
510                 dir = 1;
511         } else {
512                 this_delta = _transport_frame - slave_transport_frame;
513                 dir = -1;
514         }
515
516         if (_slave->starting()) {
517                 slave_speed = 0.0f;
518         }
519
520         if (_slave->is_always_synced() || config.get_timecode_source_is_synced()) {
521
522                 /* if the TC source is synced, then we assume that its
523                    speed is binary: 0.0 or 1.0
524                 */
525
526                 if (slave_speed != 0.0f) {
527                         slave_speed = 1.0f;
528                 }
529
530         } else {
531
532                 /* if we are chasing and the average delta between us and the
533                    master gets too big, we want to switch to silent
534                    motion. so keep track of that here.
535                 */
536
537                 if (_slave_state == Running) {
538                         calculate_moving_average_of_slave_delta(dir, this_delta);
539                 }
540         }
541
542         track_slave_state (slave_speed, slave_transport_frame, this_delta);
543
544         DEBUG_TRACE (DEBUG::Slave, string_compose ("slave state %1 @ %2 speed %3 cur delta %4 avg delta %5\n",
545                                                    _slave_state, slave_transport_frame, slave_speed, this_delta, average_slave_delta));
546
547
548         if (_slave_state == Running && !_slave->is_always_synced() && !config.get_timecode_source_is_synced()) {
549
550                 if (_transport_speed != 0.0f) {
551
552                         /*
553                            note that average_dir is +1 or -1
554                         */
555
556                         float delta;
557
558                         if (average_slave_delta == 0) {
559                                 delta = this_delta;
560                                 delta *= dir;
561                         } else {
562                                 delta = average_slave_delta;
563                                 delta *= average_dir;
564                         }
565
566 #ifndef NDEBUG
567                         if (slave_speed != 0.0) {
568                                 DEBUG_TRACE (DEBUG::Slave, string_compose ("delta = %1 speed = %2 ts = %3 M@%4 S@%5 avgdelta %6\n",
569                                                                            (int) (dir * this_delta),
570                                                                            slave_speed,
571                                                                            _transport_speed,
572                                                                            _transport_frame,
573                                                                            slave_transport_frame,
574                                                                            average_slave_delta));
575                         }
576 #endif
577
578                         if (_slave->give_slave_full_control_over_transport_speed()) {
579                                 set_transport_speed (slave_speed, false, false);
580                                 //std::cout << "set speed = " << slave_speed << "\n";
581                         } else {
582                                 float adjusted_speed = slave_speed + (1.5 * (delta /  float(_current_frame_rate)));
583                                 request_transport_speed (adjusted_speed);
584                                 DEBUG_TRACE (DEBUG::Slave, string_compose ("adjust using %1 towards %2 ratio %3 current %4 slave @ %5\n",
585                                                                            delta, adjusted_speed, adjusted_speed/slave_speed, _transport_speed,
586                                                                            slave_speed));
587                         }
588
589 #if 1
590                         if ((framecnt_t) abs(average_slave_delta) > _slave->resolution()) {
591                                 cerr << "average slave delta greater than slave resolution (" << _slave->resolution() << "), going to silent motion\n";
592                                 goto silent_motion;
593                         }
594 #endif
595                 }
596         }
597
598
599         if (_slave_state == Running && !non_realtime_work_pending()) {
600                 /* speed is set, we're locked, and good to go */
601                 return true;
602         }
603
604   silent_motion:
605         DEBUG_TRACE (DEBUG::Slave, "silent motion\n")
606         follow_slave_silently (nframes, slave_speed);
607
608   noroll:
609         /* don't move at all */
610         DEBUG_TRACE (DEBUG::Slave, "no roll\n")
611         no_roll (nframes);
612         return false;
613 }
614
615 void
616 Session::calculate_moving_average_of_slave_delta (int dir, framecnt_t this_delta)
617 {
618         if (delta_accumulator_cnt >= delta_accumulator_size) {
619                 have_first_delta_accumulator = true;
620                 delta_accumulator_cnt = 0;
621         }
622
623         if (delta_accumulator_cnt != 0 || this_delta < _current_frame_rate) {
624                 delta_accumulator[delta_accumulator_cnt++] = (framecnt_t) dir *  (framecnt_t) this_delta;
625         }
626
627         if (have_first_delta_accumulator) {
628                 average_slave_delta = 0L;
629                 for (int i = 0; i < delta_accumulator_size; ++i) {
630                         average_slave_delta += delta_accumulator[i];
631                 }
632                 average_slave_delta /= (int32_t) delta_accumulator_size;
633                 if (average_slave_delta < 0L) {
634                         average_dir = -1;
635                         average_slave_delta = abs(average_slave_delta);
636                 } else {
637                         average_dir = 1;
638                 }
639         }
640 }
641
642 void
643 Session::track_slave_state (float slave_speed, framepos_t slave_transport_frame, framecnt_t /*this_delta*/)
644 {
645         if (slave_speed != 0.0f) {
646
647                 /* slave is running */
648
649                 switch (_slave_state) {
650                 case Stopped:
651                         if (_slave->requires_seekahead()) {
652                                 slave_wait_end = slave_transport_frame + _slave->seekahead_distance ();
653                                 DEBUG_TRACE (DEBUG::Slave, string_compose ("slave stopped, but running, requires seekahead to %1\n", slave_wait_end));
654                                 /* we can call locate() here because we are in process context */
655                                 locate (slave_wait_end, false, false);
656                                 _slave_state = Waiting;
657
658                         } else {
659
660                                 _slave_state = Running;
661
662                                 Location* al = _locations->auto_loop_location();
663
664                                 if (al && play_loop && (slave_transport_frame < al->start() || slave_transport_frame > al->end())) {
665                                         // cancel looping
666                                         request_play_loop(false);
667                                 }
668
669                                 if (slave_transport_frame != _transport_frame) {
670                                         locate (slave_transport_frame, false, false);
671                                 }
672                         }
673                         break;
674
675                 case Waiting:
676                 default:
677                         break;
678                 }
679
680                 if (_slave_state == Waiting) {
681
682                         DEBUG_TRACE (DEBUG::Slave, string_compose ("slave waiting at %1\n", slave_transport_frame));
683
684                         if (slave_transport_frame >= slave_wait_end) {
685
686                                 DEBUG_TRACE (DEBUG::Slave, string_compose ("slave start at %1 vs %2\n", slave_transport_frame, _transport_frame));
687
688                                 _slave_state = Running;
689
690                                 /* now perform a "micro-seek" within the disk buffers to realign ourselves
691                                    precisely with the master.
692                                 */
693
694
695                                 bool ok = true;
696                                 framecnt_t frame_delta = slave_transport_frame - _transport_frame;
697
698                                 boost::shared_ptr<RouteList> rl = routes.reader();
699                                 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
700                                         boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
701                                         if (tr && !tr->can_internal_playback_seek (frame_delta)) {
702                                                 ok = false;
703                                                 break;
704                                         }
705                                 }
706
707                                 if (ok) {
708                                         for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
709                                                 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
710                                                 if (tr) {
711                                                         tr->internal_playback_seek (frame_delta);
712                                                 }
713                                         }
714                                         _transport_frame += frame_delta;
715
716                                 } else {
717                                         cerr << "cannot micro-seek\n";
718                                         /* XXX what? */
719                                 }
720
721                                 memset (delta_accumulator, 0, sizeof (int32_t) * delta_accumulator_size);
722                                 average_slave_delta = 0L;
723                         }
724                 }
725
726                 if (_slave_state == Running && _transport_speed == 0.0f) {
727                         DEBUG_TRACE (DEBUG::Slave, "slave starts transport\n");
728                         start_transport ();
729                 }
730
731         } else { // slave_speed is 0
732
733                 /* slave has stopped */
734
735                 if (_transport_speed != 0.0f) {
736                         DEBUG_TRACE (DEBUG::Slave, string_compose ("slave stops transport: %1 frame %2 tf %3\n", slave_speed, slave_transport_frame, _transport_frame));
737                         stop_transport ();
738                 }
739
740                 if (slave_transport_frame != _transport_frame) {
741                         DEBUG_TRACE (DEBUG::Slave, string_compose ("slave stopped, move to %1\n", slave_transport_frame));
742                         force_locate (slave_transport_frame, false);
743                 }
744
745                 _slave_state = Stopped;
746         }
747 }
748
749 void
750 Session::follow_slave_silently (pframes_t nframes, float slave_speed)
751 {
752         if (slave_speed && _transport_speed) {
753
754                 /* something isn't right, but we should move with the master
755                    for now.
756                 */
757
758                 bool need_butler;
759
760                 silent_process_routes (nframes, need_butler);
761
762                 get_track_statistics ();
763
764                 if (need_butler) {
765                         _butler->summon ();
766                 }
767
768                 int32_t frames_moved = (int32_t) floor (_transport_speed * nframes);
769
770                 if (frames_moved < 0) {
771                         decrement_transport_position (-frames_moved);
772                 } else {
773                         increment_transport_position (frames_moved);
774                 }
775
776                 framepos_t const stop_limit = compute_stop_limit ();
777                 maybe_stop (stop_limit);
778         }
779 }
780
781 void
782 Session::process_without_events (pframes_t nframes)
783 {
784         bool session_needs_butler = false;
785         framecnt_t frames_moved;
786
787         if (!process_can_proceed()) {
788                 _silent = true;
789                 return;
790         }
791
792         if (!_exporting && _slave) {
793                 if (!follow_slave (nframes)) {
794                         return;
795                 }
796         }
797
798         if (_transport_speed == 0) {
799                 fail_roll (nframes);
800                 return;
801         }
802
803         if (_transport_speed == 1.0) {
804                 frames_moved = (framecnt_t) nframes;
805         } else {
806                 interpolation.set_target_speed (fabs(_target_transport_speed));
807                 interpolation.set_speed (fabs(_transport_speed));
808                 frames_moved = (framecnt_t) interpolation.interpolate (0, nframes, 0, 0);
809         }
810
811         if (!_exporting && !timecode_transmission_suspended()) {
812                 send_midi_time_code_for_cycle (_transport_frame, _transport_frame + frames_moved, nframes);
813         }
814
815         framepos_t const stop_limit = compute_stop_limit ();
816
817         if (maybe_stop (stop_limit)) {
818                 fail_roll (nframes);
819                 return;
820         }
821
822         if (maybe_sync_start (nframes)) {
823                 return;
824         }
825
826         click (_transport_frame, nframes);
827
828         if (process_routes (nframes, session_needs_butler)) {
829                 fail_roll (nframes);
830                 return;
831         }
832
833         get_track_statistics ();
834
835         /* XXX: I'm not sure whether this is correct, but at least it
836            matches process_with_events, so that this new frames_moved
837            is -ve when transport speed is -ve.  This means that the
838            transport position is updated correctly when we are in
839            reverse.  It seems a bit wrong that we're not using the
840            interpolator to compute this.
841         */
842
843         frames_moved = (framecnt_t) floor (_transport_speed * nframes);
844
845         if (frames_moved < 0) {
846                 decrement_transport_position (-frames_moved);
847         } else {
848                 increment_transport_position (frames_moved);
849         }
850
851         maybe_stop (stop_limit);
852         check_declick_out ();
853
854         if (session_needs_butler) {
855                 _butler->summon ();
856         }
857 }
858
859 /** Process callback used when the auditioner is active.
860  * @param nframes number of frames to process.
861  */
862 void
863 Session::process_audition (pframes_t nframes)
864 {
865         SessionEvent* ev;
866         boost::shared_ptr<RouteList> r = routes.reader ();
867
868         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
869                 if (!(*i)->is_hidden()) {
870                         (*i)->silence (nframes);
871                 }
872         }
873
874         /* run the auditioner, and if it says we need butler service, ask for it */
875
876         if (auditioner->play_audition (nframes) > 0) {
877                 _butler->summon ();
878         }
879
880         /* if using a monitor section, run it because otherwise we don't hear anything */
881
882         if (auditioner->needs_monitor()) {
883                 _monitor_out->passthru (_transport_frame, _transport_frame + nframes, nframes, false);
884         }
885
886         /* handle pending events */
887
888         while (pending_events.read (&ev, 1) == 1) {
889                 merge_event (ev);
890         }
891
892         /* if we are not in the middle of a state change,
893            and there are immediate events queued up,
894            process them.
895         */
896
897         while (!non_realtime_work_pending() && !immediate_events.empty()) {
898                 SessionEvent *ev = immediate_events.front ();
899                 immediate_events.pop_front ();
900                 process_event (ev);
901         }
902
903         if (!auditioner->auditioning()) {
904                 /* auditioner no longer active, so go back to the normal process callback */
905                 process_function = &Session::process_with_events;
906         }
907 }
908
909 bool
910 Session::maybe_sync_start (pframes_t & nframes)
911 {
912         pframes_t sync_offset;
913
914         if (!waiting_for_sync_offset) {
915                 return false;
916         }
917
918         if (_engine.get_sync_offset (sync_offset) && sync_offset < nframes) {
919
920                 /* generate silence up to the sync point, then
921                    adjust nframes + offset to reflect whatever
922                    is left to do.
923                 */
924
925                 no_roll (sync_offset);
926                 nframes -= sync_offset;
927                 Port::increment_global_port_buffer_offset (sync_offset);
928                 waiting_for_sync_offset = false;
929
930                 if (nframes == 0) {
931                         return true; // done, nothing left to process
932                 }
933
934         } else {
935
936                 /* sync offset point is not within this process()
937                    cycle, so just generate silence. and don't bother
938                    with any fancy stuff here, just the minimal silence.
939                 */
940
941                 _silent = true;
942
943                 if (Config->get_locate_while_waiting_for_sync()) {
944                         if (micro_locate (nframes)) {
945                                 /* XXX ERROR !!! XXX */
946                         }
947                 }
948
949                 return true; // done, nothing left to process
950         }
951
952         return false;
953 }
954
955 void
956 Session::queue_event (SessionEvent* ev)
957 {
958         if (_state_of_the_state & Deletion) {
959                 return;
960         } else if (_state_of_the_state & Loading) {
961                 merge_event (ev);
962         } else {
963                 pending_events.write (&ev, 1);
964         }
965 }
966
967 void
968 Session::set_next_event ()
969 {
970         if (events.empty()) {
971                 next_event = events.end();
972                 return;
973         }
974
975         if (next_event == events.end()) {
976                 next_event = events.begin();
977         }
978
979         if ((*next_event)->action_frame > _transport_frame) {
980                 next_event = events.begin();
981         }
982
983         for (; next_event != events.end(); ++next_event) {
984                 if ((*next_event)->action_frame >= _transport_frame) {
985                         break;
986                 }
987         }
988 }
989
990 void
991 Session::process_event (SessionEvent* ev)
992 {
993         bool remove = true;
994         bool del = true;
995
996         /* if we're in the middle of a state change (i.e. waiting
997            for the butler thread to complete the non-realtime
998            part of the change), we'll just have to queue this
999            event for a time when the change is complete.
1000         */
1001
1002         if (non_realtime_work_pending()) {
1003
1004                 /* except locates, which we have the capability to handle */
1005
1006                 if (ev->type != SessionEvent::Locate) {
1007                         immediate_events.insert (immediate_events.end(), ev);
1008                         _remove_event (ev);
1009                         return;
1010                 }
1011         }
1012
1013         DEBUG_TRACE (DEBUG::SessionEvents, string_compose ("Processing event: %1 @ %2\n", enum_2_string (ev->type), _transport_frame));
1014
1015         switch (ev->type) {
1016         case SessionEvent::SetLoop:
1017                 set_play_loop (ev->yes_or_no);
1018                 break;
1019
1020         case SessionEvent::AutoLoop:
1021                 if (play_loop) {
1022                         start_locate (ev->target_frame, true, false, Config->get_seamless_loop());
1023                 }
1024                 remove = false;
1025                 del = false;
1026                 break;
1027
1028         case SessionEvent::Locate:
1029                 if (ev->yes_or_no) {
1030                         // cerr << "forced locate to " << ev->target_frame << endl;
1031                         locate (ev->target_frame, false, true, false);
1032                 } else {
1033                         // cerr << "soft locate to " << ev->target_frame << endl;
1034                         start_locate (ev->target_frame, false, true, false);
1035                 }
1036                 _send_timecode_update = true;
1037                 break;
1038
1039         case SessionEvent::LocateRoll:
1040                 if (ev->yes_or_no) {
1041                         // cerr << "forced locate to+roll " << ev->target_frame << endl;
1042                         locate (ev->target_frame, true, true, false);
1043                 } else {
1044                         // cerr << "soft locate to+roll " << ev->target_frame << endl;
1045                         start_locate (ev->target_frame, true, true, false);
1046                 }
1047                 _send_timecode_update = true;
1048                 break;
1049
1050         case SessionEvent::LocateRollLocate:
1051                 // locate is handled by ::request_roll_at_and_return()
1052                 _requested_return_frame = ev->target_frame;
1053                 request_locate (ev->target2_frame, true);
1054                 break;
1055
1056
1057         case SessionEvent::SetTransportSpeed:
1058                 set_transport_speed (ev->speed, ev->yes_or_no, ev->second_yes_or_no);
1059                 break;
1060
1061         case SessionEvent::PunchIn:
1062                 // cerr << "PunchIN at " << transport_frame() << endl;
1063                 if (config.get_punch_in() && record_status() == Enabled) {
1064                         enable_record ();
1065                 }
1066                 remove = false;
1067                 del = false;
1068                 break;
1069
1070         case SessionEvent::PunchOut:
1071                 // cerr << "PunchOUT at " << transport_frame() << endl;
1072                 if (config.get_punch_out()) {
1073                         step_back_from_record ();
1074                 }
1075                 remove = false;
1076                 del = false;
1077                 break;
1078
1079         case SessionEvent::StopOnce:
1080                 if (!non_realtime_work_pending()) {
1081                         stop_transport (ev->yes_or_no);
1082                         _clear_event_type (SessionEvent::StopOnce);
1083                 }
1084                 remove = false;
1085                 del = false;
1086                 break;
1087
1088         case SessionEvent::RangeStop:
1089                 if (!non_realtime_work_pending()) {
1090                         stop_transport (ev->yes_or_no);
1091                 }
1092                 remove = false;
1093                 del = false;
1094                 break;
1095
1096         case SessionEvent::RangeLocate:
1097                 start_locate (ev->target_frame, true, true, false);
1098                 remove = false;
1099                 del = false;
1100                 break;
1101
1102         case SessionEvent::Overwrite:
1103                 overwrite_some_buffers (static_cast<Track*>(ev->ptr));
1104                 break;
1105
1106         case SessionEvent::SetTrackSpeed:
1107                 set_track_speed (static_cast<Track*> (ev->ptr), ev->speed);
1108                 break;
1109
1110         case SessionEvent::SetSyncSource:
1111                 use_sync_source (ev->slave);
1112                 break;
1113
1114         case SessionEvent::Audition:
1115                 set_audition (ev->region);
1116                 // drop reference to region
1117                 ev->region.reset ();
1118                 break;
1119
1120         case SessionEvent::InputConfigurationChange:
1121                 add_post_transport_work (PostTransportInputChange);
1122                 _butler->schedule_transport_work ();
1123                 break;
1124
1125         case SessionEvent::SetPlayAudioRange:
1126                 set_play_range (ev->audio_range, (ev->speed == 1.0f));
1127                 break;
1128
1129         case SessionEvent::RealTimeOperation:
1130                 process_rtop (ev);
1131                 del = false; // other side of RT request needs to clean up
1132                 break;
1133
1134         case SessionEvent::AdjustPlaybackBuffering:
1135                 schedule_playback_buffering_adjustment ();
1136                 break;
1137
1138         case SessionEvent::AdjustCaptureBuffering:
1139                 schedule_capture_buffering_adjustment ();
1140                 break;
1141
1142         case SessionEvent::SetTimecodeTransmission:
1143                 g_atomic_int_set (&_suspend_timecode_transmission, ev->yes_or_no ? 0 : 1);
1144                 break;
1145
1146         default:
1147           fatal << string_compose(_("Programming error: illegal event type in process_event (%1)"), ev->type) << endmsg;
1148                 /*NOTREACHED*/
1149                 break;
1150         };
1151
1152         if (remove) {
1153                 del = del && !_remove_event (ev);
1154         }
1155
1156         if (del) {
1157                 delete ev;
1158         }
1159 }
1160
1161 framepos_t
1162 Session::compute_stop_limit () const
1163 {
1164         if (!Config->get_stop_at_session_end ()) {
1165                 return max_framepos;
1166         }
1167         
1168         bool const punching_in = (config.get_punch_in () && _locations->auto_punch_location());
1169         bool const punching_out = (config.get_punch_out () && _locations->auto_punch_location());
1170
1171         if (actively_recording ()) {
1172                 /* permanently recording */
1173                 return max_framepos;
1174         } else if (punching_in && !punching_out) {
1175                 /* punching in but never out */
1176                 return max_framepos;
1177         } else if (punching_in && punching_out && _locations->auto_punch_location()->end() > current_end_frame()) {
1178                 /* punching in and punching out after session end */
1179                 return max_framepos;
1180         }
1181
1182         return current_end_frame ();
1183 }