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