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

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