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.

437 lines
12KB

  1. /*
  2. * Carla JACK API for external applications
  3. * Copyright (C) 2016-2018 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. #include "libjack.hpp"
  18. CARLA_BACKEND_USE_NAMESPACE
  19. // --------------------------------------------------------------------------------------------------------------------
  20. CARLA_EXPORT
  21. jack_port_t* jack_port_register(jack_client_t* client, const char* port_name, const char* port_type,
  22. unsigned long flags, unsigned long buffer_size)
  23. {
  24. carla_debug("%s(%p, %s, %s, 0x%lx, %lu)", __FUNCTION__, client, port_name, port_type, flags, buffer_size);
  25. JackClientState* const jclient = (JackClientState*)client;
  26. CARLA_SAFE_ASSERT_RETURN(jclient != nullptr, nullptr);
  27. CARLA_SAFE_ASSERT_RETURN(port_name != nullptr && port_name[0] != '\0', nullptr);
  28. CARLA_SAFE_ASSERT_RETURN(port_type != nullptr && port_type[0] != '\0', nullptr);
  29. const JackServerState& jserver(jclient->server);
  30. if (std::strcmp(port_type, JACK_DEFAULT_AUDIO_TYPE) == 0)
  31. {
  32. if (flags & JackPortIsInput)
  33. {
  34. const std::size_t index = jclient->audioIns.count();
  35. JackPortState* const port = new JackPortState(jclient->name, port_name, index, flags,
  36. false, false, index < jserver.numAudioIns);
  37. {
  38. const CarlaMutexLocker cms(jclient->mutex);
  39. jclient->audioIns.append(port);
  40. }
  41. return (jack_port_t*)port;
  42. }
  43. if (flags & JackPortIsOutput)
  44. {
  45. const std::size_t index = jclient->audioOuts.count();
  46. JackPortState* const port = new JackPortState(jclient->name, port_name, index, flags,
  47. false, false, index < jserver.numAudioOuts);
  48. {
  49. const CarlaMutexLocker cms(jclient->mutex);
  50. jclient->audioOuts.append(port);
  51. }
  52. return (jack_port_t*)port;
  53. }
  54. carla_stderr2("jack_port_register: invalid port flags '%x'", flags);
  55. return nullptr;
  56. }
  57. if (std::strcmp(port_type, JACK_DEFAULT_MIDI_TYPE) == 0)
  58. {
  59. if (flags & JackPortIsInput)
  60. {
  61. const std::size_t index = jclient->midiIns.count();
  62. JackPortState* const port = new JackPortState(jclient->name, port_name, index, flags,
  63. true, false, index < jserver.numMidiIns);
  64. {
  65. const CarlaMutexLocker cms(jclient->mutex);
  66. jclient->midiIns.append(port);
  67. }
  68. return (jack_port_t*)port;
  69. }
  70. if (flags & JackPortIsOutput)
  71. {
  72. const std::size_t index = jclient->midiOuts.count();
  73. JackPortState* const port = new JackPortState(jclient->name, port_name, index, flags,
  74. true, false, index < jserver.numMidiOuts);
  75. {
  76. const CarlaMutexLocker cms(jclient->mutex);
  77. jclient->midiOuts.append(port);
  78. }
  79. return (jack_port_t*)port;
  80. }
  81. carla_stderr2("jack_port_register: invalid port flags '%x'", flags);
  82. return nullptr;
  83. }
  84. carla_stderr2("jack_port_register: invalid port type '%s'", port_type);
  85. return nullptr;
  86. // unused
  87. (void)buffer_size;
  88. }
  89. CARLA_EXPORT
  90. int jack_port_unregister(jack_client_t* client, jack_port_t* port)
  91. {
  92. carla_debug("%s(%p, %p)", __FUNCTION__, client, port);
  93. JackClientState* const jclient = (JackClientState*)client;
  94. CARLA_SAFE_ASSERT_RETURN(jclient != nullptr, EINVAL);
  95. JackPortState* const jport = (JackPortState*)port;
  96. CARLA_SAFE_ASSERT_RETURN(jport != nullptr, EINVAL);
  97. CARLA_SAFE_ASSERT_RETURN(! jport->isSystem, EINVAL);
  98. {
  99. const CarlaMutexLocker cms(jclient->mutex);
  100. if (jport->isMidi)
  101. {
  102. if (jport->flags & JackPortIsInput)
  103. {
  104. CARLA_SAFE_ASSERT_RETURN(jclient->midiIns.removeOne(jport), ENOENT);
  105. return 0;
  106. }
  107. if (jport->flags & JackPortIsOutput)
  108. {
  109. CARLA_SAFE_ASSERT_RETURN(jclient->midiOuts.removeOne(jport), ENOENT);
  110. return 0;
  111. }
  112. }
  113. else
  114. {
  115. if (jport->flags & JackPortIsInput)
  116. {
  117. CARLA_SAFE_ASSERT_RETURN(jclient->audioIns.removeOne(jport), ENOENT);
  118. return 0;
  119. }
  120. if (jport->flags & JackPortIsOutput)
  121. {
  122. CARLA_SAFE_ASSERT_RETURN(jclient->audioOuts.removeOne(jport), ENOENT);
  123. return 0;
  124. }
  125. }
  126. }
  127. carla_stderr2("jack_port_register: invalid port '%s'", jport->name);
  128. return EINVAL;
  129. }
  130. CARLA_EXPORT
  131. void* jack_port_get_buffer(jack_port_t* port, jack_nframes_t)
  132. {
  133. JackPortState* const jport = (JackPortState*)port;
  134. CARLA_SAFE_ASSERT_RETURN(jport != nullptr, nullptr);
  135. return jport->buffer;
  136. }
  137. // --------------------------------------------------------------------------------------------------------------------
  138. CARLA_EXPORT
  139. jack_uuid_t jack_port_uuid(const jack_port_t* port)
  140. {
  141. carla_debug("%s(%p)", __FUNCTION__, port);
  142. JackPortState* const jport = (JackPortState*)port;
  143. CARLA_SAFE_ASSERT_RETURN(jport != nullptr, 0);
  144. return jport->uuid;
  145. }
  146. CARLA_EXPORT
  147. const char* jack_port_name(const jack_port_t* port)
  148. {
  149. carla_debug("%s(%p)", __FUNCTION__, port);
  150. JackPortState* const jport = (JackPortState*)port;
  151. CARLA_SAFE_ASSERT_RETURN(jport != nullptr, nullptr);
  152. return jport->fullname;
  153. }
  154. CARLA_EXPORT
  155. const char* jack_port_short_name(const jack_port_t* port)
  156. {
  157. carla_debug("%s(%p)", __FUNCTION__, port);
  158. JackPortState* const jport = (JackPortState*)port;
  159. CARLA_SAFE_ASSERT_RETURN(jport != nullptr, nullptr);
  160. return jport->name;
  161. }
  162. CARLA_EXPORT
  163. int jack_port_flags(const jack_port_t* port)
  164. {
  165. carla_debug("%s(%p)", __FUNCTION__, port);
  166. JackPortState* const jport = (JackPortState*)port;
  167. CARLA_SAFE_ASSERT_RETURN(jport != nullptr, 0);
  168. return jport->flags;
  169. }
  170. CARLA_EXPORT
  171. const char* jack_port_type(const jack_port_t* port)
  172. {
  173. carla_debug("%s(%p)", __FUNCTION__, port);
  174. JackPortState* const jport = (JackPortState*)port;
  175. CARLA_SAFE_ASSERT_RETURN(jport != nullptr, nullptr);
  176. static const char* const kAudioType = JACK_DEFAULT_AUDIO_TYPE;
  177. static const char* const kMidiType = JACK_DEFAULT_MIDI_TYPE;
  178. return jport->isMidi ? kMidiType : kAudioType;
  179. }
  180. CARLA_EXPORT
  181. uint32_t jack_port_type_id(const jack_port_t* port)
  182. {
  183. carla_debug("%s(%p)", __FUNCTION__, port);
  184. JackPortState* const jport = (JackPortState*)port;
  185. CARLA_SAFE_ASSERT_RETURN(jport != nullptr, 0);
  186. return jport->isMidi ? 1 : 0;
  187. }
  188. // --------------------------------------------------------------------------------------------------------------------
  189. CARLA_EXPORT
  190. int jack_port_is_mine(const jack_client_t*, const jack_port_t* port)
  191. {
  192. JackPortState* const jport = (JackPortState*)port;
  193. CARLA_SAFE_ASSERT_RETURN(jport != nullptr, 0);
  194. return jport->isSystem ? 0 : 1;
  195. }
  196. CARLA_EXPORT
  197. int jack_port_connected(const jack_port_t* port)
  198. {
  199. JackPortState* const jport = (JackPortState*)port;
  200. CARLA_SAFE_ASSERT_RETURN(jport != nullptr, 0);
  201. return jport->isConnected ? 1 : 0;
  202. }
  203. CARLA_EXPORT
  204. int jack_port_connected_to(const jack_port_t* port, const char* port_name)
  205. {
  206. carla_stderr2("%s(%p, %s) WIP", __FUNCTION__, port, port_name);
  207. JackPortState* const jport = (JackPortState*)port;
  208. CARLA_SAFE_ASSERT_RETURN(jport != nullptr, 0);
  209. if (! jport->isConnected)
  210. return 0;
  211. // TODO
  212. return 1;
  213. }
  214. // --------------------------------------------------------------------------------------------------------------------
  215. CARLA_EXPORT
  216. const char** jack_port_get_connections(const jack_port_t* port)
  217. {
  218. carla_stderr2("%s(%p)", __FUNCTION__, port);
  219. return nullptr;
  220. }
  221. CARLA_EXPORT
  222. const char** jack_port_get_all_connections(const jack_client_t* client, const jack_port_t* port)
  223. {
  224. carla_stderr2("%s(%p, %p)", __FUNCTION__, client, port);
  225. return nullptr;
  226. }
  227. // --------------------------------------------------------------------------------------------------------------------
  228. CARLA_EXPORT
  229. int jack_port_tie(jack_port_t* src, jack_port_t* dst)
  230. {
  231. carla_debug("%s(%p, %p)", __FUNCTION__, src, dst);
  232. return ENOSYS;
  233. // unused
  234. (void)src;
  235. (void)dst;
  236. }
  237. CARLA_EXPORT
  238. int jack_port_untie(jack_port_t* port)
  239. {
  240. carla_debug("%s(%p)", __FUNCTION__, port);
  241. return ENOSYS;
  242. // unused
  243. (void)port;
  244. }
  245. // --------------------------------------------------------------------------------------------------------------------
  246. CARLA_EXPORT
  247. int jack_port_set_name(jack_port_t *port, const char *port_name)
  248. {
  249. carla_stderr2("%s(%p, %s)", __FUNCTION__, port, port_name);
  250. return ENOSYS;
  251. }
  252. CARLA_EXPORT
  253. int jack_port_rename(jack_client_t* client, jack_port_t *port, const char *port_name)
  254. {
  255. carla_stderr2("%s(%p, %p, %s)", __FUNCTION__, client, port, port_name);
  256. return ENOSYS;
  257. }
  258. CARLA_EXPORT
  259. int jack_port_set_alias(jack_port_t* port, const char* alias)
  260. {
  261. carla_stderr2("%s(%p, %s)", __FUNCTION__, port, alias);
  262. return ENOSYS;
  263. }
  264. CARLA_EXPORT
  265. int jack_port_unset_alias(jack_port_t* port, const char* alias)
  266. {
  267. carla_stderr2("%s(%p, %s)", __FUNCTION__, port, alias);
  268. return ENOSYS;
  269. }
  270. CARLA_EXPORT
  271. int jack_port_get_aliases(const jack_port_t*, const char* aliases[2])
  272. {
  273. static const char nullChar = '\0';
  274. aliases[0] = aliases[1] = &nullChar;
  275. return 0;
  276. }
  277. // --------------------------------------------------------------------------------------------------------------------
  278. CARLA_EXPORT
  279. int jack_port_request_monitor(jack_port_t* port, int onoff)
  280. {
  281. carla_stderr2("%s(%p, %i)", __FUNCTION__, port, onoff);
  282. return ENOSYS;
  283. }
  284. CARLA_EXPORT
  285. int jack_port_request_monitor_by_name(jack_client_t* client, const char* port_name, int onoff)
  286. {
  287. carla_stderr2("%s(%p, %s, %i)", __FUNCTION__, client, port_name, onoff);
  288. return ENOSYS;
  289. }
  290. CARLA_EXPORT
  291. int jack_port_ensure_monitor(jack_port_t* port, int onoff)
  292. {
  293. carla_stderr2("%s(%p, %i)", __FUNCTION__, port, onoff);
  294. return ENOSYS;
  295. }
  296. CARLA_EXPORT
  297. int jack_port_monitoring_input(jack_port_t* port)
  298. {
  299. carla_stderr2("%s(%p)", __FUNCTION__, port);
  300. return 0;
  301. }
  302. // --------------------------------------------------------------------------------------------------------------------
  303. CARLA_EXPORT
  304. int jack_connect(jack_client_t* client, const char* a, const char* b)
  305. {
  306. carla_stderr2("%s(%p, %s. %s)", __FUNCTION__, client, a, b);
  307. return 0;
  308. }
  309. CARLA_EXPORT
  310. int jack_disconnect(jack_client_t* client, const char* a, const char* b)
  311. {
  312. carla_stderr2("%s(%p, %s. %s)", __FUNCTION__, client, a, b);
  313. return 0;
  314. }
  315. CARLA_EXPORT
  316. int jack_port_disconnect(jack_client_t* client, jack_port_t* port)
  317. {
  318. carla_stderr2("%s(%p, %p)", __FUNCTION__, client, port);
  319. return 0;
  320. }
  321. // --------------------------------------------------------------------------------------------------------------------
  322. CARLA_EXPORT
  323. int jack_port_name_size(void)
  324. {
  325. return STR_MAX;
  326. }
  327. CARLA_EXPORT
  328. int jack_port_type_size(void)
  329. {
  330. return STR_MAX;
  331. }
  332. CARLA_EXPORT
  333. size_t jack_port_type_get_buffer_size(jack_client_t* client, const char* port_type)
  334. {
  335. carla_debug("%s(%p, %s)", __FUNCTION__, client, port_type);
  336. CARLA_SAFE_ASSERT_RETURN(port_type != nullptr && port_type[0] != '\0', 0);
  337. if (std::strcmp(port_type, JACK_DEFAULT_MIDI_TYPE) == 0)
  338. return JackMidiPortBuffer::kMaxEventSize;
  339. return 0;
  340. // unused
  341. (void)client;
  342. }
  343. // --------------------------------------------------------------------------------------------------------------------