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.

1366 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. // Safely disable plugin for reload
  336. const ScopedDisabler m(this);
  337. if (x_client->isActive())
  338. x_client->deactivate();
  339. // Remove client ports
  340. removeClientPorts();
  341. // Delete old data
  342. deleteBuffers();
  343. uint32_t aOuts, params, j;
  344. aOuts = 2;
  345. params = FluidSynthParametersMax;
  346. aOut.ports = new CarlaEngineAudioPort*[aOuts];
  347. aOut.rindexes = new uint32_t[aOuts];
  348. param.data = new ParameterData[params];
  349. param.ranges = new ParameterRanges[params];
  350. const int portNameSize = x_engine->maxPortNameSize();
  351. CarlaString portName;
  352. // ---------------------------------------
  353. // Audio Outputs
  354. {
  355. portName.clear();
  356. if (x_engine->processMode() == PROCESS_MODE_SINGLE_CLIENT)
  357. {
  358. portName = m_name;
  359. portName += ":";
  360. }
  361. portName += "out-left";
  362. portName.truncate(portNameSize);
  363. aOut.ports[0] = (CarlaEngineAudioPort*)x_client->addPort(CarlaEnginePortTypeAudio, portName, false);
  364. aOut.rindexes[0] = 0;
  365. }
  366. {
  367. portName.clear();
  368. if (x_engine->processMode() == PROCESS_MODE_SINGLE_CLIENT)
  369. {
  370. portName = m_name;
  371. portName += ":";
  372. }
  373. portName += "out-right";
  374. portName.truncate(portNameSize);
  375. aOut.ports[1] = (CarlaEngineAudioPort*)x_client->addPort(CarlaEnginePortTypeAudio, portName, false);
  376. aOut.rindexes[1] = 1;
  377. }
  378. // ---------------------------------------
  379. // MIDI Input
  380. {
  381. portName.clear();
  382. if (x_engine->processMode() == PROCESS_MODE_SINGLE_CLIENT)
  383. {
  384. portName = m_name;
  385. portName += ":";
  386. }
  387. portName += "midi-in";
  388. portName.truncate(portNameSize);
  389. midi.portMin = (CarlaEngineMidiPort*)x_client->addPort(CarlaEnginePortTypeMIDI, portName, true);
  390. }
  391. // ---------------------------------------
  392. // Parameters
  393. {
  394. portName.clear();
  395. if (x_engine->processMode() == PROCESS_MODE_SINGLE_CLIENT)
  396. {
  397. portName = m_name;
  398. portName += ":";
  399. }
  400. portName += "control-in";
  401. portName.truncate(portNameSize);
  402. param.portCin = (CarlaEngineControlPort*)x_client->addPort(CarlaEnginePortTypeControl, portName, true);
  403. }
  404. {
  405. portName.clear();
  406. if (x_engine->processMode() == PROCESS_MODE_SINGLE_CLIENT)
  407. {
  408. portName = m_name;
  409. portName += ":";
  410. }
  411. portName += "control-out";
  412. portName.truncate(portNameSize);
  413. param.portCout = (CarlaEngineControlPort*)x_client->addPort(CarlaEnginePortTypeControl, portName, false);
  414. }
  415. // ----------------------
  416. j = FluidSynthReverbOnOff;
  417. param.data[j].index = j;
  418. param.data[j].rindex = j;
  419. param.data[j].type = PARAMETER_INPUT;
  420. param.data[j].hints = PARAMETER_IS_ENABLED | PARAMETER_IS_AUTOMABLE | PARAMETER_IS_BOOLEAN;
  421. param.data[j].midiChannel = 0;
  422. param.data[j].midiCC = -1;
  423. param.ranges[j].min = 0.0;
  424. param.ranges[j].max = 1.0;
  425. param.ranges[j].def = 0.0; // off
  426. param.ranges[j].step = 1.0;
  427. param.ranges[j].stepSmall = 1.0;
  428. param.ranges[j].stepLarge = 1.0;
  429. paramBuffers[j] = param.ranges[j].def;
  430. // ----------------------
  431. j = FluidSynthReverbRoomSize;
  432. param.data[j].index = j;
  433. param.data[j].rindex = j;
  434. param.data[j].type = PARAMETER_INPUT;
  435. param.data[j].hints = PARAMETER_IS_ENABLED | PARAMETER_IS_AUTOMABLE;
  436. param.data[j].midiChannel = 0;
  437. param.data[j].midiCC = -1;
  438. param.ranges[j].min = 0.0;
  439. param.ranges[j].max = 1.2;
  440. param.ranges[j].def = FLUID_REVERB_DEFAULT_ROOMSIZE;
  441. param.ranges[j].step = 0.01;
  442. param.ranges[j].stepSmall = 0.0001;
  443. param.ranges[j].stepLarge = 0.1;
  444. paramBuffers[j] = param.ranges[j].def;
  445. // ----------------------
  446. j = FluidSynthReverbDamp;
  447. param.data[j].index = j;
  448. param.data[j].rindex = j;
  449. param.data[j].type = PARAMETER_INPUT;
  450. param.data[j].hints = PARAMETER_IS_ENABLED | PARAMETER_IS_AUTOMABLE;
  451. param.data[j].midiChannel = 0;
  452. param.data[j].midiCC = -1;
  453. param.ranges[j].min = 0.0;
  454. param.ranges[j].max = 1.0;
  455. param.ranges[j].def = FLUID_REVERB_DEFAULT_DAMP;
  456. param.ranges[j].step = 0.01;
  457. param.ranges[j].stepSmall = 0.0001;
  458. param.ranges[j].stepLarge = 0.1;
  459. paramBuffers[j] = param.ranges[j].def;
  460. // ----------------------
  461. j = FluidSynthReverbLevel;
  462. param.data[j].index = j;
  463. param.data[j].rindex = j;
  464. param.data[j].type = PARAMETER_INPUT;
  465. param.data[j].hints = PARAMETER_IS_ENABLED | PARAMETER_IS_AUTOMABLE;
  466. param.data[j].midiChannel = 0;
  467. param.data[j].midiCC = MIDI_CONTROL_REVERB_SEND_LEVEL;
  468. param.ranges[j].min = 0.0;
  469. param.ranges[j].max = 1.0;
  470. param.ranges[j].def = FLUID_REVERB_DEFAULT_LEVEL;
  471. param.ranges[j].step = 0.01;
  472. param.ranges[j].stepSmall = 0.0001;
  473. param.ranges[j].stepLarge = 0.1;
  474. paramBuffers[j] = param.ranges[j].def;
  475. // ----------------------
  476. j = FluidSynthReverbWidth;
  477. param.data[j].index = j;
  478. param.data[j].rindex = j;
  479. param.data[j].type = PARAMETER_INPUT;
  480. param.data[j].hints = PARAMETER_IS_ENABLED | PARAMETER_IS_AUTOMABLE;
  481. param.data[j].midiChannel = 0;
  482. param.data[j].midiCC = -1;
  483. param.ranges[j].min = 0.0;
  484. param.ranges[j].max = 10.0; // should be 100, but that sounds too much
  485. param.ranges[j].def = FLUID_REVERB_DEFAULT_WIDTH;
  486. param.ranges[j].step = 0.01;
  487. param.ranges[j].stepSmall = 0.0001;
  488. param.ranges[j].stepLarge = 0.1;
  489. paramBuffers[j] = param.ranges[j].def;
  490. // ----------------------
  491. j = FluidSynthChorusOnOff;
  492. param.data[j].index = j;
  493. param.data[j].rindex = j;
  494. param.data[j].type = PARAMETER_INPUT;
  495. param.data[j].hints = PARAMETER_IS_ENABLED | PARAMETER_IS_BOOLEAN;
  496. param.data[j].midiChannel = 0;
  497. param.data[j].midiCC = -1;
  498. param.ranges[j].min = 0.0;
  499. param.ranges[j].max = 1.0;
  500. param.ranges[j].def = 0.0; // off
  501. param.ranges[j].step = 1.0;
  502. param.ranges[j].stepSmall = 1.0;
  503. param.ranges[j].stepLarge = 1.0;
  504. paramBuffers[j] = param.ranges[j].def;
  505. // ----------------------
  506. j = FluidSynthChorusNr;
  507. param.data[j].index = j;
  508. param.data[j].rindex = j;
  509. param.data[j].type = PARAMETER_INPUT;
  510. param.data[j].hints = PARAMETER_IS_ENABLED | PARAMETER_IS_INTEGER;
  511. param.data[j].midiChannel = 0;
  512. param.data[j].midiCC = -1;
  513. param.ranges[j].min = 0.0;
  514. param.ranges[j].max = 99.0;
  515. param.ranges[j].def = FLUID_CHORUS_DEFAULT_N;
  516. param.ranges[j].step = 1.0;
  517. param.ranges[j].stepSmall = 1.0;
  518. param.ranges[j].stepLarge = 10.0;
  519. paramBuffers[j] = param.ranges[j].def;
  520. // ----------------------
  521. j = FluidSynthChorusLevel;
  522. param.data[j].index = j;
  523. param.data[j].rindex = j;
  524. param.data[j].type = PARAMETER_INPUT;
  525. param.data[j].hints = PARAMETER_IS_ENABLED;
  526. param.data[j].midiChannel = 0;
  527. param.data[j].midiCC = 0; //MIDI_CONTROL_CHORUS_SEND_LEVEL;
  528. param.ranges[j].min = 0.0;
  529. param.ranges[j].max = 10.0;
  530. param.ranges[j].def = FLUID_CHORUS_DEFAULT_LEVEL;
  531. param.ranges[j].step = 0.01;
  532. param.ranges[j].stepSmall = 0.0001;
  533. param.ranges[j].stepLarge = 0.1;
  534. paramBuffers[j] = param.ranges[j].def;
  535. // ----------------------
  536. j = FluidSynthChorusSpeedHz;
  537. param.data[j].index = j;
  538. param.data[j].rindex = j;
  539. param.data[j].type = PARAMETER_INPUT;
  540. param.data[j].hints = PARAMETER_IS_ENABLED;
  541. param.data[j].midiChannel = 0;
  542. param.data[j].midiCC = -1;
  543. param.ranges[j].min = 0.29;
  544. param.ranges[j].max = 5.0;
  545. param.ranges[j].def = FLUID_CHORUS_DEFAULT_SPEED;
  546. param.ranges[j].step = 0.01;
  547. param.ranges[j].stepSmall = 0.0001;
  548. param.ranges[j].stepLarge = 0.1;
  549. paramBuffers[j] = param.ranges[j].def;
  550. // ----------------------
  551. j = FluidSynthChorusDepthMs;
  552. param.data[j].index = j;
  553. param.data[j].rindex = j;
  554. param.data[j].type = PARAMETER_INPUT;
  555. param.data[j].hints = PARAMETER_IS_ENABLED;
  556. param.data[j].midiChannel = 0;
  557. param.data[j].midiCC = -1;
  558. param.ranges[j].min = 0.0;
  559. param.ranges[j].max = 2048000.0 / x_engine->getSampleRate();
  560. param.ranges[j].def = FLUID_CHORUS_DEFAULT_DEPTH;
  561. param.ranges[j].step = 0.01;
  562. param.ranges[j].stepSmall = 0.0001;
  563. param.ranges[j].stepLarge = 0.1;
  564. paramBuffers[j] = param.ranges[j].def;
  565. // ----------------------
  566. j = FluidSynthChorusType;
  567. param.data[j].index = j;
  568. param.data[j].rindex = j;
  569. param.data[j].type = PARAMETER_INPUT;
  570. param.data[j].hints = PARAMETER_IS_ENABLED | PARAMETER_IS_INTEGER | PARAMETER_USES_SCALEPOINTS;
  571. param.data[j].midiChannel = 0;
  572. param.data[j].midiCC = -1;
  573. param.ranges[j].min = FLUID_CHORUS_MOD_SINE;
  574. param.ranges[j].max = FLUID_CHORUS_MOD_TRIANGLE;
  575. param.ranges[j].def = FLUID_CHORUS_DEFAULT_TYPE;
  576. param.ranges[j].step = 1;
  577. param.ranges[j].stepSmall = 1;
  578. param.ranges[j].stepLarge = 1;
  579. paramBuffers[j] = param.ranges[j].def;
  580. // ----------------------
  581. j = FluidSynthPolyphony;
  582. param.data[j].index = j;
  583. param.data[j].rindex = j;
  584. param.data[j].type = PARAMETER_INPUT;
  585. param.data[j].hints = PARAMETER_IS_ENABLED | PARAMETER_IS_INTEGER;
  586. param.data[j].midiChannel = 0;
  587. param.data[j].midiCC = -1;
  588. param.ranges[j].min = 1;
  589. param.ranges[j].max = 512; // max theoric is 65535
  590. param.ranges[j].def = fluid_synth_get_polyphony(f_synth);
  591. param.ranges[j].step = 1;
  592. param.ranges[j].stepSmall = 1;
  593. param.ranges[j].stepLarge = 10;
  594. paramBuffers[j] = param.ranges[j].def;
  595. // ----------------------
  596. j = FluidSynthInterpolation;
  597. param.data[j].index = j;
  598. param.data[j].rindex = j;
  599. param.data[j].type = PARAMETER_INPUT;
  600. param.data[j].hints = PARAMETER_IS_ENABLED | PARAMETER_IS_INTEGER | PARAMETER_USES_SCALEPOINTS;
  601. param.data[j].midiChannel = 0;
  602. param.data[j].midiCC = -1;
  603. param.ranges[j].min = FLUID_INTERP_NONE;
  604. param.ranges[j].max = FLUID_INTERP_HIGHEST;
  605. param.ranges[j].def = FLUID_INTERP_DEFAULT;
  606. param.ranges[j].step = 1;
  607. param.ranges[j].stepSmall = 1;
  608. param.ranges[j].stepLarge = 1;
  609. paramBuffers[j] = param.ranges[j].def;
  610. // ----------------------
  611. j = FluidSynthVoiceCount;
  612. param.data[j].index = j;
  613. param.data[j].rindex = j;
  614. param.data[j].type = PARAMETER_OUTPUT;
  615. param.data[j].hints = PARAMETER_IS_ENABLED | PARAMETER_IS_AUTOMABLE | PARAMETER_IS_INTEGER;
  616. param.data[j].midiChannel = 0;
  617. param.data[j].midiCC = -1;
  618. param.ranges[j].min = 0;
  619. param.ranges[j].max = 65535;
  620. param.ranges[j].def = 0;
  621. param.ranges[j].step = 1;
  622. param.ranges[j].stepSmall = 1;
  623. param.ranges[j].stepLarge = 1;
  624. paramBuffers[j] = param.ranges[j].def;
  625. // ---------------------------------------
  626. aOut.count = aOuts;
  627. param.count = params;
  628. // plugin checks
  629. m_hints &= ~(PLUGIN_IS_SYNTH | PLUGIN_USES_CHUNKS | PLUGIN_CAN_DRYWET | PLUGIN_CAN_VOLUME | PLUGIN_CAN_BALANCE | PLUGIN_CAN_FORCE_STEREO);
  630. m_hints |= PLUGIN_IS_SYNTH;
  631. m_hints |= PLUGIN_CAN_VOLUME;
  632. m_hints |= PLUGIN_CAN_BALANCE;
  633. m_hints |= PLUGIN_CAN_FORCE_STEREO;
  634. reloadPrograms(true);
  635. x_client->activate();
  636. qDebug("FluidSynthPlugin::reload() - end");
  637. }
  638. void reloadPrograms(const bool init)
  639. {
  640. qDebug("FluidSynthPlugin::reloadPrograms(%s)", bool2str(init));
  641. // Delete old programs
  642. if (midiprog.count > 0)
  643. {
  644. for (uint32_t i=0; i < midiprog.count; i++)
  645. free((void*)midiprog.data[i].name);
  646. delete[] midiprog.data;
  647. }
  648. midiprog.count = 0;
  649. midiprog.data = nullptr;
  650. // Query new programs
  651. fluid_sfont_t* f_sfont;
  652. fluid_preset_t f_preset;
  653. bool hasDrums = false;
  654. f_sfont = fluid_synth_get_sfont_by_id(f_synth, f_id);
  655. // initial check to know how much midi-programs we have
  656. f_sfont->iteration_start(f_sfont);
  657. while (f_sfont->iteration_next(f_sfont, &f_preset))
  658. midiprog.count += 1;
  659. // soundfonts must always have at least 1 midi-program
  660. CARLA_ASSERT(midiprog.count > 0);
  661. if (midiprog.count > 0)
  662. midiprog.data = new MidiProgramData[midiprog.count];
  663. // Update data
  664. uint32_t i = 0;
  665. f_sfont->iteration_start(f_sfont);
  666. while (f_sfont->iteration_next(f_sfont, &f_preset))
  667. {
  668. CARLA_ASSERT(i < midiprog.count);
  669. midiprog.data[i].bank = f_preset.get_banknum(&f_preset);
  670. midiprog.data[i].program = f_preset.get_num(&f_preset);
  671. midiprog.data[i].name = strdup(f_preset.get_name(&f_preset));
  672. if (midiprog.data[i].bank == 128)
  673. hasDrums = true;
  674. i++;
  675. }
  676. //f_sfont->free(f_sfont);
  677. // Update OSC Names
  678. if (x_engine->isOscControlRegisted())
  679. {
  680. x_engine->osc_send_control_set_midi_program_count(m_id, midiprog.count);
  681. for (i=0; i < midiprog.count; i++)
  682. x_engine->osc_send_control_set_midi_program_data(m_id, i, midiprog.data[i].bank, midiprog.data[i].program, midiprog.data[i].name);
  683. }
  684. if (init)
  685. {
  686. fluid_synth_program_reset(f_synth);
  687. for (i=0; i < 16 && i != 9; i++)
  688. {
  689. fluid_synth_program_select(f_synth, i, f_id, midiprog.data[0].bank, midiprog.data[0].program);
  690. #ifdef FLUIDSYNTH_VERSION_NEW_API
  691. fluid_synth_set_channel_type(f_synth, i, CHANNEL_TYPE_MELODIC);
  692. #endif
  693. }
  694. if (hasDrums)
  695. {
  696. fluid_synth_program_select(f_synth, 9, f_id, 128, 0);
  697. #ifdef FLUIDSYNTH_VERSION_NEW_API
  698. fluid_synth_set_channel_type(f_synth, 9, CHANNEL_TYPE_DRUM);
  699. #endif
  700. }
  701. else
  702. {
  703. fluid_synth_program_select(f_synth, 9, f_id, midiprog.data[0].bank, midiprog.data[0].program);
  704. #ifdef FLUIDSYNTH_VERSION_NEW_API
  705. fluid_synth_set_channel_type(f_synth, 9, CHANNEL_TYPE_MELODIC);
  706. #endif
  707. }
  708. setMidiProgram(0, false, false, false, true);
  709. }
  710. else
  711. {
  712. x_engine->callback(CALLBACK_RELOAD_PROGRAMS, m_id, 0, 0, 0.0);
  713. }
  714. }
  715. // -------------------------------------------------------------------
  716. // Plugin processing
  717. void process(float** const, float** const outBuffer, const uint32_t frames, const uint32_t framesOffset)
  718. {
  719. uint32_t i, k;
  720. uint32_t midiEventCount = 0;
  721. double aOutsPeak[2] = { 0.0 };
  722. CARLA_PROCESS_CONTINUE_CHECK;
  723. // --------------------------------------------------------------------------------------------------------
  724. // Parameters Input [Automation]
  725. if (m_active && m_activeBefore)
  726. {
  727. bool allNotesOffSent = false;
  728. const CarlaEngineControlEvent* cinEvent;
  729. uint32_t time, nEvents = param.portCin->getEventCount();
  730. unsigned char nextBankIds[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 0, 0, 0, 0, 0, 0 };
  731. if (midiprog.current >= 0 && midiprog.count > 0 && m_ctrlInChannel >= 0 && m_ctrlInChannel < 16)
  732. nextBankIds[m_ctrlInChannel] = midiprog.data[midiprog.current].bank;
  733. for (i=0; i < nEvents; i++)
  734. {
  735. cinEvent = param.portCin->getEvent(i);
  736. if (! cinEvent)
  737. continue;
  738. time = cinEvent->time - framesOffset;
  739. if (time >= frames)
  740. continue;
  741. // Control change
  742. switch (cinEvent->type)
  743. {
  744. case CarlaEngineNullEvent:
  745. break;
  746. case CarlaEngineParameterChangeEvent:
  747. {
  748. double value;
  749. // Control backend stuff
  750. if (cinEvent->channel == m_ctrlInChannel)
  751. {
  752. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(cinEvent->parameter) && (m_hints & PLUGIN_CAN_DRYWET) > 0)
  753. {
  754. value = cinEvent->value;
  755. setDryWet(value, false, false);
  756. postponeEvent(PluginPostEventParameterChange, PARAMETER_DRYWET, 0, value);
  757. continue;
  758. }
  759. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(cinEvent->parameter) && (m_hints & PLUGIN_CAN_VOLUME) > 0)
  760. {
  761. value = cinEvent->value*127/100;
  762. setVolume(value, false, false);
  763. postponeEvent(PluginPostEventParameterChange, PARAMETER_VOLUME, 0, value);
  764. continue;
  765. }
  766. if (MIDI_IS_CONTROL_BALANCE(cinEvent->parameter) && (m_hints & PLUGIN_CAN_BALANCE) > 0)
  767. {
  768. double left, right;
  769. value = cinEvent->value/0.5 - 1.0;
  770. if (value < 0.0)
  771. {
  772. left = -1.0;
  773. right = (value*2)+1.0;
  774. }
  775. else if (value > 0.0)
  776. {
  777. left = (value*2)-1.0;
  778. right = 1.0;
  779. }
  780. else
  781. {
  782. left = -1.0;
  783. right = 1.0;
  784. }
  785. setBalanceLeft(left, false, false);
  786. setBalanceRight(right, false, false);
  787. postponeEvent(PluginPostEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  788. postponeEvent(PluginPostEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  789. continue;
  790. }
  791. }
  792. // Control plugin parameters
  793. for (k=0; k < param.count; k++)
  794. {
  795. if (param.data[k].midiChannel != cinEvent->channel)
  796. continue;
  797. if (param.data[k].midiCC != cinEvent->parameter)
  798. continue;
  799. if (param.data[k].type != PARAMETER_INPUT)
  800. continue;
  801. if (param.data[k].hints & PARAMETER_IS_AUTOMABLE)
  802. {
  803. if (param.data[k].hints & PARAMETER_IS_BOOLEAN)
  804. {
  805. value = cinEvent->value < 0.5 ? param.ranges[k].min : param.ranges[k].max;
  806. }
  807. else
  808. {
  809. value = cinEvent->value * (param.ranges[k].max - param.ranges[k].min) + param.ranges[k].min;
  810. if (param.data[k].hints & PARAMETER_IS_INTEGER)
  811. value = rint(value);
  812. }
  813. setParameterValue(k, value, false, false, false);
  814. postponeEvent(PluginPostEventParameterChange, k, 0, value);
  815. }
  816. }
  817. break;
  818. }
  819. case CarlaEngineMidiBankChangeEvent:
  820. if (cinEvent->channel < 16)
  821. nextBankIds[cinEvent->channel] = rint(cinEvent->value);
  822. break;
  823. case CarlaEngineMidiProgramChangeEvent:
  824. if (cinEvent->channel < 16)
  825. {
  826. uint32_t bankId = nextBankIds[cinEvent->channel];
  827. uint32_t progId = rint(cinEvent->value);
  828. for (k=0; k < midiprog.count; k++)
  829. {
  830. if (midiprog.data[k].bank == bankId && midiprog.data[k].program == progId)
  831. {
  832. if (cinEvent->channel == m_ctrlInChannel)
  833. {
  834. setMidiProgram(k, false, false, false, false);
  835. postponeEvent(PluginPostEventMidiProgramChange, k, 0, 0.0);
  836. }
  837. else
  838. fluid_synth_program_select(f_synth, cinEvent->channel, f_id, bankId, progId);
  839. break;
  840. }
  841. }
  842. }
  843. break;
  844. case CarlaEngineAllSoundOffEvent:
  845. if (cinEvent->channel == m_ctrlInChannel)
  846. {
  847. if (! allNotesOffSent)
  848. sendMidiAllNotesOff();
  849. postponeEvent(PluginPostEventParameterChange, PARAMETER_ACTIVE, 0, 0.0);
  850. postponeEvent(PluginPostEventParameterChange, PARAMETER_ACTIVE, 0, 1.0);
  851. allNotesOffSent = true;
  852. }
  853. break;
  854. case CarlaEngineAllNotesOffEvent:
  855. if (cinEvent->channel == m_ctrlInChannel)
  856. {
  857. if (! allNotesOffSent)
  858. sendMidiAllNotesOff();
  859. allNotesOffSent = true;
  860. }
  861. break;
  862. }
  863. }
  864. } // End of Parameters Input
  865. CARLA_PROCESS_CONTINUE_CHECK;
  866. // --------------------------------------------------------------------------------------------------------
  867. // MIDI Input
  868. if (m_active && m_activeBefore)
  869. {
  870. // ----------------------------------------------------------------------------------------------------
  871. // MIDI Input (External)
  872. {
  873. engineMidiLock();
  874. for (i=0; i < MAX_MIDI_EVENTS && midiEventCount < MAX_MIDI_EVENTS; i++)
  875. {
  876. if (extMidiNotes[i].channel < 0)
  877. break;
  878. if (extMidiNotes[i].velo)
  879. fluid_synth_noteon(f_synth, m_ctrlInChannel, extMidiNotes[i].note, extMidiNotes[i].velo);
  880. else
  881. fluid_synth_noteoff(f_synth, m_ctrlInChannel, extMidiNotes[i].note);
  882. extMidiNotes[i].channel = -1; // mark as invalid
  883. midiEventCount += 1;
  884. }
  885. engineMidiUnlock();
  886. } // End of MIDI Input (External)
  887. CARLA_PROCESS_CONTINUE_CHECK;
  888. // ----------------------------------------------------------------------------------------------------
  889. // MIDI Input (System)
  890. {
  891. const CarlaEngineMidiEvent* minEvent;
  892. uint32_t time, nEvents = midi.portMin->getEventCount();
  893. for (i=0; i < nEvents && midiEventCount < MAX_MIDI_EVENTS; i++)
  894. {
  895. minEvent = midi.portMin->getEvent(i);
  896. if (! minEvent)
  897. continue;
  898. time = minEvent->time - framesOffset;
  899. if (time >= frames)
  900. continue;
  901. uint8_t status = minEvent->data[0];
  902. uint8_t channel = status & 0x0F;
  903. // Fix bad note-off
  904. if (MIDI_IS_STATUS_NOTE_ON(status) && minEvent->data[2] == 0)
  905. status -= 0x10;
  906. if (MIDI_IS_STATUS_NOTE_OFF(status))
  907. {
  908. uint8_t note = minEvent->data[1];
  909. fluid_synth_noteoff(f_synth, channel, note);
  910. postponeEvent(PluginPostEventNoteOff, channel, note, 0.0);
  911. }
  912. else if (MIDI_IS_STATUS_NOTE_ON(status))
  913. {
  914. uint8_t note = minEvent->data[1];
  915. uint8_t velo = minEvent->data[2];
  916. fluid_synth_noteon(f_synth, channel, note, velo);
  917. postponeEvent(PluginPostEventNoteOn, channel, note, velo);
  918. }
  919. else if (MIDI_IS_STATUS_POLYPHONIC_AFTERTOUCH(status))
  920. {
  921. // TODO, not in fluidsynth API?
  922. }
  923. else if (MIDI_IS_STATUS_AFTERTOUCH(status))
  924. {
  925. uint8_t pressure = minEvent->data[1];
  926. fluid_synth_channel_pressure(f_synth, channel, pressure);
  927. }
  928. else if (MIDI_IS_STATUS_PITCH_WHEEL_CONTROL(status))
  929. {
  930. uint8_t lsb = minEvent->data[1];
  931. uint8_t msb = minEvent->data[2];
  932. fluid_synth_pitch_bend(f_synth, channel, (msb << 7) | lsb);
  933. }
  934. else
  935. continue;
  936. midiEventCount += 1;
  937. }
  938. } // End of MIDI Input (System)
  939. } // End of MIDI Input
  940. CARLA_PROCESS_CONTINUE_CHECK;
  941. // --------------------------------------------------------------------------------------------------------
  942. // Plugin processing
  943. if (m_active)
  944. {
  945. if (! m_activeBefore)
  946. {
  947. for (int c=0; c < MAX_MIDI_CHANNELS; c++)
  948. {
  949. #ifdef FLUIDSYNTH_VERSION_NEW_API
  950. fluid_synth_all_notes_off(f_synth, c);
  951. fluid_synth_all_sounds_off(f_synth, c);
  952. #else
  953. fluid_synth_cc(f_synth, c, MIDI_CONTROL_ALL_SOUND_OFF, 0);
  954. fluid_synth_cc(f_synth, c, MIDI_CONTROL_ALL_NOTES_OFF, 0);
  955. #endif
  956. }
  957. }
  958. fluid_synth_process(f_synth, frames, 0, nullptr, 2, outBuffer);
  959. }
  960. CARLA_PROCESS_CONTINUE_CHECK;
  961. // --------------------------------------------------------------------------------------------------------
  962. // Post-processing (balance and volume)
  963. if (m_active)
  964. {
  965. bool do_balance = (x_balanceLeft != -1.0 || x_balanceRight != 1.0);
  966. double bal_rangeL, bal_rangeR;
  967. float oldBufLeft[do_balance ? frames : 0];
  968. for (i=0; i < aOut.count; i++)
  969. {
  970. // Balance
  971. if (do_balance)
  972. {
  973. if (i%2 == 0)
  974. memcpy(&oldBufLeft, outBuffer[i], sizeof(float)*frames);
  975. bal_rangeL = (x_balanceLeft+1.0)/2;
  976. bal_rangeR = (x_balanceRight+1.0)/2;
  977. for (k=0; k < frames; k++)
  978. {
  979. if (i%2 == 0)
  980. {
  981. // left output
  982. outBuffer[i][k] = oldBufLeft[k]*(1.0-bal_rangeL);
  983. outBuffer[i][k] += outBuffer[i+1][k]*(1.0-bal_rangeR);
  984. }
  985. else
  986. {
  987. // right
  988. outBuffer[i][k] = outBuffer[i][k]*bal_rangeR;
  989. outBuffer[i][k] += oldBufLeft[k]*bal_rangeL;
  990. }
  991. }
  992. }
  993. // Volume, using fluidsynth internals
  994. fluid_synth_set_gain(f_synth, x_volume);
  995. // Output VU
  996. if (x_engine->processMode() != PROCESS_MODE_CONTINUOUS_RACK)
  997. {
  998. for (k=0; i < 2 && k < frames; k++)
  999. {
  1000. if (abs(outBuffer[i][k]) > aOutsPeak[i])
  1001. aOutsPeak[i] = abs(outBuffer[i][k]);
  1002. }
  1003. }
  1004. }
  1005. }
  1006. else
  1007. {
  1008. // disable any output sound if not active
  1009. for (i=0; i < aOut.count; i++)
  1010. carla_zeroF(outBuffer[i], frames);
  1011. aOutsPeak[0] = 0.0;
  1012. aOutsPeak[1] = 0.0;
  1013. } // End of Post-processing
  1014. CARLA_PROCESS_CONTINUE_CHECK;
  1015. // --------------------------------------------------------------------------------------------------------
  1016. // Control Output
  1017. if (m_active)
  1018. {
  1019. k = FluidSynthVoiceCount;
  1020. paramBuffers[k] = fluid_synth_get_active_voice_count(f_synth);
  1021. fixParameterValue(paramBuffers[k], param.ranges[k]);
  1022. if (param.data[k].midiCC > 0)
  1023. {
  1024. double value = (paramBuffers[k] - param.ranges[k].min) / (param.ranges[k].max - param.ranges[k].min);
  1025. param.portCout->writeEvent(CarlaEngineParameterChangeEvent, framesOffset, param.data[k].midiChannel, param.data[k].midiCC, value);
  1026. }
  1027. } // End of Control Output
  1028. CARLA_PROCESS_CONTINUE_CHECK;
  1029. // --------------------------------------------------------------------------------------------------------
  1030. // Peak Values
  1031. x_engine->setOutputPeak(m_id, 0, aOutsPeak[0]);
  1032. x_engine->setOutputPeak(m_id, 1, aOutsPeak[1]);
  1033. m_activeBefore = m_active;
  1034. }
  1035. // -------------------------------------------------------------------
  1036. bool init(const char* const filename, const char* const name, const char* const label)
  1037. {
  1038. // ---------------------------------------------------------------
  1039. // open soundfont
  1040. f_id = fluid_synth_sfload(f_synth, filename, 0);
  1041. if (f_id < 0)
  1042. {
  1043. x_engine->setLastError("Failed to load SoundFont file");
  1044. return false;
  1045. }
  1046. // ---------------------------------------------------------------
  1047. // get info
  1048. m_filename = strdup(filename);
  1049. m_label = strdup(label);
  1050. if (name)
  1051. m_name = x_engine->getUniquePluginName(name);
  1052. else
  1053. m_name = x_engine->getUniquePluginName(label);
  1054. // ---------------------------------------------------------------
  1055. // register client
  1056. x_client = x_engine->addClient(this);
  1057. if (! x_client->isOk())
  1058. {
  1059. x_engine->setLastError("Failed to register plugin client");
  1060. return false;
  1061. }
  1062. return true;
  1063. }
  1064. private:
  1065. enum FluidSynthInputParameters {
  1066. FluidSynthReverbOnOff = 0,
  1067. FluidSynthReverbRoomSize = 1,
  1068. FluidSynthReverbDamp = 2,
  1069. FluidSynthReverbLevel = 3,
  1070. FluidSynthReverbWidth = 4,
  1071. FluidSynthChorusOnOff = 5,
  1072. FluidSynthChorusNr = 6,
  1073. FluidSynthChorusLevel = 7,
  1074. FluidSynthChorusSpeedHz = 8,
  1075. FluidSynthChorusDepthMs = 9,
  1076. FluidSynthChorusType = 10,
  1077. FluidSynthPolyphony = 11,
  1078. FluidSynthInterpolation = 12,
  1079. FluidSynthVoiceCount = 13,
  1080. FluidSynthParametersMax = 14
  1081. };
  1082. fluid_settings_t* f_settings;
  1083. fluid_synth_t* f_synth;
  1084. int f_id;
  1085. double paramBuffers[FluidSynthParametersMax];
  1086. const char* m_label;
  1087. };
  1088. /**@}*/
  1089. CARLA_BACKEND_END_NAMESPACE
  1090. #else // WANT_FLUIDSYNTH
  1091. # warning fluidsynth not available (no SF2 support)
  1092. #endif
  1093. CARLA_BACKEND_START_NAMESPACE
  1094. CarlaPlugin* CarlaPlugin::newSF2(const initializer& init)
  1095. {
  1096. qDebug("CarlaPlugin::newSF2(%p, \"%s\", \"%s\", \"%s\")", init.engine, init.filename, init.name, init.label);
  1097. #ifdef WANT_FLUIDSYNTH
  1098. short id = init.engine->getNewPluginId();
  1099. if (id < 0 || id > init.engine->maxPluginNumber())
  1100. {
  1101. init.engine->setLastError("Maximum number of plugins reached");
  1102. return nullptr;
  1103. }
  1104. if (! fluid_is_soundfont(init.filename))
  1105. {
  1106. init.engine->setLastError("Requested file is not a valid SoundFont");
  1107. return nullptr;
  1108. }
  1109. FluidSynthPlugin* const plugin = new FluidSynthPlugin(init.engine, id);
  1110. if (! plugin->init(init.filename, init.name, init.label))
  1111. {
  1112. delete plugin;
  1113. return nullptr;
  1114. }
  1115. plugin->reload();
  1116. plugin->registerToOscControl();
  1117. return plugin;
  1118. #else
  1119. init.engine->setLastError("fluidsynth support not available");
  1120. return nullptr;
  1121. #endif
  1122. }
  1123. CARLA_BACKEND_END_NAMESPACE