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.

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