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.

1769 lines
65KB

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