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.

1943 lines
72KB

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