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.

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