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;
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 vec.buf[0]->set (msg, msglen, timestamp);
150 vec.buf[1]->set (msg, msglen, timestamp);
153 output_fifo.increment_write_idx (1);
159 assert(_jack_output_port);
161 // XXX This had to be temporarily commented out to make export work again
162 if (!(timestamp < _nframes_this_cycle)) {
163 std::cerr << "assertion timestamp < _nframes_this_cycle failed!" << std::endl;
166 if (_currently_in_cycle) {
167 if (timestamp == 0) {
168 timestamp = _last_write_timestamp;
171 if (jack_midi_event_write (jack_port_get_buffer (_jack_output_port, _nframes_this_cycle),
172 timestamp, msg, msglen) == 0) {
174 _last_write_timestamp = timestamp;
178 cerr << "write of " << msglen << " failed, port holds "
179 << jack_midi_get_event_count (jack_port_get_buffer (_jack_output_port, _nframes_this_cycle))
183 cerr << "write to JACK midi port failed: not currently in a process cycle." << endl;
187 if (ret > 0 && output_parser) {
188 // ardour doesn't care about this and neither should your app, probably
189 // output_parser->raw_preparse (*output_parser, msg, ret);
190 for (int i = 0; i < ret; i++) {
191 output_parser->scanner (msg[i]);
193 // ardour doesn't care about this and neither should your app, probably
194 // output_parser->raw_postparse (*output_parser, msg, ret);
201 JACK_MidiPort::flush (void* jack_port_buffer)
203 RingBuffer< Evoral::Event<double> >::rw_vector vec;
206 output_fifo.get_read_vector (&vec);
208 if (vec.len[0] + vec.len[1]) {
209 // cerr << "Flush " << vec.len[0] + vec.len[1] << " events from non-process FIFO\n";
213 Evoral::Event<double>* evp = vec.buf[0];
215 for (size_t n = 0; n < vec.len[0]; ++n, ++evp) {
216 jack_midi_event_write (jack_port_buffer,
217 (timestamp_t) evp->time(), evp->buffer(), evp->size());
222 Evoral::Event<double>* evp = vec.buf[1];
224 for (size_t n = 0; n < vec.len[1]; ++n, ++evp) {
225 jack_midi_event_write (jack_port_buffer,
226 (timestamp_t) evp->time(), evp->buffer(), evp->size());
230 if ((written = vec.len[0] + vec.len[1]) != 0) {
231 output_fifo.increment_read_idx (written);
236 JACK_MidiPort::read (byte *, size_t)
239 Evoral::EventType type;
241 byte buffer[input_fifo.capacity()];
243 while (input_fifo.read (&time, &type, &size, buffer)) {
245 input_parser->set_timestamp (time);
246 for (uint32_t i = 0; i < size; ++i) {
247 input_parser->scanner (buffer[i]);
256 JACK_MidiPort::create_ports(const XMLNode& node)
258 Descriptor desc (node);
260 assert(!_jack_input_port);
261 assert(!_jack_output_port);
263 jack_nframes_t nframes = jack_get_buffer_size(_jack_client);
267 if (desc.mode == O_RDWR || desc.mode == O_WRONLY) {
268 _jack_output_port = jack_port_register(_jack_client,
269 string(desc.tag).append("_out").c_str(),
270 JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0);
271 if (_jack_output_port) {
272 jack_midi_clear_buffer(jack_port_get_buffer(_jack_output_port, nframes));
274 ret = ret && (_jack_output_port != NULL);
277 if (desc.mode == O_RDWR || desc.mode == O_RDONLY) {
278 _jack_input_port = jack_port_register(_jack_client,
279 string(desc.tag).append("_in").c_str(),
280 JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0);
281 if (_jack_input_port) {
282 jack_midi_clear_buffer(jack_port_get_buffer(_jack_input_port, nframes));
284 ret = ret && (_jack_input_port != NULL);
291 JACK_MidiPort::get_state () const
293 XMLNode& root (Port::get_state ());
295 if (_jack_output_port) {
297 const char** jc = jack_port_get_connections (_jack_output_port);
298 string connection_string;
300 for (int i = 0; jc[i]; ++i) {
302 connection_string += ',';
304 connection_string += jc[i];
309 if (!connection_string.empty()) {
310 root.add_property ("outbound", connection_string);
313 if (!_outbound_connections.empty()) {
314 root.add_property ("outbound", _outbound_connections);
318 if (_jack_input_port) {
320 const char** jc = jack_port_get_connections (_jack_input_port);
321 string connection_string;
323 for (int i = 0; jc[i]; ++i) {
325 connection_string += ',';
327 connection_string += jc[i];
332 if (!connection_string.empty()) {
333 root.add_property ("inbound", connection_string);
336 if (!_inbound_connections.empty()) {
337 root.add_property ("inbound", _inbound_connections);
345 JACK_MidiPort::set_state (const XMLNode& node)
347 Port::set_state (node);
348 const XMLProperty* prop;
350 if ((prop = node.property ("inbound")) != 0 && _jack_input_port) {
351 _inbound_connections = prop->value ();
354 if ((prop = node.property ("outbound")) != 0 && _jack_output_port) {
355 _outbound_connections = prop->value();
360 JACK_MidiPort::make_connections ()
362 if (!_inbound_connections.empty()) {
363 vector<string> ports;
364 split (_inbound_connections, ports, ',');
365 for (vector<string>::iterator x = ports.begin(); x != ports.end(); ++x) {
367 jack_connect (_jack_client, (*x).c_str(), jack_port_name (_jack_input_port));
368 /* ignore failures */
373 if (!_outbound_connections.empty()) {
374 vector<string> ports;
375 split (_outbound_connections, ports, ',');
376 for (vector<string>::iterator x = ports.begin(); x != ports.end(); ++x) {
378 jack_connect (_jack_client, jack_port_name (_jack_output_port), (*x).c_str());
379 /* ignore failures */
383 connect_connection.disconnect ();
387 JACK_MidiPort::set_process_thread (pthread_t thr)
389 _process_thread = thr;
393 JACK_MidiPort::is_process_thread()
395 return (pthread_self() == _process_thread);