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.

CarlaNativeJack.h 8.3KB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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 "32 bit float mono audio"
  29. #define JACK_DEFAULT_MIDI_TYPE "8 bit raw midi"
  30. /* ------------------------------------------------------------------------------------------------------------
  31. * Basic types */
  32. typedef float jack_default_audio_sample_t;
  33. typedef uint8_t jack_midi_data_t;
  34. typedef uint32_t jack_nframes_t;
  35. typedef void (*jack_shutdown_callback)(void* ptr);
  36. typedef int (*jack_process_callback)(jack_nframes_t nframes, void* ptr);
  37. /*
  38. * Helper struct for midi events.
  39. */
  40. typedef struct {
  41. uint32_t count;
  42. NativeMidiEvent* events;
  43. } NativeMidiEventsBuffer;
  44. /* ------------------------------------------------------------------------------------------------------------
  45. * Enums */
  46. enum JackPortFlags {
  47. JackPortIsInput = 0x01,
  48. JackPortIsOutput = 0x02,
  49. JackPortIsPhysical = 0x04,
  50. JackPortCanMonitor = 0x08,
  51. JackPortIsTerminal = 0x10
  52. };
  53. enum JackOptions {
  54. JackNullOption = 0x00,
  55. JackNoStartServer = 0x01,
  56. JackUseExactName = 0x02,
  57. JackServerName = 0x04,
  58. JackLoadName = 0x08,
  59. JackLoadInit = 0x10,
  60. JackSessionID = 0x20
  61. };
  62. enum JackStatus {
  63. JackFailure = 0x0001,
  64. JackInvalidOption = 0x0002,
  65. JackNameNotUnique = 0x0004,
  66. JackServerStarted = 0x0008,
  67. JackServerFailed = 0x0010,
  68. JackServerError = 0x0020,
  69. JackNoSuchClient = 0x0040,
  70. JackLoadFailure = 0x0080,
  71. JackInitFailure = 0x0100,
  72. JackShmFailure = 0x0200,
  73. JackVersionError = 0x0400,
  74. JackBackendError = 0x0800,
  75. JackClientZombie = 0x1000
  76. };
  77. typedef enum JackOptions jack_options_t;
  78. typedef enum JackStatus jack_status_t;
  79. /* ------------------------------------------------------------------------------------------------------------
  80. * Structs */
  81. typedef struct {
  82. bool registered;
  83. bool isAudio;
  84. uint flags;
  85. union {
  86. void* audio;
  87. NativeMidiEventsBuffer midi;
  88. } buffer;
  89. } jack_port_t;
  90. typedef struct {
  91. // current state
  92. bool active;
  93. const char* clientName;
  94. jack_nframes_t bufferSize;
  95. jack_nframes_t sampleRate;
  96. // callbacks
  97. jack_process_callback processCallback;
  98. void* processPtr;
  99. // ports
  100. jack_port_t portsAudioIn [16];
  101. jack_port_t portsAudioOut[16];
  102. jack_port_t portsMidiIn [16];
  103. jack_port_t portsMidiOut [16];
  104. } jack_client_t;
  105. typedef struct {
  106. uint32_t time;
  107. uint32_t size;
  108. uint8_t* buffer;
  109. } jack_midi_event_t;
  110. /* ------------------------------------------------------------------------------------------------------------
  111. * Client functions */
  112. /*
  113. * NOTE: This function purposefully returns NULL, as we *do not* want global variables.
  114. * The client pointer is passed into the plugin code directly.
  115. */
  116. static inline
  117. jack_client_t* jack_client_open(const char* clientname, jack_options_t options, jack_status_t* status, ...)
  118. {
  119. if (status != NULL)
  120. *status = JackFailure;
  121. return NULL;
  122. // unused
  123. (void)clientname;
  124. (void)options;
  125. }
  126. static inline
  127. int jack_client_close(jack_client_t* client)
  128. {
  129. memset(client, 0, sizeof(jack_client_t));
  130. return 0;
  131. }
  132. /* ------------------------------------------------------------------------------------------------------------
  133. * Callback functions */
  134. static inline
  135. int jack_on_shutdown(jack_client_t* client, jack_shutdown_callback callback, void* ptr)
  136. {
  137. return 0;
  138. // unused
  139. (void)client;
  140. (void)callback;
  141. (void)ptr;
  142. }
  143. static inline
  144. int jack_set_process_callback(jack_client_t* client, jack_process_callback callback, void* ptr)
  145. {
  146. client->processCallback = callback;
  147. client->processPtr = ptr;
  148. return 0;
  149. }
  150. /* ------------------------------------------------------------------------------------------------------------
  151. * Port functions */
  152. static inline
  153. jack_port_t* jack_port_register(jack_client_t* client, const char* name, const char* type, ulong flags, ulong buffersize)
  154. {
  155. const bool isAudio = strcmp(type, JACK_DEFAULT_AUDIO_TYPE) == 0;
  156. const bool isMIDI = strcmp(type, JACK_DEFAULT_MIDI_TYPE ) == 0;
  157. if (! (isAudio || isMIDI))
  158. return NULL;
  159. jack_port_t* ports;
  160. if (isAudio)
  161. ports = (flags & JackPortIsInput) ? client->portsAudioIn : client->portsAudioOut;
  162. else
  163. ports = (flags & JackPortIsInput) ? client->portsMidiIn : client->portsMidiOut;
  164. for (int i=0; i<16; ++i)
  165. {
  166. jack_port_t* const port = &ports[i];
  167. if (port->registered)
  168. continue;
  169. memset(port, 0, sizeof(jack_port_t));
  170. port->registered = true;
  171. port->isAudio = isAudio;
  172. port->flags = flags;
  173. return port;
  174. }
  175. return NULL;
  176. // unused
  177. (void)name;
  178. (void)type;
  179. (void)buffersize;
  180. }
  181. static inline
  182. int jack_port_unregister(jack_client_t* client, jack_port_t* port)
  183. {
  184. jack_port_t* const ports = (port->flags & JackPortIsInput) ? client->portsAudioIn : client->portsAudioOut;
  185. for (int i=0; i<8; ++i)
  186. {
  187. if (&ports[i] == port)
  188. {
  189. memset(port, 0, sizeof(jack_port_t));
  190. return 0;
  191. }
  192. }
  193. return 1;
  194. }
  195. static inline
  196. void* jack_port_get_buffer(jack_port_t* port, jack_nframes_t nframes)
  197. {
  198. return port->isAudio ? port->buffer.audio : &port->buffer.midi;
  199. // unused
  200. (void)nframes;
  201. }
  202. /* ------------------------------------------------------------------------------------------------------------
  203. * MIDI functions */
  204. static inline
  205. uint32_t jack_midi_get_event_count(void* port_buffer)
  206. {
  207. NativeMidiEventsBuffer* const midi_buffer = (NativeMidiEventsBuffer*)port_buffer;
  208. return midi_buffer->count;
  209. }
  210. static inline
  211. int jack_midi_event_get(jack_midi_event_t* event, void* port_buffer, uint32_t event_index)
  212. {
  213. NativeMidiEventsBuffer* const midi_buffer = (NativeMidiEventsBuffer*)port_buffer;
  214. if (midi_buffer->count == 0)
  215. return ENODATA;
  216. if (event_index >= midi_buffer->count)
  217. return 1; // FIXME
  218. NativeMidiEvent* const midiEvent = &midi_buffer->events[event_index];
  219. event->time = midiEvent->time;
  220. event->size = midiEvent->size;
  221. event->buffer = midiEvent->data;
  222. return 0;
  223. }
  224. /* ------------------------------------------------------------------------------------------------------------
  225. * [De/]Activate */
  226. static inline
  227. int jack_activate(jack_client_t* client)
  228. {
  229. client->active = true;
  230. return 0;
  231. }
  232. static inline
  233. int jack_deactivate(jack_client_t* client)
  234. {
  235. client->active = false;
  236. return 0;
  237. }
  238. /* ------------------------------------------------------------------------------------------------------------
  239. * Get data functions */
  240. static inline
  241. const char* jack_get_client_name(const jack_client_t* client)
  242. {
  243. return client->clientName;
  244. }
  245. static inline
  246. jack_nframes_t jack_get_buffer_size(const jack_client_t* client)
  247. {
  248. return client->bufferSize;
  249. }
  250. static inline
  251. jack_nframes_t jack_get_sample_rate(const jack_client_t* client)
  252. {
  253. return client->sampleRate;
  254. }
  255. /* ------------------------------------------------------------------------------------------------------------
  256. * Misc */
  257. static inline
  258. pthread_t jack_client_thread_id(const jack_client_t* client)
  259. {
  260. return pthread_self();
  261. // unused
  262. (void)client;
  263. }
  264. /* ------------------------------------------------------------------------------------------------------------ */
  265. #ifdef __cplusplus
  266. } /* extern "C" */
  267. #endif
  268. #endif /* CARLA_NATIVE_JACK_H_INCLUDED */