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