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 26KB

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