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.

LinuxSamplerPlugin.cpp 48KB

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