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

134 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. #include <jack/ringbuffer.h>
  27. #include "driver.h"
  28. #include "list.h"
  29. #define JACK_INVALID_PORT NULL
  30. #define MAX_PORTS 2048
  31. #define MAX_EVENT_SIZE 1024
  32. #define PORT_HASH_BITS 4
  33. #define PORT_HASH_SIZE (1 << PORT_HASH_BITS)
  34. /* Beside enum use, these are indeces for (struct a2j).stream array */
  35. #define A2J_PORT_CAPTURE 0 // ALSA playback port -> JACK capture port
  36. #define A2J_PORT_PLAYBACK 1 // JACK playback port -> ALSA capture port
  37. typedef struct a2j_port * a2j_port_hash_t[PORT_HASH_SIZE];
  38. struct alsa_midi_driver;
  39. struct a2j_port {
  40. struct a2j_port * next; /* hash - jack */
  41. struct list_head siblings; /* list - main loop */
  42. struct alsa_midi_driver * driver_ptr;
  43. bool is_dead;
  44. char name[64];
  45. snd_seq_addr_t remote;
  46. jack_port_t * jack_port;
  47. jack_ringbuffer_t * inbound_events; // alsa_midi_event_t + data
  48. int64_t last_out_time;
  49. void * jack_buf;
  50. };
  51. struct a2j_stream {
  52. snd_midi_event_t *codec;
  53. jack_ringbuffer_t *new_ports;
  54. a2j_port_hash_t port_hash;
  55. struct list_head list;
  56. };
  57. typedef struct alsa_midi_driver {
  58. JACK_DRIVER_DECL;
  59. jack_client_t * jack_client;
  60. snd_seq_t *seq;
  61. pthread_t alsa_input_thread;
  62. pthread_t alsa_output_thread;
  63. int client_id;
  64. int port_id;
  65. int queue;
  66. bool freewheeling;
  67. bool running;
  68. bool finishing;
  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. } alsa_midi_driver_t;
  75. #define NSEC_PER_SEC ((int64_t)1000 * 1000 * 1000)
  76. struct a2j_alsa_midi_event {
  77. int64_t time;
  78. int size;
  79. };
  80. #define MAX_JACKMIDI_EV_SIZE 64
  81. struct a2j_delivery_event {
  82. struct list_head siblings;
  83. /* a jack MIDI event, plus the port its destined for: everything
  84. the ALSA output thread needs to deliver the event. time is
  85. part of the jack_event.
  86. */
  87. jack_midi_event_t jack_event;
  88. jack_nframes_t time; /* realtime, not offset time */
  89. struct a2j_port* port;
  90. char midistring[MAX_JACKMIDI_EV_SIZE];
  91. };
  92. void a2j_error(const char* fmt, ...);
  93. #define A2J_DEBUG
  94. /*#undef A2J_DEBUG*/
  95. #ifdef A2J_DEBUG
  96. extern bool a2j_do_debug;
  97. extern void _a2j_debug(const char* fmt, ...);
  98. #define a2j_debug(fmt, ...) if (a2j_do_debug) { _a2j_debug ((fmt), ## __VA_ARGS__); }
  99. #else
  100. #define a2j_debug(fmt, ...)
  101. #endif
  102. #endif /* __jack_alsa_midi_h__ */