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 "pbd/xml++.h"
26 #include "pbd/error.h"
27 #include "pbd/failed_constructor.h"
28 #include "pbd/convert.h"
29 #include "pbd/strsplit.h"
30 #include "pbd/stacktrace.h"
32 #include "midi++/types.h"
33 #include "midi++/jack_midi_port.h"
34 #include "midi++/channel.h"
41 template class EventRingBuffer<timestamp_t>;
44 pthread_t JackMIDIPort::_process_thread;
45 Signal0<void> JackMIDIPort::EngineHalted;
46 Signal0<void> JackMIDIPort::MakeConnections;
48 JackMIDIPort::JackMIDIPort (string const & name, Flags flags, ARDOUR::PortEngine& pengine)
50 , _port_engine (pengine)
52 , _currently_in_cycle (false)
53 , _nframes_this_cycle (0)
54 , _last_write_timestamp (0)
62 JackMIDIPort::JackMIDIPort (const XMLNode& node, ARDOUR::PortEngine& pengine)
64 , _port_engine (pengine)
66 , _currently_in_cycle (false)
67 , _nframes_this_cycle (0)
68 , _last_write_timestamp (0)
73 Descriptor desc (node);
74 init (desc.tag, desc.flags);
79 JackMIDIPort::init (const string& /*name*/, Flags /*flags*/)
81 if (!create_port ()) {
85 MakeConnections.connect_same_thread (connect_connection, boost::bind (&JackMIDIPort::make_connections, this));
86 EngineHalted.connect_same_thread (halt_connection, boost::bind (&JackMIDIPort::engine_halted, this));
90 JackMIDIPort::~JackMIDIPort ()
93 _port_engine.unregister_port (_port_handle);
99 JackMIDIPort::parse (framecnt_t timestamp)
103 /* NOTE: parsing is done (if at all) by initiating a read from
104 the port. Each port implementation calls on the parser
105 once it has data ready.
108 _parser->set_timestamp (timestamp);
112 // cerr << "+++ READ ON " << name() << endl;
114 int nread = read (buf, sizeof (buf));
116 // cerr << "-- READ (" << nread << " ON " << name() << endl;
119 if ((size_t) nread < sizeof (buf)) {
124 } else if (nread == 0) {
126 } else if (errno == EAGAIN) {
129 fatal << "Error reading from MIDI port " << name() << endmsg;
136 JackMIDIPort::cycle_start (pframes_t nframes)
138 assert (_port_handle);
140 _currently_in_cycle = true;
141 _nframes_this_cycle = nframes;
143 assert(_nframes_this_cycle == nframes);
145 if (sends_output()) {
146 void *buffer = _port_engine.get_buffer (_port_handle, nframes);
147 jack_midi_clear_buffer (buffer);
151 if (receives_input()) {
152 void* buffer = _port_engine.get_buffer (_port_handle, nframes);
153 const pframes_t event_count = _port_engine.get_midi_event_count (buffer);
158 timestamp_t cycle_start_frame = _port_engine.sample_time_at_cycle_start ();
160 for (pframes_t i = 0; i < event_count; ++i) {
161 _port_engine.midi_event_get (time, size, &buf, buffer, i);
162 input_fifo.write (cycle_start_frame + time, (Evoral::EventType) 0, size, buf);
172 JackMIDIPort::cycle_end ()
174 if (sends_output()) {
175 flush (_port_engine.get_buffer (_port_handle, _nframes_this_cycle));
178 _currently_in_cycle = false;
179 _nframes_this_cycle = 0;
183 JackMIDIPort::engine_halted ()
189 JackMIDIPort::drain (int check_interval_usecs)
191 RingBuffer< Evoral::Event<double> >::rw_vector vec = { { 0, 0 }, { 0, 0} };
193 if (is_process_thread()) {
194 error << "Process thread called MIDI::JackMIDIPort::drain() - this cannot work" << endmsg;
199 output_fifo.get_write_vector (&vec);
200 if (vec.len[0] + vec.len[1] >= output_fifo.bufsize() - 1) {
203 usleep (check_interval_usecs);
208 JackMIDIPort::write (const byte * msg, size_t msglen, timestamp_t timestamp)
213 /* poof ! make it just vanish into thin air, since we are no
214 longer connected to JACK.
219 if (!sends_output()) {
223 if (!is_process_thread()) {
225 Glib::Threads::Mutex::Lock lm (output_fifo_lock);
226 RingBuffer< Evoral::Event<double> >::rw_vector vec = { { 0, 0 }, { 0, 0} };
228 output_fifo.get_write_vector (&vec);
230 if (vec.len[0] + vec.len[1] < 1) {
231 error << "no space in FIFO for non-process thread MIDI write" << endmsg;
236 if (!vec.buf[0]->owns_buffer()) {
237 vec.buf[0]->set_buffer (0, 0, true);
239 vec.buf[0]->set (msg, msglen, timestamp);
241 if (!vec.buf[1]->owns_buffer()) {
242 vec.buf[1]->set_buffer (0, 0, true);
244 vec.buf[1]->set (msg, msglen, timestamp);
247 output_fifo.increment_write_idx (1);
253 if (timestamp >= _nframes_this_cycle) {
254 std::cerr << "attempting to write MIDI event of " << msglen << " bytes at time "
255 << timestamp << " of " << _nframes_this_cycle
256 << " (this will not work - needs a code fix)"
260 if (_currently_in_cycle) {
261 if (timestamp == 0) {
262 timestamp = _last_write_timestamp;
265 if ((ret = _port_engine.midi_event_put (_port_engine.get_buffer (_port_handle, _nframes_this_cycle),
266 timestamp, msg, msglen)) == 0) {
268 _last_write_timestamp = timestamp;
271 cerr << "write of " << msglen << " @ " << timestamp << " failed, port holds "
272 << _port_engine.get_midi_event_count (_port_engine.get_buffer (_port_handle, _nframes_this_cycle))
273 << " port is " << _port_handle
274 << " ntf = " << _nframes_this_cycle
275 << " buf = " << _port_engine.get_buffer (_port_handle, _nframes_this_cycle)
278 PBD::stacktrace (cerr, 20);
282 cerr << "write to JACK midi port failed: not currently in a process cycle." << endl;
283 PBD::stacktrace (cerr, 20);
287 if (ret > 0 && _parser) {
288 // ardour doesn't care about this and neither should your app, probably
289 // output_parser->raw_preparse (*output_parser, msg, ret);
290 for (int i = 0; i < ret; i++) {
291 _parser->scanner (msg[i]);
293 // ardour doesn't care about this and neither should your app, probably
294 // output_parser->raw_postparse (*output_parser, msg, ret);
301 JackMIDIPort::flush (void* port_buffer)
303 RingBuffer< Evoral::Event<double> >::rw_vector vec = { { 0, 0 }, { 0, 0 } };
306 output_fifo.get_read_vector (&vec);
308 if (vec.len[0] + vec.len[1]) {
309 // cerr << "Flush " << vec.len[0] + vec.len[1] << " events from non-process FIFO\n";
313 Evoral::Event<double>* evp = vec.buf[0];
315 for (size_t n = 0; n < vec.len[0]; ++n, ++evp) {
316 _port_engine.midi_event_put (port_buffer, (timestamp_t) evp->time(), evp->buffer(), evp->size());
321 Evoral::Event<double>* evp = vec.buf[1];
323 for (size_t n = 0; n < vec.len[1]; ++n, ++evp) {
324 _port_engine.midi_event_put (port_buffer, (timestamp_t) evp->time(), evp->buffer(), evp->size());
328 if ((written = vec.len[0] + vec.len[1]) != 0) {
329 output_fifo.increment_read_idx (written);
334 JackMIDIPort::read (byte *, size_t)
336 if (!receives_input()) {
341 Evoral::EventType type;
343 byte buffer[input_fifo.capacity()];
345 while (input_fifo.read (&time, &type, &size, buffer)) {
346 _parser->set_timestamp (time);
347 for (uint32_t i = 0; i < size; ++i) {
348 _parser->scanner (buffer[i]);
356 JackMIDIPort::create_port ()
358 ARDOUR::PortFlags f = ARDOUR::PortFlags (0);
360 /* convert MIDI::Port::Flags to ARDOUR::PortFlags ... sigh */
362 if (_flags & IsInput) {
363 f = ARDOUR::PortFlags (f | ARDOUR::IsInput);
366 if (_flags & IsOutput) {
367 f = ARDOUR::PortFlags (f | ARDOUR::IsOutput);
370 _port_handle = _port_engine.register_port (_tagname, ARDOUR::DataType::MIDI, f);
372 return _port_handle == 0 ? -1 : 0;
376 JackMIDIPort::get_state () const
378 XMLNode& root = Port::get_state ();
381 byte device_inquiry[6];
383 device_inquiry[0] = 0xf0;
384 device_inquiry[0] = 0x7e;
385 device_inquiry[0] = 0x7f;
386 device_inquiry[0] = 0x06;
387 device_inquiry[0] = 0x02;
388 device_inquiry[0] = 0xf7;
390 write (device_inquiry, sizeof (device_inquiry), 0);
395 vector<string> connections;
396 _port_engine.get_connections (_port_handle, connections);
397 string connection_string;
398 for (vector<string>::iterator i = connections.begin(); i != connections.end(); ++i) {
399 if (i != connections.begin()) {
400 connection_string += ',';
402 connection_string += *i;
405 if (!connection_string.empty()) {
406 root.add_property ("connections", connection_string);
409 if (!_connections.empty()) {
410 root.add_property ("connections", _connections);
418 JackMIDIPort::set_state (const XMLNode& node)
420 const XMLProperty* prop;
422 if ((prop = node.property ("tag")) == 0 || prop->value() != _tagname) {
426 Port::set_state (node);
428 if ((prop = node.property ("connections")) != 0) {
429 _connections = prop->value ();
434 JackMIDIPort::make_connections ()
436 if (!_connections.empty()) {
437 vector<string> ports;
438 split (_connections, ports, ',');
439 for (vector<string>::iterator x = ports.begin(); x != ports.end(); ++x) {
440 _port_engine.connect (_port_handle, *x);
441 /* ignore failures */
445 connect_connection.disconnect ();
449 JackMIDIPort::set_process_thread (pthread_t thr)
451 _process_thread = thr;
455 JackMIDIPort::is_process_thread()
457 return (pthread_self() == _process_thread);
461 JackMIDIPort::reestablish ()
463 int const r = create_port ();
466 PBD::error << "could not reregister ports for " << name() << endmsg;
471 JackMIDIPort::reconnect ()