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.

483 lines
14KB

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