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 pthread_t Port::_process_thread;
43 Signal0<void> Port::JackHalted;
44 Signal0<void> Port::MakeConnections;
45 string Port::state_node_name = "MIDI-port";
47 Port::Port (string const & name, Flags flags, jack_client_t* jack_client)
48 : _currently_in_cycle (false)
49 , _nframes_this_cycle (0)
50 , _jack_client (jack_client)
52 , _last_read_index (0)
60 Port::Port (const XMLNode& node, jack_client_t* jack_client)
61 : _currently_in_cycle (false)
62 , _nframes_this_cycle (0)
63 , _jack_client (jack_client)
65 , _last_read_index (0)
69 Descriptor desc (node);
71 init (desc.tag, desc.flags);
77 Port::init (string const & name, Flags flags)
79 _ok = false; /* derived class must set to true if constructor
88 _parser = new Parser (*this);
90 for (int i = 0; i < 16; i++) {
91 _channel[i] = new Channel (i, *this);
92 _channel[i]->connect_signals ();
95 if (!create_port ()) {
99 MakeConnections.connect_same_thread (connect_connection, boost::bind (&Port::make_connections, this));
100 JackHalted.connect_same_thread (halt_connection, boost::bind (&Port::jack_halted, this));
106 for (int i = 0; i < 16; i++) {
111 if (_jack_client && _jack_port) {
112 jack_port_unregister (_jack_client, _jack_port);
119 Port::parse (nframes_t timestamp)
123 /* NOTE: parsing is done (if at all) by initiating a read from
124 the port. Each port implementation calls on the parser
125 once it has data ready.
128 _parser->set_timestamp (timestamp);
132 // cerr << "+++ READ ON " << name() << endl;
134 int nread = read (buf, sizeof (buf));
136 // cerr << "-- READ (" << nread << " ON " << name() << endl;
139 if ((size_t) nread < sizeof (buf)) {
144 } else if (nread == 0) {
146 } else if (errno == EAGAIN) {
149 fatal << "Error reading from MIDI port " << name() << endmsg;
155 /** Send a clock tick message.
156 * \return true on success.
159 Port::clock (timestamp_t timestamp)
161 static byte clockmsg = 0xf8;
163 if (sends_output()) {
164 return midimsg (&clockmsg, 1, timestamp);
171 Port::cycle_start (nframes_t nframes)
173 _currently_in_cycle = true;
174 _nframes_this_cycle = nframes;
176 assert(_nframes_this_cycle == nframes);
177 _last_read_index = 0;
178 _last_write_timestamp = 0;
180 if (sends_output()) {
181 void *buffer = jack_port_get_buffer (_jack_port, nframes);
182 jack_midi_clear_buffer (buffer);
186 if (receives_input()) {
187 void* jack_buffer = jack_port_get_buffer(_jack_port, nframes);
188 const nframes_t event_count = jack_midi_get_event_count(jack_buffer);
190 jack_midi_event_t ev;
191 timestamp_t cycle_start_frame = jack_last_frame_time (_jack_client);
193 for (nframes_t i = 0; i < event_count; ++i) {
194 jack_midi_event_get (&ev, jack_buffer, i);
195 input_fifo.write (cycle_start_frame + ev.time, (Evoral::EventType) 0, ev.size, ev.buffer);
207 if (sends_output()) {
208 flush (jack_port_get_buffer (_jack_port, _nframes_this_cycle));
211 _currently_in_cycle = false;
212 _nframes_this_cycle = 0;
215 std::ostream & MIDI::operator << ( std::ostream & os, const MIDI::Port & port )
218 os << "MIDI::Port { ";
219 os << "name: " << port.name();
221 os << "ok: " << port.ok();
227 Port::Descriptor::Descriptor (const XMLNode& node)
229 const XMLProperty *prop;
230 bool have_tag = false;
231 bool have_mode = false;
233 if ((prop = node.property ("tag")) != 0) {
238 if ((prop = node.property ("mode")) != 0) {
240 if (strings_equal_ignore_case (prop->value(), "output") || strings_equal_ignore_case (prop->value(), "out")) {
242 } else if (strings_equal_ignore_case (prop->value(), "input") || strings_equal_ignore_case (prop->value(), "in")) {
249 if (!have_tag || !have_mode) {
250 throw failed_constructor();
262 Port::write(byte * msg, size_t msglen, timestamp_t timestamp)
266 if (!sends_output()) {
270 if (!is_process_thread()) {
272 Glib::Mutex::Lock lm (output_fifo_lock);
273 RingBuffer< Evoral::Event<double> >::rw_vector vec = { { 0, 0 }, { 0, 0} };
275 output_fifo.get_write_vector (&vec);
277 if (vec.len[0] + vec.len[1] < 1) {
278 error << "no space in FIFO for non-process thread MIDI write" << endmsg;
283 if (!vec.buf[0]->owns_buffer()) {
284 vec.buf[0]->set_buffer (0, 0, true);
286 vec.buf[0]->set (msg, msglen, timestamp);
288 if (!vec.buf[1]->owns_buffer()) {
289 vec.buf[1]->set_buffer (0, 0, true);
291 vec.buf[1]->set (msg, msglen, timestamp);
294 output_fifo.increment_write_idx (1);
300 // XXX This had to be temporarily commented out to make export work again
301 if (!(timestamp < _nframes_this_cycle)) {
302 std::cerr << "assertion timestamp < _nframes_this_cycle failed!" << std::endl;
305 if (_currently_in_cycle) {
306 if (timestamp == 0) {
307 timestamp = _last_write_timestamp;
310 if (jack_midi_event_write (jack_port_get_buffer (_jack_port, _nframes_this_cycle),
311 timestamp, msg, msglen) == 0) {
313 _last_write_timestamp = timestamp;
317 cerr << "write of " << msglen << " failed, port holds "
318 << jack_midi_get_event_count (jack_port_get_buffer (_jack_port, _nframes_this_cycle))
322 cerr << "write to JACK midi port failed: not currently in a process cycle." << endl;
326 if (ret > 0 && _parser) {
327 // ardour doesn't care about this and neither should your app, probably
328 // output_parser->raw_preparse (*output_parser, msg, ret);
329 for (int i = 0; i < ret; i++) {
330 _parser->scanner (msg[i]);
332 // ardour doesn't care about this and neither should your app, probably
333 // output_parser->raw_postparse (*output_parser, msg, ret);
340 Port::flush (void* jack_port_buffer)
342 RingBuffer< Evoral::Event<double> >::rw_vector vec = { { 0, 0 }, { 0, 0 } };
345 output_fifo.get_read_vector (&vec);
347 if (vec.len[0] + vec.len[1]) {
348 // cerr << "Flush " << vec.len[0] + vec.len[1] << " events from non-process FIFO\n";
352 Evoral::Event<double>* evp = vec.buf[0];
354 for (size_t n = 0; n < vec.len[0]; ++n, ++evp) {
355 jack_midi_event_write (jack_port_buffer,
356 (timestamp_t) evp->time(), evp->buffer(), evp->size());
361 Evoral::Event<double>* evp = vec.buf[1];
363 for (size_t n = 0; n < vec.len[1]; ++n, ++evp) {
364 jack_midi_event_write (jack_port_buffer,
365 (timestamp_t) evp->time(), evp->buffer(), evp->size());
369 if ((written = vec.len[0] + vec.len[1]) != 0) {
370 output_fifo.increment_read_idx (written);
375 Port::read (byte *, size_t)
377 if (!receives_input()) {
382 Evoral::EventType type;
384 byte buffer[input_fifo.capacity()];
386 while (input_fifo.read (&time, &type, &size, buffer)) {
387 _parser->set_timestamp (time);
388 for (uint32_t i = 0; i < size; ++i) {
389 _parser->scanner (buffer[i]);
399 _jack_port = jack_port_register(_jack_client, _tagname.c_str(), JACK_DEFAULT_MIDI_TYPE, _flags, 0);
401 jack_midi_clear_buffer (jack_port_get_buffer (_jack_port, jack_get_buffer_size (_jack_client)));
404 return _jack_port == 0 ? -1 : 0;
408 Port::get_state () const
410 XMLNode* root = new XMLNode (state_node_name);
411 root->add_property ("tag", _tagname);
413 if (_flags == IsInput) {
414 root->add_property ("mode", "input");
416 root->add_property ("mode", "output");
420 byte device_inquiry[6];
422 device_inquiry[0] = 0xf0;
423 device_inquiry[0] = 0x7e;
424 device_inquiry[0] = 0x7f;
425 device_inquiry[0] = 0x06;
426 device_inquiry[0] = 0x02;
427 device_inquiry[0] = 0xf7;
429 write (device_inquiry, sizeof (device_inquiry), 0);
434 const char** jc = jack_port_get_connections (_jack_port);
435 string connection_string;
437 for (int i = 0; jc[i]; ++i) {
439 connection_string += ',';
441 connection_string += jc[i];
446 if (!connection_string.empty()) {
447 root->add_property ("connections", connection_string);
450 if (!_connections.empty()) {
451 root->add_property ("connections", _connections);
459 Port::set_state (const XMLNode& node)
461 const XMLProperty* prop;
463 if ((prop = node.property ("tag")) == 0 || prop->value() != _tagname) {
467 if ((prop = node.property ("connections")) != 0 && _jack_port) {
468 _connections = prop->value ();
473 Port::make_connections ()
475 if (!_connections.empty()) {
476 vector<string> ports;
477 split (_connections, ports, ',');
478 for (vector<string>::iterator x = ports.begin(); x != ports.end(); ++x) {
480 if (receives_input()) {
481 jack_connect (_jack_client, (*x).c_str(), jack_port_name (_jack_port));
483 jack_connect (_jack_client, jack_port_name (_jack_port), (*x).c_str());
485 /* ignore failures */
490 connect_connection.disconnect ();
494 Port::set_process_thread (pthread_t thr)
496 _process_thread = thr;
500 Port::is_process_thread()
502 return (pthread_self() == _process_thread);
506 Port::reestablish (jack_client_t* jack)
509 int const r = create_port ();
512 PBD::error << "could not reregister ports for " << name() << endmsg;