diff options
author | Carl Hetherington <carl@carlh.net> | 2010-07-08 01:00:46 +0000 |
---|---|---|
committer | Carl Hetherington <carl@carlh.net> | 2010-07-08 01:00:46 +0000 |
commit | 4885f29be158999626eb6dfa5507fe2258d388b0 (patch) | |
tree | a82c57b53937deed062cb0fad9226ce8d81626c8 /libs/midi++2/port.cc | |
parent | a15bdfc6d9b42b4078976da54bdd84325cea3d16 (diff) |
Trim midi++ port code to either do in or out, but not both in the same object.
git-svn-id: svn://localhost/ardour2/branches/3.0@7391 d708f5d6-7413-0410-9779-e7cbd77b26cf
Diffstat (limited to 'libs/midi++2/port.cc')
-rw-r--r-- | libs/midi++2/port.cc | 227 |
1 files changed, 62 insertions, 165 deletions
diff --git a/libs/midi++2/port.cc b/libs/midi++2/port.cc index c2fd4c99e2..a6097c89dc 100644 --- a/libs/midi++2/port.cc +++ b/libs/midi++2/port.cc @@ -39,81 +39,59 @@ using namespace MIDI; using namespace std; using namespace PBD; -size_t Port::nports = 0; pthread_t Port::_process_thread; Signal0<void> Port::JackHalted; Signal0<void> Port::MakeConnections; -Port::Port (string const & name, int mode, jack_client_t* jack_client) +Port::Port (string const & name, Flags flags, jack_client_t* jack_client) : _currently_in_cycle (false) , _nframes_this_cycle (0) , _jack_client (jack_client) - , _jack_input_port (0) - , _jack_output_port (0) + , _jack_port (0) , _last_read_index (0) , output_fifo (512) , input_fifo (1024) + , _flags (flags) { - init (name, mode); + init (name, flags); } Port::Port (const XMLNode& node, jack_client_t* jack_client) : _currently_in_cycle (false) , _nframes_this_cycle (0) , _jack_client (jack_client) - , _jack_input_port (0) - , _jack_output_port (0) + , _jack_port (0) , _last_read_index (0) , output_fifo (512) , input_fifo (1024) { Descriptor desc (node); - init (desc.tag, desc.mode); + init (desc.tag, desc.flags); set_state (node); } void -Port::init (string const & name, int mode) +Port::init (string const & name, Flags flags) { _ok = false; /* derived class must set to true if constructor succeeds. */ - input_parser = 0; - output_parser = 0; + _parser = 0; _tagname = name; - _mode = mode; + _flags = flags; - if (_mode == O_RDONLY || _mode == O_RDWR) { - input_parser = new Parser (*this); - } else { - input_parser = 0; - } - - if (_mode == O_WRONLY || _mode == O_RDWR) { - output_parser = new Parser (*this); - } else { - output_parser = 0; - } + _parser = new Parser (*this); for (int i = 0; i < 16; i++) { - _channel[i] = new Channel (i, *this); - - if (input_parser) { - _channel[i]->connect_input_signals (); - } - - if (output_parser) { - _channel[i]->connect_output_signals (); - } + _channel[i] = new Channel (i, *this); + _channel[i]->connect_signals (); } - create_port_names (); - - if (!create_ports ()) { + if (!create_port ()) { _ok = true; } @@ -128,18 +106,11 @@ Port::~Port () delete _channel[i]; } - if (_jack_input_port) { - if (_jack_client && _jack_input_port) { - jack_port_unregister (_jack_client, _jack_input_port); - } - _jack_input_port = 0; - } - - if (_jack_output_port) { - if (_jack_client && _jack_output_port) { - jack_port_unregister (_jack_client, _jack_output_port); + if (_jack_port) { + if (_jack_client && _jack_port) { + jack_port_unregister (_jack_client, _jack_port); } - _jack_output_port = 0; + _jack_port = 0; } } @@ -153,9 +124,7 @@ Port::parse (nframes_t timestamp) once it has data ready. */ - if (input_parser) { - input_parser->set_timestamp (timestamp); - } + _parser->set_timestamp (timestamp); while (1) { @@ -190,7 +159,7 @@ Port::clock (timestamp_t timestamp) { static byte clockmsg = 0xf8; - if (_mode != O_RDONLY) { + if (sends_output()) { return midimsg (&clockmsg, 1, timestamp); } @@ -207,16 +176,14 @@ Port::cycle_start (nframes_t nframes) _last_read_index = 0; _last_write_timestamp = 0; - if (_jack_output_port != 0) { - // output - void *buffer = jack_port_get_buffer (_jack_output_port, nframes); + if (sends_output()) { + void *buffer = jack_port_get_buffer (_jack_port, nframes); jack_midi_clear_buffer (buffer); flush (buffer); } - if (_jack_input_port != 0) { - // input - void* jack_buffer = jack_port_get_buffer(_jack_input_port, nframes); + if (receives_input()) { + void* jack_buffer = jack_port_get_buffer(_jack_port, nframes); const nframes_t event_count = jack_midi_get_event_count(jack_buffer); jack_midi_event_t ev; @@ -236,8 +203,8 @@ Port::cycle_start (nframes_t nframes) void Port::cycle_end () { - if (_jack_output_port != 0) { - flush (jack_port_get_buffer (_jack_output_port, _nframes_this_cycle)); + if (sends_output()) { + flush (jack_port_get_buffer (_jack_port, _nframes_this_cycle)); } _currently_in_cycle = false; @@ -250,8 +217,6 @@ std::ostream & MIDI::operator << ( std::ostream & os, const MIDI::Port & port ) os << "MIDI::Port { "; os << "name: " << port.name(); os << "; "; - os << "mode: " << port.mode(); - os << "; "; os << "ok: " << port.ok(); os << "; "; os << " }"; @@ -271,12 +236,10 @@ Port::Descriptor::Descriptor (const XMLNode& node) if ((prop = node.property ("mode")) != 0) { - mode = O_RDWR; - if (strings_equal_ignore_case (prop->value(), "output") || strings_equal_ignore_case (prop->value(), "out")) { - mode = O_WRONLY; + flags = IsOutput; } else if (strings_equal_ignore_case (prop->value(), "input") || strings_equal_ignore_case (prop->value(), "in")) { - mode = O_RDONLY; + flags = IsInput; } have_mode = true; @@ -291,8 +254,7 @@ void Port::jack_halted () { _jack_client = 0; - _jack_input_port = 0; - _jack_output_port = 0; + _jack_port = 0; } int @@ -300,7 +262,7 @@ Port::write(byte * msg, size_t msglen, timestamp_t timestamp) { int ret = 0; - if (!_jack_output_port) { + if (!sends_output()) { return ret; } @@ -344,7 +306,7 @@ Port::write(byte * msg, size_t msglen, timestamp_t timestamp) timestamp = _last_write_timestamp; } - if (jack_midi_event_write (jack_port_get_buffer (_jack_output_port, _nframes_this_cycle), + if (jack_midi_event_write (jack_port_get_buffer (_jack_port, _nframes_this_cycle), timestamp, msg, msglen) == 0) { ret = msglen; _last_write_timestamp = timestamp; @@ -352,7 +314,7 @@ Port::write(byte * msg, size_t msglen, timestamp_t timestamp) } else { ret = 0; cerr << "write of " << msglen << " failed, port holds " - << jack_midi_get_event_count (jack_port_get_buffer (_jack_output_port, _nframes_this_cycle)) + << jack_midi_get_event_count (jack_port_get_buffer (_jack_port, _nframes_this_cycle)) << endl; } } else { @@ -360,11 +322,11 @@ Port::write(byte * msg, size_t msglen, timestamp_t timestamp) } } - if (ret > 0 && output_parser) { + if (ret > 0 && _parser) { // ardour doesn't care about this and neither should your app, probably // output_parser->raw_preparse (*output_parser, msg, ret); for (int i = 0; i < ret; i++) { - output_parser->scanner (msg[i]); + _parser->scanner (msg[i]); } // ardour doesn't care about this and neither should your app, probably // output_parser->raw_postparse (*output_parser, msg, ret); @@ -411,64 +373,34 @@ Port::flush (void* jack_port_buffer) int Port::read (byte *, size_t) { + if (!receives_input()) { + return 0; + } + timestamp_t time; Evoral::EventType type; uint32_t size; byte buffer[input_fifo.capacity()]; while (input_fifo.read (&time, &type, &size, buffer)) { - if (input_parser) { - input_parser->set_timestamp (time); - for (uint32_t i = 0; i < size; ++i) { - input_parser->scanner (buffer[i]); - } + _parser->set_timestamp (time); + for (uint32_t i = 0; i < size; ++i) { + _parser->scanner (buffer[i]); } } return 0; } -void -Port::create_port_names () -{ - assert(!_jack_input_port); - assert(!_jack_output_port); - - if (_mode == O_RDWR || _mode == O_WRONLY) { - _jack_output_port_name = _tagname.append ("_out"); - } - - if (_mode == O_RDWR || _mode == O_RDONLY) { - _jack_input_port_name = _tagname.append ("_in"); - } -} - int -Port::create_ports () +Port::create_port () { - bool ret = true; - - jack_nframes_t nframes = jack_get_buffer_size(_jack_client); - - if (!_jack_output_port_name.empty()) { - _jack_output_port = jack_port_register(_jack_client, _jack_output_port_name.c_str(), - JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0); - if (_jack_output_port) { - jack_midi_clear_buffer(jack_port_get_buffer(_jack_output_port, nframes)); - } - ret = ret && (_jack_output_port != NULL); - } - - if (!_jack_input_port_name.empty()) { - _jack_input_port = jack_port_register(_jack_client, _jack_input_port_name.c_str(), - JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0); - if (_jack_input_port) { - jack_midi_clear_buffer(jack_port_get_buffer(_jack_input_port, nframes)); - } - ret = ret && (_jack_input_port != NULL); + _jack_port = jack_port_register(_jack_client, _tagname.c_str(), JACK_DEFAULT_MIDI_TYPE, _flags, 0); + if (_jack_port) { + jack_midi_clear_buffer (jack_port_get_buffer (_jack_port, jack_get_buffer_size (_jack_client))); } - return ret ? 0 : -1; + return _jack_port == 0 ? -1 : 0; } XMLNode& @@ -477,12 +409,10 @@ Port::get_state () const XMLNode* root = new XMLNode ("MIDI-port"); root->add_property ("tag", _tagname); - if (_mode == O_RDONLY) { + if (_flags == IsInput) { root->add_property ("mode", "input"); - } else if (_mode == O_WRONLY) { - root->add_property ("mode", "output"); } else { - root->add_property ("mode", "duplex"); + root->add_property ("mode", "output"); } #if 0 @@ -498,9 +428,9 @@ Port::get_state () const write (device_inquiry, sizeof (device_inquiry), 0); #endif - if (_jack_output_port) { + if (_jack_port) { - const char** jc = jack_port_get_connections (_jack_output_port); + const char** jc = jack_port_get_connections (_jack_port); string connection_string; if (jc) { for (int i = 0; jc[i]; ++i) { @@ -513,34 +443,11 @@ Port::get_state () const } if (!connection_string.empty()) { - root->add_property ("outbound", connection_string); + root->add_property ("connections", connection_string); } } else { - if (!_outbound_connections.empty()) { - root->add_property ("outbound", _outbound_connections); - } - } - - if (_jack_input_port) { - - const char** jc = jack_port_get_connections (_jack_input_port); - string connection_string; - if (jc) { - for (int i = 0; jc[i]; ++i) { - if (i > 0) { - connection_string += ','; - } - connection_string += jc[i]; - } - free (jc); - } - - if (!connection_string.empty()) { - root->add_property ("inbound", connection_string); - } - } else { - if (!_inbound_connections.empty()) { - root->add_property ("inbound", _inbound_connections); + if (!_connections.empty()) { + root->add_property ("connections", _connections); } } @@ -552,39 +459,29 @@ Port::set_state (const XMLNode& node) { const XMLProperty* prop; - if ((prop = node.property ("inbound")) != 0 && _jack_input_port) { - _inbound_connections = prop->value (); - } - - if ((prop = node.property ("outbound")) != 0 && _jack_output_port) { - _outbound_connections = prop->value(); + if ((prop = node.property ("connections")) != 0 && _jack_port) { + _connections = prop->value (); } } void Port::make_connections () { - if (!_inbound_connections.empty()) { + if (!_connections.empty()) { vector<string> ports; - split (_inbound_connections, ports, ','); + split (_connections, ports, ','); for (vector<string>::iterator x = ports.begin(); x != ports.end(); ++x) { if (_jack_client) { - jack_connect (_jack_client, (*x).c_str(), jack_port_name (_jack_input_port)); + if (receives_input()) { + jack_connect (_jack_client, (*x).c_str(), jack_port_name (_jack_port)); + } else { + jack_connect (_jack_client, jack_port_name (_jack_port), (*x).c_str()); + } /* ignore failures */ } } } - if (!_outbound_connections.empty()) { - vector<string> ports; - split (_outbound_connections, ports, ','); - for (vector<string>::iterator x = ports.begin(); x != ports.end(); ++x) { - if (_jack_client) { - jack_connect (_jack_client, jack_port_name (_jack_output_port), (*x).c_str()); - /* ignore failures */ - } - } - } connect_connection.disconnect (); } @@ -604,7 +501,7 @@ void Port::reestablish (void* jack) { _jack_client = static_cast<jack_client_t*> (jack); - int const r = create_ports (); + int const r = create_port (); if (r) { PBD::error << "could not reregister ports for " << name() << endmsg; |