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.

1465 lines
51KB

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