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.

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