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.

1757 lines
64KB

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