JACK tools
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.

133 lines
3.3KB

  1. /* -*- Mode: C ; c-basic-offset: 2 -*- */
  2. /*
  3. * ALSA SEQ < - > JACK MIDI bridge
  4. *
  5. * Copyright (c) 2006,2007 Dmitry S. Baikov <c0ff@konstruktiv.org>
  6. * Copyright (c) 2007,2008,2009 Nedko Arnaudov <nedko@arnaudov.name>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; version 2 of the License.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #ifndef __jack_alsa_midi_h__
  22. #define __jack_alsa_midi_h__
  23. #include <stdbool.h>
  24. #include <semaphore.h>
  25. #include <jack/midiport.h>
  26. #define JACK_INVALID_PORT NULL
  27. #define MAX_PORTS 2048
  28. #define MAX_EVENT_SIZE 1024
  29. #define PORT_HASH_BITS 4
  30. #define PORT_HASH_SIZE (1 << PORT_HASH_BITS)
  31. /* Beside enum use, these are indeces for (struct a2j).stream array */
  32. #define A2J_PORT_CAPTURE 0 // ALSA playback port -> JACK capture port
  33. #define A2J_PORT_PLAYBACK 1 // JACK playback port -> ALSA capture port
  34. typedef struct a2j_port * a2j_port_hash_t[PORT_HASH_SIZE];
  35. struct a2j;
  36. struct a2j_port
  37. {
  38. struct a2j_port * next; /* hash - jack */
  39. struct list_head siblings; /* list - main loop */
  40. struct a2j * a2j_ptr;
  41. bool is_dead;
  42. char name[64];
  43. snd_seq_addr_t remote;
  44. jack_port_t * jack_port;
  45. jack_ringbuffer_t * inbound_events; // alsa_midi_event_t + data
  46. int64_t last_out_time;
  47. void * jack_buf;
  48. };
  49. struct a2j_stream
  50. {
  51. snd_midi_event_t *codec;
  52. jack_ringbuffer_t *new_ports;
  53. a2j_port_hash_t port_hash;
  54. struct list_head list;
  55. };
  56. struct a2j
  57. {
  58. jack_client_t * jack_client;
  59. snd_seq_t *seq;
  60. pthread_t alsa_input_thread;
  61. pthread_t alsa_output_thread;
  62. int client_id;
  63. int port_id;
  64. int queue;
  65. bool freewheeling;
  66. bool running;
  67. bool finishing;
  68. jack_ringbuffer_t* port_add; // snd_seq_addr_t
  69. jack_ringbuffer_t* port_del; // struct a2j_port*
  70. jack_ringbuffer_t* outbound_events; // struct a2j_delivery_event
  71. jack_nframes_t cycle_start;
  72. sem_t output_semaphore;
  73. struct a2j_stream stream[2];
  74. };
  75. #define NSEC_PER_SEC ((int64_t)1000*1000*1000)
  76. struct a2j_alsa_midi_event
  77. {
  78. int64_t time;
  79. int size;
  80. };
  81. #define MAX_JACKMIDI_EV_SIZE 16
  82. struct a2j_delivery_event
  83. {
  84. struct list_head siblings;
  85. /* a jack MIDI event, plus the port its destined for: everything
  86. the ALSA output thread needs to deliver the event. time is
  87. part of the jack_event.
  88. */
  89. jack_midi_event_t jack_event;
  90. jack_nframes_t time; /* realtime, not offset time */
  91. struct a2j_port* port;
  92. char midistring[MAX_JACKMIDI_EV_SIZE];
  93. };
  94. void a2j_error (const char* fmt, ...);
  95. #define A2J_DEBUG
  96. /*#undef A2J_DEBUG*/
  97. #ifdef A2J_DEBUG
  98. extern bool a2j_do_debug;
  99. extern void _a2j_debug (const char* fmt, ...);
  100. #define a2j_debug(fmt, ...) if (a2j_do_debug) { _a2j_debug ((fmt), ##__VA_ARGS__); }
  101. #else
  102. #define a2j_debug(fmt,...)
  103. #endif
  104. #endif /* __jack_alsa_midi_h__ */