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