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.

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