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.

2316 lines
75KB

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