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;
41 template class EventRingBuffer<MIDI::timestamp_t>;
44 pthread_t AsyncMIDIPort::_process_thread;
46 #define port_engine AudioEngine::instance()->port_engine()
48 AsyncMIDIPort::AsyncMIDIPort (string const & name, PortFlags flags)
49 : MidiPort (name, flags)
50 , MIDI::Port (name, MIDI::Port::Flags (0))
51 , _currently_in_cycle (false)
52 , _last_write_timestamp (0)
56 #ifndef PLATFORM_WINDOWS
62 AsyncMIDIPort::~AsyncMIDIPort ()
67 AsyncMIDIPort::set_timer (boost::function<MIDI::framecnt_t (void)>& f)
74 AsyncMIDIPort::flush_output_fifo (MIDI::pframes_t nframes)
76 RingBuffer< Evoral::Event<double> >::rw_vector vec = { { 0, 0 }, { 0, 0 } };
79 output_fifo.get_read_vector (&vec);
81 MidiBuffer& mb (get_midi_buffer (nframes));
84 Evoral::Event<double>* evp = vec.buf[0];
86 for (size_t n = 0; n < vec.len[0]; ++n, ++evp) {
87 mb.push_back (evp->time(), evp->size(), evp->buffer());
92 Evoral::Event<double>* evp = vec.buf[1];
94 for (size_t n = 0; n < vec.len[1]; ++n, ++evp) {
95 mb.push_back (evp->time(), evp->size(), evp->buffer());
99 if ((written = vec.len[0] + vec.len[1]) != 0) {
100 output_fifo.increment_read_idx (written);
105 AsyncMIDIPort::cycle_start (MIDI::pframes_t nframes)
107 _currently_in_cycle = true;
108 MidiPort::cycle_start (nframes);
110 /* dump anything waiting in the output FIFO at the start of the port
114 if (ARDOUR::Port::sends_output()) {
115 flush_output_fifo (nframes);
118 /* copy incoming data from the port buffer into the input FIFO
119 and if necessary wakeup the reader
122 if (ARDOUR::Port::receives_input()) {
123 MidiBuffer& mb (get_midi_buffer (nframes));
129 when = AudioEngine::instance()->sample_time_at_cycle_start();
132 for (MidiBuffer::iterator b = mb.begin(); b != mb.end(); ++b) {
136 input_fifo.write (when, (Evoral::EventType) 0, (*b).size(), (*b).buffer());
139 #ifndef PLATFORM_WINDOWS
148 AsyncMIDIPort::cycle_end (MIDI::pframes_t nframes)
150 if (ARDOUR::Port::sends_output()) {
151 /* move any additional data from output FIFO into the port
154 flush_output_fifo (nframes);
157 MidiPort::cycle_end (nframes);
159 _currently_in_cycle = false;
162 /** wait for the output FIFO to be emptied by successive process() callbacks.
164 * Cannot be called from a processing thread.
167 AsyncMIDIPort::drain (int check_interval_usecs)
169 RingBuffer< Evoral::Event<double> >::rw_vector vec = { { 0, 0 }, { 0, 0} };
171 if (!AudioEngine::instance()->running() || AudioEngine::instance()->session() == 0) {
172 /* no more process calls - it will never drain */
177 if (is_process_thread()) {
178 error << "Process thread called MIDI::AsyncMIDIPort::drain() - this cannot work" << endmsg;
183 output_fifo.get_write_vector (&vec);
184 if (vec.len[0] + vec.len[1] >= output_fifo.bufsize() - 1) {
187 Glib::usleep (check_interval_usecs);
192 AsyncMIDIPort::write (const MIDI::byte * msg, size_t msglen, MIDI::timestamp_t timestamp)
196 if (!ARDOUR::Port::sends_output()) {
200 if (!is_process_thread()) {
202 /* this is the best estimate of "when" this MIDI data is being
206 _parser->set_timestamp (AudioEngine::instance()->sample_time() + timestamp);
207 for (size_t n = 0; n < msglen; ++n) {
208 _parser->scanner (msg[n]);
211 Glib::Threads::Mutex::Lock lm (output_fifo_lock);
212 RingBuffer< Evoral::Event<double> >::rw_vector vec = { { 0, 0 }, { 0, 0} };
214 output_fifo.get_write_vector (&vec);
216 if (vec.len[0] + vec.len[1] < 1) {
217 error << "no space in FIFO for non-process thread MIDI write" << endmsg;
222 if (!vec.buf[0]->owns_buffer()) {
223 vec.buf[0]->set_buffer (0, 0, true);
225 vec.buf[0]->set (msg, msglen, timestamp);
227 if (!vec.buf[1]->owns_buffer()) {
228 vec.buf[1]->set_buffer (0, 0, true);
230 vec.buf[1]->set (msg, msglen, timestamp);
233 output_fifo.increment_write_idx (1);
239 _parser->set_timestamp (AudioEngine::instance()->sample_time_at_cycle_start() + timestamp);
240 for (size_t n = 0; n < msglen; ++n) {
241 _parser->scanner (msg[n]);
244 if (timestamp >= _cycle_nframes) {
245 std::cerr << "attempting to write MIDI event of " << msglen << " MIDI::bytes at time "
246 << timestamp << " of " << _cycle_nframes
247 << " (this will not work - needs a code fix)"
251 /* This is the process thread, which makes checking
252 * _currently_in_cycle atomic and safe, since it is only
253 * set from cycle_start() and cycle_end(), also called
254 * only from the process thread.
257 if (_currently_in_cycle) {
259 MidiBuffer& mb (get_midi_buffer (_cycle_nframes));
261 if (timestamp == 0) {
262 timestamp = _last_write_timestamp;
265 if (mb.push_back (timestamp, msglen, msg)) {
267 _last_write_timestamp = timestamp;
270 cerr << "AsyncMIDIPort (" << ARDOUR::Port::name() << "): write of " << msglen << " @ " << timestamp << " failed\n" << endl;
271 PBD::stacktrace (cerr, 20);
275 cerr << "write to JACK midi port failed: not currently in a process cycle." << endl;
276 PBD::stacktrace (cerr, 20);
285 AsyncMIDIPort::read (MIDI::byte *, size_t)
287 if (!ARDOUR::Port::receives_input()) {
292 Evoral::EventType type;
294 vector<MIDI::byte> buffer(input_fifo.capacity());
296 while (input_fifo.read (&time, &type, &size, &buffer[0])) {
297 _parser->set_timestamp (time);
298 for (uint32_t i = 0; i < size; ++i) {
299 _parser->scanner (buffer[i]);
307 AsyncMIDIPort::parse (MIDI::framecnt_t)
311 /* see ::read() to realize why buf is not used */
312 read (buf, sizeof (buf));
316 AsyncMIDIPort::set_process_thread (pthread_t thr)
318 _process_thread = thr;
322 AsyncMIDIPort::is_process_thread()
324 return pthread_equal (pthread_self(), _process_thread);