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.

2124 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. {
  761. kData->midiprog.createNew(count);
  762. // Update data
  763. for (i=0; i < count; i++)
  764. {
  765. const ::MidiProgram* const mpDesc = fDescriptor->get_midi_program_info(fHandle, i);
  766. CARLA_ASSERT(mpDesc != nullptr);
  767. CARLA_ASSERT(mpDesc->name != nullptr);
  768. kData->midiprog.data[i].bank = mpDesc->bank;
  769. kData->midiprog.data[i].program = mpDesc->program;
  770. kData->midiprog.data[i].name = carla_strdup(mpDesc->name);
  771. }
  772. }
  773. #ifndef BUILD_BRIDGE
  774. // Update OSC Names
  775. if (kData->engine->isOscControlRegistered())
  776. {
  777. kData->engine->osc_send_control_set_midi_program_count(fId, count);
  778. for (i=0; i < count; i++)
  779. 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);
  780. }
  781. #endif
  782. if (init)
  783. {
  784. if (count > 0)
  785. setMidiProgram(0, false, false, false);
  786. }
  787. else
  788. {
  789. // Check if current program is invalid
  790. bool programChanged = false;
  791. if (count == oldCount+1)
  792. {
  793. // one midi program added, probably created by user
  794. kData->midiprog.current = oldCount;
  795. programChanged = true;
  796. }
  797. else if (current < 0 && count > 0)
  798. {
  799. // programs exist now, but not before
  800. kData->midiprog.current = 0;
  801. programChanged = true;
  802. }
  803. else if (current >= 0 && count == 0)
  804. {
  805. // programs existed before, but not anymore
  806. kData->midiprog.current = -1;
  807. programChanged = true;
  808. }
  809. else if (current >= static_cast<int32_t>(count))
  810. {
  811. // current midi program > count
  812. kData->midiprog.current = 0;
  813. programChanged = true;
  814. }
  815. else
  816. {
  817. // no change
  818. kData->midiprog.current = current;
  819. }
  820. if (programChanged)
  821. setMidiProgram(kData->midiprog.current, true, true, true);
  822. kData->engine->callback(CALLBACK_RELOAD_PROGRAMS, fId, 0, 0, 0.0f, nullptr);
  823. }
  824. }
  825. // -------------------------------------------------------------------
  826. // Plugin processing
  827. void process(float** const inBuffer, float** const outBuffer, const uint32_t frames)
  828. {
  829. uint32_t i, k;
  830. // --------------------------------------------------------------------------------------------------------
  831. // Check if active
  832. if (! kData->active)
  833. {
  834. // disable any output sound
  835. for (i=0; i < kData->audioOut.count; i++)
  836. carla_zeroFloat(outBuffer[i], frames);
  837. if (kData->activeBefore)
  838. {
  839. if (fDescriptor->deactivate != nullptr)
  840. {
  841. fDescriptor->deactivate(fHandle);
  842. if (fHandle2 != nullptr)
  843. fDescriptor->deactivate(fHandle2);
  844. }
  845. }
  846. kData->activeBefore = kData->active;
  847. return;
  848. }
  849. fMidiEventCount = 0;
  850. carla_zeroMem(fMidiEvents, sizeof(::MidiEvent)*MAX_MIDI_EVENTS*2);
  851. // --------------------------------------------------------------------------------------------------------
  852. // Check if not active before
  853. if (kData->needsReset || ! kData->activeBefore)
  854. {
  855. if (fOptions & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  856. {
  857. for (k=0, i=MAX_MIDI_CHANNELS; k < MAX_MIDI_CHANNELS; k++)
  858. {
  859. fMidiEvents[k].data[0] = MIDI_STATUS_CONTROL_CHANGE + k;
  860. fMidiEvents[k].data[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  861. fMidiEvents[k+i].data[0] = MIDI_STATUS_CONTROL_CHANGE + k;
  862. fMidiEvents[k+i].data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  863. }
  864. fMidiEventCount = MAX_MIDI_CHANNELS*2;
  865. }
  866. if (fDescriptor->activate != nullptr)
  867. {
  868. fDescriptor->activate(fHandle);
  869. if (fHandle2 != nullptr)
  870. fDescriptor->activate(fHandle2);
  871. }
  872. }
  873. CARLA_PROCESS_CONTINUE_CHECK;
  874. // --------------------------------------------------------------------------------------------------------
  875. // Set TimeInfo
  876. const EngineTimeInfo& timeInfo = kData->engine->getTimeInfo();
  877. fTimeInfo.playing = timeInfo.playing;
  878. fTimeInfo.frame = timeInfo.frame;
  879. fTimeInfo.time = timeInfo.time;
  880. if (timeInfo.valid & EngineTimeInfo::ValidBBT)
  881. {
  882. fTimeInfo.bbt.valid = true;
  883. fTimeInfo.bbt.bar = timeInfo.bbt.bar;
  884. fTimeInfo.bbt.beat = timeInfo.bbt.beat;
  885. fTimeInfo.bbt.tick = timeInfo.bbt.tick;
  886. fTimeInfo.bbt.barStartTick = timeInfo.bbt.barStartTick;
  887. fTimeInfo.bbt.beatsPerBar = timeInfo.bbt.beatsPerBar;
  888. fTimeInfo.bbt.beatType = timeInfo.bbt.beatType;
  889. fTimeInfo.bbt.ticksPerBeat = timeInfo.bbt.ticksPerBeat;
  890. fTimeInfo.bbt.beatsPerMinute = timeInfo.bbt.beatsPerMinute;
  891. }
  892. else
  893. fTimeInfo.bbt.valid = false;
  894. CARLA_PROCESS_CONTINUE_CHECK;
  895. // --------------------------------------------------------------------------------------------------------
  896. // Event Input and Processing
  897. if (kData->event.portIn != nullptr && kData->activeBefore)
  898. {
  899. // ----------------------------------------------------------------------------------------------------
  900. // MIDI Input (External)
  901. if (kData->extNotes.mutex.tryLock())
  902. {
  903. while (fMidiEventCount < MAX_MIDI_EVENTS*2 && ! kData->extNotes.data.isEmpty())
  904. {
  905. const ExternalMidiNote& note = kData->extNotes.data.getFirst(true);
  906. CARLA_ASSERT(note.channel >= 0);
  907. fMidiEvents[fMidiEventCount].port = 0;
  908. fMidiEvents[fMidiEventCount].time = 0;
  909. fMidiEvents[fMidiEventCount].data[0] = (note.velo > 0) ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF;
  910. fMidiEvents[fMidiEventCount].data[0] += note.channel;
  911. fMidiEvents[fMidiEventCount].data[1] = note.note;
  912. fMidiEvents[fMidiEventCount].data[2] = note.velo;
  913. fMidiEventCount += 1;
  914. }
  915. kData->extNotes.mutex.unlock();
  916. } // End of MIDI Input (External)
  917. // ----------------------------------------------------------------------------------------------------
  918. // Event Input (System)
  919. bool allNotesOffSent = false;
  920. bool sampleAccurate = (fOptions & PLUGIN_OPTION_FIXED_BUFFER) == 0;
  921. uint32_t time, nEvents = kData->event.portIn->getEventCount();
  922. uint32_t startTime = 0;
  923. uint32_t timeOffset = 0;
  924. uint32_t nextBankId = 0;
  925. if (kData->midiprog.current >= 0 && kData->midiprog.count > 0)
  926. nextBankId = kData->midiprog.data[kData->midiprog.current].bank;
  927. for (i=0; i < nEvents; i++)
  928. {
  929. const EngineEvent& event = kData->event.portIn->getEvent(i);
  930. time = event.time;
  931. if (time >= frames)
  932. continue;
  933. CARLA_ASSERT_INT2(time >= timeOffset, time, timeOffset);
  934. if (time > timeOffset && sampleAccurate)
  935. {
  936. if (processSingle(inBuffer, outBuffer, time - timeOffset, timeOffset))
  937. {
  938. if (fMidiEventCount > 0)
  939. {
  940. //carla_zeroMem(fMidiEvents, sizeof(::MidiEvent)*fMidiEventCount);
  941. fMidiEventCount = 0;
  942. }
  943. nextBankId = 0;
  944. timeOffset = time;
  945. }
  946. else
  947. startTime += timeOffset;
  948. }
  949. // Control change
  950. switch (event.type)
  951. {
  952. case kEngineEventTypeNull:
  953. break;
  954. case kEngineEventTypeControl:
  955. {
  956. const EngineControlEvent& ctrlEvent = event.ctrl;
  957. switch (ctrlEvent.type)
  958. {
  959. case kEngineControlEventTypeNull:
  960. break;
  961. case kEngineControlEventTypeParameter:
  962. {
  963. // Control backend stuff
  964. if (event.channel == kData->ctrlChannel)
  965. {
  966. double value;
  967. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (fHints & PLUGIN_CAN_DRYWET) > 0)
  968. {
  969. value = ctrlEvent.value;
  970. setDryWet(value, false, false);
  971. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, value);
  972. continue;
  973. }
  974. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (fHints & PLUGIN_CAN_VOLUME) > 0)
  975. {
  976. value = ctrlEvent.value*127/100;
  977. setVolume(value, false, false);
  978. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
  979. continue;
  980. }
  981. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (fHints & PLUGIN_CAN_BALANCE) > 0)
  982. {
  983. double left, right;
  984. value = ctrlEvent.value/0.5 - 1.0;
  985. if (value < 0.0)
  986. {
  987. left = -1.0;
  988. right = (value*2)+1.0;
  989. }
  990. else if (value > 0.0)
  991. {
  992. left = (value*2)-1.0;
  993. right = 1.0;
  994. }
  995. else
  996. {
  997. left = -1.0;
  998. right = 1.0;
  999. }
  1000. setBalanceLeft(left, false, false);
  1001. setBalanceRight(right, false, false);
  1002. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  1003. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  1004. continue;
  1005. }
  1006. }
  1007. // Control plugin parameters
  1008. for (k=0; k < kData->param.count; k++)
  1009. {
  1010. if (kData->param.data[k].midiChannel != event.channel)
  1011. continue;
  1012. if (kData->param.data[k].midiCC != ctrlEvent.param)
  1013. continue;
  1014. if (kData->param.data[k].type != PARAMETER_INPUT)
  1015. continue;
  1016. if ((kData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  1017. continue;
  1018. double value;
  1019. if (kData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  1020. {
  1021. value = (ctrlEvent.value < 0.5f) ? kData->param.ranges[k].min : kData->param.ranges[k].max;
  1022. }
  1023. else
  1024. {
  1025. value = kData->param.ranges[i].unnormalizeValue(ctrlEvent.value);
  1026. if (kData->param.data[k].hints & PARAMETER_IS_INTEGER)
  1027. value = std::rint(value);
  1028. }
  1029. setParameterValue(k, value, false, false, false);
  1030. postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, value);
  1031. }
  1032. break;
  1033. }
  1034. case kEngineControlEventTypeMidiBank:
  1035. if (event.channel == kData->ctrlChannel)
  1036. nextBankId = ctrlEvent.param;
  1037. break;
  1038. case kEngineControlEventTypeMidiProgram:
  1039. if (event.channel == kData->ctrlChannel)
  1040. {
  1041. const uint32_t nextProgramId = ctrlEvent.param;
  1042. for (k=0; k < kData->midiprog.count; k++)
  1043. {
  1044. if (kData->midiprog.data[k].bank == nextBankId && kData->midiprog.data[k].program == nextProgramId)
  1045. {
  1046. setMidiProgram(k, false, false, false);
  1047. postponeRtEvent(kPluginPostRtEventMidiProgramChange, k, 0, 0.0f);
  1048. break;
  1049. }
  1050. }
  1051. }
  1052. break;
  1053. case kEngineControlEventTypeAllSoundOff:
  1054. if (event.channel == kData->ctrlChannel)
  1055. {
  1056. if (! allNotesOffSent)
  1057. {
  1058. allNotesOffSent = true;
  1059. sendMidiAllNotesOff();
  1060. }
  1061. if (fDescriptor->deactivate != nullptr)
  1062. {
  1063. fDescriptor->deactivate(fHandle);
  1064. if (fHandle2 != nullptr)
  1065. fDescriptor->deactivate(fHandle2);
  1066. }
  1067. if (fDescriptor->activate != nullptr)
  1068. {
  1069. fDescriptor->activate(fHandle);
  1070. if (fHandle2 != nullptr)
  1071. fDescriptor->activate(fHandle2);
  1072. }
  1073. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_ACTIVE, 0, 0.0f);
  1074. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_ACTIVE, 0, 1.0f);
  1075. }
  1076. if (fMidiEventCount >= MAX_MIDI_EVENTS*2)
  1077. continue;
  1078. if (fOptions & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1079. {
  1080. fMidiEvents[fMidiEventCount].port = 0;
  1081. fMidiEvents[fMidiEventCount].time = sampleAccurate ? startTime : time;
  1082. fMidiEvents[fMidiEventCount].data[0] = MIDI_STATUS_CONTROL_CHANGE + event.channel;
  1083. fMidiEvents[fMidiEventCount].data[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  1084. fMidiEvents[fMidiEventCount].data[2] = 0;
  1085. fMidiEventCount += 1;
  1086. }
  1087. break;
  1088. case kEngineControlEventTypeAllNotesOff:
  1089. if (event.channel == kData->ctrlChannel)
  1090. {
  1091. if (! allNotesOffSent)
  1092. {
  1093. allNotesOffSent = true;
  1094. sendMidiAllNotesOff();
  1095. }
  1096. }
  1097. if (fMidiEventCount >= MAX_MIDI_EVENTS*2)
  1098. continue;
  1099. if (fOptions & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1100. {
  1101. fMidiEvents[fMidiEventCount].port = 0;
  1102. fMidiEvents[fMidiEventCount].time = sampleAccurate ? startTime : time;
  1103. fMidiEvents[fMidiEventCount].data[0] = MIDI_STATUS_CONTROL_CHANGE + event.channel;
  1104. fMidiEvents[fMidiEventCount].data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  1105. fMidiEvents[fMidiEventCount].data[2] = 0;
  1106. fMidiEventCount += 1;
  1107. }
  1108. break;
  1109. }
  1110. break;
  1111. }
  1112. case kEngineEventTypeMidi:
  1113. {
  1114. if (fMidiEventCount >= MAX_MIDI_EVENTS*2)
  1115. continue;
  1116. const EngineMidiEvent& midiEvent = event.midi;
  1117. uint8_t status = MIDI_GET_STATUS_FROM_DATA(midiEvent.data);
  1118. uint8_t channel = event.channel;
  1119. if (MIDI_IS_STATUS_AFTERTOUCH(status) && (fOptions & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) == 0)
  1120. continue;
  1121. if (MIDI_IS_STATUS_CONTROL_CHANGE(status) && (fOptions & PLUGIN_OPTION_SEND_CONTROL_CHANGES) == 0)
  1122. continue;
  1123. if (MIDI_IS_STATUS_POLYPHONIC_AFTERTOUCH(status) && (fOptions & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) == 0)
  1124. continue;
  1125. if (MIDI_IS_STATUS_PITCH_WHEEL_CONTROL(status) && (fOptions & PLUGIN_OPTION_SEND_PITCHBEND) == 0)
  1126. continue;
  1127. // Fix bad note-off
  1128. if (status == MIDI_STATUS_NOTE_ON && midiEvent.data[2] == 0)
  1129. status -= 0x10;
  1130. fMidiEvents[fMidiEventCount].port = 0;
  1131. fMidiEvents[fMidiEventCount].time = sampleAccurate ? startTime : time;
  1132. fMidiEvents[fMidiEventCount].data[0] = status + channel;
  1133. fMidiEvents[fMidiEventCount].data[1] = midiEvent.data[1];
  1134. fMidiEvents[fMidiEventCount].data[2] = midiEvent.data[2];
  1135. fMidiEventCount += 1;
  1136. if (status == MIDI_STATUS_NOTE_ON)
  1137. postponeRtEvent(kPluginPostRtEventNoteOn, channel, midiEvent.data[1], midiEvent.data[2]);
  1138. else if (status == MIDI_STATUS_NOTE_OFF)
  1139. postponeRtEvent(kPluginPostRtEventNoteOff, channel, midiEvent.data[1], 0.0f);
  1140. break;
  1141. }
  1142. }
  1143. }
  1144. kData->postRtEvents.trySplice();
  1145. if (frames > timeOffset)
  1146. processSingle(inBuffer, outBuffer, frames - timeOffset, timeOffset);
  1147. } // End of Event Input and Processing
  1148. // --------------------------------------------------------------------------------------------------------
  1149. // Plugin processing (no events)
  1150. else
  1151. {
  1152. processSingle(inBuffer, outBuffer, frames, 0);
  1153. } // End of Plugin processing (no events)
  1154. CARLA_PROCESS_CONTINUE_CHECK;
  1155. // --------------------------------------------------------------------------------------------------------
  1156. // Control and MIDI Output
  1157. if (fMidiOut.count > 0 || kData->event.portOut != nullptr)
  1158. {
  1159. float value, curValue;
  1160. for (k=0; k < kData->param.count; k++)
  1161. {
  1162. if (kData->param.data[k].type != PARAMETER_OUTPUT)
  1163. continue;
  1164. curValue = fDescriptor->get_parameter_value(fHandle, k);
  1165. kData->param.ranges[k].fixValue(curValue);
  1166. if (kData->param.data[k].midiCC > 0)
  1167. {
  1168. value = kData->param.ranges[k].normalizeValue(curValue);
  1169. kData->event.portOut->writeControlEvent(0, kData->param.data[k].midiChannel, kEngineControlEventTypeParameter, kData->param.data[k].midiCC, value);
  1170. }
  1171. }
  1172. // reverse lookup MIDI events
  1173. for (k = (MAX_MIDI_EVENTS*2)-1; k >= fMidiEventCount; k--)
  1174. {
  1175. if (fMidiEvents[k].data[0] == 0)
  1176. break;
  1177. const uint8_t channel = MIDI_GET_CHANNEL_FROM_DATA(fMidiEvents[k].data);
  1178. const uint8_t port = fMidiEvents[k].port;
  1179. if (kData->event.portOut != nullptr)
  1180. kData->event.portOut->writeMidiEvent(fMidiEvents[k].time, channel, port, fMidiEvents[k].data, 3);
  1181. else if (port < fMidiOut.count)
  1182. fMidiOut.ports[port]->writeMidiEvent(fMidiEvents[k].time, channel, port, fMidiEvents[k].data, 3);
  1183. }
  1184. } // End of Control and MIDI Output
  1185. // --------------------------------------------------------------------------------------------------------
  1186. kData->activeBefore = kData->active;
  1187. }
  1188. bool processSingle(float** const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t timeOffset)
  1189. {
  1190. uint32_t i, k;
  1191. // --------------------------------------------------------------------------------------------------------
  1192. // Try lock, silence otherwise
  1193. if (kData->engine->isOffline())
  1194. {
  1195. kData->singleMutex.lock();
  1196. }
  1197. else if (! kData->singleMutex.tryLock())
  1198. {
  1199. for (i=0; i < kData->audioOut.count; i++)
  1200. {
  1201. for (k=0; k < frames; k++)
  1202. outBuffer[i][k+timeOffset] = 0.0f;
  1203. }
  1204. return false;
  1205. }
  1206. for (i=0; i < kData->audioIn.count; i++)
  1207. carla_copyFloat(fAudioInBuffers[i], inBuffer[i]+timeOffset, frames);
  1208. for (i=0; i < kData->audioOut.count; i++)
  1209. carla_zeroFloat(fAudioOutBuffers[i], frames);
  1210. fIsProcessing = true;
  1211. if (fHandle2 == nullptr)
  1212. {
  1213. fDescriptor->process(fHandle, fAudioInBuffers, fAudioOutBuffers, frames, fMidiEventCount, fMidiEvents);
  1214. }
  1215. else
  1216. {
  1217. fDescriptor->process(fHandle,
  1218. (kData->audioIn.count > 0) ? &fAudioInBuffers[0] : nullptr,
  1219. (kData->audioOut.count > 0) ? &fAudioOutBuffers[0] : nullptr,
  1220. frames, fMidiEventCount, fMidiEvents);
  1221. fDescriptor->process(fHandle2,
  1222. (kData->audioIn.count > 0) ? &fAudioInBuffers[1] : nullptr,
  1223. (kData->audioOut.count > 0) ? &fAudioOutBuffers[1] : nullptr,
  1224. frames, fMidiEventCount, fMidiEvents);
  1225. }
  1226. fIsProcessing = false;
  1227. fTimeInfo.frame += frames;
  1228. // --------------------------------------------------------------------------------------------------------
  1229. // Post-processing (dry/wet, volume and balance)
  1230. {
  1231. const bool doDryWet = (fHints & PLUGIN_CAN_DRYWET) > 0 && kData->postProc.dryWet != 1.0f;
  1232. const bool doBalance = (fHints & PLUGIN_CAN_BALANCE) > 0 && (kData->postProc.balanceLeft != -1.0f || kData->postProc.balanceRight != 1.0f);
  1233. float bufValue, oldBufLeft[doBalance ? frames : 1];
  1234. for (i=0; i < kData->audioOut.count; i++)
  1235. {
  1236. // Dry/Wet
  1237. if (doDryWet)
  1238. {
  1239. for (k=0; k < frames; k++)
  1240. {
  1241. bufValue = fAudioInBuffers[(kData->audioIn.count == 1) ? 0 : i][k];
  1242. fAudioOutBuffers[i][k] = (fAudioOutBuffers[i][k] * kData->postProc.dryWet) + (bufValue * (1.0f - kData->postProc.dryWet));
  1243. }
  1244. }
  1245. // Balance
  1246. if (doBalance)
  1247. {
  1248. if (i % 2 == 0)
  1249. carla_copyFloat(oldBufLeft, fAudioOutBuffers[i], frames);
  1250. float balRangeL = (kData->postProc.balanceLeft + 1.0f)/2.0f;
  1251. float balRangeR = (kData->postProc.balanceRight + 1.0f)/2.0f;
  1252. for (k=0; k < frames; k++)
  1253. {
  1254. if (i % 2 == 0)
  1255. {
  1256. // left
  1257. fAudioOutBuffers[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  1258. fAudioOutBuffers[i][k] += fAudioOutBuffers[i+1][k] * (1.0f - balRangeR);
  1259. }
  1260. else
  1261. {
  1262. // right
  1263. fAudioOutBuffers[i][k] = fAudioOutBuffers[i][k] * balRangeR;
  1264. fAudioOutBuffers[i][k] += oldBufLeft[k] * balRangeL;
  1265. }
  1266. }
  1267. }
  1268. // Volume (and buffer copy)
  1269. {
  1270. for (k=0; k < frames; k++)
  1271. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k] * kData->postProc.volume;
  1272. }
  1273. }
  1274. } // End of Post-processing
  1275. // --------------------------------------------------------------------------------------------------------
  1276. kData->singleMutex.unlock();
  1277. return true;
  1278. }
  1279. void bufferSizeChanged(const uint32_t newBufferSize)
  1280. {
  1281. for (uint32_t i=0; i < kData->audioIn.count; i++)
  1282. {
  1283. if (fAudioInBuffers[i] != nullptr)
  1284. delete[] fAudioInBuffers[i];
  1285. fAudioInBuffers[i] = new float[newBufferSize];
  1286. }
  1287. for (uint32_t i=0; i < kData->audioOut.count; i++)
  1288. {
  1289. if (fAudioOutBuffers[i] != nullptr)
  1290. delete[] fAudioOutBuffers[i];
  1291. fAudioOutBuffers[i] = new float[newBufferSize];
  1292. }
  1293. }
  1294. // -------------------------------------------------------------------
  1295. // Post-poned events
  1296. void uiParameterChange(const uint32_t index, const float value)
  1297. {
  1298. CARLA_ASSERT(fDescriptor != nullptr);
  1299. CARLA_ASSERT(fHandle != nullptr);
  1300. CARLA_ASSERT(index < kData->param.count);
  1301. if (! fIsUiVisible)
  1302. return;
  1303. if (fDescriptor == nullptr || fHandle == nullptr)
  1304. return;
  1305. if (index >= kData->param.count)
  1306. return;
  1307. if (fDescriptor->ui_set_parameter_value != nullptr)
  1308. fDescriptor->ui_set_parameter_value(fHandle, index, value);
  1309. }
  1310. void uiMidiProgramChange(const uint32_t index)
  1311. {
  1312. CARLA_ASSERT(fDescriptor != nullptr);
  1313. CARLA_ASSERT(fHandle != nullptr);
  1314. CARLA_ASSERT(index < kData->midiprog.count);
  1315. if (! fIsUiVisible)
  1316. return;
  1317. if (fDescriptor == nullptr || fHandle == nullptr)
  1318. return;
  1319. if (index >= kData->midiprog.count)
  1320. return;
  1321. if (fDescriptor->ui_set_midi_program != nullptr)
  1322. fDescriptor->ui_set_midi_program(fHandle, kData->midiprog.data[index].bank, kData->midiprog.data[index].program);
  1323. }
  1324. void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo)
  1325. {
  1326. CARLA_ASSERT(fDescriptor != nullptr);
  1327. CARLA_ASSERT(fHandle != nullptr);
  1328. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  1329. CARLA_ASSERT(note < MAX_MIDI_NOTE);
  1330. CARLA_ASSERT(velo > 0 && velo < MAX_MIDI_VALUE);
  1331. if (! fIsUiVisible)
  1332. return;
  1333. if (fDescriptor == nullptr || fHandle == nullptr)
  1334. return;
  1335. if (channel >= MAX_MIDI_CHANNELS)
  1336. return;
  1337. if (note >= MAX_MIDI_NOTE)
  1338. return;
  1339. if (velo >= MAX_MIDI_VALUE)
  1340. return;
  1341. // TODO
  1342. }
  1343. void uiNoteOff(const uint8_t channel, const uint8_t note)
  1344. {
  1345. CARLA_ASSERT(fDescriptor != nullptr);
  1346. CARLA_ASSERT(fHandle != nullptr);
  1347. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  1348. CARLA_ASSERT(note < MAX_MIDI_NOTE);
  1349. if (! fIsUiVisible)
  1350. return;
  1351. if (fDescriptor == nullptr || fHandle == nullptr)
  1352. return;
  1353. if (channel >= MAX_MIDI_CHANNELS)
  1354. return;
  1355. if (note >= MAX_MIDI_NOTE)
  1356. return;
  1357. // TODO
  1358. }
  1359. // -------------------------------------------------------------------
  1360. // Cleanup
  1361. void initBuffers()
  1362. {
  1363. fMidiIn.initBuffers(kData->engine);
  1364. fMidiOut.initBuffers(kData->engine);
  1365. CarlaPlugin::initBuffers();
  1366. }
  1367. void deleteBuffers()
  1368. {
  1369. carla_debug("NativePlugin::deleteBuffers() - start");
  1370. if (fAudioInBuffers != nullptr)
  1371. {
  1372. for (uint32_t i=0; i < kData->audioIn.count; i++)
  1373. {
  1374. if (fAudioInBuffers[i] != nullptr)
  1375. {
  1376. delete[] fAudioInBuffers[i];
  1377. fAudioInBuffers[i] = nullptr;
  1378. }
  1379. }
  1380. delete[] fAudioInBuffers;
  1381. fAudioInBuffers = nullptr;
  1382. }
  1383. if (fAudioOutBuffers != nullptr)
  1384. {
  1385. for (uint32_t i=0; i < kData->audioOut.count; i++)
  1386. {
  1387. if (fAudioOutBuffers[i] != nullptr)
  1388. {
  1389. delete[] fAudioOutBuffers[i];
  1390. fAudioOutBuffers[i] = nullptr;
  1391. }
  1392. }
  1393. delete[] fAudioOutBuffers;
  1394. fAudioOutBuffers = nullptr;
  1395. }
  1396. fMidiIn.clear();
  1397. fMidiOut.clear();
  1398. CarlaPlugin::deleteBuffers();
  1399. carla_debug("NativePlugin::deleteBuffers() - end");
  1400. }
  1401. // -------------------------------------------------------------------
  1402. protected:
  1403. uint32_t handleGetBufferSize()
  1404. {
  1405. return kData->engine->getBufferSize();
  1406. }
  1407. double handleGetSampleRate()
  1408. {
  1409. return kData->engine->getSampleRate();
  1410. }
  1411. const ::TimeInfo* handleGetTimeInfo()
  1412. {
  1413. CARLA_ASSERT(fIsProcessing);
  1414. return &fTimeInfo;
  1415. }
  1416. bool handleWriteMidiEvent(const ::MidiEvent* const event)
  1417. {
  1418. CARLA_ASSERT(fEnabled);
  1419. CARLA_ASSERT(fMidiOut.count > 0 || kData->event.portOut != nullptr);
  1420. CARLA_ASSERT(event != nullptr);
  1421. CARLA_ASSERT(fIsProcessing);
  1422. if (! fEnabled)
  1423. return false;
  1424. if (fMidiOut.count == 0)
  1425. return false;
  1426. if (event == nullptr)
  1427. return false;
  1428. if (! fIsProcessing)
  1429. {
  1430. carla_stderr2("NativePlugin::handleWriteMidiEvent(%p) - received MIDI out event outside audio thread, ignoring", event);
  1431. return false;
  1432. }
  1433. if (fMidiEventCount >= MAX_MIDI_EVENTS*2)
  1434. return false;
  1435. // reverse-find first free event, and put it there
  1436. for (uint32_t i=(MAX_MIDI_EVENTS*2)-1; i >= fMidiEventCount; i--)
  1437. {
  1438. if (fMidiEvents[i].data[0] == 0)
  1439. {
  1440. std::memcpy(&fMidiEvents[i], event, sizeof(::MidiEvent));
  1441. break;
  1442. }
  1443. }
  1444. return true;
  1445. }
  1446. void handleUiParameterChanged(const uint32_t index, const float value)
  1447. {
  1448. setParameterValue(index, value, false, true, true);
  1449. }
  1450. void handleUiCustomDataChanged(const char* const key, const char* const value)
  1451. {
  1452. setCustomData(CUSTOM_DATA_STRING, key, value, false);
  1453. }
  1454. void handleUiClosed()
  1455. {
  1456. kData->engine->callback(CALLBACK_SHOW_GUI, fId, 0, 0, 0.0f, nullptr);
  1457. fIsUiVisible = false;
  1458. }
  1459. const char* handleUiOpenFile(const bool isDir, const char* const title, const char* const filter)
  1460. {
  1461. static CarlaString retStr;
  1462. QFileDialog::Options options(isDir ? QFileDialog::ShowDirsOnly : 0x0);
  1463. retStr = QFileDialog::getOpenFileName(nullptr, title, "", filter, nullptr, options).toUtf8().constData();
  1464. return retStr.isNotEmpty() ? (const char*)retStr : nullptr;
  1465. }
  1466. const char* handleUiSaveFile(const bool isDir, const char* const title, const char* const filter)
  1467. {
  1468. static CarlaString retStr;
  1469. QFileDialog::Options options(isDir ? QFileDialog::ShowDirsOnly : 0x0);
  1470. retStr = QFileDialog::getSaveFileName(nullptr, title, "", filter, nullptr, options).toUtf8().constData();
  1471. return (const char*)retStr;
  1472. }
  1473. public:
  1474. static size_t getPluginCount()
  1475. {
  1476. return sPluginDescriptors.count();
  1477. }
  1478. static const PluginDescriptor* getPluginDescriptor(const size_t index)
  1479. {
  1480. CARLA_ASSERT(index < sPluginDescriptors.count());
  1481. if (index < sPluginDescriptors.count())
  1482. return sPluginDescriptors.getAt(index);
  1483. return nullptr;
  1484. }
  1485. static void registerPlugin(const PluginDescriptor* desc)
  1486. {
  1487. sPluginDescriptors.append(desc);
  1488. }
  1489. // -------------------------------------------------------------------
  1490. bool init(const char* const name, const char* const label)
  1491. {
  1492. CARLA_ASSERT(kData->engine != nullptr);
  1493. CARLA_ASSERT(kData->client == nullptr);
  1494. CARLA_ASSERT(label);
  1495. // ---------------------------------------------------------------
  1496. // get descriptor that matches label
  1497. for (auto it = sPluginDescriptors.begin(); it.valid(); it.next())
  1498. {
  1499. fDescriptor = *it;
  1500. CARLA_ASSERT(fDescriptor != nullptr);
  1501. if (fDescriptor == nullptr)
  1502. break;
  1503. if (fDescriptor->label != nullptr && std::strcmp(fDescriptor->label, label) == 0)
  1504. break;
  1505. fDescriptor = nullptr;
  1506. }
  1507. if (fDescriptor == nullptr)
  1508. {
  1509. kData->engine->setLastError("Invalid internal plugin");
  1510. return false;
  1511. }
  1512. // ---------------------------------------------------------------
  1513. // get info
  1514. if (name != nullptr)
  1515. fName = kData->engine->getNewUniquePluginName(name);
  1516. else if (fDescriptor->name != nullptr)
  1517. fName = kData->engine->getNewUniquePluginName(fDescriptor->name);
  1518. else
  1519. fName = kData->engine->getNewUniquePluginName(fDescriptor->name);
  1520. // ---------------------------------------------------------------
  1521. // register client
  1522. kData->client = kData->engine->addClient(this);
  1523. if (kData->client == nullptr || ! kData->client->isOk())
  1524. {
  1525. kData->engine->setLastError("Failed to register plugin client");
  1526. return false;
  1527. }
  1528. // ---------------------------------------------------------------
  1529. // initialize plugin
  1530. fHandle = fDescriptor->instantiate(fDescriptor, &fHost);
  1531. if (fHandle == nullptr)
  1532. {
  1533. kData->engine->setLastError("Plugin failed to initialize");
  1534. return false;
  1535. }
  1536. return true;
  1537. }
  1538. class ScopedInitializer
  1539. {
  1540. public:
  1541. ScopedInitializer()
  1542. {
  1543. initDescriptors();
  1544. }
  1545. ~ScopedInitializer()
  1546. {
  1547. clearDescriptors();
  1548. }
  1549. };
  1550. private:
  1551. PluginHandle fHandle;
  1552. PluginHandle fHandle2;
  1553. HostDescriptor fHost;
  1554. const PluginDescriptor* fDescriptor;
  1555. bool fIsProcessing;
  1556. bool fIsUiVisible;
  1557. float** fAudioInBuffers;
  1558. float** fAudioOutBuffers;
  1559. uint32_t fMidiEventCount;
  1560. ::MidiEvent fMidiEvents[MAX_MIDI_EVENTS*2];
  1561. NativePluginMidiData fMidiIn;
  1562. NativePluginMidiData fMidiOut;
  1563. ::TimeInfo fTimeInfo;
  1564. static NonRtList<const PluginDescriptor*> sPluginDescriptors;
  1565. // -------------------------------------------------------------------
  1566. static void initDescriptors()
  1567. {
  1568. #ifndef BUILD_BRIDGE
  1569. carla_register_native_plugin_bypass();
  1570. carla_register_native_plugin_midiSequencer();
  1571. carla_register_native_plugin_midiSplit();
  1572. carla_register_native_plugin_midiThrough();
  1573. carla_register_native_plugin_midiTranspose();
  1574. carla_register_native_plugin_nekofilter();
  1575. # ifdef WANT_OPENGL
  1576. carla_register_native_plugin_3BandEQ();
  1577. carla_register_native_plugin_3BandSplitter();
  1578. carla_register_native_plugin_PingPongPan();
  1579. # endif
  1580. carla_register_native_plugin_Notes();
  1581. # ifdef WANT_AUDIOFILE
  1582. carla_register_native_plugin_audiofile();
  1583. # endif
  1584. # ifdef WANT_ZYNADDSUBFX
  1585. carla_register_native_plugin_zynaddsubfx();
  1586. # endif
  1587. #endif
  1588. }
  1589. static void clearDescriptors()
  1590. {
  1591. sPluginDescriptors.clear();
  1592. }
  1593. // -------------------------------------------------------------------
  1594. #define handlePtr ((NativePlugin*)handle)
  1595. static uint32_t carla_host_get_buffer_size(HostHandle handle)
  1596. {
  1597. return handlePtr->handleGetBufferSize();
  1598. }
  1599. static double carla_host_get_sample_rate(HostHandle handle)
  1600. {
  1601. return handlePtr->handleGetSampleRate();
  1602. }
  1603. static const ::TimeInfo* carla_host_get_time_info(HostHandle handle)
  1604. {
  1605. return handlePtr->handleGetTimeInfo();
  1606. }
  1607. static bool carla_host_write_midi_event(HostHandle handle, const ::MidiEvent* event)
  1608. {
  1609. return handlePtr->handleWriteMidiEvent(event);
  1610. }
  1611. static void carla_host_ui_parameter_changed(HostHandle handle, uint32_t index, float value)
  1612. {
  1613. handlePtr->handleUiParameterChanged(index, value);
  1614. }
  1615. static void carla_host_ui_custom_data_changed(HostHandle handle, const char* key, const char* value)
  1616. {
  1617. handlePtr->handleUiCustomDataChanged(key, value);
  1618. }
  1619. static void carla_host_ui_closed(HostHandle handle)
  1620. {
  1621. handlePtr->handleUiClosed();
  1622. }
  1623. static const char* carla_host_ui_open_file(HostHandle handle, bool isDir, const char* title, const char* filter)
  1624. {
  1625. return handlePtr->handleUiOpenFile(isDir, title, filter);
  1626. }
  1627. static const char* carla_host_ui_save_file(HostHandle handle, bool isDir, const char* title, const char* filter)
  1628. {
  1629. return handlePtr->handleUiSaveFile(isDir, title, filter);
  1630. }
  1631. #undef handlePtr
  1632. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(NativePlugin)
  1633. };
  1634. NonRtList<const PluginDescriptor*> NativePlugin::sPluginDescriptors;
  1635. static const NativePlugin::ScopedInitializer _si;
  1636. CARLA_BACKEND_END_NAMESPACE
  1637. void carla_register_native_plugin(const PluginDescriptor* desc)
  1638. {
  1639. CARLA_BACKEND_USE_NAMESPACE
  1640. NativePlugin::registerPlugin(desc);
  1641. }
  1642. #else // WANT_NATIVE
  1643. # warning Building without Internal plugin support
  1644. #endif
  1645. CARLA_BACKEND_START_NAMESPACE
  1646. size_t CarlaPlugin::getNativePluginCount()
  1647. {
  1648. #ifdef WANT_NATIVE
  1649. return NativePlugin::getPluginCount();
  1650. #else
  1651. return 0;
  1652. #endif
  1653. }
  1654. const PluginDescriptor* CarlaPlugin::getNativePluginDescriptor(const size_t index)
  1655. {
  1656. #ifdef WANT_NATIVE
  1657. return NativePlugin::getPluginDescriptor(index);
  1658. #else
  1659. return nullptr;
  1660. // unused
  1661. (void)index;
  1662. #endif
  1663. }
  1664. // -----------------------------------------------------------------------
  1665. CarlaPlugin* CarlaPlugin::newNative(const Initializer& init)
  1666. {
  1667. carla_debug("CarlaPlugin::newNative(%p, \"%s\", \"%s\", \"%s\")", init.engine, init.filename, init.name, init.label);
  1668. #ifdef WANT_NATIVE
  1669. NativePlugin* const plugin = new NativePlugin(init.engine, init.id);
  1670. if (! plugin->init(init.name, init.label))
  1671. {
  1672. delete plugin;
  1673. return nullptr;
  1674. }
  1675. plugin->reload();
  1676. if (init.engine->getProccessMode() == PROCESS_MODE_CONTINUOUS_RACK && ! CarlaPluginProtectedData::canRunInRack(plugin))
  1677. {
  1678. init.engine->setLastError("Carla's rack mode can only work with Mono or Stereo Internal plugins, sorry!");
  1679. delete plugin;
  1680. return nullptr;
  1681. }
  1682. return plugin;
  1683. #else
  1684. init.engine->setLastError("Internal plugins support not available");
  1685. return nullptr;
  1686. #endif
  1687. }
  1688. CARLA_BACKEND_END_NAMESPACE