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.

1680 lines
60KB

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