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.

869 lines
22KB

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