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.

2306 lines
74KB

  1. /*
  2. * Carla Native Plugin
  3. * Copyright (C) 2012-2013 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the GPL.txt file
  16. */
  17. #include "CarlaPluginInternal.hpp"
  18. #ifdef WANT_NATIVE
  19. #include "CarlaNative.h"
  20. #include <QtCore/Qt>
  21. # if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
  22. # include <QtWidgets/QFileDialog>
  23. #else
  24. # include <QtGui/QFileDialog>
  25. #endif
  26. void carla_register_all_plugins()
  27. {
  28. // Simple plugins
  29. carla_register_native_plugin_bypass();
  30. carla_register_native_plugin_lfo();
  31. //carla_register_native_plugin_midiSequencer(); // unfinished
  32. carla_register_native_plugin_midiSplit();
  33. carla_register_native_plugin_midiThrough();
  34. carla_register_native_plugin_midiTranspose();
  35. carla_register_native_plugin_nekofilter();
  36. //carla_register_native_plugin_sunvoxfile(); // unfinished
  37. // Carla
  38. //carla_register_native_plugin_carla(); // unfinished
  39. #ifdef WANT_AUDIOFILE
  40. // AudioFile
  41. carla_register_native_plugin_audiofile();
  42. #endif
  43. #ifdef WANT_MIDIFILE
  44. // MidiFile
  45. carla_register_native_plugin_midifile();
  46. #endif
  47. #ifdef WANT_OPENGL
  48. // DISTRHO plugins (OpenGL)
  49. carla_register_native_plugin_3BandEQ();
  50. carla_register_native_plugin_3BandSplitter();
  51. carla_register_native_plugin_PingPongPan();
  52. #endif
  53. // DISTRHO plugins (Qt)
  54. carla_register_native_plugin_Notes();
  55. #ifdef WANT_ZYNADDSUBFX
  56. // ZynAddSubFX
  57. carla_register_native_plugin_zynaddsubfx();
  58. #endif
  59. }
  60. CARLA_BACKEND_START_NAMESPACE
  61. #if 0
  62. }
  63. #endif
  64. struct NativePluginMidiData {
  65. uint32_t count;
  66. uint32_t* indexes;
  67. CarlaEngineEventPort** ports;
  68. NativePluginMidiData()
  69. : count(0),
  70. indexes(nullptr),
  71. ports(nullptr) {}
  72. ~NativePluginMidiData()
  73. {
  74. CARLA_ASSERT_INT(count == 0, count);
  75. CARLA_ASSERT(ports == nullptr);
  76. CARLA_ASSERT(indexes == nullptr);
  77. }
  78. void createNew(const uint32_t newCount)
  79. {
  80. CARLA_ASSERT_INT(count == 0, count);
  81. CARLA_ASSERT(ports == nullptr);
  82. CARLA_ASSERT(indexes == nullptr);
  83. CARLA_ASSERT_INT(newCount > 0, newCount);
  84. if (ports != nullptr || indexes != nullptr || newCount == 0)
  85. return;
  86. ports = new CarlaEngineEventPort*[newCount];
  87. indexes = new uint32_t[newCount];
  88. count = newCount;
  89. for (uint32_t i=0; i < newCount; ++i)
  90. ports[i] = nullptr;
  91. for (uint32_t i=0; i < newCount; ++i)
  92. indexes[i] = 0;
  93. }
  94. void clear()
  95. {
  96. if (ports != nullptr)
  97. {
  98. for (uint32_t i=0; i < count; ++i)
  99. {
  100. if (ports[i] != nullptr)
  101. {
  102. delete ports[i];
  103. ports[i] = nullptr;
  104. }
  105. }
  106. delete[] ports;
  107. ports = nullptr;
  108. }
  109. if (indexes != nullptr)
  110. {
  111. delete[] indexes;
  112. indexes = nullptr;
  113. }
  114. count = 0;
  115. }
  116. void initBuffers(CarlaEngine* const engine)
  117. {
  118. for (uint32_t i=0; i < count; ++i)
  119. {
  120. if (ports[i] != nullptr)
  121. ports[i]->initBuffer(engine);
  122. }
  123. }
  124. CARLA_DECLARE_NON_COPY_STRUCT_WITH_LEAK_DETECTOR(NativePluginMidiData)
  125. };
  126. class NativePlugin : public CarlaPlugin
  127. {
  128. public:
  129. NativePlugin(CarlaEngine* const engine, const unsigned int id)
  130. : CarlaPlugin(engine, id),
  131. fHandle(nullptr),
  132. fHandle2(nullptr),
  133. fDescriptor(nullptr),
  134. fIsProcessing(false),
  135. fIsUiVisible(false),
  136. fAudioInBuffers(nullptr),
  137. fAudioOutBuffers(nullptr),
  138. fMidiEventCount(0)
  139. {
  140. carla_debug("NativePlugin::NativePlugin(%p, %i)", engine, id);
  141. carla_zeroStruct< ::MidiEvent>(fMidiEvents, MAX_MIDI_EVENTS*2);
  142. fHost.handle = this;
  143. fHost.ui_name = nullptr;
  144. fHost.get_buffer_size = carla_host_get_buffer_size;
  145. fHost.get_sample_rate = carla_host_get_sample_rate;
  146. fHost.get_time_info = carla_host_get_time_info;
  147. fHost.write_midi_event = carla_host_write_midi_event;
  148. fHost.ui_parameter_changed = carla_host_ui_parameter_changed;
  149. fHost.ui_custom_data_changed = carla_host_ui_custom_data_changed;
  150. fHost.ui_closed = carla_host_ui_closed;
  151. fHost.ui_open_file = carla_host_ui_open_file;
  152. fHost.ui_save_file = carla_host_ui_save_file;
  153. }
  154. ~NativePlugin() override
  155. {
  156. carla_debug("NativePlugin::~NativePlugin()");
  157. // close UI
  158. if (fHints & PLUGIN_HAS_GUI)
  159. {
  160. if (fIsUiVisible && fDescriptor != nullptr && fDescriptor->ui_show != nullptr && fHandle != nullptr)
  161. fDescriptor->ui_show(fHandle, false);
  162. }
  163. kData->singleMutex.lock();
  164. kData->masterMutex.lock();
  165. CARLA_ASSERT(! fIsProcessing);
  166. if (kData->active)
  167. {
  168. deactivate();
  169. kData->active = false;
  170. }
  171. if (fDescriptor != nullptr)
  172. {
  173. if (fDescriptor->cleanup != nullptr)
  174. {
  175. if (fHandle != nullptr)
  176. fDescriptor->cleanup(fHandle);
  177. if (fHandle2 != nullptr)
  178. fDescriptor->cleanup(fHandle2);
  179. }
  180. fHandle = nullptr;
  181. fHandle2 = nullptr;
  182. fDescriptor = nullptr;
  183. }
  184. if (fHost.ui_name != nullptr)
  185. {
  186. delete[] fHost.ui_name;
  187. fHost.ui_name = nullptr;
  188. }
  189. clearBuffers();
  190. }
  191. // -------------------------------------------------------------------
  192. // Information (base)
  193. PluginType type() const override
  194. {
  195. return PLUGIN_INTERNAL;
  196. }
  197. PluginCategory category() override
  198. {
  199. CARLA_ASSERT(fDescriptor != nullptr);
  200. return static_cast<PluginCategory>(fDescriptor->category);
  201. }
  202. // -------------------------------------------------------------------
  203. // Information (count)
  204. uint32_t midiInCount() const override
  205. {
  206. return fMidiIn.count;
  207. }
  208. uint32_t midiOutCount() const override
  209. {
  210. return fMidiOut.count;
  211. }
  212. uint32_t parameterScalePointCount(const uint32_t parameterId) const override
  213. {
  214. CARLA_ASSERT(fDescriptor != nullptr);
  215. CARLA_ASSERT(fHandle != nullptr);
  216. CARLA_ASSERT(parameterId < kData->param.count);
  217. if (fDescriptor->get_parameter_info != nullptr && parameterId < kData->param.count)
  218. {
  219. if (const Parameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId))
  220. return param->scalePointCount;
  221. }
  222. return 0;
  223. }
  224. // -------------------------------------------------------------------
  225. // Information (current data)
  226. // nothing
  227. // -------------------------------------------------------------------
  228. // Information (per-plugin data)
  229. unsigned int availableOptions() override
  230. {
  231. CARLA_ASSERT(fDescriptor != nullptr);
  232. if (fDescriptor == nullptr)
  233. return 0x0;
  234. unsigned int options = 0x0;
  235. options |= PLUGIN_OPTION_FIXED_BUFFER;
  236. options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  237. if (kData->engine->getProccessMode() != PROCESS_MODE_CONTINUOUS_RACK)
  238. {
  239. if (fOptions & PLUGIN_OPTION_FORCE_STEREO)
  240. options |= PLUGIN_OPTION_FORCE_STEREO;
  241. else if (kData->audioIn.count <= 1 && kData->audioOut.count <= 1 && (kData->audioIn.count != 0 || kData->audioOut.count != 0))
  242. options |= PLUGIN_OPTION_FORCE_STEREO;
  243. }
  244. if (fDescriptor->midiIns > 0)
  245. {
  246. options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  247. options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  248. options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  249. options |= PLUGIN_OPTION_SEND_PITCHBEND;
  250. options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  251. }
  252. return options;
  253. }
  254. float getParameterValue(const uint32_t parameterId) override
  255. {
  256. CARLA_ASSERT(fDescriptor != nullptr);
  257. CARLA_ASSERT(fHandle != nullptr);
  258. CARLA_ASSERT(parameterId < kData->param.count);
  259. if (fDescriptor->get_parameter_value != nullptr && parameterId < kData->param.count)
  260. return fDescriptor->get_parameter_value(fHandle, parameterId);
  261. return 0.0f;
  262. }
  263. float getParameterScalePointValue(const uint32_t parameterId, const uint32_t scalePointId) override
  264. {
  265. CARLA_ASSERT(fDescriptor != nullptr);
  266. CARLA_ASSERT(fHandle != nullptr);
  267. CARLA_ASSERT(parameterId < kData->param.count);
  268. CARLA_ASSERT(scalePointId < parameterScalePointCount(parameterId));
  269. if (fDescriptor->get_parameter_info != nullptr && parameterId < kData->param.count)
  270. {
  271. if (const Parameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId))
  272. {
  273. const ParameterScalePoint& scalePoint(param->scalePoints[scalePointId]);
  274. return scalePoint.value;
  275. }
  276. }
  277. return 0.0f;
  278. }
  279. void getLabel(char* const strBuf) override
  280. {
  281. CARLA_ASSERT(fDescriptor != nullptr);
  282. if (fDescriptor->label != nullptr)
  283. std::strncpy(strBuf, fDescriptor->label, STR_MAX);
  284. else
  285. CarlaPlugin::getLabel(strBuf);
  286. }
  287. void getMaker(char* const strBuf) override
  288. {
  289. CARLA_ASSERT(fDescriptor != nullptr);
  290. if (fDescriptor->maker != nullptr)
  291. std::strncpy(strBuf, fDescriptor->maker, STR_MAX);
  292. else
  293. CarlaPlugin::getMaker(strBuf);
  294. }
  295. void getCopyright(char* const strBuf) override
  296. {
  297. CARLA_ASSERT(fDescriptor != nullptr);
  298. if (fDescriptor->copyright != nullptr)
  299. std::strncpy(strBuf, fDescriptor->copyright, STR_MAX);
  300. else
  301. CarlaPlugin::getCopyright(strBuf);
  302. }
  303. void getRealName(char* const strBuf) override
  304. {
  305. CARLA_ASSERT(fDescriptor != nullptr);
  306. if (fDescriptor->name != nullptr)
  307. std::strncpy(strBuf, fDescriptor->name, STR_MAX);
  308. else
  309. CarlaPlugin::getRealName(strBuf);
  310. }
  311. void getParameterName(const uint32_t parameterId, char* const strBuf) override
  312. {
  313. CARLA_ASSERT(fDescriptor != nullptr);
  314. CARLA_ASSERT(fHandle != nullptr);
  315. CARLA_ASSERT(parameterId < kData->param.count);
  316. if (fDescriptor->get_parameter_info != nullptr && parameterId < kData->param.count)
  317. {
  318. const Parameter* const param(fDescriptor->get_parameter_info(fHandle, parameterId));
  319. if (param != nullptr && param->name != nullptr)
  320. {
  321. std::strncpy(strBuf, param->name, STR_MAX);
  322. return;
  323. }
  324. }
  325. CarlaPlugin::getParameterName(parameterId, strBuf);
  326. }
  327. void getParameterText(const uint32_t parameterId, char* const strBuf) override
  328. {
  329. CARLA_ASSERT(fDescriptor != nullptr);
  330. CARLA_ASSERT(fHandle != nullptr);
  331. CARLA_ASSERT(parameterId < kData->param.count);
  332. if (fDescriptor->get_parameter_text != nullptr && parameterId < kData->param.count)
  333. {
  334. if (const char* const text = fDescriptor->get_parameter_text(fHandle, parameterId))
  335. {
  336. std::strncpy(strBuf, text, STR_MAX);
  337. return;
  338. }
  339. }
  340. CarlaPlugin::getParameterText(parameterId, strBuf);
  341. }
  342. void getParameterUnit(const uint32_t parameterId, char* const strBuf) override
  343. {
  344. CARLA_ASSERT(fDescriptor != nullptr);
  345. CARLA_ASSERT(fHandle != nullptr);
  346. CARLA_ASSERT(parameterId < kData->param.count);
  347. if (fDescriptor->get_parameter_info != nullptr && parameterId < kData->param.count)
  348. {
  349. const Parameter* const param(fDescriptor->get_parameter_info(fHandle, parameterId));
  350. if (param != nullptr && param->unit != nullptr)
  351. {
  352. std::strncpy(strBuf, param->unit, STR_MAX);
  353. return;
  354. }
  355. }
  356. CarlaPlugin::getParameterUnit(parameterId, strBuf);
  357. }
  358. void getParameterScalePointLabel(const uint32_t parameterId, const uint32_t scalePointId, char* const strBuf) override
  359. {
  360. CARLA_ASSERT(fDescriptor != nullptr);
  361. CARLA_ASSERT(fHandle != nullptr);
  362. CARLA_ASSERT(parameterId < kData->param.count);
  363. CARLA_ASSERT(scalePointId < parameterScalePointCount(parameterId));
  364. if (fDescriptor->get_parameter_info != nullptr && parameterId < kData->param.count)
  365. {
  366. if (const Parameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId))
  367. {
  368. const ParameterScalePoint& scalePoint(param->scalePoints[scalePointId]);
  369. if (scalePoint.label != nullptr)
  370. {
  371. std::strncpy(strBuf, scalePoint.label, STR_MAX);
  372. return;
  373. }
  374. }
  375. }
  376. CarlaPlugin::getParameterScalePointLabel(parameterId, scalePointId, strBuf);
  377. }
  378. // -------------------------------------------------------------------
  379. // Set data (state)
  380. void prepareForSave() override
  381. {
  382. CARLA_ASSERT(fDescriptor != nullptr);
  383. CARLA_ASSERT(fHandle != nullptr);
  384. if (fDescriptor->get_state == nullptr)
  385. return;
  386. if ((fDescriptor->hints & ::PLUGIN_USES_STATE) == 0)
  387. return;
  388. if (char* data = fDescriptor->get_state(fHandle))
  389. {
  390. CarlaPlugin::setCustomData(CUSTOM_DATA_CHUNK, "State", data, false);
  391. std::free(data);
  392. }
  393. }
  394. // -------------------------------------------------------------------
  395. // Set data (internal stuff)
  396. void setName(const char* const newName) override
  397. {
  398. char uiName[std::strlen(newName)+6+1];
  399. std::strcpy(uiName, newName);
  400. std::strcat(uiName, " (GUI)");
  401. if (fHost.ui_name != nullptr)
  402. delete[] fHost.ui_name;
  403. fHost.ui_name = carla_strdup(uiName);
  404. CarlaPlugin::setName(newName);
  405. }
  406. // -------------------------------------------------------------------
  407. // Set data (plugin-specific stuff)
  408. void setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) override
  409. {
  410. CARLA_ASSERT(fDescriptor != nullptr);
  411. CARLA_ASSERT(fHandle != nullptr);
  412. CARLA_ASSERT(parameterId < kData->param.count);
  413. const float fixedValue(kData->param.fixValue(parameterId, value));
  414. if (fDescriptor->set_parameter_value != nullptr && parameterId < kData->param.count)
  415. {
  416. fDescriptor->set_parameter_value(fHandle, parameterId, fixedValue);
  417. if (fHandle2 != nullptr)
  418. fDescriptor->set_parameter_value(fHandle2, parameterId, fixedValue);
  419. }
  420. CarlaPlugin::setParameterValue(parameterId, fixedValue, sendGui, sendOsc, sendCallback);
  421. }
  422. void setCustomData(const char* const type, const char* const key, const char* const value, const bool sendGui) override
  423. {
  424. CARLA_ASSERT(fDescriptor != nullptr);
  425. CARLA_ASSERT(fHandle != nullptr);
  426. CARLA_ASSERT(type != nullptr);
  427. CARLA_ASSERT(key != nullptr);
  428. CARLA_ASSERT(value != nullptr);
  429. carla_debug("NativePlugin::setCustomData(%s, %s, %s, %s)", type, key, value, bool2str(sendGui));
  430. if (type == nullptr)
  431. return carla_stderr2("NativePlugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - type is null", type, key, value, bool2str(sendGui));
  432. if (std::strcmp(type, CUSTOM_DATA_STRING) != 0 && std::strcmp(type, CUSTOM_DATA_CHUNK) != 0)
  433. return carla_stderr2("NativePlugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - type is invalid", type, key, value, bool2str(sendGui));
  434. if (key == nullptr)
  435. return carla_stderr2("NativePlugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - key is null", type, key, value, bool2str(sendGui));
  436. if (value == nullptr)
  437. return carla_stderr2("Nativelugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - value is null", type, key, value, bool2str(sendGui));
  438. if (std::strcmp(type, CUSTOM_DATA_CHUNK) == 0)
  439. {
  440. if (fDescriptor->set_state != nullptr && (fDescriptor->hints & ::PLUGIN_USES_STATE) != 0)
  441. {
  442. const ScopedSingleProcessLocker spl(this, true);
  443. fDescriptor->set_state(fHandle, value);
  444. if (fHandle2 != nullptr)
  445. fDescriptor->set_state(fHandle2, value);
  446. }
  447. }
  448. else
  449. {
  450. if (fDescriptor->set_custom_data != nullptr)
  451. {
  452. fDescriptor->set_custom_data(fHandle, key, value);
  453. if (fHandle2 != nullptr)
  454. fDescriptor->set_custom_data(fHandle2, key, value);
  455. }
  456. if (sendGui && fIsUiVisible && fDescriptor->ui_set_custom_data != nullptr)
  457. fDescriptor->ui_set_custom_data(fHandle, key, value);
  458. if (std::strlen(key) == 6 && std::strncmp(key, "file", 4) == 0)
  459. {
  460. const ScopedDisabler sd(this);
  461. reloadPrograms(false);
  462. }
  463. }
  464. CarlaPlugin::setCustomData(type, key, value, sendGui);
  465. }
  466. void setMidiProgram(int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) override
  467. {
  468. CARLA_ASSERT(fDescriptor != nullptr);
  469. CARLA_ASSERT(fHandle != nullptr);
  470. CARLA_ASSERT(index >= -1 && index < static_cast<int32_t>(kData->midiprog.count));
  471. if (index < -1)
  472. index = -1;
  473. else if (index > static_cast<int32_t>(kData->midiprog.count))
  474. return;
  475. if (index >= 0)
  476. {
  477. const uint32_t bank = kData->midiprog.data[index].bank;
  478. const uint32_t program = kData->midiprog.data[index].program;
  479. const ScopedSingleProcessLocker spl(this, (sendGui || sendOsc || sendCallback));
  480. fDescriptor->set_midi_program(fHandle, bank, program);
  481. if (fHandle2 != nullptr)
  482. fDescriptor->set_midi_program(fHandle2, bank, program);
  483. }
  484. CarlaPlugin::setMidiProgram(index, sendGui, sendOsc, sendCallback);
  485. }
  486. // -------------------------------------------------------------------
  487. // Set gui stuff
  488. void showGui(const bool yesNo) override
  489. {
  490. CARLA_ASSERT(fDescriptor != nullptr);
  491. CARLA_ASSERT(fHandle != nullptr);
  492. if (fDescriptor->ui_show == nullptr)
  493. return;
  494. fDescriptor->ui_show(fHandle, yesNo);
  495. fIsUiVisible = yesNo;
  496. if (yesNo)
  497. {
  498. // Update UI values, FIXME? (remove?)
  499. if (fDescriptor->ui_set_custom_data != nullptr)
  500. {
  501. for (auto it = kData->custom.begin(); it.valid(); it.next())
  502. {
  503. const CustomData& cData(*it);
  504. if (std::strcmp(cData.type, CUSTOM_DATA_STRING) == 0)
  505. fDescriptor->ui_set_custom_data(fHandle, cData.key, cData.value);
  506. }
  507. }
  508. if (fDescriptor->ui_set_midi_program != nullptr && kData->midiprog.current >= 0)
  509. {
  510. const MidiProgramData& mpData = kData->midiprog.getCurrent();
  511. fDescriptor->ui_set_midi_program(fHandle, mpData.bank, mpData.program);
  512. }
  513. if (fDescriptor->ui_set_parameter_value != nullptr)
  514. {
  515. for (uint32_t i=0; i < kData->param.count; ++i)
  516. fDescriptor->ui_set_parameter_value(fHandle, i, fDescriptor->get_parameter_value(fHandle, i));
  517. }
  518. }
  519. }
  520. void idleGui() override
  521. {
  522. CARLA_ASSERT(fDescriptor != nullptr);
  523. CARLA_ASSERT(fHandle != nullptr);
  524. if (fIsUiVisible && fDescriptor->ui_idle != nullptr)
  525. fDescriptor->ui_idle(fHandle);
  526. }
  527. // -------------------------------------------------------------------
  528. // Plugin state
  529. void reload() override
  530. {
  531. carla_debug("NativePlugin::reload() - start");
  532. CARLA_ASSERT(kData->engine != nullptr);
  533. CARLA_ASSERT(fDescriptor != nullptr);
  534. CARLA_ASSERT(fHandle != nullptr);
  535. if (kData->engine == nullptr)
  536. return;
  537. if (fDescriptor == nullptr)
  538. return;
  539. if (fHandle == nullptr)
  540. return;
  541. const ProcessMode processMode(kData->engine->getProccessMode());
  542. // Safely disable plugin for reload
  543. const ScopedDisabler sd(this);
  544. if (kData->active)
  545. deactivate();
  546. clearBuffers();
  547. const float sampleRate = (float)kData->engine->getSampleRate();
  548. uint32_t aIns, aOuts, mIns, mOuts, params, j;
  549. bool forcedStereoIn, forcedStereoOut;
  550. forcedStereoIn = forcedStereoOut = false;
  551. bool needsCtrlIn, needsCtrlOut;
  552. needsCtrlIn = needsCtrlOut = false;
  553. aIns = fDescriptor->audioIns;
  554. aOuts = fDescriptor->audioOuts;
  555. mIns = fDescriptor->midiIns;
  556. mOuts = fDescriptor->midiOuts;
  557. params = (fDescriptor->get_parameter_count != nullptr && fDescriptor->get_parameter_info != nullptr) ? fDescriptor->get_parameter_count(fHandle) : 0;
  558. if ((fOptions & PLUGIN_OPTION_FORCE_STEREO) != 0 && (aIns == 1 || aOuts == 1) && mIns <= 1 && mOuts <= 1)
  559. {
  560. if (fHandle2 == nullptr)
  561. fHandle2 = fDescriptor->instantiate(fDescriptor, &fHost);
  562. if (aIns == 1)
  563. {
  564. aIns = 2;
  565. forcedStereoIn = true;
  566. }
  567. if (aOuts == 1)
  568. {
  569. aOuts = 2;
  570. forcedStereoOut = true;
  571. }
  572. }
  573. if (aIns > 0)
  574. {
  575. kData->audioIn.createNew(aIns);
  576. fAudioInBuffers = new float*[aIns];
  577. for (uint32_t i=0; i < aIns; ++i)
  578. fAudioInBuffers[i] = nullptr;
  579. }
  580. if (aOuts > 0)
  581. {
  582. kData->audioOut.createNew(aOuts);
  583. fAudioOutBuffers = new float*[aOuts];
  584. needsCtrlIn = true;
  585. for (uint32_t i=0; i < aOuts; ++i)
  586. fAudioOutBuffers[i] = nullptr;
  587. }
  588. if (mIns > 0)
  589. {
  590. fMidiIn.createNew(mIns);
  591. needsCtrlIn = (mIns == 1);
  592. }
  593. if (mOuts > 0)
  594. {
  595. fMidiOut.createNew(mOuts);
  596. needsCtrlOut = (mOuts == 1);
  597. }
  598. if (params > 0)
  599. {
  600. kData->param.createNew(params);
  601. }
  602. const uint portNameSize(kData->engine->maxPortNameSize());
  603. CarlaString portName;
  604. // Audio Ins
  605. for (j=0; j < aIns; ++j)
  606. {
  607. portName.clear();
  608. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  609. {
  610. portName = fName;
  611. portName += ":";
  612. }
  613. if (aIns > 1 && ! forcedStereoIn)
  614. {
  615. portName += "input_";
  616. portName += CarlaString(j+1);
  617. }
  618. else
  619. portName += "input";
  620. portName.truncate(portNameSize);
  621. kData->audioIn.ports[j].port = (CarlaEngineAudioPort*)kData->client->addPort(kEnginePortTypeAudio, portName, true);
  622. kData->audioIn.ports[j].rindex = j;
  623. if (forcedStereoIn)
  624. {
  625. portName += "_2";
  626. kData->audioIn.ports[1].port = (CarlaEngineAudioPort*)kData->client->addPort(kEnginePortTypeAudio, portName, true);
  627. kData->audioIn.ports[1].rindex = j;
  628. break;
  629. }
  630. }
  631. // Audio Outs
  632. for (j=0; j < aOuts; ++j)
  633. {
  634. portName.clear();
  635. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  636. {
  637. portName = fName;
  638. portName += ":";
  639. }
  640. if (aOuts > 1 && ! forcedStereoOut)
  641. {
  642. portName += "output_";
  643. portName += CarlaString(j+1);
  644. }
  645. else
  646. portName += "output";
  647. portName.truncate(portNameSize);
  648. kData->audioOut.ports[j].port = (CarlaEngineAudioPort*)kData->client->addPort(kEnginePortTypeAudio, portName, false);
  649. kData->audioOut.ports[j].rindex = j;
  650. if (forcedStereoOut)
  651. {
  652. portName += "_2";
  653. kData->audioOut.ports[1].port = (CarlaEngineAudioPort*)kData->client->addPort(kEnginePortTypeAudio, portName, false);
  654. kData->audioOut.ports[1].rindex = j;
  655. break;
  656. }
  657. }
  658. // MIDI Input (only if multiple)
  659. if (mIns > 1)
  660. {
  661. for (j=0; j < mIns; ++j)
  662. {
  663. portName.clear();
  664. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  665. {
  666. portName = fName;
  667. portName += ":";
  668. }
  669. portName += "midi-in_";
  670. portName += CarlaString(j+1);
  671. portName.truncate(portNameSize);
  672. fMidiIn.ports[j] = (CarlaEngineEventPort*)kData->client->addPort(kEnginePortTypeEvent, portName, true);
  673. fMidiIn.indexes[j] = j;
  674. }
  675. }
  676. // MIDI Output (only if multiple)
  677. if (mOuts > 1)
  678. {
  679. for (j=0; j < mOuts; ++j)
  680. {
  681. portName.clear();
  682. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  683. {
  684. portName = fName;
  685. portName += ":";
  686. }
  687. portName += "midi-out_";
  688. portName += CarlaString(j+1);
  689. portName.truncate(portNameSize);
  690. fMidiOut.ports[j] = (CarlaEngineEventPort*)kData->client->addPort(kEnginePortTypeEvent, portName, false);
  691. fMidiOut.indexes[j] = j;
  692. }
  693. }
  694. for (j=0; j < params; ++j)
  695. {
  696. const ::Parameter* const paramInfo = fDescriptor->get_parameter_info(fHandle, j);
  697. CARLA_ASSERT(paramInfo != nullptr);
  698. if (paramInfo == nullptr)
  699. continue;
  700. kData->param.data[j].index = j;
  701. kData->param.data[j].rindex = j;
  702. kData->param.data[j].hints = 0x0;
  703. kData->param.data[j].midiChannel = 0;
  704. kData->param.data[j].midiCC = -1;
  705. float min, max, def, step, stepSmall, stepLarge;
  706. // min value
  707. min = paramInfo->ranges.min;
  708. // max value
  709. max = paramInfo->ranges.max;
  710. if (min > max)
  711. max = min;
  712. else if (max < min)
  713. min = max;
  714. if (max - min == 0.0f)
  715. {
  716. carla_stderr2("WARNING - Broken plugin parameter '%s': max - min == 0.0f", paramInfo->name);
  717. max = min + 0.1f;
  718. }
  719. // default value
  720. def = paramInfo->ranges.def;
  721. if (def < min)
  722. def = min;
  723. else if (def > max)
  724. def = max;
  725. if (paramInfo->hints & ::PARAMETER_USES_SAMPLE_RATE)
  726. {
  727. min *= sampleRate;
  728. max *= sampleRate;
  729. def *= sampleRate;
  730. kData->param.data[j].hints |= PARAMETER_USES_SAMPLERATE;
  731. }
  732. if (paramInfo->hints & ::PARAMETER_IS_BOOLEAN)
  733. {
  734. step = max - min;
  735. stepSmall = step;
  736. stepLarge = step;
  737. kData->param.data[j].hints |= PARAMETER_IS_BOOLEAN;
  738. }
  739. else if (paramInfo->hints & ::PARAMETER_IS_INTEGER)
  740. {
  741. step = 1.0f;
  742. stepSmall = 1.0f;
  743. stepLarge = 10.0f;
  744. kData->param.data[j].hints |= PARAMETER_IS_INTEGER;
  745. }
  746. else
  747. {
  748. float range = max - min;
  749. step = range/100.0f;
  750. stepSmall = range/1000.0f;
  751. stepLarge = range/10.0f;
  752. }
  753. if (paramInfo->hints & ::PARAMETER_IS_OUTPUT)
  754. {
  755. kData->param.data[j].type = PARAMETER_OUTPUT;
  756. needsCtrlOut = true;
  757. }
  758. else
  759. {
  760. kData->param.data[j].type = PARAMETER_INPUT;
  761. needsCtrlIn = true;
  762. }
  763. // extra parameter hints
  764. if (paramInfo->hints & ::PARAMETER_IS_ENABLED)
  765. kData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  766. if (paramInfo->hints & ::PARAMETER_IS_AUTOMABLE)
  767. kData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  768. if (paramInfo->hints & ::PARAMETER_IS_LOGARITHMIC)
  769. kData->param.data[j].hints |= PARAMETER_IS_LOGARITHMIC;
  770. if (paramInfo->hints & ::PARAMETER_USES_SCALEPOINTS)
  771. kData->param.data[j].hints |= PARAMETER_USES_SCALEPOINTS;
  772. if (paramInfo->hints & ::PARAMETER_USES_CUSTOM_TEXT)
  773. kData->param.data[j].hints |= PARAMETER_USES_CUSTOM_TEXT;
  774. kData->param.ranges[j].min = min;
  775. kData->param.ranges[j].max = max;
  776. kData->param.ranges[j].def = def;
  777. kData->param.ranges[j].step = step;
  778. kData->param.ranges[j].stepSmall = stepSmall;
  779. kData->param.ranges[j].stepLarge = stepLarge;
  780. }
  781. if (needsCtrlIn)
  782. {
  783. portName.clear();
  784. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  785. {
  786. portName = fName;
  787. portName += ":";
  788. }
  789. portName += "events-in";
  790. portName.truncate(portNameSize);
  791. kData->event.portIn = (CarlaEngineEventPort*)kData->client->addPort(kEnginePortTypeEvent, portName, true);
  792. }
  793. if (needsCtrlOut)
  794. {
  795. portName.clear();
  796. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  797. {
  798. portName = fName;
  799. portName += ":";
  800. }
  801. portName += "events-out";
  802. portName.truncate(portNameSize);
  803. kData->event.portOut = (CarlaEngineEventPort*)kData->client->addPort(kEnginePortTypeEvent, portName, false);
  804. }
  805. if (forcedStereoIn || forcedStereoOut)
  806. fOptions |= PLUGIN_OPTION_FORCE_STEREO;
  807. else
  808. fOptions &= ~PLUGIN_OPTION_FORCE_STEREO;
  809. // plugin hints
  810. fHints = 0x0;
  811. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  812. fHints |= PLUGIN_CAN_DRYWET;
  813. if (aOuts > 0)
  814. fHints |= PLUGIN_CAN_VOLUME;
  815. if (aOuts >= 2 && aOuts % 2 == 0)
  816. fHints |= PLUGIN_CAN_BALANCE;
  817. // native plugin hints
  818. if (fDescriptor->hints & ::PLUGIN_IS_RTSAFE)
  819. fHints |= PLUGIN_IS_RTSAFE;
  820. if (fDescriptor->hints & ::PLUGIN_IS_SYNTH)
  821. fHints |= PLUGIN_IS_SYNTH;
  822. if (fDescriptor->hints & ::PLUGIN_HAS_GUI)
  823. fHints |= PLUGIN_HAS_GUI;
  824. if (fDescriptor->hints & ::PLUGIN_USES_SINGLE_THREAD)
  825. fHints |= PLUGIN_HAS_SINGLE_THREAD;
  826. // extra plugin hints
  827. kData->extraHints = 0x0;
  828. if (aIns <= 2 && aOuts <= 2 && (aIns == aOuts || aIns == 0 || aOuts == 0) && mIns <= 1 && mOuts <= 1)
  829. kData->extraHints |= PLUGIN_HINT_CAN_RUN_RACK;
  830. bufferSizeChanged(kData->engine->getBufferSize());
  831. reloadPrograms(true);
  832. if (kData->active)
  833. activate();
  834. carla_debug("NativePlugin::reload() - end");
  835. }
  836. void reloadPrograms(const bool init) override
  837. {
  838. carla_debug("NativePlugin::reloadPrograms(%s)", bool2str(init));
  839. uint32_t i, oldCount = kData->midiprog.count;
  840. const int32_t current = kData->midiprog.current;
  841. // Delete old programs
  842. kData->midiprog.clear();
  843. // Query new programs
  844. uint32_t count = 0;
  845. if (fDescriptor->get_midi_program_count != nullptr && fDescriptor->get_midi_program_info != nullptr)
  846. count = fDescriptor->get_midi_program_count(fHandle);
  847. if (count > 0)
  848. {
  849. kData->midiprog.createNew(count);
  850. // Update data
  851. for (i=0; i < count; ++i)
  852. {
  853. const ::MidiProgram* const mpDesc = fDescriptor->get_midi_program_info(fHandle, i);
  854. CARLA_ASSERT(mpDesc != nullptr);
  855. CARLA_ASSERT(mpDesc->name != nullptr);
  856. kData->midiprog.data[i].bank = mpDesc->bank;
  857. kData->midiprog.data[i].program = mpDesc->program;
  858. kData->midiprog.data[i].name = carla_strdup(mpDesc->name);
  859. }
  860. }
  861. #ifndef BUILD_BRIDGE
  862. // Update OSC Names
  863. if (kData->engine->isOscControlRegistered())
  864. {
  865. kData->engine->osc_send_control_set_midi_program_count(fId, count);
  866. for (i=0; i < count; ++i)
  867. 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);
  868. }
  869. #endif
  870. if (init)
  871. {
  872. if (count > 0)
  873. setMidiProgram(0, false, false, false);
  874. }
  875. else
  876. {
  877. // Check if current program is invalid
  878. bool programChanged = false;
  879. if (count == oldCount+1)
  880. {
  881. // one midi program added, probably created by user
  882. kData->midiprog.current = oldCount;
  883. programChanged = true;
  884. }
  885. else if (current < 0 && count > 0)
  886. {
  887. // programs exist now, but not before
  888. kData->midiprog.current = 0;
  889. programChanged = true;
  890. }
  891. else if (current >= 0 && count == 0)
  892. {
  893. // programs existed before, but not anymore
  894. kData->midiprog.current = -1;
  895. programChanged = true;
  896. }
  897. else if (current >= static_cast<int32_t>(count))
  898. {
  899. // current midi program > count
  900. kData->midiprog.current = 0;
  901. programChanged = true;
  902. }
  903. else
  904. {
  905. // no change
  906. kData->midiprog.current = current;
  907. }
  908. if (programChanged)
  909. setMidiProgram(kData->midiprog.current, true, true, true);
  910. kData->engine->callback(CALLBACK_RELOAD_PROGRAMS, fId, 0, 0, 0.0f, nullptr);
  911. }
  912. }
  913. // -------------------------------------------------------------------
  914. // Plugin processing
  915. void activate() override
  916. {
  917. CARLA_ASSERT(fDescriptor != nullptr);
  918. CARLA_ASSERT(fHandle != nullptr);
  919. if (fDescriptor->activate != nullptr)
  920. {
  921. fDescriptor->activate(fHandle);
  922. if (fHandle2 != nullptr)
  923. fDescriptor->activate(fHandle2);
  924. }
  925. }
  926. void deactivate() override
  927. {
  928. CARLA_ASSERT(fDescriptor != nullptr);
  929. CARLA_ASSERT(fHandle != 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. #ifndef BUILD_BRIDGE
  1068. // Control backend stuff
  1069. if (event.channel == kData->ctrlChannel)
  1070. {
  1071. float value;
  1072. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (fHints & PLUGIN_CAN_DRYWET) > 0)
  1073. {
  1074. value = ctrlEvent.value;
  1075. setDryWet(value, false, false);
  1076. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, value);
  1077. continue;
  1078. }
  1079. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (fHints & PLUGIN_CAN_VOLUME) > 0)
  1080. {
  1081. value = ctrlEvent.value*127.0f/100.0f;
  1082. setVolume(value, false, false);
  1083. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
  1084. continue;
  1085. }
  1086. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (fHints & PLUGIN_CAN_BALANCE) > 0)
  1087. {
  1088. float left, right;
  1089. value = ctrlEvent.value/0.5f - 1.0f;
  1090. if (value < 0.0f)
  1091. {
  1092. left = -1.0f;
  1093. right = (value*2.0f)+1.0f;
  1094. }
  1095. else if (value > 0.0f)
  1096. {
  1097. left = (value*2.0f)-1.0f;
  1098. right = 1.0f;
  1099. }
  1100. else
  1101. {
  1102. left = -1.0f;
  1103. right = 1.0f;
  1104. }
  1105. setBalanceLeft(left, false, false);
  1106. setBalanceRight(right, false, false);
  1107. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  1108. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  1109. continue;
  1110. }
  1111. }
  1112. #endif
  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. sendMidiAllNotesOffToCallback();
  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. sendMidiAllNotesOffToCallback();
  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. #ifndef BUILD_BRIDGE
  1346. // --------------------------------------------------------------------------------------------------------
  1347. // Post-processing (dry/wet, volume and balance)
  1348. {
  1349. const bool doDryWet = (fHints & PLUGIN_CAN_DRYWET) != 0 && kData->postProc.dryWet != 1.0f;
  1350. const bool doBalance = (fHints & PLUGIN_CAN_BALANCE) != 0 && (kData->postProc.balanceLeft != -1.0f || kData->postProc.balanceRight != 1.0f);
  1351. bool isPair;
  1352. float bufValue, oldBufLeft[doBalance ? frames : 1];
  1353. for (i=0; i < kData->audioOut.count; ++i)
  1354. {
  1355. // Dry/Wet
  1356. if (doDryWet)
  1357. {
  1358. for (k=0; k < frames; ++k)
  1359. {
  1360. bufValue = fAudioInBuffers[(kData->audioIn.count == 1) ? 0 : i][k];
  1361. fAudioOutBuffers[i][k] = (fAudioOutBuffers[i][k] * kData->postProc.dryWet) + (bufValue * (1.0f - kData->postProc.dryWet));
  1362. }
  1363. }
  1364. // Balance
  1365. if (doBalance)
  1366. {
  1367. isPair = (i % 2 == 0);
  1368. if (isPair)
  1369. {
  1370. CARLA_ASSERT(i+1 < kData->audioOut.count);
  1371. carla_copyFloat(oldBufLeft, fAudioOutBuffers[i], frames);
  1372. }
  1373. float balRangeL = (kData->postProc.balanceLeft + 1.0f)/2.0f;
  1374. float balRangeR = (kData->postProc.balanceRight + 1.0f)/2.0f;
  1375. for (k=0; k < frames; ++k)
  1376. {
  1377. if (isPair)
  1378. {
  1379. // left
  1380. fAudioOutBuffers[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  1381. fAudioOutBuffers[i][k] += fAudioOutBuffers[i+1][k] * (1.0f - balRangeR);
  1382. }
  1383. else
  1384. {
  1385. // right
  1386. fAudioOutBuffers[i][k] = fAudioOutBuffers[i][k] * balRangeR;
  1387. fAudioOutBuffers[i][k] += oldBufLeft[k] * balRangeL;
  1388. }
  1389. }
  1390. }
  1391. // Volume (and buffer copy)
  1392. {
  1393. for (k=0; k < frames; ++k)
  1394. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k] * kData->postProc.volume;
  1395. }
  1396. }
  1397. } // End of Post-processing
  1398. #else
  1399. for (i=0; i < kData->audioOut.count; ++i)
  1400. {
  1401. for (k=0; k < frames; ++k)
  1402. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k];
  1403. }
  1404. #endif
  1405. // --------------------------------------------------------------------------------------------------------
  1406. kData->singleMutex.unlock();
  1407. return true;
  1408. }
  1409. void bufferSizeChanged(const uint32_t newBufferSize) override
  1410. {
  1411. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  1412. carla_debug("NativePlugin::bufferSizeChanged(%i)", newBufferSize);
  1413. for (uint32_t i=0; i < kData->audioIn.count; ++i)
  1414. {
  1415. if (fAudioInBuffers[i] != nullptr)
  1416. delete[] fAudioInBuffers[i];
  1417. fAudioInBuffers[i] = new float[newBufferSize];
  1418. }
  1419. for (uint32_t i=0; i < kData->audioOut.count; ++i)
  1420. {
  1421. if (fAudioOutBuffers[i] != nullptr)
  1422. delete[] fAudioOutBuffers[i];
  1423. fAudioOutBuffers[i] = new float[newBufferSize];
  1424. }
  1425. }
  1426. // -------------------------------------------------------------------
  1427. // Plugin buffers
  1428. void initBuffers() override
  1429. {
  1430. fMidiIn.initBuffers(kData->engine);
  1431. fMidiOut.initBuffers(kData->engine);
  1432. CarlaPlugin::initBuffers();
  1433. }
  1434. void clearBuffers() override
  1435. {
  1436. carla_debug("NativePlugin::clearBuffers() - start");
  1437. if (fAudioInBuffers != nullptr)
  1438. {
  1439. for (uint32_t i=0; i < kData->audioIn.count; ++i)
  1440. {
  1441. if (fAudioInBuffers[i] != nullptr)
  1442. {
  1443. delete[] fAudioInBuffers[i];
  1444. fAudioInBuffers[i] = nullptr;
  1445. }
  1446. }
  1447. delete[] fAudioInBuffers;
  1448. fAudioInBuffers = nullptr;
  1449. }
  1450. if (fAudioOutBuffers != nullptr)
  1451. {
  1452. for (uint32_t i=0; i < kData->audioOut.count; ++i)
  1453. {
  1454. if (fAudioOutBuffers[i] != nullptr)
  1455. {
  1456. delete[] fAudioOutBuffers[i];
  1457. fAudioOutBuffers[i] = nullptr;
  1458. }
  1459. }
  1460. delete[] fAudioOutBuffers;
  1461. fAudioOutBuffers = nullptr;
  1462. }
  1463. fMidiIn.clear();
  1464. fMidiOut.clear();
  1465. CarlaPlugin::clearBuffers();
  1466. carla_debug("NativePlugin::clearBuffers() - end");
  1467. }
  1468. // -------------------------------------------------------------------
  1469. // Post-poned UI Stuff
  1470. void uiParameterChange(const uint32_t index, const float value) override
  1471. {
  1472. CARLA_ASSERT(fDescriptor != nullptr);
  1473. CARLA_ASSERT(fHandle != nullptr);
  1474. CARLA_ASSERT(index < kData->param.count);
  1475. if (! fIsUiVisible)
  1476. return;
  1477. if (fDescriptor == nullptr || fHandle == nullptr)
  1478. return;
  1479. if (index >= kData->param.count)
  1480. return;
  1481. if (fDescriptor->ui_set_parameter_value != nullptr)
  1482. fDescriptor->ui_set_parameter_value(fHandle, index, value);
  1483. }
  1484. void uiMidiProgramChange(const uint32_t index) override
  1485. {
  1486. CARLA_ASSERT(fDescriptor != nullptr);
  1487. CARLA_ASSERT(fHandle != nullptr);
  1488. CARLA_ASSERT(index < kData->midiprog.count);
  1489. if (! fIsUiVisible)
  1490. return;
  1491. if (fDescriptor == nullptr || fHandle == nullptr)
  1492. return;
  1493. if (index >= kData->midiprog.count)
  1494. return;
  1495. if (fDescriptor->ui_set_midi_program != nullptr)
  1496. fDescriptor->ui_set_midi_program(fHandle, kData->midiprog.data[index].bank, kData->midiprog.data[index].program);
  1497. }
  1498. void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) override
  1499. {
  1500. CARLA_ASSERT(fDescriptor != nullptr);
  1501. CARLA_ASSERT(fHandle != nullptr);
  1502. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  1503. CARLA_ASSERT(note < MAX_MIDI_NOTE);
  1504. CARLA_ASSERT(velo > 0 && velo < MAX_MIDI_VALUE);
  1505. if (! fIsUiVisible)
  1506. return;
  1507. if (fDescriptor == nullptr || fHandle == nullptr)
  1508. return;
  1509. if (channel >= MAX_MIDI_CHANNELS)
  1510. return;
  1511. if (note >= MAX_MIDI_NOTE)
  1512. return;
  1513. if (velo >= MAX_MIDI_VALUE)
  1514. return;
  1515. // TODO
  1516. }
  1517. void uiNoteOff(const uint8_t channel, const uint8_t note) override
  1518. {
  1519. CARLA_ASSERT(fDescriptor != nullptr);
  1520. CARLA_ASSERT(fHandle != nullptr);
  1521. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  1522. CARLA_ASSERT(note < MAX_MIDI_NOTE);
  1523. if (! fIsUiVisible)
  1524. return;
  1525. if (fDescriptor == nullptr || fHandle == nullptr)
  1526. return;
  1527. if (channel >= MAX_MIDI_CHANNELS)
  1528. return;
  1529. if (note >= MAX_MIDI_NOTE)
  1530. return;
  1531. // TODO
  1532. }
  1533. // -------------------------------------------------------------------
  1534. protected:
  1535. uint32_t handleGetBufferSize()
  1536. {
  1537. return kData->engine->getBufferSize();
  1538. }
  1539. double handleGetSampleRate()
  1540. {
  1541. return kData->engine->getSampleRate();
  1542. }
  1543. const ::TimeInfo* handleGetTimeInfo()
  1544. {
  1545. CARLA_ASSERT(fIsProcessing);
  1546. return &fTimeInfo;
  1547. }
  1548. bool handleWriteMidiEvent(const ::MidiEvent* const event)
  1549. {
  1550. CARLA_ASSERT(fEnabled);
  1551. CARLA_ASSERT(fIsProcessing);
  1552. CARLA_ASSERT(fMidiOut.count > 0 || kData->event.portOut != nullptr);
  1553. CARLA_ASSERT(event != nullptr);
  1554. CARLA_ASSERT(event->data[0] != 0);
  1555. if (! fEnabled)
  1556. return false;
  1557. if (fMidiOut.count == 0)
  1558. return false;
  1559. if (event == nullptr)
  1560. return false;
  1561. if (event->data[0] == 0)
  1562. return false;
  1563. if (! fIsProcessing)
  1564. {
  1565. carla_stderr2("NativePlugin::handleWriteMidiEvent(%p) - received MIDI out event outside audio thread, ignoring", event);
  1566. return false;
  1567. }
  1568. if (fMidiEventCount >= MAX_MIDI_EVENTS*2)
  1569. return false;
  1570. // reverse-find first free event, and put it there
  1571. for (uint32_t i=(MAX_MIDI_EVENTS*2)-1; i >= fMidiEventCount; --i)
  1572. {
  1573. if (fMidiEvents[i].data[0] == 0)
  1574. {
  1575. std::memcpy(&fMidiEvents[i], event, sizeof(::MidiEvent));
  1576. break;
  1577. }
  1578. }
  1579. return true;
  1580. }
  1581. void handleUiParameterChanged(const uint32_t index, const float value)
  1582. {
  1583. setParameterValue(index, value, false, true, true);
  1584. }
  1585. void handleUiCustomDataChanged(const char* const key, const char* const value)
  1586. {
  1587. setCustomData(CUSTOM_DATA_STRING, key, value, false);
  1588. }
  1589. void handleUiClosed()
  1590. {
  1591. kData->engine->callback(CALLBACK_SHOW_GUI, fId, 0, 0, 0.0f, nullptr);
  1592. fIsUiVisible = false;
  1593. }
  1594. const char* handleUiOpenFile(const bool isDir, const char* const title, const char* const filter)
  1595. {
  1596. static CarlaString retStr;
  1597. QFileDialog::Options options(isDir ? QFileDialog::ShowDirsOnly : 0x0);
  1598. retStr = QFileDialog::getOpenFileName(nullptr, title, "", filter, nullptr, options).toUtf8().constData();
  1599. return retStr.isNotEmpty() ? (const char*)retStr : nullptr;
  1600. }
  1601. const char* handleUiSaveFile(const bool isDir, const char* const title, const char* const filter)
  1602. {
  1603. static CarlaString retStr;
  1604. QFileDialog::Options options(isDir ? QFileDialog::ShowDirsOnly : 0x0);
  1605. retStr = QFileDialog::getSaveFileName(nullptr, title, "", filter, nullptr, options).toUtf8().constData();
  1606. return retStr.isNotEmpty() ? (const char*)retStr : nullptr;
  1607. }
  1608. // -------------------------------------------------------------------
  1609. public:
  1610. static size_t getPluginCount()
  1611. {
  1612. return sPluginDescriptors.count();
  1613. }
  1614. static const PluginDescriptor* getPluginDescriptor(const size_t index)
  1615. {
  1616. CARLA_ASSERT(index < sPluginDescriptors.count());
  1617. if (index < sPluginDescriptors.count())
  1618. return sPluginDescriptors.getAt(index);
  1619. return nullptr;
  1620. }
  1621. static void registerPlugin(const PluginDescriptor* desc)
  1622. {
  1623. sPluginDescriptors.append(desc);
  1624. }
  1625. // -------------------------------------------------------------------
  1626. bool init(const char* const name, const char* const label)
  1627. {
  1628. CARLA_ASSERT(kData->engine != nullptr);
  1629. CARLA_ASSERT(kData->client == nullptr);
  1630. CARLA_ASSERT(label != nullptr);
  1631. // ---------------------------------------------------------------
  1632. // first checks
  1633. if (kData->engine == nullptr)
  1634. {
  1635. return false;
  1636. }
  1637. if (kData->client != nullptr)
  1638. {
  1639. kData->engine->setLastError("Plugin client is already registered");
  1640. return false;
  1641. }
  1642. if (label == nullptr)
  1643. {
  1644. kData->engine->setLastError("null label");
  1645. return false;
  1646. }
  1647. // ---------------------------------------------------------------
  1648. // get descriptor that matches label
  1649. for (auto it = sPluginDescriptors.begin(); it.valid(); it.next())
  1650. {
  1651. fDescriptor = *it;
  1652. CARLA_ASSERT(fDescriptor != nullptr);
  1653. if (fDescriptor == nullptr)
  1654. break;
  1655. if (fDescriptor->label != nullptr && std::strcmp(fDescriptor->label, label) == 0)
  1656. break;
  1657. fDescriptor = nullptr;
  1658. }
  1659. if (fDescriptor == nullptr)
  1660. {
  1661. kData->engine->setLastError("Invalid internal plugin");
  1662. return false;
  1663. }
  1664. // ---------------------------------------------------------------
  1665. // get info
  1666. if (name != nullptr)
  1667. fName = kData->engine->getUniquePluginName(name);
  1668. else if (fDescriptor->name != nullptr)
  1669. fName = kData->engine->getUniquePluginName(fDescriptor->name);
  1670. else
  1671. fName = kData->engine->getUniquePluginName(label);
  1672. {
  1673. CARLA_ASSERT(fHost.ui_name == nullptr);
  1674. char uiName[fName.length()+6+1];
  1675. std::strcpy(uiName, (const char*)fName);
  1676. std::strcat(uiName, " (GUI)");
  1677. fHost.ui_name = carla_strdup(uiName);
  1678. }
  1679. // ---------------------------------------------------------------
  1680. // register client
  1681. kData->client = kData->engine->addClient(this);
  1682. if (kData->client == nullptr || ! kData->client->isOk())
  1683. {
  1684. kData->engine->setLastError("Failed to register plugin client");
  1685. return false;
  1686. }
  1687. // ---------------------------------------------------------------
  1688. // initialize plugin
  1689. fHandle = fDescriptor->instantiate(fDescriptor, &fHost);
  1690. if (fHandle == nullptr)
  1691. {
  1692. kData->engine->setLastError("Plugin failed to initialize");
  1693. return false;
  1694. }
  1695. // ---------------------------------------------------------------
  1696. // load plugin settings
  1697. {
  1698. // set default options
  1699. fOptions = 0x0;
  1700. fOptions |= PLUGIN_OPTION_FIXED_BUFFER;
  1701. fOptions |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  1702. if (kData->engine->getOptions().forceStereo)
  1703. fOptions |= PLUGIN_OPTION_FORCE_STEREO;
  1704. if (fDescriptor->midiIns > 0)
  1705. {
  1706. fOptions |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  1707. fOptions |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  1708. fOptions |= PLUGIN_OPTION_SEND_PITCHBEND;
  1709. fOptions |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  1710. }
  1711. // load settings
  1712. kData->idStr = "Native/";
  1713. kData->idStr += label;
  1714. fOptions = kData->loadSettings(fOptions, availableOptions());
  1715. }
  1716. return true;
  1717. }
  1718. class ScopedInitializer
  1719. {
  1720. public:
  1721. ScopedInitializer()
  1722. {
  1723. carla_register_all_plugins();
  1724. }
  1725. ~ScopedInitializer()
  1726. {
  1727. sPluginDescriptors.clear();
  1728. }
  1729. };
  1730. private:
  1731. PluginHandle fHandle;
  1732. PluginHandle fHandle2;
  1733. HostDescriptor fHost;
  1734. const PluginDescriptor* fDescriptor;
  1735. bool fIsProcessing;
  1736. bool fIsUiVisible;
  1737. float** fAudioInBuffers;
  1738. float** fAudioOutBuffers;
  1739. uint32_t fMidiEventCount;
  1740. ::MidiEvent fMidiEvents[MAX_MIDI_EVENTS*2];
  1741. NativePluginMidiData fMidiIn;
  1742. NativePluginMidiData fMidiOut;
  1743. ::TimeInfo fTimeInfo;
  1744. static NonRtList<const PluginDescriptor*> sPluginDescriptors;
  1745. // -------------------------------------------------------------------
  1746. #define handlePtr ((NativePlugin*)handle)
  1747. static uint32_t carla_host_get_buffer_size(HostHandle handle)
  1748. {
  1749. return handlePtr->handleGetBufferSize();
  1750. }
  1751. static double carla_host_get_sample_rate(HostHandle handle)
  1752. {
  1753. return handlePtr->handleGetSampleRate();
  1754. }
  1755. static const ::TimeInfo* carla_host_get_time_info(HostHandle handle)
  1756. {
  1757. return handlePtr->handleGetTimeInfo();
  1758. }
  1759. static bool carla_host_write_midi_event(HostHandle handle, const ::MidiEvent* event)
  1760. {
  1761. return handlePtr->handleWriteMidiEvent(event);
  1762. }
  1763. static void carla_host_ui_parameter_changed(HostHandle handle, uint32_t index, float value)
  1764. {
  1765. handlePtr->handleUiParameterChanged(index, value);
  1766. }
  1767. static void carla_host_ui_custom_data_changed(HostHandle handle, const char* key, const char* value)
  1768. {
  1769. handlePtr->handleUiCustomDataChanged(key, value);
  1770. }
  1771. static void carla_host_ui_closed(HostHandle handle)
  1772. {
  1773. handlePtr->handleUiClosed();
  1774. }
  1775. static const char* carla_host_ui_open_file(HostHandle handle, bool isDir, const char* title, const char* filter)
  1776. {
  1777. return handlePtr->handleUiOpenFile(isDir, title, filter);
  1778. }
  1779. static const char* carla_host_ui_save_file(HostHandle handle, bool isDir, const char* title, const char* filter)
  1780. {
  1781. return handlePtr->handleUiSaveFile(isDir, title, filter);
  1782. }
  1783. #undef handlePtr
  1784. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(NativePlugin)
  1785. };
  1786. NonRtList<const PluginDescriptor*> NativePlugin::sPluginDescriptors;
  1787. static const NativePlugin::ScopedInitializer _si;
  1788. CARLA_BACKEND_END_NAMESPACE
  1789. void carla_register_native_plugin(const PluginDescriptor* desc)
  1790. {
  1791. CARLA_BACKEND_USE_NAMESPACE
  1792. NativePlugin::registerPlugin(desc);
  1793. }
  1794. #else // WANT_NATIVE
  1795. # warning Building without Internal plugin support
  1796. #endif
  1797. CARLA_BACKEND_START_NAMESPACE
  1798. // -----------------------------------------------------------------------
  1799. #ifdef WANT_NATIVE
  1800. size_t CarlaPlugin::getNativePluginCount()
  1801. {
  1802. return NativePlugin::getPluginCount();
  1803. }
  1804. const PluginDescriptor* CarlaPlugin::getNativePluginDescriptor(const size_t index)
  1805. {
  1806. return NativePlugin::getPluginDescriptor(index);
  1807. }
  1808. #endif
  1809. // -----------------------------------------------------------------------
  1810. CarlaPlugin* CarlaPlugin::newNative(const Initializer& init)
  1811. {
  1812. carla_debug("CarlaPlugin::newNative({%p, \"%s\", \"%s\", \"%s\"})", init.engine, init.filename, init.name, init.label);
  1813. #ifdef WANT_NATIVE
  1814. NativePlugin* const plugin(new NativePlugin(init.engine, init.id));
  1815. if (! plugin->init(init.name, init.label))
  1816. {
  1817. delete plugin;
  1818. return nullptr;
  1819. }
  1820. plugin->reload();
  1821. if (init.engine->getProccessMode() == PROCESS_MODE_CONTINUOUS_RACK && ! CarlaPluginProtectedData::canRunInRack(plugin))
  1822. {
  1823. init.engine->setLastError("Carla's rack mode can only work with Mono or Stereo Internal plugins, sorry!");
  1824. delete plugin;
  1825. return nullptr;
  1826. }
  1827. return plugin;
  1828. #else
  1829. init.engine->setLastError("Internal plugins support not available");
  1830. return nullptr;
  1831. #endif
  1832. }
  1833. CARLA_BACKEND_END_NAMESPACE