Collection of tools useful for audio production
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.

1365 lines
45KB

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