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.

1327 lines
44KB

  1. /*
  2. * Carla Backend
  3. * Copyright (C) 2011-2012 Filipe Coelho <falktx@gmail.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. #else
  24. #warning fluidsynth not available (no SF2 support)
  25. #endif
  26. CARLA_BACKEND_START_NAMESPACE
  27. #if 0
  28. } /* adjust editor indent */
  29. #endif
  30. #ifdef WANT_FLUIDSYNTH
  31. /*!
  32. * @defgroup CarlaBackendFluidSynthPlugin Carla Backend FluidSynth Plugin
  33. *
  34. * The Carla Backend FluidSynth Plugin.\n
  35. * http://www.fluidsynth.org/
  36. * @{
  37. */
  38. class FluidSynthPlugin : public CarlaPlugin
  39. {
  40. public:
  41. FluidSynthPlugin(CarlaEngine* const engine, unsigned short id) : CarlaPlugin(engine, id)
  42. {
  43. qDebug("FluidSynthPlugin::FluidSynthPlugin()");
  44. m_type = PLUGIN_SF2;
  45. m_label = nullptr;
  46. // create settings
  47. f_settings = new_fluid_settings();
  48. // define settings
  49. fluid_settings_setnum(f_settings, "synth.sample-rate", x_engine->getSampleRate());
  50. fluid_settings_setint(f_settings, "synth.threadsafe-api ", 0);
  51. // create synth
  52. f_synth = new_fluid_synth(f_settings);
  53. #ifdef FLUIDSYNTH_VERSION_NEW_API
  54. fluid_synth_set_sample_rate(f_synth, x_engine->getSampleRate());
  55. #endif
  56. // set default values
  57. fluid_synth_set_reverb_on(f_synth, 0);
  58. fluid_synth_set_reverb(f_synth, FLUID_REVERB_DEFAULT_ROOMSIZE, FLUID_REVERB_DEFAULT_DAMP, FLUID_REVERB_DEFAULT_WIDTH, FLUID_REVERB_DEFAULT_LEVEL);
  59. fluid_synth_set_chorus_on(f_synth, 0);
  60. 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);
  61. fluid_synth_set_polyphony(f_synth, 64);
  62. for (int i=0; i < 16; i++)
  63. fluid_synth_set_interp_method(f_synth, i, FLUID_INTERP_DEFAULT);
  64. }
  65. ~FluidSynthPlugin()
  66. {
  67. qDebug("FluidSynthPlugin::~FluidSynthPlugin()");
  68. if (m_label)
  69. free((void*)m_label);
  70. delete_fluid_synth(f_synth);
  71. delete_fluid_settings(f_settings);
  72. }
  73. // -------------------------------------------------------------------
  74. // Information (base)
  75. PluginCategory category()
  76. {
  77. return PLUGIN_CATEGORY_SYNTH;
  78. }
  79. // -------------------------------------------------------------------
  80. // Information (count)
  81. uint32_t parameterScalePointCount(uint32_t parameterId)
  82. {
  83. assert(parameterId < param.count);
  84. switch (parameterId)
  85. {
  86. case FluidSynthChorusType:
  87. return 2;
  88. case FluidSynthInterpolation:
  89. return 4;
  90. default:
  91. return 0;
  92. }
  93. }
  94. // -------------------------------------------------------------------
  95. // Information (per-plugin data)
  96. double getParameterValue(uint32_t parameterId)
  97. {
  98. assert(parameterId < param.count);
  99. return param_buffers[parameterId];
  100. }
  101. double getParameterScalePointValue(uint32_t parameterId, uint32_t scalePointId)
  102. {
  103. assert(parameterId < param.count);
  104. assert(scalePointId < parameterScalePointCount(parameterId));
  105. switch (parameterId)
  106. {
  107. case FluidSynthChorusType:
  108. switch (scalePointId)
  109. {
  110. case 0:
  111. return FLUID_CHORUS_MOD_SINE;
  112. case 1:
  113. return FLUID_CHORUS_MOD_TRIANGLE;
  114. default:
  115. return FLUID_CHORUS_DEFAULT_TYPE;
  116. }
  117. case FluidSynthInterpolation:
  118. switch (scalePointId)
  119. {
  120. case 0:
  121. return FLUID_INTERP_NONE;
  122. case 1:
  123. return FLUID_INTERP_LINEAR;
  124. case 2:
  125. return FLUID_INTERP_4THORDER;
  126. case 3:
  127. return FLUID_INTERP_7THORDER;
  128. default:
  129. return FLUID_INTERP_DEFAULT;
  130. }
  131. default:
  132. return 0.0;
  133. }
  134. }
  135. void getLabel(char* const strBuf)
  136. {
  137. strncpy(strBuf, m_label, STR_MAX);
  138. }
  139. void getMaker(char* const strBuf)
  140. {
  141. strncpy(strBuf, "FluidSynth SF2 engine", STR_MAX);
  142. }
  143. void getCopyright(char* const strBuf)
  144. {
  145. strncpy(strBuf, "GNU GPL v2+", STR_MAX);
  146. }
  147. void getRealName(char* const strBuf)
  148. {
  149. getLabel(strBuf);
  150. }
  151. void getParameterName(uint32_t parameterId, char* const strBuf)
  152. {
  153. assert(parameterId < param.count);
  154. switch (parameterId)
  155. {
  156. case FluidSynthReverbOnOff:
  157. strncpy(strBuf, "Reverb On/Off", STR_MAX);
  158. break;
  159. case FluidSynthReverbRoomSize:
  160. strncpy(strBuf, "Reverb Room Size", STR_MAX);
  161. break;
  162. case FluidSynthReverbDamp:
  163. strncpy(strBuf, "Reverb Damp", STR_MAX);
  164. break;
  165. case FluidSynthReverbLevel:
  166. strncpy(strBuf, "Reverb Level", STR_MAX);
  167. break;
  168. case FluidSynthReverbWidth:
  169. strncpy(strBuf, "Reverb Width", STR_MAX);
  170. break;
  171. case FluidSynthChorusOnOff:
  172. strncpy(strBuf, "Chorus On/Off", STR_MAX);
  173. break;
  174. case FluidSynthChorusNr:
  175. strncpy(strBuf, "Chorus Voice Count", STR_MAX);
  176. break;
  177. case FluidSynthChorusLevel:
  178. strncpy(strBuf, "Chorus Level", STR_MAX);
  179. break;
  180. case FluidSynthChorusSpeedHz:
  181. strncpy(strBuf, "Chorus Speed", STR_MAX);
  182. break;
  183. case FluidSynthChorusDepthMs:
  184. strncpy(strBuf, "Chorus Depth", STR_MAX);
  185. break;
  186. case FluidSynthChorusType:
  187. strncpy(strBuf, "Chorus Type", STR_MAX);
  188. break;
  189. case FluidSynthPolyphony:
  190. strncpy(strBuf, "Polyphony", STR_MAX);
  191. break;
  192. case FluidSynthInterpolation:
  193. strncpy(strBuf, "Interpolation", STR_MAX);
  194. break;
  195. case FluidSynthVoiceCount:
  196. strncpy(strBuf, "Voice Count", STR_MAX);
  197. break;
  198. default:
  199. *strBuf = 0;
  200. break;
  201. }
  202. }
  203. void getParameterUnit(uint32_t parameterId, char* const strBuf)
  204. {
  205. assert(parameterId < param.count);
  206. switch (parameterId)
  207. {
  208. case FluidSynthChorusSpeedHz:
  209. strncpy(strBuf, "Hz", STR_MAX);
  210. break;
  211. case FluidSynthChorusDepthMs:
  212. strncpy(strBuf, "ms", STR_MAX);
  213. break;
  214. default:
  215. *strBuf = 0;
  216. break;
  217. }
  218. }
  219. void getParameterScalePointLabel(uint32_t parameterId, uint32_t scalePointId, char* const strBuf)
  220. {
  221. assert(parameterId < param.count);
  222. assert(scalePointId < parameterScalePointCount(parameterId));
  223. switch (parameterId)
  224. {
  225. case FluidSynthChorusType:
  226. switch (scalePointId)
  227. {
  228. case 0:
  229. strncpy(strBuf, "Sine wave", STR_MAX);
  230. return;
  231. case 1:
  232. strncpy(strBuf, "Triangle wave", STR_MAX);
  233. return;
  234. }
  235. case FluidSynthInterpolation:
  236. switch (scalePointId)
  237. {
  238. case 0:
  239. strncpy(strBuf, "None", STR_MAX);
  240. return;
  241. case 1:
  242. strncpy(strBuf, "Straight-line", STR_MAX);
  243. return;
  244. case 2:
  245. strncpy(strBuf, "Fourth-order", STR_MAX);
  246. return;
  247. case 3:
  248. strncpy(strBuf, "Seventh-order", STR_MAX);
  249. return;
  250. }
  251. }
  252. *strBuf = 0;
  253. }
  254. // -------------------------------------------------------------------
  255. // Set data (plugin-specific stuff)
  256. void setParameterValue(uint32_t parameterId, double value, bool sendGui, bool sendOsc, bool sendCallback)
  257. {
  258. assert(parameterId < param.count);
  259. param_buffers[parameterId] = fixParameterValue(value, param.ranges[parameterId]);
  260. switch (parameterId)
  261. {
  262. case FluidSynthReverbOnOff:
  263. value = value > 0.5 ? 1 : 0;
  264. fluid_synth_set_reverb_on(f_synth, value);
  265. break;
  266. case FluidSynthReverbRoomSize:
  267. case FluidSynthReverbDamp:
  268. case FluidSynthReverbLevel:
  269. case FluidSynthReverbWidth:
  270. fluid_synth_set_reverb(f_synth, param_buffers[FluidSynthReverbRoomSize], param_buffers[FluidSynthReverbDamp], param_buffers[FluidSynthReverbWidth], param_buffers[FluidSynthReverbLevel]);
  271. break;
  272. case FluidSynthChorusOnOff:
  273. {
  274. const CarlaPluginScopedDisabler m(this, ! x_engine->isOffline());
  275. value = value > 0.5 ? 1 : 0;
  276. fluid_synth_set_chorus_on(f_synth, value);
  277. break;
  278. }
  279. case FluidSynthChorusNr:
  280. case FluidSynthChorusLevel:
  281. case FluidSynthChorusSpeedHz:
  282. case FluidSynthChorusDepthMs:
  283. case FluidSynthChorusType:
  284. {
  285. const CarlaPluginScopedDisabler m(this, ! x_engine->isOffline());
  286. fluid_synth_set_chorus(f_synth, rint(param_buffers[FluidSynthChorusNr]), param_buffers[FluidSynthChorusLevel], param_buffers[FluidSynthChorusSpeedHz], param_buffers[FluidSynthChorusDepthMs], rint(param_buffers[FluidSynthChorusType]));
  287. break;
  288. }
  289. case FluidSynthPolyphony:
  290. {
  291. const CarlaPluginScopedDisabler m(this, ! x_engine->isOffline());
  292. fluid_synth_set_polyphony(f_synth, rint(value));
  293. break;
  294. }
  295. case FluidSynthInterpolation:
  296. {
  297. const CarlaPluginScopedDisabler m(this, ! x_engine->isOffline());
  298. for (int i=0; i < 16; i++)
  299. fluid_synth_set_interp_method(f_synth, i, rint(value));
  300. break;
  301. }
  302. default:
  303. break;
  304. }
  305. CarlaPlugin::setParameterValue(parameterId, value, sendGui, sendOsc, sendCallback);
  306. }
  307. void setMidiProgram(int32_t index, bool sendGui, bool sendOsc, bool sendCallback, bool block)
  308. {
  309. assert(index < (int32_t)midiprog.count);
  310. if (cin_channel < 0 || cin_channel > 15)
  311. return;
  312. if (index >= 0)
  313. {
  314. if (x_engine->isOffline())
  315. {
  316. if (block) engineProcessLock();
  317. fluid_synth_program_select(f_synth, cin_channel, f_id, midiprog.data[index].bank, midiprog.data[index].program);
  318. if (block) engineProcessUnlock();
  319. }
  320. else
  321. {
  322. const CarlaPluginScopedDisabler m(this, block);
  323. fluid_synth_program_select(f_synth, cin_channel, f_id, midiprog.data[index].bank, midiprog.data[index].program);
  324. }
  325. }
  326. CarlaPlugin::setMidiProgram(index, sendGui, sendOsc, sendCallback, block);
  327. }
  328. // -------------------------------------------------------------------
  329. // Plugin state
  330. void reload()
  331. {
  332. qDebug("FluidSynthPlugin::reload() - start");
  333. // Safely disable plugin for reload
  334. const CarlaPluginScopedDisabler 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[STR_MAX];
  350. // ---------------------------------------
  351. // Audio Outputs
  352. #ifndef BUILD_BRIDGE
  353. if (carlaOptions.process_mode != 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.process_mode != 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.process_mode != 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.process_mode != 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.process_mode != 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;
  419. param.ranges[j].step = 1.0;
  420. param.ranges[j].stepSmall = 1.0;
  421. param.ranges[j].stepLarge = 1.0;
  422. param_buffers[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. param_buffers[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. param_buffers[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. param_buffers[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. param_buffers[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;
  494. param.ranges[j].step = 1.0;
  495. param.ranges[j].stepSmall = 1.0;
  496. param.ranges[j].stepLarge = 1.0;
  497. param_buffers[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. param_buffers[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. param_buffers[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. param_buffers[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. param_buffers[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. param_buffers[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. param_buffers[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. param_buffers[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. param_buffers[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(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. f_sfont = fluid_synth_get_sfont_by_id(f_synth, f_id);
  646. // initial check to know how much midi-programs we have
  647. f_sfont->iteration_start(f_sfont);
  648. while (f_sfont->iteration_next(f_sfont, &f_preset))
  649. midiprog.count += 1;
  650. if (midiprog.count > 0)
  651. midiprog.data = new midi_program_t [midiprog.count];
  652. // Update data
  653. uint32_t i = 0;
  654. f_sfont->iteration_start(f_sfont);
  655. while (f_sfont->iteration_next(f_sfont, &f_preset))
  656. {
  657. assert(i < midiprog.count);
  658. midiprog.data[i].bank = f_preset.get_banknum(&f_preset);
  659. midiprog.data[i].program = f_preset.get_num(&f_preset);
  660. midiprog.data[i].name = strdup(f_preset.get_name(&f_preset));
  661. i++;
  662. }
  663. //f_sfont->free(f_sfont);
  664. #ifndef BUILD_BRIDGE
  665. // Update OSC Names
  666. //osc_global_send_set_midi_program_count(m_id, midiprog.count);
  667. //for (i=0; i < midiprog.count; i++)
  668. // osc_global_send_set_midi_program_data(m_id, i, midiprog.data[i].bank, midiprog.data[i].program, midiprog.data[i].name);
  669. x_engine->callback(CALLBACK_RELOAD_PROGRAMS, m_id, 0, 0, 0.0);
  670. #endif
  671. if (init)
  672. {
  673. fluid_synth_program_reset(f_synth);
  674. if (midiprog.count > 0)
  675. {
  676. for (i=0; i < 16 && i != 9; i++)
  677. {
  678. fluid_synth_program_select(f_synth, i, f_id, midiprog.data[0].bank, midiprog.data[0].program);
  679. #ifdef FLUIDSYNTH_VERSION_NEW_API
  680. fluid_synth_set_channel_type(f_synth, i, CHANNEL_TYPE_MELODIC);
  681. #endif
  682. }
  683. fluid_synth_program_select(f_synth, 9, f_id, 128, 0);
  684. #ifdef FLUIDSYNTH_VERSION_NEW_API
  685. fluid_synth_set_channel_type(f_synth, 9, CHANNEL_TYPE_DRUM);
  686. #endif
  687. setMidiProgram(0, false, false, false, true);
  688. }
  689. }
  690. }
  691. // -------------------------------------------------------------------
  692. // Plugin processing
  693. void process(float**, float** outBuffer, uint32_t frames, uint32_t framesOffset)
  694. {
  695. uint32_t i, k;
  696. uint32_t midiEventCount = 0;
  697. double aouts_peak_tmp[2] = { 0.0 };
  698. CARLA_PROCESS_CONTINUE_CHECK;
  699. // --------------------------------------------------------------------------------------------------------
  700. // Parameters Input [Automation]
  701. if (m_active && m_activeBefore)
  702. {
  703. bool allNotesOffSent = false;
  704. const CarlaEngineControlEvent* cinEvent;
  705. uint32_t time, nEvents = param.portCin->getEventCount();
  706. unsigned char nextBankIds[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 0, 0, 0, 0, 0, 0 };
  707. if (midiprog.current >= 0 && midiprog.count > 0 && cin_channel >= 0 && cin_channel < 16)
  708. nextBankIds[cin_channel] = midiprog.data[midiprog.current].bank;
  709. for (i=0; i < nEvents; i++)
  710. {
  711. cinEvent = param.portCin->getEvent(i);
  712. if (! cinEvent)
  713. continue;
  714. time = cinEvent->time - framesOffset;
  715. if (time >= frames)
  716. continue;
  717. // Control change
  718. switch (cinEvent->type)
  719. {
  720. case CarlaEngineEventNull:
  721. break;
  722. case CarlaEngineEventControlChange:
  723. {
  724. double value;
  725. // Control backend stuff
  726. if (cinEvent->channel == cin_channel)
  727. {
  728. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(cinEvent->controller) && (m_hints & PLUGIN_CAN_DRYWET) > 0)
  729. {
  730. value = cinEvent->value;
  731. setDryWet(value, false, false);
  732. postponeEvent(PluginPostEventParameterChange, PARAMETER_DRYWET, 0, value);
  733. continue;
  734. }
  735. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(cinEvent->controller) && (m_hints & PLUGIN_CAN_VOLUME) > 0)
  736. {
  737. value = cinEvent->value*127/100;
  738. setVolume(value, false, false);
  739. postponeEvent(PluginPostEventParameterChange, PARAMETER_VOLUME, 0, value);
  740. continue;
  741. }
  742. if (MIDI_IS_CONTROL_BALANCE(cinEvent->controller) && (m_hints & PLUGIN_CAN_BALANCE) > 0)
  743. {
  744. double left, right;
  745. value = cinEvent->value/0.5 - 1.0;
  746. if (value < 0)
  747. {
  748. left = -1.0;
  749. right = (value*2)+1.0;
  750. }
  751. else if (value > 0)
  752. {
  753. left = (value*2)-1.0;
  754. right = 1.0;
  755. }
  756. else
  757. {
  758. left = -1.0;
  759. right = 1.0;
  760. }
  761. setBalanceLeft(left, false, false);
  762. setBalanceRight(right, false, false);
  763. postponeEvent(PluginPostEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  764. postponeEvent(PluginPostEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  765. continue;
  766. }
  767. }
  768. // Control plugin parameters
  769. for (k=0; k < param.count; k++)
  770. {
  771. if (param.data[k].midiChannel != cinEvent->channel)
  772. continue;
  773. if (param.data[k].midiCC != cinEvent->controller)
  774. continue;
  775. if (param.data[k].type != PARAMETER_INPUT)
  776. continue;
  777. if (param.data[k].hints & PARAMETER_IS_AUTOMABLE)
  778. {
  779. if (param.data[k].hints & PARAMETER_IS_BOOLEAN)
  780. {
  781. value = cinEvent->value < 0.5 ? param.ranges[k].min : param.ranges[k].max;
  782. }
  783. else
  784. {
  785. value = cinEvent->value * (param.ranges[k].max - param.ranges[k].min) + param.ranges[k].min;
  786. if (param.data[k].hints & PARAMETER_IS_INTEGER)
  787. value = rint(value);
  788. }
  789. setParameterValue(k, value, false, false, false);
  790. postponeEvent(PluginPostEventParameterChange, k, 0, value);
  791. }
  792. }
  793. break;
  794. }
  795. case CarlaEngineEventMidiBankChange:
  796. if (cinEvent->channel < 16)
  797. nextBankIds[cinEvent->channel] = rint(cinEvent->value);
  798. break;
  799. case CarlaEngineEventMidiProgramChange:
  800. if (cinEvent->channel < 16)
  801. {
  802. uint32_t bankId = nextBankIds[cinEvent->channel];
  803. uint32_t progId = rint(cinEvent->value);
  804. for (k=0; k < midiprog.count; k++)
  805. {
  806. if (midiprog.data[k].bank == bankId && midiprog.data[k].program == progId)
  807. {
  808. if (cinEvent->channel == cin_channel)
  809. {
  810. setMidiProgram(k, false, false, false, false);
  811. postponeEvent(PluginPostEventMidiProgramChange, k, 0, 0.0);
  812. }
  813. else
  814. fluid_synth_program_select(f_synth, cinEvent->channel, f_id, bankId, progId);
  815. break;
  816. }
  817. }
  818. }
  819. break;
  820. case CarlaEngineEventAllSoundOff:
  821. if (cinEvent->channel == cin_channel)
  822. {
  823. if (! allNotesOffSent)
  824. sendMidiAllNotesOff();
  825. allNotesOffSent = true;
  826. #ifdef FLUIDSYNTH_VERSION_NEW_API
  827. fluid_synth_all_notes_off(f_synth, cin_channel);
  828. fluid_synth_all_sounds_off(f_synth, cin_channel);
  829. }
  830. else if (cinEvent->channel < 16)
  831. {
  832. fluid_synth_all_notes_off(f_synth, cinEvent->channel);
  833. fluid_synth_all_sounds_off(f_synth, cinEvent->channel);
  834. #endif
  835. }
  836. break;
  837. case CarlaEngineEventAllNotesOff:
  838. if (cinEvent->channel == cin_channel)
  839. {
  840. if (! allNotesOffSent)
  841. sendMidiAllNotesOff();
  842. allNotesOffSent = true;
  843. #ifdef FLUIDSYNTH_VERSION_NEW_API
  844. fluid_synth_all_notes_off(f_synth, cin_channel);
  845. }
  846. else if (cinEvent->channel < 16)
  847. {
  848. fluid_synth_all_notes_off(f_synth, cinEvent->channel);
  849. #endif
  850. }
  851. break;
  852. }
  853. }
  854. } // End of Parameters Input
  855. CARLA_PROCESS_CONTINUE_CHECK;
  856. // --------------------------------------------------------------------------------------------------------
  857. // MIDI Input (External)
  858. if (cin_channel >= 0 && cin_channel < 16 && m_active && m_activeBefore)
  859. {
  860. engineMidiLock();
  861. for (i=0; i < MAX_MIDI_EVENTS && midiEventCount < MAX_MIDI_EVENTS; i++)
  862. {
  863. if (extMidiNotes[i].channel < 0)
  864. break;
  865. if (extMidiNotes[i].velo)
  866. fluid_synth_noteon(f_synth, cin_channel, extMidiNotes[i].note, extMidiNotes[i].velo);
  867. else
  868. fluid_synth_noteoff(f_synth, cin_channel, extMidiNotes[i].note);
  869. extMidiNotes[i].channel = -1;
  870. midiEventCount += 1;
  871. }
  872. engineMidiUnlock();
  873. } // End of MIDI Input (External)
  874. CARLA_PROCESS_CONTINUE_CHECK;
  875. // --------------------------------------------------------------------------------------------------------
  876. // MIDI Input (System)
  877. if (m_active && m_activeBefore)
  878. {
  879. const CarlaEngineMidiEvent* minEvent;
  880. uint32_t time, nEvents = midi.portMin->getEventCount();
  881. for (i=0; i < nEvents && midiEventCount < MAX_MIDI_EVENTS; i++)
  882. {
  883. minEvent = midi.portMin->getEvent(i);
  884. if (! minEvent)
  885. continue;
  886. time = minEvent->time - framesOffset;
  887. if (time >= frames)
  888. continue;
  889. uint8_t status = minEvent->data[0];
  890. uint8_t channel = status & 0x0F;
  891. // Fix bad note-off
  892. if (MIDI_IS_STATUS_NOTE_ON(status) && minEvent->data[2] == 0)
  893. status -= 0x10;
  894. if (MIDI_IS_STATUS_NOTE_OFF(status))
  895. {
  896. uint8_t note = minEvent->data[1];
  897. fluid_synth_noteoff(f_synth, channel, note);
  898. if (channel == cin_channel)
  899. postponeEvent(PluginPostEventNoteOff, channel, note, 0.0);
  900. }
  901. else if (MIDI_IS_STATUS_NOTE_ON(status))
  902. {
  903. uint8_t note = minEvent->data[1];
  904. uint8_t velo = minEvent->data[2];
  905. fluid_synth_noteon(f_synth, channel, note, velo);
  906. if (channel == cin_channel)
  907. postponeEvent(PluginPostEventNoteOn, channel, note, velo);
  908. }
  909. else if (MIDI_IS_STATUS_AFTERTOUCH(status))
  910. {
  911. uint8_t pressure = minEvent->data[1];
  912. fluid_synth_channel_pressure(f_synth, channel, pressure);
  913. }
  914. else if (MIDI_IS_STATUS_PITCH_WHEEL_CONTROL(status))
  915. {
  916. uint8_t lsb = minEvent->data[1];
  917. uint8_t msb = minEvent->data[2];
  918. fluid_synth_pitch_bend(f_synth, channel, (msb << 7) | lsb);
  919. }
  920. else
  921. continue;
  922. midiEventCount += 1;
  923. }
  924. } // End of MIDI Input (System)
  925. CARLA_PROCESS_CONTINUE_CHECK;
  926. // --------------------------------------------------------------------------------------------------------
  927. // Plugin processing
  928. if (m_active)
  929. {
  930. if (! m_activeBefore)
  931. {
  932. if (cin_channel >= 0 && cin_channel < 16)
  933. {
  934. fluid_synth_cc(f_synth, cin_channel, MIDI_CONTROL_ALL_SOUND_OFF, 0);
  935. fluid_synth_cc(f_synth, cin_channel, MIDI_CONTROL_ALL_NOTES_OFF, 0);
  936. }
  937. #ifdef FLUIDSYNTH_VERSION_NEW_API
  938. for (i=0; i < 16; i++)
  939. {
  940. fluid_synth_all_notes_off(f_synth, i);
  941. fluid_synth_all_sounds_off(f_synth, i);
  942. }
  943. #endif
  944. }
  945. fluid_synth_process(f_synth, frames, 0, nullptr, 2, outBuffer);
  946. }
  947. CARLA_PROCESS_CONTINUE_CHECK;
  948. // --------------------------------------------------------------------------------------------------------
  949. // Post-processing (volume and balance)
  950. if (m_active)
  951. {
  952. bool do_balance = (x_bal_left != -1.0 || x_bal_right != 1.0);
  953. double bal_rangeL, bal_rangeR;
  954. float oldBufLeft[do_balance ? frames : 0];
  955. for (i=0; i < aout.count; i++)
  956. {
  957. // Volume, using fluidsynth internals
  958. fluid_synth_set_gain(f_synth, x_vol);
  959. // Balance
  960. if (do_balance)
  961. {
  962. if (i%2 == 0)
  963. memcpy(&oldBufLeft, outBuffer[i], sizeof(float)*frames);
  964. bal_rangeL = (x_bal_left+1.0)/2;
  965. bal_rangeR = (x_bal_right+1.0)/2;
  966. for (k=0; k < frames; k++)
  967. {
  968. if (i%2 == 0)
  969. {
  970. // left output
  971. outBuffer[i][k] = oldBufLeft[k]*(1.0-bal_rangeL);
  972. outBuffer[i][k] += outBuffer[i+1][k]*(1.0-bal_rangeR);
  973. }
  974. else
  975. {
  976. // right
  977. outBuffer[i][k] = outBuffer[i][k]*bal_rangeR;
  978. outBuffer[i][k] += oldBufLeft[k]*bal_rangeL;
  979. }
  980. }
  981. }
  982. // Output VU
  983. for (k=0; i < 2 && k < frames; k++)
  984. {
  985. if (abs(outBuffer[i][k]) > aouts_peak_tmp[i])
  986. aouts_peak_tmp[i] = abs(outBuffer[i][k]);
  987. }
  988. }
  989. }
  990. else
  991. {
  992. // disable any output sound if not active
  993. for (i=0; i < aout.count; i++)
  994. memset(outBuffer[i], 0.0f, sizeof(float)*frames);
  995. aouts_peak_tmp[0] = 0.0;
  996. aouts_peak_tmp[1] = 0.0;
  997. } // End of Post-processing
  998. CARLA_PROCESS_CONTINUE_CHECK;
  999. // --------------------------------------------------------------------------------------------------------
  1000. // Control Output
  1001. if (m_active)
  1002. {
  1003. k = FluidSynthVoiceCount;
  1004. param_buffers[k] = fluid_synth_get_active_voice_count(f_synth);
  1005. fixParameterValue(param_buffers[k], param.ranges[k]);
  1006. if (param.data[k].midiCC > 0)
  1007. {
  1008. double value = (param_buffers[k] - param.ranges[k].min) / (param.ranges[k].max - param.ranges[k].min);
  1009. param.portCout->writeEvent(CarlaEngineEventControlChange, framesOffset, param.data[k].midiChannel, param.data[k].midiCC, value);
  1010. }
  1011. } // End of Control Output
  1012. CARLA_PROCESS_CONTINUE_CHECK;
  1013. // --------------------------------------------------------------------------------------------------------
  1014. // Peak Values
  1015. x_engine->setOutputPeak(m_id, 0, aouts_peak_tmp[0]);
  1016. x_engine->setOutputPeak(m_id, 1, aouts_peak_tmp[1]);
  1017. m_activeBefore = m_active;
  1018. }
  1019. // -------------------------------------------------------------------
  1020. bool init(const char* const filename, const char* const name, const char* const label)
  1021. {
  1022. // ---------------------------------------------------------------
  1023. // open soundfont
  1024. f_id = fluid_synth_sfload(f_synth, filename, 0);
  1025. if (f_id < 0)
  1026. {
  1027. setLastError("Failed to load SoundFont file");
  1028. return false;
  1029. }
  1030. // ---------------------------------------------------------------
  1031. // get info
  1032. m_filename = strdup(filename);
  1033. m_label = strdup(label);
  1034. if (name)
  1035. m_name = x_engine->getUniqueName(name);
  1036. else
  1037. m_name = x_engine->getUniqueName(label);
  1038. // ---------------------------------------------------------------
  1039. // register client
  1040. x_client = x_engine->addClient(this);
  1041. if (! x_client->isOk())
  1042. {
  1043. setLastError("Failed to register plugin client");
  1044. return false;
  1045. }
  1046. return true;
  1047. }
  1048. private:
  1049. enum FluidSynthInputParameters {
  1050. FluidSynthReverbOnOff = 0,
  1051. FluidSynthReverbRoomSize = 1,
  1052. FluidSynthReverbDamp = 2,
  1053. FluidSynthReverbLevel = 3,
  1054. FluidSynthReverbWidth = 4,
  1055. FluidSynthChorusOnOff = 5,
  1056. FluidSynthChorusNr = 6,
  1057. FluidSynthChorusLevel = 7,
  1058. FluidSynthChorusSpeedHz = 8,
  1059. FluidSynthChorusDepthMs = 9,
  1060. FluidSynthChorusType = 10,
  1061. FluidSynthPolyphony = 11,
  1062. FluidSynthInterpolation = 12,
  1063. FluidSynthVoiceCount = 13,
  1064. FluidSynthParametersMax = 14
  1065. };
  1066. fluid_settings_t* f_settings;
  1067. fluid_synth_t* f_synth;
  1068. int f_id;
  1069. double param_buffers[FluidSynthParametersMax];
  1070. const char* m_label;
  1071. };
  1072. #endif // WANT_FLUIDSYNTH
  1073. CarlaPlugin* CarlaPlugin::newSF2(const initializer& init)
  1074. {
  1075. qDebug("CarlaPlugin::newSF2(%p, \"%s\", \"%s\", \"%s\")", init.engine, init.filename, init.name, init.label);
  1076. #ifdef WANT_FLUIDSYNTH
  1077. short id = init.engine->getNewPluginId();
  1078. if (id < 0)
  1079. {
  1080. setLastError("Maximum number of plugins reached");
  1081. return nullptr;
  1082. }
  1083. if (! fluid_is_soundfont(init.filename))
  1084. {
  1085. setLastError("Requested file is not a valid SoundFont");
  1086. return nullptr;
  1087. }
  1088. FluidSynthPlugin* const plugin = new FluidSynthPlugin(init.engine, id);
  1089. if (! plugin->init(init.filename, init.name, init.label))
  1090. {
  1091. delete plugin;
  1092. return nullptr;
  1093. }
  1094. plugin->reload();
  1095. plugin->registerToOsc();
  1096. return plugin;
  1097. #else
  1098. setLastError("fluidsynth support not available");
  1099. return nullptr;
  1100. #endif
  1101. }
  1102. /**@}*/
  1103. CARLA_BACKEND_END_NAMESPACE