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.

1738 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. #include <QtCore/QStringList>
  23. #if (FLUIDSYNTH_VERSION_MAJOR >= 1 && FLUIDSYNTH_VERSION_MINOR >= 1 && FLUIDSYNTH_VERSION_MICRO >= 4)
  24. # define FLUIDSYNTH_VERSION_NEW_API
  25. #endif
  26. CARLA_BACKEND_START_NAMESPACE
  27. #if 0
  28. }
  29. #endif
  30. #define FLUID_DEFAULT_POLYPHONY 64
  31. class FluidSynthPlugin : public CarlaPlugin
  32. {
  33. public:
  34. FluidSynthPlugin(CarlaEngine* const engine, const uint id, const bool use16Outs)
  35. : CarlaPlugin(engine, id),
  36. fUses16Outs(use16Outs),
  37. fSettings(nullptr),
  38. fSynth(nullptr),
  39. fSynthId(0),
  40. fAudio16Buffers(nullptr),
  41. fLabel(nullptr)
  42. {
  43. carla_debug("FluidSynthPlugin::FluidSynthPlugin(%p, %i, %s)", engine, id, bool2str(use16Outs));
  44. FLOAT_CLEAR(fParamBuffers, FluidSynthParametersMax);
  45. carla_fill<int32_t>(fCurMidiProgs, MAX_MIDI_CHANNELS, 0);
  46. // create settings
  47. fSettings = new_fluid_settings();
  48. CARLA_SAFE_ASSERT_RETURN(fSettings != nullptr,);
  49. // define settings
  50. fluid_settings_setint(fSettings, "synth.audio-channels", use16Outs ? 16 : 1);
  51. fluid_settings_setint(fSettings, "synth.audio-groups", use16Outs ? 16 : 1);
  52. fluid_settings_setnum(fSettings, "synth.sample-rate", pData->engine->getSampleRate());
  53. //fluid_settings_setnum(fSettings, "synth.cpu-cores", 2);
  54. fluid_settings_setint(fSettings, "synth.parallel-render", 1);
  55. fluid_settings_setint(fSettings, "synth.threadsafe-api", 0);
  56. // create synth
  57. fSynth = new_fluid_synth(fSettings);
  58. CARLA_SAFE_ASSERT_RETURN(fSynth != nullptr,);
  59. #ifdef FLUIDSYNTH_VERSION_NEW_API
  60. fluid_synth_set_sample_rate(fSynth, (float)pData->engine->getSampleRate());
  61. #endif
  62. // set default values
  63. fluid_synth_set_reverb_on(fSynth, 1);
  64. fluid_synth_set_reverb(fSynth, FLUID_REVERB_DEFAULT_ROOMSIZE, FLUID_REVERB_DEFAULT_DAMP, FLUID_REVERB_DEFAULT_WIDTH, FLUID_REVERB_DEFAULT_LEVEL);
  65. fluid_synth_set_chorus_on(fSynth, 1);
  66. fluid_synth_set_chorus(fSynth, FLUID_CHORUS_DEFAULT_N, FLUID_CHORUS_DEFAULT_LEVEL, FLUID_CHORUS_DEFAULT_SPEED, FLUID_CHORUS_DEFAULT_DEPTH, FLUID_CHORUS_DEFAULT_TYPE);
  67. fluid_synth_set_polyphony(fSynth, FLUID_DEFAULT_POLYPHONY);
  68. fluid_synth_set_gain(fSynth, 1.0f);
  69. for (int i=0; i < MAX_MIDI_CHANNELS; ++i)
  70. fluid_synth_set_interp_method(fSynth, i, FLUID_INTERP_DEFAULT);
  71. }
  72. ~FluidSynthPlugin() override
  73. {
  74. carla_debug("FluidSynthPlugin::~FluidSynthPlugin()");
  75. pData->singleMutex.lock();
  76. pData->masterMutex.lock();
  77. if (pData->client != nullptr && pData->client->isActive())
  78. pData->client->deactivate();
  79. if (pData->active)
  80. {
  81. deactivate();
  82. pData->active = false;
  83. }
  84. if (fSynth != nullptr)
  85. {
  86. delete_fluid_synth(fSynth);
  87. fSynth = nullptr;
  88. }
  89. if (fSettings != nullptr)
  90. {
  91. delete_fluid_settings(fSettings);
  92. fSettings = nullptr;
  93. }
  94. if (fLabel != nullptr)
  95. {
  96. delete[] fLabel;
  97. fLabel = nullptr;
  98. }
  99. clearBuffers();
  100. }
  101. // -------------------------------------------------------------------
  102. // Information (base)
  103. PluginType getType() const noexcept override
  104. {
  105. return PLUGIN_FILE_SF2;
  106. }
  107. PluginCategory getCategory() const noexcept override
  108. {
  109. return PLUGIN_CATEGORY_SYNTH;
  110. }
  111. // -------------------------------------------------------------------
  112. // Information (count)
  113. uint32_t getParameterScalePointCount(const uint32_t parameterId) const noexcept override
  114. {
  115. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0);
  116. switch (parameterId)
  117. {
  118. case FluidSynthChorusType:
  119. return 2;
  120. case FluidSynthInterpolation:
  121. return 4;
  122. default:
  123. return 0;
  124. }
  125. }
  126. // -------------------------------------------------------------------
  127. // Information (current data)
  128. // nothing
  129. // -------------------------------------------------------------------
  130. // Information (per-plugin data)
  131. uint getOptionsAvailable() const noexcept override
  132. {
  133. uint options = 0x0;
  134. options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  135. options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  136. options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  137. options |= PLUGIN_OPTION_SEND_PITCHBEND;
  138. options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  139. return options;
  140. }
  141. float getParameterValue(const uint32_t parameterId) const noexcept override
  142. {
  143. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0.0f);
  144. return fParamBuffers[parameterId];
  145. }
  146. float getParameterScalePointValue(const uint32_t parameterId, const uint32_t scalePointId) const noexcept override
  147. {
  148. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0.0f);
  149. CARLA_SAFE_ASSERT_RETURN(scalePointId < getParameterScalePointCount(parameterId), 0.0f);
  150. switch (parameterId)
  151. {
  152. case FluidSynthChorusType:
  153. switch (scalePointId)
  154. {
  155. case 0:
  156. return FLUID_CHORUS_MOD_SINE;
  157. case 1:
  158. return FLUID_CHORUS_MOD_TRIANGLE;
  159. default:
  160. return FLUID_CHORUS_DEFAULT_TYPE;
  161. }
  162. case FluidSynthInterpolation:
  163. switch (scalePointId)
  164. {
  165. case 0:
  166. return FLUID_INTERP_NONE;
  167. case 1:
  168. return FLUID_INTERP_LINEAR;
  169. case 2:
  170. return FLUID_INTERP_4THORDER;
  171. case 3:
  172. return FLUID_INTERP_7THORDER;
  173. default:
  174. return FLUID_INTERP_DEFAULT;
  175. }
  176. default:
  177. return 0.0f;
  178. }
  179. }
  180. void getLabel(char* const strBuf) const noexcept override
  181. {
  182. if (fLabel != nullptr)
  183. {
  184. std::strncpy(strBuf, fLabel, STR_MAX);
  185. return;
  186. }
  187. CarlaPlugin::getLabel(strBuf);
  188. }
  189. void getMaker(char* const strBuf) const noexcept override
  190. {
  191. std::strncpy(strBuf, "FluidSynth SF2 engine", STR_MAX);
  192. }
  193. void getCopyright(char* const strBuf) const noexcept override
  194. {
  195. std::strncpy(strBuf, "GNU GPL v2+", STR_MAX);
  196. }
  197. void getRealName(char* const strBuf) const noexcept override
  198. {
  199. getLabel(strBuf);
  200. }
  201. void getParameterName(const uint32_t parameterId, char* const strBuf) const noexcept override
  202. {
  203. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  204. switch (parameterId)
  205. {
  206. case FluidSynthReverbOnOff:
  207. std::strncpy(strBuf, "Reverb On/Off", STR_MAX);
  208. return;
  209. case FluidSynthReverbRoomSize:
  210. std::strncpy(strBuf, "Reverb Room Size", STR_MAX);
  211. return;
  212. case FluidSynthReverbDamp:
  213. std::strncpy(strBuf, "Reverb Damp", STR_MAX);
  214. return;
  215. case FluidSynthReverbLevel:
  216. std::strncpy(strBuf, "Reverb Level", STR_MAX);
  217. return;
  218. case FluidSynthReverbWidth:
  219. std::strncpy(strBuf, "Reverb Width", STR_MAX);
  220. return;
  221. case FluidSynthChorusOnOff:
  222. std::strncpy(strBuf, "Chorus On/Off", STR_MAX);
  223. return;
  224. case FluidSynthChorusNr:
  225. std::strncpy(strBuf, "Chorus Voice Count", STR_MAX);
  226. return;
  227. case FluidSynthChorusLevel:
  228. std::strncpy(strBuf, "Chorus Level", STR_MAX);
  229. return;
  230. case FluidSynthChorusSpeedHz:
  231. std::strncpy(strBuf, "Chorus Speed", STR_MAX);
  232. return;
  233. case FluidSynthChorusDepthMs:
  234. std::strncpy(strBuf, "Chorus Depth", STR_MAX);
  235. return;
  236. case FluidSynthChorusType:
  237. std::strncpy(strBuf, "Chorus Type", STR_MAX);
  238. return;
  239. case FluidSynthPolyphony:
  240. std::strncpy(strBuf, "Polyphony", STR_MAX);
  241. return;
  242. case FluidSynthInterpolation:
  243. std::strncpy(strBuf, "Interpolation", STR_MAX);
  244. return;
  245. case FluidSynthVoiceCount:
  246. std::strncpy(strBuf, "Voice Count", STR_MAX);
  247. return;
  248. }
  249. CarlaPlugin::getParameterName(parameterId, strBuf);
  250. }
  251. void getParameterUnit(const uint32_t parameterId, char* const strBuf) const noexcept override
  252. {
  253. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  254. switch (parameterId)
  255. {
  256. case FluidSynthChorusSpeedHz:
  257. std::strncpy(strBuf, "Hz", STR_MAX);
  258. return;
  259. case FluidSynthChorusDepthMs:
  260. std::strncpy(strBuf, "ms", STR_MAX);
  261. return;
  262. }
  263. CarlaPlugin::getParameterUnit(parameterId, strBuf);
  264. }
  265. void getParameterScalePointLabel(const uint32_t parameterId, const uint32_t scalePointId, char* const strBuf) const noexcept override
  266. {
  267. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  268. CARLA_SAFE_ASSERT_RETURN(scalePointId < getParameterScalePointCount(parameterId),);
  269. switch (parameterId)
  270. {
  271. case FluidSynthChorusType:
  272. switch (scalePointId)
  273. {
  274. case 0:
  275. std::strncpy(strBuf, "Sine wave", STR_MAX);
  276. return;
  277. case 1:
  278. std::strncpy(strBuf, "Triangle wave", STR_MAX);
  279. return;
  280. }
  281. case FluidSynthInterpolation:
  282. switch (scalePointId)
  283. {
  284. case 0:
  285. std::strncpy(strBuf, "None", STR_MAX);
  286. return;
  287. case 1:
  288. std::strncpy(strBuf, "Straight-line", STR_MAX);
  289. return;
  290. case 2:
  291. std::strncpy(strBuf, "Fourth-order", STR_MAX);
  292. return;
  293. case 3:
  294. std::strncpy(strBuf, "Seventh-order", STR_MAX);
  295. return;
  296. }
  297. }
  298. CarlaPlugin::getParameterScalePointLabel(parameterId, scalePointId, strBuf);
  299. }
  300. // -------------------------------------------------------------------
  301. // Set data (state)
  302. void prepareForSave() override
  303. {
  304. char strBuf[STR_MAX+1];
  305. std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i", fCurMidiProgs[0], fCurMidiProgs[1], fCurMidiProgs[2], fCurMidiProgs[3],
  306. fCurMidiProgs[4], fCurMidiProgs[5], fCurMidiProgs[6], fCurMidiProgs[7],
  307. fCurMidiProgs[8], fCurMidiProgs[9], fCurMidiProgs[10], fCurMidiProgs[11],
  308. fCurMidiProgs[12], fCurMidiProgs[13], fCurMidiProgs[14], fCurMidiProgs[15]);
  309. CarlaPlugin::setCustomData(CUSTOM_DATA_TYPE_STRING, "midiPrograms", strBuf, false);
  310. }
  311. // -------------------------------------------------------------------
  312. // Set data (internal stuff)
  313. void setCtrlChannel(const int8_t channel, const bool sendOsc, const bool sendCallback) noexcept override
  314. {
  315. if (channel >= 0 && channel < MAX_MIDI_CHANNELS)
  316. pData->midiprog.current = fCurMidiProgs[channel];
  317. CarlaPlugin::setCtrlChannel(channel, sendOsc, sendCallback);
  318. }
  319. // -------------------------------------------------------------------
  320. // Set data (plugin-specific stuff)
  321. void setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  322. {
  323. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  324. const float fixedValue(pData->param.getFixedValue(parameterId, value));
  325. fParamBuffers[parameterId] = fixedValue;
  326. {
  327. const ScopedSingleProcessLocker spl(this, (sendGui || sendOsc || sendCallback));
  328. switch (parameterId)
  329. {
  330. case FluidSynthReverbOnOff:
  331. try {
  332. fluid_synth_set_reverb_on(fSynth, (fixedValue > 0.5f) ? 1 : 0);
  333. } catch(...) {}
  334. break;
  335. case FluidSynthReverbRoomSize:
  336. case FluidSynthReverbDamp:
  337. case FluidSynthReverbLevel:
  338. case FluidSynthReverbWidth:
  339. try {
  340. fluid_synth_set_reverb(fSynth, fParamBuffers[FluidSynthReverbRoomSize], fParamBuffers[FluidSynthReverbDamp], fParamBuffers[FluidSynthReverbWidth], fParamBuffers[FluidSynthReverbLevel]);
  341. } catch(...) {}
  342. break;
  343. case FluidSynthChorusOnOff:
  344. try {
  345. fluid_synth_set_chorus_on(fSynth, (value > 0.5f) ? 1 : 0);
  346. } catch(...) {}
  347. break;
  348. case FluidSynthChorusNr:
  349. case FluidSynthChorusLevel:
  350. case FluidSynthChorusSpeedHz:
  351. case FluidSynthChorusDepthMs:
  352. case FluidSynthChorusType:
  353. try {
  354. fluid_synth_set_chorus(fSynth, (int)fParamBuffers[FluidSynthChorusNr], fParamBuffers[FluidSynthChorusLevel], fParamBuffers[FluidSynthChorusSpeedHz], fParamBuffers[FluidSynthChorusDepthMs], (int)fParamBuffers[FluidSynthChorusType]);
  355. } catch(...) {}
  356. break;
  357. case FluidSynthPolyphony:
  358. try {
  359. fluid_synth_set_polyphony(fSynth, (int)value);
  360. } catch(...) {}
  361. break;
  362. case FluidSynthInterpolation:
  363. for (int i=0; i < MAX_MIDI_CHANNELS; ++i)
  364. {
  365. try {
  366. fluid_synth_set_interp_method(fSynth, i, (int)value);
  367. }
  368. catch(...) {
  369. break;
  370. }
  371. }
  372. break;
  373. default:
  374. break;
  375. }
  376. }
  377. CarlaPlugin::setParameterValue(parameterId, value, sendGui, sendOsc, sendCallback);
  378. }
  379. void setCustomData(const char* const type, const char* const key, const char* const value, const bool sendGui) override
  380. {
  381. CARLA_SAFE_ASSERT_RETURN(fSynth != nullptr,);
  382. CARLA_SAFE_ASSERT_RETURN(type != nullptr && type[0] != '\0',);
  383. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  384. CARLA_SAFE_ASSERT_RETURN(value != nullptr && value[0] != '\0',);
  385. carla_debug("FluidSynthPlugin::setCustomData(%s, \"%s\", \"%s\", %s)", type, key, value, bool2str(sendGui));
  386. if (std::strcmp(type, CUSTOM_DATA_TYPE_STRING) != 0)
  387. return carla_stderr2("FluidSynthPlugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - type is not string", type, key, value, bool2str(sendGui));
  388. if (std::strcmp(key, "midiPrograms") != 0)
  389. return carla_stderr2("FluidSynthPlugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - type is not string", type, key, value, bool2str(sendGui));
  390. if (fUses16Outs)
  391. {
  392. QStringList midiProgramList(QString(value).split(":", QString::SkipEmptyParts));
  393. if (midiProgramList.count() == MAX_MIDI_CHANNELS)
  394. {
  395. int i = 0;
  396. foreach (const QString& midiProg, midiProgramList)
  397. {
  398. CARLA_SAFE_ASSERT_BREAK(i < MAX_MIDI_CHANNELS);
  399. bool ok;
  400. int index = midiProg.toInt(&ok);
  401. if (ok && index >= 0 && index < static_cast<int>(pData->midiprog.count))
  402. {
  403. const uint32_t bank = pData->midiprog.data[index].bank;
  404. const uint32_t program = pData->midiprog.data[index].program;
  405. fluid_synth_program_select(fSynth, i, fSynthId, bank, program);
  406. fCurMidiProgs[i] = index;
  407. if (pData->ctrlChannel == static_cast<int32_t>(i))
  408. {
  409. pData->midiprog.current = index;
  410. pData->engine->callback(ENGINE_CALLBACK_MIDI_PROGRAM_CHANGED, pData->id, index, 0, 0.0f, nullptr);
  411. }
  412. }
  413. ++i;
  414. }
  415. CARLA_SAFE_ASSERT(i == MAX_MIDI_CHANNELS);
  416. }
  417. }
  418. CarlaPlugin::setCustomData(type, key, value, sendGui);
  419. }
  420. void setMidiProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  421. {
  422. CARLA_SAFE_ASSERT_RETURN(fSynth != nullptr,);
  423. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->midiprog.count),);
  424. if (index >= 0 && pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  425. {
  426. const uint32_t bank = pData->midiprog.data[index].bank;
  427. const uint32_t program = pData->midiprog.data[index].program;
  428. //const ScopedSingleProcessLocker spl(this, (sendGui || sendOsc || sendCallback));
  429. try {
  430. fluid_synth_program_select(fSynth, pData->ctrlChannel, fSynthId, bank, program);
  431. } catch(...) {}
  432. fCurMidiProgs[pData->ctrlChannel] = index;
  433. }
  434. CarlaPlugin::setMidiProgram(index, sendGui, sendOsc, sendCallback);
  435. }
  436. // -------------------------------------------------------------------
  437. // Set ui stuff
  438. // nothing
  439. // -------------------------------------------------------------------
  440. // Plugin state
  441. void reload() override
  442. {
  443. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr,);
  444. CARLA_SAFE_ASSERT_RETURN(fSynth != nullptr,);
  445. carla_debug("FluidSynthPlugin::reload() - start");
  446. const EngineProcessMode processMode(pData->engine->getProccessMode());
  447. // Safely disable plugin for reload
  448. const ScopedDisabler sd(this);
  449. if (pData->active)
  450. deactivate();
  451. clearBuffers();
  452. uint32_t aOuts, params;
  453. aOuts = fUses16Outs ? 32 : 2;
  454. params = FluidSynthParametersMax;
  455. pData->audioOut.createNew(aOuts);
  456. pData->param.createNew(params, false);
  457. const uint portNameSize(pData->engine->getMaxPortNameSize());
  458. CarlaString portName;
  459. // ---------------------------------------
  460. // Audio Outputs
  461. if (fUses16Outs)
  462. {
  463. for (uint32_t i=0; i < 32; ++i)
  464. {
  465. portName.clear();
  466. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  467. {
  468. portName = pData->name;
  469. portName += ":";
  470. }
  471. portName += "out-";
  472. if ((i+2)/2 < 9)
  473. portName += "0";
  474. portName += CarlaString((i+2)/2);
  475. if (i % 2 == 0)
  476. portName += "L";
  477. else
  478. portName += "R";
  479. portName.truncate(portNameSize);
  480. pData->audioOut.ports[i].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  481. pData->audioOut.ports[i].rindex = i;
  482. }
  483. fAudio16Buffers = new float*[aOuts];
  484. for (uint32_t i=0; i < aOuts; ++i)
  485. fAudio16Buffers[i] = nullptr;
  486. }
  487. else
  488. {
  489. // out-left
  490. portName.clear();
  491. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  492. {
  493. portName = pData->name;
  494. portName += ":";
  495. }
  496. portName += "out-left";
  497. portName.truncate(portNameSize);
  498. pData->audioOut.ports[0].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  499. pData->audioOut.ports[0].rindex = 0;
  500. // out-right
  501. portName.clear();
  502. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  503. {
  504. portName = pData->name;
  505. portName += ":";
  506. }
  507. portName += "out-right";
  508. portName.truncate(portNameSize);
  509. pData->audioOut.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  510. pData->audioOut.ports[1].rindex = 1;
  511. }
  512. // ---------------------------------------
  513. // Event Input
  514. {
  515. portName.clear();
  516. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  517. {
  518. portName = pData->name;
  519. portName += ":";
  520. }
  521. portName += "events-in";
  522. portName.truncate(portNameSize);
  523. pData->event.portIn = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true);
  524. }
  525. // ---------------------------------------
  526. // Event Output
  527. {
  528. portName.clear();
  529. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  530. {
  531. portName = pData->name;
  532. portName += ":";
  533. }
  534. portName += "events-out";
  535. portName.truncate(portNameSize);
  536. pData->event.portOut = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false);
  537. }
  538. // ---------------------------------------
  539. // Parameters
  540. {
  541. int j;
  542. // ----------------------
  543. j = FluidSynthReverbOnOff;
  544. pData->param.data[j].type = PARAMETER_INPUT;
  545. pData->param.data[j].hints = PARAMETER_IS_ENABLED /*| PARAMETER_IS_AUTOMABLE*/ | PARAMETER_IS_BOOLEAN;
  546. pData->param.data[j].index = j;
  547. pData->param.data[j].rindex = j;
  548. pData->param.data[j].midiCC = -1;
  549. pData->param.data[j].midiChannel = 0;
  550. pData->param.ranges[j].min = 0.0f;
  551. pData->param.ranges[j].max = 1.0f;
  552. pData->param.ranges[j].def = 1.0f;
  553. pData->param.ranges[j].step = 1.0f;
  554. pData->param.ranges[j].stepSmall = 1.0f;
  555. pData->param.ranges[j].stepLarge = 1.0f;
  556. // ----------------------
  557. j = FluidSynthReverbRoomSize;
  558. pData->param.data[j].type = PARAMETER_INPUT;
  559. pData->param.data[j].hints = PARAMETER_IS_ENABLED /*| PARAMETER_IS_AUTOMABLE*/;
  560. pData->param.data[j].index = j;
  561. pData->param.data[j].rindex = j;
  562. pData->param.data[j].midiCC = -1;
  563. pData->param.data[j].midiChannel = 0;
  564. pData->param.ranges[j].min = 0.0f;
  565. pData->param.ranges[j].max = 1.2f;
  566. pData->param.ranges[j].def = FLUID_REVERB_DEFAULT_ROOMSIZE;
  567. pData->param.ranges[j].step = 0.01f;
  568. pData->param.ranges[j].stepSmall = 0.0001f;
  569. pData->param.ranges[j].stepLarge = 0.1f;
  570. // ----------------------
  571. j = FluidSynthReverbDamp;
  572. pData->param.data[j].type = PARAMETER_INPUT;
  573. pData->param.data[j].hints = PARAMETER_IS_ENABLED /*| PARAMETER_IS_AUTOMABLE*/;
  574. pData->param.data[j].index = j;
  575. pData->param.data[j].rindex = j;
  576. pData->param.data[j].midiCC = -1;
  577. pData->param.data[j].midiChannel = 0;
  578. pData->param.ranges[j].min = 0.0f;
  579. pData->param.ranges[j].max = 1.0f;
  580. pData->param.ranges[j].def = FLUID_REVERB_DEFAULT_DAMP;
  581. pData->param.ranges[j].step = 0.01f;
  582. pData->param.ranges[j].stepSmall = 0.0001f;
  583. pData->param.ranges[j].stepLarge = 0.1f;
  584. // ----------------------
  585. j = FluidSynthReverbLevel;
  586. pData->param.data[j].type = PARAMETER_INPUT;
  587. pData->param.data[j].hints = PARAMETER_IS_ENABLED /*| PARAMETER_IS_AUTOMABLE*/;
  588. pData->param.data[j].index = j;
  589. pData->param.data[j].rindex = j;
  590. pData->param.data[j].midiCC = MIDI_CONTROL_REVERB_SEND_LEVEL;
  591. pData->param.data[j].midiChannel = 0;
  592. pData->param.ranges[j].min = 0.0f;
  593. pData->param.ranges[j].max = 1.0f;
  594. pData->param.ranges[j].def = FLUID_REVERB_DEFAULT_LEVEL;
  595. pData->param.ranges[j].step = 0.01f;
  596. pData->param.ranges[j].stepSmall = 0.0001f;
  597. pData->param.ranges[j].stepLarge = 0.1f;
  598. // ----------------------
  599. j = FluidSynthReverbWidth;
  600. pData->param.data[j].type = PARAMETER_INPUT;
  601. pData->param.data[j].hints = PARAMETER_IS_ENABLED /*| PARAMETER_IS_AUTOMABLE*/;
  602. pData->param.data[j].index = j;
  603. pData->param.data[j].rindex = j;
  604. pData->param.data[j].midiCC = -1;
  605. pData->param.data[j].midiChannel = 0;
  606. pData->param.ranges[j].min = 0.0f;
  607. pData->param.ranges[j].max = 10.0f; // should be 100, but that sounds too much
  608. pData->param.ranges[j].def = FLUID_REVERB_DEFAULT_WIDTH;
  609. pData->param.ranges[j].step = 0.01f;
  610. pData->param.ranges[j].stepSmall = 0.0001f;
  611. pData->param.ranges[j].stepLarge = 0.1f;
  612. // ----------------------
  613. j = FluidSynthChorusOnOff;
  614. pData->param.data[j].type = PARAMETER_INPUT;
  615. pData->param.data[j].hints = PARAMETER_IS_ENABLED | PARAMETER_IS_BOOLEAN;
  616. pData->param.data[j].index = j;
  617. pData->param.data[j].rindex = j;
  618. pData->param.data[j].midiCC = -1;
  619. pData->param.data[j].midiChannel = 0;
  620. pData->param.ranges[j].min = 0.0f;
  621. pData->param.ranges[j].max = 1.0f;
  622. pData->param.ranges[j].def = 1.0f;
  623. pData->param.ranges[j].step = 1.0f;
  624. pData->param.ranges[j].stepSmall = 1.0f;
  625. pData->param.ranges[j].stepLarge = 1.0f;
  626. // ----------------------
  627. j = FluidSynthChorusNr;
  628. pData->param.data[j].type = PARAMETER_INPUT;
  629. pData->param.data[j].hints = PARAMETER_IS_ENABLED | PARAMETER_IS_INTEGER;
  630. pData->param.data[j].index = j;
  631. pData->param.data[j].rindex = j;
  632. pData->param.data[j].midiCC = -1;
  633. pData->param.data[j].midiChannel = 0;
  634. pData->param.ranges[j].min = 0.0f;
  635. pData->param.ranges[j].max = 99.0f;
  636. pData->param.ranges[j].def = FLUID_CHORUS_DEFAULT_N;
  637. pData->param.ranges[j].step = 1.0f;
  638. pData->param.ranges[j].stepSmall = 1.0f;
  639. pData->param.ranges[j].stepLarge = 10.0f;
  640. // ----------------------
  641. j = FluidSynthChorusLevel;
  642. pData->param.data[j].type = PARAMETER_INPUT;
  643. pData->param.data[j].hints = PARAMETER_IS_ENABLED;
  644. pData->param.data[j].index = j;
  645. pData->param.data[j].rindex = j;
  646. pData->param.data[j].midiCC = 0; //MIDI_CONTROL_CHORUS_SEND_LEVEL;
  647. pData->param.data[j].midiChannel = 0;
  648. pData->param.ranges[j].min = 0.0f;
  649. pData->param.ranges[j].max = 10.0f;
  650. pData->param.ranges[j].def = FLUID_CHORUS_DEFAULT_LEVEL;
  651. pData->param.ranges[j].step = 0.01f;
  652. pData->param.ranges[j].stepSmall = 0.0001f;
  653. pData->param.ranges[j].stepLarge = 0.1f;
  654. // ----------------------
  655. j = FluidSynthChorusSpeedHz;
  656. pData->param.data[j].type = PARAMETER_INPUT;
  657. pData->param.data[j].hints = PARAMETER_IS_ENABLED;
  658. pData->param.data[j].index = j;
  659. pData->param.data[j].rindex = j;
  660. pData->param.data[j].midiCC = -1;
  661. pData->param.data[j].midiChannel = 0;
  662. pData->param.ranges[j].min = 0.29f;
  663. pData->param.ranges[j].max = 5.0f;
  664. pData->param.ranges[j].def = FLUID_CHORUS_DEFAULT_SPEED;
  665. pData->param.ranges[j].step = 0.01f;
  666. pData->param.ranges[j].stepSmall = 0.0001f;
  667. pData->param.ranges[j].stepLarge = 0.1f;
  668. // ----------------------
  669. j = FluidSynthChorusDepthMs;
  670. pData->param.data[j].type = PARAMETER_INPUT;
  671. pData->param.data[j].hints = PARAMETER_IS_ENABLED;
  672. pData->param.data[j].index = j;
  673. pData->param.data[j].rindex = j;
  674. pData->param.data[j].midiCC = -1;
  675. pData->param.data[j].midiChannel = 0;
  676. pData->param.ranges[j].min = 0.0f;
  677. pData->param.ranges[j].max = float(2048.0 * 1000.0 / pData->engine->getSampleRate()); // FIXME?
  678. pData->param.ranges[j].def = FLUID_CHORUS_DEFAULT_DEPTH;
  679. pData->param.ranges[j].step = 0.01f;
  680. pData->param.ranges[j].stepSmall = 0.0001f;
  681. pData->param.ranges[j].stepLarge = 0.1f;
  682. // ----------------------
  683. j = FluidSynthChorusType;
  684. pData->param.data[j].type = PARAMETER_INPUT;
  685. pData->param.data[j].hints = PARAMETER_IS_ENABLED | PARAMETER_IS_INTEGER | PARAMETER_USES_SCALEPOINTS;
  686. pData->param.data[j].index = j;
  687. pData->param.data[j].rindex = j;
  688. pData->param.data[j].midiCC = -1;
  689. pData->param.data[j].midiChannel = 0;
  690. pData->param.ranges[j].min = FLUID_CHORUS_MOD_SINE;
  691. pData->param.ranges[j].max = FLUID_CHORUS_MOD_TRIANGLE;
  692. pData->param.ranges[j].def = FLUID_CHORUS_DEFAULT_TYPE;
  693. pData->param.ranges[j].step = 1.0f;
  694. pData->param.ranges[j].stepSmall = 1.0f;
  695. pData->param.ranges[j].stepLarge = 1.0f;
  696. // ----------------------
  697. j = FluidSynthPolyphony;
  698. pData->param.data[j].type = PARAMETER_INPUT;
  699. pData->param.data[j].hints = PARAMETER_IS_ENABLED | PARAMETER_IS_INTEGER;
  700. pData->param.data[j].index = j;
  701. pData->param.data[j].rindex = j;
  702. pData->param.data[j].midiCC = -1;
  703. pData->param.data[j].midiChannel = 0;
  704. pData->param.ranges[j].min = 1.0f;
  705. pData->param.ranges[j].max = 512.0f; // max theoric is 65535
  706. pData->param.ranges[j].def = (float)fluid_synth_get_polyphony(fSynth);
  707. pData->param.ranges[j].step = 1.0f;
  708. pData->param.ranges[j].stepSmall = 1.0f;
  709. pData->param.ranges[j].stepLarge = 10.0f;
  710. // ----------------------
  711. j = FluidSynthInterpolation;
  712. pData->param.data[j].type = PARAMETER_INPUT;
  713. pData->param.data[j].hints = PARAMETER_IS_ENABLED | PARAMETER_IS_INTEGER | PARAMETER_USES_SCALEPOINTS;
  714. pData->param.data[j].index = j;
  715. pData->param.data[j].rindex = j;
  716. pData->param.data[j].midiCC = -1;
  717. pData->param.data[j].midiChannel = 0;
  718. pData->param.ranges[j].min = FLUID_INTERP_NONE;
  719. pData->param.ranges[j].max = FLUID_INTERP_HIGHEST;
  720. pData->param.ranges[j].def = FLUID_INTERP_DEFAULT;
  721. pData->param.ranges[j].step = 1.0f;
  722. pData->param.ranges[j].stepSmall = 1.0f;
  723. pData->param.ranges[j].stepLarge = 1.0f;
  724. // ----------------------
  725. j = FluidSynthVoiceCount;
  726. pData->param.data[j].type = PARAMETER_OUTPUT;
  727. pData->param.data[j].hints = PARAMETER_IS_ENABLED | PARAMETER_IS_AUTOMABLE | PARAMETER_IS_INTEGER;
  728. pData->param.data[j].index = j;
  729. pData->param.data[j].rindex = j;
  730. pData->param.data[j].midiCC = -1;
  731. pData->param.data[j].midiChannel = 0;
  732. pData->param.ranges[j].min = 0.0f;
  733. pData->param.ranges[j].max = 65535.0f;
  734. pData->param.ranges[j].def = 0.0f;
  735. pData->param.ranges[j].step = 1.0f;
  736. pData->param.ranges[j].stepSmall = 1.0f;
  737. pData->param.ranges[j].stepLarge = 1.0f;
  738. for (j=0; j<FluidSynthParametersMax; ++j)
  739. fParamBuffers[j] = pData->param.ranges[j].def;
  740. }
  741. // ---------------------------------------
  742. // plugin hints
  743. pData->hints = 0x0;
  744. pData->hints |= PLUGIN_IS_SYNTH;
  745. pData->hints |= PLUGIN_CAN_VOLUME;
  746. if (! fUses16Outs)
  747. pData->hints |= PLUGIN_CAN_BALANCE;
  748. // extra plugin hints
  749. pData->extraHints = 0x0;
  750. pData->extraHints |= PLUGIN_EXTRA_HINT_HAS_MIDI_IN;
  751. if (fUses16Outs)
  752. pData->extraHints |= PLUGIN_EXTRA_HINT_USES_MULTI_PROGS;
  753. else
  754. pData->extraHints |= PLUGIN_EXTRA_HINT_CAN_RUN_RACK;
  755. bufferSizeChanged(pData->engine->getBufferSize());
  756. reloadPrograms(true);
  757. if (pData->active)
  758. activate();
  759. carla_debug("FluidSynthPlugin::reload() - end");
  760. }
  761. void reloadPrograms(const bool doInit) override
  762. {
  763. carla_debug("FluidSynthPlugin::reloadPrograms(%s)", bool2str(doInit));
  764. // save drum info in case we have one program for it
  765. bool hasDrums = false;
  766. uint32_t drumIndex, drumProg;
  767. drumIndex = drumProg = 0;
  768. // Delete old programs
  769. pData->midiprog.clear();
  770. // Query new programs
  771. uint32_t count = 0;
  772. if (fluid_sfont_t* const f_sfont = fluid_synth_get_sfont_by_id(fSynth, fSynthId))
  773. {
  774. fluid_preset_t f_preset;
  775. // initial check to know how many midi-programs we have
  776. f_sfont->iteration_start(f_sfont);
  777. while (f_sfont->iteration_next(f_sfont, &f_preset))
  778. ++count;
  779. // sound kits must always have at least 1 midi-program
  780. CARLA_SAFE_ASSERT_RETURN(count > 0,);
  781. pData->midiprog.createNew(count);
  782. // Update data
  783. int tmp;
  784. uint32_t i = 0;
  785. f_sfont->iteration_start(f_sfont);
  786. for (; f_sfont->iteration_next(f_sfont, &f_preset);)
  787. {
  788. CARLA_SAFE_ASSERT_BREAK(i < count);
  789. tmp = f_preset.get_banknum(&f_preset);
  790. pData->midiprog.data[i].bank = (tmp >= 0) ? static_cast<uint32_t>(tmp) : 0;
  791. tmp = f_preset.get_num(&f_preset);
  792. pData->midiprog.data[i].program = (tmp >= 0) ? static_cast<uint32_t>(tmp) : 0;
  793. pData->midiprog.data[i].name = carla_strdup(f_preset.get_name(&f_preset));
  794. if (pData->midiprog.data[i].bank == 128 && ! hasDrums)
  795. {
  796. hasDrums = true;
  797. drumIndex = i;
  798. drumProg = pData->midiprog.data[i].program;
  799. }
  800. ++i;
  801. }
  802. }
  803. else
  804. {
  805. // failing means 0 midi-programs, it shouldn't happen!
  806. carla_safe_assert("fluid_sfont_t* const f_sfont = fluid_synth_get_sfont_by_id(fSynth, fSynthId)", __FILE__, __LINE__);
  807. return;
  808. }
  809. #ifndef BUILD_BRIDGE
  810. // Update OSC Names
  811. if (pData->engine->isOscControlRegistered())
  812. {
  813. pData->engine->oscSend_control_set_midi_program_count(pData->id, count);
  814. for (uint32_t i=0; i < count; ++i)
  815. 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);
  816. }
  817. #endif
  818. if (doInit)
  819. {
  820. fluid_synth_program_reset(fSynth);
  821. // select first program, or 128 for ch10
  822. for (int i=0; i < MAX_MIDI_CHANNELS && i != 9; ++i)
  823. {
  824. #ifdef FLUIDSYNTH_VERSION_NEW_API
  825. fluid_synth_set_channel_type(fSynth, i, CHANNEL_TYPE_MELODIC);
  826. #endif
  827. fluid_synth_program_select(fSynth, i, fSynthId, pData->midiprog.data[0].bank, pData->midiprog.data[0].program);
  828. fCurMidiProgs[i] = 0;
  829. }
  830. if (hasDrums)
  831. {
  832. #ifdef FLUIDSYNTH_VERSION_NEW_API
  833. fluid_synth_set_channel_type(fSynth, 9, CHANNEL_TYPE_DRUM);
  834. #endif
  835. fluid_synth_program_select(fSynth, 9, fSynthId, 128, drumProg);
  836. fCurMidiProgs[9] = static_cast<int32_t>(drumIndex);
  837. }
  838. else
  839. {
  840. #ifdef FLUIDSYNTH_VERSION_NEW_API
  841. fluid_synth_set_channel_type(fSynth, 9, CHANNEL_TYPE_MELODIC);
  842. #endif
  843. fluid_synth_program_select(fSynth, 9, fSynthId, pData->midiprog.data[0].bank, pData->midiprog.data[0].program);
  844. fCurMidiProgs[9] = 0;
  845. }
  846. pData->midiprog.current = 0;
  847. }
  848. else
  849. {
  850. pData->engine->callback(ENGINE_CALLBACK_RELOAD_PROGRAMS, pData->id, 0, 0, 0.0f, nullptr);
  851. }
  852. }
  853. // -------------------------------------------------------------------
  854. // Plugin processing
  855. void process(float** const, float** const outBuffer, const uint32_t frames) override
  856. {
  857. // --------------------------------------------------------------------------------------------------------
  858. // Check if active
  859. if (! pData->active)
  860. {
  861. // disable any output sound
  862. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  863. FLOAT_CLEAR(outBuffer[i], frames);
  864. return;
  865. }
  866. // --------------------------------------------------------------------------------------------------------
  867. // Check if needs reset
  868. if (pData->needsReset)
  869. {
  870. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  871. {
  872. for (int i=0; i < MAX_MIDI_CHANNELS; ++i)
  873. {
  874. #ifdef FLUIDSYNTH_VERSION_NEW_API
  875. fluid_synth_all_notes_off(fSynth, i);
  876. fluid_synth_all_sounds_off(fSynth, i);
  877. #else
  878. fluid_synth_cc(fSynth, i, MIDI_CONTROL_ALL_SOUND_OFF, 0);
  879. fluid_synth_cc(fSynth, i, MIDI_CONTROL_ALL_NOTES_OFF, 0);
  880. #endif
  881. }
  882. }
  883. else if (pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  884. {
  885. for (int i=0; i < MAX_MIDI_NOTE; ++i)
  886. fluid_synth_noteoff(fSynth, pData->ctrlChannel, i);
  887. }
  888. pData->needsReset = false;
  889. }
  890. // --------------------------------------------------------------------------------------------------------
  891. // Event Input and Processing
  892. {
  893. // ----------------------------------------------------------------------------------------------------
  894. // MIDI Input (External)
  895. if (pData->extNotes.mutex.tryLock())
  896. {
  897. for (RtLinkedList<ExternalMidiNote>::Itenerator it = pData->extNotes.data.begin(); it.valid(); it.next())
  898. {
  899. const ExternalMidiNote& note(it.getValue());
  900. CARLA_SAFE_ASSERT_CONTINUE(note.channel >= 0 && note.channel < MAX_MIDI_CHANNELS);
  901. if (note.velo > 0)
  902. fluid_synth_noteon(fSynth, note.channel, note.note, note.velo);
  903. else
  904. fluid_synth_noteoff(fSynth,note.channel, note.note);
  905. }
  906. pData->extNotes.data.clear();
  907. pData->extNotes.mutex.unlock();
  908. } // End of MIDI Input (External)
  909. // ----------------------------------------------------------------------------------------------------
  910. // Event Input (System)
  911. bool allNotesOffSent = false;
  912. uint32_t time, nEvents = pData->event.portIn->getEventCount();
  913. uint32_t timeOffset = 0;
  914. uint32_t nextBankIds[MAX_MIDI_CHANNELS] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 0, 0, 0, 0, 0, 0 };
  915. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0 && pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  916. nextBankIds[pData->ctrlChannel] = pData->midiprog.data[pData->midiprog.current].bank;
  917. for (uint32_t i=0; i < nEvents; ++i)
  918. {
  919. const EngineEvent& event(pData->event.portIn->getEvent(i));
  920. time = event.time;
  921. CARLA_SAFE_ASSERT_CONTINUE(time < frames);
  922. CARLA_SAFE_ASSERT_BREAK(time >= timeOffset);
  923. if (time > timeOffset)
  924. {
  925. if (processSingle(outBuffer, time - timeOffset, timeOffset))
  926. {
  927. timeOffset = time;
  928. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0 && pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  929. nextBankIds[pData->ctrlChannel] = pData->midiprog.data[pData->midiprog.current].bank;
  930. }
  931. }
  932. // Control change
  933. switch (event.type)
  934. {
  935. case kEngineEventTypeNull:
  936. break;
  937. case kEngineEventTypeControl:
  938. {
  939. const EngineControlEvent& ctrlEvent = event.ctrl;
  940. switch (ctrlEvent.type)
  941. {
  942. case kEngineControlEventTypeNull:
  943. break;
  944. case kEngineControlEventTypeParameter:
  945. {
  946. #ifndef BUILD_BRIDGE
  947. // Control backend stuff
  948. if (event.channel == pData->ctrlChannel)
  949. {
  950. float value;
  951. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_DRYWET) != 0)
  952. {
  953. value = ctrlEvent.value;
  954. setDryWet(value, false, false);
  955. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, value);
  956. }
  957. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_VOLUME) != 0)
  958. {
  959. value = ctrlEvent.value*127.0f/100.0f;
  960. setVolume(value, false, false);
  961. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
  962. }
  963. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_BALANCE) != 0)
  964. {
  965. float left, right;
  966. value = ctrlEvent.value/0.5f - 1.0f;
  967. if (value < 0.0f)
  968. {
  969. left = -1.0f;
  970. right = (value*2.0f)+1.0f;
  971. }
  972. else if (value > 0.0f)
  973. {
  974. left = (value*2.0f)-1.0f;
  975. right = 1.0f;
  976. }
  977. else
  978. {
  979. left = -1.0f;
  980. right = 1.0f;
  981. }
  982. setBalanceLeft(left, false, false);
  983. setBalanceRight(right, false, false);
  984. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  985. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  986. }
  987. }
  988. #endif
  989. // Control plugin parameters
  990. for (uint32_t k=0; k < pData->param.count; ++k)
  991. {
  992. if (pData->param.data[k].midiChannel != event.channel)
  993. continue;
  994. if (pData->param.data[k].midiCC != ctrlEvent.param)
  995. continue;
  996. if (pData->param.data[k].hints != PARAMETER_INPUT)
  997. continue;
  998. if ((pData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  999. continue;
  1000. float value;
  1001. if (pData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  1002. {
  1003. value = (ctrlEvent.value < 0.5f) ? pData->param.ranges[k].min : pData->param.ranges[k].max;
  1004. }
  1005. else
  1006. {
  1007. value = pData->param.ranges[k].getUnnormalizedValue(ctrlEvent.value);
  1008. if (pData->param.data[k].hints & PARAMETER_IS_INTEGER)
  1009. value = std::rint(value);
  1010. }
  1011. setParameterValue(k, value, false, false, false);
  1012. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, value);
  1013. }
  1014. if ((pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0 && ctrlEvent.param <= 0x5F)
  1015. {
  1016. fluid_synth_cc(fSynth, event.channel, ctrlEvent.param, int(ctrlEvent.value*127.0f));
  1017. }
  1018. break;
  1019. }
  1020. case kEngineControlEventTypeMidiBank:
  1021. if (event.channel < MAX_MIDI_CHANNELS && (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  1022. nextBankIds[event.channel] = ctrlEvent.param;
  1023. break;
  1024. case kEngineControlEventTypeMidiProgram:
  1025. if (event.channel < MAX_MIDI_CHANNELS && (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  1026. {
  1027. const uint32_t bankId(nextBankIds[event.channel]);
  1028. const uint32_t progId(ctrlEvent.param);
  1029. for (uint32_t k=0; k < pData->midiprog.count; ++k)
  1030. {
  1031. if (pData->midiprog.data[k].bank == bankId && pData->midiprog.data[k].program == progId)
  1032. {
  1033. fluid_synth_program_select(fSynth, event.channel, fSynthId, bankId, progId);
  1034. fCurMidiProgs[event.channel] = static_cast<int32_t>(k);
  1035. if (event.channel == pData->ctrlChannel)
  1036. pData->postponeRtEvent(kPluginPostRtEventMidiProgramChange, static_cast<int32_t>(k), 0, 0.0f);
  1037. break;
  1038. }
  1039. }
  1040. }
  1041. break;
  1042. case kEngineControlEventTypeAllSoundOff:
  1043. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1044. {
  1045. #ifdef FLUIDSYNTH_VERSION_NEW_API
  1046. fluid_synth_all_sounds_off(fSynth, event.channel);
  1047. #else
  1048. fluid_synth_cc(fSynth, event.channel, MIDI_CONTROL_ALL_SOUND_OFF, 0);
  1049. #endif
  1050. }
  1051. break;
  1052. case kEngineControlEventTypeAllNotesOff:
  1053. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1054. {
  1055. if (event.channel == pData->ctrlChannel && ! allNotesOffSent)
  1056. {
  1057. allNotesOffSent = true;
  1058. sendMidiAllNotesOffToCallback();
  1059. }
  1060. #ifdef FLUIDSYNTH_VERSION_NEW_API
  1061. fluid_synth_all_notes_off(fSynth, event.channel);
  1062. #else
  1063. fluid_synth_cc(fSynth, event.channel, MIDI_CONTROL_ALL_NOTES_OFF, 0);
  1064. #endif
  1065. }
  1066. break;
  1067. }
  1068. break;
  1069. }
  1070. case kEngineEventTypeMidi:
  1071. {
  1072. const EngineMidiEvent& midiEvent(event.midi);
  1073. uint8_t status = uint8_t(MIDI_GET_STATUS_FROM_DATA(midiEvent.data));
  1074. uint8_t channel = event.channel;
  1075. // Fix bad note-off
  1076. if (MIDI_IS_STATUS_NOTE_ON(status) && midiEvent.data[2] == 0)
  1077. status = MIDI_STATUS_NOTE_OFF;
  1078. if (MIDI_IS_STATUS_NOTE_OFF(status))
  1079. {
  1080. const uint8_t note = midiEvent.data[1];
  1081. fluid_synth_noteoff(fSynth, channel, note);
  1082. pData->postponeRtEvent(kPluginPostRtEventNoteOff, channel, note, 0.0f);
  1083. }
  1084. else if (MIDI_IS_STATUS_NOTE_ON(status))
  1085. {
  1086. const uint8_t note = midiEvent.data[1];
  1087. const uint8_t velo = midiEvent.data[2];
  1088. fluid_synth_noteon(fSynth, channel, note, velo);
  1089. pData->postponeRtEvent(kPluginPostRtEventNoteOn, channel, note, velo);
  1090. }
  1091. else if (MIDI_IS_STATUS_POLYPHONIC_AFTERTOUCH(status) && (pData->options & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) != 0)
  1092. {
  1093. //const uint8_t note = midiEvent.data[1];
  1094. //const uint8_t pressure = midiEvent.data[2];
  1095. // TODO, not in fluidsynth API
  1096. }
  1097. else if (MIDI_IS_STATUS_CONTROL_CHANGE(status) && (pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0)
  1098. {
  1099. const uint8_t control = midiEvent.data[1];
  1100. const uint8_t value = midiEvent.data[2];
  1101. fluid_synth_cc(fSynth, channel, control, value);
  1102. }
  1103. else if (MIDI_IS_STATUS_CHANNEL_PRESSURE(status) && (pData->options & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) != 0)
  1104. {
  1105. const uint8_t pressure = midiEvent.data[1];
  1106. fluid_synth_channel_pressure(fSynth, channel, pressure);;
  1107. }
  1108. else if (MIDI_IS_STATUS_PITCH_WHEEL_CONTROL(status) && (pData->options & PLUGIN_OPTION_SEND_PITCHBEND) != 0)
  1109. {
  1110. const uint8_t lsb = midiEvent.data[1];
  1111. const uint8_t msb = midiEvent.data[2];
  1112. const int value = ((msb << 7) | lsb) - 8192;
  1113. fluid_synth_pitch_bend(fSynth, channel, value);
  1114. }
  1115. break;
  1116. }
  1117. }
  1118. }
  1119. pData->postRtEvents.trySplice();
  1120. if (frames > timeOffset)
  1121. processSingle(outBuffer, frames - timeOffset, timeOffset);
  1122. } // End of Event Input and Processing
  1123. CARLA_PROCESS_CONTINUE_CHECK;
  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. FLOAT_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. FLOAT_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