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.
24 #include "pbd/error.h"
26 #include "midi++/types.h"
27 #include "midi++/jack.h"
33 pthread_t JACK_MidiPort::_process_thread;
35 JACK_MidiPort::JACK_MidiPort(const XMLNode& node, jack_client_t* jack_client)
37 , _jack_client(jack_client)
38 , _jack_input_port(NULL)
39 , _jack_output_port(NULL)
41 , non_process_thread_fifo (512)
43 int err = create_ports (node);
50 JACK_MidiPort::~JACK_MidiPort()
56 JACK_MidiPort::cycle_start (nframes_t nframes)
58 Port::cycle_start(nframes);
59 assert(_nframes_this_cycle == nframes);
61 _last_write_timestamp = 0;
63 if (_jack_output_port != 0) {
65 void *buffer = jack_port_get_buffer (_jack_output_port, nframes);
66 jack_midi_clear_buffer (buffer);
70 if (_jack_input_port != 0) {
72 void* jack_buffer = jack_port_get_buffer(_jack_input_port, nframes);
73 const nframes_t event_count = jack_midi_get_event_count(jack_buffer);
77 for (nframes_t i=0; i < event_count; ++i) {
79 jack_midi_event_get (&ev, jack_buffer, i);
82 for (size_t i = 0; i < ev.size; i++) {
83 // the midi events here are used for MIDI clock only
84 input_parser->set_midi_clock_timestamp(ev.time + jack_last_frame_time(_jack_client));
85 input_parser->scanner (ev.buffer[i]);
93 JACK_MidiPort::cycle_end ()
95 flush(jack_port_get_buffer(_jack_output_port, _nframes_this_cycle));
99 JACK_MidiPort::write(byte * msg, size_t msglen, timestamp_t timestamp)
103 if (!is_process_thread()) {
105 Glib::Mutex::Lock lm (non_process_thread_fifo_lock);
106 RingBuffer< Evoral::Event<double> >::rw_vector vec;
108 non_process_thread_fifo.get_write_vector (&vec);
110 if (vec.len[0] + vec.len[1] < 1) {
111 error << "no space in FIFO for non-process thread MIDI write" << endmsg;
116 vec.buf[0]->set (msg, msglen, timestamp);
118 vec.buf[1]->set (msg, msglen, timestamp);
121 non_process_thread_fifo.increment_write_idx (1);
127 assert(_jack_output_port);
129 // XXX This had to be temporarily commented out to make export work again
130 if (!(timestamp < _nframes_this_cycle)) {
131 std::cerr << "assertion timestamp < _nframes_this_cycle failed!" << std::endl;
134 if (_currently_in_cycle) {
135 if (timestamp == 0) {
136 timestamp = _last_write_timestamp;
139 if (jack_midi_event_write (jack_port_get_buffer (_jack_output_port, _nframes_this_cycle),
140 timestamp, msg, msglen) == 0) {
142 _last_write_timestamp = timestamp;
146 cerr << "write of " << msglen << " failed, port holds "
147 << jack_midi_get_event_count (jack_port_get_buffer (_jack_output_port, _nframes_this_cycle))
151 cerr << "write to JACK midi port failed: not currently in a process cycle." << endl;
155 if (ret > 0 && output_parser) {
156 output_parser->raw_preparse (*output_parser, msg, ret);
157 for (int i = 0; i < ret; i++) {
158 output_parser->scanner (msg[i]);
160 output_parser->raw_postparse (*output_parser, msg, ret);
167 JACK_MidiPort::flush (void* jack_port_buffer)
169 RingBuffer< Evoral::Event<double> >::rw_vector vec;
172 non_process_thread_fifo.get_read_vector (&vec);
174 if (vec.len[0] + vec.len[1]) {
175 // cerr << "Flush " << vec.len[0] + vec.len[1] << " events from non-process FIFO\n";
179 Evoral::Event<double>* evp = vec.buf[0];
181 for (size_t n = 0; n < vec.len[0]; ++n, ++evp) {
182 jack_midi_event_write (jack_port_buffer,
183 (timestamp_t) evp->time(), evp->buffer(), evp->size());
188 Evoral::Event<double>* evp = vec.buf[1];
190 for (size_t n = 0; n < vec.len[1]; ++n, ++evp) {
191 jack_midi_event_write (jack_port_buffer,
192 (timestamp_t) evp->time(), evp->buffer(), evp->size());
196 if ((written = vec.len[0] + vec.len[1]) != 0) {
197 non_process_thread_fifo.increment_read_idx (written);
202 JACK_MidiPort::read(byte * buf, size_t bufsize)
204 assert(_currently_in_cycle);
205 assert(_jack_input_port);
207 jack_midi_event_t ev;
209 cerr << "JACK_MidiPort::read called" << endl;
211 int err = jack_midi_event_get (&ev,
212 jack_port_get_buffer(_jack_input_port, _nframes_this_cycle),
215 // XXX this doesn't handle ev.size > max
218 size_t limit = min (bufsize, ev.size);
219 memcpy(buf, ev.buffer, limit);
222 input_parser->raw_preparse (*input_parser, buf, limit);
223 for (size_t i = 0; i < limit; i++) {
224 input_parser->scanner (buf[i]);
226 input_parser->raw_postparse (*input_parser, buf, limit);
236 JACK_MidiPort::create_ports(const XMLNode& node)
238 Descriptor desc (node);
240 assert(!_jack_input_port);
241 assert(!_jack_output_port);
243 jack_nframes_t nframes = jack_get_buffer_size(_jack_client);
247 if (desc.mode == O_RDWR || desc.mode == O_WRONLY) {
248 _jack_output_port = jack_port_register(_jack_client,
249 string(desc.tag).append("_out").c_str(),
250 JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0);
251 if (_jack_output_port) {
252 jack_midi_clear_buffer(jack_port_get_buffer(_jack_output_port, nframes));
254 ret = ret && (_jack_output_port != NULL);
257 if (desc.mode == O_RDWR || desc.mode == O_RDONLY) {
258 _jack_input_port = jack_port_register(_jack_client,
259 string(desc.tag).append("_in").c_str(),
260 JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0);
261 if (_jack_input_port) {
262 jack_midi_clear_buffer(jack_port_get_buffer(_jack_input_port, nframes));
264 ret = ret && (_jack_input_port != NULL);
271 JACK_MidiPort::get_state () const
273 XMLNode& root (Port::get_state ());
278 JACK_MidiPort::set_state (const XMLNode& /*node*/)
283 JACK_MidiPort::set_process_thread (pthread_t thr)
285 _process_thread = thr;
289 JACK_MidiPort::is_process_thread()
291 return (pthread_self() == _process_thread);