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.

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