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.

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