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.

2300 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. #ifndef CARLA_OS_WIN
  36. carla_register_native_plugin_nekofilter();
  37. #endif
  38. carla_register_native_plugin_sunvoxfile(); // unfinished
  39. // Carla
  40. //carla_register_native_plugin_carla(); // unfinished
  41. #ifdef WANT_AUDIOFILE
  42. // AudioFile
  43. carla_register_native_plugin_audiofile();
  44. #endif
  45. #ifdef WANT_MIDIFILE
  46. // MidiFile
  47. carla_register_native_plugin_midifile();
  48. #endif
  49. #ifdef WANT_OPENGL
  50. // DISTRHO plugins (OpenGL)
  51. carla_register_native_plugin_3BandEQ();
  52. carla_register_native_plugin_3BandSplitter();
  53. carla_register_native_plugin_PingPongPan();
  54. #endif
  55. // DISTRHO plugins (Qt)
  56. carla_register_native_plugin_Notes();
  57. #ifdef WANT_ZYNADDSUBFX
  58. // ZynAddSubFX
  59. carla_register_native_plugin_zynaddsubfx();
  60. #endif
  61. }
  62. CARLA_BACKEND_START_NAMESPACE
  63. #if 0
  64. }
  65. #endif
  66. struct NativePluginMidiData {
  67. uint32_t count;
  68. uint32_t* indexes;
  69. CarlaEngineEventPort** ports;
  70. NativePluginMidiData()
  71. : count(0),
  72. indexes(nullptr),
  73. ports(nullptr) {}
  74. ~NativePluginMidiData()
  75. {
  76. CARLA_ASSERT_INT(count == 0, count);
  77. CARLA_ASSERT(ports == nullptr);
  78. CARLA_ASSERT(indexes == nullptr);
  79. }
  80. void createNew(const uint32_t newCount)
  81. {
  82. CARLA_ASSERT_INT(count == 0, count);
  83. CARLA_ASSERT(ports == nullptr);
  84. CARLA_ASSERT(indexes == nullptr);
  85. CARLA_ASSERT_INT(newCount > 0, newCount);
  86. if (ports != nullptr || indexes != nullptr || newCount == 0)
  87. return;
  88. ports = new CarlaEngineEventPort*[newCount];
  89. indexes = new uint32_t[newCount];
  90. count = newCount;
  91. for (uint32_t i=0; i < newCount; ++i)
  92. ports[i] = nullptr;
  93. for (uint32_t i=0; i < newCount; ++i)
  94. indexes[i] = 0;
  95. }
  96. void clear()
  97. {
  98. if (ports != nullptr)
  99. {
  100. for (uint32_t i=0; i < count; ++i)
  101. {
  102. if (ports[i] != nullptr)
  103. {
  104. delete ports[i];
  105. ports[i] = nullptr;
  106. }
  107. }
  108. delete[] ports;
  109. ports = nullptr;
  110. }
  111. if (indexes != nullptr)
  112. {
  113. delete[] indexes;
  114. indexes = nullptr;
  115. }
  116. count = 0;
  117. }
  118. void initBuffers(CarlaEngine* const engine)
  119. {
  120. for (uint32_t i=0; i < count; ++i)
  121. {
  122. if (ports[i] != nullptr)
  123. ports[i]->initBuffer(engine);
  124. }
  125. }
  126. CARLA_DECLARE_NON_COPY_STRUCT_WITH_LEAK_DETECTOR(NativePluginMidiData)
  127. };
  128. class NativePlugin : public CarlaPlugin
  129. {
  130. public:
  131. NativePlugin(CarlaEngine* const engine, const unsigned int id)
  132. : CarlaPlugin(engine, id),
  133. fHandle(nullptr),
  134. fHandle2(nullptr),
  135. fDescriptor(nullptr),
  136. fIsProcessing(false),
  137. fIsUiVisible(false),
  138. fAudioInBuffers(nullptr),
  139. fAudioOutBuffers(nullptr),
  140. fMidiEventCount(0)
  141. {
  142. carla_debug("NativePlugin::NativePlugin(%p, %i)", engine, id);
  143. carla_zeroStruct< ::MidiEvent>(fMidiEvents, MAX_MIDI_EVENTS*2);
  144. fHost.handle = this;
  145. fHost.ui_name = nullptr;
  146. fHost.get_buffer_size = carla_host_get_buffer_size;
  147. fHost.get_sample_rate = carla_host_get_sample_rate;
  148. fHost.get_time_info = carla_host_get_time_info;
  149. fHost.write_midi_event = carla_host_write_midi_event;
  150. fHost.ui_parameter_changed = carla_host_ui_parameter_changed;
  151. fHost.ui_custom_data_changed = carla_host_ui_custom_data_changed;
  152. fHost.ui_closed = carla_host_ui_closed;
  153. fHost.ui_open_file = carla_host_ui_open_file;
  154. fHost.ui_save_file = carla_host_ui_save_file;
  155. }
  156. ~NativePlugin() override
  157. {
  158. carla_debug("NativePlugin::~NativePlugin()");
  159. // close UI
  160. if (fHints & PLUGIN_HAS_GUI)
  161. {
  162. if (fIsUiVisible && fDescriptor != nullptr && fDescriptor->ui_show != nullptr && fHandle != nullptr)
  163. fDescriptor->ui_show(fHandle, false);
  164. }
  165. kData->singleMutex.lock();
  166. kData->masterMutex.lock();
  167. CARLA_ASSERT(! fIsProcessing);
  168. if (kData->active)
  169. {
  170. deactivate();
  171. kData->active = false;
  172. }
  173. if (fDescriptor != nullptr)
  174. {
  175. if (fDescriptor->cleanup != nullptr)
  176. {
  177. if (fHandle != nullptr)
  178. fDescriptor->cleanup(fHandle);
  179. if (fHandle2 != nullptr)
  180. fDescriptor->cleanup(fHandle2);
  181. }
  182. fHandle = nullptr;
  183. fHandle2 = nullptr;
  184. fDescriptor = nullptr;
  185. }
  186. if (fHost.ui_name != nullptr)
  187. {
  188. delete[] fHost.ui_name;
  189. fHost.ui_name = nullptr;
  190. }
  191. clearBuffers();
  192. }
  193. // -------------------------------------------------------------------
  194. // Information (base)
  195. PluginType type() const override
  196. {
  197. return PLUGIN_INTERNAL;
  198. }
  199. PluginCategory category() override
  200. {
  201. CARLA_ASSERT(fDescriptor != nullptr);
  202. return static_cast<PluginCategory>(fDescriptor->category);
  203. }
  204. // -------------------------------------------------------------------
  205. // Information (count)
  206. uint32_t midiInCount() const override
  207. {
  208. return fMidiIn.count;
  209. }
  210. uint32_t midiOutCount() const override
  211. {
  212. return fMidiOut.count;
  213. }
  214. uint32_t parameterScalePointCount(const uint32_t parameterId) const override
  215. {
  216. CARLA_ASSERT(fDescriptor != nullptr);
  217. CARLA_ASSERT(fHandle != nullptr);
  218. CARLA_ASSERT(parameterId < kData->param.count);
  219. if (fDescriptor->get_parameter_info != nullptr && parameterId < kData->param.count)
  220. {
  221. if (const Parameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId))
  222. return param->scalePointCount;
  223. }
  224. return 0;
  225. }
  226. // -------------------------------------------------------------------
  227. // Information (current data)
  228. // nothing
  229. // -------------------------------------------------------------------
  230. // Information (per-plugin data)
  231. unsigned int availableOptions() override
  232. {
  233. CARLA_ASSERT(fDescriptor != nullptr);
  234. if (fDescriptor == nullptr)
  235. return 0x0;
  236. unsigned int options = 0x0;
  237. options |= PLUGIN_OPTION_FIXED_BUFFER;
  238. options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  239. if (kData->engine->getProccessMode() != PROCESS_MODE_CONTINUOUS_RACK)
  240. {
  241. if (fOptions & PLUGIN_OPTION_FORCE_STEREO)
  242. options |= PLUGIN_OPTION_FORCE_STEREO;
  243. else if (kData->audioIn.count <= 1 && kData->audioOut.count <= 1 && (kData->audioIn.count != 0 || kData->audioOut.count != 0))
  244. options |= PLUGIN_OPTION_FORCE_STEREO;
  245. }
  246. if (fDescriptor->midiIns > 0)
  247. {
  248. options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  249. options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  250. options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  251. options |= PLUGIN_OPTION_SEND_PITCHBEND;
  252. options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  253. }
  254. return options;
  255. }
  256. float getParameterValue(const uint32_t parameterId) override
  257. {
  258. CARLA_ASSERT(fDescriptor != nullptr);
  259. CARLA_ASSERT(fHandle != nullptr);
  260. CARLA_ASSERT(parameterId < kData->param.count);
  261. if (fDescriptor->get_parameter_value != nullptr && parameterId < kData->param.count)
  262. return fDescriptor->get_parameter_value(fHandle, parameterId);
  263. return 0.0f;
  264. }
  265. float getParameterScalePointValue(const uint32_t parameterId, const uint32_t scalePointId) override
  266. {
  267. CARLA_ASSERT(fDescriptor != nullptr);
  268. CARLA_ASSERT(fHandle != nullptr);
  269. CARLA_ASSERT(parameterId < kData->param.count);
  270. CARLA_ASSERT(scalePointId < parameterScalePointCount(parameterId));
  271. if (fDescriptor->get_parameter_info != nullptr && parameterId < kData->param.count)
  272. {
  273. if (const Parameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId))
  274. {
  275. const ParameterScalePoint& scalePoint(param->scalePoints[scalePointId]);
  276. return scalePoint.value;
  277. }
  278. }
  279. return 0.0f;
  280. }
  281. void getLabel(char* const strBuf) override
  282. {
  283. CARLA_ASSERT(fDescriptor != nullptr);
  284. if (fDescriptor->label != nullptr)
  285. std::strncpy(strBuf, fDescriptor->label, STR_MAX);
  286. else
  287. CarlaPlugin::getLabel(strBuf);
  288. }
  289. void getMaker(char* const strBuf) override
  290. {
  291. CARLA_ASSERT(fDescriptor != nullptr);
  292. if (fDescriptor->maker != nullptr)
  293. std::strncpy(strBuf, fDescriptor->maker, STR_MAX);
  294. else
  295. CarlaPlugin::getMaker(strBuf);
  296. }
  297. void getCopyright(char* const strBuf) override
  298. {
  299. CARLA_ASSERT(fDescriptor != nullptr);
  300. if (fDescriptor->copyright != nullptr)
  301. std::strncpy(strBuf, fDescriptor->copyright, STR_MAX);
  302. else
  303. CarlaPlugin::getCopyright(strBuf);
  304. }
  305. void getRealName(char* const strBuf) override
  306. {
  307. CARLA_ASSERT(fDescriptor != nullptr);
  308. if (fDescriptor->name != nullptr)
  309. std::strncpy(strBuf, fDescriptor->name, STR_MAX);
  310. else
  311. CarlaPlugin::getRealName(strBuf);
  312. }
  313. void getParameterName(const uint32_t parameterId, char* const strBuf) override
  314. {
  315. CARLA_ASSERT(fDescriptor != nullptr);
  316. CARLA_ASSERT(fHandle != nullptr);
  317. CARLA_ASSERT(parameterId < kData->param.count);
  318. if (fDescriptor->get_parameter_info != nullptr && parameterId < kData->param.count)
  319. {
  320. const Parameter* const param(fDescriptor->get_parameter_info(fHandle, parameterId));
  321. if (param != nullptr && param->name != nullptr)
  322. {
  323. std::strncpy(strBuf, param->name, STR_MAX);
  324. return;
  325. }
  326. }
  327. CarlaPlugin::getParameterName(parameterId, strBuf);
  328. }
  329. void getParameterText(const uint32_t parameterId, char* const strBuf) override
  330. {
  331. CARLA_ASSERT(fDescriptor != nullptr);
  332. CARLA_ASSERT(fHandle != nullptr);
  333. CARLA_ASSERT(parameterId < kData->param.count);
  334. if (fDescriptor->get_parameter_text != nullptr && parameterId < kData->param.count)
  335. {
  336. if (const char* const text = fDescriptor->get_parameter_text(fHandle, parameterId))
  337. {
  338. std::strncpy(strBuf, text, STR_MAX);
  339. return;
  340. }
  341. }
  342. CarlaPlugin::getParameterText(parameterId, strBuf);
  343. }
  344. void getParameterUnit(const uint32_t parameterId, char* const strBuf) override
  345. {
  346. CARLA_ASSERT(fDescriptor != nullptr);
  347. CARLA_ASSERT(fHandle != nullptr);
  348. CARLA_ASSERT(parameterId < kData->param.count);
  349. if (fDescriptor->get_parameter_info != nullptr && parameterId < kData->param.count)
  350. {
  351. const Parameter* const param(fDescriptor->get_parameter_info(fHandle, parameterId));
  352. if (param != nullptr && param->unit != nullptr)
  353. {
  354. std::strncpy(strBuf, param->unit, STR_MAX);
  355. return;
  356. }
  357. }
  358. CarlaPlugin::getParameterUnit(parameterId, strBuf);
  359. }
  360. void getParameterScalePointLabel(const uint32_t parameterId, const uint32_t scalePointId, char* const strBuf) override
  361. {
  362. CARLA_ASSERT(fDescriptor != nullptr);
  363. CARLA_ASSERT(fHandle != nullptr);
  364. CARLA_ASSERT(parameterId < kData->param.count);
  365. CARLA_ASSERT(scalePointId < parameterScalePointCount(parameterId));
  366. if (fDescriptor->get_parameter_info != nullptr && parameterId < kData->param.count)
  367. {
  368. if (const Parameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId))
  369. {
  370. const ParameterScalePoint& scalePoint(param->scalePoints[scalePointId]);
  371. if (scalePoint.label != nullptr)
  372. {
  373. std::strncpy(strBuf, scalePoint.label, STR_MAX);
  374. return;
  375. }
  376. }
  377. }
  378. CarlaPlugin::getParameterScalePointLabel(parameterId, scalePointId, strBuf);
  379. }
  380. // -------------------------------------------------------------------
  381. // Set data (state)
  382. void prepareForSave() override
  383. {
  384. CARLA_ASSERT(fDescriptor != nullptr);
  385. CARLA_ASSERT(fHandle != nullptr);
  386. if (fDescriptor->get_state == nullptr)
  387. return;
  388. if ((fDescriptor->hints & ::PLUGIN_USES_STATE) == 0)
  389. return;
  390. if (char* data = fDescriptor->get_state(fHandle))
  391. {
  392. CarlaPlugin::setCustomData(CUSTOM_DATA_CHUNK, "State", data, false);
  393. std::free(data);
  394. }
  395. }
  396. // -------------------------------------------------------------------
  397. // Set data (internal stuff)
  398. void setName(const char* const newName) override
  399. {
  400. char uiName[std::strlen(newName)+6+1];
  401. std::strcpy(uiName, newName);
  402. std::strcat(uiName, " (GUI)");
  403. if (fHost.ui_name != nullptr)
  404. delete[] fHost.ui_name;
  405. fHost.ui_name = carla_strdup(uiName);
  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)
  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)
  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. // TODO!
  958. if (fOptions & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  959. {
  960. for (k=0, i=MAX_MIDI_CHANNELS; k < MAX_MIDI_CHANNELS; ++k)
  961. {
  962. fMidiEvents[k].data[0] = MIDI_STATUS_CONTROL_CHANGE + k;
  963. fMidiEvents[k].data[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  964. fMidiEvents[k].size = 2;
  965. fMidiEvents[k+i].data[0] = MIDI_STATUS_CONTROL_CHANGE + k;
  966. fMidiEvents[k+i].data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  967. fMidiEvents[k+i].size = 2;
  968. }
  969. fMidiEventCount = MAX_MIDI_CHANNELS*2;
  970. }
  971. else
  972. {
  973. }
  974. kData->needsReset = false;
  975. }
  976. CARLA_PROCESS_CONTINUE_CHECK;
  977. // --------------------------------------------------------------------------------------------------------
  978. // Set TimeInfo
  979. const EngineTimeInfo& timeInfo(kData->engine->getTimeInfo());
  980. fTimeInfo.playing = timeInfo.playing;
  981. fTimeInfo.frame = timeInfo.frame;
  982. fTimeInfo.usecs = timeInfo.usecs;
  983. if (timeInfo.valid & EngineTimeInfo::ValidBBT)
  984. {
  985. fTimeInfo.bbt.valid = true;
  986. fTimeInfo.bbt.bar = timeInfo.bbt.bar;
  987. fTimeInfo.bbt.beat = timeInfo.bbt.beat;
  988. fTimeInfo.bbt.tick = timeInfo.bbt.tick;
  989. fTimeInfo.bbt.barStartTick = timeInfo.bbt.barStartTick;
  990. fTimeInfo.bbt.beatsPerBar = timeInfo.bbt.beatsPerBar;
  991. fTimeInfo.bbt.beatType = timeInfo.bbt.beatType;
  992. fTimeInfo.bbt.ticksPerBeat = timeInfo.bbt.ticksPerBeat;
  993. fTimeInfo.bbt.beatsPerMinute = timeInfo.bbt.beatsPerMinute;
  994. }
  995. else
  996. fTimeInfo.bbt.valid = false;
  997. CARLA_PROCESS_CONTINUE_CHECK;
  998. // --------------------------------------------------------------------------------------------------------
  999. // Event Input and Processing
  1000. if (kData->event.portIn != nullptr)
  1001. {
  1002. // ----------------------------------------------------------------------------------------------------
  1003. // MIDI Input (External)
  1004. if (kData->extNotes.mutex.tryLock())
  1005. {
  1006. while (fMidiEventCount < MAX_MIDI_EVENTS*2 && ! kData->extNotes.data.isEmpty())
  1007. {
  1008. const ExternalMidiNote& note(kData->extNotes.data.getFirst(true));
  1009. CARLA_ASSERT(note.channel >= 0 && note.channel < MAX_MIDI_CHANNELS);
  1010. fMidiEvents[fMidiEventCount].data[0] = (note.velo > 0) ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF;
  1011. fMidiEvents[fMidiEventCount].data[0] += note.channel;
  1012. fMidiEvents[fMidiEventCount].data[1] = note.note;
  1013. fMidiEvents[fMidiEventCount].data[2] = note.velo;
  1014. fMidiEventCount += 1;
  1015. }
  1016. kData->extNotes.mutex.unlock();
  1017. } // End of MIDI Input (External)
  1018. // ----------------------------------------------------------------------------------------------------
  1019. // Event Input (System)
  1020. bool allNotesOffSent = false;
  1021. bool sampleAccurate = (fOptions & PLUGIN_OPTION_FIXED_BUFFER) == 0;
  1022. uint32_t time, nEvents = kData->event.portIn->getEventCount();
  1023. uint32_t startTime = 0;
  1024. uint32_t timeOffset = 0;
  1025. uint32_t nextBankId = 0;
  1026. if (kData->midiprog.current >= 0 && kData->midiprog.count > 0)
  1027. nextBankId = kData->midiprog.data[kData->midiprog.current].bank;
  1028. for (i=0; i < nEvents; ++i)
  1029. {
  1030. const EngineEvent& event = kData->event.portIn->getEvent(i);
  1031. time = event.time;
  1032. if (time >= frames)
  1033. continue;
  1034. CARLA_ASSERT_INT2(time >= timeOffset, time, timeOffset);
  1035. if (time > timeOffset && sampleAccurate)
  1036. {
  1037. if (processSingle(inBuffer, outBuffer, time - timeOffset, timeOffset))
  1038. {
  1039. startTime = 0;
  1040. timeOffset = time;
  1041. nextBankId = 0;
  1042. if (kData->midiprog.current >= 0 && kData->midiprog.count > 0)
  1043. nextBankId = kData->midiprog.data[kData->midiprog.current].bank;
  1044. else
  1045. nextBankId = 0;
  1046. if (fMidiEventCount > 0)
  1047. {
  1048. carla_zeroStruct< ::MidiEvent>(fMidiEvents, fMidiEventCount);
  1049. fMidiEventCount = 0;
  1050. }
  1051. }
  1052. else
  1053. startTime += timeOffset;
  1054. }
  1055. // Control change
  1056. switch (event.type)
  1057. {
  1058. case kEngineEventTypeNull:
  1059. break;
  1060. case kEngineEventTypeControl:
  1061. {
  1062. const EngineControlEvent& ctrlEvent = event.ctrl;
  1063. switch (ctrlEvent.type)
  1064. {
  1065. case kEngineControlEventTypeNull:
  1066. break;
  1067. case kEngineControlEventTypeParameter:
  1068. {
  1069. // Control backend stuff
  1070. if (event.channel == kData->ctrlChannel)
  1071. {
  1072. float value;
  1073. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (fHints & PLUGIN_CAN_DRYWET) > 0)
  1074. {
  1075. value = ctrlEvent.value;
  1076. setDryWet(value, false, false);
  1077. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, value);
  1078. continue;
  1079. }
  1080. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (fHints & PLUGIN_CAN_VOLUME) > 0)
  1081. {
  1082. value = ctrlEvent.value*127.0f/100.0f;
  1083. setVolume(value, false, false);
  1084. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
  1085. continue;
  1086. }
  1087. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (fHints & PLUGIN_CAN_BALANCE) > 0)
  1088. {
  1089. float left, right;
  1090. value = ctrlEvent.value/0.5f - 1.0f;
  1091. if (value < 0.0f)
  1092. {
  1093. left = -1.0f;
  1094. right = (value*2.0f)+1.0f;
  1095. }
  1096. else if (value > 0.0f)
  1097. {
  1098. left = (value*2.0f)-1.0f;
  1099. right = 1.0f;
  1100. }
  1101. else
  1102. {
  1103. left = -1.0f;
  1104. right = 1.0f;
  1105. }
  1106. setBalanceLeft(left, false, false);
  1107. setBalanceRight(right, false, false);
  1108. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  1109. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  1110. continue;
  1111. }
  1112. }
  1113. // Control plugin parameters
  1114. for (k=0; k < kData->param.count; ++k)
  1115. {
  1116. if (kData->param.data[k].midiChannel != event.channel)
  1117. continue;
  1118. if (kData->param.data[k].midiCC != ctrlEvent.param)
  1119. continue;
  1120. if (kData->param.data[k].type != PARAMETER_INPUT)
  1121. continue;
  1122. if ((kData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  1123. continue;
  1124. float value;
  1125. if (kData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  1126. {
  1127. value = (ctrlEvent.value < 0.5f) ? kData->param.ranges[k].min : kData->param.ranges[k].max;
  1128. }
  1129. else
  1130. {
  1131. value = kData->param.ranges[i].unnormalizeValue(ctrlEvent.value);
  1132. if (kData->param.data[k].hints & PARAMETER_IS_INTEGER)
  1133. value = std::rint(value);
  1134. }
  1135. setParameterValue(k, value, false, false, false);
  1136. postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, value);
  1137. }
  1138. break;
  1139. }
  1140. case kEngineControlEventTypeMidiBank:
  1141. if (event.channel == kData->ctrlChannel && (fOptions & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  1142. nextBankId = ctrlEvent.param;
  1143. break;
  1144. case kEngineControlEventTypeMidiProgram:
  1145. if (event.channel == kData->ctrlChannel && (fOptions & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  1146. {
  1147. const uint32_t nextProgramId = ctrlEvent.param;
  1148. for (k=0; k < kData->midiprog.count; ++k)
  1149. {
  1150. if (kData->midiprog.data[k].bank == nextBankId && kData->midiprog.data[k].program == nextProgramId)
  1151. {
  1152. setMidiProgram(k, false, false, false);
  1153. postponeRtEvent(kPluginPostRtEventMidiProgramChange, k, 0, 0.0f);
  1154. break;
  1155. }
  1156. }
  1157. }
  1158. break;
  1159. case kEngineControlEventTypeAllSoundOff:
  1160. if (event.channel == kData->ctrlChannel)
  1161. {
  1162. if (! allNotesOffSent)
  1163. {
  1164. sendMidiAllNotesOff();
  1165. allNotesOffSent = true;
  1166. }
  1167. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_ACTIVE, 0, 0.0f);
  1168. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_ACTIVE, 0, 1.0f);
  1169. }
  1170. if (fMidiEventCount >= MAX_MIDI_EVENTS*2)
  1171. continue;
  1172. if (fOptions & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1173. {
  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].data[3] = 0;
  1180. fMidiEvents[fMidiEventCount].size = 2;
  1181. fMidiEventCount += 1;
  1182. }
  1183. break;
  1184. case kEngineControlEventTypeAllNotesOff:
  1185. if (event.channel == kData->ctrlChannel)
  1186. {
  1187. if (! allNotesOffSent)
  1188. {
  1189. allNotesOffSent = true;
  1190. sendMidiAllNotesOff();
  1191. }
  1192. }
  1193. if (fMidiEventCount >= MAX_MIDI_EVENTS*2)
  1194. continue;
  1195. if (fOptions & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1196. {
  1197. fMidiEvents[fMidiEventCount].port = 0;
  1198. fMidiEvents[fMidiEventCount].time = sampleAccurate ? startTime : time;
  1199. fMidiEvents[fMidiEventCount].data[0] = MIDI_STATUS_CONTROL_CHANGE + event.channel;
  1200. fMidiEvents[fMidiEventCount].data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  1201. fMidiEvents[fMidiEventCount].data[2] = 0;
  1202. fMidiEvents[fMidiEventCount].data[3] = 0;
  1203. fMidiEvents[fMidiEventCount].size = 2;
  1204. fMidiEventCount += 1;
  1205. }
  1206. break;
  1207. }
  1208. break;
  1209. }
  1210. case kEngineEventTypeMidi:
  1211. {
  1212. if (fMidiEventCount >= MAX_MIDI_EVENTS*2)
  1213. continue;
  1214. const EngineMidiEvent& midiEvent = event.midi;
  1215. uint8_t status = MIDI_GET_STATUS_FROM_DATA(midiEvent.data);
  1216. uint8_t channel = event.channel;
  1217. if (MIDI_IS_STATUS_AFTERTOUCH(status) && (fOptions & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) == 0)
  1218. continue;
  1219. if (MIDI_IS_STATUS_CONTROL_CHANGE(status) && (fOptions & PLUGIN_OPTION_SEND_CONTROL_CHANGES) == 0)
  1220. continue;
  1221. if (MIDI_IS_STATUS_POLYPHONIC_AFTERTOUCH(status) && (fOptions & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) == 0)
  1222. continue;
  1223. if (MIDI_IS_STATUS_PITCH_WHEEL_CONTROL(status) && (fOptions & PLUGIN_OPTION_SEND_PITCHBEND) == 0)
  1224. continue;
  1225. // Fix bad note-off
  1226. if (status == MIDI_STATUS_NOTE_ON && midiEvent.data[2] == 0)
  1227. status -= 0x10;
  1228. fMidiEvents[fMidiEventCount].port = 0;
  1229. fMidiEvents[fMidiEventCount].time = sampleAccurate ? startTime : time;
  1230. fMidiEvents[fMidiEventCount].size = midiEvent.size;
  1231. fMidiEvents[fMidiEventCount].data[0] = status + channel;
  1232. fMidiEvents[fMidiEventCount].data[1] = midiEvent.data[1];
  1233. fMidiEvents[fMidiEventCount].data[2] = midiEvent.data[2];
  1234. fMidiEvents[fMidiEventCount].data[3] = midiEvent.data[3];
  1235. fMidiEventCount += 1;
  1236. if (status == MIDI_STATUS_NOTE_ON)
  1237. postponeRtEvent(kPluginPostRtEventNoteOn, channel, midiEvent.data[1], midiEvent.data[2]);
  1238. else if (status == MIDI_STATUS_NOTE_OFF)
  1239. postponeRtEvent(kPluginPostRtEventNoteOff, channel, midiEvent.data[1], 0.0f);
  1240. break;
  1241. }
  1242. }
  1243. }
  1244. kData->postRtEvents.trySplice();
  1245. if (frames > timeOffset)
  1246. processSingle(inBuffer, outBuffer, frames - timeOffset, timeOffset);
  1247. } // End of Event Input and Processing
  1248. // --------------------------------------------------------------------------------------------------------
  1249. // Plugin processing (no events)
  1250. else
  1251. {
  1252. processSingle(inBuffer, outBuffer, frames, 0);
  1253. } // End of Plugin processing (no events)
  1254. CARLA_PROCESS_CONTINUE_CHECK;
  1255. // --------------------------------------------------------------------------------------------------------
  1256. // Control and MIDI Output
  1257. if (fMidiOut.count > 0 || kData->event.portOut != nullptr)
  1258. {
  1259. float value, curValue;
  1260. for (k=0; k < kData->param.count; ++k)
  1261. {
  1262. if (kData->param.data[k].type != PARAMETER_OUTPUT)
  1263. continue;
  1264. curValue = fDescriptor->get_parameter_value(fHandle, k);
  1265. kData->param.ranges[k].fixValue(curValue);
  1266. if (kData->param.data[k].midiCC > 0)
  1267. {
  1268. value = kData->param.ranges[k].normalizeValue(curValue);
  1269. kData->event.portOut->writeControlEvent(0, kData->param.data[k].midiChannel, kEngineControlEventTypeParameter, kData->param.data[k].midiCC, value);
  1270. }
  1271. }
  1272. // reverse lookup MIDI events
  1273. for (k = (MAX_MIDI_EVENTS*2)-1; k >= fMidiEventCount; --k)
  1274. {
  1275. if (fMidiEvents[k].data[0] == 0)
  1276. break;
  1277. const uint8_t channel = MIDI_GET_CHANNEL_FROM_DATA(fMidiEvents[k].data);
  1278. const uint8_t port = fMidiEvents[k].port;
  1279. if (kData->event.portOut != nullptr)
  1280. kData->event.portOut->writeMidiEvent(fMidiEvents[k].time, channel, port, fMidiEvents[k].data, fMidiEvents[k].size);
  1281. else if (port < fMidiOut.count)
  1282. fMidiOut.ports[port]->writeMidiEvent(fMidiEvents[k].time, channel, port, fMidiEvents[k].data, fMidiEvents[k].size);
  1283. }
  1284. } // End of Control and MIDI Output
  1285. }
  1286. bool processSingle(float** const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t timeOffset)
  1287. {
  1288. CARLA_ASSERT(frames > 0);
  1289. if (frames == 0)
  1290. return false;
  1291. if (kData->audioIn.count > 0)
  1292. {
  1293. CARLA_ASSERT(inBuffer != nullptr);
  1294. if (inBuffer == nullptr)
  1295. return false;
  1296. }
  1297. if (kData->audioOut.count > 0)
  1298. {
  1299. CARLA_ASSERT(outBuffer != nullptr);
  1300. if (outBuffer == nullptr)
  1301. return false;
  1302. }
  1303. uint32_t i, k;
  1304. // --------------------------------------------------------------------------------------------------------
  1305. // Try lock, silence otherwise
  1306. if (kData->engine->isOffline())
  1307. {
  1308. kData->singleMutex.lock();
  1309. }
  1310. else if (! kData->singleMutex.tryLock())
  1311. {
  1312. for (i=0; i < kData->audioOut.count; ++i)
  1313. {
  1314. for (k=0; k < frames; ++k)
  1315. outBuffer[i][k+timeOffset] = 0.0f;
  1316. }
  1317. return false;
  1318. }
  1319. // --------------------------------------------------------------------------------------------------------
  1320. // Reset audio buffers
  1321. for (i=0; i < kData->audioIn.count; ++i)
  1322. carla_copyFloat(fAudioInBuffers[i], inBuffer[i]+timeOffset, frames);
  1323. for (i=0; i < kData->audioOut.count; ++i)
  1324. carla_zeroFloat(fAudioOutBuffers[i], frames);
  1325. // --------------------------------------------------------------------------------------------------------
  1326. // Run plugin
  1327. fIsProcessing = true;
  1328. if (fHandle2 == nullptr)
  1329. {
  1330. fDescriptor->process(fHandle, fAudioInBuffers, fAudioOutBuffers, frames, fMidiEventCount, fMidiEvents);
  1331. }
  1332. else
  1333. {
  1334. fDescriptor->process(fHandle,
  1335. (kData->audioIn.count > 0) ? &fAudioInBuffers[0] : nullptr,
  1336. (kData->audioOut.count > 0) ? &fAudioOutBuffers[0] : nullptr,
  1337. frames, fMidiEventCount, fMidiEvents);
  1338. fDescriptor->process(fHandle2,
  1339. (kData->audioIn.count > 0) ? &fAudioInBuffers[1] : nullptr,
  1340. (kData->audioOut.count > 0) ? &fAudioOutBuffers[1] : nullptr,
  1341. frames, fMidiEventCount, fMidiEvents);
  1342. }
  1343. fIsProcessing = false;
  1344. fTimeInfo.frame += frames;
  1345. // --------------------------------------------------------------------------------------------------------
  1346. // Post-processing (dry/wet, volume and balance)
  1347. {
  1348. const bool doDryWet = (fHints & PLUGIN_CAN_DRYWET) != 0 && kData->postProc.dryWet != 1.0f;
  1349. const bool doBalance = (fHints & PLUGIN_CAN_BALANCE) != 0 && (kData->postProc.balanceLeft != -1.0f || kData->postProc.balanceRight != 1.0f);
  1350. bool isPair;
  1351. float bufValue, oldBufLeft[doBalance ? frames : 1];
  1352. for (i=0; i < kData->audioOut.count; ++i)
  1353. {
  1354. // Dry/Wet
  1355. if (doDryWet)
  1356. {
  1357. for (k=0; k < frames; ++k)
  1358. {
  1359. bufValue = fAudioInBuffers[(kData->audioIn.count == 1) ? 0 : i][k];
  1360. fAudioOutBuffers[i][k] = (fAudioOutBuffers[i][k] * kData->postProc.dryWet) + (bufValue * (1.0f - kData->postProc.dryWet));
  1361. }
  1362. }
  1363. // Balance
  1364. if (doBalance)
  1365. {
  1366. isPair = (i % 2 == 0);
  1367. if (isPair)
  1368. {
  1369. CARLA_ASSERT(i+1 < kData->audioOut.count);
  1370. carla_copyFloat(oldBufLeft, fAudioOutBuffers[i], frames);
  1371. }
  1372. float balRangeL = (kData->postProc.balanceLeft + 1.0f)/2.0f;
  1373. float balRangeR = (kData->postProc.balanceRight + 1.0f)/2.0f;
  1374. for (k=0; k < frames; ++k)
  1375. {
  1376. if (isPair)
  1377. {
  1378. // left
  1379. fAudioOutBuffers[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  1380. fAudioOutBuffers[i][k] += fAudioOutBuffers[i+1][k] * (1.0f - balRangeR);
  1381. }
  1382. else
  1383. {
  1384. // right
  1385. fAudioOutBuffers[i][k] = fAudioOutBuffers[i][k] * balRangeR;
  1386. fAudioOutBuffers[i][k] += oldBufLeft[k] * balRangeL;
  1387. }
  1388. }
  1389. }
  1390. // Volume (and buffer copy)
  1391. {
  1392. for (k=0; k < frames; ++k)
  1393. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k] * kData->postProc.volume;
  1394. }
  1395. }
  1396. } // End of Post-processing
  1397. // --------------------------------------------------------------------------------------------------------
  1398. kData->singleMutex.unlock();
  1399. return true;
  1400. }
  1401. void bufferSizeChanged(const uint32_t newBufferSize) override
  1402. {
  1403. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  1404. carla_debug("NativePlugin::bufferSizeChanged(%i)", newBufferSize);
  1405. for (uint32_t i=0; i < kData->audioIn.count; ++i)
  1406. {
  1407. if (fAudioInBuffers[i] != nullptr)
  1408. delete[] fAudioInBuffers[i];
  1409. fAudioInBuffers[i] = new float[newBufferSize];
  1410. }
  1411. for (uint32_t i=0; i < kData->audioOut.count; ++i)
  1412. {
  1413. if (fAudioOutBuffers[i] != nullptr)
  1414. delete[] fAudioOutBuffers[i];
  1415. fAudioOutBuffers[i] = new float[newBufferSize];
  1416. }
  1417. }
  1418. // -------------------------------------------------------------------
  1419. // Plugin buffers
  1420. void initBuffers() override
  1421. {
  1422. fMidiIn.initBuffers(kData->engine);
  1423. fMidiOut.initBuffers(kData->engine);
  1424. CarlaPlugin::initBuffers();
  1425. }
  1426. void clearBuffers() override
  1427. {
  1428. carla_debug("NativePlugin::clearBuffers() - start");
  1429. if (fAudioInBuffers != nullptr)
  1430. {
  1431. for (uint32_t i=0; i < kData->audioIn.count; ++i)
  1432. {
  1433. if (fAudioInBuffers[i] != nullptr)
  1434. {
  1435. delete[] fAudioInBuffers[i];
  1436. fAudioInBuffers[i] = nullptr;
  1437. }
  1438. }
  1439. delete[] fAudioInBuffers;
  1440. fAudioInBuffers = nullptr;
  1441. }
  1442. if (fAudioOutBuffers != nullptr)
  1443. {
  1444. for (uint32_t i=0; i < kData->audioOut.count; ++i)
  1445. {
  1446. if (fAudioOutBuffers[i] != nullptr)
  1447. {
  1448. delete[] fAudioOutBuffers[i];
  1449. fAudioOutBuffers[i] = nullptr;
  1450. }
  1451. }
  1452. delete[] fAudioOutBuffers;
  1453. fAudioOutBuffers = nullptr;
  1454. }
  1455. fMidiIn.clear();
  1456. fMidiOut.clear();
  1457. CarlaPlugin::clearBuffers();
  1458. carla_debug("NativePlugin::clearBuffers() - end");
  1459. }
  1460. // -------------------------------------------------------------------
  1461. // Post-poned UI Stuff
  1462. void uiParameterChange(const uint32_t index, const float value) override
  1463. {
  1464. CARLA_ASSERT(fDescriptor != nullptr);
  1465. CARLA_ASSERT(fHandle != nullptr);
  1466. CARLA_ASSERT(index < kData->param.count);
  1467. if (! fIsUiVisible)
  1468. return;
  1469. if (fDescriptor == nullptr || fHandle == nullptr)
  1470. return;
  1471. if (index >= kData->param.count)
  1472. return;
  1473. if (fDescriptor->ui_set_parameter_value != nullptr)
  1474. fDescriptor->ui_set_parameter_value(fHandle, index, value);
  1475. }
  1476. void uiMidiProgramChange(const uint32_t index) override
  1477. {
  1478. CARLA_ASSERT(fDescriptor != nullptr);
  1479. CARLA_ASSERT(fHandle != nullptr);
  1480. CARLA_ASSERT(index < kData->midiprog.count);
  1481. if (! fIsUiVisible)
  1482. return;
  1483. if (fDescriptor == nullptr || fHandle == nullptr)
  1484. return;
  1485. if (index >= kData->midiprog.count)
  1486. return;
  1487. if (fDescriptor->ui_set_midi_program != nullptr)
  1488. fDescriptor->ui_set_midi_program(fHandle, kData->midiprog.data[index].bank, kData->midiprog.data[index].program);
  1489. }
  1490. void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) override
  1491. {
  1492. CARLA_ASSERT(fDescriptor != nullptr);
  1493. CARLA_ASSERT(fHandle != nullptr);
  1494. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  1495. CARLA_ASSERT(note < MAX_MIDI_NOTE);
  1496. CARLA_ASSERT(velo > 0 && velo < MAX_MIDI_VALUE);
  1497. if (! fIsUiVisible)
  1498. return;
  1499. if (fDescriptor == nullptr || fHandle == nullptr)
  1500. return;
  1501. if (channel >= MAX_MIDI_CHANNELS)
  1502. return;
  1503. if (note >= MAX_MIDI_NOTE)
  1504. return;
  1505. if (velo >= MAX_MIDI_VALUE)
  1506. return;
  1507. // TODO
  1508. }
  1509. void uiNoteOff(const uint8_t channel, const uint8_t note) 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. if (! fIsUiVisible)
  1516. return;
  1517. if (fDescriptor == nullptr || fHandle == nullptr)
  1518. return;
  1519. if (channel >= MAX_MIDI_CHANNELS)
  1520. return;
  1521. if (note >= MAX_MIDI_NOTE)
  1522. return;
  1523. // TODO
  1524. }
  1525. // -------------------------------------------------------------------
  1526. protected:
  1527. uint32_t handleGetBufferSize()
  1528. {
  1529. return kData->engine->getBufferSize();
  1530. }
  1531. double handleGetSampleRate()
  1532. {
  1533. return kData->engine->getSampleRate();
  1534. }
  1535. const ::TimeInfo* handleGetTimeInfo()
  1536. {
  1537. CARLA_ASSERT(fIsProcessing);
  1538. return &fTimeInfo;
  1539. }
  1540. bool handleWriteMidiEvent(const ::MidiEvent* const event)
  1541. {
  1542. CARLA_ASSERT(fEnabled);
  1543. CARLA_ASSERT(fIsProcessing);
  1544. CARLA_ASSERT(fMidiOut.count > 0 || kData->event.portOut != nullptr);
  1545. CARLA_ASSERT(event != nullptr);
  1546. CARLA_ASSERT(event->data[0] != 0);
  1547. if (! fEnabled)
  1548. return false;
  1549. if (fMidiOut.count == 0)
  1550. return false;
  1551. if (event == nullptr)
  1552. return false;
  1553. if (event->data[0] == 0)
  1554. return false;
  1555. if (! fIsProcessing)
  1556. {
  1557. carla_stderr2("NativePlugin::handleWriteMidiEvent(%p) - received MIDI out event outside audio thread, ignoring", event);
  1558. return false;
  1559. }
  1560. if (fMidiEventCount >= MAX_MIDI_EVENTS*2)
  1561. return false;
  1562. // reverse-find first free event, and put it there
  1563. for (uint32_t i=(MAX_MIDI_EVENTS*2)-1; i >= fMidiEventCount; --i)
  1564. {
  1565. if (fMidiEvents[i].data[0] == 0)
  1566. {
  1567. std::memcpy(&fMidiEvents[i], event, sizeof(::MidiEvent));
  1568. break;
  1569. }
  1570. }
  1571. return true;
  1572. }
  1573. void handleUiParameterChanged(const uint32_t index, const float value)
  1574. {
  1575. setParameterValue(index, value, false, true, true);
  1576. }
  1577. void handleUiCustomDataChanged(const char* const key, const char* const value)
  1578. {
  1579. setCustomData(CUSTOM_DATA_STRING, key, value, false);
  1580. }
  1581. void handleUiClosed()
  1582. {
  1583. kData->engine->callback(CALLBACK_SHOW_GUI, fId, 0, 0, 0.0f, nullptr);
  1584. fIsUiVisible = false;
  1585. }
  1586. const char* handleUiOpenFile(const bool isDir, const char* const title, const char* const filter)
  1587. {
  1588. static CarlaString retStr;
  1589. QFileDialog::Options options(isDir ? QFileDialog::ShowDirsOnly : 0x0);
  1590. retStr = QFileDialog::getOpenFileName(nullptr, title, "", filter, nullptr, options).toUtf8().constData();
  1591. return retStr.isNotEmpty() ? (const char*)retStr : nullptr;
  1592. }
  1593. const char* handleUiSaveFile(const bool isDir, const char* const title, const char* const filter)
  1594. {
  1595. static CarlaString retStr;
  1596. QFileDialog::Options options(isDir ? QFileDialog::ShowDirsOnly : 0x0);
  1597. retStr = QFileDialog::getSaveFileName(nullptr, title, "", filter, nullptr, options).toUtf8().constData();
  1598. return retStr.isNotEmpty() ? (const char*)retStr : nullptr;
  1599. }
  1600. // -------------------------------------------------------------------
  1601. public:
  1602. static size_t getPluginCount()
  1603. {
  1604. return sPluginDescriptors.count();
  1605. }
  1606. static const PluginDescriptor* getPluginDescriptor(const size_t index)
  1607. {
  1608. CARLA_ASSERT(index < sPluginDescriptors.count());
  1609. if (index < sPluginDescriptors.count())
  1610. return sPluginDescriptors.getAt(index);
  1611. return nullptr;
  1612. }
  1613. static void registerPlugin(const PluginDescriptor* desc)
  1614. {
  1615. sPluginDescriptors.append(desc);
  1616. }
  1617. // -------------------------------------------------------------------
  1618. bool init(const char* const name, const char* const label)
  1619. {
  1620. CARLA_ASSERT(kData->engine != nullptr);
  1621. CARLA_ASSERT(kData->client == nullptr);
  1622. CARLA_ASSERT(label != nullptr);
  1623. // ---------------------------------------------------------------
  1624. // first checks
  1625. if (kData->engine == nullptr)
  1626. {
  1627. return false;
  1628. }
  1629. if (kData->client != nullptr)
  1630. {
  1631. kData->engine->setLastError("Plugin client is already registered");
  1632. return false;
  1633. }
  1634. if (label == nullptr)
  1635. {
  1636. kData->engine->setLastError("null label");
  1637. return false;
  1638. }
  1639. // ---------------------------------------------------------------
  1640. // get descriptor that matches label
  1641. for (auto it = sPluginDescriptors.begin(); it.valid(); it.next())
  1642. {
  1643. fDescriptor = *it;
  1644. CARLA_ASSERT(fDescriptor != nullptr);
  1645. if (fDescriptor == nullptr)
  1646. break;
  1647. if (fDescriptor->label != nullptr && std::strcmp(fDescriptor->label, label) == 0)
  1648. break;
  1649. fDescriptor = nullptr;
  1650. }
  1651. if (fDescriptor == nullptr)
  1652. {
  1653. kData->engine->setLastError("Invalid internal plugin");
  1654. return false;
  1655. }
  1656. // ---------------------------------------------------------------
  1657. // get info
  1658. if (name != nullptr)
  1659. fName = kData->engine->getUniquePluginName(name);
  1660. else if (fDescriptor->name != nullptr)
  1661. fName = kData->engine->getUniquePluginName(fDescriptor->name);
  1662. else
  1663. fName = kData->engine->getUniquePluginName(label);
  1664. {
  1665. CARLA_ASSERT(fHost.ui_name == nullptr);
  1666. char uiName[fName.length()+6+1];
  1667. std::strcpy(uiName, (const char*)fName);
  1668. std::strcat(uiName, " (GUI)");
  1669. fHost.ui_name = carla_strdup(uiName);
  1670. }
  1671. // ---------------------------------------------------------------
  1672. // register client
  1673. kData->client = kData->engine->addClient(this);
  1674. if (kData->client == nullptr || ! kData->client->isOk())
  1675. {
  1676. kData->engine->setLastError("Failed to register plugin client");
  1677. return false;
  1678. }
  1679. // ---------------------------------------------------------------
  1680. // initialize plugin
  1681. fHandle = fDescriptor->instantiate(fDescriptor, &fHost);
  1682. if (fHandle == nullptr)
  1683. {
  1684. kData->engine->setLastError("Plugin failed to initialize");
  1685. return false;
  1686. }
  1687. // ---------------------------------------------------------------
  1688. // load plugin settings
  1689. {
  1690. // set default options
  1691. fOptions = 0x0;
  1692. fOptions |= PLUGIN_OPTION_FIXED_BUFFER;
  1693. fOptions |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  1694. if (kData->engine->getOptions().forceStereo)
  1695. fOptions |= PLUGIN_OPTION_FORCE_STEREO;
  1696. if (fDescriptor->midiIns > 0)
  1697. {
  1698. fOptions |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  1699. fOptions |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  1700. fOptions |= PLUGIN_OPTION_SEND_PITCHBEND;
  1701. fOptions |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  1702. }
  1703. // load settings
  1704. kData->idStr = "Native/";
  1705. kData->idStr += label;
  1706. fOptions = kData->loadSettings(fOptions, availableOptions());
  1707. }
  1708. return true;
  1709. }
  1710. class ScopedInitializer
  1711. {
  1712. public:
  1713. ScopedInitializer()
  1714. {
  1715. carla_register_all_plugins();
  1716. }
  1717. ~ScopedInitializer()
  1718. {
  1719. sPluginDescriptors.clear();
  1720. }
  1721. };
  1722. private:
  1723. PluginHandle fHandle;
  1724. PluginHandle fHandle2;
  1725. HostDescriptor fHost;
  1726. const PluginDescriptor* fDescriptor;
  1727. bool fIsProcessing;
  1728. bool fIsUiVisible;
  1729. float** fAudioInBuffers;
  1730. float** fAudioOutBuffers;
  1731. uint32_t fMidiEventCount;
  1732. ::MidiEvent fMidiEvents[MAX_MIDI_EVENTS*2];
  1733. NativePluginMidiData fMidiIn;
  1734. NativePluginMidiData fMidiOut;
  1735. ::TimeInfo fTimeInfo;
  1736. static NonRtList<const PluginDescriptor*> sPluginDescriptors;
  1737. // -------------------------------------------------------------------
  1738. #define handlePtr ((NativePlugin*)handle)
  1739. static uint32_t carla_host_get_buffer_size(HostHandle handle)
  1740. {
  1741. return handlePtr->handleGetBufferSize();
  1742. }
  1743. static double carla_host_get_sample_rate(HostHandle handle)
  1744. {
  1745. return handlePtr->handleGetSampleRate();
  1746. }
  1747. static const ::TimeInfo* carla_host_get_time_info(HostHandle handle)
  1748. {
  1749. return handlePtr->handleGetTimeInfo();
  1750. }
  1751. static bool carla_host_write_midi_event(HostHandle handle, const ::MidiEvent* event)
  1752. {
  1753. return handlePtr->handleWriteMidiEvent(event);
  1754. }
  1755. static void carla_host_ui_parameter_changed(HostHandle handle, uint32_t index, float value)
  1756. {
  1757. handlePtr->handleUiParameterChanged(index, value);
  1758. }
  1759. static void carla_host_ui_custom_data_changed(HostHandle handle, const char* key, const char* value)
  1760. {
  1761. handlePtr->handleUiCustomDataChanged(key, value);
  1762. }
  1763. static void carla_host_ui_closed(HostHandle handle)
  1764. {
  1765. handlePtr->handleUiClosed();
  1766. }
  1767. static const char* carla_host_ui_open_file(HostHandle handle, bool isDir, const char* title, const char* filter)
  1768. {
  1769. return handlePtr->handleUiOpenFile(isDir, title, filter);
  1770. }
  1771. static const char* carla_host_ui_save_file(HostHandle handle, bool isDir, const char* title, const char* filter)
  1772. {
  1773. return handlePtr->handleUiSaveFile(isDir, title, filter);
  1774. }
  1775. #undef handlePtr
  1776. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(NativePlugin)
  1777. };
  1778. NonRtList<const PluginDescriptor*> NativePlugin::sPluginDescriptors;
  1779. static const NativePlugin::ScopedInitializer _si;
  1780. CARLA_BACKEND_END_NAMESPACE
  1781. void carla_register_native_plugin(const PluginDescriptor* desc)
  1782. {
  1783. CARLA_BACKEND_USE_NAMESPACE
  1784. NativePlugin::registerPlugin(desc);
  1785. }
  1786. #else // WANT_NATIVE
  1787. # warning Building without Internal plugin support
  1788. #endif
  1789. CARLA_BACKEND_START_NAMESPACE
  1790. // -----------------------------------------------------------------------
  1791. #ifdef WANT_NATIVE
  1792. size_t CarlaPlugin::getNativePluginCount()
  1793. {
  1794. return NativePlugin::getPluginCount();
  1795. }
  1796. const PluginDescriptor* CarlaPlugin::getNativePluginDescriptor(const size_t index)
  1797. {
  1798. return NativePlugin::getPluginDescriptor(index);
  1799. }
  1800. #endif
  1801. // -----------------------------------------------------------------------
  1802. CarlaPlugin* CarlaPlugin::newNative(const Initializer& init)
  1803. {
  1804. carla_debug("CarlaPlugin::newNative({%p, \"%s\", \"%s\", \"%s\"})", init.engine, init.filename, init.name, init.label);
  1805. #ifdef WANT_NATIVE
  1806. NativePlugin* const plugin(new NativePlugin(init.engine, init.id));
  1807. if (! plugin->init(init.name, init.label))
  1808. {
  1809. delete plugin;
  1810. return nullptr;
  1811. }
  1812. plugin->reload();
  1813. if (init.engine->getProccessMode() == PROCESS_MODE_CONTINUOUS_RACK && ! CarlaPluginProtectedData::canRunInRack(plugin))
  1814. {
  1815. init.engine->setLastError("Carla's rack mode can only work with Mono or Stereo Internal plugins, sorry!");
  1816. delete plugin;
  1817. return nullptr;
  1818. }
  1819. return plugin;
  1820. #else
  1821. init.engine->setLastError("Internal plugins support not available");
  1822. return nullptr;
  1823. #endif
  1824. }
  1825. CARLA_BACKEND_END_NAMESPACE