Remove unnecessary 0 checks before delete; see http://www.parashift.com/c++-faq-lite...
[ardour.git] / libs / ardour / session_process.cc
index 4f82de1c0ed1917b23462d2bc1ec3ef8bb46140a..c2e78df643e4203cca9abe80ffdfd7ac9949ac10 100644 (file)
@@ -44,16 +44,19 @@ using namespace ARDOUR;
 using namespace PBD;
 using namespace std;
 
+/** Called by the audio engine when there is work to be done with JACK.
+ * @param nframes Number of frames to process.
+ */
 void
 Session::process (nframes_t nframes)
 {
+       // This is no more the appriopriate place to call cycle
+       // start. cycle_start needs to be called at the Route::roll()
+       // where the signals which we want to mixdown have been calculated.
+       //
        MIDI::Manager::instance()->cycle_start(nframes);
 
-       if (synced_to_jack() && waiting_to_start) {
-               if ( _engine.transport_state() == AudioEngine::TransportRolling) {
-                       actually_start_transport ();
-               }
-       }
+       _silent = false;
 
        if (non_realtime_work_pending()) {
                if (!transport_work_requested ()) {
@@ -63,9 +66,17 @@ Session::process (nframes_t nframes)
        
        (this->*process_function) (nframes);
        
-       MIDI::Manager::instance()->cycle_end();
-
+       // the ticker is for sending time information like MidiClock
+       nframes_t transport_frames = transport_frame();
+       BBT_Time  transport_bbt;
+       bbt_time(transport_frames, transport_bbt);
+       SMPTE::Time transport_smpte;
+       smpte_time(transport_frames, transport_smpte);
+       tick (transport_frames, transport_bbt, transport_smpte); /* EMIT SIGNAL */
+       
        SendFeedback (); /* EMIT SIGNAL */
+       
+       MIDI::Manager::instance()->cycle_end();
 }
 
 void
@@ -98,7 +109,7 @@ Session::no_roll (nframes_t nframes, nframes_t offset)
 
        for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
                
-               if ((*i)->hidden()) {
+               if ((*i)->is_hidden()) {
                        continue;
                }
                
@@ -137,7 +148,7 @@ Session::process_routes (nframes_t nframes, nframes_t offset)
 
                int ret;
 
-               if ((*i)->hidden()) {
+               if ((*i)->is_hidden()) {
                        continue;
                }
 
@@ -183,7 +194,7 @@ Session::silent_process_routes (nframes_t nframes, nframes_t offset)
 
                int ret;
 
-               if ((*i)->hidden()) {
+               if ((*i)->is_hidden()) {
                        continue;
                }
 
@@ -255,7 +266,7 @@ Session::commit_diskstreams (nframes_t nframes, bool &needs_butler)
        }
 }
 
-
+/** Process callback used when the auditioner is not active */
 void
 Session::process_with_events (nframes_t nframes)
 {
@@ -263,10 +274,10 @@ Session::process_with_events (nframes_t nframes)
        nframes_t this_nframes;
        nframes_t end_frame;
        nframes_t offset;
+       bool session_needs_butler = false;
        nframes_t stop_limit;
        long           frames_moved;
-       bool           session_needs_butler = false;
-
+       
        /* make sure the auditioner is silent */
 
        if (auditioner) {
@@ -299,7 +310,7 @@ Session::process_with_events (nframes_t nframes)
        }
 
        if (!process_can_proceed()) {
-               no_roll (nframes, 0);
+               _silent = true;
                return;
        }
 
@@ -314,8 +325,8 @@ Session::process_with_events (nframes_t nframes)
                Event* this_event;
                Events::iterator the_next_one;
                
-               if (post_transport_work & (PostTransportLocate|PostTransportStop)) {
-                       no_roll (nframes, 0);
+               if (!process_can_proceed()) {
+                       _silent = true;
                        return;
                }
                
@@ -330,7 +341,9 @@ Session::process_with_events (nframes_t nframes)
                        return;
                }
        
-               send_midi_time_code_for_cycle(nframes);
+               if (!_exporting) {
+                       send_midi_time_code_for_cycle (nframes);
+               }
 
                if (actively_recording()) {
                        stop_limit = max_frames;
@@ -354,6 +367,8 @@ Session::process_with_events (nframes_t nframes)
 
                offset = 0;
 
+               /* yes folks, here it is, the actual loop where we really truly
+                  process some audio */
                while (nframes) {
 
                        this_nframes = nframes; /* real (jack) time relative */
@@ -484,12 +499,12 @@ Session::follow_slave (nframes_t nframes, nframes_t offset)
        cerr << "delta = " << (int) (dir * this_delta)
             << " speed = " << slave_speed 
             << " ts = " << _transport_speed 
-            << " M@"<< slave_transport_frame << " S@" << _transport_frame 
+            << " M@ "<< slave_transport_frame << " S@ " << _transport_frame 
             << " avgdelta = " << average_slave_delta 
             << endl;
 #endif 
 
-       if (Config->get_timecode_source_is_synced()) {
+       if (_slave->is_always_synced() || Config->get_timecode_source_is_synced()) {
 
                /* if the TC source is synced, then we assume that its 
                   speed is binary: 0.0 or 1.0
@@ -512,18 +527,18 @@ Session::follow_slave (nframes_t nframes, nframes_t offset)
                }
 
                if (delta_accumulator_cnt != 0 || this_delta < _current_frame_rate) {
-                       delta_accumulator[delta_accumulator_cnt++] = dir*this_delta;
+                       delta_accumulator[delta_accumulator_cnt++] = long(dir) * long(this_delta);
                }
                
                if (have_first_delta_accumulator) {
-                       average_slave_delta = 0;
+                       average_slave_delta = 0L;
                        for (int i = 0; i < delta_accumulator_size; ++i) {
                                average_slave_delta += delta_accumulator[i];
                        }
-                       average_slave_delta /= delta_accumulator_size;
-                       if (average_slave_delta < 0) {
+                       average_slave_delta /= long(delta_accumulator_size);
+                       if (average_slave_delta < 0L) {
                                average_dir = -1;
-                               average_slave_delta = -average_slave_delta;
+                               average_slave_delta = abs(average_slave_delta);
                        } else {
                                average_dir = 1;
                        }
@@ -599,8 +614,8 @@ Session::follow_slave (nframes_t nframes, nframes_t offset)
                                        /* XXX what? */
                                }
 
-                               memset (delta_accumulator, 0, sizeof (nframes_t) * delta_accumulator_size);
-                               average_slave_delta = 0;
+                               memset (delta_accumulator, 0, sizeof (long) * delta_accumulator_size);
+                               average_slave_delta = 0L;
                                this_delta = 0;
                        }
                }
@@ -637,7 +652,7 @@ Session::follow_slave (nframes_t nframes, nframes_t offset)
                slave_state = Stopped;
        }
 
-       if (slave_state == Running && !Config->get_timecode_source_is_synced()) {
+       if (slave_state == Running && !_slave->is_always_synced() && !Config->get_timecode_source_is_synced()) {
 
 
                if (_transport_speed != 0.0f) {
@@ -660,18 +675,20 @@ Session::follow_slave (nframes_t nframes, nframes_t offset)
                        float adjusted_speed = slave_speed +
                                (delta / (adjust_seconds * _current_frame_rate));
                        
-                       // cerr << "adjust using " << delta
-                       // << " towards " << adjusted_speed
-                       // << " ratio = " << adjusted_speed / slave_speed
-                       // << " current = " << _transport_speed
-                       // << " slave @ " << slave_speed
-                       // << endl;
+#if 0
+                       cerr << "adjust using " << delta
+                            << " towards " << adjusted_speed
+                            << " ratio = " << adjusted_speed / slave_speed
+                            << " current = " << _transport_speed
+                            << " slave @ " << slave_speed
+                            << endl;
+#endif
                        
                        request_transport_speed (adjusted_speed);
                        
 #if 1
-                       if ((nframes_t) average_slave_delta > _slave->resolution()) {
-                               // cerr << "not locked\n";
+                       if (abs(average_slave_delta) > (long) _slave->resolution()) {
+                               cerr << "average slave delta greater than slave resolution, going to silent motion\n";
                                goto silent_motion;
                        }
 #endif
@@ -726,6 +743,7 @@ Session::follow_slave (nframes_t nframes, nframes_t offset)
 
   noroll:
        /* don't move at all */
+  cerr << "********* noroll" << endl;
        no_roll (nframes, 0);
        return false;
 }
@@ -738,72 +756,74 @@ Session::process_without_events (nframes_t nframes)
        long frames_moved;
        nframes_t offset = 0;
 
-       {
-               if (post_transport_work & (PostTransportLocate|PostTransportStop)) {
-                       no_roll (nframes, 0);
-                       return;
-               }
-
-               if (!_exporting && _slave) {
-                       if (!follow_slave (nframes, 0)) {
-                               return;
-                       }
-               } 
+       if (!process_can_proceed()) {
+               _silent = true;
+               return;
+       }
 
-               if (_transport_speed == 0) {
-                       no_roll (nframes, 0);
+       if (!_exporting && _slave) {
+               if (!follow_slave (nframes, 0)) {
                        return;
                }
-       
-               send_midi_time_code_for_cycle(nframes);
+       } 
+
+       if (_transport_speed == 0) {
+               no_roll (nframes, 0);
+               return;
+       }
                
-               if (actively_recording()) {
-                       stop_limit = max_frames;
+       if (!_exporting) {
+               send_midi_time_code_for_cycle (nframes);
+       }
+
+       if (actively_recording()) {
+               stop_limit = max_frames;
+       } else {
+               if (Config->get_stop_at_session_end()) {
+                       stop_limit = current_end_frame();
                } else {
-                       if (Config->get_stop_at_session_end()) {
-                               stop_limit = current_end_frame();
-                       } else {
-                               stop_limit = max_frames;
-                       }
+                       stop_limit = max_frames;
                }
+       }
                
-               if (maybe_stop (stop_limit)) {
-                       no_roll (nframes, 0);
-                       return;
-               
+       if (maybe_stop (stop_limit)) {
+               no_roll (nframes, 0);
+               return;
+       } 
 
-               if (maybe_sync_start (nframes, offset)) {
-                       return;
-               }
+       if (maybe_sync_start (nframes, offset)) {
+               return;
+       }
 
-               click (_transport_frame, nframes, offset);
+       click (_transport_frame, nframes, offset);
 
-               prepare_diskstreams ();
+       prepare_diskstreams ();
        
-               frames_moved = (long) floor (_transport_speed * nframes);
-
-               if (process_routes (nframes, offset)) {
-                       no_roll (nframes, offset);
-                       return;
-               }
+       frames_moved = (long) floor (_transport_speed * nframes);
 
-               commit_diskstreams (nframes, session_needs_butler);
+       if (process_routes (nframes, offset)) {
+               no_roll (nframes, offset);
+               return;
+       }
 
-               if (frames_moved < 0) {
-                       decrement_transport_position (-frames_moved);
-               } else {
-                       increment_transport_position (frames_moved);
-               }
+       commit_diskstreams (nframes, session_needs_butler);
 
-               maybe_stop (stop_limit);
-               check_declick_out ();
+       if (frames_moved < 0) {
+               decrement_transport_position (-frames_moved);
+       } else {
+               increment_transport_position (frames_moved);
+       }
 
-       } /* implicit release of route lock */
+       maybe_stop (stop_limit);
+       check_declick_out ();
 
        if (session_needs_butler)
                summon_butler ();
 }
 
+/** Process callback used when the auditioner is active.
+ * @param nframes number of frames to process.
+ */
 void
 Session::process_audition (nframes_t nframes)
 {
@@ -811,7 +831,7 @@ Session::process_audition (nframes_t nframes)
        boost::shared_ptr<RouteList> r = routes.reader ();
 
        for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
-               if (!(*i)->hidden()) {
+               if (!(*i)->is_hidden()) {
                        (*i)->silence (nframes, 0);
                }
        }
@@ -840,6 +860,7 @@ Session::process_audition (nframes_t nframes)
        }
 
        if (!auditioner->active()) {
+               /* auditioner no longer active, so go back to the normal process callback */
                process_function = &Session::process_with_events;
        }
 }
@@ -848,25 +869,45 @@ bool
 Session::maybe_sync_start (nframes_t& nframes, nframes_t& offset)
 {
        nframes_t sync_offset;
-       
+
        if (!waiting_for_sync_offset) {
                return false;
        }
 
        if (_engine.get_sync_offset (sync_offset) && sync_offset < nframes) {
 
+               /* generate silence up to the sync point, then
+                  adjust nframes + offset to reflect whatever
+                  is left to do.
+               */
+
                no_roll (sync_offset, 0);
                nframes -= sync_offset;
                offset += sync_offset;
                waiting_for_sync_offset = false;
                
                if (nframes == 0) {
-                       return true; // done
+                       return true; // done, nothing left to process
                }
                
        } else {
+
+               /* sync offset point is not within this process()
+                  cycle, so just generate silence. and don't bother 
+                  with any fancy stuff here, just the minimal silence.
+               */
+
+               g_atomic_int_inc (&processing_prohibited);
                no_roll (nframes, 0);
-               return true; // done
+               g_atomic_int_dec_and_test (&processing_prohibited);
+
+               if (Config->get_locate_while_waiting_for_sync()) {
+                       if (micro_locate (nframes)) {
+                               /* XXX ERROR !!! XXX */
+                       }
+               }
+
+               return true; // done, nothing left to process
        }
 
        return false;