2 Copyright (C) 1998 Paul Barton-Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <glibmm/timer.h>
26 #include "pbd/error.h"
27 #include "pbd/stacktrace.h"
29 #include "midi++/types.h"
31 #include "ardour/async_midi_port.h"
32 #include "ardour/audioengine.h"
33 #include "ardour/midi_buffer.h"
36 using namespace ARDOUR;
40 pthread_t AsyncMIDIPort::_process_thread;
42 #define port_engine AudioEngine::instance()->port_engine()
44 AsyncMIDIPort::AsyncMIDIPort (string const & name, PortFlags flags)
45 : MidiPort (name, flags)
46 , MIDI::Port (name, MIDI::Port::Flags (0))
47 , _currently_in_cycle (false)
48 , _last_write_timestamp (0)
56 AsyncMIDIPort::~AsyncMIDIPort ()
61 AsyncMIDIPort::set_timer (boost::function<MIDI::framecnt_t (void)>& f)
68 AsyncMIDIPort::flush_output_fifo (MIDI::pframes_t nframes)
70 RingBuffer< Evoral::Event<double> >::rw_vector vec = { { 0, 0 }, { 0, 0 } };
73 output_fifo.get_read_vector (&vec);
75 MidiBuffer& mb (get_midi_buffer (nframes));
78 Evoral::Event<double>* evp = vec.buf[0];
81 assert (evp->buffer());
83 for (size_t n = 0; n < vec.len[0]; ++n, ++evp) {
84 if (mb.push_back (evp->time(), evp->size(), evp->buffer())) {
91 Evoral::Event<double>* evp = vec.buf[1];
94 assert (evp->buffer());
96 for (size_t n = 0; n < vec.len[1]; ++n, ++evp) {
97 if (mb.push_back (evp->time(), evp->size(), evp->buffer())) {
103 /* do this "atomically" after we're done pushing events into the
107 output_fifo.increment_read_idx (written);
111 AsyncMIDIPort::cycle_start (MIDI::pframes_t nframes)
113 _currently_in_cycle = true;
114 MidiPort::cycle_start (nframes);
116 /* dump anything waiting in the output FIFO at the start of the port
120 if (ARDOUR::Port::sends_output()) {
121 flush_output_fifo (nframes);
124 /* copy incoming data from the port buffer into the input FIFO
125 and if necessary wakeup the reader
128 if (ARDOUR::Port::receives_input()) {
129 MidiBuffer& mb (get_midi_buffer (nframes));
135 when = AudioEngine::instance()->sample_time_at_cycle_start();
138 for (MidiBuffer::iterator b = mb.begin(); b != mb.end(); ++b) {
142 input_fifo.write (when, (Evoral::EventType) 0, (*b).size(), (*b).buffer());
152 AsyncMIDIPort::cycle_end (MIDI::pframes_t nframes)
154 if (ARDOUR::Port::sends_output()) {
155 /* move any additional data from output FIFO into the port
158 flush_output_fifo (nframes);
161 MidiPort::cycle_end (nframes);
163 _currently_in_cycle = false;
166 /** wait for the output FIFO to be emptied by successive process() callbacks.
168 * Cannot be called from a processing thread.
171 AsyncMIDIPort::drain (int check_interval_usecs)
173 RingBuffer< Evoral::Event<double> >::rw_vector vec = { { 0, 0 }, { 0, 0} };
175 if (!AudioEngine::instance()->running() || AudioEngine::instance()->session() == 0) {
176 /* no more process calls - it will never drain */
181 if (is_process_thread()) {
182 error << "Process thread called MIDI::AsyncMIDIPort::drain() - this cannot work" << endmsg;
187 output_fifo.get_write_vector (&vec);
188 if (vec.len[0] + vec.len[1] >= output_fifo.bufsize() - 1) {
191 Glib::usleep (check_interval_usecs);
196 AsyncMIDIPort::write (const MIDI::byte * msg, size_t msglen, MIDI::timestamp_t timestamp)
200 if (!ARDOUR::Port::sends_output()) {
204 if (!is_process_thread()) {
206 /* this is the best estimate of "when" this MIDI data is being
210 _parser->set_timestamp (AudioEngine::instance()->sample_time() + timestamp);
211 for (size_t n = 0; n < msglen; ++n) {
212 _parser->scanner (msg[n]);
215 Glib::Threads::Mutex::Lock lm (output_fifo_lock);
216 RingBuffer< Evoral::Event<double> >::rw_vector vec = { { 0, 0 }, { 0, 0} };
218 output_fifo.get_write_vector (&vec);
220 if (vec.len[0] + vec.len[1] < 1) {
221 error << "no space in FIFO for non-process thread MIDI write" << endmsg;
226 /* force each event inside the ringbuffer to own its
227 own buffer, but let that be null and of zero size
228 initially. When ::set() is called, the buffer will
229 be allocated to hold a *copy* of the data we're
230 storing, and then that buffer will be used over and
231 over, occasionally being upwardly resized as
234 if (!vec.buf[0]->owns_buffer()) {
235 vec.buf[0]->set_buffer (0, 0, true);
237 vec.buf[0]->set (msg, msglen, timestamp);
239 /* see comment in previous branch of if() statement */
240 if (!vec.buf[1]->owns_buffer()) {
241 vec.buf[1]->set_buffer (0, 0, true);
243 vec.buf[1]->set (msg, msglen, timestamp);
246 output_fifo.increment_write_idx (1);
252 _parser->set_timestamp (AudioEngine::instance()->sample_time_at_cycle_start() + timestamp);
253 for (size_t n = 0; n < msglen; ++n) {
254 _parser->scanner (msg[n]);
257 if (timestamp >= _cycle_nframes) {
258 std::cerr << "attempting to write MIDI event of " << msglen << " MIDI::bytes at time "
259 << timestamp << " of " << _cycle_nframes
260 << " (this will not work - needs a code fix)"
264 /* This is the process thread, which makes checking
265 * _currently_in_cycle atomic and safe, since it is only
266 * set from cycle_start() and cycle_end(), also called
267 * only from the process thread.
270 if (_currently_in_cycle) {
272 MidiBuffer& mb (get_midi_buffer (_cycle_nframes));
274 if (timestamp == 0) {
275 timestamp = _last_write_timestamp;
278 if (mb.push_back (timestamp, msglen, msg)) {
280 _last_write_timestamp = timestamp;
283 cerr << "AsyncMIDIPort (" << ARDOUR::Port::name() << "): write of " << msglen << " @ " << timestamp << " failed\n" << endl;
284 PBD::stacktrace (cerr, 20);
288 cerr << "write to JACK midi port failed: not currently in a process cycle." << endl;
289 PBD::stacktrace (cerr, 20);
298 AsyncMIDIPort::read (MIDI::byte *, size_t)
300 if (!ARDOUR::Port::receives_input()) {
305 Evoral::EventType type;
307 vector<MIDI::byte> buffer(input_fifo.capacity());
309 while (input_fifo.read (&time, &type, &size, &buffer[0])) {
310 _parser->set_timestamp (time);
311 for (uint32_t i = 0; i < size; ++i) {
312 _parser->scanner (buffer[i]);
320 AsyncMIDIPort::parse (MIDI::framecnt_t)
324 /* see ::read() to realize why buf is not used */
325 read (buf, sizeof (buf));
329 AsyncMIDIPort::set_process_thread (pthread_t thr)
331 _process_thread = thr;
335 AsyncMIDIPort::is_process_thread()
337 return pthread_equal (pthread_self(), _process_thread);