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.

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