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

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
10 years ago
11 years ago
10 years ago
10 years ago
10 years ago
11 years ago
10 years ago
11 years ago
10 years ago
10 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
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
11 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
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
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
11 years ago
10 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
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
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
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
9 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
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
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
9 years ago
9 years ago
11 years ago
9 years ago
11 years ago
9 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
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
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553
  1. /*
  2. * Carla Native Plugin
  3. * Copyright (C) 2012-2017 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 "CarlaMathUtils.hpp"
  20. #include "CarlaNative.h"
  21. #include "water/text/StringArray.h"
  22. using water::jmax;
  23. using water::String;
  24. using water::StringArray;
  25. CARLA_EXTERN_C
  26. std::size_t carla_getNativePluginCount() noexcept;
  27. CARLA_EXTERN_C
  28. const NativePluginDescriptor* carla_getNativePluginDescriptor(const std::size_t index) noexcept;
  29. // -----------------------------------------------------------------------
  30. static LinkedList<const NativePluginDescriptor*> gPluginDescriptors;
  31. void carla_register_native_plugin(const NativePluginDescriptor* desc)
  32. {
  33. gPluginDescriptors.append(desc);
  34. }
  35. // -----------------------------------------------------------------------
  36. static
  37. class NativePluginInitializer
  38. {
  39. public:
  40. NativePluginInitializer() noexcept
  41. : fNeedsInit(true) {}
  42. ~NativePluginInitializer() noexcept
  43. {
  44. gPluginDescriptors.clear();
  45. }
  46. void initIfNeeded() noexcept
  47. {
  48. if (! fNeedsInit)
  49. return;
  50. fNeedsInit = false;
  51. try {
  52. carla_register_all_native_plugins();
  53. } CARLA_SAFE_EXCEPTION("carla_register_all_native_plugins")
  54. }
  55. private:
  56. bool fNeedsInit;
  57. } sPluginInitializer;
  58. // -----------------------------------------------------------------------
  59. std::size_t carla_getNativePluginCount() noexcept
  60. {
  61. sPluginInitializer.initIfNeeded();
  62. return gPluginDescriptors.count();
  63. }
  64. const NativePluginDescriptor* carla_getNativePluginDescriptor(const std::size_t index) noexcept
  65. {
  66. sPluginInitializer.initIfNeeded();
  67. return gPluginDescriptors.getAt(index, nullptr);
  68. }
  69. // -----------------------------------------------------------------------
  70. CARLA_BACKEND_START_NAMESPACE
  71. // -------------------------------------------------------------------
  72. // Fallback data
  73. static const CustomData kCustomDataFallback = { nullptr, nullptr, nullptr };
  74. // -----------------------------------------------------------------------
  75. struct NativePluginMidiData {
  76. uint32_t count;
  77. uint32_t* indexes;
  78. CarlaEngineEventPort** ports;
  79. NativePluginMidiData() noexcept
  80. : count(0),
  81. indexes(nullptr),
  82. ports(nullptr) {}
  83. ~NativePluginMidiData() noexcept
  84. {
  85. CARLA_SAFE_ASSERT_INT(count == 0, count);
  86. CARLA_SAFE_ASSERT(indexes == nullptr);
  87. CARLA_SAFE_ASSERT(ports == nullptr);
  88. }
  89. void createNew(const uint32_t newCount)
  90. {
  91. CARLA_SAFE_ASSERT_INT(count == 0, count);
  92. CARLA_SAFE_ASSERT_RETURN(indexes == nullptr,);
  93. CARLA_SAFE_ASSERT_RETURN(ports == nullptr,);
  94. CARLA_SAFE_ASSERT_RETURN(newCount > 0,);
  95. indexes = new uint32_t[newCount];
  96. ports = new CarlaEngineEventPort*[newCount];
  97. count = newCount;
  98. for (uint32_t i=0; i < newCount; ++i)
  99. indexes[i] = 0;
  100. for (uint32_t i=0; i < newCount; ++i)
  101. ports[i] = nullptr;
  102. }
  103. void clear() noexcept
  104. {
  105. if (ports != nullptr)
  106. {
  107. for (uint32_t i=0; i < count; ++i)
  108. {
  109. if (ports[i] != nullptr)
  110. {
  111. delete ports[i];
  112. ports[i] = nullptr;
  113. }
  114. }
  115. delete[] ports;
  116. ports = nullptr;
  117. }
  118. if (indexes != nullptr)
  119. {
  120. delete[] indexes;
  121. indexes = nullptr;
  122. }
  123. count = 0;
  124. }
  125. void initBuffers() const noexcept
  126. {
  127. for (uint32_t i=0; i < count; ++i)
  128. {
  129. if (ports[i] != nullptr)
  130. ports[i]->initBuffer();
  131. }
  132. }
  133. CARLA_DECLARE_NON_COPY_STRUCT(NativePluginMidiData)
  134. };
  135. // -----------------------------------------------------
  136. class CarlaPluginNative : public CarlaPlugin
  137. {
  138. public:
  139. CarlaPluginNative(CarlaEngine* const engine, const uint id)
  140. : CarlaPlugin(engine, id),
  141. fHandle(nullptr),
  142. fHandle2(nullptr),
  143. fHost(),
  144. fDescriptor(nullptr),
  145. fIsProcessing(false),
  146. fIsOffline(false),
  147. fIsUiAvailable(false),
  148. fIsUiVisible(false),
  149. fAudioInBuffers(nullptr),
  150. fAudioOutBuffers(nullptr),
  151. fMidiEventCount(0),
  152. fCurBufferSize(engine->getBufferSize()),
  153. fCurSampleRate(engine->getSampleRate()),
  154. fMidiIn(),
  155. fMidiOut(),
  156. fTimeInfo()
  157. {
  158. carla_debug("CarlaPluginNative::CarlaPluginNative(%p, %i)", engine, id);
  159. carla_fill(fCurMidiProgs, 0, MAX_MIDI_CHANNELS);
  160. carla_zeroStructs(fMidiEvents, kPluginMaxMidiEvents*2);
  161. carla_zeroStruct(fTimeInfo);
  162. fHost.handle = this;
  163. fHost.resourceDir = carla_strdup(engine->getOptions().resourceDir);
  164. fHost.uiName = nullptr;
  165. fHost.uiParentId = engine->getOptions().frontendWinId;
  166. fHost.get_buffer_size = carla_host_get_buffer_size;
  167. fHost.get_sample_rate = carla_host_get_sample_rate;
  168. fHost.is_offline = carla_host_is_offline;
  169. fHost.get_time_info = carla_host_get_time_info;
  170. fHost.write_midi_event = carla_host_write_midi_event;
  171. fHost.ui_parameter_changed = carla_host_ui_parameter_changed;
  172. fHost.ui_custom_data_changed = carla_host_ui_custom_data_changed;
  173. fHost.ui_closed = carla_host_ui_closed;
  174. fHost.ui_open_file = carla_host_ui_open_file;
  175. fHost.ui_save_file = carla_host_ui_save_file;
  176. fHost.dispatcher = carla_host_dispatcher;
  177. }
  178. ~CarlaPluginNative() override
  179. {
  180. carla_debug("CarlaPluginNative::~CarlaPluginNative()");
  181. // close UI
  182. if (pData->hints & PLUGIN_HAS_CUSTOM_UI)
  183. {
  184. if (fIsUiVisible && fDescriptor != nullptr && fDescriptor->ui_show != nullptr && fHandle != nullptr)
  185. fDescriptor->ui_show(fHandle, false);
  186. pData->transientTryCounter = 0;
  187. }
  188. pData->singleMutex.lock();
  189. pData->masterMutex.lock();
  190. if (pData->client != nullptr && pData->client->isActive())
  191. pData->client->deactivate();
  192. CARLA_ASSERT(! fIsProcessing);
  193. if (pData->active)
  194. {
  195. deactivate();
  196. pData->active = false;
  197. }
  198. if (fDescriptor != nullptr)
  199. {
  200. if (fDescriptor->cleanup != nullptr)
  201. {
  202. if (fHandle != nullptr)
  203. fDescriptor->cleanup(fHandle);
  204. if (fHandle2 != nullptr)
  205. fDescriptor->cleanup(fHandle2);
  206. }
  207. fHandle = nullptr;
  208. fHandle2 = nullptr;
  209. fDescriptor = nullptr;
  210. }
  211. if (fHost.resourceDir != nullptr)
  212. {
  213. delete[] fHost.resourceDir;
  214. fHost.resourceDir = nullptr;
  215. }
  216. if (fHost.uiName != nullptr)
  217. {
  218. delete[] fHost.uiName;
  219. fHost.uiName = nullptr;
  220. }
  221. clearBuffers();
  222. }
  223. // -------------------------------------------------------------------
  224. // Information (base)
  225. PluginType getType() const noexcept override
  226. {
  227. return PLUGIN_INTERNAL;
  228. }
  229. PluginCategory getCategory() const noexcept override
  230. {
  231. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, PLUGIN_CATEGORY_NONE);
  232. return static_cast<PluginCategory>(fDescriptor->category);
  233. }
  234. // -------------------------------------------------------------------
  235. // Information (count)
  236. uint32_t getMidiInCount() const noexcept override
  237. {
  238. return fMidiIn.count;
  239. }
  240. uint32_t getMidiOutCount() const noexcept override
  241. {
  242. return fMidiOut.count;
  243. }
  244. uint32_t getParameterScalePointCount(const uint32_t parameterId) const noexcept override
  245. {
  246. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, 0);
  247. CARLA_SAFE_ASSERT_RETURN(fDescriptor->get_parameter_info != nullptr, 0);
  248. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr, 0);
  249. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0);
  250. // FIXME - try
  251. if (const NativeParameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId))
  252. return param->scalePointCount;
  253. carla_safe_assert("const Parameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId)", __FILE__, __LINE__);
  254. return 0;
  255. }
  256. // -------------------------------------------------------------------
  257. // Information (current data)
  258. // nothing
  259. // -------------------------------------------------------------------
  260. // Information (per-plugin data)
  261. uint getOptionsAvailable() const noexcept override
  262. {
  263. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, 0x0);
  264. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr, 0x0);
  265. bool hasMidiProgs = false;
  266. if (fDescriptor->get_midi_program_count != nullptr)
  267. {
  268. try {
  269. hasMidiProgs = fDescriptor->get_midi_program_count(fHandle) > 0;
  270. } catch (...) {}
  271. }
  272. uint options = 0x0;
  273. // can't disable fixed buffers if using MIDI output
  274. if (fMidiOut.count == 0 && (fDescriptor->hints & NATIVE_PLUGIN_NEEDS_FIXED_BUFFERS) == 0)
  275. options |= PLUGIN_OPTION_FIXED_BUFFERS;
  276. // can't disable forced stereo if enabled in the engine
  277. if (pData->engine->getOptions().forceStereo)
  278. pass();
  279. // if inputs or outputs are just 1, then yes we can force stereo
  280. else if (pData->audioIn.count == 1 || pData->audioOut.count == 1 || fHandle2 != nullptr)
  281. options |= PLUGIN_OPTION_FORCE_STEREO;
  282. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_CONTROL_CHANGES)
  283. options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  284. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_CHANNEL_PRESSURE)
  285. options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  286. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_NOTE_AFTERTOUCH)
  287. options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  288. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_PITCHBEND)
  289. options |= PLUGIN_OPTION_SEND_PITCHBEND;
  290. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_ALL_SOUND_OFF)
  291. options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  292. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_PROGRAM_CHANGES)
  293. options |= PLUGIN_OPTION_SEND_PROGRAM_CHANGES;
  294. else if (hasMidiProgs)
  295. options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  296. return options;
  297. }
  298. float getParameterValue(const uint32_t parameterId) const noexcept override
  299. {
  300. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, 0.0f);
  301. CARLA_SAFE_ASSERT_RETURN(fDescriptor->get_parameter_value != nullptr, 0.0f);
  302. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr, 0.0f);
  303. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0.0f);
  304. // FIXME - try
  305. return fDescriptor->get_parameter_value(fHandle, parameterId);
  306. }
  307. float getParameterScalePointValue(const uint32_t parameterId, const uint32_t scalePointId) const noexcept override
  308. {
  309. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, 0.0f);
  310. CARLA_SAFE_ASSERT_RETURN(fDescriptor->get_parameter_info != nullptr, 0.0f);
  311. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr, 0.0f);
  312. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0.0f);
  313. // FIXME - try
  314. if (const NativeParameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId))
  315. {
  316. CARLA_SAFE_ASSERT_RETURN(scalePointId < param->scalePointCount, 0.0f);
  317. const NativeParameterScalePoint* scalePoint(&param->scalePoints[scalePointId]);
  318. return scalePoint->value;
  319. }
  320. carla_safe_assert("const Parameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId)", __FILE__, __LINE__);
  321. return 0.0f;
  322. }
  323. void getLabel(char* const strBuf) const noexcept override
  324. {
  325. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  326. if (fDescriptor->label != nullptr)
  327. {
  328. std::strncpy(strBuf, fDescriptor->label, STR_MAX);
  329. return;
  330. }
  331. CarlaPlugin::getLabel(strBuf);
  332. }
  333. void getMaker(char* const strBuf) const noexcept override
  334. {
  335. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  336. if (fDescriptor->maker != nullptr)
  337. {
  338. std::strncpy(strBuf, fDescriptor->maker, STR_MAX);
  339. return;
  340. }
  341. CarlaPlugin::getMaker(strBuf);
  342. }
  343. void getCopyright(char* const strBuf) const noexcept override
  344. {
  345. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  346. if (fDescriptor->copyright != nullptr)
  347. {
  348. std::strncpy(strBuf, fDescriptor->copyright, STR_MAX);
  349. return;
  350. }
  351. CarlaPlugin::getCopyright(strBuf);
  352. }
  353. void getRealName(char* const strBuf) const noexcept override
  354. {
  355. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  356. if (fDescriptor->name != nullptr)
  357. {
  358. std::strncpy(strBuf, fDescriptor->name, STR_MAX);
  359. return;
  360. }
  361. CarlaPlugin::getRealName(strBuf);
  362. }
  363. void getParameterName(const uint32_t parameterId, char* const strBuf) const noexcept override
  364. {
  365. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  366. CARLA_SAFE_ASSERT_RETURN(fDescriptor->get_parameter_info != nullptr,);
  367. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  368. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  369. // FIXME - try
  370. if (const NativeParameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId))
  371. {
  372. if (param->name != nullptr)
  373. {
  374. std::strncpy(strBuf, param->name, STR_MAX);
  375. return;
  376. }
  377. carla_safe_assert("param->name != nullptr", __FILE__, __LINE__);
  378. return CarlaPlugin::getParameterName(parameterId, strBuf);
  379. }
  380. carla_safe_assert("const Parameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId)", __FILE__, __LINE__);
  381. CarlaPlugin::getParameterName(parameterId, strBuf);
  382. }
  383. void getParameterUnit(const uint32_t parameterId, char* const strBuf) const noexcept override
  384. {
  385. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  386. CARLA_SAFE_ASSERT_RETURN(fDescriptor->get_parameter_info != nullptr,);
  387. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  388. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  389. // FIXME - try
  390. if (const NativeParameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId))
  391. {
  392. if (param->unit != nullptr)
  393. {
  394. std::strncpy(strBuf, param->unit, STR_MAX);
  395. return;
  396. }
  397. return CarlaPlugin::getParameterUnit(parameterId, strBuf);
  398. }
  399. carla_safe_assert("const Parameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId)", __FILE__, __LINE__);
  400. CarlaPlugin::getParameterUnit(parameterId, strBuf);
  401. }
  402. void getParameterScalePointLabel(const uint32_t parameterId, const uint32_t scalePointId, char* const strBuf) const noexcept override
  403. {
  404. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  405. CARLA_SAFE_ASSERT_RETURN(fDescriptor->get_parameter_info != nullptr,);
  406. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  407. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  408. // FIXME - try
  409. if (const NativeParameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId))
  410. {
  411. CARLA_SAFE_ASSERT_RETURN(scalePointId < param->scalePointCount,);
  412. const NativeParameterScalePoint* scalePoint(&param->scalePoints[scalePointId]);
  413. if (scalePoint->label != nullptr)
  414. {
  415. std::strncpy(strBuf, scalePoint->label, STR_MAX);
  416. return;
  417. }
  418. carla_safe_assert("scalePoint->label != nullptr", __FILE__, __LINE__);
  419. return CarlaPlugin::getParameterScalePointLabel(parameterId, scalePointId, strBuf);
  420. }
  421. carla_safe_assert("const Parameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId)", __FILE__, __LINE__);
  422. CarlaPlugin::getParameterScalePointLabel(parameterId, scalePointId, strBuf);
  423. }
  424. // -------------------------------------------------------------------
  425. // Set data (state)
  426. void prepareForSave() override
  427. {
  428. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  429. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  430. if (pData->midiprog.count > 0 && fDescriptor->category == NATIVE_PLUGIN_CATEGORY_SYNTH)
  431. {
  432. char strBuf[STR_MAX+1];
  433. std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i",
  434. fCurMidiProgs[0], fCurMidiProgs[1], fCurMidiProgs[2], fCurMidiProgs[3],
  435. fCurMidiProgs[4], fCurMidiProgs[5], fCurMidiProgs[6], fCurMidiProgs[7],
  436. fCurMidiProgs[8], fCurMidiProgs[9], fCurMidiProgs[10], fCurMidiProgs[11],
  437. fCurMidiProgs[12], fCurMidiProgs[13], fCurMidiProgs[14], fCurMidiProgs[15]);
  438. strBuf[STR_MAX] = '\0';
  439. CarlaPlugin::setCustomData(CUSTOM_DATA_TYPE_STRING, "midiPrograms", strBuf, false);
  440. }
  441. if (fDescriptor == nullptr || fDescriptor->get_state == nullptr || (fDescriptor->hints & NATIVE_PLUGIN_USES_STATE) == 0)
  442. return;
  443. if (char* data = fDescriptor->get_state(fHandle))
  444. {
  445. CarlaPlugin::setCustomData(CUSTOM_DATA_TYPE_CHUNK, "State", data, false);
  446. std::free(data);
  447. }
  448. }
  449. // -------------------------------------------------------------------
  450. // Set data (internal stuff)
  451. void setName(const char* const newName) override
  452. {
  453. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  454. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  455. CARLA_SAFE_ASSERT_RETURN(newName != nullptr && newName[0] != '\0',);
  456. char uiName[std::strlen(newName)+6+1];
  457. std::strcpy(uiName, newName);
  458. std::strcat(uiName, " (GUI)");
  459. if (fHost.uiName != nullptr)
  460. delete[] fHost.uiName;
  461. fHost.uiName = carla_strdup(uiName);
  462. if (fDescriptor->dispatcher != nullptr && fIsUiVisible)
  463. fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_UI_NAME_CHANGED, 0, 0, uiName, 0.0f);
  464. CarlaPlugin::setName(newName);
  465. }
  466. void setCtrlChannel(const int8_t channel, const bool sendOsc, const bool sendCallback) noexcept override
  467. {
  468. if (channel >= 0 && channel < MAX_MIDI_CHANNELS && pData->midiprog.count > 0)
  469. pData->midiprog.current = fCurMidiProgs[channel];
  470. CarlaPlugin::setCtrlChannel(channel, sendOsc, sendCallback);
  471. }
  472. // -------------------------------------------------------------------
  473. // Set data (plugin-specific stuff)
  474. void setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  475. {
  476. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  477. CARLA_SAFE_ASSERT_RETURN(fDescriptor->set_parameter_value != nullptr,);
  478. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  479. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  480. const float fixedValue(pData->param.getFixedValue(parameterId, value));
  481. // FIXME - try
  482. fDescriptor->set_parameter_value(fHandle, parameterId, fixedValue);
  483. if (fHandle2 != nullptr)
  484. fDescriptor->set_parameter_value(fHandle2, parameterId, fixedValue);
  485. CarlaPlugin::setParameterValue(parameterId, fixedValue, sendGui, sendOsc, sendCallback);
  486. }
  487. void setCustomData(const char* const type, const char* const key, const char* const value, const bool sendGui) override
  488. {
  489. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  490. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  491. CARLA_SAFE_ASSERT_RETURN(type != nullptr && type[0] != '\0',);
  492. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  493. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  494. carla_debug("CarlaPluginNative::setCustomData(%s, %s, %s, %s)", type, key, value, bool2str(sendGui));
  495. if (std::strcmp(type, CUSTOM_DATA_TYPE_PROPERTY) == 0)
  496. return CarlaPlugin::setCustomData(type, key, value, sendGui);
  497. if (std::strcmp(type, CUSTOM_DATA_TYPE_STRING) != 0 && std::strcmp(type, CUSTOM_DATA_TYPE_CHUNK) != 0)
  498. return carla_stderr2("CarlaPluginNative::setCustomData(\"%s\", \"%s\", \"%s\", %s) - type is invalid", type, key, value, bool2str(sendGui));
  499. if (std::strcmp(type, CUSTOM_DATA_TYPE_CHUNK) == 0)
  500. {
  501. if (fDescriptor->set_state != nullptr && (fDescriptor->hints & NATIVE_PLUGIN_USES_STATE) != 0)
  502. {
  503. const ScopedSingleProcessLocker spl(this, true);
  504. fDescriptor->set_state(fHandle, value);
  505. if (fHandle2 != nullptr)
  506. fDescriptor->set_state(fHandle2, value);
  507. }
  508. }
  509. else if (std::strcmp(key, "midiPrograms") == 0 && fDescriptor->set_midi_program != nullptr)
  510. {
  511. StringArray midiProgramList(StringArray::fromTokens(value, ":", ""));
  512. if (midiProgramList.size() == MAX_MIDI_CHANNELS)
  513. {
  514. uint8_t channel = 0;
  515. for (String *it=midiProgramList.begin(), *end=midiProgramList.end(); it != end; ++it)
  516. {
  517. const int index(it->getIntValue());
  518. if (index >= 0 && index < static_cast<int>(pData->midiprog.count))
  519. {
  520. const uint32_t bank = pData->midiprog.data[index].bank;
  521. const uint32_t program = pData->midiprog.data[index].program;
  522. fDescriptor->set_midi_program(fHandle, channel, bank, program);
  523. if (fHandle2 != nullptr)
  524. fDescriptor->set_midi_program(fHandle2, channel, bank, program);
  525. fCurMidiProgs[channel] = index;
  526. if (pData->ctrlChannel == static_cast<int32_t>(channel))
  527. {
  528. pData->midiprog.current = index;
  529. pData->engine->callback(ENGINE_CALLBACK_MIDI_PROGRAM_CHANGED, pData->id, index, 0, 0.0f, nullptr);
  530. }
  531. }
  532. ++channel;
  533. }
  534. CARLA_SAFE_ASSERT(channel == MAX_MIDI_CHANNELS);
  535. }
  536. }
  537. else
  538. {
  539. if (fDescriptor->set_custom_data != nullptr)
  540. {
  541. fDescriptor->set_custom_data(fHandle, key, value);
  542. if (fHandle2 != nullptr)
  543. fDescriptor->set_custom_data(fHandle2, key, value);
  544. }
  545. if (sendGui && fIsUiVisible && fDescriptor->ui_set_custom_data != nullptr)
  546. fDescriptor->ui_set_custom_data(fHandle, key, value);
  547. }
  548. CarlaPlugin::setCustomData(type, key, value, sendGui);
  549. }
  550. void setMidiProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  551. {
  552. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  553. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  554. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->midiprog.count),);
  555. // TODO, put into check below
  556. if ((pData->hints & PLUGIN_IS_SYNTH) != 0 && (pData->ctrlChannel < 0 || pData->ctrlChannel >= MAX_MIDI_CHANNELS))
  557. return CarlaPlugin::setMidiProgram(index, sendGui, sendOsc, sendCallback);
  558. if (index >= 0)
  559. {
  560. const uint8_t channel = uint8_t((pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS) ? pData->ctrlChannel : 0);
  561. const uint32_t bank = pData->midiprog.data[index].bank;
  562. const uint32_t program = pData->midiprog.data[index].program;
  563. const ScopedSingleProcessLocker spl(this, (sendGui || sendOsc || sendCallback));
  564. try {
  565. fDescriptor->set_midi_program(fHandle, channel, bank, program);
  566. } catch(...) {}
  567. if (fHandle2 != nullptr)
  568. {
  569. try {
  570. fDescriptor->set_midi_program(fHandle2, channel, bank, program);
  571. } catch(...) {}
  572. }
  573. fCurMidiProgs[channel] = index;
  574. }
  575. CarlaPlugin::setMidiProgram(index, sendGui, sendOsc, sendCallback);
  576. }
  577. // -------------------------------------------------------------------
  578. // Set ui stuff
  579. void showCustomUI(const bool yesNo) override
  580. {
  581. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  582. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  583. if (fDescriptor->ui_show == nullptr)
  584. return;
  585. fIsUiAvailable = true;
  586. fDescriptor->ui_show(fHandle, yesNo);
  587. // UI might not be available, see NATIVE_HOST_OPCODE_UI_UNAVAILABLE
  588. if (yesNo && ! fIsUiAvailable)
  589. return;
  590. fIsUiVisible = yesNo;
  591. if (! yesNo)
  592. {
  593. pData->transientTryCounter = 0;
  594. return;
  595. }
  596. #ifndef BUILD_BRIDGE
  597. if ((fDescriptor->hints & NATIVE_PLUGIN_USES_PARENT_ID) == 0 && std::strstr(fDescriptor->label, "file") == nullptr)
  598. pData->tryTransient();
  599. #endif
  600. if (fDescriptor->ui_set_custom_data != nullptr)
  601. {
  602. for (LinkedList<CustomData>::Itenerator it = pData->custom.begin2(); it.valid(); it.next())
  603. {
  604. const CustomData& cData(it.getValue(kCustomDataFallback));
  605. CARLA_SAFE_ASSERT_CONTINUE(cData.isValid());
  606. if (std::strcmp(cData.type, CUSTOM_DATA_TYPE_STRING) == 0 && std::strcmp(cData.key, "midiPrograms") != 0)
  607. fDescriptor->ui_set_custom_data(fHandle, cData.key, cData.value);
  608. }
  609. }
  610. if (fDescriptor->ui_set_midi_program != nullptr && pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  611. {
  612. const int32_t index = pData->midiprog.current;
  613. const uint8_t channel = uint8_t((pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS) ? pData->ctrlChannel : 0);
  614. const uint32_t bank = pData->midiprog.data[index].bank;
  615. const uint32_t program = pData->midiprog.data[index].program;
  616. fDescriptor->ui_set_midi_program(fHandle, channel, bank, program);
  617. }
  618. if (fDescriptor->ui_set_parameter_value != nullptr)
  619. {
  620. for (uint32_t i=0; i < pData->param.count; ++i)
  621. fDescriptor->ui_set_parameter_value(fHandle, i, fDescriptor->get_parameter_value(fHandle, i));
  622. }
  623. }
  624. void uiIdle() override
  625. {
  626. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  627. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  628. if (fIsUiVisible && fDescriptor->ui_idle != nullptr)
  629. fDescriptor->ui_idle(fHandle);
  630. CarlaPlugin::uiIdle();
  631. }
  632. // -------------------------------------------------------------------
  633. // Plugin state
  634. void reload() override
  635. {
  636. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr,);
  637. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  638. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  639. carla_debug("CarlaPluginNative::reload() - start");
  640. const EngineProcessMode processMode(pData->engine->getProccessMode());
  641. // Safely disable plugin for reload
  642. const ScopedDisabler sd(this);
  643. if (pData->active)
  644. deactivate();
  645. clearBuffers();
  646. const float sampleRate((float)pData->engine->getSampleRate());
  647. uint32_t aIns, aOuts, mIns, mOuts, params, j;
  648. bool forcedStereoIn, forcedStereoOut;
  649. forcedStereoIn = forcedStereoOut = false;
  650. bool needsCtrlIn, needsCtrlOut;
  651. needsCtrlIn = needsCtrlOut = false;
  652. aIns = fDescriptor->audioIns;
  653. aOuts = fDescriptor->audioOuts;
  654. mIns = fDescriptor->midiIns;
  655. mOuts = fDescriptor->midiOuts;
  656. params = (fDescriptor->get_parameter_count != nullptr && fDescriptor->get_parameter_info != nullptr) ? fDescriptor->get_parameter_count(fHandle) : 0;
  657. if ((pData->options & PLUGIN_OPTION_FORCE_STEREO) != 0 && (aIns == 1 || aOuts == 1) && mIns <= 1 && mOuts <= 1)
  658. {
  659. if (fHandle2 == nullptr)
  660. fHandle2 = fDescriptor->instantiate(&fHost);
  661. if (fHandle2 != nullptr)
  662. {
  663. if (aIns == 1)
  664. {
  665. aIns = 2;
  666. forcedStereoIn = true;
  667. }
  668. if (aOuts == 1)
  669. {
  670. aOuts = 2;
  671. forcedStereoOut = true;
  672. }
  673. }
  674. }
  675. if (aIns > 0)
  676. {
  677. pData->audioIn.createNew(aIns);
  678. fAudioInBuffers = new float*[aIns];
  679. for (uint32_t i=0; i < aIns; ++i)
  680. fAudioInBuffers[i] = nullptr;
  681. }
  682. if (aOuts > 0)
  683. {
  684. pData->audioOut.createNew(aOuts);
  685. fAudioOutBuffers = new float*[aOuts];
  686. needsCtrlIn = true;
  687. for (uint32_t i=0; i < aOuts; ++i)
  688. fAudioOutBuffers[i] = nullptr;
  689. }
  690. if (mIns > 0)
  691. {
  692. fMidiIn.createNew(mIns);
  693. needsCtrlIn = (mIns == 1);
  694. }
  695. if (mOuts > 0)
  696. {
  697. fMidiOut.createNew(mOuts);
  698. needsCtrlOut = (mOuts == 1);
  699. }
  700. if (params > 0)
  701. {
  702. pData->param.createNew(params, true);
  703. }
  704. const uint portNameSize(pData->engine->getMaxPortNameSize());
  705. CarlaString portName;
  706. // Audio Ins
  707. for (j=0; j < aIns; ++j)
  708. {
  709. portName.clear();
  710. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  711. {
  712. portName = pData->name;
  713. portName += ":";
  714. }
  715. if (aIns > 1 && ! forcedStereoIn)
  716. {
  717. portName += "input_";
  718. portName += CarlaString(j+1);
  719. }
  720. else
  721. portName += "input";
  722. portName.truncate(portNameSize);
  723. pData->audioIn.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true, j);
  724. pData->audioIn.ports[j].rindex = j;
  725. if (forcedStereoIn)
  726. {
  727. portName += "_2";
  728. pData->audioIn.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true, 1);
  729. pData->audioIn.ports[1].rindex = j;
  730. break;
  731. }
  732. }
  733. // Audio Outs
  734. for (j=0; j < aOuts; ++j)
  735. {
  736. portName.clear();
  737. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  738. {
  739. portName = pData->name;
  740. portName += ":";
  741. }
  742. if (aOuts > 1 && ! forcedStereoOut)
  743. {
  744. portName += "output_";
  745. portName += CarlaString(j+1);
  746. }
  747. else
  748. portName += "output";
  749. portName.truncate(portNameSize);
  750. pData->audioOut.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false, j);
  751. pData->audioOut.ports[j].rindex = j;
  752. if (forcedStereoOut)
  753. {
  754. portName += "_2";
  755. pData->audioOut.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false, 1);
  756. pData->audioOut.ports[1].rindex = j;
  757. break;
  758. }
  759. }
  760. // MIDI Input (only if multiple)
  761. if (mIns > 1)
  762. {
  763. for (j=0; j < mIns; ++j)
  764. {
  765. portName.clear();
  766. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  767. {
  768. portName = pData->name;
  769. portName += ":";
  770. }
  771. portName += "midi-in_";
  772. portName += CarlaString(j+1);
  773. portName.truncate(portNameSize);
  774. fMidiIn.ports[j] = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true, j);
  775. fMidiIn.indexes[j] = j;
  776. }
  777. pData->event.portIn = fMidiIn.ports[0];
  778. }
  779. // MIDI Output (only if multiple)
  780. if (mOuts > 1)
  781. {
  782. for (j=0; j < mOuts; ++j)
  783. {
  784. portName.clear();
  785. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  786. {
  787. portName = pData->name;
  788. portName += ":";
  789. }
  790. portName += "midi-out_";
  791. portName += CarlaString(j+1);
  792. portName.truncate(portNameSize);
  793. fMidiOut.ports[j] = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false, j);
  794. fMidiOut.indexes[j] = j;
  795. }
  796. pData->event.portOut = fMidiOut.ports[0];
  797. }
  798. for (j=0; j < params; ++j)
  799. {
  800. const NativeParameter* const paramInfo(fDescriptor->get_parameter_info(fHandle, j));
  801. CARLA_SAFE_ASSERT_CONTINUE(paramInfo != nullptr);
  802. pData->param.data[j].type = PARAMETER_UNKNOWN;
  803. pData->param.data[j].index = static_cast<int32_t>(j);
  804. pData->param.data[j].rindex = static_cast<int32_t>(j);
  805. float min, max, def, step, stepSmall, stepLarge;
  806. // min value
  807. min = paramInfo->ranges.min;
  808. // max value
  809. max = paramInfo->ranges.max;
  810. if (min > max)
  811. max = min;
  812. else if (max < min)
  813. min = max;
  814. if (carla_isEqual(min, max))
  815. {
  816. carla_stderr2("WARNING - Broken plugin parameter '%s': max == min", paramInfo->name);
  817. max = min + 0.1f;
  818. }
  819. // default value
  820. def = paramInfo->ranges.def;
  821. if (def < min)
  822. def = min;
  823. else if (def > max)
  824. def = max;
  825. if (paramInfo->hints & NATIVE_PARAMETER_USES_SAMPLE_RATE)
  826. {
  827. min *= sampleRate;
  828. max *= sampleRate;
  829. def *= sampleRate;
  830. pData->param.data[j].hints |= PARAMETER_USES_SAMPLERATE;
  831. }
  832. if (paramInfo->hints & NATIVE_PARAMETER_IS_BOOLEAN)
  833. {
  834. step = max - min;
  835. stepSmall = step;
  836. stepLarge = step;
  837. pData->param.data[j].hints |= PARAMETER_IS_BOOLEAN;
  838. }
  839. else if (paramInfo->hints & NATIVE_PARAMETER_IS_INTEGER)
  840. {
  841. step = 1.0f;
  842. stepSmall = 1.0f;
  843. stepLarge = 10.0f;
  844. pData->param.data[j].hints |= PARAMETER_IS_INTEGER;
  845. }
  846. else
  847. {
  848. float range = max - min;
  849. step = range/100.0f;
  850. stepSmall = range/1000.0f;
  851. stepLarge = range/10.0f;
  852. }
  853. if (paramInfo->hints & NATIVE_PARAMETER_IS_OUTPUT)
  854. {
  855. pData->param.data[j].type = PARAMETER_OUTPUT;
  856. needsCtrlOut = true;
  857. }
  858. else
  859. {
  860. pData->param.data[j].type = PARAMETER_INPUT;
  861. needsCtrlIn = true;
  862. }
  863. // extra parameter hints
  864. if (paramInfo->hints & NATIVE_PARAMETER_IS_ENABLED)
  865. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  866. if (paramInfo->hints & NATIVE_PARAMETER_IS_AUTOMABLE)
  867. pData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  868. if (paramInfo->hints & NATIVE_PARAMETER_IS_LOGARITHMIC)
  869. pData->param.data[j].hints |= PARAMETER_IS_LOGARITHMIC;
  870. if (paramInfo->hints & NATIVE_PARAMETER_USES_SCALEPOINTS)
  871. pData->param.data[j].hints |= PARAMETER_USES_SCALEPOINTS;
  872. pData->param.ranges[j].min = min;
  873. pData->param.ranges[j].max = max;
  874. pData->param.ranges[j].def = def;
  875. pData->param.ranges[j].step = step;
  876. pData->param.ranges[j].stepSmall = stepSmall;
  877. pData->param.ranges[j].stepLarge = stepLarge;
  878. }
  879. if (needsCtrlIn && mIns <= 1)
  880. {
  881. portName.clear();
  882. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  883. {
  884. portName = pData->name;
  885. portName += ":";
  886. }
  887. portName += "events-in";
  888. portName.truncate(portNameSize);
  889. pData->event.portIn = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true, 0);
  890. }
  891. if (needsCtrlOut && mOuts <= 1)
  892. {
  893. portName.clear();
  894. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  895. {
  896. portName = pData->name;
  897. portName += ":";
  898. }
  899. portName += "events-out";
  900. portName.truncate(portNameSize);
  901. pData->event.portOut = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false, 0);
  902. }
  903. if (forcedStereoIn || forcedStereoOut)
  904. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  905. else
  906. pData->options &= ~PLUGIN_OPTION_FORCE_STEREO;
  907. // plugin hints
  908. pData->hints = 0x0;
  909. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  910. pData->hints |= PLUGIN_CAN_DRYWET;
  911. if (aOuts > 0)
  912. pData->hints |= PLUGIN_CAN_VOLUME;
  913. if (aOuts >= 2 && aOuts % 2 == 0)
  914. pData->hints |= PLUGIN_CAN_BALANCE;
  915. // native plugin hints
  916. if (fDescriptor->hints & NATIVE_PLUGIN_IS_RTSAFE)
  917. pData->hints |= PLUGIN_IS_RTSAFE;
  918. if (fDescriptor->hints & NATIVE_PLUGIN_IS_SYNTH)
  919. pData->hints |= PLUGIN_IS_SYNTH;
  920. if (fDescriptor->hints & NATIVE_PLUGIN_HAS_UI)
  921. pData->hints |= PLUGIN_HAS_CUSTOM_UI;
  922. if (fDescriptor->hints & NATIVE_PLUGIN_NEEDS_FIXED_BUFFERS)
  923. pData->hints |= PLUGIN_NEEDS_FIXED_BUFFERS;
  924. if (fDescriptor->hints & NATIVE_PLUGIN_NEEDS_UI_MAIN_THREAD)
  925. pData->hints |= PLUGIN_NEEDS_UI_MAIN_THREAD;
  926. if (fDescriptor->hints & NATIVE_PLUGIN_USES_MULTI_PROGS)
  927. pData->hints |= PLUGIN_USES_MULTI_PROGS;
  928. // extra plugin hints
  929. pData->extraHints = 0x0;
  930. if (aIns <= 2 && aOuts <= 2 && (aIns == aOuts || aIns == 0 || aOuts == 0) && mIns <= 1 && mOuts <= 1)
  931. pData->extraHints |= PLUGIN_EXTRA_HINT_CAN_RUN_RACK;
  932. bufferSizeChanged(pData->engine->getBufferSize());
  933. reloadPrograms(true);
  934. if (pData->active)
  935. activate();
  936. carla_debug("CarlaPluginNative::reload() - end");
  937. }
  938. void reloadPrograms(const bool doInit) override
  939. {
  940. carla_debug("CarlaPluginNative::reloadPrograms(%s)", bool2str(doInit));
  941. uint32_t i, oldCount = pData->midiprog.count;
  942. const int32_t current = pData->midiprog.current;
  943. // Delete old programs
  944. pData->midiprog.clear();
  945. // Query new programs
  946. uint32_t count = 0;
  947. if (fDescriptor->get_midi_program_count != nullptr && fDescriptor->get_midi_program_info != nullptr && fDescriptor->set_midi_program != nullptr)
  948. count = fDescriptor->get_midi_program_count(fHandle);
  949. if (count > 0)
  950. {
  951. pData->midiprog.createNew(count);
  952. // Update data
  953. for (i=0; i < count; ++i)
  954. {
  955. const NativeMidiProgram* const mpDesc(fDescriptor->get_midi_program_info(fHandle, i));
  956. CARLA_SAFE_ASSERT_CONTINUE(mpDesc != nullptr);
  957. pData->midiprog.data[i].bank = mpDesc->bank;
  958. pData->midiprog.data[i].program = mpDesc->program;
  959. pData->midiprog.data[i].name = carla_strdup(mpDesc->name);
  960. }
  961. }
  962. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  963. // Update OSC Names
  964. if (pData->engine->isOscControlRegistered() && pData->id < pData->engine->getCurrentPluginCount())
  965. {
  966. pData->engine->oscSend_control_set_midi_program_count(pData->id, count);
  967. for (i=0; i < count; ++i)
  968. pData->engine->oscSend_control_set_midi_program_data(pData->id, i, pData->midiprog.data[i].bank, pData->midiprog.data[i].program, pData->midiprog.data[i].name);
  969. }
  970. #endif
  971. if (doInit)
  972. {
  973. if (count > 0)
  974. setMidiProgram(0, false, false, false);
  975. }
  976. else
  977. {
  978. // Check if current program is invalid
  979. bool programChanged = false;
  980. if (count == oldCount+1)
  981. {
  982. // one midi program added, probably created by user
  983. pData->midiprog.current = static_cast<int32_t>(oldCount);
  984. programChanged = true;
  985. }
  986. else if (current < 0 && count > 0)
  987. {
  988. // programs exist now, but not before
  989. pData->midiprog.current = 0;
  990. programChanged = true;
  991. }
  992. else if (current >= 0 && count == 0)
  993. {
  994. // programs existed before, but not anymore
  995. pData->midiprog.current = -1;
  996. programChanged = true;
  997. }
  998. else if (current >= static_cast<int32_t>(count))
  999. {
  1000. // current midi program > count
  1001. pData->midiprog.current = 0;
  1002. programChanged = true;
  1003. }
  1004. else
  1005. {
  1006. // no change
  1007. pData->midiprog.current = current;
  1008. }
  1009. if (programChanged)
  1010. setMidiProgram(pData->midiprog.current, true, true, true);
  1011. pData->engine->callback(ENGINE_CALLBACK_RELOAD_PROGRAMS, pData->id, 0, 0, 0.0f, nullptr);
  1012. }
  1013. }
  1014. // -------------------------------------------------------------------
  1015. // Plugin processing
  1016. void activate() noexcept override
  1017. {
  1018. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1019. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1020. if (fDescriptor->activate != nullptr)
  1021. {
  1022. try {
  1023. fDescriptor->activate(fHandle);
  1024. } catch(...) {}
  1025. if (fHandle2 != nullptr)
  1026. {
  1027. try {
  1028. fDescriptor->activate(fHandle2);
  1029. } catch(...) {}
  1030. }
  1031. }
  1032. }
  1033. void deactivate() noexcept override
  1034. {
  1035. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1036. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1037. if (fDescriptor->deactivate != nullptr)
  1038. {
  1039. try {
  1040. fDescriptor->deactivate(fHandle);
  1041. } catch(...) {}
  1042. if (fHandle2 != nullptr)
  1043. {
  1044. try {
  1045. fDescriptor->deactivate(fHandle2);
  1046. } catch(...) {}
  1047. }
  1048. }
  1049. }
  1050. void process(const float** const audioIn, float** const audioOut, const float** const cvIn, float** const cvOut, const uint32_t frames) override
  1051. {
  1052. // --------------------------------------------------------------------------------------------------------
  1053. // Check if active
  1054. if (! pData->active)
  1055. {
  1056. // disable any output sound
  1057. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1058. carla_zeroFloats(audioOut[i], frames);
  1059. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  1060. carla_zeroFloats(cvOut[i], frames);
  1061. return;
  1062. }
  1063. fMidiEventCount = 0;
  1064. carla_zeroStructs(fMidiEvents, kPluginMaxMidiEvents*2);
  1065. // --------------------------------------------------------------------------------------------------------
  1066. // Check if needs reset
  1067. if (pData->needsReset)
  1068. {
  1069. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1070. {
  1071. fMidiEventCount = MAX_MIDI_CHANNELS*2;
  1072. for (uint8_t k=0, i=MAX_MIDI_CHANNELS; k < MAX_MIDI_CHANNELS; ++k)
  1073. {
  1074. fMidiEvents[k].data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (k & MIDI_CHANNEL_BIT));
  1075. fMidiEvents[k].data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  1076. fMidiEvents[k].data[2] = 0;
  1077. fMidiEvents[k].size = 3;
  1078. fMidiEvents[k+i].data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (k & MIDI_CHANNEL_BIT));
  1079. fMidiEvents[k+i].data[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  1080. fMidiEvents[k+i].data[2] = 0;
  1081. fMidiEvents[k+i].size = 3;
  1082. }
  1083. }
  1084. else if (pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  1085. {
  1086. fMidiEventCount = MAX_MIDI_NOTE;
  1087. for (uint8_t k=0; k < MAX_MIDI_NOTE; ++k)
  1088. {
  1089. fMidiEvents[k].data[0] = uint8_t(MIDI_STATUS_NOTE_OFF | (pData->ctrlChannel & MIDI_CHANNEL_BIT));
  1090. fMidiEvents[k].data[1] = k;
  1091. fMidiEvents[k].data[2] = 0;
  1092. fMidiEvents[k].size = 3;
  1093. }
  1094. }
  1095. pData->needsReset = false;
  1096. }
  1097. // --------------------------------------------------------------------------------------------------------
  1098. // Set TimeInfo
  1099. const EngineTimeInfo& timeInfo(pData->engine->getTimeInfo());
  1100. fTimeInfo.playing = timeInfo.playing;
  1101. fTimeInfo.frame = timeInfo.frame;
  1102. fTimeInfo.usecs = timeInfo.usecs;
  1103. if (timeInfo.bbt.valid)
  1104. {
  1105. fTimeInfo.bbt.valid = true;
  1106. fTimeInfo.bbt.bar = timeInfo.bbt.bar;
  1107. fTimeInfo.bbt.beat = timeInfo.bbt.beat;
  1108. fTimeInfo.bbt.tick = timeInfo.bbt.tick;
  1109. fTimeInfo.bbt.barStartTick = timeInfo.bbt.barStartTick;
  1110. fTimeInfo.bbt.beatsPerBar = timeInfo.bbt.beatsPerBar;
  1111. fTimeInfo.bbt.beatType = timeInfo.bbt.beatType;
  1112. fTimeInfo.bbt.ticksPerBeat = timeInfo.bbt.ticksPerBeat;
  1113. fTimeInfo.bbt.beatsPerMinute = timeInfo.bbt.beatsPerMinute;
  1114. }
  1115. else
  1116. {
  1117. fTimeInfo.bbt.valid = false;
  1118. }
  1119. // --------------------------------------------------------------------------------------------------------
  1120. // Event Input and Processing
  1121. if (pData->event.portIn != nullptr)
  1122. {
  1123. // ----------------------------------------------------------------------------------------------------
  1124. // MIDI Input (External)
  1125. if (pData->extNotes.mutex.tryLock())
  1126. {
  1127. ExternalMidiNote note = { 0, 0, 0 };
  1128. for (; fMidiEventCount < kPluginMaxMidiEvents*2 && ! pData->extNotes.data.isEmpty();)
  1129. {
  1130. note = pData->extNotes.data.getFirst(note, true);
  1131. CARLA_SAFE_ASSERT_CONTINUE(note.channel >= 0 && note.channel < MAX_MIDI_CHANNELS);
  1132. NativeMidiEvent& nativeEvent(fMidiEvents[fMidiEventCount++]);
  1133. nativeEvent.data[0] = uint8_t((note.velo > 0 ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF) | (note.channel & MIDI_CHANNEL_BIT));
  1134. nativeEvent.data[1] = note.note;
  1135. nativeEvent.data[2] = note.velo;
  1136. nativeEvent.size = 3;
  1137. }
  1138. pData->extNotes.mutex.unlock();
  1139. } // End of MIDI Input (External)
  1140. // ----------------------------------------------------------------------------------------------------
  1141. // Event Input (System)
  1142. #ifndef BUILD_BRIDGE
  1143. bool allNotesOffSent = false;
  1144. #endif
  1145. bool sampleAccurate = (pData->options & PLUGIN_OPTION_FIXED_BUFFERS) == 0;
  1146. uint32_t startTime = 0;
  1147. uint32_t timeOffset = 0;
  1148. uint32_t nextBankId;
  1149. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  1150. nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
  1151. else
  1152. nextBankId = 0;
  1153. for (uint32_t m=0, max=jmax(1U, fMidiIn.count); m < max; ++m)
  1154. {
  1155. CarlaEngineEventPort* const eventPort(m == 0 ? pData->event.portIn : fMidiIn.ports[m]);
  1156. for (uint32_t i=0, numEvents=eventPort->getEventCount(); i < numEvents; ++i)
  1157. {
  1158. const EngineEvent& event(eventPort->getEvent(i));
  1159. if (event.time >= frames)
  1160. continue;
  1161. CARLA_ASSERT_INT2(event.time >= timeOffset, event.time, timeOffset);
  1162. if (event.time > timeOffset && sampleAccurate)
  1163. {
  1164. if (processSingle(audioIn, audioOut, cvIn, cvOut, event.time - timeOffset, timeOffset))
  1165. {
  1166. startTime = 0;
  1167. timeOffset = event.time;
  1168. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  1169. nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
  1170. else
  1171. nextBankId = 0;
  1172. if (fMidiEventCount > 0)
  1173. {
  1174. carla_zeroStructs(fMidiEvents, fMidiEventCount);
  1175. fMidiEventCount = 0;
  1176. }
  1177. }
  1178. else
  1179. startTime += timeOffset;
  1180. }
  1181. // Control change
  1182. switch (event.type)
  1183. {
  1184. case kEngineEventTypeNull:
  1185. break;
  1186. case kEngineEventTypeControl: {
  1187. const EngineControlEvent& ctrlEvent = event.ctrl;
  1188. switch (ctrlEvent.type)
  1189. {
  1190. case kEngineControlEventTypeNull:
  1191. break;
  1192. case kEngineControlEventTypeParameter: {
  1193. #ifndef BUILD_BRIDGE
  1194. // Control backend stuff
  1195. if (event.channel == pData->ctrlChannel)
  1196. {
  1197. float value;
  1198. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_DRYWET) > 0)
  1199. {
  1200. value = ctrlEvent.value;
  1201. setDryWet(value, false, false);
  1202. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, value);
  1203. }
  1204. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_VOLUME) > 0)
  1205. {
  1206. value = ctrlEvent.value*127.0f/100.0f;
  1207. setVolume(value, false, false);
  1208. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
  1209. }
  1210. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_BALANCE) > 0)
  1211. {
  1212. float left, right;
  1213. value = ctrlEvent.value/0.5f - 1.0f;
  1214. if (value < 0.0f)
  1215. {
  1216. left = -1.0f;
  1217. right = (value*2.0f)+1.0f;
  1218. }
  1219. else if (value > 0.0f)
  1220. {
  1221. left = (value*2.0f)-1.0f;
  1222. right = 1.0f;
  1223. }
  1224. else
  1225. {
  1226. left = -1.0f;
  1227. right = 1.0f;
  1228. }
  1229. setBalanceLeft(left, false, false);
  1230. setBalanceRight(right, false, false);
  1231. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  1232. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  1233. }
  1234. }
  1235. #endif
  1236. // Control plugin parameters
  1237. for (uint32_t k=0; k < pData->param.count; ++k)
  1238. {
  1239. if (pData->param.data[k].midiChannel != event.channel)
  1240. continue;
  1241. if (pData->param.data[k].midiCC != ctrlEvent.param)
  1242. continue;
  1243. if (pData->param.data[k].type != PARAMETER_INPUT)
  1244. continue;
  1245. if ((pData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  1246. continue;
  1247. float value;
  1248. if (pData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  1249. {
  1250. value = (ctrlEvent.value < 0.5f) ? pData->param.ranges[k].min : pData->param.ranges[k].max;
  1251. }
  1252. else
  1253. {
  1254. if (pData->param.data[k].hints & PARAMETER_IS_LOGARITHMIC)
  1255. value = pData->param.ranges[k].getUnnormalizedLogValue(ctrlEvent.value);
  1256. else
  1257. value = pData->param.ranges[k].getUnnormalizedValue(ctrlEvent.value);
  1258. if (pData->param.data[k].hints & PARAMETER_IS_INTEGER)
  1259. value = std::rint(value);
  1260. }
  1261. setParameterValue(k, value, false, false, false);
  1262. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, value);
  1263. }
  1264. if ((pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0 && ctrlEvent.param < MAX_MIDI_CONTROL)
  1265. {
  1266. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1267. continue;
  1268. NativeMidiEvent& nativeEvent(fMidiEvents[fMidiEventCount++]);
  1269. carla_zeroStruct(nativeEvent);
  1270. nativeEvent.time = sampleAccurate ? startTime : event.time;
  1271. nativeEvent.data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  1272. nativeEvent.data[1] = uint8_t(ctrlEvent.param);
  1273. nativeEvent.data[2] = uint8_t(ctrlEvent.value*127.0f);
  1274. nativeEvent.size = 3;
  1275. }
  1276. break;
  1277. } // case kEngineControlEventTypeParameter
  1278. case kEngineControlEventTypeMidiBank:
  1279. if (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES)
  1280. {
  1281. if (event.channel == pData->ctrlChannel)
  1282. nextBankId = ctrlEvent.param;
  1283. }
  1284. else if (pData->options & PLUGIN_OPTION_SEND_PROGRAM_CHANGES)
  1285. {
  1286. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1287. continue;
  1288. NativeMidiEvent& nativeEvent(fMidiEvents[fMidiEventCount++]);
  1289. carla_zeroStruct(nativeEvent);
  1290. nativeEvent.time = sampleAccurate ? startTime : event.time;
  1291. nativeEvent.data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  1292. nativeEvent.data[1] = MIDI_CONTROL_BANK_SELECT;
  1293. nativeEvent.data[2] = uint8_t(ctrlEvent.param);
  1294. nativeEvent.size = 3;
  1295. }
  1296. break;
  1297. case kEngineControlEventTypeMidiProgram:
  1298. if (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES)
  1299. {
  1300. if (event.channel < MAX_MIDI_CHANNELS)
  1301. {
  1302. const uint32_t nextProgramId(ctrlEvent.param);
  1303. for (uint32_t k=0; k < pData->midiprog.count; ++k)
  1304. {
  1305. if (pData->midiprog.data[k].bank == nextBankId && pData->midiprog.data[k].program == nextProgramId)
  1306. {
  1307. fDescriptor->set_midi_program(fHandle, event.channel, nextBankId, nextProgramId);
  1308. if (fHandle2 != nullptr)
  1309. fDescriptor->set_midi_program(fHandle2, event.channel, nextBankId, nextProgramId);
  1310. fCurMidiProgs[event.channel] = static_cast<int32_t>(k);
  1311. if (event.channel == pData->ctrlChannel)
  1312. pData->postponeRtEvent(kPluginPostRtEventMidiProgramChange, static_cast<int32_t>(k), 0, 0.0f);
  1313. break;
  1314. }
  1315. }
  1316. }
  1317. }
  1318. else if (pData->options & PLUGIN_OPTION_SEND_PROGRAM_CHANGES)
  1319. {
  1320. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1321. continue;
  1322. NativeMidiEvent& nativeEvent(fMidiEvents[fMidiEventCount++]);
  1323. carla_zeroStruct(nativeEvent);
  1324. nativeEvent.time = sampleAccurate ? startTime : event.time;
  1325. nativeEvent.data[0] = uint8_t(MIDI_STATUS_PROGRAM_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  1326. nativeEvent.data[1] = uint8_t(ctrlEvent.param);
  1327. nativeEvent.size = 2;
  1328. }
  1329. break;
  1330. case kEngineControlEventTypeAllSoundOff:
  1331. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1332. {
  1333. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1334. continue;
  1335. NativeMidiEvent& nativeEvent(fMidiEvents[fMidiEventCount++]);
  1336. carla_zeroStruct(nativeEvent);
  1337. nativeEvent.time = sampleAccurate ? startTime : event.time;
  1338. nativeEvent.data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  1339. nativeEvent.data[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  1340. nativeEvent.data[2] = 0;
  1341. nativeEvent.size = 3;
  1342. }
  1343. break;
  1344. case kEngineControlEventTypeAllNotesOff:
  1345. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1346. {
  1347. #ifndef BUILD_BRIDGE
  1348. if (event.channel == pData->ctrlChannel && ! allNotesOffSent)
  1349. {
  1350. allNotesOffSent = true;
  1351. sendMidiAllNotesOffToCallback();
  1352. }
  1353. #endif
  1354. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1355. continue;
  1356. NativeMidiEvent& nativeEvent(fMidiEvents[fMidiEventCount++]);
  1357. carla_zeroStruct(nativeEvent);
  1358. nativeEvent.time = sampleAccurate ? startTime : event.time;
  1359. nativeEvent.data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  1360. nativeEvent.data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  1361. nativeEvent.data[2] = 0;
  1362. nativeEvent.size = 3;
  1363. }
  1364. break;
  1365. }
  1366. break;
  1367. }
  1368. case kEngineEventTypeMidi: {
  1369. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1370. continue;
  1371. const EngineMidiEvent& midiEvent(event.midi);
  1372. if (midiEvent.size > 4)
  1373. continue;
  1374. #ifdef CARLA_PROPER_CPP11_SUPPORT
  1375. static_assert(4 <= EngineMidiEvent::kDataSize, "Incorrect data");
  1376. #endif
  1377. uint8_t status = uint8_t(MIDI_GET_STATUS_FROM_DATA(midiEvent.data));
  1378. if (status == MIDI_STATUS_CHANNEL_PRESSURE && (pData->options & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) == 0)
  1379. continue;
  1380. if (status == MIDI_STATUS_CONTROL_CHANGE && (pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) == 0)
  1381. continue;
  1382. if (status == MIDI_STATUS_POLYPHONIC_AFTERTOUCH && (pData->options & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) == 0)
  1383. continue;
  1384. if (status == MIDI_STATUS_PITCH_WHEEL_CONTROL && (pData->options & PLUGIN_OPTION_SEND_PITCHBEND) == 0)
  1385. continue;
  1386. // Fix bad note-off
  1387. if (status == MIDI_STATUS_NOTE_ON && midiEvent.data[2] == 0)
  1388. status = MIDI_STATUS_NOTE_OFF;
  1389. NativeMidiEvent& nativeEvent(fMidiEvents[fMidiEventCount++]);
  1390. carla_zeroStruct(nativeEvent);
  1391. nativeEvent.port = midiEvent.port;
  1392. nativeEvent.time = sampleAccurate ? startTime : event.time;
  1393. nativeEvent.size = midiEvent.size;
  1394. nativeEvent.data[0] = uint8_t(status | (event.channel & MIDI_CHANNEL_BIT));
  1395. nativeEvent.data[1] = midiEvent.size >= 2 ? midiEvent.data[1] : 0;
  1396. nativeEvent.data[2] = midiEvent.size >= 3 ? midiEvent.data[2] : 0;
  1397. nativeEvent.data[3] = midiEvent.size == 4 ? midiEvent.data[3] : 0;
  1398. if (status == MIDI_STATUS_NOTE_ON)
  1399. pData->postponeRtEvent(kPluginPostRtEventNoteOn, event.channel, midiEvent.data[1], midiEvent.data[2]);
  1400. else if (status == MIDI_STATUS_NOTE_OFF)
  1401. pData->postponeRtEvent(kPluginPostRtEventNoteOff, event.channel, midiEvent.data[1], 0.0f);
  1402. } break;
  1403. } // switch (event.type)
  1404. }
  1405. pData->postRtEvents.trySplice();
  1406. if (frames > timeOffset)
  1407. processSingle(audioIn, audioOut, cvIn, cvOut, frames - timeOffset, timeOffset);
  1408. } // eventPort
  1409. } // End of Event Input and Processing
  1410. // --------------------------------------------------------------------------------------------------------
  1411. // Plugin processing (no events)
  1412. else
  1413. {
  1414. processSingle(audioIn, audioOut, cvIn, cvOut, frames, 0);
  1415. } // End of Plugin processing (no events)
  1416. // --------------------------------------------------------------------------------------------------------
  1417. // Control and MIDI Output
  1418. if (pData->event.portOut != nullptr)
  1419. {
  1420. #ifndef BUILD_BRIDGE
  1421. float value, curValue;
  1422. for (uint32_t k=0; k < pData->param.count; ++k)
  1423. {
  1424. if (pData->param.data[k].type != PARAMETER_OUTPUT)
  1425. continue;
  1426. curValue = fDescriptor->get_parameter_value(fHandle, k);
  1427. pData->param.ranges[k].fixValue(curValue);
  1428. if (pData->param.data[k].midiCC > 0)
  1429. {
  1430. value = pData->param.ranges[k].getNormalizedValue(curValue);
  1431. pData->event.portOut->writeControlEvent(0, pData->param.data[k].midiChannel, kEngineControlEventTypeParameter, static_cast<uint16_t>(pData->param.data[k].midiCC), value);
  1432. }
  1433. }
  1434. #endif
  1435. // reverse lookup MIDI events
  1436. for (uint32_t k = (kPluginMaxMidiEvents*2)-1; k >= fMidiEventCount; --k)
  1437. {
  1438. if (fMidiEvents[k].data[0] == 0)
  1439. break;
  1440. const uint8_t channel = uint8_t(MIDI_GET_CHANNEL_FROM_DATA(fMidiEvents[k].data));
  1441. const uint8_t port = fMidiEvents[k].port;
  1442. if (fMidiOut.count > 1 && port < fMidiOut.count)
  1443. fMidiOut.ports[port]->writeMidiEvent(fMidiEvents[k].time, channel, fMidiEvents[k].size, fMidiEvents[k].data);
  1444. else
  1445. pData->event.portOut->writeMidiEvent(fMidiEvents[k].time, channel, fMidiEvents[k].size, fMidiEvents[k].data);
  1446. }
  1447. } // End of Control and MIDI Output
  1448. }
  1449. bool processSingle(const float** const audioIn, float** const audioOut, const float** const cvIn, float** const cvOut, const uint32_t frames, const uint32_t timeOffset)
  1450. {
  1451. CARLA_SAFE_ASSERT_RETURN(frames > 0, false);
  1452. if (pData->audioIn.count > 0)
  1453. {
  1454. CARLA_SAFE_ASSERT_RETURN(audioIn != nullptr, false);
  1455. }
  1456. if (pData->audioOut.count > 0)
  1457. {
  1458. CARLA_SAFE_ASSERT_RETURN(audioOut != nullptr, false);
  1459. }
  1460. if (pData->cvIn.count > 0)
  1461. {
  1462. CARLA_SAFE_ASSERT_RETURN(cvIn != nullptr, false);
  1463. }
  1464. if (pData->cvOut.count > 0)
  1465. {
  1466. CARLA_SAFE_ASSERT_RETURN(cvOut != nullptr, false);
  1467. }
  1468. // --------------------------------------------------------------------------------------------------------
  1469. // Try lock, silence otherwise
  1470. if (fIsOffline)
  1471. {
  1472. pData->singleMutex.lock();
  1473. }
  1474. else if (! pData->singleMutex.tryLock())
  1475. {
  1476. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1477. {
  1478. for (uint32_t k=0; k < frames; ++k)
  1479. audioOut[i][k+timeOffset] = 0.0f;
  1480. }
  1481. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  1482. {
  1483. for (uint32_t k=0; k < frames; ++k)
  1484. cvOut[i][k+timeOffset] = 0.0f;
  1485. }
  1486. return false;
  1487. }
  1488. // --------------------------------------------------------------------------------------------------------
  1489. // Set audio buffers
  1490. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1491. carla_copyFloats(fAudioInBuffers[i], audioIn[i]+timeOffset, frames);
  1492. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1493. carla_zeroFloats(fAudioOutBuffers[i], frames);
  1494. #if 0
  1495. // --------------------------------------------------------------------------------------------------------
  1496. // Set CV buffers
  1497. for (uint32_t i=0; i < pData->cvIn.count; ++i)
  1498. carla_copyFloats(fCvInBuffers[i], cvIn[i]+timeOffset, frames);
  1499. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  1500. carla_zeroFloats(fCvOutBuffers[i], frames);
  1501. #endif
  1502. // --------------------------------------------------------------------------------------------------------
  1503. // Run plugin
  1504. fIsProcessing = true;
  1505. if (fHandle2 == nullptr)
  1506. {
  1507. fDescriptor->process(fHandle, fAudioInBuffers, fAudioOutBuffers, frames, fMidiEvents, fMidiEventCount);
  1508. }
  1509. else
  1510. {
  1511. fDescriptor->process(fHandle,
  1512. (pData->audioIn.count > 0) ? &fAudioInBuffers[0] : nullptr,
  1513. (pData->audioOut.count > 0) ? &fAudioOutBuffers[0] : nullptr,
  1514. frames, fMidiEvents, fMidiEventCount);
  1515. fDescriptor->process(fHandle2,
  1516. (pData->audioIn.count > 0) ? &fAudioInBuffers[1] : nullptr,
  1517. (pData->audioOut.count > 0) ? &fAudioOutBuffers[1] : nullptr,
  1518. frames, fMidiEvents, fMidiEventCount);
  1519. }
  1520. fIsProcessing = false;
  1521. fTimeInfo.frame += frames;
  1522. #ifndef BUILD_BRIDGE
  1523. // --------------------------------------------------------------------------------------------------------
  1524. // Post-processing (dry/wet, volume and balance)
  1525. {
  1526. const bool doDryWet = (pData->hints & PLUGIN_CAN_DRYWET) != 0 && carla_isNotEqual(pData->postProc.dryWet, 1.0f);
  1527. const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) != 0 && ! (carla_isEqual(pData->postProc.balanceLeft, -1.0f) && carla_isEqual(pData->postProc.balanceRight, 1.0f));
  1528. bool isPair;
  1529. float bufValue, oldBufLeft[doBalance ? frames : 1];
  1530. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1531. {
  1532. // Dry/Wet
  1533. if (doDryWet)
  1534. {
  1535. for (uint32_t k=0; k < frames; ++k)
  1536. {
  1537. bufValue = fAudioInBuffers[(pData->audioIn.count == 1) ? 0 : i][k];
  1538. fAudioOutBuffers[i][k] = (fAudioOutBuffers[i][k] * pData->postProc.dryWet) + (bufValue * (1.0f - pData->postProc.dryWet));
  1539. }
  1540. }
  1541. // Balance
  1542. if (doBalance)
  1543. {
  1544. isPair = (i % 2 == 0);
  1545. if (isPair)
  1546. {
  1547. CARLA_ASSERT(i+1 < pData->audioOut.count);
  1548. carla_copyFloats(oldBufLeft, fAudioOutBuffers[i], frames);
  1549. }
  1550. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  1551. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  1552. for (uint32_t k=0; k < frames; ++k)
  1553. {
  1554. if (isPair)
  1555. {
  1556. // left
  1557. fAudioOutBuffers[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  1558. fAudioOutBuffers[i][k] += fAudioOutBuffers[i+1][k] * (1.0f - balRangeR);
  1559. }
  1560. else
  1561. {
  1562. // right
  1563. fAudioOutBuffers[i][k] = fAudioOutBuffers[i][k] * balRangeR;
  1564. fAudioOutBuffers[i][k] += oldBufLeft[k] * balRangeL;
  1565. }
  1566. }
  1567. }
  1568. // Volume (and buffer copy)
  1569. {
  1570. for (uint32_t k=0; k < frames; ++k)
  1571. audioOut[i][k+timeOffset] = fAudioOutBuffers[i][k] * pData->postProc.volume;
  1572. }
  1573. }
  1574. } // End of Post-processing
  1575. #else
  1576. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1577. {
  1578. for (uint32_t k=0; k < frames; ++k)
  1579. audioOut[i][k+timeOffset] = fAudioOutBuffers[i][k];
  1580. }
  1581. #endif
  1582. #if 0
  1583. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  1584. {
  1585. for (uint32_t k=0; k < frames; ++k)
  1586. cvOut[i][k+timeOffset] = fCvOutBuffers[i][k];
  1587. }
  1588. #endif
  1589. // --------------------------------------------------------------------------------------------------------
  1590. pData->singleMutex.unlock();
  1591. return true;
  1592. }
  1593. void bufferSizeChanged(const uint32_t newBufferSize) override
  1594. {
  1595. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  1596. carla_debug("CarlaPluginNative::bufferSizeChanged(%i)", newBufferSize);
  1597. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1598. {
  1599. if (fAudioInBuffers[i] != nullptr)
  1600. delete[] fAudioInBuffers[i];
  1601. fAudioInBuffers[i] = new float[newBufferSize];
  1602. }
  1603. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1604. {
  1605. if (fAudioOutBuffers[i] != nullptr)
  1606. delete[] fAudioOutBuffers[i];
  1607. fAudioOutBuffers[i] = new float[newBufferSize];
  1608. }
  1609. if (fCurBufferSize == newBufferSize)
  1610. return;
  1611. fCurBufferSize = newBufferSize;
  1612. if (fDescriptor != nullptr && fDescriptor->dispatcher != nullptr)
  1613. {
  1614. fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_BUFFER_SIZE_CHANGED, 0, static_cast<intptr_t>(newBufferSize), nullptr, 0.0f);
  1615. if (fHandle2 != nullptr)
  1616. fDescriptor->dispatcher(fHandle2, NATIVE_PLUGIN_OPCODE_BUFFER_SIZE_CHANGED, 0, static_cast<intptr_t>(newBufferSize), nullptr, 0.0f);
  1617. }
  1618. }
  1619. void sampleRateChanged(const double newSampleRate) override
  1620. {
  1621. CARLA_ASSERT_INT(newSampleRate > 0.0, newSampleRate);
  1622. carla_debug("CarlaPluginNative::sampleRateChanged(%g)", newSampleRate);
  1623. if (carla_isEqual(fCurSampleRate, newSampleRate))
  1624. return;
  1625. fCurSampleRate = newSampleRate;
  1626. if (fDescriptor != nullptr && fDescriptor->dispatcher != nullptr)
  1627. {
  1628. fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_SAMPLE_RATE_CHANGED, 0, 0, nullptr, float(newSampleRate));
  1629. if (fHandle2 != nullptr)
  1630. fDescriptor->dispatcher(fHandle2, NATIVE_PLUGIN_OPCODE_SAMPLE_RATE_CHANGED, 0, 0, nullptr, float(newSampleRate));
  1631. }
  1632. }
  1633. void offlineModeChanged(const bool isOffline) override
  1634. {
  1635. if (fIsOffline == isOffline)
  1636. return;
  1637. fIsOffline = isOffline;
  1638. if (fDescriptor != nullptr && fDescriptor->dispatcher != nullptr)
  1639. {
  1640. fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_OFFLINE_CHANGED, 0, isOffline ? 1 : 0, nullptr, 0.0f);
  1641. if (fHandle2 != nullptr)
  1642. fDescriptor->dispatcher(fHandle2, NATIVE_PLUGIN_OPCODE_OFFLINE_CHANGED, 0, isOffline ? 1 : 0, nullptr, 0.0f);
  1643. }
  1644. }
  1645. // -------------------------------------------------------------------
  1646. // Plugin buffers
  1647. void initBuffers() const noexcept override
  1648. {
  1649. fMidiIn.initBuffers();
  1650. fMidiOut.initBuffers();
  1651. CarlaPlugin::initBuffers();
  1652. }
  1653. void clearBuffers() noexcept override
  1654. {
  1655. carla_debug("CarlaPluginNative::clearBuffers() - start");
  1656. if (fAudioInBuffers != nullptr)
  1657. {
  1658. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1659. {
  1660. if (fAudioInBuffers[i] != nullptr)
  1661. {
  1662. delete[] fAudioInBuffers[i];
  1663. fAudioInBuffers[i] = nullptr;
  1664. }
  1665. }
  1666. delete[] fAudioInBuffers;
  1667. fAudioInBuffers = nullptr;
  1668. }
  1669. if (fAudioOutBuffers != nullptr)
  1670. {
  1671. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1672. {
  1673. if (fAudioOutBuffers[i] != nullptr)
  1674. {
  1675. delete[] fAudioOutBuffers[i];
  1676. fAudioOutBuffers[i] = nullptr;
  1677. }
  1678. }
  1679. delete[] fAudioOutBuffers;
  1680. fAudioOutBuffers = nullptr;
  1681. }
  1682. if (fMidiIn.count > 1)
  1683. pData->event.portIn = nullptr;
  1684. if (fMidiOut.count > 1)
  1685. pData->event.portOut = nullptr;
  1686. fMidiIn.clear();
  1687. fMidiOut.clear();
  1688. CarlaPlugin::clearBuffers();
  1689. carla_debug("CarlaPluginNative::clearBuffers() - end");
  1690. }
  1691. // -------------------------------------------------------------------
  1692. // Post-poned UI Stuff
  1693. void uiParameterChange(const uint32_t index, const float value) noexcept override
  1694. {
  1695. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1696. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1697. CARLA_SAFE_ASSERT_RETURN(index < pData->param.count,);
  1698. if (! fIsUiVisible)
  1699. return;
  1700. if (fDescriptor->ui_set_parameter_value != nullptr)
  1701. fDescriptor->ui_set_parameter_value(fHandle, index, value);
  1702. }
  1703. void uiMidiProgramChange(const uint32_t index) noexcept override
  1704. {
  1705. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1706. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1707. CARLA_SAFE_ASSERT_RETURN(index < pData->midiprog.count,);
  1708. if (! fIsUiVisible)
  1709. return;
  1710. if (index >= pData->midiprog.count)
  1711. return;
  1712. if (fDescriptor->ui_set_midi_program != nullptr)
  1713. fDescriptor->ui_set_midi_program(fHandle, 0, pData->midiprog.data[index].bank, pData->midiprog.data[index].program);
  1714. }
  1715. void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) noexcept override
  1716. {
  1717. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1718. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1719. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1720. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1721. CARLA_SAFE_ASSERT_RETURN(velo > 0 && velo < MAX_MIDI_VALUE,);
  1722. if (! fIsUiVisible)
  1723. return;
  1724. // TODO
  1725. }
  1726. void uiNoteOff(const uint8_t channel, const uint8_t note) noexcept override
  1727. {
  1728. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1729. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1730. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1731. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1732. if (! fIsUiVisible)
  1733. return;
  1734. if (fDescriptor == nullptr || fHandle == nullptr)
  1735. return;
  1736. // TODO
  1737. }
  1738. // -------------------------------------------------------------------
  1739. protected:
  1740. const NativeTimeInfo* handleGetTimeInfo() const noexcept
  1741. {
  1742. CARLA_SAFE_ASSERT_RETURN(fIsProcessing, nullptr);
  1743. return &fTimeInfo;
  1744. }
  1745. bool handleWriteMidiEvent(const NativeMidiEvent* const event)
  1746. {
  1747. CARLA_SAFE_ASSERT_RETURN(pData->enabled, false);
  1748. CARLA_SAFE_ASSERT_RETURN(fIsProcessing, false);
  1749. CARLA_SAFE_ASSERT_RETURN(fMidiOut.count > 0 || pData->event.portOut != nullptr, false);
  1750. CARLA_SAFE_ASSERT_RETURN(event != nullptr, false);
  1751. CARLA_SAFE_ASSERT_RETURN(event->data[0] != 0, false);
  1752. // reverse-find first free event, and put it there
  1753. for (uint32_t i=(kPluginMaxMidiEvents*2)-1; i >= fMidiEventCount; --i)
  1754. {
  1755. if (fMidiEvents[i].data[0] != 0)
  1756. continue;
  1757. std::memcpy(&fMidiEvents[i], event, sizeof(NativeMidiEvent));
  1758. return true;
  1759. }
  1760. carla_stdout("CarlaPluginNative::handleWriteMidiEvent(%p) - buffer full", event);
  1761. return false;
  1762. }
  1763. void handleUiParameterChanged(const uint32_t index, const float value)
  1764. {
  1765. setParameterValue(index, value, false, true, true);
  1766. }
  1767. void handleUiCustomDataChanged(const char* const key, const char* const value)
  1768. {
  1769. setCustomData(CUSTOM_DATA_TYPE_STRING, key, value, false);
  1770. }
  1771. void handleUiClosed()
  1772. {
  1773. pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, 0, 0, 0.0f, nullptr);
  1774. fIsUiVisible = false;
  1775. }
  1776. const char* handleUiOpenFile(const bool isDir, const char* const title, const char* const filter)
  1777. {
  1778. return pData->engine->runFileCallback(FILE_CALLBACK_OPEN, isDir, title, filter);
  1779. }
  1780. const char* handleUiSaveFile(const bool isDir, const char* const title, const char* const filter)
  1781. {
  1782. return pData->engine->runFileCallback(FILE_CALLBACK_SAVE, isDir, title, filter);
  1783. }
  1784. intptr_t handleDispatcher(const NativeHostDispatcherOpcode opcode, const int32_t index, const intptr_t value, void* const ptr, const float opt)
  1785. {
  1786. carla_debug("CarlaPluginNative::handleDispatcher(%i, %i, " P_INTPTR ", %p, %f)", opcode, index, value, ptr, opt);
  1787. intptr_t ret = 0;
  1788. switch (opcode)
  1789. {
  1790. case NATIVE_HOST_OPCODE_NULL:
  1791. break;
  1792. case NATIVE_HOST_OPCODE_UPDATE_PARAMETER:
  1793. // TODO
  1794. pData->engine->callback(ENGINE_CALLBACK_UPDATE, pData->id, -1, 0, 0.0f, nullptr);
  1795. break;
  1796. case NATIVE_HOST_OPCODE_UPDATE_MIDI_PROGRAM:
  1797. // TODO
  1798. pData->engine->callback(ENGINE_CALLBACK_UPDATE, pData->id, -1, 0, 0.0f, nullptr);
  1799. break;
  1800. case NATIVE_HOST_OPCODE_RELOAD_PARAMETERS:
  1801. reload(); // FIXME
  1802. pData->engine->callback(ENGINE_CALLBACK_RELOAD_PARAMETERS, pData->id, -1, 0, 0.0f, nullptr);
  1803. break;
  1804. case NATIVE_HOST_OPCODE_RELOAD_MIDI_PROGRAMS:
  1805. reloadPrograms(false);
  1806. pData->engine->callback(ENGINE_CALLBACK_RELOAD_PROGRAMS, pData->id, -1, 0, 0.0f, nullptr);
  1807. break;
  1808. case NATIVE_HOST_OPCODE_RELOAD_ALL:
  1809. reload();
  1810. pData->engine->callback(ENGINE_CALLBACK_RELOAD_ALL, pData->id, -1, 0, 0.0f, nullptr);
  1811. break;
  1812. case NATIVE_HOST_OPCODE_UI_UNAVAILABLE:
  1813. pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, -1, 0, 0.0f, nullptr);
  1814. fIsUiAvailable = false;
  1815. break;
  1816. case NATIVE_HOST_OPCODE_HOST_IDLE:
  1817. pData->engine->callback(ENGINE_CALLBACK_IDLE, 0, 0, 0, 0.0f, nullptr);
  1818. break;
  1819. case NATIVE_HOST_OPCODE_INTERNAL_PLUGIN:
  1820. ret = 1;
  1821. break;
  1822. }
  1823. return ret;
  1824. // unused for now
  1825. (void)index;
  1826. (void)value;
  1827. (void)ptr;
  1828. (void)opt;
  1829. }
  1830. // -------------------------------------------------------------------
  1831. public:
  1832. void* getNativeHandle() const noexcept override
  1833. {
  1834. return fHandle;
  1835. }
  1836. const void* getNativeDescriptor() const noexcept override
  1837. {
  1838. return fDescriptor;
  1839. }
  1840. // -------------------------------------------------------------------
  1841. bool init(const char* const name, const char* const label, const uint options)
  1842. {
  1843. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  1844. // ---------------------------------------------------------------
  1845. // first checks
  1846. if (pData->client != nullptr)
  1847. {
  1848. pData->engine->setLastError("Plugin client is already registered");
  1849. return false;
  1850. }
  1851. if (label == nullptr && label[0] != '\0')
  1852. {
  1853. pData->engine->setLastError("null label");
  1854. return false;
  1855. }
  1856. // ---------------------------------------------------------------
  1857. // get descriptor that matches label
  1858. sPluginInitializer.initIfNeeded();
  1859. for (LinkedList<const NativePluginDescriptor*>::Itenerator it = gPluginDescriptors.begin2(); it.valid(); it.next())
  1860. {
  1861. fDescriptor = it.getValue(nullptr);
  1862. CARLA_SAFE_ASSERT_BREAK(fDescriptor != nullptr);
  1863. carla_debug("Check vs \"%s\"", fDescriptor->label);
  1864. if (fDescriptor->label != nullptr && std::strcmp(fDescriptor->label, label) == 0)
  1865. break;
  1866. fDescriptor = nullptr;
  1867. }
  1868. if (fDescriptor == nullptr)
  1869. {
  1870. pData->engine->setLastError("Invalid internal plugin");
  1871. return false;
  1872. }
  1873. // ---------------------------------------------------------------
  1874. // set icon
  1875. if (std::strcmp(fDescriptor->label, "audiofile") == 0)
  1876. pData->iconName = carla_strdup_safe("file");
  1877. else if (std::strcmp(fDescriptor->label, "midifile") == 0)
  1878. pData->iconName = carla_strdup_safe("file");
  1879. // ---------------------------------------------------------------
  1880. // get info
  1881. if (name != nullptr && name[0] != '\0')
  1882. pData->name = pData->engine->getUniquePluginName(name);
  1883. else if (fDescriptor->name != nullptr && fDescriptor->name[0] != '\0')
  1884. pData->name = pData->engine->getUniquePluginName(fDescriptor->name);
  1885. else
  1886. pData->name = pData->engine->getUniquePluginName(label);
  1887. {
  1888. CARLA_ASSERT(fHost.uiName == nullptr);
  1889. char uiName[std::strlen(pData->name)+6+1];
  1890. std::strcpy(uiName, pData->name);
  1891. std::strcat(uiName, " (GUI)");
  1892. fHost.uiName = carla_strdup(uiName);
  1893. }
  1894. // ---------------------------------------------------------------
  1895. // register client
  1896. pData->client = pData->engine->addClient(this);
  1897. if (pData->client == nullptr || ! pData->client->isOk())
  1898. {
  1899. pData->engine->setLastError("Failed to register plugin client");
  1900. return false;
  1901. }
  1902. // ---------------------------------------------------------------
  1903. // initialize plugin
  1904. fHandle = fDescriptor->instantiate(&fHost);
  1905. if (fHandle == nullptr)
  1906. {
  1907. pData->engine->setLastError("Plugin failed to initialize");
  1908. return false;
  1909. }
  1910. // ---------------------------------------------------------------
  1911. // set default options
  1912. bool hasMidiProgs = false;
  1913. if (fDescriptor->get_midi_program_count != nullptr)
  1914. {
  1915. try {
  1916. hasMidiProgs = fDescriptor->get_midi_program_count(fHandle) > 0;
  1917. } catch (...) {}
  1918. }
  1919. pData->options = 0x0;
  1920. if (fMidiOut.count != 0 || (fDescriptor->hints & NATIVE_PLUGIN_NEEDS_FIXED_BUFFERS) != 0)
  1921. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  1922. else if (options & PLUGIN_OPTION_FIXED_BUFFERS)
  1923. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  1924. if (pData->engine->getOptions().forceStereo)
  1925. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  1926. else if (options & PLUGIN_OPTION_FORCE_STEREO)
  1927. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  1928. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_CHANNEL_PRESSURE)
  1929. pData->options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  1930. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_NOTE_AFTERTOUCH)
  1931. pData->options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  1932. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_PITCHBEND)
  1933. pData->options |= PLUGIN_OPTION_SEND_PITCHBEND;
  1934. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_ALL_SOUND_OFF)
  1935. pData->options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  1936. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_CONTROL_CHANGES)
  1937. {
  1938. if (options & PLUGIN_OPTION_SEND_CONTROL_CHANGES)
  1939. pData->options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  1940. }
  1941. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_PROGRAM_CHANGES)
  1942. {
  1943. CARLA_SAFE_ASSERT(! hasMidiProgs);
  1944. pData->options |= PLUGIN_OPTION_SEND_PROGRAM_CHANGES;
  1945. }
  1946. else if (hasMidiProgs)
  1947. pData->options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  1948. return true;
  1949. }
  1950. private:
  1951. NativePluginHandle fHandle;
  1952. NativePluginHandle fHandle2;
  1953. NativeHostDescriptor fHost;
  1954. const NativePluginDescriptor* fDescriptor;
  1955. bool fIsProcessing;
  1956. bool fIsOffline;
  1957. bool fIsUiAvailable;
  1958. bool fIsUiVisible;
  1959. float** fAudioInBuffers;
  1960. float** fAudioOutBuffers;
  1961. uint32_t fMidiEventCount;
  1962. NativeMidiEvent fMidiEvents[kPluginMaxMidiEvents*2];
  1963. int32_t fCurMidiProgs[MAX_MIDI_CHANNELS];
  1964. uint32_t fCurBufferSize;
  1965. double fCurSampleRate;
  1966. NativePluginMidiData fMidiIn;
  1967. NativePluginMidiData fMidiOut;
  1968. NativeTimeInfo fTimeInfo;
  1969. // -------------------------------------------------------------------
  1970. #define handlePtr ((CarlaPluginNative*)handle)
  1971. static uint32_t carla_host_get_buffer_size(NativeHostHandle handle) noexcept
  1972. {
  1973. return handlePtr->fCurBufferSize;
  1974. }
  1975. static double carla_host_get_sample_rate(NativeHostHandle handle) noexcept
  1976. {
  1977. return handlePtr->fCurSampleRate;
  1978. }
  1979. static bool carla_host_is_offline(NativeHostHandle handle) noexcept
  1980. {
  1981. return handlePtr->fIsOffline;
  1982. }
  1983. static const NativeTimeInfo* carla_host_get_time_info(NativeHostHandle handle) noexcept
  1984. {
  1985. return handlePtr->handleGetTimeInfo();
  1986. }
  1987. static bool carla_host_write_midi_event(NativeHostHandle handle, const NativeMidiEvent* event)
  1988. {
  1989. return handlePtr->handleWriteMidiEvent(event);
  1990. }
  1991. static void carla_host_ui_parameter_changed(NativeHostHandle handle, uint32_t index, float value)
  1992. {
  1993. handlePtr->handleUiParameterChanged(index, value);
  1994. }
  1995. static void carla_host_ui_custom_data_changed(NativeHostHandle handle, const char* key, const char* value)
  1996. {
  1997. handlePtr->handleUiCustomDataChanged(key, value);
  1998. }
  1999. static void carla_host_ui_closed(NativeHostHandle handle)
  2000. {
  2001. handlePtr->handleUiClosed();
  2002. }
  2003. static const char* carla_host_ui_open_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
  2004. {
  2005. return handlePtr->handleUiOpenFile(isDir, title, filter);
  2006. }
  2007. static const char* carla_host_ui_save_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
  2008. {
  2009. return handlePtr->handleUiSaveFile(isDir, title, filter);
  2010. }
  2011. static intptr_t carla_host_dispatcher(NativeHostHandle handle, NativeHostDispatcherOpcode opcode, int32_t index, intptr_t value, void* ptr, float opt)
  2012. {
  2013. return handlePtr->handleDispatcher(opcode, index, value, ptr, opt);
  2014. }
  2015. #undef handlePtr
  2016. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaPluginNative)
  2017. };
  2018. // -----------------------------------------------------------------------
  2019. CarlaPlugin* CarlaPlugin::newNative(const Initializer& init)
  2020. {
  2021. carla_debug("CarlaPlugin::newNative({%p, \"%s\", \"%s\", \"%s\", " P_INT64 "})", init.engine, init.filename, init.name, init.label, init.uniqueId);
  2022. CarlaPluginNative* const plugin(new CarlaPluginNative(init.engine, init.id));
  2023. if (! plugin->init(init.name, init.label, init.options))
  2024. {
  2025. delete plugin;
  2026. return nullptr;
  2027. }
  2028. return plugin;
  2029. }
  2030. // -----------------------------------------------------------------------
  2031. CARLA_BACKEND_END_NAMESPACE