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