jack2 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.

940 lines
24KB

  1. /*
  2. * ALSA SEQ < - > JACK MIDI bridge
  3. *
  4. * Copyright (c) 2006,2007 Dmitry S. Baikov <c0ff@konstruktiv.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. /*
  21. * alsa_seqmidi_read:
  22. * add new ports
  23. * reads queued snd_seq_event's
  24. * if PORT_EXIT: mark port as dead
  25. * if PORT_ADD, PORT_CHANGE: send addr to port_thread (it also may mark port as dead)
  26. * else process input event
  27. * remove dead ports and send them to port_thread
  28. *
  29. * alsa_seqmidi_write:
  30. * remove dead ports and send them to port_thread
  31. * add new ports
  32. * queue output events
  33. *
  34. * port_thread:
  35. * wait for port_sem
  36. * free deleted ports
  37. * create new ports or mark existing as dead
  38. */
  39. #include <alsa/asoundlib.h>
  40. #include <stdlib.h>
  41. #include <stdio.h>
  42. #include <signal.h>
  43. #include <semaphore.h>
  44. #include <pthread.h>
  45. #include <time.h>
  46. #include <ctype.h>
  47. #include <limits.h>
  48. #include "midiport.h"
  49. #include "ringbuffer.h"
  50. #include "alsa_midi_impl.h"
  51. #include "JackError.h"
  52. #define NSEC_PER_SEC ((int64_t)1000*1000*1000)
  53. enum {
  54. MAX_PORTS = 64,
  55. MAX_EVENT_SIZE = 1024,
  56. };
  57. typedef struct port_t port_t;
  58. enum {
  59. PORT_HASH_BITS = 4,
  60. PORT_HASH_SIZE = 1 << PORT_HASH_BITS
  61. };
  62. typedef port_t* port_hash_t[PORT_HASH_SIZE];
  63. struct port_t {
  64. port_t *next;
  65. int is_dead;
  66. char name[64];
  67. snd_seq_addr_t remote;
  68. jack_port_t *jack_port;
  69. jack_ringbuffer_t *early_events; // alsa_midi_event_t + data
  70. int64_t last_out_time;
  71. void *jack_buf;
  72. };
  73. typedef struct {
  74. snd_midi_event_t *codec;
  75. jack_ringbuffer_t *new_ports;
  76. port_t *ports[MAX_PORTS];
  77. } stream_t;
  78. typedef struct alsa_seqmidi {
  79. alsa_midi_t ops;
  80. jack_client_t *jack;
  81. snd_seq_t *seq;
  82. snd_seq_queue_timer_t* timer;
  83. int client_id;
  84. int port_id;
  85. int queue;
  86. int keep_walking;
  87. pthread_t port_thread;
  88. sem_t port_sem;
  89. jack_ringbuffer_t *port_add; // snd_seq_addr_t
  90. jack_ringbuffer_t *port_del; // port_t*
  91. stream_t stream[2];
  92. char alsa_name[32];
  93. int midi_in_cnt;
  94. int midi_out_cnt;
  95. } alsa_seqmidi_t;
  96. struct alsa_midi_event {
  97. int64_t time;
  98. int size;
  99. };
  100. typedef struct alsa_midi_event alsa_midi_event_t;
  101. struct process_info {
  102. int dir;
  103. jack_nframes_t nframes;
  104. jack_nframes_t period_start;
  105. jack_nframes_t sample_rate;
  106. jack_nframes_t cur_frames;
  107. int64_t alsa_time;
  108. };
  109. enum PortType { PORT_INPUT = 0, PORT_OUTPUT = 1 };
  110. typedef void (*port_jack_func)(alsa_seqmidi_t *self, port_t *port,struct process_info* info);
  111. static void do_jack_input(alsa_seqmidi_t *self, port_t *port, struct process_info* info);
  112. static void do_jack_output(alsa_seqmidi_t *self, port_t *port, struct process_info* info);
  113. typedef struct {
  114. int alsa_mask;
  115. int jack_caps;
  116. char name[9];
  117. port_jack_func jack_func;
  118. } port_type_t;
  119. static port_type_t port_type[2] = {
  120. {
  121. SND_SEQ_PORT_CAP_SUBS_READ,
  122. JackPortIsOutput,
  123. "playback",
  124. do_jack_input
  125. },
  126. {
  127. SND_SEQ_PORT_CAP_SUBS_WRITE,
  128. JackPortIsInput,
  129. "capture",
  130. do_jack_output
  131. }
  132. };
  133. static void alsa_seqmidi_delete(alsa_midi_t *m);
  134. static int alsa_seqmidi_attach(alsa_midi_t *m);
  135. static int alsa_seqmidi_detach(alsa_midi_t *m);
  136. static int alsa_seqmidi_start(alsa_midi_t *m);
  137. static int alsa_seqmidi_stop(alsa_midi_t *m);
  138. static void alsa_seqmidi_read(alsa_midi_t *m, jack_nframes_t nframes);
  139. static void alsa_seqmidi_write(alsa_midi_t *m, jack_nframes_t nframes);
  140. static
  141. void stream_init(alsa_seqmidi_t *self, int dir)
  142. {
  143. stream_t *str = &self->stream[dir];
  144. str->new_ports = jack_ringbuffer_create(MAX_PORTS*sizeof(port_t*));
  145. snd_midi_event_new(MAX_EVENT_SIZE, &str->codec);
  146. }
  147. static void port_free(alsa_seqmidi_t *self, port_t *port);
  148. static void free_ports(alsa_seqmidi_t *self, jack_ringbuffer_t *ports);
  149. static
  150. void stream_attach(alsa_seqmidi_t *self, int dir)
  151. {
  152. }
  153. static
  154. void stream_detach(alsa_seqmidi_t *self, int dir)
  155. {
  156. stream_t *str = &self->stream[dir];
  157. int i;
  158. free_ports(self, str->new_ports);
  159. // delete all ports from hash
  160. for (i=0; i<PORT_HASH_SIZE; ++i) {
  161. port_t *port = str->ports[i];
  162. while (port) {
  163. port_t *next = port->next;
  164. port_free(self, port);
  165. port = next;
  166. }
  167. str->ports[i] = NULL;
  168. }
  169. }
  170. static
  171. void stream_close(alsa_seqmidi_t *self, int dir)
  172. {
  173. stream_t *str = &self->stream[dir];
  174. if (str->codec)
  175. snd_midi_event_free(str->codec);
  176. if (str->new_ports)
  177. jack_ringbuffer_free(str->new_ports);
  178. }
  179. alsa_midi_t* alsa_seqmidi_new(jack_client_t *client, const char* alsa_name)
  180. {
  181. alsa_seqmidi_t *self = calloc(1, sizeof(alsa_seqmidi_t));
  182. debug_log("midi: new");
  183. if (!self)
  184. return NULL;
  185. self->jack = client;
  186. if (!alsa_name)
  187. alsa_name = "jack_midi";
  188. snprintf(self->alsa_name, sizeof(self->alsa_name), "%s", alsa_name);
  189. self->port_add = jack_ringbuffer_create(2*MAX_PORTS*sizeof(snd_seq_addr_t));
  190. self->port_del = jack_ringbuffer_create(2*MAX_PORTS*sizeof(port_t*));
  191. sem_init(&self->port_sem, 0, 0);
  192. stream_init(self, PORT_INPUT);
  193. stream_init(self, PORT_OUTPUT);
  194. self->midi_in_cnt = 0;
  195. self->midi_out_cnt = 0;
  196. self->ops.destroy = alsa_seqmidi_delete;
  197. self->ops.attach = alsa_seqmidi_attach;
  198. self->ops.detach = alsa_seqmidi_detach;
  199. self->ops.start = alsa_seqmidi_start;
  200. self->ops.stop = alsa_seqmidi_stop;
  201. self->ops.read = alsa_seqmidi_read;
  202. self->ops.write = alsa_seqmidi_write;
  203. return &self->ops;
  204. }
  205. static
  206. void alsa_seqmidi_delete(alsa_midi_t *m)
  207. {
  208. alsa_seqmidi_t *self = (alsa_seqmidi_t*) m;
  209. debug_log("midi: delete");
  210. alsa_seqmidi_detach(m);
  211. stream_close(self, PORT_OUTPUT);
  212. stream_close(self, PORT_INPUT);
  213. jack_ringbuffer_free(self->port_add);
  214. jack_ringbuffer_free(self->port_del);
  215. sem_close(&self->port_sem);
  216. free(self);
  217. }
  218. static
  219. int alsa_seqmidi_attach(alsa_midi_t *m)
  220. {
  221. alsa_seqmidi_t *self = (alsa_seqmidi_t*) m;
  222. int err;
  223. debug_log("midi: attach");
  224. if (self->seq)
  225. return -EALREADY;
  226. if ((err = snd_seq_open(&self->seq, "hw", SND_SEQ_OPEN_DUPLEX, 0)) < 0) {
  227. error_log("failed to open alsa seq");
  228. return err;
  229. }
  230. if ((err = snd_seq_queue_timer_malloc(&self->timer)) < 0) {
  231. error_log("failed to allocate timer");
  232. return err;
  233. }
  234. snd_seq_set_client_name(self->seq, self->alsa_name);
  235. self->port_id = snd_seq_create_simple_port(self->seq, "port",
  236. SND_SEQ_PORT_CAP_READ|SND_SEQ_PORT_CAP_WRITE
  237. #ifndef JACK_MIDI_DEBUG
  238. |SND_SEQ_PORT_CAP_NO_EXPORT
  239. #endif
  240. ,SND_SEQ_PORT_TYPE_APPLICATION);
  241. self->client_id = snd_seq_client_id(self->seq);
  242. self->queue = snd_seq_alloc_queue(self->seq);
  243. // set high resolution
  244. if (snd_seq_get_queue_timer(self->seq, self->queue, self->timer) == 0) {
  245. snd_seq_queue_timer_set_resolution(self->timer, UINT_MAX);
  246. snd_seq_set_queue_timer(self->seq, self->queue, self->timer);
  247. } else {
  248. error_log("failed to set alsa timer in high resolution");
  249. }
  250. snd_seq_start_queue(self->seq, self->queue, 0);
  251. stream_attach(self, PORT_INPUT);
  252. stream_attach(self, PORT_OUTPUT);
  253. snd_seq_nonblock(self->seq, 1);
  254. return 0;
  255. }
  256. static
  257. int alsa_seqmidi_detach(alsa_midi_t *m)
  258. {
  259. alsa_seqmidi_t *self = (alsa_seqmidi_t*) m;
  260. debug_log("midi: detach");
  261. if (!self->seq)
  262. return -EALREADY;
  263. alsa_seqmidi_stop(m);
  264. jack_ringbuffer_reset(self->port_add);
  265. free_ports(self, self->port_del);
  266. stream_detach(self, PORT_INPUT);
  267. stream_detach(self, PORT_OUTPUT);
  268. snd_seq_queue_timer_free(self->timer);
  269. snd_seq_close(self->seq);
  270. self->seq = NULL;
  271. return 0;
  272. }
  273. static void* port_thread(void *);
  274. static void add_existing_ports(alsa_seqmidi_t *self);
  275. static void update_ports(alsa_seqmidi_t *self);
  276. static void add_ports(stream_t *str);
  277. static
  278. int alsa_seqmidi_start(alsa_midi_t *m)
  279. {
  280. alsa_seqmidi_t *self = (alsa_seqmidi_t*) m;
  281. int err;
  282. debug_log("midi: start");
  283. if (!self->seq)
  284. return -EBADF;
  285. if (self->keep_walking)
  286. return -EALREADY;
  287. snd_seq_connect_from(self->seq, self->port_id, SND_SEQ_CLIENT_SYSTEM, SND_SEQ_PORT_SYSTEM_ANNOUNCE);
  288. snd_seq_drop_input(self->seq);
  289. add_existing_ports(self);
  290. update_ports(self);
  291. add_ports(&self->stream[PORT_INPUT]);
  292. add_ports(&self->stream[PORT_OUTPUT]);
  293. self->keep_walking = 1;
  294. if ((err = pthread_create(&self->port_thread, NULL, port_thread, self))) {
  295. self->keep_walking = 0;
  296. return -errno;
  297. }
  298. return 0;
  299. }
  300. static
  301. int alsa_seqmidi_stop(alsa_midi_t *m)
  302. {
  303. alsa_seqmidi_t *self = (alsa_seqmidi_t*) m;
  304. debug_log("midi: stop");
  305. if (!self->keep_walking)
  306. return -EALREADY;
  307. snd_seq_disconnect_from(self->seq, self->port_id, SND_SEQ_CLIENT_SYSTEM, SND_SEQ_PORT_SYSTEM_ANNOUNCE);
  308. self->keep_walking = 0;
  309. sem_post(&self->port_sem);
  310. pthread_join(self->port_thread, NULL);
  311. self->port_thread = 0;
  312. return 0;
  313. }
  314. static
  315. int alsa_connect_from(alsa_seqmidi_t *self, int client, int port)
  316. {
  317. snd_seq_port_subscribe_t* sub;
  318. snd_seq_addr_t seq_addr;
  319. int err;
  320. snd_seq_port_subscribe_alloca(&sub);
  321. seq_addr.client = client;
  322. seq_addr.port = port;
  323. snd_seq_port_subscribe_set_sender(sub, &seq_addr);
  324. seq_addr.client = self->client_id;
  325. seq_addr.port = self->port_id;
  326. snd_seq_port_subscribe_set_dest(sub, &seq_addr);
  327. snd_seq_port_subscribe_set_time_update(sub, 1);
  328. snd_seq_port_subscribe_set_queue(sub, self->queue);
  329. snd_seq_port_subscribe_set_time_real(sub, 1);
  330. if ((err=snd_seq_subscribe_port(self->seq, sub)))
  331. error_log("can't subscribe to %d:%d - %s", client, port, snd_strerror(err));
  332. return err;
  333. }
  334. /*
  335. * ==================== Port routines =============================
  336. */
  337. static inline
  338. int port_hash(snd_seq_addr_t addr)
  339. {
  340. return (addr.client + addr.port) % PORT_HASH_SIZE;
  341. }
  342. static
  343. port_t* port_get(port_hash_t hash, snd_seq_addr_t addr)
  344. {
  345. port_t **pport = &hash[port_hash(addr)];
  346. while (*pport) {
  347. port_t *port = *pport;
  348. if (port->remote.client == addr.client && port->remote.port == addr.port)
  349. return port;
  350. pport = &port->next;
  351. }
  352. return NULL;
  353. }
  354. static
  355. void port_insert(port_hash_t hash, port_t *port)
  356. {
  357. port_t **pport = &hash[port_hash(port->remote)];
  358. port->next = *pport;
  359. *pport = port;
  360. }
  361. static
  362. void port_setdead(port_hash_t hash, snd_seq_addr_t addr)
  363. {
  364. port_t *port = port_get(hash, addr);
  365. if (port)
  366. port->is_dead = 1; // see jack_process
  367. else {
  368. debug_log("port_setdead: not found (%d:%d)", addr.client, addr.port);
  369. }
  370. }
  371. static
  372. void port_free(alsa_seqmidi_t *self, port_t *port)
  373. {
  374. //snd_seq_disconnect_from(self->seq, self->port_id, port->remote.client, port->remote.port);
  375. //snd_seq_disconnect_to(self->seq, self->port_id, port->remote.client, port->remote.port);
  376. if (port->early_events)
  377. jack_ringbuffer_free(port->early_events);
  378. if (port->jack_port)
  379. jack_port_unregister(self->jack, port->jack_port);
  380. info_log("port deleted: %s", port->name);
  381. free(port);
  382. }
  383. static
  384. port_t* port_create(alsa_seqmidi_t *self, int type, snd_seq_addr_t addr, const snd_seq_port_info_t *info)
  385. {
  386. snd_seq_client_info_t* client_info;
  387. port_t *port;
  388. char *c;
  389. int err;
  390. int jack_caps;
  391. char name[128];
  392. port = calloc(1, sizeof(port_t));
  393. if (!port)
  394. return NULL;
  395. port->remote = addr;
  396. snd_seq_client_info_alloca (&client_info);
  397. snd_seq_get_any_client_info (self->seq, addr.client, client_info);
  398. const char *device_name = snd_seq_client_info_get_name(client_info);
  399. snprintf(port->name, sizeof(port->name), "alsa_pcm:%s/midi_%s_%d",
  400. device_name, port_type[type].name, addr.port+1);
  401. // replace all offending characters by -
  402. for (c = port->name; *c; ++c)
  403. if (!isalnum(*c) && *c != '/' && *c != '_' && *c != ':' && *c != '(' && *c != ')')
  404. *c = '-';
  405. jack_caps = port_type[type].jack_caps;
  406. /* mark anything that looks like a hardware port as physical&terminal */
  407. if (snd_seq_port_info_get_type (info) & (SND_SEQ_PORT_TYPE_HARDWARE|SND_SEQ_PORT_TYPE_PORT|SND_SEQ_PORT_TYPE_SPECIFIC)) {
  408. jack_caps |= (JackPortIsPhysical | JackPortIsTerminal);
  409. }
  410. if (jack_caps & JackPortIsOutput)
  411. snprintf(name, sizeof(name), "system:midi_capture_%d", ++self->midi_in_cnt);
  412. else
  413. snprintf(name, sizeof(name), "system:midi_playback_%d", ++self->midi_out_cnt);
  414. port->jack_port = jack_port_register(self->jack,
  415. name, JACK_DEFAULT_MIDI_TYPE, jack_caps, 0);
  416. if (!port->jack_port)
  417. goto failed;
  418. jack_port_set_alias (port->jack_port, port->name);
  419. jack_port_set_default_metadata (port->jack_port, device_name);
  420. /* generate an alias */
  421. snprintf(port->name, sizeof(port->name), "%s:midi/%s_%d",
  422. snd_seq_client_info_get_name (client_info), port_type[type].name, addr.port+1);
  423. // replace all offending characters by -
  424. for (c = port->name; *c; ++c)
  425. if (!isalnum(*c) && *c != '/' && *c != '_' && *c != ':' && *c != '(' && *c != ')')
  426. *c = '-';
  427. jack_port_set_alias (port->jack_port, port->name);
  428. jack_port_set_default_metadata (port->jack_port, device_name);
  429. if (type == PORT_INPUT)
  430. err = alsa_connect_from(self, port->remote.client, port->remote.port);
  431. else
  432. err = snd_seq_connect_to(self->seq, self->port_id, port->remote.client, port->remote.port);
  433. if (err)
  434. goto failed;
  435. port->early_events = jack_ringbuffer_create(MAX_EVENT_SIZE*16);
  436. info_log("port created: %s", port->name);
  437. return port;
  438. failed:
  439. port_free(self, port);
  440. return NULL;
  441. }
  442. /*
  443. * ==================== Port add/del handling thread ==============================
  444. */
  445. static
  446. void update_port_type(alsa_seqmidi_t *self, int type, snd_seq_addr_t addr, int caps, const snd_seq_port_info_t *info)
  447. {
  448. stream_t *str = &self->stream[type];
  449. int alsa_mask = port_type[type].alsa_mask;
  450. port_t *port = port_get(str->ports, addr);
  451. debug_log("update_port_type(%d:%d)", addr.client, addr.port);
  452. if (port && (caps & alsa_mask)!=alsa_mask) {
  453. debug_log("setdead: %s", port->name);
  454. port->is_dead = 1;
  455. }
  456. if (!port && (caps & alsa_mask)==alsa_mask) {
  457. assert (jack_ringbuffer_write_space(str->new_ports) >= sizeof(port));
  458. port = port_create(self, type, addr, info);
  459. if (port)
  460. jack_ringbuffer_write(str->new_ports, (char*)&port, sizeof(port));
  461. }
  462. }
  463. static
  464. void update_port(alsa_seqmidi_t *self, snd_seq_addr_t addr, const snd_seq_port_info_t *info)
  465. {
  466. unsigned int port_caps = snd_seq_port_info_get_capability(info);
  467. if (port_caps & SND_SEQ_PORT_CAP_NO_EXPORT)
  468. return;
  469. update_port_type(self, PORT_INPUT, addr, port_caps, info);
  470. update_port_type(self, PORT_OUTPUT,addr, port_caps, info);
  471. }
  472. static
  473. void free_ports(alsa_seqmidi_t *self, jack_ringbuffer_t *ports)
  474. {
  475. port_t *port;
  476. int sz;
  477. while ((sz = jack_ringbuffer_read(ports, (char*)&port, sizeof(port)))) {
  478. assert (sz == sizeof(port));
  479. port_free(self, port);
  480. }
  481. }
  482. static
  483. void update_ports(alsa_seqmidi_t *self)
  484. {
  485. snd_seq_addr_t addr;
  486. snd_seq_port_info_t *info;
  487. int size;
  488. snd_seq_port_info_alloca(&info);
  489. while ((size = jack_ringbuffer_read(self->port_add, (char*)&addr, sizeof(addr)))) {
  490. int err;
  491. assert (size == sizeof(addr));
  492. assert (addr.client != self->client_id);
  493. if ((err=snd_seq_get_any_port_info(self->seq, addr.client, addr.port, info))>=0) {
  494. update_port(self, addr, info);
  495. } else {
  496. //port_setdead(self->stream[PORT_INPUT].ports, addr);
  497. //port_setdead(self->stream[PORT_OUTPUT].ports, addr);
  498. }
  499. }
  500. }
  501. static
  502. void* port_thread(void *arg)
  503. {
  504. alsa_seqmidi_t *self = arg;
  505. while (self->keep_walking) {
  506. sem_wait(&self->port_sem);
  507. free_ports(self, self->port_del);
  508. update_ports(self);
  509. }
  510. debug_log("port_thread exited");
  511. return NULL;
  512. }
  513. static
  514. void add_existing_ports(alsa_seqmidi_t *self)
  515. {
  516. snd_seq_addr_t addr;
  517. snd_seq_client_info_t *client_info;
  518. snd_seq_port_info_t *port_info;
  519. snd_seq_client_info_alloca(&client_info);
  520. snd_seq_port_info_alloca(&port_info);
  521. snd_seq_client_info_set_client(client_info, -1);
  522. while (snd_seq_query_next_client(self->seq, client_info) >= 0)
  523. {
  524. addr.client = snd_seq_client_info_get_client(client_info);
  525. if (addr.client == SND_SEQ_CLIENT_SYSTEM || addr.client == self->client_id)
  526. continue;
  527. snd_seq_port_info_set_client(port_info, addr.client);
  528. snd_seq_port_info_set_port(port_info, -1);
  529. while (snd_seq_query_next_port(self->seq, port_info) >= 0)
  530. {
  531. addr.port = snd_seq_port_info_get_port(port_info);
  532. update_port(self, addr, port_info);
  533. }
  534. }
  535. }
  536. /*
  537. * =================== Input/output port handling =========================
  538. */
  539. static
  540. void set_process_info(struct process_info *info, alsa_seqmidi_t *self, int dir, jack_nframes_t nframes)
  541. {
  542. const snd_seq_real_time_t* alsa_time;
  543. snd_seq_queue_status_t *status;
  544. snd_seq_queue_status_alloca(&status);
  545. info->dir = dir;
  546. info->period_start = jack_last_frame_time(self->jack);
  547. info->nframes = nframes;
  548. info->sample_rate = jack_get_sample_rate(self->jack);
  549. info->cur_frames = jack_frame_time(self->jack);
  550. // immediately get alsa'a real time (uhh, why everybody has their own 'real' time)
  551. snd_seq_get_queue_status(self->seq, self->queue, status);
  552. alsa_time = snd_seq_queue_status_get_real_time(status);
  553. info->alsa_time = alsa_time->tv_sec * NSEC_PER_SEC + alsa_time->tv_nsec;
  554. if (info->period_start + info->nframes < info->cur_frames) {
  555. int periods_lost = (info->cur_frames - info->period_start) / info->nframes;
  556. info->period_start += periods_lost * info->nframes;
  557. debug_log("xrun detected: %d periods lost\n", periods_lost);
  558. }
  559. }
  560. static
  561. void add_ports(stream_t *str)
  562. {
  563. port_t *port;
  564. while (jack_ringbuffer_read(str->new_ports, (char*)&port, sizeof(port))) {
  565. debug_log("jack: inserted port %s\n", port->name);
  566. port_insert(str->ports, port);
  567. }
  568. }
  569. static
  570. void jack_process(alsa_seqmidi_t *self, struct process_info *info)
  571. {
  572. stream_t *str = &self->stream[info->dir];
  573. port_jack_func process = port_type[info->dir].jack_func;
  574. int i, del=0;
  575. add_ports(str);
  576. // process ports
  577. for (i=0; i<PORT_HASH_SIZE; ++i) {
  578. port_t **pport = &str->ports[i];
  579. while (*pport) {
  580. port_t *port = *pport;
  581. port->jack_buf = jack_port_get_buffer(port->jack_port, info->nframes);
  582. if (info->dir == PORT_INPUT)
  583. jack_midi_clear_buffer(port->jack_buf);
  584. if (!port->is_dead)
  585. (*process)(self, port, info);
  586. else if (jack_ringbuffer_write_space(self->port_del) >= sizeof(port)) {
  587. debug_log("jack: removed port %s", port->name);
  588. *pport = port->next;
  589. jack_ringbuffer_write(self->port_del, (char*)&port, sizeof(port));
  590. del++;
  591. continue;
  592. }
  593. pport = &port->next;
  594. }
  595. }
  596. if (del)
  597. sem_post(&self->port_sem);
  598. }
  599. /*
  600. * ============================ Input ==============================
  601. */
  602. static
  603. void do_jack_input(alsa_seqmidi_t *self, port_t *port, struct process_info *info)
  604. {
  605. // process port->early_events
  606. alsa_midi_event_t ev;
  607. while (jack_ringbuffer_read(port->early_events, (char*)&ev, sizeof(ev))) {
  608. jack_midi_data_t* buf;
  609. int64_t time = ev.time - info->period_start;
  610. if (time < 0)
  611. time = 0;
  612. else if (time >= info->nframes)
  613. time = info->nframes - 1;
  614. buf = jack_midi_event_reserve(port->jack_buf, (jack_nframes_t)time, ev.size);
  615. if (buf)
  616. jack_ringbuffer_read(port->early_events, (char*)buf, ev.size);
  617. else
  618. jack_ringbuffer_read_advance(port->early_events, ev.size);
  619. debug_log("input: it's time for %d bytes at %lld", ev.size, time);
  620. }
  621. }
  622. static
  623. void port_event(alsa_seqmidi_t *self, snd_seq_event_t *ev)
  624. {
  625. const snd_seq_addr_t addr = ev->data.addr;
  626. if (addr.client == self->client_id)
  627. return;
  628. if (ev->type == SND_SEQ_EVENT_PORT_START || ev->type == SND_SEQ_EVENT_PORT_CHANGE) {
  629. assert (jack_ringbuffer_write_space(self->port_add) >= sizeof(addr));
  630. debug_log("port_event: add/change %d:%d", addr.client, addr.port);
  631. jack_ringbuffer_write(self->port_add, (char*)&addr, sizeof(addr));
  632. sem_post(&self->port_sem);
  633. } else if (ev->type == SND_SEQ_EVENT_PORT_EXIT) {
  634. debug_log("port_event: del %d:%d", addr.client, addr.port);
  635. port_setdead(self->stream[PORT_INPUT].ports, addr);
  636. port_setdead(self->stream[PORT_OUTPUT].ports, addr);
  637. }
  638. }
  639. static
  640. void input_event(alsa_seqmidi_t *self, snd_seq_event_t *alsa_event, struct process_info* info)
  641. {
  642. jack_midi_data_t data[MAX_EVENT_SIZE];
  643. stream_t *str = &self->stream[PORT_INPUT];
  644. long size;
  645. int64_t alsa_time, time_offset;
  646. int64_t frame_offset, event_frame;
  647. port_t *port;
  648. port = port_get(str->ports, alsa_event->source);
  649. if (!port)
  650. return;
  651. /*
  652. * RPNs, NRPNs, Bank Change, etc. need special handling
  653. * but seems, ALSA does it for us already.
  654. */
  655. snd_midi_event_reset_decode(str->codec);
  656. if ((size = snd_midi_event_decode(str->codec, data, sizeof(data), alsa_event))<0)
  657. return;
  658. // fixup NoteOn with vel 0
  659. if ((data[0] & 0xF0) == 0x90 && data[2] == 0x00) {
  660. data[0] = 0x80 + (data[0] & 0x0F);
  661. data[2] = 0x40;
  662. }
  663. alsa_time = alsa_event->time.time.tv_sec * NSEC_PER_SEC + alsa_event->time.time.tv_nsec;
  664. time_offset = info->alsa_time - alsa_time;
  665. frame_offset = (info->sample_rate * time_offset) / NSEC_PER_SEC;
  666. event_frame = (int64_t)info->cur_frames - info->period_start - frame_offset + info->nframes;
  667. debug_log("input: %d bytes at event_frame = %d", (int)size, (int)event_frame);
  668. if (event_frame >= info->nframes &&
  669. jack_ringbuffer_write_space(port->early_events) >= (sizeof(alsa_midi_event_t) + size)) {
  670. alsa_midi_event_t ev;
  671. ev.time = event_frame + info->period_start;
  672. ev.size = size;
  673. jack_ringbuffer_write(port->early_events, (char*)&ev, sizeof(ev));
  674. jack_ringbuffer_write(port->early_events, (char*)data, size);
  675. debug_log("postponed to next frame +%d", (int) (event_frame - info->nframes));
  676. return;
  677. }
  678. if (event_frame < 0)
  679. event_frame = 0;
  680. else if (event_frame >= info->nframes)
  681. event_frame = info->nframes - 1;
  682. jack_midi_event_write(port->jack_buf, event_frame, data, size);
  683. }
  684. static
  685. void alsa_seqmidi_read(alsa_midi_t *m, jack_nframes_t nframes)
  686. {
  687. alsa_seqmidi_t *self = (alsa_seqmidi_t*) m;
  688. int res;
  689. snd_seq_event_t *event;
  690. struct process_info info;
  691. if (!self->keep_walking)
  692. return;
  693. set_process_info(&info, self, PORT_INPUT, nframes);
  694. jack_process(self, &info);
  695. while ((res = snd_seq_event_input(self->seq, &event))>0) {
  696. if (event->source.client == SND_SEQ_CLIENT_SYSTEM)
  697. port_event(self, event);
  698. else
  699. input_event(self, event, &info);
  700. }
  701. }
  702. /*
  703. * ============================ Output ==============================
  704. */
  705. static
  706. void do_jack_output(alsa_seqmidi_t *self, port_t *port, struct process_info* info)
  707. {
  708. stream_t *str = &self->stream[info->dir];
  709. int nevents = jack_midi_get_event_count(port->jack_buf);
  710. int i, err;
  711. for (i=0; i<nevents; ++i) {
  712. jack_midi_event_t jack_event;
  713. snd_seq_event_t alsa_event;
  714. int64_t frame_offset;
  715. int64_t out_time;
  716. snd_seq_real_time_t out_rt;
  717. jack_midi_event_get(&jack_event, port->jack_buf, i);
  718. snd_seq_ev_clear(&alsa_event);
  719. snd_midi_event_reset_encode(str->codec);
  720. if (!snd_midi_event_encode(str->codec, jack_event.buffer, jack_event.size, &alsa_event))
  721. continue; // invalid event
  722. snd_seq_ev_set_source(&alsa_event, self->port_id);
  723. snd_seq_ev_set_dest(&alsa_event, port->remote.client, port->remote.port);
  724. /* NOTE: in case of xrun it could become negative, so it is essential to use signed type! */
  725. frame_offset = (int64_t)jack_event.time + info->period_start + info->nframes - info->cur_frames;
  726. if (frame_offset < 0) {
  727. frame_offset = info->nframes + jack_event.time;
  728. error_log("internal xrun detected: frame_offset = %"PRId64"\n", frame_offset);
  729. }
  730. /* Ken Ellinwood reported problems with this assert.
  731. * Seems, magic 2 should be replaced with nperiods. */
  732. //FIXME: assert (frame_offset < info->nframes*2);
  733. //if (frame_offset < info->nframes * info->nperiods)
  734. // debug_log("alsa_out: BLAH-BLAH-BLAH");
  735. out_time = info->alsa_time + (frame_offset * NSEC_PER_SEC) / info->sample_rate;
  736. debug_log("alsa_out: frame_offset = %lld, info->alsa_time = %lld, out_time = %lld, port->last_out_time = %lld",
  737. frame_offset, info->alsa_time, out_time, port->last_out_time);
  738. // we should use absolute time to prevent reordering caused by rounding errors
  739. if (out_time < port->last_out_time) {
  740. debug_log("alsa_out: limiting out_time %lld at %lld", out_time, port->last_out_time);
  741. out_time = port->last_out_time;
  742. } else
  743. port->last_out_time = out_time;
  744. out_rt.tv_nsec = out_time % NSEC_PER_SEC;
  745. out_rt.tv_sec = out_time / NSEC_PER_SEC;
  746. snd_seq_ev_schedule_real(&alsa_event, self->queue, 0, &out_rt);
  747. err = snd_seq_event_output(self->seq, &alsa_event);
  748. 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);
  749. }
  750. return;
  751. // may be unused
  752. (void)err;
  753. }
  754. static
  755. void alsa_seqmidi_write(alsa_midi_t *m, jack_nframes_t nframes)
  756. {
  757. alsa_seqmidi_t *self = (alsa_seqmidi_t*) m;
  758. struct process_info info;
  759. if (!self->keep_walking)
  760. return;
  761. set_process_info(&info, self, PORT_OUTPUT, nframes);
  762. jack_process(self, &info);
  763. snd_seq_drain_output(self->seq);
  764. }