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.

2264 lines
73KB

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