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.

1708 lines
61KB

  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. CARLA_SAFE_ASSERT_RETURN(fSynth != nullptr,);
  305. char strBuf[STR_MAX+1];
  306. std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i", fCurMidiProgs[0], fCurMidiProgs[1], fCurMidiProgs[2], fCurMidiProgs[3],
  307. fCurMidiProgs[4], fCurMidiProgs[5], fCurMidiProgs[6], fCurMidiProgs[7],
  308. fCurMidiProgs[8], fCurMidiProgs[9], fCurMidiProgs[10], fCurMidiProgs[11],
  309. fCurMidiProgs[12], fCurMidiProgs[13], fCurMidiProgs[14], fCurMidiProgs[15]);
  310. CarlaPlugin::setCustomData(CUSTOM_DATA_TYPE_STRING, "midiPrograms", strBuf, false);
  311. }
  312. // -------------------------------------------------------------------
  313. // Set data (internal stuff)
  314. void setCtrlChannel(const int8_t channel, const bool sendOsc, const bool sendCallback) override
  315. {
  316. if (channel < MAX_MIDI_CHANNELS)
  317. pData->midiprog.current = fCurMidiProgs[channel];
  318. CarlaPlugin::setCtrlChannel(channel, sendOsc, sendCallback);
  319. }
  320. // -------------------------------------------------------------------
  321. // Set data (plugin-specific stuff)
  322. void setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) override
  323. {
  324. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  325. const float fixedValue(pData->param.getFixedValue(parameterId, value));
  326. fParamBuffers[parameterId] = fixedValue;
  327. {
  328. const ScopedSingleProcessLocker spl(this, (sendGui || sendOsc || sendCallback));
  329. switch (parameterId)
  330. {
  331. case FluidSynthReverbOnOff:
  332. fluid_synth_set_reverb_on(fSynth, (fixedValue > 0.5f) ? 1 : 0);
  333. break;
  334. case FluidSynthReverbRoomSize:
  335. case FluidSynthReverbDamp:
  336. case FluidSynthReverbLevel:
  337. case FluidSynthReverbWidth:
  338. fluid_synth_set_reverb(fSynth, fParamBuffers[FluidSynthReverbRoomSize], fParamBuffers[FluidSynthReverbDamp], fParamBuffers[FluidSynthReverbWidth], fParamBuffers[FluidSynthReverbLevel]);
  339. break;
  340. case FluidSynthChorusOnOff:
  341. fluid_synth_set_chorus_on(fSynth, (value > 0.5f) ? 1 : 0);
  342. break;
  343. case FluidSynthChorusNr:
  344. case FluidSynthChorusLevel:
  345. case FluidSynthChorusSpeedHz:
  346. case FluidSynthChorusDepthMs:
  347. case FluidSynthChorusType:
  348. fluid_synth_set_chorus(fSynth, (int)fParamBuffers[FluidSynthChorusNr], fParamBuffers[FluidSynthChorusLevel], fParamBuffers[FluidSynthChorusSpeedHz], fParamBuffers[FluidSynthChorusDepthMs], (int)fParamBuffers[FluidSynthChorusType]);
  349. break;
  350. case FluidSynthPolyphony:
  351. fluid_synth_set_polyphony(fSynth, (int)value);
  352. break;
  353. case FluidSynthInterpolation:
  354. for (int i=0; i < MAX_MIDI_CHANNELS; ++i)
  355. fluid_synth_set_interp_method(fSynth, i, (int)value);
  356. break;
  357. default:
  358. break;
  359. }
  360. }
  361. CarlaPlugin::setParameterValue(parameterId, value, sendGui, sendOsc, sendCallback);
  362. }
  363. void setCustomData(const char* const type, const char* const key, const char* const value, const bool sendGui) override
  364. {
  365. CARLA_SAFE_ASSERT_RETURN(fSynth != nullptr,);
  366. CARLA_SAFE_ASSERT_RETURN(type != nullptr && type[0] != '\0',);
  367. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  368. CARLA_SAFE_ASSERT_RETURN(value != nullptr && value[0] != '\0',);
  369. carla_debug("FluidSynthPlugin::setCustomData(%s, \"%s\", \"%s\", %s)", type, key, value, bool2str(sendGui));
  370. if (std::strcmp(type, CUSTOM_DATA_TYPE_STRING) != 0)
  371. return carla_stderr2("FluidSynthPlugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - type is not string", type, key, value, bool2str(sendGui));
  372. if (std::strcmp(key, "midiPrograms") != 0)
  373. return carla_stderr2("FluidSynthPlugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - type is not string", type, key, value, bool2str(sendGui));
  374. if (! fUses16Outs)
  375. return CarlaPlugin::setCustomData(type, key, value, sendGui);
  376. QStringList midiProgramList(QString(value).split(":", QString::SkipEmptyParts));
  377. if (midiProgramList.count() == MAX_MIDI_CHANNELS)
  378. {
  379. uint i = 0;
  380. foreach (const QString& midiProg, midiProgramList)
  381. {
  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 gui 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, j;
  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 (j=0; j < 32; ++j)
  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 ((j+2)/2 < 9)
  458. portName += "0";
  459. portName += CarlaString((j+2)/2);
  460. if (j % 2 == 0)
  461. portName += "L";
  462. else
  463. portName += "R";
  464. portName.truncate(portNameSize);
  465. pData->audioOut.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  466. pData->audioOut.ports[j].rindex = j;
  467. }
  468. fAudio16Buffers = new float*[aOuts];
  469. for (j=0; j < aOuts; ++j)
  470. fAudio16Buffers[j] = 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 += "event-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 += "event-out";
  520. portName.truncate(portNameSize);
  521. pData->event.portOut = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false);
  522. }
  523. // ----------------------
  524. j = FluidSynthReverbOnOff;
  525. pData->param.data[j].type = PARAMETER_INPUT;
  526. pData->param.data[j].hints = PARAMETER_IS_ENABLED /*| PARAMETER_IS_AUTOMABLE*/ | PARAMETER_IS_BOOLEAN;
  527. pData->param.data[j].index = j;
  528. pData->param.data[j].rindex = j;
  529. pData->param.data[j].midiChannel = 0;
  530. pData->param.data[j].midiCC = -1;
  531. pData->param.ranges[j].min = 0.0f;
  532. pData->param.ranges[j].max = 1.0f;
  533. pData->param.ranges[j].def = 1.0f;
  534. pData->param.ranges[j].step = 1.0f;
  535. pData->param.ranges[j].stepSmall = 1.0f;
  536. pData->param.ranges[j].stepLarge = 1.0f;
  537. fParamBuffers[j] = pData->param.ranges[j].def;
  538. // ----------------------
  539. j = FluidSynthReverbRoomSize;
  540. pData->param.data[j].type = PARAMETER_INPUT;
  541. pData->param.data[j].hints = PARAMETER_IS_ENABLED /*| PARAMETER_IS_AUTOMABLE*/;
  542. pData->param.data[j].index = j;
  543. pData->param.data[j].rindex = j;
  544. pData->param.data[j].midiChannel = 0;
  545. pData->param.data[j].midiCC = -1;
  546. pData->param.ranges[j].min = 0.0f;
  547. pData->param.ranges[j].max = 1.2f;
  548. pData->param.ranges[j].def = FLUID_REVERB_DEFAULT_ROOMSIZE;
  549. pData->param.ranges[j].step = 0.01f;
  550. pData->param.ranges[j].stepSmall = 0.0001f;
  551. pData->param.ranges[j].stepLarge = 0.1f;
  552. fParamBuffers[j] = pData->param.ranges[j].def;
  553. // ----------------------
  554. j = FluidSynthReverbDamp;
  555. pData->param.data[j].type = PARAMETER_INPUT;
  556. pData->param.data[j].hints = PARAMETER_IS_ENABLED /*| PARAMETER_IS_AUTOMABLE*/;
  557. pData->param.data[j].index = j;
  558. pData->param.data[j].rindex = j;
  559. pData->param.data[j].midiChannel = 0;
  560. pData->param.data[j].midiCC = -1;
  561. pData->param.ranges[j].min = 0.0f;
  562. pData->param.ranges[j].max = 1.0f;
  563. pData->param.ranges[j].def = FLUID_REVERB_DEFAULT_DAMP;
  564. pData->param.ranges[j].step = 0.01f;
  565. pData->param.ranges[j].stepSmall = 0.0001f;
  566. pData->param.ranges[j].stepLarge = 0.1f;
  567. fParamBuffers[j] = pData->param.ranges[j].def;
  568. // ----------------------
  569. j = FluidSynthReverbLevel;
  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].midiChannel = 0;
  575. pData->param.data[j].midiCC = MIDI_CONTROL_REVERB_SEND_LEVEL;
  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_LEVEL;
  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. fParamBuffers[j] = pData->param.ranges[j].def;
  583. // ----------------------
  584. j = FluidSynthReverbWidth;
  585. pData->param.data[j].type = PARAMETER_INPUT;
  586. pData->param.data[j].hints = PARAMETER_IS_ENABLED /*| PARAMETER_IS_AUTOMABLE*/;
  587. pData->param.data[j].index = j;
  588. pData->param.data[j].rindex = j;
  589. pData->param.data[j].midiChannel = 0;
  590. pData->param.data[j].midiCC = -1;
  591. pData->param.ranges[j].min = 0.0f;
  592. pData->param.ranges[j].max = 10.0f; // should be 100, but that sounds too much
  593. pData->param.ranges[j].def = FLUID_REVERB_DEFAULT_WIDTH;
  594. pData->param.ranges[j].step = 0.01f;
  595. pData->param.ranges[j].stepSmall = 0.0001f;
  596. pData->param.ranges[j].stepLarge = 0.1f;
  597. fParamBuffers[j] = pData->param.ranges[j].def;
  598. // ----------------------
  599. j = FluidSynthChorusOnOff;
  600. pData->param.data[j].type = PARAMETER_INPUT;
  601. pData->param.data[j].hints = PARAMETER_IS_ENABLED | PARAMETER_IS_BOOLEAN;
  602. pData->param.data[j].index = j;
  603. pData->param.data[j].rindex = j;
  604. pData->param.data[j].midiChannel = 0;
  605. pData->param.data[j].midiCC = -1;
  606. pData->param.ranges[j].min = 0.0f;
  607. pData->param.ranges[j].max = 1.0f;
  608. pData->param.ranges[j].def = 1.0f;
  609. pData->param.ranges[j].step = 1.0f;
  610. pData->param.ranges[j].stepSmall = 1.0f;
  611. pData->param.ranges[j].stepLarge = 1.0f;
  612. fParamBuffers[j] = pData->param.ranges[j].def;
  613. // ----------------------
  614. j = FluidSynthChorusNr;
  615. pData->param.data[j].type = PARAMETER_INPUT;
  616. pData->param.data[j].hints = PARAMETER_IS_ENABLED | PARAMETER_IS_INTEGER;
  617. pData->param.data[j].index = j;
  618. pData->param.data[j].rindex = j;
  619. pData->param.data[j].midiChannel = 0;
  620. pData->param.data[j].midiCC = -1;
  621. pData->param.ranges[j].min = 0.0f;
  622. pData->param.ranges[j].max = 99.0f;
  623. pData->param.ranges[j].def = FLUID_CHORUS_DEFAULT_N;
  624. pData->param.ranges[j].step = 1.0f;
  625. pData->param.ranges[j].stepSmall = 1.0f;
  626. pData->param.ranges[j].stepLarge = 10.0f;
  627. fParamBuffers[j] = pData->param.ranges[j].def;
  628. // ----------------------
  629. j = FluidSynthChorusLevel;
  630. pData->param.data[j].type = PARAMETER_INPUT;
  631. pData->param.data[j].hints = PARAMETER_IS_ENABLED;
  632. pData->param.data[j].index = j;
  633. pData->param.data[j].rindex = j;
  634. pData->param.data[j].midiChannel = 0;
  635. pData->param.data[j].midiCC = 0; //MIDI_CONTROL_CHORUS_SEND_LEVEL;
  636. pData->param.ranges[j].min = 0.0f;
  637. pData->param.ranges[j].max = 10.0f;
  638. pData->param.ranges[j].def = FLUID_CHORUS_DEFAULT_LEVEL;
  639. pData->param.ranges[j].step = 0.01f;
  640. pData->param.ranges[j].stepSmall = 0.0001f;
  641. pData->param.ranges[j].stepLarge = 0.1f;
  642. fParamBuffers[j] = pData->param.ranges[j].def;
  643. // ----------------------
  644. j = FluidSynthChorusSpeedHz;
  645. pData->param.data[j].type = PARAMETER_INPUT;
  646. pData->param.data[j].hints = PARAMETER_IS_ENABLED;
  647. pData->param.data[j].index = j;
  648. pData->param.data[j].rindex = j;
  649. pData->param.data[j].midiChannel = 0;
  650. pData->param.data[j].midiCC = -1;
  651. pData->param.ranges[j].min = 0.29f;
  652. pData->param.ranges[j].max = 5.0f;
  653. pData->param.ranges[j].def = FLUID_CHORUS_DEFAULT_SPEED;
  654. pData->param.ranges[j].step = 0.01f;
  655. pData->param.ranges[j].stepSmall = 0.0001f;
  656. pData->param.ranges[j].stepLarge = 0.1f;
  657. fParamBuffers[j] = pData->param.ranges[j].def;
  658. // ----------------------
  659. j = FluidSynthChorusDepthMs;
  660. pData->param.data[j].type = PARAMETER_INPUT;
  661. pData->param.data[j].hints = PARAMETER_IS_ENABLED;
  662. pData->param.data[j].index = j;
  663. pData->param.data[j].rindex = j;
  664. pData->param.data[j].midiChannel = 0;
  665. pData->param.data[j].midiCC = -1;
  666. pData->param.ranges[j].min = 0.0f;
  667. pData->param.ranges[j].max = float(2048.0 * 1000.0 / pData->engine->getSampleRate()); // FIXME?
  668. pData->param.ranges[j].def = FLUID_CHORUS_DEFAULT_DEPTH;
  669. pData->param.ranges[j].step = 0.01f;
  670. pData->param.ranges[j].stepSmall = 0.0001f;
  671. pData->param.ranges[j].stepLarge = 0.1f;
  672. fParamBuffers[j] = pData->param.ranges[j].def;
  673. // ----------------------
  674. j = FluidSynthChorusType;
  675. pData->param.data[j].type = PARAMETER_INPUT;
  676. pData->param.data[j].hints = PARAMETER_IS_ENABLED | PARAMETER_IS_INTEGER | PARAMETER_USES_SCALEPOINTS;
  677. pData->param.data[j].index = j;
  678. pData->param.data[j].rindex = j;
  679. pData->param.data[j].midiChannel = 0;
  680. pData->param.data[j].midiCC = -1;
  681. pData->param.ranges[j].min = FLUID_CHORUS_MOD_SINE;
  682. pData->param.ranges[j].max = FLUID_CHORUS_MOD_TRIANGLE;
  683. pData->param.ranges[j].def = FLUID_CHORUS_DEFAULT_TYPE;
  684. pData->param.ranges[j].step = 1.0f;
  685. pData->param.ranges[j].stepSmall = 1.0f;
  686. pData->param.ranges[j].stepLarge = 1.0f;
  687. fParamBuffers[j] = pData->param.ranges[j].def;
  688. // ----------------------
  689. j = FluidSynthPolyphony;
  690. pData->param.data[j].type = PARAMETER_INPUT;
  691. pData->param.data[j].hints = PARAMETER_IS_ENABLED | PARAMETER_IS_INTEGER;
  692. pData->param.data[j].index = j;
  693. pData->param.data[j].rindex = j;
  694. pData->param.data[j].midiChannel = 0;
  695. pData->param.data[j].midiCC = -1;
  696. pData->param.ranges[j].min = 1.0f;
  697. pData->param.ranges[j].max = 512.0f; // max theoric is 65535
  698. pData->param.ranges[j].def = (float)fluid_synth_get_polyphony(fSynth);
  699. pData->param.ranges[j].step = 1.0f;
  700. pData->param.ranges[j].stepSmall = 1.0f;
  701. pData->param.ranges[j].stepLarge = 10.0f;
  702. fParamBuffers[j] = pData->param.ranges[j].def;
  703. // ----------------------
  704. j = FluidSynthInterpolation;
  705. pData->param.data[j].type = PARAMETER_INPUT;
  706. pData->param.data[j].hints = PARAMETER_IS_ENABLED | PARAMETER_IS_INTEGER | PARAMETER_USES_SCALEPOINTS;
  707. pData->param.data[j].index = j;
  708. pData->param.data[j].rindex = j;
  709. pData->param.data[j].midiChannel = 0;
  710. pData->param.data[j].midiCC = -1;
  711. pData->param.ranges[j].min = FLUID_INTERP_NONE;
  712. pData->param.ranges[j].max = FLUID_INTERP_HIGHEST;
  713. pData->param.ranges[j].def = FLUID_INTERP_DEFAULT;
  714. pData->param.ranges[j].step = 1.0f;
  715. pData->param.ranges[j].stepSmall = 1.0f;
  716. pData->param.ranges[j].stepLarge = 1.0f;
  717. fParamBuffers[j] = pData->param.ranges[j].def;
  718. // ----------------------
  719. j = FluidSynthVoiceCount;
  720. pData->param.data[j].type = PARAMETER_OUTPUT;
  721. pData->param.data[j].hints = PARAMETER_IS_ENABLED | PARAMETER_IS_AUTOMABLE | PARAMETER_IS_INTEGER;
  722. pData->param.data[j].index = j;
  723. pData->param.data[j].rindex = j;
  724. pData->param.data[j].midiChannel = 0;
  725. pData->param.data[j].midiCC = -1;
  726. pData->param.ranges[j].min = 0.0f;
  727. pData->param.ranges[j].max = 65535.0f;
  728. pData->param.ranges[j].def = 0.0f;
  729. pData->param.ranges[j].step = 1.0f;
  730. pData->param.ranges[j].stepSmall = 1.0f;
  731. pData->param.ranges[j].stepLarge = 1.0f;
  732. fParamBuffers[j] = pData->param.ranges[j].def;
  733. // ---------------------------------------
  734. // plugin hints
  735. pData->hints = 0x0;
  736. pData->hints |= PLUGIN_IS_SYNTH;
  737. pData->hints |= PLUGIN_CAN_VOLUME;
  738. pData->hints |= PLUGIN_CAN_BALANCE;
  739. // extra plugin hints
  740. pData->extraHints = 0x0;
  741. pData->extraHints |= PLUGIN_EXTRA_HINT_HAS_MIDI_IN;
  742. if (! fUses16Outs)
  743. pData->extraHints |= PLUGIN_EXTRA_HINT_CAN_RUN_RACK;
  744. bufferSizeChanged(pData->engine->getBufferSize());
  745. reloadPrograms(true);
  746. if (pData->active)
  747. activate();
  748. carla_debug("FluidSynthPlugin::reload() - end");
  749. }
  750. void reloadPrograms(const bool init) override
  751. {
  752. carla_debug("FluidSynthPlugin::reloadPrograms(%s)", bool2str(init));
  753. // Delete old programs
  754. pData->midiprog.clear();
  755. // Query new programs
  756. uint32_t count = 0;
  757. fluid_sfont_t* f_sfont;
  758. fluid_preset_t f_preset;
  759. bool hasDrums = false;
  760. uint32_t drumIndex, drumProg;
  761. f_sfont = fluid_synth_get_sfont_by_id(fSynth, fSynthId);
  762. // initial check to know how much midi-programs we have
  763. f_sfont->iteration_start(f_sfont);
  764. while (f_sfont->iteration_next(f_sfont, &f_preset))
  765. count += 1;
  766. // soundfonts must always have at least 1 midi-program
  767. CARLA_ASSERT(count > 0);
  768. if (count == 0)
  769. return;
  770. pData->midiprog.createNew(count);
  771. // Update data
  772. uint32_t i = 0;
  773. f_sfont->iteration_start(f_sfont);
  774. while (f_sfont->iteration_next(f_sfont, &f_preset))
  775. {
  776. CARLA_ASSERT(i < pData->midiprog.count);
  777. pData->midiprog.data[i].bank = f_preset.get_banknum(&f_preset);
  778. pData->midiprog.data[i].program = f_preset.get_num(&f_preset);
  779. pData->midiprog.data[i].name = carla_strdup(f_preset.get_name(&f_preset));
  780. if (pData->midiprog.data[i].bank == 128 && ! hasDrums)
  781. {
  782. hasDrums = true;
  783. drumIndex = i;
  784. drumProg = pData->midiprog.data[i].program;
  785. }
  786. ++i;
  787. }
  788. //f_sfont->free(f_sfont);
  789. #ifndef BUILD_BRIDGE
  790. // Update OSC Names
  791. if (pData->engine->isOscControlRegistered())
  792. {
  793. pData->engine->oscSend_control_set_midi_program_count(pData->id, count);
  794. for (i=0; i < count; ++i)
  795. 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);
  796. }
  797. #endif
  798. if (init)
  799. {
  800. fluid_synth_program_reset(fSynth);
  801. // select first program, or 128 for ch10
  802. for (i=0; i < MAX_MIDI_CHANNELS && i != 9; ++i)
  803. {
  804. #ifdef FLUIDSYNTH_VERSION_NEW_API
  805. fluid_synth_set_channel_type(fSynth, i, CHANNEL_TYPE_MELODIC);
  806. #endif
  807. fluid_synth_program_select(fSynth, i, fSynthId, pData->midiprog.data[0].bank, pData->midiprog.data[0].program);
  808. fCurMidiProgs[i] = 0;
  809. }
  810. if (hasDrums)
  811. {
  812. #ifdef FLUIDSYNTH_VERSION_NEW_API
  813. fluid_synth_set_channel_type(fSynth, 9, CHANNEL_TYPE_DRUM);
  814. #endif
  815. fluid_synth_program_select(fSynth, 9, fSynthId, 128, drumProg);
  816. fCurMidiProgs[9] = drumIndex;
  817. }
  818. else
  819. {
  820. #ifdef FLUIDSYNTH_VERSION_NEW_API
  821. fluid_synth_set_channel_type(fSynth, 9, CHANNEL_TYPE_MELODIC);
  822. #endif
  823. fluid_synth_program_select(fSynth, 9, fSynthId, pData->midiprog.data[0].bank, pData->midiprog.data[0].program);
  824. fCurMidiProgs[9] = 0;
  825. }
  826. pData->midiprog.current = 0;
  827. }
  828. else
  829. {
  830. pData->engine->callback(ENGINE_CALLBACK_RELOAD_PROGRAMS, pData->id, 0, 0, 0.0f, nullptr);
  831. }
  832. }
  833. // -------------------------------------------------------------------
  834. // Plugin processing
  835. void process(float** const, float** const outBuffer, const uint32_t frames) override
  836. {
  837. uint32_t i, k;
  838. // --------------------------------------------------------------------------------------------------------
  839. // Check if active
  840. if (! pData->active)
  841. {
  842. // disable any output sound
  843. for (i=0; i < pData->audioOut.count; ++i)
  844. {
  845. #ifdef HAVE_JUCE
  846. FloatVectorOperations::clear(outBuffer[i], frames);
  847. #else
  848. #endif
  849. }
  850. return;
  851. }
  852. // --------------------------------------------------------------------------------------------------------
  853. // Check if needs reset
  854. if (pData->needsReset)
  855. {
  856. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  857. {
  858. for (int c=0; c < MAX_MIDI_CHANNELS; ++c)
  859. {
  860. #ifdef FLUIDSYNTH_VERSION_NEW_API
  861. fluid_synth_all_notes_off(fSynth, c);
  862. fluid_synth_all_sounds_off(fSynth, c);
  863. #else
  864. fluid_synth_cc(fSynth, c, MIDI_CONTROL_ALL_SOUND_OFF, 0);
  865. fluid_synth_cc(fSynth, c, MIDI_CONTROL_ALL_NOTES_OFF, 0);
  866. #endif
  867. }
  868. }
  869. else if (pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  870. {
  871. for (k=0; k < MAX_MIDI_NOTE; ++k)
  872. fluid_synth_noteoff(fSynth, pData->ctrlChannel, k);
  873. }
  874. pData->needsReset = false;
  875. }
  876. // --------------------------------------------------------------------------------------------------------
  877. // Event Input and Processing
  878. {
  879. // ----------------------------------------------------------------------------------------------------
  880. // MIDI Input (External)
  881. if (pData->extNotes.mutex.tryLock())
  882. {
  883. while (! pData->extNotes.data.isEmpty())
  884. {
  885. const ExternalMidiNote& note(pData->extNotes.data.getFirst(true));
  886. CARLA_ASSERT(note.channel >= 0 && note.channel < MAX_MIDI_CHANNELS);
  887. if (note.velo > 0)
  888. fluid_synth_noteon(fSynth, note.channel, note.note, note.velo);
  889. else
  890. fluid_synth_noteoff(fSynth,note.channel, note.note);
  891. }
  892. pData->extNotes.mutex.unlock();
  893. } // End of MIDI Input (External)
  894. // ----------------------------------------------------------------------------------------------------
  895. // Event Input (System)
  896. bool allNotesOffSent = false;
  897. uint32_t time, nEvents = pData->event.portIn->getEventCount();
  898. uint32_t timeOffset = 0;
  899. uint32_t nextBankIds[MAX_MIDI_CHANNELS] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 0, 0, 0, 0, 0, 0 };
  900. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0 && pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  901. nextBankIds[pData->ctrlChannel] = pData->midiprog.data[pData->midiprog.current].bank;
  902. for (i=0; i < nEvents; ++i)
  903. {
  904. const EngineEvent& event(pData->event.portIn->getEvent(i));
  905. time = event.time;
  906. if (time >= frames)
  907. continue;
  908. CARLA_ASSERT_INT2(time >= timeOffset, time, timeOffset);
  909. if (time > timeOffset)
  910. {
  911. if (processSingle(outBuffer, time - timeOffset, timeOffset))
  912. {
  913. timeOffset = time;
  914. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0 && pData->ctrlChannel >= 0 && pData->ctrlChannel < 16)
  915. nextBankIds[pData->ctrlChannel] = pData->midiprog.data[pData->midiprog.current].bank;
  916. }
  917. }
  918. // Control change
  919. switch (event.type)
  920. {
  921. case kEngineEventTypeNull:
  922. break;
  923. case kEngineEventTypeControl:
  924. {
  925. const EngineControlEvent& ctrlEvent = event.ctrl;
  926. switch (ctrlEvent.type)
  927. {
  928. case kEngineControlEventTypeNull:
  929. break;
  930. case kEngineControlEventTypeParameter:
  931. {
  932. #ifndef BUILD_BRIDGE
  933. // Control backend stuff
  934. if (event.channel == pData->ctrlChannel)
  935. {
  936. float value;
  937. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_DRYWET) != 0)
  938. {
  939. value = ctrlEvent.value;
  940. setDryWet(value, false, false);
  941. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, value);
  942. }
  943. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_VOLUME) != 0)
  944. {
  945. value = ctrlEvent.value*127.0f/100.0f;
  946. setVolume(value, false, false);
  947. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
  948. }
  949. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_BALANCE) != 0)
  950. {
  951. float left, right;
  952. value = ctrlEvent.value/0.5f - 1.0f;
  953. if (value < 0.0f)
  954. {
  955. left = -1.0f;
  956. right = (value*2.0f)+1.0f;
  957. }
  958. else if (value > 0.0f)
  959. {
  960. left = (value*2.0f)-1.0f;
  961. right = 1.0f;
  962. }
  963. else
  964. {
  965. left = -1.0f;
  966. right = 1.0f;
  967. }
  968. setBalanceLeft(left, false, false);
  969. setBalanceRight(right, false, false);
  970. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  971. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  972. }
  973. }
  974. #endif
  975. // Control plugin parameters
  976. for (k=0; k < pData->param.count; ++k)
  977. {
  978. if (pData->param.data[k].midiChannel != event.channel)
  979. continue;
  980. if (pData->param.data[k].midiCC != ctrlEvent.param)
  981. continue;
  982. if (pData->param.data[k].hints != PARAMETER_INPUT)
  983. continue;
  984. if ((pData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  985. continue;
  986. float value;
  987. if (pData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  988. {
  989. value = (ctrlEvent.value < 0.5f) ? pData->param.ranges[k].min : pData->param.ranges[k].max;
  990. }
  991. else
  992. {
  993. value = pData->param.ranges[k].getUnnormalizedValue(ctrlEvent.value);
  994. if (pData->param.data[k].hints & PARAMETER_IS_INTEGER)
  995. value = std::rint(value);
  996. }
  997. setParameterValue(k, value, false, false, false);
  998. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, value);
  999. }
  1000. if ((pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0 && ctrlEvent.param <= 0x5F)
  1001. {
  1002. fluid_synth_cc(fSynth, event.channel, ctrlEvent.param, int(ctrlEvent.value*127.0f));
  1003. }
  1004. break;
  1005. }
  1006. case kEngineControlEventTypeMidiBank:
  1007. if (event.channel < MAX_MIDI_CHANNELS && (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  1008. nextBankIds[event.channel] = ctrlEvent.param;
  1009. break;
  1010. case kEngineControlEventTypeMidiProgram:
  1011. if (event.channel < MAX_MIDI_CHANNELS && (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  1012. {
  1013. const uint32_t bankId(nextBankIds[event.channel]);
  1014. const uint32_t progId(ctrlEvent.param);
  1015. for (k=0; k < pData->midiprog.count; ++k)
  1016. {
  1017. if (pData->midiprog.data[k].bank == bankId && pData->midiprog.data[k].program == progId)
  1018. {
  1019. fluid_synth_program_select(fSynth, event.channel, fSynthId, bankId, progId);
  1020. fCurMidiProgs[event.channel] = k;
  1021. if (event.channel == pData->ctrlChannel)
  1022. pData->postponeRtEvent(kPluginPostRtEventMidiProgramChange, k, 0, 0.0f);
  1023. break;
  1024. }
  1025. }
  1026. }
  1027. break;
  1028. case kEngineControlEventTypeAllSoundOff:
  1029. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1030. {
  1031. #ifdef FLUIDSYNTH_VERSION_NEW_API
  1032. fluid_synth_all_sounds_off(fSynth, event.channel);
  1033. #else
  1034. fluid_synth_cc(fSynth, event.channel, MIDI_CONTROL_ALL_SOUND_OFF, 0);
  1035. #endif
  1036. }
  1037. break;
  1038. case kEngineControlEventTypeAllNotesOff:
  1039. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1040. {
  1041. if (event.channel == pData->ctrlChannel && ! allNotesOffSent)
  1042. {
  1043. allNotesOffSent = true;
  1044. sendMidiAllNotesOffToCallback();
  1045. }
  1046. #ifdef FLUIDSYNTH_VERSION_NEW_API
  1047. fluid_synth_all_notes_off(fSynth, event.channel);
  1048. #else
  1049. fluid_synth_cc(fSynth, event.channel, MIDI_CONTROL_ALL_NOTES_OFF, 0);
  1050. #endif
  1051. }
  1052. break;
  1053. }
  1054. break;
  1055. }
  1056. case kEngineEventTypeMidi:
  1057. {
  1058. const EngineMidiEvent& midiEvent(event.midi);
  1059. uint8_t status = MIDI_GET_STATUS_FROM_DATA(midiEvent.data);
  1060. uint8_t channel = event.channel;
  1061. // Fix bad note-off
  1062. if (MIDI_IS_STATUS_NOTE_ON(status) && midiEvent.data[2] == 0)
  1063. status = MIDI_STATUS_NOTE_OFF;
  1064. if (MIDI_IS_STATUS_NOTE_OFF(status))
  1065. {
  1066. const uint8_t note = midiEvent.data[1];
  1067. fluid_synth_noteoff(fSynth, channel, note);
  1068. pData->postponeRtEvent(kPluginPostRtEventNoteOff, channel, note, 0.0f);
  1069. }
  1070. else if (MIDI_IS_STATUS_NOTE_ON(status))
  1071. {
  1072. const uint8_t note = midiEvent.data[1];
  1073. const uint8_t velo = midiEvent.data[2];
  1074. fluid_synth_noteon(fSynth, channel, note, velo);
  1075. pData->postponeRtEvent(kPluginPostRtEventNoteOn, channel, note, velo);
  1076. }
  1077. else if (MIDI_IS_STATUS_POLYPHONIC_AFTERTOUCH(status) && (pData->options & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) != 0)
  1078. {
  1079. //const uint8_t note = midiEvent.data[1];
  1080. //const uint8_t pressure = midiEvent.data[2];
  1081. // TODO, not in fluidsynth API
  1082. }
  1083. else if (MIDI_IS_STATUS_CONTROL_CHANGE(status) && (pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0)
  1084. {
  1085. const uint8_t control = midiEvent.data[1];
  1086. const uint8_t value = midiEvent.data[2];
  1087. fluid_synth_cc(fSynth, channel, control, value);
  1088. }
  1089. else if (MIDI_IS_STATUS_CHANNEL_PRESSURE(status) && (pData->options & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) != 0)
  1090. {
  1091. const uint8_t pressure = midiEvent.data[1];
  1092. fluid_synth_channel_pressure(fSynth, channel, pressure);;
  1093. }
  1094. else if (MIDI_IS_STATUS_PITCH_WHEEL_CONTROL(status) && (pData->options & PLUGIN_OPTION_SEND_PITCHBEND) != 0)
  1095. {
  1096. const uint8_t lsb = midiEvent.data[1];
  1097. const uint8_t msb = midiEvent.data[2];
  1098. fluid_synth_pitch_bend(fSynth, channel, (msb << 7) | lsb);
  1099. }
  1100. break;
  1101. }
  1102. }
  1103. }
  1104. pData->postRtEvents.trySplice();
  1105. if (frames > timeOffset)
  1106. processSingle(outBuffer, frames - timeOffset, timeOffset);
  1107. } // End of Event Input and Processing
  1108. CARLA_PROCESS_CONTINUE_CHECK;
  1109. // --------------------------------------------------------------------------------------------------------
  1110. // Control Output
  1111. {
  1112. k = FluidSynthVoiceCount;
  1113. fParamBuffers[k] = (float)fluid_synth_get_active_voice_count(fSynth);
  1114. pData->param.ranges[k].fixValue(fParamBuffers[k]);
  1115. if (pData->param.data[k].midiCC > 0)
  1116. {
  1117. float value(pData->param.ranges[k].getNormalizedValue(fParamBuffers[k]));
  1118. pData->event.portOut->writeControlEvent(0, pData->param.data[k].midiChannel, kEngineControlEventTypeParameter, pData->param.data[k].midiCC, value);
  1119. }
  1120. } // End of Control Output
  1121. }
  1122. bool processSingle(float** const outBuffer, const uint32_t frames, const uint32_t timeOffset)
  1123. {
  1124. CARLA_SAFE_ASSERT_RETURN(outBuffer != nullptr, false);
  1125. CARLA_SAFE_ASSERT_RETURN(frames > 0, false);
  1126. uint32_t i, k;
  1127. // --------------------------------------------------------------------------------------------------------
  1128. // Try lock, silence otherwise
  1129. if (pData->engine->isOffline())
  1130. {
  1131. pData->singleMutex.lock();
  1132. }
  1133. else if (! pData->singleMutex.tryLock())
  1134. {
  1135. for (i=0; i < pData->audioOut.count; ++i)
  1136. {
  1137. for (k=0; k < frames; ++k)
  1138. outBuffer[i][k+timeOffset] = 0.0f;
  1139. }
  1140. return false;
  1141. }
  1142. // --------------------------------------------------------------------------------------------------------
  1143. // Fill plugin buffers and Run plugin
  1144. if (fUses16Outs)
  1145. {
  1146. for (i=0; i < pData->audioOut.count; ++i)
  1147. FLOAT_CLEAR(fAudio16Buffers[i], frames);
  1148. fluid_synth_process(fSynth, frames, 0, nullptr, pData->audioOut.count, fAudio16Buffers);
  1149. }
  1150. else
  1151. fluid_synth_write_float(fSynth, frames, outBuffer[0] + timeOffset, 0, 1, outBuffer[1] + timeOffset, 0, 1);
  1152. #ifndef BUILD_BRIDGE
  1153. // --------------------------------------------------------------------------------------------------------
  1154. // Post-processing (volume and balance)
  1155. {
  1156. // note - balance not possible with fUses16Outs, so we can safely skip fAudioOutBuffers
  1157. const bool doVolume = (pData->hints & PLUGIN_CAN_VOLUME) > 0 && pData->postProc.volume != 1.0f;
  1158. const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) > 0 && (pData->postProc.balanceLeft != -1.0f || pData->postProc.balanceRight != 1.0f);
  1159. float oldBufLeft[doBalance ? frames : 1];
  1160. for (i=0; i < pData->audioOut.count; ++i)
  1161. {
  1162. // Balance
  1163. if (doBalance)
  1164. {
  1165. if (i % 2 == 0)
  1166. FLOAT_COPY(oldBufLeft, outBuffer[i]+timeOffset, frames);
  1167. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  1168. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  1169. for (k=0; k < frames; ++k)
  1170. {
  1171. if (i % 2 == 0)
  1172. {
  1173. // left
  1174. outBuffer[i][k+timeOffset] = oldBufLeft[k] * (1.0f - balRangeL);
  1175. outBuffer[i][k+timeOffset] += outBuffer[i+1][k+timeOffset] * (1.0f - balRangeR);
  1176. }
  1177. else
  1178. {
  1179. // right
  1180. outBuffer[i][k+timeOffset] = outBuffer[i][k+timeOffset] * balRangeR;
  1181. outBuffer[i][k+timeOffset] += oldBufLeft[k] * balRangeL;
  1182. }
  1183. }
  1184. }
  1185. // Volume
  1186. if (fUses16Outs)
  1187. {
  1188. for (k=0; k < frames; ++k)
  1189. outBuffer[i][k+timeOffset] = fAudio16Buffers[i][k] * pData->postProc.volume;
  1190. }
  1191. else if (doVolume)
  1192. {
  1193. for (k=0; k < frames; ++k)
  1194. outBuffer[i][k+timeOffset] *= pData->postProc.volume;
  1195. }
  1196. }
  1197. } // End of Post-processing
  1198. #else
  1199. if (fUses16Outs)
  1200. {
  1201. for (i=0; i < pData->audioOut.count; ++i)
  1202. {
  1203. for (k=0; k < frames; ++k)
  1204. outBuffer[i][k+timeOffset] = fAudio16Buffers[i][k];
  1205. }
  1206. }
  1207. #endif
  1208. // --------------------------------------------------------------------------------------------------------
  1209. pData->singleMutex.unlock();
  1210. return true;
  1211. }
  1212. void bufferSizeChanged(const uint32_t newBufferSize) override
  1213. {
  1214. if (! fUses16Outs)
  1215. return;
  1216. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1217. {
  1218. if (fAudio16Buffers[i] != nullptr)
  1219. delete[] fAudio16Buffers[i];
  1220. fAudio16Buffers[i] = new float[newBufferSize];
  1221. }
  1222. }
  1223. // -------------------------------------------------------------------
  1224. // Plugin buffers
  1225. void clearBuffers() override
  1226. {
  1227. carla_debug("FluidSynthPlugin::clearBuffers() - start");
  1228. if (fAudio16Buffers != nullptr)
  1229. {
  1230. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1231. {
  1232. if (fAudio16Buffers[i] != nullptr)
  1233. {
  1234. delete[] fAudio16Buffers[i];
  1235. fAudio16Buffers[i] = nullptr;
  1236. }
  1237. }
  1238. delete[] fAudio16Buffers;
  1239. fAudio16Buffers = nullptr;
  1240. }
  1241. CarlaPlugin::clearBuffers();
  1242. carla_debug("FluidSynthPlugin::clearBuffers() - end");
  1243. }
  1244. // -------------------------------------------------------------------
  1245. const void* getExtraStuff() const noexcept override
  1246. {
  1247. return fUses16Outs ? (const void*)0x1 : nullptr;
  1248. }
  1249. bool init(const char* const filename, const char* const name, const char* const label)
  1250. {
  1251. CARLA_ASSERT(fSynth != nullptr);
  1252. CARLA_ASSERT(filename != nullptr);
  1253. CARLA_ASSERT(label != nullptr);
  1254. // ---------------------------------------------------------------
  1255. // first checks
  1256. if (pData->engine == nullptr)
  1257. {
  1258. return false;
  1259. }
  1260. if (pData->client != nullptr)
  1261. {
  1262. pData->engine->setLastError("Plugin client is already registered");
  1263. return false;
  1264. }
  1265. if (fSynth == nullptr)
  1266. {
  1267. pData->engine->setLastError("null synth");
  1268. return false;
  1269. }
  1270. if (filename == nullptr)
  1271. {
  1272. pData->engine->setLastError("null filename");
  1273. return false;
  1274. }
  1275. if (label == nullptr)
  1276. {
  1277. pData->engine->setLastError("null label");
  1278. return false;
  1279. }
  1280. // ---------------------------------------------------------------
  1281. // open soundfont
  1282. fSynthId = fluid_synth_sfload(fSynth, filename, 0);
  1283. if (fSynthId < 0)
  1284. {
  1285. pData->engine->setLastError("Failed to load SoundFont file");
  1286. return false;
  1287. }
  1288. // ---------------------------------------------------------------
  1289. // get info
  1290. CarlaString label2(label);
  1291. if (fUses16Outs && ! label2.endsWith(" (16 outs)"))
  1292. label2 += " (16 outs)";
  1293. pData->filename = carla_strdup(filename);
  1294. fLabel = label2.dup();
  1295. if (name != nullptr)
  1296. pData->name = pData->engine->getUniquePluginName(name);
  1297. else
  1298. pData->name = pData->engine->getUniquePluginName(label);
  1299. // ---------------------------------------------------------------
  1300. // register client
  1301. pData->client = pData->engine->addClient(this);
  1302. if (pData->client == nullptr || ! pData->client->isOk())
  1303. {
  1304. pData->engine->setLastError("Failed to register plugin client");
  1305. return false;
  1306. }
  1307. // ---------------------------------------------------------------
  1308. // load plugin settings
  1309. {
  1310. // set default options
  1311. pData->options = 0x0;
  1312. pData->options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  1313. pData->options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  1314. pData->options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  1315. pData->options |= PLUGIN_OPTION_SEND_PITCHBEND;
  1316. pData->options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  1317. // set identifier string
  1318. CarlaString identifier("SF2/");
  1319. if (const char* const shortname = std::strrchr(filename, OS_SEP))
  1320. identifier += shortname+1;
  1321. else
  1322. identifier += label;
  1323. pData->identifier = identifier.dup();
  1324. // load settings
  1325. pData->options = pData->loadSettings(pData->options, getOptionsAvailable());
  1326. }
  1327. return true;
  1328. }
  1329. private:
  1330. enum FluidSynthInputParameters {
  1331. FluidSynthReverbOnOff = 0,
  1332. FluidSynthReverbRoomSize = 1,
  1333. FluidSynthReverbDamp = 2,
  1334. FluidSynthReverbLevel = 3,
  1335. FluidSynthReverbWidth = 4,
  1336. FluidSynthChorusOnOff = 5,
  1337. FluidSynthChorusNr = 6,
  1338. FluidSynthChorusLevel = 7,
  1339. FluidSynthChorusSpeedHz = 8,
  1340. FluidSynthChorusDepthMs = 9,
  1341. FluidSynthChorusType = 10,
  1342. FluidSynthPolyphony = 11,
  1343. FluidSynthInterpolation = 12,
  1344. FluidSynthVoiceCount = 13,
  1345. FluidSynthParametersMax = 14
  1346. };
  1347. const bool fUses16Outs;
  1348. fluid_settings_t* fSettings;
  1349. fluid_synth_t* fSynth;
  1350. int fSynthId;
  1351. float** fAudio16Buffers;
  1352. float fParamBuffers[FluidSynthParametersMax];
  1353. int32_t fCurMidiProgs[MAX_MIDI_CHANNELS];
  1354. const char* fLabel;
  1355. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(FluidSynthPlugin)
  1356. };
  1357. CARLA_BACKEND_END_NAMESPACE
  1358. #endif // WANT_FLUIDSYNTH
  1359. CARLA_BACKEND_START_NAMESPACE
  1360. CarlaPlugin* CarlaPlugin::newFluidSynth(const Initializer& init, const bool use16Outs)
  1361. {
  1362. carla_debug("CarlaPlugin::newFluidSynth({%p, \"%s\", \"%s\", \"%s\"}, %s)", init.engine, init.filename, init.name, init.label, bool2str(use16Outs));
  1363. #ifdef WANT_FLUIDSYNTH
  1364. if (! fluid_is_soundfont(init.filename))
  1365. {
  1366. init.engine->setLastError("Requested file is not a valid SoundFont");
  1367. return nullptr;
  1368. }
  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. FluidSynthPlugin* const plugin(new FluidSynthPlugin(init.engine, init.id, use16Outs));
  1375. if (! plugin->init(init.filename, init.name, init.label))
  1376. {
  1377. delete plugin;
  1378. return nullptr;
  1379. }
  1380. plugin->reload();
  1381. return plugin;
  1382. #else
  1383. init.engine->setLastError("fluidsynth support not available");
  1384. return nullptr;
  1385. // unused
  1386. (void)use16Outs;
  1387. #endif
  1388. }
  1389. CARLA_BACKEND_END_NAMESPACE