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.

1435 lines
50KB

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