1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
3 * ALSA SEQ < - > JACK MIDI bridge
5 * Copyright (c) 2006,2007 Dmitry S. Baikov <c0ff@konstruktiv.org>
6 * Copyright (c) 2007,2008 Nedko Arnaudov <nedko@arnaudov.name>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <semaphore.h>
25 #include <alsa/asoundlib.h>
26 #include <jack/jack.h>
27 #include <jack/ringbuffer.h>
31 #include "port_hash.h"
35 /* This should be part of JACK API */
36 #define JACK_IS_VALID_PORT_NAME_CHAR(c) \
49 a2j_alsa_connect_from(
54 snd_seq_port_subscribe_t
* sub
;
55 snd_seq_addr_t seq_addr
;
58 snd_seq_port_subscribe_alloca(&sub
);
59 seq_addr
.client
= client
;
61 snd_seq_port_subscribe_set_sender(sub
, &seq_addr
);
62 seq_addr
.client
= self
->client_id
;
63 seq_addr
.port
= self
->port_id
;
64 snd_seq_port_subscribe_set_dest(sub
, &seq_addr
);
66 snd_seq_port_subscribe_set_time_update(sub
, 1);
67 snd_seq_port_subscribe_set_queue(sub
, self
->queue
);
68 snd_seq_port_subscribe_set_time_real(sub
, 1);
70 if ((err
=snd_seq_subscribe_port(self
->seq
, sub
)))
71 a2j_error("can't subscribe to %d:%d - %s", client
, port
, snd_strerror(err
));
80 struct a2j_port
*port
= a2j_port_get(hash
, addr
);
82 port
->is_dead
= true; // see jack_process_internal
84 a2j_debug("port_setdead: not found (%d:%d)", addr
.client
, addr
.port
);
89 struct a2j_port
* port
)
91 //snd_seq_disconnect_from(self->seq, self->port_id, port->remote.client, port->remote.port);
92 //snd_seq_disconnect_to(self->seq, self->port_id, port->remote.client, port->remote.port);
93 if (port
->early_events
)
94 jack_ringbuffer_free(port
->early_events
);
95 if (port
->jack_port
!= JACK_INVALID_PORT
)
96 jack_port_unregister(port
->a2j_ptr
->jack_client
, port
->jack_port
);
103 struct a2j_port
* port_ptr
,
105 snd_seq_client_info_t
* client_info_ptr
,
106 const snd_seq_port_info_t
* port_info_ptr
,
115 sizeof(port_ptr
->name
),
117 snd_seq_client_info_get_name(client_info_ptr
),
118 snd_seq_client_info_get_client(client_info_ptr
),
119 type
== A2J_PORT_CAPTURE
? "capture": "playback",
120 snd_seq_port_info_get_name(port_info_ptr
));
126 sizeof(port_ptr
->name
),
128 snd_seq_client_info_get_name(client_info_ptr
),
129 type
== A2J_PORT_CAPTURE
? "capture": "playback",
130 snd_seq_port_info_get_name(port_info_ptr
));
133 // replace all offending characters with ' '
134 for (c
= port_ptr
->name
; *c
; ++c
)
135 if (!JACK_IS_VALID_PORT_NAME_CHAR(*c
))
144 const snd_seq_port_info_t
* info
)
146 struct a2j_port
*port
;
149 snd_seq_client_info_t
* client_info_ptr
;
151 struct a2j_stream
* stream_ptr
;
153 stream_ptr
= &self
->stream
[type
];
155 err
= snd_seq_client_info_malloc(&client_info_ptr
);
158 a2j_error("Failed to allocate client info");
162 client
= snd_seq_port_info_get_client(info
);
164 err
= snd_seq_get_any_client_info(self
->seq
, client
, client_info_ptr
);
167 a2j_error("Failed to get client info");
168 goto fail_free_client_info
;
171 a2j_debug("client name: '%s'", snd_seq_client_info_get_name(client_info_ptr
));
172 a2j_debug("port name: '%s'", snd_seq_port_info_get_name(info
));
174 port
= calloc(1, sizeof(struct a2j_port
));
177 goto fail_free_client_info
;
180 port
->a2j_ptr
= self
;
182 port
->jack_port
= JACK_INVALID_PORT
;
185 a2j_port_fill_name(port
, type
, client_info_ptr
, info
, true);
187 /* Add port to list early, before registering to JACK, so map functionality is guaranteed to work during port registration */
188 list_add_tail(&port
->siblings
, &stream_ptr
->list
);
190 if (type
== A2J_PORT_CAPTURE
)
192 jack_caps
= JackPortIsOutput
;
196 jack_caps
= JackPortIsInput
;
199 /* mark anything that looks like a hardware port as physical&terminal */
200 if (snd_seq_port_info_get_type(info
) & (SND_SEQ_PORT_TYPE_HARDWARE
|SND_SEQ_PORT_TYPE_PORT
|SND_SEQ_PORT_TYPE_SPECIFIC
))
202 jack_caps
|= JackPortIsPhysical
|JackPortIsTerminal
;
205 port
->jack_port
= jack_port_register(self
->jack_client
, port
->name
, JACK_DEFAULT_MIDI_TYPE
, jack_caps
, 0);
206 if (port
->jack_port
== JACK_INVALID_PORT
)
208 a2j_error("jack_port_register() failed for '%s'", port
->name
);
212 if (type
== A2J_PORT_CAPTURE
)
213 err
= a2j_alsa_connect_from(self
, port
->remote
.client
, port
->remote
.port
);
215 err
= snd_seq_connect_to(self
->seq
, self
->port_id
, port
->remote
.client
, port
->remote
.port
);
218 a2j_info("port skipped: %s", port
->name
);
222 port
->early_events
= jack_ringbuffer_create(MAX_EVENT_SIZE
*16);
224 a2j_info("port created: %s", port
->name
);
228 list_del(&port
->siblings
);
232 fail_free_client_info
:
233 snd_seq_client_info_free(client_info_ptr
);