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.

2606 lines
87KB

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