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.

1717 lines
63KB

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