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.

CarlaPlugin.cpp 90KB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 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
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
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
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
10 years ago
10 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
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
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
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
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 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
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
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735
  1. /*
  2. * Carla Plugin
  3. * Copyright (C) 2011-2020 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. #include "CarlaBackendUtils.hpp"
  20. #include "CarlaBase64Utils.hpp"
  21. #include "CarlaMathUtils.hpp"
  22. #include "CarlaMIDI.h"
  23. #include "CarlaPluginUI.hpp"
  24. #include "CarlaScopeUtils.hpp"
  25. #include "CarlaStringList.hpp"
  26. #include <ctime>
  27. #include "water/files/File.h"
  28. #include "water/streams/MemoryOutputStream.h"
  29. #include "water/xml/XmlDocument.h"
  30. #include "water/xml/XmlElement.h"
  31. using water::CharPointer_UTF8;
  32. using water::File;
  33. using water::MemoryOutputStream;
  34. using water::Result;
  35. using water::String;
  36. using water::XmlDocument;
  37. using water::XmlElement;
  38. CARLA_BACKEND_START_NAMESPACE
  39. // -------------------------------------------------------------------------------------------------------------------
  40. // Fallback data
  41. static const ParameterData kParameterDataNull = { PARAMETER_UNKNOWN, 0x0, PARAMETER_NULL, -1, 0, CONTROL_INDEX_NONE, 0.0f, 1.0f, 0x0 };
  42. static const ParameterRanges kParameterRangesNull = { 0.0f, 0.0f, 1.0f, 0.01f, 0.0001f, 0.1f };
  43. static const MidiProgramData kMidiProgramDataNull = { 0, 0, nullptr };
  44. static const CustomData kCustomDataFallback = { nullptr, nullptr, nullptr };
  45. static /* */ CustomData kCustomDataFallbackNC = { nullptr, nullptr, nullptr };
  46. static const PluginPostRtEvent kPluginPostRtEventFallback = { kPluginPostRtEventNull, false, 0, 0, 0, 0.0f };
  47. // -------------------------------------------------------------------------------------------------------------------
  48. // ParamSymbol struct, needed for CarlaPlugin::loadStateSave()
  49. struct ParamSymbol {
  50. int32_t index;
  51. const char* symbol;
  52. ParamSymbol(const uint32_t i, const char* const s)
  53. : index(static_cast<int32_t>(i)),
  54. symbol(carla_strdup(s)) {}
  55. ~ParamSymbol() noexcept
  56. {
  57. CARLA_SAFE_ASSERT_RETURN(symbol != nullptr,)
  58. delete[] symbol;
  59. symbol = nullptr;
  60. }
  61. #ifdef CARLA_PROPER_CPP11_SUPPORT
  62. ParamSymbol() = delete;
  63. CARLA_DECLARE_NON_COPY_STRUCT(ParamSymbol)
  64. #endif
  65. };
  66. // -------------------------------------------------------------------------------------------------------------------
  67. // Constructor and destructor
  68. CarlaPlugin::CarlaPlugin(CarlaEngine* const engine, const uint id)
  69. : pData(new ProtectedData(engine, id))
  70. {
  71. CARLA_SAFE_ASSERT_RETURN(engine != nullptr,);
  72. CARLA_SAFE_ASSERT(id < engine->getMaxPluginNumber());
  73. carla_debug("CarlaPlugin::CarlaPlugin(%p, %i)", engine, id);
  74. switch (engine->getProccessMode())
  75. {
  76. case ENGINE_PROCESS_MODE_SINGLE_CLIENT:
  77. case ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS:
  78. CARLA_SAFE_ASSERT(id < MAX_DEFAULT_PLUGINS);
  79. break;
  80. case ENGINE_PROCESS_MODE_CONTINUOUS_RACK:
  81. CARLA_SAFE_ASSERT(id < MAX_RACK_PLUGINS);
  82. break;
  83. case ENGINE_PROCESS_MODE_PATCHBAY:
  84. CARLA_SAFE_ASSERT(id < MAX_PATCHBAY_PLUGINS);
  85. break;
  86. case ENGINE_PROCESS_MODE_BRIDGE:
  87. CARLA_SAFE_ASSERT(id == 0);
  88. break;
  89. }
  90. }
  91. CarlaPlugin::~CarlaPlugin()
  92. {
  93. carla_debug("CarlaPlugin::~CarlaPlugin()");
  94. delete pData;
  95. }
  96. // -------------------------------------------------------------------
  97. // Information (base)
  98. uint CarlaPlugin::getId() const noexcept
  99. {
  100. return pData->id;
  101. }
  102. uint CarlaPlugin::getHints() const noexcept
  103. {
  104. return pData->hints;
  105. }
  106. uint CarlaPlugin::getOptionsEnabled() const noexcept
  107. {
  108. return pData->options;
  109. }
  110. bool CarlaPlugin::isEnabled() const noexcept
  111. {
  112. return pData->enabled;
  113. }
  114. const char* CarlaPlugin::getName() const noexcept
  115. {
  116. return pData->name;
  117. }
  118. const char* CarlaPlugin::getFilename() const noexcept
  119. {
  120. return pData->filename;
  121. }
  122. const char* CarlaPlugin::getIconName() const noexcept
  123. {
  124. return pData->iconName;
  125. }
  126. PluginCategory CarlaPlugin::getCategory() const noexcept
  127. {
  128. return getPluginCategoryFromName(pData->name);
  129. }
  130. int64_t CarlaPlugin::getUniqueId() const noexcept
  131. {
  132. return 0;
  133. }
  134. uint32_t CarlaPlugin::getLatencyInFrames() const noexcept
  135. {
  136. return 0;
  137. }
  138. // -------------------------------------------------------------------
  139. // Information (count)
  140. uint32_t CarlaPlugin::getAudioInCount() const noexcept
  141. {
  142. return pData->audioIn.count;
  143. }
  144. uint32_t CarlaPlugin::getAudioOutCount() const noexcept
  145. {
  146. return pData->audioOut.count;
  147. }
  148. uint32_t CarlaPlugin::getCVInCount() const noexcept
  149. {
  150. return pData->cvIn.count;
  151. }
  152. uint32_t CarlaPlugin::getCVOutCount() const noexcept
  153. {
  154. return pData->cvOut.count;
  155. }
  156. uint32_t CarlaPlugin::getMidiInCount() const noexcept
  157. {
  158. return (pData->extraHints & PLUGIN_EXTRA_HINT_HAS_MIDI_IN) ? 1 : 0;
  159. }
  160. uint32_t CarlaPlugin::getMidiOutCount() const noexcept
  161. {
  162. return (pData->extraHints & PLUGIN_EXTRA_HINT_HAS_MIDI_OUT) ? 1 : 0;
  163. }
  164. uint32_t CarlaPlugin::getParameterCount() const noexcept
  165. {
  166. return pData->param.count;
  167. }
  168. uint32_t CarlaPlugin::getParameterScalePointCount(const uint32_t parameterId) const noexcept
  169. {
  170. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0);
  171. return 0;
  172. }
  173. uint32_t CarlaPlugin::getProgramCount() const noexcept
  174. {
  175. return pData->prog.count;
  176. }
  177. uint32_t CarlaPlugin::getMidiProgramCount() const noexcept
  178. {
  179. return pData->midiprog.count;
  180. }
  181. uint32_t CarlaPlugin::getCustomDataCount() const noexcept
  182. {
  183. return static_cast<uint32_t>(pData->custom.count());
  184. }
  185. // -------------------------------------------------------------------
  186. // Information (current data)
  187. int32_t CarlaPlugin::getCurrentProgram() const noexcept
  188. {
  189. return pData->prog.current;
  190. }
  191. int32_t CarlaPlugin::getCurrentMidiProgram() const noexcept
  192. {
  193. return pData->midiprog.current;
  194. }
  195. const ParameterData& CarlaPlugin::getParameterData(const uint32_t parameterId) const noexcept
  196. {
  197. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, kParameterDataNull);
  198. return pData->param.data[parameterId];
  199. }
  200. const ParameterRanges& CarlaPlugin::getParameterRanges(const uint32_t parameterId) const noexcept
  201. {
  202. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, kParameterRangesNull);
  203. return pData->param.ranges[parameterId];
  204. }
  205. bool CarlaPlugin::isParameterOutput(const uint32_t parameterId) const noexcept
  206. {
  207. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, false);
  208. return (pData->param.data[parameterId].type == PARAMETER_OUTPUT);
  209. }
  210. const MidiProgramData& CarlaPlugin::getMidiProgramData(const uint32_t index) const noexcept
  211. {
  212. CARLA_SAFE_ASSERT_RETURN(index < pData->midiprog.count, kMidiProgramDataNull);
  213. return pData->midiprog.data[index];
  214. }
  215. const CustomData& CarlaPlugin::getCustomData(const uint32_t index) const noexcept
  216. {
  217. return pData->custom.getAt(index, kCustomDataFallback);
  218. }
  219. std::size_t CarlaPlugin::getChunkData(void** const dataPtr) noexcept
  220. {
  221. CARLA_SAFE_ASSERT_RETURN(dataPtr != nullptr, 0);
  222. CARLA_SAFE_ASSERT(false); // this should never happen
  223. return 0;
  224. }
  225. // -------------------------------------------------------------------
  226. // Information (per-plugin data)
  227. uint CarlaPlugin::getOptionsAvailable() const noexcept
  228. {
  229. CARLA_SAFE_ASSERT(false); // this should never happen
  230. return 0x0;
  231. }
  232. float CarlaPlugin::getParameterValue(const uint32_t parameterId) const noexcept
  233. {
  234. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(), 0.0f);
  235. CARLA_SAFE_ASSERT(false); // this should never happen
  236. return 0.0f;
  237. }
  238. float CarlaPlugin::getParameterScalePointValue(const uint32_t parameterId, const uint32_t scalePointId) const noexcept
  239. {
  240. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(), 0.0f);
  241. CARLA_SAFE_ASSERT_RETURN(scalePointId < getParameterScalePointCount(parameterId), 0.0f);
  242. CARLA_SAFE_ASSERT(false); // this should never happen
  243. return 0.0f;
  244. }
  245. bool CarlaPlugin::getLabel(char* const strBuf) const noexcept
  246. {
  247. strBuf[0] = '\0';
  248. return false;
  249. }
  250. bool CarlaPlugin::getMaker(char* const strBuf) const noexcept
  251. {
  252. strBuf[0] = '\0';
  253. return false;
  254. }
  255. bool CarlaPlugin::getCopyright(char* const strBuf) const noexcept
  256. {
  257. strBuf[0] = '\0';
  258. return false;
  259. }
  260. bool CarlaPlugin::getRealName(char* const strBuf) const noexcept
  261. {
  262. strBuf[0] = '\0';
  263. return false;
  264. }
  265. bool CarlaPlugin::getParameterName(const uint32_t parameterId, char* const strBuf) const noexcept
  266. {
  267. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(), false);
  268. CARLA_SAFE_ASSERT(false); // this should never happen
  269. strBuf[0] = '\0';
  270. return false;
  271. }
  272. bool CarlaPlugin::getParameterSymbol(const uint32_t parameterId, char* const strBuf) const noexcept
  273. {
  274. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(), false);
  275. strBuf[0] = '\0';
  276. return false;
  277. }
  278. bool CarlaPlugin::getParameterText(const uint32_t parameterId, char* const strBuf) noexcept
  279. {
  280. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(), false);
  281. CARLA_SAFE_ASSERT(false); // this should never happen
  282. strBuf[0] = '\0';
  283. return false;
  284. }
  285. bool CarlaPlugin::getParameterUnit(const uint32_t parameterId, char* const strBuf) const noexcept
  286. {
  287. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(), false);
  288. strBuf[0] = '\0';
  289. return false;
  290. }
  291. bool CarlaPlugin::getParameterComment(const uint32_t parameterId, char* const strBuf) const noexcept
  292. {
  293. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(), false);
  294. strBuf[0] = '\0';
  295. return false;
  296. }
  297. bool CarlaPlugin::getParameterGroupName(const uint32_t parameterId, char* const strBuf) const noexcept
  298. {
  299. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(), false);
  300. strBuf[0] = '\0';
  301. return false;
  302. }
  303. bool CarlaPlugin::getParameterScalePointLabel(const uint32_t parameterId, const uint32_t scalePointId, char* const strBuf) const noexcept
  304. {
  305. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(), false);
  306. CARLA_SAFE_ASSERT_RETURN(scalePointId < getParameterScalePointCount(parameterId), false);
  307. CARLA_SAFE_ASSERT(false); // this should never happen
  308. strBuf[0] = '\0';
  309. return false;
  310. }
  311. float CarlaPlugin::getInternalParameterValue(const int32_t parameterId) const noexcept
  312. {
  313. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  314. CARLA_SAFE_ASSERT_RETURN(parameterId != PARAMETER_NULL && parameterId > PARAMETER_MAX, 0.0f);
  315. switch (parameterId)
  316. {
  317. case PARAMETER_ACTIVE:
  318. return pData->active;
  319. case PARAMETER_CTRL_CHANNEL:
  320. return pData->ctrlChannel;
  321. case PARAMETER_DRYWET:
  322. return pData->postProc.dryWet;
  323. case PARAMETER_VOLUME:
  324. return pData->postProc.volume;
  325. case PARAMETER_BALANCE_LEFT:
  326. return pData->postProc.balanceLeft;
  327. case PARAMETER_BALANCE_RIGHT:
  328. return pData->postProc.balanceRight;
  329. case PARAMETER_PANNING:
  330. return pData->postProc.panning;
  331. };
  332. #endif
  333. CARLA_SAFE_ASSERT_RETURN(parameterId >= 0, 0.0f);
  334. return getParameterValue(static_cast<uint32_t>(parameterId));
  335. }
  336. bool CarlaPlugin::getProgramName(const uint32_t index, char* const strBuf) const noexcept
  337. {
  338. CARLA_SAFE_ASSERT_RETURN(index < pData->prog.count, false);
  339. CARLA_SAFE_ASSERT_RETURN(pData->prog.names[index] != nullptr, false);
  340. std::strncpy(strBuf, pData->prog.names[index], STR_MAX);
  341. return true;
  342. }
  343. bool CarlaPlugin::getMidiProgramName(const uint32_t index, char* const strBuf) const noexcept
  344. {
  345. CARLA_SAFE_ASSERT_RETURN(index < pData->midiprog.count, false);
  346. CARLA_SAFE_ASSERT_RETURN(pData->midiprog.data[index].name != nullptr, false);
  347. std::strncpy(strBuf, pData->midiprog.data[index].name, STR_MAX);
  348. return true;
  349. }
  350. void CarlaPlugin::getParameterCountInfo(uint32_t& ins, uint32_t& outs) const noexcept
  351. {
  352. ins = 0;
  353. outs = 0;
  354. for (uint32_t i=0; i < pData->param.count; ++i)
  355. {
  356. if (pData->param.data[i].type == PARAMETER_INPUT)
  357. ++ins;
  358. else if (pData->param.data[i].type == PARAMETER_OUTPUT)
  359. ++outs;
  360. }
  361. }
  362. // -------------------------------------------------------------------
  363. // Set data (state)
  364. void CarlaPlugin::prepareForSave(bool)
  365. {
  366. }
  367. void CarlaPlugin::resetParameters() noexcept
  368. {
  369. for (uint i=0; i < pData->param.count; ++i)
  370. {
  371. const ParameterData& paramData(pData->param.data[i]);
  372. const ParameterRanges& paramRanges(pData->param.ranges[i]);
  373. if (paramData.type != PARAMETER_INPUT)
  374. continue;
  375. if ((paramData.hints & PARAMETER_IS_ENABLED) == 0)
  376. continue;
  377. setParameterValue(i, paramRanges.def, true, true, true);
  378. }
  379. }
  380. void CarlaPlugin::randomizeParameters() noexcept
  381. {
  382. float value, random;
  383. char strBuf[STR_MAX+1];
  384. strBuf[STR_MAX] = '\0';
  385. std::srand(static_cast<uint>(std::time(nullptr)));
  386. for (uint i=0; i < pData->param.count; ++i)
  387. {
  388. const ParameterData& paramData(pData->param.data[i]);
  389. if (paramData.type != PARAMETER_INPUT)
  390. continue;
  391. if ((paramData.hints & PARAMETER_IS_ENABLED) == 0)
  392. continue;
  393. if (! getParameterName(i, strBuf))
  394. strBuf[0] = '\0';
  395. if (std::strstr(strBuf, "olume") != nullptr)
  396. continue;
  397. if (std::strstr(strBuf, "Master") != nullptr)
  398. continue;
  399. const ParameterRanges& paramRanges(pData->param.ranges[i]);
  400. if (paramData.hints & PARAMETER_IS_BOOLEAN)
  401. {
  402. random = static_cast<float>(std::rand()) / static_cast<float>(RAND_MAX);
  403. value = random > 0.5f ? paramRanges.max : paramRanges.min;
  404. }
  405. else
  406. {
  407. random = static_cast<float>(std::rand()) / static_cast<float>(RAND_MAX);
  408. value = random * (paramRanges.max - paramRanges.min) + paramRanges.min;
  409. if (paramData.hints & PARAMETER_IS_INTEGER)
  410. value = std::rint(value);
  411. }
  412. setParameterValue(i, value, true, true, true);
  413. }
  414. }
  415. const CarlaStateSave& CarlaPlugin::getStateSave(const bool callPrepareForSave)
  416. {
  417. pData->stateSave.clear();
  418. if (callPrepareForSave)
  419. {
  420. pData->stateSave.temporary = true;
  421. prepareForSave(true);
  422. }
  423. const PluginType pluginType(getType());
  424. char strBuf[STR_MAX+1];
  425. carla_zeroChars(strBuf, STR_MAX+1);
  426. // ---------------------------------------------------------------
  427. // Basic info
  428. if (! getLabel(strBuf))
  429. strBuf[0] = '\0';
  430. pData->stateSave.type = carla_strdup(getPluginTypeAsString(pluginType));
  431. pData->stateSave.name = carla_strdup(pData->name);
  432. pData->stateSave.label = carla_strdup(strBuf);
  433. pData->stateSave.uniqueId = getUniqueId();
  434. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  435. pData->stateSave.options = pData->options;
  436. #endif
  437. if (pData->filename != nullptr)
  438. pData->stateSave.binary = carla_strdup(pData->filename);
  439. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  440. // ---------------------------------------------------------------
  441. // Internals
  442. pData->stateSave.active = pData->active;
  443. pData->stateSave.dryWet = pData->postProc.dryWet;
  444. pData->stateSave.volume = pData->postProc.volume;
  445. pData->stateSave.balanceLeft = pData->postProc.balanceLeft;
  446. pData->stateSave.balanceRight = pData->postProc.balanceRight;
  447. pData->stateSave.panning = pData->postProc.panning;
  448. pData->stateSave.ctrlChannel = pData->ctrlChannel;
  449. #endif
  450. if (pData->hints & PLUGIN_IS_BRIDGE)
  451. waitForBridgeSaveSignal();
  452. // ---------------------------------------------------------------
  453. // Chunk
  454. bool usingChunk = false;
  455. if (pData->options & PLUGIN_OPTION_USE_CHUNKS)
  456. {
  457. void* data = nullptr;
  458. const std::size_t dataSize(getChunkData(&data));
  459. if (data != nullptr && dataSize > 0)
  460. {
  461. pData->stateSave.chunk = CarlaString::asBase64(data, dataSize).dup();
  462. if (pluginType != PLUGIN_INTERNAL)
  463. usingChunk = true;
  464. }
  465. }
  466. // ---------------------------------------------------------------
  467. // Current Program
  468. if (pData->prog.current >= 0 && pluginType != PLUGIN_LV2)
  469. {
  470. pData->stateSave.currentProgramIndex = pData->prog.current;
  471. pData->stateSave.currentProgramName = carla_strdup(pData->prog.names[pData->prog.current]);
  472. }
  473. // ---------------------------------------------------------------
  474. // Current MIDI Program
  475. if (pData->midiprog.current >= 0 && pluginType != PLUGIN_LV2 && pluginType != PLUGIN_SF2)
  476. {
  477. const MidiProgramData& mpData(pData->midiprog.getCurrent());
  478. pData->stateSave.currentMidiBank = static_cast<int32_t>(mpData.bank);
  479. pData->stateSave.currentMidiProgram = static_cast<int32_t>(mpData.program);
  480. }
  481. // ---------------------------------------------------------------
  482. // Parameters
  483. const float sampleRate(static_cast<float>(pData->engine->getSampleRate()));
  484. for (uint32_t i=0; i < pData->param.count; ++i)
  485. {
  486. const ParameterData& paramData(pData->param.data[i]);
  487. if ((paramData.hints & PARAMETER_IS_ENABLED) == 0)
  488. continue;
  489. const bool dummy = paramData.type != PARAMETER_INPUT || usingChunk;
  490. if (dummy && paramData.mappedControlIndex <= CONTROL_INDEX_NONE)
  491. continue;
  492. CarlaStateSave::Parameter* const stateParameter(new CarlaStateSave::Parameter());
  493. stateParameter->dummy = dummy;
  494. stateParameter->index = paramData.index;
  495. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  496. if (paramData.mappedControlIndex != CONTROL_INDEX_MIDI_LEARN)
  497. {
  498. stateParameter->mappedControlIndex = paramData.mappedControlIndex;
  499. stateParameter->midiChannel = paramData.midiChannel;
  500. if (paramData.hints & PARAMETER_MAPPED_RANGES_SET)
  501. {
  502. stateParameter->mappedMinimum = paramData.mappedMinimum;
  503. stateParameter->mappedMaximum = paramData.mappedMaximum;
  504. stateParameter->mappedRangeValid = true;
  505. if (paramData.hints & PARAMETER_USES_SAMPLERATE)
  506. {
  507. stateParameter->mappedMinimum /= sampleRate;
  508. stateParameter->mappedMaximum /= sampleRate;
  509. }
  510. }
  511. }
  512. #endif
  513. if (! getParameterName(i, strBuf))
  514. strBuf[0] = '\0';
  515. stateParameter->name = carla_strdup(strBuf);
  516. if (! getParameterSymbol(i, strBuf))
  517. strBuf[0] = '\0';
  518. stateParameter->symbol = carla_strdup(strBuf);;
  519. if (! dummy)
  520. {
  521. stateParameter->value = getParameterValue(i);
  522. if (paramData.hints & PARAMETER_USES_SAMPLERATE)
  523. stateParameter->value /= sampleRate;
  524. }
  525. pData->stateSave.parameters.append(stateParameter);
  526. }
  527. // ---------------------------------------------------------------
  528. // Custom Data
  529. for (LinkedList<CustomData>::Itenerator it = pData->custom.begin2(); it.valid(); it.next())
  530. {
  531. const CustomData& cData(it.getValue(kCustomDataFallback));
  532. CARLA_SAFE_ASSERT_CONTINUE(cData.isValid());
  533. CarlaStateSave::CustomData* stateCustomData(new CarlaStateSave::CustomData());
  534. stateCustomData->type = carla_strdup(cData.type);
  535. stateCustomData->key = carla_strdup(cData.key);
  536. stateCustomData->value = carla_strdup(cData.value);
  537. pData->stateSave.customData.append(stateCustomData);
  538. }
  539. return pData->stateSave;
  540. }
  541. void CarlaPlugin::loadStateSave(const CarlaStateSave& stateSave)
  542. {
  543. const bool usesMultiProgs(pData->hints & PLUGIN_USES_MULTI_PROGS);
  544. const PluginType pluginType(getType());
  545. char strBuf[STR_MAX+1];
  546. carla_zeroChars(strBuf, STR_MAX+1);
  547. // ---------------------------------------------------------------
  548. // Part 1 - PRE-set custom data (only those which reload programs)
  549. for (CarlaStateSave::CustomDataItenerator it = stateSave.customData.begin2(); it.valid(); it.next())
  550. {
  551. const CarlaStateSave::CustomData* const stateCustomData(it.getValue(nullptr));
  552. CARLA_SAFE_ASSERT_CONTINUE(stateCustomData != nullptr);
  553. CARLA_SAFE_ASSERT_CONTINUE(stateCustomData->isValid());
  554. const char* const key(stateCustomData->key);
  555. /**/ if (pluginType == PLUGIN_DSSI && (std::strcmp (key, "reloadprograms") == 0 ||
  556. std::strcmp (key, "load" ) == 0 ||
  557. std::strncmp(key, "patches", 7) == 0 ))
  558. pass();
  559. else if (usesMultiProgs && std::strcmp(key, "midiPrograms") == 0)
  560. pass();
  561. else
  562. continue;
  563. setCustomData(stateCustomData->type, key, stateCustomData->value, true);
  564. }
  565. // ---------------------------------------------------------------
  566. // Part 2 - set program
  567. if (stateSave.currentProgramIndex >= 0 && stateSave.currentProgramName != nullptr)
  568. {
  569. int32_t programId = -1;
  570. // index < count
  571. if (stateSave.currentProgramIndex < static_cast<int32_t>(pData->prog.count))
  572. {
  573. programId = stateSave.currentProgramIndex;
  574. }
  575. // index not valid, try to find by name
  576. else
  577. {
  578. for (uint32_t i=0; i < pData->prog.count; ++i)
  579. {
  580. if (getProgramName(i, strBuf) && std::strcmp(stateSave.currentProgramName, strBuf) == 0)
  581. {
  582. programId = static_cast<int32_t>(i);
  583. break;
  584. }
  585. }
  586. }
  587. // set program now, if valid
  588. if (programId >= 0)
  589. setProgram(programId, true, true, true);
  590. }
  591. // ---------------------------------------------------------------
  592. // Part 3 - set midi program
  593. if (stateSave.currentMidiBank >= 0 && stateSave.currentMidiProgram >= 0 && ! usesMultiProgs)
  594. setMidiProgramById(static_cast<uint32_t>(stateSave.currentMidiBank), static_cast<uint32_t>(stateSave.currentMidiProgram), true, true, true);
  595. // ---------------------------------------------------------------
  596. // Part 4a - get plugin parameter symbols
  597. LinkedList<ParamSymbol*> paramSymbols;
  598. if (pluginType == PLUGIN_LADSPA || pluginType == PLUGIN_LV2)
  599. {
  600. for (uint32_t i=0; i < pData->param.count; ++i)
  601. {
  602. if (getParameterSymbol(i, strBuf))
  603. {
  604. ParamSymbol* const paramSymbol(new ParamSymbol(i, strBuf));
  605. paramSymbols.append(paramSymbol);
  606. }
  607. }
  608. }
  609. // ---------------------------------------------------------------
  610. // Part 4b - set parameter values (carefully)
  611. const float sampleRate(static_cast<float>(pData->engine->getSampleRate()));
  612. for (CarlaStateSave::ParameterItenerator it = stateSave.parameters.begin2(); it.valid(); it.next())
  613. {
  614. CarlaStateSave::Parameter* const stateParameter(it.getValue(nullptr));
  615. CARLA_SAFE_ASSERT_CONTINUE(stateParameter != nullptr);
  616. int32_t index = -1;
  617. if (pluginType == PLUGIN_LADSPA)
  618. {
  619. // Try to set by symbol, otherwise use index
  620. if (stateParameter->symbol != nullptr && stateParameter->symbol[0] != '\0')
  621. {
  622. for (LinkedList<ParamSymbol*>::Itenerator it2 = paramSymbols.begin2(); it2.valid(); it2.next())
  623. {
  624. ParamSymbol* const paramSymbol(it2.getValue(nullptr));
  625. CARLA_SAFE_ASSERT_CONTINUE(paramSymbol != nullptr);
  626. CARLA_SAFE_ASSERT_CONTINUE(paramSymbol->symbol != nullptr);
  627. if (std::strcmp(stateParameter->symbol, paramSymbol->symbol) == 0)
  628. {
  629. index = paramSymbol->index;
  630. break;
  631. }
  632. }
  633. if (index == -1)
  634. index = stateParameter->index;
  635. }
  636. else
  637. {
  638. index = stateParameter->index;
  639. }
  640. }
  641. else if (pluginType == PLUGIN_LV2)
  642. {
  643. // Symbol only
  644. if (stateParameter->symbol != nullptr && stateParameter->symbol[0] != '\0')
  645. {
  646. for (LinkedList<ParamSymbol*>::Itenerator it2 = paramSymbols.begin2(); it2.valid(); it2.next())
  647. {
  648. ParamSymbol* const paramSymbol(it2.getValue(nullptr));
  649. CARLA_SAFE_ASSERT_CONTINUE(paramSymbol != nullptr);
  650. CARLA_SAFE_ASSERT_CONTINUE(paramSymbol->symbol != nullptr);
  651. if (std::strcmp(stateParameter->symbol, paramSymbol->symbol) == 0)
  652. {
  653. index = paramSymbol->index;
  654. break;
  655. }
  656. }
  657. if (index == -1)
  658. carla_stderr("Failed to find LV2 parameter symbol '%s' for '%s'",
  659. stateParameter->symbol, pData->name);
  660. }
  661. else
  662. {
  663. carla_stderr("LV2 Plugin parameter '%s' has no symbol", stateParameter->name);
  664. }
  665. }
  666. else
  667. {
  668. // Index only
  669. index = stateParameter->index;
  670. }
  671. // Now set parameter
  672. if (index >= 0 && index < static_cast<int32_t>(pData->param.count))
  673. {
  674. //CARLA_SAFE_ASSERT(stateParameter->isInput == (pData
  675. if (! stateParameter->dummy)
  676. {
  677. if (pData->param.data[index].hints & PARAMETER_USES_SAMPLERATE)
  678. stateParameter->value *= sampleRate;
  679. setParameterValue(static_cast<uint32_t>(index), stateParameter->value, true, true, true);
  680. }
  681. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  682. if (stateParameter->mappedRangeValid)
  683. {
  684. if (pData->param.data[index].hints & PARAMETER_USES_SAMPLERATE)
  685. {
  686. stateParameter->mappedMinimum *= sampleRate;
  687. stateParameter->mappedMaximum *= sampleRate;
  688. }
  689. setParameterMappedRange(static_cast<uint32_t>(index),
  690. stateParameter->mappedMinimum,
  691. stateParameter->mappedMaximum, true, true);
  692. }
  693. setParameterMappedControlIndex(static_cast<uint32_t>(index),
  694. stateParameter->mappedControlIndex, true, true, false);
  695. setParameterMidiChannel(static_cast<uint32_t>(index), stateParameter->midiChannel, true, true);
  696. #endif
  697. }
  698. else
  699. carla_stderr("Could not set parameter '%s' value for '%s'",
  700. stateParameter->name, pData->name);
  701. }
  702. // ---------------------------------------------------------------
  703. // Part 4c - clear
  704. for (LinkedList<ParamSymbol*>::Itenerator it = paramSymbols.begin2(); it.valid(); it.next())
  705. {
  706. ParamSymbol* const paramSymbol(it.getValue(nullptr));
  707. delete paramSymbol;
  708. }
  709. paramSymbols.clear();
  710. // ---------------------------------------------------------------
  711. // Part 5 - set custom data
  712. for (CarlaStateSave::CustomDataItenerator it = stateSave.customData.begin2(); it.valid(); it.next())
  713. {
  714. const CarlaStateSave::CustomData* const stateCustomData(it.getValue(nullptr));
  715. CARLA_SAFE_ASSERT_CONTINUE(stateCustomData != nullptr);
  716. CARLA_SAFE_ASSERT_CONTINUE(stateCustomData->isValid());
  717. const char* const key(stateCustomData->key);
  718. if (pluginType == PLUGIN_DSSI && (std::strcmp (key, "reloadprograms") == 0 ||
  719. std::strcmp (key, "load" ) == 0 ||
  720. std::strncmp(key, "patches", 7) == 0 ))
  721. continue;
  722. if (usesMultiProgs && std::strcmp(key, "midiPrograms") == 0)
  723. continue;
  724. setCustomData(stateCustomData->type, key, stateCustomData->value, true);
  725. }
  726. // ---------------------------------------------------------------
  727. // Part 5x - set lv2 state
  728. if (pluginType == PLUGIN_LV2)
  729. {
  730. for (LinkedList<CustomData>::Itenerator it = pData->custom.begin2(); it.valid(); it.next())
  731. {
  732. const CustomData& customData(it.getValue(kCustomDataFallback));
  733. CARLA_SAFE_ASSERT_CONTINUE(customData.isValid());
  734. if (std::strcmp(customData.type, CUSTOM_DATA_TYPE_PROPERTY) == 0)
  735. continue;
  736. restoreLV2State(stateSave.temporary);
  737. break;
  738. }
  739. }
  740. // ---------------------------------------------------------------
  741. // Part 6 - set chunk
  742. if (stateSave.chunk != nullptr && (pData->options & PLUGIN_OPTION_USE_CHUNKS) != 0)
  743. {
  744. std::vector<uint8_t> chunk(carla_getChunkFromBase64String(stateSave.chunk));
  745. #ifdef CARLA_PROPER_CPP11_SUPPORT
  746. setChunkData(chunk.data(), chunk.size());
  747. #else
  748. setChunkData(&chunk.front(), chunk.size());
  749. #endif
  750. }
  751. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  752. // ---------------------------------------------------------------
  753. // Part 6 - set internal stuff
  754. const uint availOptions(getOptionsAvailable());
  755. for (uint i=0; i<10; ++i) // FIXME - get this value somehow...
  756. {
  757. const uint option(1u << i);
  758. if (availOptions & option)
  759. setOption(option, (stateSave.options & option) != 0, true);
  760. }
  761. setDryWet(stateSave.dryWet, true, true);
  762. setVolume(stateSave.volume, true, true);
  763. setBalanceLeft(stateSave.balanceLeft, true, true);
  764. setBalanceRight(stateSave.balanceRight, true, true);
  765. setPanning(stateSave.panning, true, true);
  766. setCtrlChannel(stateSave.ctrlChannel, true, true);
  767. setActive(stateSave.active, true, true);
  768. if (! pData->engine->isLoadingProject())
  769. pData->engine->callback(true, true, ENGINE_CALLBACK_UPDATE, pData->id, 0, 0, 0, 0.0f, nullptr);
  770. #endif
  771. }
  772. bool CarlaPlugin::saveStateToFile(const char* const filename)
  773. {
  774. CARLA_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', false);
  775. carla_debug("CarlaPlugin::saveStateToFile(\"%s\")", filename);
  776. MemoryOutputStream out, streamState;
  777. getStateSave().dumpToMemoryStream(streamState);
  778. out << "<?xml version='1.0' encoding='UTF-8'?>\n";
  779. out << "<!DOCTYPE CARLA-PRESET>\n";
  780. out << "<CARLA-PRESET VERSION='2.0'>\n";
  781. out << streamState;
  782. out << "</CARLA-PRESET>\n";
  783. const String jfilename = String(CharPointer_UTF8(filename));
  784. File file(jfilename);
  785. if (file.replaceWithData(out.getData(), out.getDataSize()))
  786. return true;
  787. pData->engine->setLastError("Failed to write file");
  788. return false;
  789. }
  790. bool CarlaPlugin::loadStateFromFile(const char* const filename)
  791. {
  792. // TODO set errors
  793. CARLA_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', false);
  794. carla_debug("CarlaPlugin::loadStateFromFile(\"%s\")", filename);
  795. const String jfilename = String(CharPointer_UTF8(filename));
  796. File file(jfilename);
  797. CARLA_SAFE_ASSERT_RETURN(file.existsAsFile(), false);
  798. XmlDocument xml(file);
  799. CarlaScopedPointer<XmlElement> xmlElement(xml.getDocumentElement(true));
  800. CARLA_SAFE_ASSERT_RETURN(xmlElement != nullptr, false);
  801. CARLA_SAFE_ASSERT_RETURN(xmlElement->getTagName().equalsIgnoreCase("carla-preset"), false);
  802. // completely load file
  803. xmlElement = xml.getDocumentElement(false);
  804. CARLA_SAFE_ASSERT_RETURN(xmlElement != nullptr, false);
  805. if (pData->stateSave.fillFromXmlElement(xmlElement))
  806. {
  807. loadStateSave(pData->stateSave);
  808. return true;
  809. }
  810. return false;
  811. }
  812. bool CarlaPlugin::exportAsLV2(const char* const lv2path)
  813. {
  814. CARLA_SAFE_ASSERT_RETURN(lv2path != nullptr && lv2path[0] != '\0', false);
  815. carla_debug("CarlaPlugin::exportAsLV2(\"%s\")", lv2path);
  816. CarlaString bundlepath(lv2path);
  817. if (! bundlepath.endsWith(".lv2"))
  818. bundlepath += ".lv2";
  819. const File bundlefolder(bundlepath.buffer());
  820. if (bundlefolder.existsAsFile())
  821. {
  822. pData->engine->setLastError("Requested filename already exists as file, use a folder instead");
  823. return false;
  824. }
  825. if (! bundlefolder.exists())
  826. {
  827. const Result res(bundlefolder.createDirectory());
  828. if (res.failed())
  829. {
  830. pData->engine->setLastError(res.getErrorMessage().toRawUTF8());
  831. return false;
  832. }
  833. }
  834. CarlaString symbol(pData->name);
  835. symbol.toBasic();
  836. {
  837. const CarlaString pluginFilename(bundlepath + CARLA_OS_SEP_STR + symbol + ".xml");
  838. if (! saveStateToFile(pluginFilename))
  839. return false;
  840. }
  841. {
  842. MemoryOutputStream manifestStream;
  843. manifestStream << "@prefix lv2: <http://lv2plug.in/ns/lv2core#> .\n";
  844. manifestStream << "@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n";
  845. manifestStream << "@prefix ui: <http://lv2plug.in/ns/extensions/ui#> .\n";
  846. manifestStream << "\n";
  847. manifestStream << "<" << symbol.buffer() << ".ttl>\n";
  848. manifestStream << " a lv2:Plugin ;\n";
  849. manifestStream << " lv2:binary <" << symbol.buffer() << CARLA_LIB_EXT "> ;\n";
  850. manifestStream << " rdfs:seeAlso <" << symbol.buffer() << ".ttl> .\n";
  851. manifestStream << "\n";
  852. manifestStream << "<ext-ui>\n";
  853. manifestStream << " a <http://kxstudio.sf.net/ns/lv2ext/external-ui#Widget> ;\n";
  854. manifestStream << " ui:binary <" << symbol.buffer() << CARLA_LIB_EXT "> ;\n";
  855. manifestStream << " lv2:extensionData <http://lv2plug.in/ns/extensions/ui#idleInterface> ,\n";
  856. manifestStream << " <http://lv2plug.in/ns/extensions/ui#showInterface> ;\n";
  857. manifestStream << " lv2:requiredFeature <http://lv2plug.in/ns/ext/instance-access> .\n";
  858. manifestStream << "\n";
  859. const CarlaString manifestFilename(bundlepath + CARLA_OS_SEP_STR "manifest.ttl");
  860. const File manifestFile(manifestFilename.buffer());
  861. if (! manifestFile.replaceWithData(manifestStream.getData(), manifestStream.getDataSize()))
  862. {
  863. pData->engine->setLastError("Failed to write manifest.ttl file");
  864. return false;
  865. }
  866. }
  867. {
  868. MemoryOutputStream mainStream;
  869. mainStream << "@prefix atom: <http://lv2plug.in/ns/ext/atom#> .\n";
  870. mainStream << "@prefix doap: <http://usefulinc.com/ns/doap#> .\n";
  871. mainStream << "@prefix foaf: <http://xmlns.com/foaf/0.1/> .\n";
  872. mainStream << "@prefix lv2: <http://lv2plug.in/ns/lv2core#> .\n";
  873. mainStream << "@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n";
  874. mainStream << "@prefix ui: <http://lv2plug.in/ns/extensions/ui#> .\n";
  875. mainStream << "\n";
  876. mainStream << "<>\n";
  877. mainStream << " a lv2:Plugin ;\n";
  878. mainStream << "\n";
  879. mainStream << " lv2:requiredFeature <http://lv2plug.in/ns/ext/buf-size#boundedBlockLength> ,\n";
  880. mainStream << " <http://lv2plug.in/ns/ext/options#options> ,\n";
  881. mainStream << " <http://lv2plug.in/ns/ext/urid#map> ;\n";
  882. mainStream << "\n";
  883. if (pData->hints & PLUGIN_HAS_CUSTOM_UI)
  884. {
  885. mainStream << " ui:ui <ext-ui> ;\n";
  886. mainStream << "\n";
  887. }
  888. const uint32_t midiIns = getMidiInCount();
  889. const uint32_t midiOuts = getMidiOutCount();
  890. int portIndex = 0;
  891. if (midiIns > 0)
  892. {
  893. mainStream << " lv2:port [\n";
  894. mainStream << " a lv2:InputPort, atom:AtomPort ;\n";
  895. mainStream << " lv2:index 0 ;\n";
  896. mainStream << " lv2:symbol \"clv2_events_in\" ;\n";
  897. mainStream << " lv2:name \"Events Input\" ;\n";
  898. mainStream << " atom:bufferType atom:Sequence ;\n";
  899. mainStream << " atom:supports <http://lv2plug.in/ns/ext/midi#MidiEvent> ,\n";
  900. mainStream << " <http://lv2plug.in/ns/ext/time#Position> ;\n";
  901. mainStream << " ] ;\n";
  902. ++portIndex;
  903. for (uint32_t i=1; i<midiIns; ++i)
  904. {
  905. const String portIndexNum(portIndex++);
  906. const String portIndexLabel(portIndex);
  907. mainStream << " lv2:port [\n";
  908. mainStream << " a lv2:InputPort, atom:AtomPort ;\n";
  909. mainStream << " lv2:index " << portIndexNum << " ;\n";
  910. mainStream << " lv2:symbol \"clv2_midi_in_" << portIndexLabel << "\" ;\n";
  911. mainStream << " lv2:name \"MIDI Input " << portIndexLabel << "\" ;\n";
  912. mainStream << " ] ;\n";
  913. }
  914. }
  915. else
  916. {
  917. mainStream << " lv2:port [\n";
  918. mainStream << " a lv2:InputPort, atom:AtomPort ;\n";
  919. mainStream << " lv2:index 0 ;\n";
  920. mainStream << " lv2:symbol \"clv2_time_info\" ;\n";
  921. mainStream << " lv2:name \"Time Info\" ;\n";
  922. mainStream << " atom:bufferType atom:Sequence ;\n";
  923. mainStream << " atom:supports <http://lv2plug.in/ns/ext/time#Position> ;\n";
  924. mainStream << " ] ;\n";
  925. ++portIndex;
  926. }
  927. for (uint32_t i=0; i<midiOuts; ++i)
  928. {
  929. const String portIndexNum(portIndex++);
  930. const String portIndexLabel(portIndex);
  931. mainStream << " lv2:port [\n";
  932. mainStream << " a lv2:InputPort, atom:AtomPort ;\n";
  933. mainStream << " lv2:index " << portIndexNum << " ;\n";
  934. mainStream << " lv2:symbol \"clv2_midi_out_" << portIndexLabel << "\" ;\n";
  935. mainStream << " lv2:name \"MIDI Output " << portIndexLabel << "\" ;\n";
  936. mainStream << " atom:bufferType atom:Sequence ;\n";
  937. mainStream << " atom:supports <http://lv2plug.in/ns/ext/midi#MidiEvent> ;\n";
  938. mainStream << " ] ;\n";
  939. }
  940. mainStream << " lv2:port [\n";
  941. mainStream << " a lv2:InputPort, lv2:ControlPort ;\n";
  942. mainStream << " lv2:index " << String(portIndex++) << " ;\n";
  943. mainStream << " lv2:name \"freewheel\" ;\n";
  944. mainStream << " lv2:symbol \"clv2_freewheel\" ;\n";
  945. mainStream << " lv2:default 0 ;\n";
  946. mainStream << " lv2:minimum 0 ;\n";
  947. mainStream << " lv2:maximum 1 ;\n";
  948. mainStream << " lv2:designation lv2:freeWheeling ;\n";
  949. mainStream << " lv2:portProperty lv2:toggled , lv2:integer ;\n";
  950. mainStream << " lv2:portProperty <http://lv2plug.in/ns/ext/port-props#notOnGUI> ;\n";
  951. mainStream << " ] ;\n";
  952. for (uint32_t i=0; i<pData->audioIn.count; ++i)
  953. {
  954. const String portIndexNum(portIndex++);
  955. const String portIndexLabel(i+1);
  956. mainStream << " lv2:port [\n";
  957. mainStream << " a lv2:InputPort, lv2:AudioPort ;\n";
  958. mainStream << " lv2:index " << portIndexNum << " ;\n";
  959. mainStream << " lv2:symbol \"clv2_audio_in_" << portIndexLabel << "\" ;\n";
  960. mainStream << " lv2:name \"Audio Input " << portIndexLabel << "\" ;\n";
  961. mainStream << " ] ;\n";
  962. }
  963. for (uint32_t i=0; i<pData->audioOut.count; ++i)
  964. {
  965. const String portIndexNum(portIndex++);
  966. const String portIndexLabel(i+1);
  967. mainStream << " lv2:port [\n";
  968. mainStream << " a lv2:OutputPort, lv2:AudioPort ;\n";
  969. mainStream << " lv2:index " << portIndexNum << " ;\n";
  970. mainStream << " lv2:symbol \"clv2_audio_out_" << portIndexLabel << "\" ;\n";
  971. mainStream << " lv2:name \"Audio Output " << portIndexLabel << "\" ;\n";
  972. mainStream << " ] ;\n";
  973. }
  974. CarlaStringList uniqueSymbolNames;
  975. char strBufName[STR_MAX+1];
  976. char strBufSymbol[STR_MAX+1];
  977. carla_zeroChars(strBufName, STR_MAX+1);
  978. carla_zeroChars(strBufSymbol, STR_MAX+1);
  979. for (uint32_t i=0; i<pData->param.count; ++i)
  980. {
  981. const ParameterData& paramData(pData->param.data[i]);
  982. const ParameterRanges& paramRanges(pData->param.ranges[i]);
  983. const String portIndexNum(portIndex++);
  984. mainStream << " lv2:port [\n";
  985. if (paramData.type == PARAMETER_INPUT)
  986. mainStream << " a lv2:InputPort, lv2:ControlPort ;\n";
  987. else
  988. mainStream << " a lv2:OutputPort, lv2:ControlPort ;\n";
  989. if (paramData.hints & PARAMETER_IS_BOOLEAN)
  990. mainStream << " lv2:portProperty lv2:toggled ;\n";
  991. if (paramData.hints & PARAMETER_IS_INTEGER)
  992. mainStream << " lv2:portProperty lv2:integer ;\n";
  993. // TODO logarithmic, enabled (not on gui), automable, samplerate, scalepoints
  994. if (! getParameterName(i, strBufName))
  995. strBufName[0] = '\0';
  996. if (! getParameterSymbol(i, strBufSymbol))
  997. strBufSymbol[0] = '\0';
  998. if (strBufSymbol[0] == '\0')
  999. {
  1000. CarlaString s(strBufName);
  1001. s.toBasic();
  1002. std::memcpy(strBufSymbol, s.buffer(), s.length()+1);
  1003. if (strBufSymbol[0] >= '0' && strBufSymbol[0] <= '9')
  1004. {
  1005. const size_t len(std::strlen(strBufSymbol));
  1006. std::memmove(strBufSymbol+1, strBufSymbol, len);
  1007. strBufSymbol[0] = '_';
  1008. strBufSymbol[len+1] = '\0';
  1009. }
  1010. }
  1011. if (uniqueSymbolNames.contains(strBufSymbol))
  1012. std::snprintf(strBufSymbol, STR_MAX, "clv2_param_%d", i+1);
  1013. mainStream << " lv2:index " << portIndexNum << " ;\n";
  1014. mainStream << " lv2:symbol \"" << strBufSymbol << "\" ;\n";
  1015. mainStream << " lv2:name \"\"\"" << strBufName << "\"\"\" ;\n";
  1016. mainStream << " lv2:default " << String(paramRanges.def) << " ;\n";
  1017. mainStream << " lv2:minimum " << String(paramRanges.min) << " ;\n";
  1018. mainStream << " lv2:maximum " << String(paramRanges.max) << " ;\n";
  1019. // TODO midiCC, midiChannel
  1020. mainStream << " ] ;\n";
  1021. }
  1022. char strBuf[STR_MAX+1];
  1023. carla_zeroChars(strBuf, STR_MAX+1);
  1024. if (! getMaker(strBuf))
  1025. strBuf[0] = '\0';
  1026. mainStream << " rdfs:comment \"Plugin generated using Carla LV2 export.\" ;\n";
  1027. mainStream << " doap:name \"\"\"" << getName() << "\"\"\" ;\n";
  1028. mainStream << " doap:maintainer [ foaf:name \"\"\"" << strBuf << "\"\"\" ] .\n";
  1029. mainStream << "\n";
  1030. const CarlaString mainFilename(bundlepath + CARLA_OS_SEP_STR + symbol + ".ttl");
  1031. const File mainFile(mainFilename.buffer());
  1032. if (! mainFile.replaceWithData(mainStream.getData(), mainStream.getDataSize()))
  1033. {
  1034. pData->engine->setLastError("Failed to write main plugin ttl file");
  1035. return false;
  1036. }
  1037. }
  1038. const CarlaString binaryFilename(bundlepath + CARLA_OS_SEP_STR + symbol + CARLA_LIB_EXT);
  1039. const File binaryFileSource(File::getSpecialLocation(File::currentExecutableFile).getSiblingFile("carla-bridge-lv2" CARLA_LIB_EXT));
  1040. const File binaryFileTarget(binaryFilename.buffer());
  1041. const EngineOptions& opts(pData->engine->getOptions());
  1042. const CarlaString binFolderTarget(bundlepath + CARLA_OS_SEP_STR + "bin");
  1043. const CarlaString resFolderTarget(bundlepath + CARLA_OS_SEP_STR + "res");
  1044. if (! binaryFileSource.copyFileTo(binaryFileTarget))
  1045. {
  1046. pData->engine->setLastError("Failed to copy plugin binary");
  1047. return false;
  1048. }
  1049. #ifdef CARLA_OS_WIN
  1050. File(opts.resourceDir).copyDirectoryTo(File(resFolderTarget.buffer()));
  1051. // Copying all the binaries is pointless, just go through the expected needed bits
  1052. const File binFolder1(opts.binaryDir);
  1053. const File binFolder2(binFolderTarget.buffer());
  1054. binFolder2.createDirectory();
  1055. static const char* files[] = {
  1056. "carla-bridge-native.exe",
  1057. "carla-bridge-win32.exe",
  1058. "carla-discovery-win32.exe",
  1059. "carla-discovery-win64.exe",
  1060. "libcarla_utils.dll"
  1061. };
  1062. for (int i=0; i<5; ++i)
  1063. binFolder1.getChildFile(files[i]).copyFileTo(binFolder2.getChildFile(files[i]));;
  1064. #else
  1065. File(opts.binaryDir).createSymbolicLink(File(binFolderTarget.buffer()), true);
  1066. File(opts.resourceDir).createSymbolicLink(File(resFolderTarget.buffer()), true);
  1067. #endif
  1068. return true;
  1069. }
  1070. // -------------------------------------------------------------------
  1071. // Set data (internal stuff)
  1072. void CarlaPlugin::setId(const uint newId) noexcept
  1073. {
  1074. pData->id = newId;
  1075. }
  1076. void CarlaPlugin::setName(const char* const newName)
  1077. {
  1078. CARLA_SAFE_ASSERT_RETURN(newName != nullptr && newName[0] != '\0',);
  1079. if (pData->name != nullptr)
  1080. delete[] pData->name;
  1081. pData->name = carla_strdup(newName);
  1082. }
  1083. void CarlaPlugin::setOption(const uint option, const bool yesNo, const bool sendCallback)
  1084. {
  1085. CARLA_SAFE_ASSERT_UINT2_RETURN(getOptionsAvailable() & option, getOptionsAvailable(), option,);
  1086. if (yesNo)
  1087. pData->options |= option;
  1088. else
  1089. pData->options &= ~option;
  1090. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1091. if (sendCallback)
  1092. pData->engine->callback(true, true,
  1093. ENGINE_CALLBACK_OPTION_CHANGED,
  1094. pData->id,
  1095. static_cast<int>(option),
  1096. yesNo ? 1 : 0,
  1097. 0, 0.0f, nullptr);
  1098. #else
  1099. // unused
  1100. return; (void)sendCallback;
  1101. #endif
  1102. }
  1103. void CarlaPlugin::setEnabled(const bool yesNo) noexcept
  1104. {
  1105. if (pData->enabled == yesNo)
  1106. return;
  1107. pData->masterMutex.lock();
  1108. pData->enabled = yesNo;
  1109. if (yesNo && ! pData->client->isActive())
  1110. pData->client->activate();
  1111. pData->masterMutex.unlock();
  1112. }
  1113. void CarlaPlugin::setActive(const bool active, const bool sendOsc, const bool sendCallback) noexcept
  1114. {
  1115. if (pData->engineBridged) {
  1116. CARLA_SAFE_ASSERT_RETURN(!sendOsc && !sendCallback,);
  1117. } else if (pData->enginePlugin) {
  1118. // nothing here
  1119. } else {
  1120. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  1121. }
  1122. if (pData->active == active)
  1123. return;
  1124. {
  1125. const ScopedSingleProcessLocker spl(this, true);
  1126. if (active)
  1127. activate();
  1128. else
  1129. deactivate();
  1130. }
  1131. pData->active = active;
  1132. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1133. const float value = active ? 1.0f : 0.0f;
  1134. pData->engine->callback(sendCallback, sendOsc,
  1135. ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED,
  1136. pData->id,
  1137. PARAMETER_ACTIVE,
  1138. 0, 0,
  1139. value,
  1140. nullptr);
  1141. #endif
  1142. }
  1143. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1144. void CarlaPlugin::setDryWet(const float value, const bool sendOsc, const bool sendCallback) noexcept
  1145. {
  1146. if (pData->engineBridged) {
  1147. CARLA_SAFE_ASSERT_RETURN(!sendOsc && !sendCallback,);
  1148. } else {
  1149. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  1150. }
  1151. CARLA_SAFE_ASSERT(value >= 0.0f && value <= 1.0f);
  1152. const float fixedValue(carla_fixedValue<float>(0.0f, 1.0f, value));
  1153. if (carla_isEqual(pData->postProc.dryWet, fixedValue))
  1154. return;
  1155. pData->postProc.dryWet = fixedValue;
  1156. pData->engine->callback(sendCallback, sendOsc,
  1157. ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED,
  1158. pData->id,
  1159. PARAMETER_DRYWET,
  1160. 0, 0,
  1161. fixedValue,
  1162. nullptr);
  1163. }
  1164. void CarlaPlugin::setVolume(const float value, const bool sendOsc, const bool sendCallback) noexcept
  1165. {
  1166. if (pData->engineBridged) {
  1167. CARLA_SAFE_ASSERT_RETURN(!sendOsc && !sendCallback,);
  1168. } else {
  1169. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  1170. }
  1171. CARLA_SAFE_ASSERT(value >= 0.0f && value <= 1.27f);
  1172. const float fixedValue(carla_fixedValue<float>(0.0f, 1.27f, value));
  1173. if (carla_isEqual(pData->postProc.volume, fixedValue))
  1174. return;
  1175. pData->postProc.volume = fixedValue;
  1176. pData->engine->callback(sendCallback, sendOsc,
  1177. ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED,
  1178. pData->id,
  1179. PARAMETER_VOLUME,
  1180. 0, 0,
  1181. fixedValue,
  1182. nullptr);
  1183. }
  1184. void CarlaPlugin::setBalanceLeft(const float value, const bool sendOsc, const bool sendCallback) noexcept
  1185. {
  1186. if (pData->engineBridged) {
  1187. CARLA_SAFE_ASSERT_RETURN(!sendOsc && !sendCallback,);
  1188. } else {
  1189. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  1190. }
  1191. CARLA_SAFE_ASSERT(value >= -1.0f && value <= 1.0f);
  1192. const float fixedValue(carla_fixedValue<float>(-1.0f, 1.0f, value));
  1193. if (carla_isEqual(pData->postProc.balanceLeft, fixedValue))
  1194. return;
  1195. pData->postProc.balanceLeft = fixedValue;
  1196. pData->engine->callback(sendCallback, sendOsc,
  1197. ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED,
  1198. pData->id,
  1199. PARAMETER_BALANCE_LEFT,
  1200. 0, 0,
  1201. fixedValue,
  1202. nullptr);
  1203. }
  1204. void CarlaPlugin::setBalanceRight(const float value, const bool sendOsc, const bool sendCallback) noexcept
  1205. {
  1206. if (pData->engineBridged) {
  1207. CARLA_SAFE_ASSERT_RETURN(!sendOsc && !sendCallback,);
  1208. } else {
  1209. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  1210. }
  1211. CARLA_SAFE_ASSERT(value >= -1.0f && value <= 1.0f);
  1212. const float fixedValue(carla_fixedValue<float>(-1.0f, 1.0f, value));
  1213. if (carla_isEqual(pData->postProc.balanceRight, fixedValue))
  1214. return;
  1215. pData->postProc.balanceRight = fixedValue;
  1216. pData->engine->callback(sendCallback, sendOsc,
  1217. ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED,
  1218. pData->id,
  1219. PARAMETER_BALANCE_RIGHT,
  1220. 0, 0,
  1221. fixedValue,
  1222. nullptr);
  1223. }
  1224. void CarlaPlugin::setPanning(const float value, const bool sendOsc, const bool sendCallback) noexcept
  1225. {
  1226. if (pData->engineBridged) {
  1227. CARLA_SAFE_ASSERT_RETURN(!sendOsc && !sendCallback,);
  1228. } else {
  1229. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  1230. }
  1231. CARLA_SAFE_ASSERT(value >= -1.0f && value <= 1.0f);
  1232. const float fixedValue(carla_fixedValue<float>(-1.0f, 1.0f, value));
  1233. if (carla_isEqual(pData->postProc.panning, fixedValue))
  1234. return;
  1235. pData->postProc.panning = fixedValue;
  1236. pData->engine->callback(sendCallback, sendOsc,
  1237. ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED,
  1238. pData->id,
  1239. PARAMETER_PANNING,
  1240. 0, 0,
  1241. fixedValue,
  1242. nullptr);
  1243. }
  1244. void CarlaPlugin::setDryWetRT(const float value, const bool sendCallbackLater) noexcept
  1245. {
  1246. CARLA_SAFE_ASSERT(value >= 0.0f && value <= 1.0f);
  1247. const float fixedValue(carla_fixedValue<float>(0.0f, 1.0f, value));
  1248. if (carla_isEqual(pData->postProc.dryWet, fixedValue))
  1249. return;
  1250. pData->postProc.dryWet = fixedValue;
  1251. pData->postponeRtEvent(kPluginPostRtEventParameterChange, sendCallbackLater, PARAMETER_DRYWET, 0, 0, fixedValue);
  1252. }
  1253. void CarlaPlugin::setVolumeRT(const float value, const bool sendCallbackLater) noexcept
  1254. {
  1255. CARLA_SAFE_ASSERT(value >= 0.0f && value <= 1.27f);
  1256. const float fixedValue(carla_fixedValue<float>(0.0f, 1.27f, value));
  1257. if (carla_isEqual(pData->postProc.volume, fixedValue))
  1258. return;
  1259. pData->postProc.volume = fixedValue;
  1260. pData->postponeRtEvent(kPluginPostRtEventParameterChange, sendCallbackLater, PARAMETER_VOLUME, 0, 0, fixedValue);
  1261. }
  1262. void CarlaPlugin::setBalanceLeftRT(const float value, const bool sendCallbackLater) noexcept
  1263. {
  1264. CARLA_SAFE_ASSERT(value >= -1.0f && value <= 1.0f);
  1265. const float fixedValue(carla_fixedValue<float>(-1.0f, 1.0f, value));
  1266. if (carla_isEqual(pData->postProc.balanceLeft, fixedValue))
  1267. return;
  1268. pData->postProc.balanceLeft = fixedValue;
  1269. pData->postponeRtEvent(kPluginPostRtEventParameterChange, sendCallbackLater, PARAMETER_BALANCE_LEFT, 0, 0, fixedValue);
  1270. }
  1271. void CarlaPlugin::setBalanceRightRT(const float value, const bool sendCallbackLater) noexcept
  1272. {
  1273. CARLA_SAFE_ASSERT(value >= -1.0f && value <= 1.0f);
  1274. const float fixedValue(carla_fixedValue<float>(-1.0f, 1.0f, value));
  1275. if (carla_isEqual(pData->postProc.balanceRight, fixedValue))
  1276. return;
  1277. pData->postProc.balanceRight = fixedValue;
  1278. pData->postponeRtEvent(kPluginPostRtEventParameterChange, sendCallbackLater, PARAMETER_BALANCE_RIGHT, 0, 0, fixedValue);
  1279. }
  1280. void CarlaPlugin::setPanningRT(const float value, const bool sendCallbackLater) noexcept
  1281. {
  1282. CARLA_SAFE_ASSERT(value >= -1.0f && value <= 1.0f);
  1283. const float fixedValue(carla_fixedValue<float>(-1.0f, 1.0f, value));
  1284. if (carla_isEqual(pData->postProc.panning, fixedValue))
  1285. return;
  1286. pData->postProc.panning = fixedValue;
  1287. pData->postponeRtEvent(kPluginPostRtEventParameterChange, sendCallbackLater, PARAMETER_PANNING, 0, 0, fixedValue);
  1288. }
  1289. #endif // ! BUILD_BRIDGE_ALTERNATIVE_ARCH
  1290. void CarlaPlugin::setCtrlChannel(const int8_t channel, const bool sendOsc, const bool sendCallback) noexcept
  1291. {
  1292. if (pData->engineBridged) {
  1293. CARLA_SAFE_ASSERT_RETURN(!sendOsc && !sendCallback,);
  1294. } else {
  1295. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  1296. }
  1297. CARLA_SAFE_ASSERT_RETURN(channel >= -1 && channel < MAX_MIDI_CHANNELS,);
  1298. if (pData->ctrlChannel == channel)
  1299. return;
  1300. pData->ctrlChannel = channel;
  1301. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1302. const float channelf = static_cast<float>(channel);
  1303. pData->engine->callback(sendCallback, sendOsc,
  1304. ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED,
  1305. pData->id,
  1306. PARAMETER_CTRL_CHANNEL,
  1307. 0, 0,
  1308. channelf, nullptr);
  1309. #endif
  1310. }
  1311. // -------------------------------------------------------------------
  1312. // Set data (plugin-specific stuff)
  1313. void CarlaPlugin::setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept
  1314. {
  1315. if (pData->engineBridged) {
  1316. // NOTE: some LV2 plugins feedback messages to UI on purpose
  1317. CARLA_SAFE_ASSERT_RETURN(getType() == PLUGIN_LV2 || !sendGui,);
  1318. } else if (pData->enginePlugin) {
  1319. // nothing here
  1320. } else {
  1321. CARLA_SAFE_ASSERT_RETURN(sendGui || sendOsc || sendCallback,); // never call this from RT
  1322. }
  1323. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  1324. if (sendGui && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1325. uiParameterChange(parameterId, value);
  1326. pData->engine->callback(sendCallback, sendOsc,
  1327. ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED,
  1328. pData->id,
  1329. static_cast<int>(parameterId),
  1330. 0, 0,
  1331. value,
  1332. nullptr);
  1333. }
  1334. void CarlaPlugin::setParameterValueRT(const uint32_t parameterId, const float value, const bool sendCallbackLater) noexcept
  1335. {
  1336. pData->postponeRtEvent(kPluginPostRtEventParameterChange,
  1337. sendCallbackLater, static_cast<int32_t>(parameterId), 0, 0, value);
  1338. }
  1339. void CarlaPlugin::setParameterValueByRealIndex(const int32_t rindex, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept
  1340. {
  1341. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1342. CARLA_SAFE_ASSERT_RETURN(rindex > PARAMETER_MAX && rindex != PARAMETER_NULL,);
  1343. switch (rindex)
  1344. {
  1345. case PARAMETER_ACTIVE:
  1346. return setActive((value > 0.0f), sendOsc, sendCallback);
  1347. case PARAMETER_CTRL_CHANNEL:
  1348. return setCtrlChannel(int8_t(value), sendOsc, sendCallback);
  1349. case PARAMETER_DRYWET:
  1350. return setDryWet(value, sendOsc, sendCallback);
  1351. case PARAMETER_VOLUME:
  1352. return setVolume(value, sendOsc, sendCallback);
  1353. case PARAMETER_BALANCE_LEFT:
  1354. return setBalanceLeft(value, sendOsc, sendCallback);
  1355. case PARAMETER_BALANCE_RIGHT:
  1356. return setBalanceRight(value, sendOsc, sendCallback);
  1357. case PARAMETER_PANNING:
  1358. return setPanning(value, sendOsc, sendCallback);
  1359. }
  1360. #endif
  1361. CARLA_SAFE_ASSERT_RETURN(rindex >= 0,);
  1362. for (uint32_t i=0; i < pData->param.count; ++i)
  1363. {
  1364. if (pData->param.data[i].rindex == rindex)
  1365. {
  1366. //if (carla_isNotEqual(getParameterValue(i), value))
  1367. setParameterValue(i, value, sendGui, sendOsc, sendCallback);
  1368. break;
  1369. }
  1370. }
  1371. }
  1372. void CarlaPlugin::setParameterMidiChannel(const uint32_t parameterId, const uint8_t channel, const bool sendOsc, const bool sendCallback) noexcept
  1373. {
  1374. if (pData->engineBridged) {
  1375. CARLA_SAFE_ASSERT_RETURN(!sendOsc && !sendCallback,);
  1376. } else {
  1377. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  1378. }
  1379. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  1380. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1381. if (pData->param.data[parameterId].midiChannel == channel)
  1382. return;
  1383. pData->param.data[parameterId].midiChannel = channel;
  1384. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1385. pData->engine->callback(sendCallback, sendOsc,
  1386. ENGINE_CALLBACK_PARAMETER_MIDI_CHANNEL_CHANGED,
  1387. pData->id,
  1388. static_cast<int>(parameterId),
  1389. channel,
  1390. 0, 0.0f, nullptr);
  1391. #endif
  1392. }
  1393. void CarlaPlugin::setParameterMappedControlIndex(const uint32_t parameterId, const int16_t index,
  1394. const bool sendOsc, const bool sendCallback,
  1395. const bool reconfigureNow) noexcept
  1396. {
  1397. if (pData->engineBridged) {
  1398. CARLA_SAFE_ASSERT_RETURN(!sendOsc && !sendCallback,);
  1399. } else {
  1400. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  1401. }
  1402. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  1403. CARLA_SAFE_ASSERT_RETURN(index >= CONTROL_INDEX_NONE && index <= CONTROL_INDEX_MAX_ALLOWED,);
  1404. ParameterData& paramData(pData->param.data[parameterId]);
  1405. if (paramData.mappedControlIndex == index)
  1406. return;
  1407. const ParameterRanges& paramRanges(pData->param.ranges[parameterId]);
  1408. if ((paramData.hints & PARAMETER_MAPPED_RANGES_SET) == 0x0)
  1409. setParameterMappedRange(parameterId, paramRanges.min, paramRanges.max, true, true);
  1410. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1411. char strBuf[STR_MAX+1];
  1412. carla_zeroChars(strBuf, STR_MAX+1);
  1413. if (! getParameterName(parameterId, strBuf))
  1414. std::snprintf(strBuf, STR_MAX, "Param %u", parameterId);
  1415. const uint portNameSize = pData->engine->getMaxPortNameSize();
  1416. if (portNameSize < STR_MAX)
  1417. strBuf[portNameSize] = '\0';
  1418. if (index == CONTROL_INDEX_CV)
  1419. {
  1420. CARLA_SAFE_ASSERT_RETURN(pData->event.cvSourcePorts != nullptr,);
  1421. CARLA_SAFE_ASSERT_RETURN(paramData.type == PARAMETER_INPUT,);
  1422. CARLA_SAFE_ASSERT_RETURN(paramData.hints & PARAMETER_CAN_BE_CV_CONTROLLED,);
  1423. CarlaEngineCVPort* const cvPort =
  1424. (CarlaEngineCVPort*)pData->client->addPort(kEnginePortTypeCV, strBuf, true, parameterId);
  1425. cvPort->setRange(paramData.mappedMinimum, paramData.mappedMaximum);
  1426. pData->event.cvSourcePorts->addCVSource(cvPort, parameterId, reconfigureNow);
  1427. }
  1428. else if (paramData.mappedControlIndex == CONTROL_INDEX_CV)
  1429. {
  1430. CARLA_SAFE_ASSERT_RETURN(pData->event.cvSourcePorts != nullptr,);
  1431. CARLA_SAFE_ASSERT(pData->client->removePort(kEnginePortTypeCV, strBuf, true));
  1432. CARLA_SAFE_ASSERT(pData->event.cvSourcePorts->removeCVSource(parameterId));
  1433. }
  1434. else if (paramData.mappedControlIndex == CONTROL_INDEX_NONE)
  1435. {
  1436. // when doing MIDI CC mapping, ensure ranges are within bounds
  1437. if (paramData.mappedMinimum < paramRanges.min || paramData.mappedMaximum > paramRanges.max)
  1438. setParameterMappedRange(parameterId,
  1439. std::max(paramData.mappedMinimum, paramRanges.min),
  1440. std::min(paramData.mappedMaximum, paramRanges.max),
  1441. true, true);
  1442. }
  1443. #endif
  1444. paramData.mappedControlIndex = index;
  1445. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1446. if (index == CONTROL_INDEX_MIDI_LEARN)
  1447. pData->midiLearnParameterIndex = static_cast<int32_t>(parameterId);
  1448. else
  1449. pData->midiLearnParameterIndex = -1;
  1450. pData->engine->callback(sendCallback, sendOsc,
  1451. ENGINE_CALLBACK_PARAMETER_MAPPED_CONTROL_INDEX_CHANGED,
  1452. pData->id,
  1453. static_cast<int>(parameterId),
  1454. index,
  1455. 0, 0.0f, nullptr);
  1456. #else
  1457. return;
  1458. // unused
  1459. (void)reconfigureNow;
  1460. #endif
  1461. }
  1462. void CarlaPlugin::setParameterMappedRange(const uint32_t parameterId, const float minimum, const float maximum,
  1463. const bool sendOsc, const bool sendCallback) noexcept
  1464. {
  1465. if (pData->engineBridged) {
  1466. CARLA_SAFE_ASSERT_RETURN(!sendOsc && !sendCallback,);
  1467. } else {
  1468. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  1469. }
  1470. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  1471. ParameterData& paramData(pData->param.data[parameterId]);
  1472. if (carla_isEqual(paramData.mappedMinimum, minimum) &&
  1473. carla_isEqual(paramData.mappedMaximum, maximum) &&
  1474. (paramData.hints & PARAMETER_MAPPED_RANGES_SET) != 0x0)
  1475. return;
  1476. if (paramData.mappedControlIndex != CONTROL_INDEX_NONE && paramData.mappedControlIndex != CONTROL_INDEX_CV)
  1477. {
  1478. const ParameterRanges& paramRanges(pData->param.ranges[parameterId]);
  1479. CARLA_SAFE_ASSERT_RETURN(minimum >= paramRanges.min,);
  1480. CARLA_SAFE_ASSERT_RETURN(maximum <= paramRanges.max,);
  1481. }
  1482. paramData.hints |= PARAMETER_MAPPED_RANGES_SET;
  1483. paramData.mappedMinimum = minimum;
  1484. paramData.mappedMaximum = maximum;
  1485. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1486. if (pData->event.cvSourcePorts != nullptr && paramData.mappedControlIndex == CONTROL_INDEX_CV)
  1487. pData->event.cvSourcePorts->setCVSourceRange(parameterId, minimum, maximum);
  1488. char strBuf[STR_MAX+1];
  1489. carla_zeroChars(strBuf, STR_MAX+1);
  1490. std::snprintf(strBuf, STR_MAX, "%.12g:%.12g", static_cast<double>(minimum), static_cast<double>(maximum));
  1491. pData->engine->callback(sendCallback, sendOsc,
  1492. ENGINE_CALLBACK_PARAMETER_MAPPED_RANGE_CHANGED,
  1493. pData->id,
  1494. static_cast<int>(parameterId),
  1495. 0, 0, 0.0f,
  1496. strBuf);
  1497. #endif
  1498. }
  1499. void CarlaPlugin::setCustomData(const char* const type, const char* const key, const char* const value, const bool)
  1500. {
  1501. CARLA_SAFE_ASSERT_RETURN(type != nullptr && type[0] != '\0',);
  1502. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  1503. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  1504. // Ignore some keys
  1505. if (std::strcmp(type, CUSTOM_DATA_TYPE_STRING) == 0)
  1506. {
  1507. const PluginType ptype = getType();
  1508. if ((ptype == PLUGIN_INTERNAL && std::strncmp(key, "CarlaAlternateFile", 18) == 0) ||
  1509. (ptype == PLUGIN_DSSI && std::strcmp (key, "guiVisible") == 0) ||
  1510. (ptype == PLUGIN_LV2 && std::strncmp(key, "OSC:", 4) == 0))
  1511. return;
  1512. }
  1513. // Check if we already have this key
  1514. for (LinkedList<CustomData>::Itenerator it = pData->custom.begin2(); it.valid(); it.next())
  1515. {
  1516. CustomData& customData(it.getValue(kCustomDataFallbackNC));
  1517. CARLA_SAFE_ASSERT_CONTINUE(customData.isValid());
  1518. if (std::strcmp(customData.key, key) == 0)
  1519. {
  1520. if (customData.value != nullptr)
  1521. delete[] customData.value;
  1522. customData.value = carla_strdup(value);
  1523. return;
  1524. }
  1525. }
  1526. // Otherwise store it
  1527. CustomData customData;
  1528. customData.type = carla_strdup(type);
  1529. customData.key = carla_strdup(key);
  1530. customData.value = carla_strdup(value);
  1531. pData->custom.append(customData);
  1532. }
  1533. void CarlaPlugin::setChunkData(const void* const data, const std::size_t dataSize)
  1534. {
  1535. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  1536. CARLA_SAFE_ASSERT_RETURN(dataSize > 0,);
  1537. CARLA_SAFE_ASSERT(false); // this should never happen
  1538. }
  1539. void CarlaPlugin::setProgram(const int32_t index,
  1540. const bool sendGui, const bool sendOsc, const bool sendCallback, const bool) noexcept
  1541. {
  1542. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->prog.count),);
  1543. pData->prog.current = index;
  1544. pData->engine->callback(sendCallback, sendOsc,
  1545. ENGINE_CALLBACK_PROGRAM_CHANGED,
  1546. pData->id,
  1547. index,
  1548. 0, 0, 0.0f, nullptr);
  1549. // Change default parameter values
  1550. if (index >= 0)
  1551. {
  1552. if (sendGui && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1553. uiProgramChange(static_cast<uint32_t>(index));
  1554. switch (getType())
  1555. {
  1556. case PLUGIN_SF2:
  1557. case PLUGIN_SFZ:
  1558. break;
  1559. default:
  1560. pData->updateParameterValues(this, sendCallback, sendOsc, true);
  1561. break;
  1562. }
  1563. }
  1564. }
  1565. void CarlaPlugin::setMidiProgram(const int32_t index,
  1566. const bool sendGui, const bool sendOsc, const bool sendCallback, const bool) noexcept
  1567. {
  1568. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->midiprog.count),);
  1569. pData->midiprog.current = index;
  1570. pData->engine->callback(sendCallback, sendOsc,
  1571. ENGINE_CALLBACK_MIDI_PROGRAM_CHANGED,
  1572. pData->id,
  1573. index,
  1574. 0, 0, 0.0f, nullptr);
  1575. // Change default parameter values
  1576. if (index >= 0)
  1577. {
  1578. if (sendGui && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1579. uiMidiProgramChange(static_cast<uint32_t>(index));
  1580. switch (getType())
  1581. {
  1582. case PLUGIN_SF2:
  1583. case PLUGIN_SFZ:
  1584. break;
  1585. default:
  1586. pData->updateParameterValues(this, sendCallback, sendOsc, true);
  1587. break;
  1588. }
  1589. }
  1590. }
  1591. void CarlaPlugin::setMidiProgramById(const uint32_t bank, const uint32_t program, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept
  1592. {
  1593. for (uint32_t i=0; i < pData->midiprog.count; ++i)
  1594. {
  1595. if (pData->midiprog.data[i].bank == bank && pData->midiprog.data[i].program == program)
  1596. return setMidiProgram(static_cast<int32_t>(i), sendGui, sendOsc, sendCallback);
  1597. }
  1598. }
  1599. void CarlaPlugin::setProgramRT(const uint32_t uindex, const bool sendCallbackLater) noexcept
  1600. {
  1601. CARLA_SAFE_ASSERT_RETURN(uindex < pData->prog.count,);
  1602. const int32_t index = static_cast<int32_t>(uindex);
  1603. pData->prog.current = index;
  1604. // Change default parameter values
  1605. switch (getType())
  1606. {
  1607. case PLUGIN_SF2:
  1608. case PLUGIN_SFZ:
  1609. break;
  1610. default:
  1611. pData->updateDefaultParameterValues(this);
  1612. break;
  1613. }
  1614. pData->postponeRtEvent(kPluginPostRtEventProgramChange, sendCallbackLater, index, 0, 0, 0.0f);
  1615. }
  1616. void CarlaPlugin::setMidiProgramRT(const uint32_t uindex, const bool sendCallbackLater) noexcept
  1617. {
  1618. CARLA_SAFE_ASSERT_RETURN(uindex < pData->midiprog.count,);
  1619. const int32_t index = static_cast<int32_t>(uindex);
  1620. pData->midiprog.current = index;
  1621. // Change default parameter values
  1622. switch (getType())
  1623. {
  1624. case PLUGIN_SF2:
  1625. case PLUGIN_SFZ:
  1626. break;
  1627. default:
  1628. pData->updateDefaultParameterValues(this);
  1629. break;
  1630. }
  1631. pData->postponeRtEvent(kPluginPostRtEventMidiProgramChange, sendCallbackLater, index, 0, 0, 0.0f);
  1632. }
  1633. // -------------------------------------------------------------------
  1634. // Plugin state
  1635. void CarlaPlugin::reloadPrograms(const bool)
  1636. {
  1637. }
  1638. // -------------------------------------------------------------------
  1639. // Plugin processing
  1640. void CarlaPlugin::activate() noexcept
  1641. {
  1642. CARLA_SAFE_ASSERT(! pData->active);
  1643. }
  1644. void CarlaPlugin::deactivate() noexcept
  1645. {
  1646. CARLA_SAFE_ASSERT(pData->active);
  1647. }
  1648. void CarlaPlugin::bufferSizeChanged(const uint32_t)
  1649. {
  1650. }
  1651. void CarlaPlugin::sampleRateChanged(const double)
  1652. {
  1653. }
  1654. void CarlaPlugin::offlineModeChanged(const bool)
  1655. {
  1656. }
  1657. // -------------------------------------------------------------------
  1658. // Misc
  1659. void CarlaPlugin::idle()
  1660. {
  1661. if (! pData->enabled)
  1662. return;
  1663. const bool hasUI(pData->hints & PLUGIN_HAS_CUSTOM_UI);
  1664. const bool needsUiMainThread(pData->hints & PLUGIN_NEEDS_UI_MAIN_THREAD);
  1665. const uint32_t latency(getLatencyInFrames());
  1666. if (pData->latency.frames != latency)
  1667. {
  1668. carla_stdout("latency changed to %i samples", latency);
  1669. const ScopedSingleProcessLocker sspl(this, true);
  1670. pData->client->setLatency(latency);
  1671. #ifndef BUILD_BRIDGE
  1672. pData->latency.recreateBuffers(pData->latency.channels, latency);
  1673. #else
  1674. pData->latency.frames = latency;
  1675. #endif
  1676. }
  1677. ProtectedData::PostRtEvents::Access rtEvents(pData->postRtEvents);
  1678. if (rtEvents.isEmpty())
  1679. return;
  1680. for (RtLinkedList<PluginPostRtEvent>::Itenerator it = rtEvents.getDataIterator(); it.valid(); it.next())
  1681. {
  1682. const PluginPostRtEvent& event(it.getValue(kPluginPostRtEventFallback));
  1683. CARLA_SAFE_ASSERT_CONTINUE(event.type != kPluginPostRtEventNull);
  1684. switch (event.type)
  1685. {
  1686. case kPluginPostRtEventNull: {
  1687. } break;
  1688. case kPluginPostRtEventDebug: {
  1689. pData->engine->callback(true, true,
  1690. ENGINE_CALLBACK_DEBUG, pData->id,
  1691. event.value1, event.value2, event.value3, event.valuef, nullptr);
  1692. } break;
  1693. case kPluginPostRtEventParameterChange: {
  1694. // Update UI
  1695. if (event.value1 >= 0 && hasUI)
  1696. {
  1697. if (needsUiMainThread)
  1698. pData->postUiEvents.append(event);
  1699. else
  1700. uiParameterChange(static_cast<uint32_t>(event.value1), event.valuef);
  1701. }
  1702. if (event.sendCallback)
  1703. {
  1704. // Update Host
  1705. pData->engine->callback(true, true,
  1706. ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED,
  1707. pData->id,
  1708. event.value1,
  1709. 0, 0,
  1710. event.valuef,
  1711. nullptr);
  1712. }
  1713. } break;
  1714. case kPluginPostRtEventProgramChange: {
  1715. // Update UI
  1716. if (event.value1 >= 0 && hasUI)
  1717. {
  1718. if (needsUiMainThread)
  1719. pData->postUiEvents.append(event);
  1720. else
  1721. uiProgramChange(static_cast<uint32_t>(event.value1));
  1722. }
  1723. // Update param values
  1724. for (uint32_t j=0; j < pData->param.count; ++j)
  1725. {
  1726. const float paramDefault(pData->param.ranges[j].def);
  1727. const float paramValue(getParameterValue(j));
  1728. pData->engine->callback(true, true,
  1729. ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED,
  1730. pData->id,
  1731. static_cast<int>(j),
  1732. 0, 0,
  1733. paramValue,
  1734. nullptr);
  1735. pData->engine->callback(true, true,
  1736. ENGINE_CALLBACK_PARAMETER_DEFAULT_CHANGED,
  1737. pData->id,
  1738. static_cast<int>(j),
  1739. 0, 0,
  1740. paramDefault,
  1741. nullptr);
  1742. }
  1743. if (event.sendCallback)
  1744. {
  1745. // Update Host
  1746. pData->engine->callback(true, true,
  1747. ENGINE_CALLBACK_PROGRAM_CHANGED,
  1748. pData->id,
  1749. event.value1,
  1750. 0, 0, 0.0f, nullptr);
  1751. }
  1752. } break;
  1753. case kPluginPostRtEventMidiProgramChange: {
  1754. // Update UI
  1755. if (event.value1 >= 0 && hasUI)
  1756. {
  1757. if (needsUiMainThread)
  1758. pData->postUiEvents.append(event);
  1759. else
  1760. uiMidiProgramChange(static_cast<uint32_t>(event.value1));
  1761. }
  1762. // Update param values
  1763. for (uint32_t j=0; j < pData->param.count; ++j)
  1764. {
  1765. const float paramDefault(pData->param.ranges[j].def);
  1766. const float paramValue(getParameterValue(j));
  1767. pData->engine->callback(true, true,
  1768. ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED,
  1769. pData->id,
  1770. static_cast<int>(j),
  1771. 0, 0,
  1772. paramValue,
  1773. nullptr);
  1774. pData->engine->callback(true, true,
  1775. ENGINE_CALLBACK_PARAMETER_DEFAULT_CHANGED,
  1776. pData->id,
  1777. static_cast<int>(j),
  1778. 0, 0,
  1779. paramDefault,
  1780. nullptr);
  1781. }
  1782. if (event.sendCallback)
  1783. {
  1784. // Update Host
  1785. pData->engine->callback(true, true,
  1786. ENGINE_CALLBACK_MIDI_PROGRAM_CHANGED,
  1787. pData->id,
  1788. event.value1,
  1789. 0, 0, 0.0f, nullptr);
  1790. }
  1791. } break;
  1792. case kPluginPostRtEventNoteOn: {
  1793. CARLA_SAFE_ASSERT_BREAK(event.value1 >= 0 && event.value1 < MAX_MIDI_CHANNELS);
  1794. CARLA_SAFE_ASSERT_BREAK(event.value2 >= 0 && event.value2 < MAX_MIDI_NOTE);
  1795. CARLA_SAFE_ASSERT_BREAK(event.value3 >= 0 && event.value3 < MAX_MIDI_VALUE);
  1796. const uint8_t channel = static_cast<uint8_t>(event.value1);
  1797. const uint8_t note = static_cast<uint8_t>(event.value2);
  1798. const uint8_t velocity = static_cast<uint8_t>(event.value3);
  1799. // Update UI
  1800. if (hasUI)
  1801. {
  1802. if (needsUiMainThread)
  1803. pData->postUiEvents.append(event);
  1804. else
  1805. uiNoteOn(channel, note, velocity);
  1806. }
  1807. if (event.sendCallback)
  1808. {
  1809. // Update Host
  1810. pData->engine->callback(true, true,
  1811. ENGINE_CALLBACK_NOTE_ON,
  1812. pData->id,
  1813. event.value1,
  1814. event.value2,
  1815. event.value3,
  1816. 0.0f, nullptr);
  1817. }
  1818. } break;
  1819. case kPluginPostRtEventNoteOff: {
  1820. CARLA_SAFE_ASSERT_BREAK(event.value1 >= 0 && event.value1 < MAX_MIDI_CHANNELS);
  1821. CARLA_SAFE_ASSERT_BREAK(event.value2 >= 0 && event.value2 < MAX_MIDI_NOTE);
  1822. const uint8_t channel = static_cast<uint8_t>(event.value1);
  1823. const uint8_t note = static_cast<uint8_t>(event.value2);
  1824. // Update UI
  1825. if (hasUI)
  1826. {
  1827. if (needsUiMainThread)
  1828. pData->postUiEvents.append(event);
  1829. else
  1830. uiNoteOff(channel, note);
  1831. }
  1832. if (event.sendCallback)
  1833. {
  1834. // Update Host
  1835. pData->engine->callback(true, true,
  1836. ENGINE_CALLBACK_NOTE_OFF,
  1837. pData->id,
  1838. event.value1,
  1839. event.value2,
  1840. 0, 0.0f, nullptr);
  1841. }
  1842. } break;
  1843. case kPluginPostRtEventMidiLearn: {
  1844. CARLA_SAFE_ASSERT_BREAK(event.value1 >= 0 && event.value1 < MAX_MIDI_CHANNELS);
  1845. CARLA_SAFE_ASSERT_BREAK(event.value2 >= 0 && event.value2 < MAX_MIDI_NOTE);
  1846. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1847. if (event.sendCallback)
  1848. {
  1849. const int32_t parameterId = event.value1;
  1850. const int32_t midiCC = event.value2;
  1851. const int32_t midiChannel = event.value2;
  1852. pData->engine->callback(true, true,
  1853. ENGINE_CALLBACK_PARAMETER_MAPPED_CONTROL_INDEX_CHANGED,
  1854. pData->id,
  1855. parameterId,
  1856. midiCC,
  1857. 0, 0.0f, nullptr);
  1858. pData->engine->callback(true, true,
  1859. ENGINE_CALLBACK_PARAMETER_MIDI_CHANNEL_CHANGED,
  1860. pData->id,
  1861. parameterId,
  1862. midiChannel,
  1863. 0, 0.0f, nullptr);
  1864. }
  1865. #endif
  1866. } break;
  1867. }
  1868. }
  1869. }
  1870. bool CarlaPlugin::tryLock(const bool forcedOffline) noexcept
  1871. {
  1872. if (forcedOffline)
  1873. {
  1874. #ifndef STOAT_TEST_BUILD
  1875. pData->masterMutex.lock();
  1876. return true;
  1877. #endif
  1878. }
  1879. return pData->masterMutex.tryLock();
  1880. }
  1881. void CarlaPlugin::unlock() noexcept
  1882. {
  1883. pData->masterMutex.unlock();
  1884. }
  1885. // -------------------------------------------------------------------
  1886. // Plugin buffers
  1887. void CarlaPlugin::initBuffers() const noexcept
  1888. {
  1889. pData->audioIn.initBuffers();
  1890. pData->audioOut.initBuffers();
  1891. pData->cvIn.initBuffers();
  1892. pData->cvOut.initBuffers();
  1893. pData->event.initBuffers();
  1894. }
  1895. void CarlaPlugin::clearBuffers() noexcept
  1896. {
  1897. pData->clearBuffers();
  1898. }
  1899. // -------------------------------------------------------------------
  1900. // OSC stuff
  1901. // FIXME
  1902. void CarlaPlugin::handleOscMessage(const char* const, const int, const void* const, const char* const, const lo_message)
  1903. {
  1904. // do nothing
  1905. }
  1906. // -------------------------------------------------------------------
  1907. // MIDI events
  1908. void CarlaPlugin::sendMidiSingleNote(const uint8_t channel, const uint8_t note, const uint8_t velo, const bool sendGui, const bool sendOsc, const bool sendCallback)
  1909. {
  1910. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1911. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1912. CARLA_SAFE_ASSERT_RETURN(velo < MAX_MIDI_VALUE,);
  1913. if (! pData->active)
  1914. return;
  1915. ExternalMidiNote extNote;
  1916. extNote.channel = static_cast<int8_t>(channel);
  1917. extNote.note = note;
  1918. extNote.velo = velo;
  1919. pData->extNotes.appendNonRT(extNote);
  1920. if (sendGui && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1921. {
  1922. if (velo > 0)
  1923. uiNoteOn(channel, note, velo);
  1924. else
  1925. uiNoteOff(channel, note);
  1926. }
  1927. pData->engine->callback(sendCallback, sendOsc,
  1928. (velo > 0) ? ENGINE_CALLBACK_NOTE_ON : ENGINE_CALLBACK_NOTE_OFF,
  1929. pData->id,
  1930. channel,
  1931. note,
  1932. velo,
  1933. 0.0f, nullptr);
  1934. }
  1935. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1936. void CarlaPlugin::postponeRtAllNotesOff()
  1937. {
  1938. if (pData->ctrlChannel < 0 || pData->ctrlChannel >= MAX_MIDI_CHANNELS)
  1939. return;
  1940. PluginPostRtEvent postEvent = {
  1941. kPluginPostRtEventNoteOff,
  1942. true,
  1943. pData->ctrlChannel,
  1944. 0, 0, 0.0f
  1945. };
  1946. for (int32_t i=0; i < MAX_MIDI_NOTE; ++i)
  1947. {
  1948. postEvent.value2 = i;
  1949. pData->postRtEvents.appendRT(postEvent);
  1950. }
  1951. }
  1952. #endif
  1953. // -------------------------------------------------------------------
  1954. // UI Stuff
  1955. void CarlaPlugin::setCustomUITitle(const char* const title) noexcept
  1956. {
  1957. pData->uiTitle = title;
  1958. }
  1959. void CarlaPlugin::showCustomUI(const bool yesNo)
  1960. {
  1961. if (yesNo) {
  1962. CARLA_SAFE_ASSERT(false);
  1963. }
  1964. }
  1965. void* CarlaPlugin::embedCustomUI(void*)
  1966. {
  1967. return nullptr;
  1968. }
  1969. void CarlaPlugin::uiIdle()
  1970. {
  1971. if (pData->hints & PLUGIN_NEEDS_UI_MAIN_THREAD)
  1972. {
  1973. // Update parameter outputs
  1974. for (uint32_t i=0; i < pData->param.count; ++i)
  1975. {
  1976. if (pData->param.data[i].type == PARAMETER_OUTPUT)
  1977. uiParameterChange(i, getParameterValue(i));
  1978. }
  1979. const CarlaMutexLocker sl(pData->postUiEvents.mutex);
  1980. for (LinkedList<PluginPostRtEvent>::Itenerator it = pData->postUiEvents.data.begin2(); it.valid(); it.next())
  1981. {
  1982. const PluginPostRtEvent& event(it.getValue(kPluginPostRtEventFallback));
  1983. CARLA_SAFE_ASSERT_CONTINUE(event.type != kPluginPostRtEventNull);
  1984. switch (event.type)
  1985. {
  1986. case kPluginPostRtEventNull:
  1987. case kPluginPostRtEventDebug:
  1988. case kPluginPostRtEventMidiLearn:
  1989. break;
  1990. case kPluginPostRtEventParameterChange:
  1991. uiParameterChange(static_cast<uint32_t>(event.value1), event.valuef);
  1992. break;
  1993. case kPluginPostRtEventProgramChange:
  1994. uiProgramChange(static_cast<uint32_t>(event.value1));
  1995. break;
  1996. case kPluginPostRtEventMidiProgramChange:
  1997. uiMidiProgramChange(static_cast<uint32_t>(event.value1));
  1998. break;
  1999. case kPluginPostRtEventNoteOn:
  2000. uiNoteOn(static_cast<uint8_t>(event.value1),
  2001. static_cast<uint8_t>(event.value2),
  2002. static_cast<uint8_t>(event.value3));
  2003. break;
  2004. case kPluginPostRtEventNoteOff:
  2005. uiNoteOff(static_cast<uint8_t>(event.value1),
  2006. static_cast<uint8_t>(event.value2));
  2007. break;
  2008. }
  2009. }
  2010. pData->postUiEvents.data.clear();
  2011. }
  2012. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  2013. if (pData->transientTryCounter == 0)
  2014. return;
  2015. if (++pData->transientTryCounter % 10 != 0)
  2016. return;
  2017. if (pData->transientTryCounter >= 200)
  2018. return;
  2019. carla_stdout("Trying to get window...");
  2020. CarlaString uiTitle;
  2021. if (pData->uiTitle.isNotEmpty())
  2022. {
  2023. uiTitle = pData->uiTitle;
  2024. }
  2025. else
  2026. {
  2027. uiTitle = pData->name;
  2028. uiTitle += " (GUI)";
  2029. }
  2030. if (CarlaPluginUI::tryTransientWinIdMatch(getUiBridgeProcessId(), uiTitle,
  2031. pData->engine->getOptions().frontendWinId, pData->transientFirstTry))
  2032. {
  2033. pData->transientTryCounter = 0;
  2034. pData->transientFirstTry = false;
  2035. }
  2036. #endif
  2037. }
  2038. void CarlaPlugin::uiParameterChange(const uint32_t index, const float value) noexcept
  2039. {
  2040. CARLA_SAFE_ASSERT_RETURN(index < getParameterCount(),);
  2041. return;
  2042. // unused
  2043. (void)value;
  2044. }
  2045. void CarlaPlugin::uiProgramChange(const uint32_t index) noexcept
  2046. {
  2047. CARLA_SAFE_ASSERT_RETURN(index < getProgramCount(),);
  2048. }
  2049. void CarlaPlugin::uiMidiProgramChange(const uint32_t index) noexcept
  2050. {
  2051. CARLA_SAFE_ASSERT_RETURN(index < getMidiProgramCount(),);
  2052. }
  2053. void CarlaPlugin::uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) noexcept
  2054. {
  2055. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  2056. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  2057. CARLA_SAFE_ASSERT_RETURN(velo > 0 && velo < MAX_MIDI_VALUE,);
  2058. }
  2059. void CarlaPlugin::uiNoteOff(const uint8_t channel, const uint8_t note) noexcept
  2060. {
  2061. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  2062. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  2063. }
  2064. CarlaEngine* CarlaPlugin::getEngine() const noexcept
  2065. {
  2066. return pData->engine;
  2067. }
  2068. CarlaEngineClient* CarlaPlugin::getEngineClient() const noexcept
  2069. {
  2070. return pData->client;
  2071. }
  2072. CarlaEngineAudioPort* CarlaPlugin::getAudioInPort(const uint32_t index) const noexcept
  2073. {
  2074. return pData->audioIn.ports[index].port;
  2075. }
  2076. CarlaEngineAudioPort* CarlaPlugin::getAudioOutPort(const uint32_t index) const noexcept
  2077. {
  2078. return pData->audioOut.ports[index].port;
  2079. }
  2080. CarlaEngineCVPort* CarlaPlugin::getCVInPort(const uint32_t index) const noexcept
  2081. {
  2082. return pData->cvIn.ports[index].port;
  2083. }
  2084. CarlaEngineCVPort* CarlaPlugin::getCVOutPort(const uint32_t index) const noexcept
  2085. {
  2086. return pData->cvOut.ports[index].port;
  2087. }
  2088. CarlaEngineEventPort* CarlaPlugin::getDefaultEventInPort() const noexcept
  2089. {
  2090. return pData->event.portIn;
  2091. }
  2092. CarlaEngineEventPort* CarlaPlugin::getDefaultEventOutPort() const noexcept
  2093. {
  2094. return pData->event.portOut;
  2095. }
  2096. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  2097. void CarlaPlugin::checkForMidiLearn(EngineEvent& event) noexcept
  2098. {
  2099. if (pData->midiLearnParameterIndex < 0)
  2100. return;
  2101. if (event.ctrl.param == MIDI_CONTROL_BANK_SELECT || event.ctrl.param == MIDI_CONTROL_BANK_SELECT__LSB)
  2102. return;
  2103. if (event.ctrl.param >= MAX_MIDI_CONTROL)
  2104. return;
  2105. const uint32_t parameterId = static_cast<uint32_t>(pData->midiLearnParameterIndex);
  2106. CARLA_SAFE_ASSERT_UINT2_RETURN(parameterId < pData->param.count, parameterId, pData->param.count,);
  2107. ParameterData& paramData(pData->param.data[parameterId]);
  2108. CARLA_SAFE_ASSERT_INT_RETURN(paramData.mappedControlIndex == CONTROL_INDEX_MIDI_LEARN,
  2109. paramData.mappedControlIndex,);
  2110. event.ctrl.handled = true;
  2111. paramData.mappedControlIndex = static_cast<int16_t>(event.ctrl.param);
  2112. paramData.midiChannel = event.channel;
  2113. pData->postponeRtEvent(kPluginPostRtEventMidiLearn, true,
  2114. pData->midiLearnParameterIndex, event.ctrl.param, event.channel, 0.0f);
  2115. pData->midiLearnParameterIndex = -1;
  2116. }
  2117. #endif
  2118. void* CarlaPlugin::getNativeHandle() const noexcept
  2119. {
  2120. return nullptr;
  2121. }
  2122. const void* CarlaPlugin::getNativeDescriptor() const noexcept
  2123. {
  2124. return nullptr;
  2125. }
  2126. uintptr_t CarlaPlugin::getUiBridgeProcessId() const noexcept
  2127. {
  2128. return 0;
  2129. }
  2130. // -------------------------------------------------------------------
  2131. uint32_t CarlaPlugin::getPatchbayNodeId() const noexcept
  2132. {
  2133. return pData->nodeId;
  2134. }
  2135. void CarlaPlugin::setPatchbayNodeId(const uint32_t nodeId) noexcept
  2136. {
  2137. pData->nodeId = nodeId;
  2138. }
  2139. // -------------------------------------------------------------------
  2140. void CarlaPlugin::cloneLV2Files(const CarlaPlugin&)
  2141. {
  2142. carla_stderr2("Warning: cloneLV2Files() called for non-implemented type");
  2143. }
  2144. void CarlaPlugin::restoreLV2State(const bool temporary) noexcept
  2145. {
  2146. carla_stderr2("Warning: restoreLV2State(%s) called for non-implemented type", bool2str(temporary));
  2147. }
  2148. void CarlaPlugin::prepareForDeletion() noexcept
  2149. {
  2150. carla_debug("CarlaPlugin::prepareForDeletion");
  2151. const CarlaMutexLocker cml(pData->masterMutex);
  2152. pData->client->deactivate(true);
  2153. }
  2154. void CarlaPlugin::waitForBridgeSaveSignal() noexcept
  2155. {
  2156. }
  2157. // -------------------------------------------------------------------
  2158. // Scoped Disabler
  2159. CarlaPlugin::ScopedDisabler::ScopedDisabler(CarlaPlugin* const plugin) noexcept
  2160. : fPlugin(plugin),
  2161. fWasEnabled(false)
  2162. {
  2163. CARLA_SAFE_ASSERT_RETURN(plugin != nullptr,);
  2164. CARLA_SAFE_ASSERT_RETURN(plugin->pData != nullptr,);
  2165. CARLA_SAFE_ASSERT_RETURN(plugin->pData->client != nullptr,);
  2166. carla_debug("CarlaPlugin::ScopedDisabler(%p)", plugin);
  2167. plugin->pData->masterMutex.lock();
  2168. if (plugin->pData->enabled)
  2169. {
  2170. fWasEnabled = true;
  2171. plugin->pData->enabled = false;
  2172. if (plugin->pData->client->isActive())
  2173. plugin->pData->client->deactivate(false);
  2174. }
  2175. }
  2176. CarlaPlugin::ScopedDisabler::~ScopedDisabler() noexcept
  2177. {
  2178. CARLA_SAFE_ASSERT_RETURN(fPlugin != nullptr,);
  2179. CARLA_SAFE_ASSERT_RETURN(fPlugin->pData != nullptr,);
  2180. CARLA_SAFE_ASSERT_RETURN(fPlugin->pData->client != nullptr,);
  2181. carla_debug("CarlaPlugin::~ScopedDisabler()");
  2182. if (fWasEnabled)
  2183. {
  2184. fPlugin->pData->enabled = true;
  2185. fPlugin->pData->client->activate();
  2186. }
  2187. fPlugin->pData->masterMutex.unlock();
  2188. }
  2189. // -------------------------------------------------------------------
  2190. // Scoped Process Locker
  2191. CarlaPlugin::ScopedSingleProcessLocker::ScopedSingleProcessLocker(CarlaPlugin* const plugin, const bool block) noexcept
  2192. : fPlugin(plugin),
  2193. fBlock(block)
  2194. {
  2195. CARLA_SAFE_ASSERT_RETURN(fPlugin != nullptr,);
  2196. CARLA_SAFE_ASSERT_RETURN(fPlugin->pData != nullptr,);
  2197. carla_debug("CarlaPlugin::ScopedSingleProcessLocker(%p, %s)", plugin, bool2str(block));
  2198. if (! fBlock)
  2199. return;
  2200. plugin->pData->singleMutex.lock();
  2201. }
  2202. CarlaPlugin::ScopedSingleProcessLocker::~ScopedSingleProcessLocker() noexcept
  2203. {
  2204. CARLA_SAFE_ASSERT_RETURN(fPlugin != nullptr,);
  2205. CARLA_SAFE_ASSERT_RETURN(fPlugin->pData != nullptr,);
  2206. carla_debug("CarlaPlugin::~ScopedSingleProcessLocker()");
  2207. if (! fBlock)
  2208. return;
  2209. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  2210. if (fPlugin->pData->singleMutex.wasTryLockCalled())
  2211. fPlugin->pData->needsReset = true;
  2212. #endif
  2213. fPlugin->pData->singleMutex.unlock();
  2214. }
  2215. // -------------------------------------------------------------------
  2216. CARLA_BACKEND_END_NAMESPACE