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
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
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
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
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
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
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
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426
  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 WANT_LINUXSAMPLER
  26. #include "CarlaBackendUtils.hpp"
  27. #include "CarlaMathUtils.hpp"
  28. #include "linuxsampler/EngineFactory.h"
  29. #include <linuxsampler/Sampler.h>
  30. #include <QtCore/QFileInfo>
  31. #include <QtCore/QStringList>
  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<String, DeviceCreationParameter*>()),
  45. fEngine(engine),
  46. fPlugin(plugin)
  47. {
  48. CARLA_ASSERT(engine != nullptr);
  49. CARLA_ASSERT(plugin != nullptr);
  50. AcquireChannels(uses16Outs ? 32 : 2);
  51. }
  52. ~AudioOutputDevicePlugin() override {}
  53. // -------------------------------------------------------------------
  54. // LinuxSampler virtual methods
  55. void Play() override
  56. {
  57. }
  58. bool IsPlaying() override
  59. {
  60. return (fEngine->isRunning() && fPlugin->isEnabled());
  61. }
  62. void Stop() override
  63. {
  64. }
  65. uint MaxSamplesPerCycle() override
  66. {
  67. return fEngine->getBufferSize();
  68. }
  69. uint SampleRate() override
  70. {
  71. return uint(fEngine->getSampleRate());
  72. }
  73. 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. // 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 fEngine;
  90. const CarlaPlugin* const fPlugin;
  91. };
  92. // -----------------------------------------------------------------------
  93. // LinuxSampler MidiInputPort Plugin
  94. class MidiInputPortPlugin : public MidiInputPort
  95. {
  96. public:
  97. MidiInputPortPlugin(MidiInputDevice* const device, const int portNum)
  98. : MidiInputPort(device, portNum)
  99. {
  100. }
  101. ~MidiInputPortPlugin() override {}
  102. };
  103. // -----------------------------------------------------------------------
  104. // LinuxSampler MidiInputDevice Plugin
  105. class MidiInputDevicePlugin : public MidiInputDevice
  106. {
  107. public:
  108. MidiInputDevicePlugin(Sampler* const sampler)
  109. : MidiInputDevice(std::map<String, DeviceCreationParameter*>(), sampler)
  110. {
  111. }
  112. // -------------------------------------------------------------------
  113. // LinuxSampler virtual methods
  114. void Listen() override
  115. {
  116. }
  117. void StopListen() override
  118. {
  119. }
  120. String Driver() override
  121. {
  122. return "MidiInputDevicePlugin";
  123. }
  124. MidiInputPort* CreateMidiPort() override
  125. {
  126. return new MidiInputPortPlugin(this, int(Ports.size()));
  127. }
  128. MidiInputPortPlugin* CreateMidiPortPlugin()
  129. {
  130. return new MidiInputPortPlugin(this, int(Ports.size()));
  131. }
  132. };
  133. } // namespace LinuxSampler
  134. // -----------------------------------------------------------------------
  135. CARLA_BACKEND_START_NAMESPACE
  136. #if 0
  137. }
  138. #endif
  139. class LinuxSamplerPlugin : public CarlaPlugin
  140. {
  141. public:
  142. LinuxSamplerPlugin(CarlaEngine* const engine, const uint id, const char* const format, const bool use16Outs)
  143. : CarlaPlugin(engine, id),
  144. fUses16Outs(use16Outs),
  145. fFormat(carla_strdup(format)),
  146. fLabel(nullptr),
  147. fMaker(nullptr),
  148. fRealName(nullptr),
  149. fEngine(nullptr),
  150. fAudioOutputDevice(nullptr),
  151. fMidiInputDevice(nullptr),
  152. fMidiInputPort(nullptr),
  153. fInstrument(nullptr)
  154. {
  155. carla_debug("LinuxSamplerPlugin::LinuxSamplerPlugin(%p, %i, %s, %s)", engine, id, format, bool2str(use16Outs));
  156. for (int i=0; i < MAX_MIDI_CHANNELS; ++i)
  157. {
  158. fCurMidiProgs[0] = 0;
  159. fSamplerChannels[i] = nullptr;
  160. fEngineChannels[i] = nullptr;
  161. }
  162. }
  163. ~LinuxSamplerPlugin() override
  164. {
  165. carla_debug("LinuxSamplerPlugin::~LinuxSamplerPlugin()");
  166. pData->singleMutex.lock();
  167. pData->masterMutex.lock();
  168. if (pData->client != nullptr && pData->client->isActive())
  169. pData->client->deactivate();
  170. if (pData->active)
  171. {
  172. deactivate();
  173. pData->active = false;
  174. }
  175. if (fEngine != nullptr)
  176. {
  177. if (fMidiInputDevice != nullptr)
  178. {
  179. if (fMidiInputPort != nullptr)
  180. {
  181. for (int i=0; i < MAX_MIDI_CHANNELS; ++i)
  182. {
  183. if (fSamplerChannels[i] != nullptr)
  184. {
  185. if (fEngineChannels[i] != nullptr)
  186. {
  187. fMidiInputPort->Disconnect(fEngineChannels[i]);
  188. fEngineChannels[i]->DisconnectAudioOutputDevice();
  189. fEngineChannels[i] = nullptr;
  190. }
  191. fSampler.RemoveSamplerChannel(fSamplerChannels[i]);
  192. fSamplerChannels[i] = nullptr;
  193. }
  194. }
  195. delete fMidiInputPort;
  196. fMidiInputPort = nullptr;
  197. }
  198. delete fMidiInputDevice;
  199. fMidiInputDevice = nullptr;
  200. }
  201. if (fAudioOutputDevice != nullptr)
  202. {
  203. delete fAudioOutputDevice;
  204. fAudioOutputDevice = nullptr;
  205. }
  206. fInstrument = nullptr;
  207. LinuxSampler::EngineFactory::Destroy(fEngine);
  208. fEngine = nullptr;
  209. }
  210. fInstrumentIds.clear();
  211. if (fFormat != nullptr)
  212. {
  213. delete[] fFormat;
  214. fFormat = nullptr;
  215. }
  216. if (fLabel != nullptr)
  217. {
  218. delete[] fLabel;
  219. fLabel = nullptr;
  220. }
  221. if (fMaker != nullptr)
  222. {
  223. delete[] fMaker;
  224. fMaker = nullptr;
  225. }
  226. if (fRealName != nullptr)
  227. {
  228. delete[] fRealName;
  229. fRealName = nullptr;
  230. }
  231. clearBuffers();
  232. }
  233. // -------------------------------------------------------------------
  234. // Information (base)
  235. PluginType getType() const noexcept override
  236. {
  237. return getPluginTypeFromString(fFormat);
  238. }
  239. PluginCategory getCategory() const noexcept override
  240. {
  241. return PLUGIN_CATEGORY_SYNTH;
  242. }
  243. // -------------------------------------------------------------------
  244. // Information (count)
  245. // nothing
  246. // -------------------------------------------------------------------
  247. // Information (current data)
  248. // nothing
  249. // -------------------------------------------------------------------
  250. // Information (per-plugin data)
  251. uint getOptionsAvailable() const noexcept override
  252. {
  253. uint options = 0x0;
  254. options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  255. options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  256. options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  257. options |= PLUGIN_OPTION_SEND_PITCHBEND;
  258. options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  259. return options;
  260. }
  261. void getLabel(char* const strBuf) const noexcept override
  262. {
  263. if (fLabel != nullptr)
  264. {
  265. std::strncpy(strBuf, fLabel, STR_MAX);
  266. return;
  267. }
  268. CarlaPlugin::getLabel(strBuf);
  269. }
  270. void getMaker(char* const strBuf) const noexcept override
  271. {
  272. if (fMaker != nullptr)
  273. {
  274. std::strncpy(strBuf, fMaker, STR_MAX);
  275. return;
  276. }
  277. CarlaPlugin::getMaker(strBuf);
  278. }
  279. void getCopyright(char* const strBuf) const noexcept override
  280. {
  281. getMaker(strBuf);
  282. }
  283. void getRealName(char* const strBuf) const noexcept override
  284. {
  285. if (fRealName != nullptr)
  286. {
  287. std::strncpy(strBuf, fRealName, STR_MAX);
  288. return;
  289. }
  290. CarlaPlugin::getRealName(strBuf);
  291. }
  292. // -------------------------------------------------------------------
  293. // Set data (state)
  294. void prepareForSave() override
  295. {
  296. char strBuf[STR_MAX+1];
  297. std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i", fCurMidiProgs[0], fCurMidiProgs[1], fCurMidiProgs[2], fCurMidiProgs[3],
  298. fCurMidiProgs[4], fCurMidiProgs[5], fCurMidiProgs[6], fCurMidiProgs[7],
  299. fCurMidiProgs[8], fCurMidiProgs[9], fCurMidiProgs[10], fCurMidiProgs[11],
  300. fCurMidiProgs[12], fCurMidiProgs[13], fCurMidiProgs[14], fCurMidiProgs[15]);
  301. CarlaPlugin::setCustomData(CUSTOM_DATA_TYPE_STRING, "midiPrograms", strBuf, false);
  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->midiprog.current = fCurMidiProgs[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, "midiPrograms") != 0)
  322. return carla_stderr2("LinuxSamplerPlugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - type is not string", type, key, value, bool2str(sendGui));
  323. if (fUses16Outs)
  324. {
  325. QStringList midiProgramList(QString(value).split(":", QString::SkipEmptyParts));
  326. if (midiProgramList.count() == MAX_MIDI_CHANNELS)
  327. {
  328. uint i = 0;
  329. foreach (const QString& midiProg, midiProgramList)
  330. {
  331. CARLA_SAFE_ASSERT_BREAK(i < MAX_MIDI_CHANNELS);
  332. bool ok;
  333. int index = midiProg.toInt(&ok);
  334. if (ok && index >= 0 && index < static_cast<int>(pData->midiprog.count))
  335. {
  336. const uint32_t bank = pData->midiprog.data[index].bank;
  337. const uint32_t program = pData->midiprog.data[index].program;
  338. const uint32_t rIndex = bank*128 + program;
  339. /*if (pData->engine->isOffline())
  340. {
  341. fEngineChannels[i]->PrepareLoadInstrument(pData->filename, rIndex);
  342. fEngineChannels[i]->LoadInstrument();
  343. }
  344. else*/
  345. {
  346. fInstrument->LoadInstrumentInBackground(fInstrumentIds[rIndex], fEngineChannels[i]);
  347. }
  348. fCurMidiProgs[i] = index;
  349. if (pData->ctrlChannel == static_cast<int32_t>(i))
  350. {
  351. pData->midiprog.current = index;
  352. pData->engine->callback(ENGINE_CALLBACK_MIDI_PROGRAM_CHANGED, pData->id, index, 0, 0.0f, nullptr);
  353. }
  354. }
  355. ++i;
  356. }
  357. CARLA_SAFE_ASSERT(i == MAX_MIDI_CHANNELS);
  358. }
  359. }
  360. CarlaPlugin::setCustomData(type, key, value, sendGui);
  361. }
  362. void setMidiProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  363. {
  364. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->midiprog.count),);
  365. if (index >= 0 && pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  366. {
  367. const uint32_t bank = pData->midiprog.data[index].bank;
  368. const uint32_t program = pData->midiprog.data[index].program;
  369. const uint32_t rIndex = bank*128 + program;
  370. LinuxSampler::EngineChannel* const engineChannel(fEngineChannels[pData->ctrlChannel]);
  371. const ScopedSingleProcessLocker spl(this, (sendGui || sendOsc || sendCallback));
  372. /*if (pData->engine->isOffline())
  373. {
  374. engineChannel->PrepareLoadInstrument(pData->filename, rIndex);
  375. engineChannel->LoadInstrument();
  376. }
  377. else*/
  378. {
  379. try {
  380. fInstrument->LoadInstrumentInBackground(fInstrumentIds[rIndex], engineChannel);
  381. } catch(...) {}
  382. }
  383. fCurMidiProgs[pData->ctrlChannel] = index;
  384. }
  385. CarlaPlugin::setMidiProgram(index, sendGui, sendOsc, sendCallback);
  386. }
  387. // -------------------------------------------------------------------
  388. // Set ui stuff
  389. // nothing
  390. // -------------------------------------------------------------------
  391. // Plugin state
  392. void reload() override
  393. {
  394. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr,);
  395. CARLA_SAFE_ASSERT_RETURN(fInstrument != nullptr,);
  396. carla_debug("LinuxSamplerPlugin::reload() - start");
  397. const EngineProcessMode processMode(pData->engine->getProccessMode());
  398. // Safely disable plugin for reload
  399. const ScopedDisabler sd(this);
  400. if (pData->active)
  401. deactivate();
  402. clearBuffers();
  403. uint32_t aOuts;
  404. aOuts = fUses16Outs ? 32 : 2;
  405. pData->audioOut.createNew(aOuts);
  406. const uint portNameSize(pData->engine->getMaxPortNameSize());
  407. CarlaString portName;
  408. // ---------------------------------------
  409. // Audio Outputs
  410. if (fUses16Outs)
  411. {
  412. for (uint32_t i=0; i < 32; ++i)
  413. {
  414. portName.clear();
  415. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  416. {
  417. portName = pData->name;
  418. portName += ":";
  419. }
  420. portName += "out-";
  421. if ((i+2)/2 < 9)
  422. portName += "0";
  423. portName += CarlaString((i+2)/2);
  424. if (i % 2 == 0)
  425. portName += "L";
  426. else
  427. portName += "R";
  428. portName.truncate(portNameSize);
  429. pData->audioOut.ports[i].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  430. pData->audioOut.ports[i].rindex = i;
  431. }
  432. }
  433. else
  434. {
  435. // out-left
  436. portName.clear();
  437. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  438. {
  439. portName = pData->name;
  440. portName += ":";
  441. }
  442. portName += "out-left";
  443. portName.truncate(portNameSize);
  444. pData->audioOut.ports[0].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  445. pData->audioOut.ports[0].rindex = 0;
  446. // out-right
  447. portName.clear();
  448. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  449. {
  450. portName = pData->name;
  451. portName += ":";
  452. }
  453. portName += "out-right";
  454. portName.truncate(portNameSize);
  455. pData->audioOut.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  456. pData->audioOut.ports[1].rindex = 1;
  457. }
  458. // ---------------------------------------
  459. // Event Input
  460. {
  461. portName.clear();
  462. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  463. {
  464. portName = pData->name;
  465. portName += ":";
  466. }
  467. portName += "events-in";
  468. portName.truncate(portNameSize);
  469. pData->event.portIn = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true);
  470. }
  471. // ---------------------------------------
  472. // plugin hints
  473. pData->hints = 0x0;
  474. pData->hints |= PLUGIN_IS_SYNTH;
  475. pData->hints |= PLUGIN_CAN_VOLUME;
  476. if (! fUses16Outs)
  477. pData->hints |= PLUGIN_CAN_BALANCE;
  478. // extra plugin hints
  479. pData->extraHints = 0x0;
  480. pData->extraHints |= PLUGIN_EXTRA_HINT_HAS_MIDI_IN;
  481. if (fUses16Outs)
  482. pData->extraHints |= PLUGIN_EXTRA_HINT_USES_MULTI_PROGS;
  483. else
  484. pData->extraHints |= PLUGIN_EXTRA_HINT_CAN_RUN_RACK;
  485. bufferSizeChanged(pData->engine->getBufferSize());
  486. reloadPrograms(true);
  487. if (pData->active)
  488. activate();
  489. carla_debug("LinuxSamplerPlugin::reload() - end");
  490. }
  491. void reloadPrograms(bool doInit) override
  492. {
  493. carla_debug("LinuxSamplerPlugin::reloadPrograms(%s)", bool2str(doInit));
  494. // Delete old programs
  495. pData->midiprog.clear();
  496. // Query new programs
  497. uint32_t count = uint32_t(fInstrumentIds.size());
  498. // sound kits must always have at least 1 midi-program
  499. CARLA_SAFE_ASSERT_RETURN(count > 0,);
  500. pData->midiprog.createNew(count);
  501. // Update data
  502. LinuxSampler::InstrumentManager::instrument_info_t info;
  503. for (uint32_t i=0; i < pData->midiprog.count; ++i)
  504. {
  505. pData->midiprog.data[i].bank = i / 128;
  506. pData->midiprog.data[i].program = i % 128;
  507. try {
  508. info = fInstrument->GetInstrumentInfo(fInstrumentIds[i]);
  509. }
  510. catch (const LinuxSampler::InstrumentManagerException&)
  511. {
  512. continue;
  513. }
  514. pData->midiprog.data[i].name = carla_strdup(info.InstrumentName.c_str());
  515. }
  516. #ifndef BUILD_BRIDGE
  517. // Update OSC Names
  518. if (pData->engine->isOscControlRegistered())
  519. {
  520. pData->engine->oscSend_control_set_midi_program_count(pData->id, count);
  521. for (uint32_t i=0; i < count; ++i)
  522. pData->engine->oscSend_control_set_midi_program_data(pData->id, i, pData->midiprog.data[i].bank, pData->midiprog.data[i].program, pData->midiprog.data[i].name);
  523. }
  524. #endif
  525. if (doInit)
  526. {
  527. for (int i=0; i < MAX_MIDI_CHANNELS; ++i)
  528. {
  529. CARLA_SAFE_ASSERT_CONTINUE(fEngineChannels[i] != nullptr);
  530. /*fEngineChannels[i]->PrepareLoadInstrument(pData->filename, 0);
  531. fEngineChannels[i]->LoadInstrument();*/
  532. fInstrument->LoadInstrumentInBackground(fInstrumentIds[0], fEngineChannels[i]);
  533. fCurMidiProgs[i] = 0;
  534. }
  535. pData->midiprog.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. FLOAT_CLEAR(outBuffer[i], 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. bool allNotesOffSent = false;
  614. bool sampleAccurate = (pData->options & PLUGIN_OPTION_FIXED_BUFFERS) == 0;
  615. uint32_t nEvents = pData->event.portIn->getEventCount();
  616. uint32_t startTime = 0;
  617. uint32_t timeOffset = 0;
  618. uint32_t nextBankIds[MAX_MIDI_CHANNELS] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 0, 0, 0, 0, 0, 0 };
  619. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0 && pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  620. nextBankIds[pData->ctrlChannel] = pData->midiprog.data[pData->midiprog.current].bank;
  621. for (uint32_t i=0; i < nEvents; ++i)
  622. {
  623. const EngineEvent& event(pData->event.portIn->getEvent(i));
  624. CARLA_SAFE_ASSERT_CONTINUE(event.time < frames);
  625. CARLA_SAFE_ASSERT_BREAK(event.time >= timeOffset);
  626. if (event.time > timeOffset && sampleAccurate)
  627. {
  628. if (processSingle(outBuffer, event.time - timeOffset, timeOffset))
  629. {
  630. startTime = 0;
  631. timeOffset = event.time;
  632. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0 && pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  633. nextBankIds[pData->ctrlChannel] = pData->midiprog.data[pData->midiprog.current].bank;
  634. }
  635. else
  636. startTime += timeOffset;
  637. }
  638. // Control change
  639. switch (event.type)
  640. {
  641. case kEngineEventTypeNull:
  642. break;
  643. case kEngineEventTypeControl:
  644. {
  645. const EngineControlEvent& ctrlEvent = event.ctrl;
  646. switch (ctrlEvent.type)
  647. {
  648. case kEngineControlEventTypeNull:
  649. break;
  650. case kEngineControlEventTypeParameter:
  651. {
  652. #ifndef BUILD_BRIDGE
  653. // Control backend stuff
  654. if (event.channel == pData->ctrlChannel)
  655. {
  656. float value;
  657. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_DRYWET) != 0)
  658. {
  659. value = ctrlEvent.value;
  660. setDryWet(value, false, false);
  661. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, value);
  662. }
  663. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_VOLUME) != 0)
  664. {
  665. value = ctrlEvent.value*127.0f/100.0f;
  666. setVolume(value, false, false);
  667. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
  668. }
  669. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_BALANCE) != 0)
  670. {
  671. float left, right;
  672. value = ctrlEvent.value/0.5f - 1.0f;
  673. if (value < 0.0f)
  674. {
  675. left = -1.0f;
  676. right = (value*2.0f)+1.0f;
  677. }
  678. else if (value > 0.0f)
  679. {
  680. left = (value*2.0f)-1.0f;
  681. right = 1.0f;
  682. }
  683. else
  684. {
  685. left = -1.0f;
  686. right = 1.0f;
  687. }
  688. setBalanceLeft(left, false, false);
  689. setBalanceRight(right, false, false);
  690. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  691. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  692. }
  693. }
  694. #endif
  695. // Control plugin parameters
  696. for (uint32_t k=0; k < pData->param.count; ++k)
  697. {
  698. if (pData->param.data[k].midiChannel != event.channel)
  699. continue;
  700. if (pData->param.data[k].midiCC != ctrlEvent.param)
  701. continue;
  702. if (pData->param.data[k].hints != PARAMETER_INPUT)
  703. continue;
  704. if ((pData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  705. continue;
  706. float value;
  707. if (pData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  708. {
  709. value = (ctrlEvent.value < 0.5f) ? pData->param.ranges[k].min : pData->param.ranges[k].max;
  710. }
  711. else
  712. {
  713. value = pData->param.ranges[k].getUnnormalizedValue(ctrlEvent.value);
  714. if (pData->param.data[k].hints & PARAMETER_IS_INTEGER)
  715. value = std::rint(value);
  716. }
  717. setParameterValue(k, value, false, false, false);
  718. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, value);
  719. }
  720. if ((pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0 && ctrlEvent.param <= 0x5F)
  721. {
  722. fMidiInputPort->DispatchControlChange(uint8_t(ctrlEvent.param), uint8_t(ctrlEvent.value*127.0f), event.channel, static_cast<int32_t>(sampleAccurate ? startTime : event.time));
  723. }
  724. break;
  725. }
  726. case kEngineControlEventTypeMidiBank:
  727. if (event.channel < MAX_MIDI_CHANNELS && (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  728. nextBankIds[event.channel] = ctrlEvent.param;
  729. break;
  730. case kEngineControlEventTypeMidiProgram:
  731. if (event.channel < MAX_MIDI_CHANNELS && (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  732. {
  733. const uint32_t bankId(nextBankIds[event.channel]);
  734. const uint32_t progId(ctrlEvent.param);
  735. const uint32_t rIndex = bankId*128 + progId;
  736. for (uint32_t k=0; k < pData->midiprog.count; ++k)
  737. {
  738. if (pData->midiprog.data[k].bank == bankId && pData->midiprog.data[k].program == progId)
  739. {
  740. LinuxSampler::EngineChannel* const engineChannel(fEngineChannels[pData->ctrlChannel]);
  741. /*if (pData->engine->isOffline())
  742. {
  743. engineChannel->PrepareLoadInstrument(pData->filename, rIndex);
  744. engineChannel->LoadInstrument();
  745. }
  746. else*/
  747. {
  748. fInstrument->LoadInstrumentInBackground(fInstrumentIds[rIndex], engineChannel);
  749. }
  750. fCurMidiProgs[event.channel] = static_cast<int32_t>(k);
  751. if (event.channel == pData->ctrlChannel)
  752. pData->postponeRtEvent(kPluginPostRtEventMidiProgramChange, static_cast<int32_t>(k), 0, 0.0f);
  753. break;
  754. }
  755. }
  756. }
  757. break;
  758. case kEngineControlEventTypeAllSoundOff:
  759. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  760. {
  761. fMidiInputPort->DispatchControlChange(MIDI_CONTROL_ALL_SOUND_OFF, 0, event.channel, static_cast<int32_t>(sampleAccurate ? startTime : event.time));
  762. }
  763. break;
  764. case kEngineControlEventTypeAllNotesOff:
  765. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  766. {
  767. if (event.channel == pData->ctrlChannel && ! allNotesOffSent)
  768. {
  769. allNotesOffSent = true;
  770. sendMidiAllNotesOffToCallback();
  771. }
  772. fMidiInputPort->DispatchControlChange(MIDI_CONTROL_ALL_NOTES_OFF, 0, event.channel, static_cast<int32_t>(sampleAccurate ? startTime : event.time));
  773. }
  774. break;
  775. }
  776. break;
  777. }
  778. case kEngineEventTypeMidi:
  779. {
  780. const EngineMidiEvent& midiEvent(event.midi);
  781. uint8_t status = uint8_t(MIDI_GET_STATUS_FROM_DATA(midiEvent.data));
  782. uint8_t channel = event.channel;
  783. // Fix bad note-off
  784. if (MIDI_IS_STATUS_NOTE_ON(status) && midiEvent.data[2] == 0)
  785. status = MIDI_STATUS_NOTE_OFF;
  786. if (MIDI_IS_STATUS_POLYPHONIC_AFTERTOUCH(status) && (pData->options & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) == 0)
  787. continue;
  788. if (MIDI_IS_STATUS_CONTROL_CHANGE(status) && (pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) == 0)
  789. continue;
  790. if (MIDI_IS_STATUS_CHANNEL_PRESSURE(status) && (pData->options & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) == 0)
  791. continue;
  792. if (MIDI_IS_STATUS_PITCH_WHEEL_CONTROL(status) && (pData->options & PLUGIN_OPTION_SEND_PITCHBEND) == 0)
  793. continue;
  794. // put back channel in data
  795. uint8_t data[EngineMidiEvent::kDataSize];
  796. std::memcpy(data, event.midi.data, EngineMidiEvent::kDataSize);
  797. if (status < 0xF0 && channel < MAX_MIDI_CHANNELS)
  798. data[0] = uint8_t(data[0] + channel);
  799. fMidiInputPort->DispatchRaw(data, static_cast<int32_t>(sampleAccurate ? startTime : event.time));
  800. if (status == MIDI_STATUS_NOTE_ON)
  801. pData->postponeRtEvent(kPluginPostRtEventNoteOn, channel, data[1], data[2]);
  802. else if (status == MIDI_STATUS_NOTE_OFF)
  803. pData->postponeRtEvent(kPluginPostRtEventNoteOff, channel,data[1], 0.0f);
  804. break;
  805. }
  806. }
  807. }
  808. pData->postRtEvents.trySplice();
  809. if (frames > timeOffset)
  810. processSingle(outBuffer, frames - timeOffset, timeOffset);
  811. } // End of Event Input and Processing
  812. }
  813. bool processSingle(float** const outBuffer, const uint32_t frames, const uint32_t timeOffset)
  814. {
  815. CARLA_SAFE_ASSERT_RETURN(outBuffer != nullptr, false);
  816. CARLA_SAFE_ASSERT_RETURN(frames > 0, false);
  817. // --------------------------------------------------------------------------------------------------------
  818. // Try lock, silence otherwise
  819. if (pData->engine->isOffline())
  820. {
  821. pData->singleMutex.lock();
  822. }
  823. else if (! pData->singleMutex.tryLock())
  824. {
  825. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  826. {
  827. for (uint32_t k=0; k < frames; ++k)
  828. outBuffer[i][k+timeOffset] = 0.0f;
  829. }
  830. return false;
  831. }
  832. // --------------------------------------------------------------------------------------------------------
  833. // Run plugin
  834. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  835. {
  836. if (LinuxSampler::AudioChannel* const outDev = fAudioOutputDevice->Channel(i))
  837. outDev->SetBuffer(outBuffer[i] + timeOffset);
  838. }
  839. fAudioOutputDevice->Render(frames);
  840. #ifndef BUILD_BRIDGE
  841. // --------------------------------------------------------------------------------------------------------
  842. // Post-processing (dry/wet, volume and balance)
  843. {
  844. const bool doVolume = (pData->hints & PLUGIN_CAN_VOLUME) > 0 && pData->postProc.volume != 1.0f;
  845. const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) > 0 && (pData->postProc.balanceLeft != -1.0f || pData->postProc.balanceRight != 1.0f);
  846. float oldBufLeft[doBalance ? frames : 1];
  847. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  848. {
  849. // Balance
  850. if (doBalance)
  851. {
  852. if (i % 2 == 0)
  853. FLOAT_COPY(oldBufLeft, outBuffer[i], frames);
  854. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  855. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  856. for (uint32_t k=0; k < frames; ++k)
  857. {
  858. if (i % 2 == 0)
  859. {
  860. // left
  861. outBuffer[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  862. outBuffer[i][k] += outBuffer[i+1][k] * (1.0f - balRangeR);
  863. }
  864. else
  865. {
  866. // right
  867. outBuffer[i][k] = outBuffer[i][k] * balRangeR;
  868. outBuffer[i][k] += oldBufLeft[k] * balRangeL;
  869. }
  870. }
  871. }
  872. // Volume
  873. if (doVolume)
  874. {
  875. for (uint32_t k=0; k < frames; ++k)
  876. outBuffer[i][k+timeOffset] *= pData->postProc.volume;
  877. }
  878. }
  879. } // End of Post-processing
  880. #endif
  881. // --------------------------------------------------------------------------------------------------------
  882. pData->singleMutex.unlock();
  883. return true;
  884. }
  885. #ifndef CARLA_OS_WIN // FIXME, need to update linuxsampler win32 build
  886. void bufferSizeChanged(const uint32_t) override
  887. {
  888. CARLA_SAFE_ASSERT_RETURN(fAudioOutputDevice != nullptr,);
  889. fAudioOutputDevice->ReconnectAll();
  890. }
  891. void sampleRateChanged(const double) override
  892. {
  893. CARLA_SAFE_ASSERT_RETURN(fAudioOutputDevice != nullptr,);
  894. fAudioOutputDevice->ReconnectAll();
  895. }
  896. #endif
  897. // -------------------------------------------------------------------
  898. // Plugin buffers
  899. // nothing
  900. // -------------------------------------------------------------------
  901. const void* getExtraStuff() const noexcept override
  902. {
  903. static const char xtrue[] = "true";
  904. static const char xfalse[] = "false";
  905. return fUses16Outs ? xtrue : xfalse;
  906. }
  907. bool init(const char* const filename, const char* const name, const char* const label)
  908. {
  909. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  910. // ---------------------------------------------------------------
  911. // first checks
  912. if (pData->client != nullptr)
  913. {
  914. pData->engine->setLastError("Plugin client is already registered");
  915. return false;
  916. }
  917. if (filename == nullptr || filename[0] == '\0')
  918. {
  919. pData->engine->setLastError("null filename");
  920. return false;
  921. }
  922. if (label == nullptr || label[0] == '\0')
  923. {
  924. pData->engine->setLastError("null label");
  925. return false;
  926. }
  927. // ---------------------------------------------------------------
  928. // Store format
  929. CarlaString cstype(fFormat);
  930. cstype.toLower();
  931. const char* const ctype(cstype.buffer());
  932. // ---------------------------------------------------------------
  933. // Create the LinuxSampler Engine
  934. try {
  935. fEngine = LinuxSampler::EngineFactory::Create(ctype);
  936. }
  937. catch (LinuxSampler::Exception& e)
  938. {
  939. pData->engine->setLastError(e.what());
  940. return false;
  941. }
  942. // ---------------------------------------------------------------
  943. // Init LinuxSampler stuff
  944. fAudioOutputDevice = new LinuxSampler::AudioOutputDevicePlugin(pData->engine, this, fUses16Outs);
  945. fMidiInputDevice = new LinuxSampler::MidiInputDevicePlugin(&fSampler);
  946. fMidiInputPort = fMidiInputDevice->CreateMidiPortPlugin();
  947. for (uint i=0; i < MAX_MIDI_CHANNELS; ++i)
  948. {
  949. fSamplerChannels[i] = fSampler.AddSamplerChannel();
  950. CARLA_SAFE_ASSERT_CONTINUE(fSamplerChannels[i] != nullptr);
  951. fSamplerChannels[i]->SetEngineType(ctype);
  952. fSamplerChannels[i]->SetAudioOutputDevice(fAudioOutputDevice);
  953. fEngineChannels[i] = fSamplerChannels[i]->GetEngineChannel();
  954. CARLA_SAFE_ASSERT_CONTINUE(fEngineChannels[i] != nullptr);
  955. fEngineChannels[i]->Connect(fAudioOutputDevice);
  956. fEngineChannels[i]->Volume(LinuxSampler::kVolumeMax);
  957. if (fUses16Outs)
  958. {
  959. fEngineChannels[i]->SetOutputChannel(0, i*2);
  960. fEngineChannels[i]->SetOutputChannel(1, i*2 +1);
  961. }
  962. else
  963. {
  964. fEngineChannels[i]->SetOutputChannel(0, 0);
  965. fEngineChannels[i]->SetOutputChannel(1, 1);
  966. }
  967. fMidiInputPort->Connect(fEngineChannels[i], static_cast<LinuxSampler::midi_chan_t>(i));
  968. }
  969. // ---------------------------------------------------------------
  970. // Get the Engine's Instrument Manager
  971. fInstrument = fEngine->GetInstrumentManager();
  972. if (fInstrument == nullptr)
  973. {
  974. pData->engine->setLastError("Failed to get LinuxSampler instrument manager");
  975. return false;
  976. }
  977. // ---------------------------------------------------------------
  978. // Load the Instrument via filename
  979. try {
  980. fInstrumentIds = fInstrument->GetInstrumentFileContent(filename);
  981. }
  982. catch (const LinuxSampler::InstrumentManagerException& e)
  983. {
  984. pData->engine->setLastError(e.what());
  985. return false;
  986. }
  987. // ---------------------------------------------------------------
  988. // Get info
  989. if (fInstrumentIds.size() == 0)
  990. {
  991. pData->engine->setLastError("Failed to find any instruments");
  992. return false;
  993. }
  994. LinuxSampler::InstrumentManager::instrument_info_t info;
  995. try {
  996. info = fInstrument->GetInstrumentInfo(fInstrumentIds[0]);
  997. }
  998. catch (const LinuxSampler::InstrumentManagerException& e)
  999. {
  1000. pData->engine->setLastError(e.what());
  1001. return false;
  1002. }
  1003. CarlaString label2(label);
  1004. if (fUses16Outs && ! label2.endsWith(" (16 outs)"))
  1005. label2 += " (16 outs)";
  1006. fLabel = label2.dup();
  1007. fMaker = carla_strdup(info.Artists.c_str());
  1008. fRealName = carla_strdup(info.InstrumentName.c_str());
  1009. pData->filename = carla_strdup(filename);
  1010. if (name != nullptr && name[0] != '\0')
  1011. pData->name = pData->engine->getUniquePluginName(name);
  1012. else if (fRealName[0] != '\0')
  1013. pData->name = pData->engine->getUniquePluginName(fRealName);
  1014. else
  1015. pData->name = pData->engine->getUniquePluginName(label);
  1016. // ---------------------------------------------------------------
  1017. // register client
  1018. pData->client = pData->engine->addClient(this);
  1019. if (pData->client == nullptr || ! pData->client->isOk())
  1020. {
  1021. pData->engine->setLastError("Failed to register plugin client");
  1022. return false;
  1023. }
  1024. // ---------------------------------------------------------------
  1025. // load plugin settings
  1026. {
  1027. // set default options
  1028. pData->options = 0x0;
  1029. pData->options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  1030. pData->options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  1031. pData->options |= PLUGIN_OPTION_SEND_PITCHBEND;
  1032. pData->options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  1033. #ifndef BUILD_BRIDGE
  1034. // set identifier string
  1035. CarlaString identifier(fFormat);
  1036. identifier += "/";
  1037. if (const char* const shortname = std::strrchr(filename, OS_SEP))
  1038. identifier += shortname+1;
  1039. else
  1040. identifier += label;
  1041. pData->identifier = identifier.dup();
  1042. // load settings
  1043. pData->options = pData->loadSettings(pData->options, getOptionsAvailable());
  1044. #endif
  1045. }
  1046. return true;
  1047. }
  1048. // -------------------------------------------------------------------
  1049. private:
  1050. const bool fUses16Outs;
  1051. const char* fFormat;
  1052. const char* fLabel;
  1053. const char* fMaker;
  1054. const char* fRealName;
  1055. int32_t fCurMidiProgs[MAX_MIDI_CHANNELS];
  1056. LinuxSampler::Sampler fSampler;
  1057. LinuxSampler::Engine* fEngine;
  1058. LinuxSampler::SamplerChannel* fSamplerChannels[MAX_MIDI_CHANNELS];
  1059. LinuxSampler::EngineChannel* fEngineChannels[MAX_MIDI_CHANNELS];
  1060. LinuxSampler::AudioOutputDevicePlugin* fAudioOutputDevice;
  1061. LinuxSampler::MidiInputDevicePlugin* fMidiInputDevice;
  1062. LinuxSampler::MidiInputPortPlugin* fMidiInputPort;
  1063. LinuxSampler::InstrumentManager* fInstrument;
  1064. std::vector<LinuxSampler::InstrumentManager::instrument_id_t> fInstrumentIds;
  1065. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(LinuxSamplerPlugin)
  1066. };
  1067. CARLA_BACKEND_END_NAMESPACE
  1068. #endif // WANT_LINUXSAMPLER
  1069. CARLA_BACKEND_START_NAMESPACE
  1070. CarlaPlugin* CarlaPlugin::newLinuxSampler(const Initializer& init, const char* const format, const bool use16Outs)
  1071. {
  1072. 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));
  1073. #ifdef WANT_LINUXSAMPLER
  1074. if (init.engine->getProccessMode() == ENGINE_PROCESS_MODE_CONTINUOUS_RACK && use16Outs)
  1075. {
  1076. init.engine->setLastError("Carla's rack mode can only work with Stereo modules, please choose the 2-channel only sample-library version");
  1077. return nullptr;
  1078. }
  1079. // -------------------------------------------------------------------
  1080. // Check if file exists
  1081. {
  1082. QFileInfo file(init.filename);
  1083. if (! file.exists())
  1084. {
  1085. init.engine->setLastError("Requested file does not exist");
  1086. return nullptr;
  1087. }
  1088. if (! file.isFile())
  1089. {
  1090. init.engine->setLastError("Requested file is not valid");
  1091. return nullptr;
  1092. }
  1093. if (! file.isReadable())
  1094. {
  1095. init.engine->setLastError("Requested file is not readable");
  1096. return nullptr;
  1097. }
  1098. }
  1099. LinuxSamplerPlugin* const plugin(new LinuxSamplerPlugin(init.engine, init.id, format, use16Outs));
  1100. if (! plugin->init(init.filename, init.name, init.label))
  1101. {
  1102. delete plugin;
  1103. return nullptr;
  1104. }
  1105. plugin->reload();
  1106. return plugin;
  1107. #else
  1108. init.engine->setLastError("linuxsampler support not available");
  1109. return nullptr;
  1110. // unused
  1111. (void)format;
  1112. (void)use16Outs;
  1113. #endif
  1114. }
  1115. CARLA_BACKEND_END_NAMESPACE