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.

348 lines
11KB

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