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