jack1 codebase
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.

216 lines
5.9KB

  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 input, 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",
  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. } else {
  99. snprintf (port_ptr->name,
  100. sizeof(port_ptr->name),
  101. "%s: %s",
  102. snd_seq_client_info_get_name(client_info_ptr),
  103. snd_seq_port_info_get_name(port_info_ptr));
  104. }
  105. // replace all offending characters with ' '
  106. for (c = port_ptr->name; *c; ++c) {
  107. if (!JACK_IS_VALID_PORT_NAME_CHAR(*c)) {
  108. *c = ' ';
  109. }
  110. }
  111. }
  112. struct a2j_port *
  113. a2j_port_create (struct a2j * self, snd_seq_addr_t addr, const snd_seq_port_info_t * info)
  114. {
  115. struct a2j_port *port;
  116. int err;
  117. int client;
  118. snd_seq_client_info_t * client_info_ptr;
  119. int jack_caps;
  120. struct a2j_stream * stream_ptr;
  121. stream_ptr = &self->stream;
  122. if ((err = snd_seq_client_info_malloc (&client_info_ptr)) != 0) {
  123. a2j_error("Failed to allocate client info");
  124. goto fail;
  125. }
  126. client = snd_seq_port_info_get_client (info);
  127. err = snd_seq_get_any_client_info (self->seq, client, client_info_ptr);
  128. if (err != 0) {
  129. a2j_error("Failed to get client info");
  130. goto fail_free_client_info;
  131. }
  132. a2j_debug ("client name: '%s'", snd_seq_client_info_get_name(client_info_ptr));
  133. a2j_debug ("port name: '%s'", snd_seq_port_info_get_name(info));
  134. port = calloc (1, sizeof(struct a2j_port));
  135. if (!port) {
  136. goto fail_free_client_info;
  137. }
  138. port->a2j_ptr = self;
  139. port->jack_port = JACK_INVALID_PORT;
  140. port->remote = addr;
  141. a2j_port_fill_name (port, self->input, client_info_ptr, info, true);
  142. /* Add port to list early, before registering to JACK, so map functionality is guaranteed to work during port registration */
  143. list_add_tail (&port->siblings, &stream_ptr->list);
  144. if (self->input) {
  145. jack_caps = JackPortIsOutput;
  146. } else {
  147. jack_caps = JackPortIsInput;
  148. }
  149. /* mark anything that looks like a hardware port as physical&terminal */
  150. if (snd_seq_port_info_get_type (info) & (SND_SEQ_PORT_TYPE_HARDWARE|SND_SEQ_PORT_TYPE_PORT|SND_SEQ_PORT_TYPE_SPECIFIC)) {
  151. jack_caps |= JackPortIsPhysical|JackPortIsTerminal;
  152. }
  153. port->jack_port = jack_port_register (self->jack_client, port->name, JACK_DEFAULT_MIDI_TYPE, jack_caps, 0);
  154. if (port->jack_port == JACK_INVALID_PORT) {
  155. a2j_error("jack_port_register() failed for '%s'", port->name);
  156. goto fail_free_port;
  157. }
  158. if (self->input) {
  159. err = a2j_alsa_connect_from(self, port->remote.client, port->remote.port);
  160. } else {
  161. err = snd_seq_connect_to(self->seq, self->port_id, port->remote.client, port->remote.port);
  162. }
  163. if (err) {
  164. a2j_info("port skipped: %s", port->name);
  165. goto fail_free_port;
  166. }
  167. port->inbound_events = jack_ringbuffer_create(MAX_EVENT_SIZE*16);
  168. a2j_info("port created: %s", port->name);
  169. return port;
  170. fail_free_port:
  171. list_del (&port->siblings);
  172. a2j_port_free (port);
  173. fail_free_client_info:
  174. snd_seq_client_info_free (client_info_ptr);
  175. fail:
  176. return NULL;
  177. }