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.

CarlaPluginNative.cpp 114KB

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
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
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
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
10 years ago
10 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
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
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
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
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
10 years ago
10 years ago
10 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
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
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
6 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
11 years ago
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
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
9 years ago
9 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
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
10 years ago
10 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186
  1. /*
  2. * Carla Native Plugin
  3. * Copyright (C) 2012-2020 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #include "CarlaPluginInternal.hpp"
  18. #include "CarlaEngine.hpp"
  19. #include "CarlaBackendUtils.hpp"
  20. #include "CarlaMathUtils.hpp"
  21. #include "CarlaNative.h"
  22. #include "water/misc/Time.h"
  23. #include "water/text/StringArray.h"
  24. using water::jmax;
  25. using water::String;
  26. using water::StringArray;
  27. // -----------------------------------------------------------------------
  28. // used in carla-base.cpp
  29. std::size_t carla_getNativePluginCount() noexcept;
  30. const NativePluginDescriptor* carla_getNativePluginDescriptor(const std::size_t index) noexcept;
  31. // -----------------------------------------------------------------------
  32. static LinkedList<const NativePluginDescriptor*> gPluginDescriptors;
  33. void carla_register_native_plugin(const NativePluginDescriptor* desc)
  34. {
  35. gPluginDescriptors.append(desc);
  36. }
  37. // -----------------------------------------------------------------------
  38. static
  39. class NativePluginInitializer
  40. {
  41. public:
  42. NativePluginInitializer() noexcept
  43. : fNeedsInit(true) {}
  44. ~NativePluginInitializer() noexcept
  45. {
  46. gPluginDescriptors.clear();
  47. }
  48. void initIfNeeded() noexcept
  49. {
  50. if (! fNeedsInit)
  51. return;
  52. fNeedsInit = false;
  53. try {
  54. carla_register_all_native_plugins();
  55. } CARLA_SAFE_EXCEPTION("carla_register_all_native_plugins")
  56. }
  57. private:
  58. bool fNeedsInit;
  59. } sPluginInitializer;
  60. // -----------------------------------------------------------------------
  61. std::size_t carla_getNativePluginCount() noexcept
  62. {
  63. sPluginInitializer.initIfNeeded();
  64. return gPluginDescriptors.count();
  65. }
  66. const NativePluginDescriptor* carla_getNativePluginDescriptor(const std::size_t index) noexcept
  67. {
  68. sPluginInitializer.initIfNeeded();
  69. return gPluginDescriptors.getAt(index, nullptr);
  70. }
  71. // -----------------------------------------------------------------------
  72. CARLA_BACKEND_START_NAMESPACE
  73. // -------------------------------------------------------------------
  74. // Fallback data
  75. static const CustomData kCustomDataFallback = { nullptr, nullptr, nullptr };
  76. static EngineEvent kNullEngineEvent = {
  77. kEngineEventTypeNull, 0, 0, {{ kEngineControlEventTypeNull, 0, -1, 0.0f, true }}
  78. };
  79. // -----------------------------------------------------------------------
  80. struct NativePluginMidiOutData {
  81. uint32_t count;
  82. uint32_t* indexes;
  83. CarlaEngineEventPort** ports;
  84. NativePluginMidiOutData() noexcept
  85. : count(0),
  86. indexes(nullptr),
  87. ports(nullptr) {}
  88. ~NativePluginMidiOutData() noexcept
  89. {
  90. CARLA_SAFE_ASSERT_INT(count == 0, count);
  91. CARLA_SAFE_ASSERT(indexes == nullptr);
  92. CARLA_SAFE_ASSERT(ports == nullptr);
  93. }
  94. bool createNew(const uint32_t newCount)
  95. {
  96. CARLA_SAFE_ASSERT_INT(count == 0, count);
  97. CARLA_SAFE_ASSERT_RETURN(indexes == nullptr, false);
  98. CARLA_SAFE_ASSERT_RETURN(ports == nullptr, false);
  99. CARLA_SAFE_ASSERT_RETURN(newCount > 0, false);
  100. indexes = new uint32_t[newCount];
  101. ports = new CarlaEngineEventPort*[newCount];
  102. count = newCount;
  103. carla_zeroStructs(indexes, newCount);
  104. carla_zeroStructs(ports, newCount);
  105. return true;
  106. }
  107. void clear() noexcept
  108. {
  109. if (ports != nullptr)
  110. {
  111. for (uint32_t i=0; i < count; ++i)
  112. {
  113. if (ports[i] != nullptr)
  114. {
  115. delete ports[i];
  116. ports[i] = nullptr;
  117. }
  118. }
  119. delete[] ports;
  120. ports = nullptr;
  121. }
  122. if (indexes != nullptr)
  123. {
  124. delete[] indexes;
  125. indexes = nullptr;
  126. }
  127. count = 0;
  128. }
  129. void initBuffers() const noexcept
  130. {
  131. for (uint32_t i=0; i < count; ++i)
  132. {
  133. if (ports[i] != nullptr)
  134. ports[i]->initBuffer();
  135. }
  136. }
  137. CARLA_DECLARE_NON_COPY_STRUCT(NativePluginMidiOutData)
  138. };
  139. struct NativePluginMidiInData : NativePluginMidiOutData {
  140. struct MultiPortData {
  141. uint32_t cachedEventCount;
  142. uint32_t usedIndex;
  143. };
  144. MultiPortData* multiportData;
  145. NativePluginMidiInData() noexcept
  146. : NativePluginMidiOutData(),
  147. multiportData(nullptr) {}
  148. ~NativePluginMidiInData() noexcept
  149. {
  150. CARLA_SAFE_ASSERT(multiportData == nullptr);
  151. }
  152. bool createNew(const uint32_t newCount)
  153. {
  154. if (! NativePluginMidiOutData::createNew(newCount))
  155. return false;
  156. multiportData = new MultiPortData[newCount];
  157. carla_zeroStructs(multiportData, newCount);
  158. return true;
  159. }
  160. void clear() noexcept
  161. {
  162. if (multiportData != nullptr)
  163. {
  164. delete[] multiportData;
  165. multiportData = nullptr;
  166. }
  167. NativePluginMidiOutData::clear();
  168. }
  169. void initBuffers(CarlaEngineEventPort* const port) const noexcept
  170. {
  171. if (count == 1)
  172. {
  173. CARLA_SAFE_ASSERT_RETURN(port != nullptr,);
  174. carla_zeroStruct(multiportData[0]);
  175. multiportData[0].cachedEventCount = port->getEventCount();
  176. return;
  177. }
  178. for (uint32_t i=0; i < count; ++i)
  179. {
  180. carla_zeroStruct(multiportData[i]);
  181. if (ports[i] != nullptr)
  182. {
  183. ports[i]->initBuffer();
  184. multiportData[i].cachedEventCount = ports[i]->getEventCount();
  185. }
  186. }
  187. }
  188. CARLA_DECLARE_NON_COPY_STRUCT(NativePluginMidiInData)
  189. };
  190. // -----------------------------------------------------
  191. class CarlaPluginNative : public CarlaPlugin
  192. {
  193. public:
  194. CarlaPluginNative(CarlaEngine* const engine, const uint id)
  195. : CarlaPlugin(engine, id),
  196. fHandle(nullptr),
  197. fHandle2(nullptr),
  198. fHost(),
  199. fDescriptor(nullptr),
  200. fIsProcessing(false),
  201. fIsOffline(engine->isOffline()),
  202. fIsUiAvailable(false),
  203. fIsUiVisible(false),
  204. fNeedsIdle(false),
  205. fInlineDisplayNeedsRedraw(false),
  206. fInlineDisplayLastRedrawTime(0),
  207. fLastProjectFilename(),
  208. fLastProjectFolder(),
  209. fAudioAndCvInBuffers(nullptr),
  210. fAudioAndCvOutBuffers(nullptr),
  211. fMidiEventInCount(0),
  212. fMidiEventOutCount(0),
  213. fCurBufferSize(engine->getBufferSize()),
  214. fCurSampleRate(engine->getSampleRate()),
  215. fMidiIn(),
  216. fMidiOut(),
  217. fTimeInfo()
  218. {
  219. carla_debug("CarlaPluginNative::CarlaPluginNative(%p, %i)", engine, id);
  220. carla_fill(fCurMidiProgs, 0, MAX_MIDI_CHANNELS);
  221. carla_zeroStructs(fMidiInEvents, kPluginMaxMidiEvents);
  222. carla_zeroStructs(fMidiOutEvents, kPluginMaxMidiEvents);
  223. carla_zeroStruct(fTimeInfo);
  224. fHost.handle = this;
  225. fHost.resourceDir = carla_strdup(engine->getOptions().resourceDir);
  226. fHost.uiName = nullptr;
  227. fHost.uiParentId = engine->getOptions().frontendWinId;
  228. fHost.get_buffer_size = carla_host_get_buffer_size;
  229. fHost.get_sample_rate = carla_host_get_sample_rate;
  230. fHost.is_offline = carla_host_is_offline;
  231. fHost.get_time_info = carla_host_get_time_info;
  232. fHost.write_midi_event = carla_host_write_midi_event;
  233. fHost.ui_parameter_changed = carla_host_ui_parameter_changed;
  234. fHost.ui_custom_data_changed = carla_host_ui_custom_data_changed;
  235. fHost.ui_closed = carla_host_ui_closed;
  236. fHost.ui_open_file = carla_host_ui_open_file;
  237. fHost.ui_save_file = carla_host_ui_save_file;
  238. fHost.dispatcher = carla_host_dispatcher;
  239. }
  240. ~CarlaPluginNative() override
  241. {
  242. carla_debug("CarlaPluginNative::~CarlaPluginNative()");
  243. fInlineDisplayNeedsRedraw = false;
  244. // close UI
  245. if (pData->hints & PLUGIN_HAS_CUSTOM_UI)
  246. {
  247. if (fIsUiVisible && fDescriptor != nullptr && fDescriptor->ui_show != nullptr && fHandle != nullptr)
  248. fDescriptor->ui_show(fHandle, false);
  249. #ifndef BUILD_BRIDGE
  250. pData->transientTryCounter = 0;
  251. #endif
  252. }
  253. pData->singleMutex.lock();
  254. pData->masterMutex.lock();
  255. if (pData->client != nullptr && pData->client->isActive())
  256. pData->client->deactivate(true);
  257. CARLA_ASSERT(! fIsProcessing);
  258. if (pData->active)
  259. {
  260. deactivate();
  261. pData->active = false;
  262. }
  263. if (fDescriptor != nullptr)
  264. {
  265. if (fDescriptor->cleanup != nullptr)
  266. {
  267. if (fHandle != nullptr)
  268. fDescriptor->cleanup(fHandle);
  269. if (fHandle2 != nullptr)
  270. fDescriptor->cleanup(fHandle2);
  271. }
  272. fHandle = nullptr;
  273. fHandle2 = nullptr;
  274. fDescriptor = nullptr;
  275. }
  276. if (fHost.resourceDir != nullptr)
  277. {
  278. delete[] fHost.resourceDir;
  279. fHost.resourceDir = nullptr;
  280. }
  281. if (fHost.uiName != nullptr)
  282. {
  283. std::free(const_cast<char*>(fHost.uiName));
  284. fHost.uiName = nullptr;
  285. }
  286. clearBuffers();
  287. }
  288. // -------------------------------------------------------------------
  289. // Information (base)
  290. PluginType getType() const noexcept override
  291. {
  292. return PLUGIN_INTERNAL;
  293. }
  294. PluginCategory getCategory() const noexcept override
  295. {
  296. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, PLUGIN_CATEGORY_NONE);
  297. return static_cast<PluginCategory>(fDescriptor->category);
  298. }
  299. // -------------------------------------------------------------------
  300. // Information (count)
  301. uint32_t getMidiInCount() const noexcept override
  302. {
  303. return fMidiIn.count;
  304. }
  305. uint32_t getMidiOutCount() const noexcept override
  306. {
  307. return fMidiOut.count;
  308. }
  309. uint32_t getParameterScalePointCount(const uint32_t parameterId) const noexcept override
  310. {
  311. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, 0);
  312. CARLA_SAFE_ASSERT_RETURN(fDescriptor->get_parameter_info != nullptr, 0);
  313. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr, 0);
  314. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0);
  315. // FIXME - try
  316. if (const NativeParameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId))
  317. return param->scalePointCount;
  318. carla_safe_assert("const Parameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId)", __FILE__, __LINE__);
  319. return 0;
  320. }
  321. // -------------------------------------------------------------------
  322. // Information (current data)
  323. // nothing
  324. // -------------------------------------------------------------------
  325. // Information (per-plugin data)
  326. uint getOptionsAvailable() const noexcept override
  327. {
  328. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, 0x0);
  329. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr, 0x0);
  330. bool hasMidiProgs = false;
  331. if (fDescriptor->get_midi_program_count != nullptr)
  332. {
  333. try {
  334. hasMidiProgs = fDescriptor->get_midi_program_count(fHandle) > 0;
  335. } catch (...) {}
  336. }
  337. uint options = 0x0;
  338. // can't disable fixed buffers if required by the plugin
  339. if ((fDescriptor->hints & NATIVE_PLUGIN_NEEDS_FIXED_BUFFERS) == 0x0)
  340. options |= PLUGIN_OPTION_FIXED_BUFFERS;
  341. // can't disable forced stereo if enabled in the engine, or using CV
  342. if (pData->engine->getOptions().forceStereo || pData->cvIn.count != 0 || pData->cvOut.count != 0)
  343. pass();
  344. // if inputs or outputs are just 1, then yes we can force stereo
  345. else if (pData->audioIn.count == 1 || pData->audioOut.count == 1 || fHandle2 != nullptr)
  346. options |= PLUGIN_OPTION_FORCE_STEREO;
  347. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_CONTROL_CHANGES)
  348. options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  349. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_CHANNEL_PRESSURE)
  350. options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  351. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_NOTE_AFTERTOUCH)
  352. options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  353. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_PITCHBEND)
  354. options |= PLUGIN_OPTION_SEND_PITCHBEND;
  355. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_ALL_SOUND_OFF)
  356. options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  357. if (fDescriptor->midiIns > 0)
  358. options |= PLUGIN_OPTION_SKIP_SENDING_NOTES;
  359. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_PROGRAM_CHANGES)
  360. options |= PLUGIN_OPTION_SEND_PROGRAM_CHANGES;
  361. else if (hasMidiProgs)
  362. options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  363. return options;
  364. }
  365. float getParameterValue(const uint32_t parameterId) const noexcept override
  366. {
  367. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, 0.0f);
  368. CARLA_SAFE_ASSERT_RETURN(fDescriptor->get_parameter_value != nullptr, 0.0f);
  369. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr, 0.0f);
  370. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0.0f);
  371. // FIXME - try
  372. return fDescriptor->get_parameter_value(fHandle, parameterId);
  373. }
  374. float getParameterScalePointValue(const uint32_t parameterId, const uint32_t scalePointId) const noexcept override
  375. {
  376. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, 0.0f);
  377. CARLA_SAFE_ASSERT_RETURN(fDescriptor->get_parameter_info != nullptr, 0.0f);
  378. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr, 0.0f);
  379. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0.0f);
  380. // FIXME - try
  381. if (const NativeParameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId))
  382. {
  383. CARLA_SAFE_ASSERT_RETURN(scalePointId < param->scalePointCount, 0.0f);
  384. const NativeParameterScalePoint* scalePoint(&param->scalePoints[scalePointId]);
  385. return scalePoint->value;
  386. }
  387. carla_safe_assert("const Parameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId)", __FILE__, __LINE__);
  388. return 0.0f;
  389. }
  390. bool getLabel(char* const strBuf) const noexcept override
  391. {
  392. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, false);
  393. if (fDescriptor->label != nullptr)
  394. {
  395. std::strncpy(strBuf, fDescriptor->label, STR_MAX);
  396. return true;
  397. }
  398. return CarlaPlugin::getLabel(strBuf);
  399. }
  400. bool getMaker(char* const strBuf) const noexcept override
  401. {
  402. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, false);
  403. if (fDescriptor->maker != nullptr)
  404. {
  405. std::strncpy(strBuf, fDescriptor->maker, STR_MAX);
  406. return true;
  407. }
  408. return CarlaPlugin::getMaker(strBuf);
  409. }
  410. bool getCopyright(char* const strBuf) const noexcept override
  411. {
  412. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, false);
  413. if (fDescriptor->copyright != nullptr)
  414. {
  415. std::strncpy(strBuf, fDescriptor->copyright, STR_MAX);
  416. return true;
  417. }
  418. return CarlaPlugin::getCopyright(strBuf);
  419. }
  420. bool getRealName(char* const strBuf) const noexcept override
  421. {
  422. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, false);
  423. if (fDescriptor->name != nullptr)
  424. {
  425. std::strncpy(strBuf, fDescriptor->name, STR_MAX);
  426. return true;
  427. }
  428. return CarlaPlugin::getRealName(strBuf);
  429. }
  430. bool getParameterName(const uint32_t parameterId, char* const strBuf) const noexcept override
  431. {
  432. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, false);
  433. CARLA_SAFE_ASSERT_RETURN(fDescriptor->get_parameter_info != nullptr, false);
  434. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr, false);
  435. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, false);
  436. // FIXME - try
  437. if (const NativeParameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId))
  438. {
  439. if (param->name != nullptr)
  440. {
  441. std::strncpy(strBuf, param->name, STR_MAX);
  442. return true;
  443. }
  444. carla_safe_assert("param->name != nullptr", __FILE__, __LINE__);
  445. return CarlaPlugin::getParameterName(parameterId, strBuf);
  446. }
  447. carla_safe_assert("const Parameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId)", __FILE__, __LINE__);
  448. return CarlaPlugin::getParameterName(parameterId, strBuf);
  449. }
  450. bool getParameterUnit(const uint32_t parameterId, char* const strBuf) const noexcept override
  451. {
  452. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, false);
  453. CARLA_SAFE_ASSERT_RETURN(fDescriptor->get_parameter_info != nullptr, false);
  454. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr, false);
  455. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, false);
  456. // FIXME - try
  457. if (const NativeParameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId))
  458. {
  459. if (param->unit != nullptr)
  460. {
  461. std::strncpy(strBuf, param->unit, STR_MAX);
  462. return true;
  463. }
  464. return CarlaPlugin::getParameterUnit(parameterId, strBuf);
  465. }
  466. carla_safe_assert("const Parameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId)", __FILE__, __LINE__);
  467. return CarlaPlugin::getParameterUnit(parameterId, strBuf);
  468. }
  469. bool getParameterComment(const uint32_t parameterId, char* const strBuf) const noexcept override
  470. {
  471. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, false);
  472. CARLA_SAFE_ASSERT_RETURN(fDescriptor->get_parameter_info != nullptr, false);
  473. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr, false);
  474. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, false);
  475. // FIXME - try
  476. if (const NativeParameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId))
  477. {
  478. if (param->comment != nullptr)
  479. {
  480. std::strncpy(strBuf, param->comment, STR_MAX);
  481. return true;
  482. }
  483. return CarlaPlugin::getParameterComment(parameterId, strBuf);
  484. }
  485. carla_safe_assert("const Parameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId)", __FILE__, __LINE__);
  486. return CarlaPlugin::getParameterComment(parameterId, strBuf);
  487. }
  488. bool getParameterGroupName(const uint32_t parameterId, char* const strBuf) const noexcept override
  489. {
  490. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, false);
  491. CARLA_SAFE_ASSERT_RETURN(fDescriptor->get_parameter_info != nullptr, false);
  492. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr, false);
  493. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, false);
  494. // FIXME - try
  495. if (const NativeParameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId))
  496. {
  497. if (param->groupName != nullptr)
  498. {
  499. std::strncpy(strBuf, param->groupName, STR_MAX);
  500. return true;
  501. }
  502. return CarlaPlugin::getParameterGroupName(parameterId, strBuf);
  503. }
  504. carla_safe_assert("const Parameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId)", __FILE__, __LINE__);
  505. return CarlaPlugin::getParameterGroupName(parameterId, strBuf);
  506. }
  507. bool getParameterScalePointLabel(const uint32_t parameterId, const uint32_t scalePointId, char* const strBuf) const noexcept override
  508. {
  509. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, false);
  510. CARLA_SAFE_ASSERT_RETURN(fDescriptor->get_parameter_info != nullptr, false);
  511. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr, false);
  512. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, false);
  513. // FIXME - try
  514. if (const NativeParameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId))
  515. {
  516. CARLA_SAFE_ASSERT_RETURN(scalePointId < param->scalePointCount, false);
  517. const NativeParameterScalePoint* scalePoint(&param->scalePoints[scalePointId]);
  518. if (scalePoint->label != nullptr)
  519. {
  520. std::strncpy(strBuf, scalePoint->label, STR_MAX);
  521. return true;
  522. }
  523. carla_safe_assert("scalePoint->label != nullptr", __FILE__, __LINE__);
  524. return CarlaPlugin::getParameterScalePointLabel(parameterId, scalePointId, strBuf);
  525. }
  526. carla_safe_assert("const Parameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId)", __FILE__, __LINE__);
  527. return CarlaPlugin::getParameterScalePointLabel(parameterId, scalePointId, strBuf);
  528. }
  529. // -------------------------------------------------------------------
  530. // Set data (state)
  531. void prepareForSave(bool) override
  532. {
  533. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  534. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  535. if (pData->midiprog.count > 0 && fDescriptor->category == NATIVE_PLUGIN_CATEGORY_SYNTH)
  536. {
  537. char strBuf[STR_MAX+1];
  538. std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i",
  539. fCurMidiProgs[0], fCurMidiProgs[1], fCurMidiProgs[2], fCurMidiProgs[3],
  540. fCurMidiProgs[4], fCurMidiProgs[5], fCurMidiProgs[6], fCurMidiProgs[7],
  541. fCurMidiProgs[8], fCurMidiProgs[9], fCurMidiProgs[10], fCurMidiProgs[11],
  542. fCurMidiProgs[12], fCurMidiProgs[13], fCurMidiProgs[14], fCurMidiProgs[15]);
  543. strBuf[STR_MAX] = '\0';
  544. CarlaPlugin::setCustomData(CUSTOM_DATA_TYPE_STRING, "midiPrograms", strBuf, false);
  545. }
  546. if (fDescriptor == nullptr || fDescriptor->get_state == nullptr || (fDescriptor->hints & NATIVE_PLUGIN_USES_STATE) == 0)
  547. return;
  548. if (char* data = fDescriptor->get_state(fHandle))
  549. {
  550. CarlaPlugin::setCustomData(CUSTOM_DATA_TYPE_CHUNK, "State", data, false);
  551. std::free(data);
  552. }
  553. }
  554. // -------------------------------------------------------------------
  555. // Set data (internal stuff)
  556. void setName(const char* const newName) override
  557. {
  558. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  559. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  560. CARLA_SAFE_ASSERT_RETURN(newName != nullptr && newName[0] != '\0',);
  561. CarlaPlugin::setName(newName);
  562. if (pData->uiTitle.isEmpty())
  563. setWindowTitle(nullptr);
  564. }
  565. void setCtrlChannel(const int8_t channel, const bool sendOsc, const bool sendCallback) noexcept override
  566. {
  567. if (channel >= 0 && channel < MAX_MIDI_CHANNELS && pData->midiprog.count > 0)
  568. pData->midiprog.current = fCurMidiProgs[channel];
  569. CarlaPlugin::setCtrlChannel(channel, sendOsc, sendCallback);
  570. }
  571. void setWindowTitle(const char* const title) noexcept
  572. {
  573. CarlaString uiName;
  574. if (title != nullptr)
  575. {
  576. uiName = title;
  577. }
  578. else
  579. {
  580. uiName = pData->name;
  581. uiName += " (GUI)";
  582. }
  583. std::free(const_cast<char*>(fHost.uiName));
  584. fHost.uiName = uiName.releaseBufferPointer();
  585. if (fDescriptor->dispatcher != nullptr && fIsUiVisible)
  586. {
  587. try {
  588. fDescriptor->dispatcher(fHandle,
  589. NATIVE_PLUGIN_OPCODE_UI_NAME_CHANGED,
  590. 0, 0,
  591. const_cast<char*>(fHost.uiName),
  592. 0.0f);
  593. } CARLA_SAFE_EXCEPTION("set custom ui title");
  594. }
  595. }
  596. // -------------------------------------------------------------------
  597. // Set data (plugin-specific stuff)
  598. void setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  599. {
  600. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  601. CARLA_SAFE_ASSERT_RETURN(fDescriptor->set_parameter_value != nullptr,);
  602. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  603. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  604. const float fixedValue(pData->param.getFixedValue(parameterId, value));
  605. // FIXME - try
  606. fDescriptor->set_parameter_value(fHandle, parameterId, fixedValue);
  607. if (fHandle2 != nullptr)
  608. fDescriptor->set_parameter_value(fHandle2, parameterId, fixedValue);
  609. CarlaPlugin::setParameterValue(parameterId, fixedValue, sendGui, sendOsc, sendCallback);
  610. }
  611. void setParameterValueRT(const uint32_t parameterId, const float value, const bool sendCallbackLater) noexcept override
  612. {
  613. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  614. CARLA_SAFE_ASSERT_RETURN(fDescriptor->set_parameter_value != nullptr,);
  615. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  616. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  617. const float fixedValue(pData->param.getFixedValue(parameterId, value));
  618. // FIXME - try
  619. fDescriptor->set_parameter_value(fHandle, parameterId, fixedValue);
  620. if (fHandle2 != nullptr)
  621. fDescriptor->set_parameter_value(fHandle2, parameterId, fixedValue);
  622. CarlaPlugin::setParameterValueRT(parameterId, fixedValue, sendCallbackLater);
  623. }
  624. void setCustomData(const char* const type, const char* const key, const char* const value, const bool sendGui) override
  625. {
  626. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  627. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  628. CARLA_SAFE_ASSERT_RETURN(type != nullptr && type[0] != '\0',);
  629. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  630. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  631. carla_debug("CarlaPluginNative::setCustomData(%s, %s, ..., %s)", type, key, bool2str(sendGui));
  632. if (std::strcmp(type, CUSTOM_DATA_TYPE_PROPERTY) == 0)
  633. return CarlaPlugin::setCustomData(type, key, value, sendGui);
  634. if (std::strcmp(type, CUSTOM_DATA_TYPE_STRING) != 0 && std::strcmp(type, CUSTOM_DATA_TYPE_CHUNK) != 0)
  635. return carla_stderr2("CarlaPluginNative::setCustomData(\"%s\", \"%s\", \"%s\", %s) - type is invalid", type, key, value, bool2str(sendGui));
  636. if (std::strcmp(type, CUSTOM_DATA_TYPE_CHUNK) == 0)
  637. {
  638. if (fDescriptor->set_state != nullptr && (fDescriptor->hints & NATIVE_PLUGIN_USES_STATE) != 0)
  639. {
  640. const ScopedSingleProcessLocker spl(this, true);
  641. fDescriptor->set_state(fHandle, value);
  642. if (fHandle2 != nullptr)
  643. fDescriptor->set_state(fHandle2, value);
  644. }
  645. }
  646. else if (std::strcmp(key, "midiPrograms") == 0 && fDescriptor->set_midi_program != nullptr)
  647. {
  648. StringArray midiProgramList(StringArray::fromTokens(value, ":", ""));
  649. if (midiProgramList.size() == MAX_MIDI_CHANNELS)
  650. {
  651. uint8_t channel = 0;
  652. for (String *it=midiProgramList.begin(), *end=midiProgramList.end(); it != end; ++it)
  653. {
  654. const int index(it->getIntValue());
  655. if (index >= 0 && index < static_cast<int>(pData->midiprog.count))
  656. {
  657. const uint32_t bank = pData->midiprog.data[index].bank;
  658. const uint32_t program = pData->midiprog.data[index].program;
  659. fDescriptor->set_midi_program(fHandle, channel, bank, program);
  660. if (fHandle2 != nullptr)
  661. fDescriptor->set_midi_program(fHandle2, channel, bank, program);
  662. fCurMidiProgs[channel] = index;
  663. if (pData->ctrlChannel == static_cast<int32_t>(channel))
  664. {
  665. pData->midiprog.current = index;
  666. pData->engine->callback(true, true,
  667. ENGINE_CALLBACK_MIDI_PROGRAM_CHANGED,
  668. pData->id,
  669. index,
  670. 0, 0, 0.0f, nullptr);
  671. }
  672. }
  673. ++channel;
  674. }
  675. CARLA_SAFE_ASSERT(channel == MAX_MIDI_CHANNELS);
  676. }
  677. }
  678. else
  679. {
  680. if (fDescriptor->set_custom_data != nullptr)
  681. {
  682. fDescriptor->set_custom_data(fHandle, key, value);
  683. if (fHandle2 != nullptr)
  684. fDescriptor->set_custom_data(fHandle2, key, value);
  685. }
  686. if (sendGui && fIsUiVisible && fDescriptor->ui_set_custom_data != nullptr)
  687. fDescriptor->ui_set_custom_data(fHandle, key, value);
  688. }
  689. CarlaPlugin::setCustomData(type, key, value, sendGui);
  690. }
  691. void setMidiProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback, const bool doingInit) noexcept override
  692. {
  693. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  694. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  695. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->midiprog.count),);
  696. CARLA_SAFE_ASSERT_RETURN(sendGui || sendOsc || sendCallback || doingInit,);
  697. // TODO, put into check below
  698. if ((pData->hints & PLUGIN_IS_SYNTH) != 0 && (pData->ctrlChannel < 0 || pData->ctrlChannel >= MAX_MIDI_CHANNELS))
  699. return CarlaPlugin::setMidiProgram(index, sendGui, sendOsc, sendCallback, doingInit);
  700. if (index >= 0)
  701. {
  702. const uint8_t channel = uint8_t((pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS) ? pData->ctrlChannel : 0);
  703. const uint32_t bank = pData->midiprog.data[index].bank;
  704. const uint32_t program = pData->midiprog.data[index].program;
  705. const ScopedSingleProcessLocker spl(this, (sendGui || sendOsc || sendCallback));
  706. try {
  707. fDescriptor->set_midi_program(fHandle, channel, bank, program);
  708. } catch(...) {}
  709. if (fHandle2 != nullptr)
  710. {
  711. try {
  712. fDescriptor->set_midi_program(fHandle2, channel, bank, program);
  713. } catch(...) {}
  714. }
  715. fCurMidiProgs[channel] = index;
  716. }
  717. CarlaPlugin::setMidiProgram(index, sendGui, sendOsc, sendCallback, doingInit);
  718. }
  719. // FIXME: this is never used
  720. void setMidiProgramRT(const uint32_t index, const bool sendCallbackLater) noexcept override
  721. {
  722. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  723. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  724. CARLA_SAFE_ASSERT_RETURN(index < pData->midiprog.count,);
  725. // TODO, put into check below
  726. if ((pData->hints & PLUGIN_IS_SYNTH) != 0 && (pData->ctrlChannel < 0 || pData->ctrlChannel >= MAX_MIDI_CHANNELS))
  727. return CarlaPlugin::setMidiProgramRT(index, sendCallbackLater);
  728. const uint8_t channel = uint8_t((pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS) ? pData->ctrlChannel : 0);
  729. const uint32_t bank = pData->midiprog.data[index].bank;
  730. const uint32_t program = pData->midiprog.data[index].program;
  731. try {
  732. fDescriptor->set_midi_program(fHandle, channel, bank, program);
  733. } catch(...) {}
  734. if (fHandle2 != nullptr)
  735. {
  736. try {
  737. fDescriptor->set_midi_program(fHandle2, channel, bank, program);
  738. } catch(...) {}
  739. }
  740. fCurMidiProgs[channel] = static_cast<int32_t>(index);
  741. CarlaPlugin::setMidiProgramRT(index, sendCallbackLater);
  742. }
  743. // -------------------------------------------------------------------
  744. // Set ui stuff
  745. void setCustomUITitle(const char* const title) noexcept override
  746. {
  747. setWindowTitle(title);
  748. CarlaPlugin::setCustomUITitle(title);
  749. }
  750. void showCustomUI(const bool yesNo) override
  751. {
  752. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  753. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  754. if (fDescriptor->ui_show == nullptr)
  755. return;
  756. fIsUiAvailable = true;
  757. fDescriptor->ui_show(fHandle, yesNo);
  758. // UI might not be available, see NATIVE_HOST_OPCODE_UI_UNAVAILABLE
  759. if (yesNo && ! fIsUiAvailable)
  760. return;
  761. fIsUiVisible = yesNo;
  762. if (! yesNo)
  763. {
  764. #ifndef BUILD_BRIDGE
  765. pData->transientTryCounter = 0;
  766. #endif
  767. return;
  768. }
  769. #ifndef BUILD_BRIDGE
  770. if ((fDescriptor->hints & NATIVE_PLUGIN_USES_PARENT_ID) == 0 && std::strstr(fDescriptor->label, "file") == nullptr)
  771. pData->tryTransient();
  772. #endif
  773. if (fDescriptor->ui_set_custom_data != nullptr)
  774. {
  775. for (LinkedList<CustomData>::Itenerator it = pData->custom.begin2(); it.valid(); it.next())
  776. {
  777. const CustomData& cData(it.getValue(kCustomDataFallback));
  778. CARLA_SAFE_ASSERT_CONTINUE(cData.isValid());
  779. if (std::strcmp(cData.type, CUSTOM_DATA_TYPE_STRING) == 0 && std::strcmp(cData.key, "midiPrograms") != 0)
  780. fDescriptor->ui_set_custom_data(fHandle, cData.key, cData.value);
  781. }
  782. }
  783. if (fDescriptor->ui_set_midi_program != nullptr && pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  784. {
  785. const int32_t index = pData->midiprog.current;
  786. const uint8_t channel = uint8_t((pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS) ? pData->ctrlChannel : 0);
  787. const uint32_t bank = pData->midiprog.data[index].bank;
  788. const uint32_t program = pData->midiprog.data[index].program;
  789. fDescriptor->ui_set_midi_program(fHandle, channel, bank, program);
  790. }
  791. if (fDescriptor->ui_set_parameter_value != nullptr)
  792. {
  793. for (uint32_t i=0; i < pData->param.count; ++i)
  794. fDescriptor->ui_set_parameter_value(fHandle, i, fDescriptor->get_parameter_value(fHandle, i));
  795. }
  796. }
  797. void idle() override
  798. {
  799. if (fNeedsIdle)
  800. {
  801. fNeedsIdle = false;
  802. fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_IDLE, 0, 0, nullptr, 0.0f);
  803. }
  804. if (fInlineDisplayNeedsRedraw)
  805. {
  806. // TESTING
  807. CARLA_SAFE_ASSERT(pData->enabled)
  808. CARLA_SAFE_ASSERT(!pData->engine->isAboutToClose());
  809. CARLA_SAFE_ASSERT(pData->client->isActive());
  810. if (pData->enabled && !pData->engine->isAboutToClose() && pData->client->isActive())
  811. {
  812. const int64_t timeNow = water::Time::currentTimeMillis();
  813. if (timeNow - fInlineDisplayLastRedrawTime > (1000 / 30))
  814. {
  815. fInlineDisplayNeedsRedraw = false;
  816. fInlineDisplayLastRedrawTime = timeNow;
  817. pData->engine->callback(true, true,
  818. ENGINE_CALLBACK_INLINE_DISPLAY_REDRAW,
  819. pData->id,
  820. 0, 0, 0, 0.0f, nullptr);
  821. }
  822. }
  823. else
  824. {
  825. fInlineDisplayNeedsRedraw = false;
  826. }
  827. }
  828. CarlaPlugin::idle();
  829. }
  830. void uiIdle() override
  831. {
  832. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  833. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  834. if (fIsUiVisible && fDescriptor->ui_idle != nullptr)
  835. fDescriptor->ui_idle(fHandle);
  836. CarlaPlugin::uiIdle();
  837. }
  838. // -------------------------------------------------------------------
  839. // Plugin state
  840. void reload() override
  841. {
  842. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr,);
  843. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  844. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  845. carla_debug("CarlaPluginNative::reload() - start");
  846. const EngineProcessMode processMode(pData->engine->getProccessMode());
  847. // Safely disable plugin for reload
  848. const ScopedDisabler sd(this);
  849. if (pData->active)
  850. deactivate();
  851. clearBuffers();
  852. uint32_t aIns, aOuts, cvIns, cvOuts, mIns, mOuts, j;
  853. bool forcedStereoIn, forcedStereoOut;
  854. forcedStereoIn = forcedStereoOut = false;
  855. bool needsCtrlIn, needsCtrlOut;
  856. needsCtrlIn = needsCtrlOut = false;
  857. aIns = fDescriptor->audioIns;
  858. aOuts = fDescriptor->audioOuts;
  859. cvIns = fDescriptor->cvIns;
  860. cvOuts = fDescriptor->cvOuts;
  861. mIns = fDescriptor->midiIns;
  862. mOuts = fDescriptor->midiOuts;
  863. if ((pData->options & PLUGIN_OPTION_FORCE_STEREO) != 0 && (aIns == 1 || aOuts == 1) && mIns <= 1 && mOuts <= 1)
  864. {
  865. if (fHandle2 == nullptr)
  866. fHandle2 = fDescriptor->instantiate(&fHost);
  867. if (fHandle2 != nullptr)
  868. {
  869. if (aIns == 1)
  870. {
  871. aIns = 2;
  872. forcedStereoIn = true;
  873. }
  874. if (aOuts == 1)
  875. {
  876. aOuts = 2;
  877. forcedStereoOut = true;
  878. }
  879. }
  880. }
  881. if (aIns > 0)
  882. {
  883. pData->audioIn.createNew(aIns);
  884. }
  885. if (aOuts > 0)
  886. {
  887. pData->audioOut.createNew(aOuts);
  888. needsCtrlIn = true;
  889. }
  890. if (cvIns > 0)
  891. {
  892. pData->cvIn.createNew(cvIns);
  893. }
  894. if (cvOuts > 0)
  895. {
  896. pData->cvOut.createNew(cvOuts);
  897. }
  898. if (const uint32_t acIns = aIns + cvIns)
  899. {
  900. fAudioAndCvInBuffers = new float*[acIns];
  901. carla_zeroPointers(fAudioAndCvInBuffers, acIns);
  902. }
  903. if (const uint32_t acOuts = aOuts + cvOuts)
  904. {
  905. fAudioAndCvOutBuffers = new float*[acOuts];
  906. carla_zeroPointers(fAudioAndCvOutBuffers, acOuts);
  907. }
  908. if (mIns > 0)
  909. {
  910. fMidiIn.createNew(mIns);
  911. needsCtrlIn = (mIns == 1);
  912. }
  913. if (mOuts > 0)
  914. {
  915. fMidiOut.createNew(mOuts);
  916. needsCtrlOut = (mOuts == 1);
  917. }
  918. const uint portNameSize(pData->engine->getMaxPortNameSize());
  919. CarlaString portName;
  920. // Audio Ins
  921. for (j=0; j < aIns; ++j)
  922. {
  923. portName.clear();
  924. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  925. {
  926. portName = pData->name;
  927. portName += ":";
  928. }
  929. if (fDescriptor->get_buffer_port_name != nullptr)
  930. {
  931. portName += fDescriptor->get_buffer_port_name(fHandle, forcedStereoIn ? 0 : j, false);
  932. }
  933. else if (aIns > 1 && ! forcedStereoIn)
  934. {
  935. portName += "input_";
  936. portName += CarlaString(j+1);
  937. }
  938. else
  939. portName += "input";
  940. portName.truncate(portNameSize);
  941. pData->audioIn.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true, j);
  942. pData->audioIn.ports[j].rindex = j;
  943. if (forcedStereoIn)
  944. {
  945. portName += "_2";
  946. pData->audioIn.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true, 1);
  947. pData->audioIn.ports[1].rindex = j;
  948. break;
  949. }
  950. }
  951. // Audio Outs
  952. for (j=0; j < aOuts; ++j)
  953. {
  954. portName.clear();
  955. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  956. {
  957. portName = pData->name;
  958. portName += ":";
  959. }
  960. if (fDescriptor->get_buffer_port_name != nullptr)
  961. {
  962. portName += fDescriptor->get_buffer_port_name(fHandle, forcedStereoOut ? 0 : j, true);
  963. }
  964. else if (aOuts > 1 && ! forcedStereoOut)
  965. {
  966. portName += "output_";
  967. portName += CarlaString(j+1);
  968. }
  969. else
  970. portName += "output";
  971. portName.truncate(portNameSize);
  972. pData->audioOut.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false, j);
  973. pData->audioOut.ports[j].rindex = j;
  974. if (forcedStereoOut)
  975. {
  976. portName += "_2";
  977. pData->audioOut.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false, 1);
  978. pData->audioOut.ports[1].rindex = j;
  979. break;
  980. }
  981. }
  982. // CV Ins
  983. for (j=0; j < cvIns; ++j)
  984. {
  985. portName.clear();
  986. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  987. {
  988. portName = pData->name;
  989. portName += ":";
  990. }
  991. if (fDescriptor->get_buffer_port_name != nullptr)
  992. {
  993. portName += fDescriptor->get_buffer_port_name(fHandle, fDescriptor->audioIns + j, false);
  994. }
  995. else if (cvIns > 1)
  996. {
  997. portName += "cv_input_";
  998. portName += CarlaString(j+1);
  999. }
  1000. else
  1001. portName += "cv_input";
  1002. portName.truncate(portNameSize);
  1003. float min = -1.0f, max = 1.0f;
  1004. if (fDescriptor->get_buffer_port_range != nullptr)
  1005. {
  1006. if (const NativePortRange* const range = fDescriptor->get_buffer_port_range(fHandle,
  1007. fDescriptor->audioIns + j,
  1008. false))
  1009. {
  1010. min = range->minimum;
  1011. max = range->maximum;
  1012. }
  1013. }
  1014. pData->cvIn.ports[j].port = (CarlaEngineCVPort*)pData->client->addPort(kEnginePortTypeCV, portName, true, j);
  1015. pData->cvIn.ports[j].rindex = j;
  1016. pData->cvIn.ports[j].port->setRange(min, max);
  1017. }
  1018. // CV Outs
  1019. for (j=0; j < cvOuts; ++j)
  1020. {
  1021. portName.clear();
  1022. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  1023. {
  1024. portName = pData->name;
  1025. portName += ":";
  1026. }
  1027. if (fDescriptor->get_buffer_port_name != nullptr)
  1028. {
  1029. portName += fDescriptor->get_buffer_port_name(fHandle, fDescriptor->audioOuts + j, true);
  1030. }
  1031. else if (cvOuts > 1)
  1032. {
  1033. portName += "cv_output_";
  1034. portName += CarlaString(j+1);
  1035. }
  1036. else
  1037. portName += "cv_output";
  1038. portName.truncate(portNameSize);
  1039. float min = -1.0f, max = 1.0f;
  1040. if (fDescriptor->get_buffer_port_range != nullptr)
  1041. {
  1042. if (const NativePortRange* const range = fDescriptor->get_buffer_port_range(fHandle,
  1043. fDescriptor->audioOuts + j,
  1044. true))
  1045. {
  1046. min = range->minimum;
  1047. max = range->maximum;
  1048. }
  1049. }
  1050. pData->cvOut.ports[j].port = (CarlaEngineCVPort*)pData->client->addPort(kEnginePortTypeCV, portName, false, j);
  1051. pData->cvOut.ports[j].rindex = j;
  1052. pData->cvOut.ports[j].port->setRange(min, max);
  1053. }
  1054. // MIDI Input (only if multiple)
  1055. if (mIns > 1)
  1056. {
  1057. for (j=0; j < mIns; ++j)
  1058. {
  1059. portName.clear();
  1060. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  1061. {
  1062. portName = pData->name;
  1063. portName += ":";
  1064. }
  1065. portName += "midi-in_";
  1066. portName += CarlaString(j+1);
  1067. portName.truncate(portNameSize);
  1068. fMidiIn.ports[j] = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true, j);
  1069. fMidiIn.indexes[j] = j;
  1070. }
  1071. pData->event.portIn = fMidiIn.ports[0];
  1072. }
  1073. // MIDI Output (only if multiple)
  1074. if (mOuts > 1)
  1075. {
  1076. for (j=0; j < mOuts; ++j)
  1077. {
  1078. portName.clear();
  1079. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  1080. {
  1081. portName = pData->name;
  1082. portName += ":";
  1083. }
  1084. portName += "midi-out_";
  1085. portName += CarlaString(j+1);
  1086. portName.truncate(portNameSize);
  1087. fMidiOut.ports[j] = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false, j);
  1088. fMidiOut.indexes[j] = j;
  1089. }
  1090. pData->event.portOut = fMidiOut.ports[0];
  1091. }
  1092. reloadParameters(&needsCtrlIn, &needsCtrlOut);
  1093. if (needsCtrlIn || mIns == 1)
  1094. {
  1095. portName.clear();
  1096. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  1097. {
  1098. portName = pData->name;
  1099. portName += ":";
  1100. }
  1101. portName += "events-in";
  1102. portName.truncate(portNameSize);
  1103. pData->event.portIn = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true, 0);
  1104. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1105. pData->event.cvSourcePorts = pData->client->createCVSourcePorts();
  1106. #endif
  1107. }
  1108. if (needsCtrlOut || mOuts == 1)
  1109. {
  1110. portName.clear();
  1111. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  1112. {
  1113. portName = pData->name;
  1114. portName += ":";
  1115. }
  1116. portName += "events-out";
  1117. portName.truncate(portNameSize);
  1118. pData->event.portOut = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false, 0);
  1119. }
  1120. if (forcedStereoIn || forcedStereoOut)
  1121. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  1122. else
  1123. pData->options &= ~PLUGIN_OPTION_FORCE_STEREO;
  1124. // plugin hints
  1125. pData->hints = 0x0;
  1126. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  1127. pData->hints |= PLUGIN_CAN_DRYWET;
  1128. if (aOuts > 0)
  1129. pData->hints |= PLUGIN_CAN_VOLUME;
  1130. if (aOuts >= 2 && aOuts % 2 == 0)
  1131. pData->hints |= PLUGIN_CAN_BALANCE;
  1132. // native plugin hints
  1133. if (fDescriptor->hints & NATIVE_PLUGIN_IS_RTSAFE)
  1134. pData->hints |= PLUGIN_IS_RTSAFE;
  1135. if (fDescriptor->hints & NATIVE_PLUGIN_IS_SYNTH)
  1136. pData->hints |= PLUGIN_IS_SYNTH;
  1137. if (fDescriptor->hints & NATIVE_PLUGIN_HAS_UI)
  1138. pData->hints |= PLUGIN_HAS_CUSTOM_UI;
  1139. if (fDescriptor->hints & NATIVE_PLUGIN_NEEDS_FIXED_BUFFERS)
  1140. pData->hints |= PLUGIN_NEEDS_FIXED_BUFFERS;
  1141. if (fDescriptor->hints & NATIVE_PLUGIN_NEEDS_UI_MAIN_THREAD)
  1142. pData->hints |= PLUGIN_NEEDS_UI_MAIN_THREAD;
  1143. if (fDescriptor->hints & NATIVE_PLUGIN_USES_MULTI_PROGS)
  1144. pData->hints |= PLUGIN_USES_MULTI_PROGS;
  1145. if (fDescriptor->hints & NATIVE_PLUGIN_HAS_INLINE_DISPLAY)
  1146. pData->hints |= PLUGIN_HAS_INLINE_DISPLAY;
  1147. // extra plugin hints
  1148. pData->extraHints = 0x0;
  1149. bufferSizeChanged(pData->engine->getBufferSize());
  1150. reloadPrograms(true);
  1151. if (pData->active)
  1152. activate();
  1153. carla_debug("CarlaPluginNative::reload() - end");
  1154. }
  1155. void reloadParameters(bool* const needsCtrlIn, bool* const needsCtrlOut)
  1156. {
  1157. carla_debug("CarlaPluginNative::reloadParameters() - start");
  1158. const float sampleRate = static_cast<float>(pData->engine->getSampleRate());
  1159. const uint32_t params = (fDescriptor->get_parameter_count != nullptr && fDescriptor->get_parameter_info != nullptr)
  1160. ? fDescriptor->get_parameter_count(fHandle)
  1161. : 0;
  1162. pData->param.clear();
  1163. if (params > 0)
  1164. {
  1165. pData->param.createNew(params, true);
  1166. }
  1167. for (uint32_t j=0; j < params; ++j)
  1168. {
  1169. const NativeParameter* const paramInfo(fDescriptor->get_parameter_info(fHandle, j));
  1170. CARLA_SAFE_ASSERT_CONTINUE(paramInfo != nullptr);
  1171. pData->param.data[j].type = PARAMETER_UNKNOWN;
  1172. pData->param.data[j].index = static_cast<int32_t>(j);
  1173. pData->param.data[j].rindex = static_cast<int32_t>(j);
  1174. float min, max, def, step, stepSmall, stepLarge;
  1175. // min value
  1176. min = paramInfo->ranges.min;
  1177. // max value
  1178. max = paramInfo->ranges.max;
  1179. if (min > max)
  1180. max = min;
  1181. if (carla_isEqual(min, max))
  1182. {
  1183. carla_stderr2("WARNING - Broken plugin parameter '%s': max == min", paramInfo->name);
  1184. max = min + 0.1f;
  1185. }
  1186. // default value
  1187. def = paramInfo->ranges.def;
  1188. if (def < min)
  1189. def = min;
  1190. else if (def > max)
  1191. def = max;
  1192. if (paramInfo->hints & NATIVE_PARAMETER_USES_SAMPLE_RATE)
  1193. {
  1194. min *= sampleRate;
  1195. max *= sampleRate;
  1196. def *= sampleRate;
  1197. pData->param.data[j].hints |= PARAMETER_USES_SAMPLERATE;
  1198. }
  1199. if (paramInfo->hints & NATIVE_PARAMETER_IS_BOOLEAN)
  1200. {
  1201. step = max - min;
  1202. stepSmall = step;
  1203. stepLarge = step;
  1204. pData->param.data[j].hints |= PARAMETER_IS_BOOLEAN;
  1205. }
  1206. else if (paramInfo->hints & NATIVE_PARAMETER_IS_INTEGER)
  1207. {
  1208. step = 1.0f;
  1209. stepSmall = 1.0f;
  1210. stepLarge = 10.0f;
  1211. pData->param.data[j].hints |= PARAMETER_IS_INTEGER;
  1212. }
  1213. else
  1214. {
  1215. float range = max - min;
  1216. step = range/100.0f;
  1217. stepSmall = range/1000.0f;
  1218. stepLarge = range/10.0f;
  1219. }
  1220. if (paramInfo->hints & NATIVE_PARAMETER_IS_OUTPUT)
  1221. {
  1222. pData->param.data[j].type = PARAMETER_OUTPUT;
  1223. if (needsCtrlOut != nullptr)
  1224. *needsCtrlOut = true;
  1225. }
  1226. else
  1227. {
  1228. pData->param.data[j].type = PARAMETER_INPUT;
  1229. if (needsCtrlIn != nullptr)
  1230. *needsCtrlIn = true;
  1231. }
  1232. // extra parameter hints
  1233. if (paramInfo->hints & NATIVE_PARAMETER_IS_ENABLED)
  1234. {
  1235. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  1236. if (paramInfo->hints & NATIVE_PARAMETER_IS_AUTOMATABLE)
  1237. {
  1238. pData->param.data[j].hints |= PARAMETER_IS_AUTOMATABLE;
  1239. pData->param.data[j].hints |= PARAMETER_CAN_BE_CV_CONTROLLED;
  1240. }
  1241. }
  1242. if (paramInfo->hints & NATIVE_PARAMETER_IS_LOGARITHMIC)
  1243. pData->param.data[j].hints |= PARAMETER_IS_LOGARITHMIC;
  1244. if (paramInfo->hints & NATIVE_PARAMETER_USES_SCALEPOINTS)
  1245. pData->param.data[j].hints |= PARAMETER_USES_SCALEPOINTS;
  1246. pData->param.ranges[j].min = min;
  1247. pData->param.ranges[j].max = max;
  1248. pData->param.ranges[j].def = def;
  1249. pData->param.ranges[j].step = step;
  1250. pData->param.ranges[j].stepSmall = stepSmall;
  1251. pData->param.ranges[j].stepLarge = stepLarge;
  1252. }
  1253. carla_debug("CarlaPluginNative::reloadParameters() - end");
  1254. }
  1255. void reloadPrograms(const bool doInit) override
  1256. {
  1257. carla_debug("CarlaPluginNative::reloadPrograms(%s)", bool2str(doInit));
  1258. uint32_t i, oldCount = pData->midiprog.count;
  1259. const int32_t current = pData->midiprog.current;
  1260. // Delete old programs
  1261. pData->midiprog.clear();
  1262. // Query new programs
  1263. uint32_t count = 0;
  1264. if (fDescriptor->get_midi_program_count != nullptr && fDescriptor->get_midi_program_info != nullptr && fDescriptor->set_midi_program != nullptr)
  1265. count = fDescriptor->get_midi_program_count(fHandle);
  1266. if (count > 0)
  1267. {
  1268. pData->midiprog.createNew(count);
  1269. // Update data
  1270. for (i=0; i < count; ++i)
  1271. {
  1272. const NativeMidiProgram* const mpDesc(fDescriptor->get_midi_program_info(fHandle, i));
  1273. CARLA_SAFE_ASSERT_CONTINUE(mpDesc != nullptr);
  1274. pData->midiprog.data[i].bank = mpDesc->bank;
  1275. pData->midiprog.data[i].program = mpDesc->program;
  1276. pData->midiprog.data[i].name = carla_strdup(mpDesc->name);
  1277. }
  1278. }
  1279. if (doInit)
  1280. {
  1281. if (count > 0)
  1282. setMidiProgram(0, false, false, false, true);
  1283. }
  1284. else
  1285. {
  1286. // Check if current program is invalid
  1287. bool programChanged = false;
  1288. if (count == oldCount+1)
  1289. {
  1290. // one midi program added, probably created by user
  1291. pData->midiprog.current = static_cast<int32_t>(oldCount);
  1292. programChanged = true;
  1293. }
  1294. else if (current < 0 && count > 0)
  1295. {
  1296. // programs exist now, but not before
  1297. pData->midiprog.current = 0;
  1298. programChanged = true;
  1299. }
  1300. else if (current >= 0 && count == 0)
  1301. {
  1302. // programs existed before, but not anymore
  1303. pData->midiprog.current = -1;
  1304. programChanged = true;
  1305. }
  1306. else if (current >= static_cast<int32_t>(count))
  1307. {
  1308. // current midi program > count
  1309. pData->midiprog.current = 0;
  1310. programChanged = true;
  1311. }
  1312. else
  1313. {
  1314. // no change
  1315. pData->midiprog.current = current;
  1316. }
  1317. if (programChanged)
  1318. setMidiProgram(pData->midiprog.current, true, true, true, false);
  1319. pData->engine->callback(true, true,
  1320. ENGINE_CALLBACK_RELOAD_PROGRAMS,
  1321. pData->id,
  1322. 0, 0, 0, 0.0f, nullptr);
  1323. }
  1324. }
  1325. // -------------------------------------------------------------------
  1326. // Plugin processing
  1327. void activate() noexcept override
  1328. {
  1329. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1330. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1331. if (fDescriptor->activate != nullptr)
  1332. {
  1333. try {
  1334. fDescriptor->activate(fHandle);
  1335. } catch(...) {}
  1336. if (fHandle2 != nullptr)
  1337. {
  1338. try {
  1339. fDescriptor->activate(fHandle2);
  1340. } catch(...) {}
  1341. }
  1342. }
  1343. }
  1344. void deactivate() noexcept override
  1345. {
  1346. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1347. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1348. if (fDescriptor->deactivate != nullptr)
  1349. {
  1350. try {
  1351. fDescriptor->deactivate(fHandle);
  1352. } catch(...) {}
  1353. if (fHandle2 != nullptr)
  1354. {
  1355. try {
  1356. fDescriptor->deactivate(fHandle2);
  1357. } catch(...) {}
  1358. }
  1359. }
  1360. }
  1361. EngineEvent& findNextEvent()
  1362. {
  1363. if (fMidiIn.count == 1)
  1364. {
  1365. NativePluginMidiInData::MultiPortData& multiportData(fMidiIn.multiportData[0]);
  1366. if (multiportData.usedIndex == multiportData.cachedEventCount)
  1367. {
  1368. const uint32_t eventCount = pData->event.portIn->getEventCount();
  1369. CARLA_SAFE_ASSERT_INT2(eventCount == multiportData.cachedEventCount,
  1370. eventCount, multiportData.cachedEventCount);
  1371. return kNullEngineEvent;
  1372. }
  1373. return pData->event.portIn->getEvent(multiportData.usedIndex++);
  1374. }
  1375. uint32_t lowestSampleTime = 9999999;
  1376. uint32_t portMatching = 0;
  1377. bool found = false;
  1378. // process events in order for multiple ports
  1379. for (uint32_t m=0; m < fMidiIn.count; ++m)
  1380. {
  1381. CarlaEngineEventPort* const eventPort(fMidiIn.ports[m]);
  1382. NativePluginMidiInData::MultiPortData& multiportData(fMidiIn.multiportData[m]);
  1383. if (multiportData.usedIndex == multiportData.cachedEventCount)
  1384. continue;
  1385. const EngineEvent& event(eventPort->getEventUnchecked(multiportData.usedIndex));
  1386. if (event.time < lowestSampleTime)
  1387. {
  1388. lowestSampleTime = event.time;
  1389. portMatching = m;
  1390. found = true;
  1391. }
  1392. }
  1393. if (found)
  1394. {
  1395. CarlaEngineEventPort* const eventPort(fMidiIn.ports[portMatching]);
  1396. NativePluginMidiInData::MultiPortData& multiportData(fMidiIn.multiportData[portMatching]);
  1397. return eventPort->getEvent(multiportData.usedIndex++);
  1398. }
  1399. return kNullEngineEvent;
  1400. }
  1401. void process(const float* const* const audioIn, float** const audioOut,
  1402. const float* const* const cvIn, float** const cvOut, const uint32_t frames) override
  1403. {
  1404. // --------------------------------------------------------------------------------------------------------
  1405. // Check if active
  1406. if (! pData->active)
  1407. {
  1408. // disable any output sound
  1409. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1410. carla_zeroFloats(audioOut[i], frames);
  1411. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  1412. carla_zeroFloats(cvOut[i], frames);
  1413. return;
  1414. }
  1415. fMidiEventInCount = fMidiEventOutCount = 0;
  1416. carla_zeroStructs(fMidiInEvents, kPluginMaxMidiEvents);
  1417. carla_zeroStructs(fMidiOutEvents, kPluginMaxMidiEvents);
  1418. // --------------------------------------------------------------------------------------------------------
  1419. // Check if needs reset
  1420. if (pData->needsReset)
  1421. {
  1422. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1423. {
  1424. fMidiEventInCount = MAX_MIDI_CHANNELS*2;
  1425. for (uint8_t k=0, i=MAX_MIDI_CHANNELS; k < MAX_MIDI_CHANNELS; ++k)
  1426. {
  1427. fMidiInEvents[k].data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (k & MIDI_CHANNEL_BIT));
  1428. fMidiInEvents[k].data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  1429. fMidiInEvents[k].data[2] = 0;
  1430. fMidiInEvents[k].size = 3;
  1431. fMidiInEvents[k+i].data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (k & MIDI_CHANNEL_BIT));
  1432. fMidiInEvents[k+i].data[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  1433. fMidiInEvents[k+i].data[2] = 0;
  1434. fMidiInEvents[k+i].size = 3;
  1435. }
  1436. }
  1437. else if (pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  1438. {
  1439. fMidiEventInCount = MAX_MIDI_NOTE;
  1440. for (uint8_t k=0; k < MAX_MIDI_NOTE; ++k)
  1441. {
  1442. fMidiInEvents[k].data[0] = uint8_t(MIDI_STATUS_NOTE_OFF | (pData->ctrlChannel & MIDI_CHANNEL_BIT));
  1443. fMidiInEvents[k].data[1] = k;
  1444. fMidiInEvents[k].data[2] = 0;
  1445. fMidiInEvents[k].size = 3;
  1446. }
  1447. }
  1448. pData->needsReset = false;
  1449. }
  1450. // --------------------------------------------------------------------------------------------------------
  1451. // Set TimeInfo
  1452. const EngineTimeInfo timeInfo(pData->engine->getTimeInfo());
  1453. fTimeInfo.playing = timeInfo.playing;
  1454. fTimeInfo.frame = timeInfo.frame;
  1455. fTimeInfo.usecs = timeInfo.usecs;
  1456. if (timeInfo.bbt.valid)
  1457. {
  1458. fTimeInfo.bbt.valid = true;
  1459. fTimeInfo.bbt.bar = timeInfo.bbt.bar;
  1460. fTimeInfo.bbt.beat = timeInfo.bbt.beat;
  1461. fTimeInfo.bbt.tick = timeInfo.bbt.tick;
  1462. fTimeInfo.bbt.barStartTick = timeInfo.bbt.barStartTick;
  1463. fTimeInfo.bbt.beatsPerBar = timeInfo.bbt.beatsPerBar;
  1464. fTimeInfo.bbt.beatType = timeInfo.bbt.beatType;
  1465. fTimeInfo.bbt.ticksPerBeat = timeInfo.bbt.ticksPerBeat;
  1466. fTimeInfo.bbt.beatsPerMinute = timeInfo.bbt.beatsPerMinute;
  1467. }
  1468. else
  1469. {
  1470. fTimeInfo.bbt.valid = false;
  1471. }
  1472. #if 0
  1473. // This test code has proven to be quite useful
  1474. // So I am leaving it behind, I might need it again..
  1475. if (pData->id == 1)
  1476. {
  1477. static int64_t last_frame = timeInfo.frame;
  1478. static int64_t last_dev_frame = 0;
  1479. static double last_val = timeInfo.bbt.barStartTick + ((timeInfo.bbt.beat-1) * timeInfo.bbt.ticksPerBeat) + timeInfo.bbt.tick;
  1480. static double last_dev_val = 0.0;
  1481. int64_t cur_frame = timeInfo.frame;
  1482. int64_t cur_dev_frame = cur_frame - last_frame;
  1483. double cur_val = timeInfo.bbt.barStartTick + ((timeInfo.bbt.beat-1) * timeInfo.bbt.ticksPerBeat) + timeInfo.bbt.tick;
  1484. double cur_dev_val = cur_val - last_val;
  1485. if (std::abs(last_dev_val - cur_dev_val) >= 0.0001 || last_dev_frame != cur_dev_frame)
  1486. {
  1487. carla_stdout("currently %u at %u => %f : DEV1: %li : DEV2: %f",
  1488. frames,
  1489. timeInfo.frame,
  1490. cur_val,
  1491. cur_dev_frame,
  1492. cur_dev_val);
  1493. }
  1494. last_val = cur_val;
  1495. last_dev_val = cur_dev_val;
  1496. last_frame = cur_frame;
  1497. last_dev_frame = cur_dev_frame;
  1498. }
  1499. #endif
  1500. // --------------------------------------------------------------------------------------------------------
  1501. // Event Input and Processing
  1502. if (pData->event.portIn != nullptr)
  1503. {
  1504. // ----------------------------------------------------------------------------------------------------
  1505. // MIDI Input (External)
  1506. if (pData->extNotes.mutex.tryLock())
  1507. {
  1508. ExternalMidiNote note = { 0, 0, 0 };
  1509. for (; fMidiEventInCount < kPluginMaxMidiEvents && ! pData->extNotes.data.isEmpty();)
  1510. {
  1511. note = pData->extNotes.data.getFirst(note, true);
  1512. CARLA_SAFE_ASSERT_CONTINUE(note.channel >= 0 && note.channel < MAX_MIDI_CHANNELS);
  1513. NativeMidiEvent& nativeEvent(fMidiInEvents[fMidiEventInCount++]);
  1514. nativeEvent.data[0] = uint8_t((note.velo > 0 ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF) | (note.channel & MIDI_CHANNEL_BIT));
  1515. nativeEvent.data[1] = note.note;
  1516. nativeEvent.data[2] = note.velo;
  1517. nativeEvent.size = 3;
  1518. }
  1519. pData->extNotes.mutex.unlock();
  1520. } // End of MIDI Input (External)
  1521. // ----------------------------------------------------------------------------------------------------
  1522. // Event Input (System)
  1523. #ifndef BUILD_BRIDGE
  1524. bool allNotesOffSent = false;
  1525. #endif
  1526. const bool isSampleAccurate = (pData->options & PLUGIN_OPTION_FIXED_BUFFERS) == 0;
  1527. uint32_t startTime = 0;
  1528. uint32_t timeOffset = 0;
  1529. uint32_t nextBankId;
  1530. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  1531. nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
  1532. else
  1533. nextBankId = 0;
  1534. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1535. if (cvIn != nullptr && pData->event.cvSourcePorts != nullptr)
  1536. pData->event.cvSourcePorts->initPortBuffers(cvIn + pData->cvIn.count, frames, isSampleAccurate, pData->event.portIn);
  1537. #endif
  1538. for (;;)
  1539. {
  1540. EngineEvent& event(findNextEvent());
  1541. if (event.type == kEngineEventTypeNull)
  1542. break;
  1543. uint32_t eventTime = event.time;
  1544. CARLA_SAFE_ASSERT_UINT2_CONTINUE(eventTime < frames, eventTime, frames);
  1545. if (eventTime < timeOffset)
  1546. {
  1547. carla_stderr2("Timing error, eventTime:%u < timeOffset:%u for '%s'",
  1548. eventTime, timeOffset, pData->name);
  1549. eventTime = timeOffset;
  1550. }
  1551. if (isSampleAccurate && eventTime > timeOffset)
  1552. {
  1553. if (processSingle(audioIn, audioOut, cvIn, cvOut, eventTime - timeOffset, timeOffset))
  1554. {
  1555. startTime = 0;
  1556. timeOffset = eventTime;
  1557. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  1558. nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
  1559. else
  1560. nextBankId = 0;
  1561. if (fMidiEventInCount > 0)
  1562. {
  1563. carla_zeroStructs(fMidiInEvents, fMidiEventInCount);
  1564. fMidiEventInCount = 0;
  1565. }
  1566. if (fMidiEventOutCount > 0)
  1567. {
  1568. carla_zeroStructs(fMidiOutEvents, fMidiEventOutCount);
  1569. fMidiEventOutCount = 0;
  1570. }
  1571. }
  1572. else
  1573. startTime += timeOffset;
  1574. }
  1575. // Control change
  1576. switch (event.type)
  1577. {
  1578. case kEngineEventTypeNull:
  1579. break;
  1580. case kEngineEventTypeControl: {
  1581. EngineControlEvent& ctrlEvent(event.ctrl);
  1582. switch (ctrlEvent.type)
  1583. {
  1584. case kEngineControlEventTypeNull:
  1585. break;
  1586. case kEngineControlEventTypeParameter: {
  1587. float value;
  1588. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1589. // non-midi
  1590. if (event.channel == kEngineEventNonMidiChannel)
  1591. {
  1592. const uint32_t k = ctrlEvent.param;
  1593. CARLA_SAFE_ASSERT_CONTINUE(k < pData->param.count);
  1594. ctrlEvent.handled = true;
  1595. value = pData->param.getFinalUnnormalizedValue(k, ctrlEvent.normalizedValue);
  1596. setParameterValueRT(k, value, true);
  1597. continue;
  1598. }
  1599. // Control backend stuff
  1600. if (event.channel == pData->ctrlChannel)
  1601. {
  1602. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_DRYWET) > 0)
  1603. {
  1604. ctrlEvent.handled = true;
  1605. value = ctrlEvent.normalizedValue;
  1606. setDryWetRT(value, true);
  1607. }
  1608. else if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_VOLUME) > 0)
  1609. {
  1610. ctrlEvent.handled = true;
  1611. value = ctrlEvent.normalizedValue*127.0f/100.0f;
  1612. setVolumeRT(value, true);
  1613. }
  1614. else if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_BALANCE) > 0)
  1615. {
  1616. float left, right;
  1617. value = ctrlEvent.normalizedValue/0.5f - 1.0f;
  1618. if (value < 0.0f)
  1619. {
  1620. left = -1.0f;
  1621. right = (value*2.0f)+1.0f;
  1622. }
  1623. else if (value > 0.0f)
  1624. {
  1625. left = (value*2.0f)-1.0f;
  1626. right = 1.0f;
  1627. }
  1628. else
  1629. {
  1630. left = -1.0f;
  1631. right = 1.0f;
  1632. }
  1633. ctrlEvent.handled = true;
  1634. setBalanceLeftRT(left, true);
  1635. setBalanceRightRT(right, true);
  1636. }
  1637. }
  1638. #endif
  1639. // Control plugin parameters
  1640. for (uint32_t k=0; k < pData->param.count; ++k)
  1641. {
  1642. if (pData->param.data[k].midiChannel != event.channel)
  1643. continue;
  1644. if (pData->param.data[k].mappedControlIndex != ctrlEvent.param)
  1645. continue;
  1646. if (pData->param.data[k].type != PARAMETER_INPUT)
  1647. continue;
  1648. if ((pData->param.data[k].hints & PARAMETER_IS_AUTOMATABLE) == 0)
  1649. continue;
  1650. ctrlEvent.handled = true;
  1651. value = pData->param.getFinalUnnormalizedValue(k, ctrlEvent.normalizedValue);
  1652. setParameterValueRT(k, value, true);
  1653. }
  1654. if ((pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0 && ctrlEvent.param < MAX_MIDI_VALUE)
  1655. {
  1656. if (fMidiEventInCount >= kPluginMaxMidiEvents)
  1657. continue;
  1658. NativeMidiEvent& nativeEvent(fMidiInEvents[fMidiEventInCount++]);
  1659. carla_zeroStruct(nativeEvent);
  1660. nativeEvent.time = isSampleAccurate ? startTime : eventTime;
  1661. nativeEvent.data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  1662. nativeEvent.data[1] = uint8_t(ctrlEvent.param);
  1663. nativeEvent.data[2] = uint8_t(ctrlEvent.normalizedValue*127.0f);
  1664. nativeEvent.size = 3;
  1665. }
  1666. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1667. if (! ctrlEvent.handled)
  1668. checkForMidiLearn(event);
  1669. #endif
  1670. break;
  1671. } // case kEngineControlEventTypeParameter
  1672. case kEngineControlEventTypeMidiBank:
  1673. if (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES)
  1674. {
  1675. if (event.channel == pData->ctrlChannel)
  1676. nextBankId = ctrlEvent.param;
  1677. }
  1678. else if (pData->options & PLUGIN_OPTION_SEND_PROGRAM_CHANGES)
  1679. {
  1680. if (fMidiEventInCount >= kPluginMaxMidiEvents)
  1681. continue;
  1682. NativeMidiEvent& nativeEvent(fMidiInEvents[fMidiEventInCount++]);
  1683. carla_zeroStruct(nativeEvent);
  1684. nativeEvent.time = isSampleAccurate ? startTime : eventTime;
  1685. nativeEvent.data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  1686. nativeEvent.data[1] = MIDI_CONTROL_BANK_SELECT;
  1687. nativeEvent.data[2] = uint8_t(ctrlEvent.param);
  1688. nativeEvent.size = 3;
  1689. }
  1690. break;
  1691. case kEngineControlEventTypeMidiProgram:
  1692. if (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES)
  1693. {
  1694. if (event.channel < MAX_MIDI_CHANNELS)
  1695. {
  1696. const uint32_t nextProgramId(ctrlEvent.param);
  1697. for (uint32_t k=0; k < pData->midiprog.count; ++k)
  1698. {
  1699. if (pData->midiprog.data[k].bank == nextBankId && pData->midiprog.data[k].program == nextProgramId)
  1700. {
  1701. fDescriptor->set_midi_program(fHandle, event.channel, nextBankId, nextProgramId);
  1702. if (fHandle2 != nullptr)
  1703. fDescriptor->set_midi_program(fHandle2, event.channel, nextBankId, nextProgramId);
  1704. fCurMidiProgs[event.channel] = static_cast<int32_t>(k);
  1705. if (event.channel == pData->ctrlChannel)
  1706. {
  1707. pData->postponeMidiProgramChangeRtEvent(true, k);
  1708. }
  1709. break;
  1710. }
  1711. }
  1712. }
  1713. }
  1714. else if (pData->options & PLUGIN_OPTION_SEND_PROGRAM_CHANGES)
  1715. {
  1716. if (fMidiEventInCount >= kPluginMaxMidiEvents)
  1717. continue;
  1718. NativeMidiEvent& nativeEvent(fMidiInEvents[fMidiEventInCount++]);
  1719. carla_zeroStruct(nativeEvent);
  1720. nativeEvent.time = isSampleAccurate ? startTime : eventTime;
  1721. nativeEvent.data[0] = uint8_t(MIDI_STATUS_PROGRAM_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  1722. nativeEvent.data[1] = uint8_t(ctrlEvent.param);
  1723. nativeEvent.size = 2;
  1724. }
  1725. break;
  1726. case kEngineControlEventTypeAllSoundOff:
  1727. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1728. {
  1729. if (fMidiEventInCount >= kPluginMaxMidiEvents)
  1730. continue;
  1731. NativeMidiEvent& nativeEvent(fMidiInEvents[fMidiEventInCount++]);
  1732. carla_zeroStruct(nativeEvent);
  1733. nativeEvent.time = isSampleAccurate ? startTime : eventTime;
  1734. nativeEvent.data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  1735. nativeEvent.data[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  1736. nativeEvent.data[2] = 0;
  1737. nativeEvent.size = 3;
  1738. }
  1739. break;
  1740. case kEngineControlEventTypeAllNotesOff:
  1741. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1742. {
  1743. #ifndef BUILD_BRIDGE
  1744. if (event.channel == pData->ctrlChannel && ! allNotesOffSent)
  1745. {
  1746. allNotesOffSent = true;
  1747. postponeRtAllNotesOff();
  1748. }
  1749. #endif
  1750. if (fMidiEventInCount >= kPluginMaxMidiEvents)
  1751. continue;
  1752. NativeMidiEvent& nativeEvent(fMidiInEvents[fMidiEventInCount++]);
  1753. carla_zeroStruct(nativeEvent);
  1754. nativeEvent.time = isSampleAccurate ? startTime : eventTime;
  1755. nativeEvent.data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  1756. nativeEvent.data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  1757. nativeEvent.data[2] = 0;
  1758. nativeEvent.size = 3;
  1759. }
  1760. break;
  1761. }
  1762. break;
  1763. }
  1764. case kEngineEventTypeMidi: {
  1765. if (fMidiEventInCount >= kPluginMaxMidiEvents)
  1766. continue;
  1767. const EngineMidiEvent& midiEvent(event.midi);
  1768. if (midiEvent.size > 4)
  1769. continue;
  1770. #ifdef CARLA_PROPER_CPP11_SUPPORT
  1771. static_assert(4 <= EngineMidiEvent::kDataSize, "Incorrect data");
  1772. #endif
  1773. uint8_t status = uint8_t(MIDI_GET_STATUS_FROM_DATA(midiEvent.data));
  1774. if ((status == MIDI_STATUS_NOTE_OFF || status == MIDI_STATUS_NOTE_ON) && (pData->options & PLUGIN_OPTION_SKIP_SENDING_NOTES))
  1775. continue;
  1776. if (status == MIDI_STATUS_CHANNEL_PRESSURE && (pData->options & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) == 0)
  1777. continue;
  1778. if (status == MIDI_STATUS_CONTROL_CHANGE && (pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) == 0)
  1779. continue;
  1780. if (status == MIDI_STATUS_POLYPHONIC_AFTERTOUCH && (pData->options & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) == 0)
  1781. continue;
  1782. if (status == MIDI_STATUS_PITCH_WHEEL_CONTROL && (pData->options & PLUGIN_OPTION_SEND_PITCHBEND) == 0)
  1783. continue;
  1784. // Fix bad note-off
  1785. if (status == MIDI_STATUS_NOTE_ON && midiEvent.data[2] == 0)
  1786. status = MIDI_STATUS_NOTE_OFF;
  1787. NativeMidiEvent& nativeEvent(fMidiInEvents[fMidiEventInCount++]);
  1788. carla_zeroStruct(nativeEvent);
  1789. nativeEvent.port = midiEvent.port;
  1790. nativeEvent.time = isSampleAccurate ? startTime : eventTime;
  1791. nativeEvent.size = midiEvent.size;
  1792. nativeEvent.data[0] = uint8_t(status | (event.channel & MIDI_CHANNEL_BIT));
  1793. nativeEvent.data[1] = midiEvent.size >= 2 ? midiEvent.data[1] : 0;
  1794. nativeEvent.data[2] = midiEvent.size >= 3 ? midiEvent.data[2] : 0;
  1795. nativeEvent.data[3] = midiEvent.size == 4 ? midiEvent.data[3] : 0;
  1796. if (status == MIDI_STATUS_NOTE_ON)
  1797. {
  1798. pData->postponeNoteOnRtEvent(true, event.channel, midiEvent.data[1], midiEvent.data[2]);
  1799. }
  1800. else if (status == MIDI_STATUS_NOTE_OFF)
  1801. {
  1802. pData->postponeNoteOffRtEvent(true, event.channel, midiEvent.data[1]);
  1803. }
  1804. } break;
  1805. } // switch (event.type)
  1806. }
  1807. pData->postRtEvents.trySplice();
  1808. if (frames > timeOffset)
  1809. processSingle(audioIn, audioOut, cvIn, cvOut, frames - timeOffset, timeOffset);
  1810. } // End of Event Input and Processing
  1811. // --------------------------------------------------------------------------------------------------------
  1812. // Plugin processing (no events)
  1813. else
  1814. {
  1815. processSingle(audioIn, audioOut, cvIn, cvOut, frames, 0);
  1816. } // End of Plugin processing (no events)
  1817. #ifndef BUILD_BRIDGE
  1818. // --------------------------------------------------------------------------------------------------------
  1819. // Control Output
  1820. if (pData->event.portOut != nullptr)
  1821. {
  1822. float value, curValue;
  1823. for (uint32_t k=0; k < pData->param.count; ++k)
  1824. {
  1825. if (pData->param.data[k].type != PARAMETER_OUTPUT)
  1826. continue;
  1827. curValue = fDescriptor->get_parameter_value(fHandle, k);
  1828. pData->param.ranges[k].fixValue(curValue);
  1829. if (pData->param.data[k].mappedControlIndex > 0)
  1830. {
  1831. value = pData->param.ranges[k].getNormalizedValue(curValue);
  1832. pData->event.portOut->writeControlEvent(0,
  1833. pData->param.data[k].midiChannel,
  1834. kEngineControlEventTypeParameter,
  1835. static_cast<uint16_t>(pData->param.data[k].mappedControlIndex),
  1836. -1,
  1837. value);
  1838. }
  1839. }
  1840. } // End of Control Output
  1841. #endif
  1842. }
  1843. bool processSingle(const float* const* const audioIn, float** const audioOut,
  1844. const float* const* const cvIn, float** const cvOut,
  1845. const uint32_t frames, const uint32_t timeOffset)
  1846. {
  1847. CARLA_SAFE_ASSERT_RETURN(frames > 0, false);
  1848. if (pData->audioIn.count > 0) {
  1849. CARLA_SAFE_ASSERT_RETURN(audioIn != nullptr, false);
  1850. }
  1851. if (pData->audioOut.count > 0) {
  1852. CARLA_SAFE_ASSERT_RETURN(audioOut != nullptr, false);
  1853. }
  1854. if (pData->cvIn.count > 0) {
  1855. CARLA_SAFE_ASSERT_RETURN(cvIn != nullptr, false);
  1856. }
  1857. if (pData->cvOut.count > 0) {
  1858. CARLA_SAFE_ASSERT_RETURN(cvOut != nullptr, false);
  1859. }
  1860. // --------------------------------------------------------------------------------------------------------
  1861. // Try lock, silence otherwise
  1862. if (fIsOffline)
  1863. {
  1864. pData->singleMutex.lock();
  1865. }
  1866. else if (! pData->singleMutex.tryLock())
  1867. {
  1868. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1869. {
  1870. for (uint32_t k=0; k < frames; ++k)
  1871. audioOut[i][k+timeOffset] = 0.0f;
  1872. }
  1873. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  1874. {
  1875. for (uint32_t k=0; k < frames; ++k)
  1876. cvOut[i][k+timeOffset] = 0.0f;
  1877. }
  1878. return false;
  1879. }
  1880. // --------------------------------------------------------------------------------------------------------
  1881. // Set audio buffers
  1882. {
  1883. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1884. carla_copyFloats(fAudioAndCvInBuffers[i], audioIn[i]+timeOffset, frames);
  1885. for (uint32_t i=0; i < pData->cvIn.count; ++i)
  1886. carla_copyFloats(fAudioAndCvInBuffers[pData->audioIn.count+i], cvIn[i]+timeOffset, frames);
  1887. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1888. carla_zeroFloats(fAudioAndCvOutBuffers[i], frames);
  1889. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  1890. carla_zeroFloats(fAudioAndCvOutBuffers[pData->audioOut.count+i], frames);
  1891. }
  1892. // --------------------------------------------------------------------------------------------------------
  1893. // Run plugin
  1894. fIsProcessing = true;
  1895. if (fHandle2 == nullptr)
  1896. {
  1897. fDescriptor->process(fHandle,
  1898. fAudioAndCvInBuffers, fAudioAndCvOutBuffers, frames,
  1899. fMidiInEvents, fMidiEventInCount);
  1900. }
  1901. else
  1902. {
  1903. fDescriptor->process(fHandle,
  1904. (fAudioAndCvInBuffers != nullptr) ? &fAudioAndCvInBuffers[0] : nullptr,
  1905. (fAudioAndCvOutBuffers != nullptr) ? &fAudioAndCvOutBuffers[0] : nullptr,
  1906. frames, fMidiInEvents, fMidiEventInCount);
  1907. fDescriptor->process(fHandle2,
  1908. (fAudioAndCvInBuffers != nullptr) ? &fAudioAndCvInBuffers[1] : nullptr,
  1909. (fAudioAndCvOutBuffers != nullptr) ? &fAudioAndCvOutBuffers[1] : nullptr,
  1910. frames, fMidiInEvents, fMidiEventInCount);
  1911. }
  1912. fIsProcessing = false;
  1913. if (fTimeInfo.playing)
  1914. fTimeInfo.frame += frames;
  1915. uint32_t i=0;
  1916. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1917. // --------------------------------------------------------------------------------------------------------
  1918. // Post-processing (dry/wet, volume and balance)
  1919. {
  1920. const bool doDryWet = (pData->hints & PLUGIN_CAN_DRYWET) != 0 && carla_isNotEqual(pData->postProc.dryWet, 1.0f);
  1921. const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) != 0 && ! (carla_isEqual(pData->postProc.balanceLeft, -1.0f) && carla_isEqual(pData->postProc.balanceRight, 1.0f));
  1922. bool isPair;
  1923. float bufValue, oldBufLeft[doBalance ? frames : 1];
  1924. for (; i < pData->audioOut.count; ++i)
  1925. {
  1926. // Dry/Wet
  1927. if (doDryWet)
  1928. {
  1929. for (uint32_t k=0; k < frames; ++k)
  1930. {
  1931. bufValue = fAudioAndCvInBuffers[(pData->audioIn.count == 1) ? 0 : i][k];
  1932. fAudioAndCvOutBuffers[i][k] = (fAudioAndCvOutBuffers[i][k] * pData->postProc.dryWet) + (bufValue * (1.0f - pData->postProc.dryWet));
  1933. }
  1934. }
  1935. // Balance
  1936. if (doBalance)
  1937. {
  1938. isPair = (i % 2 == 0);
  1939. if (isPair)
  1940. {
  1941. CARLA_ASSERT(i+1 < pData->audioOut.count);
  1942. carla_copyFloats(oldBufLeft, fAudioAndCvOutBuffers[i], frames);
  1943. }
  1944. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  1945. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  1946. for (uint32_t k=0; k < frames; ++k)
  1947. {
  1948. if (isPair)
  1949. {
  1950. // left
  1951. fAudioAndCvOutBuffers[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  1952. fAudioAndCvOutBuffers[i][k] += fAudioAndCvOutBuffers[i+1][k] * (1.0f - balRangeR);
  1953. }
  1954. else
  1955. {
  1956. // right
  1957. fAudioAndCvOutBuffers[i][k] = fAudioAndCvOutBuffers[i][k] * balRangeR;
  1958. fAudioAndCvOutBuffers[i][k] += oldBufLeft[k] * balRangeL;
  1959. }
  1960. }
  1961. }
  1962. // Volume (and buffer copy)
  1963. {
  1964. for (uint32_t k=0; k < frames; ++k)
  1965. audioOut[i][k+timeOffset] = fAudioAndCvOutBuffers[i][k] * pData->postProc.volume;
  1966. }
  1967. }
  1968. } // End of Post-processing
  1969. #else
  1970. for (; i < pData->audioOut.count; ++i)
  1971. {
  1972. for (uint32_t k=0; k < frames; ++k)
  1973. audioOut[i][k+timeOffset] = fAudioAndCvOutBuffers[i][k];
  1974. }
  1975. #endif
  1976. // CV stuff too
  1977. for (; i < pData->cvOut.count; ++i)
  1978. {
  1979. for (uint32_t k=0; k < frames; ++k)
  1980. cvOut[i][k+timeOffset] = fAudioAndCvOutBuffers[pData->audioOut.count+i][k];
  1981. }
  1982. // --------------------------------------------------------------------------------------------------------
  1983. // MIDI Output
  1984. if (pData->event.portOut != nullptr)
  1985. {
  1986. for (uint32_t k = 0; k < fMidiEventOutCount; ++k)
  1987. {
  1988. const uint8_t channel = uint8_t(MIDI_GET_CHANNEL_FROM_DATA(fMidiOutEvents[k].data));
  1989. const uint8_t port = fMidiOutEvents[k].port;
  1990. if (fMidiOut.count > 1 && port < fMidiOut.count)
  1991. fMidiOut.ports[port]->writeMidiEvent(fMidiOutEvents[k].time+timeOffset, channel, fMidiOutEvents[k].size, fMidiOutEvents[k].data);
  1992. else
  1993. pData->event.portOut->writeMidiEvent(fMidiOutEvents[k].time+timeOffset, channel, fMidiOutEvents[k].size, fMidiOutEvents[k].data);
  1994. }
  1995. }
  1996. // --------------------------------------------------------------------------------------------------------
  1997. pData->singleMutex.unlock();
  1998. return true;
  1999. }
  2000. void bufferSizeChanged(const uint32_t newBufferSize) override
  2001. {
  2002. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  2003. carla_debug("CarlaPluginNative::bufferSizeChanged(%i)", newBufferSize);
  2004. for (uint32_t i=0; i < (pData->audioIn.count+pData->cvIn.count); ++i)
  2005. {
  2006. if (fAudioAndCvInBuffers[i] != nullptr)
  2007. delete[] fAudioAndCvInBuffers[i];
  2008. fAudioAndCvInBuffers[i] = new float[newBufferSize];
  2009. }
  2010. for (uint32_t i=0; i < (pData->audioOut.count+pData->cvOut.count); ++i)
  2011. {
  2012. if (fAudioAndCvOutBuffers[i] != nullptr)
  2013. delete[] fAudioAndCvOutBuffers[i];
  2014. fAudioAndCvOutBuffers[i] = new float[newBufferSize];
  2015. }
  2016. if (fCurBufferSize == newBufferSize)
  2017. return;
  2018. fCurBufferSize = newBufferSize;
  2019. if (fDescriptor != nullptr && fDescriptor->dispatcher != nullptr)
  2020. {
  2021. fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_BUFFER_SIZE_CHANGED, 0, static_cast<intptr_t>(newBufferSize), nullptr, 0.0f);
  2022. if (fHandle2 != nullptr)
  2023. fDescriptor->dispatcher(fHandle2, NATIVE_PLUGIN_OPCODE_BUFFER_SIZE_CHANGED, 0, static_cast<intptr_t>(newBufferSize), nullptr, 0.0f);
  2024. }
  2025. }
  2026. void sampleRateChanged(const double newSampleRate) override
  2027. {
  2028. CARLA_ASSERT_INT(newSampleRate > 0.0, newSampleRate);
  2029. carla_debug("CarlaPluginNative::sampleRateChanged(%g)", newSampleRate);
  2030. if (carla_isEqual(fCurSampleRate, newSampleRate))
  2031. return;
  2032. fCurSampleRate = newSampleRate;
  2033. if (fDescriptor != nullptr && fDescriptor->dispatcher != nullptr)
  2034. {
  2035. fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_SAMPLE_RATE_CHANGED, 0, 0, nullptr, float(newSampleRate));
  2036. if (fHandle2 != nullptr)
  2037. fDescriptor->dispatcher(fHandle2, NATIVE_PLUGIN_OPCODE_SAMPLE_RATE_CHANGED, 0, 0, nullptr, float(newSampleRate));
  2038. }
  2039. }
  2040. void offlineModeChanged(const bool isOffline) override
  2041. {
  2042. if (fIsOffline == isOffline)
  2043. return;
  2044. fIsOffline = isOffline;
  2045. if (fDescriptor != nullptr && fDescriptor->dispatcher != nullptr)
  2046. {
  2047. fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_OFFLINE_CHANGED, 0, isOffline ? 1 : 0, nullptr, 0.0f);
  2048. if (fHandle2 != nullptr)
  2049. fDescriptor->dispatcher(fHandle2, NATIVE_PLUGIN_OPCODE_OFFLINE_CHANGED, 0, isOffline ? 1 : 0, nullptr, 0.0f);
  2050. }
  2051. }
  2052. // -------------------------------------------------------------------
  2053. // Plugin buffers
  2054. void initBuffers() const noexcept override
  2055. {
  2056. CarlaPlugin::initBuffers();
  2057. fMidiIn.initBuffers(pData->event.portIn);
  2058. fMidiOut.initBuffers();
  2059. }
  2060. void clearBuffers() noexcept override
  2061. {
  2062. carla_debug("CarlaPluginNative::clearBuffers() - start");
  2063. if (fAudioAndCvInBuffers != nullptr)
  2064. {
  2065. for (uint32_t i=0; i < (pData->audioIn.count+pData->cvIn.count); ++i)
  2066. {
  2067. if (fAudioAndCvInBuffers[i] != nullptr)
  2068. {
  2069. delete[] fAudioAndCvInBuffers[i];
  2070. fAudioAndCvInBuffers[i] = nullptr;
  2071. }
  2072. }
  2073. delete[] fAudioAndCvInBuffers;
  2074. fAudioAndCvInBuffers = nullptr;
  2075. }
  2076. if (fAudioAndCvOutBuffers != nullptr)
  2077. {
  2078. for (uint32_t i=0; i < (pData->audioOut.count+pData->cvOut.count); ++i)
  2079. {
  2080. if (fAudioAndCvOutBuffers[i] != nullptr)
  2081. {
  2082. delete[] fAudioAndCvOutBuffers[i];
  2083. fAudioAndCvOutBuffers[i] = nullptr;
  2084. }
  2085. }
  2086. delete[] fAudioAndCvOutBuffers;
  2087. fAudioAndCvOutBuffers = nullptr;
  2088. }
  2089. if (fMidiIn.count > 1)
  2090. pData->event.portIn = nullptr;
  2091. if (fMidiOut.count > 1)
  2092. pData->event.portOut = nullptr;
  2093. fMidiIn.clear();
  2094. fMidiOut.clear();
  2095. CarlaPlugin::clearBuffers();
  2096. carla_debug("CarlaPluginNative::clearBuffers() - end");
  2097. }
  2098. // -------------------------------------------------------------------
  2099. // Post-poned UI Stuff
  2100. void uiParameterChange(const uint32_t index, const float value) noexcept override
  2101. {
  2102. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  2103. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  2104. CARLA_SAFE_ASSERT_RETURN(index < pData->param.count,);
  2105. if (! fIsUiVisible)
  2106. return;
  2107. if (fDescriptor->ui_set_parameter_value != nullptr)
  2108. fDescriptor->ui_set_parameter_value(fHandle, index, value);
  2109. }
  2110. void uiMidiProgramChange(const uint32_t index) noexcept override
  2111. {
  2112. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  2113. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  2114. CARLA_SAFE_ASSERT_RETURN(index < pData->midiprog.count,);
  2115. if (! fIsUiVisible)
  2116. return;
  2117. if (index >= pData->midiprog.count)
  2118. return;
  2119. if (fDescriptor->ui_set_midi_program != nullptr)
  2120. fDescriptor->ui_set_midi_program(fHandle, 0, pData->midiprog.data[index].bank, pData->midiprog.data[index].program);
  2121. }
  2122. void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) noexcept override
  2123. {
  2124. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  2125. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  2126. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  2127. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  2128. CARLA_SAFE_ASSERT_RETURN(velo > 0 && velo < MAX_MIDI_VALUE,);
  2129. if (! fIsUiVisible)
  2130. return;
  2131. if (fDescriptor->dispatcher != nullptr)
  2132. {
  2133. uint8_t data[3] = {
  2134. uint8_t(MIDI_STATUS_NOTE_ON | (channel & MIDI_CHANNEL_BIT)),
  2135. note,
  2136. velo
  2137. };
  2138. fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_UI_MIDI_EVENT,
  2139. 3, 0,
  2140. data,
  2141. 0.0f);
  2142. }
  2143. }
  2144. void uiNoteOff(const uint8_t channel, const uint8_t note) noexcept override
  2145. {
  2146. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  2147. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  2148. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  2149. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  2150. if (! fIsUiVisible)
  2151. return;
  2152. if (fDescriptor == nullptr || fHandle == nullptr)
  2153. return;
  2154. if (fDescriptor->dispatcher != nullptr)
  2155. {
  2156. uint8_t data[3] = {
  2157. uint8_t(MIDI_STATUS_NOTE_OFF | (channel & MIDI_CHANNEL_BIT)),
  2158. note,
  2159. 0
  2160. };
  2161. fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_UI_MIDI_EVENT,
  2162. 3, 0,
  2163. data,
  2164. 0.0f);
  2165. }
  2166. }
  2167. // -------------------------------------------------------------------
  2168. const NativeInlineDisplayImageSurface* renderInlineDisplay(const uint32_t width, const uint32_t height) const
  2169. {
  2170. CARLA_SAFE_ASSERT_RETURN(fDescriptor->hints & NATIVE_PLUGIN_HAS_INLINE_DISPLAY, nullptr);
  2171. CARLA_SAFE_ASSERT_RETURN(fDescriptor->render_inline_display, nullptr);
  2172. CARLA_SAFE_ASSERT_RETURN(width > 0, nullptr);
  2173. CARLA_SAFE_ASSERT_RETURN(height > 0, nullptr);
  2174. return fDescriptor->render_inline_display(fHandle, width, height);
  2175. }
  2176. // -------------------------------------------------------------------
  2177. protected:
  2178. const NativeTimeInfo* handleGetTimeInfo() const noexcept
  2179. {
  2180. CARLA_SAFE_ASSERT_RETURN(fIsProcessing, nullptr);
  2181. return &fTimeInfo;
  2182. }
  2183. bool handleWriteMidiEvent(const NativeMidiEvent* const event)
  2184. {
  2185. CARLA_SAFE_ASSERT_RETURN(pData->enabled, false);
  2186. CARLA_SAFE_ASSERT_RETURN(fIsProcessing, false);
  2187. CARLA_SAFE_ASSERT_RETURN(fMidiOut.count > 0 || pData->event.portOut != nullptr, false);
  2188. CARLA_SAFE_ASSERT_RETURN(event != nullptr, false);
  2189. CARLA_SAFE_ASSERT_RETURN(event->data[0] != 0, false);
  2190. if (fMidiEventOutCount == kPluginMaxMidiEvents)
  2191. {
  2192. carla_stdout("CarlaPluginNative::handleWriteMidiEvent(%p) - buffer full", event);
  2193. return false;
  2194. }
  2195. std::memcpy(&fMidiOutEvents[fMidiEventOutCount++], event, sizeof(NativeMidiEvent));
  2196. return true;
  2197. }
  2198. void handleUiParameterChanged(const uint32_t index, const float value)
  2199. {
  2200. setParameterValue(index, value, false, true, true);
  2201. }
  2202. void handleUiCustomDataChanged(const char* const key, const char* const value)
  2203. {
  2204. setCustomData(CUSTOM_DATA_TYPE_STRING, key, value, false);
  2205. }
  2206. void handleUiClosed()
  2207. {
  2208. pData->engine->callback(true, true, ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, 0, 0, 0, 0.0f, nullptr);
  2209. fIsUiVisible = false;
  2210. }
  2211. const char* handleUiOpenFile(const bool isDir, const char* const title, const char* const filter)
  2212. {
  2213. return pData->engine->runFileCallback(FILE_CALLBACK_OPEN, isDir, title, filter);
  2214. }
  2215. const char* handleUiSaveFile(const bool isDir, const char* const title, const char* const filter)
  2216. {
  2217. return pData->engine->runFileCallback(FILE_CALLBACK_SAVE, isDir, title, filter);
  2218. }
  2219. intptr_t handleDispatcher(const NativeHostDispatcherOpcode opcode,
  2220. const int32_t index, const intptr_t value, void* const ptr, const float opt)
  2221. {
  2222. #ifdef DEBUG
  2223. if (opcode != NATIVE_HOST_OPCODE_QUEUE_INLINE_DISPLAY && opcode != NATIVE_HOST_OPCODE_REQUEST_IDLE) {
  2224. carla_debug("CarlaPluginNative::handleDispatcher(%i, %i, " P_INTPTR ", %p, %f)",
  2225. opcode, index, value, ptr, static_cast<double>(opt));
  2226. }
  2227. #endif
  2228. switch (opcode)
  2229. {
  2230. case NATIVE_HOST_OPCODE_NULL:
  2231. break;
  2232. case NATIVE_HOST_OPCODE_UPDATE_PARAMETER:
  2233. // TODO
  2234. pData->engine->callback(true, true, ENGINE_CALLBACK_UPDATE, pData->id, -1, 0, 0, 0.0f, nullptr);
  2235. break;
  2236. case NATIVE_HOST_OPCODE_UPDATE_MIDI_PROGRAM:
  2237. // TODO
  2238. pData->engine->callback(true, true, ENGINE_CALLBACK_UPDATE, pData->id, -1, 0, 0, 0.0f, nullptr);
  2239. break;
  2240. case NATIVE_HOST_OPCODE_RELOAD_PARAMETERS:
  2241. reloadParameters(nullptr, nullptr);
  2242. pData->engine->callback(true, true, ENGINE_CALLBACK_RELOAD_PARAMETERS, pData->id, -1, 0, 0, 0.0f, nullptr);
  2243. break;
  2244. case NATIVE_HOST_OPCODE_RELOAD_MIDI_PROGRAMS:
  2245. reloadPrograms(false);
  2246. pData->engine->callback(true, true, ENGINE_CALLBACK_RELOAD_PROGRAMS, pData->id, -1, 0, 0, 0.0f, nullptr);
  2247. break;
  2248. case NATIVE_HOST_OPCODE_RELOAD_ALL:
  2249. reload();
  2250. pData->engine->callback(true, true, ENGINE_CALLBACK_RELOAD_ALL, pData->id, -1, 0, 0, 0.0f, nullptr);
  2251. break;
  2252. case NATIVE_HOST_OPCODE_UI_UNAVAILABLE:
  2253. pData->engine->callback(true, true, ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, -1, 0, 0, 0.0f, nullptr);
  2254. fIsUiAvailable = false;
  2255. break;
  2256. case NATIVE_HOST_OPCODE_HOST_IDLE:
  2257. pData->engine->callback(true, false, ENGINE_CALLBACK_IDLE, 0, 0, 0, 0, 0.0f, nullptr);
  2258. break;
  2259. case NATIVE_HOST_OPCODE_INTERNAL_PLUGIN:
  2260. return 1;
  2261. case NATIVE_HOST_OPCODE_QUEUE_INLINE_DISPLAY:
  2262. switch (pData->engine->getProccessMode())
  2263. {
  2264. case ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS:
  2265. case ENGINE_PROCESS_MODE_PATCHBAY:
  2266. fInlineDisplayNeedsRedraw = true;
  2267. break;
  2268. default:
  2269. break;
  2270. }
  2271. break;
  2272. case NATIVE_HOST_OPCODE_UI_TOUCH_PARAMETER:
  2273. CARLA_SAFE_ASSERT_RETURN(index >= 0, 0);
  2274. pData->engine->touchPluginParameter(pData->id, static_cast<uint32_t>(index), value != 0);
  2275. break;
  2276. case NATIVE_HOST_OPCODE_REQUEST_IDLE:
  2277. fNeedsIdle = true;
  2278. break;
  2279. case NATIVE_HOST_OPCODE_GET_FILE_PATH:
  2280. CARLA_SAFE_ASSERT_RETURN(ptr != nullptr, 0);
  2281. {
  2282. const EngineOptions& opts(pData->engine->getOptions());
  2283. const char* const filetype = (const char*)ptr;
  2284. const char* ret = nullptr;
  2285. if (std::strcmp(filetype, "carla") == 0)
  2286. {
  2287. ret = pData->engine->getCurrentProjectFilename();
  2288. if (fLastProjectFilename != ret)
  2289. {
  2290. fLastProjectFilename = ret;
  2291. bool found;
  2292. const size_t r = fLastProjectFilename.rfind(CARLA_OS_SEP, &found);
  2293. if (found)
  2294. {
  2295. fLastProjectFolder = ret;
  2296. fLastProjectFolder[r] = '\0';
  2297. }
  2298. else
  2299. {
  2300. fLastProjectFolder.clear();
  2301. }
  2302. }
  2303. ret = fLastProjectFolder.buffer();
  2304. }
  2305. else if (std::strcmp(filetype, "audio") == 0)
  2306. ret = opts.pathAudio;
  2307. else if (std::strcmp(filetype, "midi") == 0)
  2308. ret = opts.pathMIDI;
  2309. return static_cast<intptr_t>((uintptr_t)ret);
  2310. }
  2311. break;
  2312. case NATIVE_HOST_OPCODE_UI_RESIZE:
  2313. case NATIVE_HOST_OPCODE_PREVIEW_BUFFER_DATA:
  2314. // unused here
  2315. break;
  2316. }
  2317. return 0;
  2318. // unused for now
  2319. (void)opt;
  2320. }
  2321. // -------------------------------------------------------------------
  2322. public:
  2323. void* getNativeHandle() const noexcept override
  2324. {
  2325. return fHandle;
  2326. }
  2327. const void* getNativeDescriptor() const noexcept override
  2328. {
  2329. return fDescriptor;
  2330. }
  2331. // -------------------------------------------------------------------
  2332. bool init(const CarlaPluginPtr plugin,
  2333. const char* const name, const char* const label, const uint options)
  2334. {
  2335. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  2336. // ---------------------------------------------------------------
  2337. // first checks
  2338. if (pData->client != nullptr)
  2339. {
  2340. pData->engine->setLastError("Plugin client is already registered");
  2341. return false;
  2342. }
  2343. if (label == nullptr || label[0] == '\0')
  2344. {
  2345. pData->engine->setLastError("null label");
  2346. return false;
  2347. }
  2348. // ---------------------------------------------------------------
  2349. // get descriptor that matches label
  2350. sPluginInitializer.initIfNeeded();
  2351. for (LinkedList<const NativePluginDescriptor*>::Itenerator it = gPluginDescriptors.begin2(); it.valid(); it.next())
  2352. {
  2353. fDescriptor = it.getValue(nullptr);
  2354. CARLA_SAFE_ASSERT_BREAK(fDescriptor != nullptr);
  2355. carla_debug("Check vs \"%s\"", fDescriptor->label);
  2356. if (fDescriptor->label != nullptr && std::strcmp(fDescriptor->label, label) == 0)
  2357. break;
  2358. fDescriptor = nullptr;
  2359. }
  2360. if (fDescriptor == nullptr)
  2361. {
  2362. pData->engine->setLastError("Invalid internal plugin");
  2363. return false;
  2364. }
  2365. // ---------------------------------------------------------------
  2366. // set icon
  2367. /**/ if (std::strcmp(fDescriptor->label, "audiofile") == 0)
  2368. pData->iconName = carla_strdup_safe("file");
  2369. else if (std::strcmp(fDescriptor->label, "midifile") == 0)
  2370. pData->iconName = carla_strdup_safe("file");
  2371. else if (std::strcmp(fDescriptor->label, "3bandeq") == 0)
  2372. pData->iconName = carla_strdup_safe("distrho");
  2373. else if (std::strcmp(fDescriptor->label, "3bandsplitter") == 0)
  2374. pData->iconName = carla_strdup_safe("distrho");
  2375. else if (std::strcmp(fDescriptor->label, "kars") == 0)
  2376. pData->iconName = carla_strdup_safe("distrho");
  2377. else if (std::strcmp(fDescriptor->label, "nekobi") == 0)
  2378. pData->iconName = carla_strdup_safe("distrho");
  2379. else if (std::strcmp(fDescriptor->label, "pingpongpan") == 0)
  2380. pData->iconName = carla_strdup_safe("distrho");
  2381. // ---------------------------------------------------------------
  2382. // set info
  2383. if (name != nullptr && name[0] != '\0')
  2384. pData->name = pData->engine->getUniquePluginName(name);
  2385. else if (fDescriptor->name != nullptr && fDescriptor->name[0] != '\0')
  2386. pData->name = pData->engine->getUniquePluginName(fDescriptor->name);
  2387. else
  2388. pData->name = pData->engine->getUniquePluginName(label);
  2389. {
  2390. CARLA_ASSERT(fHost.uiName == nullptr);
  2391. CarlaString uiName;
  2392. if (pData->uiTitle.isNotEmpty())
  2393. {
  2394. uiName = pData->uiTitle;
  2395. }
  2396. else
  2397. {
  2398. uiName = pData->name;
  2399. uiName += " (GUI)";
  2400. }
  2401. fHost.uiName = uiName.releaseBufferPointer();
  2402. }
  2403. // ---------------------------------------------------------------
  2404. // register client
  2405. pData->client = pData->engine->addClient(plugin);
  2406. if (pData->client == nullptr || ! pData->client->isOk())
  2407. {
  2408. pData->engine->setLastError("Failed to register plugin client");
  2409. return false;
  2410. }
  2411. // ---------------------------------------------------------------
  2412. // initialize plugin
  2413. fHandle = fDescriptor->instantiate(&fHost);
  2414. if (fHandle == nullptr)
  2415. {
  2416. pData->engine->setLastError("Plugin failed to initialize");
  2417. return false;
  2418. }
  2419. // ---------------------------------------------------------------
  2420. // set options
  2421. bool hasMidiProgs = false;
  2422. if (fDescriptor->get_midi_program_count != nullptr)
  2423. {
  2424. try {
  2425. hasMidiProgs = fDescriptor->get_midi_program_count(fHandle) > 0;
  2426. } catch (...) {}
  2427. }
  2428. pData->options = 0x0;
  2429. if (fDescriptor->hints & NATIVE_PLUGIN_NEEDS_FIXED_BUFFERS)
  2430. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  2431. else if (options & PLUGIN_OPTION_FIXED_BUFFERS)
  2432. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  2433. if (pData->engine->getOptions().forceStereo)
  2434. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  2435. else if (options & PLUGIN_OPTION_FORCE_STEREO)
  2436. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  2437. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_CONTROL_CHANGES)
  2438. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_CONTROL_CHANGES))
  2439. pData->options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  2440. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_CHANNEL_PRESSURE)
  2441. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_CHANNEL_PRESSURE))
  2442. pData->options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  2443. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_NOTE_AFTERTOUCH)
  2444. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH))
  2445. pData->options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  2446. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_PITCHBEND)
  2447. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_PITCHBEND))
  2448. pData->options |= PLUGIN_OPTION_SEND_PITCHBEND;
  2449. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_ALL_SOUND_OFF)
  2450. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_ALL_SOUND_OFF))
  2451. pData->options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  2452. if (fDescriptor->midiIns > 0)
  2453. if (isPluginOptionInverseEnabled(options, PLUGIN_OPTION_SKIP_SENDING_NOTES))
  2454. pData->options |= PLUGIN_OPTION_SKIP_SENDING_NOTES;
  2455. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_PROGRAM_CHANGES)
  2456. {
  2457. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_PROGRAM_CHANGES))
  2458. pData->options |= PLUGIN_OPTION_SEND_PROGRAM_CHANGES;
  2459. // makes no sense for a plugin to set program changes supported, but it has no midi programs
  2460. CARLA_SAFE_ASSERT(! hasMidiProgs);
  2461. }
  2462. else if (hasMidiProgs)
  2463. {
  2464. if (isPluginOptionEnabled(options, PLUGIN_OPTION_MAP_PROGRAM_CHANGES))
  2465. pData->options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  2466. }
  2467. return true;
  2468. }
  2469. private:
  2470. NativePluginHandle fHandle;
  2471. NativePluginHandle fHandle2;
  2472. NativeHostDescriptor fHost;
  2473. const NativePluginDescriptor* fDescriptor;
  2474. bool fIsProcessing;
  2475. bool fIsOffline;
  2476. bool fIsUiAvailable;
  2477. bool fIsUiVisible;
  2478. volatile bool fNeedsIdle;
  2479. bool fInlineDisplayNeedsRedraw;
  2480. int64_t fInlineDisplayLastRedrawTime;
  2481. CarlaString fLastProjectFilename;
  2482. CarlaString fLastProjectFolder;
  2483. float** fAudioAndCvInBuffers;
  2484. float** fAudioAndCvOutBuffers;
  2485. uint32_t fMidiEventInCount;
  2486. uint32_t fMidiEventOutCount;
  2487. NativeMidiEvent fMidiInEvents[kPluginMaxMidiEvents];
  2488. NativeMidiEvent fMidiOutEvents[kPluginMaxMidiEvents];
  2489. int32_t fCurMidiProgs[MAX_MIDI_CHANNELS];
  2490. uint32_t fCurBufferSize;
  2491. double fCurSampleRate;
  2492. NativePluginMidiInData fMidiIn;
  2493. NativePluginMidiOutData fMidiOut;
  2494. NativeTimeInfo fTimeInfo;
  2495. // -------------------------------------------------------------------
  2496. #define handlePtr ((CarlaPluginNative*)handle)
  2497. static uint32_t carla_host_get_buffer_size(NativeHostHandle handle) noexcept
  2498. {
  2499. return handlePtr->fCurBufferSize;
  2500. }
  2501. static double carla_host_get_sample_rate(NativeHostHandle handle) noexcept
  2502. {
  2503. return handlePtr->fCurSampleRate;
  2504. }
  2505. static bool carla_host_is_offline(NativeHostHandle handle) noexcept
  2506. {
  2507. return handlePtr->fIsOffline;
  2508. }
  2509. static const NativeTimeInfo* carla_host_get_time_info(NativeHostHandle handle) noexcept
  2510. {
  2511. return handlePtr->handleGetTimeInfo();
  2512. }
  2513. static bool carla_host_write_midi_event(NativeHostHandle handle, const NativeMidiEvent* event)
  2514. {
  2515. return handlePtr->handleWriteMidiEvent(event);
  2516. }
  2517. static void carla_host_ui_parameter_changed(NativeHostHandle handle, uint32_t index, float value)
  2518. {
  2519. handlePtr->handleUiParameterChanged(index, value);
  2520. }
  2521. static void carla_host_ui_custom_data_changed(NativeHostHandle handle, const char* key, const char* value)
  2522. {
  2523. handlePtr->handleUiCustomDataChanged(key, value);
  2524. }
  2525. static void carla_host_ui_closed(NativeHostHandle handle)
  2526. {
  2527. handlePtr->handleUiClosed();
  2528. }
  2529. static const char* carla_host_ui_open_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
  2530. {
  2531. return handlePtr->handleUiOpenFile(isDir, title, filter);
  2532. }
  2533. static const char* carla_host_ui_save_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
  2534. {
  2535. return handlePtr->handleUiSaveFile(isDir, title, filter);
  2536. }
  2537. static intptr_t carla_host_dispatcher(NativeHostHandle handle, NativeHostDispatcherOpcode opcode, int32_t index, intptr_t value, void* ptr, float opt)
  2538. {
  2539. return handlePtr->handleDispatcher(opcode, index, value, ptr, opt);
  2540. }
  2541. #undef handlePtr
  2542. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaPluginNative)
  2543. };
  2544. // -----------------------------------------------------------------------
  2545. CarlaPluginPtr CarlaPlugin::newNative(const Initializer& init)
  2546. {
  2547. carla_debug("CarlaPlugin::newNative({%p, \"%s\", \"%s\", \"%s\", " P_INT64 "})",
  2548. init.engine, init.filename, init.name, init.label, init.uniqueId);
  2549. std::shared_ptr<CarlaPluginNative> plugin(new CarlaPluginNative(init.engine, init.id));
  2550. if (! plugin->init(plugin, init.name, init.label, init.options))
  2551. return nullptr;
  2552. return plugin;
  2553. }
  2554. // used in CarlaStandalone.cpp
  2555. const void* carla_render_inline_display_internal(const CarlaPluginPtr& plugin, uint32_t width, uint32_t height);
  2556. const void* carla_render_inline_display_internal(const CarlaPluginPtr& plugin, uint32_t width, uint32_t height)
  2557. {
  2558. const std::shared_ptr<CarlaPluginNative>& nativePlugin((const std::shared_ptr<CarlaPluginNative>&)plugin);
  2559. return nativePlugin->renderInlineDisplay(width, height);
  2560. }
  2561. // -----------------------------------------------------------------------
  2562. CARLA_BACKEND_END_NAMESPACE