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.

2223 lines
72KB

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