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.

1706 lines
62KB

  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 FluidSynthPlugin : public CarlaPlugin
  32. {
  33. public:
  34. FluidSynthPlugin(CarlaEngine* const engine, const uint id, const bool use16Outs)
  35. : CarlaPlugin(engine, id),
  36. kUse16Outs(use16Outs),
  37. fSettings(nullptr),
  38. fSynth(nullptr),
  39. fSynthId(0),
  40. fAudio16Buffers(nullptr),
  41. fLabel(nullptr)
  42. {
  43. carla_debug("FluidSynthPlugin::FluidSynthPlugin(%p, %i, %s)", engine, id, bool2str(use16Outs));
  44. FloatVectorOperations::clear(fParamBuffers, FluidSynthParametersMax);
  45. carla_fill<int32_t>(fCurMidiProgs, 0, MAX_MIDI_CHANNELS);
  46. // create settings
  47. fSettings = new_fluid_settings();
  48. CARLA_SAFE_ASSERT_RETURN(fSettings != nullptr,);
  49. // define settings
  50. fluid_settings_setint(fSettings, "synth.audio-channels", use16Outs ? 16 : 1);
  51. fluid_settings_setint(fSettings, "synth.audio-groups", use16Outs ? 16 : 1);
  52. fluid_settings_setnum(fSettings, "synth.sample-rate", pData->engine->getSampleRate());
  53. //fluid_settings_setnum(fSettings, "synth.cpu-cores", 2);
  54. fluid_settings_setint(fSettings, "synth.parallel-render", 1);
  55. fluid_settings_setint(fSettings, "synth.threadsafe-api", 0);
  56. // create synth
  57. fSynth = new_fluid_synth(fSettings);
  58. CARLA_SAFE_ASSERT_RETURN(fSynth != nullptr,);
  59. #ifdef FLUIDSYNTH_VERSION_NEW_API
  60. fluid_synth_set_sample_rate(fSynth, (float)pData->engine->getSampleRate());
  61. #endif
  62. // set default values
  63. fluid_synth_set_reverb_on(fSynth, 1);
  64. fluid_synth_set_reverb(fSynth, FLUID_REVERB_DEFAULT_ROOMSIZE, FLUID_REVERB_DEFAULT_DAMP, FLUID_REVERB_DEFAULT_WIDTH, FLUID_REVERB_DEFAULT_LEVEL);
  65. fluid_synth_set_chorus_on(fSynth, 1);
  66. 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);
  67. fluid_synth_set_polyphony(fSynth, FLUID_DEFAULT_POLYPHONY);
  68. fluid_synth_set_gain(fSynth, 1.0f);
  69. for (int i=0; i < MAX_MIDI_CHANNELS; ++i)
  70. fluid_synth_set_interp_method(fSynth, i, FLUID_INTERP_DEFAULT);
  71. }
  72. ~FluidSynthPlugin() override
  73. {
  74. carla_debug("FluidSynthPlugin::~FluidSynthPlugin()");
  75. pData->singleMutex.lock();
  76. pData->masterMutex.lock();
  77. if (pData->client != nullptr && pData->client->isActive())
  78. pData->client->deactivate();
  79. if (pData->active)
  80. {
  81. deactivate();
  82. pData->active = false;
  83. }
  84. if (fSynth != nullptr)
  85. {
  86. delete_fluid_synth(fSynth);
  87. fSynth = nullptr;
  88. }
  89. if (fSettings != nullptr)
  90. {
  91. delete_fluid_settings(fSettings);
  92. fSettings = nullptr;
  93. }
  94. if (fLabel != nullptr)
  95. {
  96. delete[] fLabel;
  97. fLabel = nullptr;
  98. }
  99. clearBuffers();
  100. }
  101. // -------------------------------------------------------------------
  102. // Information (base)
  103. PluginType getType() const noexcept override
  104. {
  105. return PLUGIN_SF2;
  106. }
  107. PluginCategory getCategory() const noexcept override
  108. {
  109. return PLUGIN_CATEGORY_SYNTH;
  110. }
  111. // -------------------------------------------------------------------
  112. // Information (count)
  113. uint32_t getParameterScalePointCount(const uint32_t parameterId) const noexcept override
  114. {
  115. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0);
  116. switch (parameterId)
  117. {
  118. case FluidSynthChorusType:
  119. return 2;
  120. case FluidSynthInterpolation:
  121. return 4;
  122. default:
  123. return 0;
  124. }
  125. }
  126. // -------------------------------------------------------------------
  127. // Information (current data)
  128. // nothing
  129. // -------------------------------------------------------------------
  130. // Information (per-plugin data)
  131. uint getOptionsAvailable() const noexcept override
  132. {
  133. uint options = 0x0;
  134. options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  135. options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  136. options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  137. options |= PLUGIN_OPTION_SEND_PITCHBEND;
  138. options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  139. return options;
  140. }
  141. float getParameterValue(const uint32_t parameterId) const noexcept override
  142. {
  143. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0.0f);
  144. return fParamBuffers[parameterId];
  145. }
  146. float getParameterScalePointValue(const uint32_t parameterId, const uint32_t scalePointId) const noexcept override
  147. {
  148. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0.0f);
  149. CARLA_SAFE_ASSERT_RETURN(scalePointId < getParameterScalePointCount(parameterId), 0.0f);
  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. const float fixedValue(pData->param.getFixedValue(parameterId, value));
  325. fParamBuffers[parameterId] = fixedValue;
  326. {
  327. const ScopedSingleProcessLocker spl(this, (sendGui || sendOsc || sendCallback));
  328. switch (parameterId)
  329. {
  330. case FluidSynthReverbOnOff:
  331. try {
  332. fluid_synth_set_reverb_on(fSynth, (fixedValue > 0.5f) ? 1 : 0);
  333. } catch(...) {}
  334. break;
  335. case FluidSynthReverbRoomSize:
  336. case FluidSynthReverbDamp:
  337. case FluidSynthReverbLevel:
  338. case FluidSynthReverbWidth:
  339. try {
  340. fluid_synth_set_reverb(fSynth, fParamBuffers[FluidSynthReverbRoomSize], fParamBuffers[FluidSynthReverbDamp], fParamBuffers[FluidSynthReverbWidth], fParamBuffers[FluidSynthReverbLevel]);
  341. } catch(...) {}
  342. break;
  343. case FluidSynthChorusOnOff:
  344. try {
  345. fluid_synth_set_chorus_on(fSynth, (value > 0.5f) ? 1 : 0);
  346. } catch(...) {}
  347. break;
  348. case FluidSynthChorusNr:
  349. case FluidSynthChorusLevel:
  350. case FluidSynthChorusSpeedHz:
  351. case FluidSynthChorusDepthMs:
  352. case FluidSynthChorusType:
  353. try {
  354. fluid_synth_set_chorus(fSynth, (int)fParamBuffers[FluidSynthChorusNr], fParamBuffers[FluidSynthChorusLevel], fParamBuffers[FluidSynthChorusSpeedHz], fParamBuffers[FluidSynthChorusDepthMs], (int)fParamBuffers[FluidSynthChorusType]);
  355. } catch(...) {}
  356. break;
  357. case FluidSynthPolyphony:
  358. try {
  359. fluid_synth_set_polyphony(fSynth, (int)value);
  360. } catch(...) {}
  361. break;
  362. case FluidSynthInterpolation:
  363. for (int i=0; i < MAX_MIDI_CHANNELS; ++i)
  364. {
  365. try {
  366. fluid_synth_set_interp_method(fSynth, i, (int)value);
  367. }
  368. catch(...) {
  369. break;
  370. }
  371. }
  372. break;
  373. default:
  374. break;
  375. }
  376. }
  377. CarlaPlugin::setParameterValue(parameterId, value, sendGui, sendOsc, sendCallback);
  378. }
  379. void setCustomData(const char* const type, const char* const key, const char* const value, const bool sendGui) override
  380. {
  381. CARLA_SAFE_ASSERT_RETURN(fSynth != nullptr,);
  382. CARLA_SAFE_ASSERT_RETURN(type != nullptr && type[0] != '\0',);
  383. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  384. CARLA_SAFE_ASSERT_RETURN(value != nullptr && value[0] != '\0',);
  385. carla_debug("FluidSynthPlugin::setCustomData(%s, \"%s\", \"%s\", %s)", type, key, value, bool2str(sendGui));
  386. if (std::strcmp(type, CUSTOM_DATA_TYPE_STRING) != 0)
  387. return carla_stderr2("FluidSynthPlugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - type is not string", type, key, value, bool2str(sendGui));
  388. if (std::strcmp(key, "midiPrograms") != 0)
  389. return carla_stderr2("FluidSynthPlugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - type is not string", type, key, value, bool2str(sendGui));
  390. StringArray midiProgramList(StringArray::fromTokens(value, ":", ""));
  391. if (midiProgramList.size() == MAX_MIDI_CHANNELS)
  392. {
  393. uint8_t channel = 0;
  394. for (String *it=midiProgramList.begin(), *end=midiProgramList.end(); it != end; ++it)
  395. {
  396. const int index(it->getIntValue());
  397. if (index >= 0 && index < static_cast<int>(pData->midiprog.count))
  398. {
  399. const uint32_t bank = pData->midiprog.data[index].bank;
  400. const uint32_t program = pData->midiprog.data[index].program;
  401. fluid_synth_program_select(fSynth, channel, fSynthId, bank, program);
  402. fCurMidiProgs[channel] = index;
  403. if (pData->ctrlChannel == static_cast<int32_t>(channel))
  404. {
  405. pData->midiprog.current = index;
  406. pData->engine->callback(ENGINE_CALLBACK_MIDI_PROGRAM_CHANGED, pData->id, index, 0, 0.0f, nullptr);
  407. }
  408. }
  409. ++channel;
  410. }
  411. CARLA_SAFE_ASSERT(channel == MAX_MIDI_CHANNELS);
  412. }
  413. CarlaPlugin::setCustomData(type, key, value, sendGui);
  414. }
  415. void setMidiProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  416. {
  417. CARLA_SAFE_ASSERT_RETURN(fSynth != nullptr,);
  418. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->midiprog.count),);
  419. if (index >= 0 && pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  420. {
  421. const uint32_t bank = pData->midiprog.data[index].bank;
  422. const uint32_t program = pData->midiprog.data[index].program;
  423. //const ScopedSingleProcessLocker spl(this, (sendGui || sendOsc || sendCallback));
  424. try {
  425. fluid_synth_program_select(fSynth, pData->ctrlChannel, fSynthId, bank, program);
  426. } catch(...) {}
  427. fCurMidiProgs[pData->ctrlChannel] = index;
  428. }
  429. CarlaPlugin::setMidiProgram(index, sendGui, sendOsc, sendCallback);
  430. }
  431. // -------------------------------------------------------------------
  432. // Set ui stuff
  433. // nothing
  434. // -------------------------------------------------------------------
  435. // Plugin state
  436. void reload() override
  437. {
  438. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr,);
  439. CARLA_SAFE_ASSERT_RETURN(fSynth != nullptr,);
  440. carla_debug("FluidSynthPlugin::reload() - start");
  441. const EngineProcessMode processMode(pData->engine->getProccessMode());
  442. // Safely disable plugin for reload
  443. const ScopedDisabler sd(this);
  444. if (pData->active)
  445. deactivate();
  446. clearBuffers();
  447. uint32_t aOuts, params;
  448. aOuts = kUse16Outs ? 32 : 2;
  449. params = FluidSynthParametersMax;
  450. pData->audioOut.createNew(aOuts);
  451. pData->param.createNew(params, false);
  452. const uint portNameSize(pData->engine->getMaxPortNameSize());
  453. CarlaString portName;
  454. // ---------------------------------------
  455. // Audio Outputs
  456. if (kUse16Outs)
  457. {
  458. for (uint32_t i=0; i < 32; ++i)
  459. {
  460. portName.clear();
  461. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  462. {
  463. portName = pData->name;
  464. portName += ":";
  465. }
  466. portName += "out-";
  467. if ((i+2)/2 < 9)
  468. portName += "0";
  469. portName += CarlaString((i+2)/2);
  470. if (i % 2 == 0)
  471. portName += "L";
  472. else
  473. portName += "R";
  474. portName.truncate(portNameSize);
  475. pData->audioOut.ports[i].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  476. pData->audioOut.ports[i].rindex = i;
  477. }
  478. fAudio16Buffers = new float*[aOuts];
  479. for (uint32_t i=0; i < aOuts; ++i)
  480. fAudio16Buffers[i] = nullptr;
  481. }
  482. else
  483. {
  484. // out-left
  485. portName.clear();
  486. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  487. {
  488. portName = pData->name;
  489. portName += ":";
  490. }
  491. portName += "out-left";
  492. portName.truncate(portNameSize);
  493. pData->audioOut.ports[0].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  494. pData->audioOut.ports[0].rindex = 0;
  495. // out-right
  496. portName.clear();
  497. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  498. {
  499. portName = pData->name;
  500. portName += ":";
  501. }
  502. portName += "out-right";
  503. portName.truncate(portNameSize);
  504. pData->audioOut.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  505. pData->audioOut.ports[1].rindex = 1;
  506. }
  507. // ---------------------------------------
  508. // Event Input
  509. {
  510. portName.clear();
  511. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  512. {
  513. portName = pData->name;
  514. portName += ":";
  515. }
  516. portName += "events-in";
  517. portName.truncate(portNameSize);
  518. pData->event.portIn = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true);
  519. }
  520. // ---------------------------------------
  521. // Event Output
  522. {
  523. portName.clear();
  524. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  525. {
  526. portName = pData->name;
  527. portName += ":";
  528. }
  529. portName += "events-out";
  530. portName.truncate(portNameSize);
  531. pData->event.portOut = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false);
  532. }
  533. // ---------------------------------------
  534. // Parameters
  535. {
  536. int j;
  537. // ----------------------
  538. j = FluidSynthReverbOnOff;
  539. pData->param.data[j].type = PARAMETER_INPUT;
  540. pData->param.data[j].hints = PARAMETER_IS_ENABLED /*| PARAMETER_IS_AUTOMABLE*/ | PARAMETER_IS_BOOLEAN;
  541. pData->param.data[j].index = j;
  542. pData->param.data[j].rindex = j;
  543. pData->param.ranges[j].min = 0.0f;
  544. pData->param.ranges[j].max = 1.0f;
  545. pData->param.ranges[j].def = 1.0f;
  546. pData->param.ranges[j].step = 1.0f;
  547. pData->param.ranges[j].stepSmall = 1.0f;
  548. pData->param.ranges[j].stepLarge = 1.0f;
  549. // ----------------------
  550. j = FluidSynthReverbRoomSize;
  551. pData->param.data[j].type = PARAMETER_INPUT;
  552. pData->param.data[j].hints = PARAMETER_IS_ENABLED /*| PARAMETER_IS_AUTOMABLE*/;
  553. pData->param.data[j].index = j;
  554. pData->param.data[j].rindex = j;
  555. pData->param.ranges[j].min = 0.0f;
  556. pData->param.ranges[j].max = 1.2f;
  557. pData->param.ranges[j].def = FLUID_REVERB_DEFAULT_ROOMSIZE;
  558. pData->param.ranges[j].step = 0.01f;
  559. pData->param.ranges[j].stepSmall = 0.0001f;
  560. pData->param.ranges[j].stepLarge = 0.1f;
  561. // ----------------------
  562. j = FluidSynthReverbDamp;
  563. pData->param.data[j].type = PARAMETER_INPUT;
  564. pData->param.data[j].hints = PARAMETER_IS_ENABLED /*| PARAMETER_IS_AUTOMABLE*/;
  565. pData->param.data[j].index = j;
  566. pData->param.data[j].rindex = j;
  567. pData->param.ranges[j].min = 0.0f;
  568. pData->param.ranges[j].max = 1.0f;
  569. pData->param.ranges[j].def = FLUID_REVERB_DEFAULT_DAMP;
  570. pData->param.ranges[j].step = 0.01f;
  571. pData->param.ranges[j].stepSmall = 0.0001f;
  572. pData->param.ranges[j].stepLarge = 0.1f;
  573. // ----------------------
  574. j = FluidSynthReverbLevel;
  575. pData->param.data[j].type = PARAMETER_INPUT;
  576. pData->param.data[j].hints = PARAMETER_IS_ENABLED /*| PARAMETER_IS_AUTOMABLE*/;
  577. pData->param.data[j].index = j;
  578. pData->param.data[j].rindex = j;
  579. pData->param.data[j].midiCC = MIDI_CONTROL_REVERB_SEND_LEVEL;
  580. pData->param.ranges[j].min = 0.0f;
  581. pData->param.ranges[j].max = 1.0f;
  582. pData->param.ranges[j].def = FLUID_REVERB_DEFAULT_LEVEL;
  583. pData->param.ranges[j].step = 0.01f;
  584. pData->param.ranges[j].stepSmall = 0.0001f;
  585. pData->param.ranges[j].stepLarge = 0.1f;
  586. // ----------------------
  587. j = FluidSynthReverbWidth;
  588. pData->param.data[j].type = PARAMETER_INPUT;
  589. pData->param.data[j].hints = PARAMETER_IS_ENABLED /*| PARAMETER_IS_AUTOMABLE*/;
  590. pData->param.data[j].index = j;
  591. pData->param.data[j].rindex = j;
  592. pData->param.ranges[j].min = 0.0f;
  593. pData->param.ranges[j].max = 10.0f; // should be 100, but that sounds too much
  594. pData->param.ranges[j].def = FLUID_REVERB_DEFAULT_WIDTH;
  595. pData->param.ranges[j].step = 0.01f;
  596. pData->param.ranges[j].stepSmall = 0.0001f;
  597. pData->param.ranges[j].stepLarge = 0.1f;
  598. // ----------------------
  599. j = FluidSynthChorusOnOff;
  600. pData->param.data[j].type = PARAMETER_INPUT;
  601. pData->param.data[j].hints = PARAMETER_IS_ENABLED | PARAMETER_IS_BOOLEAN;
  602. pData->param.data[j].index = j;
  603. pData->param.data[j].rindex = j;
  604. pData->param.ranges[j].min = 0.0f;
  605. pData->param.ranges[j].max = 1.0f;
  606. pData->param.ranges[j].def = 1.0f;
  607. pData->param.ranges[j].step = 1.0f;
  608. pData->param.ranges[j].stepSmall = 1.0f;
  609. pData->param.ranges[j].stepLarge = 1.0f;
  610. // ----------------------
  611. j = FluidSynthChorusNr;
  612. pData->param.data[j].type = PARAMETER_INPUT;
  613. pData->param.data[j].hints = PARAMETER_IS_ENABLED | PARAMETER_IS_INTEGER;
  614. pData->param.data[j].index = j;
  615. pData->param.data[j].rindex = j;
  616. pData->param.ranges[j].min = 0.0f;
  617. pData->param.ranges[j].max = 99.0f;
  618. pData->param.ranges[j].def = FLUID_CHORUS_DEFAULT_N;
  619. pData->param.ranges[j].step = 1.0f;
  620. pData->param.ranges[j].stepSmall = 1.0f;
  621. pData->param.ranges[j].stepLarge = 10.0f;
  622. // ----------------------
  623. j = FluidSynthChorusLevel;
  624. pData->param.data[j].type = PARAMETER_INPUT;
  625. pData->param.data[j].hints = PARAMETER_IS_ENABLED;
  626. pData->param.data[j].index = j;
  627. pData->param.data[j].rindex = j;
  628. pData->param.ranges[j].min = 0.0f;
  629. pData->param.ranges[j].max = 10.0f;
  630. pData->param.ranges[j].def = FLUID_CHORUS_DEFAULT_LEVEL;
  631. pData->param.ranges[j].step = 0.01f;
  632. pData->param.ranges[j].stepSmall = 0.0001f;
  633. pData->param.ranges[j].stepLarge = 0.1f;
  634. // ----------------------
  635. j = FluidSynthChorusSpeedHz;
  636. pData->param.data[j].type = PARAMETER_INPUT;
  637. pData->param.data[j].hints = PARAMETER_IS_ENABLED;
  638. pData->param.data[j].index = j;
  639. pData->param.data[j].rindex = j;
  640. pData->param.ranges[j].min = 0.29f;
  641. pData->param.ranges[j].max = 5.0f;
  642. pData->param.ranges[j].def = FLUID_CHORUS_DEFAULT_SPEED;
  643. pData->param.ranges[j].step = 0.01f;
  644. pData->param.ranges[j].stepSmall = 0.0001f;
  645. pData->param.ranges[j].stepLarge = 0.1f;
  646. // ----------------------
  647. j = FluidSynthChorusDepthMs;
  648. pData->param.data[j].type = PARAMETER_INPUT;
  649. pData->param.data[j].hints = PARAMETER_IS_ENABLED;
  650. pData->param.data[j].index = j;
  651. pData->param.data[j].rindex = j;
  652. pData->param.ranges[j].min = 0.0f;
  653. pData->param.ranges[j].max = float(2048.0 * 1000.0 / pData->engine->getSampleRate()); // FIXME?
  654. pData->param.ranges[j].def = FLUID_CHORUS_DEFAULT_DEPTH;
  655. pData->param.ranges[j].step = 0.01f;
  656. pData->param.ranges[j].stepSmall = 0.0001f;
  657. pData->param.ranges[j].stepLarge = 0.1f;
  658. // ----------------------
  659. j = FluidSynthChorusType;
  660. pData->param.data[j].type = PARAMETER_INPUT;
  661. pData->param.data[j].hints = PARAMETER_IS_ENABLED | PARAMETER_IS_INTEGER | PARAMETER_USES_SCALEPOINTS;
  662. pData->param.data[j].index = j;
  663. pData->param.data[j].rindex = j;
  664. pData->param.ranges[j].min = FLUID_CHORUS_MOD_SINE;
  665. pData->param.ranges[j].max = FLUID_CHORUS_MOD_TRIANGLE;
  666. pData->param.ranges[j].def = FLUID_CHORUS_DEFAULT_TYPE;
  667. pData->param.ranges[j].step = 1.0f;
  668. pData->param.ranges[j].stepSmall = 1.0f;
  669. pData->param.ranges[j].stepLarge = 1.0f;
  670. // ----------------------
  671. j = FluidSynthPolyphony;
  672. pData->param.data[j].type = PARAMETER_INPUT;
  673. pData->param.data[j].hints = PARAMETER_IS_ENABLED | PARAMETER_IS_INTEGER;
  674. pData->param.data[j].index = j;
  675. pData->param.data[j].rindex = j;
  676. pData->param.ranges[j].min = 1.0f;
  677. pData->param.ranges[j].max = 512.0f; // max theoric is 65535
  678. pData->param.ranges[j].def = (float)fluid_synth_get_polyphony(fSynth);
  679. pData->param.ranges[j].step = 1.0f;
  680. pData->param.ranges[j].stepSmall = 1.0f;
  681. pData->param.ranges[j].stepLarge = 10.0f;
  682. // ----------------------
  683. j = FluidSynthInterpolation;
  684. pData->param.data[j].type = PARAMETER_INPUT;
  685. pData->param.data[j].hints = PARAMETER_IS_ENABLED | PARAMETER_IS_INTEGER | PARAMETER_USES_SCALEPOINTS;
  686. pData->param.data[j].index = j;
  687. pData->param.data[j].rindex = j;
  688. pData->param.ranges[j].min = FLUID_INTERP_NONE;
  689. pData->param.ranges[j].max = FLUID_INTERP_HIGHEST;
  690. pData->param.ranges[j].def = FLUID_INTERP_DEFAULT;
  691. pData->param.ranges[j].step = 1.0f;
  692. pData->param.ranges[j].stepSmall = 1.0f;
  693. pData->param.ranges[j].stepLarge = 1.0f;
  694. // ----------------------
  695. j = FluidSynthVoiceCount;
  696. pData->param.data[j].type = PARAMETER_OUTPUT;
  697. pData->param.data[j].hints = PARAMETER_IS_ENABLED | PARAMETER_IS_AUTOMABLE | PARAMETER_IS_INTEGER;
  698. pData->param.data[j].index = j;
  699. pData->param.data[j].rindex = j;
  700. pData->param.ranges[j].min = 0.0f;
  701. pData->param.ranges[j].max = 65535.0f;
  702. pData->param.ranges[j].def = 0.0f;
  703. pData->param.ranges[j].step = 1.0f;
  704. pData->param.ranges[j].stepSmall = 1.0f;
  705. pData->param.ranges[j].stepLarge = 1.0f;
  706. for (j=0; j<FluidSynthParametersMax; ++j)
  707. fParamBuffers[j] = pData->param.ranges[j].def;
  708. }
  709. // ---------------------------------------
  710. // plugin hints
  711. pData->hints = 0x0;
  712. pData->hints |= PLUGIN_IS_SYNTH;
  713. pData->hints |= PLUGIN_CAN_VOLUME;
  714. if (! kUse16Outs)
  715. pData->hints |= PLUGIN_CAN_BALANCE;
  716. // extra plugin hints
  717. pData->extraHints = 0x0;
  718. pData->extraHints |= PLUGIN_EXTRA_HINT_HAS_MIDI_IN;
  719. pData->extraHints |= PLUGIN_EXTRA_HINT_USES_MULTI_PROGS;
  720. if (kUse16Outs)
  721. pData->extraHints |= PLUGIN_EXTRA_HINT_CAN_RUN_RACK;
  722. bufferSizeChanged(pData->engine->getBufferSize());
  723. reloadPrograms(true);
  724. if (pData->active)
  725. activate();
  726. carla_debug("FluidSynthPlugin::reload() - end");
  727. }
  728. void reloadPrograms(const bool doInit) override
  729. {
  730. carla_debug("FluidSynthPlugin::reloadPrograms(%s)", bool2str(doInit));
  731. // save drum info in case we have one program for it
  732. bool hasDrums = false;
  733. uint32_t drumIndex, drumProg;
  734. drumIndex = drumProg = 0;
  735. // Delete old programs
  736. pData->midiprog.clear();
  737. // Query new programs
  738. uint32_t count = 0;
  739. if (fluid_sfont_t* const f_sfont = fluid_synth_get_sfont_by_id(fSynth, fSynthId))
  740. {
  741. fluid_preset_t f_preset;
  742. // initial check to know how many midi-programs we have
  743. f_sfont->iteration_start(f_sfont);
  744. while (f_sfont->iteration_next(f_sfont, &f_preset))
  745. ++count;
  746. // sound kits must always have at least 1 midi-program
  747. CARLA_SAFE_ASSERT_RETURN(count > 0,);
  748. pData->midiprog.createNew(count);
  749. // Update data
  750. int tmp;
  751. uint32_t i = 0;
  752. f_sfont->iteration_start(f_sfont);
  753. for (; f_sfont->iteration_next(f_sfont, &f_preset);)
  754. {
  755. CARLA_SAFE_ASSERT_BREAK(i < count);
  756. tmp = f_preset.get_banknum(&f_preset);
  757. pData->midiprog.data[i].bank = (tmp >= 0) ? static_cast<uint32_t>(tmp) : 0;
  758. tmp = f_preset.get_num(&f_preset);
  759. pData->midiprog.data[i].program = (tmp >= 0) ? static_cast<uint32_t>(tmp) : 0;
  760. pData->midiprog.data[i].name = carla_strdup(f_preset.get_name(&f_preset));
  761. if (pData->midiprog.data[i].bank == 128 && ! hasDrums)
  762. {
  763. hasDrums = true;
  764. drumIndex = i;
  765. drumProg = pData->midiprog.data[i].program;
  766. }
  767. ++i;
  768. }
  769. }
  770. else
  771. {
  772. // failing means 0 midi-programs, it shouldn't happen!
  773. carla_safe_assert("fluid_sfont_t* const f_sfont = fluid_synth_get_sfont_by_id(fSynth, fSynthId)", __FILE__, __LINE__);
  774. return;
  775. }
  776. #ifndef BUILD_BRIDGE
  777. // Update OSC Names
  778. if (pData->engine->isOscControlRegistered())
  779. {
  780. pData->engine->oscSend_control_set_midi_program_count(pData->id, count);
  781. for (uint32_t i=0; i < count; ++i)
  782. 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);
  783. }
  784. #endif
  785. if (doInit)
  786. {
  787. fluid_synth_program_reset(fSynth);
  788. // select first program, or 128 for ch10
  789. for (int i=0; i < MAX_MIDI_CHANNELS && i != 9; ++i)
  790. {
  791. #ifdef FLUIDSYNTH_VERSION_NEW_API
  792. fluid_synth_set_channel_type(fSynth, i, CHANNEL_TYPE_MELODIC);
  793. #endif
  794. fluid_synth_program_select(fSynth, i, fSynthId, pData->midiprog.data[0].bank, pData->midiprog.data[0].program);
  795. fCurMidiProgs[i] = 0;
  796. }
  797. if (hasDrums)
  798. {
  799. #ifdef FLUIDSYNTH_VERSION_NEW_API
  800. fluid_synth_set_channel_type(fSynth, 9, CHANNEL_TYPE_DRUM);
  801. #endif
  802. fluid_synth_program_select(fSynth, 9, fSynthId, 128, drumProg);
  803. fCurMidiProgs[9] = static_cast<int32_t>(drumIndex);
  804. }
  805. else
  806. {
  807. #ifdef FLUIDSYNTH_VERSION_NEW_API
  808. fluid_synth_set_channel_type(fSynth, 9, CHANNEL_TYPE_MELODIC);
  809. #endif
  810. fluid_synth_program_select(fSynth, 9, fSynthId, pData->midiprog.data[0].bank, pData->midiprog.data[0].program);
  811. fCurMidiProgs[9] = 0;
  812. }
  813. pData->midiprog.current = 0;
  814. }
  815. else
  816. {
  817. pData->engine->callback(ENGINE_CALLBACK_RELOAD_PROGRAMS, pData->id, 0, 0, 0.0f, nullptr);
  818. }
  819. }
  820. // -------------------------------------------------------------------
  821. // Plugin processing
  822. void process(float** const, float** const outBuffer, const uint32_t frames) override
  823. {
  824. // --------------------------------------------------------------------------------------------------------
  825. // Check if active
  826. if (! pData->active)
  827. {
  828. // disable any output sound
  829. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  830. FloatVectorOperations::clear(outBuffer[i], static_cast<int>(frames));
  831. return;
  832. }
  833. // --------------------------------------------------------------------------------------------------------
  834. // Check if needs reset
  835. if (pData->needsReset)
  836. {
  837. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  838. {
  839. for (int i=0; i < MAX_MIDI_CHANNELS; ++i)
  840. {
  841. #ifdef FLUIDSYNTH_VERSION_NEW_API
  842. fluid_synth_all_notes_off(fSynth, i);
  843. fluid_synth_all_sounds_off(fSynth, i);
  844. #else
  845. fluid_synth_cc(fSynth, i, MIDI_CONTROL_ALL_SOUND_OFF, 0);
  846. fluid_synth_cc(fSynth, i, MIDI_CONTROL_ALL_NOTES_OFF, 0);
  847. #endif
  848. }
  849. }
  850. else if (pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  851. {
  852. for (int i=0; i < MAX_MIDI_NOTE; ++i)
  853. fluid_synth_noteoff(fSynth, pData->ctrlChannel, i);
  854. }
  855. pData->needsReset = false;
  856. }
  857. // --------------------------------------------------------------------------------------------------------
  858. // Event Input and Processing
  859. {
  860. // ----------------------------------------------------------------------------------------------------
  861. // MIDI Input (External)
  862. if (pData->extNotes.mutex.tryLock())
  863. {
  864. for (RtLinkedList<ExternalMidiNote>::Itenerator it = pData->extNotes.data.begin(); it.valid(); it.next())
  865. {
  866. const ExternalMidiNote& note(it.getValue());
  867. CARLA_SAFE_ASSERT_CONTINUE(note.channel >= 0 && note.channel < MAX_MIDI_CHANNELS);
  868. if (note.velo > 0)
  869. fluid_synth_noteon(fSynth, note.channel, note.note, note.velo);
  870. else
  871. fluid_synth_noteoff(fSynth,note.channel, note.note);
  872. }
  873. pData->extNotes.data.clear();
  874. pData->extNotes.mutex.unlock();
  875. } // End of MIDI Input (External)
  876. // ----------------------------------------------------------------------------------------------------
  877. // Event Input (System)
  878. #ifndef BUILD_BRIDGE
  879. bool allNotesOffSent = false;
  880. #endif
  881. uint32_t time, nEvents = pData->event.portIn->getEventCount();
  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; i < nEvents; ++i)
  887. {
  888. const EngineEvent& event(pData->event.portIn->getEvent(i));
  889. time = event.time;
  890. CARLA_SAFE_ASSERT_CONTINUE(time < frames);
  891. CARLA_SAFE_ASSERT_BREAK(time >= timeOffset);
  892. if (time > timeOffset)
  893. {
  894. if (processSingle(outBuffer, time - timeOffset, timeOffset))
  895. {
  896. timeOffset = 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 <= 0x5F)
  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. for (uint32_t k=0; k < pData->midiprog.count; ++k)
  999. {
  1000. if (pData->midiprog.data[k].bank == bankId && pData->midiprog.data[k].program == progId)
  1001. {
  1002. fluid_synth_program_select(fSynth, event.channel, fSynthId, bankId, progId);
  1003. fCurMidiProgs[event.channel] = static_cast<int32_t>(k);
  1004. if (event.channel == pData->ctrlChannel)
  1005. pData->postponeRtEvent(kPluginPostRtEventMidiProgramChange, static_cast<int32_t>(k), 0, 0.0f);
  1006. break;
  1007. }
  1008. }
  1009. }
  1010. break;
  1011. case kEngineControlEventTypeAllSoundOff:
  1012. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1013. {
  1014. #ifdef FLUIDSYNTH_VERSION_NEW_API
  1015. fluid_synth_all_sounds_off(fSynth, event.channel);
  1016. #else
  1017. fluid_synth_cc(fSynth, event.channel, MIDI_CONTROL_ALL_SOUND_OFF, 0);
  1018. #endif
  1019. }
  1020. break;
  1021. case kEngineControlEventTypeAllNotesOff:
  1022. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1023. {
  1024. #ifndef BUILD_BRIDGE
  1025. if (event.channel == pData->ctrlChannel && ! allNotesOffSent)
  1026. {
  1027. allNotesOffSent = true;
  1028. sendMidiAllNotesOffToCallback();
  1029. }
  1030. #endif
  1031. #ifdef FLUIDSYNTH_VERSION_NEW_API
  1032. fluid_synth_all_notes_off(fSynth, event.channel);
  1033. #else
  1034. fluid_synth_cc(fSynth, event.channel, MIDI_CONTROL_ALL_NOTES_OFF, 0);
  1035. #endif
  1036. }
  1037. break;
  1038. }
  1039. break;
  1040. }
  1041. case kEngineEventTypeMidi:
  1042. {
  1043. const EngineMidiEvent& midiEvent(event.midi);
  1044. uint8_t status = uint8_t(MIDI_GET_STATUS_FROM_DATA(midiEvent.data));
  1045. uint8_t channel = event.channel;
  1046. // Fix bad note-off
  1047. if (MIDI_IS_STATUS_NOTE_ON(status) && midiEvent.data[2] == 0)
  1048. status = MIDI_STATUS_NOTE_OFF;
  1049. if (MIDI_IS_STATUS_NOTE_OFF(status))
  1050. {
  1051. const uint8_t note = midiEvent.data[1];
  1052. fluid_synth_noteoff(fSynth, channel, note);
  1053. pData->postponeRtEvent(kPluginPostRtEventNoteOff, channel, note, 0.0f);
  1054. }
  1055. else if (MIDI_IS_STATUS_NOTE_ON(status))
  1056. {
  1057. const uint8_t note = midiEvent.data[1];
  1058. const uint8_t velo = midiEvent.data[2];
  1059. fluid_synth_noteon(fSynth, channel, note, velo);
  1060. pData->postponeRtEvent(kPluginPostRtEventNoteOn, channel, note, velo);
  1061. }
  1062. else if (MIDI_IS_STATUS_POLYPHONIC_AFTERTOUCH(status) && (pData->options & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) != 0)
  1063. {
  1064. //const uint8_t note = midiEvent.data[1];
  1065. //const uint8_t pressure = midiEvent.data[2];
  1066. // TODO, not in fluidsynth API
  1067. }
  1068. else if (MIDI_IS_STATUS_CONTROL_CHANGE(status) && (pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0)
  1069. {
  1070. const uint8_t control = midiEvent.data[1];
  1071. const uint8_t value = midiEvent.data[2];
  1072. fluid_synth_cc(fSynth, channel, control, value);
  1073. }
  1074. else if (MIDI_IS_STATUS_CHANNEL_PRESSURE(status) && (pData->options & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) != 0)
  1075. {
  1076. const uint8_t pressure = midiEvent.data[1];
  1077. fluid_synth_channel_pressure(fSynth, channel, pressure);;
  1078. }
  1079. else if (MIDI_IS_STATUS_PITCH_WHEEL_CONTROL(status) && (pData->options & PLUGIN_OPTION_SEND_PITCHBEND) != 0)
  1080. {
  1081. const uint8_t lsb = midiEvent.data[1];
  1082. const uint8_t msb = midiEvent.data[2];
  1083. const int value = ((msb << 7) | lsb);
  1084. fluid_synth_pitch_bend(fSynth, channel, value);
  1085. }
  1086. break;
  1087. }
  1088. }
  1089. }
  1090. pData->postRtEvents.trySplice();
  1091. if (frames > timeOffset)
  1092. processSingle(outBuffer, frames - timeOffset, timeOffset);
  1093. } // End of Event Input and Processing
  1094. #ifndef BUILD_BRIDGE
  1095. // --------------------------------------------------------------------------------------------------------
  1096. // Control Output
  1097. {
  1098. uint32_t k = FluidSynthVoiceCount;
  1099. fParamBuffers[k] = float(fluid_synth_get_active_voice_count(fSynth));
  1100. pData->param.ranges[k].fixValue(fParamBuffers[k]);
  1101. if (pData->param.data[k].midiCC > 0)
  1102. {
  1103. float value(pData->param.ranges[k].getNormalizedValue(fParamBuffers[k]));
  1104. pData->event.portOut->writeControlEvent(0, pData->param.data[k].midiChannel, kEngineControlEventTypeParameter, static_cast<uint16_t>(pData->param.data[k].midiCC), value);
  1105. }
  1106. } // End of Control Output
  1107. #endif
  1108. }
  1109. bool processSingle(float** const outBuffer, const uint32_t frames, const uint32_t timeOffset)
  1110. {
  1111. CARLA_SAFE_ASSERT_RETURN(outBuffer != nullptr, false);
  1112. CARLA_SAFE_ASSERT_RETURN(frames > 0, false);
  1113. // --------------------------------------------------------------------------------------------------------
  1114. // Try lock, silence otherwise
  1115. if (pData->engine->isOffline())
  1116. {
  1117. pData->singleMutex.lock();
  1118. }
  1119. else if (! pData->singleMutex.tryLock())
  1120. {
  1121. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1122. {
  1123. for (uint32_t k=0; k < frames; ++k)
  1124. outBuffer[i][k+timeOffset] = 0.0f;
  1125. }
  1126. return false;
  1127. }
  1128. // --------------------------------------------------------------------------------------------------------
  1129. // Fill plugin buffers and Run plugin
  1130. if (kUse16Outs)
  1131. {
  1132. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1133. FloatVectorOperations::clear(fAudio16Buffers[i], static_cast<int>(frames));
  1134. // FIXME use '32' or '16' instead of outs
  1135. fluid_synth_process(fSynth, static_cast<int>(frames), 0, nullptr, static_cast<int>(pData->audioOut.count), fAudio16Buffers);
  1136. }
  1137. else
  1138. fluid_synth_write_float(fSynth, static_cast<int>(frames), outBuffer[0] + timeOffset, 0, 1, outBuffer[1] + timeOffset, 0, 1);
  1139. #ifndef BUILD_BRIDGE
  1140. // --------------------------------------------------------------------------------------------------------
  1141. // Post-processing (volume and balance)
  1142. {
  1143. // note - balance not possible with kUse16Outs, so we can safely skip fAudioOutBuffers
  1144. const bool doVolume = (pData->hints & PLUGIN_CAN_VOLUME) > 0 && pData->postProc.volume != 1.0f;
  1145. const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) > 0 && (pData->postProc.balanceLeft != -1.0f || pData->postProc.balanceRight != 1.0f);
  1146. float oldBufLeft[doBalance ? frames : 1];
  1147. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1148. {
  1149. // Balance
  1150. if (doBalance)
  1151. {
  1152. if (i % 2 == 0)
  1153. FloatVectorOperations::copy(oldBufLeft, outBuffer[i]+timeOffset, static_cast<int>(frames));
  1154. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  1155. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  1156. for (uint32_t k=0; k < frames; ++k)
  1157. {
  1158. if (i % 2 == 0)
  1159. {
  1160. // left
  1161. outBuffer[i][k+timeOffset] = oldBufLeft[k] * (1.0f - balRangeL);
  1162. outBuffer[i][k+timeOffset] += outBuffer[i+1][k+timeOffset] * (1.0f - balRangeR);
  1163. }
  1164. else
  1165. {
  1166. // right
  1167. outBuffer[i][k+timeOffset] = outBuffer[i][k+timeOffset] * balRangeR;
  1168. outBuffer[i][k+timeOffset] += oldBufLeft[k] * balRangeL;
  1169. }
  1170. }
  1171. }
  1172. // Volume
  1173. if (kUse16Outs)
  1174. {
  1175. for (uint32_t k=0; k < frames; ++k)
  1176. outBuffer[i][k+timeOffset] = fAudio16Buffers[i][k] * pData->postProc.volume;
  1177. }
  1178. else if (doVolume)
  1179. {
  1180. for (uint32_t k=0; k < frames; ++k)
  1181. outBuffer[i][k+timeOffset] *= pData->postProc.volume;
  1182. }
  1183. }
  1184. } // End of Post-processing
  1185. #else
  1186. if (kUse16Outs)
  1187. {
  1188. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1189. {
  1190. for (uint32_t k=0; k < frames; ++k)
  1191. outBuffer[i][k+timeOffset] = fAudio16Buffers[i][k];
  1192. }
  1193. }
  1194. #endif
  1195. // --------------------------------------------------------------------------------------------------------
  1196. pData->singleMutex.unlock();
  1197. return true;
  1198. }
  1199. void bufferSizeChanged(const uint32_t newBufferSize) override
  1200. {
  1201. if (! kUse16Outs)
  1202. return;
  1203. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1204. {
  1205. if (fAudio16Buffers[i] != nullptr)
  1206. delete[] fAudio16Buffers[i];
  1207. fAudio16Buffers[i] = new float[newBufferSize];
  1208. }
  1209. }
  1210. void sampleRateChanged(const double newSampleRate) override
  1211. {
  1212. CARLA_SAFE_ASSERT_RETURN(fSettings != nullptr,);
  1213. fluid_settings_setnum(fSettings, "synth.sample-rate", newSampleRate);
  1214. #ifdef FLUIDSYNTH_VERSION_NEW_API
  1215. CARLA_SAFE_ASSERT_RETURN(fSynth != nullptr,);
  1216. fluid_synth_set_sample_rate(fSynth, float(newSampleRate));
  1217. #endif
  1218. }
  1219. // -------------------------------------------------------------------
  1220. // Plugin buffers
  1221. void clearBuffers() noexcept override
  1222. {
  1223. carla_debug("FluidSynthPlugin::clearBuffers() - start");
  1224. if (fAudio16Buffers != nullptr)
  1225. {
  1226. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1227. {
  1228. if (fAudio16Buffers[i] != nullptr)
  1229. {
  1230. delete[] fAudio16Buffers[i];
  1231. fAudio16Buffers[i] = nullptr;
  1232. }
  1233. }
  1234. delete[] fAudio16Buffers;
  1235. fAudio16Buffers = nullptr;
  1236. }
  1237. CarlaPlugin::clearBuffers();
  1238. carla_debug("FluidSynthPlugin::clearBuffers() - end");
  1239. }
  1240. // -------------------------------------------------------------------
  1241. const void* getExtraStuff() const noexcept override
  1242. {
  1243. static const char xtrue[] = "true";
  1244. static const char xfalse[] = "false";
  1245. return kUse16Outs ? xtrue : xfalse;
  1246. }
  1247. // -------------------------------------------------------------------
  1248. bool init(const char* const filename, const char* const name, const char* const label)
  1249. {
  1250. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  1251. // ---------------------------------------------------------------
  1252. // first checks
  1253. if (pData->client != nullptr)
  1254. {
  1255. pData->engine->setLastError("Plugin client is already registered");
  1256. return false;
  1257. }
  1258. if (fSynth == nullptr)
  1259. {
  1260. pData->engine->setLastError("null synth");
  1261. return false;
  1262. }
  1263. if (filename == nullptr || filename[0] == '\0')
  1264. {
  1265. pData->engine->setLastError("null filename");
  1266. return false;
  1267. }
  1268. if (label == nullptr || label[0] == '\0')
  1269. {
  1270. pData->engine->setLastError("null label");
  1271. return false;
  1272. }
  1273. // ---------------------------------------------------------------
  1274. // open soundfont
  1275. const int synthId(fluid_synth_sfload(fSynth, filename, 0));
  1276. if (synthId < 0)
  1277. {
  1278. pData->engine->setLastError("Failed to load SoundFont file");
  1279. return false;
  1280. }
  1281. fSynthId = static_cast<uint>(synthId);
  1282. // ---------------------------------------------------------------
  1283. // get info
  1284. CarlaString label2(label);
  1285. if (kUse16Outs && ! label2.endsWith(" (16 outs)"))
  1286. label2 += " (16 outs)";
  1287. fLabel = label2.dup();
  1288. pData->filename = carla_strdup(filename);
  1289. if (name != nullptr && name[0] != '\0')
  1290. pData->name = pData->engine->getUniquePluginName(name);
  1291. else
  1292. pData->name = pData->engine->getUniquePluginName(label);
  1293. // ---------------------------------------------------------------
  1294. // register client
  1295. pData->client = pData->engine->addClient(this);
  1296. if (pData->client == nullptr || ! pData->client->isOk())
  1297. {
  1298. pData->engine->setLastError("Failed to register plugin client");
  1299. return false;
  1300. }
  1301. // ---------------------------------------------------------------
  1302. // set default options
  1303. pData->options = 0x0;
  1304. pData->options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  1305. pData->options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  1306. pData->options |= PLUGIN_OPTION_SEND_PITCHBEND;
  1307. pData->options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  1308. return true;
  1309. }
  1310. private:
  1311. enum FluidSynthInputParameters {
  1312. FluidSynthReverbOnOff = 0,
  1313. FluidSynthReverbRoomSize = 1,
  1314. FluidSynthReverbDamp = 2,
  1315. FluidSynthReverbLevel = 3,
  1316. FluidSynthReverbWidth = 4,
  1317. FluidSynthChorusOnOff = 5,
  1318. FluidSynthChorusNr = 6,
  1319. FluidSynthChorusLevel = 7,
  1320. FluidSynthChorusSpeedHz = 8,
  1321. FluidSynthChorusDepthMs = 9,
  1322. FluidSynthChorusType = 10,
  1323. FluidSynthPolyphony = 11,
  1324. FluidSynthInterpolation = 12,
  1325. FluidSynthVoiceCount = 13,
  1326. FluidSynthParametersMax = 14
  1327. };
  1328. const bool kUse16Outs;
  1329. fluid_settings_t* fSettings;
  1330. fluid_synth_t* fSynth;
  1331. uint fSynthId;
  1332. float** fAudio16Buffers;
  1333. float fParamBuffers[FluidSynthParametersMax];
  1334. int32_t fCurMidiProgs[MAX_MIDI_CHANNELS];
  1335. const char* fLabel;
  1336. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(FluidSynthPlugin)
  1337. };
  1338. CARLA_BACKEND_END_NAMESPACE
  1339. #endif // HAVE_FLUIDSYNTH
  1340. CARLA_BACKEND_START_NAMESPACE
  1341. // -------------------------------------------------------------------------------------------------------------------
  1342. CarlaPlugin* CarlaPlugin::newFluidSynth(const Initializer& init, const bool use16Outs)
  1343. {
  1344. carla_debug("CarlaPlugin::newFluidSynth({%p, \"%s\", \"%s\", \"%s\", " P_INT64 "}, %s)", init.engine, init.filename, init.name, init.label, init.uniqueId, bool2str(use16Outs));
  1345. #ifdef HAVE_FLUIDSYNTH
  1346. if (init.engine->getProccessMode() == ENGINE_PROCESS_MODE_CONTINUOUS_RACK && use16Outs)
  1347. {
  1348. init.engine->setLastError("Carla's rack mode can only work with Stereo modules, please choose the 2-channel only SoundFont version");
  1349. return nullptr;
  1350. }
  1351. if (! fluid_is_soundfont(init.filename))
  1352. {
  1353. init.engine->setLastError("Requested file is not a valid SoundFont");
  1354. return nullptr;
  1355. }
  1356. FluidSynthPlugin* const plugin(new FluidSynthPlugin(init.engine, init.id, use16Outs));
  1357. if (! plugin->init(init.filename, init.name, init.label))
  1358. {
  1359. delete plugin;
  1360. return nullptr;
  1361. }
  1362. plugin->reload();
  1363. return plugin;
  1364. #else
  1365. init.engine->setLastError("fluidsynth support not available");
  1366. return nullptr;
  1367. // unused
  1368. (void)use16Outs;
  1369. #endif
  1370. }
  1371. CarlaPlugin* CarlaPlugin::newFileSF2(const Initializer& init, const bool use16Outs)
  1372. {
  1373. carla_debug("CarlaPlugin::newFileSF2({%p, \"%s\", \"%s\", \"%s\"}, %s)", init.engine, init.filename, init.name, init.label, bool2str(use16Outs));
  1374. #ifdef HAVE_FLUIDSYNTH
  1375. return newFluidSynth(init, use16Outs);
  1376. #else
  1377. init.engine->setLastError("SF2 support not available");
  1378. return nullptr;
  1379. // unused
  1380. (void)use16Outs;
  1381. #endif
  1382. }
  1383. // -------------------------------------------------------------------------------------------------------------------
  1384. CARLA_BACKEND_END_NAMESPACE