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.

1425 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. #ifndef BUILD_BRIDGE
  311. void setCtrlChannel(const int8_t channel, const bool sendOsc, const bool sendCallback) noexcept override
  312. {
  313. if (channel >= 0 && channel < MAX_MIDI_CHANNELS)
  314. pData->midiprog.current = fCurMidiProgs[channel];
  315. CarlaPlugin::setCtrlChannel(channel, sendOsc, sendCallback);
  316. }
  317. #endif
  318. // -------------------------------------------------------------------
  319. // Set data (plugin-specific stuff)
  320. void setCustomData(const char* const type, const char* const key, const char* const value, const bool sendGui) override
  321. {
  322. CARLA_SAFE_ASSERT_RETURN(type != nullptr && type[0] != '\0',);
  323. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  324. CARLA_SAFE_ASSERT_RETURN(value != nullptr && value[0] != '\0',);
  325. carla_debug("LinuxSamplerPlugin::setCustomData(%s, \"%s\", \"%s\", %s)", type, key, value, bool2str(sendGui));
  326. if (std::strcmp(type, CUSTOM_DATA_TYPE_STRING) != 0)
  327. return carla_stderr2("LinuxSamplerPlugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - type is not string", type, key, value, bool2str(sendGui));
  328. if (std::strcmp(key, "midiPrograms") != 0)
  329. return carla_stderr2("LinuxSamplerPlugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - type is not string", type, key, value, bool2str(sendGui));
  330. if (kMaxChannels > 1 && fInstrumentIds.size() > 1)
  331. {
  332. StringArray midiProgramList(StringArray::fromTokens(value, ":", ""));
  333. if (midiProgramList.size() == MAX_MIDI_CHANNELS)
  334. {
  335. uint8_t channel = 0;
  336. for (juce::String *it=midiProgramList.begin(), *end=midiProgramList.end(); it != end; ++it)
  337. {
  338. const int index(it->getIntValue());
  339. if (index >= 0 && index < static_cast<int>(pData->midiprog.count))
  340. {
  341. const uint32_t bank = pData->midiprog.data[index].bank;
  342. const uint32_t program = pData->midiprog.data[index].program;
  343. const uint32_t rIndex = bank*128 + program;
  344. /*if (pData->engine->isOffline())
  345. {
  346. fEngineChannels[channel]->PrepareLoadInstrument(pData->filename, rIndex);
  347. fEngineChannels[channel]->LoadInstrument();
  348. }
  349. else*/
  350. {
  351. fInstrument->LoadInstrumentInBackground(fInstrumentIds[rIndex], fEngineChannels[channel]);
  352. }
  353. fCurMidiProgs[channel] = index;
  354. if (pData->ctrlChannel == static_cast<int32_t>(channel))
  355. {
  356. pData->midiprog.current = index;
  357. pData->engine->callback(ENGINE_CALLBACK_MIDI_PROGRAM_CHANGED, pData->id, index, 0, 0.0f, nullptr);
  358. }
  359. }
  360. ++channel;
  361. }
  362. CARLA_SAFE_ASSERT(channel == MAX_MIDI_CHANNELS);
  363. }
  364. }
  365. CarlaPlugin::setCustomData(type, key, value, sendGui);
  366. }
  367. void setMidiProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  368. {
  369. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->midiprog.count),);
  370. const int8_t channel(kIsGIG ? pData->ctrlChannel : 0);
  371. if (index >= 0 && channel >= 0 && channel < MAX_MIDI_CHANNELS)
  372. {
  373. const uint32_t bank = pData->midiprog.data[index].bank;
  374. const uint32_t program = pData->midiprog.data[index].program;
  375. const uint32_t rIndex = bank*128 + program;
  376. LinuxSampler::EngineChannel* const engineChannel(fEngineChannels[channel]);
  377. CARLA_SAFE_ASSERT_RETURN(engineChannel != nullptr,);
  378. const ScopedSingleProcessLocker spl(this, (sendGui || sendOsc || sendCallback));
  379. /*if (pData->engine->isOffline())
  380. {
  381. engineChannel->PrepareLoadInstrument(pData->filename, rIndex);
  382. engineChannel->LoadInstrument();
  383. }
  384. else*/
  385. {
  386. try {
  387. fInstrument->LoadInstrumentInBackground(fInstrumentIds[rIndex], engineChannel);
  388. } CARLA_SAFE_EXCEPTION("LinuxSampler setMidiProgram loadInstrument");
  389. }
  390. fCurMidiProgs[channel] = index;
  391. }
  392. CarlaPlugin::setMidiProgram(index, sendGui, sendOsc, sendCallback);
  393. }
  394. // -------------------------------------------------------------------
  395. // Set ui stuff
  396. // nothing
  397. // -------------------------------------------------------------------
  398. // Plugin state
  399. void reload() override
  400. {
  401. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr,);
  402. CARLA_SAFE_ASSERT_RETURN(fInstrument != nullptr,);
  403. carla_debug("LinuxSamplerPlugin::reload() - start");
  404. const EngineProcessMode processMode(pData->engine->getProccessMode());
  405. // Safely disable plugin for reload
  406. const ScopedDisabler sd(this);
  407. if (pData->active)
  408. deactivate();
  409. clearBuffers();
  410. uint32_t aOuts;
  411. aOuts = kUses16Outs ? 32 : 2;
  412. pData->audioOut.createNew(aOuts);
  413. const uint portNameSize(pData->engine->getMaxPortNameSize());
  414. CarlaString portName;
  415. // ---------------------------------------
  416. // Audio Outputs
  417. if (kUses16Outs)
  418. {
  419. for (uint32_t i=0; i < 32; ++i)
  420. {
  421. portName.clear();
  422. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  423. {
  424. portName = pData->name;
  425. portName += ":";
  426. }
  427. portName += "out-";
  428. if ((i+2)/2 < 9)
  429. portName += "0";
  430. portName += CarlaString((i+2)/2);
  431. if (i % 2 == 0)
  432. portName += "L";
  433. else
  434. portName += "R";
  435. portName.truncate(portNameSize);
  436. pData->audioOut.ports[i].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  437. pData->audioOut.ports[i].rindex = i;
  438. }
  439. }
  440. else
  441. {
  442. // out-left
  443. portName.clear();
  444. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  445. {
  446. portName = pData->name;
  447. portName += ":";
  448. }
  449. portName += "out-left";
  450. portName.truncate(portNameSize);
  451. pData->audioOut.ports[0].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  452. pData->audioOut.ports[0].rindex = 0;
  453. // out-right
  454. portName.clear();
  455. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  456. {
  457. portName = pData->name;
  458. portName += ":";
  459. }
  460. portName += "out-right";
  461. portName.truncate(portNameSize);
  462. pData->audioOut.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  463. pData->audioOut.ports[1].rindex = 1;
  464. }
  465. // ---------------------------------------
  466. // Event Input
  467. {
  468. portName.clear();
  469. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  470. {
  471. portName = pData->name;
  472. portName += ":";
  473. }
  474. portName += "events-in";
  475. portName.truncate(portNameSize);
  476. pData->event.portIn = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true);
  477. }
  478. // ---------------------------------------
  479. // plugin hints
  480. pData->hints = 0x0;
  481. pData->hints |= PLUGIN_IS_SYNTH;
  482. pData->hints |= PLUGIN_CAN_VOLUME;
  483. if (! kUses16Outs)
  484. pData->hints |= PLUGIN_CAN_BALANCE;
  485. // extra plugin hints
  486. pData->extraHints = 0x0;
  487. pData->extraHints |= PLUGIN_EXTRA_HINT_HAS_MIDI_IN;
  488. if (! kUses16Outs)
  489. pData->extraHints |= PLUGIN_EXTRA_HINT_CAN_RUN_RACK;
  490. if (fInstrumentIds.size() > 1)
  491. pData->extraHints |= PLUGIN_EXTRA_HINT_USES_MULTI_PROGS;
  492. bufferSizeChanged(pData->engine->getBufferSize());
  493. reloadPrograms(true);
  494. if (pData->active)
  495. activate();
  496. carla_debug("LinuxSamplerPlugin::reload() - end");
  497. }
  498. void reloadPrograms(bool doInit) override
  499. {
  500. carla_debug("LinuxSamplerPlugin::reloadPrograms(%s)", bool2str(doInit));
  501. // Delete old programs
  502. pData->midiprog.clear();
  503. // Query new programs
  504. const uint32_t count(static_cast<uint32_t>(fInstrumentIds.size()));
  505. // sound kits must always have at least 1 midi-program
  506. CARLA_SAFE_ASSERT_RETURN(count > 0,);
  507. pData->midiprog.createNew(count);
  508. // Update data
  509. LinuxSampler::InstrumentManager::instrument_info_t info;
  510. for (uint32_t i=0; i < pData->midiprog.count; ++i)
  511. {
  512. pData->midiprog.data[i].bank = i / 128;
  513. pData->midiprog.data[i].program = i % 128;
  514. try {
  515. info = fInstrument->GetInstrumentInfo(fInstrumentIds[i]);
  516. }
  517. catch (const LinuxSampler::InstrumentManagerException&)
  518. {
  519. continue;
  520. }
  521. pData->midiprog.data[i].name = carla_strdup(info.InstrumentName.c_str());
  522. }
  523. #ifndef BUILD_BRIDGE
  524. // Update OSC Names
  525. if (pData->engine->isOscControlRegistered())
  526. {
  527. pData->engine->oscSend_control_set_midi_program_count(pData->id, count);
  528. for (uint32_t i=0; i < count; ++i)
  529. 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);
  530. }
  531. #endif
  532. if (doInit)
  533. {
  534. for (uint i=0; i<kMaxChannels; ++i)
  535. {
  536. CARLA_SAFE_ASSERT_CONTINUE(fEngineChannels[i] != nullptr);
  537. /*fEngineChannels[i]->PrepareLoadInstrument(pData->filename, 0);
  538. fEngineChannels[i]->LoadInstrument();*/
  539. fInstrument->LoadInstrumentInBackground(fInstrumentIds[0], fEngineChannels[i]);
  540. fCurMidiProgs[i] = 0;
  541. }
  542. pData->midiprog.current = 0;
  543. }
  544. else
  545. {
  546. pData->engine->callback(ENGINE_CALLBACK_RELOAD_PROGRAMS, pData->id, 0, 0, 0.0f, nullptr);
  547. }
  548. }
  549. // -------------------------------------------------------------------
  550. // Plugin processing
  551. #if 0
  552. void activate() override
  553. {
  554. for (int i=0; i < MAX_MIDI_CHANNELS; ++i)
  555. {
  556. if (fAudioOutputDevices[i] != nullptr)
  557. fAudioOutputDevices[i]->Play();
  558. }
  559. }
  560. void deactivate() override
  561. {
  562. for (int i=0; i < MAX_MIDI_CHANNELS; ++i)
  563. {
  564. if (fAudioOutputDevices[i] != nullptr)
  565. fAudioOutputDevices[i]->Stop();
  566. }
  567. }
  568. #endif
  569. void process(float** const, float** const outBuffer, const uint32_t frames) override
  570. {
  571. // --------------------------------------------------------------------------------------------------------
  572. // Check if active
  573. if (! pData->active)
  574. {
  575. // disable any output sound
  576. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  577. FloatVectorOperations::clear(outBuffer[i], static_cast<int>(frames));
  578. return;
  579. }
  580. // --------------------------------------------------------------------------------------------------------
  581. // Check if needs reset
  582. if (pData->needsReset)
  583. {
  584. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  585. {
  586. for (uint i=0; i < MAX_MIDI_CHANNELS; ++i)
  587. {
  588. fMidiInputPort->DispatchControlChange(MIDI_CONTROL_ALL_NOTES_OFF, 0, i);
  589. fMidiInputPort->DispatchControlChange(MIDI_CONTROL_ALL_SOUND_OFF, 0, i);
  590. }
  591. }
  592. else if (pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  593. {
  594. for (uint8_t i=0; i < MAX_MIDI_NOTE; ++i)
  595. fMidiInputPort->DispatchNoteOff(i, 0, uint(pData->ctrlChannel));
  596. }
  597. pData->needsReset = false;
  598. }
  599. // --------------------------------------------------------------------------------------------------------
  600. // Event Input and Processing
  601. {
  602. // ----------------------------------------------------------------------------------------------------
  603. // MIDI Input (External)
  604. if (pData->extNotes.mutex.tryLock())
  605. {
  606. for (RtLinkedList<ExternalMidiNote>::Itenerator it = pData->extNotes.data.begin(); it.valid(); it.next())
  607. {
  608. const ExternalMidiNote& note(it.getValue());
  609. CARLA_SAFE_ASSERT_CONTINUE(note.channel >= 0 && note.channel < MAX_MIDI_CHANNELS);
  610. if (note.velo > 0)
  611. fMidiInputPort->DispatchNoteOn(note.note, note.velo, static_cast<uint>(note.channel));
  612. else
  613. fMidiInputPort->DispatchNoteOff(note.note, note.velo, static_cast<uint>(note.channel));
  614. }
  615. pData->extNotes.data.clear();
  616. pData->extNotes.mutex.unlock();
  617. } // End of MIDI Input (External)
  618. // ----------------------------------------------------------------------------------------------------
  619. // Event Input (System)
  620. #ifndef BUILD_BRIDGE
  621. bool allNotesOffSent = false;
  622. #endif
  623. bool sampleAccurate = (pData->options & PLUGIN_OPTION_FIXED_BUFFERS) == 0;
  624. uint32_t nEvents = pData->event.portIn->getEventCount();
  625. uint32_t startTime = 0;
  626. uint32_t timeOffset = 0;
  627. uint32_t nextBankIds[MAX_MIDI_CHANNELS] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 0, 0, 0, 0, 0, 0 };
  628. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0 && pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  629. nextBankIds[pData->ctrlChannel] = pData->midiprog.data[pData->midiprog.current].bank;
  630. for (uint32_t i=0; i < nEvents; ++i)
  631. {
  632. const EngineEvent& event(pData->event.portIn->getEvent(i));
  633. CARLA_SAFE_ASSERT_CONTINUE(event.time < frames);
  634. CARLA_SAFE_ASSERT_BREAK(event.time >= timeOffset);
  635. if (event.time > timeOffset && sampleAccurate)
  636. {
  637. if (processSingle(outBuffer, event.time - timeOffset, timeOffset))
  638. {
  639. startTime = 0;
  640. timeOffset = event.time;
  641. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0 && pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  642. nextBankIds[pData->ctrlChannel] = pData->midiprog.data[pData->midiprog.current].bank;
  643. }
  644. else
  645. startTime += timeOffset;
  646. }
  647. // Control change
  648. switch (event.type)
  649. {
  650. case kEngineEventTypeNull:
  651. break;
  652. case kEngineEventTypeControl:
  653. {
  654. const EngineControlEvent& ctrlEvent = event.ctrl;
  655. switch (ctrlEvent.type)
  656. {
  657. case kEngineControlEventTypeNull:
  658. break;
  659. case kEngineControlEventTypeParameter:
  660. {
  661. #ifndef BUILD_BRIDGE
  662. // Control backend stuff
  663. if (event.channel == pData->ctrlChannel)
  664. {
  665. float value;
  666. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_DRYWET) != 0)
  667. {
  668. value = ctrlEvent.value;
  669. setDryWet(value, false, false);
  670. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, value);
  671. }
  672. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_VOLUME) != 0)
  673. {
  674. value = ctrlEvent.value*127.0f/100.0f;
  675. setVolume(value, false, false);
  676. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
  677. }
  678. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_BALANCE) != 0)
  679. {
  680. float left, right;
  681. value = ctrlEvent.value/0.5f - 1.0f;
  682. if (value < 0.0f)
  683. {
  684. left = -1.0f;
  685. right = (value*2.0f)+1.0f;
  686. }
  687. else if (value > 0.0f)
  688. {
  689. left = (value*2.0f)-1.0f;
  690. right = 1.0f;
  691. }
  692. else
  693. {
  694. left = -1.0f;
  695. right = 1.0f;
  696. }
  697. setBalanceLeft(left, false, false);
  698. setBalanceRight(right, false, false);
  699. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  700. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  701. }
  702. }
  703. // Control plugin parameters
  704. for (uint32_t k=0; k < pData->param.count; ++k)
  705. {
  706. if (pData->param.data[k].midiChannel != event.channel)
  707. continue;
  708. if (pData->param.data[k].midiCC != ctrlEvent.param)
  709. continue;
  710. if (pData->param.data[k].hints != PARAMETER_INPUT)
  711. continue;
  712. if ((pData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  713. continue;
  714. float value;
  715. if (pData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  716. {
  717. value = (ctrlEvent.value < 0.5f) ? pData->param.ranges[k].min : pData->param.ranges[k].max;
  718. }
  719. else
  720. {
  721. value = pData->param.ranges[k].getUnnormalizedValue(ctrlEvent.value);
  722. if (pData->param.data[k].hints & PARAMETER_IS_INTEGER)
  723. value = std::rint(value);
  724. }
  725. setParameterValue(k, value, false, false, false);
  726. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, value);
  727. }
  728. #endif
  729. if ((pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0 && ctrlEvent.param <= 0x5F)
  730. {
  731. fMidiInputPort->DispatchControlChange(uint8_t(ctrlEvent.param), uint8_t(ctrlEvent.value*127.0f), event.channel, static_cast<int32_t>(sampleAccurate ? startTime : event.time));
  732. }
  733. break;
  734. }
  735. case kEngineControlEventTypeMidiBank:
  736. if (event.channel < MAX_MIDI_CHANNELS && (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  737. nextBankIds[event.channel] = ctrlEvent.param;
  738. break;
  739. case kEngineControlEventTypeMidiProgram:
  740. if (event.channel < MAX_MIDI_CHANNELS && (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  741. {
  742. const uint32_t bankId(nextBankIds[event.channel]);
  743. const uint32_t progId(ctrlEvent.param);
  744. const uint32_t rIndex = bankId*128 + progId;
  745. for (uint32_t k=0; k < pData->midiprog.count; ++k)
  746. {
  747. if (pData->midiprog.data[k].bank == bankId && pData->midiprog.data[k].program == progId)
  748. {
  749. LinuxSampler::EngineChannel* const engineChannel(fEngineChannels[kIsGIG ? event.channel : 0]);
  750. CARLA_SAFE_ASSERT_CONTINUE(engineChannel != nullptr);
  751. /*if (pData->engine->isOffline())
  752. {
  753. engineChannel->PrepareLoadInstrument(pData->filename, rIndex);
  754. engineChannel->LoadInstrument();
  755. }
  756. else*/
  757. {
  758. fInstrument->LoadInstrumentInBackground(fInstrumentIds[rIndex], engineChannel);
  759. }
  760. fCurMidiProgs[event.channel] = static_cast<int32_t>(k);
  761. if (event.channel == pData->ctrlChannel)
  762. pData->postponeRtEvent(kPluginPostRtEventMidiProgramChange, static_cast<int32_t>(k), 0, 0.0f);
  763. break;
  764. }
  765. }
  766. }
  767. break;
  768. case kEngineControlEventTypeAllSoundOff:
  769. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  770. {
  771. fMidiInputPort->DispatchControlChange(MIDI_CONTROL_ALL_SOUND_OFF, 0, event.channel, static_cast<int32_t>(sampleAccurate ? startTime : event.time));
  772. }
  773. break;
  774. case kEngineControlEventTypeAllNotesOff:
  775. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  776. {
  777. #ifndef BUILD_BRIDGE
  778. if (event.channel == pData->ctrlChannel && ! allNotesOffSent)
  779. {
  780. allNotesOffSent = true;
  781. sendMidiAllNotesOffToCallback();
  782. }
  783. #endif
  784. fMidiInputPort->DispatchControlChange(MIDI_CONTROL_ALL_NOTES_OFF, 0, event.channel, static_cast<int32_t>(sampleAccurate ? startTime : event.time));
  785. }
  786. break;
  787. }
  788. break;
  789. }
  790. case kEngineEventTypeMidi:
  791. {
  792. const EngineMidiEvent& midiEvent(event.midi);
  793. uint8_t status = uint8_t(MIDI_GET_STATUS_FROM_DATA(midiEvent.data));
  794. uint8_t channel = event.channel;
  795. // Fix bad note-off
  796. if (MIDI_IS_STATUS_NOTE_ON(status) && midiEvent.data[2] == 0)
  797. status = MIDI_STATUS_NOTE_OFF;
  798. if (MIDI_IS_STATUS_POLYPHONIC_AFTERTOUCH(status) && (pData->options & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) == 0)
  799. continue;
  800. if (MIDI_IS_STATUS_CONTROL_CHANGE(status) && (pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) == 0)
  801. continue;
  802. if (MIDI_IS_STATUS_CHANNEL_PRESSURE(status) && (pData->options & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) == 0)
  803. continue;
  804. if (MIDI_IS_STATUS_PITCH_WHEEL_CONTROL(status) && (pData->options & PLUGIN_OPTION_SEND_PITCHBEND) == 0)
  805. continue;
  806. // put back channel in data
  807. uint8_t data[EngineMidiEvent::kDataSize];
  808. std::memcpy(data, event.midi.data, EngineMidiEvent::kDataSize);
  809. if (status < 0xF0 && channel < MAX_MIDI_CHANNELS)
  810. data[0] = uint8_t(data[0] + channel);
  811. fMidiInputPort->DispatchRaw(data, static_cast<int32_t>(sampleAccurate ? startTime : event.time));
  812. if (status == MIDI_STATUS_NOTE_ON)
  813. pData->postponeRtEvent(kPluginPostRtEventNoteOn, channel, data[1], data[2]);
  814. else if (status == MIDI_STATUS_NOTE_OFF)
  815. pData->postponeRtEvent(kPluginPostRtEventNoteOff, channel,data[1], 0.0f);
  816. break;
  817. }
  818. }
  819. }
  820. pData->postRtEvents.trySplice();
  821. if (frames > timeOffset)
  822. processSingle(outBuffer, frames - timeOffset, timeOffset);
  823. } // End of Event Input and Processing
  824. }
  825. bool processSingle(float** const outBuffer, const uint32_t frames, const uint32_t timeOffset)
  826. {
  827. CARLA_SAFE_ASSERT_RETURN(outBuffer != nullptr, false);
  828. CARLA_SAFE_ASSERT_RETURN(frames > 0, false);
  829. // --------------------------------------------------------------------------------------------------------
  830. // Try lock, silence otherwise
  831. if (pData->engine->isOffline())
  832. {
  833. pData->singleMutex.lock();
  834. }
  835. else if (! pData->singleMutex.tryLock())
  836. {
  837. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  838. {
  839. for (uint32_t k=0; k < frames; ++k)
  840. outBuffer[i][k+timeOffset] = 0.0f;
  841. }
  842. return false;
  843. }
  844. // --------------------------------------------------------------------------------------------------------
  845. // Run plugin
  846. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  847. {
  848. if (LinuxSampler::AudioChannel* const outDev = fAudioOutputDevice->Channel(i))
  849. outDev->SetBuffer(outBuffer[i] + timeOffset);
  850. }
  851. fAudioOutputDevice->Render(frames);
  852. #ifndef BUILD_BRIDGE
  853. // --------------------------------------------------------------------------------------------------------
  854. // Post-processing (dry/wet, volume and balance)
  855. {
  856. const bool doVolume = (pData->hints & PLUGIN_CAN_VOLUME) > 0 && pData->postProc.volume != 1.0f;
  857. const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) > 0 && (pData->postProc.balanceLeft != -1.0f || pData->postProc.balanceRight != 1.0f);
  858. float oldBufLeft[doBalance ? frames : 1];
  859. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  860. {
  861. // Balance
  862. if (doBalance)
  863. {
  864. if (i % 2 == 0)
  865. FloatVectorOperations::copy(oldBufLeft, outBuffer[i], static_cast<int>(frames));
  866. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  867. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  868. for (uint32_t k=0; k < frames; ++k)
  869. {
  870. if (i % 2 == 0)
  871. {
  872. // left
  873. outBuffer[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  874. outBuffer[i][k] += outBuffer[i+1][k] * (1.0f - balRangeR);
  875. }
  876. else
  877. {
  878. // right
  879. outBuffer[i][k] = outBuffer[i][k] * balRangeR;
  880. outBuffer[i][k] += oldBufLeft[k] * balRangeL;
  881. }
  882. }
  883. }
  884. // Volume
  885. if (doVolume)
  886. {
  887. for (uint32_t k=0; k < frames; ++k)
  888. outBuffer[i][k+timeOffset] *= pData->postProc.volume;
  889. }
  890. }
  891. } // End of Post-processing
  892. #endif
  893. // --------------------------------------------------------------------------------------------------------
  894. pData->singleMutex.unlock();
  895. return true;
  896. }
  897. #ifndef CARLA_OS_WIN // FIXME, need to update linuxsampler win32 build
  898. void bufferSizeChanged(const uint32_t) override
  899. {
  900. CARLA_SAFE_ASSERT_RETURN(fAudioOutputDevice != nullptr,);
  901. fAudioOutputDevice->ReconnectAll();
  902. }
  903. void sampleRateChanged(const double) override
  904. {
  905. CARLA_SAFE_ASSERT_RETURN(fAudioOutputDevice != nullptr,);
  906. fAudioOutputDevice->ReconnectAll();
  907. }
  908. #endif
  909. // -------------------------------------------------------------------
  910. // Plugin buffers
  911. // nothing
  912. // -------------------------------------------------------------------
  913. const void* getExtraStuff() const noexcept override
  914. {
  915. static const char xtrue[] = "true";
  916. static const char xfalse[] = "false";
  917. return kUses16Outs ? xtrue : xfalse;
  918. }
  919. bool init(const char* const filename, const char* const name, const char* const label)
  920. {
  921. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  922. // ---------------------------------------------------------------
  923. // first checks
  924. if (pData->client != nullptr)
  925. {
  926. pData->engine->setLastError("Plugin client is already registered");
  927. return false;
  928. }
  929. if (filename == nullptr || filename[0] == '\0')
  930. {
  931. pData->engine->setLastError("null filename");
  932. return false;
  933. }
  934. // ---------------------------------------------------------------
  935. // Init LinuxSampler stuff
  936. fAudioOutputDevice = new LinuxSampler::AudioOutputDevicePlugin(pData->engine, this, kUses16Outs);
  937. fMidiInputDevice = new LinuxSampler::MidiInputDevicePlugin(sSampler);
  938. fMidiInputPort = fMidiInputDevice->CreateMidiPortPlugin();
  939. for (uint i=0; i<kMaxChannels; ++i)
  940. {
  941. fSamplerChannels[i] = sSampler->AddSamplerChannel();
  942. CARLA_SAFE_ASSERT_CONTINUE(fSamplerChannels[i] != nullptr);
  943. fSamplerChannels[i]->SetEngineType(kIsGIG ? "GIG" : "SFZ");
  944. fSamplerChannels[i]->SetAudioOutputDevice(fAudioOutputDevice);
  945. fEngineChannels[i] = fSamplerChannels[i]->GetEngineChannel();
  946. CARLA_SAFE_ASSERT_CONTINUE(fEngineChannels[i] != nullptr);
  947. fEngineChannels[i]->Connect(fAudioOutputDevice);
  948. fEngineChannels[i]->Volume(LinuxSampler::kVolumeMax);
  949. if (kUses16Outs)
  950. {
  951. fEngineChannels[i]->SetOutputChannel(0, i*2);
  952. fEngineChannels[i]->SetOutputChannel(1, i*2 +1);
  953. }
  954. else
  955. {
  956. fEngineChannels[i]->SetOutputChannel(0, 0);
  957. fEngineChannels[i]->SetOutputChannel(1, 1);
  958. }
  959. fMidiInputPort->Connect(fEngineChannels[i], static_cast<LinuxSampler::midi_chan_t>(i));
  960. }
  961. // ---------------------------------------------------------------
  962. // Get the Engine's Instrument Manager
  963. fInstrument = kIsGIG ? sEngineGIG->engine->GetInstrumentManager() : sEngineSFZ->engine->GetInstrumentManager();
  964. if (fInstrument == nullptr)
  965. {
  966. pData->engine->setLastError("Failed to get LinuxSampler instrument manager");
  967. return false;
  968. }
  969. // ---------------------------------------------------------------
  970. // Load the Instrument via filename
  971. try {
  972. fInstrumentIds = fInstrument->GetInstrumentFileContent(filename);
  973. }
  974. catch (const LinuxSampler::InstrumentManagerException& e)
  975. {
  976. pData->engine->setLastError(e.what());
  977. return false;
  978. }
  979. // ---------------------------------------------------------------
  980. // Get info
  981. if (fInstrumentIds.size() == 0)
  982. {
  983. pData->engine->setLastError("Failed to find any instruments");
  984. return false;
  985. }
  986. LinuxSampler::InstrumentManager::instrument_info_t info;
  987. try {
  988. info = fInstrument->GetInstrumentInfo(fInstrumentIds[0]);
  989. }
  990. catch (const LinuxSampler::InstrumentManagerException& e)
  991. {
  992. pData->engine->setLastError(e.what());
  993. return false;
  994. }
  995. CarlaString label2(label != nullptr ? label : File(filename).getFileNameWithoutExtension().toRawUTF8());
  996. if (kUses16Outs && ! label2.endsWith(" (16 outs)"))
  997. label2 += " (16 outs)";
  998. fLabel = label2.dup();
  999. fMaker = carla_strdup(info.Artists.c_str());
  1000. fRealName = carla_strdup(info.InstrumentName.c_str());
  1001. pData->filename = carla_strdup(filename);
  1002. if (name != nullptr && name[0] != '\0')
  1003. pData->name = pData->engine->getUniquePluginName(name);
  1004. else if (fRealName[0] != '\0')
  1005. pData->name = pData->engine->getUniquePluginName(fRealName);
  1006. else
  1007. pData->name = pData->engine->getUniquePluginName(fLabel);
  1008. // ---------------------------------------------------------------
  1009. // register client
  1010. pData->client = pData->engine->addClient(this);
  1011. if (pData->client == nullptr || ! pData->client->isOk())
  1012. {
  1013. pData->engine->setLastError("Failed to register plugin client");
  1014. return false;
  1015. }
  1016. // ---------------------------------------------------------------
  1017. // set default options
  1018. pData->options = 0x0;
  1019. pData->options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  1020. pData->options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  1021. pData->options |= PLUGIN_OPTION_SEND_PITCHBEND;
  1022. pData->options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  1023. return true;
  1024. }
  1025. // -------------------------------------------------------------------
  1026. private:
  1027. const bool kIsGIG; // SFZ if false
  1028. const bool kUses16Outs;
  1029. const uint kMaxChannels; // 1 or 16 depending on format
  1030. const char* fLabel;
  1031. const char* fMaker;
  1032. const char* fRealName;
  1033. int32_t fCurMidiProgs[MAX_MIDI_CHANNELS];
  1034. LinuxSampler::SamplerChannel* fSamplerChannels[MAX_MIDI_CHANNELS];
  1035. LinuxSampler::EngineChannel* fEngineChannels[MAX_MIDI_CHANNELS];
  1036. LinuxSampler::AudioOutputDevicePlugin* fAudioOutputDevice;
  1037. LinuxSampler::MidiInputDevicePlugin* fMidiInputDevice;
  1038. LinuxSampler::MidiInputPortPlugin* fMidiInputPort;
  1039. LinuxSampler::InstrumentManager* fInstrument;
  1040. std::vector<LinuxSampler::InstrumentManager::instrument_id_t> fInstrumentIds;
  1041. SharedResourcePointer<LinuxSampler::Sampler> sSampler;
  1042. SharedResourcePointer<LinuxSampler::EngineGIG> sEngineGIG;
  1043. SharedResourcePointer<LinuxSampler::EngineSFZ> sEngineSFZ;
  1044. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(LinuxSamplerPlugin)
  1045. };
  1046. CARLA_BACKEND_END_NAMESPACE
  1047. #endif // HAVE_LINUXSAMPLER
  1048. CARLA_BACKEND_START_NAMESPACE
  1049. // -------------------------------------------------------------------------------------------------------------------
  1050. CarlaPlugin* CarlaPlugin::newLinuxSampler(const Initializer& init, const char* const format, const bool use16Outs)
  1051. {
  1052. 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));
  1053. #ifdef HAVE_LINUXSAMPLER
  1054. CarlaString sformat(format);
  1055. sformat.toLower();
  1056. if (sformat != "gig" && sformat != "sfz")
  1057. {
  1058. init.engine->setLastError("Unsupported format requested for LinuxSampler");
  1059. return nullptr;
  1060. }
  1061. if (init.engine->getProccessMode() == ENGINE_PROCESS_MODE_CONTINUOUS_RACK && use16Outs)
  1062. {
  1063. init.engine->setLastError("Carla's rack mode can only work with Stereo modules, please choose the 2-channel only sample-library version");
  1064. return nullptr;
  1065. }
  1066. // -------------------------------------------------------------------
  1067. // Check if file exists
  1068. if (! File(init.filename).existsAsFile())
  1069. {
  1070. init.engine->setLastError("Requested file is not valid or does not exist");
  1071. return nullptr;
  1072. }
  1073. LinuxSamplerPlugin* const plugin(new LinuxSamplerPlugin(init.engine, init.id, (sformat == "gig"), use16Outs));
  1074. if (! plugin->init(init.filename, init.name, init.label))
  1075. {
  1076. delete plugin;
  1077. return nullptr;
  1078. }
  1079. plugin->reload();
  1080. return plugin;
  1081. #else
  1082. init.engine->setLastError("linuxsampler support not available");
  1083. return nullptr;
  1084. // unused
  1085. (void)format;
  1086. (void)use16Outs;
  1087. #endif
  1088. }
  1089. CarlaPlugin* CarlaPlugin::newFileGIG(const Initializer& init, const bool use16Outs)
  1090. {
  1091. carla_debug("CarlaPlugin::newFileGIG({%p, \"%s\", \"%s\", \"%s\"}, %s)", init.engine, init.filename, init.name, init.label, bool2str(use16Outs));
  1092. #ifdef HAVE_LINUXSAMPLER
  1093. return newLinuxSampler(init, "GIG", use16Outs);
  1094. #else
  1095. init.engine->setLastError("GIG support not available");
  1096. return nullptr;
  1097. // unused
  1098. (void)use16Outs;
  1099. #endif
  1100. }
  1101. CarlaPlugin* CarlaPlugin::newFileSFZ(const Initializer& init)
  1102. {
  1103. carla_debug("CarlaPlugin::newFileSFZ({%p, \"%s\", \"%s\", \"%s\"})", init.engine, init.filename, init.name, init.label);
  1104. #ifdef HAVE_LINUXSAMPLER
  1105. return newLinuxSampler(init, "SFZ", false);
  1106. #else
  1107. init.engine->setLastError("SFZ support not available");
  1108. return nullptr;
  1109. #endif
  1110. }
  1111. // -------------------------------------------------------------------------------------------------------------------
  1112. CARLA_BACKEND_END_NAMESPACE