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.

CarlaEngineOsc.cpp 25KB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  1. /*
  2. * Carla Engine OSC
  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. #include "CarlaEngineOsc.hpp"
  18. #include "CarlaEngine.hpp"
  19. #include "CarlaPlugin.hpp"
  20. #include "CarlaMIDI.h"
  21. #ifndef BUILD_BRIDGE
  22. # include "CarlaBridgeUtils.hpp"
  23. #endif
  24. CARLA_BACKEND_START_NAMESPACE
  25. #ifndef BUILD_BRIDGE
  26. // -------------------------------------------------------------------
  27. // Bridge Helper, defined in CarlaPlugin.cpp
  28. extern int CarlaPluginSetOscBridgeInfo(CarlaPlugin* const plugin, const PluginBridgeInfoType type,
  29. const int argc, const lo_arg* const* const argv, const char* const types);
  30. #endif
  31. // -----------------------------------------------------------------------
  32. CarlaEngineOsc::CarlaEngineOsc(CarlaEngine* const engine)
  33. : kEngine(engine),
  34. fServerTCP(nullptr),
  35. fServerUDP(nullptr)
  36. {
  37. carla_debug("CarlaEngineOsc::CarlaEngineOsc(%p)", engine);
  38. CARLA_ASSERT(engine != nullptr);
  39. }
  40. CarlaEngineOsc::~CarlaEngineOsc()
  41. {
  42. carla_debug("CarlaEngineOsc::~CarlaEngineOsc()");
  43. CARLA_ASSERT(fName.isEmpty());
  44. CARLA_ASSERT(fServerPathTCP.isEmpty());
  45. CARLA_ASSERT(fServerPathUDP.isEmpty());
  46. CARLA_ASSERT(fServerTCP == nullptr);
  47. CARLA_ASSERT(fServerUDP == nullptr);
  48. }
  49. // -----------------------------------------------------------------------
  50. void CarlaEngineOsc::init(const char* const name)
  51. {
  52. carla_debug("CarlaEngineOsc::init(\"%s\")", name);
  53. CARLA_ASSERT(fName.isEmpty());
  54. CARLA_ASSERT(fServerPathTCP.isEmpty());
  55. CARLA_ASSERT(fServerPathUDP.isEmpty());
  56. CARLA_ASSERT(fServerTCP == nullptr);
  57. CARLA_ASSERT(fServerUDP == nullptr);
  58. CARLA_ASSERT(name != nullptr);
  59. fName = name;
  60. fName.toBasic();
  61. fServerTCP = lo_server_new_with_proto(nullptr, LO_TCP, osc_error_handler_TCP);
  62. if (fServerTCP != nullptr)
  63. {
  64. if (char* const tmpServerPathTCP = lo_server_get_url(fServerTCP))
  65. {
  66. fServerPathTCP = tmpServerPathTCP;
  67. fServerPathTCP += fName;
  68. std::free(tmpServerPathTCP);
  69. }
  70. lo_server_add_method(fServerTCP, nullptr, nullptr, osc_message_handler_TCP, this);
  71. }
  72. fServerUDP = lo_server_new_with_proto(nullptr, LO_UDP, osc_error_handler_UDP);
  73. if (fServerUDP != nullptr)
  74. {
  75. if (char* const tmpServerPathUDP = lo_server_get_url(fServerUDP))
  76. {
  77. fServerPathUDP = tmpServerPathUDP;
  78. fServerPathUDP += fName;
  79. std::free(tmpServerPathUDP);
  80. }
  81. lo_server_add_method(fServerUDP, nullptr, nullptr, osc_message_handler_UDP, this);
  82. }
  83. CARLA_ASSERT(fName.isNotEmpty());
  84. CARLA_ASSERT(fServerPathTCP.isNotEmpty());
  85. CARLA_ASSERT(fServerPathUDP.isNotEmpty());
  86. CARLA_ASSERT(fServerTCP != nullptr);
  87. CARLA_ASSERT(fServerUDP != nullptr);
  88. }
  89. void CarlaEngineOsc::idle()
  90. {
  91. if (fServerTCP != nullptr)
  92. {
  93. while (lo_server_recv_noblock(fServerTCP, 0) != 0) {}
  94. }
  95. if (fServerUDP != nullptr)
  96. {
  97. while (lo_server_recv_noblock(fServerUDP, 0) != 0) {}
  98. }
  99. }
  100. void CarlaEngineOsc::close()
  101. {
  102. carla_debug("CarlaEngineOsc::close()");
  103. CARLA_ASSERT(fName.isNotEmpty());
  104. CARLA_ASSERT(fServerPathTCP.isNotEmpty());
  105. CARLA_ASSERT(fServerPathUDP.isNotEmpty());
  106. CARLA_ASSERT(fServerTCP != nullptr);
  107. CARLA_ASSERT(fServerUDP != nullptr);
  108. fName.clear();
  109. if (fServerTCP != nullptr)
  110. {
  111. lo_server_del_method(fServerTCP, nullptr, nullptr);
  112. lo_server_free(fServerTCP);
  113. fServerTCP = nullptr;
  114. }
  115. if (fServerUDP != nullptr)
  116. {
  117. lo_server_del_method(fServerUDP, nullptr, nullptr);
  118. lo_server_free(fServerUDP);
  119. fServerUDP = nullptr;
  120. }
  121. fServerPathTCP.clear();
  122. fServerPathUDP.clear();
  123. #ifndef BUILD_BRIDGE
  124. fControlData.free();
  125. #endif
  126. CARLA_ASSERT(fName.isEmpty());
  127. CARLA_ASSERT(fServerPathTCP.isEmpty());
  128. CARLA_ASSERT(fServerPathUDP.isEmpty());
  129. CARLA_ASSERT(fServerTCP == nullptr);
  130. CARLA_ASSERT(fServerUDP == nullptr);
  131. }
  132. // -----------------------------------------------------------------------
  133. bool isDigit(const char c)
  134. {
  135. return (c >= '0' && c <= '9');
  136. }
  137. int CarlaEngineOsc::handleMessage(const bool isTCP, const char* const path, const int argc, const lo_arg* const* const argv, const char* const types, const lo_message msg)
  138. {
  139. carla_debug("CarlaEngineOsc::handleMessage(%s, \"%s\", %i, %p, \"%s\", %p)", bool2str(isTCP), path, argc, argv, types, msg);
  140. CARLA_ASSERT(fName.isNotEmpty());
  141. CARLA_ASSERT(fServerPathTCP.isNotEmpty());
  142. CARLA_ASSERT(fServerPathUDP.isNotEmpty());
  143. CARLA_ASSERT(fServerTCP != nullptr);
  144. CARLA_ASSERT(fServerUDP != nullptr);
  145. CARLA_ASSERT(path != nullptr);
  146. if (path == nullptr)
  147. {
  148. carla_stderr("CarlaEngineOsc::handleMessage() - got invalid path");
  149. return 1;
  150. }
  151. if (fName.isEmpty())
  152. {
  153. carla_stderr("CarlaEngineOsc::handleMessage(%s, \"%s\", ...) - received message but client is offline", bool2str(isTCP), path);
  154. return 1;
  155. }
  156. #ifndef BUILD_BRIDGE
  157. // Initial path check
  158. if (std::strcmp(path, "/register") == 0)
  159. {
  160. const lo_address source = lo_message_get_source(msg);
  161. return handleMsgRegister(isTCP, argc, argv, types, source);
  162. }
  163. if (std::strcmp(path, "/unregister") == 0)
  164. {
  165. return handleMsgUnregister();
  166. }
  167. #endif
  168. const size_t nameSize = fName.length();
  169. // Check if message is for this client
  170. if (std::strlen(path) <= nameSize || std::strncmp(path+1, (const char*)fName, nameSize) != 0)
  171. {
  172. carla_stderr("CarlaEngineOsc::handleMessage() - message not for this client -> '%s' != '/%s/'", path, (const char*)fName);
  173. return 1;
  174. }
  175. // Get plugin id from message
  176. // eg, /carla/23/method
  177. unsigned int pluginId = 0;
  178. size_t offset;
  179. if (isDigit(path[nameSize+2]))
  180. {
  181. if (isDigit(path[nameSize+3]))
  182. {
  183. if (isDigit(path[nameSize+5]))
  184. {
  185. carla_stderr2("CarlaEngineOsc::handleMessage() - invalid plugin id, over 999? (value: \"%s\")", path+nameSize);
  186. return 1;
  187. }
  188. else if (isDigit(path[nameSize+4]))
  189. {
  190. // 3 digits, /xyz/method
  191. offset = 6;
  192. pluginId += (path[nameSize+2]-'0')*100;
  193. pluginId += (path[nameSize+3]-'0')*10;
  194. pluginId += (path[nameSize+4]-'0');
  195. }
  196. else
  197. {
  198. // 2 digits, /xy/method
  199. offset = 5;
  200. pluginId += (path[nameSize+2]-'0')*10;
  201. pluginId += (path[nameSize+3]-'0');
  202. }
  203. }
  204. else
  205. {
  206. // single digit, /x/method
  207. offset = 4;
  208. pluginId += path[nameSize+2]-'0';
  209. }
  210. }
  211. else
  212. {
  213. carla_stderr("CarlaEngineOsc::handleMessage() - invalid message '%s'", path);
  214. return 1;
  215. }
  216. if (pluginId > kEngine->currentPluginCount())
  217. {
  218. carla_stderr("CarlaEngineOsc::handleMessage() - failed to get plugin, wrong id '%i'", pluginId);
  219. return 1;
  220. }
  221. // Get plugin
  222. CarlaPlugin* const plugin = kEngine->getPluginUnchecked(pluginId);
  223. if (plugin == nullptr || plugin->id() != pluginId)
  224. {
  225. carla_stderr("CarlaEngineOsc::handleMessage() - invalid plugin id '%i', probably has been removed", pluginId);
  226. return 1;
  227. }
  228. // Get method from path, "/Carla/i/method" -> "method"
  229. char method[32] = { 0 };
  230. std::strncpy(method, path + (nameSize + offset), 31);
  231. if (method[0] == '\0')
  232. {
  233. carla_stderr("CarlaEngineOsc::handleMessage(%s, \"%s\", ...) - received message without method", bool2str(isTCP), path);
  234. return 1;
  235. }
  236. // Common OSC methods (DSSI and bridge UIs)
  237. if (std::strcmp(method, "update") == 0)
  238. {
  239. const lo_address source = lo_message_get_source(msg);
  240. return handleMsgUpdate(plugin, argc, argv, types, source);
  241. }
  242. if (std::strcmp(method, "configure") == 0)
  243. return handleMsgConfigure(plugin, argc, argv, types);
  244. if (std::strcmp(method, "control") == 0)
  245. return handleMsgControl(plugin, argc, argv, types);
  246. if (std::strcmp(method, "program") == 0)
  247. return handleMsgProgram(plugin, argc, argv, types);
  248. if (std::strcmp(method, "midi") == 0)
  249. return handleMsgMidi(plugin, argc, argv, types);
  250. if (std::strcmp(method, "exiting") == 0)
  251. return handleMsgExiting(plugin);
  252. #ifndef BUILD_BRIDGE
  253. // Internal methods
  254. if (std::strcmp(method, "set_active") == 0)
  255. return handleMsgSetActive(plugin, argc, argv, types);
  256. if (std::strcmp(method, "set_drywet") == 0)
  257. return handleMsgSetDryWet(plugin, argc, argv, types);
  258. if (std::strcmp(method, "set_volume") == 0)
  259. return handleMsgSetVolume(plugin, argc, argv, types);
  260. if (std::strcmp(method, "set_balance_left") == 0)
  261. return handleMsgSetBalanceLeft(plugin, argc, argv, types);
  262. if (std::strcmp(method, "set_balance_right") == 0)
  263. return handleMsgSetBalanceRight(plugin, argc, argv, types);
  264. if (std::strcmp(method, "set_panning") == 0)
  265. return handleMsgSetPanning(plugin, argc, argv, types);
  266. if (std::strcmp(method, "set_parameter_value") == 0)
  267. return handleMsgSetParameterValue(plugin, argc, argv, types);
  268. if (std::strcmp(method, "set_parameter_midi_cc") == 0)
  269. return handleMsgSetParameterMidiCC(plugin, argc, argv, types);
  270. if (std::strcmp(method, "set_parameter_midi_channel") == 0)
  271. return handleMsgSetParameterMidiChannel(plugin, argc, argv, types);
  272. if (std::strcmp(method, "set_program") == 0)
  273. return handleMsgSetProgram(plugin, argc, argv, types);
  274. if (std::strcmp(method, "set_midi_program") == 0)
  275. return handleMsgSetMidiProgram(plugin, argc, argv, types);
  276. if (std::strcmp(method, "note_on") == 0)
  277. return handleMsgNoteOn(plugin, argc, argv, types);
  278. if (std::strcmp(method, "note_off") == 0)
  279. return handleMsgNoteOff(plugin, argc, argv, types);
  280. // Plugin Bridges
  281. if ((plugin->hints() & PLUGIN_IS_BRIDGE) > 0 && std::strlen(method) > 11 && std::strncmp(method, "bridge_", 7) == 0)
  282. {
  283. if (std::strcmp(method+7, "audio_count") == 0)
  284. return CarlaPluginSetOscBridgeInfo(plugin, kPluginBridgeAudioCount, argc, argv, types);
  285. if (std::strcmp(method+7, "midi_count") == 0)
  286. return CarlaPluginSetOscBridgeInfo(plugin, kPluginBridgeMidiCount, argc, argv, types);
  287. if (std::strcmp(method+7, "parameter_count") == 0)
  288. return CarlaPluginSetOscBridgeInfo(plugin, kPluginBridgeParameterCount, argc, argv, types);
  289. if (std::strcmp(method+7, "program_count") == 0)
  290. return CarlaPluginSetOscBridgeInfo(plugin, kPluginBridgeProgramCount, argc, argv, types);
  291. if (std::strcmp(method+7, "midi_program_count") == 0)
  292. return CarlaPluginSetOscBridgeInfo(plugin, kPluginBridgeMidiProgramCount, argc, argv, types);
  293. if (std::strcmp(method+7, "plugin_info") == 0)
  294. return CarlaPluginSetOscBridgeInfo(plugin, kPluginBridgePluginInfo, argc, argv, types);
  295. if (std::strcmp(method+7, "parameter_info") == 0)
  296. return CarlaPluginSetOscBridgeInfo(plugin, kPluginBridgeParameterInfo, argc, argv, types);
  297. if (std::strcmp(method+7, "parameter_data") == 0)
  298. return CarlaPluginSetOscBridgeInfo(plugin, kPluginBridgeParameterData, argc, argv, types);
  299. if (std::strcmp(method+7, "parameter_ranges") == 0)
  300. return CarlaPluginSetOscBridgeInfo(plugin, kPluginBridgeParameterRanges, argc, argv, types);
  301. if (std::strcmp(method+7, "program_info") == 0)
  302. return CarlaPluginSetOscBridgeInfo(plugin, kPluginBridgeProgramInfo, argc, argv, types);
  303. if (std::strcmp(method+7, "midi_program_info") == 0)
  304. return CarlaPluginSetOscBridgeInfo(plugin, kPluginBridgeMidiProgramInfo, argc, argv, types);
  305. if (std::strcmp(method+7, "configure") == 0)
  306. return CarlaPluginSetOscBridgeInfo(plugin, kPluginBridgeConfigure, argc, argv, types);
  307. if (std::strcmp(method+7, "set_parameter_value") == 0)
  308. return CarlaPluginSetOscBridgeInfo(plugin, kPluginBridgeSetParameterValue, argc, argv, types);
  309. if (std::strcmp(method+7, "set_default_value") == 0)
  310. return CarlaPluginSetOscBridgeInfo(plugin, kPluginBridgeSetDefaultValue, argc, argv, types);
  311. if (std::strcmp(method+7, "set_program") == 0)
  312. return CarlaPluginSetOscBridgeInfo(plugin, kPluginBridgeSetProgram, argc, argv, types);
  313. if (std::strcmp(method+7, "set_midi_program") == 0)
  314. return CarlaPluginSetOscBridgeInfo(plugin, kPluginBridgeSetMidiProgram, argc, argv, types);
  315. if (std::strcmp(method+7, "set_custom_data") == 0)
  316. return CarlaPluginSetOscBridgeInfo(plugin, kPluginBridgeSetCustomData, argc, argv, types);
  317. if (std::strcmp(method+7, "set_chunk_data") == 0)
  318. return CarlaPluginSetOscBridgeInfo(plugin, kPluginBridgeSetChunkData, argc, argv, types);
  319. if (std::strcmp(method+7, "update") == 0)
  320. return CarlaPluginSetOscBridgeInfo(plugin, kPluginBridgeUpdateNow, argc, argv, types);
  321. if (std::strcmp(method+7, "error") == 0)
  322. return CarlaPluginSetOscBridgeInfo(plugin, kPluginBridgeError, argc, argv, types);
  323. }
  324. #endif
  325. // Plugin-specific methods
  326. #ifdef WANT_LV2
  327. if (std::strcmp(method, "lv2_atom_transfer") == 0)
  328. return handleMsgLv2AtomTransfer(plugin, argc, argv, types);
  329. if (std::strcmp(method, "lv2_event_transfer") == 0)
  330. return handleMsgLv2EventTransfer(plugin, argc, argv, types);
  331. #endif
  332. carla_stderr("CarlaEngineOsc::handleMessage() - unsupported OSC method '%s'", method);
  333. return 1;
  334. }
  335. // -----------------------------------------------------------------------
  336. #ifndef BUILD_BRIDGE
  337. int CarlaEngineOsc::handleMsgRegister(const bool isTCP, const int argc, const lo_arg* const* const argv, const char* const types, const lo_address source)
  338. {
  339. carla_debug("CarlaEngineOsc::handleMsgRegister()");
  340. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(1, "s");
  341. if (fControlData.path != nullptr)
  342. {
  343. carla_stderr("CarlaEngineOsc::handleMsgRegister() - OSC backend already registered to %s", fControlData.path);
  344. return 1;
  345. }
  346. const char* const url = &argv[0]->s;
  347. carla_debug("CarlaEngineOsc::handleMsgRegister() - OSC backend registered to %s", url);
  348. {
  349. const char* host = lo_address_get_hostname(source);
  350. const char* port = lo_address_get_port(source);
  351. fControlData.source = lo_address_new_with_proto(isTCP ? LO_TCP : LO_UDP, host, port);
  352. }
  353. {
  354. char* host = lo_url_get_hostname(url);
  355. char* port = lo_url_get_port(url);
  356. fControlData.path = carla_strdup_free(lo_url_get_path(url));
  357. fControlData.target = lo_address_new_with_proto(isTCP ? LO_TCP : LO_UDP, host, port);
  358. std::free(host);
  359. std::free(port);
  360. }
  361. for (unsigned short i=0; i < kEngine->currentPluginCount(); ++i)
  362. {
  363. CarlaPlugin* const plugin = kEngine->getPluginUnchecked(i);
  364. if (plugin && plugin->enabled())
  365. plugin->registerToOscClient();
  366. }
  367. return 0;
  368. }
  369. int CarlaEngineOsc::handleMsgUnregister()
  370. {
  371. carla_debug("CarlaEngineOsc::handleMsgUnregister()");
  372. if (fControlData.path == nullptr)
  373. {
  374. carla_stderr("CarlaEngineOsc::handleMsgUnregister() - OSC backend is not registered yet");
  375. return 1;
  376. }
  377. fControlData.free();
  378. return 0;
  379. }
  380. #endif
  381. // -----------------------------------------------------------------------
  382. int CarlaEngineOsc::handleMsgUpdate(CARLA_ENGINE_OSC_HANDLE_ARGS2, const lo_address source)
  383. {
  384. carla_debug("CarlaEngineOsc::handleMsgUpdate()");
  385. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(1, "s");
  386. const char* const url = (const char*)&argv[0]->s;
  387. plugin->updateOscData(source, url);
  388. return 0;
  389. }
  390. int CarlaEngineOsc::handleMsgConfigure(CARLA_ENGINE_OSC_HANDLE_ARGS2)
  391. {
  392. carla_debug("CarlaEngineOsc::handleMsgConfigure()");
  393. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(2, "ss");
  394. const char* const key = (const char*)&argv[0]->s;
  395. const char* const value = (const char*)&argv[1]->s;
  396. plugin->setCustomData(CUSTOM_DATA_STRING, key, value, false);
  397. return 0;
  398. }
  399. int CarlaEngineOsc::handleMsgControl(CARLA_ENGINE_OSC_HANDLE_ARGS2)
  400. {
  401. carla_debug("CarlaEngineOsc::handleMsgControl()");
  402. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(2, "if");
  403. const int32_t rindex = argv[0]->i;
  404. const float value = argv[1]->f;
  405. plugin->setParameterValueByRealIndex(rindex, value, false, true, true);
  406. return 0;
  407. }
  408. int CarlaEngineOsc::handleMsgProgram(CARLA_ENGINE_OSC_HANDLE_ARGS2)
  409. {
  410. carla_debug("CarlaEngineOsc::handleMsgProgram()");
  411. if (argc == 2)
  412. {
  413. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(2, "ii");
  414. const int32_t bank = argv[0]->i;
  415. const int32_t program = argv[1]->i;
  416. CARLA_SAFE_ASSERT_INT(bank >= 0, bank);
  417. CARLA_SAFE_ASSERT_INT(program >= 0, program);
  418. if (bank < 0)
  419. return 1;
  420. if (program < 0)
  421. return 1;
  422. plugin->setMidiProgramById(static_cast<uint32_t>(bank), static_cast<uint32_t>(program), false, true, true);
  423. return 0;
  424. }
  425. else
  426. {
  427. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(1, "i");
  428. const int32_t program = argv[0]->i;
  429. CARLA_SAFE_ASSERT_INT(program >= 0, program);
  430. if (program < 0)
  431. return 1;
  432. if (program < static_cast<int32_t>(plugin->programCount()))
  433. {
  434. plugin->setProgram(program, false, true, true);
  435. return 0;
  436. }
  437. carla_stderr("CarlaEngineOsc::handleMsgProgram() - programId '%i' out of bounds", program);
  438. return 1;
  439. }
  440. }
  441. int CarlaEngineOsc::handleMsgMidi(CARLA_ENGINE_OSC_HANDLE_ARGS2)
  442. {
  443. carla_debug("CarlaEngineOsc::handleMsgMidi()");
  444. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(1, "m");
  445. #ifdef BUILD_BRIDGE
  446. CARLA_ASSERT(false); // this should never happen
  447. return 1;
  448. // unused
  449. (void)plugin;
  450. (void)argv;
  451. #else
  452. if (plugin->midiInCount() == 0)
  453. {
  454. carla_stderr("CarlaEngineOsc::handleMsgMidi() - recived midi when plugin has no midi inputs");
  455. return 1;
  456. }
  457. const uint8_t* const data = argv[0]->m;
  458. uint8_t status = data[1];
  459. uint8_t channel = status & 0x0F;
  460. // Fix bad note-off
  461. if (MIDI_IS_STATUS_NOTE_ON(status) && data[3] == 0)
  462. status -= 0x10;
  463. if (MIDI_IS_STATUS_NOTE_OFF(status))
  464. {
  465. const uint8_t note = data[2];
  466. CARLA_SAFE_ASSERT_INT(note < MAX_MIDI_NOTE, note);
  467. if (note >= MAX_MIDI_NOTE)
  468. return 1;
  469. plugin->sendMidiSingleNote(channel, note, 0, false, true, true);
  470. }
  471. else if (MIDI_IS_STATUS_NOTE_ON(status))
  472. {
  473. const uint8_t note = data[2];
  474. const uint8_t velo = data[3];
  475. CARLA_SAFE_ASSERT_INT(note < MAX_MIDI_NOTE, note);
  476. CARLA_SAFE_ASSERT_INT(velo < MAX_MIDI_VALUE, velo);
  477. if (note >= MAX_MIDI_NOTE)
  478. return 1;
  479. if (velo >= MAX_MIDI_VALUE)
  480. return 1;
  481. plugin->sendMidiSingleNote(channel, note, velo, false, true, true);
  482. }
  483. return 0;
  484. #endif
  485. }
  486. int CarlaEngineOsc::handleMsgExiting(CARLA_ENGINE_OSC_HANDLE_ARGS1)
  487. {
  488. carla_debug("CarlaEngineOsc::handleMsgExiting()");
  489. // TODO - check for non-UIs (dssi-vst) and set to -1 instead
  490. kEngine->callback(CALLBACK_SHOW_GUI, plugin->id(), 0, 0, 0.0f, nullptr);
  491. plugin->freeOscData();
  492. return 0;
  493. }
  494. // -----------------------------------------------------------------------
  495. #ifndef BUILD_BRIDGE
  496. int CarlaEngineOsc::handleMsgSetActive(CARLA_ENGINE_OSC_HANDLE_ARGS2)
  497. {
  498. carla_debug("CarlaEngineOsc::handleMsgSetActive()");
  499. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(1, "i");
  500. const bool active = (argv[0]->i != 0);
  501. plugin->setActive(active, false, true);
  502. return 0;
  503. }
  504. int CarlaEngineOsc::handleMsgSetDryWet(CARLA_ENGINE_OSC_HANDLE_ARGS2)
  505. {
  506. carla_debug("CarlaEngineOsc::handleMsgSetDryWet()");
  507. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(1, "f");
  508. const float value = argv[0]->f;
  509. plugin->setDryWet(value, false, true);
  510. return 0;
  511. }
  512. int CarlaEngineOsc::handleMsgSetVolume(CARLA_ENGINE_OSC_HANDLE_ARGS2)
  513. {
  514. carla_debug("CarlaEngineOsc::handleMsgSetVolume()");
  515. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(1, "f");
  516. const float value = argv[0]->f;
  517. plugin->setVolume(value, false, true);
  518. return 0;
  519. }
  520. int CarlaEngineOsc::handleMsgSetBalanceLeft(CARLA_ENGINE_OSC_HANDLE_ARGS2)
  521. {
  522. carla_debug("CarlaEngineOsc::handleMsgSetBalanceLeft()");
  523. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(1, "f");
  524. const float value = argv[0]->f;
  525. plugin->setBalanceLeft(value, false, true);
  526. return 0;
  527. }
  528. int CarlaEngineOsc::handleMsgSetBalanceRight(CARLA_ENGINE_OSC_HANDLE_ARGS2)
  529. {
  530. carla_debug("CarlaEngineOsc::handleMsgSetBalanceRight()");
  531. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(1, "f");
  532. const float value = argv[0]->f;
  533. plugin->setBalanceRight(value, false, true);
  534. return 0;
  535. }
  536. int CarlaEngineOsc::handleMsgSetPanning(CARLA_ENGINE_OSC_HANDLE_ARGS2)
  537. {
  538. carla_debug("CarlaEngineOsc::handleMsgSetPanning()");
  539. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(1, "f");
  540. const float value = argv[0]->f;
  541. plugin->setPanning(value, false, true);
  542. return 0;
  543. }
  544. int CarlaEngineOsc::handleMsgSetParameterValue(CARLA_ENGINE_OSC_HANDLE_ARGS2)
  545. {
  546. carla_debug("CarlaEngineOsc::handleMsgSetParameterValue()");
  547. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(2, "if");
  548. const int32_t index = argv[0]->i;
  549. const float value = argv[1]->f;
  550. CARLA_SAFE_ASSERT_INT(index >= 0, index);
  551. if (index < 0)
  552. return 1;
  553. plugin->setParameterValue(static_cast<uint32_t>(index), value, true, false, true);
  554. return 0;
  555. }
  556. int CarlaEngineOsc::handleMsgSetParameterMidiCC(CARLA_ENGINE_OSC_HANDLE_ARGS2)
  557. {
  558. carla_debug("CarlaEngineOsc::handleMsgSetParameterMidiCC()");
  559. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(2, "ii");
  560. const int32_t index = argv[0]->i;
  561. const int32_t cc = argv[1]->i;
  562. CARLA_SAFE_ASSERT_INT(index >= 0, index);
  563. CARLA_SAFE_ASSERT_INT(cc >= -1 && cc <= 0x5F, cc);
  564. if (index < 0)
  565. return 1;
  566. if (cc < -1 || cc > 0x5F)
  567. return 1;
  568. plugin->setParameterMidiCC(static_cast<uint32_t>(index), static_cast<int16_t>(cc), false, true);
  569. return 0;
  570. }
  571. int CarlaEngineOsc::handleMsgSetParameterMidiChannel(CARLA_ENGINE_OSC_HANDLE_ARGS2)
  572. {
  573. carla_debug("CarlaEngineOsc::handleMsgSetParameterMidiChannel()");
  574. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(2, "ii");
  575. const int32_t index = argv[0]->i;
  576. const int32_t channel = argv[1]->i;
  577. CARLA_SAFE_ASSERT_INT(index >= 0, index);
  578. CARLA_SAFE_ASSERT_INT(channel >= 0 && channel < MAX_MIDI_CHANNELS, channel);
  579. if (index < 0)
  580. return 1;
  581. if (channel < 0 || channel >= MAX_MIDI_CHANNELS)
  582. return 1;
  583. plugin->setParameterMidiChannel(static_cast<uint32_t>(index), static_cast<uint8_t>(channel), false, true);
  584. return 0;
  585. }
  586. int CarlaEngineOsc::handleMsgSetProgram(CARLA_ENGINE_OSC_HANDLE_ARGS2)
  587. {
  588. carla_debug("CarlaEngineOsc::handleMsgSetProgram()");
  589. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(1, "i");
  590. const int32_t index = argv[0]->i;
  591. plugin->setProgram(index, true, false, true);
  592. return 0;
  593. }
  594. int CarlaEngineOsc::handleMsgSetMidiProgram(CARLA_ENGINE_OSC_HANDLE_ARGS2)
  595. {
  596. carla_debug("CarlaEngineOsc::handleMsgSetMidiProgram()");
  597. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(1, "i");
  598. const int32_t index = argv[0]->i;
  599. plugin->setMidiProgram(index, true, false, true);
  600. return 0;
  601. }
  602. int CarlaEngineOsc::handleMsgNoteOn(CARLA_ENGINE_OSC_HANDLE_ARGS2)
  603. {
  604. carla_debug("CarlaEngineOsc::handleMsgNoteOn()");
  605. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(3, "iii");
  606. const int32_t channel = argv[0]->i;
  607. const int32_t note = argv[1]->i;
  608. const int32_t velo = argv[2]->i;
  609. CARLA_SAFE_ASSERT_INT(channel >= 0 && channel < MAX_MIDI_CHANNELS, channel);
  610. CARLA_SAFE_ASSERT_INT(note >= 0 && note < MAX_MIDI_NOTE, note);
  611. CARLA_SAFE_ASSERT_INT(velo >= 0 && velo < MAX_MIDI_VALUE, velo);
  612. if (channel < 0 || channel >= MAX_MIDI_CHANNELS)
  613. return 1;
  614. if (note < 0 || note >= MAX_MIDI_NOTE)
  615. return 1;
  616. if (velo < 0 || velo >= MAX_MIDI_VALUE)
  617. return 1;
  618. plugin->sendMidiSingleNote(static_cast<uint8_t>(channel), static_cast<uint8_t>(note), static_cast<uint8_t>(velo), true, false, true);
  619. return 0;
  620. }
  621. int CarlaEngineOsc::handleMsgNoteOff(CARLA_ENGINE_OSC_HANDLE_ARGS2)
  622. {
  623. carla_debug("CarlaEngineOsc::handleMsgNoteOff()");
  624. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(2, "ii");
  625. const int32_t channel = argv[0]->i;
  626. const int32_t note = argv[1]->i;
  627. CARLA_SAFE_ASSERT_INT(channel >= 0 && channel < MAX_MIDI_CHANNELS, channel);
  628. CARLA_SAFE_ASSERT_INT(note >= 0 && note < MAX_MIDI_NOTE, note);
  629. if (channel < 0 || channel >= MAX_MIDI_CHANNELS)
  630. return 1;
  631. if (note < 0 || note >= MAX_MIDI_NOTE)
  632. return 1;
  633. plugin->sendMidiSingleNote(static_cast<uint8_t>(channel), static_cast<uint8_t>(note), 0, true, false, true);
  634. return 0;
  635. }
  636. #endif
  637. CARLA_BACKEND_END_NAMESPACE