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.

216 lines
5.3KB

  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. #ifdef __cplusplus
  21. extern "C" {
  22. #endif
  23. /* ------------------------------------------------------------------------------------------------------------
  24. * Macros */
  25. #define JACK_DEFAULT_AUDIO_TYPE "audio"
  26. #define JACK_DEFAULT_MIDI_TYPE "midi"
  27. /* ------------------------------------------------------------------------------------------------------------
  28. * Basic types */
  29. typedef float jack_default_audio_sample_t;
  30. typedef uint32_t jack_nframes_t;
  31. typedef void (*jack_shutdown_callback)(void* ptr);
  32. typedef int (*jack_process_callback)(jack_nframes_t nframes, void* ptr);
  33. /* ------------------------------------------------------------------------------------------------------------
  34. * Enums */
  35. typedef enum {
  36. JackPortIsInput = 1 << 0,
  37. JackPortIsOutput = 1 << 1
  38. } jack___t;
  39. typedef enum {
  40. JackNoStartServer = 0,
  41. JackServerName
  42. } jack_options_t;
  43. typedef enum {
  44. JackNoError = 0x0
  45. } jack_status_t;
  46. /* ------------------------------------------------------------------------------------------------------------
  47. * Structs */
  48. typedef struct {
  49. bool isInput;
  50. void* buffer;
  51. } jack_port_t;
  52. typedef struct {
  53. // current state
  54. bool active;
  55. const char* clientName;
  56. jack_nframes_t bufferSize;
  57. jack_nframes_t sampleRate;
  58. // callbacks
  59. jack_process_callback processCallback;
  60. void* processPtr;
  61. // ports
  62. jack_port_t* portsAudioIn[8];
  63. jack_port_t* portsAudioOut[8];
  64. } jack_client_t;
  65. /* ------------------------------------------------------------------------------------------------------------
  66. * Client functions, defined in the plugin code */
  67. jack_client_t* jack_client_open(const char* clientname, jack_options_t options, jack_status_t* status, ...);
  68. int jack_client_close(jack_client_t* client);
  69. /* ------------------------------------------------------------------------------------------------------------
  70. * Callback functions */
  71. static inline
  72. int jack_on_shutdown(jack_client_t* client, jack_shutdown_callback callback, void* ptr)
  73. {
  74. return 0;
  75. // unused
  76. (void)client;
  77. (void)callback;
  78. (void)ptr;
  79. }
  80. static inline
  81. int jack_set_process_callback(jack_client_t* client, jack_process_callback callback, void* ptr)
  82. {
  83. client->processCallback = callback;
  84. client->processPtr = ptr;
  85. return 0;
  86. }
  87. /* ------------------------------------------------------------------------------------------------------------
  88. * Port functions */
  89. static inline
  90. jack_port_t* jack_port_register(jack_client_t* client, const char* name, const char* type, jack___t hints, jack_nframes_t buffersize)
  91. {
  92. jack_port_t** const ports = (hints & JackPortIsInput) ? client->portsAudioIn : client->portsAudioOut;
  93. for (int i=0; i<8; ++i)
  94. {
  95. if (ports[i] != NULL)
  96. continue;
  97. jack_port_t* const port = (jack_port_t*)calloc(1, sizeof(jack_port_t));
  98. if (port == NULL)
  99. return NULL;
  100. port->isInput = (hints & JackPortIsInput);
  101. ports[i] = port;
  102. return port;
  103. }
  104. return NULL;
  105. // unused
  106. (void)name;
  107. (void)type;
  108. (void)buffersize;
  109. }
  110. static inline
  111. int jack_port_unregister(jack_client_t* client, jack_port_t* port)
  112. {
  113. jack_port_t** const ports = port->isInput ? client->portsAudioIn : client->portsAudioOut;
  114. for (int i=0; i<8; ++i)
  115. {
  116. if (ports[i] == port)
  117. {
  118. ports[i] = nullptr;
  119. return 0;
  120. }
  121. }
  122. return 1;
  123. }
  124. static inline
  125. void* jack_port_get_buffer(const jack_port_t* port, jack_nframes_t nframes)
  126. {
  127. return port->buffer;
  128. // unused
  129. (void)nframes;
  130. }
  131. /* ------------------------------------------------------------------------------------------------------------
  132. * [De/]Activate */
  133. static inline
  134. int jack_activate(jack_client_t* client)
  135. {
  136. client->active = true;
  137. return 0;
  138. }
  139. static inline
  140. int jack_deactivate(jack_client_t* client)
  141. {
  142. client->active = false;
  143. return 0;
  144. }
  145. /* ------------------------------------------------------------------------------------------------------------
  146. * Get data functions */
  147. static inline
  148. const char* jack_get_client_name(const jack_client_t* client)
  149. {
  150. return client->clientName;
  151. }
  152. static inline
  153. jack_nframes_t jack_get_buffer_size(const jack_client_t* client)
  154. {
  155. return client->bufferSize;
  156. }
  157. static inline
  158. jack_nframes_t jack_get_sample_rate(const jack_client_t* client)
  159. {
  160. return client->sampleRate;
  161. }
  162. /* ------------------------------------------------------------------------------------------------------------ */
  163. #ifdef __cplusplus
  164. } /* extern "C" */
  165. #endif
  166. #endif /* CARLA_NATIVE_JACK_H_INCLUDED */