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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557
  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. fAudioOutBuffers(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 numEventInputBuses = v3_cpp_obj(fV3.component)->get_bus_count(fV3.component, V3_EVENT, V3_INPUT);
  690. const int32_t numAudioOutputBuses = v3_cpp_obj(fV3.component)->get_bus_count(fV3.component, V3_AUDIO, V3_OUTPUT);
  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(numEventInputBuses >= 0);
  695. CARLA_SAFE_ASSERT(numAudioOutputBuses >= 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. fAudioOutBuffers = new float*[aOuts + cvIns];
  754. for (uint32_t i=0; i < aOuts + cvIns; ++i)
  755. fAudioOutBuffers[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].type = PARAMETER_INPUT;
  844. pData->param.data[j].index = ij;
  845. pData->param.data[j].rindex = ij;
  846. v3_param_info paramInfo = {};
  847. CARLA_SAFE_ASSERT_BREAK(v3_cpp_obj(fV3.controller)->get_parameter_info(fV3.controller, ij, &paramInfo) == V3_OK);
  848. if (paramInfo.flags & (V3_PARAM_IS_BYPASS|V3_PARAM_IS_HIDDEN|V3_PARAM_PROGRAM_CHANGE))
  849. continue;
  850. float min, max, def, step, stepSmall, stepLarge;
  851. min = static_cast<float>(v3_cpp_obj(fV3.controller)->normalised_parameter_to_plain(fV3.controller, j, 0.0));
  852. max = static_cast<float>(v3_cpp_obj(fV3.controller)->normalised_parameter_to_plain(fV3.controller, j, 1.0));
  853. def = static_cast<float>(v3_cpp_obj(fV3.controller)->normalised_parameter_to_plain(fV3.controller, j, paramInfo.default_normalised_value));
  854. if (min >= max)
  855. max = min + 0.1f;
  856. if (def < min)
  857. def = min;
  858. else if (def > max)
  859. def = max;
  860. if (paramInfo.flags & V3_PARAM_READ_ONLY)
  861. pData->param.data[j].type = PARAMETER_OUTPUT;
  862. else
  863. pData->param.data[j].type = PARAMETER_INPUT;
  864. if (paramInfo.step_count == 1)
  865. {
  866. step = max - min;
  867. stepSmall = step;
  868. stepLarge = step;
  869. pData->param.data[j].hints |= PARAMETER_IS_BOOLEAN;
  870. }
  871. /*
  872. else if (paramInfo.step_count != 0 && (paramInfo.flags & V3_PARAM_IS_LIST) != 0x0)
  873. {
  874. step = 1.0f;
  875. stepSmall = 1.0f;
  876. stepLarge = 10.0f;
  877. pData->param.data[j].hints |= PARAMETER_IS_INTEGER;
  878. }
  879. */
  880. else
  881. {
  882. float range = max - min;
  883. step = range/100.0f;
  884. stepSmall = range/1000.0f;
  885. stepLarge = range/10.0f;
  886. }
  887. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  888. pData->param.data[j].hints |= PARAMETER_USES_CUSTOM_TEXT;
  889. if (paramInfo.flags & V3_PARAM_CAN_AUTOMATE)
  890. {
  891. pData->param.data[j].hints |= PARAMETER_IS_AUTOMATABLE;
  892. if ((paramInfo.flags & V3_PARAM_IS_LIST) == 0x0)
  893. pData->param.data[j].hints |= PARAMETER_CAN_BE_CV_CONTROLLED;
  894. }
  895. pData->param.ranges[j].min = min;
  896. pData->param.ranges[j].max = max;
  897. pData->param.ranges[j].def = def;
  898. pData->param.ranges[j].step = step;
  899. pData->param.ranges[j].stepSmall = stepSmall;
  900. pData->param.ranges[j].stepLarge = stepLarge;
  901. }
  902. if (params > 0)
  903. {
  904. fEvents.paramInputs = new carla_v3_input_param_changes(pData->param);
  905. fEvents.paramOutputs = new carla_v3_output_param_changes;
  906. }
  907. if (needsCtrlIn)
  908. {
  909. portName.clear();
  910. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  911. {
  912. portName = pData->name;
  913. portName += ":";
  914. }
  915. portName += "events-in";
  916. portName.truncate(portNameSize);
  917. pData->event.portIn = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true, 0);
  918. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  919. pData->event.cvSourcePorts = pData->client->createCVSourcePorts();
  920. #endif
  921. fEvents.eventInputs = new carla_v3_input_event_list;
  922. }
  923. if (needsCtrlOut)
  924. {
  925. portName.clear();
  926. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  927. {
  928. portName = pData->name;
  929. portName += ":";
  930. }
  931. portName += "events-out";
  932. portName.truncate(portNameSize);
  933. pData->event.portOut = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false, 0);
  934. fEvents.eventOutputs = new carla_v3_output_event_list;
  935. }
  936. // plugin hints
  937. const PluginCategory v3category = getPluginCategoryFromV3SubCategories(fV3ClassInfo.v2.sub_categories);
  938. pData->hints = 0x0;
  939. if (v3category == PLUGIN_CATEGORY_SYNTH)
  940. pData->hints |= PLUGIN_IS_SYNTH;
  941. if (fV3.view != nullptr &&
  942. v3_cpp_obj(fV3.view)->is_platform_type_supported(fV3.view, V3_VIEW_PLATFORM_TYPE_NATIVE) == V3_TRUE)
  943. {
  944. pData->hints |= PLUGIN_HAS_CUSTOM_UI;
  945. pData->hints |= PLUGIN_HAS_CUSTOM_EMBED_UI;
  946. }
  947. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  948. pData->hints |= PLUGIN_CAN_DRYWET;
  949. if (aOuts > 0)
  950. pData->hints |= PLUGIN_CAN_VOLUME;
  951. if (aOuts >= 2 && aOuts % 2 == 0)
  952. pData->hints |= PLUGIN_CAN_BALANCE;
  953. // extra plugin hints
  954. pData->extraHints = 0x0;
  955. if (numEventInputBuses > 0)
  956. pData->extraHints |= PLUGIN_EXTRA_HINT_HAS_MIDI_IN;
  957. if (numEventOutputBuses > 0)
  958. pData->extraHints |= PLUGIN_EXTRA_HINT_HAS_MIDI_OUT;
  959. // check initial latency
  960. if ((fLastKnownLatency = v3_cpp_obj(fV3.processor)->get_latency_samples(fV3.processor)) != 0)
  961. {
  962. pData->client->setLatency(fLastKnownLatency);
  963. #ifndef BUILD_BRIDGE
  964. pData->latency.recreateBuffers(std::max(aIns+cvIns, aOuts+cvOuts), fLastKnownLatency);
  965. #endif
  966. }
  967. // initial audio setup
  968. v3_process_setup setup = {
  969. pData->engine->isOffline() ? V3_OFFLINE : V3_REALTIME,
  970. V3_SAMPLE_32,
  971. static_cast<int32_t>(pData->engine->getBufferSize()),
  972. pData->engine->getSampleRate()
  973. };
  974. v3_cpp_obj(fV3.processor)->setup_processing(fV3.processor, &setup);
  975. // activate all buses
  976. for (int32_t j=0; j<numAudioInputBuses; ++j)
  977. {
  978. v3_bus_info busInfo = {};
  979. CARLA_SAFE_ASSERT_BREAK(v3_cpp_obj(fV3.component)->get_bus_info(fV3.component, V3_AUDIO, V3_INPUT, j, &busInfo) == V3_OK);
  980. if ((busInfo.flags & V3_DEFAULT_ACTIVE) == 0x0) {
  981. CARLA_SAFE_ASSERT_BREAK(v3_cpp_obj(fV3.component)->activate_bus(fV3.component, V3_AUDIO, V3_INPUT, j, true) == V3_OK);
  982. }
  983. }
  984. for (int32_t j=0; j<numAudioInputBuses; ++j)
  985. {
  986. v3_bus_info busInfo = {};
  987. CARLA_SAFE_ASSERT_BREAK(v3_cpp_obj(fV3.component)->get_bus_info(fV3.component, V3_AUDIO, V3_OUTPUT, j, &busInfo) == V3_OK);
  988. if ((busInfo.flags & V3_DEFAULT_ACTIVE) == 0x0) {
  989. CARLA_SAFE_ASSERT_BREAK(v3_cpp_obj(fV3.component)->activate_bus(fV3.component, V3_AUDIO, V3_OUTPUT, j, true) == V3_OK);
  990. }
  991. }
  992. bufferSizeChanged(pData->engine->getBufferSize());
  993. reloadPrograms(true);
  994. if (pData->active)
  995. activate();
  996. carla_debug("CarlaPluginVST3::reload() - end");
  997. }
  998. // -------------------------------------------------------------------
  999. // Plugin processing
  1000. void activate() noexcept override
  1001. {
  1002. CARLA_SAFE_ASSERT_RETURN(fV3.component != nullptr,);
  1003. CARLA_SAFE_ASSERT_RETURN(fV3.processor != nullptr,);
  1004. try {
  1005. v3_cpp_obj(fV3.component)->set_active(fV3.component, true);
  1006. } CARLA_SAFE_EXCEPTION("set_active on");
  1007. try {
  1008. v3_cpp_obj(fV3.processor)->set_processing(fV3.processor, true);
  1009. } CARLA_SAFE_EXCEPTION("set_processing on");
  1010. fFirstActive = true;
  1011. }
  1012. void deactivate() noexcept override
  1013. {
  1014. CARLA_SAFE_ASSERT_RETURN(fV3.component != nullptr,);
  1015. CARLA_SAFE_ASSERT_RETURN(fV3.processor != nullptr,);
  1016. try {
  1017. v3_cpp_obj(fV3.processor)->set_processing(fV3.processor, false);
  1018. } CARLA_SAFE_EXCEPTION("set_processing off");
  1019. try {
  1020. v3_cpp_obj(fV3.component)->set_active(fV3.component, false);
  1021. } CARLA_SAFE_EXCEPTION("set_active off");
  1022. }
  1023. void process(const float* const* const audioIn, float** const audioOut,
  1024. const float* const* const cvIn, float** const cvOut, const uint32_t frames) override
  1025. {
  1026. // --------------------------------------------------------------------------------------------------------
  1027. // Check if active
  1028. if (! pData->active)
  1029. {
  1030. // disable any output sound
  1031. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1032. carla_zeroFloats(audioOut[i], frames);
  1033. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  1034. carla_zeroFloats(cvOut[i], frames);
  1035. return;
  1036. }
  1037. fEvents.init();
  1038. // --------------------------------------------------------------------------------------------------------
  1039. // Check if needs reset
  1040. if (pData->needsReset)
  1041. {
  1042. /*
  1043. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1044. {
  1045. }
  1046. else
  1047. */
  1048. if (pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS && fEvents.eventInputs != nullptr)
  1049. {
  1050. fEvents.eventInputs->numEvents = MAX_MIDI_NOTE;
  1051. for (uint8_t i=0; i < MAX_MIDI_NOTE; ++i)
  1052. {
  1053. v3_event& event(fEvents.eventInputs->events[i]);
  1054. carla_zeroStruct(event);
  1055. event.type = V3_EVENT_NOTE_OFF;
  1056. event.note_off.channel = (pData->ctrlChannel & MIDI_CHANNEL_BIT);
  1057. event.note_off.pitch = i;
  1058. }
  1059. }
  1060. pData->needsReset = false;
  1061. }
  1062. // --------------------------------------------------------------------------------------------------------
  1063. // Set TimeInfo
  1064. const EngineTimeInfo timeInfo(pData->engine->getTimeInfo());
  1065. fV3TimeContext.state = V3_PROCESS_CTX_PROJECT_TIME_VALID | V3_PROCESS_CTX_CONT_TIME_VALID;
  1066. fV3TimeContext.sample_rate = pData->engine->getSampleRate();
  1067. fV3TimeContext.project_time_in_samples = fV3TimeContext.continuous_time_in_samples
  1068. = static_cast<int64_t>(timeInfo.frame);
  1069. if (fFirstActive || ! fLastTimeInfo.compareIgnoringRollingFrames(timeInfo, frames))
  1070. fLastTimeInfo = timeInfo;
  1071. if (timeInfo.playing)
  1072. fV3TimeContext.state |= V3_PROCESS_CTX_PLAYING;
  1073. if (timeInfo.usecs != 0)
  1074. {
  1075. fV3TimeContext.system_time_ns = static_cast<int64_t>(timeInfo.usecs / 1000);
  1076. fV3TimeContext.state |= V3_PROCESS_CTX_SYSTEM_TIME_VALID;
  1077. }
  1078. if (timeInfo.bbt.valid)
  1079. {
  1080. CARLA_SAFE_ASSERT_INT(timeInfo.bbt.bar > 0, timeInfo.bbt.bar);
  1081. CARLA_SAFE_ASSERT_INT(timeInfo.bbt.beat > 0, timeInfo.bbt.beat);
  1082. const double ppqBar = static_cast<double>(timeInfo.bbt.beatsPerBar) * (timeInfo.bbt.bar - 1);
  1083. // const double ppqBeat = static_cast<double>(timeInfo.bbt.beat - 1);
  1084. // const double ppqTick = timeInfo.bbt.tick / timeInfo.bbt.ticksPerBeat;
  1085. // PPQ Pos
  1086. fV3TimeContext.project_time_quarters = static_cast<double>(timeInfo.frame) / (fV3TimeContext.sample_rate * 60 / timeInfo.bbt.beatsPerMinute);
  1087. // fTimeInfo.project_time_quarters = ppqBar + ppqBeat + ppqTick;
  1088. fV3TimeContext.state |= V3_PROCESS_CTX_PROJECT_TIME_VALID;
  1089. // Tempo
  1090. fV3TimeContext.bpm = timeInfo.bbt.beatsPerMinute;
  1091. fV3TimeContext.state |= V3_PROCESS_CTX_TEMPO_VALID;
  1092. // Bars
  1093. fV3TimeContext.bar_position_quarters = ppqBar;
  1094. fV3TimeContext.state |= V3_PROCESS_CTX_BAR_POSITION_VALID;
  1095. // Time Signature
  1096. fV3TimeContext.time_sig_numerator = static_cast<int32_t>(timeInfo.bbt.beatsPerBar + 0.5f);
  1097. fV3TimeContext.time_sig_denom = static_cast<int32_t>(timeInfo.bbt.beatType + 0.5f);
  1098. fV3TimeContext.state |= V3_PROCESS_CTX_TIME_SIG_VALID;
  1099. }
  1100. else
  1101. {
  1102. // Tempo
  1103. fV3TimeContext.bpm = 120.0;
  1104. fV3TimeContext.state |= V3_PROCESS_CTX_TEMPO_VALID;
  1105. // Time Signature
  1106. fV3TimeContext.time_sig_numerator = 4;
  1107. fV3TimeContext.time_sig_denom = 4;
  1108. fV3TimeContext.state |= V3_PROCESS_CTX_TIME_SIG_VALID;
  1109. // Missing info
  1110. fV3TimeContext.project_time_quarters = 0.0;
  1111. fV3TimeContext.bar_position_quarters = 0.0;
  1112. }
  1113. // --------------------------------------------------------------------------------------------------------
  1114. // Event Input and Processing
  1115. if (pData->event.portIn != nullptr)
  1116. {
  1117. // ----------------------------------------------------------------------------------------------------
  1118. // MIDI Input (External)
  1119. if (fEvents.eventInputs != nullptr && pData->extNotes.mutex.tryLock())
  1120. {
  1121. ExternalMidiNote note = { 0, 0, 0 };
  1122. uint16_t numEvents = fEvents.eventInputs->numEvents;
  1123. for (; numEvents < kPluginMaxMidiEvents && ! pData->extNotes.data.isEmpty();)
  1124. {
  1125. note = pData->extNotes.data.getFirst(note, true);
  1126. CARLA_SAFE_ASSERT_CONTINUE(note.channel >= 0 && note.channel < MAX_MIDI_CHANNELS);
  1127. v3_event& event(fEvents.eventInputs->events[numEvents++]);
  1128. carla_zeroStruct(event);
  1129. if (note.velo > 0)
  1130. {
  1131. event.type = V3_EVENT_NOTE_ON;
  1132. event.note_on.channel = (note.channel & MIDI_CHANNEL_BIT);
  1133. event.note_on.pitch = note.note;
  1134. event.note_on.velocity = static_cast<float>(note.velo) / 127.f;
  1135. }
  1136. else
  1137. {
  1138. event.type = V3_EVENT_NOTE_OFF;
  1139. event.note_off.channel = (note.channel & MIDI_CHANNEL_BIT);
  1140. event.note_off.pitch = note.note;
  1141. }
  1142. }
  1143. pData->extNotes.mutex.unlock();
  1144. fEvents.eventInputs->numEvents = numEvents;
  1145. } // End of MIDI Input (External)
  1146. // ----------------------------------------------------------------------------------------------------
  1147. // Event Input (System)
  1148. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1149. bool allNotesOffSent = false;
  1150. #endif
  1151. bool isSampleAccurate = (pData->options & PLUGIN_OPTION_FIXED_BUFFERS) == 0;
  1152. uint32_t startTime = 0;
  1153. uint32_t timeOffset = 0;
  1154. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1155. if (cvIn != nullptr && pData->event.cvSourcePorts != nullptr)
  1156. pData->event.cvSourcePorts->initPortBuffers(cvIn, frames, isSampleAccurate, pData->event.portIn);
  1157. #endif
  1158. for (uint32_t i=0, numEvents = pData->event.portIn->getEventCount(); i < numEvents; ++i)
  1159. {
  1160. EngineEvent& event(pData->event.portIn->getEvent(i));
  1161. uint32_t eventTime = event.time;
  1162. CARLA_SAFE_ASSERT_UINT2_CONTINUE(eventTime < frames, eventTime, frames);
  1163. if (eventTime < timeOffset)
  1164. {
  1165. carla_stderr2("Timing error, eventTime:%u < timeOffset:%u for '%s'",
  1166. eventTime, timeOffset, pData->name);
  1167. eventTime = timeOffset;
  1168. }
  1169. if (isSampleAccurate && eventTime > timeOffset)
  1170. {
  1171. if (processSingle(audioIn, audioOut, eventTime - timeOffset, timeOffset))
  1172. {
  1173. startTime = 0;
  1174. timeOffset = eventTime;
  1175. // TODO
  1176. }
  1177. else
  1178. {
  1179. startTime += timeOffset;
  1180. }
  1181. }
  1182. switch (event.type)
  1183. {
  1184. case kEngineEventTypeNull:
  1185. break;
  1186. case kEngineEventTypeControl: {
  1187. EngineControlEvent& ctrlEvent(event.ctrl);
  1188. switch (ctrlEvent.type)
  1189. {
  1190. case kEngineControlEventTypeNull:
  1191. break;
  1192. case kEngineControlEventTypeParameter: {
  1193. float value;
  1194. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1195. // non-midi
  1196. if (event.channel == kEngineEventNonMidiChannel)
  1197. {
  1198. const uint32_t k = ctrlEvent.param;
  1199. CARLA_SAFE_ASSERT_CONTINUE(k < pData->param.count);
  1200. ctrlEvent.handled = true;
  1201. value = pData->param.getFinalUnnormalizedValue(k, ctrlEvent.normalizedValue);
  1202. setParameterValueRT(k, value, event.time, true);
  1203. continue;
  1204. }
  1205. // Control backend stuff
  1206. if (event.channel == pData->ctrlChannel)
  1207. {
  1208. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_DRYWET) != 0)
  1209. {
  1210. ctrlEvent.handled = true;
  1211. value = ctrlEvent.normalizedValue;
  1212. setDryWetRT(value, true);
  1213. }
  1214. else if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_VOLUME) != 0)
  1215. {
  1216. ctrlEvent.handled = true;
  1217. value = ctrlEvent.normalizedValue*127.0f/100.0f;
  1218. setVolumeRT(value, true);
  1219. }
  1220. else if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_BALANCE) != 0)
  1221. {
  1222. float left, right;
  1223. value = ctrlEvent.normalizedValue/0.5f - 1.0f;
  1224. if (value < 0.0f)
  1225. {
  1226. left = -1.0f;
  1227. right = (value*2.0f)+1.0f;
  1228. }
  1229. else if (value > 0.0f)
  1230. {
  1231. left = (value*2.0f)-1.0f;
  1232. right = 1.0f;
  1233. }
  1234. else
  1235. {
  1236. left = -1.0f;
  1237. right = 1.0f;
  1238. }
  1239. ctrlEvent.handled = true;
  1240. setBalanceLeftRT(left, true);
  1241. setBalanceRightRT(right, true);
  1242. }
  1243. }
  1244. #endif
  1245. // Control plugin parameters
  1246. uint32_t k;
  1247. for (k=0; k < pData->param.count; ++k)
  1248. {
  1249. if (pData->param.data[k].midiChannel != event.channel)
  1250. continue;
  1251. if (pData->param.data[k].mappedControlIndex != ctrlEvent.param)
  1252. continue;
  1253. if (pData->param.data[k].type != PARAMETER_INPUT)
  1254. continue;
  1255. if ((pData->param.data[k].hints & PARAMETER_IS_AUTOMATABLE) == 0)
  1256. continue;
  1257. ctrlEvent.handled = true;
  1258. value = pData->param.getFinalUnnormalizedValue(k, ctrlEvent.normalizedValue);
  1259. setParameterValueRT(k, value, event.time, true);
  1260. }
  1261. if ((pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0 && ctrlEvent.param < MAX_MIDI_VALUE)
  1262. {
  1263. // TODO
  1264. }
  1265. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1266. if (! ctrlEvent.handled)
  1267. checkForMidiLearn(event);
  1268. #endif
  1269. break;
  1270. } // case kEngineControlEventTypeParameter
  1271. case kEngineControlEventTypeMidiBank:
  1272. if ((pData->options & PLUGIN_OPTION_SEND_PROGRAM_CHANGES) != 0)
  1273. {
  1274. // TODO
  1275. }
  1276. break;
  1277. case kEngineControlEventTypeMidiProgram:
  1278. if (event.channel == pData->ctrlChannel && (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  1279. {
  1280. if (ctrlEvent.param < pData->prog.count)
  1281. {
  1282. setProgramRT(ctrlEvent.param, true);
  1283. break;
  1284. }
  1285. }
  1286. else if (pData->options & PLUGIN_OPTION_SEND_PROGRAM_CHANGES)
  1287. {
  1288. // TODO
  1289. }
  1290. break;
  1291. case kEngineControlEventTypeAllSoundOff:
  1292. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1293. {
  1294. // TODO
  1295. }
  1296. break;
  1297. case kEngineControlEventTypeAllNotesOff:
  1298. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1299. {
  1300. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1301. if (event.channel == pData->ctrlChannel && ! allNotesOffSent)
  1302. {
  1303. allNotesOffSent = true;
  1304. postponeRtAllNotesOff();
  1305. }
  1306. #endif
  1307. // TODO
  1308. }
  1309. break;
  1310. } // switch (ctrlEvent.type)
  1311. break;
  1312. } // case kEngineEventTypeControl
  1313. case kEngineEventTypeMidi: {
  1314. const EngineMidiEvent& midiEvent(event.midi);
  1315. if (midiEvent.size > 3)
  1316. continue;
  1317. #ifdef CARLA_PROPER_CPP11_SUPPORT
  1318. static_assert(3 <= EngineMidiEvent::kDataSize, "Incorrect data");
  1319. #endif
  1320. uint8_t status = uint8_t(MIDI_GET_STATUS_FROM_DATA(midiEvent.data));
  1321. if ((status == MIDI_STATUS_NOTE_OFF || status == MIDI_STATUS_NOTE_ON) && (pData->options & PLUGIN_OPTION_SKIP_SENDING_NOTES))
  1322. continue;
  1323. if (status == MIDI_STATUS_CHANNEL_PRESSURE && (pData->options & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) == 0)
  1324. continue;
  1325. if (status == MIDI_STATUS_CONTROL_CHANGE && (pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) == 0)
  1326. continue;
  1327. if (status == MIDI_STATUS_POLYPHONIC_AFTERTOUCH && (pData->options & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) == 0)
  1328. continue;
  1329. if (status == MIDI_STATUS_PITCH_WHEEL_CONTROL && (pData->options & PLUGIN_OPTION_SEND_PITCHBEND) == 0)
  1330. continue;
  1331. // Fix bad note-off
  1332. if (status == MIDI_STATUS_NOTE_ON && midiEvent.data[2] == 0)
  1333. status = MIDI_STATUS_NOTE_OFF;
  1334. // TODO
  1335. if (status == MIDI_STATUS_NOTE_ON)
  1336. {
  1337. pData->postponeNoteOnRtEvent(true, event.channel, midiEvent.data[1], midiEvent.data[2]);
  1338. }
  1339. else if (status == MIDI_STATUS_NOTE_OFF)
  1340. {
  1341. pData->postponeNoteOffRtEvent(true, event.channel, midiEvent.data[1]);
  1342. }
  1343. } break;
  1344. } // switch (event.type)
  1345. }
  1346. pData->postRtEvents.trySplice();
  1347. if (frames > timeOffset)
  1348. processSingle(audioIn, audioOut, frames - timeOffset, timeOffset);
  1349. } // End of Event Input and Processing
  1350. // --------------------------------------------------------------------------------------------------------
  1351. // Plugin processing (no events)
  1352. else
  1353. {
  1354. processSingle(audioIn, audioOut, frames, 0);
  1355. } // End of Plugin processing (no events)
  1356. // --------------------------------------------------------------------------------------------------------
  1357. // MIDI Output
  1358. if (pData->event.portOut != nullptr)
  1359. {
  1360. // TODO
  1361. } // End of MIDI Output
  1362. fFirstActive = false;
  1363. // --------------------------------------------------------------------------------------------------------
  1364. }
  1365. bool processSingle(const float* const* const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t timeOffset)
  1366. {
  1367. CARLA_SAFE_ASSERT_RETURN(frames > 0, false);
  1368. if (pData->audioIn.count > 0)
  1369. {
  1370. CARLA_SAFE_ASSERT_RETURN(inBuffer != nullptr, false);
  1371. }
  1372. if (pData->audioOut.count > 0)
  1373. {
  1374. CARLA_SAFE_ASSERT_RETURN(outBuffer != nullptr, false);
  1375. CARLA_SAFE_ASSERT_RETURN(fAudioOutBuffers != nullptr, false);
  1376. }
  1377. // --------------------------------------------------------------------------------------------------------
  1378. // Try lock, silence otherwise
  1379. #ifndef STOAT_TEST_BUILD
  1380. if (pData->engine->isOffline())
  1381. {
  1382. pData->singleMutex.lock();
  1383. }
  1384. else
  1385. #endif
  1386. if (! pData->singleMutex.tryLock())
  1387. {
  1388. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1389. {
  1390. for (uint32_t k=0; k < frames; ++k)
  1391. outBuffer[i][k+timeOffset] = 0.0f;
  1392. }
  1393. return false;
  1394. }
  1395. // --------------------------------------------------------------------------------------------------------
  1396. // Set audio buffers
  1397. float* bufferAudioIn[std::max(1u, pData->audioIn.count + pData->cvIn.count)];
  1398. float* bufferAudioOut[std::max(1u, pData->audioOut.count + pData->cvOut.count)];
  1399. {
  1400. uint32_t i=0;
  1401. for (; i < pData->audioIn.count; ++i)
  1402. bufferAudioIn[i] = const_cast<float*>(inBuffer[i]+timeOffset);
  1403. for (; i < pData->cvIn.count; ++i)
  1404. bufferAudioIn[i] = const_cast<float*>(inBuffer[i]+timeOffset);
  1405. }
  1406. {
  1407. uint32_t i=0;
  1408. for (; i < pData->audioOut.count; ++i)
  1409. bufferAudioOut[i] = fAudioOutBuffers[i]+timeOffset;
  1410. for (; i < pData->cvOut.count; ++i)
  1411. bufferAudioOut[i] = fAudioOutBuffers[i]+timeOffset;
  1412. }
  1413. for (uint32_t i=0; i < pData->audioOut.count + pData->cvOut.count; ++i)
  1414. carla_zeroFloats(fAudioOutBuffers[i], frames);
  1415. // --------------------------------------------------------------------------------------------------------
  1416. // Set MIDI events
  1417. // TODO
  1418. // --------------------------------------------------------------------------------------------------------
  1419. // Run plugin
  1420. fEvents.prepare();
  1421. v3_audio_bus_buffers processInputs = {
  1422. static_cast<int32_t>(pData->audioIn.count + pData->cvIn.count),
  1423. 0, { bufferAudioIn }
  1424. };
  1425. v3_audio_bus_buffers processOutputs = {
  1426. static_cast<int32_t>(pData->audioOut.count + pData->cvOut.count),
  1427. 0, { bufferAudioOut }
  1428. };
  1429. v3_process_data processData = {
  1430. pData->engine->isOffline() ? V3_OFFLINE : V3_REALTIME,
  1431. V3_SAMPLE_32,
  1432. static_cast<int32_t>(frames),
  1433. static_cast<int32_t>(pData->audioIn.count + pData->cvIn.count),
  1434. static_cast<int32_t>(pData->audioOut.count + pData->cvOut.count),
  1435. &processInputs,
  1436. &processOutputs,
  1437. fEvents.paramInputs != nullptr ? (v3_param_changes**)&fEvents.paramInputs : nullptr,
  1438. fEvents.paramOutputs != nullptr ? (v3_param_changes**)&fEvents.paramOutputs : nullptr,
  1439. fEvents.eventInputs != nullptr ? (v3_event_list**)&fEvents.eventInputs : nullptr,
  1440. fEvents.eventOutputs != nullptr ? (v3_event_list**)&fEvents.eventOutputs : nullptr,
  1441. &fV3TimeContext
  1442. };
  1443. try {
  1444. v3_cpp_obj(fV3.processor)->process(fV3.processor, &processData);
  1445. } CARLA_SAFE_EXCEPTION("process");
  1446. fEvents.init();
  1447. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1448. // --------------------------------------------------------------------------------------------------------
  1449. // Post-processing (dry/wet, volume and balance)
  1450. {
  1451. const bool doDryWet = (pData->hints & PLUGIN_CAN_DRYWET) != 0 && carla_isNotEqual(pData->postProc.dryWet, 1.0f);
  1452. const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) != 0 && ! (carla_isEqual(pData->postProc.balanceLeft, -1.0f) && carla_isEqual(pData->postProc.balanceRight, 1.0f));
  1453. const bool isMono = (pData->audioIn.count == 1);
  1454. bool isPair;
  1455. float bufValue, oldBufLeft[doBalance ? frames : 1];
  1456. uint32_t i=0;
  1457. for (; i < pData->audioOut.count; ++i)
  1458. {
  1459. // Dry/Wet
  1460. if (doDryWet)
  1461. {
  1462. const uint32_t c = isMono ? 0 : i;
  1463. for (uint32_t k=0; k < frames; ++k)
  1464. {
  1465. bufValue = inBuffer[c][k+timeOffset];
  1466. fAudioOutBuffers[i][k] = (fAudioOutBuffers[i][k] * pData->postProc.dryWet) + (bufValue * (1.0f - pData->postProc.dryWet));
  1467. }
  1468. }
  1469. // Balance
  1470. if (doBalance)
  1471. {
  1472. isPair = (i % 2 == 0);
  1473. if (isPair)
  1474. {
  1475. CARLA_ASSERT(i+1 < pData->audioOut.count);
  1476. carla_copyFloats(oldBufLeft, fAudioOutBuffers[i], frames);
  1477. }
  1478. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  1479. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  1480. for (uint32_t k=0; k < frames; ++k)
  1481. {
  1482. if (isPair)
  1483. {
  1484. // left
  1485. fAudioOutBuffers[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  1486. fAudioOutBuffers[i][k] += fAudioOutBuffers[i+1][k] * (1.0f - balRangeR);
  1487. }
  1488. else
  1489. {
  1490. // right
  1491. fAudioOutBuffers[i][k] = fAudioOutBuffers[i][k] * balRangeR;
  1492. fAudioOutBuffers[i][k] += oldBufLeft[k] * balRangeL;
  1493. }
  1494. }
  1495. }
  1496. // Volume (and buffer copy)
  1497. {
  1498. for (uint32_t k=0; k < frames; ++k)
  1499. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k] * pData->postProc.volume;
  1500. }
  1501. }
  1502. for (; i < pData->cvOut.count; ++i)
  1503. carla_copyFloats(outBuffer[i] + timeOffset, fAudioOutBuffers[i] + timeOffset, frames);
  1504. } // End of Post-processing
  1505. #else // BUILD_BRIDGE_ALTERNATIVE_ARCH
  1506. for (uint32_t i=0; i < pData->audioOut.count + pData->cvOut.count; ++i)
  1507. carla_copyFloats(outBuffer[i] + timeOffset, fAudioOutBuffers[i] + timeOffset, frames);
  1508. #endif
  1509. // --------------------------------------------------------------------------------------------------------
  1510. pData->singleMutex.unlock();
  1511. return true;
  1512. }
  1513. void bufferSizeChanged(const uint32_t newBufferSize) override
  1514. {
  1515. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  1516. carla_debug("CarlaPluginVST2::bufferSizeChanged(%i)", newBufferSize);
  1517. if (pData->active)
  1518. deactivate();
  1519. for (uint32_t i=0; i < pData->audioOut.count + pData->cvOut.count; ++i)
  1520. {
  1521. if (fAudioOutBuffers[i] != nullptr)
  1522. delete[] fAudioOutBuffers[i];
  1523. fAudioOutBuffers[i] = new float[newBufferSize];
  1524. }
  1525. v3_process_setup setup = {
  1526. pData->engine->isOffline() ? V3_OFFLINE : V3_REALTIME,
  1527. V3_SAMPLE_32,
  1528. static_cast<int32_t>(newBufferSize),
  1529. pData->engine->getSampleRate()
  1530. };
  1531. v3_cpp_obj(fV3.processor)->setup_processing(fV3.processor, &setup);
  1532. if (pData->active)
  1533. activate();
  1534. }
  1535. void sampleRateChanged(const double newSampleRate) override
  1536. {
  1537. CARLA_ASSERT_INT(newSampleRate > 0.0, newSampleRate);
  1538. carla_debug("CarlaPluginVST3::sampleRateChanged(%g)", newSampleRate);
  1539. if (pData->active)
  1540. deactivate();
  1541. v3_process_setup setup = {
  1542. pData->engine->isOffline() ? V3_OFFLINE : V3_REALTIME,
  1543. V3_SAMPLE_32,
  1544. static_cast<int32_t>(pData->engine->getBufferSize()),
  1545. newSampleRate
  1546. };
  1547. v3_cpp_obj(fV3.processor)->setup_processing(fV3.processor, &setup);
  1548. if (pData->active)
  1549. activate();
  1550. }
  1551. void offlineModeChanged(const bool isOffline) override
  1552. {
  1553. carla_debug("CarlaPluginVST3::offlineModeChanged(%d)", isOffline);
  1554. if (pData->active)
  1555. deactivate();
  1556. v3_process_setup setup = {
  1557. isOffline ? V3_OFFLINE : V3_REALTIME,
  1558. V3_SAMPLE_32,
  1559. static_cast<int32_t>(pData->engine->getBufferSize()),
  1560. pData->engine->getSampleRate()
  1561. };
  1562. v3_cpp_obj(fV3.processor)->setup_processing(fV3.processor, &setup);
  1563. if (pData->active)
  1564. activate();
  1565. }
  1566. // -------------------------------------------------------------------
  1567. // Plugin buffers
  1568. void clearBuffers() noexcept override
  1569. {
  1570. carla_debug("CarlaPluginVST2::clearBuffers() - start");
  1571. if (fAudioOutBuffers != nullptr)
  1572. {
  1573. for (uint32_t i=0; i < pData->audioOut.count + pData->cvOut.count; ++i)
  1574. {
  1575. if (fAudioOutBuffers[i] != nullptr)
  1576. {
  1577. delete[] fAudioOutBuffers[i];
  1578. fAudioOutBuffers[i] = nullptr;
  1579. }
  1580. }
  1581. delete[] fAudioOutBuffers;
  1582. fAudioOutBuffers = nullptr;
  1583. }
  1584. CarlaPlugin::clearBuffers();
  1585. carla_debug("CarlaPluginVST2::clearBuffers() - end");
  1586. }
  1587. // -------------------------------------------------------------------
  1588. // Post-poned UI Stuff
  1589. void uiParameterChange(const uint32_t index, const float value) noexcept override
  1590. {
  1591. CARLA_SAFE_ASSERT_RETURN(fV3.controller != nullptr,);
  1592. CARLA_SAFE_ASSERT_RETURN(index < pData->param.count,);
  1593. const double normalized = v3_cpp_obj(fV3.controller)->plain_parameter_to_normalised(fV3.controller, index, value);
  1594. v3_cpp_obj(fV3.controller)->set_parameter_normalised(fV3.controller, index, normalized);
  1595. }
  1596. // ----------------------------------------------------------------------------------------------------------------
  1597. const void* getNativeDescriptor() const noexcept override
  1598. {
  1599. return fV3.component;
  1600. }
  1601. const void* getExtraStuff() const noexcept override
  1602. {
  1603. return fV3.controller;
  1604. }
  1605. // ----------------------------------------------------------------------------------------------------------------
  1606. bool init(const CarlaPluginPtr plugin,
  1607. const char* const filename,
  1608. const char* name,
  1609. const char* /*const label*/,
  1610. const uint options)
  1611. {
  1612. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  1613. // ------------------------------------------------------------------------------------------------------------
  1614. // first checks
  1615. if (pData->client != nullptr)
  1616. {
  1617. pData->engine->setLastError("Plugin client is already registered");
  1618. return false;
  1619. }
  1620. if (filename == nullptr || filename[0] == '\0')
  1621. {
  1622. pData->engine->setLastError("null filename");
  1623. return false;
  1624. }
  1625. V3_ENTRYFN v3_entry;
  1626. V3_EXITFN v3_exit;
  1627. V3_GETFN v3_get;
  1628. // filename is full path to binary
  1629. if (water::File(filename).existsAsFile())
  1630. {
  1631. if (! pData->libOpen(filename))
  1632. {
  1633. pData->engine->setLastError(pData->libError(filename));
  1634. return false;
  1635. }
  1636. v3_entry = pData->libSymbol<V3_ENTRYFN>(V3_ENTRYFNNAME);
  1637. v3_exit = pData->libSymbol<V3_EXITFN>(V3_EXITFNNAME);
  1638. v3_get = pData->libSymbol<V3_GETFN>(V3_GETFNNAME);
  1639. }
  1640. // assume filename is a vst3 bundle
  1641. else
  1642. {
  1643. #ifdef CARLA_OS_MAC
  1644. if (! fMacBundleLoader.load(filename))
  1645. {
  1646. pData->engine->setLastError("Failed to load VST3 bundle executable");
  1647. return false;
  1648. }
  1649. v3_entry = (V3_ENTRYFN)CFBundleGetFunctionPointerForName(fMacBundleLoader.getRef(), CFSTR(V3_ENTRYFNNAME));
  1650. v3_exit = (V3_EXITFN)CFBundleGetFunctionPointerForName(fMacBundleLoader.getRef(), CFSTR(V3_EXITFNNAME));
  1651. v3_get = (V3_GETFN)CFBundleGetFunctionPointerForName(fMacBundleLoader.getRef(), CFSTR(V3_GETFNNAME));
  1652. #else
  1653. water::String binaryfilename = filename;
  1654. if (!binaryfilename.endsWithChar(CARLA_OS_SEP))
  1655. binaryfilename += CARLA_OS_SEP_STR;
  1656. binaryfilename += "Contents" CARLA_OS_SEP_STR V3_CONTENT_DIR CARLA_OS_SEP_STR;
  1657. binaryfilename += water::File(filename).getFileNameWithoutExtension();
  1658. #ifdef CARLA_OS_WIN
  1659. binaryfilename += ".vst3";
  1660. #else
  1661. binaryfilename += ".so";
  1662. #endif
  1663. if (! water::File(binaryfilename).existsAsFile())
  1664. {
  1665. pData->engine->setLastError("Failed to find a suitable VST3 bundle binary");
  1666. return false;
  1667. }
  1668. if (! pData->libOpen(binaryfilename.toRawUTF8()))
  1669. {
  1670. pData->engine->setLastError(pData->libError(binaryfilename.toRawUTF8()));
  1671. return false;
  1672. }
  1673. v3_entry = pData->libSymbol<V3_ENTRYFN>(V3_ENTRYFNNAME);
  1674. v3_exit = pData->libSymbol<V3_EXITFN>(V3_EXITFNNAME);
  1675. v3_get = pData->libSymbol<V3_GETFN>(V3_GETFNNAME);
  1676. #endif
  1677. }
  1678. // ------------------------------------------------------------------------------------------------------------
  1679. // ensure entry and exit points are available
  1680. if (v3_entry == nullptr || v3_exit == nullptr || v3_get == nullptr)
  1681. {
  1682. pData->engine->setLastError("Not a VST3 plugin");
  1683. return false;
  1684. }
  1685. // ------------------------------------------------------------------------------------------------------------
  1686. // call entry point
  1687. #if defined(CARLA_OS_MAC)
  1688. v3_entry(pData->lib == nullptr ? fMacBundleLoader.getRef() : nullptr);
  1689. #elif defined(CARLA_OS_WIN)
  1690. v3_entry();
  1691. #else
  1692. v3_entry(pData->lib);
  1693. #endif
  1694. // ------------------------------------------------------------------------------------------------------------
  1695. // fetch initial factory
  1696. v3_plugin_factory** const factory = v3_get();
  1697. if (factory == nullptr)
  1698. {
  1699. pData->engine->setLastError("VST3 factory failed to create a valid instance");
  1700. return false;
  1701. }
  1702. // ------------------------------------------------------------------------------------------------------------
  1703. // initialize and find requested plugin
  1704. fV3.exitfn = v3_exit;
  1705. fV3.factory1 = factory;
  1706. if (! fV3.queryFactories(getHostContext()))
  1707. {
  1708. pData->engine->setLastError("VST3 plugin failed to properly create factories");
  1709. return false;
  1710. }
  1711. if (! fV3.findPlugin(fV3ClassInfo))
  1712. {
  1713. pData->engine->setLastError("Failed to find the requested plugin in the VST3 bundle");
  1714. return false;
  1715. }
  1716. if (! fV3.initializePlugin(fV3ClassInfo.v1.class_id, getHostContext()))
  1717. {
  1718. pData->engine->setLastError("VST3 plugin failed to initialize");
  1719. return false;
  1720. }
  1721. // ------------------------------------------------------------------------------------------------------------
  1722. // do some basic safety checks
  1723. if (v3_cpp_obj(fV3.processor)->can_process_sample_size(fV3.processor, V3_SAMPLE_32) != V3_OK)
  1724. {
  1725. pData->engine->setLastError("VST3 plugin does not support 32bit audio, cannot continue");
  1726. return false;
  1727. }
  1728. // ------------------------------------------------------------------------------------------------------------
  1729. // get info
  1730. if (name != nullptr && name[0] != '\0')
  1731. {
  1732. pData->name = pData->engine->getUniquePluginName(name);
  1733. }
  1734. else
  1735. {
  1736. if (fV3ClassInfo.v1.name[0] != '\0')
  1737. pData->name = pData->engine->getUniquePluginName(fV3ClassInfo.v1.name);
  1738. else if (const char* const shortname = std::strrchr(filename, CARLA_OS_SEP))
  1739. pData->name = pData->engine->getUniquePluginName(shortname+1);
  1740. else
  1741. pData->name = pData->engine->getUniquePluginName("unknown");
  1742. }
  1743. pData->filename = carla_strdup(filename);
  1744. // ------------------------------------------------------------------------------------------------------------
  1745. // register client
  1746. pData->client = pData->engine->addClient(plugin);
  1747. if (pData->client == nullptr || ! pData->client->isOk())
  1748. {
  1749. pData->engine->setLastError("Failed to register plugin client");
  1750. return false;
  1751. }
  1752. // ------------------------------------------------------------------------------------------------------------
  1753. // set default options
  1754. pData->options = 0x0;
  1755. if (fLastKnownLatency != 0 /*|| hasMidiOutput()*/ || isPluginOptionEnabled(options, PLUGIN_OPTION_FIXED_BUFFERS))
  1756. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  1757. if (isPluginOptionEnabled(options, PLUGIN_OPTION_USE_CHUNKS))
  1758. pData->options |= PLUGIN_OPTION_USE_CHUNKS;
  1759. /*
  1760. if (hasMidiInput())
  1761. {
  1762. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_CONTROL_CHANGES))
  1763. pData->options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  1764. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_CHANNEL_PRESSURE))
  1765. pData->options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  1766. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH))
  1767. pData->options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  1768. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_PITCHBEND))
  1769. pData->options |= PLUGIN_OPTION_SEND_PITCHBEND;
  1770. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_ALL_SOUND_OFF))
  1771. pData->options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  1772. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_PROGRAM_CHANGES))
  1773. pData->options |= PLUGIN_OPTION_SEND_PROGRAM_CHANGES;
  1774. if (isPluginOptionInverseEnabled(options, PLUGIN_OPTION_SKIP_SENDING_NOTES))
  1775. pData->options |= PLUGIN_OPTION_SKIP_SENDING_NOTES;
  1776. }
  1777. */
  1778. /*
  1779. if (numPrograms > 1 && (pData->options & PLUGIN_OPTION_SEND_PROGRAM_CHANGES) == 0)
  1780. if (isPluginOptionEnabled(options, PLUGIN_OPTION_MAP_PROGRAM_CHANGES))
  1781. pData->options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  1782. */
  1783. // ------------------------------------------------------------------------------------------------------------
  1784. return true;
  1785. }
  1786. protected:
  1787. void handlePluginUIClosed() override
  1788. {
  1789. // CARLA_SAFE_ASSERT_RETURN(fUI.window != nullptr,);
  1790. carla_debug("CarlaPluginVST3::handlePluginUIClosed()");
  1791. showCustomUI(false);
  1792. pData->engine->callback(true, true,
  1793. ENGINE_CALLBACK_UI_STATE_CHANGED,
  1794. pData->id,
  1795. 0,
  1796. 0, 0, 0.0f, nullptr);
  1797. }
  1798. void handlePluginUIResized(const uint width, const uint height) override
  1799. {
  1800. // CARLA_SAFE_ASSERT_RETURN(fUI.window != nullptr,);
  1801. carla_debug("CarlaPluginVST3::handlePluginUIResized(%u, %u)", width, height);
  1802. return; // unused
  1803. (void)width; (void)height;
  1804. }
  1805. private:
  1806. #ifdef CARLA_OS_MAC
  1807. BundleLoader fMacBundleLoader;
  1808. #endif
  1809. bool fFirstActive; // first process() call after activate()
  1810. float** fAudioOutBuffers;
  1811. uint32_t fLastKnownLatency;
  1812. EngineTimeInfo fLastTimeInfo;
  1813. v3_process_context fV3TimeContext;
  1814. CarlaScopedPointer<carla_v3_host_application> fV3Application;
  1815. inline v3_funknown** getHostContext() noexcept { return (v3_funknown**)&fV3Application; }
  1816. // v3_class_info_2 is ABI compatible with v3_class_info
  1817. union ClassInfo {
  1818. v3_class_info v1;
  1819. v3_class_info_2 v2;
  1820. } fV3ClassInfo;
  1821. struct Pointers {
  1822. V3_EXITFN exitfn;
  1823. v3_plugin_factory** factory1;
  1824. v3_plugin_factory_2** factory2;
  1825. v3_plugin_factory_3** factory3;
  1826. v3_component** component;
  1827. v3_edit_controller** controller;
  1828. v3_audio_processor** processor;
  1829. v3_plugin_view** view;
  1830. bool shouldTerminateComponent;
  1831. bool shouldTerminateController;
  1832. Pointers()
  1833. : exitfn(nullptr),
  1834. factory1(nullptr),
  1835. factory2(nullptr),
  1836. factory3(nullptr),
  1837. component(nullptr),
  1838. controller(nullptr),
  1839. processor(nullptr),
  1840. view(nullptr),
  1841. shouldTerminateComponent(false),
  1842. shouldTerminateController(false) {}
  1843. ~Pointers()
  1844. {
  1845. // must have been cleaned up by now
  1846. CARLA_SAFE_ASSERT(exitfn == nullptr);
  1847. }
  1848. // must have exitfn and factory1 set
  1849. bool queryFactories(v3_funknown** const hostContext)
  1850. {
  1851. // query 2nd factory
  1852. if (v3_cpp_obj_query_interface(factory1, v3_plugin_factory_2_iid, &factory2) == V3_OK)
  1853. {
  1854. CARLA_SAFE_ASSERT_RETURN(factory2 != nullptr, exit());
  1855. }
  1856. else
  1857. {
  1858. CARLA_SAFE_ASSERT(factory2 == nullptr);
  1859. factory2 = nullptr;
  1860. }
  1861. // query 3rd factory
  1862. if (factory2 != nullptr && v3_cpp_obj_query_interface(factory2, v3_plugin_factory_3_iid, &factory3) == V3_OK)
  1863. {
  1864. CARLA_SAFE_ASSERT_RETURN(factory3 != nullptr, exit());
  1865. }
  1866. else
  1867. {
  1868. CARLA_SAFE_ASSERT(factory3 == nullptr);
  1869. factory3 = nullptr;
  1870. }
  1871. // set host context (application) if 3rd factory provided
  1872. if (factory3 != nullptr)
  1873. v3_cpp_obj(factory3)->set_host_context(factory3, hostContext);
  1874. return true;
  1875. }
  1876. // must have all possible factories and exitfn set
  1877. bool findPlugin(ClassInfo& classInfo)
  1878. {
  1879. // get factory info
  1880. v3_factory_info factoryInfo = {};
  1881. CARLA_SAFE_ASSERT_RETURN(v3_cpp_obj(factory1)->get_factory_info(factory1, &factoryInfo) == V3_OK, exit());
  1882. // get num classes
  1883. const int32_t numClasses = v3_cpp_obj(factory1)->num_classes(factory1);
  1884. CARLA_SAFE_ASSERT_RETURN(numClasses > 0, exit());
  1885. // go through all relevant classes
  1886. for (int32_t i=0; i<numClasses; ++i)
  1887. {
  1888. carla_zeroStruct(classInfo);
  1889. if (factory2 != nullptr)
  1890. v3_cpp_obj(factory2)->get_class_info_2(factory2, i, &classInfo.v2);
  1891. else
  1892. v3_cpp_obj(factory1)->get_class_info(factory1, i, &classInfo.v1);
  1893. // safety check
  1894. CARLA_SAFE_ASSERT_CONTINUE(classInfo.v1.cardinality == 0x7FFFFFFF);
  1895. // only check for audio plugins
  1896. if (std::strcmp(classInfo.v1.category, "Audio Module Class") != 0)
  1897. continue;
  1898. // FIXME multi-plugin bundle
  1899. break;
  1900. }
  1901. return true;
  1902. }
  1903. bool initializePlugin(const v3_tuid uid, v3_funknown** const hostContext)
  1904. {
  1905. // create instance
  1906. void* instance = nullptr;
  1907. CARLA_SAFE_ASSERT_RETURN(v3_cpp_obj(factory1)->create_instance(factory1, uid, v3_component_iid,
  1908. &instance) == V3_OK,
  1909. exit());
  1910. CARLA_SAFE_ASSERT_RETURN(instance != nullptr, exit());
  1911. component = static_cast<v3_component**>(instance);
  1912. // initialize instance
  1913. CARLA_SAFE_ASSERT_RETURN(v3_cpp_obj_initialize(component, hostContext) == V3_OK, exit());
  1914. shouldTerminateComponent = true;
  1915. // create edit controller
  1916. if (v3_cpp_obj_query_interface(component, v3_edit_controller_iid, &controller) != V3_OK)
  1917. controller = nullptr;
  1918. // if we cannot cast from component, try to create edit controller from factory
  1919. if (controller == nullptr)
  1920. {
  1921. v3_tuid cuid = {};
  1922. if (v3_cpp_obj(component)->get_controller_class_id(component, cuid) == V3_OK)
  1923. {
  1924. instance = nullptr;
  1925. if (v3_cpp_obj(factory1)->create_instance(factory1, cuid, v3_edit_controller_iid, &instance) == V3_OK && instance != nullptr)
  1926. controller = static_cast<v3_edit_controller**>(instance);
  1927. }
  1928. CARLA_SAFE_ASSERT_RETURN(controller != nullptr, exit());
  1929. // component is separate from controller, needs its dedicated initialize and terminate
  1930. CARLA_SAFE_ASSERT_RETURN(v3_cpp_obj_initialize(controller, hostContext) == V3_OK, exit());
  1931. shouldTerminateController = true;
  1932. }
  1933. // create processor
  1934. CARLA_SAFE_ASSERT_RETURN(v3_cpp_obj_query_interface(component, v3_audio_processor_iid,
  1935. &processor) == V3_OK, exit());
  1936. CARLA_SAFE_ASSERT_RETURN(processor != nullptr, exit());
  1937. // create view, ignoring result
  1938. view = v3_cpp_obj(controller)->create_view(controller, "view");
  1939. return true;
  1940. }
  1941. bool exit()
  1942. {
  1943. // must be deleted by now
  1944. CARLA_SAFE_ASSERT(view == nullptr);
  1945. if (processor != nullptr)
  1946. {
  1947. v3_cpp_obj_unref(processor);
  1948. processor = nullptr;
  1949. }
  1950. if (controller != nullptr)
  1951. {
  1952. if (shouldTerminateController)
  1953. {
  1954. v3_cpp_obj_terminate(controller);
  1955. shouldTerminateController = false;
  1956. }
  1957. v3_cpp_obj_unref(controller);
  1958. component = nullptr;
  1959. }
  1960. if (component != nullptr)
  1961. {
  1962. if (shouldTerminateComponent)
  1963. {
  1964. v3_cpp_obj_terminate(component);
  1965. shouldTerminateComponent = false;
  1966. }
  1967. v3_cpp_obj_unref(component);
  1968. component = nullptr;
  1969. }
  1970. if (factory3 != nullptr)
  1971. {
  1972. v3_cpp_obj_unref(factory3);
  1973. factory3 = nullptr;
  1974. }
  1975. if (factory2 != nullptr)
  1976. {
  1977. v3_cpp_obj_unref(factory2);
  1978. factory2 = nullptr;
  1979. }
  1980. if (factory1 != nullptr)
  1981. {
  1982. v3_cpp_obj_unref(factory1);
  1983. factory1 = nullptr;
  1984. }
  1985. if (exitfn != nullptr)
  1986. {
  1987. exitfn();
  1988. exitfn = nullptr;
  1989. }
  1990. // return false so it can be used as error/fail condition
  1991. return false;
  1992. }
  1993. CARLA_DECLARE_NON_COPYABLE(Pointers)
  1994. } fV3;
  1995. struct Events {
  1996. carla_v3_input_param_changes* paramInputs;
  1997. carla_v3_output_param_changes* paramOutputs;
  1998. carla_v3_input_event_list* eventInputs;
  1999. carla_v3_output_event_list* eventOutputs;
  2000. Events() noexcept
  2001. : paramInputs(nullptr),
  2002. paramOutputs(nullptr),
  2003. eventInputs(nullptr),
  2004. eventOutputs(nullptr) {}
  2005. ~Events()
  2006. {
  2007. delete paramInputs;
  2008. delete paramOutputs;
  2009. delete eventInputs;
  2010. delete eventOutputs;
  2011. }
  2012. void init()
  2013. {
  2014. if (paramInputs != nullptr)
  2015. paramInputs->init();
  2016. if (eventInputs != nullptr)
  2017. eventInputs->numEvents = 0;
  2018. }
  2019. void prepare()
  2020. {
  2021. if (paramInputs != nullptr)
  2022. paramInputs->prepare();
  2023. }
  2024. CARLA_DECLARE_NON_COPYABLE(Events)
  2025. } fEvents;
  2026. struct UI {
  2027. bool isAttached;
  2028. bool isEmbed;
  2029. bool isVisible;
  2030. CarlaPluginUI* window;
  2031. UI() noexcept
  2032. : isAttached(false),
  2033. isEmbed(false),
  2034. isVisible(false),
  2035. window(nullptr) {}
  2036. ~UI()
  2037. {
  2038. CARLA_ASSERT(isEmbed || ! isVisible);
  2039. if (window != nullptr)
  2040. {
  2041. delete window;
  2042. window = nullptr;
  2043. }
  2044. }
  2045. CARLA_DECLARE_NON_COPYABLE(UI)
  2046. } fUI;
  2047. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaPluginVST3)
  2048. };
  2049. // --------------------------------------------------------------------------------------------------------------------
  2050. CarlaPluginPtr CarlaPlugin::newVST3(const Initializer& init)
  2051. {
  2052. carla_debug("CarlaPlugin::newVST3({%p, \"%s\", \"%s\", \"%s\"})",
  2053. init.engine, init.filename, init.name, init.label);
  2054. #ifdef USE_JUCE_FOR_VST3
  2055. if (std::getenv("CARLA_DO_NOT_USE_JUCE_FOR_VST3") == nullptr)
  2056. return newJuce(init, "VST3");
  2057. #endif
  2058. std::shared_ptr<CarlaPluginVST3> plugin(new CarlaPluginVST3(init.engine, init.id));
  2059. if (! plugin->init(plugin, init.filename, init.name, init.label, init.options))
  2060. return nullptr;
  2061. return plugin;
  2062. }
  2063. // -------------------------------------------------------------------------------------------------------------------
  2064. CARLA_BACKEND_END_NAMESPACE