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"
33 #include "pbd/stacktrace.h"
35 #include "midi++/types.h"
36 #include "midi++/jack_midi_port.h"
37 #include "midi++/channel.h"
43 pthread_t JackMIDIPort::_process_thread;
44 Signal0<void> JackMIDIPort::JackHalted;
45 Signal0<void> JackMIDIPort::MakeConnections;
47 JackMIDIPort::JackMIDIPort (string const & name, Flags flags, jack_client_t* jack_client)
49 , _currently_in_cycle (false)
50 , _nframes_this_cycle (0)
51 , _jack_client (jack_client)
61 JackMIDIPort::JackMIDIPort (const XMLNode& node, jack_client_t* jack_client)
63 , _currently_in_cycle (false)
64 , _nframes_this_cycle (0)
65 , _jack_client (jack_client)
73 Descriptor desc (node);
74 init (desc.tag, desc.flags);
79 JackMIDIPort::init (string const & name, Flags flags)
81 if (!create_port ()) {
85 MakeConnections.connect_same_thread (connect_connection, boost::bind (&JackMIDIPort::make_connections, this));
86 JackHalted.connect_same_thread (halt_connection, boost::bind (&JackMIDIPort::jack_halted, this));
90 JackMIDIPort::~JackMIDIPort ()
92 for (int i = 0; i < 16; i++) {
98 jack_port_unregister (_jack_client, _jack_port);
105 JackMIDIPort::parse (framecnt_t timestamp)
109 /* NOTE: parsing is done (if at all) by initiating a read from
110 the port. Each port implementation calls on the parser
111 once it has data ready.
114 _parser->set_timestamp (timestamp);
118 // cerr << "+++ READ ON " << name() << endl;
120 int nread = read (buf, sizeof (buf));
122 // cerr << "-- READ (" << nread << " ON " << name() << endl;
125 if ((size_t) nread < sizeof (buf)) {
130 } else if (nread == 0) {
132 } else if (errno == EAGAIN) {
135 fatal << "Error reading from MIDI port " << name() << endmsg;
142 JackMIDIPort::cycle_start (pframes_t nframes)
146 _currently_in_cycle = true;
147 _nframes_this_cycle = nframes;
149 assert(_nframes_this_cycle == nframes);
151 if (sends_output()) {
152 void *buffer = jack_port_get_buffer (_jack_port, nframes);
153 jack_midi_clear_buffer (buffer);
157 if (receives_input()) {
158 void* jack_buffer = jack_port_get_buffer(_jack_port, nframes);
159 const pframes_t event_count = jack_midi_get_event_count(jack_buffer);
161 jack_midi_event_t ev;
162 timestamp_t cycle_start_frame = jack_last_frame_time (_jack_client);
164 for (pframes_t i = 0; i < event_count; ++i) {
165 jack_midi_event_get (&ev, jack_buffer, i);
166 input_fifo.write (cycle_start_frame + ev.time, (Evoral::EventType) 0, ev.size, ev.buffer);
176 JackMIDIPort::cycle_end ()
178 if (sends_output()) {
179 flush (jack_port_get_buffer (_jack_port, _nframes_this_cycle));
182 _currently_in_cycle = false;
183 _nframes_this_cycle = 0;
187 JackMIDIPort::jack_halted ()
194 JackMIDIPort::drain (int check_interval_usecs)
196 RingBuffer< Evoral::Event<double> >::rw_vector vec = { { 0, 0 }, { 0, 0} };
198 if (is_process_thread()) {
199 error << "Process thread called MIDI::JackMIDIPort::drain() - this cannot work" << endmsg;
204 output_fifo.get_write_vector (&vec);
205 if (vec.len[0] + vec.len[1] >= output_fifo.bufsize() - 1) {
208 usleep (check_interval_usecs);
213 JackMIDIPort::write(byte * msg, size_t msglen, timestamp_t timestamp)
217 if (!sends_output()) {
221 if (!is_process_thread()) {
223 Glib::Mutex::Lock lm (output_fifo_lock);
224 RingBuffer< Evoral::Event<double> >::rw_vector vec = { { 0, 0 }, { 0, 0} };
226 output_fifo.get_write_vector (&vec);
228 if (vec.len[0] + vec.len[1] < 1) {
229 error << "no space in FIFO for non-process thread MIDI write" << endmsg;
234 if (!vec.buf[0]->owns_buffer()) {
235 vec.buf[0]->set_buffer (0, 0, true);
237 vec.buf[0]->set (msg, msglen, timestamp);
239 if (!vec.buf[1]->owns_buffer()) {
240 vec.buf[1]->set_buffer (0, 0, true);
242 vec.buf[1]->set (msg, msglen, timestamp);
245 output_fifo.increment_write_idx (1);
251 // XXX This had to be temporarily commented out to make export work again
252 if (!(timestamp < _nframes_this_cycle)) {
253 std::cerr << "attempting to write MIDI event of " << msglen << " bytes at time "
254 << timestamp << " of " << _nframes_this_cycle
255 << " (this will not work - needs a code fix)"
259 if (_currently_in_cycle) {
260 if (timestamp == 0) {
261 timestamp = _last_write_timestamp;
264 if (jack_midi_event_write (jack_port_get_buffer (_jack_port, _nframes_this_cycle),
265 timestamp, msg, msglen) == 0) {
267 _last_write_timestamp = timestamp;
271 cerr << "write of " << msglen << " failed, port holds "
272 << jack_midi_get_event_count (jack_port_get_buffer (_jack_port, _nframes_this_cycle))
276 cerr << "write to JACK midi port failed: not currently in a process cycle." << endl;
277 PBD::stacktrace (cerr, 20);
281 if (ret > 0 && _parser) {
282 // ardour doesn't care about this and neither should your app, probably
283 // output_parser->raw_preparse (*output_parser, msg, ret);
284 for (int i = 0; i < ret; i++) {
285 _parser->scanner (msg[i]);
287 // ardour doesn't care about this and neither should your app, probably
288 // output_parser->raw_postparse (*output_parser, msg, ret);
295 JackMIDIPort::flush (void* jack_port_buffer)
297 RingBuffer< Evoral::Event<double> >::rw_vector vec = { { 0, 0 }, { 0, 0 } };
300 output_fifo.get_read_vector (&vec);
302 if (vec.len[0] + vec.len[1]) {
303 // cerr << "Flush " << vec.len[0] + vec.len[1] << " events from non-process FIFO\n";
307 Evoral::Event<double>* evp = vec.buf[0];
309 for (size_t n = 0; n < vec.len[0]; ++n, ++evp) {
310 jack_midi_event_write (jack_port_buffer,
311 (timestamp_t) evp->time(), evp->buffer(), evp->size());
316 Evoral::Event<double>* evp = vec.buf[1];
318 for (size_t n = 0; n < vec.len[1]; ++n, ++evp) {
319 jack_midi_event_write (jack_port_buffer,
320 (timestamp_t) evp->time(), evp->buffer(), evp->size());
324 if ((written = vec.len[0] + vec.len[1]) != 0) {
325 output_fifo.increment_read_idx (written);
330 JackMIDIPort::read (byte *, size_t)
332 if (!receives_input()) {
337 Evoral::EventType type;
339 byte buffer[input_fifo.capacity()];
341 while (input_fifo.read (&time, &type, &size, buffer)) {
342 _parser->set_timestamp (time);
343 for (uint32_t i = 0; i < size; ++i) {
344 _parser->scanner (buffer[i]);
352 JackMIDIPort::create_port ()
354 _jack_port = jack_port_register(_jack_client, _tagname.c_str(), JACK_DEFAULT_MIDI_TYPE, _flags, 0);
355 return _jack_port == 0 ? -1 : 0;
359 JackMIDIPort::get_state () const
361 XMLNode& root = Port::get_state ();
364 byte device_inquiry[6];
366 device_inquiry[0] = 0xf0;
367 device_inquiry[0] = 0x7e;
368 device_inquiry[0] = 0x7f;
369 device_inquiry[0] = 0x06;
370 device_inquiry[0] = 0x02;
371 device_inquiry[0] = 0xf7;
373 write (device_inquiry, sizeof (device_inquiry), 0);
378 const char** jc = jack_port_get_connections (_jack_port);
379 string connection_string;
381 for (int i = 0; jc[i]; ++i) {
383 connection_string += ',';
385 connection_string += jc[i];
390 if (!connection_string.empty()) {
391 root.add_property ("connections", connection_string);
394 if (!_connections.empty()) {
395 root.add_property ("connections", _connections);
403 JackMIDIPort::set_state (const XMLNode& node)
405 const XMLProperty* prop;
407 if ((prop = node.property ("tag")) == 0 || prop->value() != _tagname) {
411 Port::set_state (node);
413 if ((prop = node.property ("connections")) != 0) {
414 _connections = prop->value ();
419 JackMIDIPort::make_connections ()
421 if (!_connections.empty()) {
422 vector<string> ports;
423 split (_connections, ports, ',');
424 for (vector<string>::iterator x = ports.begin(); x != ports.end(); ++x) {
426 if (receives_input()) {
427 jack_connect (_jack_client, (*x).c_str(), jack_port_name (_jack_port));
429 jack_connect (_jack_client, jack_port_name (_jack_port), (*x).c_str());
431 /* ignore failures */
436 connect_connection.disconnect ();
440 JackMIDIPort::set_process_thread (pthread_t thr)
442 _process_thread = thr;
446 JackMIDIPort::is_process_thread()
448 return (pthread_self() == _process_thread);
452 JackMIDIPort::reestablish (jack_client_t* jack)
455 int const r = create_port ();
458 PBD::error << "could not reregister ports for " << name() << endmsg;
463 JackMIDIPort::reconnect ()