2 Copyright (C) 1998 Paul Barton-Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include <jack/jack.h>
26 #include <jack/midiport.h>
28 #include "pbd/xml++.h"
29 #include "pbd/error.h"
30 #include "pbd/failed_constructor.h"
31 #include "pbd/convert.h"
32 #include "pbd/strsplit.h"
34 #include "midi++/types.h"
35 #include "midi++/port.h"
36 #include "midi++/channel.h"
42 size_t Port::nports = 0;
43 pthread_t Port::_process_thread;
44 Signal0<void> Port::JackHalted;
45 Signal0<void> Port::MakeConnections;
47 Port::Port (const XMLNode& node, jack_client_t* jack_client)
48 : _currently_in_cycle (false)
49 , _nframes_this_cycle (0)
50 , _jack_client (jack_client)
51 , _jack_input_port (0)
52 , _jack_output_port (0)
53 , _last_read_index (0)
57 Descriptor desc (node);
59 _ok = false; /* derived class must set to true if constructor
69 if (_mode == O_RDONLY || _mode == O_RDWR) {
70 input_parser = new Parser (*this);
75 if (_mode == O_WRONLY || _mode == O_RDWR) {
76 output_parser = new Parser (*this);
81 for (int i = 0; i < 16; i++) {
82 _channel[i] = new Channel (i, *this);
85 _channel[i]->connect_input_signals ();
89 _channel[i]->connect_output_signals ();
93 if (!create_ports (node)) {
97 MakeConnections.connect_same_thread (connect_connection, boost::bind (&Port::make_connections, this));
98 JackHalted.connect_same_thread (halt_connection, boost::bind (&Port::jack_halted, this));
106 for (int i = 0; i < 16; i++) {
110 if (_jack_input_port) {
112 jack_port_unregister (_jack_client, _jack_input_port);
114 _jack_input_port = 0;
117 if (_jack_output_port) {
119 jack_port_unregister (_jack_client, _jack_input_port);
121 _jack_input_port = 0;
126 Port::parse (nframes_t timestamp)
130 /* NOTE: parsing is done (if at all) by initiating a read from
131 the port. Each port implementation calls on the parser
132 once it has data ready.
136 input_parser->set_timestamp (timestamp);
141 // cerr << "+++ READ ON " << name() << endl;
143 int nread = read (buf, sizeof (buf));
145 // cerr << "-- READ (" << nread << " ON " << name() << endl;
148 if ((size_t) nread < sizeof (buf)) {
153 } else if (nread == 0) {
155 } else if (errno == EAGAIN) {
158 fatal << "Error reading from MIDI port " << name() << endmsg;
164 /** Send a clock tick message.
165 * \return true on success.
168 Port::clock (timestamp_t timestamp)
170 static byte clockmsg = 0xf8;
172 if (_mode != O_RDONLY) {
173 return midimsg (&clockmsg, 1, timestamp);
180 Port::cycle_start (nframes_t nframes)
182 _currently_in_cycle = true;
183 _nframes_this_cycle = nframes;
185 assert(_nframes_this_cycle == nframes);
186 _last_read_index = 0;
187 _last_write_timestamp = 0;
189 if (_jack_output_port != 0) {
191 void *buffer = jack_port_get_buffer (_jack_output_port, nframes);
192 jack_midi_clear_buffer (buffer);
196 if (_jack_input_port != 0) {
198 void* jack_buffer = jack_port_get_buffer(_jack_input_port, nframes);
199 const nframes_t event_count = jack_midi_get_event_count(jack_buffer);
201 jack_midi_event_t ev;
202 timestamp_t cycle_start_frame = jack_last_frame_time (_jack_client);
204 for (nframes_t i = 0; i < event_count; ++i) {
205 jack_midi_event_get (&ev, jack_buffer, i);
206 input_fifo.write (cycle_start_frame + ev.time, (Evoral::EventType) 0, ev.size, ev.buffer);
218 if (_jack_output_port != 0) {
219 flush (jack_port_get_buffer (_jack_output_port, _nframes_this_cycle));
222 _currently_in_cycle = false;
223 _nframes_this_cycle = 0;
226 std::ostream & MIDI::operator << ( std::ostream & os, const MIDI::Port & port )
229 os << "MIDI::Port { ";
230 os << "name: " << port.name();
232 os << "mode: " << port.mode();
234 os << "ok: " << port.ok();
240 Port::Descriptor::Descriptor (const XMLNode& node)
242 const XMLProperty *prop;
243 bool have_tag = false;
244 bool have_mode = false;
246 if ((prop = node.property ("tag")) != 0) {
251 if ((prop = node.property ("mode")) != 0) {
255 if (strings_equal_ignore_case (prop->value(), "output") || strings_equal_ignore_case (prop->value(), "out")) {
257 } else if (strings_equal_ignore_case (prop->value(), "input") || strings_equal_ignore_case (prop->value(), "in")) {
264 if (!have_tag || !have_mode) {
265 throw failed_constructor();
273 _jack_input_port = 0;
274 _jack_output_port = 0;
278 Port::write(byte * msg, size_t msglen, timestamp_t timestamp)
282 if (!_jack_output_port) {
286 if (!is_process_thread()) {
288 Glib::Mutex::Lock lm (output_fifo_lock);
289 RingBuffer< Evoral::Event<double> >::rw_vector vec = { { 0, 0 }, { 0, 0} };
291 output_fifo.get_write_vector (&vec);
293 if (vec.len[0] + vec.len[1] < 1) {
294 error << "no space in FIFO for non-process thread MIDI write" << endmsg;
299 if (!vec.buf[0]->owns_buffer()) {
300 vec.buf[0]->set_buffer (0, 0, true);
302 vec.buf[0]->set (msg, msglen, timestamp);
304 if (!vec.buf[1]->owns_buffer()) {
305 vec.buf[1]->set_buffer (0, 0, true);
307 vec.buf[1]->set (msg, msglen, timestamp);
310 output_fifo.increment_write_idx (1);
316 // XXX This had to be temporarily commented out to make export work again
317 if (!(timestamp < _nframes_this_cycle)) {
318 std::cerr << "assertion timestamp < _nframes_this_cycle failed!" << std::endl;
321 if (_currently_in_cycle) {
322 if (timestamp == 0) {
323 timestamp = _last_write_timestamp;
326 if (jack_midi_event_write (jack_port_get_buffer (_jack_output_port, _nframes_this_cycle),
327 timestamp, msg, msglen) == 0) {
329 _last_write_timestamp = timestamp;
333 cerr << "write of " << msglen << " failed, port holds "
334 << jack_midi_get_event_count (jack_port_get_buffer (_jack_output_port, _nframes_this_cycle))
338 cerr << "write to JACK midi port failed: not currently in a process cycle." << endl;
342 if (ret > 0 && output_parser) {
343 // ardour doesn't care about this and neither should your app, probably
344 // output_parser->raw_preparse (*output_parser, msg, ret);
345 for (int i = 0; i < ret; i++) {
346 output_parser->scanner (msg[i]);
348 // ardour doesn't care about this and neither should your app, probably
349 // output_parser->raw_postparse (*output_parser, msg, ret);
356 Port::flush (void* jack_port_buffer)
358 RingBuffer< Evoral::Event<double> >::rw_vector vec = { { 0, 0 }, { 0, 0 } };
361 output_fifo.get_read_vector (&vec);
363 if (vec.len[0] + vec.len[1]) {
364 // cerr << "Flush " << vec.len[0] + vec.len[1] << " events from non-process FIFO\n";
368 Evoral::Event<double>* evp = vec.buf[0];
370 for (size_t n = 0; n < vec.len[0]; ++n, ++evp) {
371 jack_midi_event_write (jack_port_buffer,
372 (timestamp_t) evp->time(), evp->buffer(), evp->size());
377 Evoral::Event<double>* evp = vec.buf[1];
379 for (size_t n = 0; n < vec.len[1]; ++n, ++evp) {
380 jack_midi_event_write (jack_port_buffer,
381 (timestamp_t) evp->time(), evp->buffer(), evp->size());
385 if ((written = vec.len[0] + vec.len[1]) != 0) {
386 output_fifo.increment_read_idx (written);
391 Port::read (byte *, size_t)
394 Evoral::EventType type;
396 byte buffer[input_fifo.capacity()];
398 while (input_fifo.read (&time, &type, &size, buffer)) {
400 input_parser->set_timestamp (time);
401 for (uint32_t i = 0; i < size; ++i) {
402 input_parser->scanner (buffer[i]);
411 Port::create_ports(const XMLNode& node)
413 Descriptor desc (node);
415 assert(!_jack_input_port);
416 assert(!_jack_output_port);
418 if (desc.mode == O_RDWR || desc.mode == O_WRONLY) {
419 _jack_output_port_name = string(desc.tag).append ("_out");
422 if (desc.mode == O_RDWR || desc.mode == O_RDONLY) {
423 _jack_input_port_name = string(desc.tag).append ("_in");
426 return create_ports ();
430 Port::create_ports ()
434 jack_nframes_t nframes = jack_get_buffer_size(_jack_client);
436 if (!_jack_output_port_name.empty()) {
437 _jack_output_port = jack_port_register(_jack_client, _jack_output_port_name.c_str(),
438 JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0);
439 if (_jack_output_port) {
440 jack_midi_clear_buffer(jack_port_get_buffer(_jack_output_port, nframes));
442 ret = ret && (_jack_output_port != NULL);
445 if (!_jack_input_port_name.empty()) {
446 _jack_input_port = jack_port_register(_jack_client, _jack_input_port_name.c_str(),
447 JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0);
448 if (_jack_input_port) {
449 jack_midi_clear_buffer(jack_port_get_buffer(_jack_input_port, nframes));
451 ret = ret && (_jack_input_port != NULL);
458 Port::get_state () const
460 XMLNode* root = new XMLNode ("MIDI-port");
461 root->add_property ("tag", _tagname);
463 if (_mode == O_RDONLY) {
464 root->add_property ("mode", "input");
465 } else if (_mode == O_WRONLY) {
466 root->add_property ("mode", "output");
468 root->add_property ("mode", "duplex");
472 byte device_inquiry[6];
474 device_inquiry[0] = 0xf0;
475 device_inquiry[0] = 0x7e;
476 device_inquiry[0] = 0x7f;
477 device_inquiry[0] = 0x06;
478 device_inquiry[0] = 0x02;
479 device_inquiry[0] = 0xf7;
481 write (device_inquiry, sizeof (device_inquiry), 0);
484 if (_jack_output_port) {
486 const char** jc = jack_port_get_connections (_jack_output_port);
487 string connection_string;
489 for (int i = 0; jc[i]; ++i) {
491 connection_string += ',';
493 connection_string += jc[i];
498 if (!connection_string.empty()) {
499 root->add_property ("outbound", connection_string);
502 if (!_outbound_connections.empty()) {
503 root->add_property ("outbound", _outbound_connections);
507 if (_jack_input_port) {
509 const char** jc = jack_port_get_connections (_jack_input_port);
510 string connection_string;
512 for (int i = 0; jc[i]; ++i) {
514 connection_string += ',';
516 connection_string += jc[i];
521 if (!connection_string.empty()) {
522 root->add_property ("inbound", connection_string);
525 if (!_inbound_connections.empty()) {
526 root->add_property ("inbound", _inbound_connections);
534 Port::set_state (const XMLNode& node)
536 const XMLProperty* prop;
538 if ((prop = node.property ("inbound")) != 0 && _jack_input_port) {
539 _inbound_connections = prop->value ();
542 if ((prop = node.property ("outbound")) != 0 && _jack_output_port) {
543 _outbound_connections = prop->value();
548 Port::make_connections ()
550 if (!_inbound_connections.empty()) {
551 vector<string> ports;
552 split (_inbound_connections, ports, ',');
553 for (vector<string>::iterator x = ports.begin(); x != ports.end(); ++x) {
555 jack_connect (_jack_client, (*x).c_str(), jack_port_name (_jack_input_port));
556 /* ignore failures */
561 if (!_outbound_connections.empty()) {
562 vector<string> ports;
563 split (_outbound_connections, ports, ',');
564 for (vector<string>::iterator x = ports.begin(); x != ports.end(); ++x) {
566 jack_connect (_jack_client, jack_port_name (_jack_output_port), (*x).c_str());
567 /* ignore failures */
571 connect_connection.disconnect ();
575 Port::set_process_thread (pthread_t thr)
577 _process_thread = thr;
581 Port::is_process_thread()
583 return (pthread_self() == _process_thread);
587 Port::reestablish (void* jack)
589 _jack_client = static_cast<jack_client_t*> (jack);
590 int const r = create_ports ();
593 PBD::error << "could not reregister ports for " << name() << endmsg;