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.

1470 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/juce_core.h"
  31. #include <linuxsampler/Sampler.h>
  32. // -----------------------------------------------------------------------
  33. namespace LinuxSampler {
  34. using CarlaBackend::CarlaEngine;
  35. using CarlaBackend::CarlaPlugin;
  36. // -----------------------------------------------------------------------
  37. // LinuxSampler static values
  38. static const float kVolumeMax = 3.16227766f; // +10 dB
  39. static const uint kMaxStreams = 90*2; // default is not *2
  40. static const uint kMaxVoices = 64*2;
  41. // -----------------------------------------------------------------------
  42. // LinuxSampler AudioOutputDevice Plugin
  43. class AudioOutputDevicePlugin : public AudioOutputDevice
  44. {
  45. public:
  46. AudioOutputDevicePlugin(const CarlaEngine* const engine, const CarlaPlugin* const plugin, const bool uses16Outs)
  47. : AudioOutputDevice(std::map<std::string, DeviceCreationParameter*>()),
  48. kEngine(engine),
  49. kPlugin(plugin)
  50. {
  51. CARLA_ASSERT(engine != nullptr);
  52. CARLA_ASSERT(plugin != nullptr);
  53. AcquireChannels(uses16Outs ? 32 : 2);
  54. }
  55. ~AudioOutputDevicePlugin() override {}
  56. // -------------------------------------------------------------------
  57. // LinuxSampler virtual methods
  58. void Play() override {}
  59. void Stop() override {}
  60. bool IsPlaying() override
  61. {
  62. return (kEngine->isRunning() && kPlugin->isEnabled());
  63. }
  64. uint MaxSamplesPerCycle() override
  65. {
  66. return kEngine->getBufferSize();
  67. }
  68. uint SampleRate() override
  69. {
  70. return uint(kEngine->getSampleRate());
  71. }
  72. std::string Driver() override
  73. {
  74. return "AudioOutputDevicePlugin";
  75. }
  76. AudioChannel* CreateChannel(uint channelNr) override
  77. {
  78. return new AudioChannel(channelNr, nullptr, 0);
  79. }
  80. // -------------------------------------------------------------------
  81. /* */ bool isAutonomousDevice() override { return false; }
  82. static bool isAutonomousDriver() { return false; }
  83. // -------------------------------------------------------------------
  84. // Give public access to the RenderAudio call
  85. int Render(const uint samples)
  86. {
  87. return RenderAudio(samples);
  88. }
  89. // -------------------------------------------------------------------
  90. private:
  91. const CarlaEngine* const kEngine;
  92. const CarlaPlugin* const kPlugin;
  93. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(AudioOutputDevicePlugin)
  94. };
  95. // -----------------------------------------------------------------------
  96. // LinuxSampler MidiInputPort Plugin
  97. class MidiInputPortPlugin : public MidiInputPort
  98. {
  99. public:
  100. MidiInputPortPlugin(MidiInputDevice* const device, const int portNum = 0)
  101. : MidiInputPort(device, portNum) {}
  102. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(MidiInputPortPlugin)
  103. };
  104. // -----------------------------------------------------------------------
  105. // LinuxSampler MidiInputDevice Plugin
  106. class MidiInputDevicePlugin : public MidiInputDevice
  107. {
  108. public:
  109. MidiInputDevicePlugin(Sampler* const sampler)
  110. : MidiInputDevice(std::map<std::string, DeviceCreationParameter*>(), sampler)
  111. {
  112. AcquirePorts(1);
  113. }
  114. ~MidiInputDevicePlugin() override
  115. {
  116. for (std::map<int,MidiInputPort*>::iterator it = Ports.begin(), end = Ports.end(); it != end; ++it)
  117. delete dynamic_cast<MidiInputPortPlugin*>(it->second);
  118. Ports.clear();
  119. }
  120. // -------------------------------------------------------------------
  121. // LinuxSampler virtual methods
  122. void Listen() override {}
  123. void StopListen() override {}
  124. std::string Driver() override
  125. {
  126. return "MidiInputDevicePlugin";
  127. }
  128. MidiInputPort* CreateMidiPort() override
  129. {
  130. return new MidiInputPortPlugin(this, int(PortCount()));
  131. }
  132. // -------------------------------------------------------------------
  133. /* */ bool isAutonomousDevice() override { return false; }
  134. static bool isAutonomousDriver() { return false; }
  135. // -------------------------------------------------------------------
  136. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(MidiInputDevicePlugin)
  137. };
  138. } // namespace LinuxSampler
  139. // -----------------------------------------------------------------------
  140. using juce::File;
  141. using juce::SharedResourcePointer;
  142. using juce::StringArray;
  143. CARLA_BACKEND_START_NAMESPACE
  144. // -----------------------------------------------------------------------
  145. class CarlaPluginLinuxSampler : public CarlaPlugin
  146. {
  147. public:
  148. CarlaPluginLinuxSampler(CarlaEngine* const engine, const uint id, const bool isGIG, const bool use16Outs)
  149. : CarlaPlugin(engine, id),
  150. kIsGIG(isGIG),
  151. kUses16Outs(use16Outs && isGIG),
  152. kMaxChannels(isGIG ? MAX_MIDI_CHANNELS : 1),
  153. fLabel(nullptr),
  154. fMaker(nullptr),
  155. fRealName(nullptr),
  156. sSampler(),
  157. fAudioOutputDevice(engine, this, kUses16Outs),
  158. fMidiInputDevice(sSampler),
  159. fMidiInputPort(nullptr),
  160. fInstrumentIds(),
  161. fInstrumentInfo()
  162. {
  163. carla_debug("CarlaPluginLinuxSampler::CarlaPluginLinuxSampler(%p, %i, %s, %s)", engine, id, bool2str(isGIG), bool2str(use16Outs));
  164. // TODO - option for this
  165. sSampler->SetGlobalMaxStreams(LinuxSampler::kMaxStreams);
  166. sSampler->SetGlobalMaxVoices(LinuxSampler::kMaxVoices);
  167. carla_zeroStructs(fCurProgs, MAX_MIDI_CHANNELS);
  168. carla_zeroStructs(fSamplerChannels, MAX_MIDI_CHANNELS);
  169. carla_zeroStructs(fEngineChannels, MAX_MIDI_CHANNELS);
  170. carla_zeroFloats(fParamBuffers, LinuxSamplerParametersMax);
  171. if (use16Outs && ! isGIG)
  172. carla_stderr("Tried to use SFZ with 16 stereo outs, this doesn't make much sense so single stereo mode will be used instead");
  173. }
  174. ~CarlaPluginLinuxSampler() override
  175. {
  176. carla_debug("CarlaPluginLinuxSampler::~CarlaPluginLinuxSampler()");
  177. pData->singleMutex.lock();
  178. pData->masterMutex.lock();
  179. if (pData->client != nullptr && pData->client->isActive())
  180. pData->client->deactivate();
  181. if (pData->active)
  182. {
  183. deactivate();
  184. pData->active = false;
  185. }
  186. fMidiInputPort = nullptr;
  187. for (uint i=0; i<kMaxChannels; ++i)
  188. {
  189. if (fSamplerChannels[i] != nullptr)
  190. {
  191. if (fEngineChannels[i] != nullptr)
  192. {
  193. fEngineChannels[i]->DisconnectAudioOutputDevice();
  194. fEngineChannels[i]->DisconnectAllMidiInputPorts();
  195. fEngineChannels[i] = nullptr;
  196. }
  197. sSampler->RemoveSamplerChannel(fSamplerChannels[i]);
  198. fSamplerChannels[i] = nullptr;
  199. }
  200. }
  201. fInstrumentIds.clear();
  202. fInstrumentInfo.clear();
  203. if (fLabel != nullptr)
  204. {
  205. delete[] fLabel;
  206. fLabel = nullptr;
  207. }
  208. if (fMaker != nullptr)
  209. {
  210. delete[] fMaker;
  211. fMaker = nullptr;
  212. }
  213. if (fRealName != nullptr)
  214. {
  215. delete[] fRealName;
  216. fRealName = nullptr;
  217. }
  218. clearBuffers();
  219. }
  220. // -------------------------------------------------------------------
  221. // Information (base)
  222. PluginType getType() const noexcept override
  223. {
  224. return kIsGIG ? PLUGIN_GIG : PLUGIN_SFZ;
  225. }
  226. PluginCategory getCategory() const noexcept override
  227. {
  228. return PLUGIN_CATEGORY_SYNTH;
  229. }
  230. // -------------------------------------------------------------------
  231. // Information (count)
  232. // nothing
  233. // -------------------------------------------------------------------
  234. // Information (current data)
  235. // nothing
  236. // -------------------------------------------------------------------
  237. // Information (per-plugin data)
  238. uint getOptionsAvailable() const noexcept override
  239. {
  240. uint options = 0x0;
  241. options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  242. options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  243. options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  244. options |= PLUGIN_OPTION_SEND_PITCHBEND;
  245. options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  246. if (kIsGIG)
  247. options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  248. return options;
  249. }
  250. float getParameterValue(const uint32_t parameterId) const noexcept override
  251. {
  252. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0.0f);
  253. return fParamBuffers[parameterId];
  254. }
  255. void getLabel(char* const strBuf) const noexcept override
  256. {
  257. if (fLabel != nullptr)
  258. {
  259. std::strncpy(strBuf, fLabel, STR_MAX);
  260. return;
  261. }
  262. CarlaPlugin::getLabel(strBuf);
  263. }
  264. void getMaker(char* const strBuf) const noexcept override
  265. {
  266. if (fMaker != nullptr)
  267. {
  268. std::strncpy(strBuf, fMaker, STR_MAX);
  269. return;
  270. }
  271. CarlaPlugin::getMaker(strBuf);
  272. }
  273. void getCopyright(char* const strBuf) const noexcept override
  274. {
  275. getMaker(strBuf);
  276. }
  277. void getRealName(char* const strBuf) const noexcept override
  278. {
  279. if (fRealName != nullptr)
  280. {
  281. std::strncpy(strBuf, fRealName, STR_MAX);
  282. return;
  283. }
  284. CarlaPlugin::getRealName(strBuf);
  285. }
  286. void getParameterName(const uint32_t parameterId, char* const strBuf) const noexcept override
  287. {
  288. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  289. switch (parameterId)
  290. {
  291. case LinuxSamplerDiskStreamCount:
  292. std::strncpy(strBuf, "Disk Stream Count", STR_MAX);
  293. return;
  294. case LinuxSamplerVoiceCount:
  295. std::strncpy(strBuf, "Voice Count", STR_MAX);
  296. return;
  297. }
  298. CarlaPlugin::getParameterName(parameterId, strBuf);
  299. }
  300. // -------------------------------------------------------------------
  301. // Set data (state)
  302. void prepareForSave() override
  303. {
  304. if (kMaxChannels > 1 && fInstrumentIds.size() > 1)
  305. {
  306. char strBuf[STR_MAX+1];
  307. 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],
  308. fCurProgs[4], fCurProgs[5], fCurProgs[6], fCurProgs[7],
  309. fCurProgs[8], fCurProgs[9], fCurProgs[10], fCurProgs[11],
  310. fCurProgs[12], fCurProgs[13], fCurProgs[14], fCurProgs[15]);
  311. CarlaPlugin::setCustomData(CUSTOM_DATA_TYPE_STRING, "programs", strBuf, false);
  312. }
  313. }
  314. // -------------------------------------------------------------------
  315. // Set data (internal stuff)
  316. void setCtrlChannel(const int8_t channel, const bool sendOsc, const bool sendCallback) noexcept override
  317. {
  318. if (channel >= 0 && channel < MAX_MIDI_CHANNELS)
  319. pData->prog.current = static_cast<int32_t>(fCurProgs[channel]);
  320. CarlaPlugin::setCtrlChannel(channel, sendOsc, sendCallback);
  321. }
  322. // -------------------------------------------------------------------
  323. // Set data (plugin-specific stuff)
  324. void setCustomData(const char* const type, const char* const key, const char* const value, const bool sendGui) override
  325. {
  326. CARLA_SAFE_ASSERT_RETURN(type != nullptr && type[0] != '\0',);
  327. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  328. CARLA_SAFE_ASSERT_RETURN(value != nullptr && value[0] != '\0',);
  329. carla_debug("CarlaPluginLinuxSampler::setCustomData(%s, \"%s\", \"%s\", %s)", type, key, value, bool2str(sendGui));
  330. if (std::strcmp(type, CUSTOM_DATA_TYPE_PROPERTY) == 0)
  331. return CarlaPlugin::setCustomData(type, key, value, sendGui);
  332. if (std::strcmp(type, CUSTOM_DATA_TYPE_STRING) != 0)
  333. return carla_stderr2("CarlaPluginLinuxSampler::setCustomData(\"%s\", \"%s\", \"%s\", %s) - type is not string", type, key, value, bool2str(sendGui));
  334. if (std::strcmp(key, "programs") != 0)
  335. return carla_stderr2("CarlaPluginLinuxSampler::setCustomData(\"%s\", \"%s\", \"%s\", %s) - key is not programs", type, key, value, bool2str(sendGui));
  336. if (kMaxChannels > 1 && fInstrumentIds.size() > 1)
  337. {
  338. StringArray programList(StringArray::fromTokens(value, ":", ""));
  339. if (programList.size() == MAX_MIDI_CHANNELS)
  340. {
  341. uint8_t channel = 0;
  342. for (juce::String *it=programList.begin(), *end=programList.end(); it != end; ++it)
  343. {
  344. const int index(it->getIntValue());
  345. if (index >= 0)
  346. setProgramInternal(static_cast<uint>(index), channel, true, false);
  347. if (++channel >= MAX_MIDI_CHANNELS)
  348. break;
  349. }
  350. CARLA_SAFE_ASSERT(channel == MAX_MIDI_CHANNELS);
  351. }
  352. }
  353. CarlaPlugin::setCustomData(type, key, value, sendGui);
  354. }
  355. void setProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  356. {
  357. CARLA_SAFE_ASSERT_RETURN(sendGui || sendOsc || sendCallback,); // never call this from RT
  358. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->prog.count),);
  359. const int8_t channel(kIsGIG ? pData->ctrlChannel : int8_t(0));
  360. if (index >= 0 && channel >= 0)
  361. setProgramInternal(static_cast<uint>(index), static_cast<uint8_t>(channel), sendCallback, false);
  362. CarlaPlugin::setProgram(index, sendGui, sendOsc, sendCallback);
  363. }
  364. void setProgramInternal(const uint32_t index, const uint8_t channel, const bool sendCallback, const bool inRtContent) noexcept
  365. {
  366. CARLA_SAFE_ASSERT_RETURN(index < pData->prog.count,);
  367. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  368. if (fCurProgs[channel] == index)
  369. return;
  370. LinuxSampler::EngineChannel* const engineChannel(fEngineChannels[kIsGIG ? channel : 0]);
  371. CARLA_SAFE_ASSERT_RETURN(engineChannel != nullptr,);
  372. const ScopedSingleProcessLocker spl(this, !inRtContent);
  373. if (pData->engine->isOffline())
  374. {
  375. try {
  376. engineChannel->PrepareLoadInstrument(pData->filename, index);
  377. engineChannel->LoadInstrument();
  378. } CARLA_SAFE_EXCEPTION("LoadInstrument");
  379. }
  380. else
  381. {
  382. try {
  383. LinuxSampler::InstrumentManager::LoadInstrumentInBackground(fInstrumentIds[index], engineChannel);
  384. } CARLA_SAFE_EXCEPTION("LoadInstrumentInBackground");
  385. }
  386. fCurProgs[channel] = index;
  387. if (pData->ctrlChannel == channel)
  388. {
  389. const int32_t iindex(static_cast<int32_t>(index));
  390. pData->prog.current = iindex;
  391. if (inRtContent)
  392. pData->postponeRtEvent(kPluginPostRtEventProgramChange, iindex, 0, 0.0f);
  393. else if (sendCallback)
  394. pData->engine->callback(ENGINE_CALLBACK_PROGRAM_CHANGED, pData->id, iindex, 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, i);
  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, 0);
  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, 1);
  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, 0);
  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 (kMaxChannels > 1 && fInstrumentIds.size() > 1)
  524. pData->hints |= PLUGIN_USES_MULTI_PROGS;
  525. if (! kUses16Outs)
  526. pData->extraHints |= PLUGIN_EXTRA_HINT_CAN_RUN_RACK;
  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() && pData->id < pData->engine->getCurrentPluginCount())
  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.begin2(); 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. uint32_t startTime = 0;
  650. uint32_t timeOffset = 0;
  651. for (uint32_t i=0, numEvents=pData->event.portIn->getEventCount(); i < numEvents; ++i)
  652. {
  653. const EngineEvent& event(pData->event.portIn->getEvent(i));
  654. CARLA_SAFE_ASSERT_CONTINUE(event.time < frames);
  655. CARLA_SAFE_ASSERT_BREAK(event.time >= timeOffset);
  656. if (event.time > timeOffset)
  657. {
  658. if (processSingle(audioOut, event.time - timeOffset, timeOffset))
  659. {
  660. startTime = 0;
  661. timeOffset = event.time;
  662. }
  663. else
  664. startTime += timeOffset;
  665. }
  666. // Control change
  667. switch (event.type)
  668. {
  669. case kEngineEventTypeNull:
  670. break;
  671. case kEngineEventTypeControl:
  672. {
  673. const EngineControlEvent& ctrlEvent = event.ctrl;
  674. switch (ctrlEvent.type)
  675. {
  676. case kEngineControlEventTypeNull:
  677. break;
  678. case kEngineControlEventTypeParameter:
  679. {
  680. #ifndef BUILD_BRIDGE
  681. // Control backend stuff
  682. if (event.channel == pData->ctrlChannel)
  683. {
  684. float value;
  685. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_DRYWET) != 0)
  686. {
  687. value = ctrlEvent.value;
  688. setDryWet(value, false, false);
  689. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, value);
  690. }
  691. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_VOLUME) != 0)
  692. {
  693. value = ctrlEvent.value*127.0f/100.0f;
  694. setVolume(value, false, false);
  695. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
  696. }
  697. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_BALANCE) != 0)
  698. {
  699. float left, right;
  700. value = ctrlEvent.value/0.5f - 1.0f;
  701. if (value < 0.0f)
  702. {
  703. left = -1.0f;
  704. right = (value*2.0f)+1.0f;
  705. }
  706. else if (value > 0.0f)
  707. {
  708. left = (value*2.0f)-1.0f;
  709. right = 1.0f;
  710. }
  711. else
  712. {
  713. left = -1.0f;
  714. right = 1.0f;
  715. }
  716. setBalanceLeft(left, false, false);
  717. setBalanceRight(right, false, false);
  718. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  719. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  720. }
  721. }
  722. #endif
  723. // Control plugin parameters
  724. for (uint32_t k=0; k < pData->param.count; ++k)
  725. {
  726. if (pData->param.data[k].midiChannel != event.channel)
  727. continue;
  728. if (pData->param.data[k].midiCC != ctrlEvent.param)
  729. continue;
  730. if (pData->param.data[k].hints != PARAMETER_INPUT)
  731. continue;
  732. if ((pData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  733. continue;
  734. float value;
  735. if (pData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  736. {
  737. value = (ctrlEvent.value < 0.5f) ? pData->param.ranges[k].min : pData->param.ranges[k].max;
  738. }
  739. else
  740. {
  741. value = pData->param.ranges[k].getUnnormalizedValue(ctrlEvent.value);
  742. if (pData->param.data[k].hints & PARAMETER_IS_INTEGER)
  743. value = std::rint(value);
  744. }
  745. setParameterValue(k, value, false, false, false);
  746. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, value);
  747. }
  748. if ((pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0 && ctrlEvent.param < MAX_MIDI_CONTROL)
  749. {
  750. fMidiInputPort->DispatchControlChange(uint8_t(ctrlEvent.param), uint8_t(ctrlEvent.value*127.0f), event.channel, static_cast<int32_t>(startTime));
  751. }
  752. break;
  753. }
  754. case kEngineControlEventTypeMidiBank:
  755. break;
  756. case kEngineControlEventTypeMidiProgram:
  757. if (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES)
  758. {
  759. setProgramInternal(ctrlEvent.param, event.channel, false, true);
  760. }
  761. break;
  762. case kEngineControlEventTypeAllSoundOff:
  763. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  764. {
  765. fMidiInputPort->DispatchControlChange(MIDI_CONTROL_ALL_SOUND_OFF, 0, event.channel, static_cast<int32_t>(startTime));
  766. }
  767. break;
  768. case kEngineControlEventTypeAllNotesOff:
  769. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  770. {
  771. #ifndef BUILD_BRIDGE
  772. if (event.channel == pData->ctrlChannel && ! allNotesOffSent)
  773. {
  774. allNotesOffSent = true;
  775. sendMidiAllNotesOffToCallback();
  776. }
  777. #endif
  778. fMidiInputPort->DispatchControlChange(MIDI_CONTROL_ALL_NOTES_OFF, 0, event.channel, static_cast<int32_t>(startTime));
  779. }
  780. break;
  781. }
  782. break;
  783. }
  784. case kEngineEventTypeMidi: {
  785. const EngineMidiEvent& midiEvent(event.midi);
  786. const uint8_t* const midiData(midiEvent.size > EngineMidiEvent::kDataSize ? midiEvent.dataExt : midiEvent.data);
  787. uint8_t status = uint8_t(MIDI_GET_STATUS_FROM_DATA(midiData));
  788. if (status == MIDI_STATUS_CHANNEL_PRESSURE && (pData->options & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) == 0)
  789. continue;
  790. if (status == MIDI_STATUS_CONTROL_CHANGE && (pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) == 0)
  791. continue;
  792. if (status == MIDI_STATUS_POLYPHONIC_AFTERTOUCH && (pData->options & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) == 0)
  793. continue;
  794. if (status == MIDI_STATUS_PITCH_WHEEL_CONTROL && (pData->options & PLUGIN_OPTION_SEND_PITCHBEND) == 0)
  795. continue;
  796. // Fix bad note-off
  797. if (status == MIDI_STATUS_NOTE_ON && midiData[2] == 0)
  798. status = MIDI_STATUS_NOTE_OFF;
  799. // put back channel in data
  800. uint8_t midiData2[midiEvent.size];
  801. midiData2[0] = uint8_t(status | (event.channel & MIDI_CHANNEL_BIT));
  802. std::memcpy(midiData2+1, midiData+1, static_cast<std::size_t>(midiEvent.size-1));
  803. fMidiInputPort->DispatchRaw(midiData2, static_cast<int32_t>(startTime));
  804. if (status == MIDI_STATUS_NOTE_ON)
  805. pData->postponeRtEvent(kPluginPostRtEventNoteOn, event.channel, midiData[1], midiData[2]);
  806. else if (status == MIDI_STATUS_NOTE_OFF)
  807. pData->postponeRtEvent(kPluginPostRtEventNoteOff, event.channel, midiData[1], 0.0f);
  808. } break;
  809. }
  810. }
  811. pData->postRtEvents.trySplice();
  812. if (frames > timeOffset)
  813. processSingle(audioOut, frames - timeOffset, timeOffset);
  814. } // End of Event Input and Processing
  815. // --------------------------------------------------------------------------------------------------------
  816. // Parameter outputs
  817. uint diskStreamCount=0, voiceCount=0;
  818. for (uint i=0; i<kMaxChannels; ++i)
  819. {
  820. LinuxSampler::EngineChannel* const engineChannel(fEngineChannels[i]);
  821. CARLA_SAFE_ASSERT_CONTINUE(engineChannel != nullptr);
  822. diskStreamCount += engineChannel->GetDiskStreamCount();
  823. /**/ voiceCount += engineChannel->GetVoiceCount();
  824. }
  825. fParamBuffers[LinuxSamplerDiskStreamCount] = static_cast<float>(diskStreamCount);
  826. fParamBuffers[LinuxSamplerVoiceCount] = static_cast<float>(voiceCount);
  827. }
  828. bool processSingle(float** const outBuffer, const uint32_t frames, const uint32_t timeOffset)
  829. {
  830. CARLA_SAFE_ASSERT_RETURN(outBuffer != nullptr, false);
  831. CARLA_SAFE_ASSERT_RETURN(frames > 0, false);
  832. // --------------------------------------------------------------------------------------------------------
  833. // Try lock, silence otherwise
  834. if (pData->engine->isOffline())
  835. {
  836. pData->singleMutex.lock();
  837. }
  838. else if (! pData->singleMutex.tryLock())
  839. {
  840. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  841. {
  842. for (uint32_t k=0; k < frames; ++k)
  843. outBuffer[i][k+timeOffset] = 0.0f;
  844. }
  845. return false;
  846. }
  847. // --------------------------------------------------------------------------------------------------------
  848. // Run plugin
  849. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  850. {
  851. if (LinuxSampler::AudioChannel* const outDev = fAudioOutputDevice.Channel(i))
  852. outDev->SetBuffer(outBuffer[i] + timeOffset);
  853. }
  854. fAudioOutputDevice.Render(frames);
  855. #ifndef BUILD_BRIDGE
  856. // --------------------------------------------------------------------------------------------------------
  857. // Post-processing (dry/wet, volume and balance)
  858. {
  859. const bool doVolume = (pData->hints & PLUGIN_CAN_VOLUME) != 0 && carla_isNotEqual(pData->postProc.volume, 1.0f);
  860. const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) != 0 && ! (carla_isEqual(pData->postProc.balanceLeft, -1.0f) && carla_isEqual(pData->postProc.balanceRight, 1.0f));
  861. float oldBufLeft[doBalance ? frames : 1];
  862. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  863. {
  864. // Balance
  865. if (doBalance)
  866. {
  867. if (i % 2 == 0)
  868. FloatVectorOperations::copy(oldBufLeft, outBuffer[i], static_cast<int>(frames));
  869. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  870. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  871. for (uint32_t k=0; k < frames; ++k)
  872. {
  873. if (i % 2 == 0)
  874. {
  875. // left
  876. outBuffer[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  877. outBuffer[i][k] += outBuffer[i+1][k] * (1.0f - balRangeR);
  878. }
  879. else
  880. {
  881. // right
  882. outBuffer[i][k] = outBuffer[i][k] * balRangeR;
  883. outBuffer[i][k] += oldBufLeft[k] * balRangeL;
  884. }
  885. }
  886. }
  887. // Volume
  888. if (doVolume)
  889. {
  890. for (uint32_t k=0; k < frames; ++k)
  891. outBuffer[i][k+timeOffset] *= pData->postProc.volume;
  892. }
  893. }
  894. } // End of Post-processing
  895. #endif
  896. // --------------------------------------------------------------------------------------------------------
  897. pData->singleMutex.unlock();
  898. return true;
  899. }
  900. #ifndef CARLA_OS_WIN // FIXME, need to update linuxsampler win32 build
  901. void bufferSizeChanged(const uint32_t) override
  902. {
  903. fAudioOutputDevice.ReconnectAll();
  904. }
  905. void sampleRateChanged(const double) override
  906. {
  907. fAudioOutputDevice.ReconnectAll();
  908. }
  909. #endif
  910. // -------------------------------------------------------------------
  911. // Plugin buffers
  912. // nothing
  913. // -------------------------------------------------------------------
  914. const void* getExtraStuff() const noexcept override
  915. {
  916. static const char xtrue[] = "true";
  917. static const char xfalse[] = "false";
  918. return kUses16Outs ? xtrue : xfalse;
  919. }
  920. bool init(const char* const filename, const char* const name, const char* const label, const uint options)
  921. {
  922. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  923. // ---------------------------------------------------------------
  924. // first checks
  925. if (pData->client != nullptr)
  926. {
  927. pData->engine->setLastError("Plugin client is already registered");
  928. return false;
  929. }
  930. if (filename == nullptr || filename[0] == '\0')
  931. {
  932. pData->engine->setLastError("null filename");
  933. return false;
  934. }
  935. // ---------------------------------------------------------------
  936. // Init LinuxSampler stuff
  937. fMidiInputPort = fMidiInputDevice.GetPort(0);
  938. if (fMidiInputPort == nullptr)
  939. {
  940. pData->engine->setLastError("Failed to get LinuxSampler midi input port");
  941. return false;
  942. }
  943. for (uint i=0; i<kMaxChannels; ++i)
  944. {
  945. LinuxSampler::SamplerChannel* const samplerChannel(sSampler->AddSamplerChannel());
  946. CARLA_SAFE_ASSERT_CONTINUE(samplerChannel != nullptr);
  947. samplerChannel->SetEngineType(kIsGIG ? "GIG" : "SFZ");
  948. samplerChannel->SetAudioOutputDevice(&fAudioOutputDevice);
  949. samplerChannel->SetMidiInputChannel(kIsGIG ? static_cast<LinuxSampler::midi_chan_t>(i) : LinuxSampler::midi_chan_all);
  950. LinuxSampler::EngineChannel* const engineChannel(samplerChannel->GetEngineChannel());
  951. CARLA_SAFE_ASSERT_CONTINUE(engineChannel != nullptr);
  952. engineChannel->Pan(0.0f);
  953. engineChannel->Volume(kIsGIG ? LinuxSampler::kVolumeMax/10.0f : LinuxSampler::kVolumeMax); // FIXME
  954. engineChannel->SetMidiInstrumentMapToDefault();
  955. engineChannel->Connect(fMidiInputPort);
  956. if (kUses16Outs)
  957. {
  958. engineChannel->SetOutputChannel(0, i*2);
  959. engineChannel->SetOutputChannel(1, i*2 +1);
  960. }
  961. else
  962. {
  963. engineChannel->SetOutputChannel(0, 0);
  964. engineChannel->SetOutputChannel(1, 1);
  965. }
  966. fSamplerChannels[i] = samplerChannel;
  967. fEngineChannels[i] = engineChannel;
  968. }
  969. if (fSamplerChannels[0] == nullptr || fEngineChannels[0] == nullptr)
  970. {
  971. pData->engine->setLastError("Failed to create LinuxSampler audio channels");
  972. return false;
  973. }
  974. // ---------------------------------------------------------------
  975. // Get Engine
  976. LinuxSampler::Engine* const engine(fEngineChannels[0]->GetEngine());
  977. if (engine == nullptr)
  978. {
  979. pData->engine->setLastError("Failed to get LinuxSampler engine via channel");
  980. return false;
  981. }
  982. // ---------------------------------------------------------------
  983. // Get the Engine's Instrument Manager
  984. LinuxSampler::InstrumentManager* const instrumentMgr(engine->GetInstrumentManager());
  985. if (instrumentMgr == nullptr)
  986. {
  987. pData->engine->setLastError("Failed to get LinuxSampler instrument manager");
  988. return false;
  989. }
  990. // ---------------------------------------------------------------
  991. // Load the Instrument via filename
  992. try {
  993. fInstrumentIds = instrumentMgr->GetInstrumentFileContent(filename);
  994. }
  995. catch (const LinuxSampler::InstrumentManagerException& e)
  996. {
  997. pData->engine->setLastError(e.what());
  998. return false;
  999. }
  1000. // ---------------------------------------------------------------
  1001. // Get info
  1002. const std::size_t numInstruments(fInstrumentIds.size());
  1003. if (numInstruments == 0)
  1004. {
  1005. pData->engine->setLastError("Failed to find any instruments");
  1006. return false;
  1007. }
  1008. for (std::size_t i=0; i<numInstruments; ++i)
  1009. {
  1010. try {
  1011. fInstrumentInfo.push_back(instrumentMgr->GetInstrumentInfo(fInstrumentIds[i]));
  1012. } CARLA_SAFE_EXCEPTION("GetInstrumentInfo");
  1013. instrumentMgr->SetMode(fInstrumentIds[i], LinuxSampler::InstrumentManager::ON_DEMAND);
  1014. }
  1015. CARLA_SAFE_ASSERT(fInstrumentInfo.size() == numInstruments);
  1016. // ---------------------------------------------------------------
  1017. CarlaString label2(label != nullptr ? label : File(filename).getFileNameWithoutExtension().toRawUTF8());
  1018. if (kUses16Outs && ! label2.endsWith(" (16 outs)"))
  1019. label2 += " (16 outs)";
  1020. fLabel = label2.dup();
  1021. fMaker = carla_strdup(fInstrumentInfo[0].Artists.c_str());
  1022. fRealName = carla_strdup(instrumentMgr->GetInstrumentName(fInstrumentIds[0]).c_str());
  1023. pData->filename = carla_strdup(filename);
  1024. if (name != nullptr && name[0] != '\0')
  1025. pData->name = pData->engine->getUniquePluginName(name);
  1026. else if (fRealName[0] != '\0')
  1027. pData->name = pData->engine->getUniquePluginName(fRealName);
  1028. else
  1029. pData->name = pData->engine->getUniquePluginName(fLabel);
  1030. // ---------------------------------------------------------------
  1031. // register client
  1032. pData->client = pData->engine->addClient(this);
  1033. if (pData->client == nullptr || ! pData->client->isOk())
  1034. {
  1035. pData->engine->setLastError("Failed to register plugin client");
  1036. return false;
  1037. }
  1038. // ---------------------------------------------------------------
  1039. // set default options
  1040. pData->options = 0x0;
  1041. pData->options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  1042. pData->options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  1043. pData->options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  1044. pData->options |= PLUGIN_OPTION_SEND_PITCHBEND;
  1045. pData->options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  1046. if (kIsGIG)
  1047. pData->options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  1048. // TODO: read some options
  1049. return true;
  1050. (void)options;
  1051. }
  1052. // -------------------------------------------------------------------
  1053. private:
  1054. enum LinuxSamplerParameters {
  1055. LinuxSamplerDiskStreamCount = 0,
  1056. LinuxSamplerVoiceCount = 1,
  1057. LinuxSamplerParametersMax = 2
  1058. };
  1059. const bool kIsGIG; // SFZ if false
  1060. const bool kUses16Outs;
  1061. const uint kMaxChannels; // 16 for gig, 1 for sfz
  1062. const char* fLabel;
  1063. const char* fMaker;
  1064. const char* fRealName;
  1065. uint32_t fCurProgs[MAX_MIDI_CHANNELS];
  1066. float fParamBuffers[LinuxSamplerParametersMax];
  1067. SharedResourcePointer<LinuxSampler::Sampler> sSampler;
  1068. LinuxSampler::AudioOutputDevicePlugin fAudioOutputDevice;
  1069. LinuxSampler::MidiInputDevicePlugin fMidiInputDevice;
  1070. LinuxSampler::MidiInputPort* fMidiInputPort;
  1071. LinuxSampler::SamplerChannel* fSamplerChannels[MAX_MIDI_CHANNELS];
  1072. LinuxSampler::EngineChannel* fEngineChannels[MAX_MIDI_CHANNELS];
  1073. std::vector<LinuxSampler::InstrumentManager::instrument_id_t> fInstrumentIds;
  1074. std::vector<LinuxSampler::InstrumentManager::instrument_info_t> fInstrumentInfo;
  1075. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaPluginLinuxSampler)
  1076. };
  1077. CARLA_BACKEND_END_NAMESPACE
  1078. #endif // HAVE_LINUXSAMPLER
  1079. CARLA_BACKEND_START_NAMESPACE
  1080. // -------------------------------------------------------------------------------------------------------------------
  1081. CarlaPlugin* CarlaPlugin::newLinuxSampler(const Initializer& init, const char* const format, const bool use16Outs)
  1082. {
  1083. 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));
  1084. #ifdef HAVE_LINUXSAMPLER
  1085. CarlaString sformat(format);
  1086. sformat.toLower();
  1087. if (sformat != "gig" && sformat != "sfz")
  1088. {
  1089. init.engine->setLastError("Unsupported format requested for LinuxSampler");
  1090. return nullptr;
  1091. }
  1092. if (init.engine->getProccessMode() == ENGINE_PROCESS_MODE_CONTINUOUS_RACK && use16Outs)
  1093. {
  1094. init.engine->setLastError("Carla's rack mode can only work with Stereo modules, please choose the 2-channel only sample-library version");
  1095. return nullptr;
  1096. }
  1097. // -------------------------------------------------------------------
  1098. // Check if file exists
  1099. if (! File(init.filename).existsAsFile())
  1100. {
  1101. init.engine->setLastError("Requested file is not valid or does not exist");
  1102. return nullptr;
  1103. }
  1104. CarlaPluginLinuxSampler* const plugin(new CarlaPluginLinuxSampler(init.engine, init.id, (sformat == "gig"), use16Outs));
  1105. if (! plugin->init(init.filename, init.name, init.label, init.options))
  1106. {
  1107. delete plugin;
  1108. return nullptr;
  1109. }
  1110. return plugin;
  1111. #else
  1112. init.engine->setLastError("linuxsampler support not available");
  1113. return nullptr;
  1114. // unused
  1115. (void)format;
  1116. (void)use16Outs;
  1117. #endif
  1118. }
  1119. CarlaPlugin* CarlaPlugin::newFileGIG(const Initializer& init, const bool use16Outs)
  1120. {
  1121. carla_debug("CarlaPlugin::newFileGIG({%p, \"%s\", \"%s\", \"%s\"}, %s)", init.engine, init.filename, init.name, init.label, bool2str(use16Outs));
  1122. #ifdef HAVE_LINUXSAMPLER
  1123. return newLinuxSampler(init, "GIG", use16Outs);
  1124. #else
  1125. init.engine->setLastError("GIG support not available");
  1126. return nullptr;
  1127. // unused
  1128. (void)use16Outs;
  1129. #endif
  1130. }
  1131. CarlaPlugin* CarlaPlugin::newFileSFZ(const Initializer& init)
  1132. {
  1133. carla_debug("CarlaPlugin::newFileSFZ({%p, \"%s\", \"%s\", \"%s\"})", init.engine, init.filename, init.name, init.label);
  1134. #ifdef HAVE_LINUXSAMPLER
  1135. return newLinuxSampler(init, "SFZ", false);
  1136. #else
  1137. init.engine->setLastError("SFZ support not available");
  1138. return nullptr;
  1139. #endif
  1140. }
  1141. // -------------------------------------------------------------------------------------------------------------------
  1142. CARLA_BACKEND_END_NAMESPACE