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

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
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
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067
  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. else
  794. {
  795. fInlineDisplayNeedsRedraw = false;
  796. }
  797. }
  798. CarlaPlugin::idle();
  799. }
  800. void uiIdle() override
  801. {
  802. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  803. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  804. if (fIsUiVisible && fDescriptor->ui_idle != nullptr)
  805. fDescriptor->ui_idle(fHandle);
  806. CarlaPlugin::uiIdle();
  807. }
  808. // -------------------------------------------------------------------
  809. // Plugin state
  810. void reload() override
  811. {
  812. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr,);
  813. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  814. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  815. carla_debug("CarlaPluginNative::reload() - start");
  816. const EngineProcessMode processMode(pData->engine->getProccessMode());
  817. // Safely disable plugin for reload
  818. const ScopedDisabler sd(this);
  819. if (pData->active)
  820. deactivate();
  821. clearBuffers();
  822. uint32_t aIns, aOuts, cvIns, cvOuts, mIns, mOuts, j;
  823. bool forcedStereoIn, forcedStereoOut;
  824. forcedStereoIn = forcedStereoOut = false;
  825. bool needsCtrlIn, needsCtrlOut;
  826. needsCtrlIn = needsCtrlOut = false;
  827. aIns = fDescriptor->audioIns;
  828. aOuts = fDescriptor->audioOuts;
  829. cvIns = fDescriptor->cvIns;
  830. cvOuts = fDescriptor->cvOuts;
  831. mIns = fDescriptor->midiIns;
  832. mOuts = fDescriptor->midiOuts;
  833. if ((pData->options & PLUGIN_OPTION_FORCE_STEREO) != 0 && (aIns == 1 || aOuts == 1) && mIns <= 1 && mOuts <= 1)
  834. {
  835. if (fHandle2 == nullptr)
  836. fHandle2 = fDescriptor->instantiate(&fHost);
  837. if (fHandle2 != nullptr)
  838. {
  839. if (aIns == 1)
  840. {
  841. aIns = 2;
  842. forcedStereoIn = true;
  843. }
  844. if (aOuts == 1)
  845. {
  846. aOuts = 2;
  847. forcedStereoOut = true;
  848. }
  849. }
  850. }
  851. if (aIns > 0)
  852. {
  853. pData->audioIn.createNew(aIns);
  854. }
  855. if (aOuts > 0)
  856. {
  857. pData->audioOut.createNew(aOuts);
  858. needsCtrlIn = true;
  859. }
  860. if (cvIns > 0)
  861. {
  862. pData->cvIn.createNew(cvIns);
  863. }
  864. if (cvOuts > 0)
  865. {
  866. pData->cvOut.createNew(cvOuts);
  867. }
  868. if (const uint32_t acIns = aIns + cvIns)
  869. {
  870. fAudioAndCvInBuffers = new float*[acIns];
  871. carla_zeroPointers(fAudioAndCvInBuffers, acIns);
  872. }
  873. if (const uint32_t acOuts = aOuts + cvOuts)
  874. {
  875. fAudioAndCvOutBuffers = new float*[acOuts];
  876. carla_zeroPointers(fAudioAndCvOutBuffers, acOuts);
  877. }
  878. if (mIns > 0)
  879. {
  880. fMidiIn.createNew(mIns);
  881. needsCtrlIn = (mIns == 1);
  882. }
  883. if (mOuts > 0)
  884. {
  885. fMidiOut.createNew(mOuts);
  886. needsCtrlOut = (mOuts == 1);
  887. }
  888. const uint portNameSize(pData->engine->getMaxPortNameSize());
  889. CarlaString portName;
  890. // Audio Ins
  891. for (j=0; j < aIns; ++j)
  892. {
  893. portName.clear();
  894. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  895. {
  896. portName = pData->name;
  897. portName += ":";
  898. }
  899. if (fDescriptor->get_buffer_port_name != nullptr)
  900. {
  901. portName += fDescriptor->get_buffer_port_name(fHandle, forcedStereoIn ? 0 : j, false);
  902. }
  903. else if (aIns > 1 && ! forcedStereoIn)
  904. {
  905. portName += "input_";
  906. portName += CarlaString(j+1);
  907. }
  908. else
  909. portName += "input";
  910. portName.truncate(portNameSize);
  911. pData->audioIn.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true, j);
  912. pData->audioIn.ports[j].rindex = j;
  913. if (forcedStereoIn)
  914. {
  915. portName += "_2";
  916. pData->audioIn.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true, 1);
  917. pData->audioIn.ports[1].rindex = j;
  918. break;
  919. }
  920. }
  921. // Audio Outs
  922. for (j=0; j < aOuts; ++j)
  923. {
  924. portName.clear();
  925. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  926. {
  927. portName = pData->name;
  928. portName += ":";
  929. }
  930. if (fDescriptor->get_buffer_port_name != nullptr)
  931. {
  932. portName += fDescriptor->get_buffer_port_name(fHandle, forcedStereoOut ? 0 : j, true);
  933. }
  934. else if (aOuts > 1 && ! forcedStereoOut)
  935. {
  936. portName += "output_";
  937. portName += CarlaString(j+1);
  938. }
  939. else
  940. portName += "output";
  941. portName.truncate(portNameSize);
  942. pData->audioOut.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false, j);
  943. pData->audioOut.ports[j].rindex = j;
  944. if (forcedStereoOut)
  945. {
  946. portName += "_2";
  947. pData->audioOut.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false, 1);
  948. pData->audioOut.ports[1].rindex = j;
  949. break;
  950. }
  951. }
  952. // CV Ins
  953. for (j=0; j < cvIns; ++j)
  954. {
  955. portName.clear();
  956. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  957. {
  958. portName = pData->name;
  959. portName += ":";
  960. }
  961. if (fDescriptor->get_buffer_port_name != nullptr)
  962. {
  963. portName += fDescriptor->get_buffer_port_name(fHandle, fDescriptor->audioIns + j, false);
  964. }
  965. else if (cvIns > 1)
  966. {
  967. portName += "cv_input_";
  968. portName += CarlaString(j+1);
  969. }
  970. else
  971. portName += "cv_input";
  972. portName.truncate(portNameSize);
  973. float min = -1.0f, max = 1.0f;
  974. if (fDescriptor->get_buffer_port_range != nullptr)
  975. {
  976. if (const NativePortRange* const range = fDescriptor->get_buffer_port_range(fHandle,
  977. fDescriptor->audioIns + j,
  978. false))
  979. {
  980. min = range->minimum;
  981. max = range->maximum;
  982. }
  983. }
  984. pData->cvIn.ports[j].port = (CarlaEngineCVPort*)pData->client->addPort(kEnginePortTypeCV, portName, true, j);
  985. pData->cvIn.ports[j].rindex = j;
  986. pData->cvIn.ports[j].port->setRange(min, max);
  987. }
  988. // CV Outs
  989. for (j=0; j < cvOuts; ++j)
  990. {
  991. portName.clear();
  992. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  993. {
  994. portName = pData->name;
  995. portName += ":";
  996. }
  997. if (fDescriptor->get_buffer_port_name != nullptr)
  998. {
  999. portName += fDescriptor->get_buffer_port_name(fHandle, fDescriptor->audioOuts + j, true);
  1000. }
  1001. else if (cvOuts > 1)
  1002. {
  1003. portName += "cv_output_";
  1004. portName += CarlaString(j+1);
  1005. }
  1006. else
  1007. portName += "cv_output";
  1008. portName.truncate(portNameSize);
  1009. float min = -1.0f, max = 1.0f;
  1010. if (fDescriptor->get_buffer_port_range != nullptr)
  1011. {
  1012. if (const NativePortRange* const range = fDescriptor->get_buffer_port_range(fHandle,
  1013. fDescriptor->audioOuts + j,
  1014. true))
  1015. {
  1016. min = range->minimum;
  1017. max = range->maximum;
  1018. }
  1019. }
  1020. pData->cvOut.ports[j].port = (CarlaEngineCVPort*)pData->client->addPort(kEnginePortTypeCV, portName, false, j);
  1021. pData->cvOut.ports[j].rindex = j;
  1022. pData->cvOut.ports[j].port->setRange(min, max);
  1023. }
  1024. // MIDI Input (only if multiple)
  1025. if (mIns > 1)
  1026. {
  1027. for (j=0; j < mIns; ++j)
  1028. {
  1029. portName.clear();
  1030. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  1031. {
  1032. portName = pData->name;
  1033. portName += ":";
  1034. }
  1035. portName += "midi-in_";
  1036. portName += CarlaString(j+1);
  1037. portName.truncate(portNameSize);
  1038. fMidiIn.ports[j] = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true, j);
  1039. fMidiIn.indexes[j] = j;
  1040. }
  1041. pData->event.portIn = fMidiIn.ports[0];
  1042. }
  1043. // MIDI Output (only if multiple)
  1044. if (mOuts > 1)
  1045. {
  1046. for (j=0; j < mOuts; ++j)
  1047. {
  1048. portName.clear();
  1049. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  1050. {
  1051. portName = pData->name;
  1052. portName += ":";
  1053. }
  1054. portName += "midi-out_";
  1055. portName += CarlaString(j+1);
  1056. portName.truncate(portNameSize);
  1057. fMidiOut.ports[j] = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false, j);
  1058. fMidiOut.indexes[j] = j;
  1059. }
  1060. pData->event.portOut = fMidiOut.ports[0];
  1061. }
  1062. reloadParameters(&needsCtrlIn, &needsCtrlOut);
  1063. if (needsCtrlIn || mIns == 1)
  1064. {
  1065. portName.clear();
  1066. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  1067. {
  1068. portName = pData->name;
  1069. portName += ":";
  1070. }
  1071. portName += "events-in";
  1072. portName.truncate(portNameSize);
  1073. pData->event.portIn = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true, 0);
  1074. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1075. pData->event.cvSourcePorts = pData->client->createCVSourcePorts();
  1076. #endif
  1077. }
  1078. if (needsCtrlOut || mOuts == 1)
  1079. {
  1080. portName.clear();
  1081. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  1082. {
  1083. portName = pData->name;
  1084. portName += ":";
  1085. }
  1086. portName += "events-out";
  1087. portName.truncate(portNameSize);
  1088. pData->event.portOut = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false, 0);
  1089. }
  1090. if (forcedStereoIn || forcedStereoOut)
  1091. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  1092. else
  1093. pData->options &= ~PLUGIN_OPTION_FORCE_STEREO;
  1094. // plugin hints
  1095. pData->hints = 0x0;
  1096. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  1097. pData->hints |= PLUGIN_CAN_DRYWET;
  1098. if (aOuts > 0)
  1099. pData->hints |= PLUGIN_CAN_VOLUME;
  1100. if (aOuts >= 2 && aOuts % 2 == 0)
  1101. pData->hints |= PLUGIN_CAN_BALANCE;
  1102. // native plugin hints
  1103. if (fDescriptor->hints & NATIVE_PLUGIN_IS_RTSAFE)
  1104. pData->hints |= PLUGIN_IS_RTSAFE;
  1105. if (fDescriptor->hints & NATIVE_PLUGIN_IS_SYNTH)
  1106. pData->hints |= PLUGIN_IS_SYNTH;
  1107. if (fDescriptor->hints & NATIVE_PLUGIN_HAS_UI)
  1108. pData->hints |= PLUGIN_HAS_CUSTOM_UI;
  1109. if (fDescriptor->hints & NATIVE_PLUGIN_NEEDS_FIXED_BUFFERS)
  1110. pData->hints |= PLUGIN_NEEDS_FIXED_BUFFERS;
  1111. if (fDescriptor->hints & NATIVE_PLUGIN_NEEDS_UI_MAIN_THREAD)
  1112. pData->hints |= PLUGIN_NEEDS_UI_MAIN_THREAD;
  1113. if (fDescriptor->hints & NATIVE_PLUGIN_USES_MULTI_PROGS)
  1114. pData->hints |= PLUGIN_USES_MULTI_PROGS;
  1115. if (fDescriptor->hints & NATIVE_PLUGIN_HAS_INLINE_DISPLAY)
  1116. pData->hints |= PLUGIN_HAS_INLINE_DISPLAY;
  1117. // extra plugin hints
  1118. pData->extraHints = 0x0;
  1119. bufferSizeChanged(pData->engine->getBufferSize());
  1120. reloadPrograms(true);
  1121. if (pData->active)
  1122. activate();
  1123. carla_debug("CarlaPluginNative::reload() - end");
  1124. }
  1125. void reloadParameters(bool* const needsCtrlIn, bool* const needsCtrlOut)
  1126. {
  1127. carla_debug("CarlaPluginNative::reloadParameters() - start");
  1128. const float sampleRate = static_cast<float>(pData->engine->getSampleRate());
  1129. const uint32_t params = (fDescriptor->get_parameter_count != nullptr && fDescriptor->get_parameter_info != nullptr)
  1130. ? fDescriptor->get_parameter_count(fHandle)
  1131. : 0;
  1132. pData->param.clear();
  1133. if (params > 0)
  1134. {
  1135. pData->param.createNew(params, true);
  1136. }
  1137. for (uint32_t j=0; j < params; ++j)
  1138. {
  1139. const NativeParameter* const paramInfo(fDescriptor->get_parameter_info(fHandle, j));
  1140. CARLA_SAFE_ASSERT_CONTINUE(paramInfo != nullptr);
  1141. pData->param.data[j].type = PARAMETER_UNKNOWN;
  1142. pData->param.data[j].index = static_cast<int32_t>(j);
  1143. pData->param.data[j].rindex = static_cast<int32_t>(j);
  1144. float min, max, def, step, stepSmall, stepLarge;
  1145. // min value
  1146. min = paramInfo->ranges.min;
  1147. // max value
  1148. max = paramInfo->ranges.max;
  1149. if (min > max)
  1150. max = min;
  1151. if (carla_isEqual(min, max))
  1152. {
  1153. carla_stderr2("WARNING - Broken plugin parameter '%s': max == min", paramInfo->name);
  1154. max = min + 0.1f;
  1155. }
  1156. // default value
  1157. def = paramInfo->ranges.def;
  1158. if (def < min)
  1159. def = min;
  1160. else if (def > max)
  1161. def = max;
  1162. if (paramInfo->hints & NATIVE_PARAMETER_USES_SAMPLE_RATE)
  1163. {
  1164. min *= sampleRate;
  1165. max *= sampleRate;
  1166. def *= sampleRate;
  1167. pData->param.data[j].hints |= PARAMETER_USES_SAMPLERATE;
  1168. }
  1169. if (paramInfo->hints & NATIVE_PARAMETER_IS_BOOLEAN)
  1170. {
  1171. step = max - min;
  1172. stepSmall = step;
  1173. stepLarge = step;
  1174. pData->param.data[j].hints |= PARAMETER_IS_BOOLEAN;
  1175. }
  1176. else if (paramInfo->hints & NATIVE_PARAMETER_IS_INTEGER)
  1177. {
  1178. step = 1.0f;
  1179. stepSmall = 1.0f;
  1180. stepLarge = 10.0f;
  1181. pData->param.data[j].hints |= PARAMETER_IS_INTEGER;
  1182. }
  1183. else
  1184. {
  1185. float range = max - min;
  1186. step = range/100.0f;
  1187. stepSmall = range/1000.0f;
  1188. stepLarge = range/10.0f;
  1189. }
  1190. if (paramInfo->hints & NATIVE_PARAMETER_IS_OUTPUT)
  1191. {
  1192. pData->param.data[j].type = PARAMETER_OUTPUT;
  1193. if (needsCtrlOut != nullptr)
  1194. *needsCtrlOut = true;
  1195. }
  1196. else
  1197. {
  1198. pData->param.data[j].type = PARAMETER_INPUT;
  1199. if (needsCtrlIn != nullptr)
  1200. *needsCtrlIn = true;
  1201. }
  1202. // extra parameter hints
  1203. if (paramInfo->hints & NATIVE_PARAMETER_IS_ENABLED)
  1204. {
  1205. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  1206. if (paramInfo->hints & NATIVE_PARAMETER_IS_AUTOMABLE)
  1207. {
  1208. pData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  1209. pData->param.data[j].hints |= PARAMETER_CAN_BE_CV_CONTROLLED;
  1210. }
  1211. }
  1212. if (paramInfo->hints & NATIVE_PARAMETER_IS_LOGARITHMIC)
  1213. pData->param.data[j].hints |= PARAMETER_IS_LOGARITHMIC;
  1214. if (paramInfo->hints & NATIVE_PARAMETER_USES_SCALEPOINTS)
  1215. pData->param.data[j].hints |= PARAMETER_USES_SCALEPOINTS;
  1216. pData->param.ranges[j].min = min;
  1217. pData->param.ranges[j].max = max;
  1218. pData->param.ranges[j].def = def;
  1219. pData->param.ranges[j].step = step;
  1220. pData->param.ranges[j].stepSmall = stepSmall;
  1221. pData->param.ranges[j].stepLarge = stepLarge;
  1222. }
  1223. carla_debug("CarlaPluginNative::reloadParameters() - end");
  1224. }
  1225. void reloadPrograms(const bool doInit) override
  1226. {
  1227. carla_debug("CarlaPluginNative::reloadPrograms(%s)", bool2str(doInit));
  1228. uint32_t i, oldCount = pData->midiprog.count;
  1229. const int32_t current = pData->midiprog.current;
  1230. // Delete old programs
  1231. pData->midiprog.clear();
  1232. // Query new programs
  1233. uint32_t count = 0;
  1234. if (fDescriptor->get_midi_program_count != nullptr && fDescriptor->get_midi_program_info != nullptr && fDescriptor->set_midi_program != nullptr)
  1235. count = fDescriptor->get_midi_program_count(fHandle);
  1236. if (count > 0)
  1237. {
  1238. pData->midiprog.createNew(count);
  1239. // Update data
  1240. for (i=0; i < count; ++i)
  1241. {
  1242. const NativeMidiProgram* const mpDesc(fDescriptor->get_midi_program_info(fHandle, i));
  1243. CARLA_SAFE_ASSERT_CONTINUE(mpDesc != nullptr);
  1244. pData->midiprog.data[i].bank = mpDesc->bank;
  1245. pData->midiprog.data[i].program = mpDesc->program;
  1246. pData->midiprog.data[i].name = carla_strdup(mpDesc->name);
  1247. }
  1248. }
  1249. if (doInit)
  1250. {
  1251. if (count > 0)
  1252. setMidiProgram(0, false, false, false, true);
  1253. }
  1254. else
  1255. {
  1256. // Check if current program is invalid
  1257. bool programChanged = false;
  1258. if (count == oldCount+1)
  1259. {
  1260. // one midi program added, probably created by user
  1261. pData->midiprog.current = static_cast<int32_t>(oldCount);
  1262. programChanged = true;
  1263. }
  1264. else if (current < 0 && count > 0)
  1265. {
  1266. // programs exist now, but not before
  1267. pData->midiprog.current = 0;
  1268. programChanged = true;
  1269. }
  1270. else if (current >= 0 && count == 0)
  1271. {
  1272. // programs existed before, but not anymore
  1273. pData->midiprog.current = -1;
  1274. programChanged = true;
  1275. }
  1276. else if (current >= static_cast<int32_t>(count))
  1277. {
  1278. // current midi program > count
  1279. pData->midiprog.current = 0;
  1280. programChanged = true;
  1281. }
  1282. else
  1283. {
  1284. // no change
  1285. pData->midiprog.current = current;
  1286. }
  1287. if (programChanged)
  1288. setMidiProgram(pData->midiprog.current, true, true, true, false);
  1289. pData->engine->callback(true, true,
  1290. ENGINE_CALLBACK_RELOAD_PROGRAMS,
  1291. pData->id,
  1292. 0, 0, 0, 0.0f, nullptr);
  1293. }
  1294. }
  1295. // -------------------------------------------------------------------
  1296. // Plugin processing
  1297. void activate() noexcept override
  1298. {
  1299. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1300. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1301. if (fDescriptor->activate != nullptr)
  1302. {
  1303. try {
  1304. fDescriptor->activate(fHandle);
  1305. } catch(...) {}
  1306. if (fHandle2 != nullptr)
  1307. {
  1308. try {
  1309. fDescriptor->activate(fHandle2);
  1310. } catch(...) {}
  1311. }
  1312. }
  1313. }
  1314. void deactivate() noexcept override
  1315. {
  1316. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1317. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1318. if (fDescriptor->deactivate != nullptr)
  1319. {
  1320. try {
  1321. fDescriptor->deactivate(fHandle);
  1322. } catch(...) {}
  1323. if (fHandle2 != nullptr)
  1324. {
  1325. try {
  1326. fDescriptor->deactivate(fHandle2);
  1327. } catch(...) {}
  1328. }
  1329. }
  1330. }
  1331. const EngineEvent& findNextEvent()
  1332. {
  1333. if (fMidiIn.count == 1)
  1334. {
  1335. NativePluginMidiInData::MultiPortData& multiportData(fMidiIn.multiportData[0]);
  1336. if (multiportData.usedIndex == multiportData.cachedEventCount)
  1337. {
  1338. const uint32_t eventCount = pData->event.portIn->getEventCount();
  1339. CARLA_SAFE_ASSERT_INT2(eventCount == multiportData.cachedEventCount,
  1340. eventCount, multiportData.cachedEventCount);
  1341. return kNullEngineEvent;
  1342. }
  1343. return pData->event.portIn->getEvent(multiportData.usedIndex++);
  1344. }
  1345. uint32_t lowestSampleTime = 9999999;
  1346. uint32_t portMatching = 0;
  1347. bool found = false;
  1348. // process events in order for multiple ports
  1349. for (uint32_t m=0; m < fMidiIn.count; ++m)
  1350. {
  1351. CarlaEngineEventPort* const eventPort(fMidiIn.ports[m]);
  1352. NativePluginMidiInData::MultiPortData& multiportData(fMidiIn.multiportData[m]);
  1353. if (multiportData.usedIndex == multiportData.cachedEventCount)
  1354. continue;
  1355. const EngineEvent& event(eventPort->getEventUnchecked(multiportData.usedIndex));
  1356. if (event.time < lowestSampleTime)
  1357. {
  1358. lowestSampleTime = event.time;
  1359. portMatching = m;
  1360. found = true;
  1361. }
  1362. }
  1363. if (found)
  1364. {
  1365. CarlaEngineEventPort* const eventPort(fMidiIn.ports[portMatching]);
  1366. NativePluginMidiInData::MultiPortData& multiportData(fMidiIn.multiportData[portMatching]);
  1367. return eventPort->getEvent(multiportData.usedIndex++);
  1368. }
  1369. return kNullEngineEvent;
  1370. }
  1371. void process(const float* const* const audioIn, float** const audioOut,
  1372. const float* const* const cvIn, float** const cvOut, const uint32_t frames) override
  1373. {
  1374. // --------------------------------------------------------------------------------------------------------
  1375. // Check if active
  1376. if (! pData->active)
  1377. {
  1378. // disable any output sound
  1379. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1380. carla_zeroFloats(audioOut[i], frames);
  1381. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  1382. carla_zeroFloats(cvOut[i], frames);
  1383. return;
  1384. }
  1385. fMidiEventInCount = fMidiEventOutCount = 0;
  1386. carla_zeroStructs(fMidiInEvents, kPluginMaxMidiEvents);
  1387. carla_zeroStructs(fMidiOutEvents, kPluginMaxMidiEvents);
  1388. // --------------------------------------------------------------------------------------------------------
  1389. // Check if needs reset
  1390. if (pData->needsReset)
  1391. {
  1392. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1393. {
  1394. fMidiEventInCount = MAX_MIDI_CHANNELS*2;
  1395. for (uint8_t k=0, i=MAX_MIDI_CHANNELS; k < MAX_MIDI_CHANNELS; ++k)
  1396. {
  1397. fMidiInEvents[k].data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (k & MIDI_CHANNEL_BIT));
  1398. fMidiInEvents[k].data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  1399. fMidiInEvents[k].data[2] = 0;
  1400. fMidiInEvents[k].size = 3;
  1401. fMidiInEvents[k+i].data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (k & MIDI_CHANNEL_BIT));
  1402. fMidiInEvents[k+i].data[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  1403. fMidiInEvents[k+i].data[2] = 0;
  1404. fMidiInEvents[k+i].size = 3;
  1405. }
  1406. }
  1407. else if (pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  1408. {
  1409. fMidiEventInCount = MAX_MIDI_NOTE;
  1410. for (uint8_t k=0; k < MAX_MIDI_NOTE; ++k)
  1411. {
  1412. fMidiInEvents[k].data[0] = uint8_t(MIDI_STATUS_NOTE_OFF | (pData->ctrlChannel & MIDI_CHANNEL_BIT));
  1413. fMidiInEvents[k].data[1] = k;
  1414. fMidiInEvents[k].data[2] = 0;
  1415. fMidiInEvents[k].size = 3;
  1416. }
  1417. }
  1418. pData->needsReset = false;
  1419. }
  1420. // --------------------------------------------------------------------------------------------------------
  1421. // Set TimeInfo
  1422. const EngineTimeInfo timeInfo(pData->engine->getTimeInfo());
  1423. fTimeInfo.playing = timeInfo.playing;
  1424. fTimeInfo.frame = timeInfo.frame;
  1425. fTimeInfo.usecs = timeInfo.usecs;
  1426. if (timeInfo.bbt.valid)
  1427. {
  1428. fTimeInfo.bbt.valid = true;
  1429. fTimeInfo.bbt.bar = timeInfo.bbt.bar;
  1430. fTimeInfo.bbt.beat = timeInfo.bbt.beat;
  1431. fTimeInfo.bbt.tick = timeInfo.bbt.tick;
  1432. fTimeInfo.bbt.barStartTick = timeInfo.bbt.barStartTick;
  1433. fTimeInfo.bbt.beatsPerBar = timeInfo.bbt.beatsPerBar;
  1434. fTimeInfo.bbt.beatType = timeInfo.bbt.beatType;
  1435. fTimeInfo.bbt.ticksPerBeat = timeInfo.bbt.ticksPerBeat;
  1436. fTimeInfo.bbt.beatsPerMinute = timeInfo.bbt.beatsPerMinute;
  1437. }
  1438. else
  1439. {
  1440. fTimeInfo.bbt.valid = false;
  1441. }
  1442. #if 0
  1443. // This test code has proven to be quite useful
  1444. // So I am leaving it behind, I might need it again..
  1445. if (pData->id == 1)
  1446. {
  1447. static int64_t last_frame = timeInfo.frame;
  1448. static int64_t last_dev_frame = 0;
  1449. static double last_val = timeInfo.bbt.barStartTick + ((timeInfo.bbt.beat-1) * timeInfo.bbt.ticksPerBeat) + timeInfo.bbt.tick;
  1450. static double last_dev_val = 0.0;
  1451. int64_t cur_frame = timeInfo.frame;
  1452. int64_t cur_dev_frame = cur_frame - last_frame;
  1453. double cur_val = timeInfo.bbt.barStartTick + ((timeInfo.bbt.beat-1) * timeInfo.bbt.ticksPerBeat) + timeInfo.bbt.tick;
  1454. double cur_dev_val = cur_val - last_val;
  1455. if (std::abs(last_dev_val - cur_dev_val) >= 0.0001 || last_dev_frame != cur_dev_frame)
  1456. {
  1457. carla_stdout("currently %u at %u => %f : DEV1: %li : DEV2: %f",
  1458. frames,
  1459. timeInfo.frame,
  1460. cur_val,
  1461. cur_dev_frame,
  1462. cur_dev_val);
  1463. }
  1464. last_val = cur_val;
  1465. last_dev_val = cur_dev_val;
  1466. last_frame = cur_frame;
  1467. last_dev_frame = cur_dev_frame;
  1468. }
  1469. #endif
  1470. // --------------------------------------------------------------------------------------------------------
  1471. // Event Input and Processing
  1472. if (pData->event.portIn != nullptr)
  1473. {
  1474. // ----------------------------------------------------------------------------------------------------
  1475. // MIDI Input (External)
  1476. if (pData->extNotes.mutex.tryLock())
  1477. {
  1478. ExternalMidiNote note = { 0, 0, 0 };
  1479. for (; fMidiEventInCount < kPluginMaxMidiEvents && ! pData->extNotes.data.isEmpty();)
  1480. {
  1481. note = pData->extNotes.data.getFirst(note, true);
  1482. CARLA_SAFE_ASSERT_CONTINUE(note.channel >= 0 && note.channel < MAX_MIDI_CHANNELS);
  1483. NativeMidiEvent& nativeEvent(fMidiInEvents[fMidiEventInCount++]);
  1484. nativeEvent.data[0] = uint8_t((note.velo > 0 ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF) | (note.channel & MIDI_CHANNEL_BIT));
  1485. nativeEvent.data[1] = note.note;
  1486. nativeEvent.data[2] = note.velo;
  1487. nativeEvent.size = 3;
  1488. }
  1489. pData->extNotes.mutex.unlock();
  1490. } // End of MIDI Input (External)
  1491. // ----------------------------------------------------------------------------------------------------
  1492. // Event Input (System)
  1493. #ifndef BUILD_BRIDGE
  1494. bool allNotesOffSent = false;
  1495. #endif
  1496. const bool isSampleAccurate = (pData->options & PLUGIN_OPTION_FIXED_BUFFERS) == 0;
  1497. uint32_t startTime = 0;
  1498. uint32_t timeOffset = 0;
  1499. uint32_t nextBankId;
  1500. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  1501. nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
  1502. else
  1503. nextBankId = 0;
  1504. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1505. if (cvIn != nullptr && pData->event.cvSourcePorts != nullptr)
  1506. pData->event.cvSourcePorts->initPortBuffers(cvIn + pData->cvIn.count, frames, isSampleAccurate, pData->event.portIn);
  1507. #endif
  1508. for (;;)
  1509. {
  1510. const EngineEvent& event(findNextEvent());
  1511. if (event.type == kEngineEventTypeNull)
  1512. break;
  1513. uint32_t eventTime = event.time;
  1514. CARLA_SAFE_ASSERT_UINT2_CONTINUE(eventTime < frames, eventTime, frames);
  1515. if (eventTime < timeOffset)
  1516. {
  1517. carla_stderr2("Timing error, eventTime:%u < timeOffset:%u for '%s'",
  1518. eventTime, timeOffset, pData->name);
  1519. eventTime = timeOffset;
  1520. }
  1521. if (isSampleAccurate && eventTime > timeOffset)
  1522. {
  1523. if (processSingle(audioIn, audioOut, cvIn, cvOut, eventTime - timeOffset, timeOffset))
  1524. {
  1525. startTime = 0;
  1526. timeOffset = eventTime;
  1527. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  1528. nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
  1529. else
  1530. nextBankId = 0;
  1531. if (fMidiEventInCount > 0)
  1532. {
  1533. carla_zeroStructs(fMidiInEvents, fMidiEventInCount);
  1534. fMidiEventInCount = 0;
  1535. }
  1536. if (fMidiEventOutCount > 0)
  1537. {
  1538. carla_zeroStructs(fMidiOutEvents, fMidiEventOutCount);
  1539. fMidiEventOutCount = 0;
  1540. }
  1541. }
  1542. else
  1543. startTime += timeOffset;
  1544. }
  1545. // Control change
  1546. switch (event.type)
  1547. {
  1548. case kEngineEventTypeNull:
  1549. break;
  1550. case kEngineEventTypeControl: {
  1551. const EngineControlEvent& ctrlEvent = event.ctrl;
  1552. switch (ctrlEvent.type)
  1553. {
  1554. case kEngineControlEventTypeNull:
  1555. break;
  1556. case kEngineControlEventTypeParameter: {
  1557. float value;
  1558. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1559. // non-midi
  1560. if (event.channel == kEngineEventNonMidiChannel)
  1561. {
  1562. const uint32_t k = ctrlEvent.param;
  1563. CARLA_SAFE_ASSERT_CONTINUE(k < pData->param.count);
  1564. value = pData->param.getFinalUnnormalizedValue(k, ctrlEvent.value);
  1565. setParameterValueRT(k, value, true);
  1566. continue;
  1567. }
  1568. // Control backend stuff
  1569. if (event.channel == pData->ctrlChannel)
  1570. {
  1571. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_DRYWET) > 0)
  1572. {
  1573. value = ctrlEvent.value;
  1574. setDryWetRT(value, true);
  1575. }
  1576. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_VOLUME) > 0)
  1577. {
  1578. value = ctrlEvent.value*127.0f/100.0f;
  1579. setVolumeRT(value, true);
  1580. }
  1581. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_BALANCE) > 0)
  1582. {
  1583. float left, right;
  1584. value = ctrlEvent.value/0.5f - 1.0f;
  1585. if (value < 0.0f)
  1586. {
  1587. left = -1.0f;
  1588. right = (value*2.0f)+1.0f;
  1589. }
  1590. else if (value > 0.0f)
  1591. {
  1592. left = (value*2.0f)-1.0f;
  1593. right = 1.0f;
  1594. }
  1595. else
  1596. {
  1597. left = -1.0f;
  1598. right = 1.0f;
  1599. }
  1600. setBalanceLeftRT(left, true);
  1601. setBalanceRightRT(right, true);
  1602. }
  1603. }
  1604. #endif
  1605. // Control plugin parameters
  1606. for (uint32_t k=0; k < pData->param.count; ++k)
  1607. {
  1608. if (pData->param.data[k].midiChannel != event.channel)
  1609. continue;
  1610. if (pData->param.data[k].mappedControlIndex != ctrlEvent.param)
  1611. continue;
  1612. if (pData->param.data[k].type != PARAMETER_INPUT)
  1613. continue;
  1614. if ((pData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  1615. continue;
  1616. value = pData->param.getFinalUnnormalizedValue(k, ctrlEvent.value);
  1617. setParameterValueRT(k, value, true);
  1618. }
  1619. if ((pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0 && ctrlEvent.param < MAX_MIDI_VALUE)
  1620. {
  1621. if (fMidiEventInCount >= kPluginMaxMidiEvents)
  1622. continue;
  1623. NativeMidiEvent& nativeEvent(fMidiInEvents[fMidiEventInCount++]);
  1624. carla_zeroStruct(nativeEvent);
  1625. nativeEvent.time = isSampleAccurate ? startTime : eventTime;
  1626. nativeEvent.data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  1627. nativeEvent.data[1] = uint8_t(ctrlEvent.param);
  1628. nativeEvent.data[2] = uint8_t(ctrlEvent.value*127.0f);
  1629. nativeEvent.size = 3;
  1630. }
  1631. break;
  1632. } // case kEngineControlEventTypeParameter
  1633. case kEngineControlEventTypeMidiBank:
  1634. if (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES)
  1635. {
  1636. if (event.channel == pData->ctrlChannel)
  1637. nextBankId = ctrlEvent.param;
  1638. }
  1639. else if (pData->options & PLUGIN_OPTION_SEND_PROGRAM_CHANGES)
  1640. {
  1641. if (fMidiEventInCount >= kPluginMaxMidiEvents)
  1642. continue;
  1643. NativeMidiEvent& nativeEvent(fMidiInEvents[fMidiEventInCount++]);
  1644. carla_zeroStruct(nativeEvent);
  1645. nativeEvent.time = isSampleAccurate ? startTime : eventTime;
  1646. nativeEvent.data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  1647. nativeEvent.data[1] = MIDI_CONTROL_BANK_SELECT;
  1648. nativeEvent.data[2] = uint8_t(ctrlEvent.param);
  1649. nativeEvent.size = 3;
  1650. }
  1651. break;
  1652. case kEngineControlEventTypeMidiProgram:
  1653. if (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES)
  1654. {
  1655. if (event.channel < MAX_MIDI_CHANNELS)
  1656. {
  1657. const uint32_t nextProgramId(ctrlEvent.param);
  1658. for (uint32_t k=0; k < pData->midiprog.count; ++k)
  1659. {
  1660. if (pData->midiprog.data[k].bank == nextBankId && pData->midiprog.data[k].program == nextProgramId)
  1661. {
  1662. fDescriptor->set_midi_program(fHandle, event.channel, nextBankId, nextProgramId);
  1663. if (fHandle2 != nullptr)
  1664. fDescriptor->set_midi_program(fHandle2, event.channel, nextBankId, nextProgramId);
  1665. fCurMidiProgs[event.channel] = static_cast<int32_t>(k);
  1666. if (event.channel == pData->ctrlChannel)
  1667. {
  1668. pData->postponeRtEvent(kPluginPostRtEventMidiProgramChange,
  1669. true,
  1670. static_cast<int32_t>(k),
  1671. 0, 0, 0.0f);
  1672. }
  1673. break;
  1674. }
  1675. }
  1676. }
  1677. }
  1678. else if (pData->options & PLUGIN_OPTION_SEND_PROGRAM_CHANGES)
  1679. {
  1680. if (fMidiEventInCount >= kPluginMaxMidiEvents)
  1681. continue;
  1682. NativeMidiEvent& nativeEvent(fMidiInEvents[fMidiEventInCount++]);
  1683. carla_zeroStruct(nativeEvent);
  1684. nativeEvent.time = isSampleAccurate ? startTime : eventTime;
  1685. nativeEvent.data[0] = uint8_t(MIDI_STATUS_PROGRAM_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  1686. nativeEvent.data[1] = uint8_t(ctrlEvent.param);
  1687. nativeEvent.size = 2;
  1688. }
  1689. break;
  1690. case kEngineControlEventTypeAllSoundOff:
  1691. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1692. {
  1693. if (fMidiEventInCount >= kPluginMaxMidiEvents)
  1694. continue;
  1695. NativeMidiEvent& nativeEvent(fMidiInEvents[fMidiEventInCount++]);
  1696. carla_zeroStruct(nativeEvent);
  1697. nativeEvent.time = isSampleAccurate ? startTime : eventTime;
  1698. nativeEvent.data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  1699. nativeEvent.data[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  1700. nativeEvent.data[2] = 0;
  1701. nativeEvent.size = 3;
  1702. }
  1703. break;
  1704. case kEngineControlEventTypeAllNotesOff:
  1705. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1706. {
  1707. #ifndef BUILD_BRIDGE
  1708. if (event.channel == pData->ctrlChannel && ! allNotesOffSent)
  1709. {
  1710. allNotesOffSent = true;
  1711. postponeRtAllNotesOff();
  1712. }
  1713. #endif
  1714. if (fMidiEventInCount >= kPluginMaxMidiEvents)
  1715. continue;
  1716. NativeMidiEvent& nativeEvent(fMidiInEvents[fMidiEventInCount++]);
  1717. carla_zeroStruct(nativeEvent);
  1718. nativeEvent.time = isSampleAccurate ? startTime : eventTime;
  1719. nativeEvent.data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  1720. nativeEvent.data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  1721. nativeEvent.data[2] = 0;
  1722. nativeEvent.size = 3;
  1723. }
  1724. break;
  1725. }
  1726. break;
  1727. }
  1728. case kEngineEventTypeMidi: {
  1729. if (fMidiEventInCount >= kPluginMaxMidiEvents)
  1730. continue;
  1731. const EngineMidiEvent& midiEvent(event.midi);
  1732. if (midiEvent.size > 4)
  1733. continue;
  1734. #ifdef CARLA_PROPER_CPP11_SUPPORT
  1735. static_assert(4 <= EngineMidiEvent::kDataSize, "Incorrect data");
  1736. #endif
  1737. uint8_t status = uint8_t(MIDI_GET_STATUS_FROM_DATA(midiEvent.data));
  1738. if (status == MIDI_STATUS_CHANNEL_PRESSURE && (pData->options & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) == 0)
  1739. continue;
  1740. if (status == MIDI_STATUS_CONTROL_CHANGE && (pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) == 0)
  1741. continue;
  1742. if (status == MIDI_STATUS_POLYPHONIC_AFTERTOUCH && (pData->options & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) == 0)
  1743. continue;
  1744. if (status == MIDI_STATUS_PITCH_WHEEL_CONTROL && (pData->options & PLUGIN_OPTION_SEND_PITCHBEND) == 0)
  1745. continue;
  1746. // Fix bad note-off
  1747. if (status == MIDI_STATUS_NOTE_ON && midiEvent.data[2] == 0)
  1748. status = MIDI_STATUS_NOTE_OFF;
  1749. NativeMidiEvent& nativeEvent(fMidiInEvents[fMidiEventInCount++]);
  1750. carla_zeroStruct(nativeEvent);
  1751. nativeEvent.port = midiEvent.port;
  1752. nativeEvent.time = isSampleAccurate ? startTime : eventTime;
  1753. nativeEvent.size = midiEvent.size;
  1754. nativeEvent.data[0] = uint8_t(status | (event.channel & MIDI_CHANNEL_BIT));
  1755. nativeEvent.data[1] = midiEvent.size >= 2 ? midiEvent.data[1] : 0;
  1756. nativeEvent.data[2] = midiEvent.size >= 3 ? midiEvent.data[2] : 0;
  1757. nativeEvent.data[3] = midiEvent.size == 4 ? midiEvent.data[3] : 0;
  1758. if (status == MIDI_STATUS_NOTE_ON)
  1759. {
  1760. pData->postponeRtEvent(kPluginPostRtEventNoteOn,
  1761. true,
  1762. event.channel,
  1763. midiEvent.data[1],
  1764. midiEvent.data[2],
  1765. 0.0f);
  1766. }
  1767. else if (status == MIDI_STATUS_NOTE_OFF)
  1768. {
  1769. pData->postponeRtEvent(kPluginPostRtEventNoteOff,
  1770. true,
  1771. event.channel,
  1772. midiEvent.data[1],
  1773. 0, 0.0f);
  1774. }
  1775. } break;
  1776. } // switch (event.type)
  1777. }
  1778. pData->postRtEvents.trySplice();
  1779. if (frames > timeOffset)
  1780. processSingle(audioIn, audioOut, cvIn, cvOut, frames - timeOffset, timeOffset);
  1781. } // End of Event Input and Processing
  1782. // --------------------------------------------------------------------------------------------------------
  1783. // Plugin processing (no events)
  1784. else
  1785. {
  1786. processSingle(audioIn, audioOut, cvIn, cvOut, frames, 0);
  1787. } // End of Plugin processing (no events)
  1788. #ifndef BUILD_BRIDGE
  1789. // --------------------------------------------------------------------------------------------------------
  1790. // Control Output
  1791. if (pData->event.portOut != nullptr)
  1792. {
  1793. float value, curValue;
  1794. for (uint32_t k=0; k < pData->param.count; ++k)
  1795. {
  1796. if (pData->param.data[k].type != PARAMETER_OUTPUT)
  1797. continue;
  1798. curValue = fDescriptor->get_parameter_value(fHandle, k);
  1799. pData->param.ranges[k].fixValue(curValue);
  1800. if (pData->param.data[k].mappedControlIndex > 0)
  1801. {
  1802. value = pData->param.ranges[k].getNormalizedValue(curValue);
  1803. pData->event.portOut->writeControlEvent(0,
  1804. pData->param.data[k].midiChannel,
  1805. kEngineControlEventTypeParameter,
  1806. static_cast<uint16_t>(pData->param.data[k].mappedControlIndex),
  1807. value);
  1808. }
  1809. }
  1810. } // End of Control Output
  1811. #endif
  1812. }
  1813. bool processSingle(const float* const* const audioIn, float** const audioOut,
  1814. const float* const* const cvIn, float** const cvOut,
  1815. const uint32_t frames, const uint32_t timeOffset)
  1816. {
  1817. CARLA_SAFE_ASSERT_RETURN(frames > 0, false);
  1818. if (pData->audioIn.count > 0) {
  1819. CARLA_SAFE_ASSERT_RETURN(audioIn != nullptr, false);
  1820. }
  1821. if (pData->audioOut.count > 0) {
  1822. CARLA_SAFE_ASSERT_RETURN(audioOut != nullptr, false);
  1823. }
  1824. if (pData->cvIn.count > 0) {
  1825. CARLA_SAFE_ASSERT_RETURN(cvIn != nullptr, false);
  1826. }
  1827. if (pData->cvOut.count > 0) {
  1828. CARLA_SAFE_ASSERT_RETURN(cvOut != nullptr, false);
  1829. }
  1830. // --------------------------------------------------------------------------------------------------------
  1831. // Try lock, silence otherwise
  1832. if (fIsOffline)
  1833. {
  1834. pData->singleMutex.lock();
  1835. }
  1836. else if (! pData->singleMutex.tryLock())
  1837. {
  1838. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1839. {
  1840. for (uint32_t k=0; k < frames; ++k)
  1841. audioOut[i][k+timeOffset] = 0.0f;
  1842. }
  1843. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  1844. {
  1845. for (uint32_t k=0; k < frames; ++k)
  1846. cvOut[i][k+timeOffset] = 0.0f;
  1847. }
  1848. return false;
  1849. }
  1850. // --------------------------------------------------------------------------------------------------------
  1851. // Set audio buffers
  1852. {
  1853. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1854. carla_copyFloats(fAudioAndCvInBuffers[i], audioIn[i]+timeOffset, frames);
  1855. for (uint32_t i=0; i < pData->cvIn.count; ++i)
  1856. carla_copyFloats(fAudioAndCvInBuffers[pData->audioIn.count+i], cvIn[i]+timeOffset, frames);
  1857. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1858. carla_zeroFloats(fAudioAndCvOutBuffers[i], frames);
  1859. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  1860. carla_zeroFloats(fAudioAndCvOutBuffers[pData->audioOut.count+i], frames);
  1861. }
  1862. // --------------------------------------------------------------------------------------------------------
  1863. // Run plugin
  1864. fIsProcessing = true;
  1865. if (fHandle2 == nullptr)
  1866. {
  1867. fDescriptor->process(fHandle,
  1868. fAudioAndCvInBuffers, fAudioAndCvOutBuffers, frames,
  1869. fMidiInEvents, fMidiEventInCount);
  1870. }
  1871. else
  1872. {
  1873. fDescriptor->process(fHandle,
  1874. (fAudioAndCvInBuffers != nullptr) ? &fAudioAndCvInBuffers[0] : nullptr,
  1875. (fAudioAndCvOutBuffers != nullptr) ? &fAudioAndCvOutBuffers[0] : nullptr,
  1876. frames, fMidiInEvents, fMidiEventInCount);
  1877. fDescriptor->process(fHandle2,
  1878. (fAudioAndCvInBuffers != nullptr) ? &fAudioAndCvInBuffers[1] : nullptr,
  1879. (fAudioAndCvOutBuffers != nullptr) ? &fAudioAndCvOutBuffers[1] : nullptr,
  1880. frames, fMidiInEvents, fMidiEventInCount);
  1881. }
  1882. fIsProcessing = false;
  1883. if (fTimeInfo.playing)
  1884. fTimeInfo.frame += frames;
  1885. uint32_t i=0;
  1886. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1887. // --------------------------------------------------------------------------------------------------------
  1888. // Post-processing (dry/wet, volume and balance)
  1889. {
  1890. const bool doDryWet = (pData->hints & PLUGIN_CAN_DRYWET) != 0 && carla_isNotEqual(pData->postProc.dryWet, 1.0f);
  1891. const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) != 0 && ! (carla_isEqual(pData->postProc.balanceLeft, -1.0f) && carla_isEqual(pData->postProc.balanceRight, 1.0f));
  1892. bool isPair;
  1893. float bufValue, oldBufLeft[doBalance ? frames : 1];
  1894. for (; i < pData->audioOut.count; ++i)
  1895. {
  1896. // Dry/Wet
  1897. if (doDryWet)
  1898. {
  1899. for (uint32_t k=0; k < frames; ++k)
  1900. {
  1901. bufValue = fAudioAndCvInBuffers[(pData->audioIn.count == 1) ? 0 : i][k];
  1902. fAudioAndCvOutBuffers[i][k] = (fAudioAndCvOutBuffers[i][k] * pData->postProc.dryWet) + (bufValue * (1.0f - pData->postProc.dryWet));
  1903. }
  1904. }
  1905. // Balance
  1906. if (doBalance)
  1907. {
  1908. isPair = (i % 2 == 0);
  1909. if (isPair)
  1910. {
  1911. CARLA_ASSERT(i+1 < pData->audioOut.count);
  1912. carla_copyFloats(oldBufLeft, fAudioAndCvOutBuffers[i], frames);
  1913. }
  1914. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  1915. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  1916. for (uint32_t k=0; k < frames; ++k)
  1917. {
  1918. if (isPair)
  1919. {
  1920. // left
  1921. fAudioAndCvOutBuffers[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  1922. fAudioAndCvOutBuffers[i][k] += fAudioAndCvOutBuffers[i+1][k] * (1.0f - balRangeR);
  1923. }
  1924. else
  1925. {
  1926. // right
  1927. fAudioAndCvOutBuffers[i][k] = fAudioAndCvOutBuffers[i][k] * balRangeR;
  1928. fAudioAndCvOutBuffers[i][k] += oldBufLeft[k] * balRangeL;
  1929. }
  1930. }
  1931. }
  1932. // Volume (and buffer copy)
  1933. {
  1934. for (uint32_t k=0; k < frames; ++k)
  1935. audioOut[i][k+timeOffset] = fAudioAndCvOutBuffers[i][k] * pData->postProc.volume;
  1936. }
  1937. }
  1938. } // End of Post-processing
  1939. #else
  1940. for (; i < pData->audioOut.count; ++i)
  1941. {
  1942. for (uint32_t k=0; k < frames; ++k)
  1943. audioOut[i][k+timeOffset] = fAudioAndCvOutBuffers[i][k];
  1944. }
  1945. #endif
  1946. // CV stuff too
  1947. for (; i < pData->cvOut.count; ++i)
  1948. {
  1949. for (uint32_t k=0; k < frames; ++k)
  1950. cvOut[i][k+timeOffset] = fAudioAndCvOutBuffers[pData->audioOut.count+i][k];
  1951. }
  1952. // --------------------------------------------------------------------------------------------------------
  1953. // MIDI Output
  1954. if (pData->event.portOut != nullptr)
  1955. {
  1956. for (uint32_t k = 0; k < fMidiEventOutCount; ++k)
  1957. {
  1958. const uint8_t channel = uint8_t(MIDI_GET_CHANNEL_FROM_DATA(fMidiOutEvents[k].data));
  1959. const uint8_t port = fMidiOutEvents[k].port;
  1960. if (fMidiOut.count > 1 && port < fMidiOut.count)
  1961. fMidiOut.ports[port]->writeMidiEvent(fMidiOutEvents[k].time+timeOffset, channel, fMidiOutEvents[k].size, fMidiOutEvents[k].data);
  1962. else
  1963. pData->event.portOut->writeMidiEvent(fMidiOutEvents[k].time+timeOffset, channel, fMidiOutEvents[k].size, fMidiOutEvents[k].data);
  1964. }
  1965. }
  1966. // --------------------------------------------------------------------------------------------------------
  1967. pData->singleMutex.unlock();
  1968. return true;
  1969. }
  1970. void bufferSizeChanged(const uint32_t newBufferSize) override
  1971. {
  1972. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  1973. carla_debug("CarlaPluginNative::bufferSizeChanged(%i)", newBufferSize);
  1974. for (uint32_t i=0; i < (pData->audioIn.count+pData->cvIn.count); ++i)
  1975. {
  1976. if (fAudioAndCvInBuffers[i] != nullptr)
  1977. delete[] fAudioAndCvInBuffers[i];
  1978. fAudioAndCvInBuffers[i] = new float[newBufferSize];
  1979. }
  1980. for (uint32_t i=0; i < (pData->audioOut.count+pData->cvOut.count); ++i)
  1981. {
  1982. if (fAudioAndCvOutBuffers[i] != nullptr)
  1983. delete[] fAudioAndCvOutBuffers[i];
  1984. fAudioAndCvOutBuffers[i] = new float[newBufferSize];
  1985. }
  1986. if (fCurBufferSize == newBufferSize)
  1987. return;
  1988. fCurBufferSize = newBufferSize;
  1989. if (fDescriptor != nullptr && fDescriptor->dispatcher != nullptr)
  1990. {
  1991. fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_BUFFER_SIZE_CHANGED, 0, static_cast<intptr_t>(newBufferSize), nullptr, 0.0f);
  1992. if (fHandle2 != nullptr)
  1993. fDescriptor->dispatcher(fHandle2, NATIVE_PLUGIN_OPCODE_BUFFER_SIZE_CHANGED, 0, static_cast<intptr_t>(newBufferSize), nullptr, 0.0f);
  1994. }
  1995. }
  1996. void sampleRateChanged(const double newSampleRate) override
  1997. {
  1998. CARLA_ASSERT_INT(newSampleRate > 0.0, newSampleRate);
  1999. carla_debug("CarlaPluginNative::sampleRateChanged(%g)", newSampleRate);
  2000. if (carla_isEqual(fCurSampleRate, newSampleRate))
  2001. return;
  2002. fCurSampleRate = newSampleRate;
  2003. if (fDescriptor != nullptr && fDescriptor->dispatcher != nullptr)
  2004. {
  2005. fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_SAMPLE_RATE_CHANGED, 0, 0, nullptr, float(newSampleRate));
  2006. if (fHandle2 != nullptr)
  2007. fDescriptor->dispatcher(fHandle2, NATIVE_PLUGIN_OPCODE_SAMPLE_RATE_CHANGED, 0, 0, nullptr, float(newSampleRate));
  2008. }
  2009. }
  2010. void offlineModeChanged(const bool isOffline) override
  2011. {
  2012. if (fIsOffline == isOffline)
  2013. return;
  2014. fIsOffline = isOffline;
  2015. if (fDescriptor != nullptr && fDescriptor->dispatcher != nullptr)
  2016. {
  2017. fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_OFFLINE_CHANGED, 0, isOffline ? 1 : 0, nullptr, 0.0f);
  2018. if (fHandle2 != nullptr)
  2019. fDescriptor->dispatcher(fHandle2, NATIVE_PLUGIN_OPCODE_OFFLINE_CHANGED, 0, isOffline ? 1 : 0, nullptr, 0.0f);
  2020. }
  2021. }
  2022. // -------------------------------------------------------------------
  2023. // Plugin buffers
  2024. void initBuffers() const noexcept override
  2025. {
  2026. CarlaPlugin::initBuffers();
  2027. fMidiIn.initBuffers(pData->event.portIn);
  2028. fMidiOut.initBuffers();
  2029. }
  2030. void clearBuffers() noexcept override
  2031. {
  2032. carla_debug("CarlaPluginNative::clearBuffers() - start");
  2033. if (fAudioAndCvInBuffers != nullptr)
  2034. {
  2035. for (uint32_t i=0; i < (pData->audioIn.count+pData->cvIn.count); ++i)
  2036. {
  2037. if (fAudioAndCvInBuffers[i] != nullptr)
  2038. {
  2039. delete[] fAudioAndCvInBuffers[i];
  2040. fAudioAndCvInBuffers[i] = nullptr;
  2041. }
  2042. }
  2043. delete[] fAudioAndCvInBuffers;
  2044. fAudioAndCvInBuffers = nullptr;
  2045. }
  2046. if (fAudioAndCvOutBuffers != nullptr)
  2047. {
  2048. for (uint32_t i=0; i < (pData->audioOut.count+pData->cvOut.count); ++i)
  2049. {
  2050. if (fAudioAndCvOutBuffers[i] != nullptr)
  2051. {
  2052. delete[] fAudioAndCvOutBuffers[i];
  2053. fAudioAndCvOutBuffers[i] = nullptr;
  2054. }
  2055. }
  2056. delete[] fAudioAndCvOutBuffers;
  2057. fAudioAndCvOutBuffers = nullptr;
  2058. }
  2059. if (fMidiIn.count > 1)
  2060. pData->event.portIn = nullptr;
  2061. if (fMidiOut.count > 1)
  2062. pData->event.portOut = nullptr;
  2063. fMidiIn.clear();
  2064. fMidiOut.clear();
  2065. CarlaPlugin::clearBuffers();
  2066. carla_debug("CarlaPluginNative::clearBuffers() - end");
  2067. }
  2068. // -------------------------------------------------------------------
  2069. // Post-poned UI Stuff
  2070. void uiParameterChange(const uint32_t index, const float value) noexcept override
  2071. {
  2072. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  2073. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  2074. CARLA_SAFE_ASSERT_RETURN(index < pData->param.count,);
  2075. if (! fIsUiVisible)
  2076. return;
  2077. if (fDescriptor->ui_set_parameter_value != nullptr)
  2078. fDescriptor->ui_set_parameter_value(fHandle, index, value);
  2079. }
  2080. void uiMidiProgramChange(const uint32_t index) noexcept override
  2081. {
  2082. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  2083. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  2084. CARLA_SAFE_ASSERT_RETURN(index < pData->midiprog.count,);
  2085. if (! fIsUiVisible)
  2086. return;
  2087. if (index >= pData->midiprog.count)
  2088. return;
  2089. if (fDescriptor->ui_set_midi_program != nullptr)
  2090. fDescriptor->ui_set_midi_program(fHandle, 0, pData->midiprog.data[index].bank, pData->midiprog.data[index].program);
  2091. }
  2092. void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) noexcept override
  2093. {
  2094. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  2095. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  2096. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  2097. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  2098. CARLA_SAFE_ASSERT_RETURN(velo > 0 && velo < MAX_MIDI_VALUE,);
  2099. if (! fIsUiVisible)
  2100. return;
  2101. // TODO
  2102. }
  2103. void uiNoteOff(const uint8_t channel, const uint8_t note) noexcept override
  2104. {
  2105. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  2106. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  2107. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  2108. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  2109. if (! fIsUiVisible)
  2110. return;
  2111. if (fDescriptor == nullptr || fHandle == nullptr)
  2112. return;
  2113. // TODO
  2114. }
  2115. // -------------------------------------------------------------------
  2116. const NativeInlineDisplayImageSurface* renderInlineDisplay(const uint32_t width, const uint32_t height)
  2117. {
  2118. CARLA_SAFE_ASSERT_RETURN(fDescriptor->hints & NATIVE_PLUGIN_HAS_INLINE_DISPLAY, nullptr);
  2119. CARLA_SAFE_ASSERT_RETURN(fDescriptor->render_inline_display, nullptr);
  2120. CARLA_SAFE_ASSERT_RETURN(width > 0, nullptr);
  2121. CARLA_SAFE_ASSERT_RETURN(height > 0, nullptr);
  2122. return fDescriptor->render_inline_display(fHandle, width, height);
  2123. }
  2124. // -------------------------------------------------------------------
  2125. protected:
  2126. const NativeTimeInfo* handleGetTimeInfo() const noexcept
  2127. {
  2128. CARLA_SAFE_ASSERT_RETURN(fIsProcessing, nullptr);
  2129. return &fTimeInfo;
  2130. }
  2131. bool handleWriteMidiEvent(const NativeMidiEvent* const event)
  2132. {
  2133. CARLA_SAFE_ASSERT_RETURN(pData->enabled, false);
  2134. CARLA_SAFE_ASSERT_RETURN(fIsProcessing, false);
  2135. CARLA_SAFE_ASSERT_RETURN(fMidiOut.count > 0 || pData->event.portOut != nullptr, false);
  2136. CARLA_SAFE_ASSERT_RETURN(event != nullptr, false);
  2137. CARLA_SAFE_ASSERT_RETURN(event->data[0] != 0, false);
  2138. if (fMidiEventOutCount == kPluginMaxMidiEvents)
  2139. {
  2140. carla_stdout("CarlaPluginNative::handleWriteMidiEvent(%p) - buffer full", event);
  2141. return false;
  2142. }
  2143. std::memcpy(&fMidiOutEvents[fMidiEventOutCount++], event, sizeof(NativeMidiEvent));
  2144. return true;
  2145. }
  2146. void handleUiParameterChanged(const uint32_t index, const float value)
  2147. {
  2148. setParameterValue(index, value, false, true, true);
  2149. }
  2150. void handleUiCustomDataChanged(const char* const key, const char* const value)
  2151. {
  2152. setCustomData(CUSTOM_DATA_TYPE_STRING, key, value, false);
  2153. }
  2154. void handleUiClosed()
  2155. {
  2156. pData->engine->callback(true, true, ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, 0, 0, 0, 0.0f, nullptr);
  2157. fIsUiVisible = false;
  2158. }
  2159. const char* handleUiOpenFile(const bool isDir, const char* const title, const char* const filter)
  2160. {
  2161. return pData->engine->runFileCallback(FILE_CALLBACK_OPEN, isDir, title, filter);
  2162. }
  2163. const char* handleUiSaveFile(const bool isDir, const char* const title, const char* const filter)
  2164. {
  2165. return pData->engine->runFileCallback(FILE_CALLBACK_SAVE, isDir, title, filter);
  2166. }
  2167. intptr_t handleDispatcher(const NativeHostDispatcherOpcode opcode,
  2168. const int32_t index, const intptr_t value, void* const ptr, const float opt)
  2169. {
  2170. carla_debug("CarlaPluginNative::handleDispatcher(%i, %i, " P_INTPTR ", %p, %f)",
  2171. opcode, index, value, ptr, static_cast<double>(opt));
  2172. intptr_t ret = 0;
  2173. switch (opcode)
  2174. {
  2175. case NATIVE_HOST_OPCODE_NULL:
  2176. break;
  2177. case NATIVE_HOST_OPCODE_UPDATE_PARAMETER:
  2178. // TODO
  2179. pData->engine->callback(true, true, ENGINE_CALLBACK_UPDATE, pData->id, -1, 0, 0, 0.0f, nullptr);
  2180. break;
  2181. case NATIVE_HOST_OPCODE_UPDATE_MIDI_PROGRAM:
  2182. // TODO
  2183. pData->engine->callback(true, true, ENGINE_CALLBACK_UPDATE, pData->id, -1, 0, 0, 0.0f, nullptr);
  2184. break;
  2185. case NATIVE_HOST_OPCODE_RELOAD_PARAMETERS:
  2186. reloadParameters(nullptr, nullptr);
  2187. pData->engine->callback(true, true, ENGINE_CALLBACK_RELOAD_ALL, pData->id, -1, 0, 0, 0.0f, nullptr);
  2188. break;
  2189. case NATIVE_HOST_OPCODE_RELOAD_MIDI_PROGRAMS:
  2190. reloadPrograms(false);
  2191. pData->engine->callback(true, true, ENGINE_CALLBACK_RELOAD_PROGRAMS, pData->id, -1, 0, 0, 0.0f, nullptr);
  2192. break;
  2193. case NATIVE_HOST_OPCODE_RELOAD_ALL:
  2194. reload();
  2195. pData->engine->callback(true, true, ENGINE_CALLBACK_RELOAD_ALL, pData->id, -1, 0, 0, 0.0f, nullptr);
  2196. break;
  2197. case NATIVE_HOST_OPCODE_UI_UNAVAILABLE:
  2198. pData->engine->callback(true, true, ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, -1, 0, 0, 0.0f, nullptr);
  2199. fIsUiAvailable = false;
  2200. break;
  2201. case NATIVE_HOST_OPCODE_HOST_IDLE:
  2202. pData->engine->callback(true, false, ENGINE_CALLBACK_IDLE, 0, 0, 0, 0, 0.0f, nullptr);
  2203. break;
  2204. case NATIVE_HOST_OPCODE_INTERNAL_PLUGIN:
  2205. ret = 1;
  2206. break;
  2207. case NATIVE_HOST_OPCODE_QUEUE_INLINE_DISPLAY:
  2208. fInlineDisplayNeedsRedraw = true;
  2209. break;
  2210. case NATIVE_HOST_OPCODE_UI_TOUCH_PARAMETER:
  2211. CARLA_SAFE_ASSERT_RETURN(index >= 0, 0);
  2212. pData->engine->touchPluginParameter(pData->id, static_cast<uint32_t>(index), value != 0);
  2213. break;
  2214. case NATIVE_HOST_OPCODE_REQUEST_IDLE:
  2215. fNeedsIdle = true;
  2216. break;
  2217. case NATIVE_HOST_OPCODE_GET_FILE_PATH:
  2218. CARLA_SAFE_ASSERT_RETURN(ptr != nullptr, 0);
  2219. {
  2220. const EngineOptions& opts(pData->engine->getOptions());
  2221. const char* const filetype = (const char*)ptr;
  2222. if (std::strcmp(filetype, "audio") == 0)
  2223. return static_cast<intptr_t>((uintptr_t)opts.pathAudio);
  2224. if (std::strcmp(filetype, "midi") == 0)
  2225. return static_cast<intptr_t>((uintptr_t)opts.pathMIDI);
  2226. }
  2227. break;
  2228. case NATIVE_HOST_OPCODE_UI_RESIZE:
  2229. // unused here
  2230. break;
  2231. }
  2232. return ret;
  2233. // unused for now
  2234. (void)opt;
  2235. }
  2236. // -------------------------------------------------------------------
  2237. public:
  2238. void* getNativeHandle() const noexcept override
  2239. {
  2240. return fHandle;
  2241. }
  2242. const void* getNativeDescriptor() const noexcept override
  2243. {
  2244. return fDescriptor;
  2245. }
  2246. // -------------------------------------------------------------------
  2247. bool init(const char* const name, const char* const label, const uint options)
  2248. {
  2249. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  2250. // ---------------------------------------------------------------
  2251. // first checks
  2252. if (pData->client != nullptr)
  2253. {
  2254. pData->engine->setLastError("Plugin client is already registered");
  2255. return false;
  2256. }
  2257. if (label == nullptr || label[0] == '\0')
  2258. {
  2259. pData->engine->setLastError("null label");
  2260. return false;
  2261. }
  2262. // ---------------------------------------------------------------
  2263. // get descriptor that matches label
  2264. sPluginInitializer.initIfNeeded();
  2265. for (LinkedList<const NativePluginDescriptor*>::Itenerator it = gPluginDescriptors.begin2(); it.valid(); it.next())
  2266. {
  2267. fDescriptor = it.getValue(nullptr);
  2268. CARLA_SAFE_ASSERT_BREAK(fDescriptor != nullptr);
  2269. carla_debug("Check vs \"%s\"", fDescriptor->label);
  2270. if (fDescriptor->label != nullptr && std::strcmp(fDescriptor->label, label) == 0)
  2271. break;
  2272. fDescriptor = nullptr;
  2273. }
  2274. if (fDescriptor == nullptr)
  2275. {
  2276. pData->engine->setLastError("Invalid internal plugin");
  2277. return false;
  2278. }
  2279. // ---------------------------------------------------------------
  2280. // set icon
  2281. /**/ if (std::strcmp(fDescriptor->label, "audiofile") == 0)
  2282. pData->iconName = carla_strdup_safe("file");
  2283. else if (std::strcmp(fDescriptor->label, "midifile") == 0)
  2284. pData->iconName = carla_strdup_safe("file");
  2285. else if (std::strcmp(fDescriptor->label, "3bandeq") == 0)
  2286. pData->iconName = carla_strdup_safe("distrho");
  2287. else if (std::strcmp(fDescriptor->label, "3bandsplitter") == 0)
  2288. pData->iconName = carla_strdup_safe("distrho");
  2289. else if (std::strcmp(fDescriptor->label, "kars") == 0)
  2290. pData->iconName = carla_strdup_safe("distrho");
  2291. else if (std::strcmp(fDescriptor->label, "nekobi") == 0)
  2292. pData->iconName = carla_strdup_safe("distrho");
  2293. else if (std::strcmp(fDescriptor->label, "pingpongpan") == 0)
  2294. pData->iconName = carla_strdup_safe("distrho");
  2295. // ---------------------------------------------------------------
  2296. // set info
  2297. if (name != nullptr && name[0] != '\0')
  2298. pData->name = pData->engine->getUniquePluginName(name);
  2299. else if (fDescriptor->name != nullptr && fDescriptor->name[0] != '\0')
  2300. pData->name = pData->engine->getUniquePluginName(fDescriptor->name);
  2301. else
  2302. pData->name = pData->engine->getUniquePluginName(label);
  2303. {
  2304. CARLA_ASSERT(fHost.uiName == nullptr);
  2305. char uiName[std::strlen(pData->name)+6+1];
  2306. std::strcpy(uiName, pData->name);
  2307. std::strcat(uiName, " (GUI)");
  2308. fHost.uiName = carla_strdup(uiName);
  2309. }
  2310. // ---------------------------------------------------------------
  2311. // register client
  2312. pData->client = pData->engine->addClient(this);
  2313. if (pData->client == nullptr || ! pData->client->isOk())
  2314. {
  2315. pData->engine->setLastError("Failed to register plugin client");
  2316. return false;
  2317. }
  2318. // ---------------------------------------------------------------
  2319. // initialize plugin
  2320. fHandle = fDescriptor->instantiate(&fHost);
  2321. if (fHandle == nullptr)
  2322. {
  2323. pData->engine->setLastError("Plugin failed to initialize");
  2324. return false;
  2325. }
  2326. // ---------------------------------------------------------------
  2327. // set options
  2328. bool hasMidiProgs = false;
  2329. if (fDescriptor->get_midi_program_count != nullptr)
  2330. {
  2331. try {
  2332. hasMidiProgs = fDescriptor->get_midi_program_count(fHandle) > 0;
  2333. } catch (...) {}
  2334. }
  2335. pData->options = 0x0;
  2336. if (fDescriptor->hints & NATIVE_PLUGIN_NEEDS_FIXED_BUFFERS)
  2337. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  2338. else if (options & PLUGIN_OPTION_FIXED_BUFFERS)
  2339. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  2340. if (pData->engine->getOptions().forceStereo)
  2341. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  2342. else if (options & PLUGIN_OPTION_FORCE_STEREO)
  2343. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  2344. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_CONTROL_CHANGES)
  2345. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_CONTROL_CHANGES))
  2346. pData->options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  2347. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_CHANNEL_PRESSURE)
  2348. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_CHANNEL_PRESSURE))
  2349. pData->options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  2350. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_NOTE_AFTERTOUCH)
  2351. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH))
  2352. pData->options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  2353. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_PITCHBEND)
  2354. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_PITCHBEND))
  2355. pData->options |= PLUGIN_OPTION_SEND_PITCHBEND;
  2356. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_ALL_SOUND_OFF)
  2357. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_ALL_SOUND_OFF))
  2358. pData->options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  2359. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_PROGRAM_CHANGES)
  2360. {
  2361. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_PROGRAM_CHANGES))
  2362. pData->options |= PLUGIN_OPTION_SEND_PROGRAM_CHANGES;
  2363. // makes no sense for a plugin to set program changes supported, but it has no midi programs
  2364. CARLA_SAFE_ASSERT(! hasMidiProgs);
  2365. }
  2366. else if (hasMidiProgs)
  2367. {
  2368. if (isPluginOptionEnabled(options, PLUGIN_OPTION_MAP_PROGRAM_CHANGES))
  2369. pData->options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  2370. }
  2371. return true;
  2372. }
  2373. private:
  2374. NativePluginHandle fHandle;
  2375. NativePluginHandle fHandle2;
  2376. NativeHostDescriptor fHost;
  2377. const NativePluginDescriptor* fDescriptor;
  2378. bool fIsProcessing;
  2379. bool fIsOffline;
  2380. bool fIsUiAvailable;
  2381. bool fIsUiVisible;
  2382. volatile bool fNeedsIdle;
  2383. bool fInlineDisplayNeedsRedraw;
  2384. int64_t fInlineDisplayLastRedrawTime;
  2385. float** fAudioAndCvInBuffers;
  2386. float** fAudioAndCvOutBuffers;
  2387. uint32_t fMidiEventInCount;
  2388. uint32_t fMidiEventOutCount;
  2389. NativeMidiEvent fMidiInEvents[kPluginMaxMidiEvents];
  2390. NativeMidiEvent fMidiOutEvents[kPluginMaxMidiEvents];
  2391. int32_t fCurMidiProgs[MAX_MIDI_CHANNELS];
  2392. uint32_t fCurBufferSize;
  2393. double fCurSampleRate;
  2394. NativePluginMidiInData fMidiIn;
  2395. NativePluginMidiOutData fMidiOut;
  2396. NativeTimeInfo fTimeInfo;
  2397. // -------------------------------------------------------------------
  2398. #define handlePtr ((CarlaPluginNative*)handle)
  2399. static uint32_t carla_host_get_buffer_size(NativeHostHandle handle) noexcept
  2400. {
  2401. return handlePtr->fCurBufferSize;
  2402. }
  2403. static double carla_host_get_sample_rate(NativeHostHandle handle) noexcept
  2404. {
  2405. return handlePtr->fCurSampleRate;
  2406. }
  2407. static bool carla_host_is_offline(NativeHostHandle handle) noexcept
  2408. {
  2409. return handlePtr->fIsOffline;
  2410. }
  2411. static const NativeTimeInfo* carla_host_get_time_info(NativeHostHandle handle) noexcept
  2412. {
  2413. return handlePtr->handleGetTimeInfo();
  2414. }
  2415. static bool carla_host_write_midi_event(NativeHostHandle handle, const NativeMidiEvent* event)
  2416. {
  2417. return handlePtr->handleWriteMidiEvent(event);
  2418. }
  2419. static void carla_host_ui_parameter_changed(NativeHostHandle handle, uint32_t index, float value)
  2420. {
  2421. handlePtr->handleUiParameterChanged(index, value);
  2422. }
  2423. static void carla_host_ui_custom_data_changed(NativeHostHandle handle, const char* key, const char* value)
  2424. {
  2425. handlePtr->handleUiCustomDataChanged(key, value);
  2426. }
  2427. static void carla_host_ui_closed(NativeHostHandle handle)
  2428. {
  2429. handlePtr->handleUiClosed();
  2430. }
  2431. static const char* carla_host_ui_open_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
  2432. {
  2433. return handlePtr->handleUiOpenFile(isDir, title, filter);
  2434. }
  2435. static const char* carla_host_ui_save_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
  2436. {
  2437. return handlePtr->handleUiSaveFile(isDir, title, filter);
  2438. }
  2439. static intptr_t carla_host_dispatcher(NativeHostHandle handle, NativeHostDispatcherOpcode opcode, int32_t index, intptr_t value, void* ptr, float opt)
  2440. {
  2441. return handlePtr->handleDispatcher(opcode, index, value, ptr, opt);
  2442. }
  2443. #undef handlePtr
  2444. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaPluginNative)
  2445. };
  2446. // -----------------------------------------------------------------------
  2447. CarlaPlugin* CarlaPlugin::newNative(const Initializer& init)
  2448. {
  2449. carla_debug("CarlaPlugin::newNative({%p, \"%s\", \"%s\", \"%s\", " P_INT64 "})", init.engine, init.filename, init.name, init.label, init.uniqueId);
  2450. CarlaPluginNative* const plugin(new CarlaPluginNative(init.engine, init.id));
  2451. if (! plugin->init(init.name, init.label, init.options))
  2452. {
  2453. delete plugin;
  2454. return nullptr;
  2455. }
  2456. return plugin;
  2457. }
  2458. // used in CarlaStandalone.cpp
  2459. const void* carla_render_inline_display_internal(CarlaPlugin* plugin, uint32_t width, uint32_t height);
  2460. const void* carla_render_inline_display_internal(CarlaPlugin* plugin, uint32_t width, uint32_t height)
  2461. {
  2462. CarlaPluginNative* const nativePlugin = (CarlaPluginNative*)plugin;
  2463. return nativePlugin->renderInlineDisplay(width, height);
  2464. }
  2465. // -----------------------------------------------------------------------
  2466. CARLA_BACKEND_END_NAMESPACE