new automation state model, sort of working, but not really
[ardour.git] / libs / ardour / session_midi.cc
index aa079f88278e4b9070ccc77e7116d6b4c94a2a58..fe08b4012721e0c9f7c699e1d0d84362ae50e10a 100644 (file)
 #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>
 #include <ardour/audioengine.h>
 #include <ardour/session.h>
 #include <ardour/audio_track.h>
-#include <ardour/diskstream.h>
+#include <ardour/audio_diskstream.h>
 #include <ardour/slave.h>
 #include <ardour/cycles.h>
 
@@ -45,7 +45,7 @@
 
 using namespace std;
 using namespace ARDOUR;
-//using namespace sigc;
+using namespace PBD;
 using namespace MIDI;
 
 MachineControl::CommandSignature MMC_CommandSignature;
@@ -84,111 +84,6 @@ Session::use_config_midi_ports ()
  MTC, MMC, etc.
 **********************************************************************/
 
-void
-Session::set_mmc_control (bool yn)
-{
-       if (mmc_control == yn) {
-               return;
-       }
-       
-       mmc_control = yn;
-       set_dirty();
-       poke_midi_thread ();
-       
-       ControlChanged (MMCControl); /* EMIT SIGNAL */
-}
-
-void
-Session::set_midi_control (bool yn)
-{
-       if (midi_control == yn) {
-               return;
-       }
-
-       midi_control = yn;
-       set_dirty();
-       poke_midi_thread ();
-
-       if (_midi_port) {
-               RWLockMonitor lm (route_lock, false, __LINE__, __FILE__);
-               for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
-                       (*i)->reset_midi_control (_midi_port, midi_control);
-               }
-       }
-
-       ControlChanged (MidiControl); /* EMIT SIGNAL */
-}
-
-void
-Session::set_send_mtc (bool yn)
-{
-       /* set the persistent option value regardless */
-
-       send_midi_timecode = yn;
-       set_dirty();
-
-       /* only set the internal flag if we have
-          a port.
-       */
-
-       if (_mtc_port == 0 || send_mtc == yn) {
-               return;
-       }
-
-       send_mtc = yn;
-       ControlChanged (SendMTC); /* EMIT SIGNAL */
-}
-
-void
-Session::set_send_mmc (bool yn)
-{
-       if (_mmc_port == 0) {
-               return;
-       }
-
-       if (send_midi_machine_control == yn) {
-               return;
-       }
-
-       /* only set the internal flag if we have
-          a port.
-       */
-
-       if (_mmc_port) {
-               send_mmc = yn;
-       }
-
-       /* set the persistent option value regardless */
-
-       send_midi_machine_control = yn;
-       set_dirty();
-
-       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
-{
-       return send_mtc;
-}
-
-bool
-Session::get_send_mmc () const
-{
-       return send_mmc;
-}
-
 int
 Session::set_mtc_port (string port_tag)
 {
@@ -490,21 +385,20 @@ Session::setup_midi_control ()
 
        if (_mmc_port != 0) {
 
-               send_mmc = send_midi_machine_control;
+               Config->set_send_mmc (session_send_mmc);
 
        } else {
 
                mmc = 0;
-               send_mmc = false;
+               session_send_mmc = false;
        }
 
        if (_mtc_port != 0) {
 
-               send_mtc = send_midi_timecode;
+               Config->set_send_mtc (session_send_mtc);
 
        } else {
-
-               send_mtc = false;
+               session_send_mtc = false;
        }
 }
 
@@ -549,7 +443,7 @@ Session::midi_read (MIDI::Port* port)
 void
 Session::spp_start (Parser& ignored)
 {
-       if (mmc_control && (_slave_type != MTC)) {
+       if (Config->get_mmc_control() && (Config->get_slave_source() != MTC)) {
                request_transport_speed (1.0);
        }
 }
@@ -563,7 +457,7 @@ Session::spp_continue (Parser& ignored)
 void
 Session::spp_stop (Parser& ignored)
 {
-       if (mmc_control) {
+       if (Config->get_mmc_control()) {
                request_stop ();
        }
 }
@@ -571,7 +465,7 @@ Session::spp_stop (Parser& ignored)
 void
 Session::mmc_deferred_play (MIDI::MachineControl &mmc)
 {
-       if (mmc_control && (_slave_type != MTC)) {
+       if (Config->get_mmc_control() && (Config->get_slave_source() != MTC)) {
                request_transport_speed (1.0);
        }
 }
@@ -579,7 +473,7 @@ Session::mmc_deferred_play (MIDI::MachineControl &mmc)
 void
 Session::mmc_record_pause (MIDI::MachineControl &mmc)
 {
-       if (mmc_control) {
+       if (Config->get_mmc_control()) {
                maybe_enable_record();
        }
 }
@@ -587,7 +481,7 @@ Session::mmc_record_pause (MIDI::MachineControl &mmc)
 void
 Session::mmc_record_strobe (MIDI::MachineControl &mmc)
 {
-       if (!mmc_control
+       if (!Config->get_mmc_control()
                return;
 
        /* record strobe does an implicit "Play" command */
@@ -601,7 +495,7 @@ Session::mmc_record_strobe (MIDI::MachineControl &mmc)
                */
                
                save_state ("", true);
-               atomic_set (&_record_status, Enabled);
+               g_atomic_int_set (&_record_status, Enabled);
                RecordStateChanged (); /* EMIT SIGNAL */
                
                request_transport_speed (1.0);
@@ -615,7 +509,7 @@ Session::mmc_record_strobe (MIDI::MachineControl &mmc)
 void
 Session::mmc_record_exit (MIDI::MachineControl &mmc)
 {
-       if (mmc_control) {
+       if (Config->get_mmc_control()) {
                disable_record (false);
        }
 }
@@ -623,7 +517,7 @@ Session::mmc_record_exit (MIDI::MachineControl &mmc)
 void
 Session::mmc_stop (MIDI::MachineControl &mmc)
 {
-       if (mmc_control) {
+       if (Config->get_mmc_control()) {
                request_stop ();
        }
 }
@@ -631,7 +525,7 @@ Session::mmc_stop (MIDI::MachineControl &mmc)
 void
 Session::mmc_pause (MIDI::MachineControl &mmc)
 {
-       if (mmc_control) {
+       if (Config->get_mmc_control()) {
 
                /* We support RECORD_PAUSE, so the spec says that
                   we must interpret PAUSE like RECORD_PAUSE if
@@ -652,7 +546,7 @@ void
 
 Session::mmc_step (MIDI::MachineControl &mmc, int steps)
 {
-       if (!mmc_control) {
+       if (!Config->get_mmc_control()) {
                return;
        }
 
@@ -671,7 +565,7 @@ Session::mmc_step (MIDI::MachineControl &mmc, int steps)
        }
        
        double diff_secs = diff.tv_sec + (diff.tv_usec / 1000000.0);
-       double cur_speed = (((steps * 0.5) * smpte_frames_per_second) / diff_secs) / smpte_frames_per_second;
+       double cur_speed = (((steps * 0.5) * Config->get_smpte_frames_per_second()) / diff_secs) / Config->get_smpte_frames_per_second();
        
        if (_transport_speed == 0 || cur_speed * _transport_speed < 0) {
                /* change direction */
@@ -703,7 +597,7 @@ Session::mmc_step (MIDI::MachineControl &mmc, int steps)
 void
 Session::mmc_rewind (MIDI::MachineControl &mmc)
 {
-       if (mmc_control) {
+       if (Config->get_mmc_control()) {
                request_transport_speed(-8.0f);
        }
 }
@@ -711,7 +605,7 @@ Session::mmc_rewind (MIDI::MachineControl &mmc)
 void
 Session::mmc_fast_forward (MIDI::MachineControl &mmc)
 {
-       if (mmc_control) {
+       if (Config->get_mmc_control()) {
                request_transport_speed(8.0f);
        }
 }
@@ -719,12 +613,12 @@ Session::mmc_fast_forward (MIDI::MachineControl &mmc)
 void
 Session::mmc_locate (MIDI::MachineControl &mmc, const MIDI::byte* mmc_tc)
 {
-       if (!mmc_control) {
+       if (!Config->get_mmc_control()) {
                return;
        }
 
-       jack_nframes_t target_frame;
-       SMPTE_Time smpte;
+       nframes_t target_frame;
+       SMPTE::Time smpte;
 
        smpte.hours = mmc_tc[0] & 0xf;
        smpte.minutes = mmc_tc[1];
@@ -758,18 +652,14 @@ Session::mmc_locate (MIDI::MachineControl &mmc, const MIDI::byte* mmc_tc)
 void
 Session::mmc_shuttle (MIDI::MachineControl &mmc, float speed, bool forw)
 {
-       cerr << "MMC shuttle, speed = " << speed << endl;
-
-       if (!mmc_control) {
+       if (!Config->get_mmc_control()) {
                return;
        }
 
-       if (shuttle_speed_threshold >= 0 && speed > shuttle_speed_threshold) {
-               speed *= shuttle_speed_factor;
+       if (Config->get_shuttle_speed_threshold() >= 0 && speed > Config->get_shuttle_speed_threshold()) {
+               speed *= Config->get_shuttle_speed_factor();
        }
 
-       cerr << "requested MMC control speed = " << speed << endl;
-       
        if (forw) {
                request_transport_speed (speed);
        } else {
@@ -780,15 +670,15 @@ Session::mmc_shuttle (MIDI::MachineControl &mmc, float speed, bool forw)
 void
 Session::mmc_record_enable (MIDI::MachineControl &mmc, size_t trk, bool enabled)
 {
-       if (mmc_control) {
+       if (Config->get_mmc_control()) {
 
                RouteList::iterator i;
-               RWLockMonitor (route_lock, false, __LINE__, __FILE__);
+               boost::shared_ptr<RouteList> r = routes.reader();
                
-               for (i = routes.begin(); i != routes.end(); ++i) {
+               for (i = r->begin(); i != r->end(); ++i) {
                        AudioTrack *at;
 
-                       if ((at = dynamic_cast<AudioTrack*>(*i)) != 0) {
+                       if ((at = dynamic_cast<AudioTrack*>((*i).get())) != 0) {
                                if (trk == at->remote_control_id()) {
                                        at->set_record_enable (enabled, &mmc);
                                        break;
@@ -813,8 +703,8 @@ Session::send_midi_time_code_in_another_thread ()
 void
 Session::send_time_code_in_another_thread (bool full)
 {
-       jack_nframes_t two_smpte_frames_duration;
-       jack_nframes_t quarter_frame_duration;
+       nframes_t two_smpte_frames_duration;
+       nframes_t quarter_frame_duration;
 
        /* Duration of two smpte frames */
        two_smpte_frames_duration = ((long) _frames_per_smpte_frame) << 1;
@@ -858,9 +748,9 @@ Session::send_full_time_code ()
 
 {
        MIDI::byte msg[10];
-       SMPTE_Time smpte;
+       SMPTE::Time smpte;
 
-       if (_mtc_port == 0 || !send_mtc) {
+       if (_mtc_port == 0 || !session_send_mtc) {
                return 0;
        }
 
@@ -884,8 +774,8 @@ Session::send_full_time_code ()
                outbound_mtc_smpte_frame = _transport_frame;
                if (((mtc_smpte_bits >> 5) != MIDI::MTC_25_FPS) && (transmitting_smpte_time.frames % 2)) {
                        // start MTC quarter frame transmission on an even frame
-                       smpte_increment( transmitting_smpte_time );
-                       outbound_mtc_smpte_frame += (jack_nframes_t) _frames_per_smpte_frame;
+                       SMPTE::increment( transmitting_smpte_time );
+                       outbound_mtc_smpte_frame += (nframes_t) _frames_per_smpte_frame;
                }
        }
 
@@ -908,7 +798,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;
@@ -923,12 +813,12 @@ Session::send_full_time_code ()
 int
 Session::send_midi_time_code ()
 {
-       if (_mtc_port == 0 || !send_mtc || transmitting_smpte_time.negative || (next_quarter_frame_to_send < 0) )  {
+       if (_mtc_port == 0 || !session_send_mtc || transmitting_smpte_time.negative || (next_quarter_frame_to_send < 0) )  {
                return 0;
        }
 
-       jack_nframes_t two_smpte_frames_duration;
-       jack_nframes_t quarter_frame_duration;
+       nframes_t two_smpte_frames_duration;
+       nframes_t quarter_frame_duration;
 
        /* Duration of two smpte frames */
        two_smpte_frames_duration = ((long) _frames_per_smpte_frame) << 1;
@@ -940,7 +830,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:
@@ -985,8 +875,8 @@ Session::send_midi_time_code ()
                                // Wrap quarter frame counter
                                next_quarter_frame_to_send = 0;
                                // Increment smpte time twice
-                               smpte_increment( transmitting_smpte_time );
-                               smpte_increment( transmitting_smpte_time );        
+                               SMPTE::increment( transmitting_smpte_time );
+                               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
@@ -1002,11 +892,11 @@ Session::send_midi_time_code ()
 **********************************************************************/
 
 void
-Session::send_mmc_in_another_thread (MIDI::MachineControl::Command cmd, jack_nframes_t target_frame)
+Session::send_mmc_in_another_thread (MIDI::MachineControl::Command cmd, nframes_t target_frame)
 {
        MIDIRequest* request;
 
-       if (_mtc_port == 0 || !send_mmc) {
+       if (_mtc_port == 0 || !session_send_mmc) {
                return;
        }
 
@@ -1020,13 +910,13 @@ Session::send_mmc_in_another_thread (MIDI::MachineControl::Command cmd, jack_nfr
 }
 
 void
-Session::deliver_mmc (MIDI::MachineControl::Command cmd, jack_nframes_t where)
+Session::deliver_mmc (MIDI::MachineControl::Command cmd, nframes_t where)
 {
        using namespace MIDI;
        int nbytes = 4;
-       SMPTE_Time smpte;
+       SMPTE::Time smpte;
 
-       if (_mmc_port == 0 || !send_mmc) {
+       if (_mmc_port == 0 || !session_send_mmc) {
                return;
        }
 
@@ -1075,7 +965,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;
@@ -1221,7 +1111,7 @@ Session::terminate_midi_thread ()
 void
 Session::poke_midi_thread ()
 {
-       char c;
+       static char c = 0;
 
        if (write (midi_request_pipe[1], &c, 1) != 1) {
                error << string_compose(_("cannot send signal to midi thread! (%1)"), strerror (errno)) << endmsg;
@@ -1279,7 +1169,7 @@ Session::midi_thread_work ()
                   on the appropriate port.
                */
 
-               if (mmc_control && _mmc_port && _mmc_port->selectable() >= 0) {
+               if (Config->get_mmc_control() && _mmc_port && _mmc_port->selectable() >= 0) {
                        pfd[nfds].fd = _mmc_port->selectable();
                        pfd[nfds].events = POLLIN|POLLHUP|POLLERR;
                        ports[nfds] = _mmc_port;
@@ -1291,14 +1181,14 @@ Session::midi_thread_work ()
                   the relevant port.
                */
 
-               if (_mtc_port && (_mtc_port != _mmc_port || !mmc_control) && _mtc_port->selectable() >= 0) {
+               if (_mtc_port && (_mtc_port != _mmc_port || !Config->get_mmc_control()) && _mtc_port->selectable() >= 0) {
                        pfd[nfds].fd = _mtc_port->selectable();
                        pfd[nfds].events = POLLIN|POLLHUP|POLLERR;
                        ports[nfds] = _mtc_port;
                        nfds++;
                }
 
-               if (_midi_port && (_midi_port != _mmc_port || !mmc_control) && (_midi_port != _mtc_port) && _midi_port->selectable() >= 0) {
+               if (_midi_port && (_midi_port != _mmc_port || !Config->get_mmc_control()) && (_midi_port != _mtc_port) && _midi_port->selectable() >= 0) {
                        pfd[nfds].fd = _midi_port->selectable();
                        pfd[nfds].events = POLLIN|POLLHUP|POLLERR;
                        ports[nfds] = _midi_port;
@@ -1457,14 +1347,3 @@ Session::midi_thread_work ()
        }
 }
 
-bool
-Session::get_mmc_control () const
-{
-       return mmc_control;
-}
-
-bool
-Session::get_midi_control () const
-{
-       return midi_control;
-}