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.

CarlaPluginLinuxSampler.cpp 51KB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
9 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
9 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468
  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. {
  52. CARLA_ASSERT(engine != nullptr);
  53. CARLA_ASSERT(plugin != nullptr);
  54. AcquireChannels(uses16Outs ? 32 : 2);
  55. }
  56. ~AudioOutputDevicePlugin() override {}
  57. // -------------------------------------------------------------------
  58. // LinuxSampler virtual methods
  59. void Play() override {}
  60. void Stop() override {}
  61. bool IsPlaying() override
  62. {
  63. return (kEngine->isRunning() && kPlugin->isEnabled());
  64. }
  65. uint MaxSamplesPerCycle() override
  66. {
  67. return kEngine->getBufferSize();
  68. }
  69. uint SampleRate() override
  70. {
  71. return uint(kEngine->getSampleRate());
  72. }
  73. std::string Driver() override
  74. {
  75. return "AudioOutputDevicePlugin";
  76. }
  77. AudioChannel* CreateChannel(uint channelNr) override
  78. {
  79. return new AudioChannel(channelNr, nullptr, 0);
  80. }
  81. // -------------------------------------------------------------------
  82. /* */ bool isAutonomousDevice() override { return false; }
  83. static bool isAutonomousDriver() { return false; }
  84. // -------------------------------------------------------------------
  85. // Give public access to the RenderAudio call
  86. int Render(const uint samples)
  87. {
  88. return RenderAudio(samples);
  89. }
  90. // -------------------------------------------------------------------
  91. private:
  92. const CarlaEngine* const kEngine;
  93. const CarlaPlugin* const kPlugin;
  94. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(AudioOutputDevicePlugin)
  95. };
  96. // -----------------------------------------------------------------------
  97. // LinuxSampler MidiInputPort Plugin
  98. class MidiInputPortPlugin : public MidiInputPort
  99. {
  100. public:
  101. MidiInputPortPlugin(MidiInputDevice* const device, const int portNum = 0)
  102. : MidiInputPort(device, portNum) {}
  103. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(MidiInputPortPlugin)
  104. };
  105. // -----------------------------------------------------------------------
  106. // LinuxSampler MidiInputDevice Plugin
  107. class MidiInputDevicePlugin : public MidiInputDevice
  108. {
  109. public:
  110. MidiInputDevicePlugin(Sampler* const sampler)
  111. : MidiInputDevice(std::map<std::string, DeviceCreationParameter*>(), sampler)
  112. {
  113. AcquirePorts(1);
  114. }
  115. ~MidiInputDevicePlugin() override
  116. {
  117. for (std::map<int,MidiInputPort*>::iterator it = Ports.begin(), end = Ports.end(); it != end; ++it)
  118. delete dynamic_cast<MidiInputPortPlugin*>(it->second);
  119. Ports.clear();
  120. }
  121. // -------------------------------------------------------------------
  122. // LinuxSampler virtual methods
  123. void Listen() override {}
  124. void StopListen() override {}
  125. std::string Driver() override
  126. {
  127. return "MidiInputDevicePlugin";
  128. }
  129. MidiInputPort* CreateMidiPort() override
  130. {
  131. return new MidiInputPortPlugin(this, int(PortCount()));
  132. }
  133. // -------------------------------------------------------------------
  134. /* */ bool isAutonomousDevice() override { return false; }
  135. static bool isAutonomousDriver() { return false; }
  136. // -------------------------------------------------------------------
  137. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(MidiInputDevicePlugin)
  138. };
  139. } // namespace LinuxSampler
  140. // -----------------------------------------------------------------------
  141. using juce::File;
  142. using juce::SharedResourcePointer;
  143. using juce::StringArray;
  144. CARLA_BACKEND_START_NAMESPACE
  145. // -----------------------------------------------------------------------
  146. class CarlaPluginLinuxSampler : public CarlaPlugin
  147. {
  148. public:
  149. CarlaPluginLinuxSampler(CarlaEngine* const engine, const uint id, const bool isGIG, const bool use16Outs)
  150. : CarlaPlugin(engine, id),
  151. kIsGIG(isGIG),
  152. kUses16Outs(use16Outs && isGIG),
  153. kMaxChannels(isGIG ? MAX_MIDI_CHANNELS : 1),
  154. fLabel(nullptr),
  155. fMaker(nullptr),
  156. fRealName(nullptr),
  157. sSampler(),
  158. fAudioOutputDevice(engine, this, kUses16Outs),
  159. fMidiInputDevice(sSampler),
  160. fMidiInputPort(nullptr),
  161. fInstrumentIds(),
  162. fInstrumentInfo()
  163. {
  164. carla_debug("CarlaPluginLinuxSampler::CarlaPluginLinuxSampler(%p, %i, %s, %s)", engine, id, bool2str(isGIG), bool2str(use16Outs));
  165. // TODO - option for this
  166. sSampler->SetGlobalMaxStreams(LinuxSampler::kMaxStreams);
  167. sSampler->SetGlobalMaxVoices(LinuxSampler::kMaxVoices);
  168. carla_zeroStructs(fCurProgs, MAX_MIDI_CHANNELS);
  169. carla_zeroStructs(fSamplerChannels, MAX_MIDI_CHANNELS);
  170. carla_zeroStructs(fEngineChannels, MAX_MIDI_CHANNELS);
  171. carla_zeroFloats(fParamBuffers, LinuxSamplerParametersMax);
  172. if (use16Outs && ! isGIG)
  173. carla_stderr("Tried to use SFZ with 16 stereo outs, this doesn't make much sense so single stereo mode will be used instead");
  174. }
  175. ~CarlaPluginLinuxSampler() override
  176. {
  177. carla_debug("CarlaPluginLinuxSampler::~CarlaPluginLinuxSampler()");
  178. pData->singleMutex.lock();
  179. pData->masterMutex.lock();
  180. if (pData->client != nullptr && pData->client->isActive())
  181. pData->client->deactivate();
  182. if (pData->active)
  183. {
  184. deactivate();
  185. pData->active = false;
  186. }
  187. fMidiInputPort = nullptr;
  188. for (uint i=0; i<kMaxChannels; ++i)
  189. {
  190. if (fSamplerChannels[i] != nullptr)
  191. {
  192. if (fEngineChannels[i] != nullptr)
  193. {
  194. fEngineChannels[i]->DisconnectAudioOutputDevice();
  195. fEngineChannels[i]->DisconnectAllMidiInputPorts();
  196. fEngineChannels[i] = nullptr;
  197. }
  198. sSampler->RemoveSamplerChannel(fSamplerChannels[i]);
  199. fSamplerChannels[i] = nullptr;
  200. }
  201. }
  202. fInstrumentIds.clear();
  203. fInstrumentInfo.clear();
  204. if (fLabel != nullptr)
  205. {
  206. delete[] fLabel;
  207. fLabel = nullptr;
  208. }
  209. if (fMaker != nullptr)
  210. {
  211. delete[] fMaker;
  212. fMaker = nullptr;
  213. }
  214. if (fRealName != nullptr)
  215. {
  216. delete[] fRealName;
  217. fRealName = nullptr;
  218. }
  219. clearBuffers();
  220. }
  221. // -------------------------------------------------------------------
  222. // Information (base)
  223. PluginType getType() const noexcept override
  224. {
  225. return kIsGIG ? PLUGIN_GIG : PLUGIN_SFZ;
  226. }
  227. PluginCategory getCategory() const noexcept override
  228. {
  229. return PLUGIN_CATEGORY_SYNTH;
  230. }
  231. // -------------------------------------------------------------------
  232. // Information (count)
  233. // nothing
  234. // -------------------------------------------------------------------
  235. // Information (current data)
  236. // nothing
  237. // -------------------------------------------------------------------
  238. // Information (per-plugin data)
  239. uint getOptionsAvailable() const noexcept override
  240. {
  241. uint options = 0x0;
  242. options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  243. options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  244. options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  245. options |= PLUGIN_OPTION_SEND_PITCHBEND;
  246. options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  247. if (kIsGIG)
  248. options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  249. return options;
  250. }
  251. float getParameterValue(const uint32_t parameterId) const noexcept override
  252. {
  253. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0.0f);
  254. return fParamBuffers[parameterId];
  255. }
  256. void getLabel(char* const strBuf) const noexcept override
  257. {
  258. if (fLabel != nullptr)
  259. {
  260. std::strncpy(strBuf, fLabel, STR_MAX);
  261. return;
  262. }
  263. CarlaPlugin::getLabel(strBuf);
  264. }
  265. void getMaker(char* const strBuf) const noexcept override
  266. {
  267. if (fMaker != nullptr)
  268. {
  269. std::strncpy(strBuf, fMaker, STR_MAX);
  270. return;
  271. }
  272. CarlaPlugin::getMaker(strBuf);
  273. }
  274. void getCopyright(char* const strBuf) const noexcept override
  275. {
  276. getMaker(strBuf);
  277. }
  278. void getRealName(char* const strBuf) const noexcept override
  279. {
  280. if (fRealName != nullptr)
  281. {
  282. std::strncpy(strBuf, fRealName, STR_MAX);
  283. return;
  284. }
  285. CarlaPlugin::getRealName(strBuf);
  286. }
  287. void getParameterName(const uint32_t parameterId, char* const strBuf) const noexcept override
  288. {
  289. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  290. switch (parameterId)
  291. {
  292. case LinuxSamplerDiskStreamCount:
  293. std::strncpy(strBuf, "Disk Stream Count", STR_MAX);
  294. return;
  295. case LinuxSamplerVoiceCount:
  296. std::strncpy(strBuf, "Voice Count", STR_MAX);
  297. return;
  298. }
  299. CarlaPlugin::getParameterName(parameterId, strBuf);
  300. }
  301. // -------------------------------------------------------------------
  302. // Set data (state)
  303. void prepareForSave() override
  304. {
  305. if (kMaxChannels > 1 && fInstrumentIds.size() > 1)
  306. {
  307. char strBuf[STR_MAX+1];
  308. 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],
  309. fCurProgs[4], fCurProgs[5], fCurProgs[6], fCurProgs[7],
  310. fCurProgs[8], fCurProgs[9], fCurProgs[10], fCurProgs[11],
  311. fCurProgs[12], fCurProgs[13], fCurProgs[14], fCurProgs[15]);
  312. CarlaPlugin::setCustomData(CUSTOM_DATA_TYPE_STRING, "programs", strBuf, false);
  313. }
  314. }
  315. // -------------------------------------------------------------------
  316. // Set data (internal stuff)
  317. void setCtrlChannel(const int8_t channel, const bool sendOsc, const bool sendCallback) noexcept override
  318. {
  319. if (channel >= 0 && channel < MAX_MIDI_CHANNELS)
  320. pData->prog.current = static_cast<int32_t>(fCurProgs[channel]);
  321. CarlaPlugin::setCtrlChannel(channel, sendOsc, sendCallback);
  322. }
  323. // -------------------------------------------------------------------
  324. // Set data (plugin-specific stuff)
  325. void setCustomData(const char* const type, const char* const key, const char* const value, const bool sendGui) override
  326. {
  327. CARLA_SAFE_ASSERT_RETURN(type != nullptr && type[0] != '\0',);
  328. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  329. CARLA_SAFE_ASSERT_RETURN(value != nullptr && value[0] != '\0',);
  330. carla_debug("CarlaPluginLinuxSampler::setCustomData(%s, \"%s\", \"%s\", %s)", type, key, value, bool2str(sendGui));
  331. if (std::strcmp(type, CUSTOM_DATA_TYPE_PROPERTY) == 0)
  332. return CarlaPlugin::setCustomData(type, key, value, 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, i);
  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, 0);
  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, 1);
  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, 0);
  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 (kMaxChannels > 1 && fInstrumentIds.size() > 1)
  525. pData->hints |= PLUGIN_USES_MULTI_PROGS;
  526. if (! kUses16Outs)
  527. pData->extraHints |= PLUGIN_EXTRA_HINT_CAN_RUN_RACK;
  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() && pData->id < pData->engine->getCurrentPluginCount())
  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.begin2(); 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_isNotEqual(pData->postProc.volume, 1.0f);
  862. const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) != 0 && ! (carla_isEqual(pData->postProc.balanceLeft, -1.0f) && carla_isEqual(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_SEND_CONTROL_CHANGES;
  1044. pData->options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  1045. pData->options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  1046. pData->options |= PLUGIN_OPTION_SEND_PITCHBEND;
  1047. pData->options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  1048. if (kIsGIG)
  1049. pData->options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  1050. return true;
  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))
  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