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.

3061 lines
110KB

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