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.

1726 lines
63KB

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