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.

1737 lines
63KB

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