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.

354 lines
12KB

  1. /*
  2. * Carla OSC utils
  3. * Copyright (C) 2012-2013 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 GPL.txt file
  16. */
  17. #ifndef __CARLA_OSC_UTILS_HPP__
  18. #define __CARLA_OSC_UTILS_HPP__
  19. #include "CarlaUtils.hpp"
  20. #include <cstdint>
  21. #include <lo/lo.h>
  22. // -------------------------------------------------
  23. struct CarlaOscData {
  24. const char* path;
  25. lo_address source;
  26. lo_address target;
  27. CarlaOscData()
  28. : path(nullptr),
  29. source(nullptr),
  30. target(nullptr) {}
  31. ~CarlaOscData()
  32. {
  33. free();
  34. }
  35. void free()
  36. {
  37. if (path != nullptr)
  38. {
  39. delete[] path;
  40. path = nullptr;
  41. }
  42. if (source != nullptr)
  43. {
  44. lo_address_free(source);
  45. source = nullptr;
  46. }
  47. if (target != nullptr)
  48. {
  49. lo_address_free(target);
  50. target = nullptr;
  51. }
  52. }
  53. };
  54. // -------------------------------------------------
  55. static inline
  56. void osc_send_configure(const CarlaOscData* const oscData, const char* const key, const char* const value)
  57. {
  58. CARLA_ASSERT(oscData != nullptr && oscData->path != nullptr);
  59. CARLA_ASSERT(key != nullptr);
  60. CARLA_ASSERT(value != nullptr);
  61. carla_debug("osc_send_configure(path:\"%s\", \"%s\", \"%s\")", oscData->path, key, value);
  62. if (oscData != nullptr && oscData->path != nullptr && oscData->target != nullptr && key != nullptr && value != nullptr)
  63. {
  64. char targetPath[std::strlen(oscData->path)+11];
  65. std::strcpy(targetPath, oscData->path);
  66. std::strcat(targetPath, "/configure");
  67. lo_send(oscData->target, targetPath, "ss", key, value);
  68. }
  69. }
  70. static inline
  71. void osc_send_control(const CarlaOscData* const oscData, const int32_t index, const float value)
  72. {
  73. CARLA_ASSERT(oscData != nullptr && oscData->path != nullptr);
  74. CARLA_ASSERT(index != -1); // -1 == PARAMETER_NULL
  75. carla_debug("osc_send_control(path:\"%s\", %i, %f)", oscData->path, index, value);
  76. if (oscData != nullptr && oscData->path != nullptr && oscData->target != nullptr && index != -1)
  77. {
  78. char targetPath[std::strlen(oscData->path)+9];
  79. std::strcpy(targetPath, oscData->path);
  80. std::strcat(targetPath, "/control");
  81. lo_send(oscData->target, targetPath, "if", index, value);
  82. }
  83. }
  84. static inline
  85. void osc_send_program(const CarlaOscData* const oscData, const int32_t index)
  86. {
  87. CARLA_ASSERT(oscData != nullptr && oscData->path != nullptr);
  88. CARLA_ASSERT(index >= 0);
  89. carla_debug("osc_send_program(path:\"%s\", %i)", oscData->path, index);
  90. if (oscData != nullptr && oscData->path != nullptr && oscData->target != nullptr && index >= 0)
  91. {
  92. char targetPath[std::strlen(oscData->path)+9];
  93. std::strcpy(targetPath, oscData->path);
  94. std::strcat(targetPath, "/program");
  95. lo_send(oscData->target, targetPath, "i", index);
  96. }
  97. }
  98. static inline
  99. void osc_send_program(const CarlaOscData* const oscData, const int32_t bank, const int32_t program)
  100. {
  101. CARLA_ASSERT(oscData != nullptr && oscData->path != nullptr);
  102. CARLA_ASSERT(program >= 0);
  103. CARLA_ASSERT(bank >= 0);
  104. carla_debug("osc_send_program(path:\"%s\", %i, %i)", oscData->path, bank, program);
  105. if (oscData != nullptr && oscData->path != nullptr && oscData->target != nullptr && bank >= 0 && program >= 0)
  106. {
  107. char targetPath[std::strlen(oscData->path)+9];
  108. std::strcpy(targetPath, oscData->path);
  109. std::strcat(targetPath, "/program");
  110. lo_send(oscData->target, targetPath, "ii", bank, program);
  111. }
  112. }
  113. static inline
  114. void osc_send_midi_program(const CarlaOscData* const oscData, const int32_t index)
  115. {
  116. CARLA_ASSERT(oscData != nullptr && oscData->path != nullptr);
  117. CARLA_ASSERT(index >= 0);
  118. carla_debug("osc_send_midi_program(path:\"%s\", %i)", oscData->path, index);
  119. if (oscData != nullptr && oscData->path != nullptr && oscData->target != nullptr && index >= 0)
  120. {
  121. char targetPath[std::strlen(oscData->path)+14];
  122. std::strcpy(targetPath, oscData->path);
  123. std::strcat(targetPath, "/midi-program");
  124. lo_send(oscData->target, targetPath, "i", index);
  125. }
  126. }
  127. static inline
  128. void osc_send_midi_program(const CarlaOscData* const oscData, const int32_t bank, const int32_t program)
  129. {
  130. CARLA_ASSERT(oscData != nullptr && oscData->path != nullptr);
  131. CARLA_ASSERT(program >= 0);
  132. CARLA_ASSERT(bank >= 0);
  133. carla_debug("osc_send_midi_program(path:\"%s\", %i, %i)", oscData->path, bank, program);
  134. if (oscData != nullptr && oscData->path != nullptr && oscData->target != nullptr && bank >= 0 && program >= 0)
  135. {
  136. char targetPath[std::strlen(oscData->path)+14];
  137. std::strcpy(targetPath, oscData->path);
  138. std::strcat(targetPath, "/midi-program");
  139. lo_send(oscData->target, targetPath, "ii", bank, program);
  140. }
  141. }
  142. static inline
  143. void osc_send_midi(const CarlaOscData* const oscData, const uint8_t buf[4])
  144. {
  145. CARLA_ASSERT(oscData != nullptr && oscData->path != nullptr);
  146. CARLA_ASSERT(buf[0] == 0);
  147. CARLA_ASSERT(buf[1] != 0);
  148. carla_debug("osc_send_midi(path:\"%s\", 0x%X, %03u, %03u)", oscData->path, buf[1], buf[2], buf[3]);
  149. if (oscData != nullptr && oscData->path != nullptr && oscData->target != nullptr && buf[0] == 0 && buf[1] != 0)
  150. {
  151. char targetPath[std::strlen(oscData->path)+6];
  152. std::strcpy(targetPath, oscData->path);
  153. std::strcat(targetPath, "/midi");
  154. lo_send(oscData->target, targetPath, "m", buf);
  155. }
  156. }
  157. static inline
  158. void osc_send_sample_rate(const CarlaOscData* const oscData, const float sampleRate)
  159. {
  160. CARLA_ASSERT(oscData != nullptr && oscData->path != nullptr);
  161. CARLA_ASSERT(sampleRate > 0.0f);
  162. carla_debug("osc_send_sample_rate(path:\"%s\", %f)", oscData->path, sampleRate);
  163. if (oscData != nullptr && oscData->path != nullptr && oscData->target != nullptr && sampleRate > 0.0f)
  164. {
  165. char targetPath[std::strlen(oscData->path)+13];
  166. std::strcpy(targetPath, oscData->path);
  167. std::strcat(targetPath, "/sample-rate");
  168. lo_send(oscData->target, targetPath, "f", sampleRate);
  169. }
  170. }
  171. #ifdef BUILD_BRIDGE
  172. static inline
  173. void osc_send_update(const CarlaOscData* const oscData, const char* const url)
  174. {
  175. CARLA_ASSERT(oscData != nullptr && oscData->path != nullptr);
  176. CARLA_ASSERT(url != nullptr);
  177. carla_debug("osc_send_update(path:\"%s\", \"%s\")", oscData->path, url);
  178. if (oscData != nullptr && oscData->path != nullptr && oscData->target != nullptr && url != nullptr)
  179. {
  180. char targetPath[std::strlen(oscData->path)+8];
  181. std::strcpy(targetPath, oscData->path);
  182. std::strcat(targetPath, "/update");
  183. lo_send(oscData->target, targetPath, "s", url);
  184. }
  185. }
  186. static inline
  187. void osc_send_exiting(const CarlaOscData* const oscData)
  188. {
  189. CARLA_ASSERT(oscData != nullptr && oscData->path != nullptr);
  190. carla_debug("osc_send_exiting(path:\"%s\")", oscData->path);
  191. if (oscData != nullptr && oscData->path != nullptr && oscData->target != nullptr)
  192. {
  193. char targetPath[std::strlen(oscData->path)+9];
  194. std::strcpy(targetPath, oscData->path);
  195. std::strcat(targetPath, "/exiting");
  196. lo_send(oscData->target, targetPath, "");
  197. }
  198. }
  199. #endif
  200. static inline
  201. void osc_send_show(const CarlaOscData* const oscData)
  202. {
  203. CARLA_ASSERT(oscData != nullptr && oscData->path != nullptr);
  204. carla_debug("osc_send_show(path:\"%s\")", oscData->path);
  205. if (oscData != nullptr && oscData->path != nullptr && oscData->target != nullptr)
  206. {
  207. char targetPath[std::strlen(oscData->path)+6];
  208. std::strcpy(targetPath, oscData->path);
  209. std::strcat(targetPath, "/show");
  210. lo_send(oscData->target, targetPath, "");
  211. }
  212. }
  213. static inline
  214. void osc_send_hide(const CarlaOscData* const oscData)
  215. {
  216. CARLA_ASSERT(oscData != nullptr && oscData->path != nullptr);
  217. carla_debug("osc_send_hide(path:\"%s\")", oscData->path);
  218. if (oscData != nullptr && oscData->path != nullptr && oscData->target != nullptr)
  219. {
  220. char targetPath[std::strlen(oscData->path)+6];
  221. std::strcpy(targetPath, oscData->path);
  222. std::strcat(targetPath, "/hide");
  223. lo_send(oscData->target, targetPath, "");
  224. }
  225. }
  226. static inline
  227. void osc_send_quit(const CarlaOscData* const oscData)
  228. {
  229. CARLA_ASSERT(oscData != nullptr && oscData->path != nullptr);
  230. carla_debug("osc_send_quit(path:\"%s\")", oscData->path);
  231. if (oscData != nullptr && oscData->path != nullptr && oscData->target != nullptr)
  232. {
  233. char targetPath[std::strlen(oscData->path)+6];
  234. std::strcpy(targetPath, oscData->path);
  235. std::strcat(targetPath, "/quit");
  236. lo_send(oscData->target, targetPath, "");
  237. }
  238. }
  239. // -------------------------------------------------
  240. #ifdef BUILD_BRIDGE_PLUGIN
  241. static inline
  242. void osc_send_bridge_update(const CarlaOscData* const oscData, const char* const url)
  243. {
  244. CARLA_ASSERT(oscData != nullptr && oscData->path != nullptr);
  245. CARLA_ASSERT(url != nullptr);
  246. carla_debug("osc_send_bridge_update(path:\"%s\", \"%s\")", oscData->path, url);
  247. if (oscData != nullptr && oscData->path != nullptr && oscData->target != nullptr && url != nullptr)
  248. {
  249. char targetPath[std::strlen(oscData->path)+15];
  250. std::strcpy(targetPath, oscData->path);
  251. std::strcat(targetPath, "/bridge_update");
  252. lo_send(oscData->target, targetPath, "s", url);
  253. }
  254. }
  255. static inline
  256. void osc_send_bridge_error(const CarlaOscData* const oscData, const char* const error)
  257. {
  258. CARLA_ASSERT(oscData != nullptr && oscData->path != nullptr);
  259. CARLA_ASSERT(error != nullptr);
  260. carla_debug("osc_send_bridge_error(path:\"%s\", \"%s\")", oscData->path, error);
  261. if (oscData != nullptr && oscData->path != nullptr && oscData->target != nullptr && error != nullptr)
  262. {
  263. char targetPath[std::strlen(oscData->path)+14];
  264. std::strcpy(targetPath, oscData->path);
  265. std::strcat(targetPath, "/bridge_error");
  266. lo_send(oscData->target, targetPath, "s", error);
  267. }
  268. }
  269. #endif
  270. #if defined(BRIDGE_LV2) || defined(WANT_LV2)
  271. static inline
  272. void osc_send_lv2_transfer_atom(const CarlaOscData* const oscData, const int32_t portIndex, const char* const typeStr, const char* const atomBuf)
  273. {
  274. CARLA_ASSERT(oscData != nullptr && oscData->path != nullptr);
  275. CARLA_ASSERT(portIndex >= 0);
  276. CARLA_ASSERT(typeStr != nullptr);
  277. CARLA_ASSERT(atomBuf != nullptr);
  278. carla_debug("osc_send_lv2_transfer_atom(path:\"%s\", %i, \"%s\", <atomBuf:%p>)", oscData->path, portIndex, typeStr, atomBuf);
  279. if (oscData != nullptr && oscData->path != nullptr && oscData->target != nullptr && portIndex >= 0 && typeStr != nullptr && atomBuf != nullptr)
  280. {
  281. char targetPath[std::strlen(oscData->path)+19];
  282. std::strcpy(targetPath, oscData->path);
  283. std::strcat(targetPath, "/lv2_atom_transfer");
  284. lo_send(oscData->target, targetPath, "iss", portIndex, typeStr, atomBuf);
  285. }
  286. }
  287. static inline
  288. void osc_send_lv2_transfer_event(const CarlaOscData* const oscData, const int32_t portIndex, const char* const typeStr, const char* const atomBuf)
  289. {
  290. CARLA_ASSERT(oscData != nullptr && oscData->path != nullptr);
  291. CARLA_ASSERT(portIndex >= 0);
  292. CARLA_ASSERT(typeStr != nullptr);
  293. CARLA_ASSERT(atomBuf != nullptr);
  294. carla_debug("osc_send_lv2_transfer_event(path:\"%s\", %i, \"%s\", <atomBuf:%p>)", oscData->path, portIndex, typeStr, atomBuf);
  295. if (oscData != nullptr && oscData->path != nullptr && oscData->target != nullptr && portIndex >= 0 && typeStr != nullptr && atomBuf != nullptr)
  296. {
  297. char targetPath[std::strlen(oscData->path)+20];
  298. std::strcpy(targetPath, oscData->path);
  299. std::strcat(targetPath, "/lv2_event_transfer");
  300. lo_send(oscData->target, targetPath, "iss", portIndex, typeStr, atomBuf);
  301. }
  302. }
  303. #endif
  304. // -------------------------------------------------
  305. #endif // __CARLA_OSC_UTILS_HPP__