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.

2308 lines
74KB

  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. if (kData->midiprog.current >= 0 && kData->midiprog.count > 0)
  1051. nextBankId = kData->midiprog.data[kData->midiprog.current].bank;
  1052. else
  1053. nextBankId = 0;
  1054. if (fMidiEventCount > 0)
  1055. {
  1056. carla_zeroStruct< ::MidiEvent>(fMidiEvents, fMidiEventCount);
  1057. fMidiEventCount = 0;
  1058. }
  1059. }
  1060. else
  1061. startTime += timeOffset;
  1062. }
  1063. // Control change
  1064. switch (event.type)
  1065. {
  1066. case kEngineEventTypeNull:
  1067. break;
  1068. case kEngineEventTypeControl:
  1069. {
  1070. const EngineControlEvent& ctrlEvent = event.ctrl;
  1071. switch (ctrlEvent.type)
  1072. {
  1073. case kEngineControlEventTypeNull:
  1074. break;
  1075. case kEngineControlEventTypeParameter:
  1076. {
  1077. #ifndef BUILD_BRIDGE
  1078. // Control backend stuff
  1079. if (event.channel == kData->ctrlChannel)
  1080. {
  1081. float value;
  1082. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (fHints & PLUGIN_CAN_DRYWET) > 0)
  1083. {
  1084. value = ctrlEvent.value;
  1085. setDryWet(value, false, false);
  1086. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, value);
  1087. continue;
  1088. }
  1089. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (fHints & PLUGIN_CAN_VOLUME) > 0)
  1090. {
  1091. value = ctrlEvent.value*127.0f/100.0f;
  1092. setVolume(value, false, false);
  1093. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
  1094. continue;
  1095. }
  1096. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (fHints & PLUGIN_CAN_BALANCE) > 0)
  1097. {
  1098. float left, right;
  1099. value = ctrlEvent.value/0.5f - 1.0f;
  1100. if (value < 0.0f)
  1101. {
  1102. left = -1.0f;
  1103. right = (value*2.0f)+1.0f;
  1104. }
  1105. else if (value > 0.0f)
  1106. {
  1107. left = (value*2.0f)-1.0f;
  1108. right = 1.0f;
  1109. }
  1110. else
  1111. {
  1112. left = -1.0f;
  1113. right = 1.0f;
  1114. }
  1115. setBalanceLeft(left, false, false);
  1116. setBalanceRight(right, false, false);
  1117. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  1118. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  1119. continue;
  1120. }
  1121. }
  1122. #endif
  1123. // Control plugin parameters
  1124. for (k=0; k < kData->param.count; ++k)
  1125. {
  1126. if (kData->param.data[k].midiChannel != event.channel)
  1127. continue;
  1128. if (kData->param.data[k].midiCC != ctrlEvent.param)
  1129. continue;
  1130. if (kData->param.data[k].type != PARAMETER_INPUT)
  1131. continue;
  1132. if ((kData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  1133. continue;
  1134. float value;
  1135. if (kData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  1136. {
  1137. value = (ctrlEvent.value < 0.5f) ? kData->param.ranges[k].min : kData->param.ranges[k].max;
  1138. }
  1139. else
  1140. {
  1141. value = kData->param.ranges[i].unnormalizeValue(ctrlEvent.value);
  1142. if (kData->param.data[k].hints & PARAMETER_IS_INTEGER)
  1143. value = std::rint(value);
  1144. }
  1145. setParameterValue(k, value, false, false, false);
  1146. postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, value);
  1147. }
  1148. break;
  1149. }
  1150. case kEngineControlEventTypeMidiBank:
  1151. if (event.channel == kData->ctrlChannel && (fOptions & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  1152. nextBankId = ctrlEvent.param;
  1153. break;
  1154. case kEngineControlEventTypeMidiProgram:
  1155. if (event.channel == kData->ctrlChannel && (fOptions & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  1156. {
  1157. const uint32_t nextProgramId = ctrlEvent.param;
  1158. for (k=0; k < kData->midiprog.count; ++k)
  1159. {
  1160. if (kData->midiprog.data[k].bank == nextBankId && kData->midiprog.data[k].program == nextProgramId)
  1161. {
  1162. setMidiProgram(k, false, false, false);
  1163. postponeRtEvent(kPluginPostRtEventMidiProgramChange, k, 0, 0.0f);
  1164. break;
  1165. }
  1166. }
  1167. }
  1168. break;
  1169. case kEngineControlEventTypeAllSoundOff:
  1170. if (fOptions & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1171. {
  1172. if (fMidiEventCount >= MAX_MIDI_EVENTS*2)
  1173. continue;
  1174. fMidiEvents[fMidiEventCount].port = 0;
  1175. fMidiEvents[fMidiEventCount].time = sampleAccurate ? startTime : time;
  1176. fMidiEvents[fMidiEventCount].data[0] = MIDI_STATUS_CONTROL_CHANGE + event.channel;
  1177. fMidiEvents[fMidiEventCount].data[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  1178. fMidiEvents[fMidiEventCount].data[2] = 0;
  1179. fMidiEvents[fMidiEventCount].size = 3;
  1180. fMidiEventCount += 1;
  1181. }
  1182. break;
  1183. case kEngineControlEventTypeAllNotesOff:
  1184. if (fOptions & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1185. {
  1186. if (event.channel == kData->ctrlChannel && ! allNotesOffSent)
  1187. {
  1188. allNotesOffSent = true;
  1189. sendMidiAllNotesOffToCallback();
  1190. }
  1191. if (fMidiEventCount >= MAX_MIDI_EVENTS*2)
  1192. continue;
  1193. fMidiEvents[fMidiEventCount].port = 0;
  1194. fMidiEvents[fMidiEventCount].time = sampleAccurate ? startTime : time;
  1195. fMidiEvents[fMidiEventCount].data[0] = MIDI_STATUS_CONTROL_CHANGE + event.channel;
  1196. fMidiEvents[fMidiEventCount].data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  1197. fMidiEvents[fMidiEventCount].data[2] = 0;
  1198. fMidiEvents[fMidiEventCount].size = 3;
  1199. fMidiEventCount += 1;
  1200. }
  1201. break;
  1202. }
  1203. break;
  1204. }
  1205. case kEngineEventTypeMidi:
  1206. {
  1207. if (fMidiEventCount >= MAX_MIDI_EVENTS*2)
  1208. continue;
  1209. const EngineMidiEvent& midiEvent(event.midi);
  1210. uint8_t status = MIDI_GET_STATUS_FROM_DATA(midiEvent.data);
  1211. uint8_t channel = event.channel;
  1212. if (MIDI_IS_STATUS_AFTERTOUCH(status) && (fOptions & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) == 0)
  1213. continue;
  1214. if (MIDI_IS_STATUS_CONTROL_CHANGE(status) && (fOptions & PLUGIN_OPTION_SEND_CONTROL_CHANGES) == 0)
  1215. continue;
  1216. if (MIDI_IS_STATUS_POLYPHONIC_AFTERTOUCH(status) && (fOptions & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) == 0)
  1217. continue;
  1218. if (MIDI_IS_STATUS_PITCH_WHEEL_CONTROL(status) && (fOptions & PLUGIN_OPTION_SEND_PITCHBEND) == 0)
  1219. continue;
  1220. // Fix bad note-off
  1221. if (status == MIDI_STATUS_NOTE_ON && midiEvent.data[2] == 0)
  1222. status -= 0x10;
  1223. fMidiEvents[fMidiEventCount].port = 0;
  1224. fMidiEvents[fMidiEventCount].time = sampleAccurate ? startTime : time;
  1225. fMidiEvents[fMidiEventCount].size = midiEvent.size;
  1226. fMidiEvents[fMidiEventCount].data[0] = status + channel;
  1227. fMidiEvents[fMidiEventCount].data[1] = midiEvent.data[1];
  1228. fMidiEvents[fMidiEventCount].data[2] = midiEvent.data[2];
  1229. fMidiEvents[fMidiEventCount].data[3] = midiEvent.data[3];
  1230. fMidiEventCount += 1;
  1231. if (status == MIDI_STATUS_NOTE_ON)
  1232. postponeRtEvent(kPluginPostRtEventNoteOn, channel, midiEvent.data[1], midiEvent.data[2]);
  1233. else if (status == MIDI_STATUS_NOTE_OFF)
  1234. postponeRtEvent(kPluginPostRtEventNoteOff, channel, midiEvent.data[1], 0.0f);
  1235. break;
  1236. }
  1237. }
  1238. }
  1239. kData->postRtEvents.trySplice();
  1240. if (frames > timeOffset)
  1241. processSingle(inBuffer, outBuffer, frames - timeOffset, timeOffset);
  1242. } // End of Event Input and Processing
  1243. // --------------------------------------------------------------------------------------------------------
  1244. // Plugin processing (no events)
  1245. else
  1246. {
  1247. processSingle(inBuffer, outBuffer, frames, 0);
  1248. } // End of Plugin processing (no events)
  1249. CARLA_PROCESS_CONTINUE_CHECK;
  1250. // --------------------------------------------------------------------------------------------------------
  1251. // Control and MIDI Output
  1252. if (fMidiOut.count > 0 || kData->event.portOut != nullptr)
  1253. {
  1254. float value, curValue;
  1255. for (k=0; k < kData->param.count; ++k)
  1256. {
  1257. if (kData->param.data[k].type != PARAMETER_OUTPUT)
  1258. continue;
  1259. curValue = fDescriptor->get_parameter_value(fHandle, k);
  1260. kData->param.ranges[k].fixValue(curValue);
  1261. if (kData->param.data[k].midiCC > 0)
  1262. {
  1263. value = kData->param.ranges[k].normalizeValue(curValue);
  1264. kData->event.portOut->writeControlEvent(0, kData->param.data[k].midiChannel, kEngineControlEventTypeParameter, kData->param.data[k].midiCC, value);
  1265. }
  1266. }
  1267. // reverse lookup MIDI events
  1268. for (k = (MAX_MIDI_EVENTS*2)-1; k >= fMidiEventCount; --k)
  1269. {
  1270. if (fMidiEvents[k].data[0] == 0)
  1271. break;
  1272. const uint8_t channel = MIDI_GET_CHANNEL_FROM_DATA(fMidiEvents[k].data);
  1273. const uint8_t port = fMidiEvents[k].port;
  1274. if (kData->event.portOut != nullptr)
  1275. kData->event.portOut->writeMidiEvent(fMidiEvents[k].time, channel, port, fMidiEvents[k].data, fMidiEvents[k].size);
  1276. else if (port < fMidiOut.count)
  1277. fMidiOut.ports[port]->writeMidiEvent(fMidiEvents[k].time, channel, port, fMidiEvents[k].data, fMidiEvents[k].size);
  1278. }
  1279. } // End of Control and MIDI Output
  1280. }
  1281. bool processSingle(float** const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t timeOffset)
  1282. {
  1283. CARLA_ASSERT(frames > 0);
  1284. if (frames == 0)
  1285. return false;
  1286. if (kData->audioIn.count > 0)
  1287. {
  1288. CARLA_ASSERT(inBuffer != nullptr);
  1289. if (inBuffer == nullptr)
  1290. return false;
  1291. }
  1292. if (kData->audioOut.count > 0)
  1293. {
  1294. CARLA_ASSERT(outBuffer != nullptr);
  1295. if (outBuffer == nullptr)
  1296. return false;
  1297. }
  1298. uint32_t i, k;
  1299. // --------------------------------------------------------------------------------------------------------
  1300. // Try lock, silence otherwise
  1301. if (kData->engine->isOffline())
  1302. {
  1303. kData->singleMutex.lock();
  1304. }
  1305. else if (! kData->singleMutex.tryLock())
  1306. {
  1307. for (i=0; i < kData->audioOut.count; ++i)
  1308. {
  1309. for (k=0; k < frames; ++k)
  1310. outBuffer[i][k+timeOffset] = 0.0f;
  1311. }
  1312. return false;
  1313. }
  1314. // --------------------------------------------------------------------------------------------------------
  1315. // Reset audio buffers
  1316. for (i=0; i < kData->audioIn.count; ++i)
  1317. carla_copyFloat(fAudioInBuffers[i], inBuffer[i]+timeOffset, frames);
  1318. for (i=0; i < kData->audioOut.count; ++i)
  1319. carla_zeroFloat(fAudioOutBuffers[i], frames);
  1320. // --------------------------------------------------------------------------------------------------------
  1321. // Run plugin
  1322. fIsProcessing = true;
  1323. if (fHandle2 == nullptr)
  1324. {
  1325. fDescriptor->process(fHandle, fAudioInBuffers, fAudioOutBuffers, frames, fMidiEventCount, fMidiEvents);
  1326. }
  1327. else
  1328. {
  1329. fDescriptor->process(fHandle,
  1330. (kData->audioIn.count > 0) ? &fAudioInBuffers[0] : nullptr,
  1331. (kData->audioOut.count > 0) ? &fAudioOutBuffers[0] : nullptr,
  1332. frames, fMidiEventCount, fMidiEvents);
  1333. fDescriptor->process(fHandle2,
  1334. (kData->audioIn.count > 0) ? &fAudioInBuffers[1] : nullptr,
  1335. (kData->audioOut.count > 0) ? &fAudioOutBuffers[1] : nullptr,
  1336. frames, fMidiEventCount, fMidiEvents);
  1337. }
  1338. fIsProcessing = false;
  1339. fTimeInfo.frame += frames;
  1340. #ifndef BUILD_BRIDGE
  1341. // --------------------------------------------------------------------------------------------------------
  1342. // Post-processing (dry/wet, volume and balance)
  1343. {
  1344. const bool doDryWet = (fHints & PLUGIN_CAN_DRYWET) != 0 && kData->postProc.dryWet != 1.0f;
  1345. const bool doBalance = (fHints & PLUGIN_CAN_BALANCE) != 0 && (kData->postProc.balanceLeft != -1.0f || kData->postProc.balanceRight != 1.0f);
  1346. bool isPair;
  1347. float bufValue, oldBufLeft[doBalance ? frames : 1];
  1348. for (i=0; i < kData->audioOut.count; ++i)
  1349. {
  1350. // Dry/Wet
  1351. if (doDryWet)
  1352. {
  1353. for (k=0; k < frames; ++k)
  1354. {
  1355. bufValue = fAudioInBuffers[(kData->audioIn.count == 1) ? 0 : i][k];
  1356. fAudioOutBuffers[i][k] = (fAudioOutBuffers[i][k] * kData->postProc.dryWet) + (bufValue * (1.0f - kData->postProc.dryWet));
  1357. }
  1358. }
  1359. // Balance
  1360. if (doBalance)
  1361. {
  1362. isPair = (i % 2 == 0);
  1363. if (isPair)
  1364. {
  1365. CARLA_ASSERT(i+1 < kData->audioOut.count);
  1366. carla_copyFloat(oldBufLeft, fAudioOutBuffers[i], frames);
  1367. }
  1368. float balRangeL = (kData->postProc.balanceLeft + 1.0f)/2.0f;
  1369. float balRangeR = (kData->postProc.balanceRight + 1.0f)/2.0f;
  1370. for (k=0; k < frames; ++k)
  1371. {
  1372. if (isPair)
  1373. {
  1374. // left
  1375. fAudioOutBuffers[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  1376. fAudioOutBuffers[i][k] += fAudioOutBuffers[i+1][k] * (1.0f - balRangeR);
  1377. }
  1378. else
  1379. {
  1380. // right
  1381. fAudioOutBuffers[i][k] = fAudioOutBuffers[i][k] * balRangeR;
  1382. fAudioOutBuffers[i][k] += oldBufLeft[k] * balRangeL;
  1383. }
  1384. }
  1385. }
  1386. // Volume (and buffer copy)
  1387. {
  1388. for (k=0; k < frames; ++k)
  1389. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k] * kData->postProc.volume;
  1390. }
  1391. }
  1392. } // End of Post-processing
  1393. #else
  1394. for (i=0; i < kData->audioOut.count; ++i)
  1395. {
  1396. for (k=0; k < frames; ++k)
  1397. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k];
  1398. }
  1399. #endif
  1400. // --------------------------------------------------------------------------------------------------------
  1401. kData->singleMutex.unlock();
  1402. return true;
  1403. }
  1404. void bufferSizeChanged(const uint32_t newBufferSize) override
  1405. {
  1406. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  1407. carla_debug("NativePlugin::bufferSizeChanged(%i)", newBufferSize);
  1408. for (uint32_t i=0; i < kData->audioIn.count; ++i)
  1409. {
  1410. if (fAudioInBuffers[i] != nullptr)
  1411. delete[] fAudioInBuffers[i];
  1412. fAudioInBuffers[i] = new float[newBufferSize];
  1413. }
  1414. for (uint32_t i=0; i < kData->audioOut.count; ++i)
  1415. {
  1416. if (fAudioOutBuffers[i] != nullptr)
  1417. delete[] fAudioOutBuffers[i];
  1418. fAudioOutBuffers[i] = new float[newBufferSize];
  1419. }
  1420. }
  1421. // -------------------------------------------------------------------
  1422. // Plugin buffers
  1423. void initBuffers() override
  1424. {
  1425. fMidiIn.initBuffers(kData->engine);
  1426. fMidiOut.initBuffers(kData->engine);
  1427. CarlaPlugin::initBuffers();
  1428. }
  1429. void clearBuffers() override
  1430. {
  1431. carla_debug("NativePlugin::clearBuffers() - start");
  1432. if (fAudioInBuffers != nullptr)
  1433. {
  1434. for (uint32_t i=0; i < kData->audioIn.count; ++i)
  1435. {
  1436. if (fAudioInBuffers[i] != nullptr)
  1437. {
  1438. delete[] fAudioInBuffers[i];
  1439. fAudioInBuffers[i] = nullptr;
  1440. }
  1441. }
  1442. delete[] fAudioInBuffers;
  1443. fAudioInBuffers = nullptr;
  1444. }
  1445. if (fAudioOutBuffers != nullptr)
  1446. {
  1447. for (uint32_t i=0; i < kData->audioOut.count; ++i)
  1448. {
  1449. if (fAudioOutBuffers[i] != nullptr)
  1450. {
  1451. delete[] fAudioOutBuffers[i];
  1452. fAudioOutBuffers[i] = nullptr;
  1453. }
  1454. }
  1455. delete[] fAudioOutBuffers;
  1456. fAudioOutBuffers = nullptr;
  1457. }
  1458. fMidiIn.clear();
  1459. fMidiOut.clear();
  1460. CarlaPlugin::clearBuffers();
  1461. carla_debug("NativePlugin::clearBuffers() - end");
  1462. }
  1463. // -------------------------------------------------------------------
  1464. // Post-poned UI Stuff
  1465. void uiParameterChange(const uint32_t index, const float value) override
  1466. {
  1467. CARLA_ASSERT(fDescriptor != nullptr);
  1468. CARLA_ASSERT(fHandle != nullptr);
  1469. CARLA_ASSERT(index < kData->param.count);
  1470. if (! fIsUiVisible)
  1471. return;
  1472. if (fDescriptor == nullptr || fHandle == nullptr)
  1473. return;
  1474. if (index >= kData->param.count)
  1475. return;
  1476. if (fDescriptor->ui_set_parameter_value != nullptr)
  1477. fDescriptor->ui_set_parameter_value(fHandle, index, value);
  1478. }
  1479. void uiMidiProgramChange(const uint32_t index) override
  1480. {
  1481. CARLA_ASSERT(fDescriptor != nullptr);
  1482. CARLA_ASSERT(fHandle != nullptr);
  1483. CARLA_ASSERT(index < kData->midiprog.count);
  1484. if (! fIsUiVisible)
  1485. return;
  1486. if (fDescriptor == nullptr || fHandle == nullptr)
  1487. return;
  1488. if (index >= kData->midiprog.count)
  1489. return;
  1490. if (fDescriptor->ui_set_midi_program != nullptr)
  1491. fDescriptor->ui_set_midi_program(fHandle, kData->midiprog.data[index].bank, kData->midiprog.data[index].program);
  1492. }
  1493. void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) override
  1494. {
  1495. CARLA_ASSERT(fDescriptor != nullptr);
  1496. CARLA_ASSERT(fHandle != nullptr);
  1497. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  1498. CARLA_ASSERT(note < MAX_MIDI_NOTE);
  1499. CARLA_ASSERT(velo > 0 && velo < MAX_MIDI_VALUE);
  1500. if (! fIsUiVisible)
  1501. return;
  1502. if (fDescriptor == nullptr || fHandle == nullptr)
  1503. return;
  1504. if (channel >= MAX_MIDI_CHANNELS)
  1505. return;
  1506. if (note >= MAX_MIDI_NOTE)
  1507. return;
  1508. if (velo >= MAX_MIDI_VALUE)
  1509. return;
  1510. // TODO
  1511. }
  1512. void uiNoteOff(const uint8_t channel, const uint8_t note) override
  1513. {
  1514. CARLA_ASSERT(fDescriptor != nullptr);
  1515. CARLA_ASSERT(fHandle != nullptr);
  1516. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  1517. CARLA_ASSERT(note < MAX_MIDI_NOTE);
  1518. if (! fIsUiVisible)
  1519. return;
  1520. if (fDescriptor == nullptr || fHandle == nullptr)
  1521. return;
  1522. if (channel >= MAX_MIDI_CHANNELS)
  1523. return;
  1524. if (note >= MAX_MIDI_NOTE)
  1525. return;
  1526. // TODO
  1527. }
  1528. // -------------------------------------------------------------------
  1529. protected:
  1530. uint32_t handleGetBufferSize()
  1531. {
  1532. return kData->engine->getBufferSize();
  1533. }
  1534. double handleGetSampleRate()
  1535. {
  1536. return kData->engine->getSampleRate();
  1537. }
  1538. const ::TimeInfo* handleGetTimeInfo()
  1539. {
  1540. CARLA_ASSERT(fIsProcessing);
  1541. return &fTimeInfo;
  1542. }
  1543. bool handleWriteMidiEvent(const ::MidiEvent* const event)
  1544. {
  1545. CARLA_ASSERT(fEnabled);
  1546. CARLA_ASSERT(fIsProcessing);
  1547. CARLA_ASSERT(fMidiOut.count > 0 || kData->event.portOut != nullptr);
  1548. CARLA_ASSERT(event != nullptr);
  1549. CARLA_ASSERT(event->data[0] != 0);
  1550. if (! fEnabled)
  1551. return false;
  1552. if (fMidiOut.count == 0)
  1553. return false;
  1554. if (event == nullptr)
  1555. return false;
  1556. if (event->data[0] == 0)
  1557. return false;
  1558. if (! fIsProcessing)
  1559. {
  1560. carla_stderr2("NativePlugin::handleWriteMidiEvent(%p) - received MIDI out event outside audio thread, ignoring", event);
  1561. return false;
  1562. }
  1563. if (fMidiEventCount >= MAX_MIDI_EVENTS*2)
  1564. return false;
  1565. // reverse-find first free event, and put it there
  1566. for (uint32_t i=(MAX_MIDI_EVENTS*2)-1; i >= fMidiEventCount; --i)
  1567. {
  1568. if (fMidiEvents[i].data[0] == 0)
  1569. {
  1570. std::memcpy(&fMidiEvents[i], event, sizeof(::MidiEvent));
  1571. break;
  1572. }
  1573. }
  1574. return true;
  1575. }
  1576. void handleUiParameterChanged(const uint32_t index, const float value)
  1577. {
  1578. setParameterValue(index, value, false, true, true);
  1579. }
  1580. void handleUiCustomDataChanged(const char* const key, const char* const value)
  1581. {
  1582. setCustomData(CUSTOM_DATA_STRING, key, value, false);
  1583. }
  1584. void handleUiClosed()
  1585. {
  1586. kData->engine->callback(CALLBACK_SHOW_GUI, fId, 0, 0, 0.0f, nullptr);
  1587. fIsUiVisible = false;
  1588. }
  1589. const char* handleUiOpenFile(const bool isDir, const char* const title, const char* const filter)
  1590. {
  1591. static CarlaString retStr;
  1592. QFileDialog::Options options(isDir ? QFileDialog::ShowDirsOnly : 0x0);
  1593. retStr = QFileDialog::getOpenFileName(nullptr, title, "", filter, nullptr, options).toUtf8().constData();
  1594. return retStr.isNotEmpty() ? (const char*)retStr : nullptr;
  1595. }
  1596. const char* handleUiSaveFile(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::getSaveFileName(nullptr, title, "", filter, nullptr, options).toUtf8().constData();
  1601. return retStr.isNotEmpty() ? (const char*)retStr : nullptr;
  1602. }
  1603. // -------------------------------------------------------------------
  1604. public:
  1605. static size_t getPluginCount()
  1606. {
  1607. return sPluginDescriptors.count();
  1608. }
  1609. static const PluginDescriptor* getPluginDescriptor(const size_t index)
  1610. {
  1611. CARLA_ASSERT(index < sPluginDescriptors.count());
  1612. if (index < sPluginDescriptors.count())
  1613. return sPluginDescriptors.getAt(index);
  1614. return nullptr;
  1615. }
  1616. static void registerPlugin(const PluginDescriptor* desc)
  1617. {
  1618. sPluginDescriptors.append(desc);
  1619. }
  1620. // -------------------------------------------------------------------
  1621. bool init(const char* const name, const char* const label)
  1622. {
  1623. CARLA_ASSERT(kData->engine != nullptr);
  1624. CARLA_ASSERT(kData->client == nullptr);
  1625. CARLA_ASSERT(label != nullptr);
  1626. // ---------------------------------------------------------------
  1627. // first checks
  1628. if (kData->engine == nullptr)
  1629. {
  1630. return false;
  1631. }
  1632. if (kData->client != nullptr)
  1633. {
  1634. kData->engine->setLastError("Plugin client is already registered");
  1635. return false;
  1636. }
  1637. if (label == nullptr)
  1638. {
  1639. kData->engine->setLastError("null label");
  1640. return false;
  1641. }
  1642. // ---------------------------------------------------------------
  1643. // get descriptor that matches label
  1644. for (auto it = sPluginDescriptors.begin(); it.valid(); it.next())
  1645. {
  1646. fDescriptor = *it;
  1647. CARLA_ASSERT(fDescriptor != nullptr);
  1648. if (fDescriptor == nullptr)
  1649. break;
  1650. if (fDescriptor->label != nullptr && std::strcmp(fDescriptor->label, label) == 0)
  1651. break;
  1652. fDescriptor = nullptr;
  1653. }
  1654. if (fDescriptor == nullptr)
  1655. {
  1656. kData->engine->setLastError("Invalid internal plugin");
  1657. return false;
  1658. }
  1659. // ---------------------------------------------------------------
  1660. // get info
  1661. if (name != nullptr)
  1662. fName = kData->engine->getUniquePluginName(name);
  1663. else if (fDescriptor->name != nullptr)
  1664. fName = kData->engine->getUniquePluginName(fDescriptor->name);
  1665. else
  1666. fName = kData->engine->getUniquePluginName(label);
  1667. {
  1668. CARLA_ASSERT(fHost.ui_name == nullptr);
  1669. char uiName[fName.length()+6+1];
  1670. std::strcpy(uiName, (const char*)fName);
  1671. std::strcat(uiName, " (GUI)");
  1672. fHost.ui_name = carla_strdup(uiName);
  1673. }
  1674. // ---------------------------------------------------------------
  1675. // register client
  1676. kData->client = kData->engine->addClient(this);
  1677. if (kData->client == nullptr || ! kData->client->isOk())
  1678. {
  1679. kData->engine->setLastError("Failed to register plugin client");
  1680. return false;
  1681. }
  1682. // ---------------------------------------------------------------
  1683. // initialize plugin
  1684. fHandle = fDescriptor->instantiate(fDescriptor, &fHost);
  1685. if (fHandle == nullptr)
  1686. {
  1687. kData->engine->setLastError("Plugin failed to initialize");
  1688. return false;
  1689. }
  1690. // ---------------------------------------------------------------
  1691. // load plugin settings
  1692. {
  1693. // set default options
  1694. fOptions = 0x0;
  1695. fOptions |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  1696. if (midiInCount() > 0)
  1697. fOptions |= PLUGIN_OPTION_FIXED_BUFFER;
  1698. if (kData->engine->getOptions().forceStereo)
  1699. fOptions |= PLUGIN_OPTION_FORCE_STEREO;
  1700. if (fDescriptor->midiIns > 0)
  1701. {
  1702. fOptions |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  1703. fOptions |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  1704. fOptions |= PLUGIN_OPTION_SEND_PITCHBEND;
  1705. fOptions |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  1706. }
  1707. // load settings
  1708. kData->idStr = "Native/";
  1709. kData->idStr += label;
  1710. fOptions = kData->loadSettings(fOptions, availableOptions());
  1711. // ignore settings, we need this anyway
  1712. if (midiInCount() > 0)
  1713. fOptions |= PLUGIN_OPTION_FIXED_BUFFER;
  1714. }
  1715. return true;
  1716. }
  1717. class ScopedInitializer
  1718. {
  1719. public:
  1720. ScopedInitializer()
  1721. {
  1722. carla_register_all_plugins();
  1723. }
  1724. ~ScopedInitializer()
  1725. {
  1726. sPluginDescriptors.clear();
  1727. }
  1728. };
  1729. private:
  1730. PluginHandle fHandle;
  1731. PluginHandle fHandle2;
  1732. HostDescriptor fHost;
  1733. const PluginDescriptor* fDescriptor;
  1734. bool fIsProcessing;
  1735. bool fIsUiVisible;
  1736. float** fAudioInBuffers;
  1737. float** fAudioOutBuffers;
  1738. uint32_t fMidiEventCount;
  1739. ::MidiEvent fMidiEvents[MAX_MIDI_EVENTS*2];
  1740. NativePluginMidiData fMidiIn;
  1741. NativePluginMidiData fMidiOut;
  1742. ::TimeInfo fTimeInfo;
  1743. static NonRtList<const PluginDescriptor*> sPluginDescriptors;
  1744. // -------------------------------------------------------------------
  1745. #define handlePtr ((NativePlugin*)handle)
  1746. static uint32_t carla_host_get_buffer_size(HostHandle handle)
  1747. {
  1748. return handlePtr->handleGetBufferSize();
  1749. }
  1750. static double carla_host_get_sample_rate(HostHandle handle)
  1751. {
  1752. return handlePtr->handleGetSampleRate();
  1753. }
  1754. static const ::TimeInfo* carla_host_get_time_info(HostHandle handle)
  1755. {
  1756. return handlePtr->handleGetTimeInfo();
  1757. }
  1758. static bool carla_host_write_midi_event(HostHandle handle, const ::MidiEvent* event)
  1759. {
  1760. return handlePtr->handleWriteMidiEvent(event);
  1761. }
  1762. static void carla_host_ui_parameter_changed(HostHandle handle, uint32_t index, float value)
  1763. {
  1764. handlePtr->handleUiParameterChanged(index, value);
  1765. }
  1766. static void carla_host_ui_custom_data_changed(HostHandle handle, const char* key, const char* value)
  1767. {
  1768. handlePtr->handleUiCustomDataChanged(key, value);
  1769. }
  1770. static void carla_host_ui_closed(HostHandle handle)
  1771. {
  1772. handlePtr->handleUiClosed();
  1773. }
  1774. static const char* carla_host_ui_open_file(HostHandle handle, bool isDir, const char* title, const char* filter)
  1775. {
  1776. return handlePtr->handleUiOpenFile(isDir, title, filter);
  1777. }
  1778. static const char* carla_host_ui_save_file(HostHandle handle, bool isDir, const char* title, const char* filter)
  1779. {
  1780. return handlePtr->handleUiSaveFile(isDir, title, filter);
  1781. }
  1782. #undef handlePtr
  1783. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(NativePlugin)
  1784. };
  1785. NonRtList<const PluginDescriptor*> NativePlugin::sPluginDescriptors;
  1786. static const NativePlugin::ScopedInitializer _si;
  1787. CARLA_BACKEND_END_NAMESPACE
  1788. void carla_register_native_plugin(const PluginDescriptor* desc)
  1789. {
  1790. CARLA_BACKEND_USE_NAMESPACE
  1791. NativePlugin::registerPlugin(desc);
  1792. }
  1793. #else // WANT_NATIVE
  1794. # warning Building without Internal plugin support
  1795. #endif
  1796. CARLA_BACKEND_START_NAMESPACE
  1797. // -----------------------------------------------------------------------
  1798. #ifdef WANT_NATIVE
  1799. size_t CarlaPlugin::getNativePluginCount()
  1800. {
  1801. return NativePlugin::getPluginCount();
  1802. }
  1803. const PluginDescriptor* CarlaPlugin::getNativePluginDescriptor(const size_t index)
  1804. {
  1805. return NativePlugin::getPluginDescriptor(index);
  1806. }
  1807. #endif
  1808. // -----------------------------------------------------------------------
  1809. CarlaPlugin* CarlaPlugin::newNative(const Initializer& init)
  1810. {
  1811. carla_debug("CarlaPlugin::newNative({%p, \"%s\", \"%s\", \"%s\"})", init.engine, init.filename, init.name, init.label);
  1812. #ifdef WANT_NATIVE
  1813. NativePlugin* const plugin(new NativePlugin(init.engine, init.id));
  1814. if (! plugin->init(init.name, init.label))
  1815. {
  1816. delete plugin;
  1817. return nullptr;
  1818. }
  1819. plugin->reload();
  1820. if (init.engine->getProccessMode() == PROCESS_MODE_CONTINUOUS_RACK && ! CarlaPluginProtectedData::canRunInRack(plugin))
  1821. {
  1822. init.engine->setLastError("Carla's rack mode can only work with Mono or Stereo Internal plugins, sorry!");
  1823. delete plugin;
  1824. return nullptr;
  1825. }
  1826. return plugin;
  1827. #else
  1828. init.engine->setLastError("Internal plugins support not available");
  1829. return nullptr;
  1830. #endif
  1831. }
  1832. CARLA_BACKEND_END_NAMESPACE