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.

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