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.

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