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.

856 lines
22KB

  1. /* -*- Mode: C ; c-basic-offset: 2 -*- */
  2. /*
  3. * ALSA SEQ < - > JACK MIDI bridge
  4. *
  5. * Copyright (c) 2006,2007 Dmitry S. Baikov <c0ff@konstruktiv.org>
  6. * Copyright (c) 2007,2008,2009 Nedko Arnaudov <nedko@arnaudov.name>
  7. * Copyright (c) 2009,2010,2013 Paul Davis <paul@linuxaudiosystems.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; version 2 of the License.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <stdbool.h>
  23. #include <stdarg.h>
  24. #include <string.h>
  25. #include <time.h>
  26. #include <alsa/asoundlib.h>
  27. #include <jack/jack.h>
  28. #include <jack/midiport.h>
  29. #include <jack/ringbuffer.h>
  30. #include "list.h"
  31. #include "a2j.h"
  32. #include "port_hash.h"
  33. #include "port.h"
  34. #include "port_thread.h"
  35. #ifdef A2J_DEBUG
  36. bool a2j_do_debug = false;
  37. void
  38. _a2j_debug (const char* fmt, ...)
  39. {
  40. va_list ap;
  41. va_start (ap, fmt);
  42. vfprintf (stderr, fmt, ap);
  43. fputc ('\n', stderr);
  44. }
  45. #endif
  46. void
  47. a2j_error (const char* fmt, ...)
  48. {
  49. va_list ap;
  50. va_start (ap, fmt);
  51. vfprintf (stdout, fmt, ap);
  52. fputc ('\n', stdout);
  53. }
  54. static bool
  55. a2j_stream_init (alsa_midi_driver_t* driver, int which)
  56. {
  57. struct a2j_stream *str = &driver->stream[which];
  58. str->new_ports = jack_ringbuffer_create (MAX_PORTS * sizeof(struct a2j_port *));
  59. if (str->new_ports == NULL) {
  60. return false;
  61. }
  62. snd_midi_event_new (MAX_EVENT_SIZE, &str->codec);
  63. INIT_LIST_HEAD (&str->list);
  64. return true;
  65. }
  66. static void
  67. a2j_stream_detach (struct a2j_stream * stream_ptr)
  68. {
  69. struct a2j_port * port_ptr;
  70. struct list_head * node_ptr;
  71. if (!stream_ptr) {
  72. return;
  73. }
  74. while (!list_empty (&stream_ptr->list)) {
  75. node_ptr = stream_ptr->list.next;
  76. list_del (node_ptr);
  77. port_ptr = list_entry (node_ptr, struct a2j_port, siblings);
  78. a2j_debug ("port deleted: %s", port_ptr->name);
  79. a2j_port_free (port_ptr);
  80. }
  81. }
  82. static
  83. void
  84. a2j_stream_close (alsa_midi_driver_t* driver, int which)
  85. {
  86. struct a2j_stream *str = &driver->stream[which];
  87. if (!str) {
  88. return;
  89. }
  90. if (str->codec) {
  91. snd_midi_event_free (str->codec);
  92. }
  93. if (str->new_ports) {
  94. jack_ringbuffer_free (str->new_ports);
  95. }
  96. }
  97. static void
  98. stop_threads (alsa_midi_driver_t* driver)
  99. {
  100. if (driver->running) {
  101. void* thread_status;
  102. driver->running = false; /* tell alsa io thread to stop, whenever they wake up */
  103. /* do something that we need to do anyway and will wake the io thread, then join */
  104. snd_seq_disconnect_from (driver->seq, driver->port_id, SND_SEQ_CLIENT_SYSTEM, SND_SEQ_PORT_SYSTEM_ANNOUNCE);
  105. a2j_debug ("wait for ALSA input thread\n");
  106. pthread_join (driver->alsa_input_thread, &thread_status);
  107. a2j_debug ("input thread done\n");
  108. /* wake output thread and join */
  109. sem_post (&driver->output_semaphore);
  110. pthread_join (driver->alsa_output_thread, &thread_status);
  111. a2j_debug ("output thread done\n");
  112. }
  113. }
  114. /*
  115. * =================== Input/output port handling =========================
  116. */
  117. void a2j_add_ports (struct a2j_stream * str)
  118. {
  119. struct a2j_port * port_ptr;
  120. while (jack_ringbuffer_read (str->new_ports, (char*)&port_ptr, sizeof(port_ptr))) {
  121. a2j_debug ("jack: inserted port %s", port_ptr->name);
  122. a2j_port_insert (str->port_hash, port_ptr);
  123. }
  124. }
  125. static
  126. void
  127. a2j_port_event (alsa_midi_driver_t* driver, snd_seq_event_t * ev)
  128. {
  129. const snd_seq_addr_t addr = ev->data.addr;
  130. if (addr.client == driver->client_id) {
  131. return;
  132. }
  133. if (ev->type == SND_SEQ_EVENT_PORT_START) {
  134. a2j_debug ("port_event: add %d:%d", addr.client, addr.port);
  135. a2j_new_ports (driver, addr);
  136. } else if (ev->type == SND_SEQ_EVENT_PORT_CHANGE) {
  137. a2j_debug ("port_event: change %d:%d", addr.client, addr.port);
  138. a2j_update_ports (driver, addr);
  139. } else if (ev->type == SND_SEQ_EVENT_PORT_EXIT) {
  140. a2j_debug ("port_event: del %d:%d", addr.client, addr.port);
  141. a2j_port_setdead (driver->stream[A2J_PORT_CAPTURE].port_hash, addr);
  142. a2j_port_setdead (driver->stream[A2J_PORT_PLAYBACK].port_hash, addr);
  143. }
  144. }
  145. /* --- INBOUND FROM ALSA TO JACK ---- */
  146. static void
  147. a2j_input_event (alsa_midi_driver_t* driver, snd_seq_event_t * alsa_event)
  148. {
  149. jack_midi_data_t data[MAX_EVENT_SIZE];
  150. struct a2j_stream *str = &driver->stream[A2J_PORT_CAPTURE];
  151. long size;
  152. struct a2j_port *port;
  153. jack_nframes_t now;
  154. now = jack_frame_time (driver->jack_client);
  155. if ((port = a2j_port_get (str->port_hash, alsa_event->source)) == NULL) {
  156. return;
  157. }
  158. /*
  159. * RPNs, NRPNs, Bank Change, etc. need special handling
  160. * but seems, ALSA does it for us already.
  161. */
  162. snd_midi_event_reset_decode (str->codec);
  163. if ((size = snd_midi_event_decode (str->codec, data, sizeof(data), alsa_event)) < 0) {
  164. return;
  165. }
  166. // fixup NoteOn with vel 0
  167. if ((data[0] & 0xF0) == 0x90 && data[2] == 0x00) {
  168. data[0] = 0x80 + (data[0] & 0x0F);
  169. data[2] = 0x40;
  170. }
  171. a2j_debug ("input: %d bytes at event_frame=%u", (int)size, now);
  172. if (jack_ringbuffer_write_space (port->inbound_events) >= (sizeof(struct a2j_alsa_midi_event) + size)) {
  173. struct a2j_alsa_midi_event ev;
  174. char *ev_charp = (char*)&ev;
  175. size_t limit;
  176. size_t to_write = sizeof(ev);
  177. jack_ringbuffer_data_t vec[2];
  178. jack_ringbuffer_get_write_vector ( port->inbound_events, vec );
  179. ev.time = now;
  180. ev.size = size;
  181. limit = (to_write > vec[0].len ? vec[0].len : to_write);
  182. if (limit) {
  183. memcpy ( vec[0].buf, ev_charp, limit );
  184. to_write -= limit;
  185. ev_charp += limit;
  186. vec[0].buf += limit;
  187. vec[0].len -= limit;
  188. }
  189. if (to_write) {
  190. memcpy ( vec[1].buf, ev_charp, to_write );
  191. vec[1].buf += to_write;
  192. vec[1].len -= to_write;
  193. }
  194. to_write = size;
  195. ev_charp = (char*)data;
  196. limit = (to_write > vec[0].len ? vec[0].len : to_write);
  197. if (limit) {
  198. memcpy (vec[0].buf, ev_charp, limit);
  199. }
  200. to_write -= limit;
  201. ev_charp += limit;
  202. if (to_write) {
  203. memcpy (vec[1].buf, ev_charp, to_write);
  204. }
  205. jack_ringbuffer_write_advance ( port->inbound_events, sizeof(ev) + size );
  206. } else {
  207. a2j_error ("MIDI data lost (incoming event buffer full): %ld bytes lost", size);
  208. }
  209. }
  210. static int
  211. a2j_process_incoming (alsa_midi_driver_t* driver, struct a2j_port* port, jack_nframes_t nframes)
  212. {
  213. jack_nframes_t one_period;
  214. struct a2j_alsa_midi_event ev;
  215. char *ev_buf;
  216. /* grab data queued by the ALSA input thread and write it into the JACK
  217. port buffer. it will delivered during the JACK period that this
  218. function is called from.
  219. */
  220. /* first clear the JACK port buffer in preparation for new data
  221. */
  222. a2j_debug ("PORT: %s process input", jack_port_name (port->jack_port));
  223. jack_midi_clear_buffer (port->jack_buf);
  224. one_period = jack_get_buffer_size (driver->jack_client);
  225. while (jack_ringbuffer_peek (port->inbound_events, (char*)&ev, sizeof(ev) ) == sizeof(ev) ) {
  226. jack_midi_data_t* buf;
  227. jack_nframes_t offset;
  228. a2j_debug ("Seen inbound event from read callback\n");
  229. if (ev.time >= driver->cycle_start) {
  230. a2j_debug ("event is too late\n");
  231. break;
  232. }
  233. //jack_ringbuffer_read_advance (port->inbound_events, sizeof (ev));
  234. ev_buf = (char*)alloca ( sizeof(ev) + ev.size );
  235. if (jack_ringbuffer_peek (port->inbound_events, ev_buf, sizeof(ev) + ev.size ) != sizeof(ev) + ev.size) {
  236. break;
  237. }
  238. offset = driver->cycle_start - ev.time;
  239. if (offset > one_period) {
  240. /* from a previous cycle, somehow. cram it in at the front */
  241. offset = 0;
  242. } else {
  243. /* offset from start of the current cycle */
  244. offset = one_period - offset;
  245. }
  246. a2j_debug ("event at %d offset %d", ev.time, offset);
  247. /* make sure there is space for it */
  248. buf = jack_midi_event_reserve (port->jack_buf, offset, ev.size);
  249. if (buf) {
  250. /* grab the event */
  251. memcpy ( buf, ev_buf + sizeof(ev), ev.size );
  252. } else {
  253. /* throw it away (no space) */
  254. a2j_error ("threw away MIDI event - not reserved at time %d", ev.time);
  255. }
  256. jack_ringbuffer_read_advance (port->inbound_events, sizeof(ev) + ev.size);
  257. a2j_debug ("input on %s: sucked %d bytes from inbound at %d", jack_port_name (port->jack_port), ev.size, ev.time);
  258. }
  259. return 0;
  260. }
  261. void*
  262. alsa_input_thread (void* arg)
  263. {
  264. alsa_midi_driver_t* driver = arg;
  265. int npfd;
  266. struct pollfd * pfd;
  267. snd_seq_addr_t addr;
  268. snd_seq_client_info_t * client_info;
  269. bool initial;
  270. snd_seq_event_t * event;
  271. int ret;
  272. npfd = snd_seq_poll_descriptors_count (driver->seq, POLLIN);
  273. pfd = (struct pollfd*)alloca (npfd * sizeof(struct pollfd));
  274. snd_seq_poll_descriptors (driver->seq, pfd, npfd, POLLIN);
  275. initial = true;
  276. while (driver->running) {
  277. if ((ret = poll (pfd, npfd, 1000)) > 0) {
  278. while (snd_seq_event_input (driver->seq, &event) > 0) {
  279. if (initial) {
  280. snd_seq_client_info_alloca (&client_info);
  281. snd_seq_client_info_set_client (client_info, -1);
  282. while (snd_seq_query_next_client (driver->seq, client_info) >= 0) {
  283. addr.client = snd_seq_client_info_get_client (client_info);
  284. if (addr.client == SND_SEQ_CLIENT_SYSTEM || addr.client == driver->client_id) {
  285. continue;
  286. }
  287. a2j_new_ports (driver, addr);
  288. }
  289. initial = false;
  290. }
  291. if (event->source.client == SND_SEQ_CLIENT_SYSTEM) {
  292. a2j_port_event (driver, event);
  293. } else {
  294. a2j_input_event (driver, event);
  295. }
  296. snd_seq_free_event (event);
  297. }
  298. }
  299. }
  300. return (void*)0;
  301. }
  302. /* --- OUTBOUND FROM JACK TO ALSA ---- */
  303. int
  304. a2j_process_outgoing (
  305. alsa_midi_driver_t* driver,
  306. struct a2j_port * port)
  307. {
  308. /* collect data from JACK port buffer and queue it for delivery by ALSA output thread */
  309. int nevents;
  310. jack_ringbuffer_data_t vec[2];
  311. int i;
  312. int written = 0;
  313. size_t limit;
  314. struct a2j_delivery_event* dev;
  315. size_t gap = 0;
  316. jack_ringbuffer_get_write_vector (driver->outbound_events, vec);
  317. dev = (struct a2j_delivery_event*)vec[0].buf;
  318. limit = vec[0].len / sizeof(struct a2j_delivery_event);
  319. nevents = jack_midi_get_event_count (port->jack_buf);
  320. a2j_debug ("alsa_out: port has %d events for delivery\n", nevents);
  321. for (i = 0; (i < nevents) && (written < limit); ++i) {
  322. jack_midi_event_get (&dev->jack_event, port->jack_buf, i);
  323. if (dev->jack_event.size <= MAX_JACKMIDI_EV_SIZE) {
  324. dev->time = dev->jack_event.time;
  325. dev->port = port;
  326. memcpy ( dev->midistring, dev->jack_event.buffer, dev->jack_event.size );
  327. written++;
  328. ++dev;
  329. }
  330. }
  331. /* anything left? use the second part of the vector, as much as possible */
  332. if (i < nevents) {
  333. if (vec[0].len) {
  334. gap = vec[0].len - written * sizeof(struct a2j_delivery_event);
  335. }
  336. dev = (struct a2j_delivery_event*)vec[1].buf;
  337. limit += (vec[1].len / sizeof(struct a2j_delivery_event));
  338. while ((i < nevents) && (written < limit)) {
  339. jack_midi_event_get (&dev->jack_event, port->jack_buf, i);
  340. if (dev->jack_event.size <= MAX_JACKMIDI_EV_SIZE) {
  341. dev->time = dev->jack_event.time;
  342. dev->port = port;
  343. memcpy (dev->midistring, dev->jack_event.buffer, dev->jack_event.size);
  344. written++;
  345. ++dev;
  346. }
  347. ++i;
  348. }
  349. }
  350. a2j_debug ( "done pushing events: %d ... gap: %d ", (int)written, (int)gap );
  351. /* clear JACK port buffer; advance ring buffer ptr */
  352. jack_ringbuffer_write_advance (driver->outbound_events, written * sizeof(struct a2j_delivery_event) + gap);
  353. return nevents;
  354. }
  355. static int
  356. time_sorter (struct a2j_delivery_event * a, struct a2j_delivery_event * b)
  357. {
  358. if (a->time < b->time) {
  359. return -1;
  360. } else if (a->time > b->time) {
  361. return 1;
  362. }
  363. return 0;
  364. }
  365. static void*
  366. alsa_output_thread (void * arg)
  367. {
  368. alsa_midi_driver_t * driver = (alsa_midi_driver_t*)arg;
  369. struct a2j_stream *str = &driver->stream[A2J_PORT_PLAYBACK];
  370. int i;
  371. struct list_head evlist;
  372. struct list_head * node_ptr;
  373. jack_ringbuffer_data_t vec[2];
  374. snd_seq_event_t alsa_event;
  375. struct a2j_delivery_event* ev;
  376. float sr;
  377. jack_nframes_t now;
  378. int limit;
  379. while (driver->running) {
  380. /* pre-first, handle port deletion requests */
  381. a2j_free_ports (driver);
  382. /* first, make a list of all events in the outbound_events FIFO */
  383. INIT_LIST_HEAD (&evlist);
  384. jack_ringbuffer_get_read_vector (driver->outbound_events, vec);
  385. a2j_debug ("alsa_out: output thread: got %d+%d events",
  386. (vec[0].len / sizeof(struct a2j_delivery_event)),
  387. (vec[1].len / sizeof(struct a2j_delivery_event)));
  388. ev = (struct a2j_delivery_event*)vec[0].buf;
  389. limit = vec[0].len / sizeof(struct a2j_delivery_event);
  390. for (i = 0; i < limit; ++i) {
  391. list_add_tail (&ev->siblings, &evlist);
  392. ev++;
  393. }
  394. ev = (struct a2j_delivery_event*)vec[1].buf;
  395. limit = vec[1].len / sizeof(struct a2j_delivery_event);
  396. for (i = 0; i < limit; ++i) {
  397. list_add_tail (&ev->siblings, &evlist);
  398. ev++;
  399. }
  400. if (vec[0].len < sizeof(struct a2j_delivery_event) && (vec[1].len == 0)) {
  401. /* no events: wait for some */
  402. a2j_debug ("alsa_out: output thread: wait for events");
  403. sem_wait (&driver->output_semaphore);
  404. a2j_debug ("alsa_out: output thread: AWAKE ... loop back for events");
  405. continue;
  406. }
  407. /* now sort this list by time */
  408. list_sort (&evlist, struct a2j_delivery_event, siblings, time_sorter);
  409. /* now deliver */
  410. sr = jack_get_sample_rate (driver->jack_client);
  411. list_for_each (node_ptr, &evlist){
  412. ev = list_entry (node_ptr, struct a2j_delivery_event, siblings);
  413. snd_seq_ev_clear (&alsa_event);
  414. snd_midi_event_reset_encode (str->codec);
  415. if (!snd_midi_event_encode (str->codec, (const unsigned char*)ev->midistring, ev->jack_event.size, &alsa_event)) {
  416. a2j_debug ("alsa_out: invalid event of size %d, ignored\n", ev->jack_event.size);
  417. continue; // invalid event
  418. }
  419. snd_seq_ev_set_source (&alsa_event, driver->port_id);
  420. snd_seq_ev_set_dest (&alsa_event, ev->port->remote.client, ev->port->remote.port);
  421. snd_seq_ev_set_direct (&alsa_event);
  422. now = jack_frame_time (driver->jack_client);
  423. ev->time += driver->cycle_start;
  424. a2j_debug ("alsa_out:@ %d, next event @ %d", now, ev->time);
  425. /* do we need to wait a while before delivering? */
  426. if (ev->time > now) {
  427. struct timespec nanoseconds;
  428. jack_nframes_t sleep_frames = ev->time - now;
  429. float seconds = sleep_frames / sr;
  430. /* if the gap is long enough, sleep */
  431. if (seconds > 0.001) {
  432. nanoseconds.tv_sec = (time_t)seconds;
  433. nanoseconds.tv_nsec = (long)NSEC_PER_SEC * (seconds - nanoseconds.tv_sec);
  434. a2j_debug ("alsa_out: output thread sleeps for %.2f msec", ((double)nanoseconds.tv_nsec / NSEC_PER_SEC) * 1000.0);
  435. if (nanosleep (&nanoseconds, NULL) < 0) {
  436. fprintf (stderr, "BAD SLEEP\n");
  437. /* do something ? */
  438. }
  439. }
  440. }
  441. /* its time to deliver */
  442. snd_seq_event_output (driver->seq, &alsa_event);
  443. snd_seq_drain_output (driver->seq);
  444. now = jack_frame_time (driver->jack_client);
  445. a2j_debug ("alsa_out: written %d bytes to %s at %d, DELTA = %d", ev->jack_event.size, ev->port->name, now,
  446. (int32_t)(now - ev->time));
  447. }
  448. /* free up space in the FIFO */
  449. jack_ringbuffer_read_advance (driver->outbound_events, vec[0].len + vec[1].len);
  450. /* and head back for more */
  451. }
  452. return (void*)0;
  453. }
  454. /** CORE JACK PROCESSING */
  455. /* ALSA */
  456. static void
  457. a2j_jack_process_internal (alsa_midi_driver_t* driver, int dir, jack_nframes_t nframes)
  458. {
  459. struct a2j_stream * stream_ptr;
  460. int i;
  461. struct a2j_port ** port_ptr_ptr;
  462. struct a2j_port * port_ptr;
  463. int nevents = 0;
  464. stream_ptr = &driver->stream[dir];
  465. a2j_add_ports (stream_ptr);
  466. // process ports
  467. for (i = 0; i < PORT_HASH_SIZE; i++) {
  468. port_ptr_ptr = &stream_ptr->port_hash[i];
  469. while (*port_ptr_ptr != NULL) {
  470. port_ptr = *port_ptr_ptr;
  471. if (!port_ptr->is_dead) {
  472. port_ptr->jack_buf = jack_port_get_buffer (port_ptr->jack_port, nframes);
  473. if (dir == A2J_PORT_CAPTURE) {
  474. a2j_process_incoming (driver, port_ptr, nframes);
  475. } else {
  476. nevents += a2j_process_outgoing (driver, port_ptr);
  477. }
  478. } else if (jack_ringbuffer_write_space (driver->port_del) >= sizeof(port_ptr)) {
  479. a2j_debug ("jack: removed port %s", port_ptr->name);
  480. *port_ptr_ptr = port_ptr->next;
  481. jack_ringbuffer_write (driver->port_del, (char*)&port_ptr, sizeof(port_ptr));
  482. nevents += 1; /* wake up output thread, see: a2j_free_ports */
  483. continue;
  484. }
  485. port_ptr_ptr = &port_ptr->next;
  486. }
  487. }
  488. if (dir == A2J_PORT_PLAYBACK && nevents > 0) {
  489. int sv;
  490. /* if we queued up anything for output, tell the output thread in
  491. case its waiting for us.
  492. */
  493. sem_getvalue (&driver->output_semaphore, &sv);
  494. sem_post (&driver->output_semaphore);
  495. }
  496. }
  497. /* JACK DRIVER FUNCTIONS */
  498. static int
  499. alsa_midi_read (alsa_midi_driver_t* driver, jack_nframes_t nframes)
  500. {
  501. driver->cycle_start = jack_last_frame_time (driver->jack_client);
  502. a2j_jack_process_internal (driver, A2J_PORT_CAPTURE, nframes);
  503. return 0;
  504. }
  505. static int
  506. alsa_midi_write (alsa_midi_driver_t* driver, jack_nframes_t nframes)
  507. {
  508. driver->cycle_start = jack_last_frame_time (driver->jack_client);
  509. a2j_jack_process_internal (driver, A2J_PORT_PLAYBACK, nframes);
  510. return 0;
  511. }
  512. static int
  513. alsa_midi_start (alsa_midi_driver_t* driver)
  514. {
  515. int error;
  516. snd_seq_start_queue (driver->seq, driver->queue, 0);
  517. snd_seq_drop_input (driver->seq);
  518. a2j_add_ports (&driver->stream[A2J_PORT_CAPTURE]);
  519. a2j_add_ports (&driver->stream[A2J_PORT_PLAYBACK]);
  520. driver->running = true;
  521. if (pthread_create (&driver->alsa_input_thread, NULL, alsa_input_thread, driver) < 0) {
  522. a2j_error ("cannot start ALSA input thread");
  523. return -1;
  524. }
  525. /* wake the poll loop in the alsa input thread so initial ports are fetched */
  526. if ((error = snd_seq_connect_from (driver->seq, driver->port_id, SND_SEQ_CLIENT_SYSTEM, SND_SEQ_PORT_SYSTEM_ANNOUNCE)) < 0) {
  527. a2j_error ("snd_seq_connect_from() failed");
  528. return -1;
  529. }
  530. if (pthread_create (&driver->alsa_output_thread, NULL, alsa_output_thread, driver) < 0) {
  531. a2j_error ("cannot start ALSA output thread");
  532. return -1;
  533. }
  534. return 0;
  535. }
  536. static int
  537. alsa_midi_stop (alsa_midi_driver_t* driver)
  538. {
  539. stop_threads (driver);
  540. (void)snd_seq_stop_queue (driver->seq, driver->queue, 0);
  541. return 0;
  542. }
  543. static int
  544. alsa_midi_attach (alsa_midi_driver_t* driver, jack_engine_t* engine)
  545. {
  546. int error;
  547. driver->port_del = jack_ringbuffer_create (2 * MAX_PORTS * sizeof(struct a2j_port *));
  548. if (driver->port_del == NULL) {
  549. return -1;
  550. }
  551. driver->outbound_events = jack_ringbuffer_create (MAX_EVENT_SIZE * 16 * sizeof(struct a2j_delivery_event));
  552. if (driver->outbound_events == NULL) {
  553. return -1;
  554. }
  555. if (!a2j_stream_init (driver, A2J_PORT_CAPTURE)) {
  556. return -1;
  557. }
  558. if (!a2j_stream_init (driver, A2J_PORT_PLAYBACK)) {
  559. return -1;
  560. }
  561. if ((error = snd_seq_open (&driver->seq, "hw", SND_SEQ_OPEN_DUPLEX, 0)) < 0) {
  562. a2j_error ("failed to open alsa seq");
  563. return -1;
  564. }
  565. if ((error = snd_seq_set_client_name (driver->seq, "jackmidi")) < 0) {
  566. a2j_error ("snd_seq_set_client_name() failed");
  567. return -1;
  568. }
  569. if ((driver->port_id = snd_seq_create_simple_port (
  570. driver->seq,
  571. "port",
  572. SND_SEQ_PORT_CAP_READ | SND_SEQ_PORT_CAP_WRITE
  573. #ifndef DEBUG
  574. | SND_SEQ_PORT_CAP_NO_EXPORT
  575. #endif
  576. , SND_SEQ_PORT_TYPE_APPLICATION)) < 0) {
  577. a2j_error ("snd_seq_create_simple_port() failed");
  578. return -1;
  579. }
  580. if ((driver->client_id = snd_seq_client_id (driver->seq)) < 0) {
  581. a2j_error ("snd_seq_client_id() failed");
  582. return -1;
  583. }
  584. if ((driver->queue = snd_seq_alloc_queue (driver->seq)) < 0) {
  585. a2j_error ("snd_seq_alloc_queue() failed");
  586. return -1;
  587. }
  588. if ((error = snd_seq_nonblock (driver->seq, 1)) < 0) {
  589. a2j_error ("snd_seq_nonblock() failed");
  590. return -1;
  591. }
  592. return jack_activate (driver->jack_client);
  593. }
  594. static int
  595. alsa_midi_detach (alsa_midi_driver_t* driver, jack_engine_t* engine)
  596. {
  597. driver->finishing = true;
  598. snd_seq_close (driver->seq);
  599. driver->seq = NULL;
  600. return 0;
  601. }
  602. static jack_driver_t *
  603. alsa_midi_driver_new (jack_client_t *client, const char *name)
  604. {
  605. alsa_midi_driver_t* driver = calloc (1, sizeof(alsa_midi_driver_t));
  606. jack_info ("creating alsa_midi driver ...");
  607. if (!driver) {
  608. return NULL;
  609. }
  610. jack_driver_init ((jack_driver_t*)driver);
  611. driver->attach = (JackDriverAttachFunction)alsa_midi_attach;
  612. driver->detach = (JackDriverDetachFunction)alsa_midi_detach;
  613. driver->read = (JackDriverReadFunction)alsa_midi_read;
  614. driver->write = (JackDriverWriteFunction)alsa_midi_write;
  615. driver->start = (JackDriverStartFunction)alsa_midi_start;
  616. driver->stop = (JackDriverStartFunction)alsa_midi_stop;
  617. driver->jack_client = client;
  618. if (sem_init (&driver->output_semaphore, 0, 0) < 0) {
  619. a2j_error ("can't create IO semaphore");
  620. free (driver);
  621. return NULL;
  622. }
  623. return (jack_driver_t*)driver;
  624. }
  625. static void
  626. alsa_midi_driver_delete (alsa_midi_driver_t* driver)
  627. {
  628. a2j_stream_detach (&driver->stream[A2J_PORT_CAPTURE]);
  629. a2j_stream_detach (&driver->stream[A2J_PORT_PLAYBACK]);
  630. a2j_stream_close (driver, A2J_PORT_CAPTURE);
  631. a2j_stream_close (driver, A2J_PORT_PLAYBACK);
  632. sem_destroy (&driver->output_semaphore);
  633. jack_ringbuffer_free (driver->outbound_events);
  634. jack_ringbuffer_free (driver->port_del);
  635. }
  636. /* DRIVER "PLUGIN" INTERFACE */
  637. const char driver_client_name[] = "alsa_midi";
  638. const jack_driver_desc_t *
  639. driver_get_descriptor ()
  640. {
  641. jack_driver_desc_t * desc;
  642. jack_driver_param_desc_t * params;
  643. //unsigned int i;
  644. desc = calloc (1, sizeof(jack_driver_desc_t));
  645. strcpy (desc->name, "alsa_midi");
  646. desc->nparams = 0;
  647. params = calloc (desc->nparams, sizeof(jack_driver_param_desc_t));
  648. desc->params = params;
  649. return desc;
  650. }
  651. jack_driver_t *
  652. driver_initialize (jack_client_t *client, const JSList * params)
  653. {
  654. const JSList * node;
  655. const jack_driver_param_t * param;
  656. for (node = params; node; node = jack_slist_next (node)) {
  657. param = (const jack_driver_param_t*)node->data;
  658. switch (param->character) {
  659. default:
  660. break;
  661. }
  662. }
  663. return alsa_midi_driver_new (client, NULL);
  664. }
  665. void
  666. driver_finish (jack_driver_t *driver)
  667. {
  668. alsa_midi_driver_delete ((alsa_midi_driver_t*)driver);
  669. }