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.

1689 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. FloatVectorOperations::clear(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_SAFE_ASSERT_RETURN(pData->engine != nullptr,);
  427. CARLA_SAFE_ASSERT_RETURN(fSynth != nullptr,);
  428. carla_debug("FluidSynthPlugin::reload() - start");
  429. const ProcessMode processMode(pData->engine->getProccessMode());
  430. // Safely disable plugin for reload
  431. const ScopedDisabler sd(this);
  432. if (pData->active)
  433. deactivate();
  434. clearBuffers();
  435. uint32_t aOuts, params, j;
  436. aOuts = kUses16Outs ? 32 : 2;
  437. params = FluidSynthParametersMax;
  438. pData->audioOut.createNew(aOuts);
  439. pData->param.createNew(params);
  440. const int portNameSize(pData->engine->getMaxPortNameSize());
  441. CarlaString portName;
  442. // ---------------------------------------
  443. // Audio Outputs
  444. if (kUses16Outs)
  445. {
  446. for (j=0; j < 32; ++j)
  447. {
  448. portName.clear();
  449. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  450. {
  451. portName = fName;
  452. portName += ":";
  453. }
  454. portName += "out-";
  455. if ((j+2)/2 < 9)
  456. portName += "0";
  457. portName += CarlaString((j+2)/2);
  458. if (j % 2 == 0)
  459. portName += "L";
  460. else
  461. portName += "R";
  462. portName.truncate(portNameSize);
  463. pData->audioOut.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  464. pData->audioOut.ports[j].rindex = j;
  465. }
  466. fAudio16Buffers = new float*[aOuts];
  467. for (j=0; j < aOuts; ++j)
  468. fAudio16Buffers[j] = nullptr;
  469. }
  470. else
  471. {
  472. // out-left
  473. portName.clear();
  474. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  475. {
  476. portName = fName;
  477. portName += ":";
  478. }
  479. portName += "out-left";
  480. portName.truncate(portNameSize);
  481. pData->audioOut.ports[0].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  482. pData->audioOut.ports[0].rindex = 0;
  483. // out-right
  484. portName.clear();
  485. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  486. {
  487. portName = fName;
  488. portName += ":";
  489. }
  490. portName += "out-right";
  491. portName.truncate(portNameSize);
  492. pData->audioOut.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  493. pData->audioOut.ports[1].rindex = 1;
  494. }
  495. // ---------------------------------------
  496. // Event Input
  497. {
  498. portName.clear();
  499. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  500. {
  501. portName = fName;
  502. portName += ":";
  503. }
  504. portName += "event-in";
  505. portName.truncate(portNameSize);
  506. pData->event.portIn = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true);
  507. }
  508. // ---------------------------------------
  509. // Event Output
  510. {
  511. portName.clear();
  512. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  513. {
  514. portName = fName;
  515. portName += ":";
  516. }
  517. portName += "event-out";
  518. portName.truncate(portNameSize);
  519. pData->event.portOut = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false);
  520. }
  521. // ----------------------
  522. j = FluidSynthReverbOnOff;
  523. pData->param.data[j].index = j;
  524. pData->param.data[j].rindex = j;
  525. pData->param.data[j].type = PARAMETER_INPUT;
  526. pData->param.data[j].hints = PARAMETER_IS_ENABLED | PARAMETER_IS_AUTOMABLE | PARAMETER_IS_BOOLEAN;
  527. pData->param.data[j].midiChannel = 0;
  528. pData->param.data[j].midiCC = -1;
  529. pData->param.ranges[j].min = 0.0f;
  530. pData->param.ranges[j].max = 1.0f;
  531. pData->param.ranges[j].def = 0.0f; // off
  532. pData->param.ranges[j].step = 1.0f;
  533. pData->param.ranges[j].stepSmall = 1.0f;
  534. pData->param.ranges[j].stepLarge = 1.0f;
  535. fParamBuffers[j] = pData->param.ranges[j].def;
  536. // ----------------------
  537. j = FluidSynthReverbRoomSize;
  538. pData->param.data[j].index = j;
  539. pData->param.data[j].rindex = j;
  540. pData->param.data[j].type = PARAMETER_INPUT;
  541. pData->param.data[j].hints = PARAMETER_IS_ENABLED | PARAMETER_IS_AUTOMABLE;
  542. pData->param.data[j].midiChannel = 0;
  543. pData->param.data[j].midiCC = -1;
  544. pData->param.ranges[j].min = 0.0f;
  545. pData->param.ranges[j].max = 1.2f;
  546. pData->param.ranges[j].def = FLUID_REVERB_DEFAULT_ROOMSIZE;
  547. pData->param.ranges[j].step = 0.01f;
  548. pData->param.ranges[j].stepSmall = 0.0001f;
  549. pData->param.ranges[j].stepLarge = 0.1f;
  550. fParamBuffers[j] = pData->param.ranges[j].def;
  551. // ----------------------
  552. j = FluidSynthReverbDamp;
  553. pData->param.data[j].index = j;
  554. pData->param.data[j].rindex = j;
  555. pData->param.data[j].type = PARAMETER_INPUT;
  556. pData->param.data[j].hints = PARAMETER_IS_ENABLED | PARAMETER_IS_AUTOMABLE;
  557. pData->param.data[j].midiChannel = 0;
  558. pData->param.data[j].midiCC = -1;
  559. pData->param.ranges[j].min = 0.0f;
  560. pData->param.ranges[j].max = 1.0f;
  561. pData->param.ranges[j].def = FLUID_REVERB_DEFAULT_DAMP;
  562. pData->param.ranges[j].step = 0.01f;
  563. pData->param.ranges[j].stepSmall = 0.0001f;
  564. pData->param.ranges[j].stepLarge = 0.1f;
  565. fParamBuffers[j] = pData->param.ranges[j].def;
  566. // ----------------------
  567. j = FluidSynthReverbLevel;
  568. pData->param.data[j].index = j;
  569. pData->param.data[j].rindex = j;
  570. pData->param.data[j].type = PARAMETER_INPUT;
  571. pData->param.data[j].hints = PARAMETER_IS_ENABLED | PARAMETER_IS_AUTOMABLE;
  572. pData->param.data[j].midiChannel = 0;
  573. pData->param.data[j].midiCC = MIDI_CONTROL_REVERB_SEND_LEVEL;
  574. pData->param.ranges[j].min = 0.0f;
  575. pData->param.ranges[j].max = 1.0f;
  576. pData->param.ranges[j].def = FLUID_REVERB_DEFAULT_LEVEL;
  577. pData->param.ranges[j].step = 0.01f;
  578. pData->param.ranges[j].stepSmall = 0.0001f;
  579. pData->param.ranges[j].stepLarge = 0.1f;
  580. fParamBuffers[j] = pData->param.ranges[j].def;
  581. // ----------------------
  582. j = FluidSynthReverbWidth;
  583. pData->param.data[j].index = j;
  584. pData->param.data[j].rindex = j;
  585. pData->param.data[j].type = PARAMETER_INPUT;
  586. pData->param.data[j].hints = PARAMETER_IS_ENABLED | PARAMETER_IS_AUTOMABLE;
  587. pData->param.data[j].midiChannel = 0;
  588. pData->param.data[j].midiCC = -1;
  589. pData->param.ranges[j].min = 0.0f;
  590. pData->param.ranges[j].max = 10.0f; // should be 100, but that sounds too much
  591. pData->param.ranges[j].def = FLUID_REVERB_DEFAULT_WIDTH;
  592. pData->param.ranges[j].step = 0.01f;
  593. pData->param.ranges[j].stepSmall = 0.0001f;
  594. pData->param.ranges[j].stepLarge = 0.1f;
  595. fParamBuffers[j] = pData->param.ranges[j].def;
  596. // ----------------------
  597. j = FluidSynthChorusOnOff;
  598. pData->param.data[j].index = j;
  599. pData->param.data[j].rindex = j;
  600. pData->param.data[j].type = PARAMETER_INPUT;
  601. pData->param.data[j].hints = PARAMETER_IS_ENABLED | PARAMETER_IS_BOOLEAN;
  602. pData->param.data[j].midiChannel = 0;
  603. pData->param.data[j].midiCC = -1;
  604. pData->param.ranges[j].min = 0.0f;
  605. pData->param.ranges[j].max = 1.0f;
  606. pData->param.ranges[j].def = 0.0f; // off
  607. pData->param.ranges[j].step = 1.0f;
  608. pData->param.ranges[j].stepSmall = 1.0f;
  609. pData->param.ranges[j].stepLarge = 1.0f;
  610. fParamBuffers[j] = pData->param.ranges[j].def;
  611. // ----------------------
  612. j = FluidSynthChorusNr;
  613. pData->param.data[j].index = j;
  614. pData->param.data[j].rindex = j;
  615. pData->param.data[j].type = PARAMETER_INPUT;
  616. pData->param.data[j].hints = PARAMETER_IS_ENABLED | PARAMETER_IS_INTEGER;
  617. pData->param.data[j].midiChannel = 0;
  618. pData->param.data[j].midiCC = -1;
  619. pData->param.ranges[j].min = 0.0f;
  620. pData->param.ranges[j].max = 99.0f;
  621. pData->param.ranges[j].def = FLUID_CHORUS_DEFAULT_N;
  622. pData->param.ranges[j].step = 1.0f;
  623. pData->param.ranges[j].stepSmall = 1.0f;
  624. pData->param.ranges[j].stepLarge = 10.0f;
  625. fParamBuffers[j] = pData->param.ranges[j].def;
  626. // ----------------------
  627. j = FluidSynthChorusLevel;
  628. pData->param.data[j].index = j;
  629. pData->param.data[j].rindex = j;
  630. pData->param.data[j].type = PARAMETER_INPUT;
  631. pData->param.data[j].hints = PARAMETER_IS_ENABLED;
  632. pData->param.data[j].midiChannel = 0;
  633. pData->param.data[j].midiCC = 0; //MIDI_CONTROL_CHORUS_SEND_LEVEL;
  634. pData->param.ranges[j].min = 0.0f;
  635. pData->param.ranges[j].max = 10.0f;
  636. pData->param.ranges[j].def = FLUID_CHORUS_DEFAULT_LEVEL;
  637. pData->param.ranges[j].step = 0.01f;
  638. pData->param.ranges[j].stepSmall = 0.0001f;
  639. pData->param.ranges[j].stepLarge = 0.1f;
  640. fParamBuffers[j] = pData->param.ranges[j].def;
  641. // ----------------------
  642. j = FluidSynthChorusSpeedHz;
  643. pData->param.data[j].index = j;
  644. pData->param.data[j].rindex = j;
  645. pData->param.data[j].type = PARAMETER_INPUT;
  646. pData->param.data[j].hints = PARAMETER_IS_ENABLED;
  647. pData->param.data[j].midiChannel = 0;
  648. pData->param.data[j].midiCC = -1;
  649. pData->param.ranges[j].min = 0.29f;
  650. pData->param.ranges[j].max = 5.0f;
  651. pData->param.ranges[j].def = FLUID_CHORUS_DEFAULT_SPEED;
  652. pData->param.ranges[j].step = 0.01f;
  653. pData->param.ranges[j].stepSmall = 0.0001f;
  654. pData->param.ranges[j].stepLarge = 0.1f;
  655. fParamBuffers[j] = pData->param.ranges[j].def;
  656. // ----------------------
  657. j = FluidSynthChorusDepthMs;
  658. pData->param.data[j].index = j;
  659. pData->param.data[j].rindex = j;
  660. pData->param.data[j].type = PARAMETER_INPUT;
  661. pData->param.data[j].hints = PARAMETER_IS_ENABLED;
  662. pData->param.data[j].midiChannel = 0;
  663. pData->param.data[j].midiCC = -1;
  664. pData->param.ranges[j].min = 0.0f;
  665. pData->param.ranges[j].max = 2048000.0 / pData->engine->getSampleRate();
  666. pData->param.ranges[j].def = FLUID_CHORUS_DEFAULT_DEPTH;
  667. pData->param.ranges[j].step = 0.01f;
  668. pData->param.ranges[j].stepSmall = 0.0001f;
  669. pData->param.ranges[j].stepLarge = 0.1f;
  670. fParamBuffers[j] = pData->param.ranges[j].def;
  671. // ----------------------
  672. j = FluidSynthChorusType;
  673. pData->param.data[j].index = j;
  674. pData->param.data[j].rindex = j;
  675. pData->param.data[j].type = PARAMETER_INPUT;
  676. pData->param.data[j].hints = PARAMETER_IS_ENABLED | PARAMETER_IS_INTEGER | PARAMETER_USES_SCALEPOINTS;
  677. pData->param.data[j].midiChannel = 0;
  678. pData->param.data[j].midiCC = -1;
  679. pData->param.ranges[j].min = FLUID_CHORUS_MOD_SINE;
  680. pData->param.ranges[j].max = FLUID_CHORUS_MOD_TRIANGLE;
  681. pData->param.ranges[j].def = FLUID_CHORUS_DEFAULT_TYPE;
  682. pData->param.ranges[j].step = 1.0f;
  683. pData->param.ranges[j].stepSmall = 1.0f;
  684. pData->param.ranges[j].stepLarge = 1.0f;
  685. fParamBuffers[j] = pData->param.ranges[j].def;
  686. // ----------------------
  687. j = FluidSynthPolyphony;
  688. pData->param.data[j].index = j;
  689. pData->param.data[j].rindex = j;
  690. pData->param.data[j].type = PARAMETER_INPUT;
  691. pData->param.data[j].hints = PARAMETER_IS_ENABLED | PARAMETER_IS_INTEGER;
  692. pData->param.data[j].midiChannel = 0;
  693. pData->param.data[j].midiCC = -1;
  694. pData->param.ranges[j].min = 1.0f;
  695. pData->param.ranges[j].max = 512.0f; // max theoric is 65535
  696. pData->param.ranges[j].def = fluid_synth_get_polyphony(fSynth);
  697. pData->param.ranges[j].step = 1.0f;
  698. pData->param.ranges[j].stepSmall = 1.0f;
  699. pData->param.ranges[j].stepLarge = 10.0f;
  700. fParamBuffers[j] = pData->param.ranges[j].def;
  701. // ----------------------
  702. j = FluidSynthInterpolation;
  703. pData->param.data[j].index = j;
  704. pData->param.data[j].rindex = j;
  705. pData->param.data[j].type = PARAMETER_INPUT;
  706. pData->param.data[j].hints = PARAMETER_IS_ENABLED | PARAMETER_IS_INTEGER | PARAMETER_USES_SCALEPOINTS;
  707. pData->param.data[j].midiChannel = 0;
  708. pData->param.data[j].midiCC = -1;
  709. pData->param.ranges[j].min = FLUID_INTERP_NONE;
  710. pData->param.ranges[j].max = FLUID_INTERP_HIGHEST;
  711. pData->param.ranges[j].def = FLUID_INTERP_DEFAULT;
  712. pData->param.ranges[j].step = 1.0f;
  713. pData->param.ranges[j].stepSmall = 1.0f;
  714. pData->param.ranges[j].stepLarge = 1.0f;
  715. fParamBuffers[j] = pData->param.ranges[j].def;
  716. // ----------------------
  717. j = FluidSynthVoiceCount;
  718. pData->param.data[j].index = j;
  719. pData->param.data[j].rindex = j;
  720. pData->param.data[j].type = PARAMETER_OUTPUT;
  721. pData->param.data[j].hints = PARAMETER_IS_ENABLED | PARAMETER_IS_AUTOMABLE | PARAMETER_IS_INTEGER;
  722. pData->param.data[j].midiChannel = 0;
  723. pData->param.data[j].midiCC = -1;
  724. pData->param.ranges[j].min = 0.0f;
  725. pData->param.ranges[j].max = 65535.0f;
  726. pData->param.ranges[j].def = 0.0f;
  727. pData->param.ranges[j].step = 1.0f;
  728. pData->param.ranges[j].stepSmall = 1.0f;
  729. pData->param.ranges[j].stepLarge = 1.0f;
  730. fParamBuffers[j] = pData->param.ranges[j].def;
  731. // ---------------------------------------
  732. // plugin hints
  733. fHints = 0x0;
  734. //fHints |= PLUGIN_IS_SYNTH;
  735. fHints |= PLUGIN_CAN_VOLUME;
  736. fHints |= PLUGIN_CAN_BALANCE;
  737. // extra plugin hints
  738. pData->extraHints = 0x0;
  739. pData->extraHints |= PLUGIN_EXTRA_HINT_HAS_MIDI_IN;
  740. pData->extraHints |= PLUGIN_EXTRA_HINT_CAN_RUN_RACK;
  741. bufferSizeChanged(pData->engine->getBufferSize());
  742. reloadPrograms(true);
  743. if (pData->active)
  744. activate();
  745. carla_debug("FluidSynthPlugin::reload() - end");
  746. }
  747. void reloadPrograms(const bool init) override
  748. {
  749. carla_debug("FluidSynthPlugin::reloadPrograms(%s)", bool2str(init));
  750. // Delete old programs
  751. pData->midiprog.clear();
  752. // Query new programs
  753. uint32_t count = 0;
  754. fluid_sfont_t* f_sfont;
  755. fluid_preset_t f_preset;
  756. bool hasDrums = false;
  757. uint32_t drumIndex, drumProg;
  758. f_sfont = fluid_synth_get_sfont_by_id(fSynth, fSynthId);
  759. // initial check to know how much midi-programs we have
  760. f_sfont->iteration_start(f_sfont);
  761. while (f_sfont->iteration_next(f_sfont, &f_preset))
  762. count += 1;
  763. // soundfonts must always have at least 1 midi-program
  764. CARLA_ASSERT(count > 0);
  765. if (count == 0)
  766. return;
  767. pData->midiprog.createNew(count);
  768. // Update data
  769. uint32_t i = 0;
  770. f_sfont->iteration_start(f_sfont);
  771. while (f_sfont->iteration_next(f_sfont, &f_preset))
  772. {
  773. CARLA_ASSERT(i < pData->midiprog.count);
  774. pData->midiprog.data[i].bank = f_preset.get_banknum(&f_preset);
  775. pData->midiprog.data[i].program = f_preset.get_num(&f_preset);
  776. pData->midiprog.data[i].name = carla_strdup(f_preset.get_name(&f_preset));
  777. if (pData->midiprog.data[i].bank == 128 && ! hasDrums)
  778. {
  779. hasDrums = true;
  780. drumIndex = i;
  781. drumProg = pData->midiprog.data[i].program;
  782. }
  783. ++i;
  784. }
  785. //f_sfont->free(f_sfont);
  786. #ifndef BUILD_BRIDGE
  787. // Update OSC Names
  788. if (pData->engine->isOscControlRegistered())
  789. {
  790. pData->engine->oscSend_control_set_midi_program_count(fId, count);
  791. for (i=0; i < count; ++i)
  792. 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);
  793. }
  794. #endif
  795. if (init)
  796. {
  797. fluid_synth_program_reset(fSynth);
  798. // select first program, or 128 for ch10
  799. for (i=0; i < MAX_MIDI_CHANNELS && i != 9; ++i)
  800. {
  801. #ifdef FLUIDSYNTH_VERSION_NEW_API
  802. fluid_synth_set_channel_type(fSynth, i, CHANNEL_TYPE_MELODIC);
  803. #endif
  804. fluid_synth_program_select(fSynth, i, fSynthId, pData->midiprog.data[0].bank, pData->midiprog.data[0].program);
  805. fCurMidiProgs[i] = 0;
  806. }
  807. if (hasDrums)
  808. {
  809. #ifdef FLUIDSYNTH_VERSION_NEW_API
  810. fluid_synth_set_channel_type(fSynth, 9, CHANNEL_TYPE_DRUM);
  811. #endif
  812. fluid_synth_program_select(fSynth, 9, fSynthId, 128, drumProg);
  813. fCurMidiProgs[9] = drumIndex;
  814. }
  815. else
  816. {
  817. #ifdef FLUIDSYNTH_VERSION_NEW_API
  818. fluid_synth_set_channel_type(fSynth, 9, CHANNEL_TYPE_MELODIC);
  819. #endif
  820. fluid_synth_program_select(fSynth, 9, fSynthId, pData->midiprog.data[0].bank, pData->midiprog.data[0].program);
  821. fCurMidiProgs[9] = 0;
  822. }
  823. pData->midiprog.current = 0;
  824. }
  825. else
  826. {
  827. pData->engine->callback(CALLBACK_RELOAD_PROGRAMS, fId, 0, 0, 0.0f, nullptr);
  828. }
  829. }
  830. // -------------------------------------------------------------------
  831. // Plugin processing
  832. void process(float** const, float** const outBuffer, const uint32_t frames) override
  833. {
  834. uint32_t i, k;
  835. // --------------------------------------------------------------------------------------------------------
  836. // Check if active
  837. if (! pData->active)
  838. {
  839. // disable any output sound
  840. for (i=0; i < pData->audioOut.count; ++i)
  841. FloatVectorOperations::clear(outBuffer[i], frames);
  842. return;
  843. }
  844. // --------------------------------------------------------------------------------------------------------
  845. // Check if needs reset
  846. if (pData->needsReset)
  847. {
  848. if (fOptions & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  849. {
  850. for (int c=0; c < MAX_MIDI_CHANNELS; ++c)
  851. {
  852. #ifdef FLUIDSYNTH_VERSION_NEW_API
  853. fluid_synth_all_notes_off(fSynth, c);
  854. fluid_synth_all_sounds_off(fSynth, c);
  855. #else
  856. fluid_synth_cc(fSynth, c, MIDI_CONTROL_ALL_SOUND_OFF, 0);
  857. fluid_synth_cc(fSynth, c, MIDI_CONTROL_ALL_NOTES_OFF, 0);
  858. #endif
  859. }
  860. }
  861. else if (pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  862. {
  863. for (k=0; k < MAX_MIDI_NOTE; ++k)
  864. fluid_synth_noteoff(fSynth, pData->ctrlChannel, k);
  865. }
  866. pData->needsReset = false;
  867. }
  868. // --------------------------------------------------------------------------------------------------------
  869. // Event Input and Processing
  870. {
  871. // ----------------------------------------------------------------------------------------------------
  872. // MIDI Input (External)
  873. if (pData->extNotes.mutex.tryLock())
  874. {
  875. while (! pData->extNotes.data.isEmpty())
  876. {
  877. const ExternalMidiNote& note(pData->extNotes.data.getFirst(true));
  878. CARLA_ASSERT(note.channel >= 0 && note.channel < MAX_MIDI_CHANNELS);
  879. if (note.velo > 0)
  880. fluid_synth_noteon(fSynth, note.channel, note.note, note.velo);
  881. else
  882. fluid_synth_noteoff(fSynth,note.channel, note.note);
  883. }
  884. pData->extNotes.mutex.unlock();
  885. } // End of MIDI Input (External)
  886. // ----------------------------------------------------------------------------------------------------
  887. // Event Input (System)
  888. bool allNotesOffSent = false;
  889. uint32_t time, nEvents = pData->event.portIn->getEventCount();
  890. uint32_t timeOffset = 0;
  891. uint32_t nextBankIds[MAX_MIDI_CHANNELS] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 0, 0, 0, 0, 0, 0 };
  892. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0 && pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  893. nextBankIds[pData->ctrlChannel] = pData->midiprog.data[pData->midiprog.current].bank;
  894. for (i=0; i < nEvents; ++i)
  895. {
  896. const EngineEvent& event(pData->event.portIn->getEvent(i));
  897. time = event.time;
  898. if (time >= frames)
  899. continue;
  900. CARLA_ASSERT_INT2(time >= timeOffset, time, timeOffset);
  901. if (time > timeOffset)
  902. {
  903. if (processSingle(outBuffer, time - timeOffset, timeOffset))
  904. {
  905. timeOffset = time;
  906. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0 && pData->ctrlChannel >= 0 && pData->ctrlChannel < 16)
  907. nextBankIds[pData->ctrlChannel] = pData->midiprog.data[pData->midiprog.current].bank;
  908. }
  909. }
  910. // Control change
  911. switch (event.type)
  912. {
  913. case kEngineEventTypeNull:
  914. break;
  915. case kEngineEventTypeControl:
  916. {
  917. const EngineControlEvent& ctrlEvent = event.ctrl;
  918. switch (ctrlEvent.type)
  919. {
  920. case kEngineControlEventTypeNull:
  921. break;
  922. case kEngineControlEventTypeParameter:
  923. {
  924. #ifndef BUILD_BRIDGE
  925. // Control backend stuff
  926. if (event.channel == pData->ctrlChannel)
  927. {
  928. float value;
  929. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (fHints & PLUGIN_CAN_DRYWET) > 0)
  930. {
  931. value = ctrlEvent.value;
  932. setDryWet(value, false, false);
  933. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, value);
  934. }
  935. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (fHints & PLUGIN_CAN_VOLUME) > 0)
  936. {
  937. value = ctrlEvent.value*127.0f/100.0f;
  938. setVolume(value, false, false);
  939. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
  940. }
  941. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (fHints & PLUGIN_CAN_BALANCE) > 0)
  942. {
  943. float left, right;
  944. value = ctrlEvent.value/0.5f - 1.0f;
  945. if (value < 0.0f)
  946. {
  947. left = -1.0f;
  948. right = (value*2.0f)+1.0f;
  949. }
  950. else if (value > 0.0f)
  951. {
  952. left = (value*2.0f)-1.0f;
  953. right = 1.0f;
  954. }
  955. else
  956. {
  957. left = -1.0f;
  958. right = 1.0f;
  959. }
  960. setBalanceLeft(left, false, false);
  961. setBalanceRight(right, false, false);
  962. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  963. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  964. }
  965. }
  966. #endif
  967. // Control plugin parameters
  968. for (k=0; k < pData->param.count; ++k)
  969. {
  970. if (pData->param.data[k].midiChannel != event.channel)
  971. continue;
  972. if (pData->param.data[k].midiCC != ctrlEvent.param)
  973. continue;
  974. if (pData->param.data[k].type != PARAMETER_INPUT)
  975. continue;
  976. if ((pData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  977. continue;
  978. float value;
  979. if (pData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  980. {
  981. value = (ctrlEvent.value < 0.5f) ? pData->param.ranges[k].min : pData->param.ranges[k].max;
  982. }
  983. else
  984. {
  985. value = pData->param.ranges[k].getUnnormalizedValue(ctrlEvent.value);
  986. if (pData->param.data[k].hints & PARAMETER_IS_INTEGER)
  987. value = std::rint(value);
  988. }
  989. setParameterValue(k, value, false, false, false);
  990. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, value);
  991. }
  992. if ((fOptions & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0 && ctrlEvent.param <= 0x5F)
  993. {
  994. fluid_synth_cc(fSynth, event.channel, ctrlEvent.param, ctrlEvent.value*127.0f);
  995. }
  996. break;
  997. }
  998. case kEngineControlEventTypeMidiBank:
  999. if (event.channel < MAX_MIDI_CHANNELS && (fOptions & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  1000. nextBankIds[event.channel] = ctrlEvent.param;
  1001. break;
  1002. case kEngineControlEventTypeMidiProgram:
  1003. if (event.channel < MAX_MIDI_CHANNELS && (fOptions & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  1004. {
  1005. const uint32_t bankId(nextBankIds[event.channel]);
  1006. const uint32_t progId(ctrlEvent.param);
  1007. for (k=0; k < pData->midiprog.count; ++k)
  1008. {
  1009. if (pData->midiprog.data[k].bank == bankId && pData->midiprog.data[k].program == progId)
  1010. {
  1011. fluid_synth_program_select(fSynth, event.channel, fSynthId, bankId, progId);
  1012. fCurMidiProgs[event.channel] = k;
  1013. if (event.channel == pData->ctrlChannel)
  1014. pData->postponeRtEvent(kPluginPostRtEventMidiProgramChange, k, 0, 0.0f);
  1015. break;
  1016. }
  1017. }
  1018. }
  1019. break;
  1020. case kEngineControlEventTypeAllSoundOff:
  1021. if (fOptions & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1022. {
  1023. #ifdef FLUIDSYNTH_VERSION_NEW_API
  1024. fluid_synth_all_sounds_off(fSynth, event.channel);
  1025. #else
  1026. fluid_synth_cc(fSynth, event.channel, MIDI_CONTROL_ALL_SOUND_OFF, 0);
  1027. #endif
  1028. }
  1029. break;
  1030. case kEngineControlEventTypeAllNotesOff:
  1031. if (fOptions & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1032. {
  1033. if (event.channel == pData->ctrlChannel && ! allNotesOffSent)
  1034. {
  1035. allNotesOffSent = true;
  1036. sendMidiAllNotesOffToCallback();
  1037. }
  1038. #ifdef FLUIDSYNTH_VERSION_NEW_API
  1039. fluid_synth_all_notes_off(fSynth, event.channel);
  1040. #else
  1041. fluid_synth_cc(fSynth, event.channel, MIDI_CONTROL_ALL_NOTES_OFF, 0);
  1042. #endif
  1043. }
  1044. break;
  1045. }
  1046. break;
  1047. }
  1048. case kEngineEventTypeMidi:
  1049. {
  1050. const EngineMidiEvent& midiEvent(event.midi);
  1051. uint8_t status = MIDI_GET_STATUS_FROM_DATA(midiEvent.data);
  1052. uint8_t channel = event.channel;
  1053. // Fix bad note-off
  1054. if (MIDI_IS_STATUS_NOTE_ON(status) && midiEvent.data[2] == 0)
  1055. status -= 0x10;
  1056. if (MIDI_IS_STATUS_NOTE_OFF(status))
  1057. {
  1058. const uint8_t note = midiEvent.data[1];
  1059. fluid_synth_noteoff(fSynth, channel, note);
  1060. pData->postponeRtEvent(kPluginPostRtEventNoteOff, channel, note, 0.0f);
  1061. }
  1062. else if (MIDI_IS_STATUS_NOTE_ON(status))
  1063. {
  1064. const uint8_t note = midiEvent.data[1];
  1065. const uint8_t velo = midiEvent.data[2];
  1066. fluid_synth_noteon(fSynth, channel, note, velo);
  1067. pData->postponeRtEvent(kPluginPostRtEventNoteOn, channel, note, velo);
  1068. }
  1069. else if (MIDI_IS_STATUS_POLYPHONIC_AFTERTOUCH(status) && (fOptions & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) != 0)
  1070. {
  1071. //const uint8_t note = midiEvent.data[1];
  1072. //const uint8_t pressure = midiEvent.data[2];
  1073. // TODO, not in fluidsynth API
  1074. }
  1075. else if (MIDI_IS_STATUS_CONTROL_CHANGE(status) && (fOptions & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0)
  1076. {
  1077. const uint8_t control = midiEvent.data[1];
  1078. const uint8_t value = midiEvent.data[2];
  1079. fluid_synth_cc(fSynth, channel, control, value);
  1080. }
  1081. else if (MIDI_IS_STATUS_CHANNEL_PRESSURE(status) && (fOptions & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) != 0)
  1082. {
  1083. const uint8_t pressure = midiEvent.data[1];
  1084. fluid_synth_channel_pressure(fSynth, channel, pressure);;
  1085. }
  1086. else if (MIDI_IS_STATUS_PITCH_WHEEL_CONTROL(status) && (fOptions & PLUGIN_OPTION_SEND_PITCHBEND) != 0)
  1087. {
  1088. const uint8_t lsb = midiEvent.data[1];
  1089. const uint8_t msb = midiEvent.data[2];
  1090. fluid_synth_pitch_bend(fSynth, channel, (msb << 7) | lsb);
  1091. }
  1092. break;
  1093. }
  1094. }
  1095. }
  1096. pData->postRtEvents.trySplice();
  1097. if (frames > timeOffset)
  1098. processSingle(outBuffer, frames - timeOffset, timeOffset);
  1099. } // End of Event Input and Processing
  1100. CARLA_PROCESS_CONTINUE_CHECK;
  1101. // --------------------------------------------------------------------------------------------------------
  1102. // Control Output
  1103. {
  1104. k = FluidSynthVoiceCount;
  1105. fParamBuffers[k] = fluid_synth_get_active_voice_count(fSynth);
  1106. pData->param.ranges[k].fixValue(fParamBuffers[k]);
  1107. if (pData->param.data[k].midiCC > 0)
  1108. {
  1109. float value(pData->param.ranges[k].getNormalizedValue(fParamBuffers[k]));
  1110. pData->event.portOut->writeControlEvent(0, pData->param.data[k].midiChannel, kEngineControlEventTypeParameter, pData->param.data[k].midiCC, value);
  1111. }
  1112. } // End of Control Output
  1113. }
  1114. bool processSingle(float** const outBuffer, const uint32_t frames, const uint32_t timeOffset)
  1115. {
  1116. CARLA_ASSERT(outBuffer != nullptr);
  1117. CARLA_ASSERT(frames > 0);
  1118. if (outBuffer == nullptr)
  1119. return false;
  1120. if (frames == 0)
  1121. return false;
  1122. uint32_t i, k;
  1123. // --------------------------------------------------------------------------------------------------------
  1124. // Try lock, silence otherwise
  1125. if (pData->engine->isOffline())
  1126. {
  1127. pData->singleMutex.lock();
  1128. }
  1129. else if (! pData->singleMutex.tryLock())
  1130. {
  1131. for (i=0; i < pData->audioOut.count; ++i)
  1132. {
  1133. for (k=0; k < frames; ++k)
  1134. outBuffer[i][k+timeOffset] = 0.0f;
  1135. }
  1136. return false;
  1137. }
  1138. // --------------------------------------------------------------------------------------------------------
  1139. // Fill plugin buffers and Run plugin
  1140. if (kUses16Outs)
  1141. {
  1142. for (i=0; i < pData->audioOut.count; ++i)
  1143. FloatVectorOperations::clear(fAudio16Buffers[i], frames);
  1144. fluid_synth_process(fSynth, frames, 0, nullptr, pData->audioOut.count, fAudio16Buffers);
  1145. }
  1146. else
  1147. fluid_synth_write_float(fSynth, frames, outBuffer[0] + timeOffset, 0, 1, outBuffer[1] + timeOffset, 0, 1);
  1148. #ifndef BUILD_BRIDGE
  1149. // --------------------------------------------------------------------------------------------------------
  1150. // Post-processing (volume and balance)
  1151. {
  1152. // note - balance not possible with kUses16Outs, so we can safely skip fAudioOutBuffers
  1153. const bool doVolume = (fHints & PLUGIN_CAN_VOLUME) > 0 && pData->postProc.volume != 1.0f;
  1154. const bool doBalance = (fHints & PLUGIN_CAN_BALANCE) > 0 && (pData->postProc.balanceLeft != -1.0f || pData->postProc.balanceRight != 1.0f);
  1155. float oldBufLeft[doBalance ? frames : 1];
  1156. for (i=0; i < pData->audioOut.count; ++i)
  1157. {
  1158. // Balance
  1159. if (doBalance)
  1160. {
  1161. if (i % 2 == 0)
  1162. FloatVectorOperations::copy(oldBufLeft, outBuffer[i]+timeOffset, frames);
  1163. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  1164. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  1165. for (k=0; k < frames; ++k)
  1166. {
  1167. if (i % 2 == 0)
  1168. {
  1169. // left
  1170. outBuffer[i][k+timeOffset] = oldBufLeft[k] * (1.0f - balRangeL);
  1171. outBuffer[i][k+timeOffset] += outBuffer[i+1][k+timeOffset] * (1.0f - balRangeR);
  1172. }
  1173. else
  1174. {
  1175. // right
  1176. outBuffer[i][k+timeOffset] = outBuffer[i][k+timeOffset] * balRangeR;
  1177. outBuffer[i][k+timeOffset] += oldBufLeft[k] * balRangeL;
  1178. }
  1179. }
  1180. }
  1181. // Volume
  1182. if (kUses16Outs)
  1183. {
  1184. for (k=0; k < frames; ++k)
  1185. outBuffer[i][k+timeOffset] = fAudio16Buffers[i][k] * pData->postProc.volume;
  1186. }
  1187. else if (doVolume)
  1188. {
  1189. for (k=0; k < frames; ++k)
  1190. outBuffer[i][k+timeOffset] *= pData->postProc.volume;
  1191. }
  1192. }
  1193. } // End of Post-processing
  1194. #else
  1195. if (kUses16Outs)
  1196. {
  1197. for (i=0; i < pData->audioOut.count; ++i)
  1198. {
  1199. for (k=0; k < frames; ++k)
  1200. outBuffer[i][k+timeOffset] = fAudio16Buffers[i][k];
  1201. }
  1202. }
  1203. #endif
  1204. // --------------------------------------------------------------------------------------------------------
  1205. pData->singleMutex.unlock();
  1206. return true;
  1207. }
  1208. void bufferSizeChanged(const uint32_t newBufferSize) override
  1209. {
  1210. if (! kUses16Outs)
  1211. return;
  1212. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1213. {
  1214. if (fAudio16Buffers[i] != nullptr)
  1215. delete[] fAudio16Buffers[i];
  1216. fAudio16Buffers[i] = new float[newBufferSize];
  1217. }
  1218. }
  1219. // -------------------------------------------------------------------
  1220. // Plugin buffers
  1221. void clearBuffers() override
  1222. {
  1223. carla_debug("FluidSynthPlugin::clearBuffers() - start");
  1224. if (fAudio16Buffers != nullptr)
  1225. {
  1226. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1227. {
  1228. if (fAudio16Buffers[i] != nullptr)
  1229. {
  1230. delete[] fAudio16Buffers[i];
  1231. fAudio16Buffers[i] = nullptr;
  1232. }
  1233. }
  1234. delete[] fAudio16Buffers;
  1235. fAudio16Buffers = nullptr;
  1236. }
  1237. CarlaPlugin::clearBuffers();
  1238. carla_debug("FluidSynthPlugin::clearBuffers() - end");
  1239. }
  1240. // -------------------------------------------------------------------
  1241. const void* getExtraStuff() const noexcept override
  1242. {
  1243. return kUses16Outs ? (const void*)0x1 : nullptr;
  1244. }
  1245. bool init(const char* const filename, const char* const name, const char* const label)
  1246. {
  1247. CARLA_ASSERT(fSynth != nullptr);
  1248. CARLA_ASSERT(filename != nullptr);
  1249. CARLA_ASSERT(label != nullptr);
  1250. // ---------------------------------------------------------------
  1251. // first checks
  1252. if (pData->engine == nullptr)
  1253. {
  1254. return false;
  1255. }
  1256. if (pData->client != nullptr)
  1257. {
  1258. pData->engine->setLastError("Plugin client is already registered");
  1259. return false;
  1260. }
  1261. if (fSynth == nullptr)
  1262. {
  1263. pData->engine->setLastError("null synth");
  1264. return false;
  1265. }
  1266. if (filename == nullptr)
  1267. {
  1268. pData->engine->setLastError("null filename");
  1269. return false;
  1270. }
  1271. if (label == nullptr)
  1272. {
  1273. pData->engine->setLastError("null label");
  1274. return false;
  1275. }
  1276. // ---------------------------------------------------------------
  1277. // open soundfont
  1278. fSynthId = fluid_synth_sfload(fSynth, filename, 0);
  1279. if (fSynthId < 0)
  1280. {
  1281. pData->engine->setLastError("Failed to load SoundFont file");
  1282. return false;
  1283. }
  1284. // ---------------------------------------------------------------
  1285. // get info
  1286. fFilename = filename;
  1287. fLabel = label;
  1288. if (kUses16Outs && ! fLabel.endsWith(" (16 outs)"))
  1289. fLabel += " (16 outs)";
  1290. if (name != nullptr)
  1291. fName = pData->engine->getUniquePluginName(name);
  1292. else
  1293. fName = pData->engine->getUniquePluginName(label);
  1294. // ---------------------------------------------------------------
  1295. // register client
  1296. pData->client = pData->engine->addClient(this);
  1297. if (pData->client == nullptr || ! pData->client->isOk())
  1298. {
  1299. pData->engine->setLastError("Failed to register plugin client");
  1300. return false;
  1301. }
  1302. // ---------------------------------------------------------------
  1303. // load plugin settings
  1304. {
  1305. // set default options
  1306. fOptions = 0x0;
  1307. fOptions |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  1308. fOptions |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  1309. fOptions |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  1310. fOptions |= PLUGIN_OPTION_SEND_PITCHBEND;
  1311. fOptions |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  1312. // load settings
  1313. pData->idStr = "SF2/";
  1314. pData->idStr += label;
  1315. fOptions = pData->loadSettings(fOptions, getAvailableOptions());
  1316. }
  1317. return true;
  1318. }
  1319. private:
  1320. enum FluidSynthInputParameters {
  1321. FluidSynthReverbOnOff = 0,
  1322. FluidSynthReverbRoomSize = 1,
  1323. FluidSynthReverbDamp = 2,
  1324. FluidSynthReverbLevel = 3,
  1325. FluidSynthReverbWidth = 4,
  1326. FluidSynthChorusOnOff = 5,
  1327. FluidSynthChorusNr = 6,
  1328. FluidSynthChorusLevel = 7,
  1329. FluidSynthChorusSpeedHz = 8,
  1330. FluidSynthChorusDepthMs = 9,
  1331. FluidSynthChorusType = 10,
  1332. FluidSynthPolyphony = 11,
  1333. FluidSynthInterpolation = 12,
  1334. FluidSynthVoiceCount = 13,
  1335. FluidSynthParametersMax = 14
  1336. };
  1337. const bool kUses16Outs;
  1338. CarlaString fLabel;
  1339. fluid_settings_t* fSettings;
  1340. fluid_synth_t* fSynth;
  1341. int fSynthId;
  1342. float** fAudio16Buffers;
  1343. float fParamBuffers[FluidSynthParametersMax];
  1344. int32_t fCurMidiProgs[MAX_MIDI_CHANNELS];
  1345. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(FluidSynthPlugin)
  1346. };
  1347. CARLA_BACKEND_END_NAMESPACE
  1348. #else // WANT_FLUIDSYNTH
  1349. # warning fluidsynth not available (no SF2 support)
  1350. #endif
  1351. CARLA_BACKEND_START_NAMESPACE
  1352. CarlaPlugin* CarlaPlugin::newSF2(const Initializer& init, const bool use16Outs)
  1353. {
  1354. carla_debug("CarlaPlugin::newSF2({%p, \"%s\", \"%s\", \"%s\"}, %s)", init.engine, init.filename, init.name, init.label, bool2str(use16Outs));
  1355. #ifdef WANT_FLUIDSYNTH
  1356. if (! fluid_is_soundfont(init.filename))
  1357. {
  1358. init.engine->setLastError("Requested file is not a valid SoundFont");
  1359. return nullptr;
  1360. }
  1361. if (init.engine->getProccessMode() == PROCESS_MODE_CONTINUOUS_RACK && use16Outs)
  1362. {
  1363. init.engine->setLastError("Carla's rack mode can only work with Stereo modules, please choose the 2-channel only SoundFont version");
  1364. return nullptr;
  1365. }
  1366. FluidSynthPlugin* const plugin(new FluidSynthPlugin(init.engine, init.id, use16Outs));
  1367. if (! plugin->init(init.filename, init.name, init.label))
  1368. {
  1369. delete plugin;
  1370. return nullptr;
  1371. }
  1372. plugin->reload();
  1373. return plugin;
  1374. #else
  1375. init.engine->setLastError("fluidsynth support not available");
  1376. return nullptr;
  1377. // unused
  1378. (void)use16Outs;
  1379. #endif
  1380. }
  1381. CARLA_BACKEND_END_NAMESPACE