DISTRHO Plugin Framework
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.

523 lines
13KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2016 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. * or without fee is hereby granted, provided that the above copyright notice and this
  7. * permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  10. * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  11. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  12. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  13. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include "DistrhoUIInternal.hpp"
  17. #if DISTRHO_PLUGIN_WANT_DIRECT_ACCESS
  18. # error DSSI UIs do not support direct access!
  19. #endif
  20. #include "../extra/Sleep.hpp"
  21. #include <lo/lo.h>
  22. START_NAMESPACE_DISTRHO
  23. #if ! DISTRHO_PLUGIN_WANT_MIDI_INPUT
  24. static const sendNoteFunc sendNoteCallback = nullptr;
  25. #endif
  26. // -----------------------------------------------------------------------
  27. struct OscData {
  28. lo_address addr;
  29. const char* path;
  30. lo_server server;
  31. OscData()
  32. : addr(nullptr),
  33. path(nullptr),
  34. server(nullptr) {}
  35. void idle() const
  36. {
  37. if (server == nullptr)
  38. return;
  39. while (lo_server_recv_noblock(server, 0) != 0) {}
  40. }
  41. void send_configure(const char* const key, const char* const value) const
  42. {
  43. char targetPath[std::strlen(path)+11];
  44. std::strcpy(targetPath, path);
  45. std::strcat(targetPath, "/configure");
  46. lo_send(addr, targetPath, "ss", key, value);
  47. }
  48. void send_control(const int32_t index, const float value) const
  49. {
  50. char targetPath[std::strlen(path)+9];
  51. std::strcpy(targetPath, path);
  52. std::strcat(targetPath, "/control");
  53. lo_send(addr, targetPath, "if", index, value);
  54. }
  55. void send_midi(uchar data[4]) const
  56. {
  57. char targetPath[std::strlen(path)+6];
  58. std::strcpy(targetPath, path);
  59. std::strcat(targetPath, "/midi");
  60. lo_send(addr, targetPath, "m", data);
  61. }
  62. void send_update(const char* const url) const
  63. {
  64. char targetPath[std::strlen(path)+8];
  65. std::strcpy(targetPath, path);
  66. std::strcat(targetPath, "/update");
  67. lo_send(addr, targetPath, "s", url);
  68. }
  69. void send_exiting() const
  70. {
  71. char targetPath[std::strlen(path)+9];
  72. std::strcpy(targetPath, path);
  73. std::strcat(targetPath, "/exiting");
  74. lo_send(addr, targetPath, "");
  75. }
  76. };
  77. // -----------------------------------------------------------------------
  78. class UIDssi
  79. {
  80. public:
  81. UIDssi(const OscData& oscData, const char* const uiTitle)
  82. : fUI(this, 0, nullptr, setParameterCallback, setStateCallback, sendNoteCallback, setSizeCallback),
  83. fHostClosed(false),
  84. fOscData(oscData)
  85. {
  86. fUI.setWindowTitle(uiTitle);
  87. }
  88. ~UIDssi()
  89. {
  90. if (fOscData.server != nullptr && ! fHostClosed)
  91. fOscData.send_exiting();
  92. }
  93. void exec()
  94. {
  95. for (;;)
  96. {
  97. fOscData.idle();
  98. if (fHostClosed || ! fUI.idle())
  99. break;
  100. d_msleep(30);
  101. }
  102. }
  103. // -------------------------------------------------------------------
  104. #if DISTRHO_PLUGIN_WANT_STATE
  105. void dssiui_configure(const char* key, const char* value)
  106. {
  107. fUI.stateChanged(key, value);
  108. }
  109. #endif
  110. void dssiui_control(ulong index, float value)
  111. {
  112. fUI.parameterChanged(index, value);
  113. }
  114. #if DISTRHO_PLUGIN_WANT_PROGRAMS
  115. void dssiui_program(ulong bank, ulong program)
  116. {
  117. fUI.programLoaded(bank * 128 + program);
  118. }
  119. #endif
  120. void dssiui_samplerate(const double sampleRate)
  121. {
  122. fUI.setSampleRate(sampleRate, true);
  123. }
  124. void dssiui_show()
  125. {
  126. fUI.setWindowVisible(true);
  127. }
  128. void dssiui_hide()
  129. {
  130. fUI.setWindowVisible(false);
  131. }
  132. void dssiui_quit()
  133. {
  134. fHostClosed = true;
  135. fUI.quit();
  136. }
  137. // -------------------------------------------------------------------
  138. protected:
  139. void setParameterValue(const uint32_t rindex, const float value)
  140. {
  141. if (fOscData.server == nullptr)
  142. return;
  143. fOscData.send_control(rindex, value);
  144. }
  145. void setState(const char* const key, const char* const value)
  146. {
  147. if (fOscData.server == nullptr)
  148. return;
  149. fOscData.send_configure(key, value);
  150. }
  151. #if DISTRHO_PLUGIN_WANT_MIDI_INPUT
  152. void sendNote(const uint8_t channel, const uint8_t note, const uint8_t velocity)
  153. {
  154. if (fOscData.server == nullptr)
  155. return;
  156. if (channel > 0xF)
  157. return;
  158. uint8_t mdata[4] = {
  159. 0,
  160. channel + (velocity != 0 ? 0x90 : 0x80),
  161. note,
  162. velocity
  163. };
  164. fOscData.send_midi(mdata);
  165. }
  166. #endif
  167. void setSize(const uint width, const uint height)
  168. {
  169. fUI.setWindowSize(width, height);
  170. }
  171. private:
  172. UIExporter fUI;
  173. bool fHostClosed;
  174. const OscData& fOscData;
  175. // -------------------------------------------------------------------
  176. // Callbacks
  177. #define uiPtr ((UIDssi*)ptr)
  178. static void setParameterCallback(void* ptr, uint32_t rindex, float value)
  179. {
  180. uiPtr->setParameterValue(rindex, value);
  181. }
  182. static void setStateCallback(void* ptr, const char* key, const char* value)
  183. {
  184. uiPtr->setState(key, value);
  185. }
  186. #if DISTRHO_PLUGIN_WANT_MIDI_INPUT
  187. static void sendNoteCallback(void* ptr, uint8_t channel, uint8_t note, uint8_t velocity)
  188. {
  189. uiPtr->sendNote(channel, note, velocity);
  190. }
  191. #endif
  192. static void setSizeCallback(void* ptr, uint width, uint height)
  193. {
  194. uiPtr->setSize(width, height);
  195. }
  196. #undef uiPtr
  197. };
  198. // -----------------------------------------------------------------------
  199. static OscData gOscData;
  200. static const char* gUiTitle = nullptr;
  201. static UIDssi* globalUI = nullptr;
  202. static void initUiIfNeeded()
  203. {
  204. if (globalUI != nullptr)
  205. return;
  206. if (d_lastUiSampleRate == 0.0)
  207. d_lastUiSampleRate = 44100.0;
  208. globalUI = new UIDssi(gOscData, gUiTitle);
  209. }
  210. // -----------------------------------------------------------------------
  211. int osc_debug_handler(const char* path, const char*, lo_arg**, int, lo_message, void*)
  212. {
  213. d_debug("osc_debug_handler(\"%s\")", path);
  214. return 0;
  215. #ifndef DEBUG
  216. // unused
  217. (void)path;
  218. #endif
  219. }
  220. void osc_error_handler(int num, const char* msg, const char* path)
  221. {
  222. d_stderr("osc_error_handler(%i, \"%s\", \"%s\")", num, msg, path);
  223. }
  224. #if DISTRHO_PLUGIN_WANT_STATE
  225. int osc_configure_handler(const char*, const char*, lo_arg** argv, int, lo_message, void*)
  226. {
  227. const char* const key = &argv[0]->s;
  228. const char* const value = &argv[1]->s;
  229. d_debug("osc_configure_handler(\"%s\", \"%s\")", key, value);
  230. initUiIfNeeded();
  231. globalUI->dssiui_configure(key, value);
  232. return 0;
  233. }
  234. #endif
  235. int osc_control_handler(const char*, const char*, lo_arg** argv, int, lo_message, void*)
  236. {
  237. const int32_t rindex = argv[0]->i;
  238. const float value = argv[1]->f;
  239. d_debug("osc_control_handler(%i, %f)", rindex, value);
  240. int32_t index = rindex - DISTRHO_PLUGIN_NUM_INPUTS - DISTRHO_PLUGIN_NUM_OUTPUTS;
  241. // latency
  242. #if DISTRHO_PLUGIN_WANT_LATENCY
  243. index -= 1;
  244. #endif
  245. if (index < 0)
  246. return 0;
  247. initUiIfNeeded();
  248. globalUI->dssiui_control(index, value);
  249. return 0;
  250. }
  251. #if DISTRHO_PLUGIN_WANT_PROGRAMS
  252. int osc_program_handler(const char*, const char*, lo_arg** argv, int, lo_message, void*)
  253. {
  254. const int32_t bank = argv[0]->i;
  255. const int32_t program = argv[1]->f;
  256. d_debug("osc_program_handler(%i, %i)", bank, program);
  257. initUiIfNeeded();
  258. globalUI->dssiui_program(bank, program);
  259. return 0;
  260. }
  261. #endif
  262. int osc_sample_rate_handler(const char*, const char*, lo_arg** argv, int, lo_message, void*)
  263. {
  264. const int32_t sampleRate = argv[0]->i;
  265. d_debug("osc_sample_rate_handler(%i)", sampleRate);
  266. d_lastUiSampleRate = sampleRate;
  267. if (globalUI != nullptr)
  268. globalUI->dssiui_samplerate(sampleRate);
  269. return 0;
  270. }
  271. int osc_show_handler(const char*, const char*, lo_arg**, int, lo_message, void*)
  272. {
  273. d_debug("osc_show_handler()");
  274. initUiIfNeeded();
  275. globalUI->dssiui_show();
  276. return 0;
  277. }
  278. int osc_hide_handler(const char*, const char*, lo_arg**, int, lo_message, void*)
  279. {
  280. d_debug("osc_hide_handler()");
  281. if (globalUI != nullptr)
  282. globalUI->dssiui_hide();
  283. return 0;
  284. }
  285. int osc_quit_handler(const char*, const char*, lo_arg**, int, lo_message, void*)
  286. {
  287. d_debug("osc_quit_handler()");
  288. if (globalUI != nullptr)
  289. globalUI->dssiui_quit();
  290. return 0;
  291. }
  292. END_NAMESPACE_DISTRHO
  293. // -----------------------------------------------------------------------
  294. int main(int argc, char* argv[])
  295. {
  296. USE_NAMESPACE_DISTRHO
  297. // dummy test mode
  298. if (argc == 1)
  299. {
  300. gUiTitle = "DSSI UI Test";
  301. initUiIfNeeded();
  302. globalUI->dssiui_show();
  303. globalUI->exec();
  304. delete globalUI;
  305. globalUI = nullptr;
  306. return 0;
  307. }
  308. if (argc != 5)
  309. {
  310. fprintf(stderr, "Usage: %s <osc-url> <plugin-dll> <plugin-label> <instance-name>\n", argv[0]);
  311. return 1;
  312. }
  313. const char* oscUrl = argv[1];
  314. const char* uiTitle = argv[4];
  315. char* const oscHost = lo_url_get_hostname(oscUrl);
  316. char* const oscPort = lo_url_get_port(oscUrl);
  317. char* const oscPath = lo_url_get_path(oscUrl);
  318. size_t oscPathSize = strlen(oscPath);
  319. lo_address oscAddr = lo_address_new(oscHost, oscPort);
  320. lo_server oscServer = lo_server_new_with_proto(nullptr, LO_UDP, osc_error_handler);
  321. char* const oscServerPath = lo_server_get_url(oscServer);
  322. char pluginPath[strlen(oscServerPath)+oscPathSize];
  323. strcpy(pluginPath, oscServerPath);
  324. strcat(pluginPath, oscPath+1);
  325. #if DISTRHO_PLUGIN_WANT_STATE
  326. char oscPathConfigure[oscPathSize+11];
  327. strcpy(oscPathConfigure, oscPath);
  328. strcat(oscPathConfigure, "/configure");
  329. lo_server_add_method(oscServer, oscPathConfigure, "ss", osc_configure_handler, nullptr);
  330. #endif
  331. char oscPathControl[oscPathSize+9];
  332. strcpy(oscPathControl, oscPath);
  333. strcat(oscPathControl, "/control");
  334. lo_server_add_method(oscServer, oscPathControl, "if", osc_control_handler, nullptr);
  335. d_stdout("oscServerPath: \"%s\"", oscServerPath);
  336. d_stdout("pluginPath: \"%s\"", pluginPath);
  337. d_stdout("oscPathControl: \"%s\"", oscPathControl);
  338. #if DISTRHO_PLUGIN_WANT_PROGRAMS
  339. char oscPathProgram[oscPathSize+9];
  340. strcpy(oscPathProgram, oscPath);
  341. strcat(oscPathProgram, "/program");
  342. lo_server_add_method(oscServer, oscPathProgram, "ii", osc_program_handler, nullptr);
  343. #endif
  344. char oscPathSampleRate[oscPathSize+13];
  345. strcpy(oscPathSampleRate, oscPath);
  346. strcat(oscPathSampleRate, "/sample-rate");
  347. lo_server_add_method(oscServer, oscPathSampleRate, "i", osc_sample_rate_handler, nullptr);
  348. char oscPathShow[oscPathSize+6];
  349. strcpy(oscPathShow, oscPath);
  350. strcat(oscPathShow, "/show");
  351. lo_server_add_method(oscServer, oscPathShow, "", osc_show_handler, nullptr);
  352. char oscPathHide[oscPathSize+6];
  353. strcpy(oscPathHide, oscPath);
  354. strcat(oscPathHide, "/hide");
  355. lo_server_add_method(oscServer, oscPathHide, "", osc_hide_handler, nullptr);
  356. char oscPathQuit[oscPathSize+6];
  357. strcpy(oscPathQuit, oscPath);
  358. strcat(oscPathQuit, "/quit");
  359. lo_server_add_method(oscServer, oscPathQuit, "", osc_quit_handler, nullptr);
  360. lo_server_add_method(oscServer, nullptr, nullptr, osc_debug_handler, nullptr);
  361. gUiTitle = uiTitle;
  362. gOscData.addr = oscAddr;
  363. gOscData.path = oscPath;
  364. gOscData.server = oscServer;
  365. gOscData.send_update(pluginPath);
  366. // wait for init
  367. for (int i=0; i < 100; ++i)
  368. {
  369. lo_server_recv(oscServer);
  370. if (d_lastUiSampleRate != 0.0 || globalUI != nullptr)
  371. break;
  372. d_msleep(50);
  373. }
  374. int ret = 1;
  375. if (d_lastUiSampleRate != 0.0 || globalUI != nullptr)
  376. {
  377. initUiIfNeeded();
  378. globalUI->exec();
  379. delete globalUI;
  380. globalUI = nullptr;
  381. ret = 0;
  382. }
  383. #if DISTRHO_PLUGIN_WANT_STATE
  384. lo_server_del_method(oscServer, oscPathConfigure, "ss");
  385. #endif
  386. lo_server_del_method(oscServer, oscPathControl, "if");
  387. #if DISTRHO_PLUGIN_WANT_PROGRAMS
  388. lo_server_del_method(oscServer, oscPathProgram, "ii");
  389. #endif
  390. lo_server_del_method(oscServer, oscPathSampleRate, "i");
  391. lo_server_del_method(oscServer, oscPathShow, "");
  392. lo_server_del_method(oscServer, oscPathHide, "");
  393. lo_server_del_method(oscServer, oscPathQuit, "");
  394. lo_server_del_method(oscServer, nullptr, nullptr);
  395. std::free(oscServerPath);
  396. std::free(oscHost);
  397. std::free(oscPort);
  398. std::free(oscPath);
  399. lo_address_free(oscAddr);
  400. lo_server_free(oscServer);
  401. return ret;
  402. }