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.

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