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.

1591 lines
56KB

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