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.

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