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.

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