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.

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