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.2KB

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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 <errno.h>
  21. #include <pthread.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #ifdef __cplusplus
  25. extern "C" {
  26. #endif
  27. /* ------------------------------------------------------------------------------------------------------------
  28. * Macros */
  29. #define JACK_DEFAULT_AUDIO_TYPE "32 bit float mono audio"
  30. #define JACK_DEFAULT_MIDI_TYPE "8 bit raw midi"
  31. /* ------------------------------------------------------------------------------------------------------------
  32. * Basic types */
  33. typedef float jack_default_audio_sample_t;
  34. typedef uint8_t jack_midi_data_t;
  35. typedef uint32_t jack_nframes_t;
  36. typedef void (*jack_shutdown_callback)(void* ptr);
  37. typedef int (*jack_process_callback)(jack_nframes_t nframes, void* ptr);
  38. /*
  39. * Helper struct for midi events.
  40. */
  41. typedef struct {
  42. uint32_t count;
  43. NativeMidiEvent* events;
  44. } NativeMidiEventsBuffer;
  45. /* ------------------------------------------------------------------------------------------------------------
  46. * Enums */
  47. enum JackPortFlags {
  48. JackPortIsInput = 0x01,
  49. JackPortIsOutput = 0x02,
  50. JackPortIsPhysical = 0x04,
  51. JackPortCanMonitor = 0x08,
  52. JackPortIsTerminal = 0x10
  53. };
  54. enum JackOptions {
  55. JackNullOption = 0x00,
  56. JackNoStartServer = 0x01,
  57. JackUseExactName = 0x02,
  58. JackServerName = 0x04,
  59. JackLoadName = 0x08,
  60. JackLoadInit = 0x10,
  61. JackSessionID = 0x20
  62. };
  63. enum JackStatus {
  64. JackFailure = 0x0001,
  65. JackInvalidOption = 0x0002,
  66. JackNameNotUnique = 0x0004,
  67. JackServerStarted = 0x0008,
  68. JackServerFailed = 0x0010,
  69. JackServerError = 0x0020,
  70. JackNoSuchClient = 0x0040,
  71. JackLoadFailure = 0x0080,
  72. JackInitFailure = 0x0100,
  73. JackShmFailure = 0x0200,
  74. JackVersionError = 0x0400,
  75. JackBackendError = 0x0800,
  76. JackClientZombie = 0x1000
  77. };
  78. typedef enum JackOptions jack_options_t;
  79. typedef enum JackStatus jack_status_t;
  80. /* ------------------------------------------------------------------------------------------------------------
  81. * Structs */
  82. typedef struct {
  83. bool registered;
  84. bool isAudio;
  85. uint flags;
  86. union {
  87. void* audio;
  88. NativeMidiEventsBuffer midi;
  89. } buffer;
  90. } jack_port_t;
  91. typedef struct {
  92. // current state
  93. bool active;
  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. // keep bufsize and srate
  130. const jack_nframes_t bufferSize = client->bufferSize;
  131. const jack_nframes_t sampleRate = client->sampleRate;
  132. memset(client, 0, sizeof(jack_client_t));
  133. client->bufferSize = bufferSize;
  134. client->sampleRate = sampleRate;
  135. return 0;
  136. }
  137. /* ------------------------------------------------------------------------------------------------------------
  138. * Callback functions */
  139. static inline
  140. int jack_on_shutdown(jack_client_t* client, jack_shutdown_callback callback, void* ptr)
  141. {
  142. return 0;
  143. // unused
  144. (void)client;
  145. (void)callback;
  146. (void)ptr;
  147. }
  148. static inline
  149. int jack_set_process_callback(jack_client_t* client, jack_process_callback callback, void* ptr)
  150. {
  151. client->processCallback = callback;
  152. client->processPtr = ptr;
  153. return 0;
  154. }
  155. /* ------------------------------------------------------------------------------------------------------------
  156. * Port functions */
  157. static inline
  158. jack_port_t* jack_port_register(jack_client_t* client, const char* name, const char* type, ulong flags, ulong buffersize)
  159. {
  160. const bool isAudio = strcmp(type, JACK_DEFAULT_AUDIO_TYPE) == 0;
  161. const bool isMIDI = strcmp(type, JACK_DEFAULT_MIDI_TYPE ) == 0;
  162. if (! (isAudio || isMIDI))
  163. return NULL;
  164. jack_port_t* ports;
  165. if (isAudio)
  166. ports = (flags & JackPortIsInput) ? client->portsAudioIn : client->portsAudioOut;
  167. else
  168. ports = (flags & JackPortIsInput) ? client->portsMidiIn : client->portsMidiOut;
  169. for (int i=0; i<16; ++i)
  170. {
  171. jack_port_t* const port = &ports[i];
  172. if (port->registered)
  173. continue;
  174. memset(port, 0, sizeof(jack_port_t));
  175. port->registered = true;
  176. port->isAudio = isAudio;
  177. port->flags = flags;
  178. return port;
  179. }
  180. return NULL;
  181. // unused
  182. (void)name;
  183. (void)type;
  184. (void)buffersize;
  185. }
  186. static inline
  187. int jack_port_unregister(jack_client_t* client, jack_port_t* port)
  188. {
  189. jack_port_t* const ports = (port->flags & JackPortIsInput) ? client->portsAudioIn : client->portsAudioOut;
  190. for (int i=0; i<8; ++i)
  191. {
  192. if (&ports[i] == port)
  193. {
  194. memset(port, 0, sizeof(jack_port_t));
  195. return 0;
  196. }
  197. }
  198. return 1;
  199. }
  200. static inline
  201. void* jack_port_get_buffer(jack_port_t* port, jack_nframes_t nframes)
  202. {
  203. return port->isAudio ? port->buffer.audio : &port->buffer.midi;
  204. // unused
  205. (void)nframes;
  206. }
  207. /* ------------------------------------------------------------------------------------------------------------
  208. * MIDI functions */
  209. static inline
  210. uint32_t jack_midi_get_event_count(void* port_buffer)
  211. {
  212. NativeMidiEventsBuffer* const midi_buffer = (NativeMidiEventsBuffer*)port_buffer;
  213. return midi_buffer->count;
  214. }
  215. static inline
  216. int jack_midi_event_get(jack_midi_event_t* event, void* port_buffer, uint32_t event_index)
  217. {
  218. NativeMidiEventsBuffer* const midi_buffer = (NativeMidiEventsBuffer*)port_buffer;
  219. if (midi_buffer->count == 0)
  220. return ENODATA;
  221. if (event_index >= midi_buffer->count)
  222. return 1; // FIXME
  223. NativeMidiEvent* const midiEvent = &midi_buffer->events[event_index];
  224. event->time = midiEvent->time;
  225. event->size = midiEvent->size;
  226. event->buffer = midiEvent->data;
  227. return 0;
  228. }
  229. /* ------------------------------------------------------------------------------------------------------------
  230. * [De/]Activate */
  231. static inline
  232. int jack_activate(jack_client_t* client)
  233. {
  234. client->active = true;
  235. return 0;
  236. }
  237. static inline
  238. int jack_deactivate(jack_client_t* client)
  239. {
  240. client->active = false;
  241. return 0;
  242. }
  243. /* ------------------------------------------------------------------------------------------------------------
  244. * Get data functions */
  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. #ifdef __cplusplus
  257. } /* extern "C" */
  258. #endif
  259. #endif /* CARLA_NATIVE_JACK_H_INCLUDED */