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.

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