Audio plugin host https://kx.studio/carla
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.

CarlaPluginFluidSynth.cpp 65KB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
9 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781
  1. /*
  2. * Carla FluidSynth Plugin
  3. * Copyright (C) 2011-2018 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or 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 doc/GPL.txt file.
  16. */
  17. #include "CarlaPluginInternal.hpp"
  18. #include "CarlaEngine.hpp"
  19. #ifdef HAVE_FLUIDSYNTH
  20. #include "CarlaMathUtils.hpp"
  21. #include "water/text/StringArray.h"
  22. #include <fluidsynth.h>
  23. #if (FLUIDSYNTH_VERSION_MAJOR >= 1 && FLUIDSYNTH_VERSION_MINOR >= 1 && FLUIDSYNTH_VERSION_MICRO >= 4)
  24. # define FLUIDSYNTH_VERSION_NEW_API
  25. #endif
  26. #define FLUID_DEFAULT_POLYPHONY 64
  27. using water::String;
  28. using water::StringArray;
  29. CARLA_BACKEND_START_NAMESPACE
  30. // -------------------------------------------------------------------------------------------------------------------
  31. // Fallback data
  32. static const ExternalMidiNote kExternalMidiNoteFallback = { -1, 0, 0 };
  33. // -------------------------------------------------------------------------------------------------------------------
  34. class CarlaPluginFluidSynth : public CarlaPlugin
  35. {
  36. public:
  37. CarlaPluginFluidSynth(CarlaEngine* const engine, const uint id, const bool use16Outs)
  38. : CarlaPlugin(engine, id),
  39. kUse16Outs(use16Outs),
  40. fSettings(nullptr),
  41. fSynth(nullptr),
  42. fSynthId(0),
  43. fAudio16Buffers(nullptr),
  44. fLabel(nullptr)
  45. {
  46. carla_debug("CarlaPluginFluidSynth::CarlaPluginFluidSynth(%p, %i, %s)", engine, id, bool2str(use16Outs));
  47. carla_zeroFloats(fParamBuffers, FluidSynthParametersMax);
  48. carla_fill<int32_t>(fCurMidiProgs, 0, MAX_MIDI_CHANNELS);
  49. // create settings
  50. fSettings = new_fluid_settings();
  51. CARLA_SAFE_ASSERT_RETURN(fSettings != nullptr,);
  52. // define settings
  53. fluid_settings_setint(fSettings, "synth.audio-channels", use16Outs ? 16 : 1);
  54. fluid_settings_setint(fSettings, "synth.audio-groups", use16Outs ? 16 : 1);
  55. fluid_settings_setnum(fSettings, "synth.sample-rate", pData->engine->getSampleRate());
  56. //fluid_settings_setnum(fSettings, "synth.cpu-cores", 2);
  57. fluid_settings_setint(fSettings, "synth.parallel-render", 1);
  58. fluid_settings_setint(fSettings, "synth.threadsafe-api", 0);
  59. // create synth
  60. fSynth = new_fluid_synth(fSettings);
  61. CARLA_SAFE_ASSERT_RETURN(fSynth != nullptr,);
  62. #ifdef FLUIDSYNTH_VERSION_NEW_API
  63. fluid_synth_set_sample_rate(fSynth, (float)pData->engine->getSampleRate());
  64. #endif
  65. // set default values
  66. fluid_synth_set_reverb_on(fSynth, 1);
  67. fluid_synth_set_reverb(fSynth, FLUID_REVERB_DEFAULT_ROOMSIZE, FLUID_REVERB_DEFAULT_DAMP, FLUID_REVERB_DEFAULT_WIDTH, FLUID_REVERB_DEFAULT_LEVEL);
  68. fluid_synth_set_chorus_on(fSynth, 1);
  69. fluid_synth_set_chorus(fSynth, FLUID_CHORUS_DEFAULT_N, FLUID_CHORUS_DEFAULT_LEVEL, FLUID_CHORUS_DEFAULT_SPEED, FLUID_CHORUS_DEFAULT_DEPTH, FLUID_CHORUS_DEFAULT_TYPE);
  70. fluid_synth_set_polyphony(fSynth, FLUID_DEFAULT_POLYPHONY);
  71. fluid_synth_set_gain(fSynth, 1.0f);
  72. for (int i=0; i < MAX_MIDI_CHANNELS; ++i)
  73. fluid_synth_set_interp_method(fSynth, i, FLUID_INTERP_DEFAULT);
  74. }
  75. ~CarlaPluginFluidSynth() override
  76. {
  77. carla_debug("CarlaPluginFluidSynth::~CarlaPluginFluidSynth()");
  78. pData->singleMutex.lock();
  79. pData->masterMutex.lock();
  80. if (pData->client != nullptr && pData->client->isActive())
  81. pData->client->deactivate();
  82. if (pData->active)
  83. {
  84. deactivate();
  85. pData->active = false;
  86. }
  87. if (fSynth != nullptr)
  88. {
  89. delete_fluid_synth(fSynth);
  90. fSynth = nullptr;
  91. }
  92. if (fSettings != nullptr)
  93. {
  94. delete_fluid_settings(fSettings);
  95. fSettings = nullptr;
  96. }
  97. if (fLabel != nullptr)
  98. {
  99. delete[] fLabel;
  100. fLabel = nullptr;
  101. }
  102. clearBuffers();
  103. }
  104. // -------------------------------------------------------------------
  105. // Information (base)
  106. PluginType getType() const noexcept override
  107. {
  108. return PLUGIN_SF2;
  109. }
  110. PluginCategory getCategory() const noexcept override
  111. {
  112. return PLUGIN_CATEGORY_SYNTH;
  113. }
  114. // -------------------------------------------------------------------
  115. // Information (count)
  116. uint32_t getParameterScalePointCount(const uint32_t parameterId) const noexcept override
  117. {
  118. switch (parameterId)
  119. {
  120. case FluidSynthChorusType:
  121. return 2;
  122. case FluidSynthInterpolation:
  123. return 4;
  124. default:
  125. return 0;
  126. }
  127. }
  128. // -------------------------------------------------------------------
  129. // Information (current data)
  130. // nothing
  131. // -------------------------------------------------------------------
  132. // Information (per-plugin data)
  133. uint getOptionsAvailable() const noexcept override
  134. {
  135. uint options = 0x0;
  136. options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  137. options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  138. options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  139. options |= PLUGIN_OPTION_SEND_PITCHBEND;
  140. options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  141. return options;
  142. }
  143. float getParameterValue(const uint32_t parameterId) const noexcept override
  144. {
  145. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0.0f);
  146. return fParamBuffers[parameterId];
  147. }
  148. float getParameterScalePointValue(const uint32_t parameterId, const uint32_t scalePointId) const noexcept override
  149. {
  150. switch (parameterId)
  151. {
  152. case FluidSynthChorusType:
  153. switch (scalePointId)
  154. {
  155. case 0:
  156. return FLUID_CHORUS_MOD_SINE;
  157. case 1:
  158. return FLUID_CHORUS_MOD_TRIANGLE;
  159. default:
  160. return FLUID_CHORUS_DEFAULT_TYPE;
  161. }
  162. case FluidSynthInterpolation:
  163. switch (scalePointId)
  164. {
  165. case 0:
  166. return FLUID_INTERP_NONE;
  167. case 1:
  168. return FLUID_INTERP_LINEAR;
  169. case 2:
  170. return FLUID_INTERP_4THORDER;
  171. case 3:
  172. return FLUID_INTERP_7THORDER;
  173. default:
  174. return FLUID_INTERP_DEFAULT;
  175. }
  176. default:
  177. return 0.0f;
  178. }
  179. }
  180. void getLabel(char* const strBuf) const noexcept override
  181. {
  182. if (fLabel != nullptr)
  183. {
  184. std::strncpy(strBuf, fLabel, STR_MAX);
  185. return;
  186. }
  187. CarlaPlugin::getLabel(strBuf);
  188. }
  189. void getMaker(char* const strBuf) const noexcept override
  190. {
  191. std::strncpy(strBuf, "FluidSynth SF2 engine", STR_MAX);
  192. }
  193. void getCopyright(char* const strBuf) const noexcept override
  194. {
  195. std::strncpy(strBuf, "GNU GPL v2+", STR_MAX);
  196. }
  197. void getRealName(char* const strBuf) const noexcept override
  198. {
  199. getLabel(strBuf);
  200. }
  201. void getParameterName(const uint32_t parameterId, char* const strBuf) const noexcept override
  202. {
  203. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  204. switch (parameterId)
  205. {
  206. case FluidSynthReverbOnOff:
  207. std::strncpy(strBuf, "Reverb On/Off", STR_MAX);
  208. return;
  209. case FluidSynthReverbRoomSize:
  210. std::strncpy(strBuf, "Reverb Room Size", STR_MAX);
  211. return;
  212. case FluidSynthReverbDamp:
  213. std::strncpy(strBuf, "Reverb Damp", STR_MAX);
  214. return;
  215. case FluidSynthReverbLevel:
  216. std::strncpy(strBuf, "Reverb Level", STR_MAX);
  217. return;
  218. case FluidSynthReverbWidth:
  219. std::strncpy(strBuf, "Reverb Width", STR_MAX);
  220. return;
  221. case FluidSynthChorusOnOff:
  222. std::strncpy(strBuf, "Chorus On/Off", STR_MAX);
  223. return;
  224. case FluidSynthChorusNr:
  225. std::strncpy(strBuf, "Chorus Voice Count", STR_MAX);
  226. return;
  227. case FluidSynthChorusLevel:
  228. std::strncpy(strBuf, "Chorus Level", STR_MAX);
  229. return;
  230. case FluidSynthChorusSpeedHz:
  231. std::strncpy(strBuf, "Chorus Speed", STR_MAX);
  232. return;
  233. case FluidSynthChorusDepthMs:
  234. std::strncpy(strBuf, "Chorus Depth", STR_MAX);
  235. return;
  236. case FluidSynthChorusType:
  237. std::strncpy(strBuf, "Chorus Type", STR_MAX);
  238. return;
  239. case FluidSynthPolyphony:
  240. std::strncpy(strBuf, "Polyphony", STR_MAX);
  241. return;
  242. case FluidSynthInterpolation:
  243. std::strncpy(strBuf, "Interpolation", STR_MAX);
  244. return;
  245. case FluidSynthVoiceCount:
  246. std::strncpy(strBuf, "Voice Count", STR_MAX);
  247. return;
  248. }
  249. CarlaPlugin::getParameterName(parameterId, strBuf);
  250. }
  251. void getParameterUnit(const uint32_t parameterId, char* const strBuf) const noexcept override
  252. {
  253. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  254. switch (parameterId)
  255. {
  256. case FluidSynthChorusSpeedHz:
  257. std::strncpy(strBuf, "Hz", STR_MAX);
  258. return;
  259. case FluidSynthChorusDepthMs:
  260. std::strncpy(strBuf, "ms", STR_MAX);
  261. return;
  262. }
  263. CarlaPlugin::getParameterUnit(parameterId, strBuf);
  264. }
  265. void getParameterScalePointLabel(const uint32_t parameterId, const uint32_t scalePointId, char* const strBuf) const noexcept override
  266. {
  267. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  268. CARLA_SAFE_ASSERT_RETURN(scalePointId < getParameterScalePointCount(parameterId),);
  269. switch (parameterId)
  270. {
  271. case FluidSynthChorusType:
  272. switch (scalePointId)
  273. {
  274. case 0:
  275. std::strncpy(strBuf, "Sine wave", STR_MAX);
  276. return;
  277. case 1:
  278. std::strncpy(strBuf, "Triangle wave", STR_MAX);
  279. return;
  280. }
  281. case FluidSynthInterpolation:
  282. switch (scalePointId)
  283. {
  284. case 0:
  285. std::strncpy(strBuf, "None", STR_MAX);
  286. return;
  287. case 1:
  288. std::strncpy(strBuf, "Straight-line", STR_MAX);
  289. return;
  290. case 2:
  291. std::strncpy(strBuf, "Fourth-order", STR_MAX);
  292. return;
  293. case 3:
  294. std::strncpy(strBuf, "Seventh-order", STR_MAX);
  295. return;
  296. }
  297. }
  298. CarlaPlugin::getParameterScalePointLabel(parameterId, scalePointId, strBuf);
  299. }
  300. // -------------------------------------------------------------------
  301. // Set data (state)
  302. void prepareForSave() override
  303. {
  304. char strBuf[STR_MAX+1];
  305. std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i", fCurMidiProgs[0], fCurMidiProgs[1], fCurMidiProgs[2], fCurMidiProgs[3],
  306. fCurMidiProgs[4], fCurMidiProgs[5], fCurMidiProgs[6], fCurMidiProgs[7],
  307. fCurMidiProgs[8], fCurMidiProgs[9], fCurMidiProgs[10], fCurMidiProgs[11],
  308. fCurMidiProgs[12], fCurMidiProgs[13], fCurMidiProgs[14], fCurMidiProgs[15]);
  309. CarlaPlugin::setCustomData(CUSTOM_DATA_TYPE_STRING, "midiPrograms", strBuf, false);
  310. }
  311. // -------------------------------------------------------------------
  312. // Set data (internal stuff)
  313. void setCtrlChannel(const int8_t channel, const bool sendOsc, const bool sendCallback) noexcept override
  314. {
  315. if (channel >= 0 && channel < MAX_MIDI_CHANNELS)
  316. pData->midiprog.current = fCurMidiProgs[channel];
  317. CarlaPlugin::setCtrlChannel(channel, sendOsc, sendCallback);
  318. }
  319. // -------------------------------------------------------------------
  320. // Set data (plugin-specific stuff)
  321. void setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  322. {
  323. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  324. CARLA_SAFE_ASSERT_RETURN(sendGui || sendOsc || sendCallback,);
  325. float fixedValue;
  326. {
  327. const ScopedSingleProcessLocker spl(this, (sendGui || sendOsc || sendCallback));
  328. fixedValue = setParameterValueInFluidSynth(parameterId, value);
  329. }
  330. CarlaPlugin::setParameterValue(parameterId, fixedValue, sendGui, sendOsc, sendCallback);
  331. }
  332. void setParameterValueRT(const uint32_t parameterId, const float value) noexcept override
  333. {
  334. const float fixedValue = setParameterValueInFluidSynth(parameterId, value);
  335. CarlaPlugin::setParameterValueRT(parameterId, fixedValue);
  336. }
  337. float setParameterValueInFluidSynth(const uint32_t parameterId, const float value) noexcept
  338. {
  339. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, value);
  340. const float fixedValue(pData->param.getFixedValue(parameterId, value));
  341. fParamBuffers[parameterId] = fixedValue;
  342. switch (parameterId)
  343. {
  344. case FluidSynthReverbOnOff:
  345. try {
  346. fluid_synth_set_reverb_on(fSynth, (fixedValue > 0.5f) ? 1 : 0);
  347. } CARLA_SAFE_EXCEPTION("fluid_synth_set_reverb_on")
  348. break;
  349. case FluidSynthReverbRoomSize:
  350. case FluidSynthReverbDamp:
  351. case FluidSynthReverbLevel:
  352. case FluidSynthReverbWidth:
  353. try {
  354. fluid_synth_set_reverb(fSynth,
  355. fParamBuffers[FluidSynthReverbRoomSize],
  356. fParamBuffers[FluidSynthReverbDamp],
  357. fParamBuffers[FluidSynthReverbWidth],
  358. fParamBuffers[FluidSynthReverbLevel]);
  359. } CARLA_SAFE_EXCEPTION("fluid_synth_set_reverb")
  360. break;
  361. case FluidSynthChorusOnOff:
  362. try {
  363. fluid_synth_set_chorus_on(fSynth, (value > 0.5f) ? 1 : 0);
  364. } CARLA_SAFE_EXCEPTION("fluid_synth_set_chorus_on")
  365. break;
  366. case FluidSynthChorusNr:
  367. case FluidSynthChorusLevel:
  368. case FluidSynthChorusSpeedHz:
  369. case FluidSynthChorusDepthMs:
  370. case FluidSynthChorusType:
  371. try {
  372. fluid_synth_set_chorus(fSynth,
  373. (int)fParamBuffers[FluidSynthChorusNr],
  374. fParamBuffers[FluidSynthChorusLevel],
  375. fParamBuffers[FluidSynthChorusSpeedHz],
  376. fParamBuffers[FluidSynthChorusDepthMs],
  377. (int)fParamBuffers[FluidSynthChorusType]);
  378. } CARLA_SAFE_EXCEPTION("fluid_synth_set_chorus")
  379. break;
  380. case FluidSynthPolyphony:
  381. try {
  382. fluid_synth_set_polyphony(fSynth, (int)value);
  383. } CARLA_SAFE_EXCEPTION("fluid_synth_set_polyphony")
  384. break;
  385. case FluidSynthInterpolation:
  386. for (int i=0; i < MAX_MIDI_CHANNELS; ++i)
  387. {
  388. try {
  389. fluid_synth_set_interp_method(fSynth, i, (int)value);
  390. } CARLA_SAFE_EXCEPTION_BREAK("fluid_synth_set_interp_method")
  391. }
  392. break;
  393. default:
  394. break;
  395. }
  396. return fixedValue;
  397. }
  398. void setCustomData(const char* const type, const char* const key, const char* const value, const bool sendGui) override
  399. {
  400. CARLA_SAFE_ASSERT_RETURN(fSynth != nullptr,);
  401. CARLA_SAFE_ASSERT_RETURN(type != nullptr && type[0] != '\0',);
  402. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  403. CARLA_SAFE_ASSERT_RETURN(value != nullptr && value[0] != '\0',);
  404. carla_debug("CarlaPluginFluidSynth::setCustomData(%s, \"%s\", \"%s\", %s)", type, key, value, bool2str(sendGui));
  405. if (std::strcmp(type, CUSTOM_DATA_TYPE_PROPERTY) == 0)
  406. return CarlaPlugin::setCustomData(type, key, value, sendGui);
  407. if (std::strcmp(type, CUSTOM_DATA_TYPE_STRING) != 0)
  408. return carla_stderr2("CarlaPluginFluidSynth::setCustomData(\"%s\", \"%s\", \"%s\", %s) - type is not string", type, key, value, bool2str(sendGui));
  409. if (std::strcmp(key, "midiPrograms") != 0)
  410. return carla_stderr2("CarlaPluginFluidSynth::setCustomData(\"%s\", \"%s\", \"%s\", %s) - type is not string", type, key, value, bool2str(sendGui));
  411. StringArray midiProgramList(StringArray::fromTokens(value, ":", ""));
  412. if (midiProgramList.size() == MAX_MIDI_CHANNELS)
  413. {
  414. uint8_t channel = 0;
  415. for (String *it=midiProgramList.begin(), *end=midiProgramList.end(); it != end; ++it)
  416. {
  417. const int index(it->getIntValue());
  418. if (index >= 0 && index < static_cast<int>(pData->midiprog.count))
  419. {
  420. const uint32_t bank = pData->midiprog.data[index].bank;
  421. const uint32_t program = pData->midiprog.data[index].program;
  422. fluid_synth_program_select(fSynth, channel, fSynthId, bank, program);
  423. fCurMidiProgs[channel] = index;
  424. if (pData->ctrlChannel == static_cast<int32_t>(channel))
  425. {
  426. pData->midiprog.current = index;
  427. pData->engine->callback(ENGINE_CALLBACK_MIDI_PROGRAM_CHANGED, pData->id, index, 0, 0.0f, nullptr);
  428. }
  429. }
  430. ++channel;
  431. }
  432. CARLA_SAFE_ASSERT(channel == MAX_MIDI_CHANNELS);
  433. }
  434. CarlaPlugin::setCustomData(type, key, value, sendGui);
  435. }
  436. void setMidiProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback, const bool doingInit) noexcept override
  437. {
  438. CARLA_SAFE_ASSERT_RETURN(fSynth != nullptr,);
  439. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->midiprog.count),);
  440. CARLA_SAFE_ASSERT_RETURN(sendGui || sendOsc || sendCallback || doingInit,);
  441. if (index >= 0 && pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  442. {
  443. const uint32_t bank = pData->midiprog.data[index].bank;
  444. const uint32_t program = pData->midiprog.data[index].program;
  445. const ScopedSingleProcessLocker spl(this, (sendGui || sendOsc || sendCallback));
  446. try {
  447. fluid_synth_program_select(fSynth, pData->ctrlChannel, fSynthId, bank, program);
  448. } CARLA_SAFE_EXCEPTION("fluid_synth_program_select")
  449. fCurMidiProgs[pData->ctrlChannel] = index;
  450. }
  451. CarlaPlugin::setMidiProgram(index, sendGui, sendOsc, sendCallback, doingInit);
  452. }
  453. void setMidiProgramRT(const uint32_t uindex) noexcept override
  454. {
  455. CARLA_SAFE_ASSERT_RETURN(fSynth != nullptr,);
  456. CARLA_SAFE_ASSERT_RETURN(uindex < pData->midiprog.count,);
  457. if (pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  458. {
  459. const uint32_t bank = pData->midiprog.data[uindex].bank;
  460. const uint32_t program = pData->midiprog.data[uindex].program;
  461. try {
  462. fluid_synth_program_select(fSynth, pData->ctrlChannel, fSynthId, bank, program);
  463. } CARLA_SAFE_EXCEPTION("fluid_synth_program_select")
  464. fCurMidiProgs[pData->ctrlChannel] = static_cast<int32_t>(uindex);
  465. }
  466. CarlaPlugin::setMidiProgramRT(uindex);
  467. }
  468. // -------------------------------------------------------------------
  469. // Set ui stuff
  470. // nothing
  471. // -------------------------------------------------------------------
  472. // Plugin state
  473. void reload() override
  474. {
  475. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr,);
  476. CARLA_SAFE_ASSERT_RETURN(fSynth != nullptr,);
  477. carla_debug("CarlaPluginFluidSynth::reload() - start");
  478. const EngineProcessMode processMode(pData->engine->getProccessMode());
  479. // Safely disable plugin for reload
  480. const ScopedDisabler sd(this);
  481. if (pData->active)
  482. deactivate();
  483. clearBuffers();
  484. uint32_t aOuts, params;
  485. aOuts = kUse16Outs ? 32 : 2;
  486. params = FluidSynthParametersMax;
  487. pData->audioOut.createNew(aOuts);
  488. pData->param.createNew(params, false);
  489. const uint portNameSize(pData->engine->getMaxPortNameSize());
  490. CarlaString portName;
  491. // ---------------------------------------
  492. // Audio Outputs
  493. if (kUse16Outs)
  494. {
  495. for (uint32_t i=0; i < 32; ++i)
  496. {
  497. portName.clear();
  498. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  499. {
  500. portName = pData->name;
  501. portName += ":";
  502. }
  503. portName += "out-";
  504. if ((i+2)/2 < 9)
  505. portName += "0";
  506. portName += CarlaString((i+2)/2);
  507. if (i % 2 == 0)
  508. portName += "L";
  509. else
  510. portName += "R";
  511. portName.truncate(portNameSize);
  512. pData->audioOut.ports[i].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false, i);
  513. pData->audioOut.ports[i].rindex = i;
  514. }
  515. fAudio16Buffers = new float*[aOuts];
  516. for (uint32_t i=0; i < aOuts; ++i)
  517. fAudio16Buffers[i] = nullptr;
  518. }
  519. else
  520. {
  521. // out-left
  522. portName.clear();
  523. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  524. {
  525. portName = pData->name;
  526. portName += ":";
  527. }
  528. portName += "out-left";
  529. portName.truncate(portNameSize);
  530. pData->audioOut.ports[0].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false, 0);
  531. pData->audioOut.ports[0].rindex = 0;
  532. // out-right
  533. portName.clear();
  534. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  535. {
  536. portName = pData->name;
  537. portName += ":";
  538. }
  539. portName += "out-right";
  540. portName.truncate(portNameSize);
  541. pData->audioOut.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false, 1);
  542. pData->audioOut.ports[1].rindex = 1;
  543. }
  544. // ---------------------------------------
  545. // Event Input
  546. {
  547. portName.clear();
  548. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  549. {
  550. portName = pData->name;
  551. portName += ":";
  552. }
  553. portName += "events-in";
  554. portName.truncate(portNameSize);
  555. pData->event.portIn = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true, 0);
  556. }
  557. // ---------------------------------------
  558. // Event Output
  559. {
  560. portName.clear();
  561. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  562. {
  563. portName = pData->name;
  564. portName += ":";
  565. }
  566. portName += "events-out";
  567. portName.truncate(portNameSize);
  568. pData->event.portOut = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false, 0);
  569. }
  570. // ---------------------------------------
  571. // Parameters
  572. {
  573. int j;
  574. // ----------------------
  575. j = FluidSynthReverbOnOff;
  576. pData->param.data[j].type = PARAMETER_INPUT;
  577. pData->param.data[j].hints = PARAMETER_IS_ENABLED /*| PARAMETER_IS_AUTOMABLE*/ | PARAMETER_IS_BOOLEAN;
  578. pData->param.data[j].index = j;
  579. pData->param.data[j].rindex = j;
  580. pData->param.ranges[j].min = 0.0f;
  581. pData->param.ranges[j].max = 1.0f;
  582. pData->param.ranges[j].def = 1.0f;
  583. pData->param.ranges[j].step = 1.0f;
  584. pData->param.ranges[j].stepSmall = 1.0f;
  585. pData->param.ranges[j].stepLarge = 1.0f;
  586. // ----------------------
  587. j = FluidSynthReverbRoomSize;
  588. pData->param.data[j].type = PARAMETER_INPUT;
  589. pData->param.data[j].hints = PARAMETER_IS_ENABLED /*| PARAMETER_IS_AUTOMABLE*/;
  590. pData->param.data[j].index = j;
  591. pData->param.data[j].rindex = j;
  592. pData->param.ranges[j].min = 0.0f;
  593. pData->param.ranges[j].max = 1.2f;
  594. pData->param.ranges[j].def = FLUID_REVERB_DEFAULT_ROOMSIZE;
  595. pData->param.ranges[j].step = 0.01f;
  596. pData->param.ranges[j].stepSmall = 0.0001f;
  597. pData->param.ranges[j].stepLarge = 0.1f;
  598. // ----------------------
  599. j = FluidSynthReverbDamp;
  600. pData->param.data[j].type = PARAMETER_INPUT;
  601. pData->param.data[j].hints = PARAMETER_IS_ENABLED /*| PARAMETER_IS_AUTOMABLE*/;
  602. pData->param.data[j].index = j;
  603. pData->param.data[j].rindex = j;
  604. pData->param.ranges[j].min = 0.0f;
  605. pData->param.ranges[j].max = 1.0f;
  606. pData->param.ranges[j].def = FLUID_REVERB_DEFAULT_DAMP;
  607. pData->param.ranges[j].step = 0.01f;
  608. pData->param.ranges[j].stepSmall = 0.0001f;
  609. pData->param.ranges[j].stepLarge = 0.1f;
  610. // ----------------------
  611. j = FluidSynthReverbLevel;
  612. pData->param.data[j].type = PARAMETER_INPUT;
  613. pData->param.data[j].hints = PARAMETER_IS_ENABLED /*| PARAMETER_IS_AUTOMABLE*/;
  614. pData->param.data[j].index = j;
  615. pData->param.data[j].rindex = j;
  616. pData->param.data[j].midiCC = MIDI_CONTROL_REVERB_SEND_LEVEL;
  617. pData->param.ranges[j].min = 0.0f;
  618. pData->param.ranges[j].max = 1.0f;
  619. pData->param.ranges[j].def = FLUID_REVERB_DEFAULT_LEVEL;
  620. pData->param.ranges[j].step = 0.01f;
  621. pData->param.ranges[j].stepSmall = 0.0001f;
  622. pData->param.ranges[j].stepLarge = 0.1f;
  623. // ----------------------
  624. j = FluidSynthReverbWidth;
  625. pData->param.data[j].type = PARAMETER_INPUT;
  626. pData->param.data[j].hints = PARAMETER_IS_ENABLED /*| PARAMETER_IS_AUTOMABLE*/;
  627. pData->param.data[j].index = j;
  628. pData->param.data[j].rindex = j;
  629. pData->param.ranges[j].min = 0.0f;
  630. pData->param.ranges[j].max = 10.0f; // should be 100, but that sounds too much
  631. pData->param.ranges[j].def = FLUID_REVERB_DEFAULT_WIDTH;
  632. pData->param.ranges[j].step = 0.01f;
  633. pData->param.ranges[j].stepSmall = 0.0001f;
  634. pData->param.ranges[j].stepLarge = 0.1f;
  635. // ----------------------
  636. j = FluidSynthChorusOnOff;
  637. pData->param.data[j].type = PARAMETER_INPUT;
  638. pData->param.data[j].hints = PARAMETER_IS_ENABLED | PARAMETER_IS_BOOLEAN;
  639. pData->param.data[j].index = j;
  640. pData->param.data[j].rindex = j;
  641. pData->param.ranges[j].min = 0.0f;
  642. pData->param.ranges[j].max = 1.0f;
  643. pData->param.ranges[j].def = 1.0f;
  644. pData->param.ranges[j].step = 1.0f;
  645. pData->param.ranges[j].stepSmall = 1.0f;
  646. pData->param.ranges[j].stepLarge = 1.0f;
  647. // ----------------------
  648. j = FluidSynthChorusNr;
  649. pData->param.data[j].type = PARAMETER_INPUT;
  650. pData->param.data[j].hints = PARAMETER_IS_ENABLED | PARAMETER_IS_INTEGER;
  651. pData->param.data[j].index = j;
  652. pData->param.data[j].rindex = j;
  653. pData->param.ranges[j].min = 0.0f;
  654. pData->param.ranges[j].max = 99.0f;
  655. pData->param.ranges[j].def = FLUID_CHORUS_DEFAULT_N;
  656. pData->param.ranges[j].step = 1.0f;
  657. pData->param.ranges[j].stepSmall = 1.0f;
  658. pData->param.ranges[j].stepLarge = 10.0f;
  659. // ----------------------
  660. j = FluidSynthChorusLevel;
  661. pData->param.data[j].type = PARAMETER_INPUT;
  662. pData->param.data[j].hints = PARAMETER_IS_ENABLED;
  663. pData->param.data[j].index = j;
  664. pData->param.data[j].rindex = j;
  665. pData->param.ranges[j].min = 0.0f;
  666. pData->param.ranges[j].max = 10.0f;
  667. pData->param.ranges[j].def = FLUID_CHORUS_DEFAULT_LEVEL;
  668. pData->param.ranges[j].step = 0.01f;
  669. pData->param.ranges[j].stepSmall = 0.0001f;
  670. pData->param.ranges[j].stepLarge = 0.1f;
  671. // ----------------------
  672. j = FluidSynthChorusSpeedHz;
  673. pData->param.data[j].type = PARAMETER_INPUT;
  674. pData->param.data[j].hints = PARAMETER_IS_ENABLED;
  675. pData->param.data[j].index = j;
  676. pData->param.data[j].rindex = j;
  677. pData->param.ranges[j].min = 0.29f;
  678. pData->param.ranges[j].max = 5.0f;
  679. pData->param.ranges[j].def = FLUID_CHORUS_DEFAULT_SPEED;
  680. pData->param.ranges[j].step = 0.01f;
  681. pData->param.ranges[j].stepSmall = 0.0001f;
  682. pData->param.ranges[j].stepLarge = 0.1f;
  683. // ----------------------
  684. j = FluidSynthChorusDepthMs;
  685. pData->param.data[j].type = PARAMETER_INPUT;
  686. pData->param.data[j].hints = PARAMETER_IS_ENABLED;
  687. pData->param.data[j].index = j;
  688. pData->param.data[j].rindex = j;
  689. pData->param.ranges[j].min = 0.0f;
  690. pData->param.ranges[j].max = float(2048.0 * 1000.0 / pData->engine->getSampleRate()); // FIXME?
  691. pData->param.ranges[j].def = FLUID_CHORUS_DEFAULT_DEPTH;
  692. pData->param.ranges[j].step = 0.01f;
  693. pData->param.ranges[j].stepSmall = 0.0001f;
  694. pData->param.ranges[j].stepLarge = 0.1f;
  695. // ----------------------
  696. j = FluidSynthChorusType;
  697. pData->param.data[j].type = PARAMETER_INPUT;
  698. pData->param.data[j].hints = PARAMETER_IS_ENABLED | PARAMETER_IS_INTEGER | PARAMETER_USES_SCALEPOINTS;
  699. pData->param.data[j].index = j;
  700. pData->param.data[j].rindex = j;
  701. pData->param.ranges[j].min = FLUID_CHORUS_MOD_SINE;
  702. pData->param.ranges[j].max = FLUID_CHORUS_MOD_TRIANGLE;
  703. pData->param.ranges[j].def = FLUID_CHORUS_DEFAULT_TYPE;
  704. pData->param.ranges[j].step = 1.0f;
  705. pData->param.ranges[j].stepSmall = 1.0f;
  706. pData->param.ranges[j].stepLarge = 1.0f;
  707. // ----------------------
  708. j = FluidSynthPolyphony;
  709. pData->param.data[j].type = PARAMETER_INPUT;
  710. pData->param.data[j].hints = PARAMETER_IS_ENABLED | PARAMETER_IS_INTEGER;
  711. pData->param.data[j].index = j;
  712. pData->param.data[j].rindex = j;
  713. pData->param.ranges[j].min = 1.0f;
  714. pData->param.ranges[j].max = 512.0f; // max theoric is 65535
  715. pData->param.ranges[j].def = (float)fluid_synth_get_polyphony(fSynth);
  716. pData->param.ranges[j].step = 1.0f;
  717. pData->param.ranges[j].stepSmall = 1.0f;
  718. pData->param.ranges[j].stepLarge = 10.0f;
  719. // ----------------------
  720. j = FluidSynthInterpolation;
  721. pData->param.data[j].type = PARAMETER_INPUT;
  722. pData->param.data[j].hints = PARAMETER_IS_ENABLED | PARAMETER_IS_INTEGER | PARAMETER_USES_SCALEPOINTS;
  723. pData->param.data[j].index = j;
  724. pData->param.data[j].rindex = j;
  725. pData->param.ranges[j].min = FLUID_INTERP_NONE;
  726. pData->param.ranges[j].max = FLUID_INTERP_HIGHEST;
  727. pData->param.ranges[j].def = FLUID_INTERP_DEFAULT;
  728. pData->param.ranges[j].step = 1.0f;
  729. pData->param.ranges[j].stepSmall = 1.0f;
  730. pData->param.ranges[j].stepLarge = 1.0f;
  731. // ----------------------
  732. j = FluidSynthVoiceCount;
  733. pData->param.data[j].type = PARAMETER_OUTPUT;
  734. pData->param.data[j].hints = PARAMETER_IS_ENABLED | PARAMETER_IS_AUTOMABLE | PARAMETER_IS_INTEGER;
  735. pData->param.data[j].index = j;
  736. pData->param.data[j].rindex = j;
  737. pData->param.ranges[j].min = 0.0f;
  738. pData->param.ranges[j].max = 65535.0f;
  739. pData->param.ranges[j].def = 0.0f;
  740. pData->param.ranges[j].step = 1.0f;
  741. pData->param.ranges[j].stepSmall = 1.0f;
  742. pData->param.ranges[j].stepLarge = 1.0f;
  743. for (j=0; j<FluidSynthParametersMax; ++j)
  744. fParamBuffers[j] = pData->param.ranges[j].def;
  745. }
  746. // ---------------------------------------
  747. // plugin hints
  748. pData->hints = 0x0;
  749. pData->hints |= PLUGIN_IS_SYNTH;
  750. pData->hints |= PLUGIN_CAN_VOLUME;
  751. pData->hints |= PLUGIN_USES_MULTI_PROGS;
  752. if (! kUse16Outs)
  753. pData->hints |= PLUGIN_CAN_BALANCE;
  754. // extra plugin hints
  755. pData->extraHints = 0x0;
  756. pData->extraHints |= PLUGIN_EXTRA_HINT_HAS_MIDI_IN;
  757. if (! kUse16Outs)
  758. pData->extraHints |= PLUGIN_EXTRA_HINT_CAN_RUN_RACK;
  759. bufferSizeChanged(pData->engine->getBufferSize());
  760. reloadPrograms(true);
  761. if (pData->active)
  762. activate();
  763. carla_debug("CarlaPluginFluidSynth::reload() - end");
  764. }
  765. void reloadPrograms(const bool doInit) override
  766. {
  767. carla_debug("CarlaPluginFluidSynth::reloadPrograms(%s)", bool2str(doInit));
  768. // save drum info in case we have one program for it
  769. bool hasDrums = false;
  770. uint32_t drumIndex, drumProg;
  771. drumIndex = drumProg = 0;
  772. // Delete old programs
  773. pData->midiprog.clear();
  774. // Query new programs
  775. uint32_t count = 0;
  776. if (fluid_sfont_t* const f_sfont = fluid_synth_get_sfont_by_id(fSynth, fSynthId))
  777. {
  778. fluid_preset_t f_preset;
  779. // initial check to know how many midi-programs we have
  780. f_sfont->iteration_start(f_sfont);
  781. for (; f_sfont->iteration_next(f_sfont, &f_preset);)
  782. ++count;
  783. // sound kits must always have at least 1 midi-program
  784. CARLA_SAFE_ASSERT_RETURN(count > 0,);
  785. pData->midiprog.createNew(count);
  786. // Update data
  787. int tmp;
  788. uint32_t i = 0;
  789. f_sfont->iteration_start(f_sfont);
  790. for (; f_sfont->iteration_next(f_sfont, &f_preset);)
  791. {
  792. CARLA_SAFE_ASSERT_BREAK(i < count);
  793. tmp = f_preset.get_banknum(&f_preset);
  794. pData->midiprog.data[i].bank = (tmp >= 0) ? static_cast<uint32_t>(tmp) : 0;
  795. tmp = f_preset.get_num(&f_preset);
  796. pData->midiprog.data[i].program = (tmp >= 0) ? static_cast<uint32_t>(tmp) : 0;
  797. pData->midiprog.data[i].name = carla_strdup(f_preset.get_name(&f_preset));
  798. if (pData->midiprog.data[i].bank == 128 && ! hasDrums)
  799. {
  800. hasDrums = true;
  801. drumIndex = i;
  802. drumProg = pData->midiprog.data[i].program;
  803. }
  804. ++i;
  805. }
  806. }
  807. else
  808. {
  809. // failing means 0 midi-programs, it shouldn't happen!
  810. carla_safe_assert("fluid_sfont_t* const f_sfont = fluid_synth_get_sfont_by_id(fSynth, fSynthId)", __FILE__, __LINE__);
  811. return;
  812. }
  813. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  814. // Update OSC Names
  815. if (pData->engine->isOscControlRegistered() && pData->id < pData->engine->getCurrentPluginCount())
  816. {
  817. pData->engine->oscSend_control_set_midi_program_count(pData->id, count);
  818. for (uint32_t i=0; i < count; ++i)
  819. pData->engine->oscSend_control_set_midi_program_data(pData->id, i, pData->midiprog.data[i].bank, pData->midiprog.data[i].program, pData->midiprog.data[i].name);
  820. }
  821. #endif
  822. if (doInit)
  823. {
  824. fluid_synth_program_reset(fSynth);
  825. // select first program, or 128 for ch10
  826. for (int i=0; i < MAX_MIDI_CHANNELS && i != 9; ++i)
  827. {
  828. #ifdef FLUIDSYNTH_VERSION_NEW_API
  829. fluid_synth_set_channel_type(fSynth, i, CHANNEL_TYPE_MELODIC);
  830. #endif
  831. fluid_synth_program_select(fSynth, i, fSynthId, pData->midiprog.data[0].bank, pData->midiprog.data[0].program);
  832. fCurMidiProgs[i] = 0;
  833. }
  834. if (hasDrums)
  835. {
  836. #ifdef FLUIDSYNTH_VERSION_NEW_API
  837. fluid_synth_set_channel_type(fSynth, 9, CHANNEL_TYPE_DRUM);
  838. #endif
  839. fluid_synth_program_select(fSynth, 9, fSynthId, 128, drumProg);
  840. fCurMidiProgs[9] = static_cast<int32_t>(drumIndex);
  841. }
  842. else
  843. {
  844. #ifdef FLUIDSYNTH_VERSION_NEW_API
  845. fluid_synth_set_channel_type(fSynth, 9, CHANNEL_TYPE_MELODIC);
  846. #endif
  847. fluid_synth_program_select(fSynth, 9, fSynthId, pData->midiprog.data[0].bank, pData->midiprog.data[0].program);
  848. fCurMidiProgs[9] = 0;
  849. }
  850. pData->midiprog.current = 0;
  851. }
  852. else
  853. {
  854. pData->engine->callback(ENGINE_CALLBACK_RELOAD_PROGRAMS, pData->id, 0, 0, 0.0f, nullptr);
  855. }
  856. }
  857. // -------------------------------------------------------------------
  858. // Plugin processing
  859. void process(const float** const, float** const audioOut, const float** const, float** const, const uint32_t frames) override
  860. {
  861. // --------------------------------------------------------------------------------------------------------
  862. // Check if active
  863. if (! pData->active)
  864. {
  865. // disable any output sound
  866. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  867. carla_zeroFloats(audioOut[i], frames);
  868. return;
  869. }
  870. // --------------------------------------------------------------------------------------------------------
  871. // Check if needs reset
  872. if (pData->needsReset)
  873. {
  874. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  875. {
  876. for (int i=0; i < MAX_MIDI_CHANNELS; ++i)
  877. {
  878. #ifdef FLUIDSYNTH_VERSION_NEW_API
  879. fluid_synth_all_notes_off(fSynth, i);
  880. fluid_synth_all_sounds_off(fSynth, i);
  881. #else
  882. fluid_synth_cc(fSynth, i, MIDI_CONTROL_ALL_SOUND_OFF, 0);
  883. fluid_synth_cc(fSynth, i, MIDI_CONTROL_ALL_NOTES_OFF, 0);
  884. #endif
  885. }
  886. }
  887. else if (pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  888. {
  889. for (int i=0; i < MAX_MIDI_NOTE; ++i)
  890. fluid_synth_noteoff(fSynth, pData->ctrlChannel, i);
  891. }
  892. pData->needsReset = false;
  893. }
  894. // --------------------------------------------------------------------------------------------------------
  895. // Event Input and Processing
  896. {
  897. // ----------------------------------------------------------------------------------------------------
  898. // MIDI Input (External)
  899. if (pData->extNotes.mutex.tryLock())
  900. {
  901. for (RtLinkedList<ExternalMidiNote>::Itenerator it = pData->extNotes.data.begin2(); it.valid(); it.next())
  902. {
  903. const ExternalMidiNote& note(it.getValue(kExternalMidiNoteFallback));
  904. CARLA_SAFE_ASSERT_CONTINUE(note.channel >= 0 && note.channel < MAX_MIDI_CHANNELS);
  905. if (note.velo > 0)
  906. fluid_synth_noteon(fSynth, note.channel, note.note, note.velo);
  907. else
  908. fluid_synth_noteoff(fSynth,note.channel, note.note);
  909. }
  910. pData->extNotes.data.clear();
  911. pData->extNotes.mutex.unlock();
  912. } // End of MIDI Input (External)
  913. // ----------------------------------------------------------------------------------------------------
  914. // Event Input (System)
  915. #ifndef BUILD_BRIDGE
  916. bool allNotesOffSent = false;
  917. #endif
  918. uint32_t timeOffset = 0;
  919. uint32_t nextBankIds[MAX_MIDI_CHANNELS] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 0, 0, 0, 0, 0, 0 };
  920. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0 && pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  921. nextBankIds[pData->ctrlChannel] = pData->midiprog.data[pData->midiprog.current].bank;
  922. for (uint32_t i=0, numEvents=pData->event.portIn->getEventCount(); i < numEvents; ++i)
  923. {
  924. const EngineEvent& event(pData->event.portIn->getEvent(i));
  925. if (event.time >= frames)
  926. continue;
  927. CARLA_ASSERT_INT2(event.time >= timeOffset, event.time, timeOffset);
  928. if (event.time > timeOffset)
  929. {
  930. if (processSingle(audioOut, event.time - timeOffset, timeOffset))
  931. {
  932. timeOffset = event.time;
  933. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0 && pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  934. nextBankIds[pData->ctrlChannel] = pData->midiprog.data[pData->midiprog.current].bank;
  935. }
  936. }
  937. // Control change
  938. switch (event.type)
  939. {
  940. case kEngineEventTypeNull:
  941. break;
  942. case kEngineEventTypeControl:
  943. {
  944. const EngineControlEvent& ctrlEvent = event.ctrl;
  945. switch (ctrlEvent.type)
  946. {
  947. case kEngineControlEventTypeNull:
  948. break;
  949. case kEngineControlEventTypeParameter:
  950. {
  951. #ifndef BUILD_BRIDGE
  952. // Control backend stuff
  953. if (event.channel == pData->ctrlChannel)
  954. {
  955. float value;
  956. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_DRYWET) != 0)
  957. {
  958. value = ctrlEvent.value;
  959. setDryWetRT(value);
  960. }
  961. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_VOLUME) != 0)
  962. {
  963. value = ctrlEvent.value*127.0f/100.0f;
  964. setVolumeRT(value);
  965. }
  966. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_BALANCE) != 0)
  967. {
  968. float left, right;
  969. value = ctrlEvent.value/0.5f - 1.0f;
  970. if (value < 0.0f)
  971. {
  972. left = -1.0f;
  973. right = (value*2.0f)+1.0f;
  974. }
  975. else if (value > 0.0f)
  976. {
  977. left = (value*2.0f)-1.0f;
  978. right = 1.0f;
  979. }
  980. else
  981. {
  982. left = -1.0f;
  983. right = 1.0f;
  984. }
  985. setBalanceLeftRT(left);
  986. setBalanceRightRT(right);
  987. }
  988. }
  989. #endif
  990. // Control plugin parameters
  991. for (uint32_t k=0; k < pData->param.count; ++k)
  992. {
  993. if (pData->param.data[k].midiChannel != event.channel)
  994. continue;
  995. if (pData->param.data[k].midiCC != ctrlEvent.param)
  996. continue;
  997. if (pData->param.data[k].hints != PARAMETER_INPUT)
  998. continue;
  999. if ((pData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  1000. continue;
  1001. float value;
  1002. if (pData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  1003. {
  1004. value = (ctrlEvent.value < 0.5f) ? pData->param.ranges[k].min : pData->param.ranges[k].max;
  1005. }
  1006. else
  1007. {
  1008. if (pData->param.data[k].hints & PARAMETER_IS_LOGARITHMIC)
  1009. value = pData->param.ranges[k].getUnnormalizedLogValue(ctrlEvent.value);
  1010. else
  1011. value = pData->param.ranges[k].getUnnormalizedValue(ctrlEvent.value);
  1012. if (pData->param.data[k].hints & PARAMETER_IS_INTEGER)
  1013. value = std::rint(value);
  1014. }
  1015. setParameterValueRT(k, value);
  1016. }
  1017. if ((pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0 && ctrlEvent.param < MAX_MIDI_CONTROL)
  1018. {
  1019. fluid_synth_cc(fSynth, event.channel, ctrlEvent.param, int(ctrlEvent.value*127.0f));
  1020. }
  1021. break;
  1022. }
  1023. case kEngineControlEventTypeMidiBank:
  1024. if (event.channel < MAX_MIDI_CHANNELS && (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  1025. nextBankIds[event.channel] = ctrlEvent.param;
  1026. break;
  1027. case kEngineControlEventTypeMidiProgram:
  1028. if (event.channel < MAX_MIDI_CHANNELS && (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  1029. {
  1030. const uint32_t bankId(nextBankIds[event.channel]);
  1031. const uint32_t progId(ctrlEvent.param);
  1032. // TODO int32_t midiprog.find(bank, prog)
  1033. for (uint32_t k=0; k < pData->midiprog.count; ++k)
  1034. {
  1035. if (pData->midiprog.data[k].bank == bankId && pData->midiprog.data[k].program == progId)
  1036. {
  1037. fluid_synth_program_select(fSynth, event.channel, fSynthId, bankId, progId);
  1038. fCurMidiProgs[event.channel] = static_cast<int32_t>(k);
  1039. if (event.channel == pData->ctrlChannel)
  1040. pData->postponeRtEvent(kPluginPostRtEventMidiProgramChange, static_cast<int32_t>(k), 0, 0.0f);
  1041. break;
  1042. }
  1043. }
  1044. }
  1045. break;
  1046. case kEngineControlEventTypeAllSoundOff:
  1047. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1048. {
  1049. #ifdef FLUIDSYNTH_VERSION_NEW_API
  1050. fluid_synth_all_sounds_off(fSynth, event.channel);
  1051. #else
  1052. fluid_synth_cc(fSynth, event.channel, MIDI_CONTROL_ALL_SOUND_OFF, 0);
  1053. #endif
  1054. }
  1055. break;
  1056. case kEngineControlEventTypeAllNotesOff:
  1057. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1058. {
  1059. #ifndef BUILD_BRIDGE
  1060. if (event.channel == pData->ctrlChannel && ! allNotesOffSent)
  1061. {
  1062. allNotesOffSent = true;
  1063. sendMidiAllNotesOffToCallback();
  1064. }
  1065. #endif
  1066. #ifdef FLUIDSYNTH_VERSION_NEW_API
  1067. fluid_synth_all_notes_off(fSynth, event.channel);
  1068. #else
  1069. fluid_synth_cc(fSynth, event.channel, MIDI_CONTROL_ALL_NOTES_OFF, 0);
  1070. #endif
  1071. }
  1072. break;
  1073. }
  1074. break;
  1075. }
  1076. case kEngineEventTypeMidi: {
  1077. const EngineMidiEvent& midiEvent(event.midi);
  1078. if (midiEvent.size > EngineMidiEvent::kDataSize)
  1079. continue;
  1080. uint8_t status = uint8_t(MIDI_GET_STATUS_FROM_DATA(midiEvent.data));
  1081. // Fix bad note-off
  1082. if (status == MIDI_STATUS_NOTE_ON && midiEvent.data[2] == 0)
  1083. status = MIDI_STATUS_NOTE_OFF;
  1084. switch (status)
  1085. {
  1086. case MIDI_STATUS_NOTE_OFF: {
  1087. const uint8_t note = midiEvent.data[1];
  1088. fluid_synth_noteoff(fSynth, event.channel, note);
  1089. pData->postponeRtEvent(kPluginPostRtEventNoteOff, event.channel, note, 0.0f);
  1090. break;
  1091. }
  1092. case MIDI_STATUS_NOTE_ON: {
  1093. const uint8_t note = midiEvent.data[1];
  1094. const uint8_t velo = midiEvent.data[2];
  1095. fluid_synth_noteon(fSynth, event.channel, note, velo);
  1096. pData->postponeRtEvent(kPluginPostRtEventNoteOn, event.channel, note, velo);
  1097. break;
  1098. }
  1099. case MIDI_STATUS_POLYPHONIC_AFTERTOUCH:
  1100. if (pData->options & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH)
  1101. {
  1102. //const uint8_t note = midiEvent.data[1];
  1103. //const uint8_t pressure = midiEvent.data[2];
  1104. // not in fluidsynth API
  1105. }
  1106. break;
  1107. case MIDI_STATUS_CONTROL_CHANGE:
  1108. if (pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES)
  1109. {
  1110. const uint8_t control = midiEvent.data[1];
  1111. const uint8_t value = midiEvent.data[2];
  1112. fluid_synth_cc(fSynth, event.channel, control, value);
  1113. }
  1114. break;
  1115. case MIDI_STATUS_CHANNEL_PRESSURE:
  1116. if (pData->options & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE)
  1117. {
  1118. const uint8_t pressure = midiEvent.data[1];
  1119. fluid_synth_channel_pressure(fSynth, event.channel, pressure);;
  1120. }
  1121. break;
  1122. case MIDI_STATUS_PITCH_WHEEL_CONTROL:
  1123. if (pData->options & PLUGIN_OPTION_SEND_PITCHBEND)
  1124. {
  1125. const uint8_t lsb = midiEvent.data[1];
  1126. const uint8_t msb = midiEvent.data[2];
  1127. const int value = ((msb << 7) | lsb);
  1128. fluid_synth_pitch_bend(fSynth, event.channel, value);
  1129. }
  1130. break;
  1131. default:
  1132. continue;
  1133. break;
  1134. } // switch (status)
  1135. } break;
  1136. } // switch (event.type)
  1137. }
  1138. pData->postRtEvents.trySplice();
  1139. if (frames > timeOffset)
  1140. processSingle(audioOut, frames - timeOffset, timeOffset);
  1141. } // End of Event Input and Processing
  1142. #ifndef BUILD_BRIDGE
  1143. // --------------------------------------------------------------------------------------------------------
  1144. // Control Output
  1145. {
  1146. uint32_t k = FluidSynthVoiceCount;
  1147. fParamBuffers[k] = float(fluid_synth_get_active_voice_count(fSynth));
  1148. pData->param.ranges[k].fixValue(fParamBuffers[k]);
  1149. if (pData->param.data[k].midiCC > 0)
  1150. {
  1151. float value(pData->param.ranges[k].getNormalizedValue(fParamBuffers[k]));
  1152. pData->event.portOut->writeControlEvent(0, pData->param.data[k].midiChannel, kEngineControlEventTypeParameter, static_cast<uint16_t>(pData->param.data[k].midiCC), value);
  1153. }
  1154. } // End of Control Output
  1155. #endif
  1156. }
  1157. bool processSingle(float** const outBuffer, const uint32_t frames, const uint32_t timeOffset)
  1158. {
  1159. CARLA_SAFE_ASSERT_RETURN(outBuffer != nullptr, false);
  1160. CARLA_SAFE_ASSERT_RETURN(frames > 0, false);
  1161. // --------------------------------------------------------------------------------------------------------
  1162. // Try lock, silence otherwise
  1163. #ifndef STOAT_TEST_BUILD
  1164. if (pData->engine->isOffline())
  1165. {
  1166. pData->singleMutex.lock();
  1167. }
  1168. else
  1169. #endif
  1170. if (! pData->singleMutex.tryLock())
  1171. {
  1172. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1173. {
  1174. for (uint32_t k=0; k < frames; ++k)
  1175. outBuffer[i][k+timeOffset] = 0.0f;
  1176. }
  1177. return false;
  1178. }
  1179. // --------------------------------------------------------------------------------------------------------
  1180. // Fill plugin buffers and Run plugin
  1181. if (kUse16Outs)
  1182. {
  1183. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1184. carla_zeroFloats(fAudio16Buffers[i], frames);
  1185. // FIXME use '32' or '16' instead of outs
  1186. fluid_synth_process(fSynth, static_cast<int>(frames),
  1187. 0, nullptr,
  1188. static_cast<int>(pData->audioOut.count), fAudio16Buffers);
  1189. }
  1190. else
  1191. {
  1192. fluid_synth_write_float(fSynth, static_cast<int>(frames),
  1193. outBuffer[0] + timeOffset, 0, 1,
  1194. outBuffer[1] + timeOffset, 0, 1);
  1195. }
  1196. #ifndef BUILD_BRIDGE
  1197. // --------------------------------------------------------------------------------------------------------
  1198. // Post-processing (volume and balance)
  1199. {
  1200. // note - balance not possible with kUse16Outs, so we can safely skip fAudioOutBuffers
  1201. const bool doVolume = (pData->hints & PLUGIN_CAN_VOLUME) != 0 && carla_isNotEqual(pData->postProc.volume, 1.0f);
  1202. const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) != 0 && ! (carla_isEqual(pData->postProc.balanceLeft, -1.0f) && carla_isEqual(pData->postProc.balanceRight, 1.0f));
  1203. float oldBufLeft[doBalance ? frames : 1];
  1204. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1205. {
  1206. // Balance
  1207. if (doBalance)
  1208. {
  1209. if (i % 2 == 0)
  1210. carla_copyFloats(oldBufLeft, outBuffer[i]+timeOffset, frames);
  1211. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  1212. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  1213. for (uint32_t k=0; k < frames; ++k)
  1214. {
  1215. if (i % 2 == 0)
  1216. {
  1217. // left
  1218. outBuffer[i][k+timeOffset] = oldBufLeft[k] * (1.0f - balRangeL);
  1219. outBuffer[i][k+timeOffset] += outBuffer[i+1][k+timeOffset] * (1.0f - balRangeR);
  1220. }
  1221. else
  1222. {
  1223. // right
  1224. outBuffer[i][k+timeOffset] = outBuffer[i][k+timeOffset] * balRangeR;
  1225. outBuffer[i][k+timeOffset] += oldBufLeft[k] * balRangeL;
  1226. }
  1227. }
  1228. }
  1229. // Volume
  1230. if (kUse16Outs)
  1231. {
  1232. for (uint32_t k=0; k < frames; ++k)
  1233. outBuffer[i][k+timeOffset] = fAudio16Buffers[i][k] * pData->postProc.volume;
  1234. }
  1235. else if (doVolume)
  1236. {
  1237. for (uint32_t k=0; k < frames; ++k)
  1238. outBuffer[i][k+timeOffset] *= pData->postProc.volume;
  1239. }
  1240. }
  1241. } // End of Post-processing
  1242. #else
  1243. if (kUse16Outs)
  1244. {
  1245. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1246. {
  1247. for (uint32_t k=0; k < frames; ++k)
  1248. outBuffer[i][k+timeOffset] = fAudio16Buffers[i][k];
  1249. }
  1250. }
  1251. #endif
  1252. // --------------------------------------------------------------------------------------------------------
  1253. pData->singleMutex.unlock();
  1254. return true;
  1255. }
  1256. void bufferSizeChanged(const uint32_t newBufferSize) override
  1257. {
  1258. if (! kUse16Outs)
  1259. return;
  1260. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1261. {
  1262. if (fAudio16Buffers[i] != nullptr)
  1263. delete[] fAudio16Buffers[i];
  1264. fAudio16Buffers[i] = new float[newBufferSize];
  1265. }
  1266. }
  1267. void sampleRateChanged(const double newSampleRate) override
  1268. {
  1269. CARLA_SAFE_ASSERT_RETURN(fSettings != nullptr,);
  1270. fluid_settings_setnum(fSettings, "synth.sample-rate", newSampleRate);
  1271. #ifdef FLUIDSYNTH_VERSION_NEW_API
  1272. CARLA_SAFE_ASSERT_RETURN(fSynth != nullptr,);
  1273. fluid_synth_set_sample_rate(fSynth, float(newSampleRate));
  1274. #endif
  1275. }
  1276. // -------------------------------------------------------------------
  1277. // Plugin buffers
  1278. void clearBuffers() noexcept override
  1279. {
  1280. carla_debug("CarlaPluginFluidSynth::clearBuffers() - start");
  1281. if (fAudio16Buffers != nullptr)
  1282. {
  1283. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1284. {
  1285. if (fAudio16Buffers[i] != nullptr)
  1286. {
  1287. delete[] fAudio16Buffers[i];
  1288. fAudio16Buffers[i] = nullptr;
  1289. }
  1290. }
  1291. delete[] fAudio16Buffers;
  1292. fAudio16Buffers = nullptr;
  1293. }
  1294. CarlaPlugin::clearBuffers();
  1295. carla_debug("CarlaPluginFluidSynth::clearBuffers() - end");
  1296. }
  1297. // -------------------------------------------------------------------
  1298. const void* getExtraStuff() const noexcept override
  1299. {
  1300. static const char xtrue[] = "true";
  1301. static const char xfalse[] = "false";
  1302. return kUse16Outs ? xtrue : xfalse;
  1303. }
  1304. // -------------------------------------------------------------------
  1305. bool init(const char* const filename, const char* const name, const char* const label, const uint options)
  1306. {
  1307. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  1308. // ---------------------------------------------------------------
  1309. // first checks
  1310. if (pData->client != nullptr)
  1311. {
  1312. pData->engine->setLastError("Plugin client is already registered");
  1313. return false;
  1314. }
  1315. if (fSynth == nullptr)
  1316. {
  1317. pData->engine->setLastError("null synth");
  1318. return false;
  1319. }
  1320. if (filename == nullptr || filename[0] == '\0')
  1321. {
  1322. pData->engine->setLastError("null filename");
  1323. return false;
  1324. }
  1325. if (label == nullptr || label[0] == '\0')
  1326. {
  1327. pData->engine->setLastError("null label");
  1328. return false;
  1329. }
  1330. // ---------------------------------------------------------------
  1331. // open soundfont
  1332. const int synthId(fluid_synth_sfload(fSynth, filename, 0));
  1333. if (synthId < 0)
  1334. {
  1335. pData->engine->setLastError("Failed to load SoundFont file");
  1336. return false;
  1337. }
  1338. fSynthId = static_cast<uint>(synthId);
  1339. // ---------------------------------------------------------------
  1340. // get info
  1341. CarlaString label2(label);
  1342. if (kUse16Outs && ! label2.endsWith(" (16 outs)"))
  1343. label2 += " (16 outs)";
  1344. fLabel = label2.dup();
  1345. pData->filename = carla_strdup(filename);
  1346. if (name != nullptr && name[0] != '\0')
  1347. pData->name = pData->engine->getUniquePluginName(name);
  1348. else
  1349. pData->name = pData->engine->getUniquePluginName(label);
  1350. // ---------------------------------------------------------------
  1351. // register client
  1352. pData->client = pData->engine->addClient(this);
  1353. if (pData->client == nullptr || ! pData->client->isOk())
  1354. {
  1355. pData->engine->setLastError("Failed to register plugin client");
  1356. return false;
  1357. }
  1358. // ---------------------------------------------------------------
  1359. // set default options
  1360. pData->options = 0x0;
  1361. pData->options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  1362. pData->options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  1363. pData->options |= PLUGIN_OPTION_SEND_PITCHBEND;
  1364. pData->options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  1365. if (options & PLUGIN_OPTION_SEND_CONTROL_CHANGES)
  1366. pData->options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  1367. return true;
  1368. }
  1369. private:
  1370. enum FluidSynthParameters {
  1371. FluidSynthReverbOnOff = 0,
  1372. FluidSynthReverbRoomSize = 1,
  1373. FluidSynthReverbDamp = 2,
  1374. FluidSynthReverbLevel = 3,
  1375. FluidSynthReverbWidth = 4,
  1376. FluidSynthChorusOnOff = 5,
  1377. FluidSynthChorusNr = 6,
  1378. FluidSynthChorusLevel = 7,
  1379. FluidSynthChorusSpeedHz = 8,
  1380. FluidSynthChorusDepthMs = 9,
  1381. FluidSynthChorusType = 10,
  1382. FluidSynthPolyphony = 11,
  1383. FluidSynthInterpolation = 12,
  1384. FluidSynthVoiceCount = 13,
  1385. FluidSynthParametersMax = 14
  1386. };
  1387. const bool kUse16Outs;
  1388. fluid_settings_t* fSettings;
  1389. fluid_synth_t* fSynth;
  1390. uint fSynthId;
  1391. float** fAudio16Buffers;
  1392. float fParamBuffers[FluidSynthParametersMax];
  1393. int32_t fCurMidiProgs[MAX_MIDI_CHANNELS];
  1394. const char* fLabel;
  1395. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaPluginFluidSynth)
  1396. };
  1397. CARLA_BACKEND_END_NAMESPACE
  1398. #endif // HAVE_FLUIDSYNTH
  1399. CARLA_BACKEND_START_NAMESPACE
  1400. // -------------------------------------------------------------------------------------------------------------------
  1401. CarlaPlugin* CarlaPlugin::newFluidSynth(const Initializer& init, const bool use16Outs)
  1402. {
  1403. carla_debug("CarlaPlugin::newFluidSynth({%p, \"%s\", \"%s\", \"%s\", " P_INT64 "}, %s)", init.engine, init.filename, init.name, init.label, init.uniqueId, bool2str(use16Outs));
  1404. #ifdef HAVE_FLUIDSYNTH
  1405. if (init.engine->getProccessMode() == ENGINE_PROCESS_MODE_CONTINUOUS_RACK && use16Outs)
  1406. {
  1407. init.engine->setLastError("Carla's rack mode can only work with Stereo modules,"
  1408. "please choose the 2-channel only SoundFont version");
  1409. return nullptr;
  1410. }
  1411. if (! fluid_is_soundfont(init.filename))
  1412. {
  1413. init.engine->setLastError("Requested file is not a valid SoundFont");
  1414. return nullptr;
  1415. }
  1416. CarlaPluginFluidSynth* const plugin(new CarlaPluginFluidSynth(init.engine, init.id, use16Outs));
  1417. if (! plugin->init(init.filename, init.name, init.label, init.options))
  1418. {
  1419. delete plugin;
  1420. return nullptr;
  1421. }
  1422. return plugin;
  1423. #else
  1424. init.engine->setLastError("fluidsynth support not available");
  1425. return nullptr;
  1426. // unused
  1427. (void)use16Outs;
  1428. #endif
  1429. }
  1430. CarlaPlugin* CarlaPlugin::newFileSF2(const Initializer& init, const bool use16Outs)
  1431. {
  1432. carla_debug("CarlaPlugin::newFileSF2({%p, \"%s\", \"%s\", \"%s\"}, %s)", init.engine, init.filename, init.name, init.label, bool2str(use16Outs));
  1433. #ifdef HAVE_FLUIDSYNTH
  1434. return newFluidSynth(init, use16Outs);
  1435. #else
  1436. init.engine->setLastError("SF2 support not available");
  1437. return nullptr;
  1438. // unused
  1439. (void)use16Outs;
  1440. #endif
  1441. }
  1442. // -------------------------------------------------------------------------------------------------------------------
  1443. CARLA_BACKEND_END_NAMESPACE