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.

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