summaryrefslogtreecommitdiff
path: root/libjack/midiport.c
blob: d5e607df2cb9e22f48f82e4e7d6fda2baa3b6e52 (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
/*
    Copyright (C) 2004-2006 Ian Esten
    Copyright (C) 2006 Dave Robillard

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

#include <jack/jack.h>
#include <jack/midiport.h>

#include "port.h"

enum { MIDI_INLINE_MAX = 4 }; /* 4 bytes for default event size */

typedef struct _jack_midi_port_info_private {
	jack_nframes_t nframes;         /**< Number of frames in buffer */
	uint32_t buffer_size;           /**< Size of buffer in bytes */
	uint32_t event_count;           /**< Number of events stored in this buffer */
	jack_nframes_t last_write_loc;  /**< Used for both writing and mixdown */
	uint32_t events_lost;           /**< Number of events lost in this buffer */
} POST_PACKED_STRUCTURE jack_midi_port_info_private_t;

typedef struct _jack_midi_port_internal_event {
	uint16_t time;  /* offset within buffer limit to 64k */
	uint16_t size;  /* event size limited to 64k */
	union {
		jack_shmsize_t byte_offset;
		jack_midi_data_t inline_data[MIDI_INLINE_MAX];
	} POST_PACKED_STRUCTURE;
} POST_PACKED_STRUCTURE jack_midi_port_internal_event_t;

size_t
jack_midi_internal_event_size ()
{
	return sizeof(jack_midi_port_internal_event_t);
}

static inline jack_midi_data_t*
jack_midi_event_data (void* port_buffer,
		      const jack_midi_port_internal_event_t* event)
{
	if (event->size <= MIDI_INLINE_MAX) {
		return (jack_midi_data_t*)event->inline_data;
	} else {
		return ((jack_midi_data_t*)port_buffer) + event->byte_offset;
	}
}


/* jack_midi_port_functions.buffer_init */
static void
jack_midi_buffer_init (void  *port_buffer,
		       size_t buffer_size,
		       jack_nframes_t nframes)
{
	jack_midi_port_info_private_t *info =
		(jack_midi_port_info_private_t*)port_buffer;

	/* We can also add some magic field to midi buffer to validate client calls */
	info->nframes = nframes;
	info->buffer_size = buffer_size;
	info->event_count = 0;
	info->last_write_loc = 0;
	info->events_lost = 0;
}


uint32_t
jack_midi_get_event_count (void           *port_buffer)
{
	jack_midi_port_info_private_t *info =
		(jack_midi_port_info_private_t*)port_buffer;

	return info->event_count;
}


int
jack_midi_event_get (jack_midi_event_t *event,
		     void              *port_buffer,
		     uint32_t event_idx)
{
	jack_midi_port_internal_event_t *port_event;
	jack_midi_port_info_private_t *info =
		(jack_midi_port_info_private_t*)port_buffer;

	if (event_idx >= info->event_count)
#ifdef ENODATA
	{ return ENODATA; }
#else
	{ return ENOMSG; }
#endif

	port_event = (jack_midi_port_internal_event_t*)(info + 1);
	port_event += event_idx;
	event->time = port_event->time;
	event->size = port_event->size;
	event->buffer = jack_midi_event_data (port_buffer, port_event);

	return 0;
}


size_t
jack_midi_max_event_size (void           *port_buffer)
{
	jack_midi_port_info_private_t *info =
		(jack_midi_port_info_private_t*)port_buffer;
	size_t buffer_size =
		info->buffer_size;

	/* (event_count + 1) below accounts for jack_midi_port_internal_event_t
	 * which would be needed to store the next event */
	size_t used_size = sizeof(jack_midi_port_info_private_t)
			   + info->last_write_loc
			   + ((info->event_count + 1)
			      * sizeof(jack_midi_port_internal_event_t));

	if (used_size > buffer_size) {
		return 0;
	} else if ((buffer_size - used_size) < MIDI_INLINE_MAX) {
		return MIDI_INLINE_MAX;
	} else {
		return buffer_size - used_size;
	}
}


jack_midi_data_t*
jack_midi_event_reserve (void           *port_buffer,
			 jack_nframes_t time,
			 size_t data_size)
{
	jack_midi_data_t *retbuf = (jack_midi_data_t*)port_buffer;

	jack_midi_port_info_private_t *info =
		(jack_midi_port_info_private_t*)port_buffer;
	jack_midi_port_internal_event_t *event_buffer =
		(jack_midi_port_internal_event_t*)(info + 1);
	size_t buffer_size =
		info->buffer_size;

	if (time < 0 || time >= info->nframes) {
		goto failed;
	}

	if (info->event_count > 0 && time < event_buffer[info->event_count - 1].time) {
		goto failed;
	}

	/* Check if data_size is >0 and there is enough space in the buffer for the event. */
	if (data_size <= 0) {
		goto failed; // return NULL?
	} else if (jack_midi_max_event_size (port_buffer) < data_size) {
		goto failed;
	} else {
		jack_midi_port_internal_event_t *event = &event_buffer[info->event_count];

		event->time = time;
		event->size = data_size;
		if (data_size <= MIDI_INLINE_MAX) {
			retbuf = event->inline_data;
		} else {
			info->last_write_loc += data_size;
			retbuf = &retbuf[buffer_size - 1 - info->last_write_loc];
			event->byte_offset =
				buffer_size - 1 - info->last_write_loc;
		}
		info->event_count += 1;
		return retbuf;
	}
failed:
	info->events_lost++;
	return NULL;
}


int
jack_midi_event_write (void                   *port_buffer,
		       jack_nframes_t time,
		       const jack_midi_data_t *data,
		       size_t data_size)
{
	jack_midi_data_t *retbuf =
		jack_midi_event_reserve (port_buffer, time, data_size);

	if (retbuf) {
		memcpy (retbuf, data, data_size);
		return 0;
	} else {
		return ENOBUFS;
	}
}


/* Can't check to make sure this port is an output anymore.  If this gets
 * called on an input port, all clients after the client that calls it
 * will think there are no events in the buffer as the event count has
 * been reset.
 */
void
jack_midi_clear_buffer (void           *port_buffer)
{
	jack_midi_port_info_private_t *info =
		(jack_midi_port_info_private_t*)port_buffer;

	info->event_count = 0;
	info->last_write_loc = 0;
	info->events_lost = 0;
}


/* jack_midi_port_functions.mixdown */
static void
jack_midi_port_mixdown (jack_port_t    *port, jack_nframes_t nframes)
{
	JSList         *node;
	jack_port_t    *input;
	jack_nframes_t num_events = 0;
	jack_nframes_t i          = 0;
	int err        = 0;
	jack_nframes_t lost_events = 0;

	/* The next (single) event to mix in to the buffer */
	jack_midi_port_info_private_t   *earliest_info;
	jack_midi_port_internal_event_t *earliest_event;
	jack_midi_data_t                *earliest_buffer;

	jack_midi_port_info_private_t   *in_info;       /* For finding next event */
	jack_midi_port_internal_event_t *in_events;     /* Corresponds to in_info */
	jack_midi_port_info_private_t   *out_info;      /* Output 'buffer' */

	jack_midi_clear_buffer (port->mix_buffer);

	out_info = (jack_midi_port_info_private_t*)port->mix_buffer;

	/* This function uses jack_midi_port_info_private_t.last_write_loc of the
	 * source ports to store indices of the last event read from that buffer
	 * so far.  This is OK because last_write_loc is used when writing events
	 * to a buffer, which at this stage is already complete so the value
	 * can be safely smashed. */

	/* Iterate through all connections to see how many events we need to mix,
	 * and initialise their 'last event read' (last_write_loc) to 0 */
	for (node = port->connections; node; node = jack_slist_next (node)) {
		input = (jack_port_t*)node->data;
		in_info =
			(jack_midi_port_info_private_t*)jack_output_port_buffer (input);
		num_events += in_info->event_count;
		lost_events += in_info->events_lost;
		in_info->last_write_loc = 0;
	}

	/* Write the events in the order of their timestamps */
	for (i = 0; i < num_events; ++i) {
		earliest_info = NULL;
		earliest_event = NULL;
		earliest_buffer = NULL;

		/* Find the earliest unread event, to mix next
		 * (search for an event earlier than earliest_event) */
		for (node = port->connections; node; node = jack_slist_next (node)) {
			in_info = (jack_midi_port_info_private_t*)
				  jack_output_port_buffer (((jack_port_t*)node->data));
			in_events = (jack_midi_port_internal_event_t*)(in_info + 1);

			/* If there are unread events left in this port.. */
			if (in_info->event_count > in_info->last_write_loc) {
				/* .. and this event is the new earliest .. */
				/* NOTE: that's why we compare time with <, not <= */
				if (earliest_info == NULL
				    || in_events[in_info->last_write_loc].time
				    < earliest_event->time) {
					/* .. then set this event as the next earliest */
					earliest_info = in_info;
					earliest_event = (jack_midi_port_internal_event_t*)
							 (&in_events[in_info->last_write_loc]);
				}
			}
		}

		if (earliest_info && earliest_event) {
			earliest_buffer = (jack_midi_data_t*)earliest_info;

			/* Write event to output */
			err = jack_midi_event_write (
				jack_port_buffer (port),
				earliest_event->time,
				jack_midi_event_data (earliest_buffer, earliest_event),
				earliest_event->size);

			earliest_info->last_write_loc++;

			if (err) {
				out_info->events_lost = num_events - i;
				break;
			}
		}
	}
	assert (out_info->event_count == num_events - out_info->events_lost);

	// inherit total lost events count from all connected ports.
	out_info->events_lost += lost_events;
}


uint32_t
jack_midi_get_lost_event_count (void           *port_buffer)
{
	return ((jack_midi_port_info_private_t*)port_buffer)->events_lost;
}

jack_port_functions_t jack_builtin_midi_functions = {
	.buffer_init	= jack_midi_buffer_init,
	.mixdown	= jack_midi_port_mixdown,
};