2006-06-08 21:48:38 -04:00
|
|
|
/*
|
|
|
|
Copyright (C) 2006 Paul Davis
|
|
|
|
Written by Dave Robillard
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <cerrno>
|
|
|
|
#include <cassert>
|
|
|
|
|
2008-03-17 20:58:36 -04:00
|
|
|
#include <pbd/error.h>
|
|
|
|
|
2006-06-08 21:48:38 -04:00
|
|
|
#include <midi++/types.h>
|
|
|
|
#include <midi++/jack.h>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace MIDI;
|
2008-03-17 20:58:36 -04:00
|
|
|
using namespace PBD;
|
2006-06-08 21:48:38 -04:00
|
|
|
|
2008-03-17 23:42:32 -04:00
|
|
|
pthread_t JACK_MidiPort::_process_thread;
|
|
|
|
|
2007-10-11 18:07:47 -04:00
|
|
|
JACK_MidiPort::JACK_MidiPort(const XMLNode& node, jack_client_t* jack_client)
|
|
|
|
: Port(node)
|
2006-06-08 21:48:38 -04:00
|
|
|
, _jack_client(jack_client)
|
|
|
|
, _jack_input_port(NULL)
|
|
|
|
, _jack_output_port(NULL)
|
|
|
|
, _last_read_index(0)
|
2008-03-19 13:30:42 -04:00
|
|
|
, non_process_thread_fifo (512)
|
2006-06-08 21:48:38 -04:00
|
|
|
{
|
2007-10-11 18:07:47 -04:00
|
|
|
int err = create_ports (node);
|
2006-06-08 21:48:38 -04:00
|
|
|
|
|
|
|
if (!err) {
|
|
|
|
_ok = true;
|
2007-10-11 18:07:47 -04:00
|
|
|
}
|
2006-06-08 21:48:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
JACK_MidiPort::~JACK_MidiPort()
|
|
|
|
{
|
|
|
|
// FIXME: remove port
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
JACK_MidiPort::cycle_start (nframes_t nframes)
|
|
|
|
{
|
|
|
|
Port::cycle_start(nframes);
|
|
|
|
assert(_nframes_this_cycle == nframes);
|
|
|
|
_last_read_index = 0;
|
2008-03-19 17:15:28 -04:00
|
|
|
_last_write_timestamp = 0;
|
2008-03-17 23:42:32 -04:00
|
|
|
|
|
|
|
void *buffer = jack_port_get_buffer (_jack_output_port, nframes);
|
|
|
|
jack_midi_clear_buffer (buffer);
|
|
|
|
flush (buffer);
|
2006-06-08 21:48:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
JACK_MidiPort::write(byte * msg, size_t msglen, timestamp_t timestamp)
|
|
|
|
{
|
2008-03-19 17:15:28 -04:00
|
|
|
int ret = 0;
|
|
|
|
|
2008-03-17 23:42:32 -04:00
|
|
|
if (!is_process_thread()) {
|
|
|
|
|
|
|
|
Glib::Mutex::Lock lm (non_process_thread_fifo_lock);
|
|
|
|
RingBuffer<Event>::rw_vector vec;
|
|
|
|
|
|
|
|
non_process_thread_fifo.get_write_vector (&vec);
|
|
|
|
|
|
|
|
if (vec.len[0] + vec.len[1] < 1) {
|
|
|
|
error << "no space in FIFO for non-process thread MIDI write"
|
|
|
|
<< endmsg;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vec.len[0]) {
|
|
|
|
vec.buf[0]->set (msg, msglen, timestamp);
|
|
|
|
} else {
|
|
|
|
vec.buf[1]->set (msg, msglen, timestamp);
|
|
|
|
}
|
|
|
|
|
|
|
|
non_process_thread_fifo.increment_write_idx (1);
|
2008-03-19 17:15:28 -04:00
|
|
|
|
|
|
|
ret = msglen;
|
2008-03-17 23:42:32 -04:00
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
assert(_currently_in_cycle);
|
|
|
|
assert(_jack_output_port);
|
2008-03-19 17:15:28 -04:00
|
|
|
assert(timestamp < _nframes_this_cycle);
|
|
|
|
|
|
|
|
if (timestamp == 0) {
|
|
|
|
timestamp = _last_write_timestamp;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (jack_midi_event_write (jack_port_get_buffer (_jack_output_port, _nframes_this_cycle),
|
|
|
|
timestamp, msg, msglen) == 0) {
|
|
|
|
ret = msglen;
|
|
|
|
_last_write_timestamp = timestamp;
|
2008-03-17 23:42:32 -04:00
|
|
|
|
2008-03-19 17:15:28 -04:00
|
|
|
} else {
|
|
|
|
ret = 0;
|
|
|
|
cerr << "write of " << msglen << " failed, port holds "
|
|
|
|
<< jack_midi_get_event_count (jack_port_get_buffer (_jack_output_port, _nframes_this_cycle))
|
|
|
|
<< endl;
|
|
|
|
}
|
2008-03-17 20:58:36 -04:00
|
|
|
}
|
2008-03-19 17:15:28 -04:00
|
|
|
|
|
|
|
if (ret > 0 && output_parser) {
|
|
|
|
output_parser->raw_preparse (*output_parser, msg, ret);
|
|
|
|
for (int i = 0; i < ret; i++) {
|
|
|
|
output_parser->scanner (msg[i]);
|
|
|
|
}
|
|
|
|
output_parser->raw_postparse (*output_parser, msg, ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2008-03-17 23:42:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
JACK_MidiPort::flush (void* jack_port_buffer)
|
|
|
|
{
|
|
|
|
RingBuffer<Event>::rw_vector vec;
|
|
|
|
size_t written;
|
2006-06-08 21:48:38 -04:00
|
|
|
|
2008-03-17 23:42:32 -04:00
|
|
|
non_process_thread_fifo.get_read_vector (&vec);
|
|
|
|
|
|
|
|
if (vec.len[0] + vec.len[1]) {
|
2008-03-19 14:10:51 -04:00
|
|
|
cerr << "Flush " << vec.len[0] + vec.len[1] << " events from non-process FIFO\n";
|
2008-03-17 23:42:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (vec.len[0]) {
|
|
|
|
Event* evp = vec.buf[0];
|
|
|
|
|
|
|
|
for (size_t n = 0; n < vec.len[0]; ++n, ++evp) {
|
|
|
|
jack_midi_event_write (jack_port_buffer,
|
|
|
|
(timestamp_t) evp->time(), evp->buffer(), evp->size());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vec.len[1]) {
|
|
|
|
Event* evp = vec.buf[1];
|
|
|
|
|
|
|
|
for (size_t n = 0; n < vec.len[1]; ++n, ++evp) {
|
|
|
|
jack_midi_event_write (jack_port_buffer,
|
|
|
|
(timestamp_t) evp->time(), evp->buffer(), evp->size());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((written = vec.len[0] + vec.len[1]) != 0) {
|
|
|
|
non_process_thread_fifo.increment_read_idx (written);
|
|
|
|
}
|
2006-06-08 21:48:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2008-03-19 13:25:37 -04:00
|
|
|
JACK_MidiPort::read(byte * buf, size_t bufsize)
|
2006-06-08 21:48:38 -04:00
|
|
|
{
|
|
|
|
assert(_currently_in_cycle);
|
|
|
|
assert(_jack_input_port);
|
|
|
|
|
|
|
|
jack_midi_event_t ev;
|
|
|
|
|
|
|
|
int err = jack_midi_event_get (&ev,
|
2008-03-19 13:25:37 -04:00
|
|
|
jack_port_get_buffer(_jack_input_port, _nframes_this_cycle),
|
|
|
|
_last_read_index++);
|
2006-06-08 21:48:38 -04:00
|
|
|
|
2008-03-19 13:25:37 -04:00
|
|
|
// XXX this doesn't handle ev.size > max
|
|
|
|
|
2006-06-08 21:48:38 -04:00
|
|
|
if (!err) {
|
2008-03-19 13:25:37 -04:00
|
|
|
size_t limit = min (bufsize, ev.size);
|
|
|
|
memcpy(buf, ev.buffer, limit);
|
2008-03-19 17:15:28 -04:00
|
|
|
|
|
|
|
if (input_parser) {
|
|
|
|
input_parser->raw_preparse (*input_parser, buf, limit);
|
|
|
|
for (size_t i = 0; i < limit; i++) {
|
|
|
|
input_parser->scanner (buf[i]);
|
|
|
|
}
|
|
|
|
input_parser->raw_postparse (*input_parser, buf, limit);
|
|
|
|
}
|
|
|
|
|
2008-03-19 13:25:37 -04:00
|
|
|
return limit;
|
2006-06-08 21:48:38 -04:00
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2007-10-11 18:07:47 -04:00
|
|
|
JACK_MidiPort::create_ports(const XMLNode& node)
|
2006-06-08 21:48:38 -04:00
|
|
|
{
|
2007-10-11 18:07:47 -04:00
|
|
|
Descriptor desc (node);
|
|
|
|
|
2006-06-08 21:48:38 -04:00
|
|
|
assert(!_jack_input_port);
|
|
|
|
assert(!_jack_output_port);
|
|
|
|
|
2007-05-03 22:50:31 -04:00
|
|
|
jack_nframes_t nframes = jack_get_buffer_size(_jack_client);
|
2006-06-08 21:48:38 -04:00
|
|
|
|
|
|
|
bool ret = true;
|
|
|
|
|
2007-10-11 18:07:47 -04:00
|
|
|
if (desc.mode == O_RDWR || desc.mode == O_WRONLY) {
|
2006-06-08 21:48:38 -04:00
|
|
|
_jack_output_port = jack_port_register(_jack_client,
|
2007-10-11 18:07:47 -04:00
|
|
|
string(desc.tag).append("_out").c_str(),
|
|
|
|
JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0);
|
|
|
|
jack_midi_clear_buffer(jack_port_get_buffer(_jack_output_port, nframes));
|
2006-06-08 21:48:38 -04:00
|
|
|
ret = ret && (_jack_output_port != NULL);
|
|
|
|
}
|
2007-10-11 18:07:47 -04:00
|
|
|
|
|
|
|
if (desc.mode == O_RDWR || desc.mode == O_RDONLY) {
|
2006-06-08 21:48:38 -04:00
|
|
|
_jack_input_port = jack_port_register(_jack_client,
|
2007-10-11 18:07:47 -04:00
|
|
|
string(desc.tag).append("_in").c_str(),
|
|
|
|
JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0);
|
|
|
|
jack_midi_clear_buffer(jack_port_get_buffer(_jack_input_port, nframes));
|
2006-06-08 21:48:38 -04:00
|
|
|
ret = ret && (_jack_input_port != NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret ? 0 : -1;
|
|
|
|
}
|
|
|
|
|
2007-10-11 18:07:47 -04:00
|
|
|
XMLNode&
|
|
|
|
JACK_MidiPort::get_state () const
|
|
|
|
{
|
|
|
|
XMLNode& root (Port::get_state ());
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
JACK_MidiPort::set_state (const XMLNode& node)
|
|
|
|
{
|
|
|
|
}
|
2008-03-17 23:42:32 -04:00
|
|
|
|
|
|
|
void
|
|
|
|
JACK_MidiPort::set_process_thread (pthread_t thr)
|
|
|
|
{
|
|
|
|
_process_thread = thr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
JACK_MidiPort::is_process_thread()
|
|
|
|
{
|
|
|
|
return (pthread_self() == _process_thread);
|
|
|
|
}
|