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.

CarlaPluginVST3.cpp 88KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556
  1. /*
  2. * Carla VST3 Plugin
  3. * Copyright (C) 2014-2022 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. /* TODO list
  18. * noexcept safe calls
  19. * paramId vs index
  20. */
  21. #include "CarlaPluginInternal.hpp"
  22. #include "CarlaEngine.hpp"
  23. #include "AppConfig.h"
  24. #if defined(USING_JUCE) && JUCE_PLUGINHOST_VST3
  25. # define USE_JUCE_FOR_VST3
  26. #endif
  27. #include "CarlaBackendUtils.hpp"
  28. #include "CarlaVst3Utils.hpp"
  29. #include "CarlaPluginUI.hpp"
  30. #ifdef CARLA_OS_MAC
  31. # include "CarlaMacUtils.hpp"
  32. # import <Foundation/Foundation.h>
  33. #endif
  34. #include "water/files/File.h"
  35. CARLA_BACKEND_START_NAMESPACE
  36. // --------------------------------------------------------------------------------------------------------------------
  37. static inline
  38. size_t strlen_utf16(const int16_t* const str)
  39. {
  40. size_t i = 0;
  41. while (str[i] != 0)
  42. ++i;
  43. return i;
  44. }
  45. // --------------------------------------------------------------------------------------------------------------------
  46. static inline
  47. void strncpy_utf8(char* const dst, const int16_t* const src, const size_t length)
  48. {
  49. CARLA_SAFE_ASSERT_RETURN(length > 0,);
  50. if (const size_t len = std::min(strlen_utf16(src), length-1U))
  51. {
  52. for (size_t i=0; i<len; ++i)
  53. {
  54. // skip non-ascii chars, unsupported
  55. if (src[i] >= 0x80)
  56. continue;
  57. dst[i] = static_cast<char>(src[i]);
  58. }
  59. dst[len] = 0;
  60. }
  61. else
  62. {
  63. dst[0] = 0;
  64. }
  65. }
  66. // --------------------------------------------------------------------------------------------------------------------
  67. static uint32_t V3_API v3_ref_static(void*) { return 1; }
  68. static uint32_t V3_API v3_unref_static(void*) { return 0; }
  69. struct carla_v3_host_application : v3_host_application_cpp {
  70. carla_v3_host_application()
  71. {
  72. query_interface = carla_query_interface;
  73. ref = v3_ref_static;
  74. unref = v3_unref_static;
  75. app.get_name = carla_get_name;
  76. app.create_instance = carla_create_instance;
  77. }
  78. static v3_result V3_API carla_query_interface(void* const self, const v3_tuid iid, void** const iface)
  79. {
  80. if (v3_tuid_match(iid, v3_funknown_iid) ||
  81. v3_tuid_match(iid, v3_host_application_iid))
  82. {
  83. *iface = self;
  84. return V3_OK;
  85. }
  86. *iface = nullptr;
  87. return V3_NO_INTERFACE;
  88. }
  89. static v3_result V3_API carla_get_name(void*, v3_str_128 name)
  90. {
  91. static const char hostname[] = "Carla-Discovery\0";
  92. for (size_t i=0; i<sizeof(hostname); ++i)
  93. name[i] = hostname[i];
  94. return V3_OK;
  95. }
  96. // TODO
  97. static v3_result V3_API carla_create_instance(void*, v3_tuid, v3_tuid, void**) { return V3_NOT_IMPLEMENTED; }
  98. CARLA_DECLARE_NON_COPYABLE(carla_v3_host_application)
  99. };
  100. struct carla_v3_plugin_frame : v3_plugin_frame_cpp {
  101. };
  102. // --------------------------------------------------------------------------------------------------------------------
  103. struct carla_v3_input_param_value_queue : v3_param_value_queue_cpp {
  104. const v3_param_id paramId;
  105. int8_t numUsed;
  106. struct Point {
  107. int32_t offset;
  108. float value;
  109. } points[32];
  110. carla_v3_input_param_value_queue(const v3_param_id pId)
  111. : paramId(pId),
  112. numUsed(0)
  113. {
  114. query_interface = carla_query_interface;
  115. ref = v3_ref_static;
  116. unref = v3_unref_static;
  117. queue.get_param_id = carla_get_param_id;
  118. queue.get_point_count = carla_get_point_count;
  119. queue.get_point = carla_get_point;
  120. queue.add_point = carla_add_point;
  121. }
  122. private:
  123. static v3_result V3_API carla_query_interface(void* const self, const v3_tuid iid, void** const iface)
  124. {
  125. if (v3_tuid_match(iid, v3_funknown_iid) ||
  126. v3_tuid_match(iid, v3_param_value_queue_iid))
  127. {
  128. *iface = self;
  129. return V3_OK;
  130. }
  131. *iface = nullptr;
  132. return V3_NO_INTERFACE;
  133. }
  134. static v3_param_id V3_API carla_get_param_id(void* self)
  135. {
  136. carla_v3_input_param_value_queue* const me = *static_cast<carla_v3_input_param_value_queue**>(self);
  137. return me->paramId;
  138. }
  139. static int32_t V3_API carla_get_point_count(void* self)
  140. {
  141. carla_v3_input_param_value_queue* const me = *static_cast<carla_v3_input_param_value_queue**>(self);
  142. return me->numUsed;
  143. }
  144. static v3_result V3_API carla_get_point(void* const self, const int32_t idx, int32_t* const sample_offset, double* const value)
  145. {
  146. carla_v3_input_param_value_queue* const me = *static_cast<carla_v3_input_param_value_queue**>(self);
  147. CARLA_SAFE_ASSERT_INT2_RETURN(idx < me->numUsed, idx, me->numUsed, V3_INVALID_ARG);
  148. *sample_offset = me->points[idx].offset;
  149. *value = me->points[idx].value;
  150. return V3_OK;
  151. }
  152. static v3_result V3_API carla_add_point(void*, int32_t, double, int32_t*)
  153. {
  154. // there is nothing here for input parameters, plugins are not meant to call this!
  155. return V3_NOT_IMPLEMENTED;
  156. }
  157. CARLA_DECLARE_NON_COPYABLE(carla_v3_input_param_value_queue)
  158. };
  159. struct carla_v3_input_param_changes : v3_param_changes_cpp {
  160. const uint32_t paramCount;
  161. struct UpdatedParam {
  162. bool updated;
  163. float value;
  164. }* const updatedParams;
  165. carla_v3_input_param_value_queue** const queue;
  166. // data given to plugins
  167. v3_param_value_queue*** pluginExposedQueue;
  168. int32_t pluginExposedCount;
  169. carla_v3_input_param_changes(const PluginParameterData& paramData)
  170. : paramCount(paramData.count),
  171. updatedParams(new UpdatedParam[paramData.count]),
  172. queue(new carla_v3_input_param_value_queue*[paramData.count]),
  173. pluginExposedQueue(new v3_param_value_queue**[paramData.count]),
  174. pluginExposedCount(0)
  175. {
  176. for (uint32_t i=0; i<paramData.count; ++i)
  177. queue[i] = new carla_v3_input_param_value_queue(static_cast<v3_param_id>(paramData.data[i].rindex));
  178. query_interface = carla_query_interface;
  179. ref = v3_ref_static;
  180. unref = v3_unref_static;
  181. changes.get_param_count = carla_get_param_count;
  182. changes.get_param_data = carla_get_param_data;
  183. changes.add_param_data = carla_add_param_data;
  184. }
  185. // called during start of process, gathering all parameter update requests so far
  186. void init()
  187. {
  188. for (uint32_t i=0; i<paramCount; ++i)
  189. {
  190. if (updatedParams[i].updated)
  191. {
  192. queue[i]->numUsed = 1;
  193. queue[i]->points[0].offset = 0;
  194. queue[i]->points[0].value = updatedParams[i].value;
  195. }
  196. else
  197. {
  198. queue[i]->numUsed = 0;
  199. }
  200. }
  201. }
  202. // called just before plugin processing, creating local queue
  203. void prepare()
  204. {
  205. int32_t count = 0;
  206. for (uint32_t i=0; i<paramCount; ++i)
  207. {
  208. if (queue[i]->numUsed)
  209. pluginExposedQueue[count++] = (v3_param_value_queue**)&queue[i];
  210. }
  211. pluginExposedCount = count;
  212. }
  213. // called when a parameter is set from non-rt thread
  214. void setParamValue(const uint32_t index, const float value) noexcept
  215. {
  216. updatedParams[index].value = value;
  217. updatedParams[index].updated = true;
  218. }
  219. private:
  220. static v3_result V3_API carla_query_interface(void* const self, const v3_tuid iid, void** const iface)
  221. {
  222. if (v3_tuid_match(iid, v3_funknown_iid) ||
  223. v3_tuid_match(iid, v3_param_changes_iid))
  224. {
  225. *iface = self;
  226. return V3_OK;
  227. }
  228. *iface = nullptr;
  229. return V3_NO_INTERFACE;
  230. }
  231. static int32_t V3_API carla_get_param_count(void* const self)
  232. {
  233. carla_v3_input_param_changes* const me = *static_cast<carla_v3_input_param_changes**>(self);
  234. return me->pluginExposedCount;
  235. }
  236. static v3_param_value_queue** V3_API carla_get_param_data(void* const self, const int32_t index)
  237. {
  238. carla_v3_input_param_changes* const me = *static_cast<carla_v3_input_param_changes**>(self);
  239. return me->pluginExposedQueue[index];
  240. }
  241. static v3_param_value_queue** V3_API carla_add_param_data(void*, v3_param_id*, int32_t*)
  242. {
  243. // there is nothing here for input parameters, plugins are not meant to call this!
  244. return nullptr;
  245. }
  246. CARLA_DECLARE_NON_COPYABLE(carla_v3_input_param_changes)
  247. };
  248. // --------------------------------------------------------------------------------------------------------------------
  249. struct carla_v3_output_param_changes : v3_param_changes_cpp {
  250. carla_v3_output_param_changes()
  251. {
  252. query_interface = carla_query_interface;
  253. ref = v3_ref_static;
  254. unref = v3_unref_static;
  255. changes.get_param_count = carla_get_param_count;
  256. changes.get_param_data = carla_get_param_data;
  257. changes.add_param_data = carla_add_param_data;
  258. }
  259. static v3_result V3_API carla_query_interface(void* const self, const v3_tuid iid, void** const iface)
  260. {
  261. if (v3_tuid_match(iid, v3_funknown_iid) ||
  262. v3_tuid_match(iid, v3_param_changes_iid))
  263. {
  264. *iface = self;
  265. return V3_OK;
  266. }
  267. *iface = nullptr;
  268. return V3_NO_INTERFACE;
  269. }
  270. static int32_t V3_API carla_get_param_count(void*) { return 0; }
  271. static v3_param_value_queue** V3_API carla_get_param_data(void*, int32_t) { return nullptr; }
  272. static v3_param_value_queue** V3_API carla_add_param_data(void*, v3_param_id*, int32_t*) { return nullptr; }
  273. CARLA_DECLARE_NON_COPYABLE(carla_v3_output_param_changes)
  274. };
  275. // --------------------------------------------------------------------------------------------------------------------
  276. struct carla_v3_input_event_list : v3_event_list_cpp {
  277. v3_event events[kPluginMaxMidiEvents];
  278. uint16_t numEvents;
  279. carla_v3_input_event_list()
  280. : numEvents(0)
  281. {
  282. query_interface = carla_query_interface;
  283. ref = v3_ref_static;
  284. unref = v3_unref_static;
  285. list.get_event_count = carla_get_event_count;
  286. list.get_event = carla_get_event;
  287. list.add_event = carla_add_event;
  288. }
  289. private:
  290. static v3_result V3_API carla_query_interface(void* const self, const v3_tuid iid, void** const iface)
  291. {
  292. if (v3_tuid_match(iid, v3_funknown_iid) ||
  293. v3_tuid_match(iid, v3_event_list_iid))
  294. {
  295. *iface = self;
  296. return V3_OK;
  297. }
  298. *iface = nullptr;
  299. return V3_NO_INTERFACE;
  300. }
  301. static uint32_t V3_API carla_get_event_count(void* const self)
  302. {
  303. const carla_v3_input_event_list* const me = *static_cast<const carla_v3_input_event_list**>(self);
  304. return me->numEvents;
  305. }
  306. static v3_result V3_API carla_get_event(void* const self, const int32_t index, v3_event* const event)
  307. {
  308. const carla_v3_input_event_list* const me = *static_cast<const carla_v3_input_event_list**>(self);
  309. CARLA_SAFE_ASSERT_RETURN(index < static_cast<int32_t>(me->numEvents), V3_INVALID_ARG);
  310. std::memcpy(event, &me->events[index], sizeof(v3_event));
  311. return V3_OK;
  312. }
  313. static v3_result V3_API carla_add_event(void*, v3_event*)
  314. {
  315. // there is nothing here for input events, plugins are not meant to call this!
  316. return V3_NOT_IMPLEMENTED;
  317. }
  318. CARLA_DECLARE_NON_COPYABLE(carla_v3_input_event_list)
  319. };
  320. // --------------------------------------------------------------------------------------------------------------------
  321. struct carla_v3_output_event_list : v3_event_list_cpp {
  322. carla_v3_output_event_list()
  323. {
  324. query_interface = carla_query_interface;
  325. ref = v3_ref_static;
  326. unref = v3_unref_static;
  327. list.get_event_count = carla_get_event_count;
  328. list.get_event = carla_get_event;
  329. list.add_event = carla_add_event;
  330. }
  331. private:
  332. static v3_result V3_API carla_query_interface(void* const self, const v3_tuid iid, void** const iface)
  333. {
  334. if (v3_tuid_match(iid, v3_funknown_iid) ||
  335. v3_tuid_match(iid, v3_event_list_iid))
  336. {
  337. *iface = self;
  338. return V3_OK;
  339. }
  340. *iface = nullptr;
  341. return V3_NO_INTERFACE;
  342. }
  343. static uint32_t V3_API carla_get_event_count(void*) { return 0; }
  344. static v3_result V3_API carla_get_event(void*, int32_t, v3_event*) { return V3_NOT_IMPLEMENTED; }
  345. static v3_result V3_API carla_add_event(void*, v3_event*) { return V3_NOT_IMPLEMENTED; }
  346. CARLA_DECLARE_NON_COPYABLE(carla_v3_output_event_list)
  347. };
  348. // --------------------------------------------------------------------------------------------------------------------
  349. class CarlaPluginVST3 : public CarlaPlugin,
  350. private CarlaPluginUI::Callback
  351. {
  352. public:
  353. CarlaPluginVST3(CarlaEngine* const engine, const uint id)
  354. : CarlaPlugin(engine, id),
  355. fFirstActive(true),
  356. fAudioAndCvOutBuffers(nullptr),
  357. fLastKnownLatency(0),
  358. fLastTimeInfo(),
  359. fV3TimeContext(),
  360. fV3Application(new carla_v3_host_application),
  361. fV3ClassInfo(),
  362. fV3(),
  363. fEvents(),
  364. fUI()
  365. {
  366. carla_debug("CarlaPluginVST3::CarlaPluginVST3(%p, %i)", engine, id);
  367. carla_zeroStruct(fV3TimeContext);
  368. }
  369. ~CarlaPluginVST3() override
  370. {
  371. carla_debug("CarlaPluginVST3::~CarlaPluginVST3()");
  372. // close UI
  373. if (pData->hints & PLUGIN_HAS_CUSTOM_UI)
  374. {
  375. if (! fUI.isEmbed)
  376. showCustomUI(false);
  377. if (fUI.isAttached)
  378. {
  379. fUI.isAttached = false;
  380. v3_cpp_obj(fV3.view)->removed(fV3.view);
  381. }
  382. }
  383. if (fV3.view != nullptr)
  384. {
  385. v3_cpp_obj_unref(fV3.view);
  386. fV3.view = nullptr;
  387. }
  388. pData->singleMutex.lock();
  389. pData->masterMutex.lock();
  390. if (pData->client != nullptr && pData->client->isActive())
  391. pData->client->deactivate(true);
  392. if (pData->active)
  393. {
  394. deactivate();
  395. pData->active = false;
  396. }
  397. clearBuffers();
  398. fV3.exit();
  399. }
  400. // -------------------------------------------------------------------
  401. // Information (base)
  402. PluginType getType() const noexcept override
  403. {
  404. return PLUGIN_VST3;
  405. }
  406. PluginCategory getCategory() const noexcept override
  407. {
  408. return getPluginCategoryFromV3SubCategories(fV3ClassInfo.v2.sub_categories);
  409. }
  410. uint32_t getLatencyInFrames() const noexcept override
  411. {
  412. return fLastKnownLatency;
  413. }
  414. // -------------------------------------------------------------------
  415. // Information (count)
  416. /* TODO
  417. uint32_t getMidiInCount() const noexcept override
  418. {
  419. }
  420. uint32_t getMidiOutCount() const noexcept override
  421. {
  422. }
  423. uint32_t getParameterScalePointCount(uint32_t parameterId) const noexcept override
  424. {
  425. }
  426. */
  427. // -------------------------------------------------------------------
  428. // Information (current data)
  429. /* TODO
  430. std::size_t getChunkData(void** dataPtr) noexcept override
  431. {
  432. }
  433. */
  434. // -------------------------------------------------------------------
  435. // Information (per-plugin data)
  436. uint getOptionsAvailable() const noexcept override
  437. {
  438. uint options = 0x0;
  439. // can't disable fixed buffers if using latency
  440. if (fLastKnownLatency == 0)
  441. options |= PLUGIN_OPTION_FIXED_BUFFERS;
  442. /* TODO
  443. if (numPrograms > 1)
  444. options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  445. */
  446. options |= PLUGIN_OPTION_USE_CHUNKS;
  447. /* TODO
  448. if (hasMidiInput())
  449. {
  450. options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  451. options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  452. options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  453. options |= PLUGIN_OPTION_SEND_PITCHBEND;
  454. options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  455. options |= PLUGIN_OPTION_SEND_PROGRAM_CHANGES;
  456. options |= PLUGIN_OPTION_SKIP_SENDING_NOTES;
  457. }
  458. */
  459. return options;
  460. }
  461. float getParameterValue(const uint32_t parameterId) const noexcept override
  462. {
  463. CARLA_SAFE_ASSERT_RETURN(fV3.controller != nullptr, 0.0f);
  464. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0.0f);
  465. const double normalized = v3_cpp_obj(fV3.controller)->get_parameter_normalised(fV3.controller, parameterId);
  466. return static_cast<float>(
  467. v3_cpp_obj(fV3.controller)->normalised_parameter_to_plain(fV3.controller, parameterId, normalized));
  468. }
  469. /* TODO
  470. float getParameterScalePointValue(uint32_t parameterId, uint32_t scalePointId) const noexcept override
  471. {
  472. }
  473. */
  474. bool getLabel(char* const strBuf) const noexcept override
  475. {
  476. std::strncpy(strBuf, fV3ClassInfo.v1.name, STR_MAX);
  477. return true;
  478. }
  479. bool getMaker(char* const strBuf) const noexcept override
  480. {
  481. std::strncpy(strBuf, fV3ClassInfo.v2.vendor, STR_MAX);
  482. return true;
  483. }
  484. bool getCopyright(char* const strBuf) const noexcept override
  485. {
  486. return getMaker(strBuf);
  487. }
  488. bool getRealName(char* const strBuf) const noexcept override
  489. {
  490. std::strncpy(strBuf, fV3ClassInfo.v1.name, STR_MAX);
  491. return true;
  492. }
  493. bool getParameterName(const uint32_t parameterId, char* const strBuf) const noexcept override
  494. {
  495. CARLA_SAFE_ASSERT_RETURN(fV3.controller != nullptr, 0.0f);
  496. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, false);
  497. v3_param_info paramInfo = {};
  498. CARLA_SAFE_ASSERT_RETURN(v3_cpp_obj(fV3.controller)->get_parameter_info(fV3.controller,
  499. static_cast<int32_t>(parameterId),
  500. &paramInfo) == V3_OK, false);
  501. strncpy_utf8(strBuf, paramInfo.title, STR_MAX);
  502. return true;
  503. }
  504. /* TODO
  505. bool getParameterSymbol(uint32_t parameterId, char* strBuf) const noexcept override
  506. {
  507. }
  508. */
  509. bool getParameterText(const uint32_t parameterId, char* const strBuf) noexcept override
  510. {
  511. CARLA_SAFE_ASSERT_RETURN(fV3.controller != nullptr, false);
  512. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, false);
  513. const double normalized = v3_cpp_obj(fV3.controller)->get_parameter_normalised(fV3.controller, parameterId);
  514. v3_str_128 paramText;
  515. CARLA_SAFE_ASSERT_RETURN(v3_cpp_obj(fV3.controller)->get_parameter_string_for_value(fV3.controller, parameterId, normalized, paramText) == V3_OK, false);
  516. if (paramText[0] != '\0')
  517. strncpy_utf8(strBuf, paramText, STR_MAX);
  518. else
  519. std::snprintf(strBuf, STR_MAX, "%.12g",
  520. v3_cpp_obj(fV3.controller)->normalised_parameter_to_plain(fV3.controller, parameterId, normalized));
  521. return true;
  522. }
  523. bool getParameterUnit(const uint32_t parameterId, char* const strBuf) const noexcept override
  524. {
  525. CARLA_SAFE_ASSERT_RETURN(fV3.controller != nullptr, false);
  526. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, false);
  527. v3_param_info paramInfo = {};
  528. CARLA_SAFE_ASSERT_RETURN(v3_cpp_obj(fV3.controller)->get_parameter_info(fV3.controller,
  529. static_cast<int32_t>(parameterId),
  530. &paramInfo) == V3_OK, false);
  531. strncpy_utf8(strBuf, paramInfo.units, STR_MAX);
  532. return true;
  533. }
  534. /* TODO
  535. bool getParameterGroupName(uint32_t parameterId, char* strBuf) const noexcept override
  536. {
  537. }
  538. bool getParameterScalePointLabel(uint32_t parameterId, uint32_t scalePointId, char* strBuf) const noexcept override
  539. {
  540. }
  541. */
  542. // -------------------------------------------------------------------
  543. // Set data (state)
  544. /* TODO
  545. void prepareForSave(const bool temporary) override
  546. {
  547. // component to edit controller state or vice-versa here
  548. }
  549. */
  550. // -------------------------------------------------------------------
  551. // Set data (internal stuff)
  552. /* TODO
  553. void setName(const char* newName) override
  554. {
  555. }
  556. */
  557. // -------------------------------------------------------------------
  558. // Set data (plugin-specific stuff)
  559. void setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  560. {
  561. CARLA_SAFE_ASSERT_RETURN(fV3.controller != nullptr,);
  562. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  563. const float fixedValue = pData->param.getFixedValue(parameterId, value);
  564. const double normalized = v3_cpp_obj(fV3.controller)->plain_parameter_to_normalised(fV3.controller, parameterId, fixedValue);
  565. // report value to component (next process call)
  566. fEvents.paramInputs->setParamValue(parameterId, static_cast<float>(normalized));
  567. // report value to edit controller
  568. v3_cpp_obj(fV3.controller)->set_parameter_normalised(fV3.controller, parameterId, normalized);
  569. CarlaPlugin::setParameterValue(parameterId, fixedValue, sendGui, sendOsc, sendCallback);
  570. }
  571. void setParameterValueRT(const uint32_t parameterId, const float value, const uint32_t frameOffset, const bool sendCallbackLater) noexcept override
  572. {
  573. CARLA_SAFE_ASSERT_RETURN(fV3.controller != nullptr,);
  574. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  575. const float fixedValue = pData->param.getFixedValue(parameterId, value);
  576. // TODO append value to V3 changes queue list
  577. CarlaPlugin::setParameterValueRT(parameterId, fixedValue, frameOffset, sendCallbackLater);
  578. }
  579. /*
  580. void setChunkData(const void* data, std::size_t dataSize) override
  581. {
  582. }
  583. */
  584. // -------------------------------------------------------------------
  585. // Set ui stuff
  586. void setCustomUITitle(const char* const title) noexcept override
  587. {
  588. if (fUI.window != nullptr)
  589. {
  590. try {
  591. fUI.window->setTitle(title);
  592. } CARLA_SAFE_EXCEPTION("set custom ui title");
  593. }
  594. CarlaPlugin::setCustomUITitle(title);
  595. }
  596. void showCustomUI(const bool yesNo) override
  597. {
  598. if (fUI.isVisible == yesNo)
  599. return;
  600. CARLA_SAFE_ASSERT_RETURN(fV3.view != nullptr,);
  601. if (yesNo)
  602. {
  603. CarlaString uiTitle;
  604. if (pData->uiTitle.isNotEmpty())
  605. {
  606. uiTitle = pData->uiTitle;
  607. }
  608. else
  609. {
  610. uiTitle = pData->name;
  611. uiTitle += " (GUI)";
  612. }
  613. if (fUI.window == nullptr)
  614. {
  615. const char* msg = nullptr;
  616. const EngineOptions& opts(pData->engine->getOptions());
  617. #if defined(CARLA_OS_MAC)
  618. fUI.window = CarlaPluginUI::newCocoa(this, opts.frontendWinId, opts.pluginsAreStandalone, false);
  619. #elif defined(CARLA_OS_WIN)
  620. fUI.window = CarlaPluginUI::newWindows(this, opts.frontendWinId, opts.pluginsAreStandalone, false);
  621. #elif defined(HAVE_X11)
  622. fUI.window = CarlaPluginUI::newX11(this, opts.frontendWinId, opts.pluginsAreStandalone, false, false);
  623. #else
  624. msg = "Unsupported UI type";
  625. #endif
  626. if (fUI.window == nullptr)
  627. return pData->engine->callback(true, true,
  628. ENGINE_CALLBACK_UI_STATE_CHANGED,
  629. pData->id,
  630. -1,
  631. 0, 0, 0.0f,
  632. msg);
  633. fUI.window->setTitle(uiTitle.buffer());
  634. #ifndef CARLA_OS_MAC
  635. // TODO inform plugin of what UI scale we use
  636. #endif
  637. if (v3_cpp_obj(fV3.view)->attached(fV3.view, fUI.window->getPtr(), V3_VIEW_PLATFORM_TYPE_NATIVE) == V3_OK)
  638. {
  639. v3_view_rect rect = {};
  640. if (v3_cpp_obj(fV3.view)->get_size(fV3.view, &rect) == V3_OK)
  641. {
  642. const int32_t width = rect.right - rect.left;
  643. const int32_t height = rect.bottom - rect.top;
  644. CARLA_SAFE_ASSERT_INT2(width > 1 && height > 1, width, height);
  645. if (width > 1 && height > 1)
  646. fUI.window->setSize(static_cast<uint>(width), static_cast<uint>(height), true);
  647. }
  648. }
  649. else
  650. {
  651. delete fUI.window;
  652. fUI.window = nullptr;
  653. carla_stderr2("Plugin refused to open its own UI");
  654. return pData->engine->callback(true, true,
  655. ENGINE_CALLBACK_UI_STATE_CHANGED,
  656. pData->id,
  657. -1,
  658. 0, 0, 0.0f,
  659. "Plugin refused to open its own UI");
  660. }
  661. }
  662. fUI.window->show();
  663. fUI.isVisible = true;
  664. pData->hints |= PLUGIN_NEEDS_UI_MAIN_THREAD;
  665. }
  666. else
  667. {
  668. fUI.isVisible = false;
  669. pData->hints &= ~PLUGIN_NEEDS_UI_MAIN_THREAD;
  670. CARLA_SAFE_ASSERT_RETURN(fUI.window != nullptr,);
  671. fUI.window->hide();
  672. }
  673. }
  674. // -------------------------------------------------------------------
  675. // Plugin state
  676. void reload() override
  677. {
  678. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr,);
  679. CARLA_SAFE_ASSERT_RETURN(fV3.component != nullptr,);
  680. CARLA_SAFE_ASSERT_RETURN(fV3.controller != nullptr,);
  681. CARLA_SAFE_ASSERT_RETURN(fV3.processor != nullptr,);
  682. carla_debug("CarlaPluginVST3::reload() - start");
  683. // Safely disable plugin for reload
  684. const ScopedDisabler sd(this);
  685. if (pData->active)
  686. deactivate();
  687. clearBuffers();
  688. const int32_t numAudioInputBuses = v3_cpp_obj(fV3.component)->get_bus_count(fV3.component, V3_AUDIO, V3_INPUT);
  689. const int32_t numAudioOutputBuses = v3_cpp_obj(fV3.component)->get_bus_count(fV3.component, V3_AUDIO, V3_OUTPUT);
  690. const int32_t numEventInputBuses = v3_cpp_obj(fV3.component)->get_bus_count(fV3.component, V3_EVENT, V3_INPUT);
  691. const int32_t numEventOutputBuses = v3_cpp_obj(fV3.component)->get_bus_count(fV3.component, V3_EVENT, V3_OUTPUT);
  692. const int32_t numParameters = v3_cpp_obj(fV3.controller)->get_parameter_count(fV3.controller);
  693. CARLA_SAFE_ASSERT(numAudioInputBuses >= 0);
  694. CARLA_SAFE_ASSERT(numAudioOutputBuses >= 0);
  695. CARLA_SAFE_ASSERT(numEventInputBuses >= 0);
  696. CARLA_SAFE_ASSERT(numEventOutputBuses >= 0);
  697. CARLA_SAFE_ASSERT(numParameters >= 0);
  698. uint32_t aIns, aOuts, cvIns, cvOuts, params;
  699. aIns = aOuts = cvIns = cvOuts = params = 0;
  700. bool needsCtrlIn, needsCtrlOut;
  701. needsCtrlIn = needsCtrlOut = false;
  702. for (int32_t j=0; j<numAudioInputBuses; ++j)
  703. {
  704. v3_bus_info busInfo = {};
  705. CARLA_SAFE_ASSERT_BREAK(v3_cpp_obj(fV3.component)->get_bus_info(fV3.component, V3_AUDIO, V3_INPUT, j, &busInfo) == V3_OK);
  706. CARLA_SAFE_ASSERT_BREAK(busInfo.channel_count >= 0);
  707. if (busInfo.flags & V3_IS_CONTROL_VOLTAGE)
  708. cvIns += static_cast<uint32_t>(busInfo.channel_count);
  709. else
  710. aIns += static_cast<uint32_t>(busInfo.channel_count);
  711. }
  712. for (int32_t j=0; j<numAudioOutputBuses; ++j)
  713. {
  714. v3_bus_info busInfo = {};
  715. CARLA_SAFE_ASSERT_BREAK(v3_cpp_obj(fV3.component)->get_bus_info(fV3.component, V3_AUDIO, V3_OUTPUT, j, &busInfo) == V3_OK);
  716. CARLA_SAFE_ASSERT_BREAK(busInfo.channel_count >= 0);
  717. if (busInfo.flags & V3_IS_CONTROL_VOLTAGE)
  718. cvOuts += static_cast<uint32_t>(busInfo.channel_count);
  719. else
  720. aOuts += static_cast<uint32_t>(busInfo.channel_count);
  721. }
  722. for (int32_t j=0; j<numParameters; ++j)
  723. {
  724. v3_param_info paramInfo = {};
  725. CARLA_SAFE_ASSERT_BREAK(v3_cpp_obj(fV3.controller)->get_parameter_info(fV3.controller, j, &paramInfo) == V3_OK);
  726. if ((paramInfo.flags & (V3_PARAM_IS_BYPASS|V3_PARAM_IS_HIDDEN|V3_PARAM_PROGRAM_CHANGE)) == 0x0)
  727. ++params;
  728. }
  729. if (aIns > 0)
  730. {
  731. pData->audioIn.createNew(aIns);
  732. }
  733. if (aOuts > 0)
  734. {
  735. pData->audioOut.createNew(aOuts);
  736. needsCtrlIn = true;
  737. }
  738. if (cvIns > 0)
  739. pData->cvIn.createNew(cvIns);
  740. if (cvOuts > 0)
  741. pData->cvOut.createNew(cvOuts);
  742. if (numEventInputBuses > 0)
  743. needsCtrlIn = true;
  744. if (numEventOutputBuses > 0)
  745. needsCtrlOut = true;
  746. if (params > 0)
  747. {
  748. pData->param.createNew(params, false);
  749. needsCtrlIn = true;
  750. }
  751. if (aOuts + cvIns > 0)
  752. {
  753. fAudioAndCvOutBuffers = new float*[aOuts + cvIns];
  754. for (uint32_t i=0; i < aOuts + cvIns; ++i)
  755. fAudioAndCvOutBuffers[i] = nullptr;
  756. }
  757. const EngineProcessMode processMode = pData->engine->getProccessMode();
  758. const uint portNameSize = pData->engine->getMaxPortNameSize();
  759. CarlaString portName;
  760. // Audio Ins
  761. for (uint32_t j=0; j < aIns; ++j)
  762. {
  763. portName.clear();
  764. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  765. {
  766. portName = pData->name;
  767. portName += ":";
  768. }
  769. if (aIns > 1)
  770. {
  771. portName += "input_";
  772. portName += CarlaString(j+1);
  773. }
  774. else
  775. portName += "input";
  776. portName.truncate(portNameSize);
  777. pData->audioIn.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true, j);
  778. pData->audioIn.ports[j].rindex = j;
  779. }
  780. // Audio Outs
  781. for (uint32_t j=0; j < aOuts; ++j)
  782. {
  783. portName.clear();
  784. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  785. {
  786. portName = pData->name;
  787. portName += ":";
  788. }
  789. if (aOuts > 1)
  790. {
  791. portName += "output_";
  792. portName += CarlaString(j+1);
  793. }
  794. else
  795. portName += "output";
  796. portName.truncate(portNameSize);
  797. pData->audioOut.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false, j);
  798. pData->audioOut.ports[j].rindex = j;
  799. }
  800. // CV Ins
  801. for (uint32_t j=0; j < cvIns; ++j)
  802. {
  803. portName.clear();
  804. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  805. {
  806. portName = pData->name;
  807. portName += ":";
  808. }
  809. if (cvIns > 1)
  810. {
  811. portName += "cv_input_";
  812. portName += CarlaString(j+1);
  813. }
  814. else
  815. portName += "cv_input";
  816. portName.truncate(portNameSize);
  817. pData->cvIn.ports[j].port = (CarlaEngineCVPort*)pData->client->addPort(kEnginePortTypeCV, portName, true, j);
  818. pData->cvIn.ports[j].rindex = j;
  819. }
  820. // CV Outs
  821. for (uint32_t j=0; j < cvOuts; ++j)
  822. {
  823. portName.clear();
  824. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  825. {
  826. portName = pData->name;
  827. portName += ":";
  828. }
  829. if (cvOuts > 1)
  830. {
  831. portName += "cv_output_";
  832. portName += CarlaString(j+1);
  833. }
  834. else
  835. portName += "cv_output";
  836. portName.truncate(portNameSize);
  837. pData->cvOut.ports[j].port = (CarlaEngineCVPort*)pData->client->addPort(kEnginePortTypeCV, portName, false, j);
  838. pData->cvOut.ports[j].rindex = j;
  839. }
  840. for (uint32_t j=0; j < params; ++j)
  841. {
  842. const int32_t ij = static_cast<int32_t>(j);
  843. pData->param.data[j].index = j;
  844. pData->param.data[j].rindex = ij;
  845. v3_param_info paramInfo = {};
  846. CARLA_SAFE_ASSERT_BREAK(v3_cpp_obj(fV3.controller)->get_parameter_info(fV3.controller, ij, &paramInfo) == V3_OK);
  847. if (paramInfo.flags & (V3_PARAM_IS_BYPASS|V3_PARAM_IS_HIDDEN|V3_PARAM_PROGRAM_CHANGE))
  848. continue;
  849. double min, max, def, step, stepSmall, stepLarge;
  850. min = v3_cpp_obj(fV3.controller)->normalised_parameter_to_plain(fV3.controller, j, 0.0);
  851. max = v3_cpp_obj(fV3.controller)->normalised_parameter_to_plain(fV3.controller, j, 1.0);
  852. def = v3_cpp_obj(fV3.controller)->normalised_parameter_to_plain(fV3.controller, j, paramInfo.default_normalised_value);
  853. if (min >= max)
  854. max = min + 0.1;
  855. if (def < min)
  856. def = min;
  857. else if (def > max)
  858. def = max;
  859. if (paramInfo.flags & V3_PARAM_READ_ONLY)
  860. pData->param.data[j].type = PARAMETER_OUTPUT;
  861. else
  862. pData->param.data[j].type = PARAMETER_INPUT;
  863. if (paramInfo.step_count == 1)
  864. {
  865. step = max - min;
  866. stepSmall = step;
  867. stepLarge = step;
  868. pData->param.data[j].hints |= PARAMETER_IS_BOOLEAN;
  869. }
  870. /*
  871. else if (paramInfo.step_count != 0 && (paramInfo.flags & V3_PARAM_IS_LIST) != 0x0)
  872. {
  873. step = 1.0;
  874. stepSmall = 1.0;
  875. stepLarge = std::min(max - min, 10.0);
  876. pData->param.data[j].hints |= PARAMETER_IS_INTEGER;
  877. }
  878. */
  879. else
  880. {
  881. float range = max - min;
  882. step = range/100.0;
  883. stepSmall = range/1000.0;
  884. stepLarge = range/10.0;
  885. }
  886. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  887. pData->param.data[j].hints |= PARAMETER_USES_CUSTOM_TEXT;
  888. if (paramInfo.flags & V3_PARAM_CAN_AUTOMATE)
  889. {
  890. pData->param.data[j].hints |= PARAMETER_IS_AUTOMATABLE;
  891. if ((paramInfo.flags & V3_PARAM_IS_LIST) == 0x0)
  892. pData->param.data[j].hints |= PARAMETER_CAN_BE_CV_CONTROLLED;
  893. }
  894. pData->param.ranges[j].min = min;
  895. pData->param.ranges[j].max = max;
  896. pData->param.ranges[j].def = def;
  897. pData->param.ranges[j].step = step;
  898. pData->param.ranges[j].stepSmall = stepSmall;
  899. pData->param.ranges[j].stepLarge = stepLarge;
  900. }
  901. if (params > 0)
  902. {
  903. fEvents.paramInputs = new carla_v3_input_param_changes(pData->param);
  904. fEvents.paramOutputs = new carla_v3_output_param_changes;
  905. }
  906. if (needsCtrlIn)
  907. {
  908. portName.clear();
  909. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  910. {
  911. portName = pData->name;
  912. portName += ":";
  913. }
  914. portName += "events-in";
  915. portName.truncate(portNameSize);
  916. pData->event.portIn = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true, 0);
  917. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  918. pData->event.cvSourcePorts = pData->client->createCVSourcePorts();
  919. #endif
  920. fEvents.eventInputs = new carla_v3_input_event_list;
  921. }
  922. if (needsCtrlOut)
  923. {
  924. portName.clear();
  925. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  926. {
  927. portName = pData->name;
  928. portName += ":";
  929. }
  930. portName += "events-out";
  931. portName.truncate(portNameSize);
  932. pData->event.portOut = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false, 0);
  933. fEvents.eventOutputs = new carla_v3_output_event_list;
  934. }
  935. // plugin hints
  936. const PluginCategory v3category = getPluginCategoryFromV3SubCategories(fV3ClassInfo.v2.sub_categories);
  937. pData->hints = 0x0;
  938. if (v3category == PLUGIN_CATEGORY_SYNTH)
  939. pData->hints |= PLUGIN_IS_SYNTH;
  940. if (fV3.view != nullptr &&
  941. v3_cpp_obj(fV3.view)->is_platform_type_supported(fV3.view, V3_VIEW_PLATFORM_TYPE_NATIVE) == V3_TRUE)
  942. {
  943. pData->hints |= PLUGIN_HAS_CUSTOM_UI;
  944. pData->hints |= PLUGIN_HAS_CUSTOM_EMBED_UI;
  945. }
  946. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  947. pData->hints |= PLUGIN_CAN_DRYWET;
  948. if (aOuts > 0)
  949. pData->hints |= PLUGIN_CAN_VOLUME;
  950. if (aOuts >= 2 && aOuts % 2 == 0)
  951. pData->hints |= PLUGIN_CAN_BALANCE;
  952. // extra plugin hints
  953. pData->extraHints = 0x0;
  954. if (numEventInputBuses > 0)
  955. pData->extraHints |= PLUGIN_EXTRA_HINT_HAS_MIDI_IN;
  956. if (numEventOutputBuses > 0)
  957. pData->extraHints |= PLUGIN_EXTRA_HINT_HAS_MIDI_OUT;
  958. // check initial latency
  959. if ((fLastKnownLatency = v3_cpp_obj(fV3.processor)->get_latency_samples(fV3.processor)) != 0)
  960. {
  961. pData->client->setLatency(fLastKnownLatency);
  962. #ifndef BUILD_BRIDGE
  963. pData->latency.recreateBuffers(std::max(aIns+cvIns, aOuts+cvOuts), fLastKnownLatency);
  964. #endif
  965. }
  966. // initial audio setup
  967. v3_process_setup setup = {
  968. pData->engine->isOffline() ? V3_OFFLINE : V3_REALTIME,
  969. V3_SAMPLE_32,
  970. static_cast<int32_t>(pData->engine->getBufferSize()),
  971. pData->engine->getSampleRate()
  972. };
  973. v3_cpp_obj(fV3.processor)->setup_processing(fV3.processor, &setup);
  974. // activate all buses
  975. for (int32_t j=0; j<numAudioInputBuses; ++j)
  976. {
  977. v3_bus_info busInfo = {};
  978. CARLA_SAFE_ASSERT_BREAK(v3_cpp_obj(fV3.component)->get_bus_info(fV3.component, V3_AUDIO, V3_INPUT, j, &busInfo) == V3_OK);
  979. if ((busInfo.flags & V3_DEFAULT_ACTIVE) == 0x0) {
  980. CARLA_SAFE_ASSERT_BREAK(v3_cpp_obj(fV3.component)->activate_bus(fV3.component, V3_AUDIO, V3_INPUT, j, true) == V3_OK);
  981. }
  982. }
  983. for (int32_t j=0; j<numAudioOutputBuses; ++j)
  984. {
  985. v3_bus_info busInfo = {};
  986. CARLA_SAFE_ASSERT_BREAK(v3_cpp_obj(fV3.component)->get_bus_info(fV3.component, V3_AUDIO, V3_OUTPUT, j, &busInfo) == V3_OK);
  987. if ((busInfo.flags & V3_DEFAULT_ACTIVE) == 0x0) {
  988. CARLA_SAFE_ASSERT_BREAK(v3_cpp_obj(fV3.component)->activate_bus(fV3.component, V3_AUDIO, V3_OUTPUT, j, true) == V3_OK);
  989. }
  990. }
  991. bufferSizeChanged(pData->engine->getBufferSize());
  992. reloadPrograms(true);
  993. if (pData->active)
  994. activate();
  995. carla_debug("CarlaPluginVST3::reload() - end");
  996. }
  997. // -------------------------------------------------------------------
  998. // Plugin processing
  999. void activate() noexcept override
  1000. {
  1001. CARLA_SAFE_ASSERT_RETURN(fV3.component != nullptr,);
  1002. CARLA_SAFE_ASSERT_RETURN(fV3.processor != nullptr,);
  1003. try {
  1004. v3_cpp_obj(fV3.component)->set_active(fV3.component, true);
  1005. } CARLA_SAFE_EXCEPTION("set_active on");
  1006. try {
  1007. v3_cpp_obj(fV3.processor)->set_processing(fV3.processor, true);
  1008. } CARLA_SAFE_EXCEPTION("set_processing on");
  1009. fFirstActive = true;
  1010. }
  1011. void deactivate() noexcept override
  1012. {
  1013. CARLA_SAFE_ASSERT_RETURN(fV3.component != nullptr,);
  1014. CARLA_SAFE_ASSERT_RETURN(fV3.processor != nullptr,);
  1015. try {
  1016. v3_cpp_obj(fV3.processor)->set_processing(fV3.processor, false);
  1017. } CARLA_SAFE_EXCEPTION("set_processing off");
  1018. try {
  1019. v3_cpp_obj(fV3.component)->set_active(fV3.component, false);
  1020. } CARLA_SAFE_EXCEPTION("set_active off");
  1021. }
  1022. void process(const float* const* const audioIn, float** const audioOut,
  1023. const float* const* const cvIn, float** const cvOut, const uint32_t frames) override
  1024. {
  1025. // --------------------------------------------------------------------------------------------------------
  1026. // Check if active
  1027. if (! pData->active)
  1028. {
  1029. // disable any output sound
  1030. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1031. carla_zeroFloats(audioOut[i], frames);
  1032. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  1033. carla_zeroFloats(cvOut[i], frames);
  1034. return;
  1035. }
  1036. fEvents.init();
  1037. // --------------------------------------------------------------------------------------------------------
  1038. // Check if needs reset
  1039. if (pData->needsReset)
  1040. {
  1041. /*
  1042. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1043. {
  1044. }
  1045. else
  1046. */
  1047. if (pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS && fEvents.eventInputs != nullptr)
  1048. {
  1049. fEvents.eventInputs->numEvents = MAX_MIDI_NOTE;
  1050. for (uint8_t i=0; i < MAX_MIDI_NOTE; ++i)
  1051. {
  1052. v3_event& event(fEvents.eventInputs->events[i]);
  1053. carla_zeroStruct(event);
  1054. event.type = V3_EVENT_NOTE_OFF;
  1055. event.note_off.channel = (pData->ctrlChannel & MIDI_CHANNEL_BIT);
  1056. event.note_off.pitch = i;
  1057. }
  1058. }
  1059. pData->needsReset = false;
  1060. }
  1061. // --------------------------------------------------------------------------------------------------------
  1062. // Set TimeInfo
  1063. const EngineTimeInfo timeInfo(pData->engine->getTimeInfo());
  1064. fV3TimeContext.state = V3_PROCESS_CTX_PROJECT_TIME_VALID | V3_PROCESS_CTX_CONT_TIME_VALID;
  1065. fV3TimeContext.sample_rate = pData->engine->getSampleRate();
  1066. fV3TimeContext.project_time_in_samples = fV3TimeContext.continuous_time_in_samples
  1067. = static_cast<int64_t>(timeInfo.frame);
  1068. if (fFirstActive || ! fLastTimeInfo.compareIgnoringRollingFrames(timeInfo, frames))
  1069. fLastTimeInfo = timeInfo;
  1070. if (timeInfo.playing)
  1071. fV3TimeContext.state |= V3_PROCESS_CTX_PLAYING;
  1072. if (timeInfo.usecs != 0)
  1073. {
  1074. fV3TimeContext.system_time_ns = static_cast<int64_t>(timeInfo.usecs / 1000);
  1075. fV3TimeContext.state |= V3_PROCESS_CTX_SYSTEM_TIME_VALID;
  1076. }
  1077. if (timeInfo.bbt.valid)
  1078. {
  1079. CARLA_SAFE_ASSERT_INT(timeInfo.bbt.bar > 0, timeInfo.bbt.bar);
  1080. CARLA_SAFE_ASSERT_INT(timeInfo.bbt.beat > 0, timeInfo.bbt.beat);
  1081. const double ppqBar = static_cast<double>(timeInfo.bbt.beatsPerBar) * (timeInfo.bbt.bar - 1);
  1082. // const double ppqBeat = static_cast<double>(timeInfo.bbt.beat - 1);
  1083. // const double ppqTick = timeInfo.bbt.tick / timeInfo.bbt.ticksPerBeat;
  1084. // PPQ Pos
  1085. fV3TimeContext.project_time_quarters = static_cast<double>(timeInfo.frame) / (fV3TimeContext.sample_rate * 60 / timeInfo.bbt.beatsPerMinute);
  1086. // fTimeInfo.project_time_quarters = ppqBar + ppqBeat + ppqTick;
  1087. fV3TimeContext.state |= V3_PROCESS_CTX_PROJECT_TIME_VALID;
  1088. // Tempo
  1089. fV3TimeContext.bpm = timeInfo.bbt.beatsPerMinute;
  1090. fV3TimeContext.state |= V3_PROCESS_CTX_TEMPO_VALID;
  1091. // Bars
  1092. fV3TimeContext.bar_position_quarters = ppqBar;
  1093. fV3TimeContext.state |= V3_PROCESS_CTX_BAR_POSITION_VALID;
  1094. // Time Signature
  1095. fV3TimeContext.time_sig_numerator = static_cast<int32_t>(timeInfo.bbt.beatsPerBar + 0.5f);
  1096. fV3TimeContext.time_sig_denom = static_cast<int32_t>(timeInfo.bbt.beatType + 0.5f);
  1097. fV3TimeContext.state |= V3_PROCESS_CTX_TIME_SIG_VALID;
  1098. }
  1099. else
  1100. {
  1101. // Tempo
  1102. fV3TimeContext.bpm = 120.0;
  1103. fV3TimeContext.state |= V3_PROCESS_CTX_TEMPO_VALID;
  1104. // Time Signature
  1105. fV3TimeContext.time_sig_numerator = 4;
  1106. fV3TimeContext.time_sig_denom = 4;
  1107. fV3TimeContext.state |= V3_PROCESS_CTX_TIME_SIG_VALID;
  1108. // Missing info
  1109. fV3TimeContext.project_time_quarters = 0.0;
  1110. fV3TimeContext.bar_position_quarters = 0.0;
  1111. }
  1112. // --------------------------------------------------------------------------------------------------------
  1113. // Event Input and Processing
  1114. if (pData->event.portIn != nullptr)
  1115. {
  1116. // ----------------------------------------------------------------------------------------------------
  1117. // MIDI Input (External)
  1118. if (fEvents.eventInputs != nullptr && pData->extNotes.mutex.tryLock())
  1119. {
  1120. ExternalMidiNote note = { 0, 0, 0 };
  1121. uint16_t numEvents = fEvents.eventInputs->numEvents;
  1122. for (; numEvents < kPluginMaxMidiEvents && ! pData->extNotes.data.isEmpty();)
  1123. {
  1124. note = pData->extNotes.data.getFirst(note, true);
  1125. CARLA_SAFE_ASSERT_CONTINUE(note.channel >= 0 && note.channel < MAX_MIDI_CHANNELS);
  1126. v3_event& event(fEvents.eventInputs->events[numEvents++]);
  1127. carla_zeroStruct(event);
  1128. if (note.velo > 0)
  1129. {
  1130. event.type = V3_EVENT_NOTE_ON;
  1131. event.note_on.channel = (note.channel & MIDI_CHANNEL_BIT);
  1132. event.note_on.pitch = note.note;
  1133. event.note_on.velocity = static_cast<float>(note.velo) / 127.f;
  1134. }
  1135. else
  1136. {
  1137. event.type = V3_EVENT_NOTE_OFF;
  1138. event.note_off.channel = (note.channel & MIDI_CHANNEL_BIT);
  1139. event.note_off.pitch = note.note;
  1140. }
  1141. }
  1142. pData->extNotes.mutex.unlock();
  1143. fEvents.eventInputs->numEvents = numEvents;
  1144. } // End of MIDI Input (External)
  1145. // ----------------------------------------------------------------------------------------------------
  1146. // Event Input (System)
  1147. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1148. bool allNotesOffSent = false;
  1149. #endif
  1150. bool isSampleAccurate = (pData->options & PLUGIN_OPTION_FIXED_BUFFERS) == 0;
  1151. uint32_t startTime = 0;
  1152. uint32_t timeOffset = 0;
  1153. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1154. if (cvIn != nullptr && pData->event.cvSourcePorts != nullptr)
  1155. pData->event.cvSourcePorts->initPortBuffers(cvIn, frames, isSampleAccurate, pData->event.portIn);
  1156. #endif
  1157. for (uint32_t i=0, numEvents = pData->event.portIn->getEventCount(); i < numEvents; ++i)
  1158. {
  1159. EngineEvent& event(pData->event.portIn->getEvent(i));
  1160. uint32_t eventTime = event.time;
  1161. CARLA_SAFE_ASSERT_UINT2_CONTINUE(eventTime < frames, eventTime, frames);
  1162. if (eventTime < timeOffset)
  1163. {
  1164. carla_stderr2("Timing error, eventTime:%u < timeOffset:%u for '%s'",
  1165. eventTime, timeOffset, pData->name);
  1166. eventTime = timeOffset;
  1167. }
  1168. if (isSampleAccurate && eventTime > timeOffset)
  1169. {
  1170. if (processSingle(audioIn, audioOut, eventTime - timeOffset, timeOffset))
  1171. {
  1172. startTime = 0;
  1173. timeOffset = eventTime;
  1174. // TODO
  1175. }
  1176. else
  1177. {
  1178. startTime += timeOffset;
  1179. }
  1180. }
  1181. switch (event.type)
  1182. {
  1183. case kEngineEventTypeNull:
  1184. break;
  1185. case kEngineEventTypeControl: {
  1186. EngineControlEvent& ctrlEvent(event.ctrl);
  1187. switch (ctrlEvent.type)
  1188. {
  1189. case kEngineControlEventTypeNull:
  1190. break;
  1191. case kEngineControlEventTypeParameter: {
  1192. float value;
  1193. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1194. // non-midi
  1195. if (event.channel == kEngineEventNonMidiChannel)
  1196. {
  1197. const uint32_t k = ctrlEvent.param;
  1198. CARLA_SAFE_ASSERT_CONTINUE(k < pData->param.count);
  1199. ctrlEvent.handled = true;
  1200. value = pData->param.getFinalUnnormalizedValue(k, ctrlEvent.normalizedValue);
  1201. setParameterValueRT(k, value, event.time, true);
  1202. continue;
  1203. }
  1204. // Control backend stuff
  1205. if (event.channel == pData->ctrlChannel)
  1206. {
  1207. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_DRYWET) != 0)
  1208. {
  1209. ctrlEvent.handled = true;
  1210. value = ctrlEvent.normalizedValue;
  1211. setDryWetRT(value, true);
  1212. }
  1213. else if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_VOLUME) != 0)
  1214. {
  1215. ctrlEvent.handled = true;
  1216. value = ctrlEvent.normalizedValue*127.0f/100.0f;
  1217. setVolumeRT(value, true);
  1218. }
  1219. else if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_BALANCE) != 0)
  1220. {
  1221. float left, right;
  1222. value = ctrlEvent.normalizedValue/0.5f - 1.0f;
  1223. if (value < 0.0f)
  1224. {
  1225. left = -1.0f;
  1226. right = (value*2.0f)+1.0f;
  1227. }
  1228. else if (value > 0.0f)
  1229. {
  1230. left = (value*2.0f)-1.0f;
  1231. right = 1.0f;
  1232. }
  1233. else
  1234. {
  1235. left = -1.0f;
  1236. right = 1.0f;
  1237. }
  1238. ctrlEvent.handled = true;
  1239. setBalanceLeftRT(left, true);
  1240. setBalanceRightRT(right, true);
  1241. }
  1242. }
  1243. #endif
  1244. // Control plugin parameters
  1245. uint32_t k;
  1246. for (k=0; k < pData->param.count; ++k)
  1247. {
  1248. if (pData->param.data[k].midiChannel != event.channel)
  1249. continue;
  1250. if (pData->param.data[k].mappedControlIndex != ctrlEvent.param)
  1251. continue;
  1252. if (pData->param.data[k].type != PARAMETER_INPUT)
  1253. continue;
  1254. if ((pData->param.data[k].hints & PARAMETER_IS_AUTOMATABLE) == 0)
  1255. continue;
  1256. ctrlEvent.handled = true;
  1257. value = pData->param.getFinalUnnormalizedValue(k, ctrlEvent.normalizedValue);
  1258. setParameterValueRT(k, value, event.time, true);
  1259. }
  1260. if ((pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0 && ctrlEvent.param < MAX_MIDI_VALUE)
  1261. {
  1262. // TODO
  1263. }
  1264. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1265. if (! ctrlEvent.handled)
  1266. checkForMidiLearn(event);
  1267. #endif
  1268. break;
  1269. } // case kEngineControlEventTypeParameter
  1270. case kEngineControlEventTypeMidiBank:
  1271. if ((pData->options & PLUGIN_OPTION_SEND_PROGRAM_CHANGES) != 0)
  1272. {
  1273. // TODO
  1274. }
  1275. break;
  1276. case kEngineControlEventTypeMidiProgram:
  1277. if (event.channel == pData->ctrlChannel && (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  1278. {
  1279. if (ctrlEvent.param < pData->prog.count)
  1280. {
  1281. setProgramRT(ctrlEvent.param, true);
  1282. break;
  1283. }
  1284. }
  1285. else if (pData->options & PLUGIN_OPTION_SEND_PROGRAM_CHANGES)
  1286. {
  1287. // TODO
  1288. }
  1289. break;
  1290. case kEngineControlEventTypeAllSoundOff:
  1291. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1292. {
  1293. // TODO
  1294. }
  1295. break;
  1296. case kEngineControlEventTypeAllNotesOff:
  1297. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1298. {
  1299. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1300. if (event.channel == pData->ctrlChannel && ! allNotesOffSent)
  1301. {
  1302. allNotesOffSent = true;
  1303. postponeRtAllNotesOff();
  1304. }
  1305. #endif
  1306. // TODO
  1307. }
  1308. break;
  1309. } // switch (ctrlEvent.type)
  1310. break;
  1311. } // case kEngineEventTypeControl
  1312. case kEngineEventTypeMidi: {
  1313. const EngineMidiEvent& midiEvent(event.midi);
  1314. if (midiEvent.size > 3)
  1315. continue;
  1316. #ifdef CARLA_PROPER_CPP11_SUPPORT
  1317. static_assert(3 <= EngineMidiEvent::kDataSize, "Incorrect data");
  1318. #endif
  1319. uint8_t status = uint8_t(MIDI_GET_STATUS_FROM_DATA(midiEvent.data));
  1320. if ((status == MIDI_STATUS_NOTE_OFF || status == MIDI_STATUS_NOTE_ON) && (pData->options & PLUGIN_OPTION_SKIP_SENDING_NOTES))
  1321. continue;
  1322. if (status == MIDI_STATUS_CHANNEL_PRESSURE && (pData->options & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) == 0)
  1323. continue;
  1324. if (status == MIDI_STATUS_CONTROL_CHANGE && (pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) == 0)
  1325. continue;
  1326. if (status == MIDI_STATUS_POLYPHONIC_AFTERTOUCH && (pData->options & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) == 0)
  1327. continue;
  1328. if (status == MIDI_STATUS_PITCH_WHEEL_CONTROL && (pData->options & PLUGIN_OPTION_SEND_PITCHBEND) == 0)
  1329. continue;
  1330. // Fix bad note-off
  1331. if (status == MIDI_STATUS_NOTE_ON && midiEvent.data[2] == 0)
  1332. status = MIDI_STATUS_NOTE_OFF;
  1333. // TODO
  1334. if (status == MIDI_STATUS_NOTE_ON)
  1335. {
  1336. pData->postponeNoteOnRtEvent(true, event.channel, midiEvent.data[1], midiEvent.data[2]);
  1337. }
  1338. else if (status == MIDI_STATUS_NOTE_OFF)
  1339. {
  1340. pData->postponeNoteOffRtEvent(true, event.channel, midiEvent.data[1]);
  1341. }
  1342. } break;
  1343. } // switch (event.type)
  1344. }
  1345. pData->postRtEvents.trySplice();
  1346. if (frames > timeOffset)
  1347. processSingle(audioIn, audioOut, frames - timeOffset, timeOffset);
  1348. } // End of Event Input and Processing
  1349. // --------------------------------------------------------------------------------------------------------
  1350. // Plugin processing (no events)
  1351. else
  1352. {
  1353. processSingle(audioIn, audioOut, frames, 0);
  1354. } // End of Plugin processing (no events)
  1355. // --------------------------------------------------------------------------------------------------------
  1356. // MIDI Output
  1357. if (pData->event.portOut != nullptr)
  1358. {
  1359. // TODO
  1360. } // End of MIDI Output
  1361. fFirstActive = false;
  1362. // --------------------------------------------------------------------------------------------------------
  1363. }
  1364. bool processSingle(const float* const* const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t timeOffset)
  1365. {
  1366. CARLA_SAFE_ASSERT_RETURN(frames > 0, false);
  1367. if (pData->audioIn.count > 0)
  1368. {
  1369. CARLA_SAFE_ASSERT_RETURN(inBuffer != nullptr, false);
  1370. }
  1371. if (pData->audioOut.count > 0)
  1372. {
  1373. CARLA_SAFE_ASSERT_RETURN(outBuffer != nullptr, false);
  1374. CARLA_SAFE_ASSERT_RETURN(fAudioAndCvOutBuffers != nullptr, false);
  1375. }
  1376. // --------------------------------------------------------------------------------------------------------
  1377. // Try lock, silence otherwise
  1378. #ifndef STOAT_TEST_BUILD
  1379. if (pData->engine->isOffline())
  1380. {
  1381. pData->singleMutex.lock();
  1382. }
  1383. else
  1384. #endif
  1385. if (! pData->singleMutex.tryLock())
  1386. {
  1387. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1388. {
  1389. for (uint32_t k=0; k < frames; ++k)
  1390. outBuffer[i][k+timeOffset] = 0.0f;
  1391. }
  1392. return false;
  1393. }
  1394. // --------------------------------------------------------------------------------------------------------
  1395. // Set audio buffers
  1396. float* bufferAudioIn[std::max(1u, pData->audioIn.count + pData->cvIn.count)];
  1397. float* bufferAudioOut[std::max(1u, pData->audioOut.count + pData->cvOut.count)];
  1398. {
  1399. uint32_t i=0;
  1400. for (; i < pData->audioIn.count; ++i)
  1401. bufferAudioIn[i] = const_cast<float*>(inBuffer[i]+timeOffset);
  1402. for (; i < pData->cvIn.count; ++i)
  1403. bufferAudioIn[i] = const_cast<float*>(inBuffer[i]+timeOffset);
  1404. }
  1405. {
  1406. uint32_t i=0;
  1407. for (; i < pData->audioOut.count; ++i)
  1408. bufferAudioOut[i] = fAudioAndCvOutBuffers[i]+timeOffset;
  1409. for (; i < pData->cvOut.count; ++i)
  1410. bufferAudioOut[i] = fAudioAndCvOutBuffers[i]+timeOffset;
  1411. }
  1412. for (uint32_t i=0; i < pData->audioOut.count + pData->cvOut.count; ++i)
  1413. carla_zeroFloats(fAudioAndCvOutBuffers[i], frames);
  1414. // --------------------------------------------------------------------------------------------------------
  1415. // Set MIDI events
  1416. // TODO
  1417. // --------------------------------------------------------------------------------------------------------
  1418. // Run plugin
  1419. fEvents.prepare();
  1420. v3_audio_bus_buffers processInputs = {
  1421. static_cast<int32_t>(pData->audioIn.count + pData->cvIn.count),
  1422. 0, { bufferAudioIn }
  1423. };
  1424. v3_audio_bus_buffers processOutputs = {
  1425. static_cast<int32_t>(pData->audioOut.count + pData->cvOut.count),
  1426. 0, { bufferAudioOut }
  1427. };
  1428. v3_process_data processData = {
  1429. pData->engine->isOffline() ? V3_OFFLINE : V3_REALTIME,
  1430. V3_SAMPLE_32,
  1431. static_cast<int32_t>(frames),
  1432. static_cast<int32_t>(pData->audioIn.count + pData->cvIn.count),
  1433. static_cast<int32_t>(pData->audioOut.count + pData->cvOut.count),
  1434. &processInputs,
  1435. &processOutputs,
  1436. fEvents.paramInputs != nullptr ? (v3_param_changes**)&fEvents.paramInputs : nullptr,
  1437. fEvents.paramOutputs != nullptr ? (v3_param_changes**)&fEvents.paramOutputs : nullptr,
  1438. fEvents.eventInputs != nullptr ? (v3_event_list**)&fEvents.eventInputs : nullptr,
  1439. fEvents.eventOutputs != nullptr ? (v3_event_list**)&fEvents.eventOutputs : nullptr,
  1440. &fV3TimeContext
  1441. };
  1442. try {
  1443. v3_cpp_obj(fV3.processor)->process(fV3.processor, &processData);
  1444. } CARLA_SAFE_EXCEPTION("process");
  1445. fEvents.init();
  1446. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1447. // --------------------------------------------------------------------------------------------------------
  1448. // Post-processing (dry/wet, volume and balance)
  1449. {
  1450. const bool doDryWet = (pData->hints & PLUGIN_CAN_DRYWET) != 0 && carla_isNotEqual(pData->postProc.dryWet, 1.0f);
  1451. const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) != 0 && ! (carla_isEqual(pData->postProc.balanceLeft, -1.0f) && carla_isEqual(pData->postProc.balanceRight, 1.0f));
  1452. const bool isMono = (pData->audioIn.count == 1);
  1453. bool isPair;
  1454. float bufValue, oldBufLeft[doBalance ? frames : 1];
  1455. uint32_t i=0;
  1456. for (; i < pData->audioOut.count; ++i)
  1457. {
  1458. // Dry/Wet
  1459. if (doDryWet)
  1460. {
  1461. const uint32_t c = isMono ? 0 : i;
  1462. for (uint32_t k=0; k < frames; ++k)
  1463. {
  1464. bufValue = inBuffer[c][k+timeOffset];
  1465. fAudioAndCvOutBuffers[i][k] = (fAudioAndCvOutBuffers[i][k] * pData->postProc.dryWet) + (bufValue * (1.0f - pData->postProc.dryWet));
  1466. }
  1467. }
  1468. // Balance
  1469. if (doBalance)
  1470. {
  1471. isPair = (i % 2 == 0);
  1472. if (isPair)
  1473. {
  1474. CARLA_ASSERT(i+1 < pData->audioOut.count);
  1475. carla_copyFloats(oldBufLeft, fAudioAndCvOutBuffers[i], frames);
  1476. }
  1477. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  1478. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  1479. for (uint32_t k=0; k < frames; ++k)
  1480. {
  1481. if (isPair)
  1482. {
  1483. // left
  1484. fAudioAndCvOutBuffers[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  1485. fAudioAndCvOutBuffers[i][k] += fAudioAndCvOutBuffers[i+1][k] * (1.0f - balRangeR);
  1486. }
  1487. else
  1488. {
  1489. // right
  1490. fAudioAndCvOutBuffers[i][k] = fAudioAndCvOutBuffers[i][k] * balRangeR;
  1491. fAudioAndCvOutBuffers[i][k] += oldBufLeft[k] * balRangeL;
  1492. }
  1493. }
  1494. }
  1495. // Volume (and buffer copy)
  1496. {
  1497. for (uint32_t k=0; k < frames; ++k)
  1498. outBuffer[i][k+timeOffset] = fAudioAndCvOutBuffers[i][k] * pData->postProc.volume;
  1499. }
  1500. }
  1501. for (; i < pData->cvOut.count; ++i)
  1502. carla_copyFloats(outBuffer[i] + timeOffset, fAudioAndCvOutBuffers[i] + timeOffset, frames);
  1503. } // End of Post-processing
  1504. #else // BUILD_BRIDGE_ALTERNATIVE_ARCH
  1505. for (uint32_t i=0; i < pData->audioOut.count + pData->cvOut.count; ++i)
  1506. carla_copyFloats(outBuffer[i] + timeOffset, fAudioAndCvOutBuffers[i] + timeOffset, frames);
  1507. #endif
  1508. // --------------------------------------------------------------------------------------------------------
  1509. pData->singleMutex.unlock();
  1510. return true;
  1511. }
  1512. void bufferSizeChanged(const uint32_t newBufferSize) override
  1513. {
  1514. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  1515. carla_debug("CarlaPluginVST2::bufferSizeChanged(%i)", newBufferSize);
  1516. if (pData->active)
  1517. deactivate();
  1518. for (uint32_t i=0; i < pData->audioOut.count + pData->cvOut.count; ++i)
  1519. {
  1520. if (fAudioAndCvOutBuffers[i] != nullptr)
  1521. delete[] fAudioAndCvOutBuffers[i];
  1522. fAudioAndCvOutBuffers[i] = new float[newBufferSize];
  1523. }
  1524. v3_process_setup setup = {
  1525. pData->engine->isOffline() ? V3_OFFLINE : V3_REALTIME,
  1526. V3_SAMPLE_32,
  1527. static_cast<int32_t>(newBufferSize),
  1528. pData->engine->getSampleRate()
  1529. };
  1530. v3_cpp_obj(fV3.processor)->setup_processing(fV3.processor, &setup);
  1531. if (pData->active)
  1532. activate();
  1533. }
  1534. void sampleRateChanged(const double newSampleRate) override
  1535. {
  1536. CARLA_ASSERT_INT(newSampleRate > 0.0, newSampleRate);
  1537. carla_debug("CarlaPluginVST3::sampleRateChanged(%g)", newSampleRate);
  1538. if (pData->active)
  1539. deactivate();
  1540. v3_process_setup setup = {
  1541. pData->engine->isOffline() ? V3_OFFLINE : V3_REALTIME,
  1542. V3_SAMPLE_32,
  1543. static_cast<int32_t>(pData->engine->getBufferSize()),
  1544. newSampleRate
  1545. };
  1546. v3_cpp_obj(fV3.processor)->setup_processing(fV3.processor, &setup);
  1547. if (pData->active)
  1548. activate();
  1549. }
  1550. void offlineModeChanged(const bool isOffline) override
  1551. {
  1552. carla_debug("CarlaPluginVST3::offlineModeChanged(%d)", isOffline);
  1553. if (pData->active)
  1554. deactivate();
  1555. v3_process_setup setup = {
  1556. isOffline ? V3_OFFLINE : V3_REALTIME,
  1557. V3_SAMPLE_32,
  1558. static_cast<int32_t>(pData->engine->getBufferSize()),
  1559. pData->engine->getSampleRate()
  1560. };
  1561. v3_cpp_obj(fV3.processor)->setup_processing(fV3.processor, &setup);
  1562. if (pData->active)
  1563. activate();
  1564. }
  1565. // -------------------------------------------------------------------
  1566. // Plugin buffers
  1567. void clearBuffers() noexcept override
  1568. {
  1569. carla_debug("CarlaPluginVST2::clearBuffers() - start");
  1570. if (fAudioAndCvOutBuffers != nullptr)
  1571. {
  1572. for (uint32_t i=0; i < pData->audioOut.count + pData->cvOut.count; ++i)
  1573. {
  1574. if (fAudioAndCvOutBuffers[i] != nullptr)
  1575. {
  1576. delete[] fAudioAndCvOutBuffers[i];
  1577. fAudioAndCvOutBuffers[i] = nullptr;
  1578. }
  1579. }
  1580. delete[] fAudioAndCvOutBuffers;
  1581. fAudioAndCvOutBuffers = nullptr;
  1582. }
  1583. CarlaPlugin::clearBuffers();
  1584. carla_debug("CarlaPluginVST2::clearBuffers() - end");
  1585. }
  1586. // -------------------------------------------------------------------
  1587. // Post-poned UI Stuff
  1588. void uiParameterChange(const uint32_t index, const float value) noexcept override
  1589. {
  1590. CARLA_SAFE_ASSERT_RETURN(fV3.controller != nullptr,);
  1591. CARLA_SAFE_ASSERT_RETURN(index < pData->param.count,);
  1592. const double normalized = v3_cpp_obj(fV3.controller)->plain_parameter_to_normalised(fV3.controller, index, value);
  1593. v3_cpp_obj(fV3.controller)->set_parameter_normalised(fV3.controller, index, normalized);
  1594. }
  1595. // ----------------------------------------------------------------------------------------------------------------
  1596. const void* getNativeDescriptor() const noexcept override
  1597. {
  1598. return fV3.component;
  1599. }
  1600. const void* getExtraStuff() const noexcept override
  1601. {
  1602. return fV3.controller;
  1603. }
  1604. // ----------------------------------------------------------------------------------------------------------------
  1605. bool init(const CarlaPluginPtr plugin,
  1606. const char* const filename,
  1607. const char* name,
  1608. const char* /*const label*/,
  1609. const uint options)
  1610. {
  1611. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  1612. // ------------------------------------------------------------------------------------------------------------
  1613. // first checks
  1614. if (pData->client != nullptr)
  1615. {
  1616. pData->engine->setLastError("Plugin client is already registered");
  1617. return false;
  1618. }
  1619. if (filename == nullptr || filename[0] == '\0')
  1620. {
  1621. pData->engine->setLastError("null filename");
  1622. return false;
  1623. }
  1624. V3_ENTRYFN v3_entry;
  1625. V3_EXITFN v3_exit;
  1626. V3_GETFN v3_get;
  1627. // filename is full path to binary
  1628. if (water::File(filename).existsAsFile())
  1629. {
  1630. if (! pData->libOpen(filename))
  1631. {
  1632. pData->engine->setLastError(pData->libError(filename));
  1633. return false;
  1634. }
  1635. v3_entry = pData->libSymbol<V3_ENTRYFN>(V3_ENTRYFNNAME);
  1636. v3_exit = pData->libSymbol<V3_EXITFN>(V3_EXITFNNAME);
  1637. v3_get = pData->libSymbol<V3_GETFN>(V3_GETFNNAME);
  1638. }
  1639. // assume filename is a vst3 bundle
  1640. else
  1641. {
  1642. #ifdef CARLA_OS_MAC
  1643. if (! fMacBundleLoader.load(filename))
  1644. {
  1645. pData->engine->setLastError("Failed to load VST3 bundle executable");
  1646. return false;
  1647. }
  1648. v3_entry = fMacBundleLoader.getSymbol<V3_ENTRYFN>(CFSTR(V3_ENTRYFNNAME));
  1649. v3_exit = fMacBundleLoader.getSymbol<V3_EXITFN>(CFSTR(V3_EXITFNNAME));
  1650. v3_get = fMacBundleLoader.getSymbol<V3_GETFN>(CFSTR(V3_GETFNNAME));
  1651. #else
  1652. water::String binaryfilename = filename;
  1653. if (!binaryfilename.endsWithChar(CARLA_OS_SEP))
  1654. binaryfilename += CARLA_OS_SEP_STR;
  1655. binaryfilename += "Contents" CARLA_OS_SEP_STR V3_CONTENT_DIR CARLA_OS_SEP_STR;
  1656. binaryfilename += water::File(filename).getFileNameWithoutExtension();
  1657. #ifdef CARLA_OS_WIN
  1658. binaryfilename += ".vst3";
  1659. #else
  1660. binaryfilename += ".so";
  1661. #endif
  1662. if (! water::File(binaryfilename).existsAsFile())
  1663. {
  1664. pData->engine->setLastError("Failed to find a suitable VST3 bundle binary");
  1665. return false;
  1666. }
  1667. if (! pData->libOpen(binaryfilename.toRawUTF8()))
  1668. {
  1669. pData->engine->setLastError(pData->libError(binaryfilename.toRawUTF8()));
  1670. return false;
  1671. }
  1672. v3_entry = pData->libSymbol<V3_ENTRYFN>(V3_ENTRYFNNAME);
  1673. v3_exit = pData->libSymbol<V3_EXITFN>(V3_EXITFNNAME);
  1674. v3_get = pData->libSymbol<V3_GETFN>(V3_GETFNNAME);
  1675. #endif
  1676. }
  1677. // ------------------------------------------------------------------------------------------------------------
  1678. // ensure entry and exit points are available
  1679. if (v3_entry == nullptr || v3_exit == nullptr || v3_get == nullptr)
  1680. {
  1681. pData->engine->setLastError("Not a VST3 plugin");
  1682. return false;
  1683. }
  1684. // ------------------------------------------------------------------------------------------------------------
  1685. // call entry point
  1686. #if defined(CARLA_OS_MAC)
  1687. v3_entry(pData->lib == nullptr ? fMacBundleLoader.getRef() : nullptr);
  1688. #elif defined(CARLA_OS_WIN)
  1689. v3_entry();
  1690. #else
  1691. v3_entry(pData->lib);
  1692. #endif
  1693. // ------------------------------------------------------------------------------------------------------------
  1694. // fetch initial factory
  1695. v3_plugin_factory** const factory = v3_get();
  1696. if (factory == nullptr)
  1697. {
  1698. pData->engine->setLastError("VST3 factory failed to create a valid instance");
  1699. return false;
  1700. }
  1701. // ------------------------------------------------------------------------------------------------------------
  1702. // initialize and find requested plugin
  1703. fV3.exitfn = v3_exit;
  1704. fV3.factory1 = factory;
  1705. if (! fV3.queryFactories(getHostContext()))
  1706. {
  1707. pData->engine->setLastError("VST3 plugin failed to properly create factories");
  1708. return false;
  1709. }
  1710. if (! fV3.findPlugin(fV3ClassInfo))
  1711. {
  1712. pData->engine->setLastError("Failed to find the requested plugin in the VST3 bundle");
  1713. return false;
  1714. }
  1715. if (! fV3.initializePlugin(fV3ClassInfo.v1.class_id, getHostContext()))
  1716. {
  1717. pData->engine->setLastError("VST3 plugin failed to initialize");
  1718. return false;
  1719. }
  1720. // ------------------------------------------------------------------------------------------------------------
  1721. // do some basic safety checks
  1722. if (v3_cpp_obj(fV3.processor)->can_process_sample_size(fV3.processor, V3_SAMPLE_32) != V3_OK)
  1723. {
  1724. pData->engine->setLastError("VST3 plugin does not support 32bit audio, cannot continue");
  1725. return false;
  1726. }
  1727. // ------------------------------------------------------------------------------------------------------------
  1728. // get info
  1729. if (name != nullptr && name[0] != '\0')
  1730. {
  1731. pData->name = pData->engine->getUniquePluginName(name);
  1732. }
  1733. else
  1734. {
  1735. if (fV3ClassInfo.v1.name[0] != '\0')
  1736. pData->name = pData->engine->getUniquePluginName(fV3ClassInfo.v1.name);
  1737. else if (const char* const shortname = std::strrchr(filename, CARLA_OS_SEP))
  1738. pData->name = pData->engine->getUniquePluginName(shortname+1);
  1739. else
  1740. pData->name = pData->engine->getUniquePluginName("unknown");
  1741. }
  1742. pData->filename = carla_strdup(filename);
  1743. // ------------------------------------------------------------------------------------------------------------
  1744. // register client
  1745. pData->client = pData->engine->addClient(plugin);
  1746. if (pData->client == nullptr || ! pData->client->isOk())
  1747. {
  1748. pData->engine->setLastError("Failed to register plugin client");
  1749. return false;
  1750. }
  1751. // ------------------------------------------------------------------------------------------------------------
  1752. // set default options
  1753. pData->options = 0x0;
  1754. if (fLastKnownLatency != 0 /*|| hasMidiOutput()*/ || isPluginOptionEnabled(options, PLUGIN_OPTION_FIXED_BUFFERS))
  1755. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  1756. if (isPluginOptionEnabled(options, PLUGIN_OPTION_USE_CHUNKS))
  1757. pData->options |= PLUGIN_OPTION_USE_CHUNKS;
  1758. /*
  1759. if (hasMidiInput())
  1760. {
  1761. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_CONTROL_CHANGES))
  1762. pData->options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  1763. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_CHANNEL_PRESSURE))
  1764. pData->options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  1765. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH))
  1766. pData->options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  1767. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_PITCHBEND))
  1768. pData->options |= PLUGIN_OPTION_SEND_PITCHBEND;
  1769. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_ALL_SOUND_OFF))
  1770. pData->options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  1771. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_PROGRAM_CHANGES))
  1772. pData->options |= PLUGIN_OPTION_SEND_PROGRAM_CHANGES;
  1773. if (isPluginOptionInverseEnabled(options, PLUGIN_OPTION_SKIP_SENDING_NOTES))
  1774. pData->options |= PLUGIN_OPTION_SKIP_SENDING_NOTES;
  1775. }
  1776. */
  1777. /*
  1778. if (numPrograms > 1 && (pData->options & PLUGIN_OPTION_SEND_PROGRAM_CHANGES) == 0)
  1779. if (isPluginOptionEnabled(options, PLUGIN_OPTION_MAP_PROGRAM_CHANGES))
  1780. pData->options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  1781. */
  1782. // ------------------------------------------------------------------------------------------------------------
  1783. return true;
  1784. }
  1785. protected:
  1786. void handlePluginUIClosed() override
  1787. {
  1788. // CARLA_SAFE_ASSERT_RETURN(fUI.window != nullptr,);
  1789. carla_debug("CarlaPluginVST3::handlePluginUIClosed()");
  1790. showCustomUI(false);
  1791. pData->engine->callback(true, true,
  1792. ENGINE_CALLBACK_UI_STATE_CHANGED,
  1793. pData->id,
  1794. 0,
  1795. 0, 0, 0.0f, nullptr);
  1796. }
  1797. void handlePluginUIResized(const uint width, const uint height) override
  1798. {
  1799. // CARLA_SAFE_ASSERT_RETURN(fUI.window != nullptr,);
  1800. carla_debug("CarlaPluginVST3::handlePluginUIResized(%u, %u)", width, height);
  1801. return; // unused
  1802. (void)width; (void)height;
  1803. }
  1804. private:
  1805. #ifdef CARLA_OS_MAC
  1806. BundleLoader fMacBundleLoader;
  1807. #endif
  1808. bool fFirstActive; // first process() call after activate()
  1809. float** fAudioAndCvOutBuffers;
  1810. uint32_t fLastKnownLatency;
  1811. EngineTimeInfo fLastTimeInfo;
  1812. v3_process_context fV3TimeContext;
  1813. CarlaScopedPointer<carla_v3_host_application> fV3Application;
  1814. inline v3_funknown** getHostContext() noexcept { return (v3_funknown**)&fV3Application; }
  1815. // v3_class_info_2 is ABI compatible with v3_class_info
  1816. union ClassInfo {
  1817. v3_class_info v1;
  1818. v3_class_info_2 v2;
  1819. } fV3ClassInfo;
  1820. struct Pointers {
  1821. V3_EXITFN exitfn;
  1822. v3_plugin_factory** factory1;
  1823. v3_plugin_factory_2** factory2;
  1824. v3_plugin_factory_3** factory3;
  1825. v3_component** component;
  1826. v3_edit_controller** controller;
  1827. v3_audio_processor** processor;
  1828. v3_plugin_view** view;
  1829. bool shouldTerminateComponent;
  1830. bool shouldTerminateController;
  1831. Pointers()
  1832. : exitfn(nullptr),
  1833. factory1(nullptr),
  1834. factory2(nullptr),
  1835. factory3(nullptr),
  1836. component(nullptr),
  1837. controller(nullptr),
  1838. processor(nullptr),
  1839. view(nullptr),
  1840. shouldTerminateComponent(false),
  1841. shouldTerminateController(false) {}
  1842. ~Pointers()
  1843. {
  1844. // must have been cleaned up by now
  1845. CARLA_SAFE_ASSERT(exitfn == nullptr);
  1846. }
  1847. // must have exitfn and factory1 set
  1848. bool queryFactories(v3_funknown** const hostContext)
  1849. {
  1850. // query 2nd factory
  1851. if (v3_cpp_obj_query_interface(factory1, v3_plugin_factory_2_iid, &factory2) == V3_OK)
  1852. {
  1853. CARLA_SAFE_ASSERT_RETURN(factory2 != nullptr, exit());
  1854. }
  1855. else
  1856. {
  1857. CARLA_SAFE_ASSERT(factory2 == nullptr);
  1858. factory2 = nullptr;
  1859. }
  1860. // query 3rd factory
  1861. if (factory2 != nullptr && v3_cpp_obj_query_interface(factory2, v3_plugin_factory_3_iid, &factory3) == V3_OK)
  1862. {
  1863. CARLA_SAFE_ASSERT_RETURN(factory3 != nullptr, exit());
  1864. }
  1865. else
  1866. {
  1867. CARLA_SAFE_ASSERT(factory3 == nullptr);
  1868. factory3 = nullptr;
  1869. }
  1870. // set host context (application) if 3rd factory provided
  1871. if (factory3 != nullptr)
  1872. v3_cpp_obj(factory3)->set_host_context(factory3, hostContext);
  1873. return true;
  1874. }
  1875. // must have all possible factories and exitfn set
  1876. bool findPlugin(ClassInfo& classInfo)
  1877. {
  1878. // get factory info
  1879. v3_factory_info factoryInfo = {};
  1880. CARLA_SAFE_ASSERT_RETURN(v3_cpp_obj(factory1)->get_factory_info(factory1, &factoryInfo) == V3_OK, exit());
  1881. // get num classes
  1882. const int32_t numClasses = v3_cpp_obj(factory1)->num_classes(factory1);
  1883. CARLA_SAFE_ASSERT_RETURN(numClasses > 0, exit());
  1884. // go through all relevant classes
  1885. for (int32_t i=0; i<numClasses; ++i)
  1886. {
  1887. carla_zeroStruct(classInfo);
  1888. if (factory2 != nullptr)
  1889. v3_cpp_obj(factory2)->get_class_info_2(factory2, i, &classInfo.v2);
  1890. else
  1891. v3_cpp_obj(factory1)->get_class_info(factory1, i, &classInfo.v1);
  1892. // safety check
  1893. CARLA_SAFE_ASSERT_CONTINUE(classInfo.v1.cardinality == 0x7FFFFFFF);
  1894. // only check for audio plugins
  1895. if (std::strcmp(classInfo.v1.category, "Audio Module Class") != 0)
  1896. continue;
  1897. // FIXME multi-plugin bundle
  1898. break;
  1899. }
  1900. return true;
  1901. }
  1902. bool initializePlugin(const v3_tuid uid, v3_funknown** const hostContext)
  1903. {
  1904. // create instance
  1905. void* instance = nullptr;
  1906. CARLA_SAFE_ASSERT_RETURN(v3_cpp_obj(factory1)->create_instance(factory1, uid, v3_component_iid,
  1907. &instance) == V3_OK,
  1908. exit());
  1909. CARLA_SAFE_ASSERT_RETURN(instance != nullptr, exit());
  1910. component = static_cast<v3_component**>(instance);
  1911. // initialize instance
  1912. CARLA_SAFE_ASSERT_RETURN(v3_cpp_obj_initialize(component, hostContext) == V3_OK, exit());
  1913. shouldTerminateComponent = true;
  1914. // create edit controller
  1915. if (v3_cpp_obj_query_interface(component, v3_edit_controller_iid, &controller) != V3_OK)
  1916. controller = nullptr;
  1917. // if we cannot cast from component, try to create edit controller from factory
  1918. if (controller == nullptr)
  1919. {
  1920. v3_tuid cuid = {};
  1921. if (v3_cpp_obj(component)->get_controller_class_id(component, cuid) == V3_OK)
  1922. {
  1923. instance = nullptr;
  1924. if (v3_cpp_obj(factory1)->create_instance(factory1, cuid, v3_edit_controller_iid, &instance) == V3_OK && instance != nullptr)
  1925. controller = static_cast<v3_edit_controller**>(instance);
  1926. }
  1927. CARLA_SAFE_ASSERT_RETURN(controller != nullptr, exit());
  1928. // component is separate from controller, needs its dedicated initialize and terminate
  1929. CARLA_SAFE_ASSERT_RETURN(v3_cpp_obj_initialize(controller, hostContext) == V3_OK, exit());
  1930. shouldTerminateController = true;
  1931. }
  1932. // create processor
  1933. CARLA_SAFE_ASSERT_RETURN(v3_cpp_obj_query_interface(component, v3_audio_processor_iid,
  1934. &processor) == V3_OK, exit());
  1935. CARLA_SAFE_ASSERT_RETURN(processor != nullptr, exit());
  1936. // create view, ignoring result
  1937. view = v3_cpp_obj(controller)->create_view(controller, "view");
  1938. return true;
  1939. }
  1940. bool exit()
  1941. {
  1942. // must be deleted by now
  1943. CARLA_SAFE_ASSERT(view == nullptr);
  1944. if (processor != nullptr)
  1945. {
  1946. v3_cpp_obj_unref(processor);
  1947. processor = nullptr;
  1948. }
  1949. if (controller != nullptr)
  1950. {
  1951. if (shouldTerminateController)
  1952. {
  1953. v3_cpp_obj_terminate(controller);
  1954. shouldTerminateController = false;
  1955. }
  1956. v3_cpp_obj_unref(controller);
  1957. component = nullptr;
  1958. }
  1959. if (component != nullptr)
  1960. {
  1961. if (shouldTerminateComponent)
  1962. {
  1963. v3_cpp_obj_terminate(component);
  1964. shouldTerminateComponent = false;
  1965. }
  1966. v3_cpp_obj_unref(component);
  1967. component = nullptr;
  1968. }
  1969. if (factory3 != nullptr)
  1970. {
  1971. v3_cpp_obj_unref(factory3);
  1972. factory3 = nullptr;
  1973. }
  1974. if (factory2 != nullptr)
  1975. {
  1976. v3_cpp_obj_unref(factory2);
  1977. factory2 = nullptr;
  1978. }
  1979. if (factory1 != nullptr)
  1980. {
  1981. v3_cpp_obj_unref(factory1);
  1982. factory1 = nullptr;
  1983. }
  1984. if (exitfn != nullptr)
  1985. {
  1986. exitfn();
  1987. exitfn = nullptr;
  1988. }
  1989. // return false so it can be used as error/fail condition
  1990. return false;
  1991. }
  1992. CARLA_DECLARE_NON_COPYABLE(Pointers)
  1993. } fV3;
  1994. struct Events {
  1995. carla_v3_input_param_changes* paramInputs;
  1996. carla_v3_output_param_changes* paramOutputs;
  1997. carla_v3_input_event_list* eventInputs;
  1998. carla_v3_output_event_list* eventOutputs;
  1999. Events() noexcept
  2000. : paramInputs(nullptr),
  2001. paramOutputs(nullptr),
  2002. eventInputs(nullptr),
  2003. eventOutputs(nullptr) {}
  2004. ~Events()
  2005. {
  2006. delete paramInputs;
  2007. delete paramOutputs;
  2008. delete eventInputs;
  2009. delete eventOutputs;
  2010. }
  2011. void init()
  2012. {
  2013. if (paramInputs != nullptr)
  2014. paramInputs->init();
  2015. if (eventInputs != nullptr)
  2016. eventInputs->numEvents = 0;
  2017. }
  2018. void prepare()
  2019. {
  2020. if (paramInputs != nullptr)
  2021. paramInputs->prepare();
  2022. }
  2023. CARLA_DECLARE_NON_COPYABLE(Events)
  2024. } fEvents;
  2025. struct UI {
  2026. bool isAttached;
  2027. bool isEmbed;
  2028. bool isVisible;
  2029. CarlaPluginUI* window;
  2030. UI() noexcept
  2031. : isAttached(false),
  2032. isEmbed(false),
  2033. isVisible(false),
  2034. window(nullptr) {}
  2035. ~UI()
  2036. {
  2037. CARLA_ASSERT(isEmbed || ! isVisible);
  2038. if (window != nullptr)
  2039. {
  2040. delete window;
  2041. window = nullptr;
  2042. }
  2043. }
  2044. CARLA_DECLARE_NON_COPYABLE(UI)
  2045. } fUI;
  2046. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaPluginVST3)
  2047. };
  2048. // --------------------------------------------------------------------------------------------------------------------
  2049. CarlaPluginPtr CarlaPlugin::newVST3(const Initializer& init)
  2050. {
  2051. carla_debug("CarlaPlugin::newVST3({%p, \"%s\", \"%s\", \"%s\"})",
  2052. init.engine, init.filename, init.name, init.label);
  2053. #ifdef USE_JUCE_FOR_VST3
  2054. if (std::getenv("CARLA_DO_NOT_USE_JUCE_FOR_VST3") == nullptr)
  2055. return newJuce(init, "VST3");
  2056. #endif
  2057. std::shared_ptr<CarlaPluginVST3> plugin(new CarlaPluginVST3(init.engine, init.id));
  2058. if (! plugin->init(plugin, init.filename, init.name, init.label, init.options))
  2059. return nullptr;
  2060. return plugin;
  2061. }
  2062. // -------------------------------------------------------------------------------------------------------------------
  2063. CARLA_BACKEND_END_NAMESPACE