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.

407 lines
11KB

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