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.

1731 lines
63KB

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