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.

3064 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. }
  795. void uiIdle() override
  796. {
  797. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  798. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  799. if (fIsUiVisible && fDescriptor->ui_idle != nullptr)
  800. fDescriptor->ui_idle(fHandle);
  801. CarlaPlugin::uiIdle();
  802. }
  803. // -------------------------------------------------------------------
  804. // Plugin state
  805. void reload() override
  806. {
  807. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr,);
  808. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  809. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  810. carla_debug("CarlaPluginNative::reload() - start");
  811. const EngineProcessMode processMode(pData->engine->getProccessMode());
  812. // Safely disable plugin for reload
  813. const ScopedDisabler sd(this);
  814. if (pData->active)
  815. deactivate();
  816. clearBuffers();
  817. uint32_t aIns, aOuts, cvIns, cvOuts, mIns, mOuts, j;
  818. bool forcedStereoIn, forcedStereoOut;
  819. forcedStereoIn = forcedStereoOut = false;
  820. bool needsCtrlIn, needsCtrlOut;
  821. needsCtrlIn = needsCtrlOut = false;
  822. aIns = fDescriptor->audioIns;
  823. aOuts = fDescriptor->audioOuts;
  824. cvIns = fDescriptor->cvIns;
  825. cvOuts = fDescriptor->cvOuts;
  826. mIns = fDescriptor->midiIns;
  827. mOuts = fDescriptor->midiOuts;
  828. if ((pData->options & PLUGIN_OPTION_FORCE_STEREO) != 0 && (aIns == 1 || aOuts == 1) && mIns <= 1 && mOuts <= 1)
  829. {
  830. if (fHandle2 == nullptr)
  831. fHandle2 = fDescriptor->instantiate(&fHost);
  832. if (fHandle2 != nullptr)
  833. {
  834. if (aIns == 1)
  835. {
  836. aIns = 2;
  837. forcedStereoIn = true;
  838. }
  839. if (aOuts == 1)
  840. {
  841. aOuts = 2;
  842. forcedStereoOut = true;
  843. }
  844. }
  845. }
  846. if (aIns > 0)
  847. {
  848. pData->audioIn.createNew(aIns);
  849. }
  850. if (aOuts > 0)
  851. {
  852. pData->audioOut.createNew(aOuts);
  853. needsCtrlIn = true;
  854. }
  855. if (cvIns > 0)
  856. {
  857. pData->cvIn.createNew(cvIns);
  858. }
  859. if (cvOuts > 0)
  860. {
  861. pData->cvOut.createNew(cvOuts);
  862. }
  863. if (const uint32_t acIns = aIns + cvIns)
  864. {
  865. fAudioAndCvInBuffers = new float*[acIns];
  866. carla_zeroPointers(fAudioAndCvInBuffers, acIns);
  867. }
  868. if (const uint32_t acOuts = aOuts + cvOuts)
  869. {
  870. fAudioAndCvOutBuffers = new float*[acOuts];
  871. carla_zeroPointers(fAudioAndCvOutBuffers, acOuts);
  872. }
  873. if (mIns > 0)
  874. {
  875. fMidiIn.createNew(mIns);
  876. needsCtrlIn = (mIns == 1);
  877. }
  878. if (mOuts > 0)
  879. {
  880. fMidiOut.createNew(mOuts);
  881. needsCtrlOut = (mOuts == 1);
  882. }
  883. const uint portNameSize(pData->engine->getMaxPortNameSize());
  884. CarlaString portName;
  885. // Audio Ins
  886. for (j=0; j < aIns; ++j)
  887. {
  888. portName.clear();
  889. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  890. {
  891. portName = pData->name;
  892. portName += ":";
  893. }
  894. if (fDescriptor->get_buffer_port_name != nullptr)
  895. {
  896. portName += fDescriptor->get_buffer_port_name(fHandle, forcedStereoIn ? 0 : j, false);
  897. }
  898. else if (aIns > 1 && ! forcedStereoIn)
  899. {
  900. portName += "input_";
  901. portName += CarlaString(j+1);
  902. }
  903. else
  904. portName += "input";
  905. portName.truncate(portNameSize);
  906. pData->audioIn.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true, j);
  907. pData->audioIn.ports[j].rindex = j;
  908. if (forcedStereoIn)
  909. {
  910. portName += "_2";
  911. pData->audioIn.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true, 1);
  912. pData->audioIn.ports[1].rindex = j;
  913. break;
  914. }
  915. }
  916. // Audio Outs
  917. for (j=0; j < aOuts; ++j)
  918. {
  919. portName.clear();
  920. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  921. {
  922. portName = pData->name;
  923. portName += ":";
  924. }
  925. if (fDescriptor->get_buffer_port_name != nullptr)
  926. {
  927. portName += fDescriptor->get_buffer_port_name(fHandle, forcedStereoOut ? 0 : j, true);
  928. }
  929. else if (aOuts > 1 && ! forcedStereoOut)
  930. {
  931. portName += "output_";
  932. portName += CarlaString(j+1);
  933. }
  934. else
  935. portName += "output";
  936. portName.truncate(portNameSize);
  937. pData->audioOut.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false, j);
  938. pData->audioOut.ports[j].rindex = j;
  939. if (forcedStereoOut)
  940. {
  941. portName += "_2";
  942. pData->audioOut.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false, 1);
  943. pData->audioOut.ports[1].rindex = j;
  944. break;
  945. }
  946. }
  947. // CV Ins
  948. for (j=0; j < cvIns; ++j)
  949. {
  950. portName.clear();
  951. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  952. {
  953. portName = pData->name;
  954. portName += ":";
  955. }
  956. if (fDescriptor->get_buffer_port_name != nullptr)
  957. {
  958. portName += fDescriptor->get_buffer_port_name(fHandle, fDescriptor->audioIns + j, false);
  959. }
  960. else if (cvIns > 1)
  961. {
  962. portName += "cv_input_";
  963. portName += CarlaString(j+1);
  964. }
  965. else
  966. portName += "cv_input";
  967. portName.truncate(portNameSize);
  968. float min = -1.0f, max = 1.0f;
  969. if (fDescriptor->get_buffer_port_range != nullptr)
  970. {
  971. if (const NativePortRange* const range = fDescriptor->get_buffer_port_range(fHandle,
  972. fDescriptor->audioIns + j,
  973. false))
  974. {
  975. min = range->minimum;
  976. max = range->maximum;
  977. }
  978. }
  979. pData->cvIn.ports[j].port = (CarlaEngineCVPort*)pData->client->addPort(kEnginePortTypeCV, portName, true, j);
  980. pData->cvIn.ports[j].rindex = j;
  981. pData->cvIn.ports[j].port->setRange(min, max);
  982. }
  983. // CV Outs
  984. for (j=0; j < cvOuts; ++j)
  985. {
  986. portName.clear();
  987. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  988. {
  989. portName = pData->name;
  990. portName += ":";
  991. }
  992. if (fDescriptor->get_buffer_port_name != nullptr)
  993. {
  994. portName += fDescriptor->get_buffer_port_name(fHandle, fDescriptor->audioOuts + j, true);
  995. }
  996. else if (cvOuts > 1)
  997. {
  998. portName += "cv_output_";
  999. portName += CarlaString(j+1);
  1000. }
  1001. else
  1002. portName += "cv_output";
  1003. portName.truncate(portNameSize);
  1004. float min = -1.0f, max = 1.0f;
  1005. if (fDescriptor->get_buffer_port_range != nullptr)
  1006. {
  1007. if (const NativePortRange* const range = fDescriptor->get_buffer_port_range(fHandle,
  1008. fDescriptor->audioOuts + j,
  1009. true))
  1010. {
  1011. min = range->minimum;
  1012. max = range->maximum;
  1013. }
  1014. }
  1015. pData->cvOut.ports[j].port = (CarlaEngineCVPort*)pData->client->addPort(kEnginePortTypeCV, portName, false, j);
  1016. pData->cvOut.ports[j].rindex = j;
  1017. pData->cvOut.ports[j].port->setRange(min, max);
  1018. }
  1019. // MIDI Input (only if multiple)
  1020. if (mIns > 1)
  1021. {
  1022. for (j=0; j < mIns; ++j)
  1023. {
  1024. portName.clear();
  1025. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  1026. {
  1027. portName = pData->name;
  1028. portName += ":";
  1029. }
  1030. portName += "midi-in_";
  1031. portName += CarlaString(j+1);
  1032. portName.truncate(portNameSize);
  1033. fMidiIn.ports[j] = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true, j);
  1034. fMidiIn.indexes[j] = j;
  1035. }
  1036. pData->event.portIn = fMidiIn.ports[0];
  1037. }
  1038. // MIDI Output (only if multiple)
  1039. if (mOuts > 1)
  1040. {
  1041. for (j=0; j < mOuts; ++j)
  1042. {
  1043. portName.clear();
  1044. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  1045. {
  1046. portName = pData->name;
  1047. portName += ":";
  1048. }
  1049. portName += "midi-out_";
  1050. portName += CarlaString(j+1);
  1051. portName.truncate(portNameSize);
  1052. fMidiOut.ports[j] = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false, j);
  1053. fMidiOut.indexes[j] = j;
  1054. }
  1055. pData->event.portOut = fMidiOut.ports[0];
  1056. }
  1057. reloadParameters(&needsCtrlIn, &needsCtrlOut);
  1058. if (needsCtrlIn || mIns == 1)
  1059. {
  1060. portName.clear();
  1061. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  1062. {
  1063. portName = pData->name;
  1064. portName += ":";
  1065. }
  1066. portName += "events-in";
  1067. portName.truncate(portNameSize);
  1068. pData->event.portIn = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true, 0);
  1069. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1070. pData->event.cvSourcePorts = pData->client->createCVSourcePorts();
  1071. #endif
  1072. }
  1073. if (needsCtrlOut || mOuts == 1)
  1074. {
  1075. portName.clear();
  1076. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  1077. {
  1078. portName = pData->name;
  1079. portName += ":";
  1080. }
  1081. portName += "events-out";
  1082. portName.truncate(portNameSize);
  1083. pData->event.portOut = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false, 0);
  1084. }
  1085. if (forcedStereoIn || forcedStereoOut)
  1086. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  1087. else
  1088. pData->options &= ~PLUGIN_OPTION_FORCE_STEREO;
  1089. // plugin hints
  1090. pData->hints = 0x0;
  1091. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  1092. pData->hints |= PLUGIN_CAN_DRYWET;
  1093. if (aOuts > 0)
  1094. pData->hints |= PLUGIN_CAN_VOLUME;
  1095. if (aOuts >= 2 && aOuts % 2 == 0)
  1096. pData->hints |= PLUGIN_CAN_BALANCE;
  1097. // native plugin hints
  1098. if (fDescriptor->hints & NATIVE_PLUGIN_IS_RTSAFE)
  1099. pData->hints |= PLUGIN_IS_RTSAFE;
  1100. if (fDescriptor->hints & NATIVE_PLUGIN_IS_SYNTH)
  1101. pData->hints |= PLUGIN_IS_SYNTH;
  1102. if (fDescriptor->hints & NATIVE_PLUGIN_HAS_UI)
  1103. pData->hints |= PLUGIN_HAS_CUSTOM_UI;
  1104. if (fDescriptor->hints & NATIVE_PLUGIN_NEEDS_FIXED_BUFFERS)
  1105. pData->hints |= PLUGIN_NEEDS_FIXED_BUFFERS;
  1106. if (fDescriptor->hints & NATIVE_PLUGIN_NEEDS_UI_MAIN_THREAD)
  1107. pData->hints |= PLUGIN_NEEDS_UI_MAIN_THREAD;
  1108. if (fDescriptor->hints & NATIVE_PLUGIN_USES_MULTI_PROGS)
  1109. pData->hints |= PLUGIN_USES_MULTI_PROGS;
  1110. if (fDescriptor->hints & NATIVE_PLUGIN_HAS_INLINE_DISPLAY)
  1111. pData->hints |= PLUGIN_HAS_INLINE_DISPLAY;
  1112. // extra plugin hints
  1113. pData->extraHints = 0x0;
  1114. bufferSizeChanged(pData->engine->getBufferSize());
  1115. reloadPrograms(true);
  1116. if (pData->active)
  1117. activate();
  1118. carla_debug("CarlaPluginNative::reload() - end");
  1119. }
  1120. void reloadParameters(bool* const needsCtrlIn, bool* const needsCtrlOut)
  1121. {
  1122. carla_debug("CarlaPluginNative::reloadParameters() - start");
  1123. const float sampleRate = static_cast<float>(pData->engine->getSampleRate());
  1124. const uint32_t params = (fDescriptor->get_parameter_count != nullptr && fDescriptor->get_parameter_info != nullptr)
  1125. ? fDescriptor->get_parameter_count(fHandle)
  1126. : 0;
  1127. pData->param.clear();
  1128. if (params > 0)
  1129. {
  1130. pData->param.createNew(params, true);
  1131. }
  1132. for (uint32_t j=0; j < params; ++j)
  1133. {
  1134. const NativeParameter* const paramInfo(fDescriptor->get_parameter_info(fHandle, j));
  1135. CARLA_SAFE_ASSERT_CONTINUE(paramInfo != nullptr);
  1136. pData->param.data[j].type = PARAMETER_UNKNOWN;
  1137. pData->param.data[j].index = static_cast<int32_t>(j);
  1138. pData->param.data[j].rindex = static_cast<int32_t>(j);
  1139. float min, max, def, step, stepSmall, stepLarge;
  1140. // min value
  1141. min = paramInfo->ranges.min;
  1142. // max value
  1143. max = paramInfo->ranges.max;
  1144. if (min > max)
  1145. max = min;
  1146. if (carla_isEqual(min, max))
  1147. {
  1148. carla_stderr2("WARNING - Broken plugin parameter '%s': max == min", paramInfo->name);
  1149. max = min + 0.1f;
  1150. }
  1151. // default value
  1152. def = paramInfo->ranges.def;
  1153. if (def < min)
  1154. def = min;
  1155. else if (def > max)
  1156. def = max;
  1157. if (paramInfo->hints & NATIVE_PARAMETER_USES_SAMPLE_RATE)
  1158. {
  1159. min *= sampleRate;
  1160. max *= sampleRate;
  1161. def *= sampleRate;
  1162. pData->param.data[j].hints |= PARAMETER_USES_SAMPLERATE;
  1163. }
  1164. if (paramInfo->hints & NATIVE_PARAMETER_IS_BOOLEAN)
  1165. {
  1166. step = max - min;
  1167. stepSmall = step;
  1168. stepLarge = step;
  1169. pData->param.data[j].hints |= PARAMETER_IS_BOOLEAN;
  1170. }
  1171. else if (paramInfo->hints & NATIVE_PARAMETER_IS_INTEGER)
  1172. {
  1173. step = 1.0f;
  1174. stepSmall = 1.0f;
  1175. stepLarge = 10.0f;
  1176. pData->param.data[j].hints |= PARAMETER_IS_INTEGER;
  1177. }
  1178. else
  1179. {
  1180. float range = max - min;
  1181. step = range/100.0f;
  1182. stepSmall = range/1000.0f;
  1183. stepLarge = range/10.0f;
  1184. }
  1185. if (paramInfo->hints & NATIVE_PARAMETER_IS_OUTPUT)
  1186. {
  1187. pData->param.data[j].type = PARAMETER_OUTPUT;
  1188. if (needsCtrlOut != nullptr)
  1189. *needsCtrlOut = true;
  1190. }
  1191. else
  1192. {
  1193. pData->param.data[j].type = PARAMETER_INPUT;
  1194. if (needsCtrlIn != nullptr)
  1195. *needsCtrlIn = true;
  1196. }
  1197. // extra parameter hints
  1198. if (paramInfo->hints & NATIVE_PARAMETER_IS_ENABLED)
  1199. {
  1200. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  1201. if (paramInfo->hints & NATIVE_PARAMETER_IS_AUTOMABLE)
  1202. {
  1203. pData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  1204. pData->param.data[j].hints |= PARAMETER_CAN_BE_CV_CONTROLLED;
  1205. }
  1206. }
  1207. if (paramInfo->hints & NATIVE_PARAMETER_IS_LOGARITHMIC)
  1208. pData->param.data[j].hints |= PARAMETER_IS_LOGARITHMIC;
  1209. if (paramInfo->hints & NATIVE_PARAMETER_USES_SCALEPOINTS)
  1210. pData->param.data[j].hints |= PARAMETER_USES_SCALEPOINTS;
  1211. pData->param.ranges[j].min = min;
  1212. pData->param.ranges[j].max = max;
  1213. pData->param.ranges[j].def = def;
  1214. pData->param.ranges[j].step = step;
  1215. pData->param.ranges[j].stepSmall = stepSmall;
  1216. pData->param.ranges[j].stepLarge = stepLarge;
  1217. }
  1218. carla_debug("CarlaPluginNative::reloadParameters() - end");
  1219. }
  1220. void reloadPrograms(const bool doInit) override
  1221. {
  1222. carla_debug("CarlaPluginNative::reloadPrograms(%s)", bool2str(doInit));
  1223. uint32_t i, oldCount = pData->midiprog.count;
  1224. const int32_t current = pData->midiprog.current;
  1225. // Delete old programs
  1226. pData->midiprog.clear();
  1227. // Query new programs
  1228. uint32_t count = 0;
  1229. if (fDescriptor->get_midi_program_count != nullptr && fDescriptor->get_midi_program_info != nullptr && fDescriptor->set_midi_program != nullptr)
  1230. count = fDescriptor->get_midi_program_count(fHandle);
  1231. if (count > 0)
  1232. {
  1233. pData->midiprog.createNew(count);
  1234. // Update data
  1235. for (i=0; i < count; ++i)
  1236. {
  1237. const NativeMidiProgram* const mpDesc(fDescriptor->get_midi_program_info(fHandle, i));
  1238. CARLA_SAFE_ASSERT_CONTINUE(mpDesc != nullptr);
  1239. pData->midiprog.data[i].bank = mpDesc->bank;
  1240. pData->midiprog.data[i].program = mpDesc->program;
  1241. pData->midiprog.data[i].name = carla_strdup(mpDesc->name);
  1242. }
  1243. }
  1244. if (doInit)
  1245. {
  1246. if (count > 0)
  1247. setMidiProgram(0, false, false, false, true);
  1248. }
  1249. else
  1250. {
  1251. // Check if current program is invalid
  1252. bool programChanged = false;
  1253. if (count == oldCount+1)
  1254. {
  1255. // one midi program added, probably created by user
  1256. pData->midiprog.current = static_cast<int32_t>(oldCount);
  1257. programChanged = true;
  1258. }
  1259. else if (current < 0 && count > 0)
  1260. {
  1261. // programs exist now, but not before
  1262. pData->midiprog.current = 0;
  1263. programChanged = true;
  1264. }
  1265. else if (current >= 0 && count == 0)
  1266. {
  1267. // programs existed before, but not anymore
  1268. pData->midiprog.current = -1;
  1269. programChanged = true;
  1270. }
  1271. else if (current >= static_cast<int32_t>(count))
  1272. {
  1273. // current midi program > count
  1274. pData->midiprog.current = 0;
  1275. programChanged = true;
  1276. }
  1277. else
  1278. {
  1279. // no change
  1280. pData->midiprog.current = current;
  1281. }
  1282. if (programChanged)
  1283. setMidiProgram(pData->midiprog.current, true, true, true, false);
  1284. pData->engine->callback(true, true,
  1285. ENGINE_CALLBACK_RELOAD_PROGRAMS,
  1286. pData->id,
  1287. 0, 0, 0, 0.0f, nullptr);
  1288. }
  1289. }
  1290. // -------------------------------------------------------------------
  1291. // Plugin processing
  1292. void activate() noexcept override
  1293. {
  1294. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1295. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1296. if (fDescriptor->activate != nullptr)
  1297. {
  1298. try {
  1299. fDescriptor->activate(fHandle);
  1300. } catch(...) {}
  1301. if (fHandle2 != nullptr)
  1302. {
  1303. try {
  1304. fDescriptor->activate(fHandle2);
  1305. } catch(...) {}
  1306. }
  1307. }
  1308. }
  1309. void deactivate() noexcept override
  1310. {
  1311. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1312. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1313. if (fDescriptor->deactivate != nullptr)
  1314. {
  1315. try {
  1316. fDescriptor->deactivate(fHandle);
  1317. } catch(...) {}
  1318. if (fHandle2 != nullptr)
  1319. {
  1320. try {
  1321. fDescriptor->deactivate(fHandle2);
  1322. } catch(...) {}
  1323. }
  1324. }
  1325. }
  1326. const EngineEvent& findNextEvent()
  1327. {
  1328. if (fMidiIn.count == 1)
  1329. {
  1330. NativePluginMidiInData::MultiPortData& multiportData(fMidiIn.multiportData[0]);
  1331. if (multiportData.usedIndex == multiportData.cachedEventCount)
  1332. {
  1333. const uint32_t eventCount = pData->event.portIn->getEventCount();
  1334. CARLA_SAFE_ASSERT_INT2(eventCount == multiportData.cachedEventCount,
  1335. eventCount, multiportData.cachedEventCount);
  1336. return kNullEngineEvent;
  1337. }
  1338. return pData->event.portIn->getEvent(multiportData.usedIndex++);
  1339. }
  1340. uint32_t lowestSampleTime = 9999999;
  1341. uint32_t portMatching = 0;
  1342. bool found = false;
  1343. // process events in order for multiple ports
  1344. for (uint32_t m=0; m < fMidiIn.count; ++m)
  1345. {
  1346. CarlaEngineEventPort* const eventPort(fMidiIn.ports[m]);
  1347. NativePluginMidiInData::MultiPortData& multiportData(fMidiIn.multiportData[m]);
  1348. if (multiportData.usedIndex == multiportData.cachedEventCount)
  1349. continue;
  1350. const EngineEvent& event(eventPort->getEventUnchecked(multiportData.usedIndex));
  1351. if (event.time < lowestSampleTime)
  1352. {
  1353. lowestSampleTime = event.time;
  1354. portMatching = m;
  1355. found = true;
  1356. }
  1357. }
  1358. if (found)
  1359. {
  1360. CarlaEngineEventPort* const eventPort(fMidiIn.ports[portMatching]);
  1361. NativePluginMidiInData::MultiPortData& multiportData(fMidiIn.multiportData[portMatching]);
  1362. return eventPort->getEvent(multiportData.usedIndex++);
  1363. }
  1364. return kNullEngineEvent;
  1365. }
  1366. void process(const float* const* const audioIn, float** const audioOut,
  1367. const float* const* const cvIn, float** const cvOut, const uint32_t frames) override
  1368. {
  1369. // --------------------------------------------------------------------------------------------------------
  1370. // Check if active
  1371. if (! pData->active)
  1372. {
  1373. // disable any output sound
  1374. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1375. carla_zeroFloats(audioOut[i], frames);
  1376. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  1377. carla_zeroFloats(cvOut[i], frames);
  1378. return;
  1379. }
  1380. fMidiEventInCount = fMidiEventOutCount = 0;
  1381. carla_zeroStructs(fMidiInEvents, kPluginMaxMidiEvents);
  1382. carla_zeroStructs(fMidiOutEvents, kPluginMaxMidiEvents);
  1383. // --------------------------------------------------------------------------------------------------------
  1384. // Check if needs reset
  1385. if (pData->needsReset)
  1386. {
  1387. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1388. {
  1389. fMidiEventInCount = MAX_MIDI_CHANNELS*2;
  1390. for (uint8_t k=0, i=MAX_MIDI_CHANNELS; k < MAX_MIDI_CHANNELS; ++k)
  1391. {
  1392. fMidiInEvents[k].data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (k & MIDI_CHANNEL_BIT));
  1393. fMidiInEvents[k].data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  1394. fMidiInEvents[k].data[2] = 0;
  1395. fMidiInEvents[k].size = 3;
  1396. fMidiInEvents[k+i].data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (k & MIDI_CHANNEL_BIT));
  1397. fMidiInEvents[k+i].data[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  1398. fMidiInEvents[k+i].data[2] = 0;
  1399. fMidiInEvents[k+i].size = 3;
  1400. }
  1401. }
  1402. else if (pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  1403. {
  1404. fMidiEventInCount = MAX_MIDI_NOTE;
  1405. for (uint8_t k=0; k < MAX_MIDI_NOTE; ++k)
  1406. {
  1407. fMidiInEvents[k].data[0] = uint8_t(MIDI_STATUS_NOTE_OFF | (pData->ctrlChannel & MIDI_CHANNEL_BIT));
  1408. fMidiInEvents[k].data[1] = k;
  1409. fMidiInEvents[k].data[2] = 0;
  1410. fMidiInEvents[k].size = 3;
  1411. }
  1412. }
  1413. pData->needsReset = false;
  1414. }
  1415. // --------------------------------------------------------------------------------------------------------
  1416. // Set TimeInfo
  1417. const EngineTimeInfo timeInfo(pData->engine->getTimeInfo());
  1418. fTimeInfo.playing = timeInfo.playing;
  1419. fTimeInfo.frame = timeInfo.frame;
  1420. fTimeInfo.usecs = timeInfo.usecs;
  1421. if (timeInfo.bbt.valid)
  1422. {
  1423. fTimeInfo.bbt.valid = true;
  1424. fTimeInfo.bbt.bar = timeInfo.bbt.bar;
  1425. fTimeInfo.bbt.beat = timeInfo.bbt.beat;
  1426. fTimeInfo.bbt.tick = timeInfo.bbt.tick;
  1427. fTimeInfo.bbt.barStartTick = timeInfo.bbt.barStartTick;
  1428. fTimeInfo.bbt.beatsPerBar = timeInfo.bbt.beatsPerBar;
  1429. fTimeInfo.bbt.beatType = timeInfo.bbt.beatType;
  1430. fTimeInfo.bbt.ticksPerBeat = timeInfo.bbt.ticksPerBeat;
  1431. fTimeInfo.bbt.beatsPerMinute = timeInfo.bbt.beatsPerMinute;
  1432. }
  1433. else
  1434. {
  1435. fTimeInfo.bbt.valid = false;
  1436. }
  1437. #if 0
  1438. // This test code has proven to be quite useful
  1439. // So I am leaving it behind, I might need it again..
  1440. if (pData->id == 1)
  1441. {
  1442. static int64_t last_frame = timeInfo.frame;
  1443. static int64_t last_dev_frame = 0;
  1444. static double last_val = timeInfo.bbt.barStartTick + ((timeInfo.bbt.beat-1) * timeInfo.bbt.ticksPerBeat) + timeInfo.bbt.tick;
  1445. static double last_dev_val = 0.0;
  1446. int64_t cur_frame = timeInfo.frame;
  1447. int64_t cur_dev_frame = cur_frame - last_frame;
  1448. double cur_val = timeInfo.bbt.barStartTick + ((timeInfo.bbt.beat-1) * timeInfo.bbt.ticksPerBeat) + timeInfo.bbt.tick;
  1449. double cur_dev_val = cur_val - last_val;
  1450. if (std::abs(last_dev_val - cur_dev_val) >= 0.0001 || last_dev_frame != cur_dev_frame)
  1451. {
  1452. carla_stdout("currently %u at %u => %f : DEV1: %li : DEV2: %f",
  1453. frames,
  1454. timeInfo.frame,
  1455. cur_val,
  1456. cur_dev_frame,
  1457. cur_dev_val);
  1458. }
  1459. last_val = cur_val;
  1460. last_dev_val = cur_dev_val;
  1461. last_frame = cur_frame;
  1462. last_dev_frame = cur_dev_frame;
  1463. }
  1464. #endif
  1465. // --------------------------------------------------------------------------------------------------------
  1466. // Event Input and Processing
  1467. if (pData->event.portIn != nullptr)
  1468. {
  1469. // ----------------------------------------------------------------------------------------------------
  1470. // MIDI Input (External)
  1471. if (pData->extNotes.mutex.tryLock())
  1472. {
  1473. ExternalMidiNote note = { 0, 0, 0 };
  1474. for (; fMidiEventInCount < kPluginMaxMidiEvents && ! pData->extNotes.data.isEmpty();)
  1475. {
  1476. note = pData->extNotes.data.getFirst(note, true);
  1477. CARLA_SAFE_ASSERT_CONTINUE(note.channel >= 0 && note.channel < MAX_MIDI_CHANNELS);
  1478. NativeMidiEvent& nativeEvent(fMidiInEvents[fMidiEventInCount++]);
  1479. nativeEvent.data[0] = uint8_t((note.velo > 0 ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF) | (note.channel & MIDI_CHANNEL_BIT));
  1480. nativeEvent.data[1] = note.note;
  1481. nativeEvent.data[2] = note.velo;
  1482. nativeEvent.size = 3;
  1483. }
  1484. pData->extNotes.mutex.unlock();
  1485. } // End of MIDI Input (External)
  1486. // ----------------------------------------------------------------------------------------------------
  1487. // Event Input (System)
  1488. #ifndef BUILD_BRIDGE
  1489. bool allNotesOffSent = false;
  1490. #endif
  1491. bool isSampleAccurate = (pData->options & PLUGIN_OPTION_FIXED_BUFFERS) == 0;
  1492. uint32_t startTime = 0;
  1493. uint32_t timeOffset = 0;
  1494. uint32_t nextBankId;
  1495. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  1496. nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
  1497. else
  1498. nextBankId = 0;
  1499. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1500. if (cvIn != nullptr && pData->event.cvSourcePorts != nullptr)
  1501. pData->event.cvSourcePorts->initPortBuffers(cvIn + pData->cvIn.count, frames, isSampleAccurate, pData->event.portIn);
  1502. #endif
  1503. for (;;)
  1504. {
  1505. const EngineEvent& event(findNextEvent());
  1506. if (event.type == kEngineEventTypeNull)
  1507. break;
  1508. uint32_t eventTime = event.time;
  1509. CARLA_SAFE_ASSERT_UINT2_CONTINUE(eventTime < frames, eventTime, frames);
  1510. if (eventTime < timeOffset)
  1511. {
  1512. carla_stderr2("Timing error, eventTime:%u < timeOffset:%u for '%s'",
  1513. eventTime, timeOffset, pData->name);
  1514. eventTime = timeOffset;
  1515. }
  1516. if (isSampleAccurate && eventTime > timeOffset)
  1517. {
  1518. if (processSingle(audioIn, audioOut, cvIn, cvOut, eventTime - timeOffset, timeOffset))
  1519. {
  1520. startTime = 0;
  1521. timeOffset = eventTime;
  1522. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  1523. nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
  1524. else
  1525. nextBankId = 0;
  1526. if (fMidiEventInCount > 0)
  1527. {
  1528. carla_zeroStructs(fMidiInEvents, fMidiEventInCount);
  1529. fMidiEventInCount = 0;
  1530. }
  1531. if (fMidiEventOutCount > 0)
  1532. {
  1533. carla_zeroStructs(fMidiOutEvents, fMidiEventOutCount);
  1534. fMidiEventOutCount = 0;
  1535. }
  1536. }
  1537. else
  1538. startTime += timeOffset;
  1539. }
  1540. // Control change
  1541. switch (event.type)
  1542. {
  1543. case kEngineEventTypeNull:
  1544. break;
  1545. case kEngineEventTypeControl: {
  1546. const EngineControlEvent& ctrlEvent = event.ctrl;
  1547. switch (ctrlEvent.type)
  1548. {
  1549. case kEngineControlEventTypeNull:
  1550. break;
  1551. case kEngineControlEventTypeParameter: {
  1552. #ifndef BUILD_BRIDGE
  1553. // Control backend stuff
  1554. if (event.channel == pData->ctrlChannel)
  1555. {
  1556. float value;
  1557. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_DRYWET) > 0)
  1558. {
  1559. value = ctrlEvent.value;
  1560. setDryWetRT(value, true);
  1561. }
  1562. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_VOLUME) > 0)
  1563. {
  1564. value = ctrlEvent.value*127.0f/100.0f;
  1565. setVolumeRT(value, true);
  1566. }
  1567. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_BALANCE) > 0)
  1568. {
  1569. float left, right;
  1570. value = ctrlEvent.value/0.5f - 1.0f;
  1571. if (value < 0.0f)
  1572. {
  1573. left = -1.0f;
  1574. right = (value*2.0f)+1.0f;
  1575. }
  1576. else if (value > 0.0f)
  1577. {
  1578. left = (value*2.0f)-1.0f;
  1579. right = 1.0f;
  1580. }
  1581. else
  1582. {
  1583. left = -1.0f;
  1584. right = 1.0f;
  1585. }
  1586. setBalanceLeftRT(left, true);
  1587. setBalanceRightRT(right, true);
  1588. }
  1589. }
  1590. #endif
  1591. // Control plugin parameters
  1592. for (uint32_t k=0; k < pData->param.count; ++k)
  1593. {
  1594. if (pData->param.data[k].midiChannel != event.channel)
  1595. continue;
  1596. if (pData->param.data[k].mappedControlIndex != ctrlEvent.param)
  1597. continue;
  1598. if (pData->param.data[k].type != PARAMETER_INPUT)
  1599. continue;
  1600. if ((pData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  1601. continue;
  1602. float value;
  1603. if (pData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  1604. {
  1605. value = (ctrlEvent.value < 0.5f) ? pData->param.ranges[k].min : pData->param.ranges[k].max;
  1606. }
  1607. else
  1608. {
  1609. if (pData->param.data[k].hints & PARAMETER_IS_LOGARITHMIC)
  1610. value = pData->param.ranges[k].getUnnormalizedLogValue(ctrlEvent.value);
  1611. else
  1612. value = pData->param.ranges[k].getUnnormalizedValue(ctrlEvent.value);
  1613. if (pData->param.data[k].hints & PARAMETER_IS_INTEGER)
  1614. value = std::rint(value);
  1615. }
  1616. setParameterValueRT(k, value, true);
  1617. }
  1618. if ((pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0 && ctrlEvent.param < MAX_MIDI_VALUE)
  1619. {
  1620. if (fMidiEventInCount >= kPluginMaxMidiEvents)
  1621. continue;
  1622. NativeMidiEvent& nativeEvent(fMidiInEvents[fMidiEventInCount++]);
  1623. carla_zeroStruct(nativeEvent);
  1624. nativeEvent.time = isSampleAccurate ? startTime : eventTime;
  1625. nativeEvent.data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  1626. nativeEvent.data[1] = uint8_t(ctrlEvent.param);
  1627. nativeEvent.data[2] = uint8_t(ctrlEvent.value*127.0f);
  1628. nativeEvent.size = 3;
  1629. }
  1630. break;
  1631. } // case kEngineControlEventTypeParameter
  1632. case kEngineControlEventTypeMidiBank:
  1633. if (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES)
  1634. {
  1635. if (event.channel == pData->ctrlChannel)
  1636. nextBankId = ctrlEvent.param;
  1637. }
  1638. else if (pData->options & PLUGIN_OPTION_SEND_PROGRAM_CHANGES)
  1639. {
  1640. if (fMidiEventInCount >= kPluginMaxMidiEvents)
  1641. continue;
  1642. NativeMidiEvent& nativeEvent(fMidiInEvents[fMidiEventInCount++]);
  1643. carla_zeroStruct(nativeEvent);
  1644. nativeEvent.time = isSampleAccurate ? startTime : eventTime;
  1645. nativeEvent.data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  1646. nativeEvent.data[1] = MIDI_CONTROL_BANK_SELECT;
  1647. nativeEvent.data[2] = uint8_t(ctrlEvent.param);
  1648. nativeEvent.size = 3;
  1649. }
  1650. break;
  1651. case kEngineControlEventTypeMidiProgram:
  1652. if (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES)
  1653. {
  1654. if (event.channel < MAX_MIDI_CHANNELS)
  1655. {
  1656. const uint32_t nextProgramId(ctrlEvent.param);
  1657. for (uint32_t k=0; k < pData->midiprog.count; ++k)
  1658. {
  1659. if (pData->midiprog.data[k].bank == nextBankId && pData->midiprog.data[k].program == nextProgramId)
  1660. {
  1661. fDescriptor->set_midi_program(fHandle, event.channel, nextBankId, nextProgramId);
  1662. if (fHandle2 != nullptr)
  1663. fDescriptor->set_midi_program(fHandle2, event.channel, nextBankId, nextProgramId);
  1664. fCurMidiProgs[event.channel] = static_cast<int32_t>(k);
  1665. if (event.channel == pData->ctrlChannel)
  1666. {
  1667. pData->postponeRtEvent(kPluginPostRtEventMidiProgramChange,
  1668. true,
  1669. static_cast<int32_t>(k),
  1670. 0, 0, 0.0f);
  1671. }
  1672. break;
  1673. }
  1674. }
  1675. }
  1676. }
  1677. else if (pData->options & PLUGIN_OPTION_SEND_PROGRAM_CHANGES)
  1678. {
  1679. if (fMidiEventInCount >= kPluginMaxMidiEvents)
  1680. continue;
  1681. NativeMidiEvent& nativeEvent(fMidiInEvents[fMidiEventInCount++]);
  1682. carla_zeroStruct(nativeEvent);
  1683. nativeEvent.time = isSampleAccurate ? startTime : eventTime;
  1684. nativeEvent.data[0] = uint8_t(MIDI_STATUS_PROGRAM_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  1685. nativeEvent.data[1] = uint8_t(ctrlEvent.param);
  1686. nativeEvent.size = 2;
  1687. }
  1688. break;
  1689. case kEngineControlEventTypeAllSoundOff:
  1690. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1691. {
  1692. if (fMidiEventInCount >= kPluginMaxMidiEvents)
  1693. continue;
  1694. NativeMidiEvent& nativeEvent(fMidiInEvents[fMidiEventInCount++]);
  1695. carla_zeroStruct(nativeEvent);
  1696. nativeEvent.time = isSampleAccurate ? startTime : eventTime;
  1697. nativeEvent.data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  1698. nativeEvent.data[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  1699. nativeEvent.data[2] = 0;
  1700. nativeEvent.size = 3;
  1701. }
  1702. break;
  1703. case kEngineControlEventTypeAllNotesOff:
  1704. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1705. {
  1706. #ifndef BUILD_BRIDGE
  1707. if (event.channel == pData->ctrlChannel && ! allNotesOffSent)
  1708. {
  1709. allNotesOffSent = true;
  1710. postponeRtAllNotesOff();
  1711. }
  1712. #endif
  1713. if (fMidiEventInCount >= kPluginMaxMidiEvents)
  1714. continue;
  1715. NativeMidiEvent& nativeEvent(fMidiInEvents[fMidiEventInCount++]);
  1716. carla_zeroStruct(nativeEvent);
  1717. nativeEvent.time = isSampleAccurate ? startTime : eventTime;
  1718. nativeEvent.data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  1719. nativeEvent.data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  1720. nativeEvent.data[2] = 0;
  1721. nativeEvent.size = 3;
  1722. }
  1723. break;
  1724. }
  1725. break;
  1726. }
  1727. case kEngineEventTypeMidi: {
  1728. if (fMidiEventInCount >= kPluginMaxMidiEvents)
  1729. continue;
  1730. const EngineMidiEvent& midiEvent(event.midi);
  1731. if (midiEvent.size > 4)
  1732. continue;
  1733. #ifdef CARLA_PROPER_CPP11_SUPPORT
  1734. static_assert(4 <= EngineMidiEvent::kDataSize, "Incorrect data");
  1735. #endif
  1736. uint8_t status = uint8_t(MIDI_GET_STATUS_FROM_DATA(midiEvent.data));
  1737. if (status == MIDI_STATUS_CHANNEL_PRESSURE && (pData->options & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) == 0)
  1738. continue;
  1739. if (status == MIDI_STATUS_CONTROL_CHANGE && (pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) == 0)
  1740. continue;
  1741. if (status == MIDI_STATUS_POLYPHONIC_AFTERTOUCH && (pData->options & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) == 0)
  1742. continue;
  1743. if (status == MIDI_STATUS_PITCH_WHEEL_CONTROL && (pData->options & PLUGIN_OPTION_SEND_PITCHBEND) == 0)
  1744. continue;
  1745. // Fix bad note-off
  1746. if (status == MIDI_STATUS_NOTE_ON && midiEvent.data[2] == 0)
  1747. status = MIDI_STATUS_NOTE_OFF;
  1748. NativeMidiEvent& nativeEvent(fMidiInEvents[fMidiEventInCount++]);
  1749. carla_zeroStruct(nativeEvent);
  1750. nativeEvent.port = midiEvent.port;
  1751. nativeEvent.time = isSampleAccurate ? startTime : eventTime;
  1752. nativeEvent.size = midiEvent.size;
  1753. nativeEvent.data[0] = uint8_t(status | (event.channel & MIDI_CHANNEL_BIT));
  1754. nativeEvent.data[1] = midiEvent.size >= 2 ? midiEvent.data[1] : 0;
  1755. nativeEvent.data[2] = midiEvent.size >= 3 ? midiEvent.data[2] : 0;
  1756. nativeEvent.data[3] = midiEvent.size == 4 ? midiEvent.data[3] : 0;
  1757. if (status == MIDI_STATUS_NOTE_ON)
  1758. {
  1759. pData->postponeRtEvent(kPluginPostRtEventNoteOn,
  1760. true,
  1761. event.channel,
  1762. midiEvent.data[1],
  1763. midiEvent.data[2],
  1764. 0.0f);
  1765. }
  1766. else if (status == MIDI_STATUS_NOTE_OFF)
  1767. {
  1768. pData->postponeRtEvent(kPluginPostRtEventNoteOff,
  1769. true,
  1770. event.channel,
  1771. midiEvent.data[1],
  1772. 0, 0.0f);
  1773. }
  1774. } break;
  1775. } // switch (event.type)
  1776. }
  1777. pData->postRtEvents.trySplice();
  1778. if (frames > timeOffset)
  1779. processSingle(audioIn, audioOut, cvIn, cvOut, frames - timeOffset, timeOffset);
  1780. } // End of Event Input and Processing
  1781. // --------------------------------------------------------------------------------------------------------
  1782. // Plugin processing (no events)
  1783. else
  1784. {
  1785. processSingle(audioIn, audioOut, cvIn, cvOut, frames, 0);
  1786. } // End of Plugin processing (no events)
  1787. #ifndef BUILD_BRIDGE
  1788. // --------------------------------------------------------------------------------------------------------
  1789. // Control Output
  1790. if (pData->event.portOut != nullptr)
  1791. {
  1792. float value, curValue;
  1793. for (uint32_t k=0; k < pData->param.count; ++k)
  1794. {
  1795. if (pData->param.data[k].type != PARAMETER_OUTPUT)
  1796. continue;
  1797. curValue = fDescriptor->get_parameter_value(fHandle, k);
  1798. pData->param.ranges[k].fixValue(curValue);
  1799. if (pData->param.data[k].mappedControlIndex > 0)
  1800. {
  1801. value = pData->param.ranges[k].getNormalizedValue(curValue);
  1802. pData->event.portOut->writeControlEvent(0,
  1803. pData->param.data[k].midiChannel,
  1804. kEngineControlEventTypeParameter,
  1805. static_cast<uint16_t>(pData->param.data[k].mappedControlIndex),
  1806. value);
  1807. }
  1808. }
  1809. } // End of Control Output
  1810. #endif
  1811. }
  1812. bool processSingle(const float* const* const audioIn, float** const audioOut,
  1813. const float* const* const cvIn, float** const cvOut,
  1814. const uint32_t frames, const uint32_t timeOffset)
  1815. {
  1816. CARLA_SAFE_ASSERT_RETURN(frames > 0, false);
  1817. if (pData->audioIn.count > 0) {
  1818. CARLA_SAFE_ASSERT_RETURN(audioIn != nullptr, false);
  1819. }
  1820. if (pData->audioOut.count > 0) {
  1821. CARLA_SAFE_ASSERT_RETURN(audioOut != nullptr, false);
  1822. }
  1823. if (pData->cvIn.count > 0) {
  1824. CARLA_SAFE_ASSERT_RETURN(cvIn != nullptr, false);
  1825. }
  1826. if (pData->cvOut.count > 0) {
  1827. CARLA_SAFE_ASSERT_RETURN(cvOut != nullptr, false);
  1828. }
  1829. // --------------------------------------------------------------------------------------------------------
  1830. // Try lock, silence otherwise
  1831. if (fIsOffline)
  1832. {
  1833. pData->singleMutex.lock();
  1834. }
  1835. else if (! pData->singleMutex.tryLock())
  1836. {
  1837. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1838. {
  1839. for (uint32_t k=0; k < frames; ++k)
  1840. audioOut[i][k+timeOffset] = 0.0f;
  1841. }
  1842. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  1843. {
  1844. for (uint32_t k=0; k < frames; ++k)
  1845. cvOut[i][k+timeOffset] = 0.0f;
  1846. }
  1847. return false;
  1848. }
  1849. // --------------------------------------------------------------------------------------------------------
  1850. // Set audio buffers
  1851. {
  1852. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1853. carla_copyFloats(fAudioAndCvInBuffers[i], audioIn[i]+timeOffset, frames);
  1854. for (uint32_t i=0; i < pData->cvIn.count; ++i)
  1855. carla_copyFloats(fAudioAndCvInBuffers[pData->audioIn.count+i], cvIn[i]+timeOffset, frames);
  1856. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1857. carla_zeroFloats(fAudioAndCvOutBuffers[i], frames);
  1858. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  1859. carla_zeroFloats(fAudioAndCvOutBuffers[pData->audioOut.count+i], frames);
  1860. }
  1861. // --------------------------------------------------------------------------------------------------------
  1862. // Run plugin
  1863. fIsProcessing = true;
  1864. if (fHandle2 == nullptr)
  1865. {
  1866. fDescriptor->process(fHandle,
  1867. fAudioAndCvInBuffers, fAudioAndCvOutBuffers, frames,
  1868. fMidiInEvents, fMidiEventInCount);
  1869. }
  1870. else
  1871. {
  1872. fDescriptor->process(fHandle,
  1873. (fAudioAndCvInBuffers != nullptr) ? &fAudioAndCvInBuffers[0] : nullptr,
  1874. (fAudioAndCvOutBuffers != nullptr) ? &fAudioAndCvOutBuffers[0] : nullptr,
  1875. frames, fMidiInEvents, fMidiEventInCount);
  1876. fDescriptor->process(fHandle2,
  1877. (fAudioAndCvInBuffers != nullptr) ? &fAudioAndCvInBuffers[1] : nullptr,
  1878. (fAudioAndCvOutBuffers != nullptr) ? &fAudioAndCvOutBuffers[1] : nullptr,
  1879. frames, fMidiInEvents, fMidiEventInCount);
  1880. }
  1881. fIsProcessing = false;
  1882. if (fTimeInfo.playing)
  1883. fTimeInfo.frame += frames;
  1884. uint32_t i=0;
  1885. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1886. // --------------------------------------------------------------------------------------------------------
  1887. // Post-processing (dry/wet, volume and balance)
  1888. {
  1889. const bool doDryWet = (pData->hints & PLUGIN_CAN_DRYWET) != 0 && carla_isNotEqual(pData->postProc.dryWet, 1.0f);
  1890. const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) != 0 && ! (carla_isEqual(pData->postProc.balanceLeft, -1.0f) && carla_isEqual(pData->postProc.balanceRight, 1.0f));
  1891. bool isPair;
  1892. float bufValue, oldBufLeft[doBalance ? frames : 1];
  1893. for (; i < pData->audioOut.count; ++i)
  1894. {
  1895. // Dry/Wet
  1896. if (doDryWet)
  1897. {
  1898. for (uint32_t k=0; k < frames; ++k)
  1899. {
  1900. bufValue = fAudioAndCvInBuffers[(pData->audioIn.count == 1) ? 0 : i][k];
  1901. fAudioAndCvOutBuffers[i][k] = (fAudioAndCvOutBuffers[i][k] * pData->postProc.dryWet) + (bufValue * (1.0f - pData->postProc.dryWet));
  1902. }
  1903. }
  1904. // Balance
  1905. if (doBalance)
  1906. {
  1907. isPair = (i % 2 == 0);
  1908. if (isPair)
  1909. {
  1910. CARLA_ASSERT(i+1 < pData->audioOut.count);
  1911. carla_copyFloats(oldBufLeft, fAudioAndCvOutBuffers[i], frames);
  1912. }
  1913. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  1914. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  1915. for (uint32_t k=0; k < frames; ++k)
  1916. {
  1917. if (isPair)
  1918. {
  1919. // left
  1920. fAudioAndCvOutBuffers[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  1921. fAudioAndCvOutBuffers[i][k] += fAudioAndCvOutBuffers[i+1][k] * (1.0f - balRangeR);
  1922. }
  1923. else
  1924. {
  1925. // right
  1926. fAudioAndCvOutBuffers[i][k] = fAudioAndCvOutBuffers[i][k] * balRangeR;
  1927. fAudioAndCvOutBuffers[i][k] += oldBufLeft[k] * balRangeL;
  1928. }
  1929. }
  1930. }
  1931. // Volume (and buffer copy)
  1932. {
  1933. for (uint32_t k=0; k < frames; ++k)
  1934. audioOut[i][k+timeOffset] = fAudioAndCvOutBuffers[i][k] * pData->postProc.volume;
  1935. }
  1936. }
  1937. } // End of Post-processing
  1938. #else
  1939. for (; i < pData->audioOut.count; ++i)
  1940. {
  1941. for (uint32_t k=0; k < frames; ++k)
  1942. audioOut[i][k+timeOffset] = fAudioAndCvOutBuffers[i][k];
  1943. }
  1944. #endif
  1945. // CV stuff too
  1946. for (; i < pData->cvOut.count; ++i)
  1947. {
  1948. for (uint32_t k=0; k < frames; ++k)
  1949. cvOut[i][k+timeOffset] = fAudioAndCvOutBuffers[pData->audioOut.count+i][k];
  1950. }
  1951. // --------------------------------------------------------------------------------------------------------
  1952. // MIDI Output
  1953. if (pData->event.portOut != nullptr)
  1954. {
  1955. for (uint32_t k = 0; k < fMidiEventOutCount; ++k)
  1956. {
  1957. const uint8_t channel = uint8_t(MIDI_GET_CHANNEL_FROM_DATA(fMidiOutEvents[k].data));
  1958. const uint8_t port = fMidiOutEvents[k].port;
  1959. if (fMidiOut.count > 1 && port < fMidiOut.count)
  1960. fMidiOut.ports[port]->writeMidiEvent(fMidiOutEvents[k].time+timeOffset, channel, fMidiOutEvents[k].size, fMidiOutEvents[k].data);
  1961. else
  1962. pData->event.portOut->writeMidiEvent(fMidiOutEvents[k].time+timeOffset, channel, fMidiOutEvents[k].size, fMidiOutEvents[k].data);
  1963. }
  1964. }
  1965. // --------------------------------------------------------------------------------------------------------
  1966. pData->singleMutex.unlock();
  1967. return true;
  1968. }
  1969. void bufferSizeChanged(const uint32_t newBufferSize) override
  1970. {
  1971. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  1972. carla_debug("CarlaPluginNative::bufferSizeChanged(%i)", newBufferSize);
  1973. for (uint32_t i=0; i < (pData->audioIn.count+pData->cvIn.count); ++i)
  1974. {
  1975. if (fAudioAndCvInBuffers[i] != nullptr)
  1976. delete[] fAudioAndCvInBuffers[i];
  1977. fAudioAndCvInBuffers[i] = new float[newBufferSize];
  1978. }
  1979. for (uint32_t i=0; i < (pData->audioOut.count+pData->cvOut.count); ++i)
  1980. {
  1981. if (fAudioAndCvOutBuffers[i] != nullptr)
  1982. delete[] fAudioAndCvOutBuffers[i];
  1983. fAudioAndCvOutBuffers[i] = new float[newBufferSize];
  1984. }
  1985. if (fCurBufferSize == newBufferSize)
  1986. return;
  1987. fCurBufferSize = newBufferSize;
  1988. if (fDescriptor != nullptr && fDescriptor->dispatcher != nullptr)
  1989. {
  1990. fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_BUFFER_SIZE_CHANGED, 0, static_cast<intptr_t>(newBufferSize), nullptr, 0.0f);
  1991. if (fHandle2 != nullptr)
  1992. fDescriptor->dispatcher(fHandle2, NATIVE_PLUGIN_OPCODE_BUFFER_SIZE_CHANGED, 0, static_cast<intptr_t>(newBufferSize), nullptr, 0.0f);
  1993. }
  1994. }
  1995. void sampleRateChanged(const double newSampleRate) override
  1996. {
  1997. CARLA_ASSERT_INT(newSampleRate > 0.0, newSampleRate);
  1998. carla_debug("CarlaPluginNative::sampleRateChanged(%g)", newSampleRate);
  1999. if (carla_isEqual(fCurSampleRate, newSampleRate))
  2000. return;
  2001. fCurSampleRate = newSampleRate;
  2002. if (fDescriptor != nullptr && fDescriptor->dispatcher != nullptr)
  2003. {
  2004. fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_SAMPLE_RATE_CHANGED, 0, 0, nullptr, float(newSampleRate));
  2005. if (fHandle2 != nullptr)
  2006. fDescriptor->dispatcher(fHandle2, NATIVE_PLUGIN_OPCODE_SAMPLE_RATE_CHANGED, 0, 0, nullptr, float(newSampleRate));
  2007. }
  2008. }
  2009. void offlineModeChanged(const bool isOffline) override
  2010. {
  2011. if (fIsOffline == isOffline)
  2012. return;
  2013. fIsOffline = isOffline;
  2014. if (fDescriptor != nullptr && fDescriptor->dispatcher != nullptr)
  2015. {
  2016. fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_OFFLINE_CHANGED, 0, isOffline ? 1 : 0, nullptr, 0.0f);
  2017. if (fHandle2 != nullptr)
  2018. fDescriptor->dispatcher(fHandle2, NATIVE_PLUGIN_OPCODE_OFFLINE_CHANGED, 0, isOffline ? 1 : 0, nullptr, 0.0f);
  2019. }
  2020. }
  2021. // -------------------------------------------------------------------
  2022. // Plugin buffers
  2023. void initBuffers() const noexcept override
  2024. {
  2025. CarlaPlugin::initBuffers();
  2026. fMidiIn.initBuffers(pData->event.portIn);
  2027. fMidiOut.initBuffers();
  2028. }
  2029. void clearBuffers() noexcept override
  2030. {
  2031. carla_debug("CarlaPluginNative::clearBuffers() - start");
  2032. if (fAudioAndCvInBuffers != nullptr)
  2033. {
  2034. for (uint32_t i=0; i < (pData->audioIn.count+pData->cvIn.count); ++i)
  2035. {
  2036. if (fAudioAndCvInBuffers[i] != nullptr)
  2037. {
  2038. delete[] fAudioAndCvInBuffers[i];
  2039. fAudioAndCvInBuffers[i] = nullptr;
  2040. }
  2041. }
  2042. delete[] fAudioAndCvInBuffers;
  2043. fAudioAndCvInBuffers = nullptr;
  2044. }
  2045. if (fAudioAndCvOutBuffers != nullptr)
  2046. {
  2047. for (uint32_t i=0; i < (pData->audioOut.count+pData->cvOut.count); ++i)
  2048. {
  2049. if (fAudioAndCvOutBuffers[i] != nullptr)
  2050. {
  2051. delete[] fAudioAndCvOutBuffers[i];
  2052. fAudioAndCvOutBuffers[i] = nullptr;
  2053. }
  2054. }
  2055. delete[] fAudioAndCvOutBuffers;
  2056. fAudioAndCvOutBuffers = nullptr;
  2057. }
  2058. if (fMidiIn.count > 1)
  2059. pData->event.portIn = nullptr;
  2060. if (fMidiOut.count > 1)
  2061. pData->event.portOut = nullptr;
  2062. fMidiIn.clear();
  2063. fMidiOut.clear();
  2064. CarlaPlugin::clearBuffers();
  2065. carla_debug("CarlaPluginNative::clearBuffers() - end");
  2066. }
  2067. // -------------------------------------------------------------------
  2068. // Post-poned UI Stuff
  2069. void uiParameterChange(const uint32_t index, const float value) noexcept override
  2070. {
  2071. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  2072. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  2073. CARLA_SAFE_ASSERT_RETURN(index < pData->param.count,);
  2074. if (! fIsUiVisible)
  2075. return;
  2076. if (fDescriptor->ui_set_parameter_value != nullptr)
  2077. fDescriptor->ui_set_parameter_value(fHandle, index, value);
  2078. }
  2079. void uiMidiProgramChange(const uint32_t index) noexcept override
  2080. {
  2081. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  2082. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  2083. CARLA_SAFE_ASSERT_RETURN(index < pData->midiprog.count,);
  2084. if (! fIsUiVisible)
  2085. return;
  2086. if (index >= pData->midiprog.count)
  2087. return;
  2088. if (fDescriptor->ui_set_midi_program != nullptr)
  2089. fDescriptor->ui_set_midi_program(fHandle, 0, pData->midiprog.data[index].bank, pData->midiprog.data[index].program);
  2090. }
  2091. void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) noexcept override
  2092. {
  2093. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  2094. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  2095. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  2096. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  2097. CARLA_SAFE_ASSERT_RETURN(velo > 0 && velo < MAX_MIDI_VALUE,);
  2098. if (! fIsUiVisible)
  2099. return;
  2100. // TODO
  2101. }
  2102. void uiNoteOff(const uint8_t channel, const uint8_t note) noexcept override
  2103. {
  2104. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  2105. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  2106. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  2107. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  2108. if (! fIsUiVisible)
  2109. return;
  2110. if (fDescriptor == nullptr || fHandle == nullptr)
  2111. return;
  2112. // TODO
  2113. }
  2114. // -------------------------------------------------------------------
  2115. const NativeInlineDisplayImageSurface* renderInlineDisplay(const uint32_t width, const uint32_t height)
  2116. {
  2117. CARLA_SAFE_ASSERT_RETURN(fDescriptor->hints & NATIVE_PLUGIN_HAS_INLINE_DISPLAY, nullptr);
  2118. CARLA_SAFE_ASSERT_RETURN(fDescriptor->render_inline_display, nullptr);
  2119. CARLA_SAFE_ASSERT_RETURN(width > 0, nullptr);
  2120. CARLA_SAFE_ASSERT_RETURN(height > 0, nullptr);
  2121. return fDescriptor->render_inline_display(fHandle, width, height);
  2122. }
  2123. // -------------------------------------------------------------------
  2124. protected:
  2125. const NativeTimeInfo* handleGetTimeInfo() const noexcept
  2126. {
  2127. CARLA_SAFE_ASSERT_RETURN(fIsProcessing, nullptr);
  2128. return &fTimeInfo;
  2129. }
  2130. bool handleWriteMidiEvent(const NativeMidiEvent* const event)
  2131. {
  2132. CARLA_SAFE_ASSERT_RETURN(pData->enabled, false);
  2133. CARLA_SAFE_ASSERT_RETURN(fIsProcessing, false);
  2134. CARLA_SAFE_ASSERT_RETURN(fMidiOut.count > 0 || pData->event.portOut != nullptr, false);
  2135. CARLA_SAFE_ASSERT_RETURN(event != nullptr, false);
  2136. CARLA_SAFE_ASSERT_RETURN(event->data[0] != 0, false);
  2137. if (fMidiEventOutCount == kPluginMaxMidiEvents)
  2138. {
  2139. carla_stdout("CarlaPluginNative::handleWriteMidiEvent(%p) - buffer full", event);
  2140. return false;
  2141. }
  2142. std::memcpy(&fMidiOutEvents[fMidiEventOutCount++], event, sizeof(NativeMidiEvent));
  2143. return true;
  2144. }
  2145. void handleUiParameterChanged(const uint32_t index, const float value)
  2146. {
  2147. setParameterValue(index, value, false, true, true);
  2148. }
  2149. void handleUiCustomDataChanged(const char* const key, const char* const value)
  2150. {
  2151. setCustomData(CUSTOM_DATA_TYPE_STRING, key, value, false);
  2152. }
  2153. void handleUiClosed()
  2154. {
  2155. pData->engine->callback(true, true, ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, 0, 0, 0, 0.0f, nullptr);
  2156. fIsUiVisible = false;
  2157. }
  2158. const char* handleUiOpenFile(const bool isDir, const char* const title, const char* const filter)
  2159. {
  2160. return pData->engine->runFileCallback(FILE_CALLBACK_OPEN, isDir, title, filter);
  2161. }
  2162. const char* handleUiSaveFile(const bool isDir, const char* const title, const char* const filter)
  2163. {
  2164. return pData->engine->runFileCallback(FILE_CALLBACK_SAVE, isDir, title, filter);
  2165. }
  2166. intptr_t handleDispatcher(const NativeHostDispatcherOpcode opcode,
  2167. const int32_t index, const intptr_t value, void* const ptr, const float opt)
  2168. {
  2169. carla_debug("CarlaPluginNative::handleDispatcher(%i, %i, " P_INTPTR ", %p, %f)",
  2170. opcode, index, value, ptr, static_cast<double>(opt));
  2171. intptr_t ret = 0;
  2172. switch (opcode)
  2173. {
  2174. case NATIVE_HOST_OPCODE_NULL:
  2175. break;
  2176. case NATIVE_HOST_OPCODE_UPDATE_PARAMETER:
  2177. // TODO
  2178. pData->engine->callback(true, true, ENGINE_CALLBACK_UPDATE, pData->id, -1, 0, 0, 0.0f, nullptr);
  2179. break;
  2180. case NATIVE_HOST_OPCODE_UPDATE_MIDI_PROGRAM:
  2181. // TODO
  2182. pData->engine->callback(true, true, ENGINE_CALLBACK_UPDATE, pData->id, -1, 0, 0, 0.0f, nullptr);
  2183. break;
  2184. case NATIVE_HOST_OPCODE_RELOAD_PARAMETERS:
  2185. reloadParameters(nullptr, nullptr);
  2186. pData->engine->callback(true, true, ENGINE_CALLBACK_RELOAD_ALL, pData->id, -1, 0, 0, 0.0f, nullptr);
  2187. break;
  2188. case NATIVE_HOST_OPCODE_RELOAD_MIDI_PROGRAMS:
  2189. reloadPrograms(false);
  2190. pData->engine->callback(true, true, ENGINE_CALLBACK_RELOAD_PROGRAMS, pData->id, -1, 0, 0, 0.0f, nullptr);
  2191. break;
  2192. case NATIVE_HOST_OPCODE_RELOAD_ALL:
  2193. reload();
  2194. pData->engine->callback(true, true, ENGINE_CALLBACK_RELOAD_ALL, pData->id, -1, 0, 0, 0.0f, nullptr);
  2195. break;
  2196. case NATIVE_HOST_OPCODE_UI_UNAVAILABLE:
  2197. pData->engine->callback(true, true, ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, -1, 0, 0, 0.0f, nullptr);
  2198. fIsUiAvailable = false;
  2199. break;
  2200. case NATIVE_HOST_OPCODE_HOST_IDLE:
  2201. pData->engine->callback(true, false, ENGINE_CALLBACK_IDLE, 0, 0, 0, 0, 0.0f, nullptr);
  2202. break;
  2203. case NATIVE_HOST_OPCODE_INTERNAL_PLUGIN:
  2204. ret = 1;
  2205. break;
  2206. case NATIVE_HOST_OPCODE_QUEUE_INLINE_DISPLAY:
  2207. fInlineDisplayNeedsRedraw = true;
  2208. break;
  2209. case NATIVE_HOST_OPCODE_UI_TOUCH_PARAMETER:
  2210. CARLA_SAFE_ASSERT_RETURN(index >= 0, 0);
  2211. pData->engine->touchPluginParameter(pData->id, static_cast<uint32_t>(index), value != 0);
  2212. break;
  2213. case NATIVE_HOST_OPCODE_REQUEST_IDLE:
  2214. fNeedsIdle = true;
  2215. break;
  2216. case NATIVE_HOST_OPCODE_GET_FILE_PATH:
  2217. CARLA_SAFE_ASSERT_RETURN(ptr != nullptr, 0);
  2218. {
  2219. const EngineOptions& opts(pData->engine->getOptions());
  2220. const char* const filetype = (const char*)ptr;
  2221. if (std::strcmp(filetype, "audio") == 0)
  2222. return static_cast<intptr_t>((uintptr_t)opts.pathAudio);
  2223. if (std::strcmp(filetype, "midi") == 0)
  2224. return static_cast<intptr_t>((uintptr_t)opts.pathMIDI);
  2225. }
  2226. break;
  2227. }
  2228. return ret;
  2229. // unused for now
  2230. (void)opt;
  2231. }
  2232. // -------------------------------------------------------------------
  2233. public:
  2234. void* getNativeHandle() const noexcept override
  2235. {
  2236. return fHandle;
  2237. }
  2238. const void* getNativeDescriptor() const noexcept override
  2239. {
  2240. return fDescriptor;
  2241. }
  2242. // -------------------------------------------------------------------
  2243. bool init(const char* const name, const char* const label, const uint options)
  2244. {
  2245. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  2246. // ---------------------------------------------------------------
  2247. // first checks
  2248. if (pData->client != nullptr)
  2249. {
  2250. pData->engine->setLastError("Plugin client is already registered");
  2251. return false;
  2252. }
  2253. if (label == nullptr || label[0] == '\0')
  2254. {
  2255. pData->engine->setLastError("null label");
  2256. return false;
  2257. }
  2258. // ---------------------------------------------------------------
  2259. // get descriptor that matches label
  2260. sPluginInitializer.initIfNeeded();
  2261. for (LinkedList<const NativePluginDescriptor*>::Itenerator it = gPluginDescriptors.begin2(); it.valid(); it.next())
  2262. {
  2263. fDescriptor = it.getValue(nullptr);
  2264. CARLA_SAFE_ASSERT_BREAK(fDescriptor != nullptr);
  2265. carla_debug("Check vs \"%s\"", fDescriptor->label);
  2266. if (fDescriptor->label != nullptr && std::strcmp(fDescriptor->label, label) == 0)
  2267. break;
  2268. fDescriptor = nullptr;
  2269. }
  2270. if (fDescriptor == nullptr)
  2271. {
  2272. pData->engine->setLastError("Invalid internal plugin");
  2273. return false;
  2274. }
  2275. // ---------------------------------------------------------------
  2276. // set icon
  2277. /**/ if (std::strcmp(fDescriptor->label, "audiofile") == 0)
  2278. pData->iconName = carla_strdup_safe("file");
  2279. else if (std::strcmp(fDescriptor->label, "midifile") == 0)
  2280. pData->iconName = carla_strdup_safe("file");
  2281. else if (std::strcmp(fDescriptor->label, "3bandeq") == 0)
  2282. pData->iconName = carla_strdup_safe("distrho");
  2283. else if (std::strcmp(fDescriptor->label, "3bandsplitter") == 0)
  2284. pData->iconName = carla_strdup_safe("distrho");
  2285. else if (std::strcmp(fDescriptor->label, "kars") == 0)
  2286. pData->iconName = carla_strdup_safe("distrho");
  2287. else if (std::strcmp(fDescriptor->label, "nekobi") == 0)
  2288. pData->iconName = carla_strdup_safe("distrho");
  2289. else if (std::strcmp(fDescriptor->label, "pingpongpan") == 0)
  2290. pData->iconName = carla_strdup_safe("distrho");
  2291. // ---------------------------------------------------------------
  2292. // set info
  2293. if (name != nullptr && name[0] != '\0')
  2294. pData->name = pData->engine->getUniquePluginName(name);
  2295. else if (fDescriptor->name != nullptr && fDescriptor->name[0] != '\0')
  2296. pData->name = pData->engine->getUniquePluginName(fDescriptor->name);
  2297. else
  2298. pData->name = pData->engine->getUniquePluginName(label);
  2299. {
  2300. CARLA_ASSERT(fHost.uiName == nullptr);
  2301. char uiName[std::strlen(pData->name)+6+1];
  2302. std::strcpy(uiName, pData->name);
  2303. std::strcat(uiName, " (GUI)");
  2304. fHost.uiName = carla_strdup(uiName);
  2305. }
  2306. // ---------------------------------------------------------------
  2307. // register client
  2308. pData->client = pData->engine->addClient(this);
  2309. if (pData->client == nullptr || ! pData->client->isOk())
  2310. {
  2311. pData->engine->setLastError("Failed to register plugin client");
  2312. return false;
  2313. }
  2314. // ---------------------------------------------------------------
  2315. // initialize plugin
  2316. fHandle = fDescriptor->instantiate(&fHost);
  2317. if (fHandle == nullptr)
  2318. {
  2319. pData->engine->setLastError("Plugin failed to initialize");
  2320. return false;
  2321. }
  2322. // ---------------------------------------------------------------
  2323. // set options
  2324. bool hasMidiProgs = false;
  2325. if (fDescriptor->get_midi_program_count != nullptr)
  2326. {
  2327. try {
  2328. hasMidiProgs = fDescriptor->get_midi_program_count(fHandle) > 0;
  2329. } catch (...) {}
  2330. }
  2331. pData->options = 0x0;
  2332. if (fDescriptor->hints & NATIVE_PLUGIN_NEEDS_FIXED_BUFFERS)
  2333. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  2334. else if (options & PLUGIN_OPTION_FIXED_BUFFERS)
  2335. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  2336. if (pData->engine->getOptions().forceStereo)
  2337. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  2338. else if (options & PLUGIN_OPTION_FORCE_STEREO)
  2339. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  2340. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_CONTROL_CHANGES)
  2341. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_CONTROL_CHANGES))
  2342. pData->options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  2343. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_CHANNEL_PRESSURE)
  2344. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_CHANNEL_PRESSURE))
  2345. pData->options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  2346. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_NOTE_AFTERTOUCH)
  2347. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH))
  2348. pData->options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  2349. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_PITCHBEND)
  2350. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_PITCHBEND))
  2351. pData->options |= PLUGIN_OPTION_SEND_PITCHBEND;
  2352. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_ALL_SOUND_OFF)
  2353. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_ALL_SOUND_OFF))
  2354. pData->options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  2355. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_PROGRAM_CHANGES)
  2356. {
  2357. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_PROGRAM_CHANGES))
  2358. pData->options |= PLUGIN_OPTION_SEND_PROGRAM_CHANGES;
  2359. // makes no sense for a plugin to set program changes supported, but it has no midi programs
  2360. CARLA_SAFE_ASSERT(! hasMidiProgs);
  2361. }
  2362. else if (hasMidiProgs)
  2363. {
  2364. if (isPluginOptionEnabled(options, PLUGIN_OPTION_MAP_PROGRAM_CHANGES))
  2365. pData->options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  2366. }
  2367. return true;
  2368. }
  2369. private:
  2370. NativePluginHandle fHandle;
  2371. NativePluginHandle fHandle2;
  2372. NativeHostDescriptor fHost;
  2373. const NativePluginDescriptor* fDescriptor;
  2374. bool fIsProcessing;
  2375. bool fIsOffline;
  2376. bool fIsUiAvailable;
  2377. bool fIsUiVisible;
  2378. volatile bool fNeedsIdle;
  2379. bool fInlineDisplayNeedsRedraw;
  2380. int64_t fInlineDisplayLastRedrawTime;
  2381. float** fAudioAndCvInBuffers;
  2382. float** fAudioAndCvOutBuffers;
  2383. uint32_t fMidiEventInCount;
  2384. uint32_t fMidiEventOutCount;
  2385. NativeMidiEvent fMidiInEvents[kPluginMaxMidiEvents];
  2386. NativeMidiEvent fMidiOutEvents[kPluginMaxMidiEvents];
  2387. int32_t fCurMidiProgs[MAX_MIDI_CHANNELS];
  2388. uint32_t fCurBufferSize;
  2389. double fCurSampleRate;
  2390. NativePluginMidiInData fMidiIn;
  2391. NativePluginMidiOutData fMidiOut;
  2392. NativeTimeInfo fTimeInfo;
  2393. // -------------------------------------------------------------------
  2394. #define handlePtr ((CarlaPluginNative*)handle)
  2395. static uint32_t carla_host_get_buffer_size(NativeHostHandle handle) noexcept
  2396. {
  2397. return handlePtr->fCurBufferSize;
  2398. }
  2399. static double carla_host_get_sample_rate(NativeHostHandle handle) noexcept
  2400. {
  2401. return handlePtr->fCurSampleRate;
  2402. }
  2403. static bool carla_host_is_offline(NativeHostHandle handle) noexcept
  2404. {
  2405. return handlePtr->fIsOffline;
  2406. }
  2407. static const NativeTimeInfo* carla_host_get_time_info(NativeHostHandle handle) noexcept
  2408. {
  2409. return handlePtr->handleGetTimeInfo();
  2410. }
  2411. static bool carla_host_write_midi_event(NativeHostHandle handle, const NativeMidiEvent* event)
  2412. {
  2413. return handlePtr->handleWriteMidiEvent(event);
  2414. }
  2415. static void carla_host_ui_parameter_changed(NativeHostHandle handle, uint32_t index, float value)
  2416. {
  2417. handlePtr->handleUiParameterChanged(index, value);
  2418. }
  2419. static void carla_host_ui_custom_data_changed(NativeHostHandle handle, const char* key, const char* value)
  2420. {
  2421. handlePtr->handleUiCustomDataChanged(key, value);
  2422. }
  2423. static void carla_host_ui_closed(NativeHostHandle handle)
  2424. {
  2425. handlePtr->handleUiClosed();
  2426. }
  2427. static const char* carla_host_ui_open_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
  2428. {
  2429. return handlePtr->handleUiOpenFile(isDir, title, filter);
  2430. }
  2431. static const char* carla_host_ui_save_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
  2432. {
  2433. return handlePtr->handleUiSaveFile(isDir, title, filter);
  2434. }
  2435. static intptr_t carla_host_dispatcher(NativeHostHandle handle, NativeHostDispatcherOpcode opcode, int32_t index, intptr_t value, void* ptr, float opt)
  2436. {
  2437. return handlePtr->handleDispatcher(opcode, index, value, ptr, opt);
  2438. }
  2439. #undef handlePtr
  2440. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaPluginNative)
  2441. };
  2442. // -----------------------------------------------------------------------
  2443. CarlaPlugin* CarlaPlugin::newNative(const Initializer& init)
  2444. {
  2445. carla_debug("CarlaPlugin::newNative({%p, \"%s\", \"%s\", \"%s\", " P_INT64 "})", init.engine, init.filename, init.name, init.label, init.uniqueId);
  2446. CarlaPluginNative* const plugin(new CarlaPluginNative(init.engine, init.id));
  2447. if (! plugin->init(init.name, init.label, init.options))
  2448. {
  2449. delete plugin;
  2450. return nullptr;
  2451. }
  2452. return plugin;
  2453. }
  2454. // used in CarlaStandalone.cpp
  2455. const void* carla_render_inline_display_internal(CarlaPlugin* plugin, uint32_t width, uint32_t height);
  2456. const void* carla_render_inline_display_internal(CarlaPlugin* plugin, uint32_t width, uint32_t height)
  2457. {
  2458. CarlaPluginNative* const nativePlugin = (CarlaPluginNative*)plugin;
  2459. return nativePlugin->renderInlineDisplay(width, height);
  2460. }
  2461. // -----------------------------------------------------------------------
  2462. CARLA_BACKEND_END_NAMESPACE