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>
27 #include <midi++/jackcompat.h>
32 JACK_MidiPort::JACK_MidiPort(PortRequest & req, jack_client_t* jack_client)
34 , _jack_client(jack_client)
35 , _jack_input_port(NULL)
36 , _jack_output_port(NULL)
39 int err = create_ports(req);
42 req.status = PortRequest::OK;
45 req.status = PortRequest::Unknown;
49 JACK_MidiPort::~JACK_MidiPort()
55 JACK_MidiPort::cycle_start (nframes_t nframes)
57 Port::cycle_start(nframes);
58 assert(_nframes_this_cycle == nframes);
60 jack_midi_clear_buffer(jack_port_get_buffer(_jack_output_port, nframes));
64 JACK_MidiPort::write(byte * msg, size_t msglen, timestamp_t timestamp)
66 assert(_currently_in_cycle);
67 assert(timestamp < _nframes_this_cycle);
68 assert(_jack_output_port);
70 // FIXME: return value correct?
71 return jack_midi_event_write (
72 jack_port_get_buffer(_jack_output_port, _nframes_this_cycle),
73 timestamp, msg, msglen);
77 JACK_MidiPort::read(byte * buf, size_t max, timestamp_t timestamp)
79 assert(_currently_in_cycle);
80 assert(timestamp < _nframes_this_cycle);
81 assert(_jack_input_port);
85 int err = jack_midi_event_get (&ev,
86 jack_port_get_buffer(_jack_input_port, _nframes_this_cycle),
90 memcpy(buf, ev.buffer, ev.size);
98 JACK_MidiPort::create_ports(PortRequest & req)
100 assert(!_jack_input_port);
101 assert(!_jack_output_port);
103 jack_nframes_t nframes = jack_get_buffer_size(_jack_client);
107 if (req.mode == O_RDWR || req.mode == O_WRONLY) {
108 _jack_output_port = jack_port_register(_jack_client,
109 string(req.tagname).append("_out").c_str(),
110 JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0);
111 jack_midi_clear_buffer(
112 jack_port_get_buffer(_jack_output_port, nframes));
113 ret = ret && (_jack_output_port != NULL);
116 if (req.mode == O_RDWR || req.mode == O_RDONLY) {
117 _jack_input_port = jack_port_register(_jack_client,
118 string(req.tagname).append("_in").c_str(),
119 JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0);
120 jack_midi_clear_buffer(
121 jack_port_get_buffer(_jack_input_port, nframes));
122 ret = ret && (_jack_input_port != NULL);