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.

908 lines
23KB

  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 "midiport.h"
  48. #include "ringbuffer.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. jack_nframes_t nframes;
  102. jack_nframes_t period_start;
  103. jack_nframes_t sample_rate;
  104. jack_nframes_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[9];
  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,
  121. "playback",
  122. do_jack_input
  123. },
  124. {
  125. SND_SEQ_PORT_CAP_SUBS_WRITE,
  126. JackPortIsInput,
  127. "capture",
  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 JACK_MIDI_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. snd_seq_client_info_t* client_info;
  372. port_t *port;
  373. char *c;
  374. int err;
  375. int jack_caps;
  376. port = calloc(1, sizeof(port_t));
  377. if (!port)
  378. return NULL;
  379. port->remote = addr;
  380. snd_seq_client_info_alloca (&client_info);
  381. snd_seq_get_any_client_info (self->seq, addr.client, client_info);
  382. snprintf(port->name, sizeof(port->name), "%s/midi_%s_%d",
  383. snd_seq_client_info_get_name(client_info), port_type[type].name, addr.port+1);
  384. // replace all offending characters by -
  385. for (c = port->name; *c; ++c)
  386. if (!isalnum(*c) && *c != '/' && *c != '_' && *c != ':' && *c != '(' && *c != ')')
  387. *c = '-';
  388. jack_caps = port_type[type].jack_caps;
  389. /* mark anything that looks like a hardware port as physical&terminal */
  390. if (snd_seq_port_info_get_type (info) & (SND_SEQ_PORT_TYPE_HARDWARE|SND_SEQ_PORT_TYPE_PORT|SND_SEQ_PORT_TYPE_SPECIFIC)) {
  391. jack_caps |= (JackPortIsPhysical|JackPortIsTerminal);
  392. }
  393. port->jack_port = jack_port_register(self->jack,
  394. port->name, JACK_DEFAULT_MIDI_TYPE, jack_caps, 0);
  395. if (!port->jack_port)
  396. goto failed;
  397. /* generate an alias */
  398. snprintf(port->name, sizeof(port->name), "%s:midi/%s_%d",
  399. snd_seq_client_info_get_name (client_info), port_type[type].name, addr.port+1);
  400. // replace all offending characters by -
  401. for (c = port->name; *c; ++c)
  402. if (!isalnum(*c) && *c != '/' && *c != '_' && *c != ':' && *c != '(' && *c != ')')
  403. *c = '-';
  404. jack_port_set_alias (port->jack_port, port->name);
  405. if (type == PORT_INPUT)
  406. err = alsa_connect_from(self, port->remote.client, port->remote.port);
  407. else
  408. err = snd_seq_connect_to(self->seq, self->port_id, port->remote.client, port->remote.port);
  409. if (err)
  410. goto failed;
  411. port->early_events = jack_ringbuffer_create(MAX_EVENT_SIZE*16);
  412. info_log("port created: %s", port->name);
  413. return port;
  414. failed:
  415. port_free(self, port);
  416. return NULL;
  417. }
  418. /*
  419. * ==================== Port add/del handling thread ==============================
  420. */
  421. static
  422. void update_port_type(alsa_seqmidi_t *self, int type, snd_seq_addr_t addr, int caps, const snd_seq_port_info_t *info)
  423. {
  424. stream_t *str = &self->stream[type];
  425. int alsa_mask = port_type[type].alsa_mask;
  426. port_t *port = port_get(str->ports, addr);
  427. debug_log("update_port_type(%d:%d)", addr.client, addr.port);
  428. if (port && (caps & alsa_mask)!=alsa_mask) {
  429. debug_log("setdead: %s", port->name);
  430. port->is_dead = 1;
  431. }
  432. if (!port && (caps & alsa_mask)==alsa_mask) {
  433. assert (jack_ringbuffer_write_space(str->new_ports) >= sizeof(port));
  434. port = port_create(self, type, addr, info);
  435. if (port)
  436. jack_ringbuffer_write(str->new_ports, (char*)&port, sizeof(port));
  437. }
  438. }
  439. static
  440. void update_port(alsa_seqmidi_t *self, snd_seq_addr_t addr, const snd_seq_port_info_t *info)
  441. {
  442. unsigned int port_caps = snd_seq_port_info_get_capability(info);
  443. if (port_caps & SND_SEQ_PORT_CAP_NO_EXPORT)
  444. return;
  445. update_port_type(self, PORT_INPUT, addr, port_caps, info);
  446. update_port_type(self, PORT_OUTPUT,addr, port_caps, info);
  447. }
  448. static
  449. void free_ports(alsa_seqmidi_t *self, jack_ringbuffer_t *ports)
  450. {
  451. port_t *port;
  452. int sz;
  453. while ((sz = jack_ringbuffer_read(ports, (char*)&port, sizeof(port)))) {
  454. assert (sz == sizeof(port));
  455. port_free(self, port);
  456. }
  457. }
  458. static
  459. void update_ports(alsa_seqmidi_t *self)
  460. {
  461. snd_seq_addr_t addr;
  462. snd_seq_port_info_t *info;
  463. int size;
  464. snd_seq_port_info_alloca(&info);
  465. while ((size = jack_ringbuffer_read(self->port_add, (char*)&addr, sizeof(addr)))) {
  466. int err;
  467. assert (size == sizeof(addr));
  468. assert (addr.client != self->client_id);
  469. if ((err=snd_seq_get_any_port_info(self->seq, addr.client, addr.port, info))>=0) {
  470. update_port(self, addr, info);
  471. } else {
  472. //port_setdead(self->stream[PORT_INPUT].ports, addr);
  473. //port_setdead(self->stream[PORT_OUTPUT].ports, addr);
  474. }
  475. }
  476. }
  477. static
  478. void* port_thread(void *arg)
  479. {
  480. alsa_seqmidi_t *self = arg;
  481. while (self->keep_walking) {
  482. sem_wait(&self->port_sem);
  483. free_ports(self, self->port_del);
  484. update_ports(self);
  485. }
  486. debug_log("port_thread exited");
  487. return NULL;
  488. }
  489. static
  490. void add_existing_ports(alsa_seqmidi_t *self)
  491. {
  492. snd_seq_addr_t addr;
  493. snd_seq_client_info_t *client_info;
  494. snd_seq_port_info_t *port_info;
  495. snd_seq_client_info_alloca(&client_info);
  496. snd_seq_port_info_alloca(&port_info);
  497. snd_seq_client_info_set_client(client_info, -1);
  498. while (snd_seq_query_next_client(self->seq, client_info) >= 0)
  499. {
  500. addr.client = snd_seq_client_info_get_client(client_info);
  501. if (addr.client == SND_SEQ_CLIENT_SYSTEM || addr.client == self->client_id)
  502. continue;
  503. snd_seq_port_info_set_client(port_info, addr.client);
  504. snd_seq_port_info_set_port(port_info, -1);
  505. while (snd_seq_query_next_port(self->seq, port_info) >= 0)
  506. {
  507. addr.port = snd_seq_port_info_get_port(port_info);
  508. update_port(self, addr, port_info);
  509. }
  510. }
  511. }
  512. /*
  513. * =================== Input/output port handling =========================
  514. */
  515. static
  516. void set_process_info(struct process_info *info, alsa_seqmidi_t *self, int dir, jack_nframes_t nframes)
  517. {
  518. const snd_seq_real_time_t* alsa_time;
  519. snd_seq_queue_status_t *status;
  520. snd_seq_queue_status_alloca(&status);
  521. info->dir = dir;
  522. info->period_start = jack_last_frame_time(self->jack);
  523. info->nframes = nframes;
  524. info->sample_rate = jack_get_sample_rate(self->jack);
  525. info->cur_frames = jack_frame_time(self->jack);
  526. // immediately get alsa'a real time (uhh, why everybody has their own 'real' time)
  527. snd_seq_get_queue_status(self->seq, self->queue, status);
  528. alsa_time = snd_seq_queue_status_get_real_time(status);
  529. info->alsa_time = alsa_time->tv_sec * NSEC_PER_SEC + alsa_time->tv_nsec;
  530. if (info->period_start + info->nframes < info->cur_frames) {
  531. int periods_lost = (info->cur_frames - info->period_start) / info->nframes;
  532. info->period_start += periods_lost * info->nframes;
  533. debug_log("xrun detected: %d periods lost\n", periods_lost);
  534. }
  535. }
  536. static
  537. void add_ports(stream_t *str)
  538. {
  539. port_t *port;
  540. while (jack_ringbuffer_read(str->new_ports, (char*)&port, sizeof(port))) {
  541. debug_log("jack: inserted port %s\n", port->name);
  542. port_insert(str->ports, port);
  543. }
  544. }
  545. static
  546. void jack_process(alsa_seqmidi_t *self, struct process_info *info)
  547. {
  548. stream_t *str = &self->stream[info->dir];
  549. port_jack_func process = port_type[info->dir].jack_func;
  550. int i, del=0;
  551. add_ports(str);
  552. // process ports
  553. for (i=0; i<PORT_HASH_SIZE; ++i) {
  554. port_t **pport = &str->ports[i];
  555. while (*pport) {
  556. port_t *port = *pport;
  557. port->jack_buf = jack_port_get_buffer(port->jack_port, info->nframes);
  558. if (info->dir == PORT_INPUT)
  559. jack_midi_clear_buffer(port->jack_buf);
  560. if (!port->is_dead)
  561. (*process)(self, port, info);
  562. else if (jack_ringbuffer_write_space(self->port_del) >= sizeof(port)) {
  563. debug_log("jack: removed port %s", port->name);
  564. *pport = port->next;
  565. jack_ringbuffer_write(self->port_del, (char*)&port, sizeof(port));
  566. del++;
  567. continue;
  568. }
  569. pport = &port->next;
  570. }
  571. }
  572. if (del)
  573. sem_post(&self->port_sem);
  574. }
  575. /*
  576. * ============================ Input ==============================
  577. */
  578. static
  579. void do_jack_input(alsa_seqmidi_t *self, port_t *port, struct process_info *info)
  580. {
  581. // process port->early_events
  582. alsa_midi_event_t ev;
  583. while (jack_ringbuffer_read(port->early_events, (char*)&ev, sizeof(ev))) {
  584. jack_midi_data_t* buf;
  585. jack_nframes_t time = ev.time - info->period_start;
  586. if (time < 0)
  587. time = 0;
  588. else if (time >= info->nframes)
  589. time = info->nframes - 1;
  590. buf = jack_midi_event_reserve(port->jack_buf, (jack_nframes_t)time, ev.size);
  591. if (buf)
  592. jack_ringbuffer_read(port->early_events, (char*)buf, ev.size);
  593. else
  594. jack_ringbuffer_read_advance(port->early_events, ev.size);
  595. debug_log("input: it's time for %d bytes at %lld", ev.size, time);
  596. }
  597. }
  598. static
  599. void port_event(alsa_seqmidi_t *self, snd_seq_event_t *ev)
  600. {
  601. const snd_seq_addr_t addr = ev->data.addr;
  602. if (addr.client == self->client_id)
  603. return;
  604. if (ev->type == SND_SEQ_EVENT_PORT_START || ev->type == SND_SEQ_EVENT_PORT_CHANGE) {
  605. assert (jack_ringbuffer_write_space(self->port_add) >= sizeof(addr));
  606. debug_log("port_event: add/change %d:%d", addr.client, addr.port);
  607. jack_ringbuffer_write(self->port_add, (char*)&addr, sizeof(addr));
  608. sem_post(&self->port_sem);
  609. } else if (ev->type == SND_SEQ_EVENT_PORT_EXIT) {
  610. debug_log("port_event: del %d:%d", addr.client, addr.port);
  611. port_setdead(self->stream[PORT_INPUT].ports, addr);
  612. port_setdead(self->stream[PORT_OUTPUT].ports, addr);
  613. }
  614. }
  615. static
  616. void input_event(alsa_seqmidi_t *self, snd_seq_event_t *alsa_event, struct process_info* info)
  617. {
  618. jack_midi_data_t data[MAX_EVENT_SIZE];
  619. stream_t *str = &self->stream[PORT_INPUT];
  620. long size;
  621. int64_t alsa_time, time_offset;
  622. int64_t frame_offset, event_frame;
  623. port_t *port;
  624. port = port_get(str->ports, alsa_event->source);
  625. if (!port)
  626. return;
  627. /*
  628. * RPNs, NRPNs, Bank Change, etc. need special handling
  629. * but seems, ALSA does it for us already.
  630. */
  631. snd_midi_event_reset_decode(str->codec);
  632. if ((size = snd_midi_event_decode(str->codec, data, sizeof(data), alsa_event))<0)
  633. return;
  634. // fixup NoteOn with vel 0
  635. if ((data[0] & 0xF0) == 0x90 && data[2] == 0x00) {
  636. data[0] = 0x80 + (data[0] & 0x0F);
  637. data[2] = 0x40;
  638. }
  639. alsa_time = alsa_event->time.time.tv_sec * NSEC_PER_SEC + alsa_event->time.time.tv_nsec;
  640. time_offset = info->alsa_time - alsa_time;
  641. frame_offset = (info->sample_rate * time_offset) / NSEC_PER_SEC;
  642. event_frame = info->cur_frames - info->period_start - frame_offset + info->nframes;
  643. debug_log("input: %d bytes at event_frame = %d", (int)size, (int)event_frame);
  644. if (event_frame >= info->nframes &&
  645. jack_ringbuffer_write_space(port->early_events) >= (sizeof(alsa_midi_event_t) + size)) {
  646. alsa_midi_event_t ev;
  647. ev.time = event_frame + info->period_start;
  648. ev.size = size;
  649. jack_ringbuffer_write(port->early_events, (char*)&ev, sizeof(ev));
  650. jack_ringbuffer_write(port->early_events, (char*)data, size);
  651. debug_log("postponed to next frame +%d", (int) (event_frame - info->nframes));
  652. return;
  653. }
  654. if (event_frame < 0)
  655. event_frame = 0;
  656. else if (event_frame >= info->nframes)
  657. event_frame = info->nframes - 1;
  658. jack_midi_event_write(port->jack_buf, event_frame, data, size);
  659. }
  660. static
  661. void alsa_seqmidi_read(alsa_midi_t *m, jack_nframes_t nframes)
  662. {
  663. alsa_seqmidi_t *self = (alsa_seqmidi_t*) m;
  664. int res;
  665. snd_seq_event_t *event;
  666. struct process_info info;
  667. if (!self->keep_walking)
  668. return;
  669. set_process_info(&info, self, PORT_INPUT, nframes);
  670. jack_process(self, &info);
  671. while ((res = snd_seq_event_input(self->seq, &event))>0) {
  672. if (event->source.client == SND_SEQ_CLIENT_SYSTEM)
  673. port_event(self, event);
  674. else
  675. input_event(self, event, &info);
  676. }
  677. }
  678. /*
  679. * ============================ Output ==============================
  680. */
  681. static
  682. void do_jack_output(alsa_seqmidi_t *self, port_t *port, struct process_info* info)
  683. {
  684. stream_t *str = &self->stream[info->dir];
  685. int nevents = jack_midi_get_event_count(port->jack_buf);
  686. int i;
  687. for (i=0; i<nevents; ++i) {
  688. jack_midi_event_t jack_event;
  689. snd_seq_event_t alsa_event;
  690. int64_t frame_offset;
  691. int64_t out_time;
  692. snd_seq_real_time_t out_rt;
  693. int err;
  694. jack_midi_event_get(&jack_event, port->jack_buf, i);
  695. snd_seq_ev_clear(&alsa_event);
  696. snd_midi_event_reset_encode(str->codec);
  697. if (!snd_midi_event_encode(str->codec, jack_event.buffer, jack_event.size, &alsa_event))
  698. continue; // invalid event
  699. snd_seq_ev_set_source(&alsa_event, self->port_id);
  700. snd_seq_ev_set_dest(&alsa_event, port->remote.client, port->remote.port);
  701. /* NOTE: in case of xrun it could become negative, so it is essential to use signed type! */
  702. frame_offset = (int64_t)jack_event.time + info->period_start + info->nframes - info->cur_frames;
  703. if (frame_offset < 0) {
  704. frame_offset = info->nframes + jack_event.time;
  705. error_log("internal xrun detected: frame_offset = %"PRId64"\n", frame_offset);
  706. }
  707. /* Ken Ellinwood reported problems with this assert.
  708. * Seems, magic 2 should be replaced with nperiods. */
  709. //FIXME: assert (frame_offset < info->nframes*2);
  710. //if (frame_offset < info->nframes * info->nperiods)
  711. // debug_log("alsa_out: BLAH-BLAH-BLAH");
  712. out_time = info->alsa_time + (frame_offset * NSEC_PER_SEC) / info->sample_rate;
  713. debug_log("alsa_out: frame_offset = %lld, info->alsa_time = %lld, out_time = %lld, port->last_out_time = %lld",
  714. frame_offset, info->alsa_time, out_time, port->last_out_time);
  715. // we should use absolute time to prevent reordering caused by rounding errors
  716. if (out_time < port->last_out_time) {
  717. debug_log("alsa_out: limiting out_time %lld at %lld", out_time, port->last_out_time);
  718. out_time = port->last_out_time;
  719. } else
  720. port->last_out_time = out_time;
  721. out_rt.tv_nsec = out_time % NSEC_PER_SEC;
  722. out_rt.tv_sec = out_time / NSEC_PER_SEC;
  723. snd_seq_ev_schedule_real(&alsa_event, self->queue, 0, &out_rt);
  724. err = snd_seq_event_output(self->seq, &alsa_event);
  725. 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);
  726. }
  727. }
  728. static
  729. void alsa_seqmidi_write(alsa_midi_t *m, jack_nframes_t nframes)
  730. {
  731. alsa_seqmidi_t *self = (alsa_seqmidi_t*) m;
  732. struct process_info info;
  733. if (!self->keep_walking)
  734. return;
  735. set_process_info(&info, self, PORT_OUTPUT, nframes);
  736. jack_process(self, &info);
  737. snd_seq_drain_output(self->seq);
  738. }