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.

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