fix gcc4.4 compile warnings
[ardour.git] / libs / midi++2 / jack_midiport.cc
1 /*
2   Copyright (C) 2006 Paul Davis 
3   Written by Dave Robillard
4  
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.
9  
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.
14  
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.
18 */
19
20 #include <fcntl.h>
21 #include <cerrno>
22 #include <cassert>
23 #include <cstring>
24 #include <cstdlib>
25
26 #include "pbd/error.h"
27 #include "pbd/compose.h"
28 #include "pbd/strsplit.h"
29
30 #include "midi++/types.h"
31 #include "midi++/jack.h"
32
33 using namespace std;
34 using namespace MIDI;
35 using namespace PBD;
36
37 pthread_t JACK_MidiPort::_process_thread;
38
39 Signal0<void> JACK_MidiPort::JackHalted;
40 Signal0<void> JACK_MidiPort::MakeConnections;
41
42 JACK_MidiPort::JACK_MidiPort(const XMLNode& node, jack_client_t* jack_client)
43         : Port(node)
44         , _jack_client(jack_client)
45         , _jack_input_port(NULL)
46         , _jack_output_port(NULL)
47         , _last_read_index(0)
48         , output_fifo (512)
49         , input_fifo (1024)
50 {
51         if (!create_ports (node)) {
52                 _ok = true;
53         }
54
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));
57
58         set_state (node);
59 }
60
61 JACK_MidiPort::~JACK_MidiPort()
62 {
63         if (_jack_input_port) {
64                 if (_jack_client) {
65                         jack_port_unregister (_jack_client, _jack_input_port);
66                 }
67                 _jack_input_port = 0;
68         }
69
70         if (_jack_output_port) {
71                 if (_jack_client) {
72                         jack_port_unregister (_jack_client, _jack_input_port);
73                 }
74                 _jack_input_port = 0;
75         }
76 }
77
78 void
79 JACK_MidiPort::jack_halted ()
80 {
81         _jack_client = 0;
82         _jack_input_port = 0;
83         _jack_output_port = 0;
84 }
85
86 void
87 JACK_MidiPort::cycle_start (nframes_t nframes)
88 {
89         Port::cycle_start(nframes);
90         assert(_nframes_this_cycle == nframes);
91         _last_read_index = 0;
92         _last_write_timestamp = 0;
93
94         if (_jack_output_port != 0) {
95                 // output
96                 void *buffer = jack_port_get_buffer (_jack_output_port, nframes);
97                 jack_midi_clear_buffer (buffer);
98                 flush (buffer); 
99         }
100         
101         if (_jack_input_port != 0) {
102                 // input
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);
105
106                 jack_midi_event_t ev;
107                 timestamp_t cycle_start_frame = jack_last_frame_time (_jack_client);
108
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);
112                 }       
113                 
114                 if (event_count) {
115                         xthread.wakeup ();
116                 }
117         }
118 }
119
120 void
121 JACK_MidiPort::cycle_end ()
122 {
123         if (_jack_output_port != 0) {
124                 flush (jack_port_get_buffer (_jack_output_port, _nframes_this_cycle));
125         }
126
127         Port::cycle_end();
128 }
129
130 int
131 JACK_MidiPort::write(byte * msg, size_t msglen, timestamp_t timestamp)
132 {
133         int ret = 0;
134
135         if (!is_process_thread()) {
136
137                 Glib::Mutex::Lock lm (output_fifo_lock);
138                 RingBuffer< Evoral::Event<double> >::rw_vector vec = { { 0, 0 }, { 0, 0} };
139                 
140                 output_fifo.get_write_vector (&vec);
141
142                 if (vec.len[0] + vec.len[1] < 1) {
143                         error << "no space in FIFO for non-process thread MIDI write" << endmsg;
144                         return 0;
145                 }
146
147                 if (vec.len[0]) {
148                         if (!vec.buf[0]->owns_buffer()) {
149                                 vec.buf[0]->set_buffer (0, 0, true);
150                         }
151                         vec.buf[0]->set (msg, msglen, timestamp);
152                 } else {
153                         if (!vec.buf[1]->owns_buffer()) {
154                                 vec.buf[1]->set_buffer (0, 0, true);
155                         }
156                         vec.buf[1]->set (msg, msglen, timestamp);
157                 }
158
159                 output_fifo.increment_write_idx (1);
160                 
161                 ret = msglen;
162
163         } else {
164
165                 assert(_jack_output_port);
166                 
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;
170                 }
171
172                 if (_currently_in_cycle) {
173                         if (timestamp == 0) {
174                                 timestamp = _last_write_timestamp;
175                         } 
176
177                         if (jack_midi_event_write (jack_port_get_buffer (_jack_output_port, _nframes_this_cycle), 
178                                                 timestamp, msg, msglen) == 0) {
179                                 ret = msglen;
180                                 _last_write_timestamp = timestamp;
181
182                         } else {
183                                 ret = 0;
184                                 cerr << "write of " << msglen << " failed, port holds "
185                                         << jack_midi_get_event_count (jack_port_get_buffer (_jack_output_port, _nframes_this_cycle))
186                                         << endl;
187                         }
188                 } else {
189                         cerr << "write to JACK midi port failed: not currently in a process cycle." << endl;
190                 }
191         }
192
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]);
198                 }
199                 // ardour doesn't care about this and neither should your app, probably
200                 // output_parser->raw_postparse (*output_parser, msg, ret);
201         }       
202
203         return ret;
204 }
205
206 void
207 JACK_MidiPort::flush (void* jack_port_buffer)
208 {
209         RingBuffer< Evoral::Event<double> >::rw_vector vec = { { 0, 0 }, { 0, 0 } };
210         size_t written;
211
212         output_fifo.get_read_vector (&vec);
213
214         if (vec.len[0] + vec.len[1]) {
215                 // cerr << "Flush " << vec.len[0] + vec.len[1] << " events from non-process FIFO\n";
216         }
217
218         if (vec.len[0]) {
219                 Evoral::Event<double>* evp = vec.buf[0];
220                 
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());
224                 }
225         }
226         
227         if (vec.len[1]) {
228                 Evoral::Event<double>* evp = vec.buf[1];
229
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());
233                 }
234         }
235         
236         if ((written = vec.len[0] + vec.len[1]) != 0) {
237                 output_fifo.increment_read_idx (written);
238         }
239 }
240
241 int
242 JACK_MidiPort::read (byte *, size_t)
243 {
244         timestamp_t time;
245         Evoral::EventType type;
246         uint32_t size;
247         byte buffer[input_fifo.capacity()];
248
249         while (input_fifo.read (&time, &type, &size, buffer)) {
250                 if (input_parser) {
251                         input_parser->set_timestamp (time);
252                         for (uint32_t i = 0; i < size; ++i) {
253                                 input_parser->scanner (buffer[i]);
254                         }
255                 }
256         }
257
258         return 0;
259 }
260
261 int
262 JACK_MidiPort::create_ports(const XMLNode& node)
263 {
264         Descriptor desc (node);
265
266         assert(!_jack_input_port);
267         assert(!_jack_output_port);
268         
269         jack_nframes_t nframes = jack_get_buffer_size(_jack_client);
270
271         bool ret = true;
272
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));
279                 }
280                 ret = ret && (_jack_output_port != NULL);
281         }
282         
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));
289                 }
290                 ret = ret && (_jack_input_port != NULL);
291         }
292
293         return ret ? 0 : -1;
294 }
295
296 XMLNode& 
297 JACK_MidiPort::get_state () const
298 {
299         XMLNode& root (Port::get_state ());
300
301         if (_jack_output_port) {
302                 
303                 const char** jc = jack_port_get_connections (_jack_output_port);
304                 string connection_string;
305                 if (jc) {
306                         for (int i = 0; jc[i]; ++i) {
307                                 if (i > 0) {
308                                         connection_string += ',';
309                                 }
310                                 connection_string += jc[i];
311                         }
312                         free (jc);
313                 }
314                 
315                 if (!connection_string.empty()) {
316                         root.add_property ("outbound", connection_string);
317                 }
318         } else {
319                 if (!_outbound_connections.empty()) {
320                         root.add_property ("outbound", _outbound_connections);
321                 }
322         }
323
324         if (_jack_input_port) {
325
326                 const char** jc = jack_port_get_connections (_jack_input_port);
327                 string connection_string;
328                 if (jc) {
329                         for (int i = 0; jc[i]; ++i) {
330                                 if (i > 0) {
331                                         connection_string += ',';
332                                 }
333                                 connection_string += jc[i];
334                         }
335                         free (jc);
336                 }
337
338                 if (!connection_string.empty()) {
339                         root.add_property ("inbound", connection_string);
340                 }
341         } else {
342                 if (!_inbound_connections.empty()) {
343                         root.add_property ("inbound", _inbound_connections);
344                 }
345         }
346
347         return root;
348 }
349
350 void
351 JACK_MidiPort::set_state (const XMLNode& node)
352 {
353         Port::set_state (node);
354         const XMLProperty* prop;
355
356         if ((prop = node.property ("inbound")) != 0 && _jack_input_port) {
357                 _inbound_connections = prop->value ();
358         }
359
360         if ((prop = node.property ("outbound")) != 0 && _jack_output_port) {
361                 _outbound_connections = prop->value();
362         }
363 }
364
365 void
366 JACK_MidiPort::make_connections ()
367 {
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) {
372                         if (_jack_client) {
373                                 jack_connect (_jack_client, (*x).c_str(), jack_port_name (_jack_input_port));
374                                 /* ignore failures */
375                         }
376                 }
377         }
378
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) {
383                         if (_jack_client) {
384                                 jack_connect (_jack_client, jack_port_name (_jack_output_port), (*x).c_str());
385                                 /* ignore failures */
386                         }
387                 }
388         }
389         connect_connection.disconnect ();
390 }
391
392 void
393 JACK_MidiPort::set_process_thread (pthread_t thr)
394 {
395         _process_thread = thr;
396 }
397
398 bool
399 JACK_MidiPort::is_process_thread()
400 {
401         return (pthread_self() == _process_thread);
402 }