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.

1428 lines
48KB

  1. /*
  2. * Carla LinuxSampler Plugin
  3. * Copyright (C) 2011-2014 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 doc/GPL.txt file.
  16. */
  17. /* TODO
  18. * - implement buffer size changes
  19. * - implement sample rate changes
  20. * - call outDev->ReconnectAll() after changing buffer size or sample rate
  21. * - use CARLA_SAFE_ASSERT_RETURN with err
  22. */
  23. #include "CarlaPluginInternal.hpp"
  24. #include "CarlaEngine.hpp"
  25. #ifdef WANT_LINUXSAMPLER
  26. #include "CarlaBackendUtils.hpp"
  27. #include "CarlaMathUtils.hpp"
  28. #include "linuxsampler/EngineFactory.h"
  29. #include <linuxsampler/Sampler.h>
  30. // FIXME
  31. #include <QtCore/QFileInfo>
  32. #include <QtCore/QStringList>
  33. namespace LinuxSampler {
  34. using CarlaBackend::CarlaEngine;
  35. using CarlaBackend::CarlaPlugin;
  36. // -----------------------------------------------------------------------
  37. // LinuxSampler static values
  38. static const float kVolumeMax = 3.16227766f; // +10 dB
  39. // -----------------------------------------------------------------------
  40. // LinuxSampler AudioOutputDevice Plugin
  41. class AudioOutputDevicePlugin : public AudioOutputDevice
  42. {
  43. public:
  44. AudioOutputDevicePlugin(const CarlaEngine* const engine, const CarlaPlugin* const plugin, const bool uses16Outs)
  45. : AudioOutputDevice(std::map<String, DeviceCreationParameter*>()),
  46. fEngine(engine),
  47. fPlugin(plugin)
  48. {
  49. CARLA_ASSERT(engine != nullptr);
  50. CARLA_ASSERT(plugin != nullptr);
  51. AcquireChannels(uses16Outs ? 32 : 2);
  52. }
  53. ~AudioOutputDevicePlugin() override {}
  54. // -------------------------------------------------------------------
  55. // LinuxSampler virtual methods
  56. void Play() override
  57. {
  58. }
  59. bool IsPlaying() override
  60. {
  61. return (fEngine->isRunning() && fPlugin->isEnabled());
  62. }
  63. void Stop() override
  64. {
  65. }
  66. uint MaxSamplesPerCycle() override
  67. {
  68. return fEngine->getBufferSize();
  69. }
  70. uint SampleRate() override
  71. {
  72. return uint(fEngine->getSampleRate());
  73. }
  74. String Driver() override
  75. {
  76. return "AudioOutputDevicePlugin";
  77. }
  78. AudioChannel* CreateChannel(uint channelNr) override
  79. {
  80. return new AudioChannel(channelNr, nullptr, 0);
  81. }
  82. // -------------------------------------------------------------------
  83. // Give public access to the RenderAudio call
  84. int Render(const uint samples)
  85. {
  86. return RenderAudio(samples);
  87. }
  88. // -------------------------------------------------------------------
  89. private:
  90. const CarlaEngine* const fEngine;
  91. const CarlaPlugin* const fPlugin;
  92. };
  93. // -----------------------------------------------------------------------
  94. // LinuxSampler MidiInputPort Plugin
  95. class MidiInputPortPlugin : public MidiInputPort
  96. {
  97. public:
  98. MidiInputPortPlugin(MidiInputDevice* const device, const int portNum)
  99. : MidiInputPort(device, portNum)
  100. {
  101. }
  102. ~MidiInputPortPlugin() override {}
  103. };
  104. // -----------------------------------------------------------------------
  105. // LinuxSampler MidiInputDevice Plugin
  106. class MidiInputDevicePlugin : public MidiInputDevice
  107. {
  108. public:
  109. MidiInputDevicePlugin(Sampler* const sampler)
  110. : MidiInputDevice(std::map<String, DeviceCreationParameter*>(), sampler)
  111. {
  112. }
  113. // -------------------------------------------------------------------
  114. // LinuxSampler virtual methods
  115. void Listen() override
  116. {
  117. }
  118. void StopListen() override
  119. {
  120. }
  121. String Driver() override
  122. {
  123. return "MidiInputDevicePlugin";
  124. }
  125. MidiInputPort* CreateMidiPort() override
  126. {
  127. return new MidiInputPortPlugin(this, int(Ports.size()));
  128. }
  129. MidiInputPortPlugin* CreateMidiPortPlugin()
  130. {
  131. return new MidiInputPortPlugin(this, int(Ports.size()));
  132. }
  133. };
  134. } // namespace LinuxSampler
  135. // -----------------------------------------------------------------------
  136. CARLA_BACKEND_START_NAMESPACE
  137. #if 0
  138. }
  139. #endif
  140. class LinuxSamplerPlugin : public CarlaPlugin
  141. {
  142. public:
  143. LinuxSamplerPlugin(CarlaEngine* const engine, const uint id, const char* const format, const bool use16Outs)
  144. : CarlaPlugin(engine, id),
  145. fUses16Outs(use16Outs),
  146. fFormat(carla_strdup(format)),
  147. fLabel(nullptr),
  148. fMaker(nullptr),
  149. fRealName(nullptr),
  150. fEngine(nullptr),
  151. fAudioOutputDevice(nullptr),
  152. fMidiInputDevice(nullptr),
  153. fMidiInputPort(nullptr),
  154. fInstrument(nullptr)
  155. {
  156. carla_debug("LinuxSamplerPlugin::LinuxSamplerPlugin(%p, %i, %s, %s)", engine, id, format, bool2str(use16Outs));
  157. for (int i=0; i < MAX_MIDI_CHANNELS; ++i)
  158. {
  159. fCurMidiProgs[0] = 0;
  160. fSamplerChannels[i] = nullptr;
  161. fEngineChannels[i] = nullptr;
  162. }
  163. }
  164. ~LinuxSamplerPlugin() override
  165. {
  166. carla_debug("LinuxSamplerPlugin::~LinuxSamplerPlugin()");
  167. pData->singleMutex.lock();
  168. pData->masterMutex.lock();
  169. if (pData->client != nullptr && pData->client->isActive())
  170. pData->client->deactivate();
  171. if (pData->active)
  172. {
  173. deactivate();
  174. pData->active = false;
  175. }
  176. if (fEngine != nullptr)
  177. {
  178. if (fMidiInputDevice != nullptr)
  179. {
  180. if (fMidiInputPort != nullptr)
  181. {
  182. for (int i=0; i < MAX_MIDI_CHANNELS; ++i)
  183. {
  184. if (fSamplerChannels[i] != nullptr)
  185. {
  186. if (fEngineChannels[i] != nullptr)
  187. {
  188. fMidiInputPort->Disconnect(fEngineChannels[i]);
  189. fEngineChannels[i]->DisconnectAudioOutputDevice();
  190. fEngineChannels[i] = nullptr;
  191. }
  192. fSampler.RemoveSamplerChannel(fSamplerChannels[i]);
  193. fSamplerChannels[i] = nullptr;
  194. }
  195. }
  196. delete fMidiInputPort;
  197. fMidiInputPort = nullptr;
  198. }
  199. delete fMidiInputDevice;
  200. fMidiInputDevice = nullptr;
  201. }
  202. if (fAudioOutputDevice != nullptr)
  203. {
  204. delete fAudioOutputDevice;
  205. fAudioOutputDevice = nullptr;
  206. }
  207. fInstrument = nullptr;
  208. LinuxSampler::EngineFactory::Destroy(fEngine);
  209. fEngine = nullptr;
  210. }
  211. fInstrumentIds.clear();
  212. if (fFormat != nullptr)
  213. {
  214. delete[] fFormat;
  215. fFormat = nullptr;
  216. }
  217. if (fLabel != nullptr)
  218. {
  219. delete[] fLabel;
  220. fLabel = nullptr;
  221. }
  222. if (fMaker != nullptr)
  223. {
  224. delete[] fMaker;
  225. fMaker = nullptr;
  226. }
  227. if (fRealName != nullptr)
  228. {
  229. delete[] fRealName;
  230. fRealName = nullptr;
  231. }
  232. clearBuffers();
  233. }
  234. // -------------------------------------------------------------------
  235. // Information (base)
  236. PluginType getType() const noexcept override
  237. {
  238. return getPluginTypeFromString(fFormat);
  239. }
  240. PluginCategory getCategory() const noexcept override
  241. {
  242. return PLUGIN_CATEGORY_SYNTH;
  243. }
  244. // -------------------------------------------------------------------
  245. // Information (count)
  246. // nothing
  247. // -------------------------------------------------------------------
  248. // Information (current data)
  249. // nothing
  250. // -------------------------------------------------------------------
  251. // Information (per-plugin data)
  252. uint getOptionsAvailable() const noexcept override
  253. {
  254. uint options = 0x0;
  255. options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  256. options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  257. options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  258. options |= PLUGIN_OPTION_SEND_PITCHBEND;
  259. options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  260. return options;
  261. }
  262. void getLabel(char* const strBuf) const noexcept override
  263. {
  264. if (fLabel != nullptr)
  265. {
  266. std::strncpy(strBuf, fLabel, STR_MAX);
  267. return;
  268. }
  269. CarlaPlugin::getLabel(strBuf);
  270. }
  271. void getMaker(char* const strBuf) const noexcept override
  272. {
  273. if (fMaker != nullptr)
  274. {
  275. std::strncpy(strBuf, fMaker, STR_MAX);
  276. return;
  277. }
  278. CarlaPlugin::getMaker(strBuf);
  279. }
  280. void getCopyright(char* const strBuf) const noexcept override
  281. {
  282. getMaker(strBuf);
  283. }
  284. void getRealName(char* const strBuf) const noexcept override
  285. {
  286. if (fRealName != nullptr)
  287. {
  288. std::strncpy(strBuf, fRealName, STR_MAX);
  289. return;
  290. }
  291. CarlaPlugin::getRealName(strBuf);
  292. }
  293. // -------------------------------------------------------------------
  294. // Set data (state)
  295. void prepareForSave() override
  296. {
  297. char strBuf[STR_MAX+1];
  298. std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i", fCurMidiProgs[0], fCurMidiProgs[1], fCurMidiProgs[2], fCurMidiProgs[3],
  299. fCurMidiProgs[4], fCurMidiProgs[5], fCurMidiProgs[6], fCurMidiProgs[7],
  300. fCurMidiProgs[8], fCurMidiProgs[9], fCurMidiProgs[10], fCurMidiProgs[11],
  301. fCurMidiProgs[12], fCurMidiProgs[13], fCurMidiProgs[14], fCurMidiProgs[15]);
  302. CarlaPlugin::setCustomData(CUSTOM_DATA_TYPE_STRING, "midiPrograms", strBuf, false);
  303. }
  304. // -------------------------------------------------------------------
  305. // Set data (internal stuff)
  306. void setCtrlChannel(const int8_t channel, const bool sendOsc, const bool sendCallback) noexcept override
  307. {
  308. if (channel >= 0 && channel < MAX_MIDI_CHANNELS)
  309. pData->midiprog.current = fCurMidiProgs[channel];
  310. CarlaPlugin::setCtrlChannel(channel, sendOsc, sendCallback);
  311. }
  312. // -------------------------------------------------------------------
  313. // Set data (plugin-specific stuff)
  314. void setCustomData(const char* const type, const char* const key, const char* const value, const bool sendGui) override
  315. {
  316. CARLA_SAFE_ASSERT_RETURN(type != nullptr && type[0] != '\0',);
  317. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  318. CARLA_SAFE_ASSERT_RETURN(value != nullptr && value[0] != '\0',);
  319. carla_debug("LinuxSamplerPlugin::setCustomData(%s, \"%s\", \"%s\", %s)", type, key, value, bool2str(sendGui));
  320. if (std::strcmp(type, CUSTOM_DATA_TYPE_STRING) != 0)
  321. return carla_stderr2("LinuxSamplerPlugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - type is not string", type, key, value, bool2str(sendGui));
  322. if (std::strcmp(key, "midiPrograms") != 0)
  323. return carla_stderr2("LinuxSamplerPlugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - type is not string", type, key, value, bool2str(sendGui));
  324. if (fUses16Outs)
  325. {
  326. QStringList midiProgramList(QString(value).split(":", QString::SkipEmptyParts));
  327. if (midiProgramList.count() == MAX_MIDI_CHANNELS)
  328. {
  329. uint i = 0;
  330. foreach (const QString& midiProg, midiProgramList)
  331. {
  332. CARLA_SAFE_ASSERT_BREAK(i < MAX_MIDI_CHANNELS);
  333. bool ok;
  334. int index = midiProg.toInt(&ok);
  335. if (ok && index >= 0 && index < static_cast<int>(pData->midiprog.count))
  336. {
  337. const uint32_t bank = pData->midiprog.data[index].bank;
  338. const uint32_t program = pData->midiprog.data[index].program;
  339. const uint32_t rIndex = bank*128 + program;
  340. /*if (pData->engine->isOffline())
  341. {
  342. fEngineChannels[i]->PrepareLoadInstrument(pData->filename, rIndex);
  343. fEngineChannels[i]->LoadInstrument();
  344. }
  345. else*/
  346. {
  347. fInstrument->LoadInstrumentInBackground(fInstrumentIds[rIndex], fEngineChannels[i]);
  348. }
  349. fCurMidiProgs[i] = index;
  350. if (pData->ctrlChannel == static_cast<int32_t>(i))
  351. {
  352. pData->midiprog.current = index;
  353. pData->engine->callback(ENGINE_CALLBACK_MIDI_PROGRAM_CHANGED, pData->id, index, 0, 0.0f, nullptr);
  354. }
  355. }
  356. ++i;
  357. }
  358. CARLA_SAFE_ASSERT(i == MAX_MIDI_CHANNELS);
  359. }
  360. }
  361. CarlaPlugin::setCustomData(type, key, value, sendGui);
  362. }
  363. void setMidiProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  364. {
  365. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->midiprog.count),);
  366. if (index >= 0 && pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  367. {
  368. const uint32_t bank = pData->midiprog.data[index].bank;
  369. const uint32_t program = pData->midiprog.data[index].program;
  370. const uint32_t rIndex = bank*128 + program;
  371. LinuxSampler::EngineChannel* const engineChannel(fEngineChannels[pData->ctrlChannel]);
  372. const ScopedSingleProcessLocker spl(this, (sendGui || sendOsc || sendCallback));
  373. /*if (pData->engine->isOffline())
  374. {
  375. engineChannel->PrepareLoadInstrument(pData->filename, rIndex);
  376. engineChannel->LoadInstrument();
  377. }
  378. else*/
  379. {
  380. try {
  381. fInstrument->LoadInstrumentInBackground(fInstrumentIds[rIndex], engineChannel);
  382. } catch(...) {}
  383. }
  384. fCurMidiProgs[pData->ctrlChannel] = index;
  385. }
  386. CarlaPlugin::setMidiProgram(index, sendGui, sendOsc, sendCallback);
  387. }
  388. // -------------------------------------------------------------------
  389. // Set ui stuff
  390. // nothing
  391. // -------------------------------------------------------------------
  392. // Plugin state
  393. void reload() override
  394. {
  395. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr,);
  396. CARLA_SAFE_ASSERT_RETURN(fInstrument != nullptr,);
  397. carla_debug("LinuxSamplerPlugin::reload() - start");
  398. const EngineProcessMode processMode(pData->engine->getProccessMode());
  399. // Safely disable plugin for reload
  400. const ScopedDisabler sd(this);
  401. if (pData->active)
  402. deactivate();
  403. clearBuffers();
  404. uint32_t aOuts;
  405. aOuts = fUses16Outs ? 32 : 2;
  406. pData->audioOut.createNew(aOuts);
  407. const uint portNameSize(pData->engine->getMaxPortNameSize());
  408. CarlaString portName;
  409. // ---------------------------------------
  410. // Audio Outputs
  411. if (fUses16Outs)
  412. {
  413. for (uint32_t i=0; i < 32; ++i)
  414. {
  415. portName.clear();
  416. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  417. {
  418. portName = pData->name;
  419. portName += ":";
  420. }
  421. portName += "out-";
  422. if ((i+2)/2 < 9)
  423. portName += "0";
  424. portName += CarlaString((i+2)/2);
  425. if (i % 2 == 0)
  426. portName += "L";
  427. else
  428. portName += "R";
  429. portName.truncate(portNameSize);
  430. pData->audioOut.ports[i].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  431. pData->audioOut.ports[i].rindex = i;
  432. }
  433. }
  434. else
  435. {
  436. // out-left
  437. portName.clear();
  438. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  439. {
  440. portName = pData->name;
  441. portName += ":";
  442. }
  443. portName += "out-left";
  444. portName.truncate(portNameSize);
  445. pData->audioOut.ports[0].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  446. pData->audioOut.ports[0].rindex = 0;
  447. // out-right
  448. portName.clear();
  449. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  450. {
  451. portName = pData->name;
  452. portName += ":";
  453. }
  454. portName += "out-right";
  455. portName.truncate(portNameSize);
  456. pData->audioOut.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  457. pData->audioOut.ports[1].rindex = 1;
  458. }
  459. // ---------------------------------------
  460. // Event Input
  461. {
  462. portName.clear();
  463. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  464. {
  465. portName = pData->name;
  466. portName += ":";
  467. }
  468. portName += "events-in";
  469. portName.truncate(portNameSize);
  470. pData->event.portIn = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true);
  471. }
  472. // ---------------------------------------
  473. // plugin hints
  474. pData->hints = 0x0;
  475. pData->hints |= PLUGIN_IS_SYNTH;
  476. pData->hints |= PLUGIN_CAN_VOLUME;
  477. if (! fUses16Outs)
  478. pData->hints |= PLUGIN_CAN_BALANCE;
  479. // extra plugin hints
  480. pData->extraHints = 0x0;
  481. pData->extraHints |= PLUGIN_EXTRA_HINT_HAS_MIDI_IN;
  482. if (fUses16Outs)
  483. pData->extraHints |= PLUGIN_EXTRA_HINT_USES_MULTI_PROGS;
  484. else
  485. pData->extraHints |= PLUGIN_EXTRA_HINT_CAN_RUN_RACK;
  486. bufferSizeChanged(pData->engine->getBufferSize());
  487. reloadPrograms(true);
  488. if (pData->active)
  489. activate();
  490. carla_debug("LinuxSamplerPlugin::reload() - end");
  491. }
  492. void reloadPrograms(bool doInit) override
  493. {
  494. carla_debug("LinuxSamplerPlugin::reloadPrograms(%s)", bool2str(doInit));
  495. // Delete old programs
  496. pData->midiprog.clear();
  497. // Query new programs
  498. uint32_t count = uint32_t(fInstrumentIds.size());
  499. // sound kits must always have at least 1 midi-program
  500. CARLA_SAFE_ASSERT_RETURN(count > 0,);
  501. pData->midiprog.createNew(count);
  502. // Update data
  503. LinuxSampler::InstrumentManager::instrument_info_t info;
  504. for (uint32_t i=0; i < pData->midiprog.count; ++i)
  505. {
  506. pData->midiprog.data[i].bank = i / 128;
  507. pData->midiprog.data[i].program = i % 128;
  508. try {
  509. info = fInstrument->GetInstrumentInfo(fInstrumentIds[i]);
  510. }
  511. catch (const LinuxSampler::InstrumentManagerException&)
  512. {
  513. continue;
  514. }
  515. pData->midiprog.data[i].name = carla_strdup(info.InstrumentName.c_str());
  516. }
  517. #ifndef BUILD_BRIDGE
  518. // Update OSC Names
  519. if (pData->engine->isOscControlRegistered())
  520. {
  521. pData->engine->oscSend_control_set_midi_program_count(pData->id, count);
  522. for (uint32_t i=0; i < count; ++i)
  523. pData->engine->oscSend_control_set_midi_program_data(pData->id, i, pData->midiprog.data[i].bank, pData->midiprog.data[i].program, pData->midiprog.data[i].name);
  524. }
  525. #endif
  526. if (doInit)
  527. {
  528. for (int i=0; i < MAX_MIDI_CHANNELS; ++i)
  529. {
  530. CARLA_SAFE_ASSERT_CONTINUE(fEngineChannels[i] != nullptr);
  531. /*fEngineChannels[i]->PrepareLoadInstrument(pData->filename, 0);
  532. fEngineChannels[i]->LoadInstrument();*/
  533. fInstrument->LoadInstrumentInBackground(fInstrumentIds[0], fEngineChannels[i]);
  534. fCurMidiProgs[i] = 0;
  535. }
  536. pData->midiprog.current = 0;
  537. }
  538. else
  539. {
  540. pData->engine->callback(ENGINE_CALLBACK_RELOAD_PROGRAMS, pData->id, 0, 0, 0.0f, nullptr);
  541. }
  542. }
  543. // -------------------------------------------------------------------
  544. // Plugin processing
  545. #if 0
  546. void activate() override
  547. {
  548. for (int i=0; i < MAX_MIDI_CHANNELS; ++i)
  549. {
  550. if (fAudioOutputDevices[i] != nullptr)
  551. fAudioOutputDevices[i]->Play();
  552. }
  553. }
  554. void deactivate() override
  555. {
  556. for (int i=0; i < MAX_MIDI_CHANNELS; ++i)
  557. {
  558. if (fAudioOutputDevices[i] != nullptr)
  559. fAudioOutputDevices[i]->Stop();
  560. }
  561. }
  562. #endif
  563. void process(float** const, float** const outBuffer, const uint32_t frames) override
  564. {
  565. // --------------------------------------------------------------------------------------------------------
  566. // Check if active
  567. if (! pData->active)
  568. {
  569. // disable any output sound
  570. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  571. FloatVectorOperations::clear(outBuffer[i], frames);
  572. return;
  573. }
  574. // --------------------------------------------------------------------------------------------------------
  575. // Check if needs reset
  576. if (pData->needsReset)
  577. {
  578. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  579. {
  580. for (uint i=0; i < MAX_MIDI_CHANNELS; ++i)
  581. {
  582. fMidiInputPort->DispatchControlChange(MIDI_CONTROL_ALL_NOTES_OFF, 0, i);
  583. fMidiInputPort->DispatchControlChange(MIDI_CONTROL_ALL_SOUND_OFF, 0, i);
  584. }
  585. }
  586. else if (pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  587. {
  588. for (uint8_t i=0; i < MAX_MIDI_NOTE; ++i)
  589. fMidiInputPort->DispatchNoteOff(i, 0, uint(pData->ctrlChannel));
  590. }
  591. pData->needsReset = false;
  592. }
  593. // --------------------------------------------------------------------------------------------------------
  594. // Event Input and Processing
  595. {
  596. // ----------------------------------------------------------------------------------------------------
  597. // MIDI Input (External)
  598. if (pData->extNotes.mutex.tryLock())
  599. {
  600. for (RtLinkedList<ExternalMidiNote>::Itenerator it = pData->extNotes.data.begin(); it.valid(); it.next())
  601. {
  602. const ExternalMidiNote& note(it.getValue());
  603. CARLA_SAFE_ASSERT_CONTINUE(note.channel >= 0 && note.channel < MAX_MIDI_CHANNELS);
  604. if (note.velo > 0)
  605. fMidiInputPort->DispatchNoteOn(note.note, note.velo, static_cast<uint>(note.channel));
  606. else
  607. fMidiInputPort->DispatchNoteOff(note.note, note.velo, static_cast<uint>(note.channel));
  608. }
  609. pData->extNotes.data.clear();
  610. pData->extNotes.mutex.unlock();
  611. } // End of MIDI Input (External)
  612. // ----------------------------------------------------------------------------------------------------
  613. // Event Input (System)
  614. bool allNotesOffSent = false;
  615. bool sampleAccurate = (pData->options & PLUGIN_OPTION_FIXED_BUFFERS) == 0;
  616. uint32_t nEvents = pData->event.portIn->getEventCount();
  617. uint32_t startTime = 0;
  618. uint32_t timeOffset = 0;
  619. uint32_t nextBankIds[MAX_MIDI_CHANNELS] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 0, 0, 0, 0, 0, 0 };
  620. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0 && pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  621. nextBankIds[pData->ctrlChannel] = pData->midiprog.data[pData->midiprog.current].bank;
  622. for (uint32_t i=0; i < nEvents; ++i)
  623. {
  624. const EngineEvent& event(pData->event.portIn->getEvent(i));
  625. CARLA_SAFE_ASSERT_CONTINUE(event.time < frames);
  626. CARLA_SAFE_ASSERT_BREAK(event.time >= timeOffset);
  627. if (event.time > timeOffset && sampleAccurate)
  628. {
  629. if (processSingle(outBuffer, event.time - timeOffset, timeOffset))
  630. {
  631. startTime = 0;
  632. timeOffset = event.time;
  633. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0 && pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  634. nextBankIds[pData->ctrlChannel] = pData->midiprog.data[pData->midiprog.current].bank;
  635. }
  636. else
  637. startTime += timeOffset;
  638. }
  639. // Control change
  640. switch (event.type)
  641. {
  642. case kEngineEventTypeNull:
  643. break;
  644. case kEngineEventTypeControl:
  645. {
  646. const EngineControlEvent& ctrlEvent = event.ctrl;
  647. switch (ctrlEvent.type)
  648. {
  649. case kEngineControlEventTypeNull:
  650. break;
  651. case kEngineControlEventTypeParameter:
  652. {
  653. #ifndef BUILD_BRIDGE
  654. // Control backend stuff
  655. if (event.channel == pData->ctrlChannel)
  656. {
  657. float value;
  658. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_DRYWET) != 0)
  659. {
  660. value = ctrlEvent.value;
  661. setDryWet(value, false, false);
  662. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, value);
  663. }
  664. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_VOLUME) != 0)
  665. {
  666. value = ctrlEvent.value*127.0f/100.0f;
  667. setVolume(value, false, false);
  668. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
  669. }
  670. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_BALANCE) != 0)
  671. {
  672. float left, right;
  673. value = ctrlEvent.value/0.5f - 1.0f;
  674. if (value < 0.0f)
  675. {
  676. left = -1.0f;
  677. right = (value*2.0f)+1.0f;
  678. }
  679. else if (value > 0.0f)
  680. {
  681. left = (value*2.0f)-1.0f;
  682. right = 1.0f;
  683. }
  684. else
  685. {
  686. left = -1.0f;
  687. right = 1.0f;
  688. }
  689. setBalanceLeft(left, false, false);
  690. setBalanceRight(right, false, false);
  691. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  692. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  693. }
  694. }
  695. #endif
  696. // Control plugin parameters
  697. for (uint32_t k=0; k < pData->param.count; ++k)
  698. {
  699. if (pData->param.data[k].midiChannel != event.channel)
  700. continue;
  701. if (pData->param.data[k].midiCC != ctrlEvent.param)
  702. continue;
  703. if (pData->param.data[k].hints != PARAMETER_INPUT)
  704. continue;
  705. if ((pData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  706. continue;
  707. float value;
  708. if (pData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  709. {
  710. value = (ctrlEvent.value < 0.5f) ? pData->param.ranges[k].min : pData->param.ranges[k].max;
  711. }
  712. else
  713. {
  714. value = pData->param.ranges[k].getUnnormalizedValue(ctrlEvent.value);
  715. if (pData->param.data[k].hints & PARAMETER_IS_INTEGER)
  716. value = std::rint(value);
  717. }
  718. setParameterValue(k, value, false, false, false);
  719. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, value);
  720. }
  721. if ((pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0 && ctrlEvent.param <= 0x5F)
  722. {
  723. fMidiInputPort->DispatchControlChange(uint8_t(ctrlEvent.param), uint8_t(ctrlEvent.value*127.0f), event.channel, static_cast<int32_t>(sampleAccurate ? startTime : event.time));
  724. }
  725. break;
  726. }
  727. case kEngineControlEventTypeMidiBank:
  728. if (event.channel < MAX_MIDI_CHANNELS && (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  729. nextBankIds[event.channel] = ctrlEvent.param;
  730. break;
  731. case kEngineControlEventTypeMidiProgram:
  732. if (event.channel < MAX_MIDI_CHANNELS && (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  733. {
  734. const uint32_t bankId(nextBankIds[event.channel]);
  735. const uint32_t progId(ctrlEvent.param);
  736. const uint32_t rIndex = bankId*128 + progId;
  737. for (uint32_t k=0; k < pData->midiprog.count; ++k)
  738. {
  739. if (pData->midiprog.data[k].bank == bankId && pData->midiprog.data[k].program == progId)
  740. {
  741. LinuxSampler::EngineChannel* const engineChannel(fEngineChannels[pData->ctrlChannel]);
  742. /*if (pData->engine->isOffline())
  743. {
  744. engineChannel->PrepareLoadInstrument(pData->filename, rIndex);
  745. engineChannel->LoadInstrument();
  746. }
  747. else*/
  748. {
  749. fInstrument->LoadInstrumentInBackground(fInstrumentIds[rIndex], engineChannel);
  750. }
  751. fCurMidiProgs[event.channel] = static_cast<int32_t>(k);
  752. if (event.channel == pData->ctrlChannel)
  753. pData->postponeRtEvent(kPluginPostRtEventMidiProgramChange, static_cast<int32_t>(k), 0, 0.0f);
  754. break;
  755. }
  756. }
  757. }
  758. break;
  759. case kEngineControlEventTypeAllSoundOff:
  760. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  761. {
  762. fMidiInputPort->DispatchControlChange(MIDI_CONTROL_ALL_SOUND_OFF, 0, event.channel, static_cast<int32_t>(sampleAccurate ? startTime : event.time));
  763. }
  764. break;
  765. case kEngineControlEventTypeAllNotesOff:
  766. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  767. {
  768. if (event.channel == pData->ctrlChannel && ! allNotesOffSent)
  769. {
  770. allNotesOffSent = true;
  771. sendMidiAllNotesOffToCallback();
  772. }
  773. fMidiInputPort->DispatchControlChange(MIDI_CONTROL_ALL_NOTES_OFF, 0, event.channel, static_cast<int32_t>(sampleAccurate ? startTime : event.time));
  774. }
  775. break;
  776. }
  777. break;
  778. }
  779. case kEngineEventTypeMidi:
  780. {
  781. const EngineMidiEvent& midiEvent(event.midi);
  782. uint8_t status = uint8_t(MIDI_GET_STATUS_FROM_DATA(midiEvent.data));
  783. uint8_t channel = event.channel;
  784. // Fix bad note-off
  785. if (MIDI_IS_STATUS_NOTE_ON(status) && midiEvent.data[2] == 0)
  786. status = MIDI_STATUS_NOTE_OFF;
  787. if (MIDI_IS_STATUS_POLYPHONIC_AFTERTOUCH(status) && (pData->options & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) == 0)
  788. continue;
  789. if (MIDI_IS_STATUS_CONTROL_CHANGE(status) && (pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) == 0)
  790. continue;
  791. if (MIDI_IS_STATUS_CHANNEL_PRESSURE(status) && (pData->options & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) == 0)
  792. continue;
  793. if (MIDI_IS_STATUS_PITCH_WHEEL_CONTROL(status) && (pData->options & PLUGIN_OPTION_SEND_PITCHBEND) == 0)
  794. continue;
  795. // put back channel in data
  796. uint8_t data[EngineMidiEvent::kDataSize];
  797. std::memcpy(data, event.midi.data, EngineMidiEvent::kDataSize);
  798. if (status < 0xF0 && channel < MAX_MIDI_CHANNELS)
  799. data[0] = uint8_t(data[0] + channel);
  800. fMidiInputPort->DispatchRaw(data, static_cast<int32_t>(sampleAccurate ? startTime : event.time));
  801. if (status == MIDI_STATUS_NOTE_ON)
  802. pData->postponeRtEvent(kPluginPostRtEventNoteOn, channel, data[1], data[2]);
  803. else if (status == MIDI_STATUS_NOTE_OFF)
  804. pData->postponeRtEvent(kPluginPostRtEventNoteOff, channel,data[1], 0.0f);
  805. break;
  806. }
  807. }
  808. }
  809. pData->postRtEvents.trySplice();
  810. if (frames > timeOffset)
  811. processSingle(outBuffer, frames - timeOffset, timeOffset);
  812. } // End of Event Input and Processing
  813. }
  814. bool processSingle(float** const outBuffer, const uint32_t frames, const uint32_t timeOffset)
  815. {
  816. CARLA_SAFE_ASSERT_RETURN(outBuffer != nullptr, false);
  817. CARLA_SAFE_ASSERT_RETURN(frames > 0, false);
  818. // --------------------------------------------------------------------------------------------------------
  819. // Try lock, silence otherwise
  820. if (pData->engine->isOffline())
  821. {
  822. pData->singleMutex.lock();
  823. }
  824. else if (! pData->singleMutex.tryLock())
  825. {
  826. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  827. {
  828. for (uint32_t k=0; k < frames; ++k)
  829. outBuffer[i][k+timeOffset] = 0.0f;
  830. }
  831. return false;
  832. }
  833. // --------------------------------------------------------------------------------------------------------
  834. // Run plugin
  835. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  836. {
  837. if (LinuxSampler::AudioChannel* const outDev = fAudioOutputDevice->Channel(i))
  838. outDev->SetBuffer(outBuffer[i] + timeOffset);
  839. }
  840. fAudioOutputDevice->Render(frames);
  841. #ifndef BUILD_BRIDGE
  842. // --------------------------------------------------------------------------------------------------------
  843. // Post-processing (dry/wet, volume and balance)
  844. {
  845. const bool doVolume = (pData->hints & PLUGIN_CAN_VOLUME) > 0 && pData->postProc.volume != 1.0f;
  846. const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) > 0 && (pData->postProc.balanceLeft != -1.0f || pData->postProc.balanceRight != 1.0f);
  847. float oldBufLeft[doBalance ? frames : 1];
  848. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  849. {
  850. // Balance
  851. if (doBalance)
  852. {
  853. if (i % 2 == 0)
  854. FloatVectorOperations::copy(oldBufLeft, outBuffer[i], frames);
  855. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  856. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  857. for (uint32_t k=0; k < frames; ++k)
  858. {
  859. if (i % 2 == 0)
  860. {
  861. // left
  862. outBuffer[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  863. outBuffer[i][k] += outBuffer[i+1][k] * (1.0f - balRangeR);
  864. }
  865. else
  866. {
  867. // right
  868. outBuffer[i][k] = outBuffer[i][k] * balRangeR;
  869. outBuffer[i][k] += oldBufLeft[k] * balRangeL;
  870. }
  871. }
  872. }
  873. // Volume
  874. if (doVolume)
  875. {
  876. for (uint32_t k=0; k < frames; ++k)
  877. outBuffer[i][k+timeOffset] *= pData->postProc.volume;
  878. }
  879. }
  880. } // End of Post-processing
  881. #endif
  882. // --------------------------------------------------------------------------------------------------------
  883. pData->singleMutex.unlock();
  884. return true;
  885. }
  886. #ifndef CARLA_OS_WIN // FIXME, need to update linuxsampler win32 build
  887. void bufferSizeChanged(const uint32_t) override
  888. {
  889. CARLA_SAFE_ASSERT_RETURN(fAudioOutputDevice != nullptr,);
  890. fAudioOutputDevice->ReconnectAll();
  891. }
  892. void sampleRateChanged(const double) override
  893. {
  894. CARLA_SAFE_ASSERT_RETURN(fAudioOutputDevice != nullptr,);
  895. fAudioOutputDevice->ReconnectAll();
  896. }
  897. #endif
  898. // -------------------------------------------------------------------
  899. // Plugin buffers
  900. // nothing
  901. // -------------------------------------------------------------------
  902. const void* getExtraStuff() const noexcept override
  903. {
  904. static const char xtrue[] = "true";
  905. static const char xfalse[] = "false";
  906. return fUses16Outs ? xtrue : xfalse;
  907. }
  908. bool init(const char* const filename, const char* const name, const char* const label)
  909. {
  910. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  911. // ---------------------------------------------------------------
  912. // first checks
  913. if (pData->client != nullptr)
  914. {
  915. pData->engine->setLastError("Plugin client is already registered");
  916. return false;
  917. }
  918. if (filename == nullptr || filename[0] == '\0')
  919. {
  920. pData->engine->setLastError("null filename");
  921. return false;
  922. }
  923. if (label == nullptr || label[0] == '\0')
  924. {
  925. pData->engine->setLastError("null label");
  926. return false;
  927. }
  928. // ---------------------------------------------------------------
  929. // Store format
  930. CarlaString cstype(fFormat);
  931. cstype.toLower();
  932. const char* const ctype(cstype.buffer());
  933. // ---------------------------------------------------------------
  934. // Create the LinuxSampler Engine
  935. try {
  936. fEngine = LinuxSampler::EngineFactory::Create(ctype);
  937. }
  938. catch (LinuxSampler::Exception& e)
  939. {
  940. pData->engine->setLastError(e.what());
  941. return false;
  942. }
  943. // ---------------------------------------------------------------
  944. // Init LinuxSampler stuff
  945. fAudioOutputDevice = new LinuxSampler::AudioOutputDevicePlugin(pData->engine, this, fUses16Outs);
  946. fMidiInputDevice = new LinuxSampler::MidiInputDevicePlugin(&fSampler);
  947. fMidiInputPort = fMidiInputDevice->CreateMidiPortPlugin();
  948. for (uint i=0; i < MAX_MIDI_CHANNELS; ++i)
  949. {
  950. fSamplerChannels[i] = fSampler.AddSamplerChannel();
  951. CARLA_SAFE_ASSERT_CONTINUE(fSamplerChannels[i] != nullptr);
  952. fSamplerChannels[i]->SetEngineType(ctype);
  953. fSamplerChannels[i]->SetAudioOutputDevice(fAudioOutputDevice);
  954. fEngineChannels[i] = fSamplerChannels[i]->GetEngineChannel();
  955. CARLA_SAFE_ASSERT_CONTINUE(fEngineChannels[i] != nullptr);
  956. fEngineChannels[i]->Connect(fAudioOutputDevice);
  957. fEngineChannels[i]->Volume(LinuxSampler::kVolumeMax);
  958. if (fUses16Outs)
  959. {
  960. fEngineChannels[i]->SetOutputChannel(0, i*2);
  961. fEngineChannels[i]->SetOutputChannel(1, i*2 +1);
  962. }
  963. else
  964. {
  965. fEngineChannels[i]->SetOutputChannel(0, 0);
  966. fEngineChannels[i]->SetOutputChannel(1, 1);
  967. }
  968. fMidiInputPort->Connect(fEngineChannels[i], static_cast<LinuxSampler::midi_chan_t>(i));
  969. }
  970. // ---------------------------------------------------------------
  971. // Get the Engine's Instrument Manager
  972. fInstrument = fEngine->GetInstrumentManager();
  973. if (fInstrument == nullptr)
  974. {
  975. pData->engine->setLastError("Failed to get LinuxSampler instrument manager");
  976. return false;
  977. }
  978. // ---------------------------------------------------------------
  979. // Load the Instrument via filename
  980. try {
  981. fInstrumentIds = fInstrument->GetInstrumentFileContent(filename);
  982. }
  983. catch (const LinuxSampler::InstrumentManagerException& e)
  984. {
  985. pData->engine->setLastError(e.what());
  986. return false;
  987. }
  988. // ---------------------------------------------------------------
  989. // Get info
  990. if (fInstrumentIds.size() == 0)
  991. {
  992. pData->engine->setLastError("Failed to find any instruments");
  993. return false;
  994. }
  995. LinuxSampler::InstrumentManager::instrument_info_t info;
  996. try {
  997. info = fInstrument->GetInstrumentInfo(fInstrumentIds[0]);
  998. }
  999. catch (const LinuxSampler::InstrumentManagerException& e)
  1000. {
  1001. pData->engine->setLastError(e.what());
  1002. return false;
  1003. }
  1004. CarlaString label2(label);
  1005. if (fUses16Outs && ! label2.endsWith(" (16 outs)"))
  1006. label2 += " (16 outs)";
  1007. fLabel = label2.dup();
  1008. fMaker = carla_strdup(info.Artists.c_str());
  1009. fRealName = carla_strdup(info.InstrumentName.c_str());
  1010. pData->filename = carla_strdup(filename);
  1011. if (name != nullptr && name[0] != '\0')
  1012. pData->name = pData->engine->getUniquePluginName(name);
  1013. else if (fRealName[0] != '\0')
  1014. pData->name = pData->engine->getUniquePluginName(fRealName);
  1015. else
  1016. pData->name = pData->engine->getUniquePluginName(label);
  1017. // ---------------------------------------------------------------
  1018. // register client
  1019. pData->client = pData->engine->addClient(this);
  1020. if (pData->client == nullptr || ! pData->client->isOk())
  1021. {
  1022. pData->engine->setLastError("Failed to register plugin client");
  1023. return false;
  1024. }
  1025. // ---------------------------------------------------------------
  1026. // load plugin settings
  1027. {
  1028. // set default options
  1029. pData->options = 0x0;
  1030. pData->options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  1031. pData->options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  1032. pData->options |= PLUGIN_OPTION_SEND_PITCHBEND;
  1033. pData->options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  1034. #ifndef BUILD_BRIDGE
  1035. // set identifier string
  1036. CarlaString identifier(fFormat);
  1037. identifier += "/";
  1038. if (const char* const shortname = std::strrchr(filename, OS_SEP))
  1039. identifier += shortname+1;
  1040. else
  1041. identifier += label;
  1042. pData->identifier = identifier.dup();
  1043. // load settings
  1044. pData->options = pData->loadSettings(pData->options, getOptionsAvailable());
  1045. #endif
  1046. }
  1047. return true;
  1048. }
  1049. // -------------------------------------------------------------------
  1050. private:
  1051. const bool fUses16Outs;
  1052. const char* fFormat;
  1053. const char* fLabel;
  1054. const char* fMaker;
  1055. const char* fRealName;
  1056. int32_t fCurMidiProgs[MAX_MIDI_CHANNELS];
  1057. LinuxSampler::Sampler fSampler;
  1058. LinuxSampler::Engine* fEngine;
  1059. LinuxSampler::SamplerChannel* fSamplerChannels[MAX_MIDI_CHANNELS];
  1060. LinuxSampler::EngineChannel* fEngineChannels[MAX_MIDI_CHANNELS];
  1061. LinuxSampler::AudioOutputDevicePlugin* fAudioOutputDevice;
  1062. LinuxSampler::MidiInputDevicePlugin* fMidiInputDevice;
  1063. LinuxSampler::MidiInputPortPlugin* fMidiInputPort;
  1064. LinuxSampler::InstrumentManager* fInstrument;
  1065. std::vector<LinuxSampler::InstrumentManager::instrument_id_t> fInstrumentIds;
  1066. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(LinuxSamplerPlugin)
  1067. };
  1068. CARLA_BACKEND_END_NAMESPACE
  1069. #endif // WANT_LINUXSAMPLER
  1070. CARLA_BACKEND_START_NAMESPACE
  1071. CarlaPlugin* CarlaPlugin::newLinuxSampler(const Initializer& init, const char* const format, const bool use16Outs)
  1072. {
  1073. carla_debug("LinuxSamplerPlugin::newLinuxSampler({%p, \"%s\", \"%s\", \"%s\", " P_INT64 "}, %s, %s)", init.engine, init.filename, init.name, init.label, init.uniqueId, format, bool2str(use16Outs));
  1074. #ifdef WANT_LINUXSAMPLER
  1075. if (init.engine->getProccessMode() == ENGINE_PROCESS_MODE_CONTINUOUS_RACK && use16Outs)
  1076. {
  1077. init.engine->setLastError("Carla's rack mode can only work with Stereo modules, please choose the 2-channel only sample-library version");
  1078. return nullptr;
  1079. }
  1080. // -------------------------------------------------------------------
  1081. // Check if file exists
  1082. {
  1083. QFileInfo file(init.filename);
  1084. if (! file.exists())
  1085. {
  1086. init.engine->setLastError("Requested file does not exist");
  1087. return nullptr;
  1088. }
  1089. if (! file.isFile())
  1090. {
  1091. init.engine->setLastError("Requested file is not valid");
  1092. return nullptr;
  1093. }
  1094. if (! file.isReadable())
  1095. {
  1096. init.engine->setLastError("Requested file is not readable");
  1097. return nullptr;
  1098. }
  1099. }
  1100. LinuxSamplerPlugin* const plugin(new LinuxSamplerPlugin(init.engine, init.id, format, use16Outs));
  1101. if (! plugin->init(init.filename, init.name, init.label))
  1102. {
  1103. delete plugin;
  1104. return nullptr;
  1105. }
  1106. plugin->reload();
  1107. return plugin;
  1108. #else
  1109. init.engine->setLastError("linuxsampler support not available");
  1110. return nullptr;
  1111. // unused
  1112. (void)format;
  1113. (void)use16Outs;
  1114. #endif
  1115. }
  1116. CARLA_BACKEND_END_NAMESPACE