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.

1738 lines
64KB

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