Audio plugin host https://kx.studio/carla
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.

250 lines
5.9KB

  1. /*
  2. * Carla Native Plugin API (JACK Compatibility header)
  3. * Copyright (C) 2015 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #ifndef CARLA_NATIVE_JACK_H_INCLUDED
  18. #define CARLA_NATIVE_JACK_H_INCLUDED
  19. #include "CarlaNative.h"
  20. #include <pthread.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #ifdef __cplusplus
  24. extern "C" {
  25. #endif
  26. /* ------------------------------------------------------------------------------------------------------------
  27. * Macros */
  28. #define JACK_DEFAULT_AUDIO_TYPE "audio"
  29. #define JACK_DEFAULT_MIDI_TYPE "midi"
  30. /* ------------------------------------------------------------------------------------------------------------
  31. * Basic types */
  32. typedef float jack_default_audio_sample_t;
  33. typedef uint32_t jack_nframes_t;
  34. typedef void (*jack_shutdown_callback)(void* ptr);
  35. typedef int (*jack_process_callback)(jack_nframes_t nframes, void* ptr);
  36. /* ------------------------------------------------------------------------------------------------------------
  37. * Enums */
  38. typedef enum {
  39. JackPortIsInput = 1 << 0,
  40. JackPortIsOutput = 1 << 1
  41. } jack___t;
  42. typedef enum {
  43. JackNoStartServer = 0,
  44. JackServerName
  45. } jack_options_t;
  46. typedef enum {
  47. JackNoError = 0x0
  48. } jack_status_t;
  49. /* ------------------------------------------------------------------------------------------------------------
  50. * Structs */
  51. typedef struct {
  52. bool isInput;
  53. void* buffer;
  54. } jack_port_t;
  55. typedef struct {
  56. // current state
  57. bool active;
  58. const char* clientName;
  59. jack_nframes_t bufferSize;
  60. jack_nframes_t sampleRate;
  61. // callbacks
  62. jack_process_callback processCallback;
  63. void* processPtr;
  64. // ports
  65. jack_port_t* portsAudioIn[8];
  66. jack_port_t* portsAudioOut[8];
  67. } jack_client_t;
  68. /* ------------------------------------------------------------------------------------------------------------
  69. * Client functions */
  70. extern jack_client_t* gLastJackClient;
  71. static inline
  72. jack_client_t* jack_client_open(const char* clientname, jack_options_t options, jack_status_t* status, ...)
  73. {
  74. if (status != NULL)
  75. *status = JackNoError;
  76. return gLastJackClient;
  77. // unused
  78. (void)clientname;
  79. (void)options;
  80. }
  81. static inline
  82. int jack_client_close(jack_client_t* client)
  83. {
  84. memset(client, 0, sizeof(jack_client_t));
  85. return 0;
  86. }
  87. /* ------------------------------------------------------------------------------------------------------------
  88. * Callback functions */
  89. static inline
  90. int jack_on_shutdown(jack_client_t* client, jack_shutdown_callback callback, void* ptr)
  91. {
  92. return 0;
  93. // unused
  94. (void)client;
  95. (void)callback;
  96. (void)ptr;
  97. }
  98. static inline
  99. int jack_set_process_callback(jack_client_t* client, jack_process_callback callback, void* ptr)
  100. {
  101. client->processCallback = callback;
  102. client->processPtr = ptr;
  103. return 0;
  104. }
  105. /* ------------------------------------------------------------------------------------------------------------
  106. * Port functions */
  107. static inline
  108. jack_port_t* jack_port_register(jack_client_t* client, const char* name, const char* type, jack___t hints, jack_nframes_t buffersize)
  109. {
  110. jack_port_t** const ports = (hints & JackPortIsInput) ? client->portsAudioIn : client->portsAudioOut;
  111. for (int i=0; i<8; ++i)
  112. {
  113. if (ports[i] != NULL)
  114. continue;
  115. jack_port_t* const port = (jack_port_t*)calloc(1, sizeof(jack_port_t));
  116. if (port == NULL)
  117. return NULL;
  118. port->isInput = (hints & JackPortIsInput);
  119. ports[i] = port;
  120. return port;
  121. }
  122. return NULL;
  123. // unused
  124. (void)name;
  125. (void)type;
  126. (void)buffersize;
  127. }
  128. static inline
  129. int jack_port_unregister(jack_client_t* client, jack_port_t* port)
  130. {
  131. jack_port_t** const ports = port->isInput ? client->portsAudioIn : client->portsAudioOut;
  132. for (int i=0; i<8; ++i)
  133. {
  134. if (ports[i] == port)
  135. {
  136. ports[i] = NULL;
  137. return 0;
  138. }
  139. }
  140. return 1;
  141. }
  142. static inline
  143. void* jack_port_get_buffer(const jack_port_t* port, jack_nframes_t nframes)
  144. {
  145. return port->buffer;
  146. // unused
  147. (void)nframes;
  148. }
  149. /* ------------------------------------------------------------------------------------------------------------
  150. * [De/]Activate */
  151. static inline
  152. int jack_activate(jack_client_t* client)
  153. {
  154. client->active = true;
  155. return 0;
  156. }
  157. static inline
  158. int jack_deactivate(jack_client_t* client)
  159. {
  160. client->active = false;
  161. return 0;
  162. }
  163. /* ------------------------------------------------------------------------------------------------------------
  164. * Get data functions */
  165. static inline
  166. const char* jack_get_client_name(const jack_client_t* client)
  167. {
  168. return client->clientName;
  169. }
  170. static inline
  171. jack_nframes_t jack_get_buffer_size(const jack_client_t* client)
  172. {
  173. return client->bufferSize;
  174. }
  175. static inline
  176. jack_nframes_t jack_get_sample_rate(const jack_client_t* client)
  177. {
  178. return client->sampleRate;
  179. }
  180. /* ------------------------------------------------------------------------------------------------------------
  181. * Misc */
  182. static inline
  183. pthread_t jack_client_thread_id(const jack_client_t* client)
  184. {
  185. return pthread_self();
  186. // unused
  187. (void)client;
  188. }
  189. /* ------------------------------------------------------------------------------------------------------------ */
  190. #ifdef __cplusplus
  191. } /* extern "C" */
  192. #endif
  193. #endif /* CARLA_NATIVE_JACK_H_INCLUDED */