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.

1419 lines
49KB

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