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.

2602 lines
92KB

  1. /*
  2. * Carla Native Plugin
  3. * Copyright (C) 2012-2018 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 "CarlaPluginInternal.hpp"
  18. #include "CarlaEngine.hpp"
  19. #include "CarlaMathUtils.hpp"
  20. #include "CarlaNative.h"
  21. #include "water/text/StringArray.h"
  22. using water::jmax;
  23. using water::String;
  24. using water::StringArray;
  25. CARLA_EXTERN_C
  26. std::size_t carla_getNativePluginCount() noexcept;
  27. CARLA_EXTERN_C
  28. const NativePluginDescriptor* carla_getNativePluginDescriptor(const std::size_t index) noexcept;
  29. // -----------------------------------------------------------------------
  30. static LinkedList<const NativePluginDescriptor*> gPluginDescriptors;
  31. void carla_register_native_plugin(const NativePluginDescriptor* desc)
  32. {
  33. gPluginDescriptors.append(desc);
  34. }
  35. // -----------------------------------------------------------------------
  36. static
  37. class NativePluginInitializer
  38. {
  39. public:
  40. NativePluginInitializer() noexcept
  41. : fNeedsInit(true) {}
  42. ~NativePluginInitializer() noexcept
  43. {
  44. gPluginDescriptors.clear();
  45. }
  46. void initIfNeeded() noexcept
  47. {
  48. if (! fNeedsInit)
  49. return;
  50. fNeedsInit = false;
  51. try {
  52. carla_register_all_native_plugins();
  53. } CARLA_SAFE_EXCEPTION("carla_register_all_native_plugins")
  54. }
  55. private:
  56. bool fNeedsInit;
  57. } sPluginInitializer;
  58. // -----------------------------------------------------------------------
  59. std::size_t carla_getNativePluginCount() noexcept
  60. {
  61. sPluginInitializer.initIfNeeded();
  62. return gPluginDescriptors.count();
  63. }
  64. const NativePluginDescriptor* carla_getNativePluginDescriptor(const std::size_t index) noexcept
  65. {
  66. sPluginInitializer.initIfNeeded();
  67. return gPluginDescriptors.getAt(index, nullptr);
  68. }
  69. // -----------------------------------------------------------------------
  70. CARLA_BACKEND_START_NAMESPACE
  71. // -------------------------------------------------------------------
  72. // Fallback data
  73. static const CustomData kCustomDataFallback = { nullptr, nullptr, nullptr };
  74. // -----------------------------------------------------------------------
  75. struct NativePluginMidiData {
  76. uint32_t count;
  77. uint32_t* indexes;
  78. CarlaEngineEventPort** ports;
  79. NativePluginMidiData() noexcept
  80. : count(0),
  81. indexes(nullptr),
  82. ports(nullptr) {}
  83. ~NativePluginMidiData() noexcept
  84. {
  85. CARLA_SAFE_ASSERT_INT(count == 0, count);
  86. CARLA_SAFE_ASSERT(indexes == nullptr);
  87. CARLA_SAFE_ASSERT(ports == nullptr);
  88. }
  89. void createNew(const uint32_t newCount)
  90. {
  91. CARLA_SAFE_ASSERT_INT(count == 0, count);
  92. CARLA_SAFE_ASSERT_RETURN(indexes == nullptr,);
  93. CARLA_SAFE_ASSERT_RETURN(ports == nullptr,);
  94. CARLA_SAFE_ASSERT_RETURN(newCount > 0,);
  95. indexes = new uint32_t[newCount];
  96. ports = new CarlaEngineEventPort*[newCount];
  97. count = newCount;
  98. for (uint32_t i=0; i < newCount; ++i)
  99. indexes[i] = 0;
  100. for (uint32_t i=0; i < newCount; ++i)
  101. ports[i] = nullptr;
  102. }
  103. void clear() noexcept
  104. {
  105. if (ports != nullptr)
  106. {
  107. for (uint32_t i=0; i < count; ++i)
  108. {
  109. if (ports[i] != nullptr)
  110. {
  111. delete ports[i];
  112. ports[i] = nullptr;
  113. }
  114. }
  115. delete[] ports;
  116. ports = nullptr;
  117. }
  118. if (indexes != nullptr)
  119. {
  120. delete[] indexes;
  121. indexes = nullptr;
  122. }
  123. count = 0;
  124. }
  125. void initBuffers() const noexcept
  126. {
  127. for (uint32_t i=0; i < count; ++i)
  128. {
  129. if (ports[i] != nullptr)
  130. ports[i]->initBuffer();
  131. }
  132. }
  133. CARLA_DECLARE_NON_COPY_STRUCT(NativePluginMidiData)
  134. };
  135. // -----------------------------------------------------
  136. class CarlaPluginNative : public CarlaPlugin
  137. {
  138. public:
  139. CarlaPluginNative(CarlaEngine* const engine, const uint id)
  140. : CarlaPlugin(engine, id),
  141. fHandle(nullptr),
  142. fHandle2(nullptr),
  143. fHost(),
  144. fDescriptor(nullptr),
  145. fIsProcessing(false),
  146. fIsOffline(false),
  147. fIsUiAvailable(false),
  148. fIsUiVisible(false),
  149. fAudioInBuffers(nullptr),
  150. fAudioOutBuffers(nullptr),
  151. fMidiEventCount(0),
  152. fCurBufferSize(engine->getBufferSize()),
  153. fCurSampleRate(engine->getSampleRate()),
  154. fMidiIn(),
  155. fMidiOut(),
  156. fTimeInfo()
  157. {
  158. carla_debug("CarlaPluginNative::CarlaPluginNative(%p, %i)", engine, id);
  159. carla_fill(fCurMidiProgs, 0, MAX_MIDI_CHANNELS);
  160. carla_zeroStructs(fMidiEvents, kPluginMaxMidiEvents*2);
  161. carla_zeroStruct(fTimeInfo);
  162. fHost.handle = this;
  163. fHost.resourceDir = carla_strdup(engine->getOptions().resourceDir);
  164. fHost.uiName = nullptr;
  165. fHost.uiParentId = engine->getOptions().frontendWinId;
  166. fHost.get_buffer_size = carla_host_get_buffer_size;
  167. fHost.get_sample_rate = carla_host_get_sample_rate;
  168. fHost.is_offline = carla_host_is_offline;
  169. fHost.get_time_info = carla_host_get_time_info;
  170. fHost.write_midi_event = carla_host_write_midi_event;
  171. fHost.ui_parameter_changed = carla_host_ui_parameter_changed;
  172. fHost.ui_custom_data_changed = carla_host_ui_custom_data_changed;
  173. fHost.ui_closed = carla_host_ui_closed;
  174. fHost.ui_open_file = carla_host_ui_open_file;
  175. fHost.ui_save_file = carla_host_ui_save_file;
  176. fHost.dispatcher = carla_host_dispatcher;
  177. }
  178. ~CarlaPluginNative() override
  179. {
  180. carla_debug("CarlaPluginNative::~CarlaPluginNative()");
  181. // close UI
  182. if (pData->hints & PLUGIN_HAS_CUSTOM_UI)
  183. {
  184. if (fIsUiVisible && fDescriptor != nullptr && fDescriptor->ui_show != nullptr && fHandle != nullptr)
  185. fDescriptor->ui_show(fHandle, false);
  186. #ifndef BUILD_BRIDGE
  187. pData->transientTryCounter = 0;
  188. #endif
  189. }
  190. pData->singleMutex.lock();
  191. pData->masterMutex.lock();
  192. if (pData->client != nullptr && pData->client->isActive())
  193. pData->client->deactivate();
  194. CARLA_ASSERT(! fIsProcessing);
  195. if (pData->active)
  196. {
  197. deactivate();
  198. pData->active = false;
  199. }
  200. if (fDescriptor != nullptr)
  201. {
  202. if (fDescriptor->cleanup != nullptr)
  203. {
  204. if (fHandle != nullptr)
  205. fDescriptor->cleanup(fHandle);
  206. if (fHandle2 != nullptr)
  207. fDescriptor->cleanup(fHandle2);
  208. }
  209. fHandle = nullptr;
  210. fHandle2 = nullptr;
  211. fDescriptor = nullptr;
  212. }
  213. if (fHost.resourceDir != nullptr)
  214. {
  215. delete[] fHost.resourceDir;
  216. fHost.resourceDir = nullptr;
  217. }
  218. if (fHost.uiName != nullptr)
  219. {
  220. delete[] fHost.uiName;
  221. fHost.uiName = nullptr;
  222. }
  223. clearBuffers();
  224. }
  225. // -------------------------------------------------------------------
  226. // Information (base)
  227. PluginType getType() const noexcept override
  228. {
  229. return PLUGIN_INTERNAL;
  230. }
  231. PluginCategory getCategory() const noexcept override
  232. {
  233. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, PLUGIN_CATEGORY_NONE);
  234. return static_cast<PluginCategory>(fDescriptor->category);
  235. }
  236. // -------------------------------------------------------------------
  237. // Information (count)
  238. uint32_t getMidiInCount() const noexcept override
  239. {
  240. return fMidiIn.count;
  241. }
  242. uint32_t getMidiOutCount() const noexcept override
  243. {
  244. return fMidiOut.count;
  245. }
  246. uint32_t getParameterScalePointCount(const uint32_t parameterId) const noexcept override
  247. {
  248. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, 0);
  249. CARLA_SAFE_ASSERT_RETURN(fDescriptor->get_parameter_info != nullptr, 0);
  250. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr, 0);
  251. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0);
  252. // FIXME - try
  253. if (const NativeParameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId))
  254. return param->scalePointCount;
  255. carla_safe_assert("const Parameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId)", __FILE__, __LINE__);
  256. return 0;
  257. }
  258. // -------------------------------------------------------------------
  259. // Information (current data)
  260. // nothing
  261. // -------------------------------------------------------------------
  262. // Information (per-plugin data)
  263. uint getOptionsAvailable() const noexcept override
  264. {
  265. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, 0x0);
  266. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr, 0x0);
  267. bool hasMidiProgs = false;
  268. if (fDescriptor->get_midi_program_count != nullptr)
  269. {
  270. try {
  271. hasMidiProgs = fDescriptor->get_midi_program_count(fHandle) > 0;
  272. } catch (...) {}
  273. }
  274. uint options = 0x0;
  275. // can't disable fixed buffers if using MIDI output
  276. if (fDescriptor->midiOuts == 0 && (fDescriptor->hints & NATIVE_PLUGIN_NEEDS_FIXED_BUFFERS) == 0)
  277. options |= PLUGIN_OPTION_FIXED_BUFFERS;
  278. // can't disable forced stereo if enabled in the engine
  279. if (pData->engine->getOptions().forceStereo)
  280. pass();
  281. // if inputs or outputs are just 1, then yes we can force stereo
  282. else if (pData->audioIn.count == 1 || pData->audioOut.count == 1 || fHandle2 != nullptr)
  283. options |= PLUGIN_OPTION_FORCE_STEREO;
  284. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_CONTROL_CHANGES)
  285. options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  286. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_CHANNEL_PRESSURE)
  287. options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  288. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_NOTE_AFTERTOUCH)
  289. options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  290. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_PITCHBEND)
  291. options |= PLUGIN_OPTION_SEND_PITCHBEND;
  292. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_ALL_SOUND_OFF)
  293. options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  294. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_PROGRAM_CHANGES)
  295. options |= PLUGIN_OPTION_SEND_PROGRAM_CHANGES;
  296. else if (hasMidiProgs)
  297. options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  298. return options;
  299. }
  300. float getParameterValue(const uint32_t parameterId) const noexcept override
  301. {
  302. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, 0.0f);
  303. CARLA_SAFE_ASSERT_RETURN(fDescriptor->get_parameter_value != nullptr, 0.0f);
  304. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr, 0.0f);
  305. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0.0f);
  306. // FIXME - try
  307. return fDescriptor->get_parameter_value(fHandle, parameterId);
  308. }
  309. float getParameterScalePointValue(const uint32_t parameterId, const uint32_t scalePointId) const noexcept override
  310. {
  311. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, 0.0f);
  312. CARLA_SAFE_ASSERT_RETURN(fDescriptor->get_parameter_info != nullptr, 0.0f);
  313. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr, 0.0f);
  314. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0.0f);
  315. // FIXME - try
  316. if (const NativeParameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId))
  317. {
  318. CARLA_SAFE_ASSERT_RETURN(scalePointId < param->scalePointCount, 0.0f);
  319. const NativeParameterScalePoint* scalePoint(&param->scalePoints[scalePointId]);
  320. return scalePoint->value;
  321. }
  322. carla_safe_assert("const Parameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId)", __FILE__, __LINE__);
  323. return 0.0f;
  324. }
  325. void getLabel(char* const strBuf) const noexcept override
  326. {
  327. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  328. if (fDescriptor->label != nullptr)
  329. {
  330. std::strncpy(strBuf, fDescriptor->label, STR_MAX);
  331. return;
  332. }
  333. CarlaPlugin::getLabel(strBuf);
  334. }
  335. void getMaker(char* const strBuf) const noexcept override
  336. {
  337. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  338. if (fDescriptor->maker != nullptr)
  339. {
  340. std::strncpy(strBuf, fDescriptor->maker, STR_MAX);
  341. return;
  342. }
  343. CarlaPlugin::getMaker(strBuf);
  344. }
  345. void getCopyright(char* const strBuf) const noexcept override
  346. {
  347. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  348. if (fDescriptor->copyright != nullptr)
  349. {
  350. std::strncpy(strBuf, fDescriptor->copyright, STR_MAX);
  351. return;
  352. }
  353. CarlaPlugin::getCopyright(strBuf);
  354. }
  355. void getRealName(char* const strBuf) const noexcept override
  356. {
  357. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  358. if (fDescriptor->name != nullptr)
  359. {
  360. std::strncpy(strBuf, fDescriptor->name, STR_MAX);
  361. return;
  362. }
  363. CarlaPlugin::getRealName(strBuf);
  364. }
  365. void getParameterName(const uint32_t parameterId, char* const strBuf) const noexcept override
  366. {
  367. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  368. CARLA_SAFE_ASSERT_RETURN(fDescriptor->get_parameter_info != nullptr,);
  369. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  370. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  371. // FIXME - try
  372. if (const NativeParameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId))
  373. {
  374. if (param->name != nullptr)
  375. {
  376. std::strncpy(strBuf, param->name, STR_MAX);
  377. return;
  378. }
  379. carla_safe_assert("param->name != nullptr", __FILE__, __LINE__);
  380. return CarlaPlugin::getParameterName(parameterId, strBuf);
  381. }
  382. carla_safe_assert("const Parameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId)", __FILE__, __LINE__);
  383. CarlaPlugin::getParameterName(parameterId, strBuf);
  384. }
  385. void getParameterUnit(const uint32_t parameterId, char* const strBuf) const noexcept override
  386. {
  387. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  388. CARLA_SAFE_ASSERT_RETURN(fDescriptor->get_parameter_info != nullptr,);
  389. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  390. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  391. // FIXME - try
  392. if (const NativeParameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId))
  393. {
  394. if (param->unit != nullptr)
  395. {
  396. std::strncpy(strBuf, param->unit, STR_MAX);
  397. return;
  398. }
  399. return CarlaPlugin::getParameterUnit(parameterId, strBuf);
  400. }
  401. carla_safe_assert("const Parameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId)", __FILE__, __LINE__);
  402. CarlaPlugin::getParameterUnit(parameterId, strBuf);
  403. }
  404. void getParameterScalePointLabel(const uint32_t parameterId, const uint32_t scalePointId, char* const strBuf) const noexcept override
  405. {
  406. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  407. CARLA_SAFE_ASSERT_RETURN(fDescriptor->get_parameter_info != nullptr,);
  408. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  409. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  410. // FIXME - try
  411. if (const NativeParameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId))
  412. {
  413. CARLA_SAFE_ASSERT_RETURN(scalePointId < param->scalePointCount,);
  414. const NativeParameterScalePoint* scalePoint(&param->scalePoints[scalePointId]);
  415. if (scalePoint->label != nullptr)
  416. {
  417. std::strncpy(strBuf, scalePoint->label, STR_MAX);
  418. return;
  419. }
  420. carla_safe_assert("scalePoint->label != nullptr", __FILE__, __LINE__);
  421. return CarlaPlugin::getParameterScalePointLabel(parameterId, scalePointId, strBuf);
  422. }
  423. carla_safe_assert("const Parameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId)", __FILE__, __LINE__);
  424. CarlaPlugin::getParameterScalePointLabel(parameterId, scalePointId, strBuf);
  425. }
  426. // -------------------------------------------------------------------
  427. // Set data (state)
  428. void prepareForSave() override
  429. {
  430. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  431. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  432. if (pData->midiprog.count > 0 && fDescriptor->category == NATIVE_PLUGIN_CATEGORY_SYNTH)
  433. {
  434. char strBuf[STR_MAX+1];
  435. std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i",
  436. fCurMidiProgs[0], fCurMidiProgs[1], fCurMidiProgs[2], fCurMidiProgs[3],
  437. fCurMidiProgs[4], fCurMidiProgs[5], fCurMidiProgs[6], fCurMidiProgs[7],
  438. fCurMidiProgs[8], fCurMidiProgs[9], fCurMidiProgs[10], fCurMidiProgs[11],
  439. fCurMidiProgs[12], fCurMidiProgs[13], fCurMidiProgs[14], fCurMidiProgs[15]);
  440. strBuf[STR_MAX] = '\0';
  441. CarlaPlugin::setCustomData(CUSTOM_DATA_TYPE_STRING, "midiPrograms", strBuf, false);
  442. }
  443. if (fDescriptor == nullptr || fDescriptor->get_state == nullptr || (fDescriptor->hints & NATIVE_PLUGIN_USES_STATE) == 0)
  444. return;
  445. if (char* data = fDescriptor->get_state(fHandle))
  446. {
  447. CarlaPlugin::setCustomData(CUSTOM_DATA_TYPE_CHUNK, "State", data, false);
  448. std::free(data);
  449. }
  450. }
  451. // -------------------------------------------------------------------
  452. // Set data (internal stuff)
  453. void setName(const char* const newName) override
  454. {
  455. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  456. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  457. CARLA_SAFE_ASSERT_RETURN(newName != nullptr && newName[0] != '\0',);
  458. char uiName[std::strlen(newName)+6+1];
  459. std::strcpy(uiName, newName);
  460. std::strcat(uiName, " (GUI)");
  461. if (fHost.uiName != nullptr)
  462. delete[] fHost.uiName;
  463. fHost.uiName = carla_strdup(uiName);
  464. if (fDescriptor->dispatcher != nullptr && fIsUiVisible)
  465. fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_UI_NAME_CHANGED, 0, 0, uiName, 0.0f);
  466. CarlaPlugin::setName(newName);
  467. }
  468. void setCtrlChannel(const int8_t channel, const bool sendOsc, const bool sendCallback) noexcept override
  469. {
  470. if (channel >= 0 && channel < MAX_MIDI_CHANNELS && pData->midiprog.count > 0)
  471. pData->midiprog.current = fCurMidiProgs[channel];
  472. CarlaPlugin::setCtrlChannel(channel, sendOsc, sendCallback);
  473. }
  474. // -------------------------------------------------------------------
  475. // Set data (plugin-specific stuff)
  476. void setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  477. {
  478. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  479. CARLA_SAFE_ASSERT_RETURN(fDescriptor->set_parameter_value != nullptr,);
  480. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  481. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  482. const float fixedValue(pData->param.getFixedValue(parameterId, value));
  483. // FIXME - try
  484. fDescriptor->set_parameter_value(fHandle, parameterId, fixedValue);
  485. if (fHandle2 != nullptr)
  486. fDescriptor->set_parameter_value(fHandle2, parameterId, fixedValue);
  487. CarlaPlugin::setParameterValue(parameterId, fixedValue, sendGui, sendOsc, sendCallback);
  488. }
  489. void setParameterValueRT(const uint32_t parameterId, const float value) noexcept override
  490. {
  491. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  492. CARLA_SAFE_ASSERT_RETURN(fDescriptor->set_parameter_value != nullptr,);
  493. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  494. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  495. const float fixedValue(pData->param.getFixedValue(parameterId, value));
  496. // FIXME - try
  497. fDescriptor->set_parameter_value(fHandle, parameterId, fixedValue);
  498. if (fHandle2 != nullptr)
  499. fDescriptor->set_parameter_value(fHandle2, parameterId, fixedValue);
  500. CarlaPlugin::setParameterValueRT(parameterId, fixedValue);
  501. }
  502. void setCustomData(const char* const type, const char* const key, const char* const value, const bool sendGui) override
  503. {
  504. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  505. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  506. CARLA_SAFE_ASSERT_RETURN(type != nullptr && type[0] != '\0',);
  507. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  508. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  509. carla_debug("CarlaPluginNative::setCustomData(%s, %s, %s, %s)", type, key, value, bool2str(sendGui));
  510. if (std::strcmp(type, CUSTOM_DATA_TYPE_PROPERTY) == 0)
  511. return CarlaPlugin::setCustomData(type, key, value, sendGui);
  512. if (std::strcmp(type, CUSTOM_DATA_TYPE_STRING) != 0 && std::strcmp(type, CUSTOM_DATA_TYPE_CHUNK) != 0)
  513. return carla_stderr2("CarlaPluginNative::setCustomData(\"%s\", \"%s\", \"%s\", %s) - type is invalid", type, key, value, bool2str(sendGui));
  514. if (std::strcmp(type, CUSTOM_DATA_TYPE_CHUNK) == 0)
  515. {
  516. if (fDescriptor->set_state != nullptr && (fDescriptor->hints & NATIVE_PLUGIN_USES_STATE) != 0)
  517. {
  518. const ScopedSingleProcessLocker spl(this, true);
  519. fDescriptor->set_state(fHandle, value);
  520. if (fHandle2 != nullptr)
  521. fDescriptor->set_state(fHandle2, value);
  522. }
  523. }
  524. else if (std::strcmp(key, "midiPrograms") == 0 && fDescriptor->set_midi_program != nullptr)
  525. {
  526. StringArray midiProgramList(StringArray::fromTokens(value, ":", ""));
  527. if (midiProgramList.size() == MAX_MIDI_CHANNELS)
  528. {
  529. uint8_t channel = 0;
  530. for (String *it=midiProgramList.begin(), *end=midiProgramList.end(); it != end; ++it)
  531. {
  532. const int index(it->getIntValue());
  533. if (index >= 0 && index < static_cast<int>(pData->midiprog.count))
  534. {
  535. const uint32_t bank = pData->midiprog.data[index].bank;
  536. const uint32_t program = pData->midiprog.data[index].program;
  537. fDescriptor->set_midi_program(fHandle, channel, bank, program);
  538. if (fHandle2 != nullptr)
  539. fDescriptor->set_midi_program(fHandle2, channel, bank, program);
  540. fCurMidiProgs[channel] = index;
  541. if (pData->ctrlChannel == static_cast<int32_t>(channel))
  542. {
  543. pData->midiprog.current = index;
  544. pData->engine->callback(ENGINE_CALLBACK_MIDI_PROGRAM_CHANGED, pData->id, index, 0, 0.0f, nullptr);
  545. }
  546. }
  547. ++channel;
  548. }
  549. CARLA_SAFE_ASSERT(channel == MAX_MIDI_CHANNELS);
  550. }
  551. }
  552. else
  553. {
  554. if (fDescriptor->set_custom_data != nullptr)
  555. {
  556. fDescriptor->set_custom_data(fHandle, key, value);
  557. if (fHandle2 != nullptr)
  558. fDescriptor->set_custom_data(fHandle2, key, value);
  559. }
  560. if (sendGui && fIsUiVisible && fDescriptor->ui_set_custom_data != nullptr)
  561. fDescriptor->ui_set_custom_data(fHandle, key, value);
  562. }
  563. CarlaPlugin::setCustomData(type, key, value, sendGui);
  564. }
  565. void setMidiProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback, const bool doingInit) noexcept override
  566. {
  567. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  568. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  569. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->midiprog.count),);
  570. CARLA_SAFE_ASSERT_RETURN(sendGui || sendOsc || sendCallback || doingInit,);
  571. // TODO, put into check below
  572. if ((pData->hints & PLUGIN_IS_SYNTH) != 0 && (pData->ctrlChannel < 0 || pData->ctrlChannel >= MAX_MIDI_CHANNELS))
  573. return CarlaPlugin::setMidiProgram(index, sendGui, sendOsc, sendCallback, doingInit);
  574. if (index >= 0)
  575. {
  576. const uint8_t channel = uint8_t((pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS) ? pData->ctrlChannel : 0);
  577. const uint32_t bank = pData->midiprog.data[index].bank;
  578. const uint32_t program = pData->midiprog.data[index].program;
  579. const ScopedSingleProcessLocker spl(this, (sendGui || sendOsc || sendCallback));
  580. try {
  581. fDescriptor->set_midi_program(fHandle, channel, bank, program);
  582. } catch(...) {}
  583. if (fHandle2 != nullptr)
  584. {
  585. try {
  586. fDescriptor->set_midi_program(fHandle2, channel, bank, program);
  587. } catch(...) {}
  588. }
  589. fCurMidiProgs[channel] = index;
  590. }
  591. CarlaPlugin::setMidiProgram(index, sendGui, sendOsc, sendCallback, doingInit);
  592. }
  593. void setMidiProgramRT(const uint32_t index) noexcept override
  594. {
  595. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  596. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  597. CARLA_SAFE_ASSERT_RETURN(index < pData->midiprog.count,);
  598. // TODO, put into check below
  599. if ((pData->hints & PLUGIN_IS_SYNTH) != 0 && (pData->ctrlChannel < 0 || pData->ctrlChannel >= MAX_MIDI_CHANNELS))
  600. return CarlaPlugin::setMidiProgramRT(index);
  601. const uint8_t channel = uint8_t((pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS) ? pData->ctrlChannel : 0);
  602. const uint32_t bank = pData->midiprog.data[index].bank;
  603. const uint32_t program = pData->midiprog.data[index].program;
  604. try {
  605. fDescriptor->set_midi_program(fHandle, channel, bank, program);
  606. } catch(...) {}
  607. if (fHandle2 != nullptr)
  608. {
  609. try {
  610. fDescriptor->set_midi_program(fHandle2, channel, bank, program);
  611. } catch(...) {}
  612. }
  613. fCurMidiProgs[channel] = index;
  614. CarlaPlugin::setMidiProgramRT(index);
  615. }
  616. // -------------------------------------------------------------------
  617. // Set ui stuff
  618. void showCustomUI(const bool yesNo) override
  619. {
  620. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  621. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  622. if (fDescriptor->ui_show == nullptr)
  623. return;
  624. fIsUiAvailable = true;
  625. fDescriptor->ui_show(fHandle, yesNo);
  626. // UI might not be available, see NATIVE_HOST_OPCODE_UI_UNAVAILABLE
  627. if (yesNo && ! fIsUiAvailable)
  628. return;
  629. fIsUiVisible = yesNo;
  630. if (! yesNo)
  631. {
  632. #ifndef BUILD_BRIDGE
  633. pData->transientTryCounter = 0;
  634. #endif
  635. return;
  636. }
  637. #ifndef BUILD_BRIDGE
  638. if ((fDescriptor->hints & NATIVE_PLUGIN_USES_PARENT_ID) == 0 && std::strstr(fDescriptor->label, "file") == nullptr)
  639. pData->tryTransient();
  640. #endif
  641. if (fDescriptor->ui_set_custom_data != nullptr)
  642. {
  643. for (LinkedList<CustomData>::Itenerator it = pData->custom.begin2(); it.valid(); it.next())
  644. {
  645. const CustomData& cData(it.getValue(kCustomDataFallback));
  646. CARLA_SAFE_ASSERT_CONTINUE(cData.isValid());
  647. if (std::strcmp(cData.type, CUSTOM_DATA_TYPE_STRING) == 0 && std::strcmp(cData.key, "midiPrograms") != 0)
  648. fDescriptor->ui_set_custom_data(fHandle, cData.key, cData.value);
  649. }
  650. }
  651. if (fDescriptor->ui_set_midi_program != nullptr && pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  652. {
  653. const int32_t index = pData->midiprog.current;
  654. const uint8_t channel = uint8_t((pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS) ? pData->ctrlChannel : 0);
  655. const uint32_t bank = pData->midiprog.data[index].bank;
  656. const uint32_t program = pData->midiprog.data[index].program;
  657. fDescriptor->ui_set_midi_program(fHandle, channel, bank, program);
  658. }
  659. if (fDescriptor->ui_set_parameter_value != nullptr)
  660. {
  661. for (uint32_t i=0; i < pData->param.count; ++i)
  662. fDescriptor->ui_set_parameter_value(fHandle, i, fDescriptor->get_parameter_value(fHandle, i));
  663. }
  664. }
  665. void uiIdle() override
  666. {
  667. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  668. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  669. if (fIsUiVisible && fDescriptor->ui_idle != nullptr)
  670. fDescriptor->ui_idle(fHandle);
  671. CarlaPlugin::uiIdle();
  672. }
  673. // -------------------------------------------------------------------
  674. // Plugin state
  675. void reload() override
  676. {
  677. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr,);
  678. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  679. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  680. carla_debug("CarlaPluginNative::reload() - start");
  681. const EngineProcessMode processMode(pData->engine->getProccessMode());
  682. // Safely disable plugin for reload
  683. const ScopedDisabler sd(this);
  684. if (pData->active)
  685. deactivate();
  686. clearBuffers();
  687. const float sampleRate((float)pData->engine->getSampleRate());
  688. uint32_t aIns, aOuts, mIns, mOuts, params, j;
  689. bool forcedStereoIn, forcedStereoOut;
  690. forcedStereoIn = forcedStereoOut = false;
  691. bool needsCtrlIn, needsCtrlOut;
  692. needsCtrlIn = needsCtrlOut = false;
  693. aIns = fDescriptor->audioIns;
  694. aOuts = fDescriptor->audioOuts;
  695. mIns = fDescriptor->midiIns;
  696. mOuts = fDescriptor->midiOuts;
  697. params = (fDescriptor->get_parameter_count != nullptr && fDescriptor->get_parameter_info != nullptr) ? fDescriptor->get_parameter_count(fHandle) : 0;
  698. if ((pData->options & PLUGIN_OPTION_FORCE_STEREO) != 0 && (aIns == 1 || aOuts == 1) && mIns <= 1 && mOuts <= 1)
  699. {
  700. if (fHandle2 == nullptr)
  701. fHandle2 = fDescriptor->instantiate(&fHost);
  702. if (fHandle2 != nullptr)
  703. {
  704. if (aIns == 1)
  705. {
  706. aIns = 2;
  707. forcedStereoIn = true;
  708. }
  709. if (aOuts == 1)
  710. {
  711. aOuts = 2;
  712. forcedStereoOut = true;
  713. }
  714. }
  715. }
  716. if (aIns > 0)
  717. {
  718. pData->audioIn.createNew(aIns);
  719. fAudioInBuffers = new float*[aIns];
  720. for (uint32_t i=0; i < aIns; ++i)
  721. fAudioInBuffers[i] = nullptr;
  722. }
  723. if (aOuts > 0)
  724. {
  725. pData->audioOut.createNew(aOuts);
  726. fAudioOutBuffers = new float*[aOuts];
  727. needsCtrlIn = true;
  728. for (uint32_t i=0; i < aOuts; ++i)
  729. fAudioOutBuffers[i] = nullptr;
  730. }
  731. if (mIns > 0)
  732. {
  733. fMidiIn.createNew(mIns);
  734. needsCtrlIn = (mIns == 1);
  735. }
  736. if (mOuts > 0)
  737. {
  738. fMidiOut.createNew(mOuts);
  739. needsCtrlOut = (mOuts == 1);
  740. }
  741. if (params > 0)
  742. {
  743. pData->param.createNew(params, true);
  744. }
  745. const uint portNameSize(pData->engine->getMaxPortNameSize());
  746. CarlaString portName;
  747. // Audio Ins
  748. for (j=0; j < aIns; ++j)
  749. {
  750. portName.clear();
  751. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  752. {
  753. portName = pData->name;
  754. portName += ":";
  755. }
  756. if (aIns > 1 && ! forcedStereoIn)
  757. {
  758. portName += "input_";
  759. portName += CarlaString(j+1);
  760. }
  761. else
  762. portName += "input";
  763. portName.truncate(portNameSize);
  764. pData->audioIn.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true, j);
  765. pData->audioIn.ports[j].rindex = j;
  766. if (forcedStereoIn)
  767. {
  768. portName += "_2";
  769. pData->audioIn.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true, 1);
  770. pData->audioIn.ports[1].rindex = j;
  771. break;
  772. }
  773. }
  774. // Audio Outs
  775. for (j=0; j < aOuts; ++j)
  776. {
  777. portName.clear();
  778. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  779. {
  780. portName = pData->name;
  781. portName += ":";
  782. }
  783. if (aOuts > 1 && ! forcedStereoOut)
  784. {
  785. portName += "output_";
  786. portName += CarlaString(j+1);
  787. }
  788. else
  789. portName += "output";
  790. portName.truncate(portNameSize);
  791. pData->audioOut.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false, j);
  792. pData->audioOut.ports[j].rindex = j;
  793. if (forcedStereoOut)
  794. {
  795. portName += "_2";
  796. pData->audioOut.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false, 1);
  797. pData->audioOut.ports[1].rindex = j;
  798. break;
  799. }
  800. }
  801. // MIDI Input (only if multiple)
  802. if (mIns > 1)
  803. {
  804. for (j=0; j < mIns; ++j)
  805. {
  806. portName.clear();
  807. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  808. {
  809. portName = pData->name;
  810. portName += ":";
  811. }
  812. portName += "midi-in_";
  813. portName += CarlaString(j+1);
  814. portName.truncate(portNameSize);
  815. fMidiIn.ports[j] = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true, j);
  816. fMidiIn.indexes[j] = j;
  817. }
  818. pData->event.portIn = fMidiIn.ports[0];
  819. }
  820. // MIDI Output (only if multiple)
  821. if (mOuts > 1)
  822. {
  823. for (j=0; j < mOuts; ++j)
  824. {
  825. portName.clear();
  826. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  827. {
  828. portName = pData->name;
  829. portName += ":";
  830. }
  831. portName += "midi-out_";
  832. portName += CarlaString(j+1);
  833. portName.truncate(portNameSize);
  834. fMidiOut.ports[j] = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false, j);
  835. fMidiOut.indexes[j] = j;
  836. }
  837. pData->event.portOut = fMidiOut.ports[0];
  838. }
  839. for (j=0; j < params; ++j)
  840. {
  841. const NativeParameter* const paramInfo(fDescriptor->get_parameter_info(fHandle, j));
  842. CARLA_SAFE_ASSERT_CONTINUE(paramInfo != nullptr);
  843. pData->param.data[j].type = PARAMETER_UNKNOWN;
  844. pData->param.data[j].index = static_cast<int32_t>(j);
  845. pData->param.data[j].rindex = static_cast<int32_t>(j);
  846. float min, max, def, step, stepSmall, stepLarge;
  847. // min value
  848. min = paramInfo->ranges.min;
  849. // max value
  850. max = paramInfo->ranges.max;
  851. if (min > max)
  852. max = min;
  853. else if (max < min)
  854. min = max;
  855. if (carla_isEqual(min, max))
  856. {
  857. carla_stderr2("WARNING - Broken plugin parameter '%s': max == min", paramInfo->name);
  858. max = min + 0.1f;
  859. }
  860. // default value
  861. def = paramInfo->ranges.def;
  862. if (def < min)
  863. def = min;
  864. else if (def > max)
  865. def = max;
  866. if (paramInfo->hints & NATIVE_PARAMETER_USES_SAMPLE_RATE)
  867. {
  868. min *= sampleRate;
  869. max *= sampleRate;
  870. def *= sampleRate;
  871. pData->param.data[j].hints |= PARAMETER_USES_SAMPLERATE;
  872. }
  873. if (paramInfo->hints & NATIVE_PARAMETER_IS_BOOLEAN)
  874. {
  875. step = max - min;
  876. stepSmall = step;
  877. stepLarge = step;
  878. pData->param.data[j].hints |= PARAMETER_IS_BOOLEAN;
  879. }
  880. else if (paramInfo->hints & NATIVE_PARAMETER_IS_INTEGER)
  881. {
  882. step = 1.0f;
  883. stepSmall = 1.0f;
  884. stepLarge = 10.0f;
  885. pData->param.data[j].hints |= PARAMETER_IS_INTEGER;
  886. }
  887. else
  888. {
  889. float range = max - min;
  890. step = range/100.0f;
  891. stepSmall = range/1000.0f;
  892. stepLarge = range/10.0f;
  893. }
  894. if (paramInfo->hints & NATIVE_PARAMETER_IS_OUTPUT)
  895. {
  896. pData->param.data[j].type = PARAMETER_OUTPUT;
  897. needsCtrlOut = true;
  898. }
  899. else
  900. {
  901. pData->param.data[j].type = PARAMETER_INPUT;
  902. needsCtrlIn = true;
  903. }
  904. // extra parameter hints
  905. if (paramInfo->hints & NATIVE_PARAMETER_IS_ENABLED)
  906. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  907. if (paramInfo->hints & NATIVE_PARAMETER_IS_AUTOMABLE)
  908. pData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  909. if (paramInfo->hints & NATIVE_PARAMETER_IS_LOGARITHMIC)
  910. pData->param.data[j].hints |= PARAMETER_IS_LOGARITHMIC;
  911. if (paramInfo->hints & NATIVE_PARAMETER_USES_SCALEPOINTS)
  912. pData->param.data[j].hints |= PARAMETER_USES_SCALEPOINTS;
  913. pData->param.ranges[j].min = min;
  914. pData->param.ranges[j].max = max;
  915. pData->param.ranges[j].def = def;
  916. pData->param.ranges[j].step = step;
  917. pData->param.ranges[j].stepSmall = stepSmall;
  918. pData->param.ranges[j].stepLarge = stepLarge;
  919. }
  920. if (needsCtrlIn && mIns <= 1)
  921. {
  922. portName.clear();
  923. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  924. {
  925. portName = pData->name;
  926. portName += ":";
  927. }
  928. portName += "events-in";
  929. portName.truncate(portNameSize);
  930. pData->event.portIn = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true, 0);
  931. }
  932. if (needsCtrlOut && mOuts <= 1)
  933. {
  934. portName.clear();
  935. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  936. {
  937. portName = pData->name;
  938. portName += ":";
  939. }
  940. portName += "events-out";
  941. portName.truncate(portNameSize);
  942. pData->event.portOut = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false, 0);
  943. }
  944. if (forcedStereoIn || forcedStereoOut)
  945. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  946. else
  947. pData->options &= ~PLUGIN_OPTION_FORCE_STEREO;
  948. // plugin hints
  949. pData->hints = 0x0;
  950. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  951. pData->hints |= PLUGIN_CAN_DRYWET;
  952. if (aOuts > 0)
  953. pData->hints |= PLUGIN_CAN_VOLUME;
  954. if (aOuts >= 2 && aOuts % 2 == 0)
  955. pData->hints |= PLUGIN_CAN_BALANCE;
  956. // native plugin hints
  957. if (fDescriptor->hints & NATIVE_PLUGIN_IS_RTSAFE)
  958. pData->hints |= PLUGIN_IS_RTSAFE;
  959. if (fDescriptor->hints & NATIVE_PLUGIN_IS_SYNTH)
  960. pData->hints |= PLUGIN_IS_SYNTH;
  961. if (fDescriptor->hints & NATIVE_PLUGIN_HAS_UI)
  962. pData->hints |= PLUGIN_HAS_CUSTOM_UI;
  963. if (fDescriptor->hints & NATIVE_PLUGIN_NEEDS_FIXED_BUFFERS)
  964. pData->hints |= PLUGIN_NEEDS_FIXED_BUFFERS;
  965. if (fDescriptor->hints & NATIVE_PLUGIN_NEEDS_UI_MAIN_THREAD)
  966. pData->hints |= PLUGIN_NEEDS_UI_MAIN_THREAD;
  967. if (fDescriptor->hints & NATIVE_PLUGIN_USES_MULTI_PROGS)
  968. pData->hints |= PLUGIN_USES_MULTI_PROGS;
  969. // extra plugin hints
  970. pData->extraHints = 0x0;
  971. if (aIns <= 2 && aOuts <= 2 && (aIns == aOuts || aIns == 0 || aOuts == 0) && mIns <= 1 && mOuts <= 1)
  972. pData->extraHints |= PLUGIN_EXTRA_HINT_CAN_RUN_RACK;
  973. bufferSizeChanged(pData->engine->getBufferSize());
  974. reloadPrograms(true);
  975. if (pData->active)
  976. activate();
  977. carla_debug("CarlaPluginNative::reload() - end");
  978. }
  979. void reloadPrograms(const bool doInit) override
  980. {
  981. carla_debug("CarlaPluginNative::reloadPrograms(%s)", bool2str(doInit));
  982. uint32_t i, oldCount = pData->midiprog.count;
  983. const int32_t current = pData->midiprog.current;
  984. // Delete old programs
  985. pData->midiprog.clear();
  986. // Query new programs
  987. uint32_t count = 0;
  988. if (fDescriptor->get_midi_program_count != nullptr && fDescriptor->get_midi_program_info != nullptr && fDescriptor->set_midi_program != nullptr)
  989. count = fDescriptor->get_midi_program_count(fHandle);
  990. if (count > 0)
  991. {
  992. pData->midiprog.createNew(count);
  993. // Update data
  994. for (i=0; i < count; ++i)
  995. {
  996. const NativeMidiProgram* const mpDesc(fDescriptor->get_midi_program_info(fHandle, i));
  997. CARLA_SAFE_ASSERT_CONTINUE(mpDesc != nullptr);
  998. pData->midiprog.data[i].bank = mpDesc->bank;
  999. pData->midiprog.data[i].program = mpDesc->program;
  1000. pData->midiprog.data[i].name = carla_strdup(mpDesc->name);
  1001. }
  1002. }
  1003. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1004. // Update OSC Names
  1005. if (pData->engine->isOscControlRegistered() && pData->id < pData->engine->getCurrentPluginCount())
  1006. {
  1007. pData->engine->oscSend_control_set_midi_program_count(pData->id, count);
  1008. for (i=0; i < count; ++i)
  1009. pData->engine->oscSend_control_set_midi_program_data(pData->id, i, pData->midiprog.data[i].bank, pData->midiprog.data[i].program, pData->midiprog.data[i].name);
  1010. }
  1011. #endif
  1012. if (doInit)
  1013. {
  1014. if (count > 0)
  1015. setMidiProgram(0, false, false, false, true);
  1016. }
  1017. else
  1018. {
  1019. // Check if current program is invalid
  1020. bool programChanged = false;
  1021. if (count == oldCount+1)
  1022. {
  1023. // one midi program added, probably created by user
  1024. pData->midiprog.current = static_cast<int32_t>(oldCount);
  1025. programChanged = true;
  1026. }
  1027. else if (current < 0 && count > 0)
  1028. {
  1029. // programs exist now, but not before
  1030. pData->midiprog.current = 0;
  1031. programChanged = true;
  1032. }
  1033. else if (current >= 0 && count == 0)
  1034. {
  1035. // programs existed before, but not anymore
  1036. pData->midiprog.current = -1;
  1037. programChanged = true;
  1038. }
  1039. else if (current >= static_cast<int32_t>(count))
  1040. {
  1041. // current midi program > count
  1042. pData->midiprog.current = 0;
  1043. programChanged = true;
  1044. }
  1045. else
  1046. {
  1047. // no change
  1048. pData->midiprog.current = current;
  1049. }
  1050. if (programChanged)
  1051. setMidiProgram(pData->midiprog.current, true, true, true, false);
  1052. pData->engine->callback(ENGINE_CALLBACK_RELOAD_PROGRAMS, pData->id, 0, 0, 0.0f, nullptr);
  1053. }
  1054. }
  1055. // -------------------------------------------------------------------
  1056. // Plugin processing
  1057. void activate() noexcept override
  1058. {
  1059. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1060. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1061. if (fDescriptor->activate != nullptr)
  1062. {
  1063. try {
  1064. fDescriptor->activate(fHandle);
  1065. } catch(...) {}
  1066. if (fHandle2 != nullptr)
  1067. {
  1068. try {
  1069. fDescriptor->activate(fHandle2);
  1070. } catch(...) {}
  1071. }
  1072. }
  1073. }
  1074. void deactivate() noexcept override
  1075. {
  1076. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1077. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1078. if (fDescriptor->deactivate != nullptr)
  1079. {
  1080. try {
  1081. fDescriptor->deactivate(fHandle);
  1082. } catch(...) {}
  1083. if (fHandle2 != nullptr)
  1084. {
  1085. try {
  1086. fDescriptor->deactivate(fHandle2);
  1087. } catch(...) {}
  1088. }
  1089. }
  1090. }
  1091. void process(const float** const audioIn, float** const audioOut, const float** const cvIn, float** const cvOut, const uint32_t frames) override
  1092. {
  1093. // --------------------------------------------------------------------------------------------------------
  1094. // Check if active
  1095. if (! pData->active)
  1096. {
  1097. // disable any output sound
  1098. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1099. carla_zeroFloats(audioOut[i], frames);
  1100. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  1101. carla_zeroFloats(cvOut[i], frames);
  1102. return;
  1103. }
  1104. fMidiEventCount = 0;
  1105. carla_zeroStructs(fMidiEvents, kPluginMaxMidiEvents*2);
  1106. // --------------------------------------------------------------------------------------------------------
  1107. // Check if needs reset
  1108. if (pData->needsReset)
  1109. {
  1110. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1111. {
  1112. fMidiEventCount = MAX_MIDI_CHANNELS*2;
  1113. for (uint8_t k=0, i=MAX_MIDI_CHANNELS; k < MAX_MIDI_CHANNELS; ++k)
  1114. {
  1115. fMidiEvents[k].data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (k & MIDI_CHANNEL_BIT));
  1116. fMidiEvents[k].data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  1117. fMidiEvents[k].data[2] = 0;
  1118. fMidiEvents[k].size = 3;
  1119. fMidiEvents[k+i].data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (k & MIDI_CHANNEL_BIT));
  1120. fMidiEvents[k+i].data[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  1121. fMidiEvents[k+i].data[2] = 0;
  1122. fMidiEvents[k+i].size = 3;
  1123. }
  1124. }
  1125. else if (pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  1126. {
  1127. fMidiEventCount = MAX_MIDI_NOTE;
  1128. for (uint8_t k=0; k < MAX_MIDI_NOTE; ++k)
  1129. {
  1130. fMidiEvents[k].data[0] = uint8_t(MIDI_STATUS_NOTE_OFF | (pData->ctrlChannel & MIDI_CHANNEL_BIT));
  1131. fMidiEvents[k].data[1] = k;
  1132. fMidiEvents[k].data[2] = 0;
  1133. fMidiEvents[k].size = 3;
  1134. }
  1135. }
  1136. pData->needsReset = false;
  1137. }
  1138. // --------------------------------------------------------------------------------------------------------
  1139. // Set TimeInfo
  1140. const EngineTimeInfo& timeInfo(pData->engine->getTimeInfo());
  1141. fTimeInfo.playing = timeInfo.playing;
  1142. fTimeInfo.frame = timeInfo.frame;
  1143. fTimeInfo.usecs = timeInfo.usecs;
  1144. if (timeInfo.bbt.valid)
  1145. {
  1146. fTimeInfo.bbt.valid = true;
  1147. fTimeInfo.bbt.bar = timeInfo.bbt.bar;
  1148. fTimeInfo.bbt.beat = timeInfo.bbt.beat;
  1149. fTimeInfo.bbt.tick = timeInfo.bbt.tick;
  1150. fTimeInfo.bbt.barStartTick = timeInfo.bbt.barStartTick;
  1151. fTimeInfo.bbt.beatsPerBar = timeInfo.bbt.beatsPerBar;
  1152. fTimeInfo.bbt.beatType = timeInfo.bbt.beatType;
  1153. fTimeInfo.bbt.ticksPerBeat = timeInfo.bbt.ticksPerBeat;
  1154. fTimeInfo.bbt.beatsPerMinute = timeInfo.bbt.beatsPerMinute;
  1155. }
  1156. else
  1157. {
  1158. fTimeInfo.bbt.valid = false;
  1159. }
  1160. // --------------------------------------------------------------------------------------------------------
  1161. // Event Input and Processing
  1162. if (pData->event.portIn != nullptr)
  1163. {
  1164. // ----------------------------------------------------------------------------------------------------
  1165. // MIDI Input (External)
  1166. if (pData->extNotes.mutex.tryLock())
  1167. {
  1168. ExternalMidiNote note = { 0, 0, 0 };
  1169. for (; fMidiEventCount < kPluginMaxMidiEvents*2 && ! pData->extNotes.data.isEmpty();)
  1170. {
  1171. note = pData->extNotes.data.getFirst(note, true);
  1172. CARLA_SAFE_ASSERT_CONTINUE(note.channel >= 0 && note.channel < MAX_MIDI_CHANNELS);
  1173. NativeMidiEvent& nativeEvent(fMidiEvents[fMidiEventCount++]);
  1174. nativeEvent.data[0] = uint8_t((note.velo > 0 ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF) | (note.channel & MIDI_CHANNEL_BIT));
  1175. nativeEvent.data[1] = note.note;
  1176. nativeEvent.data[2] = note.velo;
  1177. nativeEvent.size = 3;
  1178. }
  1179. pData->extNotes.mutex.unlock();
  1180. } // End of MIDI Input (External)
  1181. // ----------------------------------------------------------------------------------------------------
  1182. // Event Input (System)
  1183. #ifndef BUILD_BRIDGE
  1184. bool allNotesOffSent = false;
  1185. #endif
  1186. bool sampleAccurate = (pData->options & PLUGIN_OPTION_FIXED_BUFFERS) == 0;
  1187. uint32_t startTime = 0;
  1188. uint32_t timeOffset = 0;
  1189. uint32_t nextBankId;
  1190. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  1191. nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
  1192. else
  1193. nextBankId = 0;
  1194. for (uint32_t m=0, max=jmax(1U, fMidiIn.count); m < max; ++m)
  1195. {
  1196. CarlaEngineEventPort* const eventPort(m == 0 ? pData->event.portIn : fMidiIn.ports[m]);
  1197. for (uint32_t i=0, numEvents=eventPort->getEventCount(); i < numEvents; ++i)
  1198. {
  1199. const EngineEvent& event(eventPort->getEvent(i));
  1200. if (event.time >= frames)
  1201. continue;
  1202. CARLA_ASSERT_INT2(event.time >= timeOffset, event.time, timeOffset);
  1203. if (event.time > timeOffset && sampleAccurate)
  1204. {
  1205. if (processSingle(audioIn, audioOut, cvIn, cvOut, event.time - timeOffset, timeOffset))
  1206. {
  1207. startTime = 0;
  1208. timeOffset = event.time;
  1209. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  1210. nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
  1211. else
  1212. nextBankId = 0;
  1213. if (fMidiEventCount > 0)
  1214. {
  1215. carla_zeroStructs(fMidiEvents, fMidiEventCount);
  1216. fMidiEventCount = 0;
  1217. }
  1218. }
  1219. else
  1220. startTime += timeOffset;
  1221. }
  1222. // Control change
  1223. switch (event.type)
  1224. {
  1225. case kEngineEventTypeNull:
  1226. break;
  1227. case kEngineEventTypeControl: {
  1228. const EngineControlEvent& ctrlEvent = event.ctrl;
  1229. switch (ctrlEvent.type)
  1230. {
  1231. case kEngineControlEventTypeNull:
  1232. break;
  1233. case kEngineControlEventTypeParameter: {
  1234. #ifndef BUILD_BRIDGE
  1235. // Control backend stuff
  1236. if (event.channel == pData->ctrlChannel)
  1237. {
  1238. float value;
  1239. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_DRYWET) > 0)
  1240. {
  1241. value = ctrlEvent.value;
  1242. setDryWetRT(value);
  1243. }
  1244. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_VOLUME) > 0)
  1245. {
  1246. value = ctrlEvent.value*127.0f/100.0f;
  1247. setVolumeRT(value);
  1248. }
  1249. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_BALANCE) > 0)
  1250. {
  1251. float left, right;
  1252. value = ctrlEvent.value/0.5f - 1.0f;
  1253. if (value < 0.0f)
  1254. {
  1255. left = -1.0f;
  1256. right = (value*2.0f)+1.0f;
  1257. }
  1258. else if (value > 0.0f)
  1259. {
  1260. left = (value*2.0f)-1.0f;
  1261. right = 1.0f;
  1262. }
  1263. else
  1264. {
  1265. left = -1.0f;
  1266. right = 1.0f;
  1267. }
  1268. setBalanceLeftRT(left);
  1269. setBalanceRightRT(right);
  1270. }
  1271. }
  1272. #endif
  1273. // Control plugin parameters
  1274. for (uint32_t k=0; k < pData->param.count; ++k)
  1275. {
  1276. if (pData->param.data[k].midiChannel != event.channel)
  1277. continue;
  1278. if (pData->param.data[k].midiCC != ctrlEvent.param)
  1279. continue;
  1280. if (pData->param.data[k].type != PARAMETER_INPUT)
  1281. continue;
  1282. if ((pData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  1283. continue;
  1284. float value;
  1285. if (pData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  1286. {
  1287. value = (ctrlEvent.value < 0.5f) ? pData->param.ranges[k].min : pData->param.ranges[k].max;
  1288. }
  1289. else
  1290. {
  1291. if (pData->param.data[k].hints & PARAMETER_IS_LOGARITHMIC)
  1292. value = pData->param.ranges[k].getUnnormalizedLogValue(ctrlEvent.value);
  1293. else
  1294. value = pData->param.ranges[k].getUnnormalizedValue(ctrlEvent.value);
  1295. if (pData->param.data[k].hints & PARAMETER_IS_INTEGER)
  1296. value = std::rint(value);
  1297. }
  1298. setParameterValueRT(k, value);
  1299. }
  1300. if ((pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0 && ctrlEvent.param < MAX_MIDI_CONTROL)
  1301. {
  1302. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1303. continue;
  1304. NativeMidiEvent& nativeEvent(fMidiEvents[fMidiEventCount++]);
  1305. carla_zeroStruct(nativeEvent);
  1306. nativeEvent.time = sampleAccurate ? startTime : event.time;
  1307. nativeEvent.data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  1308. nativeEvent.data[1] = uint8_t(ctrlEvent.param);
  1309. nativeEvent.data[2] = uint8_t(ctrlEvent.value*127.0f);
  1310. nativeEvent.size = 3;
  1311. }
  1312. break;
  1313. } // case kEngineControlEventTypeParameter
  1314. case kEngineControlEventTypeMidiBank:
  1315. if (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES)
  1316. {
  1317. if (event.channel == pData->ctrlChannel)
  1318. nextBankId = ctrlEvent.param;
  1319. }
  1320. else if (pData->options & PLUGIN_OPTION_SEND_PROGRAM_CHANGES)
  1321. {
  1322. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1323. continue;
  1324. NativeMidiEvent& nativeEvent(fMidiEvents[fMidiEventCount++]);
  1325. carla_zeroStruct(nativeEvent);
  1326. nativeEvent.time = sampleAccurate ? startTime : event.time;
  1327. nativeEvent.data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  1328. nativeEvent.data[1] = MIDI_CONTROL_BANK_SELECT;
  1329. nativeEvent.data[2] = uint8_t(ctrlEvent.param);
  1330. nativeEvent.size = 3;
  1331. }
  1332. break;
  1333. case kEngineControlEventTypeMidiProgram:
  1334. if (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES)
  1335. {
  1336. if (event.channel < MAX_MIDI_CHANNELS)
  1337. {
  1338. const uint32_t nextProgramId(ctrlEvent.param);
  1339. for (uint32_t k=0; k < pData->midiprog.count; ++k)
  1340. {
  1341. if (pData->midiprog.data[k].bank == nextBankId && pData->midiprog.data[k].program == nextProgramId)
  1342. {
  1343. fDescriptor->set_midi_program(fHandle, event.channel, nextBankId, nextProgramId);
  1344. if (fHandle2 != nullptr)
  1345. fDescriptor->set_midi_program(fHandle2, event.channel, nextBankId, nextProgramId);
  1346. fCurMidiProgs[event.channel] = static_cast<int32_t>(k);
  1347. if (event.channel == pData->ctrlChannel)
  1348. pData->postponeRtEvent(kPluginPostRtEventMidiProgramChange, static_cast<int32_t>(k), 0, 0.0f);
  1349. break;
  1350. }
  1351. }
  1352. }
  1353. }
  1354. else if (pData->options & PLUGIN_OPTION_SEND_PROGRAM_CHANGES)
  1355. {
  1356. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1357. continue;
  1358. NativeMidiEvent& nativeEvent(fMidiEvents[fMidiEventCount++]);
  1359. carla_zeroStruct(nativeEvent);
  1360. nativeEvent.time = sampleAccurate ? startTime : event.time;
  1361. nativeEvent.data[0] = uint8_t(MIDI_STATUS_PROGRAM_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  1362. nativeEvent.data[1] = uint8_t(ctrlEvent.param);
  1363. nativeEvent.size = 2;
  1364. }
  1365. break;
  1366. case kEngineControlEventTypeAllSoundOff:
  1367. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1368. {
  1369. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1370. continue;
  1371. NativeMidiEvent& nativeEvent(fMidiEvents[fMidiEventCount++]);
  1372. carla_zeroStruct(nativeEvent);
  1373. nativeEvent.time = sampleAccurate ? startTime : event.time;
  1374. nativeEvent.data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  1375. nativeEvent.data[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  1376. nativeEvent.data[2] = 0;
  1377. nativeEvent.size = 3;
  1378. }
  1379. break;
  1380. case kEngineControlEventTypeAllNotesOff:
  1381. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1382. {
  1383. #ifndef BUILD_BRIDGE
  1384. if (event.channel == pData->ctrlChannel && ! allNotesOffSent)
  1385. {
  1386. allNotesOffSent = true;
  1387. sendMidiAllNotesOffToCallback();
  1388. }
  1389. #endif
  1390. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1391. continue;
  1392. NativeMidiEvent& nativeEvent(fMidiEvents[fMidiEventCount++]);
  1393. carla_zeroStruct(nativeEvent);
  1394. nativeEvent.time = sampleAccurate ? startTime : event.time;
  1395. nativeEvent.data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  1396. nativeEvent.data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  1397. nativeEvent.data[2] = 0;
  1398. nativeEvent.size = 3;
  1399. }
  1400. break;
  1401. }
  1402. break;
  1403. }
  1404. case kEngineEventTypeMidi: {
  1405. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1406. continue;
  1407. const EngineMidiEvent& midiEvent(event.midi);
  1408. if (midiEvent.size > 4)
  1409. continue;
  1410. #ifdef CARLA_PROPER_CPP11_SUPPORT
  1411. static_assert(4 <= EngineMidiEvent::kDataSize, "Incorrect data");
  1412. #endif
  1413. uint8_t status = uint8_t(MIDI_GET_STATUS_FROM_DATA(midiEvent.data));
  1414. if (status == MIDI_STATUS_CHANNEL_PRESSURE && (pData->options & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) == 0)
  1415. continue;
  1416. if (status == MIDI_STATUS_CONTROL_CHANGE && (pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) == 0)
  1417. continue;
  1418. if (status == MIDI_STATUS_POLYPHONIC_AFTERTOUCH && (pData->options & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) == 0)
  1419. continue;
  1420. if (status == MIDI_STATUS_PITCH_WHEEL_CONTROL && (pData->options & PLUGIN_OPTION_SEND_PITCHBEND) == 0)
  1421. continue;
  1422. // Fix bad note-off
  1423. if (status == MIDI_STATUS_NOTE_ON && midiEvent.data[2] == 0)
  1424. status = MIDI_STATUS_NOTE_OFF;
  1425. NativeMidiEvent& nativeEvent(fMidiEvents[fMidiEventCount++]);
  1426. carla_zeroStruct(nativeEvent);
  1427. nativeEvent.port = midiEvent.port;
  1428. nativeEvent.time = sampleAccurate ? startTime : event.time;
  1429. nativeEvent.size = midiEvent.size;
  1430. nativeEvent.data[0] = uint8_t(status | (event.channel & MIDI_CHANNEL_BIT));
  1431. nativeEvent.data[1] = midiEvent.size >= 2 ? midiEvent.data[1] : 0;
  1432. nativeEvent.data[2] = midiEvent.size >= 3 ? midiEvent.data[2] : 0;
  1433. nativeEvent.data[3] = midiEvent.size == 4 ? midiEvent.data[3] : 0;
  1434. if (status == MIDI_STATUS_NOTE_ON)
  1435. pData->postponeRtEvent(kPluginPostRtEventNoteOn, event.channel, midiEvent.data[1], midiEvent.data[2]);
  1436. else if (status == MIDI_STATUS_NOTE_OFF)
  1437. pData->postponeRtEvent(kPluginPostRtEventNoteOff, event.channel, midiEvent.data[1], 0.0f);
  1438. } break;
  1439. } // switch (event.type)
  1440. }
  1441. pData->postRtEvents.trySplice();
  1442. if (frames > timeOffset)
  1443. processSingle(audioIn, audioOut, cvIn, cvOut, frames - timeOffset, timeOffset);
  1444. } // eventPort
  1445. } // End of Event Input and Processing
  1446. // --------------------------------------------------------------------------------------------------------
  1447. // Plugin processing (no events)
  1448. else
  1449. {
  1450. processSingle(audioIn, audioOut, cvIn, cvOut, frames, 0);
  1451. } // End of Plugin processing (no events)
  1452. // --------------------------------------------------------------------------------------------------------
  1453. // Control and MIDI Output
  1454. if (pData->event.portOut != nullptr)
  1455. {
  1456. #ifndef BUILD_BRIDGE
  1457. float value, curValue;
  1458. for (uint32_t k=0; k < pData->param.count; ++k)
  1459. {
  1460. if (pData->param.data[k].type != PARAMETER_OUTPUT)
  1461. continue;
  1462. curValue = fDescriptor->get_parameter_value(fHandle, k);
  1463. pData->param.ranges[k].fixValue(curValue);
  1464. if (pData->param.data[k].midiCC > 0)
  1465. {
  1466. value = pData->param.ranges[k].getNormalizedValue(curValue);
  1467. pData->event.portOut->writeControlEvent(0, pData->param.data[k].midiChannel, kEngineControlEventTypeParameter, static_cast<uint16_t>(pData->param.data[k].midiCC), value);
  1468. }
  1469. }
  1470. #endif
  1471. // reverse lookup MIDI events
  1472. for (uint32_t k = (kPluginMaxMidiEvents*2)-1; k >= fMidiEventCount; --k)
  1473. {
  1474. if (fMidiEvents[k].data[0] == 0)
  1475. break;
  1476. const uint8_t channel = uint8_t(MIDI_GET_CHANNEL_FROM_DATA(fMidiEvents[k].data));
  1477. const uint8_t port = fMidiEvents[k].port;
  1478. if (fMidiOut.count > 1 && port < fMidiOut.count)
  1479. fMidiOut.ports[port]->writeMidiEvent(fMidiEvents[k].time, channel, fMidiEvents[k].size, fMidiEvents[k].data);
  1480. else
  1481. pData->event.portOut->writeMidiEvent(fMidiEvents[k].time, channel, fMidiEvents[k].size, fMidiEvents[k].data);
  1482. }
  1483. } // End of Control and MIDI Output
  1484. }
  1485. bool processSingle(const float** const audioIn, float** const audioOut, const float** const cvIn, float** const cvOut, const uint32_t frames, const uint32_t timeOffset)
  1486. {
  1487. CARLA_SAFE_ASSERT_RETURN(frames > 0, false);
  1488. if (pData->audioIn.count > 0)
  1489. {
  1490. CARLA_SAFE_ASSERT_RETURN(audioIn != nullptr, false);
  1491. }
  1492. if (pData->audioOut.count > 0)
  1493. {
  1494. CARLA_SAFE_ASSERT_RETURN(audioOut != nullptr, false);
  1495. }
  1496. if (pData->cvIn.count > 0)
  1497. {
  1498. CARLA_SAFE_ASSERT_RETURN(cvIn != nullptr, false);
  1499. }
  1500. if (pData->cvOut.count > 0)
  1501. {
  1502. CARLA_SAFE_ASSERT_RETURN(cvOut != nullptr, false);
  1503. }
  1504. // --------------------------------------------------------------------------------------------------------
  1505. // Try lock, silence otherwise
  1506. if (fIsOffline)
  1507. {
  1508. pData->singleMutex.lock();
  1509. }
  1510. else if (! pData->singleMutex.tryLock())
  1511. {
  1512. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1513. {
  1514. for (uint32_t k=0; k < frames; ++k)
  1515. audioOut[i][k+timeOffset] = 0.0f;
  1516. }
  1517. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  1518. {
  1519. for (uint32_t k=0; k < frames; ++k)
  1520. cvOut[i][k+timeOffset] = 0.0f;
  1521. }
  1522. return false;
  1523. }
  1524. // --------------------------------------------------------------------------------------------------------
  1525. // Set audio buffers
  1526. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1527. carla_copyFloats(fAudioInBuffers[i], audioIn[i]+timeOffset, frames);
  1528. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1529. carla_zeroFloats(fAudioOutBuffers[i], frames);
  1530. #if 0
  1531. // --------------------------------------------------------------------------------------------------------
  1532. // Set CV buffers
  1533. for (uint32_t i=0; i < pData->cvIn.count; ++i)
  1534. carla_copyFloats(fCvInBuffers[i], cvIn[i]+timeOffset, frames);
  1535. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  1536. carla_zeroFloats(fCvOutBuffers[i], frames);
  1537. #endif
  1538. // --------------------------------------------------------------------------------------------------------
  1539. // Run plugin
  1540. fIsProcessing = true;
  1541. if (fHandle2 == nullptr)
  1542. {
  1543. fDescriptor->process(fHandle, fAudioInBuffers, fAudioOutBuffers, frames, fMidiEvents, fMidiEventCount);
  1544. }
  1545. else
  1546. {
  1547. fDescriptor->process(fHandle,
  1548. (pData->audioIn.count > 0) ? &fAudioInBuffers[0] : nullptr,
  1549. (pData->audioOut.count > 0) ? &fAudioOutBuffers[0] : nullptr,
  1550. frames, fMidiEvents, fMidiEventCount);
  1551. fDescriptor->process(fHandle2,
  1552. (pData->audioIn.count > 0) ? &fAudioInBuffers[1] : nullptr,
  1553. (pData->audioOut.count > 0) ? &fAudioOutBuffers[1] : nullptr,
  1554. frames, fMidiEvents, fMidiEventCount);
  1555. }
  1556. fIsProcessing = false;
  1557. fTimeInfo.frame += frames;
  1558. #ifndef BUILD_BRIDGE
  1559. // --------------------------------------------------------------------------------------------------------
  1560. // Post-processing (dry/wet, volume and balance)
  1561. {
  1562. const bool doDryWet = (pData->hints & PLUGIN_CAN_DRYWET) != 0 && carla_isNotEqual(pData->postProc.dryWet, 1.0f);
  1563. const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) != 0 && ! (carla_isEqual(pData->postProc.balanceLeft, -1.0f) && carla_isEqual(pData->postProc.balanceRight, 1.0f));
  1564. bool isPair;
  1565. float bufValue, oldBufLeft[doBalance ? frames : 1];
  1566. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1567. {
  1568. // Dry/Wet
  1569. if (doDryWet)
  1570. {
  1571. for (uint32_t k=0; k < frames; ++k)
  1572. {
  1573. bufValue = fAudioInBuffers[(pData->audioIn.count == 1) ? 0 : i][k];
  1574. fAudioOutBuffers[i][k] = (fAudioOutBuffers[i][k] * pData->postProc.dryWet) + (bufValue * (1.0f - pData->postProc.dryWet));
  1575. }
  1576. }
  1577. // Balance
  1578. if (doBalance)
  1579. {
  1580. isPair = (i % 2 == 0);
  1581. if (isPair)
  1582. {
  1583. CARLA_ASSERT(i+1 < pData->audioOut.count);
  1584. carla_copyFloats(oldBufLeft, fAudioOutBuffers[i], frames);
  1585. }
  1586. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  1587. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  1588. for (uint32_t k=0; k < frames; ++k)
  1589. {
  1590. if (isPair)
  1591. {
  1592. // left
  1593. fAudioOutBuffers[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  1594. fAudioOutBuffers[i][k] += fAudioOutBuffers[i+1][k] * (1.0f - balRangeR);
  1595. }
  1596. else
  1597. {
  1598. // right
  1599. fAudioOutBuffers[i][k] = fAudioOutBuffers[i][k] * balRangeR;
  1600. fAudioOutBuffers[i][k] += oldBufLeft[k] * balRangeL;
  1601. }
  1602. }
  1603. }
  1604. // Volume (and buffer copy)
  1605. {
  1606. for (uint32_t k=0; k < frames; ++k)
  1607. audioOut[i][k+timeOffset] = fAudioOutBuffers[i][k] * pData->postProc.volume;
  1608. }
  1609. }
  1610. } // End of Post-processing
  1611. #else
  1612. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1613. {
  1614. for (uint32_t k=0; k < frames; ++k)
  1615. audioOut[i][k+timeOffset] = fAudioOutBuffers[i][k];
  1616. }
  1617. #endif
  1618. #if 0
  1619. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  1620. {
  1621. for (uint32_t k=0; k < frames; ++k)
  1622. cvOut[i][k+timeOffset] = fCvOutBuffers[i][k];
  1623. }
  1624. #endif
  1625. // --------------------------------------------------------------------------------------------------------
  1626. pData->singleMutex.unlock();
  1627. return true;
  1628. }
  1629. void bufferSizeChanged(const uint32_t newBufferSize) override
  1630. {
  1631. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  1632. carla_debug("CarlaPluginNative::bufferSizeChanged(%i)", newBufferSize);
  1633. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1634. {
  1635. if (fAudioInBuffers[i] != nullptr)
  1636. delete[] fAudioInBuffers[i];
  1637. fAudioInBuffers[i] = new float[newBufferSize];
  1638. }
  1639. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1640. {
  1641. if (fAudioOutBuffers[i] != nullptr)
  1642. delete[] fAudioOutBuffers[i];
  1643. fAudioOutBuffers[i] = new float[newBufferSize];
  1644. }
  1645. if (fCurBufferSize == newBufferSize)
  1646. return;
  1647. fCurBufferSize = newBufferSize;
  1648. if (fDescriptor != nullptr && fDescriptor->dispatcher != nullptr)
  1649. {
  1650. fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_BUFFER_SIZE_CHANGED, 0, static_cast<intptr_t>(newBufferSize), nullptr, 0.0f);
  1651. if (fHandle2 != nullptr)
  1652. fDescriptor->dispatcher(fHandle2, NATIVE_PLUGIN_OPCODE_BUFFER_SIZE_CHANGED, 0, static_cast<intptr_t>(newBufferSize), nullptr, 0.0f);
  1653. }
  1654. }
  1655. void sampleRateChanged(const double newSampleRate) override
  1656. {
  1657. CARLA_ASSERT_INT(newSampleRate > 0.0, newSampleRate);
  1658. carla_debug("CarlaPluginNative::sampleRateChanged(%g)", newSampleRate);
  1659. if (carla_isEqual(fCurSampleRate, newSampleRate))
  1660. return;
  1661. fCurSampleRate = newSampleRate;
  1662. if (fDescriptor != nullptr && fDescriptor->dispatcher != nullptr)
  1663. {
  1664. fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_SAMPLE_RATE_CHANGED, 0, 0, nullptr, float(newSampleRate));
  1665. if (fHandle2 != nullptr)
  1666. fDescriptor->dispatcher(fHandle2, NATIVE_PLUGIN_OPCODE_SAMPLE_RATE_CHANGED, 0, 0, nullptr, float(newSampleRate));
  1667. }
  1668. }
  1669. void offlineModeChanged(const bool isOffline) override
  1670. {
  1671. if (fIsOffline == isOffline)
  1672. return;
  1673. fIsOffline = isOffline;
  1674. if (fDescriptor != nullptr && fDescriptor->dispatcher != nullptr)
  1675. {
  1676. fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_OFFLINE_CHANGED, 0, isOffline ? 1 : 0, nullptr, 0.0f);
  1677. if (fHandle2 != nullptr)
  1678. fDescriptor->dispatcher(fHandle2, NATIVE_PLUGIN_OPCODE_OFFLINE_CHANGED, 0, isOffline ? 1 : 0, nullptr, 0.0f);
  1679. }
  1680. }
  1681. // -------------------------------------------------------------------
  1682. // Plugin buffers
  1683. void initBuffers() const noexcept override
  1684. {
  1685. fMidiIn.initBuffers();
  1686. fMidiOut.initBuffers();
  1687. CarlaPlugin::initBuffers();
  1688. }
  1689. void clearBuffers() noexcept override
  1690. {
  1691. carla_debug("CarlaPluginNative::clearBuffers() - start");
  1692. if (fAudioInBuffers != nullptr)
  1693. {
  1694. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1695. {
  1696. if (fAudioInBuffers[i] != nullptr)
  1697. {
  1698. delete[] fAudioInBuffers[i];
  1699. fAudioInBuffers[i] = nullptr;
  1700. }
  1701. }
  1702. delete[] fAudioInBuffers;
  1703. fAudioInBuffers = nullptr;
  1704. }
  1705. if (fAudioOutBuffers != nullptr)
  1706. {
  1707. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1708. {
  1709. if (fAudioOutBuffers[i] != nullptr)
  1710. {
  1711. delete[] fAudioOutBuffers[i];
  1712. fAudioOutBuffers[i] = nullptr;
  1713. }
  1714. }
  1715. delete[] fAudioOutBuffers;
  1716. fAudioOutBuffers = nullptr;
  1717. }
  1718. if (fMidiIn.count > 1)
  1719. pData->event.portIn = nullptr;
  1720. if (fMidiOut.count > 1)
  1721. pData->event.portOut = nullptr;
  1722. fMidiIn.clear();
  1723. fMidiOut.clear();
  1724. CarlaPlugin::clearBuffers();
  1725. carla_debug("CarlaPluginNative::clearBuffers() - end");
  1726. }
  1727. // -------------------------------------------------------------------
  1728. // Post-poned UI Stuff
  1729. void uiParameterChange(const uint32_t index, const float value) noexcept override
  1730. {
  1731. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1732. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1733. CARLA_SAFE_ASSERT_RETURN(index < pData->param.count,);
  1734. if (! fIsUiVisible)
  1735. return;
  1736. if (fDescriptor->ui_set_parameter_value != nullptr)
  1737. fDescriptor->ui_set_parameter_value(fHandle, index, value);
  1738. }
  1739. void uiMidiProgramChange(const uint32_t index) noexcept override
  1740. {
  1741. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1742. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1743. CARLA_SAFE_ASSERT_RETURN(index < pData->midiprog.count,);
  1744. if (! fIsUiVisible)
  1745. return;
  1746. if (index >= pData->midiprog.count)
  1747. return;
  1748. if (fDescriptor->ui_set_midi_program != nullptr)
  1749. fDescriptor->ui_set_midi_program(fHandle, 0, pData->midiprog.data[index].bank, pData->midiprog.data[index].program);
  1750. }
  1751. void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) noexcept override
  1752. {
  1753. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1754. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1755. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1756. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1757. CARLA_SAFE_ASSERT_RETURN(velo > 0 && velo < MAX_MIDI_VALUE,);
  1758. if (! fIsUiVisible)
  1759. return;
  1760. // TODO
  1761. }
  1762. void uiNoteOff(const uint8_t channel, const uint8_t note) noexcept override
  1763. {
  1764. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1765. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1766. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1767. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1768. if (! fIsUiVisible)
  1769. return;
  1770. if (fDescriptor == nullptr || fHandle == nullptr)
  1771. return;
  1772. // TODO
  1773. }
  1774. // -------------------------------------------------------------------
  1775. protected:
  1776. const NativeTimeInfo* handleGetTimeInfo() const noexcept
  1777. {
  1778. CARLA_SAFE_ASSERT_RETURN(fIsProcessing, nullptr);
  1779. return &fTimeInfo;
  1780. }
  1781. bool handleWriteMidiEvent(const NativeMidiEvent* const event)
  1782. {
  1783. CARLA_SAFE_ASSERT_RETURN(pData->enabled, false);
  1784. CARLA_SAFE_ASSERT_RETURN(fIsProcessing, false);
  1785. CARLA_SAFE_ASSERT_RETURN(fMidiOut.count > 0 || pData->event.portOut != nullptr, false);
  1786. CARLA_SAFE_ASSERT_RETURN(event != nullptr, false);
  1787. CARLA_SAFE_ASSERT_RETURN(event->data[0] != 0, false);
  1788. // reverse-find first free event, and put it there
  1789. for (uint32_t i=(kPluginMaxMidiEvents*2)-1; i >= fMidiEventCount; --i)
  1790. {
  1791. if (fMidiEvents[i].data[0] != 0)
  1792. continue;
  1793. std::memcpy(&fMidiEvents[i], event, sizeof(NativeMidiEvent));
  1794. return true;
  1795. }
  1796. carla_stdout("CarlaPluginNative::handleWriteMidiEvent(%p) - buffer full", event);
  1797. return false;
  1798. }
  1799. void handleUiParameterChanged(const uint32_t index, const float value)
  1800. {
  1801. setParameterValue(index, value, false, true, true);
  1802. }
  1803. void handleUiCustomDataChanged(const char* const key, const char* const value)
  1804. {
  1805. setCustomData(CUSTOM_DATA_TYPE_STRING, key, value, false);
  1806. }
  1807. void handleUiClosed()
  1808. {
  1809. pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, 0, 0, 0.0f, nullptr);
  1810. fIsUiVisible = false;
  1811. }
  1812. const char* handleUiOpenFile(const bool isDir, const char* const title, const char* const filter)
  1813. {
  1814. return pData->engine->runFileCallback(FILE_CALLBACK_OPEN, isDir, title, filter);
  1815. }
  1816. const char* handleUiSaveFile(const bool isDir, const char* const title, const char* const filter)
  1817. {
  1818. return pData->engine->runFileCallback(FILE_CALLBACK_SAVE, isDir, title, filter);
  1819. }
  1820. intptr_t handleDispatcher(const NativeHostDispatcherOpcode opcode, const int32_t index, const intptr_t value, void* const ptr, const float opt)
  1821. {
  1822. carla_debug("CarlaPluginNative::handleDispatcher(%i, %i, " P_INTPTR ", %p, %f)", opcode, index, value, ptr, opt);
  1823. intptr_t ret = 0;
  1824. switch (opcode)
  1825. {
  1826. case NATIVE_HOST_OPCODE_NULL:
  1827. break;
  1828. case NATIVE_HOST_OPCODE_UPDATE_PARAMETER:
  1829. // TODO
  1830. pData->engine->callback(ENGINE_CALLBACK_UPDATE, pData->id, -1, 0, 0.0f, nullptr);
  1831. break;
  1832. case NATIVE_HOST_OPCODE_UPDATE_MIDI_PROGRAM:
  1833. // TODO
  1834. pData->engine->callback(ENGINE_CALLBACK_UPDATE, pData->id, -1, 0, 0.0f, nullptr);
  1835. break;
  1836. case NATIVE_HOST_OPCODE_RELOAD_PARAMETERS:
  1837. reload(); // FIXME
  1838. pData->engine->callback(ENGINE_CALLBACK_RELOAD_PARAMETERS, pData->id, -1, 0, 0.0f, nullptr);
  1839. break;
  1840. case NATIVE_HOST_OPCODE_RELOAD_MIDI_PROGRAMS:
  1841. reloadPrograms(false);
  1842. pData->engine->callback(ENGINE_CALLBACK_RELOAD_PROGRAMS, pData->id, -1, 0, 0.0f, nullptr);
  1843. break;
  1844. case NATIVE_HOST_OPCODE_RELOAD_ALL:
  1845. reload();
  1846. pData->engine->callback(ENGINE_CALLBACK_RELOAD_ALL, pData->id, -1, 0, 0.0f, nullptr);
  1847. break;
  1848. case NATIVE_HOST_OPCODE_UI_UNAVAILABLE:
  1849. pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, -1, 0, 0.0f, nullptr);
  1850. fIsUiAvailable = false;
  1851. break;
  1852. case NATIVE_HOST_OPCODE_HOST_IDLE:
  1853. pData->engine->callback(ENGINE_CALLBACK_IDLE, 0, 0, 0, 0.0f, nullptr);
  1854. break;
  1855. case NATIVE_HOST_OPCODE_INTERNAL_PLUGIN:
  1856. ret = 1;
  1857. break;
  1858. }
  1859. return ret;
  1860. // unused for now
  1861. (void)index;
  1862. (void)value;
  1863. (void)ptr;
  1864. (void)opt;
  1865. }
  1866. // -------------------------------------------------------------------
  1867. public:
  1868. void* getNativeHandle() const noexcept override
  1869. {
  1870. return fHandle;
  1871. }
  1872. const void* getNativeDescriptor() const noexcept override
  1873. {
  1874. return fDescriptor;
  1875. }
  1876. // -------------------------------------------------------------------
  1877. bool init(const char* const name, const char* const label, const uint options)
  1878. {
  1879. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  1880. // ---------------------------------------------------------------
  1881. // first checks
  1882. if (pData->client != nullptr)
  1883. {
  1884. pData->engine->setLastError("Plugin client is already registered");
  1885. return false;
  1886. }
  1887. if (label == nullptr && label[0] != '\0')
  1888. {
  1889. pData->engine->setLastError("null label");
  1890. return false;
  1891. }
  1892. // ---------------------------------------------------------------
  1893. // get descriptor that matches label
  1894. sPluginInitializer.initIfNeeded();
  1895. for (LinkedList<const NativePluginDescriptor*>::Itenerator it = gPluginDescriptors.begin2(); it.valid(); it.next())
  1896. {
  1897. fDescriptor = it.getValue(nullptr);
  1898. CARLA_SAFE_ASSERT_BREAK(fDescriptor != nullptr);
  1899. carla_debug("Check vs \"%s\"", fDescriptor->label);
  1900. if (fDescriptor->label != nullptr && std::strcmp(fDescriptor->label, label) == 0)
  1901. break;
  1902. fDescriptor = nullptr;
  1903. }
  1904. if (fDescriptor == nullptr)
  1905. {
  1906. pData->engine->setLastError("Invalid internal plugin");
  1907. return false;
  1908. }
  1909. // ---------------------------------------------------------------
  1910. // set icon
  1911. if (std::strcmp(fDescriptor->label, "audiofile") == 0)
  1912. pData->iconName = carla_strdup_safe("file");
  1913. else if (std::strcmp(fDescriptor->label, "midifile") == 0)
  1914. pData->iconName = carla_strdup_safe("file");
  1915. // ---------------------------------------------------------------
  1916. // get info
  1917. if (name != nullptr && name[0] != '\0')
  1918. pData->name = pData->engine->getUniquePluginName(name);
  1919. else if (fDescriptor->name != nullptr && fDescriptor->name[0] != '\0')
  1920. pData->name = pData->engine->getUniquePluginName(fDescriptor->name);
  1921. else
  1922. pData->name = pData->engine->getUniquePluginName(label);
  1923. {
  1924. CARLA_ASSERT(fHost.uiName == nullptr);
  1925. char uiName[std::strlen(pData->name)+6+1];
  1926. std::strcpy(uiName, pData->name);
  1927. std::strcat(uiName, " (GUI)");
  1928. fHost.uiName = carla_strdup(uiName);
  1929. }
  1930. // ---------------------------------------------------------------
  1931. // register client
  1932. pData->client = pData->engine->addClient(this);
  1933. if (pData->client == nullptr || ! pData->client->isOk())
  1934. {
  1935. pData->engine->setLastError("Failed to register plugin client");
  1936. return false;
  1937. }
  1938. // ---------------------------------------------------------------
  1939. // initialize plugin
  1940. fHandle = fDescriptor->instantiate(&fHost);
  1941. if (fHandle == nullptr)
  1942. {
  1943. pData->engine->setLastError("Plugin failed to initialize");
  1944. return false;
  1945. }
  1946. // ---------------------------------------------------------------
  1947. // set default options
  1948. bool hasMidiProgs = false;
  1949. if (fDescriptor->get_midi_program_count != nullptr)
  1950. {
  1951. try {
  1952. hasMidiProgs = fDescriptor->get_midi_program_count(fHandle) > 0;
  1953. } catch (...) {}
  1954. }
  1955. pData->options = 0x0;
  1956. if (fDescriptor->midiOuts != 0 || (fDescriptor->hints & NATIVE_PLUGIN_NEEDS_FIXED_BUFFERS) != 0)
  1957. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  1958. else if (options & PLUGIN_OPTION_FIXED_BUFFERS)
  1959. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  1960. if (pData->engine->getOptions().forceStereo)
  1961. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  1962. else if (options & PLUGIN_OPTION_FORCE_STEREO)
  1963. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  1964. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_CHANNEL_PRESSURE)
  1965. pData->options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  1966. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_NOTE_AFTERTOUCH)
  1967. pData->options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  1968. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_PITCHBEND)
  1969. pData->options |= PLUGIN_OPTION_SEND_PITCHBEND;
  1970. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_ALL_SOUND_OFF)
  1971. pData->options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  1972. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_CONTROL_CHANGES)
  1973. {
  1974. if (options & PLUGIN_OPTION_SEND_CONTROL_CHANGES)
  1975. pData->options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  1976. }
  1977. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_PROGRAM_CHANGES)
  1978. {
  1979. CARLA_SAFE_ASSERT(! hasMidiProgs);
  1980. pData->options |= PLUGIN_OPTION_SEND_PROGRAM_CHANGES;
  1981. }
  1982. else if (hasMidiProgs)
  1983. pData->options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  1984. return true;
  1985. }
  1986. private:
  1987. NativePluginHandle fHandle;
  1988. NativePluginHandle fHandle2;
  1989. NativeHostDescriptor fHost;
  1990. const NativePluginDescriptor* fDescriptor;
  1991. bool fIsProcessing;
  1992. bool fIsOffline;
  1993. bool fIsUiAvailable;
  1994. bool fIsUiVisible;
  1995. float** fAudioInBuffers;
  1996. float** fAudioOutBuffers;
  1997. uint32_t fMidiEventCount;
  1998. NativeMidiEvent fMidiEvents[kPluginMaxMidiEvents*2];
  1999. int32_t fCurMidiProgs[MAX_MIDI_CHANNELS];
  2000. uint32_t fCurBufferSize;
  2001. double fCurSampleRate;
  2002. NativePluginMidiData fMidiIn;
  2003. NativePluginMidiData fMidiOut;
  2004. NativeTimeInfo fTimeInfo;
  2005. // -------------------------------------------------------------------
  2006. #define handlePtr ((CarlaPluginNative*)handle)
  2007. static uint32_t carla_host_get_buffer_size(NativeHostHandle handle) noexcept
  2008. {
  2009. return handlePtr->fCurBufferSize;
  2010. }
  2011. static double carla_host_get_sample_rate(NativeHostHandle handle) noexcept
  2012. {
  2013. return handlePtr->fCurSampleRate;
  2014. }
  2015. static bool carla_host_is_offline(NativeHostHandle handle) noexcept
  2016. {
  2017. return handlePtr->fIsOffline;
  2018. }
  2019. static const NativeTimeInfo* carla_host_get_time_info(NativeHostHandle handle) noexcept
  2020. {
  2021. return handlePtr->handleGetTimeInfo();
  2022. }
  2023. static bool carla_host_write_midi_event(NativeHostHandle handle, const NativeMidiEvent* event)
  2024. {
  2025. return handlePtr->handleWriteMidiEvent(event);
  2026. }
  2027. static void carla_host_ui_parameter_changed(NativeHostHandle handle, uint32_t index, float value)
  2028. {
  2029. handlePtr->handleUiParameterChanged(index, value);
  2030. }
  2031. static void carla_host_ui_custom_data_changed(NativeHostHandle handle, const char* key, const char* value)
  2032. {
  2033. handlePtr->handleUiCustomDataChanged(key, value);
  2034. }
  2035. static void carla_host_ui_closed(NativeHostHandle handle)
  2036. {
  2037. handlePtr->handleUiClosed();
  2038. }
  2039. static const char* carla_host_ui_open_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
  2040. {
  2041. return handlePtr->handleUiOpenFile(isDir, title, filter);
  2042. }
  2043. static const char* carla_host_ui_save_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
  2044. {
  2045. return handlePtr->handleUiSaveFile(isDir, title, filter);
  2046. }
  2047. static intptr_t carla_host_dispatcher(NativeHostHandle handle, NativeHostDispatcherOpcode opcode, int32_t index, intptr_t value, void* ptr, float opt)
  2048. {
  2049. return handlePtr->handleDispatcher(opcode, index, value, ptr, opt);
  2050. }
  2051. #undef handlePtr
  2052. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaPluginNative)
  2053. };
  2054. // -----------------------------------------------------------------------
  2055. CarlaPlugin* CarlaPlugin::newNative(const Initializer& init)
  2056. {
  2057. carla_debug("CarlaPlugin::newNative({%p, \"%s\", \"%s\", \"%s\", " P_INT64 "})", init.engine, init.filename, init.name, init.label, init.uniqueId);
  2058. CarlaPluginNative* const plugin(new CarlaPluginNative(init.engine, init.id));
  2059. if (! plugin->init(init.name, init.label, init.options))
  2060. {
  2061. delete plugin;
  2062. return nullptr;
  2063. }
  2064. return plugin;
  2065. }
  2066. // -----------------------------------------------------------------------
  2067. CARLA_BACKEND_END_NAMESPACE