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.

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