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.

275 lines
6.7KB

  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. enum JackPortFlags {
  39. JackPortIsInput = 0x01,
  40. JackPortIsOutput = 0x02,
  41. JackPortIsPhysical = 0x04,
  42. JackPortCanMonitor = 0x08,
  43. JackPortIsTerminal = 0x10
  44. };
  45. enum JackOptions {
  46. JackNullOption = 0x00,
  47. JackNoStartServer = 0x01,
  48. JackUseExactName = 0x02,
  49. JackServerName = 0x04,
  50. JackLoadName = 0x08,
  51. JackLoadInit = 0x10,
  52. JackSessionID = 0x20
  53. };
  54. enum JackStatus {
  55. JackFailure = 0x0001,
  56. JackInvalidOption = 0x0002,
  57. JackNameNotUnique = 0x0004,
  58. JackServerStarted = 0x0008,
  59. JackServerFailed = 0x0010,
  60. JackServerError = 0x0020,
  61. JackNoSuchClient = 0x0040,
  62. JackLoadFailure = 0x0080,
  63. JackInitFailure = 0x0100,
  64. JackShmFailure = 0x0200,
  65. JackVersionError = 0x0400,
  66. JackBackendError = 0x0800,
  67. JackClientZombie = 0x1000
  68. };
  69. typedef enum JackOptions jack_options_t;
  70. typedef enum JackStatus jack_status_t;
  71. /* ------------------------------------------------------------------------------------------------------------
  72. * Structs */
  73. typedef struct {
  74. bool registered;
  75. uint flags;
  76. void* buffer;
  77. } jack_port_t;
  78. typedef struct {
  79. // current state
  80. bool active;
  81. const char* clientName;
  82. jack_nframes_t bufferSize;
  83. jack_nframes_t sampleRate;
  84. // callbacks
  85. jack_process_callback processCallback;
  86. void* processPtr;
  87. // ports
  88. jack_port_t portsAudioIn[8];
  89. jack_port_t portsAudioOut[8];
  90. } jack_client_t;
  91. /* ------------------------------------------------------------------------------------------------------------
  92. * Client functions */
  93. /*
  94. * NOTE: This function purposefully returns NULL, as we *do not* want global variables.
  95. * The client pointer is passed into the plugin code directly.
  96. */
  97. static inline
  98. jack_client_t* jack_client_open(const char* clientname, jack_options_t options, jack_status_t* status, ...)
  99. {
  100. if (status != NULL)
  101. *status = JackFailure;
  102. return NULL;
  103. // unused
  104. (void)clientname;
  105. (void)options;
  106. }
  107. static inline
  108. int jack_client_close(jack_client_t* client)
  109. {
  110. memset(client, 0, sizeof(jack_client_t));
  111. return 0;
  112. }
  113. /* ------------------------------------------------------------------------------------------------------------
  114. * Callback functions */
  115. static inline
  116. int jack_on_shutdown(jack_client_t* client, jack_shutdown_callback callback, void* ptr)
  117. {
  118. return 0;
  119. // unused
  120. (void)client;
  121. (void)callback;
  122. (void)ptr;
  123. }
  124. static inline
  125. int jack_set_process_callback(jack_client_t* client, jack_process_callback callback, void* ptr)
  126. {
  127. client->processCallback = callback;
  128. client->processPtr = ptr;
  129. return 0;
  130. }
  131. /* ------------------------------------------------------------------------------------------------------------
  132. * Port functions */
  133. static inline
  134. jack_port_t* jack_port_register(jack_client_t* client, const char* name, const char* type, ulong flags, ulong buffersize)
  135. {
  136. jack_port_t* const ports = (flags & JackPortIsInput) ? client->portsAudioIn : client->portsAudioOut;
  137. for (int i=0; i<8; ++i)
  138. {
  139. jack_port_t* const port = &ports[i];
  140. if (port->registered)
  141. continue;
  142. port->registered = true;
  143. port->flags = flags;
  144. port->buffer = NULL;
  145. return port;
  146. }
  147. return NULL;
  148. // unused
  149. (void)name;
  150. (void)type;
  151. (void)buffersize;
  152. }
  153. static inline
  154. int jack_port_unregister(jack_client_t* client, jack_port_t* port)
  155. {
  156. jack_port_t* const ports = (port->flags & JackPortIsInput) ? client->portsAudioIn : client->portsAudioOut;
  157. for (int i=0; i<8; ++i)
  158. {
  159. if (&ports[i] == port)
  160. {
  161. port->registered = false;
  162. port->flags = 0x0;
  163. port->buffer = NULL;
  164. return 0;
  165. }
  166. }
  167. return 1;
  168. }
  169. static inline
  170. void* jack_port_get_buffer(const jack_port_t* port, jack_nframes_t nframes)
  171. {
  172. return port->buffer;
  173. // unused
  174. (void)nframes;
  175. }
  176. /* ------------------------------------------------------------------------------------------------------------
  177. * [De/]Activate */
  178. static inline
  179. int jack_activate(jack_client_t* client)
  180. {
  181. client->active = true;
  182. return 0;
  183. }
  184. static inline
  185. int jack_deactivate(jack_client_t* client)
  186. {
  187. client->active = false;
  188. return 0;
  189. }
  190. /* ------------------------------------------------------------------------------------------------------------
  191. * Get data functions */
  192. static inline
  193. const char* jack_get_client_name(const jack_client_t* client)
  194. {
  195. return client->clientName;
  196. }
  197. static inline
  198. jack_nframes_t jack_get_buffer_size(const jack_client_t* client)
  199. {
  200. return client->bufferSize;
  201. }
  202. static inline
  203. jack_nframes_t jack_get_sample_rate(const jack_client_t* client)
  204. {
  205. return client->sampleRate;
  206. }
  207. /* ------------------------------------------------------------------------------------------------------------
  208. * Misc */
  209. static inline
  210. pthread_t jack_client_thread_id(const jack_client_t* client)
  211. {
  212. return pthread_self();
  213. // unused
  214. (void)client;
  215. }
  216. /* ------------------------------------------------------------------------------------------------------------ */
  217. #ifdef __cplusplus
  218. } /* extern "C" */
  219. #endif
  220. #endif /* CARLA_NATIVE_JACK_H_INCLUDED */