JACK tools
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

218 lines
6.1KB

  1. /*
  2. * ALSA SEQ < - > JACK MIDI bridge
  3. *
  4. * Copyright (c) 2006,2007 Dmitry S. Baikov <c0ff@konstruktiv.org>
  5. * Copyright (c) 2007,2008,2009 Nedko Arnaudov <nedko@arnaudov.name>
  6. * Copyright (c) 2009,2010 Paul Davis <paul@linuxaudiosystems.com>
  7. *
  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.
  11. *
  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.
  16. *
  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
  20. */
  21. #include <stdbool.h>
  22. #include <ctype.h>
  23. #include <semaphore.h>
  24. #include <alsa/asoundlib.h>
  25. #include <jack/jack.h>
  26. #include <jack/ringbuffer.h>
  27. #include "list.h"
  28. #include "a2j.h"
  29. #include "port_hash.h"
  30. #include "port.h"
  31. /* This should be part of JACK API */
  32. #define JACK_IS_VALID_PORT_NAME_CHAR(c) \
  33. (isalnum(c) || \
  34. (c) == '/' || \
  35. (c) == '_' || \
  36. (c) == '(' || \
  37. (c) == ')' || \
  38. (c) == '-' || \
  39. (c) == '[' || \
  40. (c) == ']')
  41. static
  42. int
  43. a2j_alsa_connect_from (struct a2j * self, int client, int port)
  44. {
  45. snd_seq_port_subscribe_t* sub;
  46. snd_seq_addr_t seq_addr;
  47. int err;
  48. snd_seq_port_subscribe_alloca (&sub);
  49. seq_addr.client = client;
  50. seq_addr.port = port;
  51. snd_seq_port_subscribe_set_sender (sub, &seq_addr);
  52. seq_addr.client = self->client_id;
  53. seq_addr.port = self->port_id;
  54. snd_seq_port_subscribe_set_dest (sub, &seq_addr);
  55. snd_seq_port_subscribe_set_time_update (sub, 1);
  56. snd_seq_port_subscribe_set_queue (sub, self->queue);
  57. snd_seq_port_subscribe_set_time_real (sub, 1);
  58. if ((err = snd_seq_subscribe_port (self->seq, sub))) {
  59. a2j_error ("can't subscribe to %d:%d - %s", client, port, snd_strerror(err));
  60. }
  61. return err;
  62. }
  63. void
  64. a2j_port_setdead (a2j_port_hash_t hash, snd_seq_addr_t addr)
  65. {
  66. struct a2j_port *port = a2j_port_get(hash, addr);
  67. if (port) {
  68. port->is_dead = true; // see jack_process_internal
  69. } else {
  70. a2j_debug("port_setdead: not found (%d:%d)", addr.client, addr.port);
  71. }
  72. }
  73. void
  74. a2j_port_free (struct a2j_port * port)
  75. {
  76. // snd_seq_disconnect_from (self->seq, self->port_id, port->remote.client, port->remote.port);
  77. // snd_seq_disconnect_to (self->seq, self->port_id, port->remote.client, port->remote.port);
  78. if (port->inbound_events) {
  79. jack_ringbuffer_free (port->inbound_events);
  80. }
  81. if (port->jack_port != JACK_INVALID_PORT && !port->a2j_ptr->finishing) {
  82. jack_port_unregister (port->a2j_ptr->jack_client, port->jack_port);
  83. }
  84. free (port);
  85. }
  86. void
  87. a2j_port_fill_name (struct a2j_port * port_ptr, int dir, snd_seq_client_info_t * client_info_ptr,
  88. const snd_seq_port_info_t * port_info_ptr, bool make_unique)
  89. {
  90. char *c;
  91. if (make_unique) {
  92. snprintf (port_ptr->name,
  93. sizeof(port_ptr->name),
  94. "%s [%d] %s %s",
  95. snd_seq_client_info_get_name(client_info_ptr),
  96. snd_seq_client_info_get_client(client_info_ptr),
  97. snd_seq_port_info_get_name(port_info_ptr),
  98. (dir == A2J_PORT_CAPTURE ? "in" : "out"));
  99. } else {
  100. snprintf (port_ptr->name,
  101. sizeof(port_ptr->name),
  102. "%s %s %s",
  103. snd_seq_client_info_get_name(client_info_ptr),
  104. snd_seq_port_info_get_name(port_info_ptr),
  105. (dir == A2J_PORT_CAPTURE ? "in" : "out"));
  106. }
  107. // replace all offending characters with ' '
  108. for (c = port_ptr->name; *c; ++c) {
  109. if (!JACK_IS_VALID_PORT_NAME_CHAR(*c)) {
  110. *c = ' ';
  111. }
  112. }
  113. }
  114. struct a2j_port *
  115. a2j_port_create (struct a2j * self, int dir, snd_seq_addr_t addr, const snd_seq_port_info_t * info)
  116. {
  117. struct a2j_port *port;
  118. int err;
  119. int client;
  120. snd_seq_client_info_t * client_info_ptr;
  121. int jack_caps;
  122. struct a2j_stream * stream_ptr;
  123. stream_ptr = &self->stream[dir];
  124. if ((err = snd_seq_client_info_malloc (&client_info_ptr)) != 0) {
  125. a2j_error("Failed to allocate client info");
  126. goto fail;
  127. }
  128. client = snd_seq_port_info_get_client (info);
  129. err = snd_seq_get_any_client_info (self->seq, client, client_info_ptr);
  130. if (err != 0) {
  131. a2j_error("Failed to get client info");
  132. goto fail_free_client_info;
  133. }
  134. a2j_debug ("client name: '%s'", snd_seq_client_info_get_name(client_info_ptr));
  135. a2j_debug ("port name: '%s'", snd_seq_port_info_get_name(info));
  136. port = calloc (1, sizeof(struct a2j_port));
  137. if (!port) {
  138. goto fail_free_client_info;
  139. }
  140. port->a2j_ptr = self;
  141. port->jack_port = JACK_INVALID_PORT;
  142. port->remote = addr;
  143. a2j_port_fill_name (port, dir, client_info_ptr, info, false);
  144. /* Add port to list early, before registering to JACK, so map functionality is guaranteed to work during port registration */
  145. list_add_tail (&port->siblings, &stream_ptr->list);
  146. if (dir == A2J_PORT_CAPTURE) {
  147. jack_caps = JackPortIsOutput;
  148. } else {
  149. jack_caps = JackPortIsInput;
  150. }
  151. /* mark anything that looks like a hardware port as physical&terminal */
  152. if (snd_seq_port_info_get_type (info) & (SND_SEQ_PORT_TYPE_HARDWARE|SND_SEQ_PORT_TYPE_PORT|SND_SEQ_PORT_TYPE_SPECIFIC)) {
  153. jack_caps |= JackPortIsPhysical|JackPortIsTerminal;
  154. }
  155. port->jack_port = jack_port_register (self->jack_client, port->name, JACK_DEFAULT_MIDI_TYPE, jack_caps, 0);
  156. if (port->jack_port == JACK_INVALID_PORT) {
  157. a2j_error("jack_port_register() failed for '%s'", port->name);
  158. goto fail_free_port;
  159. }
  160. if (dir == A2J_PORT_CAPTURE) {
  161. err = a2j_alsa_connect_from (self, port->remote.client, port->remote.port);
  162. } else {
  163. err = snd_seq_connect_to (self->seq, self->port_id, port->remote.client, port->remote.port);
  164. }
  165. if (err) {
  166. a2j_debug("port skipped: %s", port->name);
  167. goto fail_free_port;
  168. }
  169. port->inbound_events = jack_ringbuffer_create(MAX_EVENT_SIZE*16);
  170. a2j_debug("port created: %s", port->name);
  171. return port;
  172. fail_free_port:
  173. list_del (&port->siblings);
  174. a2j_port_free (port);
  175. fail_free_client_info:
  176. snd_seq_client_info_free (client_info_ptr);
  177. fail:
  178. return NULL;
  179. }