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.

1536 lines
54KB

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