remove debugging output
[ardour.git] / libs / midi++2 / mmc.cc
index eb1c6cb5b044a05eac3794afd3dcaf775ad67b2c..292b6cc1b4ff105242eb02f54ca0a86b4d4bb1b4 100644 (file)
     $Id$
 */
 
+#include <fcntl.h>
 #include <map>
 
+#include "timecode/time.h"
 #include "pbd/error.h"
 #include "midi++/mmc.h"
 #include "midi++/port.h"
 #include "midi++/parser.h"
+#include "midi++/manager.h"
 
 using namespace std;
 using namespace MIDI;
@@ -192,25 +195,20 @@ static void build_mmc_cmd_map ()
 }
 
 
-MachineControl::MachineControl (Port &p, float /*version*/,
-                               CommandSignature & /*csig*/,
-                               ResponseSignature & /*rsig*/)
-
-       : _port (p)
+MachineControl::MachineControl (Manager* m, jack_client_t* jack)
 {
-       Parser *parser;
-       
        build_mmc_cmd_map ();
 
-       _receive_device_id = 0;
+       _receive_device_id = 0x7f;
        _send_device_id = 0x7f;
-       
-       if ((parser = _port.input()) != 0) {
-               parser->mmc.connect (mmc_connection, boost::bind (&MachineControl::process_mmc_message, this, _1, _2, _3));
-       } else {
-               warning << "MMC connected to a non-input port: useless!"
-                       << endmsg;
-       }
+
+       _input_port = m->add_port (new Port ("MMC in", Port::IsInput, jack));
+       _output_port = m->add_port (new Port ("MMC out", Port::IsOutput, jack));
+
+       _input_port->parser()->mmc.connect_same_thread (port_connections, boost::bind (&MachineControl::process_mmc_message, this, _1, _2, _3));
+       _input_port->parser()->start.connect_same_thread (port_connections, boost::bind (&MachineControl::spp_start, this, _1, _2));
+       _input_port->parser()->contineu.connect_same_thread (port_connections, boost::bind (&MachineControl::spp_continue, this, _1, _2));
+       _input_port->parser()->stop.connect_same_thread (port_connections, boost::bind (&MachineControl::spp_stop, this, _1, _2));
 }
 
 void
@@ -227,7 +225,6 @@ MachineControl::set_send_device_id (byte id)
 
 bool
 MachineControl::is_mmc (byte *sysex_buf, size_t len)
-
 {
        if (len < 4 || len > 48) {
                return false;
@@ -247,7 +244,6 @@ MachineControl::is_mmc (byte *sysex_buf, size_t len)
 
 void
 MachineControl::process_mmc_message (Parser &, byte *msg, size_t len)
-
 {
        size_t skiplen;
        byte *mmc_msg;
@@ -455,7 +451,6 @@ MachineControl::process_mmc_message (Parser &, byte *msg, size_t len)
 
 int
 MachineControl::do_masked_write (byte *msg, size_t len)
-
 {
        /* return the number of bytes "consumed" */
 
@@ -571,7 +566,6 @@ MachineControl::write_track_status (byte *msg, size_t /*len*/, byte reg)
 
 int
 MachineControl::do_locate (byte *msg, size_t /*msglen*/)
-
 {
        if (msg[2] == 0) {
                warning << "MIDI::MMC: locate [I/F] command not supported"
@@ -600,7 +594,6 @@ MachineControl::do_step (byte *msg, size_t /*msglen*/)
 
 int
 MachineControl::do_shuttle (byte *msg, size_t /*msglen*/)
-
 {
        size_t forward;
        byte sh = msg[2];
@@ -630,3 +623,84 @@ MachineControl::do_shuttle (byte *msg, size_t /*msglen*/)
        return 0;
 }
 
+void
+MachineControl::enable_send (bool yn)
+{
+       _enable_send = yn;
+}
+
+/** Send a MMC command to a the MMC port.
+ *  @param c command.
+ */
+void
+MachineControl::send (MachineControlCommand const & c)
+{
+       if (_output_port == 0 || !_enable_send) {
+               // cerr << "Not delivering MMC " << _mmc->port() << " - " << session_send_mmc << endl;
+               return;
+       }
+
+       MIDI::byte buffer[32];
+       MIDI::byte* b = c.fill_buffer (this, buffer);
+
+       if (_output_port->midimsg (buffer, b - buffer, 0)) {
+               error << "MMC: cannot send command" << endmsg;
+       }
+}
+
+void
+MachineControl::spp_start (Parser& parser, framecnt_t timestamp)
+{
+       SPPStart (parser, timestamp); /* EMIT SIGNAL */
+}
+
+void
+MachineControl::spp_continue (Parser& parser, framecnt_t timestamp)
+{
+       SPPContinue (parser, timestamp); /* EMIT SIGNAL */
+}
+
+void
+MachineControl::spp_stop (Parser& parser, framecnt_t timestamp)
+{
+       SPPStop (parser, timestamp); /* EMIT SIGNAL */
+}
+
+MachineControlCommand::MachineControlCommand (MachineControl::Command c)
+       : _command (c)
+{
+
+}
+
+MachineControlCommand::MachineControlCommand (Timecode::Time t)
+       : _command (MachineControl::cmdLocate)
+       , _time (t)
+{
+
+}
+
+MIDI::byte * 
+MachineControlCommand::fill_buffer (MachineControl* mmc, MIDI::byte* b) const
+{
+       *b++ = 0xf0; // SysEx
+       *b++ = 0x7f; // Real-time SysEx ID for MMC
+       *b++ = mmc->send_device_id();
+       *b++ = 0x6; // MMC command
+
+       *b++ = _command;
+
+       if (_command == MachineControl::cmdLocate) {
+               *b++ = 0x6; // byte count
+               *b++ = 0x1; // "TARGET" subcommand
+               *b++ = _time.hours;
+               *b++ = _time.minutes;
+               *b++ = _time.seconds;
+               *b++ = _time.frames;
+               *b++ = _time.subframes;
+       }
+
+       *b++ = 0xf7;
+
+       return b;
+}
+