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 GPL.txt file
  16. */
  17. #include "CarlaPluginInternal.hpp"
  18. #ifdef WANT_NATIVE
  19. #include "CarlaNative.h"
  20. #include <QtCore/Qt>
  21. #if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
  22. # include <QtWidgets/QFileDialog>
  23. #else
  24. # include <QtGui/QFileDialog>
  25. #endif
  26. 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. #ifndef BUILD_BRIDGE
  37. // Carla
  38. void carla_register_native_plugin_carla();
  39. #endif
  40. #ifdef WANT_AUDIOFILE
  41. // AudioFile
  42. void carla_register_native_plugin_audiofile();
  43. #endif
  44. #ifdef WANT_MIDIFILE
  45. // MidiFile
  46. void carla_register_native_plugin_midifile();
  47. #endif
  48. #ifdef WANT_OPENGL
  49. // DISTRHO plugins (OpenGL)
  50. void carla_register_native_plugin_3BandEQ();
  51. void carla_register_native_plugin_3BandSplitter();
  52. void carla_register_native_plugin_Nekobi();
  53. void carla_register_native_plugin_PingPongPan();
  54. void carla_register_native_plugin_StereoEnhancer();
  55. #endif
  56. // DISTRHO plugins (Qt)
  57. void carla_register_native_plugin_Notes();
  58. #ifdef WANT_ZYNADDSUBFX
  59. // ZynAddSubFX
  60. void carla_register_native_plugin_zynaddsubfx();
  61. #endif
  62. }
  63. void carla_register_all_plugins()
  64. {
  65. // Simple plugins
  66. carla_register_native_plugin_bypass();
  67. carla_register_native_plugin_lfo();
  68. //carla_register_native_plugin_midiSequencer(); // unfinished
  69. carla_register_native_plugin_midiSplit();
  70. carla_register_native_plugin_midiThrough();
  71. carla_register_native_plugin_midiTranspose();
  72. carla_register_native_plugin_nekofilter();
  73. //carla_register_native_plugin_sunvoxfile(); // unfinished
  74. #ifndef BUILD_BRIDGE
  75. // Carla
  76. //carla_register_native_plugin_carla(); // kinda unfinished
  77. #endif
  78. #ifdef WANT_AUDIOFILE
  79. // AudioFile
  80. carla_register_native_plugin_audiofile();
  81. #endif
  82. #ifdef WANT_MIDIFILE
  83. // MidiFile
  84. carla_register_native_plugin_midifile();
  85. #endif
  86. #ifdef WANT_OPENGL
  87. // DISTRHO plugins (OpenGL)
  88. carla_register_native_plugin_3BandEQ();
  89. carla_register_native_plugin_3BandSplitter();
  90. carla_register_native_plugin_Nekobi();
  91. carla_register_native_plugin_PingPongPan();
  92. carla_register_native_plugin_StereoEnhancer(); // unfinished
  93. #endif
  94. // DISTRHO plugins (Qt)
  95. carla_register_native_plugin_Notes(); // unfinished
  96. #ifdef WANT_ZYNADDSUBFX
  97. // ZynAddSubFX
  98. carla_register_native_plugin_zynaddsubfx();
  99. #endif
  100. }
  101. CARLA_BACKEND_START_NAMESPACE
  102. #if 0
  103. }
  104. #endif
  105. struct NativePluginMidiData {
  106. uint32_t count;
  107. uint32_t* indexes;
  108. CarlaEngineEventPort** ports;
  109. NativePluginMidiData()
  110. : count(0),
  111. indexes(nullptr),
  112. ports(nullptr) {}
  113. ~NativePluginMidiData()
  114. {
  115. CARLA_ASSERT_INT(count == 0, count);
  116. CARLA_ASSERT(ports == nullptr);
  117. CARLA_ASSERT(indexes == nullptr);
  118. }
  119. void createNew(const uint32_t newCount)
  120. {
  121. CARLA_ASSERT_INT(count == 0, count);
  122. CARLA_ASSERT(ports == nullptr);
  123. CARLA_ASSERT(indexes == nullptr);
  124. CARLA_ASSERT_INT(newCount > 0, newCount);
  125. if (ports != nullptr || indexes != nullptr || newCount == 0)
  126. return;
  127. ports = new CarlaEngineEventPort*[newCount];
  128. indexes = new uint32_t[newCount];
  129. count = newCount;
  130. for (uint32_t i=0; i < newCount; ++i)
  131. ports[i] = nullptr;
  132. for (uint32_t i=0; i < newCount; ++i)
  133. indexes[i] = 0;
  134. }
  135. void clear()
  136. {
  137. if (ports != nullptr)
  138. {
  139. for (uint32_t i=0; i < count; ++i)
  140. {
  141. if (ports[i] != nullptr)
  142. {
  143. delete ports[i];
  144. ports[i] = nullptr;
  145. }
  146. }
  147. delete[] ports;
  148. ports = nullptr;
  149. }
  150. if (indexes != nullptr)
  151. {
  152. delete[] indexes;
  153. indexes = nullptr;
  154. }
  155. count = 0;
  156. }
  157. void initBuffers(CarlaEngine* const engine)
  158. {
  159. for (uint32_t i=0; i < count; ++i)
  160. {
  161. if (ports[i] != nullptr)
  162. ports[i]->initBuffer(engine);
  163. }
  164. }
  165. CARLA_DECLARE_NON_COPY_STRUCT_WITH_LEAK_DETECTOR(NativePluginMidiData)
  166. };
  167. class NativePlugin : public CarlaPlugin
  168. {
  169. public:
  170. NativePlugin(CarlaEngine* const engine, const unsigned int id)
  171. : CarlaPlugin(engine, id),
  172. fHandle(nullptr),
  173. fHandle2(nullptr),
  174. fDescriptor(nullptr),
  175. fIsProcessing(false),
  176. fIsUiVisible(false),
  177. fAudioInBuffers(nullptr),
  178. fAudioOutBuffers(nullptr),
  179. #ifdef CARLA_PROPER_CPP11_SUPPORT
  180. fMidiEventCount(0),
  181. fCurMidiProgs{0}
  182. #else
  183. fMidiEventCount(0)
  184. #endif
  185. {
  186. carla_debug("NativePlugin::NativePlugin(%p, %i)", engine, id);
  187. #ifndef CARLA_PROPER_CPP11_SUPPORT
  188. carla_fill<int32_t>(fCurMidiProgs, MAX_MIDI_CHANNELS, 0);
  189. #endif
  190. carla_zeroStruct< ::MidiEvent>(fMidiEvents, MAX_MIDI_EVENTS*2);
  191. carla_zeroStruct< ::TimeInfo>(fTimeInfo);
  192. fHost.handle = this;
  193. fHost.resourceDir = carla_strdup((const char*)engine->getOptions().resourceDir);
  194. fHost.uiName = nullptr;
  195. fHost.get_buffer_size = carla_host_get_buffer_size;
  196. fHost.get_sample_rate = carla_host_get_sample_rate;
  197. fHost.is_offline = carla_host_is_offline;
  198. fHost.get_time_info = carla_host_get_time_info;
  199. fHost.write_midi_event = carla_host_write_midi_event;
  200. fHost.ui_parameter_changed = carla_host_ui_parameter_changed;
  201. fHost.ui_custom_data_changed = carla_host_ui_custom_data_changed;
  202. fHost.ui_closed = carla_host_ui_closed;
  203. fHost.ui_open_file = carla_host_ui_open_file;
  204. fHost.ui_save_file = carla_host_ui_save_file;
  205. fHost.dispatcher = carla_host_dispatcher;
  206. }
  207. ~NativePlugin() override
  208. {
  209. carla_debug("NativePlugin::~NativePlugin()");
  210. // close UI
  211. if (fHints & PLUGIN_HAS_GUI)
  212. {
  213. if (fIsUiVisible && fDescriptor != nullptr && fDescriptor->ui_show != nullptr && fHandle != nullptr)
  214. fDescriptor->ui_show(fHandle, false);
  215. }
  216. pData->singleMutex.lock();
  217. pData->masterMutex.lock();
  218. if (pData->client != nullptr && pData->client->isActive())
  219. pData->client->deactivate();
  220. CARLA_ASSERT(! fIsProcessing);
  221. if (pData->active)
  222. {
  223. deactivate();
  224. pData->active = false;
  225. }
  226. if (fDescriptor != nullptr)
  227. {
  228. if (fDescriptor->cleanup != nullptr)
  229. {
  230. if (fHandle != nullptr)
  231. fDescriptor->cleanup(fHandle);
  232. if (fHandle2 != nullptr)
  233. fDescriptor->cleanup(fHandle2);
  234. }
  235. fHandle = nullptr;
  236. fHandle2 = nullptr;
  237. fDescriptor = nullptr;
  238. }
  239. if (fHost.resource_dir != nullptr)
  240. {
  241. delete[] fHost.resource_dir;
  242. fHost.resource_dir = nullptr;
  243. }
  244. if (fHost.ui_name != nullptr)
  245. {
  246. delete[] fHost.ui_name;
  247. fHost.ui_name = nullptr;
  248. }
  249. clearBuffers();
  250. }
  251. // -------------------------------------------------------------------
  252. // Information (base)
  253. PluginType type() const override
  254. {
  255. return PLUGIN_INTERNAL;
  256. }
  257. PluginCategory category() override
  258. {
  259. CARLA_ASSERT(fDescriptor != nullptr);
  260. return static_cast<PluginCategory>(fDescriptor->category);
  261. }
  262. // -------------------------------------------------------------------
  263. // Information (count)
  264. uint32_t midiInCount() const override
  265. {
  266. return fMidiIn.count;
  267. }
  268. uint32_t midiOutCount() const override
  269. {
  270. return fMidiOut.count;
  271. }
  272. uint32_t parameterScalePointCount(const uint32_t parameterId) const override
  273. {
  274. CARLA_ASSERT(fDescriptor != nullptr);
  275. CARLA_ASSERT(fHandle != nullptr);
  276. CARLA_ASSERT(parameterId < pData->param.count);
  277. if (fDescriptor->get_parameter_info != nullptr && parameterId < pData->param.count)
  278. {
  279. if (const Parameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId))
  280. return param->scalePointCount;
  281. }
  282. return 0;
  283. }
  284. // -------------------------------------------------------------------
  285. // Information (current data)
  286. // nothing
  287. // -------------------------------------------------------------------
  288. // Information (per-plugin data)
  289. unsigned int availableOptions() override
  290. {
  291. CARLA_ASSERT(fDescriptor != nullptr);
  292. if (fDescriptor == nullptr)
  293. return 0x0;
  294. const bool hasMidiProgs(fDescriptor->get_midi_program_count != nullptr && fDescriptor->get_midi_program_count(fHandle) > 0);
  295. unsigned int options = 0x0;
  296. if (hasMidiProgs && (fDescriptor->supports & ::PLUGIN_SUPPORTS_PROGRAM_CHANGES) == 0)
  297. options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  298. if (midiInCount() == 0 && (fDescriptor->hints & ::PLUGIN_USES_STATIC_BUFFERS) == 0)
  299. options |= PLUGIN_OPTION_FIXED_BUFFER;
  300. if (pData->engine->getProccessMode() != PROCESS_MODE_CONTINUOUS_RACK)
  301. {
  302. if (fOptions & PLUGIN_OPTION_FORCE_STEREO)
  303. options |= PLUGIN_OPTION_FORCE_STEREO;
  304. else if (pData->audioIn.count <= 1 && pData->audioOut.count <= 1 && (pData->audioIn.count != 0 || pData->audioOut.count != 0))
  305. options |= PLUGIN_OPTION_FORCE_STEREO;
  306. }
  307. if (fDescriptor->supports & ::PLUGIN_SUPPORTS_CONTROL_CHANGES)
  308. options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  309. if (fDescriptor->supports & ::PLUGIN_SUPPORTS_CHANNEL_PRESSURE)
  310. options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  311. if (fDescriptor->supports & ::PLUGIN_SUPPORTS_NOTE_AFTERTOUCH)
  312. options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  313. if (fDescriptor->supports & ::PLUGIN_SUPPORTS_PITCHBEND)
  314. options |= PLUGIN_OPTION_SEND_PITCHBEND;
  315. if (fDescriptor->supports & ::PLUGIN_SUPPORTS_ALL_SOUND_OFF)
  316. options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  317. return options;
  318. }
  319. float getParameterValue(const uint32_t parameterId) override
  320. {
  321. CARLA_ASSERT(fDescriptor != nullptr);
  322. CARLA_ASSERT(fHandle != nullptr);
  323. CARLA_ASSERT(parameterId < pData->param.count);
  324. if (fDescriptor->get_parameter_value != nullptr && parameterId < pData->param.count)
  325. return fDescriptor->get_parameter_value(fHandle, parameterId);
  326. return 0.0f;
  327. }
  328. float getParameterScalePointValue(const uint32_t parameterId, const uint32_t scalePointId) override
  329. {
  330. CARLA_ASSERT(fDescriptor != nullptr);
  331. CARLA_ASSERT(fHandle != nullptr);
  332. CARLA_ASSERT(parameterId < pData->param.count);
  333. CARLA_ASSERT(scalePointId < parameterScalePointCount(parameterId));
  334. if (fDescriptor->get_parameter_info != nullptr && parameterId < pData->param.count)
  335. {
  336. if (const Parameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId))
  337. {
  338. const ParameterScalePoint& scalePoint(param->scalePoints[scalePointId]);
  339. return scalePoint.value;
  340. }
  341. }
  342. return 0.0f;
  343. }
  344. void getLabel(char* const strBuf) override
  345. {
  346. CARLA_ASSERT(fDescriptor != nullptr);
  347. if (fDescriptor->label != nullptr)
  348. std::strncpy(strBuf, fDescriptor->label, STR_MAX);
  349. else
  350. CarlaPlugin::getLabel(strBuf);
  351. }
  352. void getMaker(char* const strBuf) override
  353. {
  354. CARLA_ASSERT(fDescriptor != nullptr);
  355. if (fDescriptor->maker != nullptr)
  356. std::strncpy(strBuf, fDescriptor->maker, STR_MAX);
  357. else
  358. CarlaPlugin::getMaker(strBuf);
  359. }
  360. void getCopyright(char* const strBuf) override
  361. {
  362. CARLA_ASSERT(fDescriptor != nullptr);
  363. if (fDescriptor->copyright != nullptr)
  364. std::strncpy(strBuf, fDescriptor->copyright, STR_MAX);
  365. else
  366. CarlaPlugin::getCopyright(strBuf);
  367. }
  368. void getRealName(char* const strBuf) override
  369. {
  370. CARLA_ASSERT(fDescriptor != nullptr);
  371. if (fDescriptor->name != nullptr)
  372. std::strncpy(strBuf, fDescriptor->name, STR_MAX);
  373. else
  374. CarlaPlugin::getRealName(strBuf);
  375. }
  376. void getParameterName(const uint32_t parameterId, char* const strBuf) override
  377. {
  378. CARLA_ASSERT(fDescriptor != nullptr);
  379. CARLA_ASSERT(fHandle != nullptr);
  380. CARLA_ASSERT(parameterId < pData->param.count);
  381. if (fDescriptor->get_parameter_info != nullptr && parameterId < pData->param.count)
  382. {
  383. const Parameter* const param(fDescriptor->get_parameter_info(fHandle, parameterId));
  384. if (param != nullptr && param->name != nullptr)
  385. {
  386. std::strncpy(strBuf, param->name, STR_MAX);
  387. return;
  388. }
  389. }
  390. CarlaPlugin::getParameterName(parameterId, strBuf);
  391. }
  392. void getParameterText(const uint32_t parameterId, char* const strBuf) override
  393. {
  394. CARLA_ASSERT(fDescriptor != nullptr);
  395. CARLA_ASSERT(fHandle != nullptr);
  396. CARLA_ASSERT(parameterId < pData->param.count);
  397. if (fDescriptor->get_parameter_value != nullptr && fDescriptor->get_parameter_text != nullptr && parameterId < pData->param.count)
  398. {
  399. const float value(fDescriptor->get_parameter_value(fHandle, parameterId));
  400. if (const char* const text = fDescriptor->get_parameter_text(fHandle, parameterId, value))
  401. {
  402. std::strncpy(strBuf, text, STR_MAX);
  403. return;
  404. }
  405. }
  406. CarlaPlugin::getParameterText(parameterId, strBuf);
  407. }
  408. void getParameterUnit(const uint32_t parameterId, char* const strBuf) override
  409. {
  410. CARLA_ASSERT(fDescriptor != nullptr);
  411. CARLA_ASSERT(fHandle != nullptr);
  412. CARLA_ASSERT(parameterId < pData->param.count);
  413. if (fDescriptor->get_parameter_info != nullptr && parameterId < pData->param.count)
  414. {
  415. const Parameter* const param(fDescriptor->get_parameter_info(fHandle, parameterId));
  416. if (param != nullptr && param->unit != nullptr)
  417. {
  418. std::strncpy(strBuf, param->unit, STR_MAX);
  419. return;
  420. }
  421. }
  422. CarlaPlugin::getParameterUnit(parameterId, strBuf);
  423. }
  424. void getParameterScalePointLabel(const uint32_t parameterId, const uint32_t scalePointId, char* const strBuf) override
  425. {
  426. CARLA_ASSERT(fDescriptor != nullptr);
  427. CARLA_ASSERT(fHandle != nullptr);
  428. CARLA_ASSERT(parameterId < pData->param.count);
  429. CARLA_ASSERT(scalePointId < parameterScalePointCount(parameterId));
  430. if (fDescriptor->get_parameter_info != nullptr && parameterId < pData->param.count)
  431. {
  432. if (const Parameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId))
  433. {
  434. const ParameterScalePoint& scalePoint(param->scalePoints[scalePointId]);
  435. if (scalePoint.label != nullptr)
  436. {
  437. std::strncpy(strBuf, scalePoint.label, STR_MAX);
  438. return;
  439. }
  440. }
  441. }
  442. CarlaPlugin::getParameterScalePointLabel(parameterId, scalePointId, strBuf);
  443. }
  444. // -------------------------------------------------------------------
  445. // Set data (state)
  446. void prepareForSave() override
  447. {
  448. CARLA_ASSERT(fDescriptor != nullptr);
  449. CARLA_ASSERT(fHandle != nullptr);
  450. if (pData->midiprog.count > 0 && (fHints & PLUGIN_IS_SYNTH) != 0)
  451. {
  452. char strBuf[STR_MAX+1];
  453. std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i",
  454. fCurMidiProgs[0], fCurMidiProgs[1], fCurMidiProgs[2], fCurMidiProgs[3],
  455. fCurMidiProgs[4], fCurMidiProgs[5], fCurMidiProgs[6], fCurMidiProgs[7],
  456. fCurMidiProgs[8], fCurMidiProgs[9], fCurMidiProgs[10], fCurMidiProgs[11],
  457. fCurMidiProgs[12], fCurMidiProgs[13], fCurMidiProgs[14], fCurMidiProgs[15]);
  458. strBuf[STR_MAX] = '\0';
  459. CarlaPlugin::setCustomData(CUSTOM_DATA_STRING, "midiPrograms", strBuf, false);
  460. }
  461. if (fDescriptor == nullptr || fDescriptor->get_state == nullptr || (fDescriptor->hints & ::PLUGIN_USES_STATE) == 0)
  462. return;
  463. if (char* data = fDescriptor->get_state(fHandle))
  464. {
  465. CarlaPlugin::setCustomData(CUSTOM_DATA_CHUNK, "State", data, false);
  466. std::free(data);
  467. }
  468. }
  469. // -------------------------------------------------------------------
  470. // Set data (internal stuff)
  471. void setName(const char* const newName) override
  472. {
  473. CARLA_ASSERT(fDescriptor != nullptr);
  474. CARLA_ASSERT(fHandle != nullptr);
  475. char uiName[std::strlen(newName)+6+1];
  476. std::strcpy(uiName, newName);
  477. std::strcat(uiName, " (GUI)");
  478. if (fHost.ui_name != nullptr)
  479. delete[] fHost.ui_name;
  480. fHost.ui_name = carla_strdup(uiName);
  481. if (fDescriptor != nullptr && fDescriptor->dispatcher != nullptr)
  482. fDescriptor->dispatcher(fHandle, PLUGIN_OPCODE_UI_NAME_CHANGED, 0, 0, uiName, 0.0f);
  483. CarlaPlugin::setName(newName);
  484. }
  485. void setCtrlChannel(const int8_t channel, const bool sendOsc, const bool sendCallback) override
  486. {
  487. if (channel < MAX_MIDI_CHANNELS && pData->midiprog.count > 0)
  488. pData->midiprog.current = fCurMidiProgs[channel];
  489. CarlaPlugin::setCtrlChannel(channel, sendOsc, sendCallback);
  490. }
  491. // -------------------------------------------------------------------
  492. // Set data (plugin-specific stuff)
  493. void setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) override
  494. {
  495. CARLA_ASSERT(fDescriptor != nullptr);
  496. CARLA_ASSERT(fHandle != nullptr);
  497. CARLA_ASSERT(parameterId < pData->param.count);
  498. const float fixedValue(pData->param.fixValue(parameterId, value));
  499. if (fDescriptor->set_parameter_value != nullptr && parameterId < pData->param.count)
  500. {
  501. fDescriptor->set_parameter_value(fHandle, parameterId, fixedValue);
  502. if (fHandle2 != nullptr)
  503. fDescriptor->set_parameter_value(fHandle2, parameterId, fixedValue);
  504. }
  505. CarlaPlugin::setParameterValue(parameterId, fixedValue, sendGui, sendOsc, sendCallback);
  506. }
  507. void setCustomData(const char* const type, const char* const key, const char* const value, const bool sendGui) override
  508. {
  509. CARLA_ASSERT(fDescriptor != nullptr);
  510. CARLA_ASSERT(fHandle != nullptr);
  511. CARLA_ASSERT(type != nullptr);
  512. CARLA_ASSERT(key != nullptr);
  513. CARLA_ASSERT(value != nullptr);
  514. carla_debug("NativePlugin::setCustomData(%s, %s, %s, %s)", type, key, value, bool2str(sendGui));
  515. if (type == nullptr)
  516. return carla_stderr2("NativePlugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - type is null", type, key, value, bool2str(sendGui));
  517. if (std::strcmp(type, CUSTOM_DATA_STRING) != 0 && std::strcmp(type, CUSTOM_DATA_CHUNK) != 0)
  518. return carla_stderr2("NativePlugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - type is invalid", type, key, value, bool2str(sendGui));
  519. if (key == nullptr)
  520. return carla_stderr2("NativePlugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - key is null", type, key, value, bool2str(sendGui));
  521. if (value == nullptr)
  522. return carla_stderr2("Nativelugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - value is null", type, key, value, bool2str(sendGui));
  523. if (std::strcmp(type, CUSTOM_DATA_CHUNK) == 0)
  524. {
  525. if (fDescriptor->set_state != nullptr && (fDescriptor->hints & ::PLUGIN_USES_STATE) != 0)
  526. {
  527. const ScopedSingleProcessLocker spl(this, true);
  528. fDescriptor->set_state(fHandle, value);
  529. if (fHandle2 != nullptr)
  530. fDescriptor->set_state(fHandle2, value);
  531. }
  532. }
  533. else if (std::strcmp(key, "midiPrograms") == 0 && fDescriptor->set_midi_program != nullptr)
  534. {
  535. QStringList midiProgramList(QString(value).split(":", QString::SkipEmptyParts));
  536. if (midiProgramList.count() == MAX_MIDI_CHANNELS)
  537. {
  538. uint i = 0;
  539. foreach (const QString& midiProg, midiProgramList)
  540. {
  541. bool ok;
  542. const uint index(midiProg.toUInt(&ok));
  543. if (ok && index < pData->midiprog.count)
  544. {
  545. const uint32_t bank = pData->midiprog.data[index].bank;
  546. const uint32_t program = pData->midiprog.data[index].program;
  547. fDescriptor->set_midi_program(fHandle, i, bank, program);
  548. if (fHandle2 != nullptr)
  549. fDescriptor->set_midi_program(fHandle2, i, bank, program);
  550. fCurMidiProgs[i] = index;
  551. if (pData->ctrlChannel == static_cast<int32_t>(i))
  552. {
  553. pData->midiprog.current = index;
  554. pData->engine->callback(CALLBACK_MIDI_PROGRAM_CHANGED, fId, index, 0, 0.0f, nullptr);
  555. }
  556. }
  557. ++i;
  558. }
  559. }
  560. }
  561. else
  562. {
  563. if (fDescriptor->set_custom_data != nullptr)
  564. {
  565. fDescriptor->set_custom_data(fHandle, key, value);
  566. if (fHandle2 != nullptr)
  567. fDescriptor->set_custom_data(fHandle2, key, value);
  568. }
  569. if (sendGui && fIsUiVisible && fDescriptor->ui_set_custom_data != nullptr)
  570. fDescriptor->ui_set_custom_data(fHandle, key, value);
  571. }
  572. CarlaPlugin::setCustomData(type, key, value, sendGui);
  573. }
  574. void setMidiProgram(int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) override
  575. {
  576. CARLA_ASSERT(fDescriptor != nullptr);
  577. CARLA_ASSERT(fHandle != nullptr);
  578. CARLA_ASSERT(index >= -1 && index < static_cast<int32_t>(pData->midiprog.count));
  579. if (index < -1)
  580. index = -1;
  581. else if (index > static_cast<int32_t>(pData->midiprog.count))
  582. return;
  583. if ((fHints & PLUGIN_IS_SYNTH) != 0 && (pData->ctrlChannel < 0 || pData->ctrlChannel >= MAX_MIDI_CHANNELS))
  584. return;
  585. if (index >= 0)
  586. {
  587. const uint8_t channel = (pData->ctrlChannel >= 0 || pData->ctrlChannel < MAX_MIDI_CHANNELS) ? pData->ctrlChannel : 0;
  588. const uint32_t bank = pData->midiprog.data[index].bank;
  589. const uint32_t program = pData->midiprog.data[index].program;
  590. const ScopedSingleProcessLocker spl(this, (sendGui || sendOsc || sendCallback));
  591. fDescriptor->set_midi_program(fHandle, channel, bank, program);
  592. if (fHandle2 != nullptr)
  593. fDescriptor->set_midi_program(fHandle2, channel, bank, program);
  594. fCurMidiProgs[channel] = index;
  595. }
  596. CarlaPlugin::setMidiProgram(index, sendGui, sendOsc, sendCallback);
  597. }
  598. // -------------------------------------------------------------------
  599. // Set gui stuff
  600. void showGui(const bool yesNo) override
  601. {
  602. CARLA_ASSERT(fDescriptor != nullptr);
  603. CARLA_ASSERT(fHandle != nullptr);
  604. if (fDescriptor->ui_show == nullptr)
  605. return;
  606. fDescriptor->ui_show(fHandle, yesNo);
  607. fIsUiVisible = yesNo;
  608. if (! yesNo)
  609. return;
  610. if (fDescriptor->ui_set_custom_data != nullptr)
  611. {
  612. for (NonRtList<CustomData>::Itenerator it = pData->custom.begin(); it.valid(); it.next())
  613. {
  614. const CustomData& cData(*it);
  615. if (std::strcmp(cData.type, CUSTOM_DATA_STRING) == 0 && std::strcmp(cData.key, "midiPrograms") != 0)
  616. fDescriptor->ui_set_custom_data(fHandle, cData.key, cData.value);
  617. }
  618. }
  619. if (fDescriptor->ui_set_midi_program != nullptr && pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  620. {
  621. const uint32_t index = pData->midiprog.current;
  622. const uint8_t channel = (pData->ctrlChannel >= 0 || pData->ctrlChannel < MAX_MIDI_CHANNELS) ? pData->ctrlChannel : 0;
  623. const uint32_t bank = pData->midiprog.data[index].bank;
  624. const uint32_t program = pData->midiprog.data[index].program;
  625. fDescriptor->ui_set_midi_program(fHandle, channel, bank, program);
  626. }
  627. if (fDescriptor->ui_set_parameter_value != nullptr)
  628. {
  629. for (uint32_t i=0; i < pData->param.count; ++i)
  630. fDescriptor->ui_set_parameter_value(fHandle, i, fDescriptor->get_parameter_value(fHandle, i));
  631. }
  632. }
  633. void idleGui() override
  634. {
  635. CARLA_ASSERT(fDescriptor != nullptr);
  636. CARLA_ASSERT(fHandle != nullptr);
  637. if (fIsUiVisible && fDescriptor->ui_idle != nullptr)
  638. fDescriptor->ui_idle(fHandle);
  639. }
  640. // -------------------------------------------------------------------
  641. // Plugin state
  642. void reload() override
  643. {
  644. carla_debug("NativePlugin::reload() - start");
  645. CARLA_ASSERT(pData->engine != nullptr);
  646. CARLA_ASSERT(fDescriptor != nullptr);
  647. CARLA_ASSERT(fHandle != nullptr);
  648. if (pData->engine == nullptr)
  649. return;
  650. if (fDescriptor == nullptr)
  651. return;
  652. if (fHandle == nullptr)
  653. return;
  654. const ProcessMode processMode(pData->engine->getProccessMode());
  655. // Safely disable plugin for reload
  656. const ScopedDisabler sd(this);
  657. if (pData->active)
  658. deactivate();
  659. clearBuffers();
  660. const float sampleRate((float)pData->engine->getSampleRate());
  661. uint32_t aIns, aOuts, mIns, mOuts, params, j;
  662. bool forcedStereoIn, forcedStereoOut;
  663. forcedStereoIn = forcedStereoOut = false;
  664. bool needsCtrlIn, needsCtrlOut;
  665. needsCtrlIn = needsCtrlOut = false;
  666. aIns = fDescriptor->audioIns;
  667. aOuts = fDescriptor->audioOuts;
  668. mIns = fDescriptor->midiIns;
  669. mOuts = fDescriptor->midiOuts;
  670. params = (fDescriptor->get_parameter_count != nullptr && fDescriptor->get_parameter_info != nullptr) ? fDescriptor->get_parameter_count(fHandle) : 0;
  671. if ((fOptions & PLUGIN_OPTION_FORCE_STEREO) != 0 && (aIns == 1 || aOuts == 1) && mIns <= 1 && mOuts <= 1)
  672. {
  673. if (fHandle2 == nullptr)
  674. fHandle2 = fDescriptor->instantiate(fDescriptor, &fHost);
  675. if (fHandle2 != nullptr)
  676. {
  677. if (aIns == 1)
  678. {
  679. aIns = 2;
  680. forcedStereoIn = true;
  681. }
  682. if (aOuts == 1)
  683. {
  684. aOuts = 2;
  685. forcedStereoOut = true;
  686. }
  687. }
  688. }
  689. if (aIns > 0)
  690. {
  691. pData->audioIn.createNew(aIns);
  692. fAudioInBuffers = new float*[aIns];
  693. for (uint32_t i=0; i < aIns; ++i)
  694. fAudioInBuffers[i] = nullptr;
  695. }
  696. if (aOuts > 0)
  697. {
  698. pData->audioOut.createNew(aOuts);
  699. fAudioOutBuffers = new float*[aOuts];
  700. needsCtrlIn = true;
  701. for (uint32_t i=0; i < aOuts; ++i)
  702. fAudioOutBuffers[i] = nullptr;
  703. }
  704. if (mIns > 0)
  705. {
  706. fMidiIn.createNew(mIns);
  707. needsCtrlIn = (mIns == 1);
  708. }
  709. if (mOuts > 0)
  710. {
  711. fMidiOut.createNew(mOuts);
  712. needsCtrlOut = (mOuts == 1);
  713. }
  714. if (params > 0)
  715. {
  716. pData->param.createNew(params);
  717. }
  718. const uint portNameSize(pData->engine->maxPortNameSize());
  719. CarlaString portName;
  720. // Audio Ins
  721. for (j=0; j < aIns; ++j)
  722. {
  723. portName.clear();
  724. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  725. {
  726. portName = fName;
  727. portName += ":";
  728. }
  729. if (aIns > 1 && ! forcedStereoIn)
  730. {
  731. portName += "input_";
  732. portName += CarlaString(j+1);
  733. }
  734. else
  735. portName += "input";
  736. portName.truncate(portNameSize);
  737. pData->audioIn.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true);
  738. pData->audioIn.ports[j].rindex = j;
  739. if (forcedStereoIn)
  740. {
  741. portName += "_2";
  742. pData->audioIn.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true);
  743. pData->audioIn.ports[1].rindex = j;
  744. break;
  745. }
  746. }
  747. // Audio Outs
  748. for (j=0; j < aOuts; ++j)
  749. {
  750. portName.clear();
  751. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  752. {
  753. portName = fName;
  754. portName += ":";
  755. }
  756. if (aOuts > 1 && ! forcedStereoOut)
  757. {
  758. portName += "output_";
  759. portName += CarlaString(j+1);
  760. }
  761. else
  762. portName += "output";
  763. portName.truncate(portNameSize);
  764. pData->audioOut.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  765. pData->audioOut.ports[j].rindex = j;
  766. if (forcedStereoOut)
  767. {
  768. portName += "_2";
  769. pData->audioOut.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  770. pData->audioOut.ports[1].rindex = j;
  771. break;
  772. }
  773. }
  774. // MIDI Input (only if multiple)
  775. if (mIns > 1)
  776. {
  777. for (j=0; j < mIns; ++j)
  778. {
  779. portName.clear();
  780. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  781. {
  782. portName = fName;
  783. portName += ":";
  784. }
  785. portName += "midi-in_";
  786. portName += CarlaString(j+1);
  787. portName.truncate(portNameSize);
  788. fMidiIn.ports[j] = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true);
  789. fMidiIn.indexes[j] = j;
  790. }
  791. }
  792. // MIDI Output (only if multiple)
  793. if (mOuts > 1)
  794. {
  795. for (j=0; j < mOuts; ++j)
  796. {
  797. portName.clear();
  798. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  799. {
  800. portName = fName;
  801. portName += ":";
  802. }
  803. portName += "midi-out_";
  804. portName += CarlaString(j+1);
  805. portName.truncate(portNameSize);
  806. fMidiOut.ports[j] = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false);
  807. fMidiOut.indexes[j] = j;
  808. }
  809. }
  810. for (j=0; j < params; ++j)
  811. {
  812. const ::Parameter* const paramInfo(fDescriptor->get_parameter_info(fHandle, j));
  813. CARLA_ASSERT(paramInfo != nullptr);
  814. if (paramInfo == nullptr)
  815. continue;
  816. pData->param.data[j].index = j;
  817. pData->param.data[j].rindex = j;
  818. pData->param.data[j].hints = 0x0;
  819. pData->param.data[j].midiChannel = 0;
  820. pData->param.data[j].midiCC = -1;
  821. float min, max, def, step, stepSmall, stepLarge;
  822. // min value
  823. min = paramInfo->ranges.min;
  824. // max value
  825. max = paramInfo->ranges.max;
  826. if (min > max)
  827. max = min;
  828. else if (max < min)
  829. min = max;
  830. if (max - min == 0.0f)
  831. {
  832. carla_stderr2("WARNING - Broken plugin parameter '%s': max - min == 0.0f", paramInfo->name);
  833. max = min + 0.1f;
  834. }
  835. // default value
  836. def = paramInfo->ranges.def;
  837. if (def < min)
  838. def = min;
  839. else if (def > max)
  840. def = max;
  841. if (paramInfo->hints & ::PARAMETER_USES_SAMPLE_RATE)
  842. {
  843. min *= sampleRate;
  844. max *= sampleRate;
  845. def *= sampleRate;
  846. pData->param.data[j].hints |= PARAMETER_USES_SAMPLERATE;
  847. }
  848. if (paramInfo->hints & ::PARAMETER_IS_BOOLEAN)
  849. {
  850. step = max - min;
  851. stepSmall = step;
  852. stepLarge = step;
  853. pData->param.data[j].hints |= PARAMETER_IS_BOOLEAN;
  854. }
  855. else if (paramInfo->hints & ::PARAMETER_IS_INTEGER)
  856. {
  857. step = 1.0f;
  858. stepSmall = 1.0f;
  859. stepLarge = 10.0f;
  860. pData->param.data[j].hints |= PARAMETER_IS_INTEGER;
  861. }
  862. else
  863. {
  864. float range = max - min;
  865. step = range/100.0f;
  866. stepSmall = range/1000.0f;
  867. stepLarge = range/10.0f;
  868. }
  869. if (paramInfo->hints & ::PARAMETER_IS_OUTPUT)
  870. {
  871. pData->param.data[j].type = PARAMETER_OUTPUT;
  872. needsCtrlOut = true;
  873. }
  874. else
  875. {
  876. pData->param.data[j].type = PARAMETER_INPUT;
  877. needsCtrlIn = true;
  878. }
  879. // extra parameter hints
  880. if (paramInfo->hints & ::PARAMETER_IS_ENABLED)
  881. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  882. if (paramInfo->hints & ::PARAMETER_IS_AUTOMABLE)
  883. pData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  884. if (paramInfo->hints & ::PARAMETER_IS_LOGARITHMIC)
  885. pData->param.data[j].hints |= PARAMETER_IS_LOGARITHMIC;
  886. if (paramInfo->hints & ::PARAMETER_USES_SCALEPOINTS)
  887. pData->param.data[j].hints |= PARAMETER_USES_SCALEPOINTS;
  888. if (paramInfo->hints & ::PARAMETER_USES_CUSTOM_TEXT)
  889. pData->param.data[j].hints |= PARAMETER_USES_CUSTOM_TEXT;
  890. pData->param.ranges[j].min = min;
  891. pData->param.ranges[j].max = max;
  892. pData->param.ranges[j].def = def;
  893. pData->param.ranges[j].step = step;
  894. pData->param.ranges[j].stepSmall = stepSmall;
  895. pData->param.ranges[j].stepLarge = stepLarge;
  896. }
  897. if (needsCtrlIn)
  898. {
  899. portName.clear();
  900. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  901. {
  902. portName = fName;
  903. portName += ":";
  904. }
  905. portName += "events-in";
  906. portName.truncate(portNameSize);
  907. pData->event.portIn = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true);
  908. }
  909. if (needsCtrlOut)
  910. {
  911. portName.clear();
  912. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  913. {
  914. portName = fName;
  915. portName += ":";
  916. }
  917. portName += "events-out";
  918. portName.truncate(portNameSize);
  919. pData->event.portOut = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false);
  920. }
  921. if (forcedStereoIn || forcedStereoOut)
  922. fOptions |= PLUGIN_OPTION_FORCE_STEREO;
  923. else
  924. fOptions &= ~PLUGIN_OPTION_FORCE_STEREO;
  925. // plugin hints
  926. fHints = 0x0;
  927. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  928. fHints |= PLUGIN_CAN_DRYWET;
  929. if (aOuts > 0)
  930. fHints |= PLUGIN_CAN_VOLUME;
  931. if (aOuts >= 2 && aOuts % 2 == 0)
  932. fHints |= PLUGIN_CAN_BALANCE;
  933. // native plugin hints
  934. if (fDescriptor->hints & ::PLUGIN_IS_RTSAFE)
  935. fHints |= PLUGIN_IS_RTSAFE;
  936. if (fDescriptor->hints & ::PLUGIN_IS_SYNTH)
  937. fHints |= PLUGIN_IS_SYNTH;
  938. if (fDescriptor->hints & ::PLUGIN_HAS_GUI)
  939. fHints |= PLUGIN_HAS_GUI;
  940. if (fDescriptor->hints & ::PLUGIN_USES_GUI_AS_FILE)
  941. fHints |= PLUGIN_HAS_GUI_AS_FILE;
  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->osc_send_control_set_midi_program_count(fId, count);
  984. for (i=0; i < count; ++i)
  985. pData->engine->osc_send_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. 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. 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. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  1233. 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].unnormalizeValue(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. 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. 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_AFTERTOUCH(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. postponeRtEvent(kPluginPostRtEventNoteOn, channel, midiEvent.data[1], midiEvent.data[2]);
  1363. else if (status == MIDI_STATUS_NOTE_OFF)
  1364. 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].normalizeValue(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, fMidiEventCount, fMidiEvents);
  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, fMidiEventCount, fMidiEvents);
  1463. fDescriptor->process(fHandle2,
  1464. (pData->audioIn.count > 0) ? &fAudioInBuffers[1] : nullptr,
  1465. (pData->audioOut.count > 0) ? &fAudioOutBuffers[1] : nullptr,
  1466. frames, fMidiEventCount, fMidiEvents);
  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(pData->engine);
  1582. fMidiOut.initBuffers(pData->engine);
  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.ui_name == nullptr);
  1904. char uiName[fName.length()+6+1];
  1905. std::strcpy(uiName, (const char*)fName);
  1906. std::strcat(uiName, " (GUI)");
  1907. fHost.ui_name = 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(fDescriptor, &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 (midiInCount() > 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, availableOptions());
  1949. // ignore settings, we need this anyway
  1950. if (midiInCount() > 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 && ! CarlaPluginProtectedData::canRunInRack(plugin))
  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