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.

libjack_ports.cpp 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /*
  2. * Carla JACK API for external applications
  3. * Copyright (C) 2016-2017 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; (void)dst;
  235. }
  236. CARLA_EXPORT
  237. int jack_port_untie(jack_port_t* port)
  238. {
  239. carla_debug("%s(%p)", __FUNCTION__, port);
  240. return ENOSYS;
  241. // unused
  242. (void)port;
  243. }
  244. // --------------------------------------------------------------------------------------------------------------------
  245. CARLA_EXPORT
  246. int jack_port_set_name(jack_port_t *port, const char *port_name)
  247. {
  248. carla_stderr2("%s(%p, %s)", __FUNCTION__, port, port_name);
  249. return ENOSYS;
  250. }
  251. CARLA_EXPORT
  252. int jack_port_rename(jack_client_t* client, jack_port_t *port, const char *port_name)
  253. {
  254. carla_stderr2("%s(%p, %p, %s)", __FUNCTION__, client, port, port_name);
  255. return ENOSYS;
  256. }
  257. CARLA_EXPORT
  258. int jack_port_set_alias(jack_port_t* port, const char* alias)
  259. {
  260. carla_stderr2("%s(%p, %s)", __FUNCTION__, port, alias);
  261. return ENOSYS;
  262. }
  263. CARLA_EXPORT
  264. int jack_port_unset_alias(jack_port_t* port, const char* alias)
  265. {
  266. carla_stderr2("%s(%p, %s)", __FUNCTION__, port, alias);
  267. return ENOSYS;
  268. }
  269. CARLA_EXPORT
  270. int jack_port_get_aliases(const jack_port_t*, char* aliases[2])
  271. {
  272. aliases[0] = aliases[1] = nullptr;
  273. return 0;
  274. }
  275. // --------------------------------------------------------------------------------------------------------------------
  276. CARLA_EXPORT
  277. int jack_port_request_monitor(jack_port_t* port, int onoff)
  278. {
  279. carla_stderr2("%s(%p, %i)", __FUNCTION__, port, onoff);
  280. return ENOSYS;
  281. }
  282. CARLA_EXPORT
  283. int jack_port_request_monitor_by_name(jack_client_t* client, const char* port_name, int onoff)
  284. {
  285. carla_stderr2("%s(%p, %s, %i)", __FUNCTION__, client, port_name, onoff);
  286. return ENOSYS;
  287. }
  288. CARLA_EXPORT
  289. int jack_port_ensure_monitor(jack_port_t* port, int onoff)
  290. {
  291. carla_stderr2("%s(%p, %i)", __FUNCTION__, port, onoff);
  292. return ENOSYS;
  293. }
  294. CARLA_EXPORT
  295. int jack_port_monitoring_input(jack_port_t* port)
  296. {
  297. carla_stderr2("%s(%p)", __FUNCTION__, port);
  298. return 0;
  299. }
  300. // --------------------------------------------------------------------------------------------------------------------
  301. CARLA_EXPORT
  302. int jack_connect(jack_client_t* client, const char* a, const char* b)
  303. {
  304. carla_stderr2("%s(%p, %s. %s)", __FUNCTION__, client, a, b);
  305. return 0;
  306. }
  307. CARLA_EXPORT
  308. int jack_disconnect(jack_client_t* client, const char* a, const char* b)
  309. {
  310. carla_stderr2("%s(%p, %s. %s)", __FUNCTION__, client, a, b);
  311. return 0;
  312. }
  313. CARLA_EXPORT
  314. int jack_port_disconnect(jack_client_t* client, jack_port_t* port)
  315. {
  316. carla_stderr2("%s(%p, %p)", __FUNCTION__, client, port);
  317. return 0;
  318. }
  319. // --------------------------------------------------------------------------------------------------------------------
  320. CARLA_EXPORT
  321. int jack_port_name_size(void)
  322. {
  323. return STR_MAX;
  324. }
  325. CARLA_EXPORT
  326. int jack_port_type_size(void)
  327. {
  328. return STR_MAX;
  329. }
  330. CARLA_EXPORT
  331. size_t jack_port_type_get_buffer_size(jack_client_t* client, const char* port_type)
  332. {
  333. carla_debug("%s(%p, %s)", __FUNCTION__, client, port_type);
  334. CARLA_SAFE_ASSERT_RETURN(port_type != nullptr && port_type[0] != '\0', 0);
  335. if (std::strcmp(port_type, JACK_DEFAULT_MIDI_TYPE) == 0)
  336. return JackMidiPortBuffer::kMaxEventSize;
  337. return 0;
  338. // unused
  339. (void)client;
  340. }
  341. // --------------------------------------------------------------------------------------------------------------------