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.

2608 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 "CarlaNative.h"
  20. extern "C" {
  21. // Simple plugins
  22. void carla_register_native_plugin_bypass();
  23. void carla_register_native_plugin_lfo();
  24. void carla_register_native_plugin_midiSequencer();
  25. void carla_register_native_plugin_midiSplit();
  26. void carla_register_native_plugin_midiThrough();
  27. void carla_register_native_plugin_midiTranspose();
  28. void carla_register_native_plugin_nekofilter();
  29. void carla_register_native_plugin_sunvoxfile();
  30. void carla_register_native_plugin_vex();
  31. #ifndef BUILD_BRIDGE
  32. // Carla
  33. void carla_register_native_plugin_carla();
  34. #endif
  35. #ifdef WANT_AUDIOFILE
  36. // AudioFile
  37. void carla_register_native_plugin_audiofile();
  38. #endif
  39. #ifdef WANT_MIDIFILE
  40. // MidiFile
  41. void carla_register_native_plugin_midifile();
  42. #endif
  43. #ifdef WANT_OPENGL
  44. // DISTRHO plugins (OpenGL)
  45. void carla_register_native_plugin_3BandEQ();
  46. void carla_register_native_plugin_3BandSplitter();
  47. void carla_register_native_plugin_Nekobi();
  48. void carla_register_native_plugin_PingPongPan();
  49. //void carla_register_native_plugin_StereoEnhancer();
  50. #endif
  51. // DISTRHO plugins (Qt)
  52. void carla_register_native_plugin_Notes();
  53. #ifdef WANT_ZYNADDSUBFX
  54. // ZynAddSubFX
  55. void carla_register_native_plugin_zynaddsubfx();
  56. #endif
  57. }
  58. void carla_register_all_plugins()
  59. {
  60. // Simple plugins
  61. carla_register_native_plugin_bypass();
  62. carla_register_native_plugin_lfo();
  63. //carla_register_native_plugin_midiSequencer(); // unfinished
  64. carla_register_native_plugin_midiSplit();
  65. carla_register_native_plugin_midiThrough();
  66. carla_register_native_plugin_midiTranspose();
  67. carla_register_native_plugin_nekofilter();
  68. //carla_register_native_plugin_sunvoxfile(); // unfinished
  69. carla_register_native_plugin_vex();
  70. #ifndef BUILD_BRIDGE
  71. // Carla
  72. //carla_register_native_plugin_carla(); // kinda unfinished
  73. #endif
  74. #ifdef WANT_AUDIOFILE
  75. // AudioFile
  76. carla_register_native_plugin_audiofile();
  77. #endif
  78. #ifdef WANT_MIDIFILE
  79. // MidiFile
  80. carla_register_native_plugin_midifile();
  81. #endif
  82. #ifdef WANT_OPENGL
  83. // DISTRHO plugins (OpenGL)
  84. carla_register_native_plugin_3BandEQ();
  85. carla_register_native_plugin_3BandSplitter();
  86. carla_register_native_plugin_Nekobi();
  87. carla_register_native_plugin_PingPongPan();
  88. //carla_register_native_plugin_StereoEnhancer(); // unfinished
  89. #endif
  90. // DISTRHO plugins (Qt)
  91. carla_register_native_plugin_Notes(); // unfinished
  92. #ifdef WANT_ZYNADDSUBFX
  93. // ZynAddSubFX
  94. carla_register_native_plugin_zynaddsubfx();
  95. #endif
  96. }
  97. CARLA_BACKEND_START_NAMESPACE
  98. #if 0
  99. }
  100. #endif
  101. struct NativePluginMidiData {
  102. uint32_t count;
  103. uint32_t* indexes;
  104. CarlaEngineEventPort** ports;
  105. NativePluginMidiData()
  106. : count(0),
  107. indexes(nullptr),
  108. ports(nullptr) {}
  109. ~NativePluginMidiData()
  110. {
  111. CARLA_ASSERT_INT(count == 0, count);
  112. CARLA_ASSERT(ports == nullptr);
  113. CARLA_ASSERT(indexes == nullptr);
  114. }
  115. void createNew(const uint32_t newCount)
  116. {
  117. CARLA_ASSERT_INT(count == 0, count);
  118. CARLA_ASSERT(ports == nullptr);
  119. CARLA_ASSERT(indexes == nullptr);
  120. CARLA_ASSERT_INT(newCount > 0, newCount);
  121. if (ports != nullptr || indexes != nullptr || newCount == 0)
  122. return;
  123. ports = new CarlaEngineEventPort*[newCount];
  124. indexes = new uint32_t[newCount];
  125. count = newCount;
  126. for (uint32_t i=0; i < newCount; ++i)
  127. ports[i] = nullptr;
  128. for (uint32_t i=0; i < newCount; ++i)
  129. indexes[i] = 0;
  130. }
  131. void clear()
  132. {
  133. if (ports != nullptr)
  134. {
  135. for (uint32_t i=0; i < count; ++i)
  136. {
  137. if (ports[i] != nullptr)
  138. {
  139. delete ports[i];
  140. ports[i] = nullptr;
  141. }
  142. }
  143. delete[] ports;
  144. ports = nullptr;
  145. }
  146. if (indexes != nullptr)
  147. {
  148. delete[] indexes;
  149. indexes = nullptr;
  150. }
  151. count = 0;
  152. }
  153. void initBuffers()
  154. {
  155. for (uint32_t i=0; i < count; ++i)
  156. {
  157. if (ports[i] != nullptr)
  158. ports[i]->initBuffer();
  159. }
  160. }
  161. CARLA_DECLARE_NON_COPY_STRUCT(NativePluginMidiData)
  162. };
  163. class NativePlugin : public CarlaPlugin
  164. {
  165. public:
  166. NativePlugin(CarlaEngine* const engine, const unsigned int id)
  167. : CarlaPlugin(engine, id),
  168. fHandle(nullptr),
  169. fHandle2(nullptr),
  170. fDescriptor(nullptr),
  171. fIsProcessing(false),
  172. fIsUiVisible(false),
  173. fAudioInBuffers(nullptr),
  174. fAudioOutBuffers(nullptr),
  175. #ifdef CARLA_PROPER_CPP11_SUPPORT
  176. fMidiEventCount(0),
  177. fCurMidiProgs{0}
  178. #else
  179. fMidiEventCount(0)
  180. #endif
  181. {
  182. carla_debug("NativePlugin::NativePlugin(%p, %i)", engine, id);
  183. #ifndef CARLA_PROPER_CPP11_SUPPORT
  184. carla_fill<int32_t>(fCurMidiProgs, MAX_MIDI_CHANNELS, 0);
  185. #endif
  186. carla_zeroStruct< ::MidiEvent>(fMidiEvents, MAX_MIDI_EVENTS*2);
  187. carla_zeroStruct< ::TimeInfo>(fTimeInfo);
  188. fHost.handle = this;
  189. fHost.resourceDir = carla_strdup((const char*)engine->getOptions().resourceDir);
  190. fHost.uiName = nullptr;
  191. fHost.get_buffer_size = carla_host_get_buffer_size;
  192. fHost.get_sample_rate = carla_host_get_sample_rate;
  193. fHost.is_offline = carla_host_is_offline;
  194. fHost.get_time_info = carla_host_get_time_info;
  195. fHost.write_midi_event = carla_host_write_midi_event;
  196. fHost.ui_parameter_changed = carla_host_ui_parameter_changed;
  197. fHost.ui_custom_data_changed = carla_host_ui_custom_data_changed;
  198. fHost.ui_closed = carla_host_ui_closed;
  199. fHost.ui_open_file = carla_host_ui_open_file;
  200. fHost.ui_save_file = carla_host_ui_save_file;
  201. fHost.dispatcher = carla_host_dispatcher;
  202. }
  203. ~NativePlugin() override
  204. {
  205. carla_debug("NativePlugin::~NativePlugin()");
  206. // close UI
  207. if (fHints & PLUGIN_HAS_GUI)
  208. {
  209. if (fIsUiVisible && fDescriptor != nullptr && fDescriptor->ui_show != nullptr && fHandle != nullptr)
  210. fDescriptor->ui_show(fHandle, false);
  211. }
  212. pData->singleMutex.lock();
  213. pData->masterMutex.lock();
  214. if (pData->client != nullptr && pData->client->isActive())
  215. pData->client->deactivate();
  216. CARLA_ASSERT(! fIsProcessing);
  217. if (pData->active)
  218. {
  219. deactivate();
  220. pData->active = false;
  221. }
  222. if (fDescriptor != nullptr)
  223. {
  224. if (fDescriptor->cleanup != nullptr)
  225. {
  226. if (fHandle != nullptr)
  227. fDescriptor->cleanup(fHandle);
  228. if (fHandle2 != nullptr)
  229. fDescriptor->cleanup(fHandle2);
  230. }
  231. fHandle = nullptr;
  232. fHandle2 = nullptr;
  233. fDescriptor = nullptr;
  234. }
  235. if (fHost.resourceDir != nullptr)
  236. {
  237. delete[] fHost.resourceDir;
  238. fHost.resourceDir = nullptr;
  239. }
  240. if (fHost.uiName != nullptr)
  241. {
  242. delete[] fHost.uiName;
  243. fHost.uiName = nullptr;
  244. }
  245. clearBuffers();
  246. }
  247. // -------------------------------------------------------------------
  248. // Information (base)
  249. PluginType getType() const noexcept override
  250. {
  251. return PLUGIN_INTERNAL;
  252. }
  253. PluginCategory getCategory() const override
  254. {
  255. CARLA_ASSERT(fDescriptor != nullptr);
  256. return static_cast<PluginCategory>(fDescriptor->category);
  257. }
  258. // -------------------------------------------------------------------
  259. // Information (count)
  260. uint32_t getMidiInCount() const noexcept override
  261. {
  262. return fMidiIn.count;
  263. }
  264. uint32_t getMidiOutCount() const noexcept override
  265. {
  266. return fMidiOut.count;
  267. }
  268. uint32_t getParameterScalePointCount(const uint32_t parameterId) const override
  269. {
  270. CARLA_ASSERT(fDescriptor != nullptr);
  271. CARLA_ASSERT(fHandle != nullptr);
  272. CARLA_ASSERT(parameterId < pData->param.count);
  273. if (fDescriptor->get_parameter_info != nullptr && parameterId < pData->param.count)
  274. {
  275. if (const Parameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId))
  276. return param->scalePointCount;
  277. }
  278. return 0;
  279. }
  280. // -------------------------------------------------------------------
  281. // Information (current data)
  282. // nothing
  283. // -------------------------------------------------------------------
  284. // Information (per-plugin data)
  285. unsigned int getAvailableOptions() const override
  286. {
  287. CARLA_ASSERT(fDescriptor != nullptr);
  288. if (fDescriptor == nullptr)
  289. return 0x0;
  290. const bool hasMidiProgs(fDescriptor->get_midi_program_count != nullptr && fDescriptor->get_midi_program_count(fHandle) > 0);
  291. unsigned int options = 0x0;
  292. if (hasMidiProgs && (fDescriptor->supports & ::PLUGIN_SUPPORTS_PROGRAM_CHANGES) == 0)
  293. options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  294. if (getMidiInCount() == 0 && (fDescriptor->hints & ::PLUGIN_NEEDS_FIXED_BUFFERS) == 0)
  295. options |= PLUGIN_OPTION_FIXED_BUFFERS;
  296. if (pData->engine->getProccessMode() != PROCESS_MODE_CONTINUOUS_RACK)
  297. {
  298. if (fOptions & PLUGIN_OPTION_FORCE_STEREO)
  299. options |= PLUGIN_OPTION_FORCE_STEREO;
  300. else if (pData->audioIn.count <= 1 && pData->audioOut.count <= 1 && (pData->audioIn.count != 0 || pData->audioOut.count != 0))
  301. options |= PLUGIN_OPTION_FORCE_STEREO;
  302. }
  303. if (fDescriptor->supports & ::PLUGIN_SUPPORTS_CONTROL_CHANGES)
  304. options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  305. if (fDescriptor->supports & ::PLUGIN_SUPPORTS_CHANNEL_PRESSURE)
  306. options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  307. if (fDescriptor->supports & ::PLUGIN_SUPPORTS_NOTE_AFTERTOUCH)
  308. options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  309. if (fDescriptor->supports & ::PLUGIN_SUPPORTS_PITCHBEND)
  310. options |= PLUGIN_OPTION_SEND_PITCHBEND;
  311. if (fDescriptor->supports & ::PLUGIN_SUPPORTS_ALL_SOUND_OFF)
  312. options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  313. return options;
  314. }
  315. float getParameterValue(const uint32_t parameterId) const override
  316. {
  317. CARLA_ASSERT(fDescriptor != nullptr);
  318. CARLA_ASSERT(fHandle != nullptr);
  319. CARLA_ASSERT(parameterId < pData->param.count);
  320. if (fDescriptor->get_parameter_value != nullptr && parameterId < pData->param.count)
  321. return fDescriptor->get_parameter_value(fHandle, parameterId);
  322. return 0.0f;
  323. }
  324. float getParameterScalePointValue(const uint32_t parameterId, const uint32_t scalePointId) const override
  325. {
  326. CARLA_ASSERT(fDescriptor != nullptr);
  327. CARLA_ASSERT(fHandle != nullptr);
  328. CARLA_ASSERT(parameterId < pData->param.count);
  329. CARLA_ASSERT(scalePointId < getParameterScalePointCount(parameterId));
  330. if (fDescriptor->get_parameter_info != nullptr && parameterId < pData->param.count)
  331. {
  332. if (const Parameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId))
  333. {
  334. const ParameterScalePoint& scalePoint(param->scalePoints[scalePointId]);
  335. return scalePoint.value;
  336. }
  337. }
  338. return 0.0f;
  339. }
  340. void getLabel(char* const strBuf) const override
  341. {
  342. CARLA_ASSERT(fDescriptor != nullptr);
  343. if (fDescriptor->label != nullptr)
  344. std::strncpy(strBuf, fDescriptor->label, STR_MAX);
  345. else
  346. CarlaPlugin::getLabel(strBuf);
  347. }
  348. void getMaker(char* const strBuf) const override
  349. {
  350. CARLA_ASSERT(fDescriptor != nullptr);
  351. if (fDescriptor->maker != nullptr)
  352. std::strncpy(strBuf, fDescriptor->maker, STR_MAX);
  353. else
  354. CarlaPlugin::getMaker(strBuf);
  355. }
  356. void getCopyright(char* const strBuf) const override
  357. {
  358. CARLA_ASSERT(fDescriptor != nullptr);
  359. if (fDescriptor->copyright != nullptr)
  360. std::strncpy(strBuf, fDescriptor->copyright, STR_MAX);
  361. else
  362. CarlaPlugin::getCopyright(strBuf);
  363. }
  364. void getRealName(char* const strBuf) const override
  365. {
  366. CARLA_ASSERT(fDescriptor != nullptr);
  367. if (fDescriptor->name != nullptr)
  368. std::strncpy(strBuf, fDescriptor->name, STR_MAX);
  369. else
  370. CarlaPlugin::getRealName(strBuf);
  371. }
  372. void getParameterName(const uint32_t parameterId, char* const strBuf) const override
  373. {
  374. CARLA_ASSERT(fDescriptor != nullptr);
  375. CARLA_ASSERT(fHandle != nullptr);
  376. CARLA_ASSERT(parameterId < pData->param.count);
  377. if (fDescriptor->get_parameter_info != nullptr && parameterId < pData->param.count)
  378. {
  379. const Parameter* const param(fDescriptor->get_parameter_info(fHandle, parameterId));
  380. if (param != nullptr && param->name != nullptr)
  381. {
  382. std::strncpy(strBuf, param->name, STR_MAX);
  383. return;
  384. }
  385. }
  386. CarlaPlugin::getParameterName(parameterId, strBuf);
  387. }
  388. void getParameterText(const uint32_t parameterId, char* const strBuf) const override
  389. {
  390. CARLA_ASSERT(fDescriptor != nullptr);
  391. CARLA_ASSERT(fHandle != nullptr);
  392. CARLA_ASSERT(parameterId < pData->param.count);
  393. if (fDescriptor->get_parameter_value != nullptr && fDescriptor->get_parameter_text != nullptr && parameterId < pData->param.count)
  394. {
  395. const float value(fDescriptor->get_parameter_value(fHandle, parameterId));
  396. if (const char* const text = fDescriptor->get_parameter_text(fHandle, parameterId, value))
  397. {
  398. std::strncpy(strBuf, text, STR_MAX);
  399. return;
  400. }
  401. }
  402. CarlaPlugin::getParameterText(parameterId, strBuf);
  403. }
  404. void getParameterUnit(const uint32_t parameterId, char* const strBuf) const override
  405. {
  406. CARLA_ASSERT(fDescriptor != nullptr);
  407. CARLA_ASSERT(fHandle != nullptr);
  408. CARLA_ASSERT(parameterId < pData->param.count);
  409. if (fDescriptor->get_parameter_info != nullptr && parameterId < pData->param.count)
  410. {
  411. const Parameter* const param(fDescriptor->get_parameter_info(fHandle, parameterId));
  412. if (param != nullptr && param->unit != nullptr)
  413. {
  414. std::strncpy(strBuf, param->unit, STR_MAX);
  415. return;
  416. }
  417. }
  418. CarlaPlugin::getParameterUnit(parameterId, strBuf);
  419. }
  420. void getParameterScalePointLabel(const uint32_t parameterId, const uint32_t scalePointId, char* const strBuf) const override
  421. {
  422. CARLA_ASSERT(fDescriptor != nullptr);
  423. CARLA_ASSERT(fHandle != nullptr);
  424. CARLA_ASSERT(parameterId < pData->param.count);
  425. CARLA_ASSERT(scalePointId < getParameterScalePointCount(parameterId));
  426. if (fDescriptor->get_parameter_info != nullptr && parameterId < pData->param.count)
  427. {
  428. if (const Parameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId))
  429. {
  430. const ParameterScalePoint& scalePoint(param->scalePoints[scalePointId]);
  431. if (scalePoint.label != nullptr)
  432. {
  433. std::strncpy(strBuf, scalePoint.label, STR_MAX);
  434. return;
  435. }
  436. }
  437. }
  438. CarlaPlugin::getParameterScalePointLabel(parameterId, scalePointId, strBuf);
  439. }
  440. // -------------------------------------------------------------------
  441. // Set data (state)
  442. void prepareForSave() override
  443. {
  444. CARLA_ASSERT(fDescriptor != nullptr);
  445. CARLA_ASSERT(fHandle != nullptr);
  446. if (pData->midiprog.count > 0 /*&& (fHints & PLUGIN_IS_SYNTH) != 0*/) // TODO
  447. {
  448. char strBuf[STR_MAX+1];
  449. std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i",
  450. fCurMidiProgs[0], fCurMidiProgs[1], fCurMidiProgs[2], fCurMidiProgs[3],
  451. fCurMidiProgs[4], fCurMidiProgs[5], fCurMidiProgs[6], fCurMidiProgs[7],
  452. fCurMidiProgs[8], fCurMidiProgs[9], fCurMidiProgs[10], fCurMidiProgs[11],
  453. fCurMidiProgs[12], fCurMidiProgs[13], fCurMidiProgs[14], fCurMidiProgs[15]);
  454. strBuf[STR_MAX] = '\0';
  455. CarlaPlugin::setCustomData(CUSTOM_DATA_STRING, "midiPrograms", strBuf, false);
  456. }
  457. if (fDescriptor == nullptr || fDescriptor->get_state == nullptr || (fDescriptor->hints & ::PLUGIN_USES_STATE) == 0)
  458. return;
  459. if (char* data = fDescriptor->get_state(fHandle))
  460. {
  461. CarlaPlugin::setCustomData(CUSTOM_DATA_CHUNK, "State", data, false);
  462. std::free(data);
  463. }
  464. }
  465. // -------------------------------------------------------------------
  466. // Set data (internal stuff)
  467. void setName(const char* const newName) override
  468. {
  469. CARLA_ASSERT(fDescriptor != nullptr);
  470. CARLA_ASSERT(fHandle != nullptr);
  471. char uiName[std::strlen(newName)+6+1];
  472. std::strcpy(uiName, newName);
  473. std::strcat(uiName, " (GUI)");
  474. if (fHost.uiName != nullptr)
  475. delete[] fHost.uiName;
  476. fHost.uiName = carla_strdup(uiName);
  477. if (fDescriptor != nullptr && fDescriptor->dispatcher != nullptr)
  478. fDescriptor->dispatcher(fHandle, PLUGIN_OPCODE_UI_NAME_CHANGED, 0, 0, uiName, 0.0f);
  479. CarlaPlugin::setName(newName);
  480. }
  481. void setCtrlChannel(const int8_t channel, const bool sendOsc, const bool sendCallback) override
  482. {
  483. if (channel < MAX_MIDI_CHANNELS && pData->midiprog.count > 0)
  484. pData->midiprog.current = fCurMidiProgs[channel];
  485. CarlaPlugin::setCtrlChannel(channel, sendOsc, sendCallback);
  486. }
  487. // -------------------------------------------------------------------
  488. // Set data (plugin-specific stuff)
  489. void setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) override
  490. {
  491. CARLA_ASSERT(fDescriptor != nullptr);
  492. CARLA_ASSERT(fHandle != nullptr);
  493. CARLA_ASSERT(parameterId < pData->param.count);
  494. const float fixedValue(pData->param.getFixedValue(parameterId, value));
  495. if (fDescriptor->set_parameter_value != nullptr && parameterId < pData->param.count)
  496. {
  497. fDescriptor->set_parameter_value(fHandle, parameterId, fixedValue);
  498. if (fHandle2 != nullptr)
  499. fDescriptor->set_parameter_value(fHandle2, parameterId, fixedValue);
  500. }
  501. CarlaPlugin::setParameterValue(parameterId, fixedValue, sendGui, sendOsc, sendCallback);
  502. }
  503. void setCustomData(const char* const type, const char* const key, const char* const value, const bool sendGui) override
  504. {
  505. CARLA_ASSERT(fDescriptor != nullptr);
  506. CARLA_ASSERT(fHandle != nullptr);
  507. CARLA_ASSERT(type != nullptr);
  508. CARLA_ASSERT(key != nullptr);
  509. CARLA_ASSERT(value != nullptr);
  510. carla_debug("NativePlugin::setCustomData(%s, %s, %s, %s)", type, key, value, bool2str(sendGui));
  511. if (type == nullptr)
  512. return carla_stderr2("NativePlugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - type is null", type, key, value, bool2str(sendGui));
  513. if (std::strcmp(type, CUSTOM_DATA_STRING) != 0 && std::strcmp(type, CUSTOM_DATA_CHUNK) != 0)
  514. return carla_stderr2("NativePlugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - type is invalid", type, key, value, bool2str(sendGui));
  515. if (key == nullptr)
  516. return carla_stderr2("NativePlugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - key is null", type, key, value, bool2str(sendGui));
  517. if (value == nullptr)
  518. return carla_stderr2("Nativelugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - value is null", type, key, value, bool2str(sendGui));
  519. if (std::strcmp(type, CUSTOM_DATA_CHUNK) == 0)
  520. {
  521. if (fDescriptor->set_state != nullptr && (fDescriptor->hints & ::PLUGIN_USES_STATE) != 0)
  522. {
  523. const ScopedSingleProcessLocker spl(this, true);
  524. fDescriptor->set_state(fHandle, value);
  525. if (fHandle2 != nullptr)
  526. fDescriptor->set_state(fHandle2, value);
  527. }
  528. }
  529. else if (std::strcmp(key, "midiPrograms") == 0 && fDescriptor->set_midi_program != nullptr)
  530. {
  531. #if 0 // TODO
  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 < pData->midiprog.count)
  541. {
  542. const uint32_t bank = pData->midiprog.data[index].bank;
  543. const uint32_t program = pData->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 (pData->ctrlChannel == static_cast<int32_t>(i))
  549. {
  550. pData->midiprog.current = index;
  551. pData->engine->callback(CALLBACK_MIDI_PROGRAM_CHANGED, fId, index, 0, 0.0f, nullptr);
  552. }
  553. }
  554. ++i;
  555. }
  556. }
  557. #endif
  558. }
  559. else
  560. {
  561. if (fDescriptor->set_custom_data != nullptr)
  562. {
  563. fDescriptor->set_custom_data(fHandle, key, value);
  564. if (fHandle2 != nullptr)
  565. fDescriptor->set_custom_data(fHandle2, key, value);
  566. }
  567. if (sendGui && fIsUiVisible && fDescriptor->ui_set_custom_data != nullptr)
  568. fDescriptor->ui_set_custom_data(fHandle, key, value);
  569. }
  570. CarlaPlugin::setCustomData(type, key, value, sendGui);
  571. }
  572. void setMidiProgram(int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) override
  573. {
  574. CARLA_ASSERT(fDescriptor != nullptr);
  575. CARLA_ASSERT(fHandle != nullptr);
  576. CARLA_ASSERT(index >= -1 && index < static_cast<int32_t>(pData->midiprog.count));
  577. if (index < -1)
  578. index = -1;
  579. else if (index > static_cast<int32_t>(pData->midiprog.count))
  580. return;
  581. // TODO
  582. //if ((fHints & PLUGIN_IS_SYNTH) != 0 && (pData->ctrlChannel < 0 || pData->ctrlChannel >= MAX_MIDI_CHANNELS))
  583. // return;
  584. if (index >= 0)
  585. {
  586. const uint8_t channel = (pData->ctrlChannel >= 0 || pData->ctrlChannel < MAX_MIDI_CHANNELS) ? pData->ctrlChannel : 0;
  587. const uint32_t bank = pData->midiprog.data[index].bank;
  588. const uint32_t program = pData->midiprog.data[index].program;
  589. const ScopedSingleProcessLocker spl(this, (sendGui || sendOsc || sendCallback));
  590. fDescriptor->set_midi_program(fHandle, channel, bank, program);
  591. if (fHandle2 != nullptr)
  592. fDescriptor->set_midi_program(fHandle2, channel, bank, program);
  593. fCurMidiProgs[channel] = index;
  594. }
  595. CarlaPlugin::setMidiProgram(index, sendGui, sendOsc, sendCallback);
  596. }
  597. // -------------------------------------------------------------------
  598. // Set gui stuff
  599. void showGui(const bool yesNo) override
  600. {
  601. CARLA_ASSERT(fDescriptor != nullptr);
  602. CARLA_ASSERT(fHandle != nullptr);
  603. if (fDescriptor->ui_show == nullptr)
  604. return;
  605. fDescriptor->ui_show(fHandle, yesNo);
  606. fIsUiVisible = yesNo;
  607. if (! yesNo)
  608. return;
  609. if (fDescriptor->ui_set_custom_data != nullptr)
  610. {
  611. for (NonRtList<CustomData>::Itenerator it = pData->custom.begin(); it.valid(); it.next())
  612. {
  613. const CustomData& cData(*it);
  614. if (std::strcmp(cData.type, CUSTOM_DATA_STRING) == 0 && std::strcmp(cData.key, "midiPrograms") != 0)
  615. fDescriptor->ui_set_custom_data(fHandle, cData.key, cData.value);
  616. }
  617. }
  618. if (fDescriptor->ui_set_midi_program != nullptr && pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  619. {
  620. const uint32_t index = pData->midiprog.current;
  621. const uint8_t channel = (pData->ctrlChannel >= 0 || pData->ctrlChannel < MAX_MIDI_CHANNELS) ? pData->ctrlChannel : 0;
  622. const uint32_t bank = pData->midiprog.data[index].bank;
  623. const uint32_t program = pData->midiprog.data[index].program;
  624. fDescriptor->ui_set_midi_program(fHandle, channel, bank, program);
  625. }
  626. if (fDescriptor->ui_set_parameter_value != nullptr)
  627. {
  628. for (uint32_t i=0; i < pData->param.count; ++i)
  629. fDescriptor->ui_set_parameter_value(fHandle, i, fDescriptor->get_parameter_value(fHandle, i));
  630. }
  631. }
  632. void idleGui() override
  633. {
  634. CARLA_ASSERT(fDescriptor != nullptr);
  635. CARLA_ASSERT(fHandle != nullptr);
  636. if (fIsUiVisible && fDescriptor->ui_idle != nullptr)
  637. fDescriptor->ui_idle(fHandle);
  638. }
  639. // -------------------------------------------------------------------
  640. // Plugin state
  641. void reload() override
  642. {
  643. carla_debug("NativePlugin::reload() - start");
  644. CARLA_ASSERT(pData->engine != nullptr);
  645. CARLA_ASSERT(fDescriptor != nullptr);
  646. CARLA_ASSERT(fHandle != nullptr);
  647. if (pData->engine == nullptr)
  648. return;
  649. if (fDescriptor == nullptr)
  650. return;
  651. if (fHandle == nullptr)
  652. return;
  653. const ProcessMode processMode(pData->engine->getProccessMode());
  654. // Safely disable plugin for reload
  655. const ScopedDisabler sd(this);
  656. if (pData->active)
  657. deactivate();
  658. clearBuffers();
  659. const float sampleRate((float)pData->engine->getSampleRate());
  660. uint32_t aIns, aOuts, mIns, mOuts, params, j;
  661. bool forcedStereoIn, forcedStereoOut;
  662. forcedStereoIn = forcedStereoOut = false;
  663. bool needsCtrlIn, needsCtrlOut;
  664. needsCtrlIn = needsCtrlOut = false;
  665. aIns = fDescriptor->audioIns;
  666. aOuts = fDescriptor->audioOuts;
  667. mIns = fDescriptor->midiIns;
  668. mOuts = fDescriptor->midiOuts;
  669. params = (fDescriptor->get_parameter_count != nullptr && fDescriptor->get_parameter_info != nullptr) ? fDescriptor->get_parameter_count(fHandle) : 0;
  670. if ((fOptions & PLUGIN_OPTION_FORCE_STEREO) != 0 && (aIns == 1 || aOuts == 1) && mIns <= 1 && mOuts <= 1)
  671. {
  672. if (fHandle2 == nullptr)
  673. fHandle2 = fDescriptor->instantiate(&fHost);
  674. if (fHandle2 != nullptr)
  675. {
  676. if (aIns == 1)
  677. {
  678. aIns = 2;
  679. forcedStereoIn = true;
  680. }
  681. if (aOuts == 1)
  682. {
  683. aOuts = 2;
  684. forcedStereoOut = true;
  685. }
  686. }
  687. }
  688. if (aIns > 0)
  689. {
  690. pData->audioIn.createNew(aIns);
  691. fAudioInBuffers = new float*[aIns];
  692. for (uint32_t i=0; i < aIns; ++i)
  693. fAudioInBuffers[i] = nullptr;
  694. }
  695. if (aOuts > 0)
  696. {
  697. pData->audioOut.createNew(aOuts);
  698. fAudioOutBuffers = new float*[aOuts];
  699. needsCtrlIn = true;
  700. for (uint32_t i=0; i < aOuts; ++i)
  701. fAudioOutBuffers[i] = nullptr;
  702. }
  703. if (mIns > 0)
  704. {
  705. fMidiIn.createNew(mIns);
  706. needsCtrlIn = (mIns == 1);
  707. }
  708. if (mOuts > 0)
  709. {
  710. fMidiOut.createNew(mOuts);
  711. needsCtrlOut = (mOuts == 1);
  712. }
  713. if (params > 0)
  714. {
  715. pData->param.createNew(params);
  716. }
  717. const uint portNameSize(pData->engine->getMaxPortNameSize());
  718. CarlaString portName;
  719. // Audio Ins
  720. for (j=0; j < aIns; ++j)
  721. {
  722. portName.clear();
  723. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  724. {
  725. portName = fName;
  726. portName += ":";
  727. }
  728. if (aIns > 1 && ! forcedStereoIn)
  729. {
  730. portName += "input_";
  731. portName += CarlaString(j+1);
  732. }
  733. else
  734. portName += "input";
  735. portName.truncate(portNameSize);
  736. pData->audioIn.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true);
  737. pData->audioIn.ports[j].rindex = j;
  738. if (forcedStereoIn)
  739. {
  740. portName += "_2";
  741. pData->audioIn.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true);
  742. pData->audioIn.ports[1].rindex = j;
  743. break;
  744. }
  745. }
  746. // Audio Outs
  747. for (j=0; j < aOuts; ++j)
  748. {
  749. portName.clear();
  750. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  751. {
  752. portName = fName;
  753. portName += ":";
  754. }
  755. if (aOuts > 1 && ! forcedStereoOut)
  756. {
  757. portName += "output_";
  758. portName += CarlaString(j+1);
  759. }
  760. else
  761. portName += "output";
  762. portName.truncate(portNameSize);
  763. pData->audioOut.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  764. pData->audioOut.ports[j].rindex = j;
  765. if (forcedStereoOut)
  766. {
  767. portName += "_2";
  768. pData->audioOut.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  769. pData->audioOut.ports[1].rindex = j;
  770. break;
  771. }
  772. }
  773. // MIDI Input (only if multiple)
  774. if (mIns > 1)
  775. {
  776. for (j=0; j < mIns; ++j)
  777. {
  778. portName.clear();
  779. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  780. {
  781. portName = fName;
  782. portName += ":";
  783. }
  784. portName += "midi-in_";
  785. portName += CarlaString(j+1);
  786. portName.truncate(portNameSize);
  787. fMidiIn.ports[j] = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true);
  788. fMidiIn.indexes[j] = j;
  789. }
  790. }
  791. // MIDI Output (only if multiple)
  792. if (mOuts > 1)
  793. {
  794. for (j=0; j < mOuts; ++j)
  795. {
  796. portName.clear();
  797. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  798. {
  799. portName = fName;
  800. portName += ":";
  801. }
  802. portName += "midi-out_";
  803. portName += CarlaString(j+1);
  804. portName.truncate(portNameSize);
  805. fMidiOut.ports[j] = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false);
  806. fMidiOut.indexes[j] = j;
  807. }
  808. }
  809. for (j=0; j < params; ++j)
  810. {
  811. const ::Parameter* const paramInfo(fDescriptor->get_parameter_info(fHandle, j));
  812. CARLA_ASSERT(paramInfo != nullptr);
  813. if (paramInfo == nullptr)
  814. continue;
  815. pData->param.data[j].index = j;
  816. pData->param.data[j].rindex = j;
  817. pData->param.data[j].hints = 0x0;
  818. pData->param.data[j].midiChannel = 0;
  819. pData->param.data[j].midiCC = -1;
  820. float min, max, def, step, stepSmall, stepLarge;
  821. // min value
  822. min = paramInfo->ranges.min;
  823. // max value
  824. max = paramInfo->ranges.max;
  825. if (min > max)
  826. max = min;
  827. else if (max < min)
  828. min = max;
  829. if (max - min == 0.0f)
  830. {
  831. carla_stderr2("WARNING - Broken plugin parameter '%s': max - min == 0.0f", paramInfo->name);
  832. max = min + 0.1f;
  833. }
  834. // default value
  835. def = paramInfo->ranges.def;
  836. if (def < min)
  837. def = min;
  838. else if (def > max)
  839. def = max;
  840. if (paramInfo->hints & ::PARAMETER_USES_SAMPLE_RATE)
  841. {
  842. min *= sampleRate;
  843. max *= sampleRate;
  844. def *= sampleRate;
  845. pData->param.data[j].hints |= PARAMETER_USES_SAMPLERATE;
  846. }
  847. if (paramInfo->hints & ::PARAMETER_IS_BOOLEAN)
  848. {
  849. step = max - min;
  850. stepSmall = step;
  851. stepLarge = step;
  852. pData->param.data[j].hints |= PARAMETER_IS_BOOLEAN;
  853. }
  854. else if (paramInfo->hints & ::PARAMETER_IS_INTEGER)
  855. {
  856. step = 1.0f;
  857. stepSmall = 1.0f;
  858. stepLarge = 10.0f;
  859. pData->param.data[j].hints |= PARAMETER_IS_INTEGER;
  860. }
  861. else
  862. {
  863. float range = max - min;
  864. step = range/100.0f;
  865. stepSmall = range/1000.0f;
  866. stepLarge = range/10.0f;
  867. }
  868. if (paramInfo->hints & ::PARAMETER_IS_OUTPUT)
  869. {
  870. pData->param.data[j].type = PARAMETER_OUTPUT;
  871. needsCtrlOut = true;
  872. }
  873. else
  874. {
  875. pData->param.data[j].type = PARAMETER_INPUT;
  876. needsCtrlIn = true;
  877. }
  878. // extra parameter hints
  879. if (paramInfo->hints & ::PARAMETER_IS_ENABLED)
  880. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  881. if (paramInfo->hints & ::PARAMETER_IS_AUTOMABLE)
  882. pData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  883. if (paramInfo->hints & ::PARAMETER_IS_LOGARITHMIC)
  884. pData->param.data[j].hints |= PARAMETER_IS_LOGARITHMIC;
  885. if (paramInfo->hints & ::PARAMETER_USES_SCALEPOINTS)
  886. pData->param.data[j].hints |= PARAMETER_USES_SCALEPOINTS;
  887. if (paramInfo->hints & ::PARAMETER_USES_CUSTOM_TEXT)
  888. pData->param.data[j].hints |= PARAMETER_USES_CUSTOM_TEXT;
  889. pData->param.ranges[j].min = min;
  890. pData->param.ranges[j].max = max;
  891. pData->param.ranges[j].def = def;
  892. pData->param.ranges[j].step = step;
  893. pData->param.ranges[j].stepSmall = stepSmall;
  894. pData->param.ranges[j].stepLarge = stepLarge;
  895. }
  896. if (needsCtrlIn)
  897. {
  898. portName.clear();
  899. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  900. {
  901. portName = fName;
  902. portName += ":";
  903. }
  904. portName += "events-in";
  905. portName.truncate(portNameSize);
  906. pData->event.portIn = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true);
  907. }
  908. if (needsCtrlOut)
  909. {
  910. portName.clear();
  911. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  912. {
  913. portName = fName;
  914. portName += ":";
  915. }
  916. portName += "events-out";
  917. portName.truncate(portNameSize);
  918. pData->event.portOut = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false);
  919. }
  920. if (forcedStereoIn || forcedStereoOut)
  921. fOptions |= PLUGIN_OPTION_FORCE_STEREO;
  922. else
  923. fOptions &= ~PLUGIN_OPTION_FORCE_STEREO;
  924. // plugin hints
  925. fHints = 0x0;
  926. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  927. fHints |= PLUGIN_CAN_DRYWET;
  928. if (aOuts > 0)
  929. fHints |= PLUGIN_CAN_VOLUME;
  930. if (aOuts >= 2 && aOuts % 2 == 0)
  931. fHints |= PLUGIN_CAN_BALANCE;
  932. // native plugin hints
  933. if (fDescriptor->hints & ::PLUGIN_IS_RTSAFE)
  934. fHints |= PLUGIN_IS_RTSAFE;
  935. if (fDescriptor->hints & ::PLUGIN_HAS_GUI)
  936. fHints |= PLUGIN_HAS_GUI;
  937. if (fDescriptor->hints & ::PLUGIN_NEEDS_SINGLE_THREAD)
  938. fHints |= PLUGIN_NEEDS_SINGLE_THREAD;
  939. if (fDescriptor->hints & ::PLUGIN_NEEDS_FIXED_BUFFERS)
  940. fHints |= PLUGIN_NEEDS_FIXED_BUFFERS;
  941. // extra plugin hints
  942. pData->extraHints = 0x0;
  943. if (aIns <= 2 && aOuts <= 2 && (aIns == aOuts || aIns == 0 || aOuts == 0) && mIns <= 1 && mOuts <= 1)
  944. pData->extraHints |= PLUGIN_HINT_CAN_RUN_RACK;
  945. bufferSizeChanged(pData->engine->getBufferSize());
  946. reloadPrograms(true);
  947. if (pData->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 = pData->midiprog.count;
  955. const int32_t current = pData->midiprog.current;
  956. // Delete old programs
  957. pData->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. pData->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. pData->midiprog.data[i].bank = mpDesc->bank;
  972. pData->midiprog.data[i].program = mpDesc->program;
  973. pData->midiprog.data[i].name = carla_strdup(mpDesc->name);
  974. }
  975. }
  976. #ifndef BUILD_BRIDGE
  977. // Update OSC Names
  978. if (pData->engine->isOscControlRegistered())
  979. {
  980. pData->engine->oscSend_control_set_midi_program_count(fId, count);
  981. for (i=0; i < count; ++i)
  982. 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);
  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. pData->midiprog.current = oldCount;
  998. programChanged = true;
  999. }
  1000. else if (current < 0 && count > 0)
  1001. {
  1002. // programs exist now, but not before
  1003. pData->midiprog.current = 0;
  1004. programChanged = true;
  1005. }
  1006. else if (current >= 0 && count == 0)
  1007. {
  1008. // programs existed before, but not anymore
  1009. pData->midiprog.current = -1;
  1010. programChanged = true;
  1011. }
  1012. else if (current >= static_cast<int32_t>(count))
  1013. {
  1014. // current midi program > count
  1015. pData->midiprog.current = 0;
  1016. programChanged = true;
  1017. }
  1018. else
  1019. {
  1020. // no change
  1021. pData->midiprog.current = current;
  1022. }
  1023. if (programChanged)
  1024. setMidiProgram(pData->midiprog.current, true, true, true);
  1025. pData->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 (! pData->active)
  1058. {
  1059. // disable any output sound
  1060. for (i=0; i < pData->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 (pData->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 (pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  1086. {
  1087. for (k=0; k < MAX_MIDI_NOTE; ++k)
  1088. {
  1089. fMidiEvents[k].data[0] = MIDI_STATUS_NOTE_OFF + pData->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. pData->needsReset = false;
  1097. }
  1098. CARLA_PROCESS_CONTINUE_CHECK;
  1099. // --------------------------------------------------------------------------------------------------------
  1100. // Set TimeInfo
  1101. const EngineTimeInfo& timeInfo(pData->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 (pData->event.portIn != nullptr)
  1123. {
  1124. // ----------------------------------------------------------------------------------------------------
  1125. // MIDI Input (External)
  1126. if (pData->extNotes.mutex.tryLock())
  1127. {
  1128. while (fMidiEventCount < MAX_MIDI_EVENTS*2 && ! pData->extNotes.data.isEmpty())
  1129. {
  1130. const ExternalMidiNote& note(pData->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. pData->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_BUFFERS) == 0;
  1145. uint32_t time, nEvents = pData->event.portIn->getEventCount();
  1146. uint32_t startTime = 0;
  1147. uint32_t timeOffset = 0;
  1148. uint32_t nextBankId = 0;
  1149. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  1150. nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
  1151. for (i=0; i < nEvents; ++i)
  1152. {
  1153. const EngineEvent& event(pData->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 (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  1165. nextBankId = pData->midiprog.data[pData->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 == pData->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. pData->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. pData->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. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  1230. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  1231. }
  1232. }
  1233. #endif
  1234. // Control plugin parameters
  1235. for (k=0; k < pData->param.count; ++k)
  1236. {
  1237. if (pData->param.data[k].midiChannel != event.channel)
  1238. continue;
  1239. if (pData->param.data[k].midiCC != ctrlEvent.param)
  1240. continue;
  1241. if (pData->param.data[k].type != PARAMETER_INPUT)
  1242. continue;
  1243. if ((pData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  1244. continue;
  1245. float value;
  1246. if (pData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  1247. {
  1248. value = (ctrlEvent.value < 0.5f) ? pData->param.ranges[k].min : pData->param.ranges[k].max;
  1249. }
  1250. else
  1251. {
  1252. value = pData->param.ranges[k].getUnnormalizedValue(ctrlEvent.value);
  1253. if (pData->param.data[k].hints & PARAMETER_IS_INTEGER)
  1254. value = std::rint(value);
  1255. }
  1256. setParameterValue(k, value, false, false, false);
  1257. pData->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 == pData->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 < pData->midiprog.count; ++k)
  1282. {
  1283. if (pData->midiprog.data[k].bank == nextBankId && pData->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 == pData->ctrlChannel)
  1290. pData->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 == pData->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_CHANNEL_PRESSURE(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. pData->postponeRtEvent(kPluginPostRtEventNoteOn, channel, midiEvent.data[1], midiEvent.data[2]);
  1360. else if (status == MIDI_STATUS_NOTE_OFF)
  1361. pData->postponeRtEvent(kPluginPostRtEventNoteOff, channel, midiEvent.data[1], 0.0f);
  1362. break;
  1363. }
  1364. }
  1365. }
  1366. pData->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 || pData->event.portOut != nullptr)
  1380. {
  1381. float value, curValue;
  1382. for (k=0; k < pData->param.count; ++k)
  1383. {
  1384. if (pData->param.data[k].type != PARAMETER_OUTPUT)
  1385. continue;
  1386. curValue = fDescriptor->get_parameter_value(fHandle, k);
  1387. pData->param.ranges[k].fixValue(curValue);
  1388. if (pData->param.data[k].midiCC > 0)
  1389. {
  1390. value = pData->param.ranges[k].getNormalizedValue(curValue);
  1391. pData->event.portOut->writeControlEvent(0, pData->param.data[k].midiChannel, kEngineControlEventTypeParameter, pData->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 (pData->event.portOut != nullptr)
  1402. pData->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 (pData->audioIn.count > 0)
  1414. {
  1415. CARLA_ASSERT(inBuffer != nullptr);
  1416. if (inBuffer == nullptr)
  1417. return false;
  1418. }
  1419. if (pData->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 (pData->engine->isOffline())
  1429. {
  1430. pData->singleMutex.lock();
  1431. }
  1432. else if (! pData->singleMutex.tryLock())
  1433. {
  1434. for (i=0; i < pData->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 < pData->audioIn.count; ++i)
  1444. carla_copyFloat(fAudioInBuffers[i], inBuffer[i]+timeOffset, frames);
  1445. for (i=0; i < pData->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, fMidiEvents, fMidiEventCount);
  1453. }
  1454. else
  1455. {
  1456. fDescriptor->process(fHandle,
  1457. (pData->audioIn.count > 0) ? &fAudioInBuffers[0] : nullptr,
  1458. (pData->audioOut.count > 0) ? &fAudioOutBuffers[0] : nullptr,
  1459. frames, fMidiEvents, fMidiEventCount);
  1460. fDescriptor->process(fHandle2,
  1461. (pData->audioIn.count > 0) ? &fAudioInBuffers[1] : nullptr,
  1462. (pData->audioOut.count > 0) ? &fAudioOutBuffers[1] : nullptr,
  1463. frames, fMidiEvents, fMidiEventCount);
  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 && pData->postProc.dryWet != 1.0f;
  1472. const bool doBalance = (fHints & PLUGIN_CAN_BALANCE) != 0 && (pData->postProc.balanceLeft != -1.0f || pData->postProc.balanceRight != 1.0f);
  1473. bool isPair;
  1474. float bufValue, oldBufLeft[doBalance ? frames : 1];
  1475. for (i=0; i < pData->audioOut.count; ++i)
  1476. {
  1477. // Dry/Wet
  1478. if (doDryWet)
  1479. {
  1480. for (k=0; k < frames; ++k)
  1481. {
  1482. bufValue = fAudioInBuffers[(pData->audioIn.count == 1) ? 0 : i][k];
  1483. fAudioOutBuffers[i][k] = (fAudioOutBuffers[i][k] * pData->postProc.dryWet) + (bufValue * (1.0f - pData->postProc.dryWet));
  1484. }
  1485. }
  1486. // Balance
  1487. if (doBalance)
  1488. {
  1489. isPair = (i % 2 == 0);
  1490. if (isPair)
  1491. {
  1492. CARLA_ASSERT(i+1 < pData->audioOut.count);
  1493. carla_copyFloat(oldBufLeft, fAudioOutBuffers[i], frames);
  1494. }
  1495. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  1496. float balRangeR = (pData->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] * pData->postProc.volume;
  1517. }
  1518. }
  1519. } // End of Post-processing
  1520. #else
  1521. for (i=0; i < pData->audioOut.count; ++i)
  1522. {
  1523. for (k=0; k < frames; ++k)
  1524. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k];
  1525. }
  1526. #endif
  1527. // --------------------------------------------------------------------------------------------------------
  1528. pData->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 < pData->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 < pData->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();
  1579. fMidiOut.initBuffers();
  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 < pData->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 < pData->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 < pData->param.count);
  1623. if (! fIsUiVisible)
  1624. return;
  1625. if (fDescriptor == nullptr || fHandle == nullptr)
  1626. return;
  1627. if (index >= pData->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 < pData->midiprog.count);
  1637. if (! fIsUiVisible)
  1638. return;
  1639. if (fDescriptor == nullptr || fHandle == nullptr)
  1640. return;
  1641. if (index >= pData->midiprog.count)
  1642. return;
  1643. if (fDescriptor->ui_set_midi_program != nullptr) // TODO
  1644. fDescriptor->ui_set_midi_program(fHandle, 0, pData->midiprog.data[index].bank, pData->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 pData->engine->getBufferSize();
  1686. }
  1687. double handleGetSampleRate()
  1688. {
  1689. return pData->engine->getSampleRate();
  1690. }
  1691. bool handleIsOffline()
  1692. {
  1693. return pData->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 || pData->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. // reverse-find first free event, and put it there
  1721. for (uint32_t i=(MAX_MIDI_EVENTS*2)-1; i > fMidiEventCount; --i)
  1722. {
  1723. if (fMidiEvents[i].data[0] == 0)
  1724. {
  1725. std::memcpy(&fMidiEvents[i], event, sizeof(::MidiEvent));
  1726. return true;
  1727. }
  1728. }
  1729. return false;
  1730. }
  1731. void handleUiParameterChanged(const uint32_t index, const float value)
  1732. {
  1733. setParameterValue(index, value, false, true, true);
  1734. }
  1735. void handleUiCustomDataChanged(const char* const key, const char* const value)
  1736. {
  1737. setCustomData(CUSTOM_DATA_STRING, key, value, false);
  1738. }
  1739. void handleUiClosed()
  1740. {
  1741. pData->engine->callback(CALLBACK_SHOW_GUI, fId, 0, 0, 0.0f, nullptr);
  1742. fIsUiVisible = false;
  1743. }
  1744. const char* handleUiOpenFile(const bool isDir, const char* const title, const char* const filter)
  1745. {
  1746. static CarlaString retStr;
  1747. #if 0
  1748. QFileDialog::Options options(isDir ? QFileDialog::ShowDirsOnly : 0x0);
  1749. retStr = QFileDialog::getOpenFileName(nullptr, title, "", filter, nullptr, options).toUtf8().constData();
  1750. #endif
  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. #if 0
  1757. QFileDialog::Options options(isDir ? QFileDialog::ShowDirsOnly : 0x0);
  1758. retStr = QFileDialog::getSaveFileName(nullptr, title, "", filter, nullptr, options).toUtf8().constData();
  1759. #endif
  1760. return retStr.isNotEmpty() ? (const char*)retStr : nullptr;
  1761. }
  1762. intptr_t handleDispatcher(const ::HostDispatcherOpcode opcode, const int32_t index, const intptr_t value, void* const ptr, const float opt)
  1763. {
  1764. carla_debug("NativePlugin::handleDispatcher(%i, %i, " P_INTPTR ", %p, %f)", opcode, index, value, ptr, opt);
  1765. intptr_t ret = 0;
  1766. switch (opcode)
  1767. {
  1768. case ::HOST_OPCODE_NULL:
  1769. break;
  1770. #ifdef BUILD_BRIDGE
  1771. case ::HOST_OPCODE_SET_VOLUME:
  1772. case ::HOST_OPCODE_SET_DRYWET:
  1773. case ::HOST_OPCODE_SET_BALANCE_LEFT:
  1774. case ::HOST_OPCODE_SET_BALANCE_RIGHT:
  1775. case ::HOST_OPCODE_SET_PANNING:
  1776. break;
  1777. #else
  1778. case ::HOST_OPCODE_SET_VOLUME:
  1779. setVolume(opt, true, true);
  1780. break;
  1781. case ::HOST_OPCODE_SET_DRYWET:
  1782. setDryWet(opt, true, true);
  1783. break;
  1784. case ::HOST_OPCODE_SET_BALANCE_LEFT:
  1785. setBalanceLeft(opt, true, true);
  1786. break;
  1787. case ::HOST_OPCODE_SET_BALANCE_RIGHT:
  1788. setBalanceRight(opt, true, true);
  1789. break;
  1790. case ::HOST_OPCODE_SET_PANNING:
  1791. setPanning(opt, true, true);
  1792. break;
  1793. #endif
  1794. case HOST_OPCODE_GET_PARAMETER_MIDI_CC:
  1795. case HOST_OPCODE_SET_PARAMETER_MIDI_CC:
  1796. // TODO
  1797. break;
  1798. case ::HOST_OPCODE_SET_PROCESS_PRECISION:
  1799. // TODO
  1800. break;
  1801. case ::HOST_OPCODE_UPDATE_PARAMETER:
  1802. break;
  1803. case ::HOST_OPCODE_UPDATE_MIDI_PROGRAM:
  1804. break;
  1805. case ::HOST_OPCODE_RELOAD_PARAMETERS:
  1806. break;
  1807. case ::HOST_OPCODE_RELOAD_MIDI_PROGRAMS:
  1808. break;
  1809. case ::HOST_OPCODE_RELOAD_ALL:
  1810. break;
  1811. case HOST_OPCODE_UI_UNAVAILABLE:
  1812. pData->engine->callback(CALLBACK_SHOW_GUI, fId, -1, 0, 0.0f, nullptr);
  1813. break;
  1814. }
  1815. return ret;
  1816. // unused for now
  1817. (void)index;
  1818. (void)value;
  1819. (void)ptr;
  1820. }
  1821. // -------------------------------------------------------------------
  1822. public:
  1823. static size_t getPluginCount()
  1824. {
  1825. return sPluginDescriptors.count();
  1826. }
  1827. static const ::PluginDescriptor* getPluginDescriptor(const size_t index)
  1828. {
  1829. CARLA_ASSERT(index < sPluginDescriptors.count());
  1830. if (index < sPluginDescriptors.count())
  1831. return sPluginDescriptors.getAt(index);
  1832. return nullptr;
  1833. }
  1834. static void registerPlugin(const ::PluginDescriptor* desc)
  1835. {
  1836. sPluginDescriptors.append(desc);
  1837. }
  1838. // -------------------------------------------------------------------
  1839. bool init(const char* const name, const char* const label)
  1840. {
  1841. CARLA_ASSERT(pData->engine != nullptr);
  1842. CARLA_ASSERT(pData->client == nullptr);
  1843. CARLA_ASSERT(label != nullptr);
  1844. // ---------------------------------------------------------------
  1845. // first checks
  1846. if (pData->engine == nullptr)
  1847. {
  1848. return false;
  1849. }
  1850. if (pData->client != nullptr)
  1851. {
  1852. pData->engine->setLastError("Plugin client is already registered");
  1853. return false;
  1854. }
  1855. if (label == nullptr)
  1856. {
  1857. pData->engine->setLastError("null label");
  1858. return false;
  1859. }
  1860. // ---------------------------------------------------------------
  1861. // get descriptor that matches label
  1862. for (NonRtList<const ::PluginDescriptor*>::Itenerator it = sPluginDescriptors.begin(); it.valid(); it.next())
  1863. {
  1864. fDescriptor = *it;
  1865. CARLA_ASSERT(fDescriptor != nullptr);
  1866. if (fDescriptor == nullptr)
  1867. break;
  1868. if (fDescriptor->label != nullptr && std::strcmp(fDescriptor->label, label) == 0)
  1869. break;
  1870. fDescriptor = nullptr;
  1871. }
  1872. if (fDescriptor == nullptr)
  1873. {
  1874. pData->engine->setLastError("Invalid internal plugin");
  1875. return false;
  1876. }
  1877. // ---------------------------------------------------------------
  1878. // set icon
  1879. if (std::strcmp(fDescriptor->label, "audiofile") == 0)
  1880. fIconName = "file";
  1881. else if (std::strcmp(fDescriptor->label, "midifile") == 0)
  1882. fIconName = "file";
  1883. else if (std::strcmp(fDescriptor->label, "sunvoxfile") == 0)
  1884. fIconName = "file";
  1885. else if (std::strcmp(fDescriptor->label, "3BandEQ") == 0)
  1886. fIconName = "distrho";
  1887. else if (std::strcmp(fDescriptor->label, "3BandSplitter") == 0)
  1888. fIconName = "distrho";
  1889. else if (std::strcmp(fDescriptor->label, "Nekobi") == 0)
  1890. fIconName = "distrho";
  1891. else if (std::strcmp(fDescriptor->label, "Notes") == 0)
  1892. fIconName = "distrho";
  1893. else if (std::strcmp(fDescriptor->label, "PingPongPan") == 0)
  1894. fIconName = "distrho";
  1895. else if (std::strcmp(fDescriptor->label, "StereoEnhancer") == 0)
  1896. fIconName = "distrho";
  1897. // ---------------------------------------------------------------
  1898. // get info
  1899. if (name != nullptr)
  1900. fName = pData->engine->getUniquePluginName(name);
  1901. else if (fDescriptor->name != nullptr)
  1902. fName = pData->engine->getUniquePluginName(fDescriptor->name);
  1903. else
  1904. fName = pData->engine->getUniquePluginName(label);
  1905. {
  1906. CARLA_ASSERT(fHost.uiName == nullptr);
  1907. char uiName[fName.length()+6+1];
  1908. std::strcpy(uiName, (const char*)fName);
  1909. std::strcat(uiName, " (GUI)");
  1910. fHost.uiName = carla_strdup(uiName);
  1911. }
  1912. // ---------------------------------------------------------------
  1913. // register client
  1914. pData->client = pData->engine->addClient(this);
  1915. if (pData->client == nullptr || ! pData->client->isOk())
  1916. {
  1917. pData->engine->setLastError("Failed to register plugin client");
  1918. return false;
  1919. }
  1920. // ---------------------------------------------------------------
  1921. // initialize plugin
  1922. fHandle = fDescriptor->instantiate(&fHost);
  1923. if (fHandle == nullptr)
  1924. {
  1925. pData->engine->setLastError("Plugin failed to initialize");
  1926. return false;
  1927. }
  1928. // ---------------------------------------------------------------
  1929. // load plugin settings
  1930. {
  1931. const bool hasMidiProgs(fDescriptor->get_midi_program_count != nullptr && fDescriptor->get_midi_program_count(fHandle) > 0);
  1932. // set default options
  1933. fOptions = 0x0;
  1934. if (hasMidiProgs && (fDescriptor->supports & ::PLUGIN_SUPPORTS_PROGRAM_CHANGES) == 0)
  1935. fOptions |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  1936. if (getMidiInCount() > 0 || (fDescriptor->hints & ::PLUGIN_NEEDS_FIXED_BUFFERS) != 0)
  1937. fOptions |= PLUGIN_OPTION_FIXED_BUFFERS;
  1938. if (pData->engine->getOptions().forceStereo)
  1939. fOptions |= PLUGIN_OPTION_FORCE_STEREO;
  1940. if (fDescriptor->supports & ::PLUGIN_SUPPORTS_CHANNEL_PRESSURE)
  1941. fOptions |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  1942. if (fDescriptor->supports & ::PLUGIN_SUPPORTS_NOTE_AFTERTOUCH)
  1943. fOptions |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  1944. if (fDescriptor->supports & ::PLUGIN_SUPPORTS_PITCHBEND)
  1945. fOptions |= PLUGIN_OPTION_SEND_PITCHBEND;
  1946. if (fDescriptor->supports & ::PLUGIN_SUPPORTS_ALL_SOUND_OFF)
  1947. fOptions |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  1948. // load settings
  1949. pData->idStr = "Native/";
  1950. pData->idStr += label;
  1951. fOptions = pData->loadSettings(fOptions, getAvailableOptions());
  1952. // ignore settings, we need this anyway
  1953. if (getMidiInCount() > 0 || (fDescriptor->hints & ::PLUGIN_NEEDS_FIXED_BUFFERS) != 0)
  1954. fOptions |= PLUGIN_OPTION_FIXED_BUFFERS;
  1955. }
  1956. return true;
  1957. }
  1958. class ScopedInitializer
  1959. {
  1960. public:
  1961. ScopedInitializer()
  1962. {
  1963. carla_register_all_plugins();
  1964. }
  1965. ~ScopedInitializer()
  1966. {
  1967. sPluginDescriptors.clear();
  1968. }
  1969. };
  1970. private:
  1971. ::PluginHandle fHandle;
  1972. ::PluginHandle fHandle2;
  1973. ::HostDescriptor fHost;
  1974. const ::PluginDescriptor* fDescriptor;
  1975. bool fIsProcessing;
  1976. bool fIsUiVisible;
  1977. float** fAudioInBuffers;
  1978. float** fAudioOutBuffers;
  1979. uint32_t fMidiEventCount;
  1980. ::MidiEvent fMidiEvents[MAX_MIDI_EVENTS*2];
  1981. int32_t fCurMidiProgs[MAX_MIDI_CHANNELS];
  1982. NativePluginMidiData fMidiIn;
  1983. NativePluginMidiData fMidiOut;
  1984. ::TimeInfo fTimeInfo;
  1985. static NonRtList<const ::PluginDescriptor*> sPluginDescriptors;
  1986. // -------------------------------------------------------------------
  1987. #define handlePtr ((NativePlugin*)handle)
  1988. static uint32_t carla_host_get_buffer_size(::HostHandle handle)
  1989. {
  1990. return handlePtr->handleGetBufferSize();
  1991. }
  1992. static double carla_host_get_sample_rate(::HostHandle handle)
  1993. {
  1994. return handlePtr->handleGetSampleRate();
  1995. }
  1996. static bool carla_host_is_offline(::HostHandle handle)
  1997. {
  1998. return handlePtr->handleIsOffline();
  1999. }
  2000. static const ::TimeInfo* carla_host_get_time_info(::HostHandle handle)
  2001. {
  2002. return handlePtr->handleGetTimeInfo();
  2003. }
  2004. static bool carla_host_write_midi_event(::HostHandle handle, const ::MidiEvent* event)
  2005. {
  2006. return handlePtr->handleWriteMidiEvent(event);
  2007. }
  2008. static void carla_host_ui_parameter_changed(::HostHandle handle, uint32_t index, float value)
  2009. {
  2010. handlePtr->handleUiParameterChanged(index, value);
  2011. }
  2012. static void carla_host_ui_custom_data_changed(::HostHandle handle, const char* key, const char* value)
  2013. {
  2014. handlePtr->handleUiCustomDataChanged(key, value);
  2015. }
  2016. static void carla_host_ui_closed(::HostHandle handle)
  2017. {
  2018. handlePtr->handleUiClosed();
  2019. }
  2020. static const char* carla_host_ui_open_file(::HostHandle handle, bool isDir, const char* title, const char* filter)
  2021. {
  2022. return handlePtr->handleUiOpenFile(isDir, title, filter);
  2023. }
  2024. static const char* carla_host_ui_save_file(::HostHandle handle, bool isDir, const char* title, const char* filter)
  2025. {
  2026. return handlePtr->handleUiSaveFile(isDir, title, filter);
  2027. }
  2028. static intptr_t carla_host_dispatcher(::HostHandle handle, ::HostDispatcherOpcode opcode, int32_t index, intptr_t value, void* ptr, float opt)
  2029. {
  2030. return handlePtr->handleDispatcher(opcode, index, value, ptr, opt);
  2031. }
  2032. #undef handlePtr
  2033. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(NativePlugin)
  2034. };
  2035. NonRtList<const ::PluginDescriptor*> NativePlugin::sPluginDescriptors;
  2036. static const NativePlugin::ScopedInitializer _si;
  2037. CARLA_BACKEND_END_NAMESPACE
  2038. void carla_register_native_plugin(const PluginDescriptor* desc)
  2039. {
  2040. CARLA_BACKEND_USE_NAMESPACE
  2041. NativePlugin::registerPlugin(desc);
  2042. }
  2043. #else // WANT_NATIVE
  2044. # warning Building without Internal plugin support
  2045. #endif
  2046. CARLA_BACKEND_START_NAMESPACE
  2047. // -----------------------------------------------------------------------
  2048. #ifdef WANT_NATIVE
  2049. size_t CarlaPlugin::getNativePluginCount()
  2050. {
  2051. return NativePlugin::getPluginCount();
  2052. }
  2053. const PluginDescriptor* CarlaPlugin::getNativePluginDescriptor(const size_t index)
  2054. {
  2055. return NativePlugin::getPluginDescriptor(index);
  2056. }
  2057. #endif
  2058. // -----------------------------------------------------------------------
  2059. CarlaPlugin* CarlaPlugin::newNative(const Initializer& init)
  2060. {
  2061. carla_debug("CarlaPlugin::newNative({%p, \"%s\", \"%s\", \"%s\"})", init.engine, init.filename, init.name, init.label);
  2062. #ifdef WANT_NATIVE
  2063. NativePlugin* const plugin(new NativePlugin(init.engine, init.id));
  2064. if (! plugin->init(init.name, init.label))
  2065. {
  2066. delete plugin;
  2067. return nullptr;
  2068. }
  2069. plugin->reload();
  2070. if (init.engine->getProccessMode() == PROCESS_MODE_CONTINUOUS_RACK && ! plugin->canRunInRack())
  2071. {
  2072. init.engine->setLastError("Carla's rack mode can only work with Mono or Stereo Internal plugins, sorry!");
  2073. delete plugin;
  2074. return nullptr;
  2075. }
  2076. return plugin;
  2077. #else
  2078. init.engine->setLastError("Internal plugins support not available");
  2079. return nullptr;
  2080. #endif
  2081. }
  2082. CARLA_BACKEND_END_NAMESPACE