initial fixes to get build to work without new JACK MIDI, and to get it running with...
[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
24 #include <midi++/types.h>
25 #include <midi++/jack.h>
26 #include <midi++/port_request.h>
27 #include <midi++/jackcompat.h>
28
29 using namespace std;
30 using namespace MIDI;
31
32 JACK_MidiPort::JACK_MidiPort(PortRequest & req, jack_client_t* jack_client)
33         : Port(req)
34         , _jack_client(jack_client)
35         , _jack_input_port(NULL)
36         , _jack_output_port(NULL)
37         , _last_read_index(0)
38 {
39         int err = create_ports(req);
40
41         if (!err) {
42                 req.status = PortRequest::OK;
43                 _ok = true;
44         } else {
45                 req.status = PortRequest::Unknown;
46         }
47 }
48
49 JACK_MidiPort::~JACK_MidiPort()
50 {
51         // FIXME: remove port
52 }
53
54 void
55 JACK_MidiPort::cycle_start (nframes_t nframes)
56 {
57         Port::cycle_start(nframes);
58         assert(_nframes_this_cycle == nframes);
59         _last_read_index = 0;
60         jack_midi_clear_buffer(jack_port_get_buffer(_jack_output_port, nframes));
61 }
62
63 int
64 JACK_MidiPort::write(byte * msg, size_t msglen, timestamp_t timestamp)
65 {
66         assert(_currently_in_cycle);
67         assert(timestamp < _nframes_this_cycle);
68         assert(_jack_output_port);
69
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);
74 }
75
76 int
77 JACK_MidiPort::read(byte * buf, size_t max, timestamp_t timestamp)
78 {
79         assert(_currently_in_cycle);
80         assert(timestamp < _nframes_this_cycle);
81         assert(_jack_input_port);
82
83         jack_midi_event_t ev;
84
85         int err = jack_midi_event_get (&ev,
86                 jack_port_get_buffer(_jack_input_port, _nframes_this_cycle),
87                 _last_read_index++);
88         
89         if (!err) {
90                 memcpy(buf, ev.buffer, ev.size);
91                 return ev.size;
92         } else {
93                 return 0;
94         }
95 }
96
97 int
98 JACK_MidiPort::create_ports(PortRequest & req)
99 {
100         assert(!_jack_input_port);
101         assert(!_jack_output_port);
102         
103         jack_nframes_t nframes = jack_get_buffer_size(_jack_client);
104
105         bool ret = true;
106
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);
114         }
115
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);
123         }
124
125         return ret ? 0 : -1;
126 }
127