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.

335 lines
10KB

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