pulling trunk
[ardour.git] / libs / ardour / session_midi.cc
index 18d8b3672b1a46ab71a2fe4e3a57c5987f67da9b..d8382c69a4c36b7d9e0b1f33d2bf18310aeae65a 100644 (file)
@@ -30,7 +30,7 @@
 #include <midi++/port.h>
 #include <midi++/manager.h>
 #include <pbd/error.h>
-#include <pbd/lockmonitor.h>
+#include <glibmm/thread.h>
 #include <pbd/pthread_utils.h>
 
 #include <ardour/configuration.h>
@@ -110,7 +110,7 @@ Session::set_midi_control (bool yn)
        poke_midi_thread ();
 
        if (_midi_port) {
-               RWLockMonitor lm (route_lock, false, __LINE__, __FILE__);
+               Glib::RWLock::ReaderLock guard (route_lock);
                for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
                        (*i)->reset_midi_control (_midi_port, midi_control);
                }
@@ -166,6 +166,17 @@ Session::set_send_mmc (bool yn)
        ControlChanged (SendMMC); /* EMIT SIGNAL */
 }
 
+void
+Session::set_midi_feedback (bool yn)
+{
+}
+
+bool
+Session::get_midi_feedback () const
+{
+       return false;
+}
+
 bool
 Session::get_send_mtc () const
 {
@@ -308,6 +319,10 @@ Session::set_midi_port (string port_tag)
        }
 
        _midi_port = port;
+       
+       /* XXX need something to forward this to control protocols ? or just
+          use the signal below 
+       */
 
        Config->set_midi_port_name (port_tag);
 
@@ -449,42 +464,6 @@ Session::get_trace_midi_output(MIDI::Port *port)
 
 }
 
-
-void
-Session::set_midi_feedback (bool yn)
-{
-       if (_midi_port == 0) {
-               return;
-       }
-       
-       midi_feedback = yn;
-       set_dirty();
-
-       if (yn) {
-               /* make sure the feedback thread is alive */
-               start_feedback ();
-       } else {
-               /* maybe put the feedback thread to sleep */
-               stop_feedback ();
-       }
-
-       ControlChanged (MidiFeedback); /* EMIT SIGNAL */
-
-       send_all_midi_feedback ();
-}
-
-void
-Session::send_all_midi_feedback ()
-{
-       if (midi_feedback) {
-               // send out current state of all routes
-               RWLockMonitor lm (route_lock, false, __LINE__, __FILE__);
-               for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
-                       (*i)->send_all_midi_feedback ();
-               }
-       }
-}
-
 void
 Session::setup_midi_control ()
 {
@@ -622,8 +601,8 @@ Session::mmc_record_strobe (MIDI::MachineControl &mmc)
                */
                
                save_state ("", true);
-               atomic_set (&_record_status, Enabled);
-               RecordEnabled (); /* EMIT SIGNAL */
+               g_atomic_int_set (&_record_status, Enabled);
+               RecordStateChanged (); /* EMIT SIGNAL */
                
                request_transport_speed (1.0);
 
@@ -637,7 +616,7 @@ void
 Session::mmc_record_exit (MIDI::MachineControl &mmc)
 {
        if (mmc_control) {
-               disable_record ();
+               disable_record (false);
        }
 }
 
@@ -803,19 +782,14 @@ Session::mmc_record_enable (MIDI::MachineControl &mmc, size_t trk, bool enabled)
 {
        if (mmc_control) {
 
-               /* don't take route or diskstream lock: if using dynamic punch,
-                  this could cause a dropout. XXX is that really OK?
-                  or should we queue a rec-enable request?
-               */
-
-               size_t n;
                RouteList::iterator i;
-
-               for (n = 0, i = routes.begin(); i != routes.end(); ++i) {
+               Glib::RWLock::ReaderLock guard (route_lock);
+               
+               for (i = routes.begin(); i != routes.end(); ++i) {
                        AudioTrack *at;
 
                        if ((at = dynamic_cast<AudioTrack*>(*i)) != 0) {
-                               if (n++ == trk) {
+                               if (trk == at->remote_control_id()) {
                                        at->set_record_enable (enabled, &mmc);
                                        break;
                                }
@@ -915,6 +889,9 @@ Session::send_full_time_code ()
                }
        }
 
+       // Compensate for audio latency
+       outbound_mtc_smpte_frame += _worst_output_latency;
+
        next_quarter_frame_to_send = 0;
 
        // Sync slave to the same smpte time as we are on (except if negative, see above)
@@ -931,7 +908,7 @@ Session::send_full_time_code ()
        msg[8] = smpte.frames;
 
        {
-               LockMonitor lm (midi_lock, __LINE__, __FILE__);
+               Glib::Mutex::Lock lm (midi_lock);
     
                if (_mtc_port->midimsg (msg, sizeof (msg))) {
                        error << _("Session: could not send full MIDI time code") << endmsg;
@@ -963,7 +940,7 @@ Session::send_midi_time_code ()
 
                // Send quarter frames up to current time
                {
-                       LockMonitor lm (midi_lock, __LINE__, __FILE__);
+                       Glib::Mutex::Lock lm (midi_lock);
 
                        switch(next_quarter_frame_to_send) {
                        case 0:
@@ -1012,6 +989,8 @@ Session::send_midi_time_code ()
                                smpte_increment( transmitting_smpte_time );        
                                // Re-calculate timing of first quarter frame
                                smpte_to_sample( transmitting_smpte_time, outbound_mtc_smpte_frame, true /* use_offset */, false );
+                               // Compensate for audio latency
+                               outbound_mtc_smpte_frame += _worst_output_latency;
                        }
                }
        }
@@ -1096,7 +1075,7 @@ Session::deliver_mmc (MIDI::MachineControl::Command cmd, jack_nframes_t where)
 
                mmc_buffer[nbytes++] = 0xf7; // terminate SysEx/MMC message
 
-               LockMonitor lm (midi_lock, __LINE__, __FILE__);
+               Glib::Mutex::Lock lm (midi_lock);
 
                if (_mmc_port->write (mmc_buffer, nbytes) != nbytes) {
                        error << string_compose(_("MMC: cannot send command %1%2%3"), &hex, cmd, &dec) << endmsg;
@@ -1272,7 +1251,7 @@ Session::midi_thread_work ()
        bool restart;
        vector<MIDI::Port*> ports;
 
-       PBD::ThreadCreated (pthread_self(), X_("MIDI"));
+       PBD::ThreadCreatedWithRequestSize (pthread_self(), X_("MIDI"), 2048);
 
        memset (&rtparam, 0, sizeof (rtparam));
        rtparam.sched_priority = 9; /* XXX should be relative to audio (JACK) thread */
@@ -1468,7 +1447,7 @@ Session::midi_thread_work ()
                                tmp = i;
                                ++tmp;
                                
-                               if (!(*i)) {
+                               if (!(*i)()) {
                                        midi_timeouts.erase (i);
                                }
                                
@@ -1483,14 +1462,7 @@ Session::get_mmc_control () const
 {
        return mmc_control;
 }
-bool
-Session::get_midi_feedback () const
-{
-       /* since this a "write" function we have to check the port as well 
-          as the control toggle.
-       */
-       return _midi_port && midi_feedback;
-}
+
 bool
 Session::get_midi_control () const
 {