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 82KB

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