alsa_seqmidi.c 23.8 KB
Newer Older
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
/*
 * ALSA SEQ < - > JACK MIDI bridge
 *
 * Copyright (c) 2006,2007 Dmitry S. Baikov <c0ff@konstruktiv.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., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 */

/*
 * alsa_seqmidi_read:
 *	add new ports
 * 	reads queued snd_seq_event's
 * 	if PORT_EXIT: mark port as dead
 * 	if PORT_ADD, PORT_CHANGE: send addr to port_thread (it also may mark port as dead)
 * 	else process input event
 * 	remove dead ports and send them to port_thread
 *
 * alsa_seqmidi_write:
 * 	remove dead ports and send them to port_thread
 * 	add new ports
 * 	queue output events
 *
 * port_thread:
 * 	wait for port_sem
 * 	free deleted ports
 * 	create new ports or mark existing as dead
 */
nedko's avatar
nedko committed
40

41
#include <alsa/asoundlib.h>
42

43
44
45
46
47
48
49
50
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <semaphore.h>
#include <pthread.h>
#include <time.h>
#include <ctype.h>

51
52
#include "midiport.h"
#include "ringbuffer.h"
53
#include "alsa_midi_impl.h"
sletz's avatar
sletz committed
54
#include "JackError.h"
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

#define NSEC_PER_SEC ((int64_t)1000*1000*1000)

enum {
	MAX_PORTS = 64,
	MAX_EVENT_SIZE = 1024,
};

typedef struct port_t port_t;

enum {
	PORT_HASH_BITS = 4,
	PORT_HASH_SIZE = 1 << PORT_HASH_BITS
};

typedef port_t* port_hash_t[PORT_HASH_SIZE];

struct port_t {
	port_t *next;
	int is_dead;
	char name[64];
	snd_seq_addr_t remote;
	jack_port_t *jack_port;

	jack_ringbuffer_t *early_events; // alsa_midi_event_t + data
	int64_t last_out_time;

	void *jack_buf;
};

typedef struct {
	snd_midi_event_t *codec;

	jack_ringbuffer_t *new_ports;

	port_t *ports[MAX_PORTS];
} stream_t;

typedef struct alsa_seqmidi {
	alsa_midi_t ops;
	jack_client_t *jack;

	snd_seq_t *seq;
	int client_id;
	int port_id;
	int queue;

	int keep_walking;

	pthread_t port_thread;
	sem_t port_sem;
	jack_ringbuffer_t *port_add; // snd_seq_addr_t
	jack_ringbuffer_t *port_del; // port_t*

	stream_t stream[2];

	char alsa_name[32];
112
113
	int midi_in_cnt;
	int midi_out_cnt;
114
115
116
117
118
119
120
121
122
123
} alsa_seqmidi_t;

struct alsa_midi_event {
	int64_t time;
	int size;
};
typedef struct alsa_midi_event alsa_midi_event_t;

struct process_info {
	int dir;
124
125
126
127
	jack_nframes_t nframes;
	jack_nframes_t period_start;
	jack_nframes_t sample_rate;
	jack_nframes_t cur_frames;
128
129
130
131
132
133
134
135
136
137
138
139
	int64_t alsa_time;
};

enum PortType { PORT_INPUT = 0, PORT_OUTPUT = 1 };

typedef void (*port_jack_func)(alsa_seqmidi_t *self, port_t *port,struct process_info* info);
static void do_jack_input(alsa_seqmidi_t *self, port_t *port, struct process_info* info);
static void do_jack_output(alsa_seqmidi_t *self, port_t *port, struct process_info* info);

typedef struct {
	int alsa_mask;
	int jack_caps;
140
	char name[9];
141
142
143
144
145
146
	port_jack_func jack_func;
} port_type_t;

static port_type_t port_type[2] = {
	{
		SND_SEQ_PORT_CAP_SUBS_READ,
147
148
		JackPortIsOutput,
		"playback",
149
150
151
152
		do_jack_input
	},
	{
		SND_SEQ_PORT_CAP_SUBS_WRITE,
153
154
		JackPortIsInput,
		"capture",
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
		do_jack_output
	}
};

static void alsa_seqmidi_delete(alsa_midi_t *m);
static int alsa_seqmidi_attach(alsa_midi_t *m);
static int alsa_seqmidi_detach(alsa_midi_t *m);
static int alsa_seqmidi_start(alsa_midi_t *m);
static int alsa_seqmidi_stop(alsa_midi_t *m);
static void alsa_seqmidi_read(alsa_midi_t *m, jack_nframes_t nframes);
static void alsa_seqmidi_write(alsa_midi_t *m, jack_nframes_t nframes);

static
void stream_init(alsa_seqmidi_t *self, int dir)
{
	stream_t *str = &self->stream[dir];

	str->new_ports = jack_ringbuffer_create(MAX_PORTS*sizeof(port_t*));
	snd_midi_event_new(MAX_EVENT_SIZE, &str->codec);
}

static void port_free(alsa_seqmidi_t *self, port_t *port);
static void free_ports(alsa_seqmidi_t *self, jack_ringbuffer_t *ports);

static
void stream_attach(alsa_seqmidi_t *self, int dir)
{
}

static
void stream_detach(alsa_seqmidi_t *self, int dir)
{
	stream_t *str = &self->stream[dir];
	int i;

	free_ports(self, str->new_ports);

	// delete all ports from hash
	for (i=0; i<PORT_HASH_SIZE; ++i) {
		port_t *port = str->ports[i];
		while (port) {
			port_t *next = port->next;
			port_free(self, port);
			port = next;
		}
		str->ports[i] = NULL;
	}
}

static
void stream_close(alsa_seqmidi_t *self, int dir)
{
	stream_t *str = &self->stream[dir];

	if (str->codec)
		snd_midi_event_free(str->codec);
	if (str->new_ports)
		jack_ringbuffer_free(str->new_ports);
}

alsa_midi_t* alsa_seqmidi_new(jack_client_t *client, const char* alsa_name)
{
	alsa_seqmidi_t *self = calloc(1, sizeof(alsa_seqmidi_t));
sletz's avatar
Cleanup    
sletz committed
218
	debug_log("midi: new");
219
220
221
222
223
224
225
226
227
228
229
230
231
232
	if (!self)
		return NULL;
	self->jack = client;
	if (!alsa_name)
		alsa_name = "jack_midi";
	snprintf(self->alsa_name, sizeof(self->alsa_name), "%s", alsa_name);

	self->port_add = jack_ringbuffer_create(2*MAX_PORTS*sizeof(snd_seq_addr_t));
	self->port_del = jack_ringbuffer_create(2*MAX_PORTS*sizeof(port_t*));
	sem_init(&self->port_sem, 0, 0);

	stream_init(self, PORT_INPUT);
	stream_init(self, PORT_OUTPUT);

233
234
	self->midi_in_cnt = 0;
	self->midi_out_cnt = 0;
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
	self->ops.destroy = alsa_seqmidi_delete;
	self->ops.attach = alsa_seqmidi_attach;
	self->ops.detach = alsa_seqmidi_detach;
	self->ops.start = alsa_seqmidi_start;
	self->ops.stop = alsa_seqmidi_stop;
	self->ops.read = alsa_seqmidi_read;
	self->ops.write = alsa_seqmidi_write;
	return &self->ops;
}

static
void alsa_seqmidi_delete(alsa_midi_t *m)
{
	alsa_seqmidi_t *self = (alsa_seqmidi_t*) m;

sletz's avatar
Cleanup    
sletz committed
250
	debug_log("midi: delete");
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
	alsa_seqmidi_detach(m);

	stream_close(self, PORT_OUTPUT);
	stream_close(self, PORT_INPUT);

	jack_ringbuffer_free(self->port_add);
	jack_ringbuffer_free(self->port_del);
	sem_close(&self->port_sem);

	free(self);
}

static
int alsa_seqmidi_attach(alsa_midi_t *m)
{
	alsa_seqmidi_t *self = (alsa_seqmidi_t*) m;
	int err;

sletz's avatar
Cleanup    
sletz committed
269
	debug_log("midi: attach");
270
271
272
273
274
275
276
277
278
279
280

	if (self->seq)
		return -EALREADY;

	if ((err = snd_seq_open(&self->seq, "hw", SND_SEQ_OPEN_DUPLEX, 0)) < 0) {
		error_log("failed to open alsa seq");
		return err;
	}
	snd_seq_set_client_name(self->seq, self->alsa_name);
	self->port_id = snd_seq_create_simple_port(self->seq, "port",
		SND_SEQ_PORT_CAP_READ|SND_SEQ_PORT_CAP_WRITE
281
#ifndef JACK_MIDI_DEBUG
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
		|SND_SEQ_PORT_CAP_NO_EXPORT
#endif
		,SND_SEQ_PORT_TYPE_APPLICATION);
	self->client_id = snd_seq_client_id(self->seq);

  	self->queue = snd_seq_alloc_queue(self->seq);
  	snd_seq_start_queue(self->seq, self->queue, 0); 

	stream_attach(self, PORT_INPUT);
	stream_attach(self, PORT_OUTPUT);

	snd_seq_nonblock(self->seq, 1);

	return 0;
}

static
int alsa_seqmidi_detach(alsa_midi_t *m)
{
	alsa_seqmidi_t *self = (alsa_seqmidi_t*) m;

sletz's avatar
Cleanup    
sletz committed
303
	debug_log("midi: detach");
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

	if (!self->seq)
		return -EALREADY;

	alsa_seqmidi_stop(m);

	jack_ringbuffer_reset(self->port_add);
	free_ports(self, self->port_del);

	stream_detach(self, PORT_INPUT);
	stream_detach(self, PORT_OUTPUT);

	snd_seq_close(self->seq);
	self->seq = NULL;

	return 0;
}

static void* port_thread(void *);

static void add_existing_ports(alsa_seqmidi_t *self);
static void update_ports(alsa_seqmidi_t *self);
static void add_ports(stream_t *str);

static
int alsa_seqmidi_start(alsa_midi_t *m)
{
	alsa_seqmidi_t *self = (alsa_seqmidi_t*) m;
	int err;

sletz's avatar
Cleanup    
sletz committed
334
	debug_log("midi: start");
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
361
362
363
364

	if (!self->seq)
		return -EBADF;

	if (self->keep_walking)
		return -EALREADY;

	snd_seq_connect_from(self->seq, self->port_id, SND_SEQ_CLIENT_SYSTEM, SND_SEQ_PORT_SYSTEM_ANNOUNCE);
	snd_seq_drop_input(self->seq);

	add_existing_ports(self);
	update_ports(self);
	add_ports(&self->stream[PORT_INPUT]);
	add_ports(&self->stream[PORT_OUTPUT]);

	self->keep_walking = 1;

	if ((err = pthread_create(&self->port_thread, NULL, port_thread, self))) {
		self->keep_walking = 0;
		return -errno;
	}

	return 0;
}

static
int alsa_seqmidi_stop(alsa_midi_t *m)
{
	alsa_seqmidi_t *self = (alsa_seqmidi_t*) m;

sletz's avatar
Cleanup    
sletz committed
365
	debug_log("midi: stop");
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400

	if (!self->keep_walking)
		return -EALREADY;

	snd_seq_disconnect_from(self->seq, self->port_id, SND_SEQ_CLIENT_SYSTEM, SND_SEQ_PORT_SYSTEM_ANNOUNCE);

	self->keep_walking = 0;

	sem_post(&self->port_sem);
	pthread_join(self->port_thread, NULL);
	self->port_thread = 0;

	return 0;
}

static
int alsa_connect_from(alsa_seqmidi_t *self, int client, int port)
{
	snd_seq_port_subscribe_t* sub;
	snd_seq_addr_t seq_addr;
	int err;

	snd_seq_port_subscribe_alloca(&sub);
	seq_addr.client = client;
	seq_addr.port = port;
	snd_seq_port_subscribe_set_sender(sub, &seq_addr);
	seq_addr.client = self->client_id;
	seq_addr.port = self->port_id;
	snd_seq_port_subscribe_set_dest(sub, &seq_addr);

	snd_seq_port_subscribe_set_time_update(sub, 1);
	snd_seq_port_subscribe_set_queue(sub, self->queue);
	snd_seq_port_subscribe_set_time_real(sub, 1);

	if ((err=snd_seq_subscribe_port(self->seq, sub)))
sletz's avatar
Cleanup    
sletz committed
401
		error_log("can't subscribe to %d:%d - %s", client, port, snd_strerror(err));
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
	return err;
}

/*
 * ==================== Port routines =============================
 */
static inline
int port_hash(snd_seq_addr_t addr)
{
	return (addr.client + addr.port) % PORT_HASH_SIZE;
}

static
port_t* port_get(port_hash_t hash, snd_seq_addr_t addr)
{
	port_t **pport = &hash[port_hash(addr)];
	while (*pport) {
		port_t *port = *pport;
		if (port->remote.client == addr.client && port->remote.port == addr.port)
			return port;
		pport = &port->next;
	}
	return NULL;
}

static
void port_insert(port_hash_t hash, port_t *port)
{
	port_t **pport = &hash[port_hash(port->remote)];
	port->next = *pport;
	*pport = port;
}

static
void port_setdead(port_hash_t hash, snd_seq_addr_t addr)
{
	port_t *port = port_get(hash, addr);
	if (port)
		port->is_dead = 1; // see jack_process
	else
sletz's avatar
Cleanup    
sletz committed
442
		debug_log("port_setdead: not found (%d:%d)", addr.client, addr.port);
443
444
445
446
447
448
449
450
451
452
453
}

static
void port_free(alsa_seqmidi_t *self, port_t *port)
{
	//snd_seq_disconnect_from(self->seq, self->port_id, port->remote.client, port->remote.port);
	//snd_seq_disconnect_to(self->seq, self->port_id, port->remote.client, port->remote.port);
	if (port->early_events)
		jack_ringbuffer_free(port->early_events);
	if (port->jack_port)
		jack_port_unregister(self->jack, port->jack_port);
sletz's avatar
Cleanup    
sletz committed
454
	info_log("port deleted: %s", port->name);
455
456
457
458
459
460
461

	free(port);
}

static
port_t* port_create(alsa_seqmidi_t *self, int type, snd_seq_addr_t addr, const snd_seq_port_info_t *info)
{
462
	snd_seq_client_info_t* client_info;
463
464
465
	port_t *port;
	char *c;
	int err;
466
	int jack_caps;
sletz's avatar
sletz committed
467
        char name[128];
468
469
470
471
472
473

	port = calloc(1, sizeof(port_t));
	if (!port)
		return NULL;

	port->remote = addr;
474
475
476
477

	snd_seq_client_info_alloca (&client_info);
	snd_seq_get_any_client_info (self->seq, addr.client, client_info);

sletz's avatar
sletz committed
478
	snprintf(port->name, sizeof(port->name), "alsa_pcm:%s/midi_%s_%d",
479
480
		 snd_seq_client_info_get_name(client_info), port_type[type].name, addr.port+1);

481
482
	// replace all offending characters by -
	for (c = port->name; *c; ++c)
483
		if (!isalnum(*c) && *c != '/' && *c != '_' && *c != ':' && *c != '(' && *c != ')')
484
485
			*c = '-';

486
	jack_caps = port_type[type].jack_caps;
487

488
489
490
	/* mark anything that looks like a hardware port as physical&terminal */

	if (snd_seq_port_info_get_type (info) & (SND_SEQ_PORT_TYPE_HARDWARE|SND_SEQ_PORT_TYPE_PORT|SND_SEQ_PORT_TYPE_SPECIFIC)) {
491
		jack_caps |= (JackPortIsPhysical | JackPortIsTerminal | JackPortIsActive);
492
	}
sletz's avatar
sletz committed
493
494
495
496
497

	if (jack_caps & JackPortIsOutput)
		snprintf(name, sizeof(name) - 1, "system:midi_capture_%d", ++self->midi_in_cnt);
	else 
		snprintf(name, sizeof(name) - 1, "system:midi_playback_%d", ++self->midi_out_cnt);
498
499
			
	port->jack_port = jack_port_register(self->jack,
sletz's avatar
sletz committed
500
		name, JACK_DEFAULT_MIDI_TYPE, jack_caps, 0);
501
502
	if (!port->jack_port)
		goto failed;
503

sletz's avatar
sletz committed
504
505
	jack_port_set_alias (port->jack_port, port->name);

506
507
508
509
510
511
512
513
514
515
516
	/* generate an alias */

	snprintf(port->name, sizeof(port->name), "%s:midi/%s_%d",
		 snd_seq_client_info_get_name (client_info), port_type[type].name, addr.port+1);

	// replace all offending characters by -
	for (c = port->name; *c; ++c)
		if (!isalnum(*c) && *c != '/' && *c != '_' && *c != ':' && *c != '(' && *c != ')')
			*c = '-';

	jack_port_set_alias (port->jack_port, port->name);
517
518
519
520
521
522
523
524
525
526

	if (type == PORT_INPUT)
		err = alsa_connect_from(self, port->remote.client, port->remote.port);
	else
		err = snd_seq_connect_to(self->seq, self->port_id, port->remote.client, port->remote.port);
	if (err)
		goto failed;

	port->early_events = jack_ringbuffer_create(MAX_EVENT_SIZE*16);

sletz's avatar
Cleanup    
sletz committed
527
	info_log("port created: %s", port->name);
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
	return port;

 failed:
 	port_free(self, port);
	return NULL;
}

/*
 * ==================== Port add/del handling thread ==============================
 */
static
void update_port_type(alsa_seqmidi_t *self, int type, snd_seq_addr_t addr, int caps, const snd_seq_port_info_t *info)
{
	stream_t *str = &self->stream[type];
	int alsa_mask = port_type[type].alsa_mask;
	port_t *port = port_get(str->ports, addr);

sletz's avatar
Cleanup    
sletz committed
545
	debug_log("update_port_type(%d:%d)", addr.client, addr.port);
546
547

	if (port && (caps & alsa_mask)!=alsa_mask) {
sletz's avatar
Cleanup    
sletz committed
548
		debug_log("setdead: %s", port->name);
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
		port->is_dead = 1;
	}

	if (!port && (caps & alsa_mask)==alsa_mask) {
		assert (jack_ringbuffer_write_space(str->new_ports) >= sizeof(port));
		port = port_create(self, type, addr, info);
		if (port)
			jack_ringbuffer_write(str->new_ports, (char*)&port, sizeof(port));
	}
}

static
void update_port(alsa_seqmidi_t *self, snd_seq_addr_t addr, const snd_seq_port_info_t *info)
{
	unsigned int port_caps = snd_seq_port_info_get_capability(info);
	if (port_caps & SND_SEQ_PORT_CAP_NO_EXPORT)
		return;
	update_port_type(self, PORT_INPUT, addr, port_caps, info);
567
	update_port_type(self, PORT_OUTPUT,addr, port_caps, info);
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
}

static
void free_ports(alsa_seqmidi_t *self, jack_ringbuffer_t *ports)
{
	port_t *port;
	int sz;
	while ((sz = jack_ringbuffer_read(ports, (char*)&port, sizeof(port)))) {
		assert (sz == sizeof(port));
		port_free(self, port);
	}
}

static
void update_ports(alsa_seqmidi_t *self)
{
	snd_seq_addr_t addr;
585
	snd_seq_port_info_t *info;
586
587
	int size;

588
589
	snd_seq_port_info_alloca(&info);

590
	while ((size = jack_ringbuffer_read(self->port_add, (char*)&addr, sizeof(addr)))) {
591
		
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
		int err;

		assert (size == sizeof(addr));
		assert (addr.client != self->client_id);
		if ((err=snd_seq_get_any_port_info(self->seq, addr.client, addr.port, info))>=0) {
			update_port(self, addr, info);
		} else {
			//port_setdead(self->stream[PORT_INPUT].ports, addr);
			//port_setdead(self->stream[PORT_OUTPUT].ports, addr);
		}
	}
}

static
void* port_thread(void *arg)
{
	alsa_seqmidi_t *self = arg;

	while (self->keep_walking) {
		sem_wait(&self->port_sem);
		free_ports(self, self->port_del);
		update_ports(self);
	}
sletz's avatar
Cleanup    
sletz committed
615
	debug_log("port_thread exited");
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
	return NULL;
}

static
void add_existing_ports(alsa_seqmidi_t *self)
{
	snd_seq_addr_t addr;
	snd_seq_client_info_t *client_info;
	snd_seq_port_info_t *port_info;

	snd_seq_client_info_alloca(&client_info);
	snd_seq_port_info_alloca(&port_info);
	snd_seq_client_info_set_client(client_info, -1);
	while (snd_seq_query_next_client(self->seq, client_info) >= 0)
	{
		addr.client = snd_seq_client_info_get_client(client_info);
		if (addr.client == SND_SEQ_CLIENT_SYSTEM || addr.client == self->client_id)
			continue;
		snd_seq_port_info_set_client(port_info, addr.client);
		snd_seq_port_info_set_port(port_info, -1);
		while (snd_seq_query_next_port(self->seq, port_info) >= 0)
		{
			addr.port = snd_seq_port_info_get_port(port_info);
			update_port(self, addr, port_info);
		}
	}
}

/*
 * =================== Input/output port handling =========================
 */
static
void set_process_info(struct process_info *info, alsa_seqmidi_t *self, int dir, jack_nframes_t nframes)
{
	const snd_seq_real_time_t* alsa_time;
	snd_seq_queue_status_t *status;

	snd_seq_queue_status_alloca(&status);

	info->dir = dir;

	info->period_start = jack_last_frame_time(self->jack);
	info->nframes = nframes;
	info->sample_rate = jack_get_sample_rate(self->jack);

	info->cur_frames = jack_frame_time(self->jack);

	// immediately get alsa'a real time (uhh, why everybody has their own 'real' time)
	snd_seq_get_queue_status(self->seq, self->queue, status);
	alsa_time = snd_seq_queue_status_get_real_time(status);
	info->alsa_time = alsa_time->tv_sec * NSEC_PER_SEC + alsa_time->tv_nsec;
667
668
669
670
671
672

	if (info->period_start + info->nframes < info->cur_frames) {
		int periods_lost = (info->cur_frames - info->period_start) / info->nframes; 
		info->period_start += periods_lost * info->nframes;
		debug_log("xrun detected: %d periods lost\n", periods_lost);
	}
673
674
675
676
677
678
679
}

static
void add_ports(stream_t *str)
{
	port_t *port;
	while (jack_ringbuffer_read(str->new_ports, (char*)&port, sizeof(port))) {
680
		debug_log("jack: inserted port %s\n", port->name);
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
		port_insert(str->ports, port);
	}
}

static
void jack_process(alsa_seqmidi_t *self, struct process_info *info)
{
	stream_t *str = &self->stream[info->dir];
	port_jack_func process = port_type[info->dir].jack_func;
	int i, del=0;

	add_ports(str);

	// process ports
	for (i=0; i<PORT_HASH_SIZE; ++i) {
		port_t **pport = &str->ports[i];
		while (*pport) {
			port_t *port = *pport;
			port->jack_buf = jack_port_get_buffer(port->jack_port, info->nframes);
			if (info->dir == PORT_INPUT)
701
				jack_midi_clear_buffer(port->jack_buf);
702
703
704
705

			if (!port->is_dead)
				(*process)(self, port, info);
			else if (jack_ringbuffer_write_space(self->port_del) >= sizeof(port)) {
sletz's avatar
Cleanup    
sletz committed
706
				debug_log("jack: removed port %s", port->name);
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
				*pport = port->next;
				jack_ringbuffer_write(self->port_del, (char*)&port, sizeof(port));
				del++;
				continue;
			}

			pport = &port->next;
		}
	}

	if (del)
		sem_post(&self->port_sem);
}

/*
 * ============================ Input ==============================
 */
static
void do_jack_input(alsa_seqmidi_t *self, port_t *port, struct process_info *info)
{
	// process port->early_events
	alsa_midi_event_t ev;
	while (jack_ringbuffer_read(port->early_events, (char*)&ev, sizeof(ev))) {
		jack_midi_data_t* buf;
731
		jack_nframes_t time = ev.time - info->period_start;
732
733
734
735
		if (time < 0)
			time = 0;
		else if (time >= info->nframes)
			time = info->nframes - 1;
736
		buf = jack_midi_event_reserve(port->jack_buf, (jack_nframes_t)time, ev.size);
737
738
739
740
		if (buf)
			jack_ringbuffer_read(port->early_events, (char*)buf, ev.size);
		else
			jack_ringbuffer_read_advance(port->early_events, ev.size);
sletz's avatar
Cleanup    
sletz committed
741
		debug_log("input: it's time for %d bytes at %lld", ev.size, time);
742
743
744
745
746
747
748
749
750
751
752
753
754
755
	}
}

static
void port_event(alsa_seqmidi_t *self, snd_seq_event_t *ev)
{
	const snd_seq_addr_t addr = ev->data.addr;

	if (addr.client == self->client_id)
		return;

	if (ev->type == SND_SEQ_EVENT_PORT_START || ev->type == SND_SEQ_EVENT_PORT_CHANGE) {
		assert (jack_ringbuffer_write_space(self->port_add) >= sizeof(addr));

sletz's avatar
Cleanup    
sletz committed
756
		debug_log("port_event: add/change %d:%d", addr.client, addr.port);
757
758
759
		jack_ringbuffer_write(self->port_add, (char*)&addr, sizeof(addr));
		sem_post(&self->port_sem);
	} else if (ev->type == SND_SEQ_EVENT_PORT_EXIT) {
sletz's avatar
Cleanup    
sletz committed
760
		debug_log("port_event: del %d:%d", addr.client, addr.port);
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
		port_setdead(self->stream[PORT_INPUT].ports, addr);
		port_setdead(self->stream[PORT_OUTPUT].ports, addr);
	}
}

static
void input_event(alsa_seqmidi_t *self, snd_seq_event_t *alsa_event, struct process_info* info)
{
	jack_midi_data_t data[MAX_EVENT_SIZE];
	stream_t *str = &self->stream[PORT_INPUT];
	long size;
	int64_t alsa_time, time_offset;
	int64_t frame_offset, event_frame;
	port_t *port;

	port = port_get(str->ports, alsa_event->source);
	if (!port)
		return;

	/*
	 * RPNs, NRPNs, Bank Change, etc. need special handling
	 * but seems, ALSA does it for us already.
	 */
	snd_midi_event_reset_decode(str->codec);
	if ((size = snd_midi_event_decode(str->codec, data, sizeof(data), alsa_event))<0)
		return;

	// fixup NoteOn with vel 0
789
790
	if ((data[0] & 0xF0) == 0x90 && data[2] == 0x00) {
		data[0] = 0x80 + (data[0] & 0x0F);
791
792
793
794
795
796
		data[2] = 0x40;
	}

	alsa_time = alsa_event->time.time.tv_sec * NSEC_PER_SEC + alsa_event->time.time.tv_nsec;
	time_offset = info->alsa_time - alsa_time;
	frame_offset = (info->sample_rate * time_offset) / NSEC_PER_SEC;
797
	event_frame = (int64_t)info->cur_frames - info->period_start - frame_offset + info->nframes;
798

sletz's avatar
Cleanup    
sletz committed
799
	debug_log("input: %d bytes at event_frame = %d", (int)size, (int)event_frame);
800
801
802
803
804
805
806
807

	if (event_frame >= info->nframes &&
	    jack_ringbuffer_write_space(port->early_events) >= (sizeof(alsa_midi_event_t) + size)) {
		alsa_midi_event_t ev;
		ev.time = event_frame + info->period_start;
		ev.size = size;
		jack_ringbuffer_write(port->early_events, (char*)&ev, sizeof(ev));
		jack_ringbuffer_write(port->early_events, (char*)data, size);
sletz's avatar
Cleanup    
sletz committed
808
		debug_log("postponed to next frame +%d", (int) (event_frame - info->nframes)); 
809
810
811
812
813
814
815
816
		return;
	}

	if (event_frame < 0)
		event_frame = 0;
	else if (event_frame >= info->nframes)
		event_frame = info->nframes - 1;

817
	jack_midi_event_write(port->jack_buf, event_frame, data, size);
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
}

static
void alsa_seqmidi_read(alsa_midi_t *m, jack_nframes_t nframes)
{
	alsa_seqmidi_t *self = (alsa_seqmidi_t*) m;
	int res;
	snd_seq_event_t *event;
	struct process_info info;

	if (!self->keep_walking)
		return;

	set_process_info(&info, self, PORT_INPUT, nframes);
	jack_process(self, &info); 

	while ((res = snd_seq_event_input(self->seq, &event))>0) {
		if (event->source.client == SND_SEQ_CLIENT_SYSTEM)
			port_event(self, event);
		else
			input_event(self, event, &info);
	}
}

/*
 * ============================ Output ==============================
 */

static
void do_jack_output(alsa_seqmidi_t *self, port_t *port, struct process_info* info)
{
	stream_t *str = &self->stream[info->dir];
850
	int nevents = jack_midi_get_event_count(port->jack_buf);
851
852
853
854
855
856
857
858
859
	int i;
	for (i=0; i<nevents; ++i) {
		jack_midi_event_t jack_event;
		snd_seq_event_t alsa_event;
		int64_t frame_offset;
		int64_t out_time;
		snd_seq_real_time_t out_rt;
		int err;

860
		jack_midi_event_get(&jack_event, port->jack_buf, i);
861
862
863
864
865
866
867
868
869

		snd_seq_ev_clear(&alsa_event);
		snd_midi_event_reset_encode(str->codec);
		if (!snd_midi_event_encode(str->codec, jack_event.buffer, jack_event.size, &alsa_event))
			continue; // invalid event

		snd_seq_ev_set_source(&alsa_event, self->port_id);
		snd_seq_ev_set_dest(&alsa_event, port->remote.client, port->remote.port);

870
871
872
873
874
875
876
877
878
879
880
881
		/* NOTE: in case of xrun it could become negative, so it is essential to use signed type! */
		frame_offset = (int64_t)jack_event.time + info->period_start + info->nframes - info->cur_frames;
		if (frame_offset < 0) {
			frame_offset = info->nframes + jack_event.time;
			error_log("internal xrun detected: frame_offset = %"PRId64"\n", frame_offset);
		}
		/* Ken Ellinwood reported problems with this assert.
		 * Seems, magic 2 should be replaced with nperiods. */
		//FIXME: assert (frame_offset < info->nframes*2);
		//if (frame_offset < info->nframes * info->nperiods)
		//        debug_log("alsa_out: BLAH-BLAH-BLAH");

882
883
		out_time = info->alsa_time + (frame_offset * NSEC_PER_SEC) / info->sample_rate;

sletz's avatar
Cleanup    
sletz committed
884
		debug_log("alsa_out: frame_offset = %lld, info->alsa_time = %lld, out_time = %lld, port->last_out_time = %lld",
885
886
887
			frame_offset, info->alsa_time, out_time, port->last_out_time);

		// we should use absolute time to prevent reordering caused by rounding errors
888
889
		if (out_time < port->last_out_time) {
			debug_log("alsa_out: limiting out_time %lld at %lld", out_time, port->last_out_time);
890
			out_time = port->last_out_time;
891
		} else
892
893
894
895
896
897
898
			port->last_out_time = out_time;

		out_rt.tv_nsec = out_time % NSEC_PER_SEC;
		out_rt.tv_sec = out_time / NSEC_PER_SEC;
		snd_seq_ev_schedule_real(&alsa_event, self->queue, 0, &out_rt);

		err = snd_seq_event_output(self->seq, &alsa_event);
sletz's avatar
Cleanup    
sletz committed
899
		debug_log("alsa_out: written %d bytes to %s at %+d (%lld): %d", (int)jack_event.size, port->name, (int)frame_offset, out_time, err);
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
	}
}

static
void alsa_seqmidi_write(alsa_midi_t *m, jack_nframes_t nframes)
{
	alsa_seqmidi_t *self = (alsa_seqmidi_t*) m;
	struct process_info info;

	if (!self->keep_walking)
		return;

	set_process_info(&info, self, PORT_OUTPUT, nframes);
	jack_process(self, &info);
	snd_seq_drain_output(self->seq);
}