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.

1835 lines
68KB

  1. /*
  2. * Carla FluidSynth Plugin
  3. * Copyright (C) 2011-2019 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. #include "CarlaPluginInternal.hpp"
  18. #include "CarlaEngine.hpp"
  19. #ifdef HAVE_FLUIDSYNTH
  20. #include "CarlaBackendUtils.hpp"
  21. #include "CarlaMathUtils.hpp"
  22. #include "water/text/StringArray.h"
  23. #include <fluidsynth.h>
  24. #define FLUID_DEFAULT_POLYPHONY 64
  25. using water::String;
  26. using water::StringArray;
  27. CARLA_BACKEND_START_NAMESPACE
  28. // -------------------------------------------------------------------------------------------------------------------
  29. // Fallback data
  30. static const ExternalMidiNote kExternalMidiNoteFallback = { -1, 0, 0 };
  31. // -------------------------------------------------------------------------------------------------------------------
  32. class CarlaPluginFluidSynth : public CarlaPlugin
  33. {
  34. public:
  35. CarlaPluginFluidSynth(CarlaEngine* const engine, const uint id, const bool use16Outs)
  36. : CarlaPlugin(engine, id),
  37. kUse16Outs(use16Outs),
  38. fSettings(nullptr),
  39. fSynth(nullptr),
  40. fSynthId(0),
  41. fAudio16Buffers(nullptr),
  42. fLabel(nullptr)
  43. {
  44. carla_debug("CarlaPluginFluidSynth::CarlaPluginFluidSynth(%p, %i, %s)", engine, id, bool2str(use16Outs));
  45. carla_zeroFloats(fParamBuffers, FluidSynthParametersMax);
  46. carla_fill<int32_t>(fCurMidiProgs, 0, MAX_MIDI_CHANNELS);
  47. // create settings
  48. fSettings = new_fluid_settings();
  49. CARLA_SAFE_ASSERT_RETURN(fSettings != nullptr,);
  50. // define settings
  51. fluid_settings_setint(fSettings, "synth.audio-channels", use16Outs ? 16 : 1);
  52. fluid_settings_setint(fSettings, "synth.audio-groups", use16Outs ? 16 : 1);
  53. fluid_settings_setnum(fSettings, "synth.sample-rate", pData->engine->getSampleRate());
  54. //fluid_settings_setnum(fSettings, "synth.cpu-cores", 2);
  55. fluid_settings_setint(fSettings, "synth.parallel-render", 1);
  56. fluid_settings_setint(fSettings, "synth.threadsafe-api", 0);
  57. // create synth
  58. fSynth = new_fluid_synth(fSettings);
  59. CARLA_SAFE_ASSERT_RETURN(fSynth != nullptr,);
  60. initializeFluidDefaultsIfNeeded();
  61. fluid_synth_set_sample_rate(fSynth, (float)pData->engine->getSampleRate());
  62. // set default values
  63. fluid_synth_set_reverb_on(fSynth, 1);
  64. fluid_synth_set_reverb(fSynth,
  65. sFluidDefaults[FluidSynthReverbRoomSize],
  66. sFluidDefaults[FluidSynthReverbDamp],
  67. sFluidDefaults[FluidSynthReverbWidth],
  68. sFluidDefaults[FluidSynthReverbLevel]);
  69. fluid_synth_set_chorus_on(fSynth, 1);
  70. fluid_synth_set_chorus(fSynth,
  71. static_cast<int>(sFluidDefaults[FluidSynthChorusNr] + 0.5f),
  72. sFluidDefaults[FluidSynthChorusLevel],
  73. sFluidDefaults[FluidSynthChorusSpeedHz],
  74. sFluidDefaults[FluidSynthChorusDepthMs],
  75. static_cast<int>(sFluidDefaults[FluidSynthChorusType] + 0.5f));
  76. fluid_synth_set_polyphony(fSynth, FLUID_DEFAULT_POLYPHONY);
  77. fluid_synth_set_gain(fSynth, 1.0f);
  78. for (int i=0; i < MAX_MIDI_CHANNELS; ++i)
  79. fluid_synth_set_interp_method(fSynth, i, static_cast<int>(sFluidDefaults[FluidSynthInterpolation] + 0.5f));
  80. }
  81. ~CarlaPluginFluidSynth() override
  82. {
  83. carla_debug("CarlaPluginFluidSynth::~CarlaPluginFluidSynth()");
  84. pData->singleMutex.lock();
  85. pData->masterMutex.lock();
  86. if (pData->client != nullptr && pData->client->isActive())
  87. pData->client->deactivate();
  88. if (pData->active)
  89. {
  90. deactivate();
  91. pData->active = false;
  92. }
  93. if (fSynth != nullptr)
  94. {
  95. delete_fluid_synth(fSynth);
  96. fSynth = nullptr;
  97. }
  98. if (fSettings != nullptr)
  99. {
  100. delete_fluid_settings(fSettings);
  101. fSettings = nullptr;
  102. }
  103. if (fLabel != nullptr)
  104. {
  105. delete[] fLabel;
  106. fLabel = nullptr;
  107. }
  108. clearBuffers();
  109. }
  110. // -------------------------------------------------------------------
  111. // Information (base)
  112. PluginType getType() const noexcept override
  113. {
  114. return PLUGIN_SF2;
  115. }
  116. PluginCategory getCategory() const noexcept override
  117. {
  118. return PLUGIN_CATEGORY_SYNTH;
  119. }
  120. // -------------------------------------------------------------------
  121. // Information (count)
  122. uint32_t getParameterScalePointCount(const uint32_t parameterId) const noexcept override
  123. {
  124. switch (parameterId)
  125. {
  126. case FluidSynthChorusType:
  127. return 2;
  128. case FluidSynthInterpolation:
  129. return 4;
  130. default:
  131. return 0;
  132. }
  133. }
  134. // -------------------------------------------------------------------
  135. // Information (current data)
  136. // nothing
  137. // -------------------------------------------------------------------
  138. // Information (per-plugin data)
  139. uint getOptionsAvailable() const noexcept override
  140. {
  141. uint options = 0x0;
  142. options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  143. options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  144. options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  145. options |= PLUGIN_OPTION_SEND_PITCHBEND;
  146. options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  147. return options;
  148. }
  149. float getParameterValue(const uint32_t parameterId) const noexcept override
  150. {
  151. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0.0f);
  152. return fParamBuffers[parameterId];
  153. }
  154. float getParameterScalePointValue(const uint32_t parameterId, const uint32_t scalePointId) const noexcept override
  155. {
  156. switch (parameterId)
  157. {
  158. case FluidSynthChorusType:
  159. switch (scalePointId)
  160. {
  161. case 0:
  162. return FLUID_CHORUS_MOD_SINE;
  163. case 1:
  164. return FLUID_CHORUS_MOD_TRIANGLE;
  165. default:
  166. return sFluidDefaults[FluidSynthChorusType];
  167. }
  168. case FluidSynthInterpolation:
  169. switch (scalePointId)
  170. {
  171. case 0:
  172. return FLUID_INTERP_NONE;
  173. case 1:
  174. return FLUID_INTERP_LINEAR;
  175. case 2:
  176. return FLUID_INTERP_4THORDER;
  177. case 3:
  178. return FLUID_INTERP_7THORDER;
  179. default:
  180. return sFluidDefaults[FluidSynthInterpolation];
  181. }
  182. default:
  183. return 0.0f;
  184. }
  185. }
  186. bool getLabel(char* const strBuf) const noexcept override
  187. {
  188. if (fLabel != nullptr)
  189. {
  190. std::strncpy(strBuf, fLabel, STR_MAX);
  191. return true;
  192. }
  193. return CarlaPlugin::getLabel(strBuf);
  194. }
  195. bool getMaker(char* const strBuf) const noexcept override
  196. {
  197. std::strncpy(strBuf, "FluidSynth SF2 engine", STR_MAX);
  198. return true;
  199. }
  200. bool getCopyright(char* const strBuf) const noexcept override
  201. {
  202. std::strncpy(strBuf, "GNU GPL v2+", STR_MAX);
  203. return true;
  204. }
  205. bool getRealName(char* const strBuf) const noexcept override
  206. {
  207. return getLabel(strBuf);
  208. }
  209. bool getParameterName(const uint32_t parameterId, char* const strBuf) const noexcept override
  210. {
  211. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, false);
  212. switch (parameterId)
  213. {
  214. case FluidSynthReverbOnOff:
  215. std::strncpy(strBuf, "Reverb On/Off", STR_MAX);
  216. return true;
  217. case FluidSynthReverbRoomSize:
  218. std::strncpy(strBuf, "Reverb Room Size", STR_MAX);
  219. return true;
  220. case FluidSynthReverbDamp:
  221. std::strncpy(strBuf, "Reverb Damp", STR_MAX);
  222. return true;
  223. case FluidSynthReverbLevel:
  224. std::strncpy(strBuf, "Reverb Level", STR_MAX);
  225. return true;
  226. case FluidSynthReverbWidth:
  227. std::strncpy(strBuf, "Reverb Width", STR_MAX);
  228. return true;
  229. case FluidSynthChorusOnOff:
  230. std::strncpy(strBuf, "Chorus On/Off", STR_MAX);
  231. return true;
  232. case FluidSynthChorusNr:
  233. std::strncpy(strBuf, "Chorus Voice Count", STR_MAX);
  234. return true;
  235. case FluidSynthChorusLevel:
  236. std::strncpy(strBuf, "Chorus Level", STR_MAX);
  237. return true;
  238. case FluidSynthChorusSpeedHz:
  239. std::strncpy(strBuf, "Chorus Speed", STR_MAX);
  240. return true;
  241. case FluidSynthChorusDepthMs:
  242. std::strncpy(strBuf, "Chorus Depth", STR_MAX);
  243. return true;
  244. case FluidSynthChorusType:
  245. std::strncpy(strBuf, "Chorus Type", STR_MAX);
  246. return true;
  247. case FluidSynthPolyphony:
  248. std::strncpy(strBuf, "Polyphony", STR_MAX);
  249. return true;
  250. case FluidSynthInterpolation:
  251. std::strncpy(strBuf, "Interpolation", STR_MAX);
  252. return true;
  253. case FluidSynthVoiceCount:
  254. std::strncpy(strBuf, "Voice Count", STR_MAX);
  255. return true;
  256. }
  257. return CarlaPlugin::getParameterName(parameterId, strBuf);
  258. }
  259. bool getParameterUnit(const uint32_t parameterId, char* const strBuf) const noexcept override
  260. {
  261. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, false);
  262. switch (parameterId)
  263. {
  264. case FluidSynthChorusSpeedHz:
  265. std::strncpy(strBuf, "Hz", STR_MAX);
  266. return true;
  267. case FluidSynthChorusDepthMs:
  268. std::strncpy(strBuf, "ms", STR_MAX);
  269. return true;
  270. }
  271. return CarlaPlugin::getParameterUnit(parameterId, strBuf);
  272. }
  273. bool getParameterScalePointLabel(const uint32_t parameterId, const uint32_t scalePointId, char* const strBuf) const noexcept override
  274. {
  275. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, false);
  276. CARLA_SAFE_ASSERT_RETURN(scalePointId < getParameterScalePointCount(parameterId), false);
  277. switch (parameterId)
  278. {
  279. case FluidSynthChorusType:
  280. switch (scalePointId)
  281. {
  282. case 0:
  283. std::strncpy(strBuf, "Sine wave", STR_MAX);
  284. return true;
  285. case 1:
  286. std::strncpy(strBuf, "Triangle wave", STR_MAX);
  287. return true;
  288. }
  289. break;
  290. case FluidSynthInterpolation:
  291. switch (scalePointId)
  292. {
  293. case 0:
  294. std::strncpy(strBuf, "None", STR_MAX);
  295. return true;
  296. case 1:
  297. std::strncpy(strBuf, "Straight-line", STR_MAX);
  298. return true;
  299. case 2:
  300. std::strncpy(strBuf, "Fourth-order", STR_MAX);
  301. return true;
  302. case 3:
  303. std::strncpy(strBuf, "Seventh-order", STR_MAX);
  304. return true;
  305. }
  306. break;
  307. }
  308. return CarlaPlugin::getParameterScalePointLabel(parameterId, scalePointId, strBuf);
  309. }
  310. // -------------------------------------------------------------------
  311. // Set data (state)
  312. void prepareForSave() override
  313. {
  314. char strBuf[STR_MAX+1];
  315. 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],
  316. fCurMidiProgs[4], fCurMidiProgs[5], fCurMidiProgs[6], fCurMidiProgs[7],
  317. fCurMidiProgs[8], fCurMidiProgs[9], fCurMidiProgs[10], fCurMidiProgs[11],
  318. fCurMidiProgs[12], fCurMidiProgs[13], fCurMidiProgs[14], fCurMidiProgs[15]);
  319. CarlaPlugin::setCustomData(CUSTOM_DATA_TYPE_STRING, "midiPrograms", strBuf, false);
  320. }
  321. // -------------------------------------------------------------------
  322. // Set data (internal stuff)
  323. void setCtrlChannel(const int8_t channel, const bool sendOsc, const bool sendCallback) noexcept override
  324. {
  325. if (channel >= 0 && channel < MAX_MIDI_CHANNELS)
  326. pData->midiprog.current = fCurMidiProgs[channel];
  327. CarlaPlugin::setCtrlChannel(channel, sendOsc, sendCallback);
  328. }
  329. // -------------------------------------------------------------------
  330. // Set data (plugin-specific stuff)
  331. void setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  332. {
  333. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  334. CARLA_SAFE_ASSERT_RETURN(sendGui || sendOsc || sendCallback,);
  335. float fixedValue;
  336. {
  337. const ScopedSingleProcessLocker spl(this, (sendGui || sendOsc || sendCallback));
  338. fixedValue = setParameterValueInFluidSynth(parameterId, value);
  339. }
  340. CarlaPlugin::setParameterValue(parameterId, fixedValue, sendGui, sendOsc, sendCallback);
  341. }
  342. void setParameterValueRT(const uint32_t parameterId, const float value, const bool sendCallbackLater) noexcept override
  343. {
  344. const float fixedValue = setParameterValueInFluidSynth(parameterId, value);
  345. CarlaPlugin::setParameterValueRT(parameterId, fixedValue, sendCallbackLater);
  346. }
  347. float setParameterValueInFluidSynth(const uint32_t parameterId, const float value) noexcept
  348. {
  349. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, value);
  350. const float fixedValue(pData->param.getFixedValue(parameterId, value));
  351. fParamBuffers[parameterId] = fixedValue;
  352. switch (parameterId)
  353. {
  354. case FluidSynthReverbOnOff:
  355. try {
  356. fluid_synth_set_reverb_on(fSynth, (fixedValue > 0.5f) ? 1 : 0);
  357. } CARLA_SAFE_EXCEPTION("fluid_synth_set_reverb_on")
  358. break;
  359. case FluidSynthReverbRoomSize:
  360. case FluidSynthReverbDamp:
  361. case FluidSynthReverbLevel:
  362. case FluidSynthReverbWidth:
  363. try {
  364. fluid_synth_set_reverb(fSynth,
  365. fParamBuffers[FluidSynthReverbRoomSize],
  366. fParamBuffers[FluidSynthReverbDamp],
  367. fParamBuffers[FluidSynthReverbWidth],
  368. fParamBuffers[FluidSynthReverbLevel]);
  369. } CARLA_SAFE_EXCEPTION("fluid_synth_set_reverb")
  370. break;
  371. case FluidSynthChorusOnOff:
  372. try {
  373. fluid_synth_set_chorus_on(fSynth, (value > 0.5f) ? 1 : 0);
  374. } CARLA_SAFE_EXCEPTION("fluid_synth_set_chorus_on")
  375. break;
  376. case FluidSynthChorusNr:
  377. case FluidSynthChorusLevel:
  378. case FluidSynthChorusSpeedHz:
  379. case FluidSynthChorusDepthMs:
  380. case FluidSynthChorusType:
  381. try {
  382. fluid_synth_set_chorus(fSynth,
  383. (int)fParamBuffers[FluidSynthChorusNr],
  384. fParamBuffers[FluidSynthChorusLevel],
  385. fParamBuffers[FluidSynthChorusSpeedHz],
  386. fParamBuffers[FluidSynthChorusDepthMs],
  387. (int)fParamBuffers[FluidSynthChorusType]);
  388. } CARLA_SAFE_EXCEPTION("fluid_synth_set_chorus")
  389. break;
  390. case FluidSynthPolyphony:
  391. try {
  392. fluid_synth_set_polyphony(fSynth, (int)value);
  393. } CARLA_SAFE_EXCEPTION("fluid_synth_set_polyphony")
  394. break;
  395. case FluidSynthInterpolation:
  396. for (int i=0; i < MAX_MIDI_CHANNELS; ++i)
  397. {
  398. try {
  399. fluid_synth_set_interp_method(fSynth, i, (int)value);
  400. } CARLA_SAFE_EXCEPTION_BREAK("fluid_synth_set_interp_method")
  401. }
  402. break;
  403. default:
  404. break;
  405. }
  406. return fixedValue;
  407. }
  408. void setCustomData(const char* const type, const char* const key, const char* const value, const bool sendGui) override
  409. {
  410. CARLA_SAFE_ASSERT_RETURN(fSynth != nullptr,);
  411. CARLA_SAFE_ASSERT_RETURN(type != nullptr && type[0] != '\0',);
  412. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  413. CARLA_SAFE_ASSERT_RETURN(value != nullptr && value[0] != '\0',);
  414. carla_debug("CarlaPluginFluidSynth::setCustomData(%s, \"%s\", \"%s\", %s)", type, key, value, bool2str(sendGui));
  415. if (std::strcmp(type, CUSTOM_DATA_TYPE_PROPERTY) == 0)
  416. return CarlaPlugin::setCustomData(type, key, value, sendGui);
  417. if (std::strcmp(type, CUSTOM_DATA_TYPE_STRING) != 0)
  418. return carla_stderr2("CarlaPluginFluidSynth::setCustomData(\"%s\", \"%s\", \"%s\", %s) - type is not string", type, key, value, bool2str(sendGui));
  419. if (std::strcmp(key, "midiPrograms") != 0)
  420. return carla_stderr2("CarlaPluginFluidSynth::setCustomData(\"%s\", \"%s\", \"%s\", %s) - type is not string", type, key, value, bool2str(sendGui));
  421. StringArray midiProgramList(StringArray::fromTokens(value, ":", ""));
  422. if (midiProgramList.size() == MAX_MIDI_CHANNELS)
  423. {
  424. uint8_t channel = 0;
  425. for (String *it=midiProgramList.begin(), *end=midiProgramList.end(); it != end; ++it)
  426. {
  427. const int index(it->getIntValue());
  428. if (index >= 0 && index < static_cast<int>(pData->midiprog.count))
  429. {
  430. const uint32_t bank = pData->midiprog.data[index].bank;
  431. const uint32_t program = pData->midiprog.data[index].program;
  432. fluid_synth_program_select(fSynth, channel, fSynthId, bank, program);
  433. fCurMidiProgs[channel] = index;
  434. if (pData->ctrlChannel == static_cast<int32_t>(channel))
  435. {
  436. pData->midiprog.current = index;
  437. pData->engine->callback(true, true,
  438. ENGINE_CALLBACK_MIDI_PROGRAM_CHANGED,
  439. pData->id,
  440. index,
  441. 0, 0, 0.0f, nullptr);
  442. }
  443. }
  444. ++channel;
  445. }
  446. CARLA_SAFE_ASSERT(channel == MAX_MIDI_CHANNELS);
  447. }
  448. CarlaPlugin::setCustomData(type, key, value, sendGui);
  449. }
  450. void setMidiProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback, const bool doingInit) noexcept override
  451. {
  452. CARLA_SAFE_ASSERT_RETURN(fSynth != nullptr,);
  453. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->midiprog.count),);
  454. CARLA_SAFE_ASSERT_RETURN(sendGui || sendOsc || sendCallback || doingInit,);
  455. if (index >= 0 && pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  456. {
  457. const uint32_t bank = pData->midiprog.data[index].bank;
  458. const uint32_t program = pData->midiprog.data[index].program;
  459. const ScopedSingleProcessLocker spl(this, (sendGui || sendOsc || sendCallback));
  460. try {
  461. fluid_synth_program_select(fSynth, pData->ctrlChannel, fSynthId, bank, program);
  462. } CARLA_SAFE_EXCEPTION("fluid_synth_program_select")
  463. fCurMidiProgs[pData->ctrlChannel] = index;
  464. }
  465. CarlaPlugin::setMidiProgram(index, sendGui, sendOsc, sendCallback, doingInit);
  466. }
  467. // FIXME: this is never used
  468. void setMidiProgramRT(const uint32_t uindex, const bool sendCallbackLater) noexcept override
  469. {
  470. CARLA_SAFE_ASSERT_RETURN(fSynth != nullptr,);
  471. CARLA_SAFE_ASSERT_RETURN(uindex < pData->midiprog.count,);
  472. if (pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  473. {
  474. const uint32_t bank = pData->midiprog.data[uindex].bank;
  475. const uint32_t program = pData->midiprog.data[uindex].program;
  476. try {
  477. fluid_synth_program_select(fSynth, pData->ctrlChannel, fSynthId, bank, program);
  478. } CARLA_SAFE_EXCEPTION("fluid_synth_program_select")
  479. fCurMidiProgs[pData->ctrlChannel] = static_cast<int32_t>(uindex);
  480. }
  481. CarlaPlugin::setMidiProgramRT(uindex, sendCallbackLater);
  482. }
  483. // -------------------------------------------------------------------
  484. // Set ui stuff
  485. // nothing
  486. // -------------------------------------------------------------------
  487. // Plugin state
  488. void reload() override
  489. {
  490. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr,);
  491. CARLA_SAFE_ASSERT_RETURN(fSynth != nullptr,);
  492. carla_debug("CarlaPluginFluidSynth::reload() - start");
  493. const EngineProcessMode processMode(pData->engine->getProccessMode());
  494. // Safely disable plugin for reload
  495. const ScopedDisabler sd(this);
  496. if (pData->active)
  497. deactivate();
  498. clearBuffers();
  499. uint32_t aOuts, params;
  500. aOuts = kUse16Outs ? 32 : 2;
  501. params = FluidSynthParametersMax;
  502. pData->audioOut.createNew(aOuts);
  503. pData->param.createNew(params, false);
  504. const uint portNameSize(pData->engine->getMaxPortNameSize());
  505. CarlaString portName;
  506. // ---------------------------------------
  507. // Audio Outputs
  508. if (kUse16Outs)
  509. {
  510. for (uint32_t i=0; i < 32; ++i)
  511. {
  512. portName.clear();
  513. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  514. {
  515. portName = pData->name;
  516. portName += ":";
  517. }
  518. portName += "out-";
  519. if ((i+2)/2 < 9)
  520. portName += "0";
  521. portName += CarlaString((i+2)/2);
  522. if (i % 2 == 0)
  523. portName += "L";
  524. else
  525. portName += "R";
  526. portName.truncate(portNameSize);
  527. pData->audioOut.ports[i].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false, i);
  528. pData->audioOut.ports[i].rindex = i;
  529. }
  530. fAudio16Buffers = new float*[aOuts];
  531. for (uint32_t i=0; i < aOuts; ++i)
  532. fAudio16Buffers[i] = nullptr;
  533. }
  534. else
  535. {
  536. // out-left
  537. portName.clear();
  538. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  539. {
  540. portName = pData->name;
  541. portName += ":";
  542. }
  543. portName += "out-left";
  544. portName.truncate(portNameSize);
  545. pData->audioOut.ports[0].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false, 0);
  546. pData->audioOut.ports[0].rindex = 0;
  547. // out-right
  548. portName.clear();
  549. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  550. {
  551. portName = pData->name;
  552. portName += ":";
  553. }
  554. portName += "out-right";
  555. portName.truncate(portNameSize);
  556. pData->audioOut.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false, 1);
  557. pData->audioOut.ports[1].rindex = 1;
  558. }
  559. // ---------------------------------------
  560. // Event Input
  561. {
  562. portName.clear();
  563. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  564. {
  565. portName = pData->name;
  566. portName += ":";
  567. }
  568. portName += "events-in";
  569. portName.truncate(portNameSize);
  570. pData->event.portIn = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true, 0);
  571. }
  572. // ---------------------------------------
  573. // Event Output
  574. {
  575. portName.clear();
  576. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  577. {
  578. portName = pData->name;
  579. portName += ":";
  580. }
  581. portName += "events-out";
  582. portName.truncate(portNameSize);
  583. pData->event.portOut = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false, 0);
  584. }
  585. // ---------------------------------------
  586. // Parameters
  587. {
  588. int j;
  589. // ----------------------
  590. j = FluidSynthReverbOnOff;
  591. pData->param.data[j].type = PARAMETER_INPUT;
  592. pData->param.data[j].hints = PARAMETER_IS_ENABLED /*| PARAMETER_IS_AUTOMABLE*/ | PARAMETER_IS_BOOLEAN;
  593. pData->param.data[j].index = j;
  594. pData->param.data[j].rindex = j;
  595. pData->param.ranges[j].min = 0.0f;
  596. pData->param.ranges[j].max = 1.0f;
  597. pData->param.ranges[j].def = sFluidDefaults[j];
  598. pData->param.ranges[j].step = 1.0f;
  599. pData->param.ranges[j].stepSmall = 1.0f;
  600. pData->param.ranges[j].stepLarge = 1.0f;
  601. // ----------------------
  602. j = FluidSynthReverbRoomSize;
  603. pData->param.data[j].type = PARAMETER_INPUT;
  604. pData->param.data[j].hints = PARAMETER_IS_ENABLED /*| PARAMETER_IS_AUTOMABLE*/;
  605. pData->param.data[j].index = j;
  606. pData->param.data[j].rindex = j;
  607. pData->param.ranges[j].min = 0.0f;
  608. pData->param.ranges[j].max = 1.2f;
  609. pData->param.ranges[j].def = sFluidDefaults[j];
  610. pData->param.ranges[j].step = 0.01f;
  611. pData->param.ranges[j].stepSmall = 0.0001f;
  612. pData->param.ranges[j].stepLarge = 0.1f;
  613. // ----------------------
  614. j = FluidSynthReverbDamp;
  615. pData->param.data[j].type = PARAMETER_INPUT;
  616. pData->param.data[j].hints = PARAMETER_IS_ENABLED /*| PARAMETER_IS_AUTOMABLE*/;
  617. pData->param.data[j].index = j;
  618. pData->param.data[j].rindex = j;
  619. pData->param.ranges[j].min = 0.0f;
  620. pData->param.ranges[j].max = 1.0f;
  621. pData->param.ranges[j].def = sFluidDefaults[j];
  622. pData->param.ranges[j].step = 0.01f;
  623. pData->param.ranges[j].stepSmall = 0.0001f;
  624. pData->param.ranges[j].stepLarge = 0.1f;
  625. // ----------------------
  626. j = FluidSynthReverbLevel;
  627. pData->param.data[j].type = PARAMETER_INPUT;
  628. pData->param.data[j].hints = PARAMETER_IS_ENABLED /*| PARAMETER_IS_AUTOMABLE*/;
  629. pData->param.data[j].index = j;
  630. pData->param.data[j].rindex = j;
  631. pData->param.data[j].mappedControlIndex = MIDI_CONTROL_REVERB_SEND_LEVEL;
  632. pData->param.ranges[j].min = 0.0f;
  633. pData->param.ranges[j].max = 1.0f;
  634. pData->param.ranges[j].def = sFluidDefaults[j];
  635. pData->param.ranges[j].step = 0.01f;
  636. pData->param.ranges[j].stepSmall = 0.0001f;
  637. pData->param.ranges[j].stepLarge = 0.1f;
  638. // ----------------------
  639. j = FluidSynthReverbWidth;
  640. pData->param.data[j].type = PARAMETER_INPUT;
  641. pData->param.data[j].hints = PARAMETER_IS_ENABLED /*| PARAMETER_IS_AUTOMABLE*/;
  642. pData->param.data[j].index = j;
  643. pData->param.data[j].rindex = j;
  644. pData->param.ranges[j].min = 0.0f;
  645. pData->param.ranges[j].max = 10.0f; // should be 100, but that sounds too much
  646. pData->param.ranges[j].def = sFluidDefaults[j];
  647. pData->param.ranges[j].step = 0.01f;
  648. pData->param.ranges[j].stepSmall = 0.0001f;
  649. pData->param.ranges[j].stepLarge = 0.1f;
  650. // ----------------------
  651. j = FluidSynthChorusOnOff;
  652. pData->param.data[j].type = PARAMETER_INPUT;
  653. pData->param.data[j].hints = PARAMETER_IS_ENABLED | PARAMETER_IS_BOOLEAN;
  654. pData->param.data[j].index = j;
  655. pData->param.data[j].rindex = j;
  656. pData->param.ranges[j].min = 0.0f;
  657. pData->param.ranges[j].max = 1.0f;
  658. pData->param.ranges[j].def = sFluidDefaults[j];
  659. pData->param.ranges[j].step = 1.0f;
  660. pData->param.ranges[j].stepSmall = 1.0f;
  661. pData->param.ranges[j].stepLarge = 1.0f;
  662. // ----------------------
  663. j = FluidSynthChorusNr;
  664. pData->param.data[j].type = PARAMETER_INPUT;
  665. pData->param.data[j].hints = PARAMETER_IS_ENABLED | PARAMETER_IS_INTEGER;
  666. pData->param.data[j].index = j;
  667. pData->param.data[j].rindex = j;
  668. pData->param.ranges[j].min = 0.0f;
  669. pData->param.ranges[j].max = 99.0f;
  670. pData->param.ranges[j].def = sFluidDefaults[j];
  671. pData->param.ranges[j].step = 1.0f;
  672. pData->param.ranges[j].stepSmall = 1.0f;
  673. pData->param.ranges[j].stepLarge = 10.0f;
  674. // ----------------------
  675. j = FluidSynthChorusLevel;
  676. pData->param.data[j].type = PARAMETER_INPUT;
  677. pData->param.data[j].hints = PARAMETER_IS_ENABLED;
  678. pData->param.data[j].index = j;
  679. pData->param.data[j].rindex = j;
  680. pData->param.ranges[j].min = 0.0f;
  681. pData->param.ranges[j].max = 10.0f;
  682. pData->param.ranges[j].def = sFluidDefaults[j];
  683. pData->param.ranges[j].step = 0.01f;
  684. pData->param.ranges[j].stepSmall = 0.0001f;
  685. pData->param.ranges[j].stepLarge = 0.1f;
  686. // ----------------------
  687. j = FluidSynthChorusSpeedHz;
  688. pData->param.data[j].type = PARAMETER_INPUT;
  689. pData->param.data[j].hints = PARAMETER_IS_ENABLED;
  690. pData->param.data[j].index = j;
  691. pData->param.data[j].rindex = j;
  692. pData->param.ranges[j].min = 0.29f;
  693. pData->param.ranges[j].max = 5.0f;
  694. pData->param.ranges[j].def = sFluidDefaults[j];
  695. pData->param.ranges[j].step = 0.01f;
  696. pData->param.ranges[j].stepSmall = 0.0001f;
  697. pData->param.ranges[j].stepLarge = 0.1f;
  698. // ----------------------
  699. j = FluidSynthChorusDepthMs;
  700. pData->param.data[j].type = PARAMETER_INPUT;
  701. pData->param.data[j].hints = PARAMETER_IS_ENABLED;
  702. pData->param.data[j].index = j;
  703. pData->param.data[j].rindex = j;
  704. pData->param.ranges[j].min = 0.0f;
  705. pData->param.ranges[j].max = float(2048.0 * 1000.0 / pData->engine->getSampleRate()); // FIXME?
  706. pData->param.ranges[j].def = sFluidDefaults[j];
  707. pData->param.ranges[j].step = 0.01f;
  708. pData->param.ranges[j].stepSmall = 0.0001f;
  709. pData->param.ranges[j].stepLarge = 0.1f;
  710. // ----------------------
  711. j = FluidSynthChorusType;
  712. pData->param.data[j].type = PARAMETER_INPUT;
  713. pData->param.data[j].hints = PARAMETER_IS_ENABLED | PARAMETER_IS_INTEGER | PARAMETER_USES_SCALEPOINTS;
  714. pData->param.data[j].index = j;
  715. pData->param.data[j].rindex = j;
  716. pData->param.ranges[j].min = FLUID_CHORUS_MOD_SINE;
  717. pData->param.ranges[j].max = FLUID_CHORUS_MOD_TRIANGLE;
  718. pData->param.ranges[j].def = sFluidDefaults[j];
  719. pData->param.ranges[j].step = 1.0f;
  720. pData->param.ranges[j].stepSmall = 1.0f;
  721. pData->param.ranges[j].stepLarge = 1.0f;
  722. // ----------------------
  723. j = FluidSynthPolyphony;
  724. pData->param.data[j].type = PARAMETER_INPUT;
  725. pData->param.data[j].hints = PARAMETER_IS_ENABLED | PARAMETER_IS_INTEGER;
  726. pData->param.data[j].index = j;
  727. pData->param.data[j].rindex = j;
  728. pData->param.ranges[j].min = 1.0f;
  729. pData->param.ranges[j].max = 512.0f; // max theoric is 65535
  730. pData->param.ranges[j].def = sFluidDefaults[j];
  731. pData->param.ranges[j].step = 1.0f;
  732. pData->param.ranges[j].stepSmall = 1.0f;
  733. pData->param.ranges[j].stepLarge = 10.0f;
  734. // ----------------------
  735. j = FluidSynthInterpolation;
  736. pData->param.data[j].type = PARAMETER_INPUT;
  737. pData->param.data[j].hints = PARAMETER_IS_ENABLED | PARAMETER_IS_INTEGER | PARAMETER_USES_SCALEPOINTS;
  738. pData->param.data[j].index = j;
  739. pData->param.data[j].rindex = j;
  740. pData->param.ranges[j].min = FLUID_INTERP_NONE;
  741. pData->param.ranges[j].max = FLUID_INTERP_HIGHEST;
  742. pData->param.ranges[j].def = sFluidDefaults[j];
  743. pData->param.ranges[j].step = 1.0f;
  744. pData->param.ranges[j].stepSmall = 1.0f;
  745. pData->param.ranges[j].stepLarge = 1.0f;
  746. // ----------------------
  747. j = FluidSynthVoiceCount;
  748. pData->param.data[j].type = PARAMETER_OUTPUT;
  749. pData->param.data[j].hints = PARAMETER_IS_ENABLED | PARAMETER_IS_AUTOMABLE | PARAMETER_IS_INTEGER;
  750. pData->param.data[j].index = j;
  751. pData->param.data[j].rindex = j;
  752. pData->param.ranges[j].min = 0.0f;
  753. pData->param.ranges[j].max = 65535.0f;
  754. pData->param.ranges[j].def = sFluidDefaults[j];
  755. pData->param.ranges[j].step = 1.0f;
  756. pData->param.ranges[j].stepSmall = 1.0f;
  757. pData->param.ranges[j].stepLarge = 1.0f;
  758. for (j=0; j<FluidSynthParametersMax; ++j)
  759. fParamBuffers[j] = pData->param.ranges[j].def;
  760. }
  761. // ---------------------------------------
  762. // plugin hints
  763. pData->hints = 0x0;
  764. pData->hints |= PLUGIN_IS_SYNTH;
  765. pData->hints |= PLUGIN_CAN_VOLUME;
  766. pData->hints |= PLUGIN_USES_MULTI_PROGS;
  767. if (! kUse16Outs)
  768. pData->hints |= PLUGIN_CAN_BALANCE;
  769. // extra plugin hints
  770. pData->extraHints = 0x0;
  771. pData->extraHints |= PLUGIN_EXTRA_HINT_HAS_MIDI_IN;
  772. bufferSizeChanged(pData->engine->getBufferSize());
  773. reloadPrograms(true);
  774. if (pData->active)
  775. activate();
  776. carla_debug("CarlaPluginFluidSynth::reload() - end");
  777. }
  778. void reloadPrograms(const bool doInit) override
  779. {
  780. carla_debug("CarlaPluginFluidSynth::reloadPrograms(%s)", bool2str(doInit));
  781. // save drum info in case we have one program for it
  782. bool hasDrums = false;
  783. uint32_t drumIndex, drumProg;
  784. drumIndex = drumProg = 0;
  785. // Delete old programs
  786. pData->midiprog.clear();
  787. // Query new programs
  788. uint32_t count = 0;
  789. if (fluid_sfont_t* const f_sfont = fluid_synth_get_sfont_by_id(fSynth, fSynthId))
  790. {
  791. #if FLUIDSYNTH_VERSION_MAJOR < 2
  792. fluid_preset_t f_preset;
  793. // initial check to know how many midi-programs we have
  794. f_sfont->iteration_start(f_sfont);
  795. for (; f_sfont->iteration_next(f_sfont, &f_preset);)
  796. ++count;
  797. // sound kits must always have at least 1 midi-program
  798. CARLA_SAFE_ASSERT_RETURN(count > 0,);
  799. pData->midiprog.createNew(count);
  800. // Update data
  801. int tmp;
  802. uint32_t i = 0;
  803. f_sfont->iteration_start(f_sfont);
  804. for (; f_sfont->iteration_next(f_sfont, &f_preset);)
  805. {
  806. CARLA_SAFE_ASSERT_BREAK(i < count);
  807. tmp = f_preset.get_banknum(&f_preset);
  808. pData->midiprog.data[i].bank = (tmp >= 0) ? static_cast<uint32_t>(tmp) : 0;
  809. tmp = f_preset.get_num(&f_preset);
  810. pData->midiprog.data[i].program = (tmp >= 0) ? static_cast<uint32_t>(tmp) : 0;
  811. pData->midiprog.data[i].name = carla_strdup(f_preset.get_name(&f_preset));
  812. #else
  813. fluid_preset_t* f_preset;
  814. // initial check to know how many midi-programs we have
  815. fluid_sfont_iteration_start(f_sfont);
  816. for (; fluid_sfont_iteration_next(f_sfont);)
  817. ++count;
  818. // sound kits must always have at least 1 midi-program
  819. CARLA_SAFE_ASSERT_RETURN(count > 0,);
  820. pData->midiprog.createNew(count);
  821. // Update data
  822. int tmp;
  823. uint32_t i = 0;
  824. fluid_sfont_iteration_start(f_sfont);
  825. for (; (f_preset = fluid_sfont_iteration_next(f_sfont));)
  826. {
  827. CARLA_SAFE_ASSERT_BREAK(i < count);
  828. tmp = fluid_preset_get_banknum(f_preset);
  829. pData->midiprog.data[i].bank = (tmp >= 0) ? static_cast<uint32_t>(tmp) : 0;
  830. tmp = fluid_preset_get_num(f_preset);
  831. pData->midiprog.data[i].program = (tmp >= 0) ? static_cast<uint32_t>(tmp) : 0;
  832. pData->midiprog.data[i].name = carla_strdup(fluid_preset_get_name(f_preset));
  833. #endif
  834. if (pData->midiprog.data[i].bank == 128 && ! hasDrums)
  835. {
  836. hasDrums = true;
  837. drumIndex = i;
  838. drumProg = pData->midiprog.data[i].program;
  839. }
  840. ++i;
  841. }
  842. }
  843. else
  844. {
  845. // failing means 0 midi-programs, it shouldn't happen!
  846. carla_safe_assert("fluid_sfont_t* const f_sfont = fluid_synth_get_sfont_by_id(fSynth, fSynthId)", __FILE__, __LINE__);
  847. return;
  848. }
  849. if (doInit)
  850. {
  851. fluid_synth_program_reset(fSynth);
  852. // select first program, or 128 for ch10
  853. for (int i=0; i < MAX_MIDI_CHANNELS && i != 9; ++i)
  854. {
  855. fluid_synth_set_channel_type(fSynth, i, CHANNEL_TYPE_MELODIC);
  856. fluid_synth_program_select(fSynth, i, fSynthId, pData->midiprog.data[0].bank, pData->midiprog.data[0].program);
  857. fCurMidiProgs[i] = 0;
  858. }
  859. if (hasDrums)
  860. {
  861. fluid_synth_set_channel_type(fSynth, 9, CHANNEL_TYPE_DRUM);
  862. fluid_synth_program_select(fSynth, 9, fSynthId, 128, drumProg);
  863. fCurMidiProgs[9] = static_cast<int32_t>(drumIndex);
  864. }
  865. else
  866. {
  867. fluid_synth_set_channel_type(fSynth, 9, CHANNEL_TYPE_MELODIC);
  868. fluid_synth_program_select(fSynth, 9, fSynthId, pData->midiprog.data[0].bank, pData->midiprog.data[0].program);
  869. fCurMidiProgs[9] = 0;
  870. }
  871. pData->midiprog.current = 0;
  872. }
  873. else
  874. {
  875. pData->engine->callback(true, true, ENGINE_CALLBACK_RELOAD_PROGRAMS, pData->id, 0, 0, 0, 0.0f, nullptr);
  876. }
  877. }
  878. // -------------------------------------------------------------------
  879. // Plugin processing
  880. void process(const float* const* const, float** const audioOut,
  881. const float* const*, float**, const uint32_t frames) override
  882. {
  883. // --------------------------------------------------------------------------------------------------------
  884. // Check if active
  885. if (! pData->active)
  886. {
  887. // disable any output sound
  888. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  889. carla_zeroFloats(audioOut[i], frames);
  890. return;
  891. }
  892. // --------------------------------------------------------------------------------------------------------
  893. // Check if needs reset
  894. if (pData->needsReset)
  895. {
  896. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  897. {
  898. for (int i=0; i < MAX_MIDI_CHANNELS; ++i)
  899. {
  900. fluid_synth_all_notes_off(fSynth, i);
  901. fluid_synth_all_sounds_off(fSynth, i);
  902. }
  903. }
  904. else if (pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  905. {
  906. for (int i=0; i < MAX_MIDI_NOTE; ++i)
  907. fluid_synth_noteoff(fSynth, pData->ctrlChannel, i);
  908. }
  909. pData->needsReset = false;
  910. }
  911. // --------------------------------------------------------------------------------------------------------
  912. // Event Input and Processing
  913. {
  914. // ----------------------------------------------------------------------------------------------------
  915. // MIDI Input (External)
  916. if (pData->extNotes.mutex.tryLock())
  917. {
  918. for (RtLinkedList<ExternalMidiNote>::Itenerator it = pData->extNotes.data.begin2(); it.valid(); it.next())
  919. {
  920. const ExternalMidiNote& note(it.getValue(kExternalMidiNoteFallback));
  921. CARLA_SAFE_ASSERT_CONTINUE(note.channel >= 0 && note.channel < MAX_MIDI_CHANNELS);
  922. if (note.velo > 0)
  923. fluid_synth_noteon(fSynth, note.channel, note.note, note.velo);
  924. else
  925. fluid_synth_noteoff(fSynth,note.channel, note.note);
  926. }
  927. pData->extNotes.data.clear();
  928. pData->extNotes.mutex.unlock();
  929. } // End of MIDI Input (External)
  930. // ----------------------------------------------------------------------------------------------------
  931. // Event Input (System)
  932. #ifndef BUILD_BRIDGE
  933. bool allNotesOffSent = false;
  934. #endif
  935. uint32_t timeOffset = 0;
  936. uint32_t nextBankIds[MAX_MIDI_CHANNELS] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 0, 0, 0, 0, 0, 0 };
  937. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0 && pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  938. nextBankIds[pData->ctrlChannel] = pData->midiprog.data[pData->midiprog.current].bank;
  939. for (uint32_t i=0, numEvents=pData->event.portIn->getEventCount(); i < numEvents; ++i)
  940. {
  941. const EngineEvent& event(pData->event.portIn->getEvent(i));
  942. uint32_t eventTime = event.time;
  943. CARLA_SAFE_ASSERT_UINT2_CONTINUE(eventTime < frames, eventTime, frames);
  944. if (eventTime < timeOffset)
  945. {
  946. carla_stderr2("Timing error, eventTime:%u < timeOffset:%u for '%s'",
  947. eventTime, timeOffset, pData->name);
  948. eventTime = timeOffset;
  949. }
  950. else if (eventTime > timeOffset)
  951. {
  952. if (processSingle(audioOut, eventTime - timeOffset, timeOffset))
  953. {
  954. timeOffset = eventTime;
  955. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0 && pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  956. nextBankIds[pData->ctrlChannel] = pData->midiprog.data[pData->midiprog.current].bank;
  957. }
  958. }
  959. // Control change
  960. switch (event.type)
  961. {
  962. case kEngineEventTypeNull:
  963. break;
  964. case kEngineEventTypeControl:
  965. {
  966. const EngineControlEvent& ctrlEvent = event.ctrl;
  967. switch (ctrlEvent.type)
  968. {
  969. case kEngineControlEventTypeNull:
  970. break;
  971. case kEngineControlEventTypeParameter:
  972. {
  973. float value;
  974. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  975. // Control backend stuff
  976. if (event.channel == pData->ctrlChannel)
  977. {
  978. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_DRYWET) != 0)
  979. {
  980. value = ctrlEvent.value;
  981. setDryWetRT(value, true);
  982. }
  983. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_VOLUME) != 0)
  984. {
  985. value = ctrlEvent.value*127.0f/100.0f;
  986. setVolumeRT(value, true);
  987. }
  988. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_BALANCE) != 0)
  989. {
  990. float left, right;
  991. value = ctrlEvent.value/0.5f - 1.0f;
  992. if (value < 0.0f)
  993. {
  994. left = -1.0f;
  995. right = (value*2.0f)+1.0f;
  996. }
  997. else if (value > 0.0f)
  998. {
  999. left = (value*2.0f)-1.0f;
  1000. right = 1.0f;
  1001. }
  1002. else
  1003. {
  1004. left = -1.0f;
  1005. right = 1.0f;
  1006. }
  1007. setBalanceLeftRT(left, true);
  1008. setBalanceRightRT(right, true);
  1009. }
  1010. }
  1011. #endif
  1012. // Control plugin parameters
  1013. for (uint32_t k=0; k < pData->param.count; ++k)
  1014. {
  1015. if (pData->param.data[k].midiChannel != event.channel)
  1016. continue;
  1017. if (pData->param.data[k].mappedControlIndex != ctrlEvent.param)
  1018. continue;
  1019. if (pData->param.data[k].hints != PARAMETER_INPUT)
  1020. continue;
  1021. if ((pData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  1022. continue;
  1023. value = pData->param.getFinalUnnormalizedValue(k, ctrlEvent.value);
  1024. setParameterValueRT(k, value, true);
  1025. }
  1026. if ((pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0 && ctrlEvent.param < MAX_MIDI_VALUE)
  1027. {
  1028. fluid_synth_cc(fSynth, event.channel, ctrlEvent.param, int(ctrlEvent.value*127.0f));
  1029. }
  1030. break;
  1031. }
  1032. case kEngineControlEventTypeMidiBank:
  1033. if (event.channel < MAX_MIDI_CHANNELS && (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  1034. nextBankIds[event.channel] = ctrlEvent.param;
  1035. break;
  1036. case kEngineControlEventTypeMidiProgram:
  1037. if (event.channel < MAX_MIDI_CHANNELS && (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  1038. {
  1039. const uint32_t bankId(nextBankIds[event.channel]);
  1040. const uint32_t progId(ctrlEvent.param);
  1041. // TODO int32_t midiprog.find(bank, prog)
  1042. for (uint32_t k=0; k < pData->midiprog.count; ++k)
  1043. {
  1044. if (pData->midiprog.data[k].bank == bankId && pData->midiprog.data[k].program == progId)
  1045. {
  1046. fluid_synth_program_select(fSynth, event.channel, fSynthId, bankId, progId);
  1047. fCurMidiProgs[event.channel] = static_cast<int32_t>(k);
  1048. if (event.channel == pData->ctrlChannel)
  1049. {
  1050. pData->postponeRtEvent(kPluginPostRtEventMidiProgramChange,
  1051. true,
  1052. static_cast<int32_t>(k),
  1053. 0, 0, 0.0f);
  1054. }
  1055. break;
  1056. }
  1057. }
  1058. }
  1059. break;
  1060. case kEngineControlEventTypeAllSoundOff:
  1061. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1062. fluid_synth_all_sounds_off(fSynth, event.channel);
  1063. break;
  1064. case kEngineControlEventTypeAllNotesOff:
  1065. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1066. {
  1067. #ifndef BUILD_BRIDGE
  1068. if (event.channel == pData->ctrlChannel && ! allNotesOffSent)
  1069. {
  1070. allNotesOffSent = true;
  1071. postponeRtAllNotesOff();
  1072. }
  1073. #endif
  1074. fluid_synth_all_notes_off(fSynth, event.channel);
  1075. }
  1076. break;
  1077. }
  1078. break;
  1079. }
  1080. case kEngineEventTypeMidi: {
  1081. const EngineMidiEvent& midiEvent(event.midi);
  1082. if (midiEvent.size > EngineMidiEvent::kDataSize)
  1083. continue;
  1084. uint8_t status = uint8_t(MIDI_GET_STATUS_FROM_DATA(midiEvent.data));
  1085. // Fix bad note-off
  1086. if (status == MIDI_STATUS_NOTE_ON && midiEvent.data[2] == 0)
  1087. status = MIDI_STATUS_NOTE_OFF;
  1088. switch (status)
  1089. {
  1090. case MIDI_STATUS_NOTE_OFF: {
  1091. const uint8_t note = midiEvent.data[1];
  1092. fluid_synth_noteoff(fSynth, event.channel, note);
  1093. pData->postponeRtEvent(kPluginPostRtEventNoteOff, true, event.channel, note, 0, 0.0f);
  1094. break;
  1095. }
  1096. case MIDI_STATUS_NOTE_ON: {
  1097. const uint8_t note = midiEvent.data[1];
  1098. const uint8_t velo = midiEvent.data[2];
  1099. fluid_synth_noteon(fSynth, event.channel, note, velo);
  1100. pData->postponeRtEvent(kPluginPostRtEventNoteOn, true, event.channel, note, velo, 0.0f);
  1101. break;
  1102. }
  1103. case MIDI_STATUS_POLYPHONIC_AFTERTOUCH:
  1104. if (pData->options & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH)
  1105. {
  1106. //const uint8_t note = midiEvent.data[1];
  1107. //const uint8_t pressure = midiEvent.data[2];
  1108. // not in fluidsynth API
  1109. }
  1110. break;
  1111. case MIDI_STATUS_CONTROL_CHANGE:
  1112. if (pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES)
  1113. {
  1114. const uint8_t control = midiEvent.data[1];
  1115. const uint8_t value = midiEvent.data[2];
  1116. fluid_synth_cc(fSynth, event.channel, control, value);
  1117. }
  1118. break;
  1119. case MIDI_STATUS_CHANNEL_PRESSURE:
  1120. if (pData->options & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE)
  1121. {
  1122. const uint8_t pressure = midiEvent.data[1];
  1123. fluid_synth_channel_pressure(fSynth, event.channel, pressure);;
  1124. }
  1125. break;
  1126. case MIDI_STATUS_PITCH_WHEEL_CONTROL:
  1127. if (pData->options & PLUGIN_OPTION_SEND_PITCHBEND)
  1128. {
  1129. const uint8_t lsb = midiEvent.data[1];
  1130. const uint8_t msb = midiEvent.data[2];
  1131. const int value = ((msb << 7) | lsb);
  1132. fluid_synth_pitch_bend(fSynth, event.channel, value);
  1133. }
  1134. break;
  1135. default:
  1136. continue;
  1137. break;
  1138. } // switch (status)
  1139. } break;
  1140. } // switch (event.type)
  1141. }
  1142. pData->postRtEvents.trySplice();
  1143. if (frames > timeOffset)
  1144. processSingle(audioOut, frames - timeOffset, timeOffset);
  1145. } // End of Event Input and Processing
  1146. #ifndef BUILD_BRIDGE
  1147. // --------------------------------------------------------------------------------------------------------
  1148. // Control Output
  1149. {
  1150. uint32_t k = FluidSynthVoiceCount;
  1151. fParamBuffers[k] = float(fluid_synth_get_active_voice_count(fSynth));
  1152. pData->param.ranges[k].fixValue(fParamBuffers[k]);
  1153. if (pData->param.data[k].mappedControlIndex > 0)
  1154. {
  1155. float value(pData->param.ranges[k].getNormalizedValue(fParamBuffers[k]));
  1156. pData->event.portOut->writeControlEvent(0,
  1157. pData->param.data[k].midiChannel,
  1158. kEngineControlEventTypeParameter,
  1159. static_cast<uint16_t>(pData->param.data[k].mappedControlIndex),
  1160. value);
  1161. }
  1162. } // End of Control Output
  1163. #endif
  1164. }
  1165. bool processSingle(float** const outBuffer, const uint32_t frames, const uint32_t timeOffset)
  1166. {
  1167. CARLA_SAFE_ASSERT_RETURN(outBuffer != nullptr, false);
  1168. CARLA_SAFE_ASSERT_RETURN(frames > 0, false);
  1169. // --------------------------------------------------------------------------------------------------------
  1170. // Try lock, silence otherwise
  1171. #ifndef STOAT_TEST_BUILD
  1172. if (pData->engine->isOffline())
  1173. {
  1174. pData->singleMutex.lock();
  1175. }
  1176. else
  1177. #endif
  1178. if (! pData->singleMutex.tryLock())
  1179. {
  1180. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1181. {
  1182. for (uint32_t k=0; k < frames; ++k)
  1183. outBuffer[i][k+timeOffset] = 0.0f;
  1184. }
  1185. return false;
  1186. }
  1187. // --------------------------------------------------------------------------------------------------------
  1188. // Fill plugin buffers and Run plugin
  1189. if (kUse16Outs)
  1190. {
  1191. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1192. carla_zeroFloats(fAudio16Buffers[i], frames);
  1193. // FIXME use '32' or '16' instead of outs
  1194. fluid_synth_process(fSynth, static_cast<int>(frames),
  1195. 0, nullptr,
  1196. static_cast<int>(pData->audioOut.count), fAudio16Buffers);
  1197. }
  1198. else
  1199. {
  1200. fluid_synth_write_float(fSynth, static_cast<int>(frames),
  1201. outBuffer[0] + timeOffset, 0, 1,
  1202. outBuffer[1] + timeOffset, 0, 1);
  1203. }
  1204. #ifndef BUILD_BRIDGE
  1205. // --------------------------------------------------------------------------------------------------------
  1206. // Post-processing (volume and balance)
  1207. {
  1208. // note - balance not possible with kUse16Outs, so we can safely skip fAudioOutBuffers
  1209. const bool doVolume = (pData->hints & PLUGIN_CAN_VOLUME) != 0 && carla_isNotEqual(pData->postProc.volume, 1.0f);
  1210. const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) != 0 && ! (carla_isEqual(pData->postProc.balanceLeft, -1.0f) && carla_isEqual(pData->postProc.balanceRight, 1.0f));
  1211. float oldBufLeft[doBalance ? frames : 1];
  1212. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1213. {
  1214. // Balance
  1215. if (doBalance)
  1216. {
  1217. if (i % 2 == 0)
  1218. carla_copyFloats(oldBufLeft, outBuffer[i]+timeOffset, frames);
  1219. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  1220. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  1221. for (uint32_t k=0; k < frames; ++k)
  1222. {
  1223. if (i % 2 == 0)
  1224. {
  1225. // left
  1226. outBuffer[i][k+timeOffset] = oldBufLeft[k] * (1.0f - balRangeL);
  1227. outBuffer[i][k+timeOffset] += outBuffer[i+1][k+timeOffset] * (1.0f - balRangeR);
  1228. }
  1229. else
  1230. {
  1231. // right
  1232. outBuffer[i][k+timeOffset] = outBuffer[i][k+timeOffset] * balRangeR;
  1233. outBuffer[i][k+timeOffset] += oldBufLeft[k] * balRangeL;
  1234. }
  1235. }
  1236. }
  1237. // Volume
  1238. if (kUse16Outs)
  1239. {
  1240. for (uint32_t k=0; k < frames; ++k)
  1241. outBuffer[i][k+timeOffset] = fAudio16Buffers[i][k] * pData->postProc.volume;
  1242. }
  1243. else if (doVolume)
  1244. {
  1245. for (uint32_t k=0; k < frames; ++k)
  1246. outBuffer[i][k+timeOffset] *= pData->postProc.volume;
  1247. }
  1248. }
  1249. } // End of Post-processing
  1250. #else
  1251. if (kUse16Outs)
  1252. {
  1253. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1254. {
  1255. for (uint32_t k=0; k < frames; ++k)
  1256. outBuffer[i][k+timeOffset] = fAudio16Buffers[i][k];
  1257. }
  1258. }
  1259. #endif
  1260. // --------------------------------------------------------------------------------------------------------
  1261. pData->singleMutex.unlock();
  1262. return true;
  1263. }
  1264. void bufferSizeChanged(const uint32_t newBufferSize) override
  1265. {
  1266. if (! kUse16Outs)
  1267. return;
  1268. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1269. {
  1270. if (fAudio16Buffers[i] != nullptr)
  1271. delete[] fAudio16Buffers[i];
  1272. fAudio16Buffers[i] = new float[newBufferSize];
  1273. }
  1274. }
  1275. void sampleRateChanged(const double newSampleRate) override
  1276. {
  1277. CARLA_SAFE_ASSERT_RETURN(fSettings != nullptr,);
  1278. fluid_settings_setnum(fSettings, "synth.sample-rate", newSampleRate);
  1279. CARLA_SAFE_ASSERT_RETURN(fSynth != nullptr,);
  1280. fluid_synth_set_sample_rate(fSynth, float(newSampleRate));
  1281. }
  1282. // -------------------------------------------------------------------
  1283. // Plugin buffers
  1284. void clearBuffers() noexcept override
  1285. {
  1286. carla_debug("CarlaPluginFluidSynth::clearBuffers() - start");
  1287. if (fAudio16Buffers != nullptr)
  1288. {
  1289. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1290. {
  1291. if (fAudio16Buffers[i] != nullptr)
  1292. {
  1293. delete[] fAudio16Buffers[i];
  1294. fAudio16Buffers[i] = nullptr;
  1295. }
  1296. }
  1297. delete[] fAudio16Buffers;
  1298. fAudio16Buffers = nullptr;
  1299. }
  1300. CarlaPlugin::clearBuffers();
  1301. carla_debug("CarlaPluginFluidSynth::clearBuffers() - end");
  1302. }
  1303. // -------------------------------------------------------------------
  1304. const void* getExtraStuff() const noexcept override
  1305. {
  1306. static const char xtrue[] = "true";
  1307. static const char xfalse[] = "false";
  1308. return kUse16Outs ? xtrue : xfalse;
  1309. }
  1310. // -------------------------------------------------------------------
  1311. bool init(const char* const filename, const char* const name, const char* const label, const uint options)
  1312. {
  1313. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  1314. // ---------------------------------------------------------------
  1315. // first checks
  1316. if (pData->client != nullptr)
  1317. {
  1318. pData->engine->setLastError("Plugin client is already registered");
  1319. return false;
  1320. }
  1321. if (fSynth == nullptr)
  1322. {
  1323. pData->engine->setLastError("null synth");
  1324. return false;
  1325. }
  1326. if (filename == nullptr || filename[0] == '\0')
  1327. {
  1328. pData->engine->setLastError("null filename");
  1329. return false;
  1330. }
  1331. if (label == nullptr || label[0] == '\0')
  1332. {
  1333. pData->engine->setLastError("null label");
  1334. return false;
  1335. }
  1336. // ---------------------------------------------------------------
  1337. // open soundfont
  1338. const int synthId(fluid_synth_sfload(fSynth, filename, 0));
  1339. if (synthId < 0)
  1340. {
  1341. pData->engine->setLastError("Failed to load SoundFont file");
  1342. return false;
  1343. }
  1344. fSynthId = static_cast<uint>(synthId);
  1345. // ---------------------------------------------------------------
  1346. // get info
  1347. CarlaString label2(label);
  1348. if (kUse16Outs && ! label2.endsWith(" (16 outs)"))
  1349. label2 += " (16 outs)";
  1350. fLabel = label2.dup();
  1351. pData->filename = carla_strdup(filename);
  1352. if (name != nullptr && name[0] != '\0')
  1353. pData->name = pData->engine->getUniquePluginName(name);
  1354. else
  1355. pData->name = pData->engine->getUniquePluginName(label);
  1356. // ---------------------------------------------------------------
  1357. // register client
  1358. pData->client = pData->engine->addClient(this);
  1359. if (pData->client == nullptr || ! pData->client->isOk())
  1360. {
  1361. pData->engine->setLastError("Failed to register plugin client");
  1362. return false;
  1363. }
  1364. // ---------------------------------------------------------------
  1365. // set options
  1366. pData->options = 0x0;
  1367. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_CONTROL_CHANGES))
  1368. pData->options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  1369. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_CHANNEL_PRESSURE))
  1370. pData->options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  1371. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_PITCHBEND))
  1372. pData->options |= PLUGIN_OPTION_SEND_PITCHBEND;
  1373. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_ALL_SOUND_OFF))
  1374. pData->options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  1375. if (isPluginOptionEnabled(options, PLUGIN_OPTION_MAP_PROGRAM_CHANGES))
  1376. pData->options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  1377. return true;
  1378. }
  1379. private:
  1380. void initializeFluidDefaultsIfNeeded()
  1381. {
  1382. if (sFluidDefaultsStored)
  1383. return;
  1384. sFluidDefaultsStored = true;
  1385. // reverb defaults
  1386. sFluidDefaults[FluidSynthReverbOnOff] = 1.0f;
  1387. #if FLUIDSYNTH_VERSION_MAJOR < 2
  1388. sFluidDefaults[FluidSynthReverbRoomSize] = FLUID_REVERB_DEFAULT_ROOMSIZE;
  1389. sFluidDefaults[FluidSynthReverbDamp] = FLUID_REVERB_DEFAULT_DAMP;
  1390. sFluidDefaults[FluidSynthReverbLevel] = FLUID_REVERB_DEFAULT_LEVEL;
  1391. sFluidDefaults[FluidSynthReverbWidth] = FLUID_REVERB_DEFAULT_WIDTH;
  1392. #else
  1393. double reverbVal;
  1394. fluid_settings_getnum_default(fSettings, "synth.reverb.room-size", &reverbVal);
  1395. sFluidDefaults[FluidSynthReverbRoomSize] = reverbVal;
  1396. fluid_settings_getnum_default(fSettings, "synth.reverb.damp", &reverbVal);
  1397. sFluidDefaults[FluidSynthReverbDamp] = reverbVal;
  1398. fluid_settings_getnum_default(fSettings, "synth.reverb.level", &reverbVal);
  1399. sFluidDefaults[FluidSynthReverbLevel] = reverbVal;
  1400. fluid_settings_getnum_default(fSettings, "synth.reverb.width", &reverbVal);
  1401. sFluidDefaults[FluidSynthReverbWidth] = reverbVal;
  1402. #endif
  1403. // chorus defaults
  1404. sFluidDefaults[FluidSynthChorusOnOff] = 1.0f;
  1405. #if FLUIDSYNTH_VERSION_MAJOR < 2
  1406. sFluidDefaults[FluidSynthChorusNr] = FLUID_CHORUS_DEFAULT_N;
  1407. sFluidDefaults[FluidSynthChorusLevel] = FLUID_CHORUS_DEFAULT_LEVEL;
  1408. sFluidDefaults[FluidSynthChorusSpeedHz] = FLUID_CHORUS_DEFAULT_SPEED;
  1409. sFluidDefaults[FluidSynthChorusDepthMs] = FLUID_CHORUS_DEFAULT_DEPTH;
  1410. sFluidDefaults[FluidSynthChorusType] = FLUID_CHORUS_DEFAULT_TYPE;
  1411. #else
  1412. double chorusVal;
  1413. fluid_settings_getnum_default(fSettings, "synth.chorus.nr", &chorusVal);
  1414. sFluidDefaults[FluidSynthChorusNr] = chorusVal;
  1415. fluid_settings_getnum_default(fSettings, "synth.chorus.level", &chorusVal);
  1416. sFluidDefaults[FluidSynthChorusLevel] = chorusVal;
  1417. fluid_settings_getnum_default(fSettings, "synth.chorus.speed", &chorusVal);
  1418. sFluidDefaults[FluidSynthChorusSpeedHz] = chorusVal;
  1419. fluid_settings_getnum_default(fSettings, "synth.chorus.depth", &chorusVal);
  1420. sFluidDefaults[FluidSynthChorusDepthMs] = chorusVal;
  1421. // There is no settings for chorus default type
  1422. sFluidDefaults[FluidSynthChorusType] = (float)fluid_synth_get_chorus_type(fSynth);
  1423. #endif
  1424. // misc. defaults
  1425. sFluidDefaults[FluidSynthInterpolation] = FLUID_INTERP_DEFAULT;
  1426. }
  1427. enum FluidSynthParameters {
  1428. FluidSynthReverbOnOff = 0,
  1429. FluidSynthReverbRoomSize = 1,
  1430. FluidSynthReverbDamp = 2,
  1431. FluidSynthReverbLevel = 3,
  1432. FluidSynthReverbWidth = 4,
  1433. FluidSynthChorusOnOff = 5,
  1434. FluidSynthChorusNr = 6,
  1435. FluidSynthChorusLevel = 7,
  1436. FluidSynthChorusSpeedHz = 8,
  1437. FluidSynthChorusDepthMs = 9,
  1438. FluidSynthChorusType = 10,
  1439. FluidSynthPolyphony = 11,
  1440. FluidSynthInterpolation = 12,
  1441. FluidSynthVoiceCount = 13,
  1442. FluidSynthParametersMax = 14
  1443. };
  1444. const bool kUse16Outs;
  1445. fluid_settings_t* fSettings;
  1446. fluid_synth_t* fSynth;
  1447. uint fSynthId;
  1448. float** fAudio16Buffers;
  1449. float fParamBuffers[FluidSynthParametersMax];
  1450. static bool sFluidDefaultsStored;
  1451. static float sFluidDefaults[FluidSynthParametersMax];
  1452. int32_t fCurMidiProgs[MAX_MIDI_CHANNELS];
  1453. const char* fLabel;
  1454. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaPluginFluidSynth)
  1455. };
  1456. bool CarlaPluginFluidSynth::sFluidDefaultsStored = false;
  1457. float CarlaPluginFluidSynth::sFluidDefaults[FluidSynthParametersMax] = {
  1458. 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
  1459. };
  1460. CARLA_BACKEND_END_NAMESPACE
  1461. #endif // HAVE_FLUIDSYNTH
  1462. CARLA_BACKEND_START_NAMESPACE
  1463. // -------------------------------------------------------------------------------------------------------------------
  1464. CarlaPlugin* CarlaPlugin::newFluidSynth(const Initializer& init, const bool use16Outs)
  1465. {
  1466. carla_debug("CarlaPlugin::newFluidSynth({%p, \"%s\", \"%s\", \"%s\", " P_INT64 "}, %s)", init.engine, init.filename, init.name, init.label, init.uniqueId, bool2str(use16Outs));
  1467. #ifdef HAVE_FLUIDSYNTH
  1468. if (init.engine->getProccessMode() == ENGINE_PROCESS_MODE_CONTINUOUS_RACK && use16Outs)
  1469. {
  1470. init.engine->setLastError("Carla's rack mode can only work with Stereo modules,"
  1471. "please choose the 2-channel only SoundFont version");
  1472. return nullptr;
  1473. }
  1474. if (! fluid_is_soundfont(init.filename))
  1475. {
  1476. init.engine->setLastError("Requested file is not a valid SoundFont");
  1477. return nullptr;
  1478. }
  1479. CarlaPluginFluidSynth* const plugin(new CarlaPluginFluidSynth(init.engine, init.id, use16Outs));
  1480. if (! plugin->init(init.filename, init.name, init.label, init.options))
  1481. {
  1482. delete plugin;
  1483. return nullptr;
  1484. }
  1485. return plugin;
  1486. #else
  1487. init.engine->setLastError("fluidsynth support not available");
  1488. return nullptr;
  1489. // unused
  1490. (void)use16Outs;
  1491. #endif
  1492. }
  1493. // -------------------------------------------------------------------------------------------------------------------
  1494. CARLA_BACKEND_END_NAMESPACE