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.

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