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.

1698 lines
60KB

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