fix up and re-enable MTC transmission
[ardour.git] / libs / ardour / session_midi.cc
index f7f1d79bbed1fcfb320a5be9abab95d0f6ddafda..d877c158b85bb19c9c6ba39330ffbfc98ea94453 100644 (file)
@@ -16,7 +16,6 @@
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
-  $Id$
 */
 
 #include <string>
@@ -53,8 +52,6 @@ using namespace MIDI;
 MachineControl::CommandSignature MMC_CommandSignature;
 MachineControl::ResponseSignature MMC_ResponseSignature;
 
-MultiAllocSingleReleasePool Session::MIDIRequest::pool ("midi", sizeof (Session::MIDIRequest), 1024);
-
 int
 Session::use_config_midi_ports ()
 {
@@ -86,111 +83,6 @@ Session::use_config_midi_ports ()
  MTC, MMC, etc.
 **********************************************************************/
 
-void
-Session::set_mmc_control (bool yn)
-{
-#if 0
-       if (mmc_control == yn) {
-               return;
-       }
-       
-       mmc_control = yn;
-       set_dirty();
-       poke_midi_thread ();
-#endif
-       ControlChanged (MMCControl); /* EMIT SIGNAL */
-}
-
-void
-Session::set_midi_control (bool yn)
-{
-#if 0
-       if (midi_control == yn) {
-               return;
-       }
-
-       midi_control = yn;
-       set_dirty();
-       poke_midi_thread ();
-#endif
-       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)
-{
-       cerr << "set send mmc " << yn << endl;
-
-       if (_mmc_port == 0) {
-               cerr << "\tno 1\n";
-               return;
-       }
-
-       if (send_midi_machine_control == yn) {
-               cerr << "\tno 2\n";
-               return;
-       }
-
-       /* only set the internal flag if we have
-          a port.
-       */
-
-       if (_mmc_port) {
-               cerr << "\tyes\n";
-               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)
 {
@@ -233,9 +125,29 @@ Session::set_mtc_port (string port_tag)
        return 0;
 }
 
+void
+Session::set_mmc_receive_device_id (uint32_t device_id)
+{
+       if (mmc) {
+               mmc->set_receive_device_id (device_id);
+       }
+}
+
+void
+Session::set_mmc_send_device_id (uint32_t device_id)
+{
+       if (mmc) {
+               mmc->set_send_device_id (device_id);
+       }
+}
+
 int
 Session::set_mmc_port (string port_tag)
 {
+       MIDI::byte old_recv_device_id = 0;
+       MIDI::byte old_send_device_id = 0;
+       bool reset_id = false;
+
        if (port_tag.length() == 0) {
                if (_mmc_port == 0) {
                        return 0;
@@ -253,6 +165,9 @@ Session::set_mmc_port (string port_tag)
        _mmc_port = port;
 
        if (mmc) {
+               old_recv_device_id = mmc->receive_device_id();
+               old_recv_device_id = mmc->send_device_id();
+               reset_id = true;
                delete mmc;
        }
 
@@ -260,6 +175,10 @@ Session::set_mmc_port (string port_tag)
                                        MMC_CommandSignature,
                                        MMC_ResponseSignature);
 
+       if (reset_id) {
+               mmc->set_receive_device_id (old_recv_device_id);
+               mmc->set_send_device_id (old_send_device_id);
+       }
 
        mmc->Play.connect 
                (mem_fun (*this, &Session::mmc_deferred_play));
@@ -288,6 +207,7 @@ Session::set_mmc_port (string port_tag)
        mmc->TrackRecordStatusChange.connect
                (mem_fun (*this, &Session::mmc_record_enable));
 
+
        /* also handle MIDI SPP because its so common */
 
        _mmc_port->input()->start.connect (mem_fun (*this, &Session::spp_start));
@@ -306,6 +226,7 @@ Session::set_mmc_port (string port_tag)
 int
 Session::set_midi_port (string port_tag)
 {
+#if 0
        if (port_tag.length() == 0) {
                if (_midi_port == 0) {
                        return 0;
@@ -329,6 +250,7 @@ Session::set_midi_port (string port_tag)
        Config->set_midi_port_name (port_tag);
 
   out:
+#endif
        MIDI_PortChanged(); /* EMIT SIGNAL */
        change_midi_ports ();
        set_dirty();
@@ -476,7 +398,7 @@ Session::setup_midi_control ()
        
        mmc_buffer[0] = 0xf0; // SysEx
        mmc_buffer[1] = 0x7f; // Real Time SysEx ID for MMC
-       mmc_buffer[2] = 0x7f; // "broadcast" device ID
+       mmc_buffer[2] = (mmc ? mmc->send_device_id() : 0x7f);
        mmc_buffer[3] = 0x6;  // MCC
 
        /* Set up the qtr frame message */
@@ -489,71 +411,12 @@ Session::setup_midi_control ()
        mtc_msg[10] = 0xf1;
        mtc_msg[12] = 0xf1;
        mtc_msg[14] = 0xf1;
-
-       if (_mmc_port != 0) {
-
-               send_mmc = send_midi_machine_control;
-
-       } else {
-
-               mmc = 0;
-               send_mmc = false;
-       }
-
-       if (_mtc_port != 0) {
-
-               send_mtc = send_midi_timecode;
-
-       } else {
-
-               send_mtc = false;
-       }
-}
-
-#if 0
-int
-Session::midi_read (MIDI::Port* port)
-{
-       MIDI::byte buf[512];
-       
-       /* reading from the MIDI port activates the Parser
-          that in turn generates signals that we care
-          about. the port is already set to NONBLOCK so that
-          can read freely here.
-       */
-       
-       while (1) {
-               
-               // cerr << "+++ READ ON " << port->name() << endl;
-               
-               int nread = port->read (buf, sizeof (buf));
-
-               // cerr << "-- READ (" << nread << " ON " << port->name() << endl;
-               
-               if (nread > 0) {
-                       if ((size_t) nread < sizeof (buf)) {
-                               break;
-                       } else {
-                               continue;
-                       }
-               } else if (nread == 0) {
-                       break;
-               } else if (errno == EAGAIN) {
-                       break;
-               } else {
-                       fatal << string_compose(_("Error reading from MIDI port %1"), port->name()) << endmsg;
-                       /*NOTREACHED*/
-               }
-       }
-
-       return 0;
 }
-#endif
 
 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);
        }
 }
@@ -567,7 +430,7 @@ Session::spp_continue (Parser& ignored)
 void
 Session::spp_stop (Parser& ignored)
 {
-       if (mmc_control) {
+       if (Config->get_mmc_control()) {
                request_stop ();
        }
 }
@@ -575,7 +438,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);
        }
 }
@@ -583,7 +446,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();
        }
 }
@@ -591,7 +454,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 */
@@ -619,7 +482,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);
        }
 }
@@ -627,7 +490,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 ();
        }
 }
@@ -635,7 +498,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
@@ -655,7 +518,7 @@ static bool step_queued = false;
 void
 Session::mmc_step (MIDI::MachineControl &mmc, int steps)
 {
-       if (!mmc_control) {
+       if (!Config->get_mmc_control()) {
                return;
        }
 
@@ -674,7 +537,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) * smpte_frames_per_second()) / diff_secs) / smpte_frames_per_second();
        
        if (_transport_speed == 0 || cur_speed * _transport_speed < 0) {
                /* change direction */
@@ -706,7 +569,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);
        }
 }
@@ -714,7 +577,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);
        }
 }
@@ -722,17 +585,19 @@ 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;
+       nframes_t target_frame;
        SMPTE::Time smpte;
 
        smpte.hours = mmc_tc[0] & 0xf;
        smpte.minutes = mmc_tc[1];
        smpte.seconds = mmc_tc[2];
        smpte.frames = mmc_tc[3];
+       smpte.rate = smpte_frames_per_second();
+       smpte.drop = smpte_drop_frames();
   
        // Also takes smpte offset into account:
        smpte_to_sample( smpte, target_frame, true /* use_offset */, false /* use_subframes */ );
@@ -761,18 +626,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 {
@@ -783,15 +644,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;
-               Glib::RWLock::ReaderLock guard (route_lock);
+               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;
@@ -801,28 +662,23 @@ Session::mmc_record_enable (MIDI::MachineControl &mmc, size_t trk, bool enabled)
        }
 }
 
-
 void
 Session::change_midi_ports ()
 {
-/*
        MIDIRequest* request = new MIDIRequest;
 
        request->type = MIDIRequest::PortChange;
        midi_requests.write (&request, 1);
        poke_midi_thread ();
-*/
 }
 
 /** Send MTC Full Frame message (complete SMPTE time) for the start of this cycle.
  * This resets the MTC code, the next quarter frame message that is sent will be
  * the first one with the beginning of this cycle as the new start point.
- *
- * Audio thread only, realtime safe.  MIDI::Manager::cycle_start must
- * have been called with the appropriate nframes parameter this cycle.
  */
+
 int
-Session::send_full_time_code(jack_nframes_t nframes)
+Session::send_full_time_code(nframes_t nframes)
 {
        /* This function could easily send at a given frame offset, but would
         * that be useful?  Does ardour do sub-block accurate locating? [DR] */
@@ -832,7 +688,7 @@ Session::send_full_time_code(jack_nframes_t nframes)
 
        _send_smpte_update = false;
 
-       if (_mtc_port == 0 || !send_mtc) {
+       if (_mtc_port == 0 || !session_send_mtc) {
                return 0;
        }
        
@@ -846,7 +702,7 @@ Session::send_full_time_code(jack_nframes_t nframes)
        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;
+               outbound_mtc_smpte_frame += (nframes_t) _frames_per_smpte_frame;
        }
 
        // Compensate for audio latency
@@ -887,27 +743,22 @@ Session::send_full_time_code(jack_nframes_t nframes)
  * earlier already this cycle by send_full_time_code)
  */
 int
-Session::send_midi_time_code_for_cycle(jack_nframes_t nframes)
+Session::send_midi_time_code_for_cycle(nframes_t nframes)
 {      
        assert (next_quarter_frame_to_send >= 0);
        assert (next_quarter_frame_to_send <= 7);
        
-       if (next_quarter_frame_to_send < 0)
-       {
-               printf("Negative????\n");
-       }
-
-       if (_mtc_port == 0 || !send_mtc || transmitting_smpte_time.negative
-                       /*|| (next_quarter_frame_to_send < 0)*/ ) {
-               //printf("(MTC) Not sending MTC\n");
+       if (_mtc_port == 0 || !session_send_mtc || transmitting_smpte_time.negative
+           /*|| (next_quarter_frame_to_send < 0)*/ ) {
+               // cerr << "(MTC) Not sending MTC\n";
                return 0;
        }
        
        /* Duration of one quarter frame */
-       jack_nframes_t quarter_frame_duration = ((long) _frames_per_smpte_frame) >> 2;
+       nframes_t quarter_frame_duration = ((long) _frames_per_smpte_frame) >> 2;
        
-       //cerr << "(MTC) TR: " << _transport_frame << " - SF: " << outbound_mtc_smpte_frame
-       //<< " - NQ: " << next_quarter_frame_to_send << " - FD" << quarter_frame_duration << endl;
+       // cerr << "(MTC) TR: " << _transport_frame << " - SF: " << outbound_mtc_smpte_frame
+       // << " - NQ: " << next_quarter_frame_to_send << " - FD" << quarter_frame_duration << endl;
                
        // FIXME: this should always be true
        //assert((outbound_mtc_smpte_frame + (next_quarter_frame_to_send * quarter_frame_duration))
@@ -917,8 +768,8 @@ Session::send_midi_time_code_for_cycle(jack_nframes_t nframes)
        // Send quarter frames for this cycle
        while (_transport_frame + nframes > (outbound_mtc_smpte_frame +
                                (next_quarter_frame_to_send * quarter_frame_duration))) {
-
-               //cerr << "(MTC) Next frame to send: " << next_quarter_frame_to_send << endl;
+               
+               // cerr << "(MTC) Next frame to send: " << next_quarter_frame_to_send << endl;
 
                switch (next_quarter_frame_to_send) {
                        case 0:
@@ -947,14 +798,14 @@ Session::send_midi_time_code_for_cycle(jack_nframes_t nframes)
                                break;
                }                       
                
-               const jack_nframes_t msg_time = (outbound_mtc_smpte_frame
+               const nframes_t msg_time = (outbound_mtc_smpte_frame
                        + (quarter_frame_duration * next_quarter_frame_to_send));
        
                // This message must fall within this block or something is broken
                assert(msg_time >= _transport_frame);
                assert(msg_time < _transport_frame + nframes);
 
-               jack_nframes_t out_stamp = msg_time - _transport_frame;
+               nframes_t out_stamp = msg_time - _transport_frame;
                assert(out_stamp < nframes);
 
                if (!_mtc_port->midimsg (mtc_msg, 2, out_stamp)) {
@@ -994,25 +845,6 @@ Session::send_midi_time_code_for_cycle(jack_nframes_t nframes)
 /***********************************************************************
  OUTBOUND MMC STUFF
 **********************************************************************/
-/*
-void
-Session::send_mmc_in_another_thread (MIDI::MachineControl::Command cmd, jack_nframes_t target_frame)
-{
-       MIDIRequest* request;
-
-       if (_mtc_port == 0 || !send_mmc) {
-               return;
-       }
-
-       request = new MIDIRequest;
-       request->type = MIDIRequest::SendMMC;
-       request->mmc_cmd = cmd;
-       request->locate_frame = target_frame;
-
-       midi_requests.write (&request, 1);
-       poke_midi_thread ();
-}
-*/
 
 /** Send an MMC command at the given absolute timestamp (@a where).
  *
@@ -1020,13 +852,13 @@ Session::send_mmc_in_another_thread (MIDI::MachineControl::Command cmd, jack_nfr
  * this process cycle or horrible things will happen.
  */
 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;
 
-       if (_mmc_port == 0 || !send_mmc) {
+       if (_mmc_port == 0 || !session_send_mmc) {
                //cerr << "Not delivering MMC " << _mmc_port << " - " << send_mmc << endl;
                return;
        }
@@ -1079,8 +911,7 @@ Session::deliver_mmc (MIDI::MachineControl::Command cmd, jack_nframes_t where)
 
                assert(where >= _transport_frame);
 
-               // FIXME: timestamp correct? [DR]
-               if (!_mmc_port->midimsg (mmc_buffer, sizeof (mmc_buffer), where - _transport_frame)) {
+               if (!_mmc_port->midimsg (mmc_buffer, sizeof (mmc_buffer), 0)) {
                        error << string_compose(_("MMC: cannot send command %1%2%3"), &hex, cmd, &dec) << endmsg;
                } /*else {
                        cerr << "Sending MMC\n";
@@ -1117,80 +948,10 @@ Session::mmc_step_timeout ()
        return true;
 }
 
-
-void
-Session::send_midi_message (MIDI::Port * port, MIDI::eventType ev, MIDI::channel_t ch, MIDI::EventTwoBytes data)
-{
-       // in another thread, really
-       /*
-       MIDIRequest* request = new MIDIRequest;
-
-       request->type = MIDIRequest::SendMessage;
-       request->port = port;
-       request->ev = ev;
-       request->chan = ch;
-       request->data = data;
-       
-       midi_requests.write (&request, 1);
-       poke_midi_thread ();
-       */
-}
-
-void
-Session::deliver_midi (MIDI::Port * port, MIDI::byte* buf, int32_t bufsize)
-{
-       // in another thread, really
-       /*
-       MIDIRequest* request = new MIDIRequest;
-
-       request->type = MIDIRequest::Deliver;
-       request->port = port;
-       request->buf = buf;
-       request->size = bufsize;
-       
-       midi_requests.write (&request, 1);
-       poke_midi_thread ();
-       */
-}
-
-#if 0
-
-This is aaalll gone. 
-
-
-void
-Session::deliver_midi_message (MIDI::Port * port, MIDI::eventType ev, MIDI::channel_t ch, MIDI::EventTwoBytes data)
-{
-       if (port == 0 || ev == MIDI::none) {
-               return;
-       }
-
-       midi_msg[0] = (ev & 0xF0) | (ch & 0xF); 
-       midi_msg[1] = data.controller_number;
-       midi_msg[2] = data.value;
-
-       port->write (midi_msg, 3);
-}
-
-void
-Session::deliver_data (MIDI::Port * port, MIDI::byte* buf, int32_t size)
-{
-       if (port) {
-               port->write (buf, size);
-       }
-
-       /* this is part of the semantics of the Deliver request */
-
-       delete [] buf;
-}
-#endif
-
-
 /*---------------------------------------------------------------------------
   MIDI THREAD 
   ---------------------------------------------------------------------------*/
 
-#if 0
 int
 Session::start_midi_thread ()
 {
@@ -1214,29 +975,30 @@ Session::start_midi_thread ()
                return -1;
        }
 
-       // pthread_detach (midi_thread);
-
        return 0;
 }
 
 void
 Session::terminate_midi_thread ()
 {
-       MIDIRequest* request = new MIDIRequest;
-       void* status;
-
-       request->type = MIDIRequest::Quit;
+       if (midi_thread) {
 
-       midi_requests.write (&request, 1);
-       poke_midi_thread ();
+               MIDIRequest* request = new MIDIRequest;
+               void* status;
+               
+               request->type = MIDIRequest::Quit;
+               
+               midi_requests.write (&request, 1);
+               poke_midi_thread ();
 
-       pthread_join (midi_thread, &status);
+               pthread_join (midi_thread, &status);
+       }
 }
 
 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;
@@ -1277,10 +1039,7 @@ Session::midi_thread_work ()
 
        /* set up the port vector; 4 is the largest possible size for now */
 
-       ports.push_back (0);
-       ports.push_back (0);
-       ports.push_back (0);
-       ports.push_back (0);
+       ports.assign (4, (MIDI::Port*) 0);
 
        while (1) {
 
@@ -1290,11 +1049,7 @@ Session::midi_thread_work ()
                pfd[nfds].events = POLLIN|POLLHUP|POLLERR;
                nfds++;
 
-               /* if we are using MMC control, we obviously have to listen
-                  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;
@@ -1306,14 +1061,18 @@ 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 we are using MMC control, we obviously have to listen
+                  the relevant port.
+               */
+
+               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;
@@ -1341,7 +1100,6 @@ Session::midi_thread_work ()
                // cerr << "MIDI thread wakes at " << get_cycles () << endl;
 
                fds_ready = 0;
-               restart = false;
 
                /* check the transport request pipe */
 
@@ -1381,37 +1139,6 @@ Session::midi_thread_work ()
                        while (midi_requests.read (&request, 1) == 1) {
 
                                switch (request->type) {
-                                       
-                               case MIDIRequest::SendFullMTC:
-                                       // cerr << "send full MTC\n";
-                                       send_full_time_code ();
-                                       // cerr << "... done\n";
-                                       break;
-                                       
-                               case MIDIRequest::SendMTC:
-                                       // cerr << "send qtr MTC\n";
-                                       send_midi_time_code ();
-                                       // cerr << "... done\n";
-                                       break;
-                                       
-                               case MIDIRequest::SendMMC:
-                                       // cerr << "send MMC\n";
-                                       deliver_mmc (request->mmc_cmd, request->locate_frame);
-                                       // cerr << "... done\n";
-                                       break;
-
-                               case MIDIRequest::SendMessage:
-                                       // cerr << "send Message\n";
-                                       deliver_midi_message (request->port, request->ev, request->chan, request->data);
-                                       // cerr << "... done\n";
-                                       break;
-                                       
-                               case MIDIRequest::Deliver:
-                                       // cerr << "deliver\n";
-                                       deliver_data (_midi_port, request->buf, request->size);
-                                       // cerr << "... done\n";
-                                       break;
-                                               
                                case MIDIRequest::PortChange:
                                        /* restart poll with new ports */
                                        // cerr << "rebind\n";
@@ -1448,7 +1175,7 @@ Session::midi_thread_work ()
                        
                        if (pfd[p].revents & POLLIN) {
                                fds_ready++;
-                               midi_read (ports[p]);
+                               ports[p]->parse ();
                        }
                }
 
@@ -1471,16 +1198,4 @@ Session::midi_thread_work ()
                }
        }
 }
-#endif
-
-bool
-Session::get_mmc_control () const
-{
-       return mmc_control;
-}
 
-bool
-Session::get_midi_control () const
-{
-       return midi_control;
-}