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.

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