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.

1684 lines
60KB

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