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.

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