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.

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