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

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