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.

2119 lines
69KB

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