2 Copyright (C) 2006 Paul Davis
3 Written by Dave Robillard
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include "pbd/error.h"
27 #include "pbd/compose.h"
28 #include "pbd/strsplit.h"
30 #include "midi++/types.h"
31 #include "midi++/jack.h"
37 pthread_t JACK_MidiPort::_process_thread;
39 Signal0<void> JACK_MidiPort::JackHalted;
40 Signal0<void> JACK_MidiPort::MakeConnections;
42 JACK_MidiPort::JACK_MidiPort(const XMLNode& node, jack_client_t* jack_client)
44 , _jack_client(jack_client)
45 , _jack_input_port(NULL)
46 , _jack_output_port(NULL)
51 if (!create_ports (node)) {
55 MakeConnections.connect_same_thread (connect_connection, boost::bind (&JACK_MidiPort::make_connections, this));
56 JackHalted.connect_same_thread (halt_connection, boost::bind (&JACK_MidiPort::jack_halted, this));
61 JACK_MidiPort::~JACK_MidiPort()
63 if (_jack_input_port) {
65 jack_port_unregister (_jack_client, _jack_input_port);
70 if (_jack_output_port) {
72 jack_port_unregister (_jack_client, _jack_input_port);
79 JACK_MidiPort::jack_halted ()
83 _jack_output_port = 0;
87 JACK_MidiPort::cycle_start (nframes_t nframes)
89 Port::cycle_start(nframes);
90 assert(_nframes_this_cycle == nframes);
92 _last_write_timestamp = 0;
94 if (_jack_output_port != 0) {
96 void *buffer = jack_port_get_buffer (_jack_output_port, nframes);
97 jack_midi_clear_buffer (buffer);
101 if (_jack_input_port != 0) {
103 void* jack_buffer = jack_port_get_buffer(_jack_input_port, nframes);
104 const nframes_t event_count = jack_midi_get_event_count(jack_buffer);
106 jack_midi_event_t ev;
107 timestamp_t cycle_start_frame = jack_last_frame_time (_jack_client);
109 for (nframes_t i = 0; i < event_count; ++i) {
110 jack_midi_event_get (&ev, jack_buffer, i);
111 input_fifo.write (cycle_start_frame + ev.time, (Evoral::EventType) 0, ev.size, ev.buffer);
121 JACK_MidiPort::cycle_end ()
123 if (_jack_output_port != 0) {
124 flush (jack_port_get_buffer (_jack_output_port, _nframes_this_cycle));
131 JACK_MidiPort::write(byte * msg, size_t msglen, timestamp_t timestamp)
135 if (!is_process_thread()) {
137 Glib::Mutex::Lock lm (output_fifo_lock);
138 RingBuffer< Evoral::Event<double> >::rw_vector vec = { { 0, 0 }, { 0, 0} };
140 output_fifo.get_write_vector (&vec);
142 if (vec.len[0] + vec.len[1] < 1) {
143 error << "no space in FIFO for non-process thread MIDI write" << endmsg;
148 if (!vec.buf[0]->owns_buffer()) {
149 vec.buf[0]->set_buffer (0, 0, true);
151 vec.buf[0]->set (msg, msglen, timestamp);
153 if (!vec.buf[1]->owns_buffer()) {
154 vec.buf[1]->set_buffer (0, 0, true);
156 vec.buf[1]->set (msg, msglen, timestamp);
159 output_fifo.increment_write_idx (1);
165 assert(_jack_output_port);
167 // XXX This had to be temporarily commented out to make export work again
168 if (!(timestamp < _nframes_this_cycle)) {
169 std::cerr << "assertion timestamp < _nframes_this_cycle failed!" << std::endl;
172 if (_currently_in_cycle) {
173 if (timestamp == 0) {
174 timestamp = _last_write_timestamp;
177 if (jack_midi_event_write (jack_port_get_buffer (_jack_output_port, _nframes_this_cycle),
178 timestamp, msg, msglen) == 0) {
180 _last_write_timestamp = timestamp;
184 cerr << "write of " << msglen << " failed, port holds "
185 << jack_midi_get_event_count (jack_port_get_buffer (_jack_output_port, _nframes_this_cycle))
189 cerr << "write to JACK midi port failed: not currently in a process cycle." << endl;
193 if (ret > 0 && output_parser) {
194 // ardour doesn't care about this and neither should your app, probably
195 // output_parser->raw_preparse (*output_parser, msg, ret);
196 for (int i = 0; i < ret; i++) {
197 output_parser->scanner (msg[i]);
199 // ardour doesn't care about this and neither should your app, probably
200 // output_parser->raw_postparse (*output_parser, msg, ret);
207 JACK_MidiPort::flush (void* jack_port_buffer)
209 RingBuffer< Evoral::Event<double> >::rw_vector vec = { { 0, 0 }, { 0, 0 } };
212 output_fifo.get_read_vector (&vec);
214 if (vec.len[0] + vec.len[1]) {
215 // cerr << "Flush " << vec.len[0] + vec.len[1] << " events from non-process FIFO\n";
219 Evoral::Event<double>* evp = vec.buf[0];
221 for (size_t n = 0; n < vec.len[0]; ++n, ++evp) {
222 jack_midi_event_write (jack_port_buffer,
223 (timestamp_t) evp->time(), evp->buffer(), evp->size());
228 Evoral::Event<double>* evp = vec.buf[1];
230 for (size_t n = 0; n < vec.len[1]; ++n, ++evp) {
231 jack_midi_event_write (jack_port_buffer,
232 (timestamp_t) evp->time(), evp->buffer(), evp->size());
236 if ((written = vec.len[0] + vec.len[1]) != 0) {
237 output_fifo.increment_read_idx (written);
242 JACK_MidiPort::read (byte *, size_t)
245 Evoral::EventType type;
247 byte buffer[input_fifo.capacity()];
249 while (input_fifo.read (&time, &type, &size, buffer)) {
251 input_parser->set_timestamp (time);
252 for (uint32_t i = 0; i < size; ++i) {
253 input_parser->scanner (buffer[i]);
262 JACK_MidiPort::create_ports(const XMLNode& node)
264 Descriptor desc (node);
266 assert(!_jack_input_port);
267 assert(!_jack_output_port);
269 jack_nframes_t nframes = jack_get_buffer_size(_jack_client);
273 if (desc.mode == O_RDWR || desc.mode == O_WRONLY) {
274 _jack_output_port = jack_port_register(_jack_client,
275 string(desc.tag).append("_out").c_str(),
276 JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0);
277 if (_jack_output_port) {
278 jack_midi_clear_buffer(jack_port_get_buffer(_jack_output_port, nframes));
280 ret = ret && (_jack_output_port != NULL);
283 if (desc.mode == O_RDWR || desc.mode == O_RDONLY) {
284 _jack_input_port = jack_port_register(_jack_client,
285 string(desc.tag).append("_in").c_str(),
286 JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0);
287 if (_jack_input_port) {
288 jack_midi_clear_buffer(jack_port_get_buffer(_jack_input_port, nframes));
290 ret = ret && (_jack_input_port != NULL);
297 JACK_MidiPort::get_state () const
299 XMLNode& root (Port::get_state ());
301 if (_jack_output_port) {
303 const char** jc = jack_port_get_connections (_jack_output_port);
304 string connection_string;
306 for (int i = 0; jc[i]; ++i) {
308 connection_string += ',';
310 connection_string += jc[i];
315 if (!connection_string.empty()) {
316 root.add_property ("outbound", connection_string);
319 if (!_outbound_connections.empty()) {
320 root.add_property ("outbound", _outbound_connections);
324 if (_jack_input_port) {
326 const char** jc = jack_port_get_connections (_jack_input_port);
327 string connection_string;
329 for (int i = 0; jc[i]; ++i) {
331 connection_string += ',';
333 connection_string += jc[i];
338 if (!connection_string.empty()) {
339 root.add_property ("inbound", connection_string);
342 if (!_inbound_connections.empty()) {
343 root.add_property ("inbound", _inbound_connections);
351 JACK_MidiPort::set_state (const XMLNode& node)
353 Port::set_state (node);
354 const XMLProperty* prop;
356 if ((prop = node.property ("inbound")) != 0 && _jack_input_port) {
357 _inbound_connections = prop->value ();
360 if ((prop = node.property ("outbound")) != 0 && _jack_output_port) {
361 _outbound_connections = prop->value();
366 JACK_MidiPort::make_connections ()
368 if (!_inbound_connections.empty()) {
369 vector<string> ports;
370 split (_inbound_connections, ports, ',');
371 for (vector<string>::iterator x = ports.begin(); x != ports.end(); ++x) {
373 jack_connect (_jack_client, (*x).c_str(), jack_port_name (_jack_input_port));
374 /* ignore failures */
379 if (!_outbound_connections.empty()) {
380 vector<string> ports;
381 split (_outbound_connections, ports, ',');
382 for (vector<string>::iterator x = ports.begin(); x != ports.end(); ++x) {
384 jack_connect (_jack_client, jack_port_name (_jack_output_port), (*x).c_str());
385 /* ignore failures */
389 connect_connection.disconnect ();
393 JACK_MidiPort::set_process_thread (pthread_t thr)
395 _process_thread = thr;
399 JACK_MidiPort::is_process_thread()
401 return (pthread_self() == _process_thread);