Remove unnecessary 0 checks before delete; see http://www.parashift.com/c++-faq-lite...
[ardour.git] / libs / ardour / session_process.cc
index 09d85097d09ea981bfda17c6e529190546074810..c2e78df643e4203cca9abe80ffdfd7ac9949ac10 100644 (file)
@@ -50,16 +50,14 @@ using namespace std;
 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);
 
        _silent = false;
 
-       if (synced_to_jack() && waiting_to_start) {
-               if ( _engine.transport_state() == AudioEngine::TransportRolling) {
-                       actually_start_transport ();
-               }
-       }
-
        if (non_realtime_work_pending()) {
                if (!transport_work_requested ()) {
                        post_transport ();
@@ -68,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
@@ -336,7 +342,7 @@ Session::process_with_events (nframes_t nframes)
                }
        
                if (!_exporting) {
-                       send_midi_time_code_for_cycle(nframes);
+                       send_midi_time_code_for_cycle (nframes);
                }
 
                if (actively_recording()) {
@@ -521,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;
                        }
@@ -608,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;
                        }
                }
@@ -669,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
@@ -735,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;
 }
@@ -764,7 +773,7 @@ Session::process_without_events (nframes_t nframes)
        }
                
        if (!_exporting) {
-               send_midi_time_code_for_cycle(nframes);
+               send_midi_time_code_for_cycle (nframes);
        }
 
        if (actively_recording()) {
@@ -867,18 +876,38 @@ Session::maybe_sync_start (nframes_t& nframes, nframes_t& offset)
 
        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;