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.
26 #include <jack/jack.h>
27 #include <jack/midiport.h>
29 #include "pbd/xml++.h"
30 #include "pbd/error.h"
31 #include "pbd/failed_constructor.h"
32 #include "pbd/convert.h"
33 #include "pbd/strsplit.h"
34 #include "pbd/stacktrace.h"
36 #include "midi++/types.h"
37 #include "midi++/jack_midi_port.h"
38 #include "midi++/channel.h"
45 template class EventRingBuffer<timestamp_t>;
48 pthread_t JackMIDIPort::_process_thread;
49 Signal0<void> JackMIDIPort::JackHalted;
50 Signal0<void> JackMIDIPort::MakeConnections;
52 JackMIDIPort::JackMIDIPort (string const & name, Flags flags, jack_client_t* jack_client)
54 , _currently_in_cycle (false)
55 , _nframes_this_cycle (0)
56 , _jack_client (jack_client)
58 , _last_write_timestamp (0)
69 JackMIDIPort::JackMIDIPort (const XMLNode& node, jack_client_t* jack_client)
71 , _currently_in_cycle (false)
72 , _nframes_this_cycle (0)
73 , _jack_client (jack_client)
75 , _last_write_timestamp (0)
84 Descriptor desc (node);
85 init (desc.tag, desc.flags);
90 JackMIDIPort::init (const string& /*name*/, Flags /*flags*/)
92 if (!create_port ()) {
96 MakeConnections.connect_same_thread (connect_connection, boost::bind (&JackMIDIPort::make_connections, this));
97 JackHalted.connect_same_thread (halt_connection, boost::bind (&JackMIDIPort::jack_halted, this));
101 JackMIDIPort::~JackMIDIPort ()
105 jack_port_unregister (_jack_client, _jack_port);
112 JackMIDIPort::parse (framecnt_t timestamp)
116 /* NOTE: parsing is done (if at all) by initiating a read from
117 the port. Each port implementation calls on the parser
118 once it has data ready.
121 _parser->set_timestamp (timestamp);
125 // cerr << "+++ READ ON " << name() << endl;
127 int nread = read (buf, sizeof (buf));
129 // cerr << "-- READ (" << nread << " ON " << name() << endl;
132 if ((size_t) nread < sizeof (buf)) {
137 } else if (nread == 0) {
139 } else if (errno == EAGAIN) {
142 fatal << "Error reading from MIDI port " << name() << endmsg;
149 JackMIDIPort::cycle_start (pframes_t nframes)
153 _currently_in_cycle = true;
154 _nframes_this_cycle = nframes;
156 assert(_nframes_this_cycle == nframes);
158 if (sends_output()) {
159 void *buffer = jack_port_get_buffer (_jack_port, nframes);
160 jack_midi_clear_buffer (buffer);
164 if (receives_input()) {
165 void* jack_buffer = jack_port_get_buffer(_jack_port, nframes);
166 const pframes_t event_count = jack_midi_get_event_count(jack_buffer);
168 jack_midi_event_t ev;
169 timestamp_t cycle_start_frame = jack_last_frame_time (_jack_client);
171 for (pframes_t i = 0; i < event_count; ++i) {
172 jack_midi_event_get (&ev, jack_buffer, i);
173 input_fifo.write (cycle_start_frame + ev.time, (Evoral::EventType) 0, ev.size, ev.buffer);
185 JackMIDIPort::cycle_end ()
187 if (sends_output()) {
188 flush (jack_port_get_buffer (_jack_port, _nframes_this_cycle));
191 _currently_in_cycle = false;
192 _nframes_this_cycle = 0;
196 JackMIDIPort::jack_halted ()
203 JackMIDIPort::drain (int check_interval_usecs)
205 RingBuffer< Evoral::Event<double> >::rw_vector vec = { { 0, 0 }, { 0, 0} };
207 if (is_process_thread()) {
208 error << "Process thread called MIDI::JackMIDIPort::drain() - this cannot work" << endmsg;
213 output_fifo.get_write_vector (&vec);
214 if (vec.len[0] + vec.len[1] >= output_fifo.bufsize() - 1) {
217 usleep (check_interval_usecs);
222 JackMIDIPort::write (const byte * msg, size_t msglen, timestamp_t timestamp)
226 if (!_jack_client || !_jack_port) {
227 /* poof ! make it just vanish into thin air, since we are no
228 longer connected to JACK.
233 if (!sends_output()) {
237 if (!is_process_thread()) {
239 Glib::Threads::Mutex::Lock lm (output_fifo_lock);
240 RingBuffer< Evoral::Event<double> >::rw_vector vec = { { 0, 0 }, { 0, 0} };
242 output_fifo.get_write_vector (&vec);
244 if (vec.len[0] + vec.len[1] < 1) {
245 error << "no space in FIFO for non-process thread MIDI write" << endmsg;
250 if (!vec.buf[0]->owns_buffer()) {
251 vec.buf[0]->set_buffer (0, 0, true);
253 vec.buf[0]->set (msg, msglen, timestamp);
255 if (!vec.buf[1]->owns_buffer()) {
256 vec.buf[1]->set_buffer (0, 0, true);
258 vec.buf[1]->set (msg, msglen, timestamp);
261 output_fifo.increment_write_idx (1);
267 if (timestamp >= _nframes_this_cycle) {
268 std::cerr << "attempting to write MIDI event of " << msglen << " bytes at time "
269 << timestamp << " of " << _nframes_this_cycle
270 << " (this will not work - needs a code fix)"
274 if (_currently_in_cycle) {
275 if (timestamp == 0) {
276 timestamp = _last_write_timestamp;
279 if ((ret = jack_midi_event_write (jack_port_get_buffer (_jack_port, _nframes_this_cycle),
280 timestamp, msg, msglen)) == 0) {
282 _last_write_timestamp = timestamp;
285 cerr << "write of " << msglen << " @ " << timestamp << " failed, port holds "
286 << jack_midi_get_event_count (jack_port_get_buffer (_jack_port, _nframes_this_cycle))
287 << " port is " << _jack_port
288 << " ntf = " << _nframes_this_cycle
289 << " buf = " << jack_port_get_buffer (_jack_port, _nframes_this_cycle)
292 PBD::stacktrace (cerr, 20);
296 cerr << "write to JACK midi port failed: not currently in a process cycle." << endl;
297 PBD::stacktrace (cerr, 20);
301 if (ret > 0 && _parser) {
302 // ardour doesn't care about this and neither should your app, probably
303 // output_parser->raw_preparse (*output_parser, msg, ret);
304 for (int i = 0; i < ret; i++) {
305 _parser->scanner (msg[i]);
307 // ardour doesn't care about this and neither should your app, probably
308 // output_parser->raw_postparse (*output_parser, msg, ret);
315 JackMIDIPort::flush (void* jack_port_buffer)
317 RingBuffer< Evoral::Event<double> >::rw_vector vec = { { 0, 0 }, { 0, 0 } };
320 output_fifo.get_read_vector (&vec);
322 if (vec.len[0] + vec.len[1]) {
323 // cerr << "Flush " << vec.len[0] + vec.len[1] << " events from non-process FIFO\n";
327 Evoral::Event<double>* evp = vec.buf[0];
329 for (size_t n = 0; n < vec.len[0]; ++n, ++evp) {
330 jack_midi_event_write (jack_port_buffer,
331 (timestamp_t) evp->time(), evp->buffer(), evp->size());
336 Evoral::Event<double>* evp = vec.buf[1];
338 for (size_t n = 0; n < vec.len[1]; ++n, ++evp) {
339 jack_midi_event_write (jack_port_buffer,
340 (timestamp_t) evp->time(), evp->buffer(), evp->size());
344 if ((written = vec.len[0] + vec.len[1]) != 0) {
345 output_fifo.increment_read_idx (written);
350 JackMIDIPort::read (byte *, size_t)
352 if (!receives_input()) {
357 Evoral::EventType type;
359 byte buffer[input_fifo.capacity()];
361 while (input_fifo.read (&time, &type, &size, buffer)) {
362 _parser->set_timestamp (time);
363 for (uint32_t i = 0; i < size; ++i) {
364 _parser->scanner (buffer[i]);
372 JackMIDIPort::create_port ()
374 _jack_port = jack_port_register(_jack_client, _tagname.c_str(), JACK_DEFAULT_MIDI_TYPE, _flags, 0);
375 return _jack_port == 0 ? -1 : 0;
379 JackMIDIPort::get_state () const
381 XMLNode& root = Port::get_state ();
384 byte device_inquiry[6];
386 device_inquiry[0] = 0xf0;
387 device_inquiry[0] = 0x7e;
388 device_inquiry[0] = 0x7f;
389 device_inquiry[0] = 0x06;
390 device_inquiry[0] = 0x02;
391 device_inquiry[0] = 0xf7;
393 write (device_inquiry, sizeof (device_inquiry), 0);
398 const char** jc = jack_port_get_connections (_jack_port);
399 string connection_string;
401 for (int i = 0; jc[i]; ++i) {
403 connection_string += ',';
405 connection_string += jc[i];
410 if (!connection_string.empty()) {
411 root.add_property ("connections", connection_string);
414 if (!_connections.empty()) {
415 root.add_property ("connections", _connections);
423 JackMIDIPort::set_state (const XMLNode& node)
425 const XMLProperty* prop;
427 if ((prop = node.property ("tag")) == 0 || prop->value() != _tagname) {
431 Port::set_state (node);
433 if ((prop = node.property ("connections")) != 0) {
434 _connections = prop->value ();
439 JackMIDIPort::make_connections ()
441 if (!_connections.empty()) {
442 vector<string> ports;
443 split (_connections, ports, ',');
444 for (vector<string>::iterator x = ports.begin(); x != ports.end(); ++x) {
446 if (receives_input()) {
447 jack_connect (_jack_client, (*x).c_str(), jack_port_name (_jack_port));
449 jack_connect (_jack_client, jack_port_name (_jack_port), (*x).c_str());
451 /* ignore failures */
456 connect_connection.disconnect ();
460 JackMIDIPort::set_process_thread (pthread_t thr)
462 _process_thread = thr;
466 JackMIDIPort::is_process_thread()
468 return (pthread_equal(pthread_self(), _process_thread));
472 JackMIDIPort::reestablish (jack_client_t* jack)
475 int const r = create_port ();
478 PBD::error << "could not reregister ports for " << name() << endmsg;
483 JackMIDIPort::reconnect ()