Fix several MIDI timestamp related problems:
[ardour.git] / libs / ardour / session_transport.cc
index 9452cb3558e0b798c6d031c34e4a345219eebea2..2776fbf41dac5135ed0ee7d10bea7a6e23fa5445 100644 (file)
@@ -15,7 +15,6 @@
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
-    $Id$
 */
 
 #include <cmath>
@@ -135,6 +134,11 @@ void
 Session::realtime_stop (bool abort)
 {
        /* assume that when we start, we'll be moving forwards */
+       
+       // FIXME: where should this really be? [DR]
+       //send_full_time_code();
+       deliver_mmc (MIDI::MachineControl::cmdStop, _transport_frame);
+       deliver_mmc (MIDI::MachineControl::cmdLocate, _transport_frame);
 
        if (_transport_speed < 0.0f) {
                post_transport_work = PostTransportWork (post_transport_work | PostTransportStop | PostTransportReverse);
@@ -176,7 +180,7 @@ Session::realtime_stop (bool abort)
                waiting_for_sync_offset = true;
        }
 
-       transport_sub_state = (Config->get_auto_return() ? AutoReturning : 0);
+       transport_sub_state = ((Config->get_slave_source() == None && Config->get_auto_return()) ? AutoReturning : 0);
 }
 
 void
@@ -234,8 +238,12 @@ Session::butler_transport_work ()
                        }
                }
        }
+       
+       if (post_transport_work & PostTransportLocate) {
+               non_realtime_locate ();
+       }
 
-       if (post_transport_work & (PostTransportStop|PostTransportLocate)) {
+       if (post_transport_work & PostTransportStop) {
                non_realtime_stop (post_transport_work & PostTransportAbort, on_entry, finished);
                if (!finished) {
                        g_atomic_int_dec_and_test (&butler_should_do_transport_work);
@@ -284,6 +292,18 @@ Session::non_realtime_overwrite (int on_entry, bool& finished)
        }
 }
 
+       
+void
+Session::non_realtime_locate ()
+{
+       boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
+
+       for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
+               (*i)->non_realtime_locate (_transport_frame);
+       }
+}
+
+
 void
 Session::non_realtime_stop (bool abort, int on_entry, bool& finished)
 {
@@ -359,7 +379,7 @@ Session::non_realtime_stop (bool abort, int on_entry, bool& finished)
        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)->set_pending_declick (0);
                }
        }
@@ -372,17 +392,18 @@ Session::non_realtime_stop (bool abort, int on_entry, bool& finished)
                update_latency_compensation (true, abort);
        }
 
-       if (Config->get_auto_return() || (post_transport_work & PostTransportLocate) || synced_to_jack()) {
+       if ((Config->get_slave_source() == None && Config->get_auto_return()) || (post_transport_work & PostTransportLocate) || synced_to_jack()) {
                
                if (pending_locate_flush) {
-                       flush_all_redirects ();
+                       flush_all_inserts ();
                }
-
-               if ((Config->get_auto_return() || synced_to_jack()) && !(post_transport_work & PostTransportLocate)) {
+               
+               if (((Config->get_slave_source() == None && Config->get_auto_return()) || synced_to_jack()) && !(post_transport_work & PostTransportLocate)) {
 
                        _transport_frame = last_stop_frame;
 
-                       if (synced_to_jack()) {
+                       if (synced_to_jack() && !play_loop) {
+                               // cerr << "non-realtimestop: transport locate to " << _transport_frame << endl;
                                _engine.transport_locate (_transport_frame);
                        }
                } 
@@ -406,29 +427,28 @@ Session::non_realtime_stop (bool abort, int on_entry, bool& finished)
                                return;
                        }
                }
+
 #ifdef LEAVE_TRANSPORT_UNADJUSTED
        }
 #endif
 
        last_stop_frame = _transport_frame;
 
-       send_full_time_code ();
-       deliver_mmc (MIDI::MachineControl::cmdStop, 0);
-       deliver_mmc (MIDI::MachineControl::cmdLocate, _transport_frame);
-
        if (did_record) {
 
                /* XXX its a little odd that we're doing this here
                   when realtime_stop(), which has already executed,
                   will have done this.
+                  JLC - so let's not because it seems unnecessary and breaks loop record
                */
-               
+#if 0
                if (!Config->get_latched_record_enable()) {
                        g_atomic_int_set (&_record_status, Disabled);
                } else {
                        g_atomic_int_set (&_record_status, Enabled);
                }
                RecordStateChanged (); /* emit signal */
+#endif
        }
        
        if ((post_transport_work & PostTransportLocate) && get_record_enabled()) {
@@ -572,12 +592,12 @@ Session::set_play_loop (bool yn)
 }
 
 void
-Session::flush_all_redirects ()
+Session::flush_all_inserts ()
 {
        boost::shared_ptr<RouteList> r = routes.reader ();
 
        for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
-               (*i)->flush_redirects ();
+               (*i)->flush_processors ();
        }
 }
 
@@ -614,7 +634,7 @@ Session::start_locate (nframes_t target_frame, bool with_roll, bool with_flush,
 void
 Session::locate (nframes_t target_frame, bool with_roll, bool with_flush, bool with_loop)
 {
-       if (actively_recording()) {
+       if (actively_recording() && !with_loop) {
                return;
        }
 
@@ -626,7 +646,12 @@ Session::locate (nframes_t target_frame, bool with_roll, bool with_flush, bool w
                return;
        }
 
+       // Update SMPTE time
+       // [DR] FIXME: find out exactly where this should go below
        _transport_frame = target_frame;
+       smpte_time(_transport_frame, transmitting_smpte_time);
+       outbound_mtc_smpte_frame = _transport_frame;
+       next_quarter_frame_to_send = 0;
 
        if (_transport_speed && (!with_loop || loop_changing)) {
                /* schedule a declick. we'll be called again when its done */
@@ -640,7 +665,7 @@ Session::locate (nframes_t target_frame, bool with_roll, bool with_flush, bool w
                } 
        }
 
-       if (transport_rolling() && !Config->get_auto_play() && !with_roll && !(synced_to_jack() && play_loop)) {
+       if (transport_rolling() && (!auto_play_legal || !Config->get_auto_play()) && !with_roll && !(synced_to_jack() && play_loop)) {
                realtime_stop (false);
        } 
 
@@ -656,8 +681,6 @@ Session::locate (nframes_t target_frame, bool with_roll, bool with_flush, bool w
 
        } else {
 
-               cerr << "butler not requested\n";
-
                /* this is functionally what clear_clicks() does but with a tentative lock */
 
                Glib::RWLock::WriterLock clickm (click_lock, Glib::TRY_LOCK);
@@ -707,11 +730,33 @@ Session::locate (nframes_t target_frame, bool with_roll, bool with_flush, bool w
                        // cancel looping directly, this is called from event handling context
                        set_play_loop (false);
                }
+               else if (al && _transport_frame == al->start()) {
+                       if (with_loop) {
+                               // this is only necessary for seamless looping
+
+                               boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
+                               
+                               for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
+                                       if ((*i)->record_enabled ()) {
+                                               // tell it we've looped, so it can deal with the record state
+                                               (*i)->transport_looped(_transport_frame);
+                                       }
+                               }
+                       }
+
+                       TransportLooped(); // EMIT SIGNAL
+               }
        }
        
        loop_changing = false;
+
+       _send_smpte_update = true;
 }
 
+/** Set the transport speed.
+ * @param speed New speed
+ * @param abort
+ */
 void
 Session::set_transport_speed (float speed, bool abort)
 {
@@ -727,6 +772,8 @@ Session::set_transport_speed (float speed, bool abort)
 
        if (transport_rolling() && speed == 0.0) {
 
+               /* we are rolling and we want to stop */
+
                if (Config->get_monitoring_model() == HardwareMonitoring)
                {
                        boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
@@ -747,6 +794,8 @@ Session::set_transport_speed (float speed, bool abort)
                
        } else if (transport_stopped() && speed == 1.0) {
 
+               /* we are stopped and we want to start rolling at speed 1 */
+
                if (!get_record_enabled() && Config->get_stop_at_session_end() && _transport_frame >= current_end_frame()) {
                        return;
                }
@@ -799,7 +848,7 @@ Session::set_transport_speed (float speed, bool abort)
                   before the last stop, then we have to do extra work.
                */
 
-               if ((_transport_speed && speed * _transport_speed < 0.0f) || (_last_transport_speed * speed < 0.0f)) {
+               if ((_transport_speed && speed * _transport_speed < 0.0f) || (_last_transport_speed * speed < 0.0f) || (_last_transport_speed == 0.0f && speed < 0.0f)) {
                        post_transport_work = PostTransportWork (post_transport_work | PostTransportReverse);
                }
                
@@ -819,6 +868,8 @@ Session::set_transport_speed (float speed, bool abort)
        }
 }
 
+
+/** Stop the transport.  */
 void
 Session::stop_transport (bool abort)
 {
@@ -877,7 +928,9 @@ Session::start_transport ()
                break;
 
        case Recording:
-               disable_record (false);
+               if (!play_loop) {
+                       disable_record (false);
+               }
                break;
 
        default:
@@ -904,11 +957,14 @@ Session::actually_start_transport ()
                (*i)->realtime_set_speed ((*i)->speed(), true);
        }
 
-       send_mmc_in_another_thread (MIDI::MachineControl::cmdDeferredPlay, 0);
-       
+       deliver_mmc(MIDI::MachineControl::cmdDeferredPlay, _transport_frame);
+
        TransportStateChange (); /* EMIT SIGNAL */
 }
 
+/** Do any transport work in the audio thread that needs to be done after the
+ * transport thread is finished.  Audio thread, realtime safe.
+ */
 void
 Session::post_transport ()
 {
@@ -927,7 +983,7 @@ Session::post_transport ()
 
        if (post_transport_work & PostTransportLocate) {
 
-               if ((Config->get_auto_play() && !_exporting) || (post_transport_work & PostTransportRoll)) {
+               if (((Config->get_slave_source() == None && (auto_play_legal && Config->get_auto_play())) && !_exporting) || (post_transport_work & PostTransportRoll)) {
                        start_transport ();
                        
                } else {
@@ -1208,33 +1264,33 @@ Session::update_latency_compensation (bool with_stop, bool abort)
        boost::shared_ptr<RouteList> r = routes.reader ();
 
        for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
+               
                if (with_stop) {
                        (*i)->handle_transport_stopped (abort, (post_transport_work & PostTransportLocate), 
                                                        (!(post_transport_work & PostTransportLocate) || pending_locate_flush));
                }
-
+               
                nframes_t old_latency = (*i)->signal_latency ();
                nframes_t track_latency = (*i)->update_total_latency ();
-
+               
                if (old_latency != track_latency) {
+                       (*i)->update_port_total_latencies ();
                        update_jack = true;
                }
-               
-               if (!(*i)->hidden() && ((*i)->active())) {
+
+               if (!(*i)->is_hidden() && ((*i)->active())) {
                        _worst_track_latency = max (_worst_track_latency, track_latency);
                }
+       } 
+
+       if (update_jack) {
+               _engine.update_total_latencies ();
        }
 
        for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
                (*i)->set_latency_delay (_worst_track_latency);
        }
 
-       /* tell JACK to play catch up */
-
-       if (update_jack) {
-               _engine.update_total_latencies ();
-       }
-
        set_worst_io_latencies ();
 
        /* reflect any changes in latencies into capture offsets
@@ -1248,7 +1304,17 @@ Session::update_latency_compensation (bool with_stop, bool abort)
 }
 
 void
-Session::update_latency_compensation_proxy (void* ignored)
+Session::allow_auto_play (bool yn)
 {
-       update_latency_compensation (false, false);
+       auto_play_legal = yn;
+}
+
+void
+Session::reset_jack_connection (jack_client_t* jack)
+{
+       JACK_Slave* js;
+
+       if (_slave && ((js = dynamic_cast<JACK_Slave*> (_slave)) != 0)) {
+               js->reset_client (jack);
+       }
 }