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.

3038 lines
109KB

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