fix up and re-enable MTC transmission
[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 <pbd/error.h>
25
26 #include <midi++/types.h>
27 #include <midi++/jack.h>
28
29 using namespace std;
30 using namespace MIDI;
31 using namespace PBD;
32
33 pthread_t JACK_MidiPort::_process_thread;
34
35 JACK_MidiPort::JACK_MidiPort(const XMLNode& node, jack_client_t* jack_client)
36         : Port(node)
37         , _jack_client(jack_client)
38         , _jack_input_port(NULL)
39         , _jack_output_port(NULL)
40         , _last_read_index(0)
41         , non_process_thread_fifo (512)
42 {
43         int err = create_ports (node);
44
45         if (!err) {
46                 _ok = true;
47         } 
48 }
49
50 JACK_MidiPort::~JACK_MidiPort()
51 {
52         // FIXME: remove port
53 }
54
55 void
56 JACK_MidiPort::cycle_start (nframes_t nframes)
57 {
58         Port::cycle_start(nframes);
59         assert(_nframes_this_cycle == nframes);
60         _last_read_index = 0;
61
62         void *buffer = jack_port_get_buffer (_jack_output_port, nframes);
63         jack_midi_clear_buffer (buffer);
64         flush (buffer);
65 }
66
67 int
68 JACK_MidiPort::write(byte * msg, size_t msglen, timestamp_t timestamp)
69 {
70         if (!is_process_thread()) {
71
72                 Glib::Mutex::Lock lm (non_process_thread_fifo_lock);
73                 RingBuffer<Event>::rw_vector vec;
74                 
75                 non_process_thread_fifo.get_write_vector (&vec);
76
77                 cerr << "Non-process thread writes " << msglen << " to " << name() << endl;
78
79                 if (vec.len[0] + vec.len[1] < 1) {
80                         error << "no space in FIFO for non-process thread MIDI write"
81                               << endmsg;
82                         return 0;
83                 }
84
85                 if (vec.len[0]) {
86                         vec.buf[0]->set (msg, msglen, timestamp);
87                 } else {
88                         vec.buf[1]->set (msg, msglen, timestamp);
89                 }
90
91                 non_process_thread_fifo.increment_write_idx (1);
92
93                 return msglen;
94                                 
95         } else {
96
97                 assert(_currently_in_cycle);
98                 assert(timestamp < _nframes_this_cycle);
99                 assert(_jack_output_port);
100
101                 // FIXME: return value correct?
102                 return jack_midi_event_write (jack_port_get_buffer (_jack_output_port, _nframes_this_cycle), 
103                                               timestamp, msg, msglen);
104         }
105 }
106
107 void
108 JACK_MidiPort::flush (void* jack_port_buffer)
109 {
110         RingBuffer<Event>::rw_vector vec;
111         size_t written;
112
113         non_process_thread_fifo.get_read_vector (&vec);
114
115         if (vec.len[0] + vec.len[1]) {
116                 cerr << "Flush " << vec.len[0] + vec.len[1] << " events from non-process FIFO\n";
117         }
118
119         if (vec.len[0]) {
120                 Event* evp = vec.buf[0];
121                 
122                 for (size_t n = 0; n < vec.len[0]; ++n, ++evp) {
123                         jack_midi_event_write (jack_port_buffer,
124                                                (timestamp_t) evp->time(), evp->buffer(), evp->size());
125                 }
126         }
127         
128         if (vec.len[1]) {
129                 Event* evp = vec.buf[1];
130
131                 for (size_t n = 0; n < vec.len[1]; ++n, ++evp) {
132                         jack_midi_event_write (jack_port_buffer,
133                                                (timestamp_t) evp->time(), evp->buffer(), evp->size());
134                 }
135         }
136         
137         if ((written = vec.len[0] + vec.len[1]) != 0) {
138                 non_process_thread_fifo.increment_read_idx (written);
139         }
140 }
141
142 int
143 JACK_MidiPort::read(byte * buf, size_t bufsize)
144 {
145         assert(_currently_in_cycle);
146         assert(_jack_input_port);
147
148         jack_midi_event_t ev;
149
150         int err = jack_midi_event_get (&ev,
151                                        jack_port_get_buffer(_jack_input_port, _nframes_this_cycle),
152                                        _last_read_index++);
153         
154         // XXX this doesn't handle ev.size > max
155
156         if (!err) {
157                 size_t limit = min (bufsize, ev.size);
158                 memcpy(buf, ev.buffer, limit);
159                 return limit;
160         } else {
161                 return 0;
162         }
163 }
164
165 int
166 JACK_MidiPort::create_ports(const XMLNode& node)
167 {
168         Descriptor desc (node);
169
170         assert(!_jack_input_port);
171         assert(!_jack_output_port);
172         
173         jack_nframes_t nframes = jack_get_buffer_size(_jack_client);
174
175         bool ret = true;
176
177         if (desc.mode == O_RDWR || desc.mode == O_WRONLY) {
178                 _jack_output_port = jack_port_register(_jack_client,
179                                                        string(desc.tag).append("_out").c_str(),
180                                                        JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0);
181                 jack_midi_clear_buffer(jack_port_get_buffer(_jack_output_port, nframes));
182                 ret = ret && (_jack_output_port != NULL);
183         }
184         
185         if (desc.mode == O_RDWR || desc.mode == O_RDONLY) {
186                 _jack_input_port = jack_port_register(_jack_client,
187                                                       string(desc.tag).append("_in").c_str(),
188                                                       JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0);
189                 jack_midi_clear_buffer(jack_port_get_buffer(_jack_input_port, nframes));
190                 ret = ret && (_jack_input_port != NULL);
191         }
192
193         return ret ? 0 : -1;
194 }
195
196 XMLNode& 
197 JACK_MidiPort::get_state () const
198 {
199         XMLNode& root (Port::get_state ());
200         return root;
201 }
202
203 void
204 JACK_MidiPort::set_state (const XMLNode& node)
205 {
206 }
207
208 void
209 JACK_MidiPort::set_process_thread (pthread_t thr)
210 {
211         _process_thread = thr;
212 }
213
214 bool
215 JACK_MidiPort::is_process_thread()
216 {
217         return (pthread_self() == _process_thread);
218 }