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 <midi++/types.h>
25 #include <midi++/jack.h>
26 #include <midi++/port_request.h>
31 JACK_MidiPort::JACK_MidiPort(PortRequest & req, jack_client_t* jack_client)
33 , _jack_client(jack_client)
34 , _jack_input_port(NULL)
35 , _jack_output_port(NULL)
38 int err = create_ports(req);
41 req.status = PortRequest::OK;
44 req.status = PortRequest::Unknown;
48 JACK_MidiPort::~JACK_MidiPort()
54 JACK_MidiPort::cycle_start (nframes_t nframes)
56 Port::cycle_start(nframes);
57 assert(_nframes_this_cycle == nframes);
59 jack_midi_clear_buffer(jack_port_get_buffer(_jack_output_port, nframes));
63 JACK_MidiPort::write(byte * msg, size_t msglen, timestamp_t timestamp)
65 assert(_currently_in_cycle);
66 assert(timestamp < _nframes_this_cycle);
67 assert(_jack_output_port);
69 // FIXME: return value correct?
70 return jack_midi_event_write (
71 jack_port_get_buffer(_jack_output_port, _nframes_this_cycle),
72 timestamp, msg, msglen);
76 JACK_MidiPort::read(byte * buf, size_t max, timestamp_t timestamp)
78 assert(_currently_in_cycle);
79 assert(timestamp < _nframes_this_cycle);
80 assert(_jack_input_port);
84 int err = jack_midi_event_get (&ev,
85 jack_port_get_buffer(_jack_input_port, _nframes_this_cycle),
89 memcpy(buf, ev.buffer, ev.size);
97 JACK_MidiPort::create_ports(PortRequest & req)
99 assert(!_jack_input_port);
100 assert(!_jack_output_port);
102 jack_nframes_t nframes = jack_get_buffer_size(_jack_client);
106 if (req.mode == O_RDWR || req.mode == O_WRONLY) {
107 _jack_output_port = jack_port_register(_jack_client,
108 string(req.tagname).append("_out").c_str(),
109 JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0);
110 jack_midi_clear_buffer(
111 jack_port_get_buffer(_jack_output_port, nframes));
112 ret = ret && (_jack_output_port != NULL);
115 if (req.mode == O_RDWR || req.mode == O_RDONLY) {
116 _jack_input_port = jack_port_register(_jack_client,
117 string(req.tagname).append("_in").c_str(),
118 JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0);
119 jack_midi_clear_buffer(
120 jack_port_get_buffer(_jack_input_port, nframes));
121 ret = ret && (_jack_input_port != NULL);