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.

139 lines
3.4KB

  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. {
  41. struct a2j_port * next; /* hash - jack */
  42. struct list_head siblings; /* list - main loop */
  43. struct alsa_midi_driver * driver_ptr;
  44. bool is_dead;
  45. char name[64];
  46. snd_seq_addr_t remote;
  47. jack_port_t * jack_port;
  48. jack_ringbuffer_t * inbound_events; // alsa_midi_event_t + data
  49. int64_t last_out_time;
  50. void * jack_buf;
  51. };
  52. struct a2j_stream
  53. {
  54. snd_midi_event_t *codec;
  55. jack_ringbuffer_t *new_ports;
  56. a2j_port_hash_t port_hash;
  57. struct list_head list;
  58. };
  59. typedef struct alsa_midi_driver
  60. {
  61. JACK_DRIVER_DECL;
  62. jack_client_t * jack_client;
  63. snd_seq_t *seq;
  64. pthread_t alsa_input_thread;
  65. pthread_t alsa_output_thread;
  66. int client_id;
  67. int port_id;
  68. int queue;
  69. bool freewheeling;
  70. bool running;
  71. bool finishing;
  72. jack_ringbuffer_t* port_del; // struct a2j_port*
  73. jack_ringbuffer_t* outbound_events; // struct a2j_delivery_event
  74. jack_nframes_t cycle_start;
  75. sem_t output_semaphore;
  76. struct a2j_stream stream[2];
  77. } alsa_midi_driver_t;
  78. #define NSEC_PER_SEC ((int64_t)1000*1000*1000)
  79. struct a2j_alsa_midi_event
  80. {
  81. int64_t time;
  82. int size;
  83. };
  84. #define MAX_JACKMIDI_EV_SIZE 64
  85. struct a2j_delivery_event
  86. {
  87. struct list_head siblings;
  88. /* a jack MIDI event, plus the port its destined for: everything
  89. the ALSA output thread needs to deliver the event. time is
  90. part of the jack_event.
  91. */
  92. jack_midi_event_t jack_event;
  93. jack_nframes_t time; /* realtime, not offset time */
  94. struct a2j_port* port;
  95. char midistring[MAX_JACKMIDI_EV_SIZE];
  96. };
  97. void a2j_error (const char* fmt, ...);
  98. #define A2J_DEBUG
  99. /*#undef A2J_DEBUG*/
  100. #ifdef A2J_DEBUG
  101. extern bool a2j_do_debug;
  102. extern void _a2j_debug (const char* fmt, ...);
  103. #define a2j_debug(fmt, ...) if (a2j_do_debug) { _a2j_debug ((fmt), ##__VA_ARGS__); }
  104. #else
  105. #define a2j_debug(fmt,...)
  106. #endif
  107. #endif /* __jack_alsa_midi_h__ */