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.

1475 lines
52KB

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