summaryrefslogtreecommitdiff
path: root/libs/ardour/async_midi_port.cc
blob: 06a51279fe4262eaf754a863608b033b22505264 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
 * Copyright (C) 1998-2017 Paul Davis <paul@linuxaudiosystems.com>
 * Copyright (C) 2013-2014 John Emmas <john@creativepost.co.uk>
 * Copyright (C) 2014-2016 David Robillard <d@drobilla.net>
 * Copyright (C) 2015-2016 Robin Gareus <robin@gareus.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <iostream>
#include <vector>

#include <glibmm/timer.h>

#include "pbd/error.h"
#include "pbd/stacktrace.h"

#include "midi++/types.h"

#include "ardour/async_midi_port.h"
#include "ardour/audioengine.h"
#include "ardour/midi_buffer.h"

using namespace MIDI;
using namespace ARDOUR;
using namespace std;
using namespace PBD;

pthread_t AsyncMIDIPort::_process_thread;

#define port_engine AudioEngine::instance()->port_engine()

AsyncMIDIPort::AsyncMIDIPort (string const & name, PortFlags flags)
	: MidiPort (name, flags)
	, MIDI::Port (name, MIDI::Port::Flags (0))
	, _currently_in_cycle (false)
	, _last_write_timestamp (0)
	, _flush_at_cycle_start (false)
	, have_timer (false)
	, output_fifo (2048)
	, input_fifo (1024)
	, _xthread (true)
{
}

AsyncMIDIPort::~AsyncMIDIPort ()
{
}

void
AsyncMIDIPort::set_timer (boost::function<MIDI::samplecnt_t (void)>& f)
{
	timer = f;
	have_timer = true;
}

void
AsyncMIDIPort::flush_output_fifo (MIDI::pframes_t nframes)
{
	RingBuffer< Evoral::Event<double> >::rw_vector vec = { { 0, 0 }, { 0, 0 } };
	size_t written = 0;

	output_fifo.get_read_vector (&vec);

	MidiBuffer& mb (get_midi_buffer (nframes));

	if (vec.len[0]) {
		Evoral::Event<double>* evp = vec.buf[0];

		assert (evp->size());
		assert (evp->buffer());

		for (size_t n = 0; n < vec.len[0]; ++n, ++evp) {
			if (mb.push_back (evp->time(), evp->size(), evp->buffer())) {
				written++;
			}
		}
	}

	if (vec.len[1]) {
		Evoral::Event<double>* evp = vec.buf[1];

		assert (evp->size());
		assert (evp->buffer());

		for (size_t n = 0; n < vec.len[1]; ++n, ++evp) {
			if (mb.push_back (evp->time(), evp->size(), evp->buffer())) {
				written++;
			}
		}
	}

	/* do this "atomically" after we're done pushing events into the
	 * MidiBuffer
	 */

	output_fifo.increment_read_idx (written);
}

void
AsyncMIDIPort::cycle_start (MIDI::pframes_t nframes)
{
	_currently_in_cycle = true;
	MidiPort::cycle_start (nframes);

	/* dump anything waiting in the output FIFO at the start of the port
	 * buffer
	 */

	if (ARDOUR::Port::sends_output()) {
		flush_output_fifo (nframes);
		if (_flush_at_cycle_start) {
			flush_buffers (nframes);
		}
	}

	/* copy incoming data from the port buffer into the input FIFO
	   and if necessary wakeup the reader
	*/

	if (ARDOUR::Port::receives_input()) {

		void* buffer = port_engine.get_buffer (_port_handle, nframes);
		const pframes_t event_count = port_engine.get_midi_event_count (buffer);

		for (pframes_t i = 0; i < event_count; ++i) {

			pframes_t timestamp;
			size_t size;
			uint8_t const* buf;

			port_engine.midi_event_get (timestamp, size, &buf, buffer, i);

			if (buf[0] == 0xfe) {
				/* throw away active sensing */
				continue;
			}

			samplecnt_t when;

			if (have_timer) {
				when = timer ();
			} else {
				when = AudioEngine::instance()->sample_time_at_cycle_start() + timestamp;
			}

			input_fifo.write (when, Evoral::NO_EVENT, size, buf);
		}

		if (event_count) {
			_xthread.wakeup ();
		}

	}
}

void
AsyncMIDIPort::cycle_end (MIDI::pframes_t nframes)
{
	if (ARDOUR::Port::sends_output() && !_flush_at_cycle_start) {
		/* move any additional data from output FIFO into the port
		   buffer.
		*/
		flush_output_fifo (nframes);
	}

	MidiPort::cycle_end (nframes);

	_currently_in_cycle = false;
}

/** wait for the output FIFO to be emptied by successive process() callbacks.
 *
 * Cannot be called from a processing thread.
 */
void
AsyncMIDIPort::drain (int check_interval_usecs, int total_usecs_to_wait)
{
	RingBuffer< Evoral::Event<double> >::rw_vector vec = { { 0, 0 }, { 0, 0} };

	if (!AudioEngine::instance()->running() || AudioEngine::instance()->session() == 0) {
		/* no more process calls - it will never drain */
		return;
	}


	if (is_process_thread()) {
		error << "Process thread called MIDI::AsyncMIDIPort::drain() - this cannot work" << endmsg;
		return;
	}

	microseconds_t now = get_microseconds ();
	microseconds_t end = now + total_usecs_to_wait;

	while (now < end) {
		output_fifo.get_write_vector (&vec);
		if (vec.len[0] + vec.len[1] >= output_fifo.bufsize() - 1) {
			break;
		}
		Glib::usleep (check_interval_usecs);
		now = get_microseconds();
	}
}

int
AsyncMIDIPort::write (const MIDI::byte * msg, size_t msglen, MIDI::timestamp_t timestamp)
{
	int ret = 0;

	if (!ARDOUR::Port::sends_output()) {
		return ret;
	}

	if (!is_process_thread()) {

		/* this is the best estimate of "when" this MIDI data is being
		 * delivered
		 */

		_parser->set_timestamp (AudioEngine::instance()->sample_time() + timestamp);
		for (size_t n = 0; n < msglen; ++n) {
			_parser->scanner (msg[n]);
		}

		Glib::Threads::Mutex::Lock lm (output_fifo_lock);
		RingBuffer< Evoral::Event<double> >::rw_vector vec = { { 0, 0 }, { 0, 0} };

		output_fifo.get_write_vector (&vec);

		if (vec.len[0] + vec.len[1] < 1) {
			error << "no space in FIFO for non-process thread MIDI write" << endmsg;
			return 0;
		}

		if (vec.len[0]) {
			/* force each event inside the ringbuffer to own its
			   own buffer, but let that be null and of zero size
			   initially. When ::set() is called, the buffer will
			   be allocated to hold a *copy* of the data we're
			   storing, and then that buffer will be used over and
			   over, occasionally being upwardly resized as
			   necessary.
			*/
			if (!vec.buf[0]->owns_buffer()) {
				vec.buf[0]->set_buffer (0, 0, true);
			}
			vec.buf[0]->set (msg, msglen, timestamp);
		} else {
			/* see comment in previous branch of if() statement */
			if (!vec.buf[1]->owns_buffer()) {
				vec.buf[1]->set_buffer (0, 0, true);
			}
			vec.buf[1]->set (msg, msglen, timestamp);
		}

		output_fifo.increment_write_idx (1);

		ret = msglen;

	} else {

		_parser->set_timestamp (AudioEngine::instance()->sample_time_at_cycle_start() + timestamp);
		for (size_t n = 0; n < msglen; ++n) {
			_parser->scanner (msg[n]);
		}

		if (timestamp >= _cycle_nframes) {
			std::cerr << "attempting to write MIDI event of " << msglen << " MIDI::bytes at time "
				  << timestamp << " of " << _cycle_nframes
				  << " (this will not work - needs a code fix)"
				  << std::endl;
		}

		/* This is the process thread, which makes checking
		 * _currently_in_cycle atomic and safe, since it is only
		 * set from cycle_start() and cycle_end(), also called
		 * only from the process thread.
		 */

		if (_currently_in_cycle) {

			MidiBuffer& mb (get_midi_buffer (_cycle_nframes));

			if (timestamp == 0) {
				timestamp = _last_write_timestamp;
			}

			if (mb.push_back (timestamp, msglen, msg)) {
				ret = msglen;
				_last_write_timestamp = timestamp;

			} else {
				cerr << "AsyncMIDIPort (" << ARDOUR::Port::name() << "): write of " << msglen << " @ " << timestamp << " failed\n" << endl;
				PBD::stacktrace (cerr, 20);
				ret = 0;
			}
		} else {
			cerr << "write to JACK midi port failed: not currently in a process cycle." << endl;
			PBD::stacktrace (cerr, 20);
		}
	}

	return ret;
}


int
AsyncMIDIPort::read (MIDI::byte *, size_t)
{
	if (!ARDOUR::Port::receives_input()) {
		return 0;
	}

	timestamp_t time;
	Evoral::EventType type;
	uint32_t size;
	vector<MIDI::byte> buffer(input_fifo.capacity());

	while (input_fifo.read (&time, &type, &size, &buffer[0])) {
		_parser->set_timestamp (time);
		for (uint32_t i = 0; i < size; ++i) {
			_parser->scanner (buffer[i]);
		}
	}

	return 0;
}

void
AsyncMIDIPort::parse (MIDI::samplecnt_t)
{
	MIDI::byte buf[1];

	/* see ::read() to realize why buf is not used */
	read (buf, sizeof (buf));
}

void
AsyncMIDIPort::set_process_thread (pthread_t thr)
{
	_process_thread = thr;
}

bool
AsyncMIDIPort::is_process_thread()
{
	return pthread_equal (pthread_self(), _process_thread);
}