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.

2090 lines
68KB

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