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.

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