Collection of tools useful for audio production
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.

349 lines
11KB

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