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.

1730 lines
63KB

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