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.

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