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.

1422 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. #ifndef BUILD_BRIDGE
  619. bool allNotesOffSent = false;
  620. #endif
  621. bool sampleAccurate = (pData->options & PLUGIN_OPTION_FIXED_BUFFERS) == 0;
  622. uint32_t nEvents = pData->event.portIn->getEventCount();
  623. uint32_t startTime = 0;
  624. uint32_t timeOffset = 0;
  625. uint32_t nextBankIds[MAX_MIDI_CHANNELS] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 0, 0, 0, 0, 0, 0 };
  626. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0 && pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  627. nextBankIds[pData->ctrlChannel] = pData->midiprog.data[pData->midiprog.current].bank;
  628. for (uint32_t i=0; i < nEvents; ++i)
  629. {
  630. const EngineEvent& event(pData->event.portIn->getEvent(i));
  631. CARLA_SAFE_ASSERT_CONTINUE(event.time < frames);
  632. CARLA_SAFE_ASSERT_BREAK(event.time >= timeOffset);
  633. if (event.time > timeOffset && sampleAccurate)
  634. {
  635. if (processSingle(outBuffer, event.time - timeOffset, timeOffset))
  636. {
  637. startTime = 0;
  638. timeOffset = event.time;
  639. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0 && pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  640. nextBankIds[pData->ctrlChannel] = pData->midiprog.data[pData->midiprog.current].bank;
  641. }
  642. else
  643. startTime += timeOffset;
  644. }
  645. // Control change
  646. switch (event.type)
  647. {
  648. case kEngineEventTypeNull:
  649. break;
  650. case kEngineEventTypeControl:
  651. {
  652. const EngineControlEvent& ctrlEvent = event.ctrl;
  653. switch (ctrlEvent.type)
  654. {
  655. case kEngineControlEventTypeNull:
  656. break;
  657. case kEngineControlEventTypeParameter:
  658. {
  659. #ifndef BUILD_BRIDGE
  660. // Control backend stuff
  661. if (event.channel == pData->ctrlChannel)
  662. {
  663. float value;
  664. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_DRYWET) != 0)
  665. {
  666. value = ctrlEvent.value;
  667. setDryWet(value, false, false);
  668. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, value);
  669. }
  670. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_VOLUME) != 0)
  671. {
  672. value = ctrlEvent.value*127.0f/100.0f;
  673. setVolume(value, false, false);
  674. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
  675. }
  676. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_BALANCE) != 0)
  677. {
  678. float left, right;
  679. value = ctrlEvent.value/0.5f - 1.0f;
  680. if (value < 0.0f)
  681. {
  682. left = -1.0f;
  683. right = (value*2.0f)+1.0f;
  684. }
  685. else if (value > 0.0f)
  686. {
  687. left = (value*2.0f)-1.0f;
  688. right = 1.0f;
  689. }
  690. else
  691. {
  692. left = -1.0f;
  693. right = 1.0f;
  694. }
  695. setBalanceLeft(left, false, false);
  696. setBalanceRight(right, false, false);
  697. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  698. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  699. }
  700. }
  701. #endif
  702. // Control plugin parameters
  703. for (uint32_t k=0; k < pData->param.count; ++k)
  704. {
  705. if (pData->param.data[k].midiChannel != event.channel)
  706. continue;
  707. if (pData->param.data[k].midiCC != ctrlEvent.param)
  708. continue;
  709. if (pData->param.data[k].hints != PARAMETER_INPUT)
  710. continue;
  711. if ((pData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  712. continue;
  713. float value;
  714. if (pData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  715. {
  716. value = (ctrlEvent.value < 0.5f) ? pData->param.ranges[k].min : pData->param.ranges[k].max;
  717. }
  718. else
  719. {
  720. value = pData->param.ranges[k].getUnnormalizedValue(ctrlEvent.value);
  721. if (pData->param.data[k].hints & PARAMETER_IS_INTEGER)
  722. value = std::rint(value);
  723. }
  724. setParameterValue(k, value, false, false, false);
  725. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, value);
  726. }
  727. if ((pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0 && ctrlEvent.param <= 0x5F)
  728. {
  729. fMidiInputPort->DispatchControlChange(uint8_t(ctrlEvent.param), uint8_t(ctrlEvent.value*127.0f), event.channel, static_cast<int32_t>(sampleAccurate ? startTime : event.time));
  730. }
  731. break;
  732. }
  733. case kEngineControlEventTypeMidiBank:
  734. if (event.channel < MAX_MIDI_CHANNELS && (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  735. nextBankIds[event.channel] = ctrlEvent.param;
  736. break;
  737. case kEngineControlEventTypeMidiProgram:
  738. if (event.channel < MAX_MIDI_CHANNELS && (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  739. {
  740. const uint32_t bankId(nextBankIds[event.channel]);
  741. const uint32_t progId(ctrlEvent.param);
  742. const uint32_t rIndex = bankId*128 + progId;
  743. for (uint32_t k=0; k < pData->midiprog.count; ++k)
  744. {
  745. if (pData->midiprog.data[k].bank == bankId && pData->midiprog.data[k].program == progId)
  746. {
  747. LinuxSampler::EngineChannel* const engineChannel(fEngineChannels[kIsGIG ? event.channel : 0]);
  748. CARLA_SAFE_ASSERT_CONTINUE(engineChannel != nullptr);
  749. /*if (pData->engine->isOffline())
  750. {
  751. engineChannel->PrepareLoadInstrument(pData->filename, rIndex);
  752. engineChannel->LoadInstrument();
  753. }
  754. else*/
  755. {
  756. fInstrument->LoadInstrumentInBackground(fInstrumentIds[rIndex], engineChannel);
  757. }
  758. fCurMidiProgs[event.channel] = static_cast<int32_t>(k);
  759. if (event.channel == pData->ctrlChannel)
  760. pData->postponeRtEvent(kPluginPostRtEventMidiProgramChange, static_cast<int32_t>(k), 0, 0.0f);
  761. break;
  762. }
  763. }
  764. }
  765. break;
  766. case kEngineControlEventTypeAllSoundOff:
  767. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  768. {
  769. fMidiInputPort->DispatchControlChange(MIDI_CONTROL_ALL_SOUND_OFF, 0, event.channel, static_cast<int32_t>(sampleAccurate ? startTime : event.time));
  770. }
  771. break;
  772. case kEngineControlEventTypeAllNotesOff:
  773. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  774. {
  775. #ifndef BUILD_BRIDGE
  776. if (event.channel == pData->ctrlChannel && ! allNotesOffSent)
  777. {
  778. allNotesOffSent = true;
  779. sendMidiAllNotesOffToCallback();
  780. }
  781. #endif
  782. fMidiInputPort->DispatchControlChange(MIDI_CONTROL_ALL_NOTES_OFF, 0, event.channel, static_cast<int32_t>(sampleAccurate ? startTime : event.time));
  783. }
  784. break;
  785. }
  786. break;
  787. }
  788. case kEngineEventTypeMidi:
  789. {
  790. const EngineMidiEvent& midiEvent(event.midi);
  791. uint8_t status = uint8_t(MIDI_GET_STATUS_FROM_DATA(midiEvent.data));
  792. uint8_t channel = event.channel;
  793. // Fix bad note-off
  794. if (MIDI_IS_STATUS_NOTE_ON(status) && midiEvent.data[2] == 0)
  795. status = MIDI_STATUS_NOTE_OFF;
  796. if (MIDI_IS_STATUS_POLYPHONIC_AFTERTOUCH(status) && (pData->options & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) == 0)
  797. continue;
  798. if (MIDI_IS_STATUS_CONTROL_CHANGE(status) && (pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) == 0)
  799. continue;
  800. if (MIDI_IS_STATUS_CHANNEL_PRESSURE(status) && (pData->options & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) == 0)
  801. continue;
  802. if (MIDI_IS_STATUS_PITCH_WHEEL_CONTROL(status) && (pData->options & PLUGIN_OPTION_SEND_PITCHBEND) == 0)
  803. continue;
  804. // put back channel in data
  805. uint8_t data[EngineMidiEvent::kDataSize];
  806. std::memcpy(data, event.midi.data, EngineMidiEvent::kDataSize);
  807. if (status < 0xF0 && channel < MAX_MIDI_CHANNELS)
  808. data[0] = uint8_t(data[0] + channel);
  809. fMidiInputPort->DispatchRaw(data, static_cast<int32_t>(sampleAccurate ? startTime : event.time));
  810. if (status == MIDI_STATUS_NOTE_ON)
  811. pData->postponeRtEvent(kPluginPostRtEventNoteOn, channel, data[1], data[2]);
  812. else if (status == MIDI_STATUS_NOTE_OFF)
  813. pData->postponeRtEvent(kPluginPostRtEventNoteOff, channel,data[1], 0.0f);
  814. break;
  815. }
  816. }
  817. }
  818. pData->postRtEvents.trySplice();
  819. if (frames > timeOffset)
  820. processSingle(outBuffer, frames - timeOffset, timeOffset);
  821. } // End of Event Input and Processing
  822. }
  823. bool processSingle(float** const outBuffer, const uint32_t frames, const uint32_t timeOffset)
  824. {
  825. CARLA_SAFE_ASSERT_RETURN(outBuffer != nullptr, false);
  826. CARLA_SAFE_ASSERT_RETURN(frames > 0, false);
  827. // --------------------------------------------------------------------------------------------------------
  828. // Try lock, silence otherwise
  829. if (pData->engine->isOffline())
  830. {
  831. pData->singleMutex.lock();
  832. }
  833. else if (! pData->singleMutex.tryLock())
  834. {
  835. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  836. {
  837. for (uint32_t k=0; k < frames; ++k)
  838. outBuffer[i][k+timeOffset] = 0.0f;
  839. }
  840. return false;
  841. }
  842. // --------------------------------------------------------------------------------------------------------
  843. // Run plugin
  844. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  845. {
  846. if (LinuxSampler::AudioChannel* const outDev = fAudioOutputDevice->Channel(i))
  847. outDev->SetBuffer(outBuffer[i] + timeOffset);
  848. }
  849. fAudioOutputDevice->Render(frames);
  850. #ifndef BUILD_BRIDGE
  851. // --------------------------------------------------------------------------------------------------------
  852. // Post-processing (dry/wet, volume and balance)
  853. {
  854. const bool doVolume = (pData->hints & PLUGIN_CAN_VOLUME) > 0 && pData->postProc.volume != 1.0f;
  855. const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) > 0 && (pData->postProc.balanceLeft != -1.0f || pData->postProc.balanceRight != 1.0f);
  856. float oldBufLeft[doBalance ? frames : 1];
  857. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  858. {
  859. // Balance
  860. if (doBalance)
  861. {
  862. if (i % 2 == 0)
  863. FloatVectorOperations::copy(oldBufLeft, outBuffer[i], static_cast<int>(frames));
  864. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  865. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  866. for (uint32_t k=0; k < frames; ++k)
  867. {
  868. if (i % 2 == 0)
  869. {
  870. // left
  871. outBuffer[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  872. outBuffer[i][k] += outBuffer[i+1][k] * (1.0f - balRangeR);
  873. }
  874. else
  875. {
  876. // right
  877. outBuffer[i][k] = outBuffer[i][k] * balRangeR;
  878. outBuffer[i][k] += oldBufLeft[k] * balRangeL;
  879. }
  880. }
  881. }
  882. // Volume
  883. if (doVolume)
  884. {
  885. for (uint32_t k=0; k < frames; ++k)
  886. outBuffer[i][k+timeOffset] *= pData->postProc.volume;
  887. }
  888. }
  889. } // End of Post-processing
  890. #endif
  891. // --------------------------------------------------------------------------------------------------------
  892. pData->singleMutex.unlock();
  893. return true;
  894. }
  895. #ifndef CARLA_OS_WIN // FIXME, need to update linuxsampler win32 build
  896. void bufferSizeChanged(const uint32_t) override
  897. {
  898. CARLA_SAFE_ASSERT_RETURN(fAudioOutputDevice != nullptr,);
  899. fAudioOutputDevice->ReconnectAll();
  900. }
  901. void sampleRateChanged(const double) override
  902. {
  903. CARLA_SAFE_ASSERT_RETURN(fAudioOutputDevice != nullptr,);
  904. fAudioOutputDevice->ReconnectAll();
  905. }
  906. #endif
  907. // -------------------------------------------------------------------
  908. // Plugin buffers
  909. // nothing
  910. // -------------------------------------------------------------------
  911. const void* getExtraStuff() const noexcept override
  912. {
  913. static const char xtrue[] = "true";
  914. static const char xfalse[] = "false";
  915. return kUses16Outs ? xtrue : xfalse;
  916. }
  917. bool init(const char* const filename, const char* const name, const char* const label)
  918. {
  919. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  920. // ---------------------------------------------------------------
  921. // first checks
  922. if (pData->client != nullptr)
  923. {
  924. pData->engine->setLastError("Plugin client is already registered");
  925. return false;
  926. }
  927. if (filename == nullptr || filename[0] == '\0')
  928. {
  929. pData->engine->setLastError("null filename");
  930. return false;
  931. }
  932. // ---------------------------------------------------------------
  933. // Init LinuxSampler stuff
  934. fAudioOutputDevice = new LinuxSampler::AudioOutputDevicePlugin(pData->engine, this, kUses16Outs);
  935. fMidiInputDevice = new LinuxSampler::MidiInputDevicePlugin(sSampler);
  936. fMidiInputPort = fMidiInputDevice->CreateMidiPortPlugin();
  937. for (uint i=0; i<kMaxChannels; ++i)
  938. {
  939. fSamplerChannels[i] = sSampler->AddSamplerChannel();
  940. CARLA_SAFE_ASSERT_CONTINUE(fSamplerChannels[i] != nullptr);
  941. fSamplerChannels[i]->SetEngineType(kIsGIG ? "GIG" : "SFZ");
  942. fSamplerChannels[i]->SetAudioOutputDevice(fAudioOutputDevice);
  943. fEngineChannels[i] = fSamplerChannels[i]->GetEngineChannel();
  944. CARLA_SAFE_ASSERT_CONTINUE(fEngineChannels[i] != nullptr);
  945. fEngineChannels[i]->Connect(fAudioOutputDevice);
  946. fEngineChannels[i]->Volume(LinuxSampler::kVolumeMax);
  947. if (kUses16Outs)
  948. {
  949. fEngineChannels[i]->SetOutputChannel(0, i*2);
  950. fEngineChannels[i]->SetOutputChannel(1, i*2 +1);
  951. }
  952. else
  953. {
  954. fEngineChannels[i]->SetOutputChannel(0, 0);
  955. fEngineChannels[i]->SetOutputChannel(1, 1);
  956. }
  957. fMidiInputPort->Connect(fEngineChannels[i], static_cast<LinuxSampler::midi_chan_t>(i));
  958. }
  959. // ---------------------------------------------------------------
  960. // Get the Engine's Instrument Manager
  961. fInstrument = kIsGIG ? sEngineGIG->engine->GetInstrumentManager() : sEngineSFZ->engine->GetInstrumentManager();
  962. if (fInstrument == nullptr)
  963. {
  964. pData->engine->setLastError("Failed to get LinuxSampler instrument manager");
  965. return false;
  966. }
  967. // ---------------------------------------------------------------
  968. // Load the Instrument via filename
  969. try {
  970. fInstrumentIds = fInstrument->GetInstrumentFileContent(filename);
  971. }
  972. catch (const LinuxSampler::InstrumentManagerException& e)
  973. {
  974. pData->engine->setLastError(e.what());
  975. return false;
  976. }
  977. // ---------------------------------------------------------------
  978. // Get info
  979. if (fInstrumentIds.size() == 0)
  980. {
  981. pData->engine->setLastError("Failed to find any instruments");
  982. return false;
  983. }
  984. LinuxSampler::InstrumentManager::instrument_info_t info;
  985. try {
  986. info = fInstrument->GetInstrumentInfo(fInstrumentIds[0]);
  987. }
  988. catch (const LinuxSampler::InstrumentManagerException& e)
  989. {
  990. pData->engine->setLastError(e.what());
  991. return false;
  992. }
  993. CarlaString label2(label != nullptr ? label : File(filename).getFileNameWithoutExtension().toRawUTF8());
  994. if (kUses16Outs && ! label2.endsWith(" (16 outs)"))
  995. label2 += " (16 outs)";
  996. fLabel = label2.dup();
  997. fMaker = carla_strdup(info.Artists.c_str());
  998. fRealName = carla_strdup(info.InstrumentName.c_str());
  999. pData->filename = carla_strdup(filename);
  1000. if (name != nullptr && name[0] != '\0')
  1001. pData->name = pData->engine->getUniquePluginName(name);
  1002. else if (fRealName[0] != '\0')
  1003. pData->name = pData->engine->getUniquePluginName(fRealName);
  1004. else
  1005. pData->name = pData->engine->getUniquePluginName(fLabel);
  1006. // ---------------------------------------------------------------
  1007. // register client
  1008. pData->client = pData->engine->addClient(this);
  1009. if (pData->client == nullptr || ! pData->client->isOk())
  1010. {
  1011. pData->engine->setLastError("Failed to register plugin client");
  1012. return false;
  1013. }
  1014. // ---------------------------------------------------------------
  1015. // set default options
  1016. pData->options = 0x0;
  1017. pData->options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  1018. pData->options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  1019. pData->options |= PLUGIN_OPTION_SEND_PITCHBEND;
  1020. pData->options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  1021. return true;
  1022. }
  1023. // -------------------------------------------------------------------
  1024. private:
  1025. const bool kIsGIG; // SFZ if false
  1026. const bool kUses16Outs;
  1027. const uint kMaxChannels; // 1 or 16 depending on format
  1028. const char* fLabel;
  1029. const char* fMaker;
  1030. const char* fRealName;
  1031. int32_t fCurMidiProgs[MAX_MIDI_CHANNELS];
  1032. LinuxSampler::SamplerChannel* fSamplerChannels[MAX_MIDI_CHANNELS];
  1033. LinuxSampler::EngineChannel* fEngineChannels[MAX_MIDI_CHANNELS];
  1034. LinuxSampler::AudioOutputDevicePlugin* fAudioOutputDevice;
  1035. LinuxSampler::MidiInputDevicePlugin* fMidiInputDevice;
  1036. LinuxSampler::MidiInputPortPlugin* fMidiInputPort;
  1037. LinuxSampler::InstrumentManager* fInstrument;
  1038. std::vector<LinuxSampler::InstrumentManager::instrument_id_t> fInstrumentIds;
  1039. SharedResourcePointer<LinuxSampler::Sampler> sSampler;
  1040. SharedResourcePointer<LinuxSampler::EngineGIG> sEngineGIG;
  1041. SharedResourcePointer<LinuxSampler::EngineSFZ> sEngineSFZ;
  1042. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(LinuxSamplerPlugin)
  1043. };
  1044. CARLA_BACKEND_END_NAMESPACE
  1045. #endif // HAVE_LINUXSAMPLER
  1046. CARLA_BACKEND_START_NAMESPACE
  1047. // -------------------------------------------------------------------------------------------------------------------
  1048. CarlaPlugin* CarlaPlugin::newLinuxSampler(const Initializer& init, const char* const format, const bool use16Outs)
  1049. {
  1050. 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));
  1051. #ifdef HAVE_LINUXSAMPLER
  1052. CarlaString sformat(format);
  1053. sformat.toLower();
  1054. if (sformat != "gig" && sformat != "sfz")
  1055. {
  1056. init.engine->setLastError("Unsupported format requested for LinuxSampler");
  1057. return nullptr;
  1058. }
  1059. if (init.engine->getProccessMode() == ENGINE_PROCESS_MODE_CONTINUOUS_RACK && use16Outs)
  1060. {
  1061. init.engine->setLastError("Carla's rack mode can only work with Stereo modules, please choose the 2-channel only sample-library version");
  1062. return nullptr;
  1063. }
  1064. // -------------------------------------------------------------------
  1065. // Check if file exists
  1066. if (! File(init.filename).existsAsFile())
  1067. {
  1068. init.engine->setLastError("Requested file is not valid or does not exist");
  1069. return nullptr;
  1070. }
  1071. LinuxSamplerPlugin* const plugin(new LinuxSamplerPlugin(init.engine, init.id, (sformat == "gig"), use16Outs));
  1072. if (! plugin->init(init.filename, init.name, init.label))
  1073. {
  1074. delete plugin;
  1075. return nullptr;
  1076. }
  1077. plugin->reload();
  1078. return plugin;
  1079. #else
  1080. init.engine->setLastError("linuxsampler support not available");
  1081. return nullptr;
  1082. // unused
  1083. (void)format;
  1084. (void)use16Outs;
  1085. #endif
  1086. }
  1087. CarlaPlugin* CarlaPlugin::newFileGIG(const Initializer& init, const bool use16Outs)
  1088. {
  1089. carla_debug("CarlaPlugin::newFileGIG({%p, \"%s\", \"%s\", \"%s\"}, %s)", init.engine, init.filename, init.name, init.label, bool2str(use16Outs));
  1090. #ifdef HAVE_LINUXSAMPLER
  1091. return newLinuxSampler(init, "GIG", use16Outs);
  1092. #else
  1093. init.engine->setLastError("GIG support not available");
  1094. return nullptr;
  1095. // unused
  1096. (void)use16Outs;
  1097. #endif
  1098. }
  1099. CarlaPlugin* CarlaPlugin::newFileSFZ(const Initializer& init)
  1100. {
  1101. carla_debug("CarlaPlugin::newFileSFZ({%p, \"%s\", \"%s\", \"%s\"})", init.engine, init.filename, init.name, init.label);
  1102. #ifdef HAVE_LINUXSAMPLER
  1103. return newLinuxSampler(init, "SFZ", false);
  1104. #else
  1105. init.engine->setLastError("SFZ support not available");
  1106. return nullptr;
  1107. #endif
  1108. }
  1109. // -------------------------------------------------------------------------------------------------------------------
  1110. CARLA_BACKEND_END_NAMESPACE