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

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
10 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
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397
  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(getType()));
  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. for (LinkedList<CustomData>::Itenerator it = pData->custom.begin2(); it.valid(); it.next())
  481. {
  482. const CustomData& cData(it.getValue(kCustomDataFallback));
  483. CARLA_SAFE_ASSERT_CONTINUE(cData.isValid());
  484. CarlaStateSave::CustomData* stateCustomData(new CarlaStateSave::CustomData());
  485. stateCustomData->type = carla_strdup(cData.type);
  486. stateCustomData->key = carla_strdup(cData.key);
  487. stateCustomData->value = carla_strdup(cData.value);
  488. pData->stateSave.customData.append(stateCustomData);
  489. }
  490. return pData->stateSave;
  491. }
  492. void CarlaPlugin::loadStateSave(const CarlaStateSave& stateSave)
  493. {
  494. char strBuf[STR_MAX+1];
  495. const bool usesMultiProgs(pData->hints & PLUGIN_USES_MULTI_PROGS);
  496. const PluginType pluginType(getType());
  497. // ---------------------------------------------------------------
  498. // Part 1 - PRE-set custom data (only those which reload programs)
  499. for (CarlaStateSave::CustomDataItenerator it = stateSave.customData.begin2(); it.valid(); it.next())
  500. {
  501. const CarlaStateSave::CustomData* const stateCustomData(it.getValue(nullptr));
  502. CARLA_SAFE_ASSERT_CONTINUE(stateCustomData != nullptr);
  503. CARLA_SAFE_ASSERT_CONTINUE(stateCustomData->isValid());
  504. const char* const key(stateCustomData->key);
  505. /**/ if (pluginType == PLUGIN_DSSI && (std::strcmp (key, "reloadprograms") == 0 ||
  506. std::strcmp (key, "load" ) == 0 ||
  507. std::strncmp(key, "patches", 7) == 0 ))
  508. pass();
  509. else if (usesMultiProgs && std::strcmp(key, "midiPrograms") == 0)
  510. pass();
  511. else
  512. continue;
  513. setCustomData(stateCustomData->type, key, stateCustomData->value, true);
  514. }
  515. // ---------------------------------------------------------------
  516. // Part 2 - set program
  517. if (stateSave.currentProgramIndex >= 0 && stateSave.currentProgramName != nullptr)
  518. {
  519. int32_t programId = -1;
  520. // index < count
  521. if (stateSave.currentProgramIndex < static_cast<int32_t>(pData->prog.count))
  522. {
  523. programId = stateSave.currentProgramIndex;
  524. }
  525. // index not valid, try to find by name
  526. else
  527. {
  528. for (uint32_t i=0; i < pData->prog.count; ++i)
  529. {
  530. strBuf[0] = '\0';
  531. getProgramName(i, strBuf);
  532. if (strBuf[0] != '\0' && std::strcmp(stateSave.currentProgramName, strBuf) == 0)
  533. {
  534. programId = static_cast<int32_t>(i);
  535. break;
  536. }
  537. }
  538. }
  539. // set program now, if valid
  540. if (programId >= 0)
  541. setProgram(programId, true, true, true);
  542. }
  543. // ---------------------------------------------------------------
  544. // Part 3 - set midi program
  545. if (stateSave.currentMidiBank >= 0 && stateSave.currentMidiProgram >= 0 && ! usesMultiProgs)
  546. setMidiProgramById(static_cast<uint32_t>(stateSave.currentMidiBank), static_cast<uint32_t>(stateSave.currentMidiProgram), true, true, true);
  547. // ---------------------------------------------------------------
  548. // Part 4a - get plugin parameter symbols
  549. LinkedList<ParamSymbol*> paramSymbols;
  550. if (pluginType == PLUGIN_LADSPA || pluginType == PLUGIN_LV2)
  551. {
  552. for (uint32_t i=0; i < pData->param.count; ++i)
  553. {
  554. strBuf[0] = '\0';
  555. getParameterSymbol(i, strBuf);
  556. if (strBuf[0] != '\0')
  557. {
  558. ParamSymbol* const paramSymbol(new ParamSymbol(i, strBuf));
  559. paramSymbols.append(paramSymbol);
  560. }
  561. }
  562. }
  563. // ---------------------------------------------------------------
  564. // Part 4b - set parameter values (carefully)
  565. const float sampleRate(static_cast<float>(pData->engine->getSampleRate()));
  566. for (CarlaStateSave::ParameterItenerator it = stateSave.parameters.begin2(); it.valid(); it.next())
  567. {
  568. CarlaStateSave::Parameter* const stateParameter(it.getValue(nullptr));
  569. CARLA_SAFE_ASSERT_CONTINUE(stateParameter != nullptr);
  570. int32_t index = -1;
  571. if (pluginType == PLUGIN_LADSPA)
  572. {
  573. // Try to set by symbol, otherwise use index
  574. if (stateParameter->symbol != nullptr && stateParameter->symbol[0] != '\0')
  575. {
  576. for (LinkedList<ParamSymbol*>::Itenerator it2 = paramSymbols.begin2(); it2.valid(); it2.next())
  577. {
  578. ParamSymbol* const paramSymbol(it2.getValue(nullptr));
  579. CARLA_SAFE_ASSERT_CONTINUE(paramSymbol != nullptr);
  580. CARLA_SAFE_ASSERT_CONTINUE(paramSymbol->symbol != nullptr);
  581. if (std::strcmp(stateParameter->symbol, paramSymbol->symbol) == 0)
  582. {
  583. index = paramSymbol->index;
  584. break;
  585. }
  586. }
  587. if (index == -1)
  588. index = stateParameter->index;
  589. }
  590. else
  591. index = stateParameter->index;
  592. }
  593. else if (pluginType == PLUGIN_LV2)
  594. {
  595. // Symbol only
  596. if (stateParameter->symbol != nullptr && stateParameter->symbol[0] != '\0')
  597. {
  598. for (LinkedList<ParamSymbol*>::Itenerator it2 = paramSymbols.begin2(); it2.valid(); it2.next())
  599. {
  600. ParamSymbol* const paramSymbol(it2.getValue(nullptr));
  601. CARLA_SAFE_ASSERT_CONTINUE(paramSymbol != nullptr);
  602. CARLA_SAFE_ASSERT_CONTINUE(paramSymbol->symbol != nullptr);
  603. if (std::strcmp(stateParameter->symbol, paramSymbol->symbol) == 0)
  604. {
  605. index = paramSymbol->index;
  606. break;
  607. }
  608. }
  609. if (index == -1)
  610. carla_stderr("Failed to find LV2 parameter symbol '%s')", stateParameter->symbol);
  611. }
  612. else
  613. carla_stderr("LV2 Plugin parameter '%s' has no symbol", stateParameter->name);
  614. }
  615. else
  616. {
  617. // Index only
  618. index = stateParameter->index;
  619. }
  620. // Now set parameter
  621. if (index >= 0 && index < static_cast<int32_t>(pData->param.count))
  622. {
  623. //CARLA_SAFE_ASSERT(stateParameter->isInput == (pData
  624. if (! stateParameter->dummy)
  625. {
  626. if (pData->param.data[index].hints & PARAMETER_USES_SAMPLERATE)
  627. stateParameter->value *= sampleRate;
  628. setParameterValue(static_cast<uint32_t>(index), stateParameter->value, true, true, true);
  629. }
  630. #ifndef BUILD_BRIDGE
  631. setParameterMidiCC(static_cast<uint32_t>(index), stateParameter->midiCC, true, true);
  632. setParameterMidiChannel(static_cast<uint32_t>(index), stateParameter->midiChannel, true, true);
  633. #endif
  634. }
  635. else
  636. carla_stderr("Could not set parameter data for '%s'", stateParameter->name);
  637. }
  638. // ---------------------------------------------------------------
  639. // Part 4c - clear
  640. for (LinkedList<ParamSymbol*>::Itenerator it = paramSymbols.begin2(); it.valid(); it.next())
  641. {
  642. ParamSymbol* const paramSymbol(it.getValue(nullptr));
  643. delete paramSymbol;
  644. }
  645. paramSymbols.clear();
  646. // ---------------------------------------------------------------
  647. // Part 5 - set custom data
  648. for (CarlaStateSave::CustomDataItenerator it = stateSave.customData.begin2(); it.valid(); it.next())
  649. {
  650. const CarlaStateSave::CustomData* const stateCustomData(it.getValue(nullptr));
  651. CARLA_SAFE_ASSERT_CONTINUE(stateCustomData != nullptr);
  652. CARLA_SAFE_ASSERT_CONTINUE(stateCustomData->isValid());
  653. const char* const key(stateCustomData->key);
  654. if (pluginType == PLUGIN_DSSI && (std::strcmp (key, "reloadprograms") == 0 ||
  655. std::strcmp (key, "load" ) == 0 ||
  656. std::strncmp(key, "patches", 7) == 0 ))
  657. continue;
  658. if (usesMultiProgs && std::strcmp(key, "midiPrograms") == 0)
  659. continue;
  660. setCustomData(stateCustomData->type, key, stateCustomData->value, true);
  661. }
  662. // ---------------------------------------------------------------
  663. // Part 5x - set lv2 state
  664. if (pluginType == PLUGIN_LV2 && pData->custom.count() > 0)
  665. setCustomData(CUSTOM_DATA_TYPE_STRING, "CarlaLoadLv2StateNow", "true", true);
  666. // ---------------------------------------------------------------
  667. // Part 6 - set chunk
  668. if (stateSave.chunk != nullptr && (pData->options & PLUGIN_OPTION_USE_CHUNKS) != 0)
  669. {
  670. std::vector<uint8_t> chunk(carla_getChunkFromBase64String(stateSave.chunk));
  671. #ifdef CARLA_PROPER_CPP11_SUPPORT
  672. setChunkData(chunk.data(), chunk.size());
  673. #else
  674. setChunkData(&chunk.front(), chunk.size());
  675. #endif
  676. }
  677. #ifndef BUILD_BRIDGE
  678. // ---------------------------------------------------------------
  679. // Part 6 - set internal stuff
  680. const uint availOptions(getOptionsAvailable());
  681. for (uint i=0; i<10; ++i) // FIXME - get this value somehow...
  682. {
  683. const uint option(1u << i);
  684. if (availOptions & option)
  685. setOption(option, (stateSave.options & option) != 0, true);
  686. }
  687. setDryWet(stateSave.dryWet, true, true);
  688. setVolume(stateSave.volume, true, true);
  689. setBalanceLeft(stateSave.balanceLeft, true, true);
  690. setBalanceRight(stateSave.balanceRight, true, true);
  691. setPanning(stateSave.panning, true, true);
  692. setCtrlChannel(stateSave.ctrlChannel, true, true);
  693. setActive(stateSave.active, true, true);
  694. #endif
  695. pData->engine->callback(ENGINE_CALLBACK_UPDATE, pData->id, 0, 0, 0.0f, nullptr);
  696. }
  697. bool CarlaPlugin::saveStateToFile(const char* const filename)
  698. {
  699. CARLA_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', false);
  700. carla_debug("CarlaPlugin::saveStateToFile(\"%s\")", filename);
  701. MemoryOutputStream out, streamState;
  702. getStateSave().dumpToMemoryStream(streamState);
  703. out << "<?xml version='1.0' encoding='UTF-8'?>\n";
  704. out << "<!DOCTYPE CARLA-PRESET>\n";
  705. out << "<CARLA-PRESET VERSION='2.0'>\n";
  706. out << streamState;
  707. out << "</CARLA-PRESET>\n";
  708. const String jfilename = String(CharPointer_UTF8(filename));
  709. File file(jfilename);
  710. if (file.replaceWithData(out.getData(), out.getDataSize()))
  711. return true;
  712. pData->engine->setLastError("Failed to write file");
  713. return false;
  714. }
  715. bool CarlaPlugin::loadStateFromFile(const char* const filename)
  716. {
  717. // TODO set errors
  718. CARLA_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', false);
  719. carla_debug("CarlaPlugin::loadStateFromFile(\"%s\")", filename);
  720. const String jfilename = String(CharPointer_UTF8(filename));
  721. File file(jfilename);
  722. CARLA_SAFE_ASSERT_RETURN(file.existsAsFile(), false);
  723. XmlDocument xml(file);
  724. ScopedPointer<XmlElement> xmlElement(xml.getDocumentElement(true));
  725. CARLA_SAFE_ASSERT_RETURN(xmlElement != nullptr, false);
  726. CARLA_SAFE_ASSERT_RETURN(xmlElement->getTagName().equalsIgnoreCase("carla-preset"), false);
  727. // completely load file
  728. xmlElement = xml.getDocumentElement(false);
  729. CARLA_SAFE_ASSERT_RETURN(xmlElement != nullptr, false);
  730. if (pData->stateSave.fillFromXmlElement(xmlElement))
  731. {
  732. loadStateSave(pData->stateSave);
  733. return true;
  734. }
  735. return false;
  736. }
  737. bool CarlaPlugin::exportAsLV2(const char* const lv2path)
  738. {
  739. CARLA_SAFE_ASSERT_RETURN(lv2path != nullptr && lv2path[0] != '\0', false);
  740. carla_debug("CarlaPlugin::exportAsLV2(\"%s\")", lv2path);
  741. CarlaString bundlepath(lv2path);
  742. if (! bundlepath.endsWith(".lv2"))
  743. bundlepath += ".lv2";
  744. const File bundlefolder(bundlepath.buffer());
  745. if (bundlefolder.existsAsFile())
  746. {
  747. pData->engine->setLastError("Requested filename already exists as file, use a folder instead");
  748. return false;
  749. }
  750. if (! bundlefolder.exists())
  751. {
  752. const Result res(bundlefolder.createDirectory());
  753. if (res.failed())
  754. {
  755. pData->engine->setLastError(res.getErrorMessage().toRawUTF8());
  756. return false;
  757. }
  758. }
  759. CarlaString symbol(pData->name);
  760. symbol.toBasic();
  761. {
  762. const CarlaString pluginFilename(bundlepath + CARLA_OS_SEP_STR + symbol + ".xml");
  763. if (! saveStateToFile(pluginFilename))
  764. return false;
  765. }
  766. {
  767. MemoryOutputStream manifestStream;
  768. manifestStream << "@prefix lv2: <http://lv2plug.in/ns/lv2core#> .\n";
  769. manifestStream << "@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n";
  770. manifestStream << "@prefix ui: <http://lv2plug.in/ns/extensions/ui#> .\n";
  771. manifestStream << "\n";
  772. manifestStream << "<" << symbol.buffer() << ".ttl>\n";
  773. manifestStream << " a lv2:Plugin ;\n";
  774. manifestStream << " lv2:binary <" << symbol.buffer() << CARLA_LIB_EXT "> ;\n";
  775. manifestStream << " rdfs:seeAlso <" << symbol.buffer() << ".ttl> .\n";
  776. manifestStream << "\n";
  777. manifestStream << "<ext-ui>\n";
  778. manifestStream << " a <http://kxstudio.sf.net/ns/lv2ext/external-ui#Widget> ;\n";
  779. manifestStream << " ui:binary <" << symbol.buffer() << CARLA_LIB_EXT "> ;\n";
  780. manifestStream << " lv2:extensionData <http://lv2plug.in/ns/extensions/ui#idleInterface> ,\n";
  781. manifestStream << " <http://lv2plug.in/ns/extensions/ui#showInterface> ;\n";
  782. manifestStream << " lv2:requiredFeature <http://lv2plug.in/ns/ext/instance-access> .\n";
  783. manifestStream << "\n";
  784. const CarlaString manifestFilename(bundlepath + CARLA_OS_SEP_STR "manifest.ttl");
  785. const File manifestFile(manifestFilename.buffer());
  786. if (! manifestFile.replaceWithData(manifestStream.getData(), manifestStream.getDataSize()))
  787. {
  788. pData->engine->setLastError("Failed to write manifest.ttl file");
  789. return false;
  790. }
  791. }
  792. {
  793. MemoryOutputStream mainStream;
  794. mainStream << "@prefix atom: <http://lv2plug.in/ns/ext/atom#> .\n";
  795. mainStream << "@prefix doap: <http://usefulinc.com/ns/doap#> .\n";
  796. mainStream << "@prefix lv2: <http://lv2plug.in/ns/lv2core#> .\n";
  797. mainStream << "@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n";
  798. mainStream << "@prefix ui: <http://lv2plug.in/ns/extensions/ui#> .\n";
  799. mainStream << "\n";
  800. mainStream << "<>\n";
  801. mainStream << " a lv2:Plugin ;\n";
  802. mainStream << "\n";
  803. mainStream << " lv2:requiredFeature <http://lv2plug.in/ns/ext/buf-size#boundedBlockLength> ,\n";
  804. mainStream << " <http://lv2plug.in/ns/ext/options#options> ,\n";
  805. mainStream << " <http://lv2plug.in/ns/ext/urid#map> ;\n";
  806. mainStream << "\n";
  807. if (pData->hints & PLUGIN_HAS_CUSTOM_UI)
  808. {
  809. mainStream << " ui:ui <ext-ui> ;\n";
  810. mainStream << "\n";
  811. }
  812. const uint32_t midiIns = getMidiInCount();
  813. const uint32_t midiOuts = getMidiOutCount();
  814. int portIndex = 0;
  815. if (midiIns > 0)
  816. {
  817. mainStream << " lv2:port [\n";
  818. mainStream << " a lv2:InputPort, atom:AtomPort ;\n";
  819. mainStream << " lv2:index 0 ;\n";
  820. mainStream << " lv2:symbol \"clv2_events_in\" ;\n";
  821. mainStream << " lv2:name \"Events Input\" ;\n";
  822. mainStream << " atom:bufferType atom:Sequence ;\n";
  823. mainStream << " atom:supports <http://lv2plug.in/ns/ext/midi#MidiEvent> ,\n";
  824. mainStream << " <http://lv2plug.in/ns/ext/time#Position> ;\n";
  825. mainStream << " ] ;\n";
  826. ++portIndex;
  827. for (uint32_t i=1; i<midiIns; ++i)
  828. {
  829. const String portIndexNum(portIndex++);
  830. const String portIndexLabel(portIndex);
  831. mainStream << " lv2:port [\n";
  832. mainStream << " a lv2:InputPort, atom:AtomPort ;\n";
  833. mainStream << " lv2:index " << portIndexNum << " ;\n";
  834. mainStream << " lv2:symbol \"clv2_midi_in_" << portIndexLabel << "\" ;\n";
  835. mainStream << " lv2:name \"MIDI Input " << portIndexLabel << "\" ;\n";
  836. mainStream << " ] ;\n";
  837. }
  838. }
  839. else
  840. {
  841. mainStream << " lv2:port [\n";
  842. mainStream << " a lv2:InputPort, atom:AtomPort ;\n";
  843. mainStream << " lv2:index 0 ;\n";
  844. mainStream << " lv2:symbol \"clv2_time_info\" ;\n";
  845. mainStream << " lv2:name \"Time Info\" ;\n";
  846. mainStream << " atom:bufferType atom:Sequence ;\n";
  847. mainStream << " atom:supports <http://lv2plug.in/ns/ext/time#Position> ;\n";
  848. mainStream << " ] ;\n";
  849. ++portIndex;
  850. }
  851. for (uint32_t i=0; i<midiOuts; ++i)
  852. {
  853. const String portIndexNum(portIndex++);
  854. const String portIndexLabel(portIndex);
  855. mainStream << " lv2:port [\n";
  856. mainStream << " a lv2:InputPort, atom:AtomPort ;\n";
  857. mainStream << " lv2:index " << portIndexNum << " ;\n";
  858. mainStream << " lv2:symbol \"clv2_midi_out_" << portIndexLabel << "\" ;\n";
  859. mainStream << " lv2:name \"MIDI Output " << portIndexLabel << "\" ;\n";
  860. mainStream << " atom:bufferType atom:Sequence ;\n";
  861. mainStream << " atom:supports <http://lv2plug.in/ns/ext/midi#MidiEvent> ;\n";
  862. mainStream << " ] ;\n";
  863. }
  864. mainStream << " lv2:port [\n";
  865. mainStream << " a lv2:InputPort, lv2:ControlPort ;\n";
  866. mainStream << " lv2:index " << String(portIndex++) << " ;\n";
  867. mainStream << " lv2:name \"freewheel\" ;\n";
  868. mainStream << " lv2:symbol \"clv2_freewheel\" ;\n";
  869. mainStream << " lv2:default 0 ;\n";
  870. mainStream << " lv2:minimum 0 ;\n";
  871. mainStream << " lv2:maximum 1 ;\n";
  872. mainStream << " lv2:designation lv2:freeWheeling ;\n";
  873. mainStream << " lv2:portProperty lv2:toggled , lv2:integer ;\n";
  874. mainStream << " lv2:portProperty <http://lv2plug.in/ns/ext/port-props#notOnGUI> ;\n";
  875. mainStream << " ] ;\n";
  876. for (uint32_t i=0; i<pData->audioIn.count; ++i)
  877. {
  878. const String portIndexNum(portIndex++);
  879. const String portIndexLabel(i+1);
  880. mainStream << " lv2:port [\n";
  881. mainStream << " a lv2:InputPort, lv2:AudioPort ;\n";
  882. mainStream << " lv2:index " << portIndexNum << " ;\n";
  883. mainStream << " lv2:symbol \"clv2_audio_in_" << portIndexLabel << "\" ;\n";
  884. mainStream << " lv2:name \"Audio Input " << portIndexLabel << "\" ;\n";
  885. mainStream << " ] ;\n";
  886. }
  887. for (uint32_t i=0; i<pData->audioOut.count; ++i)
  888. {
  889. const String portIndexNum(portIndex++);
  890. const String portIndexLabel(i+1);
  891. mainStream << " lv2:port [\n";
  892. mainStream << " a lv2:OutputPort, lv2:AudioPort ;\n";
  893. mainStream << " lv2:index " << portIndexNum << " ;\n";
  894. mainStream << " lv2:symbol \"clv2_audio_out_" << portIndexLabel << "\" ;\n";
  895. mainStream << " lv2:name \"Audio Output " << portIndexLabel << "\" ;\n";
  896. mainStream << " ] ;\n";
  897. }
  898. CarlaStringList uniqueSymbolNames;
  899. char strBufName[STR_MAX+1];
  900. char strBufSymbol[STR_MAX+1];
  901. strBufName[STR_MAX] = strBufSymbol[STR_MAX] = '\0';
  902. for (uint32_t i=0; i<pData->param.count; ++i)
  903. {
  904. const ParameterData& paramData(pData->param.data[i]);
  905. const ParameterRanges& paramRanges(pData->param.ranges[i]);
  906. const String portIndexNum(portIndex++);
  907. mainStream << " lv2:port [\n";
  908. if (paramData.type == PARAMETER_INPUT)
  909. mainStream << " a lv2:InputPort, lv2:ControlPort ;\n";
  910. else
  911. mainStream << " a lv2:OutputPort, lv2:ControlPort ;\n";
  912. if (paramData.hints & PARAMETER_IS_BOOLEAN)
  913. mainStream << " lv2:portProperty lv2:toggled ;\n";
  914. if (paramData.hints & PARAMETER_IS_INTEGER)
  915. mainStream << " lv2:portProperty lv2:integer ;\n";
  916. // TODO logarithmic, enabled (not on gui), automable, samplerate, scalepoints
  917. strBufName[0] = strBufSymbol[0] = '\0';
  918. getParameterName(i, strBufName);
  919. getParameterSymbol(i, strBufSymbol);
  920. if (strBufSymbol[0] == '\0')
  921. {
  922. CarlaString s(strBufName);
  923. s.toBasic();
  924. std::memcpy(strBufSymbol, s.buffer(), s.length()+1);
  925. if (strBufSymbol[0] >= '0' && strBufSymbol[0] <= '9')
  926. {
  927. const size_t len(std::strlen(strBufSymbol));
  928. std::memmove(strBufSymbol+1, strBufSymbol, len);
  929. strBufSymbol[0] = '_';
  930. strBufSymbol[len+1] = '\0';
  931. }
  932. }
  933. if (uniqueSymbolNames.contains(strBufSymbol))
  934. {
  935. std::snprintf(strBufSymbol, STR_MAX, "clv2_param_%d", i+1);
  936. strBufSymbol[STR_MAX] = '\0';
  937. }
  938. mainStream << " lv2:index " << portIndexNum << " ;\n";
  939. mainStream << " lv2:symbol \"" << strBufSymbol << "\" ;\n";
  940. mainStream << " lv2:name \"\"\"" << strBufName << "\"\"\" ;\n";
  941. mainStream << " lv2:default " << String(paramRanges.def) << " ;\n";
  942. mainStream << " lv2:minimum " << String(paramRanges.min) << " ;\n";
  943. mainStream << " lv2:maximum " << String(paramRanges.max) << " ;\n";
  944. // TODO midiCC, midiChannel
  945. mainStream << " ] ;\n";
  946. }
  947. mainStream << " rdfs:comment \"Plugin generated using Carla LV2 export.\" ;\n";
  948. mainStream << " doap:name \"\"\"" << getName() << "\"\"\" .\n";
  949. mainStream << "\n";
  950. const CarlaString mainFilename(bundlepath + CARLA_OS_SEP_STR + symbol + ".ttl");
  951. const File mainFile(mainFilename.buffer());
  952. if (! mainFile.replaceWithData(mainStream.getData(), mainStream.getDataSize()))
  953. {
  954. pData->engine->setLastError("Failed to write main plugin ttl file");
  955. return false;
  956. }
  957. }
  958. const CarlaString binaryFilename(bundlepath + CARLA_OS_SEP_STR + symbol + CARLA_LIB_EXT);
  959. const File binaryFileSource(File::getSpecialLocation(File::currentExecutableFile).getSiblingFile("carla-bridge-lv2" CARLA_LIB_EXT));
  960. const File binaryFileTarget(binaryFilename.buffer());
  961. if (! binaryFileSource.createSymbolicLink(binaryFileTarget, true))
  962. {
  963. pData->engine->setLastError("Failed to create symbolik link of plugin binary");
  964. return false;
  965. }
  966. const EngineOptions& opts(pData->engine->getOptions());
  967. const CarlaString binFolderTarget(bundlepath + CARLA_OS_SEP_STR + "bin");
  968. const CarlaString resFolderTarget(bundlepath + CARLA_OS_SEP_STR + "res");
  969. File(opts.binaryDir).createSymbolicLink(File(binFolderTarget.buffer()), true);
  970. File(opts.resourceDir).createSymbolicLink(File(resFolderTarget.buffer()), true);
  971. return true;
  972. }
  973. // -------------------------------------------------------------------
  974. // Set data (internal stuff)
  975. void CarlaPlugin::setId(const uint newId) noexcept
  976. {
  977. pData->id = newId;
  978. }
  979. void CarlaPlugin::setName(const char* const newName)
  980. {
  981. CARLA_SAFE_ASSERT_RETURN(newName != nullptr && newName[0] != '\0',);
  982. if (pData->name != nullptr)
  983. delete[] pData->name;
  984. pData->name = carla_strdup(newName);
  985. }
  986. void CarlaPlugin::setOption(const uint option, const bool yesNo, const bool sendCallback)
  987. {
  988. CARLA_SAFE_ASSERT_RETURN(getOptionsAvailable() & option,);
  989. if (yesNo)
  990. pData->options |= option;
  991. else
  992. pData->options &= ~option;
  993. #ifndef BUILD_BRIDGE
  994. if (sendCallback)
  995. pData->engine->callback(ENGINE_CALLBACK_OPTION_CHANGED, pData->id, static_cast<int>(option), yesNo ? 1 : 0, 0.0f, nullptr);
  996. #else
  997. // unused
  998. return; (void)sendCallback;
  999. #endif
  1000. }
  1001. void CarlaPlugin::setEnabled(const bool yesNo) noexcept
  1002. {
  1003. if (pData->enabled == yesNo)
  1004. return;
  1005. pData->masterMutex.lock();
  1006. pData->enabled = yesNo;
  1007. if (yesNo && ! pData->client->isActive())
  1008. pData->client->activate();
  1009. pData->masterMutex.unlock();
  1010. }
  1011. void CarlaPlugin::setActive(const bool active, const bool sendOsc, const bool sendCallback) noexcept
  1012. {
  1013. #ifndef BUILD_BRIDGE
  1014. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  1015. #endif
  1016. if (pData->active == active)
  1017. return;
  1018. {
  1019. const ScopedSingleProcessLocker spl(this, true);
  1020. if (active)
  1021. activate();
  1022. else
  1023. deactivate();
  1024. }
  1025. pData->active = active;
  1026. #ifndef BUILD_BRIDGE
  1027. const float value(active ? 1.0f : 0.0f);
  1028. # ifdef HAVE_LIBLO
  1029. if (sendOsc && pData->engine->isOscControlRegistered())
  1030. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_ACTIVE, value);
  1031. # endif
  1032. if (sendCallback)
  1033. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_ACTIVE, 0, value, nullptr);
  1034. #endif
  1035. // may be unused
  1036. return; (void)sendOsc; (void)sendCallback;
  1037. }
  1038. #ifndef BUILD_BRIDGE
  1039. void CarlaPlugin::setDryWet(const float value, const bool sendOsc, const bool sendCallback) noexcept
  1040. {
  1041. CARLA_SAFE_ASSERT(value >= 0.0f && value <= 1.0f);
  1042. const float fixedValue(carla_fixedValue<float>(0.0f, 1.0f, value));
  1043. if (carla_isEqual(pData->postProc.dryWet, fixedValue))
  1044. return;
  1045. pData->postProc.dryWet = fixedValue;
  1046. #ifdef HAVE_LIBLO
  1047. if (sendOsc && pData->engine->isOscControlRegistered())
  1048. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_DRYWET, fixedValue);
  1049. #endif
  1050. if (sendCallback)
  1051. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_DRYWET, 0, fixedValue, nullptr);
  1052. // may be unused
  1053. return; (void)sendOsc;
  1054. }
  1055. void CarlaPlugin::setVolume(const float value, const bool sendOsc, const bool sendCallback) noexcept
  1056. {
  1057. CARLA_SAFE_ASSERT(value >= 0.0f && value <= 1.27f);
  1058. const float fixedValue(carla_fixedValue<float>(0.0f, 1.27f, value));
  1059. if (carla_isEqual(pData->postProc.volume, fixedValue))
  1060. return;
  1061. pData->postProc.volume = fixedValue;
  1062. #ifdef HAVE_LIBLO
  1063. if (sendOsc && pData->engine->isOscControlRegistered())
  1064. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_VOLUME, fixedValue);
  1065. #endif
  1066. if (sendCallback)
  1067. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_VOLUME, 0, fixedValue, nullptr);
  1068. // may be unused
  1069. return; (void)sendOsc;
  1070. }
  1071. void CarlaPlugin::setBalanceLeft(const float value, const bool sendOsc, const bool sendCallback) noexcept
  1072. {
  1073. CARLA_SAFE_ASSERT(value >= -1.0f && value <= 1.0f);
  1074. const float fixedValue(carla_fixedValue<float>(-1.0f, 1.0f, value));
  1075. if (carla_isEqual(pData->postProc.balanceLeft, fixedValue))
  1076. return;
  1077. pData->postProc.balanceLeft = fixedValue;
  1078. #ifdef HAVE_LIBLO
  1079. if (sendOsc && pData->engine->isOscControlRegistered())
  1080. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_BALANCE_LEFT, fixedValue);
  1081. #endif
  1082. if (sendCallback)
  1083. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_BALANCE_LEFT, 0, fixedValue, nullptr);
  1084. // may be unused
  1085. return; (void)sendOsc;
  1086. }
  1087. void CarlaPlugin::setBalanceRight(const float value, const bool sendOsc, const bool sendCallback) noexcept
  1088. {
  1089. CARLA_SAFE_ASSERT(value >= -1.0f && value <= 1.0f);
  1090. const float fixedValue(carla_fixedValue<float>(-1.0f, 1.0f, value));
  1091. if (carla_isEqual(pData->postProc.balanceRight, fixedValue))
  1092. return;
  1093. pData->postProc.balanceRight = fixedValue;
  1094. #ifdef HAVE_LIBLO
  1095. if (sendOsc && pData->engine->isOscControlRegistered())
  1096. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_BALANCE_RIGHT, fixedValue);
  1097. #endif
  1098. if (sendCallback)
  1099. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_BALANCE_RIGHT, 0, fixedValue, nullptr);
  1100. // may be unused
  1101. return; (void)sendOsc;
  1102. }
  1103. void CarlaPlugin::setPanning(const float value, const bool sendOsc, const bool sendCallback) noexcept
  1104. {
  1105. CARLA_SAFE_ASSERT(value >= -1.0f && value <= 1.0f);
  1106. const float fixedValue(carla_fixedValue<float>(-1.0f, 1.0f, value));
  1107. if (carla_isEqual(pData->postProc.panning, fixedValue))
  1108. return;
  1109. pData->postProc.panning = fixedValue;
  1110. #ifdef HAVE_LIBLO
  1111. if (sendOsc && pData->engine->isOscControlRegistered())
  1112. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_PANNING, fixedValue);
  1113. #endif
  1114. if (sendCallback)
  1115. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_PANNING, 0, fixedValue, nullptr);
  1116. // may be unused
  1117. return; (void)sendOsc;
  1118. }
  1119. #endif // ! BUILD_BRIDGE
  1120. void CarlaPlugin::setCtrlChannel(const int8_t channel, const bool sendOsc, const bool sendCallback) noexcept
  1121. {
  1122. #ifndef BUILD_BRIDGE
  1123. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  1124. #endif
  1125. CARLA_SAFE_ASSERT_RETURN(channel >= -1 && channel < MAX_MIDI_CHANNELS,);
  1126. if (pData->ctrlChannel == channel)
  1127. return;
  1128. pData->ctrlChannel = channel;
  1129. #ifndef BUILD_BRIDGE
  1130. const float channelf(channel);
  1131. # ifdef HAVE_LIBLO
  1132. if (sendOsc && pData->engine->isOscControlRegistered())
  1133. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_CTRL_CHANNEL, channelf);
  1134. # endif
  1135. if (sendCallback)
  1136. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_CTRL_CHANNEL, 0, channelf, nullptr);
  1137. #endif
  1138. // may be unused
  1139. return; (void)sendOsc; (void)sendCallback;
  1140. }
  1141. // -------------------------------------------------------------------
  1142. // Set data (plugin-specific stuff)
  1143. void CarlaPlugin::setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept
  1144. {
  1145. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  1146. if (sendGui && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1147. uiParameterChange(parameterId, value);
  1148. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1149. if (sendOsc && pData->engine->isOscControlRegistered())
  1150. pData->engine->oscSend_control_set_parameter_value(pData->id, static_cast<int32_t>(parameterId), value);
  1151. #endif
  1152. if (sendCallback)
  1153. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, static_cast<int>(parameterId), 0, value, nullptr);
  1154. // may be unused
  1155. return; (void)sendOsc;
  1156. }
  1157. void CarlaPlugin::setParameterValueByRealIndex(const int32_t rindex, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept
  1158. {
  1159. #ifndef BUILD_BRIDGE
  1160. CARLA_SAFE_ASSERT_RETURN(rindex > PARAMETER_MAX && rindex != PARAMETER_NULL,);
  1161. switch (rindex)
  1162. {
  1163. case PARAMETER_ACTIVE:
  1164. return setActive((value > 0.0f), sendOsc, sendCallback);
  1165. case PARAMETER_CTRL_CHANNEL:
  1166. return setCtrlChannel(int8_t(value), sendOsc, sendCallback);
  1167. case PARAMETER_DRYWET:
  1168. return setDryWet(value, sendOsc, sendCallback);
  1169. case PARAMETER_VOLUME:
  1170. return setVolume(value, sendOsc, sendCallback);
  1171. case PARAMETER_BALANCE_LEFT:
  1172. return setBalanceLeft(value, sendOsc, sendCallback);
  1173. case PARAMETER_BALANCE_RIGHT:
  1174. return setBalanceRight(value, sendOsc, sendCallback);
  1175. case PARAMETER_PANNING:
  1176. return setPanning(value, sendOsc, sendCallback);
  1177. }
  1178. #endif
  1179. CARLA_SAFE_ASSERT_RETURN(rindex >= 0,);
  1180. for (uint32_t i=0; i < pData->param.count; ++i)
  1181. {
  1182. if (pData->param.data[i].rindex == rindex)
  1183. {
  1184. //if (carla_isNotEqual(getParameterValue(i), value))
  1185. setParameterValue(i, value, sendGui, sendOsc, sendCallback);
  1186. break;
  1187. }
  1188. }
  1189. }
  1190. void CarlaPlugin::setParameterMidiChannel(const uint32_t parameterId, const uint8_t channel, const bool sendOsc, const bool sendCallback) noexcept
  1191. {
  1192. #ifndef BUILD_BRIDGE
  1193. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  1194. #endif
  1195. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  1196. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1197. pData->param.data[parameterId].midiChannel = channel;
  1198. #ifndef BUILD_BRIDGE
  1199. # ifdef HAVE_LIBLO
  1200. if (sendOsc && pData->engine->isOscControlRegistered())
  1201. pData->engine->oscSend_control_set_parameter_midi_channel(pData->id, parameterId, channel);
  1202. # endif
  1203. if (sendCallback)
  1204. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_MIDI_CHANNEL_CHANGED, pData->id, static_cast<int>(parameterId), channel, 0.0f, nullptr);
  1205. #endif
  1206. // may be unused
  1207. return; (void)sendOsc; (void)sendCallback;
  1208. }
  1209. void CarlaPlugin::setParameterMidiCC(const uint32_t parameterId, const int16_t cc, const bool sendOsc, const bool sendCallback) noexcept
  1210. {
  1211. #ifndef BUILD_BRIDGE
  1212. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  1213. #endif
  1214. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  1215. CARLA_SAFE_ASSERT_RETURN(cc >= -1 && cc < MAX_MIDI_CONTROL,);
  1216. pData->param.data[parameterId].midiCC = cc;
  1217. #ifndef BUILD_BRIDGE
  1218. # ifdef HAVE_LIBLO
  1219. if (sendOsc && pData->engine->isOscControlRegistered())
  1220. pData->engine->oscSend_control_set_parameter_midi_cc(pData->id, parameterId, cc);
  1221. # endif
  1222. if (sendCallback)
  1223. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_MIDI_CC_CHANGED, pData->id, static_cast<int>(parameterId), cc, 0.0f, nullptr);
  1224. #endif
  1225. // may be unused
  1226. return; (void)sendOsc; (void)sendCallback;
  1227. }
  1228. void CarlaPlugin::setCustomData(const char* const type, const char* const key, const char* const value, const bool)
  1229. {
  1230. CARLA_SAFE_ASSERT_RETURN(type != nullptr && type[0] != '\0',);
  1231. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  1232. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  1233. // Ignore some keys
  1234. if (std::strcmp(type, CUSTOM_DATA_TYPE_STRING) == 0)
  1235. {
  1236. if (std::strncmp(key, "OSC:", 4) == 0 || std::strncmp(key, "CarlaAlternateFile", 18) == 0 || std::strcmp(key, "guiVisible") == 0)
  1237. return;
  1238. }
  1239. // Check if we already have this key
  1240. for (LinkedList<CustomData>::Itenerator it = pData->custom.begin2(); it.valid(); it.next())
  1241. {
  1242. CustomData& customData(it.getValue(kCustomDataFallbackNC));
  1243. CARLA_SAFE_ASSERT_CONTINUE(customData.isValid());
  1244. if (std::strcmp(customData.key, key) == 0)
  1245. {
  1246. if (customData.value != nullptr)
  1247. delete[] customData.value;
  1248. customData.value = carla_strdup(value);
  1249. return;
  1250. }
  1251. }
  1252. // Otherwise store it
  1253. CustomData customData;
  1254. customData.type = carla_strdup(type);
  1255. customData.key = carla_strdup(key);
  1256. customData.value = carla_strdup(value);
  1257. pData->custom.append(customData);
  1258. }
  1259. void CarlaPlugin::setChunkData(const void* const data, const std::size_t dataSize)
  1260. {
  1261. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  1262. CARLA_SAFE_ASSERT_RETURN(dataSize > 0,);
  1263. CARLA_SAFE_ASSERT(false); // this should never happen
  1264. }
  1265. void CarlaPlugin::setProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept
  1266. {
  1267. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->prog.count),);
  1268. pData->prog.current = index;
  1269. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1270. const bool reallySendOsc(sendOsc && pData->engine->isOscControlRegistered());
  1271. if (reallySendOsc && index < 50)
  1272. pData->engine->oscSend_control_set_current_program(pData->id, index);
  1273. #else
  1274. const bool reallySendOsc(false);
  1275. #endif
  1276. if (sendCallback)
  1277. pData->engine->callback(ENGINE_CALLBACK_PROGRAM_CHANGED, pData->id, index, 0, 0.0f, nullptr);
  1278. // Change default parameter values
  1279. if (index >= 0)
  1280. {
  1281. if (sendGui && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1282. uiProgramChange(static_cast<uint32_t>(index));
  1283. if (getType() == PLUGIN_GIG || getType() == PLUGIN_SF2 || getType() == PLUGIN_SFZ)
  1284. return;
  1285. pData->updateParameterValues(this, reallySendOsc, sendCallback, true);
  1286. }
  1287. // may be unused
  1288. return; (void)sendOsc;
  1289. }
  1290. void CarlaPlugin::setMidiProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept
  1291. {
  1292. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->midiprog.count),);
  1293. pData->midiprog.current = index;
  1294. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1295. const bool reallySendOsc(sendOsc && pData->engine->isOscControlRegistered());
  1296. if (reallySendOsc && index < 50)
  1297. pData->engine->oscSend_control_set_current_midi_program(pData->id, index);
  1298. #else
  1299. const bool reallySendOsc(false);
  1300. #endif
  1301. if (sendCallback)
  1302. pData->engine->callback(ENGINE_CALLBACK_MIDI_PROGRAM_CHANGED, pData->id, index, 0, 0.0f, nullptr);
  1303. if (index >= 0)
  1304. {
  1305. if (sendGui && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1306. uiMidiProgramChange(static_cast<uint32_t>(index));
  1307. if (getType() == PLUGIN_GIG || getType() == PLUGIN_SF2 || getType() == PLUGIN_SFZ)
  1308. return;
  1309. pData->updateParameterValues(this, reallySendOsc, sendCallback, true);
  1310. }
  1311. // may be unused
  1312. return; (void)sendOsc;
  1313. }
  1314. void CarlaPlugin::setMidiProgramById(const uint32_t bank, const uint32_t program, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept
  1315. {
  1316. for (uint32_t i=0; i < pData->midiprog.count; ++i)
  1317. {
  1318. if (pData->midiprog.data[i].bank == bank && pData->midiprog.data[i].program == program)
  1319. return setMidiProgram(static_cast<int32_t>(i), sendGui, sendOsc, sendCallback);
  1320. }
  1321. }
  1322. // -------------------------------------------------------------------
  1323. // Plugin state
  1324. void CarlaPlugin::reloadPrograms(const bool)
  1325. {
  1326. }
  1327. // -------------------------------------------------------------------
  1328. // Plugin processing
  1329. void CarlaPlugin::activate() noexcept
  1330. {
  1331. CARLA_SAFE_ASSERT(! pData->active);
  1332. }
  1333. void CarlaPlugin::deactivate() noexcept
  1334. {
  1335. CARLA_SAFE_ASSERT(pData->active);
  1336. }
  1337. void CarlaPlugin::bufferSizeChanged(const uint32_t)
  1338. {
  1339. }
  1340. void CarlaPlugin::sampleRateChanged(const double)
  1341. {
  1342. }
  1343. void CarlaPlugin::offlineModeChanged(const bool)
  1344. {
  1345. }
  1346. // -------------------------------------------------------------------
  1347. // Misc
  1348. void CarlaPlugin::idle()
  1349. {
  1350. if (! pData->enabled)
  1351. return;
  1352. const bool hasUI(pData->hints & PLUGIN_HAS_CUSTOM_UI);
  1353. const bool needsUiMainThread(pData->hints & PLUGIN_NEEDS_UI_MAIN_THREAD);
  1354. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1355. const bool sendOsc(pData->engine->isOscControlRegistered());
  1356. #endif
  1357. const uint32_t latency(getLatencyInFrames());
  1358. if (pData->latency.frames != latency)
  1359. {
  1360. carla_stdout("latency changed to %i samples", latency);
  1361. const ScopedSingleProcessLocker sspl(this, true);
  1362. pData->client->setLatency(latency);
  1363. #ifndef BUILD_BRIDGE
  1364. pData->latency.recreateBuffers(pData->latency.channels, latency);
  1365. #else
  1366. pData->latency.frames = latency;
  1367. #endif
  1368. }
  1369. const CarlaMutexLocker sl(pData->postRtEvents.mutex);
  1370. for (RtLinkedList<PluginPostRtEvent>::Itenerator it = pData->postRtEvents.data.begin2(); it.valid(); it.next())
  1371. {
  1372. const PluginPostRtEvent& event(it.getValue(kPluginPostRtEventFallback));
  1373. CARLA_SAFE_ASSERT_CONTINUE(event.type != kPluginPostRtEventNull);
  1374. switch (event.type)
  1375. {
  1376. case kPluginPostRtEventNull: {
  1377. } break;
  1378. case kPluginPostRtEventDebug: {
  1379. pData->engine->callback(ENGINE_CALLBACK_DEBUG, pData->id, event.value1, event.value2, event.value3, nullptr);
  1380. } break;
  1381. case kPluginPostRtEventParameterChange: {
  1382. // Update UI
  1383. if (event.value1 >= 0 && hasUI)
  1384. {
  1385. if (needsUiMainThread)
  1386. pData->postUiEvents.append(event);
  1387. else
  1388. uiParameterChange(static_cast<uint32_t>(event.value1), event.value3);
  1389. }
  1390. if (event.value2 != 1)
  1391. {
  1392. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1393. // Update OSC control client
  1394. if (sendOsc)
  1395. pData->engine->oscSend_control_set_parameter_value(pData->id, event.value1, event.value3);
  1396. #endif
  1397. // Update Host
  1398. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, event.value1, 0, event.value3, nullptr);
  1399. }
  1400. } break;
  1401. case kPluginPostRtEventProgramChange: {
  1402. // Update UI
  1403. if (event.value1 >= 0 && hasUI)
  1404. {
  1405. if (needsUiMainThread)
  1406. pData->postUiEvents.append(event);
  1407. else
  1408. uiProgramChange(static_cast<uint32_t>(event.value1));
  1409. }
  1410. // Update param values
  1411. for (uint32_t j=0; j < pData->param.count; ++j)
  1412. {
  1413. const float paramDefault(pData->param.ranges[j].def);
  1414. const float paramValue(getParameterValue(j));
  1415. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1416. if (sendOsc && j < 50)
  1417. {
  1418. pData->engine->oscSend_control_set_parameter_value(pData->id, static_cast<int32_t>(j), paramValue);
  1419. pData->engine->oscSend_control_set_default_value(pData->id, j, paramDefault);
  1420. }
  1421. #endif
  1422. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, static_cast<int>(j), 0, paramValue, nullptr);
  1423. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_DEFAULT_CHANGED, pData->id, static_cast<int>(j), 0, paramDefault, nullptr);
  1424. }
  1425. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1426. // Update OSC control client
  1427. if (sendOsc)
  1428. pData->engine->oscSend_control_set_current_program(pData->id, event.value1);
  1429. #endif
  1430. // Update Host
  1431. pData->engine->callback(ENGINE_CALLBACK_PROGRAM_CHANGED, pData->id, event.value1, 0, 0.0f, nullptr);
  1432. } break;
  1433. case kPluginPostRtEventMidiProgramChange: {
  1434. // Update UI
  1435. if (event.value1 >= 0 && hasUI)
  1436. {
  1437. if (needsUiMainThread)
  1438. pData->postUiEvents.append(event);
  1439. else
  1440. uiMidiProgramChange(static_cast<uint32_t>(event.value1));
  1441. }
  1442. // Update param values
  1443. for (uint32_t j=0; j < pData->param.count; ++j)
  1444. {
  1445. const float paramDefault(pData->param.ranges[j].def);
  1446. const float paramValue(getParameterValue(j));
  1447. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1448. if (sendOsc && j < 50)
  1449. {
  1450. pData->engine->oscSend_control_set_parameter_value(pData->id, static_cast<int32_t>(j), paramValue);
  1451. pData->engine->oscSend_control_set_default_value(pData->id, j, paramDefault);
  1452. }
  1453. #endif
  1454. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, static_cast<int>(j), 0, paramValue, nullptr);
  1455. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_DEFAULT_CHANGED, pData->id, static_cast<int>(j), 0, paramDefault, nullptr);
  1456. }
  1457. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1458. // Update OSC control client
  1459. if (sendOsc)
  1460. pData->engine->oscSend_control_set_current_midi_program(pData->id, event.value1);
  1461. #endif
  1462. // Update Host
  1463. pData->engine->callback(ENGINE_CALLBACK_MIDI_PROGRAM_CHANGED, pData->id, event.value1, 0, 0.0f, nullptr);
  1464. } break;
  1465. case kPluginPostRtEventNoteOn: {
  1466. CARLA_SAFE_ASSERT_BREAK(event.value1 >= 0 && event.value1 < MAX_MIDI_CHANNELS);
  1467. CARLA_SAFE_ASSERT_BREAK(event.value2 >= 0 && event.value2 < MAX_MIDI_NOTE);
  1468. CARLA_SAFE_ASSERT_BREAK(event.value3 >= 0 && event.value3 < MAX_MIDI_VALUE);
  1469. const uint8_t channel = static_cast<uint8_t>(event.value1);
  1470. const uint8_t note = static_cast<uint8_t>(event.value2);
  1471. const uint8_t velocity = uint8_t(event.value3);
  1472. // Update UI
  1473. if (hasUI)
  1474. {
  1475. if (needsUiMainThread)
  1476. pData->postUiEvents.append(event);
  1477. else
  1478. uiNoteOn(channel, note, velocity);
  1479. }
  1480. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1481. // Update OSC control client
  1482. if (sendOsc)
  1483. pData->engine->oscSend_control_note_on(pData->id, channel, note, velocity);
  1484. #endif
  1485. // Update Host
  1486. pData->engine->callback(ENGINE_CALLBACK_NOTE_ON, pData->id, event.value1, event.value2, event.value3, nullptr);
  1487. } break;
  1488. case kPluginPostRtEventNoteOff: {
  1489. CARLA_SAFE_ASSERT_BREAK(event.value1 >= 0 && event.value1 < MAX_MIDI_CHANNELS);
  1490. CARLA_SAFE_ASSERT_BREAK(event.value2 >= 0 && event.value2 < MAX_MIDI_NOTE);
  1491. const uint8_t channel = static_cast<uint8_t>(event.value1);
  1492. const uint8_t note = static_cast<uint8_t>(event.value2);
  1493. // Update UI
  1494. if (hasUI)
  1495. {
  1496. if (needsUiMainThread)
  1497. pData->postUiEvents.append(event);
  1498. else
  1499. uiNoteOff(channel, note);
  1500. }
  1501. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1502. // Update OSC control client
  1503. if (sendOsc)
  1504. pData->engine->oscSend_control_note_off(pData->id, channel, note);
  1505. #endif
  1506. // Update Host
  1507. pData->engine->callback(ENGINE_CALLBACK_NOTE_OFF, pData->id, event.value1, event.value2, 0.0f, nullptr);
  1508. } break;
  1509. }
  1510. }
  1511. pData->postRtEvents.data.clear();
  1512. }
  1513. bool CarlaPlugin::tryLock(const bool forcedOffline) noexcept
  1514. {
  1515. if (forcedOffline)
  1516. {
  1517. #ifndef STOAT_TEST_BUILD
  1518. pData->masterMutex.lock();
  1519. return true;
  1520. #endif
  1521. }
  1522. return pData->masterMutex.tryLock();
  1523. }
  1524. void CarlaPlugin::unlock() noexcept
  1525. {
  1526. pData->masterMutex.unlock();
  1527. }
  1528. // -------------------------------------------------------------------
  1529. // Plugin buffers
  1530. void CarlaPlugin::initBuffers() const noexcept
  1531. {
  1532. pData->audioIn.initBuffers();
  1533. pData->audioOut.initBuffers();
  1534. pData->cvIn.initBuffers();
  1535. pData->cvOut.initBuffers();
  1536. pData->event.initBuffers();
  1537. }
  1538. void CarlaPlugin::clearBuffers() noexcept
  1539. {
  1540. pData->clearBuffers();
  1541. }
  1542. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1543. // -------------------------------------------------------------------
  1544. // OSC stuff
  1545. void CarlaPlugin::registerToOscClient() noexcept
  1546. {
  1547. if (! pData->engine->isOscControlRegistered())
  1548. return;
  1549. pData->engine->oscSend_control_add_plugin_start(pData->id, pData->name);
  1550. // Base data
  1551. {
  1552. char bufName[STR_MAX+1], bufLabel[STR_MAX+1], bufMaker[STR_MAX+1], bufCopyright[STR_MAX+1];
  1553. carla_zeroChars(bufName, STR_MAX);
  1554. carla_zeroChars(bufLabel, STR_MAX);
  1555. carla_zeroChars(bufMaker, STR_MAX);
  1556. carla_zeroChars(bufCopyright, STR_MAX);
  1557. getRealName(bufName);
  1558. getLabel(bufLabel);
  1559. getMaker(bufMaker);
  1560. getCopyright(bufCopyright);
  1561. pData->engine->oscSend_control_set_plugin_info1(pData->id, getType(), getCategory(), pData->hints, getUniqueId());
  1562. pData->engine->oscSend_control_set_plugin_info2(pData->id, bufName, bufLabel, bufMaker, bufCopyright);
  1563. }
  1564. // Base count
  1565. uint32_t paramIns, paramOuts;
  1566. {
  1567. getParameterCountInfo(paramIns, paramOuts);
  1568. if (paramIns > 49)
  1569. paramIns = 49;
  1570. if (paramOuts > 49)
  1571. paramOuts = 49;
  1572. pData->engine->oscSend_control_set_audio_count(pData->id, getAudioInCount(), getAudioOutCount());
  1573. pData->engine->oscSend_control_set_midi_count(pData->id, getMidiInCount(), getMidiOutCount());
  1574. pData->engine->oscSend_control_set_parameter_count(pData->id, paramIns, paramOuts);
  1575. }
  1576. // Plugin Parameters
  1577. if (const uint32_t count = std::min<uint32_t>(pData->param.count, 98U))
  1578. {
  1579. char bufName[STR_MAX+1], bufUnit[STR_MAX+1];
  1580. for (uint32_t i=0; i<count; ++i)
  1581. {
  1582. const ParameterData& paramData(pData->param.data[i]);
  1583. if (paramData.type == PARAMETER_INPUT)
  1584. {
  1585. if (--paramIns == 0)
  1586. break;
  1587. }
  1588. else if (paramData.type == PARAMETER_INPUT)
  1589. {
  1590. if (--paramOuts == 0)
  1591. break;
  1592. }
  1593. else
  1594. {
  1595. continue;
  1596. }
  1597. const ParameterRanges& paramRanges(pData->param.ranges[i]);
  1598. carla_zeroChars(bufName, STR_MAX);
  1599. carla_zeroChars(bufUnit, STR_MAX);
  1600. getParameterName(i, bufName);
  1601. getParameterUnit(i, bufUnit);
  1602. pData->engine->oscSend_control_set_parameter_data(pData->id, i, paramData.type, paramData.hints, bufName, bufUnit);
  1603. pData->engine->oscSend_control_set_parameter_ranges1(pData->id, i, paramRanges.def, paramRanges.min, paramRanges.max);
  1604. pData->engine->oscSend_control_set_parameter_ranges2(pData->id, i, paramRanges.step, paramRanges.stepSmall, paramRanges.stepLarge);
  1605. pData->engine->oscSend_control_set_parameter_value(pData->id, static_cast<int32_t>(i), getParameterValue(i));
  1606. if (paramData.midiCC >= 0)
  1607. pData->engine->oscSend_control_set_parameter_midi_cc(pData->id, i, paramData.midiCC);
  1608. if (paramData.midiChannel != 0)
  1609. pData->engine->oscSend_control_set_parameter_midi_channel(pData->id, i, paramData.midiChannel);
  1610. }
  1611. }
  1612. // Programs
  1613. if (const uint32_t count = std::min<uint32_t>(pData->prog.count, 50U))
  1614. {
  1615. pData->engine->oscSend_control_set_program_count(pData->id, count);
  1616. for (uint32_t i=0; i < count; ++i)
  1617. pData->engine->oscSend_control_set_program_name(pData->id, i, pData->prog.names[i]);
  1618. pData->engine->oscSend_control_set_current_program(pData->id, pData->prog.current);
  1619. }
  1620. // MIDI Programs
  1621. if (const uint32_t count = std::min<uint32_t>(pData->midiprog.count, 50U))
  1622. {
  1623. pData->engine->oscSend_control_set_midi_program_count(pData->id, count);
  1624. for (uint32_t i=0; i < count; ++i)
  1625. {
  1626. const MidiProgramData& mpData(pData->midiprog.data[i]);
  1627. pData->engine->oscSend_control_set_midi_program_data(pData->id, i, mpData.bank, mpData.program, mpData.name);
  1628. }
  1629. pData->engine->oscSend_control_set_current_midi_program(pData->id, pData->midiprog.current);
  1630. }
  1631. pData->engine->oscSend_control_add_plugin_end(pData->id);
  1632. // Internal Parameters
  1633. {
  1634. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_DRYWET, pData->postProc.dryWet);
  1635. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_VOLUME, pData->postProc.volume);
  1636. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_BALANCE_LEFT, pData->postProc.balanceLeft);
  1637. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_BALANCE_RIGHT, pData->postProc.balanceRight);
  1638. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_PANNING, pData->postProc.panning);
  1639. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_CTRL_CHANNEL, pData->ctrlChannel);
  1640. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_ACTIVE, pData->active ? 1.0f : 0.0f);
  1641. }
  1642. }
  1643. #endif
  1644. // FIXME
  1645. void CarlaPlugin::handleOscMessage(const char* const, const int, const void* const, const char* const, const lo_message)
  1646. {
  1647. // do nothing
  1648. }
  1649. //#endif // HAVE_LIBLO && ! BUILD_BRIDGE
  1650. // -------------------------------------------------------------------
  1651. // MIDI events
  1652. void CarlaPlugin::sendMidiSingleNote(const uint8_t channel, const uint8_t note, const uint8_t velo, const bool sendGui, const bool sendOsc, const bool sendCallback)
  1653. {
  1654. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1655. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1656. CARLA_SAFE_ASSERT_RETURN(velo < MAX_MIDI_VALUE,);
  1657. if (! pData->active)
  1658. return;
  1659. ExternalMidiNote extNote;
  1660. extNote.channel = static_cast<int8_t>(channel);
  1661. extNote.note = note;
  1662. extNote.velo = velo;
  1663. pData->extNotes.appendNonRT(extNote);
  1664. if (sendGui && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1665. {
  1666. if (velo > 0)
  1667. uiNoteOn(channel, note, velo);
  1668. else
  1669. uiNoteOff(channel, note);
  1670. }
  1671. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1672. if (sendOsc && pData->engine->isOscControlRegistered())
  1673. {
  1674. if (velo > 0)
  1675. pData->engine->oscSend_control_note_on(pData->id, channel, note, velo);
  1676. else
  1677. pData->engine->oscSend_control_note_off(pData->id, channel, note);
  1678. }
  1679. #endif
  1680. if (sendCallback)
  1681. pData->engine->callback((velo > 0) ? ENGINE_CALLBACK_NOTE_ON : ENGINE_CALLBACK_NOTE_OFF, pData->id, channel, note, velo, nullptr);
  1682. // may be unused
  1683. return; (void)sendOsc;
  1684. }
  1685. #ifndef BUILD_BRIDGE
  1686. void CarlaPlugin::sendMidiAllNotesOffToCallback()
  1687. {
  1688. if (pData->ctrlChannel < 0 || pData->ctrlChannel >= MAX_MIDI_CHANNELS)
  1689. return;
  1690. PluginPostRtEvent postEvent;
  1691. postEvent.type = kPluginPostRtEventNoteOff;
  1692. postEvent.value1 = pData->ctrlChannel;
  1693. postEvent.value2 = 0;
  1694. postEvent.value3 = 0.0f;
  1695. for (int32_t i=0; i < MAX_MIDI_NOTE; ++i)
  1696. {
  1697. postEvent.value2 = i;
  1698. pData->postRtEvents.appendRT(postEvent);
  1699. }
  1700. }
  1701. #endif
  1702. // -------------------------------------------------------------------
  1703. // UI Stuff
  1704. void CarlaPlugin::showCustomUI(const bool)
  1705. {
  1706. CARLA_SAFE_ASSERT(false);
  1707. }
  1708. void CarlaPlugin::uiIdle()
  1709. {
  1710. if (pData->hints & PLUGIN_NEEDS_UI_MAIN_THREAD)
  1711. {
  1712. // Update parameter outputs
  1713. for (uint32_t i=0; i < pData->param.count; ++i)
  1714. {
  1715. if (pData->param.data[i].type == PARAMETER_OUTPUT)
  1716. uiParameterChange(i, getParameterValue(i));
  1717. }
  1718. const CarlaMutexLocker sl(pData->postUiEvents.mutex);
  1719. for (LinkedList<PluginPostRtEvent>::Itenerator it = pData->postUiEvents.data.begin2(); it.valid(); it.next())
  1720. {
  1721. const PluginPostRtEvent& event(it.getValue(kPluginPostRtEventFallback));
  1722. CARLA_SAFE_ASSERT_CONTINUE(event.type != kPluginPostRtEventNull);
  1723. switch (event.type)
  1724. {
  1725. case kPluginPostRtEventNull:
  1726. case kPluginPostRtEventDebug:
  1727. break;
  1728. case kPluginPostRtEventParameterChange:
  1729. uiParameterChange(static_cast<uint32_t>(event.value1), event.value3);
  1730. break;
  1731. case kPluginPostRtEventProgramChange:
  1732. uiProgramChange(static_cast<uint32_t>(event.value1));
  1733. break;
  1734. case kPluginPostRtEventMidiProgramChange:
  1735. uiMidiProgramChange(static_cast<uint32_t>(event.value1));
  1736. break;
  1737. case kPluginPostRtEventNoteOn:
  1738. uiNoteOn(static_cast<uint8_t>(event.value1), static_cast<uint8_t>(event.value2), uint8_t(event.value3));
  1739. break;
  1740. case kPluginPostRtEventNoteOff:
  1741. uiNoteOff(static_cast<uint8_t>(event.value1), static_cast<uint8_t>(event.value2));
  1742. break;
  1743. }
  1744. }
  1745. pData->postUiEvents.data.clear();
  1746. }
  1747. if (pData->transientTryCounter == 0)
  1748. return;
  1749. if (++pData->transientTryCounter % 10 != 0)
  1750. return;
  1751. if (pData->transientTryCounter >= 200)
  1752. return;
  1753. carla_stdout("Trying to get window...");
  1754. CarlaString uiTitle(pData->name);
  1755. uiTitle += " (GUI)";
  1756. if (CarlaPluginUI::tryTransientWinIdMatch(getUiBridgeProcessId(), uiTitle, pData->engine->getOptions().frontendWinId, true))
  1757. pData->transientTryCounter = 0;
  1758. }
  1759. void CarlaPlugin::uiParameterChange(const uint32_t index, const float value) noexcept
  1760. {
  1761. CARLA_SAFE_ASSERT_RETURN(index < getParameterCount(),);
  1762. return;
  1763. // unused
  1764. (void)value;
  1765. }
  1766. void CarlaPlugin::uiProgramChange(const uint32_t index) noexcept
  1767. {
  1768. CARLA_SAFE_ASSERT_RETURN(index < getProgramCount(),);
  1769. }
  1770. void CarlaPlugin::uiMidiProgramChange(const uint32_t index) noexcept
  1771. {
  1772. CARLA_SAFE_ASSERT_RETURN(index < getMidiProgramCount(),);
  1773. }
  1774. void CarlaPlugin::uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) noexcept
  1775. {
  1776. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1777. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1778. CARLA_SAFE_ASSERT_RETURN(velo > 0 && velo < MAX_MIDI_VALUE,);
  1779. }
  1780. void CarlaPlugin::uiNoteOff(const uint8_t channel, const uint8_t note) noexcept
  1781. {
  1782. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1783. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1784. }
  1785. bool CarlaPlugin::canRunInRack() const noexcept
  1786. {
  1787. return (pData->extraHints & PLUGIN_EXTRA_HINT_CAN_RUN_RACK) != 0;
  1788. }
  1789. CarlaEngine* CarlaPlugin::getEngine() const noexcept
  1790. {
  1791. return pData->engine;
  1792. }
  1793. CarlaEngineClient* CarlaPlugin::getEngineClient() const noexcept
  1794. {
  1795. return pData->client;
  1796. }
  1797. CarlaEngineAudioPort* CarlaPlugin::getAudioInPort(const uint32_t index) const noexcept
  1798. {
  1799. return pData->audioIn.ports[index].port;
  1800. }
  1801. CarlaEngineAudioPort* CarlaPlugin::getAudioOutPort(const uint32_t index) const noexcept
  1802. {
  1803. return pData->audioOut.ports[index].port;
  1804. }
  1805. CarlaEngineCVPort* CarlaPlugin::getCVInPort(const uint32_t index) const noexcept
  1806. {
  1807. return pData->cvIn.ports[index].port;
  1808. }
  1809. CarlaEngineCVPort* CarlaPlugin::getCVOutPort(const uint32_t index) const noexcept
  1810. {
  1811. return pData->cvOut.ports[index].port;
  1812. }
  1813. CarlaEngineEventPort* CarlaPlugin::getDefaultEventInPort() const noexcept
  1814. {
  1815. return pData->event.portIn;
  1816. }
  1817. CarlaEngineEventPort* CarlaPlugin::getDefaultEventOutPort() const noexcept
  1818. {
  1819. return pData->event.portOut;
  1820. }
  1821. void* CarlaPlugin::getNativeHandle() const noexcept
  1822. {
  1823. return nullptr;
  1824. }
  1825. const void* CarlaPlugin::getNativeDescriptor() const noexcept
  1826. {
  1827. return nullptr;
  1828. }
  1829. uintptr_t CarlaPlugin::getUiBridgeProcessId() const noexcept
  1830. {
  1831. return 0;
  1832. }
  1833. // -------------------------------------------------------------------
  1834. uint32_t CarlaPlugin::getPatchbayNodeId() const noexcept
  1835. {
  1836. return pData->nodeId;
  1837. }
  1838. void CarlaPlugin::setPatchbayNodeId(const uint32_t nodeId) noexcept
  1839. {
  1840. pData->nodeId = nodeId;
  1841. }
  1842. // -------------------------------------------------------------------
  1843. // Scoped Disabler
  1844. CarlaPlugin::ScopedDisabler::ScopedDisabler(CarlaPlugin* const plugin) noexcept
  1845. : fPlugin(plugin),
  1846. fWasEnabled(false)
  1847. {
  1848. CARLA_SAFE_ASSERT_RETURN(plugin != nullptr,);
  1849. CARLA_SAFE_ASSERT_RETURN(plugin->pData != nullptr,);
  1850. CARLA_SAFE_ASSERT_RETURN(plugin->pData->client != nullptr,);
  1851. carla_debug("CarlaPlugin::ScopedDisabler(%p)", plugin);
  1852. plugin->pData->masterMutex.lock();
  1853. if (plugin->pData->enabled)
  1854. {
  1855. fWasEnabled = true;
  1856. plugin->pData->enabled = false;
  1857. if (plugin->pData->client->isActive())
  1858. plugin->pData->client->deactivate();
  1859. }
  1860. }
  1861. CarlaPlugin::ScopedDisabler::~ScopedDisabler() noexcept
  1862. {
  1863. CARLA_SAFE_ASSERT_RETURN(fPlugin != nullptr,);
  1864. CARLA_SAFE_ASSERT_RETURN(fPlugin->pData != nullptr,);
  1865. CARLA_SAFE_ASSERT_RETURN(fPlugin->pData->client != nullptr,);
  1866. carla_debug("CarlaPlugin::~ScopedDisabler()");
  1867. if (fWasEnabled)
  1868. {
  1869. fPlugin->pData->enabled = true;
  1870. fPlugin->pData->client->activate();
  1871. }
  1872. fPlugin->pData->masterMutex.unlock();
  1873. }
  1874. // -------------------------------------------------------------------
  1875. // Scoped Process Locker
  1876. CarlaPlugin::ScopedSingleProcessLocker::ScopedSingleProcessLocker(CarlaPlugin* const plugin, const bool block) noexcept
  1877. : fPlugin(plugin),
  1878. fBlock(block)
  1879. {
  1880. CARLA_SAFE_ASSERT_RETURN(fPlugin != nullptr,);
  1881. CARLA_SAFE_ASSERT_RETURN(fPlugin->pData != nullptr,);
  1882. carla_debug("CarlaPlugin::ScopedSingleProcessLocker(%p, %s)", plugin, bool2str(block));
  1883. if (! fBlock)
  1884. return;
  1885. plugin->pData->singleMutex.lock();
  1886. }
  1887. CarlaPlugin::ScopedSingleProcessLocker::~ScopedSingleProcessLocker() noexcept
  1888. {
  1889. CARLA_SAFE_ASSERT_RETURN(fPlugin != nullptr,);
  1890. CARLA_SAFE_ASSERT_RETURN(fPlugin->pData != nullptr,);
  1891. carla_debug("CarlaPlugin::~ScopedSingleProcessLocker()");
  1892. if (! fBlock)
  1893. return;
  1894. #ifndef BUILD_BRIDGE
  1895. if (fPlugin->pData->singleMutex.wasTryLockCalled())
  1896. fPlugin->pData->needsReset = true;
  1897. #endif
  1898. fPlugin->pData->singleMutex.unlock();
  1899. }
  1900. // -------------------------------------------------------------------
  1901. CARLA_BACKEND_END_NAMESPACE