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.

CarlaPlugin.cpp 85KB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
9 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614
  1. /*
  2. * Carla Plugin
  3. * Copyright (C) 2011-2018 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #include "CarlaPluginInternal.hpp"
  18. #include "CarlaEngine.hpp"
  19. #include "CarlaBackendUtils.hpp"
  20. #include "CarlaBase64Utils.hpp"
  21. #include "CarlaMathUtils.hpp"
  22. #include "CarlaPluginUI.hpp"
  23. #include "CarlaStringList.hpp"
  24. #include <ctime>
  25. #include "water/files/File.h"
  26. #include "water/streams/MemoryOutputStream.h"
  27. #include "water/xml/XmlDocument.h"
  28. #include "water/xml/XmlElement.h"
  29. using water::CharPointer_UTF8;
  30. using water::File;
  31. using water::MemoryOutputStream;
  32. using water::Result;
  33. using water::String;
  34. using water::XmlDocument;
  35. using water::XmlElement;
  36. CARLA_BACKEND_START_NAMESPACE
  37. // -------------------------------------------------------------------
  38. // Fallback data
  39. static const ParameterData kParameterDataNull = { PARAMETER_UNKNOWN, 0x0, PARAMETER_NULL, -1, -1, 0 };
  40. static const ParameterRanges kParameterRangesNull = { 0.0f, 0.0f, 1.0f, 0.01f, 0.0001f, 0.1f };
  41. static const MidiProgramData kMidiProgramDataNull = { 0, 0, nullptr };
  42. static const CustomData kCustomDataFallback = { nullptr, nullptr, nullptr };
  43. static /* */ CustomData kCustomDataFallbackNC = { nullptr, nullptr, nullptr };
  44. static const PluginPostRtEvent kPluginPostRtEventFallback = { kPluginPostRtEventNull, 0, 0, 0.0f };
  45. // -------------------------------------------------------------------
  46. // ParamSymbol struct, needed for CarlaPlugin::loadStateSave()
  47. struct ParamSymbol {
  48. int32_t index;
  49. const char* symbol;
  50. ParamSymbol(const uint32_t i, const char* const s)
  51. : index(static_cast<int32_t>(i)),
  52. symbol(carla_strdup(s)) {}
  53. ~ParamSymbol() noexcept
  54. {
  55. CARLA_SAFE_ASSERT_RETURN(symbol != nullptr,)
  56. delete[] symbol;
  57. symbol = nullptr;
  58. }
  59. #ifdef CARLA_PROPER_CPP11_SUPPORT
  60. ParamSymbol() = delete;
  61. CARLA_DECLARE_NON_COPY_STRUCT(ParamSymbol)
  62. #endif
  63. };
  64. // -------------------------------------------------------------------
  65. // Constructor and destructor
  66. CarlaPlugin::CarlaPlugin(CarlaEngine* const engine, const uint id)
  67. : pData(new ProtectedData(engine, id))
  68. {
  69. CARLA_SAFE_ASSERT_RETURN(engine != nullptr,);
  70. CARLA_SAFE_ASSERT(id < engine->getMaxPluginNumber());
  71. carla_debug("CarlaPlugin::CarlaPlugin(%p, %i)", engine, id);
  72. switch (engine->getProccessMode())
  73. {
  74. case ENGINE_PROCESS_MODE_SINGLE_CLIENT:
  75. case ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS:
  76. CARLA_SAFE_ASSERT(id < MAX_DEFAULT_PLUGINS);
  77. break;
  78. case ENGINE_PROCESS_MODE_CONTINUOUS_RACK:
  79. CARLA_SAFE_ASSERT(id < MAX_RACK_PLUGINS);
  80. break;
  81. case ENGINE_PROCESS_MODE_PATCHBAY:
  82. CARLA_SAFE_ASSERT(id < MAX_PATCHBAY_PLUGINS);
  83. break;
  84. case ENGINE_PROCESS_MODE_BRIDGE:
  85. CARLA_SAFE_ASSERT(id == 0);
  86. break;
  87. }
  88. }
  89. CarlaPlugin::~CarlaPlugin()
  90. {
  91. carla_debug("CarlaPlugin::~CarlaPlugin()");
  92. delete pData;
  93. }
  94. // -------------------------------------------------------------------
  95. // Information (base)
  96. uint CarlaPlugin::getId() const noexcept
  97. {
  98. return pData->id;
  99. }
  100. uint CarlaPlugin::getHints() const noexcept
  101. {
  102. return pData->hints;
  103. }
  104. uint CarlaPlugin::getOptionsEnabled() const noexcept
  105. {
  106. return pData->options;
  107. }
  108. bool CarlaPlugin::isEnabled() const noexcept
  109. {
  110. return pData->enabled;
  111. }
  112. const char* CarlaPlugin::getName() const noexcept
  113. {
  114. return pData->name;
  115. }
  116. const char* CarlaPlugin::getFilename() const noexcept
  117. {
  118. return pData->filename;
  119. }
  120. const char* CarlaPlugin::getIconName() const noexcept
  121. {
  122. return pData->iconName;
  123. }
  124. PluginCategory CarlaPlugin::getCategory() const noexcept
  125. {
  126. return getPluginCategoryFromName(pData->name);
  127. }
  128. int64_t CarlaPlugin::getUniqueId() const noexcept
  129. {
  130. return 0;
  131. }
  132. uint32_t CarlaPlugin::getLatencyInFrames() const noexcept
  133. {
  134. return 0;
  135. }
  136. // -------------------------------------------------------------------
  137. // Information (count)
  138. uint32_t CarlaPlugin::getAudioInCount() const noexcept
  139. {
  140. return pData->audioIn.count;
  141. }
  142. uint32_t CarlaPlugin::getAudioOutCount() const noexcept
  143. {
  144. return pData->audioOut.count;
  145. }
  146. uint32_t CarlaPlugin::getCVInCount() const noexcept
  147. {
  148. return pData->cvIn.count;
  149. }
  150. uint32_t CarlaPlugin::getCVOutCount() const noexcept
  151. {
  152. return pData->cvOut.count;
  153. }
  154. uint32_t CarlaPlugin::getMidiInCount() const noexcept
  155. {
  156. return (pData->extraHints & PLUGIN_EXTRA_HINT_HAS_MIDI_IN) ? 1 : 0;
  157. }
  158. uint32_t CarlaPlugin::getMidiOutCount() const noexcept
  159. {
  160. return (pData->extraHints & PLUGIN_EXTRA_HINT_HAS_MIDI_OUT) ? 1 : 0;
  161. }
  162. uint32_t CarlaPlugin::getParameterCount() const noexcept
  163. {
  164. return pData->param.count;
  165. }
  166. uint32_t CarlaPlugin::getParameterScalePointCount(const uint32_t parameterId) const noexcept
  167. {
  168. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0);
  169. return 0;
  170. }
  171. uint32_t CarlaPlugin::getProgramCount() const noexcept
  172. {
  173. return pData->prog.count;
  174. }
  175. uint32_t CarlaPlugin::getMidiProgramCount() const noexcept
  176. {
  177. return pData->midiprog.count;
  178. }
  179. uint32_t CarlaPlugin::getCustomDataCount() const noexcept
  180. {
  181. return static_cast<uint32_t>(pData->custom.count());
  182. }
  183. // -------------------------------------------------------------------
  184. // Information (current data)
  185. int32_t CarlaPlugin::getCurrentProgram() const noexcept
  186. {
  187. return pData->prog.current;
  188. }
  189. int32_t CarlaPlugin::getCurrentMidiProgram() const noexcept
  190. {
  191. return pData->midiprog.current;
  192. }
  193. const ParameterData& CarlaPlugin::getParameterData(const uint32_t parameterId) const noexcept
  194. {
  195. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, kParameterDataNull);
  196. return pData->param.data[parameterId];
  197. }
  198. const ParameterRanges& CarlaPlugin::getParameterRanges(const uint32_t parameterId) const noexcept
  199. {
  200. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, kParameterRangesNull);
  201. return pData->param.ranges[parameterId];
  202. }
  203. bool CarlaPlugin::isParameterOutput(const uint32_t parameterId) const noexcept
  204. {
  205. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, false);
  206. return (pData->param.data[parameterId].type == PARAMETER_OUTPUT);
  207. }
  208. const MidiProgramData& CarlaPlugin::getMidiProgramData(const uint32_t index) const noexcept
  209. {
  210. CARLA_SAFE_ASSERT_RETURN(index < pData->midiprog.count, kMidiProgramDataNull);
  211. return pData->midiprog.data[index];
  212. }
  213. const CustomData& CarlaPlugin::getCustomData(const uint32_t index) const noexcept
  214. {
  215. return pData->custom.getAt(index, kCustomDataFallback);
  216. }
  217. std::size_t CarlaPlugin::getChunkData(void** const dataPtr) noexcept
  218. {
  219. CARLA_SAFE_ASSERT_RETURN(dataPtr != nullptr, 0);
  220. CARLA_SAFE_ASSERT(false); // this should never happen
  221. return 0;
  222. }
  223. // -------------------------------------------------------------------
  224. // Information (per-plugin data)
  225. uint CarlaPlugin::getOptionsAvailable() const noexcept
  226. {
  227. CARLA_SAFE_ASSERT(false); // this should never happen
  228. return 0x0;
  229. }
  230. float CarlaPlugin::getParameterValue(const uint32_t parameterId) const noexcept
  231. {
  232. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(), 0.0f);
  233. CARLA_SAFE_ASSERT(false); // this should never happen
  234. return 0.0f;
  235. }
  236. float CarlaPlugin::getParameterScalePointValue(const uint32_t parameterId, const uint32_t scalePointId) const noexcept
  237. {
  238. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(), 0.0f);
  239. CARLA_SAFE_ASSERT_RETURN(scalePointId < getParameterScalePointCount(parameterId), 0.0f);
  240. CARLA_SAFE_ASSERT(false); // this should never happen
  241. return 0.0f;
  242. }
  243. void CarlaPlugin::getLabel(char* const strBuf) const noexcept
  244. {
  245. strBuf[0] = '\0';
  246. }
  247. void CarlaPlugin::getMaker(char* const strBuf) const noexcept
  248. {
  249. strBuf[0] = '\0';
  250. }
  251. void CarlaPlugin::getCopyright(char* const strBuf) const noexcept
  252. {
  253. strBuf[0] = '\0';
  254. }
  255. void CarlaPlugin::getRealName(char* const strBuf) const noexcept
  256. {
  257. strBuf[0] = '\0';
  258. }
  259. void CarlaPlugin::getParameterName(const uint32_t parameterId, char* const strBuf) const noexcept
  260. {
  261. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(),);
  262. CARLA_SAFE_ASSERT(false); // this should never happen
  263. strBuf[0] = '\0';
  264. }
  265. void CarlaPlugin::getParameterSymbol(const uint32_t parameterId, char* const strBuf) const noexcept
  266. {
  267. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(),);
  268. strBuf[0] = '\0';
  269. }
  270. void CarlaPlugin::getParameterText(const uint32_t parameterId, char* const strBuf) noexcept
  271. {
  272. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(),);
  273. CARLA_SAFE_ASSERT(false); // this should never happen
  274. strBuf[0] = '\0';
  275. }
  276. void CarlaPlugin::getParameterUnit(const uint32_t parameterId, char* const strBuf) const noexcept
  277. {
  278. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(),);
  279. strBuf[0] = '\0';
  280. }
  281. void CarlaPlugin::getParameterScalePointLabel(const uint32_t parameterId, const uint32_t scalePointId, char* const strBuf) const noexcept
  282. {
  283. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(),);
  284. CARLA_SAFE_ASSERT_RETURN(scalePointId < getParameterScalePointCount(parameterId),);
  285. CARLA_SAFE_ASSERT(false); // this should never happen
  286. strBuf[0] = '\0';
  287. }
  288. float CarlaPlugin::getInternalParameterValue(const int32_t parameterId) const noexcept
  289. {
  290. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  291. CARLA_SAFE_ASSERT_RETURN(parameterId != PARAMETER_NULL && parameterId > PARAMETER_MAX, 0.0f);
  292. switch (parameterId)
  293. {
  294. case PARAMETER_ACTIVE:
  295. return pData->active;
  296. case PARAMETER_CTRL_CHANNEL:
  297. return pData->ctrlChannel;
  298. case PARAMETER_DRYWET:
  299. return pData->postProc.dryWet;
  300. case PARAMETER_VOLUME:
  301. return pData->postProc.volume;
  302. case PARAMETER_BALANCE_LEFT:
  303. return pData->postProc.balanceLeft;
  304. case PARAMETER_BALANCE_RIGHT:
  305. return pData->postProc.balanceRight;
  306. case PARAMETER_PANNING:
  307. return pData->postProc.panning;
  308. };
  309. #endif
  310. CARLA_SAFE_ASSERT_RETURN(parameterId >= 0, 0.0f);
  311. return getParameterValue(static_cast<uint32_t>(parameterId));
  312. }
  313. void CarlaPlugin::getProgramName(const uint32_t index, char* const strBuf) const noexcept
  314. {
  315. CARLA_SAFE_ASSERT_RETURN(index < pData->prog.count,);
  316. CARLA_SAFE_ASSERT_RETURN(pData->prog.names[index] != nullptr,);
  317. std::strncpy(strBuf, pData->prog.names[index], STR_MAX);
  318. }
  319. void CarlaPlugin::getMidiProgramName(const uint32_t index, char* const strBuf) const noexcept
  320. {
  321. CARLA_SAFE_ASSERT_RETURN(index < pData->midiprog.count,);
  322. CARLA_SAFE_ASSERT_RETURN(pData->midiprog.data[index].name != nullptr,);
  323. std::strncpy(strBuf, pData->midiprog.data[index].name, STR_MAX);
  324. }
  325. void CarlaPlugin::getParameterCountInfo(uint32_t& ins, uint32_t& outs) const noexcept
  326. {
  327. ins = 0;
  328. outs = 0;
  329. for (uint32_t i=0; i < pData->param.count; ++i)
  330. {
  331. if (pData->param.data[i].type == PARAMETER_INPUT)
  332. ++ins;
  333. else if (pData->param.data[i].type == PARAMETER_OUTPUT)
  334. ++outs;
  335. }
  336. }
  337. // -------------------------------------------------------------------
  338. // Set data (state)
  339. void CarlaPlugin::prepareForSave()
  340. {
  341. }
  342. void CarlaPlugin::resetParameters() noexcept
  343. {
  344. for (uint i=0; i < pData->param.count; ++i)
  345. {
  346. const ParameterData& paramData(pData->param.data[i]);
  347. const ParameterRanges& paramRanges(pData->param.ranges[i]);
  348. if (paramData.type != PARAMETER_INPUT)
  349. continue;
  350. if ((paramData.hints & PARAMETER_IS_ENABLED) == 0)
  351. continue;
  352. setParameterValue(i, paramRanges.def, true, true, true);
  353. }
  354. }
  355. void CarlaPlugin::randomizeParameters() noexcept
  356. {
  357. float value, random;
  358. char strBuf[STR_MAX+1];
  359. strBuf[STR_MAX] = '\0';
  360. std::srand(static_cast<uint>(std::time(nullptr)));
  361. for (uint i=0; i < pData->param.count; ++i)
  362. {
  363. const ParameterData& paramData(pData->param.data[i]);
  364. if (paramData.type != PARAMETER_INPUT)
  365. continue;
  366. if ((paramData.hints & PARAMETER_IS_ENABLED) == 0)
  367. continue;
  368. getParameterName(i, strBuf);
  369. if (std::strstr(strBuf, "olume") != nullptr)
  370. continue;
  371. if (std::strstr(strBuf, "Master") != nullptr)
  372. continue;
  373. const ParameterRanges& paramRanges(pData->param.ranges[i]);
  374. if (paramData.hints & PARAMETER_IS_BOOLEAN)
  375. {
  376. random = static_cast<float>(std::rand()) / static_cast<float>(RAND_MAX);
  377. value = random > 0.5 ? paramRanges.max : paramRanges.min;
  378. }
  379. else
  380. {
  381. random = static_cast<float>(std::rand()) / static_cast<float>(RAND_MAX);
  382. value = random * (paramRanges.max - paramRanges.min) + paramRanges.min;
  383. if (paramData.hints & PARAMETER_IS_INTEGER)
  384. value = std::rint(value);
  385. }
  386. setParameterValue(i, value, true, true, true);
  387. }
  388. }
  389. const CarlaStateSave& CarlaPlugin::getStateSave(const bool callPrepareForSave)
  390. {
  391. if (callPrepareForSave)
  392. prepareForSave();
  393. pData->stateSave.clear();
  394. const PluginType pluginType(getType());
  395. char strBuf[STR_MAX+1];
  396. // ---------------------------------------------------------------
  397. // Basic info
  398. getLabel(strBuf);
  399. pData->stateSave.type = carla_strdup(getPluginTypeAsString(pluginType));
  400. pData->stateSave.name = carla_strdup(pData->name);
  401. pData->stateSave.label = carla_strdup(strBuf);
  402. pData->stateSave.uniqueId = getUniqueId();
  403. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  404. pData->stateSave.options = pData->options;
  405. #endif
  406. if (pData->filename != nullptr)
  407. pData->stateSave.binary = carla_strdup(pData->filename);
  408. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  409. // ---------------------------------------------------------------
  410. // Internals
  411. pData->stateSave.active = pData->active;
  412. pData->stateSave.dryWet = pData->postProc.dryWet;
  413. pData->stateSave.volume = pData->postProc.volume;
  414. pData->stateSave.balanceLeft = pData->postProc.balanceLeft;
  415. pData->stateSave.balanceRight = pData->postProc.balanceRight;
  416. pData->stateSave.panning = pData->postProc.panning;
  417. pData->stateSave.ctrlChannel = pData->ctrlChannel;
  418. #endif
  419. bool usingChunk = false;
  420. // ---------------------------------------------------------------
  421. // Chunk
  422. if (pData->options & PLUGIN_OPTION_USE_CHUNKS)
  423. {
  424. void* data = nullptr;
  425. const std::size_t dataSize(getChunkData(&data));
  426. if (data != nullptr && dataSize > 0)
  427. {
  428. pData->stateSave.chunk = CarlaString::asBase64(data, dataSize).dup();
  429. if (pluginType != PLUGIN_INTERNAL)
  430. usingChunk = true;
  431. }
  432. }
  433. // ---------------------------------------------------------------
  434. // Current Program
  435. if (pData->prog.current >= 0 && pluginType != PLUGIN_LV2)
  436. {
  437. pData->stateSave.currentProgramIndex = pData->prog.current;
  438. pData->stateSave.currentProgramName = carla_strdup(pData->prog.names[pData->prog.current]);
  439. }
  440. // ---------------------------------------------------------------
  441. // Current MIDI Program
  442. if (pData->midiprog.current >= 0 && pluginType != PLUGIN_LV2 && pluginType != PLUGIN_SF2)
  443. {
  444. const MidiProgramData& mpData(pData->midiprog.getCurrent());
  445. pData->stateSave.currentMidiBank = static_cast<int32_t>(mpData.bank);
  446. pData->stateSave.currentMidiProgram = static_cast<int32_t>(mpData.program);
  447. }
  448. // ---------------------------------------------------------------
  449. // Parameters
  450. const float sampleRate(static_cast<float>(pData->engine->getSampleRate()));
  451. for (uint32_t i=0; i < pData->param.count; ++i)
  452. {
  453. const ParameterData& paramData(pData->param.data[i]);
  454. if ((paramData.hints & PARAMETER_IS_ENABLED) == 0)
  455. continue;
  456. const bool dummy = paramData.type != PARAMETER_INPUT || usingChunk;
  457. if (dummy && paramData.midiCC <= -1)
  458. continue;
  459. CarlaStateSave::Parameter* const stateParameter(new CarlaStateSave::Parameter());
  460. stateParameter->dummy = dummy;
  461. stateParameter->index = paramData.index;
  462. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  463. stateParameter->midiCC = paramData.midiCC;
  464. stateParameter->midiChannel = paramData.midiChannel;
  465. #endif
  466. getParameterName(i, strBuf);
  467. stateParameter->name = carla_strdup(strBuf);
  468. getParameterSymbol(i, strBuf);
  469. stateParameter->symbol = carla_strdup(strBuf);;
  470. if (! dummy)
  471. {
  472. stateParameter->value = getParameterValue(i);
  473. if (paramData.hints & PARAMETER_USES_SAMPLERATE)
  474. stateParameter->value /= sampleRate;
  475. }
  476. pData->stateSave.parameters.append(stateParameter);
  477. }
  478. // ---------------------------------------------------------------
  479. // Custom Data
  480. if (pData->hints & PLUGIN_IS_BRIDGE)
  481. waitForBridgeSaveSignal();
  482. for (LinkedList<CustomData>::Itenerator it = pData->custom.begin2(); it.valid(); it.next())
  483. {
  484. const CustomData& cData(it.getValue(kCustomDataFallback));
  485. CARLA_SAFE_ASSERT_CONTINUE(cData.isValid());
  486. CarlaStateSave::CustomData* stateCustomData(new CarlaStateSave::CustomData());
  487. stateCustomData->type = carla_strdup(cData.type);
  488. stateCustomData->key = carla_strdup(cData.key);
  489. stateCustomData->value = carla_strdup(cData.value);
  490. pData->stateSave.customData.append(stateCustomData);
  491. }
  492. return pData->stateSave;
  493. }
  494. void CarlaPlugin::loadStateSave(const CarlaStateSave& stateSave)
  495. {
  496. char strBuf[STR_MAX+1];
  497. const bool usesMultiProgs(pData->hints & PLUGIN_USES_MULTI_PROGS);
  498. const PluginType pluginType(getType());
  499. // ---------------------------------------------------------------
  500. // Part 1 - PRE-set custom data (only those which reload programs)
  501. for (CarlaStateSave::CustomDataItenerator it = stateSave.customData.begin2(); it.valid(); it.next())
  502. {
  503. const CarlaStateSave::CustomData* const stateCustomData(it.getValue(nullptr));
  504. CARLA_SAFE_ASSERT_CONTINUE(stateCustomData != nullptr);
  505. CARLA_SAFE_ASSERT_CONTINUE(stateCustomData->isValid());
  506. const char* const key(stateCustomData->key);
  507. /**/ if (pluginType == PLUGIN_DSSI && (std::strcmp (key, "reloadprograms") == 0 ||
  508. std::strcmp (key, "load" ) == 0 ||
  509. std::strncmp(key, "patches", 7) == 0 ))
  510. pass();
  511. else if (usesMultiProgs && std::strcmp(key, "midiPrograms") == 0)
  512. pass();
  513. else
  514. continue;
  515. setCustomData(stateCustomData->type, key, stateCustomData->value, true);
  516. }
  517. // ---------------------------------------------------------------
  518. // Part 2 - set program
  519. if (stateSave.currentProgramIndex >= 0 && stateSave.currentProgramName != nullptr)
  520. {
  521. int32_t programId = -1;
  522. // index < count
  523. if (stateSave.currentProgramIndex < static_cast<int32_t>(pData->prog.count))
  524. {
  525. programId = stateSave.currentProgramIndex;
  526. }
  527. // index not valid, try to find by name
  528. else
  529. {
  530. for (uint32_t i=0; i < pData->prog.count; ++i)
  531. {
  532. strBuf[0] = '\0';
  533. getProgramName(i, strBuf);
  534. if (strBuf[0] != '\0' && std::strcmp(stateSave.currentProgramName, strBuf) == 0)
  535. {
  536. programId = static_cast<int32_t>(i);
  537. break;
  538. }
  539. }
  540. }
  541. // set program now, if valid
  542. if (programId >= 0)
  543. setProgram(programId, true, true, true);
  544. }
  545. // ---------------------------------------------------------------
  546. // Part 3 - set midi program
  547. if (stateSave.currentMidiBank >= 0 && stateSave.currentMidiProgram >= 0 && ! usesMultiProgs)
  548. setMidiProgramById(static_cast<uint32_t>(stateSave.currentMidiBank), static_cast<uint32_t>(stateSave.currentMidiProgram), true, true, true);
  549. // ---------------------------------------------------------------
  550. // Part 4a - get plugin parameter symbols
  551. LinkedList<ParamSymbol*> paramSymbols;
  552. if (pluginType == PLUGIN_LADSPA || pluginType == PLUGIN_LV2)
  553. {
  554. for (uint32_t i=0; i < pData->param.count; ++i)
  555. {
  556. strBuf[0] = '\0';
  557. getParameterSymbol(i, strBuf);
  558. if (strBuf[0] != '\0')
  559. {
  560. ParamSymbol* const paramSymbol(new ParamSymbol(i, strBuf));
  561. paramSymbols.append(paramSymbol);
  562. }
  563. }
  564. }
  565. // ---------------------------------------------------------------
  566. // Part 4b - set parameter values (carefully)
  567. const float sampleRate(static_cast<float>(pData->engine->getSampleRate()));
  568. for (CarlaStateSave::ParameterItenerator it = stateSave.parameters.begin2(); it.valid(); it.next())
  569. {
  570. CarlaStateSave::Parameter* const stateParameter(it.getValue(nullptr));
  571. CARLA_SAFE_ASSERT_CONTINUE(stateParameter != nullptr);
  572. int32_t index = -1;
  573. if (pluginType == PLUGIN_LADSPA)
  574. {
  575. // Try to set by symbol, otherwise use index
  576. if (stateParameter->symbol != nullptr && stateParameter->symbol[0] != '\0')
  577. {
  578. for (LinkedList<ParamSymbol*>::Itenerator it2 = paramSymbols.begin2(); it2.valid(); it2.next())
  579. {
  580. ParamSymbol* const paramSymbol(it2.getValue(nullptr));
  581. CARLA_SAFE_ASSERT_CONTINUE(paramSymbol != nullptr);
  582. CARLA_SAFE_ASSERT_CONTINUE(paramSymbol->symbol != nullptr);
  583. if (std::strcmp(stateParameter->symbol, paramSymbol->symbol) == 0)
  584. {
  585. index = paramSymbol->index;
  586. break;
  587. }
  588. }
  589. if (index == -1)
  590. index = stateParameter->index;
  591. }
  592. else
  593. {
  594. index = stateParameter->index;
  595. }
  596. }
  597. else if (pluginType == PLUGIN_LV2)
  598. {
  599. // Symbol only
  600. if (stateParameter->symbol != nullptr && stateParameter->symbol[0] != '\0')
  601. {
  602. for (LinkedList<ParamSymbol*>::Itenerator it2 = paramSymbols.begin2(); it2.valid(); it2.next())
  603. {
  604. ParamSymbol* const paramSymbol(it2.getValue(nullptr));
  605. CARLA_SAFE_ASSERT_CONTINUE(paramSymbol != nullptr);
  606. CARLA_SAFE_ASSERT_CONTINUE(paramSymbol->symbol != nullptr);
  607. if (std::strcmp(stateParameter->symbol, paramSymbol->symbol) == 0)
  608. {
  609. index = paramSymbol->index;
  610. break;
  611. }
  612. }
  613. if (index == -1)
  614. carla_stderr("Failed to find LV2 parameter symbol '%s' for '%s'",
  615. stateParameter->symbol, pData->name);
  616. }
  617. else
  618. {
  619. carla_stderr("LV2 Plugin parameter '%s' has no symbol", stateParameter->name);
  620. }
  621. }
  622. else
  623. {
  624. // Index only
  625. index = stateParameter->index;
  626. }
  627. // Now set parameter
  628. if (index >= 0 && index < static_cast<int32_t>(pData->param.count))
  629. {
  630. //CARLA_SAFE_ASSERT(stateParameter->isInput == (pData
  631. if (! stateParameter->dummy)
  632. {
  633. if (pData->param.data[index].hints & PARAMETER_USES_SAMPLERATE)
  634. stateParameter->value *= sampleRate;
  635. setParameterValue(static_cast<uint32_t>(index), stateParameter->value, true, true, true);
  636. }
  637. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  638. setParameterMidiCC(static_cast<uint32_t>(index), stateParameter->midiCC, true, true);
  639. setParameterMidiChannel(static_cast<uint32_t>(index), stateParameter->midiChannel, true, true);
  640. #endif
  641. }
  642. else
  643. carla_stderr("Could not set parameter '%s' value for '%s'",
  644. stateParameter->name, pData->name);
  645. }
  646. // ---------------------------------------------------------------
  647. // Part 4c - clear
  648. for (LinkedList<ParamSymbol*>::Itenerator it = paramSymbols.begin2(); it.valid(); it.next())
  649. {
  650. ParamSymbol* const paramSymbol(it.getValue(nullptr));
  651. delete paramSymbol;
  652. }
  653. paramSymbols.clear();
  654. // ---------------------------------------------------------------
  655. // Part 5 - set custom data
  656. for (CarlaStateSave::CustomDataItenerator it = stateSave.customData.begin2(); it.valid(); it.next())
  657. {
  658. const CarlaStateSave::CustomData* const stateCustomData(it.getValue(nullptr));
  659. CARLA_SAFE_ASSERT_CONTINUE(stateCustomData != nullptr);
  660. CARLA_SAFE_ASSERT_CONTINUE(stateCustomData->isValid());
  661. const char* const key(stateCustomData->key);
  662. if (pluginType == PLUGIN_DSSI && (std::strcmp (key, "reloadprograms") == 0 ||
  663. std::strcmp (key, "load" ) == 0 ||
  664. std::strncmp(key, "patches", 7) == 0 ))
  665. continue;
  666. if (usesMultiProgs && std::strcmp(key, "midiPrograms") == 0)
  667. continue;
  668. setCustomData(stateCustomData->type, key, stateCustomData->value, true);
  669. }
  670. // ---------------------------------------------------------------
  671. // Part 5x - set lv2 state
  672. if (pluginType == PLUGIN_LV2)
  673. {
  674. for (LinkedList<CustomData>::Itenerator it = pData->custom.begin2(); it.valid(); it.next())
  675. {
  676. const CustomData& customData(it.getValue(kCustomDataFallback));
  677. CARLA_SAFE_ASSERT_CONTINUE(customData.isValid());
  678. if (std::strcmp(customData.type, CUSTOM_DATA_TYPE_PROPERTY) == 0)
  679. continue;
  680. restoreLV2State();
  681. break;
  682. }
  683. }
  684. // ---------------------------------------------------------------
  685. // Part 6 - set chunk
  686. if (stateSave.chunk != nullptr && (pData->options & PLUGIN_OPTION_USE_CHUNKS) != 0)
  687. {
  688. std::vector<uint8_t> chunk(carla_getChunkFromBase64String(stateSave.chunk));
  689. #ifdef CARLA_PROPER_CPP11_SUPPORT
  690. setChunkData(chunk.data(), chunk.size());
  691. #else
  692. setChunkData(&chunk.front(), chunk.size());
  693. #endif
  694. }
  695. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  696. // ---------------------------------------------------------------
  697. // Part 6 - set internal stuff
  698. const uint availOptions(getOptionsAvailable());
  699. for (uint i=0; i<10; ++i) // FIXME - get this value somehow...
  700. {
  701. const uint option(1u << i);
  702. if (availOptions & option)
  703. setOption(option, (stateSave.options & option) != 0, true);
  704. }
  705. setDryWet(stateSave.dryWet, true, true);
  706. setVolume(stateSave.volume, true, true);
  707. setBalanceLeft(stateSave.balanceLeft, true, true);
  708. setBalanceRight(stateSave.balanceRight, true, true);
  709. setPanning(stateSave.panning, true, true);
  710. setCtrlChannel(stateSave.ctrlChannel, true, true);
  711. setActive(stateSave.active, true, true);
  712. if (! pData->engine->isLoadingProject())
  713. pData->engine->callback(ENGINE_CALLBACK_UPDATE, pData->id, 0, 0, 0.0f, nullptr);
  714. #endif
  715. }
  716. bool CarlaPlugin::saveStateToFile(const char* const filename)
  717. {
  718. CARLA_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', false);
  719. carla_debug("CarlaPlugin::saveStateToFile(\"%s\")", filename);
  720. MemoryOutputStream out, streamState;
  721. getStateSave().dumpToMemoryStream(streamState);
  722. out << "<?xml version='1.0' encoding='UTF-8'?>\n";
  723. out << "<!DOCTYPE CARLA-PRESET>\n";
  724. out << "<CARLA-PRESET VERSION='2.0'>\n";
  725. out << streamState;
  726. out << "</CARLA-PRESET>\n";
  727. const String jfilename = String(CharPointer_UTF8(filename));
  728. File file(jfilename);
  729. if (file.replaceWithData(out.getData(), out.getDataSize()))
  730. return true;
  731. pData->engine->setLastError("Failed to write file");
  732. return false;
  733. }
  734. bool CarlaPlugin::loadStateFromFile(const char* const filename)
  735. {
  736. // TODO set errors
  737. CARLA_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', false);
  738. carla_debug("CarlaPlugin::loadStateFromFile(\"%s\")", filename);
  739. const String jfilename = String(CharPointer_UTF8(filename));
  740. File file(jfilename);
  741. CARLA_SAFE_ASSERT_RETURN(file.existsAsFile(), false);
  742. XmlDocument xml(file);
  743. ScopedPointer<XmlElement> xmlElement(xml.getDocumentElement(true));
  744. CARLA_SAFE_ASSERT_RETURN(xmlElement != nullptr, false);
  745. CARLA_SAFE_ASSERT_RETURN(xmlElement->getTagName().equalsIgnoreCase("carla-preset"), false);
  746. // completely load file
  747. xmlElement = xml.getDocumentElement(false);
  748. CARLA_SAFE_ASSERT_RETURN(xmlElement != nullptr, false);
  749. if (pData->stateSave.fillFromXmlElement(xmlElement))
  750. {
  751. loadStateSave(pData->stateSave);
  752. return true;
  753. }
  754. return false;
  755. }
  756. bool CarlaPlugin::exportAsLV2(const char* const lv2path)
  757. {
  758. CARLA_SAFE_ASSERT_RETURN(lv2path != nullptr && lv2path[0] != '\0', false);
  759. carla_debug("CarlaPlugin::exportAsLV2(\"%s\")", lv2path);
  760. CarlaString bundlepath(lv2path);
  761. if (! bundlepath.endsWith(".lv2"))
  762. bundlepath += ".lv2";
  763. const File bundlefolder(bundlepath.buffer());
  764. if (bundlefolder.existsAsFile())
  765. {
  766. pData->engine->setLastError("Requested filename already exists as file, use a folder instead");
  767. return false;
  768. }
  769. if (! bundlefolder.exists())
  770. {
  771. const Result res(bundlefolder.createDirectory());
  772. if (res.failed())
  773. {
  774. pData->engine->setLastError(res.getErrorMessage().toRawUTF8());
  775. return false;
  776. }
  777. }
  778. CarlaString symbol(pData->name);
  779. symbol.toBasic();
  780. {
  781. const CarlaString pluginFilename(bundlepath + CARLA_OS_SEP_STR + symbol + ".xml");
  782. if (! saveStateToFile(pluginFilename))
  783. return false;
  784. }
  785. {
  786. MemoryOutputStream manifestStream;
  787. manifestStream << "@prefix lv2: <http://lv2plug.in/ns/lv2core#> .\n";
  788. manifestStream << "@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n";
  789. manifestStream << "@prefix ui: <http://lv2plug.in/ns/extensions/ui#> .\n";
  790. manifestStream << "\n";
  791. manifestStream << "<" << symbol.buffer() << ".ttl>\n";
  792. manifestStream << " a lv2:Plugin ;\n";
  793. manifestStream << " lv2:binary <" << symbol.buffer() << CARLA_LIB_EXT "> ;\n";
  794. manifestStream << " rdfs:seeAlso <" << symbol.buffer() << ".ttl> .\n";
  795. manifestStream << "\n";
  796. manifestStream << "<ext-ui>\n";
  797. manifestStream << " a <http://kxstudio.sf.net/ns/lv2ext/external-ui#Widget> ;\n";
  798. manifestStream << " ui:binary <" << symbol.buffer() << CARLA_LIB_EXT "> ;\n";
  799. manifestStream << " lv2:extensionData <http://lv2plug.in/ns/extensions/ui#idleInterface> ,\n";
  800. manifestStream << " <http://lv2plug.in/ns/extensions/ui#showInterface> ;\n";
  801. manifestStream << " lv2:requiredFeature <http://lv2plug.in/ns/ext/instance-access> .\n";
  802. manifestStream << "\n";
  803. const CarlaString manifestFilename(bundlepath + CARLA_OS_SEP_STR "manifest.ttl");
  804. const File manifestFile(manifestFilename.buffer());
  805. if (! manifestFile.replaceWithData(manifestStream.getData(), manifestStream.getDataSize()))
  806. {
  807. pData->engine->setLastError("Failed to write manifest.ttl file");
  808. return false;
  809. }
  810. }
  811. {
  812. MemoryOutputStream mainStream;
  813. mainStream << "@prefix atom: <http://lv2plug.in/ns/ext/atom#> .\n";
  814. mainStream << "@prefix doap: <http://usefulinc.com/ns/doap#> .\n";
  815. mainStream << "@prefix foaf: <http://xmlns.com/foaf/0.1/> .\n";
  816. mainStream << "@prefix lv2: <http://lv2plug.in/ns/lv2core#> .\n";
  817. mainStream << "@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n";
  818. mainStream << "@prefix ui: <http://lv2plug.in/ns/extensions/ui#> .\n";
  819. mainStream << "\n";
  820. mainStream << "<>\n";
  821. mainStream << " a lv2:Plugin ;\n";
  822. mainStream << "\n";
  823. mainStream << " lv2:requiredFeature <http://lv2plug.in/ns/ext/buf-size#boundedBlockLength> ,\n";
  824. mainStream << " <http://lv2plug.in/ns/ext/options#options> ,\n";
  825. mainStream << " <http://lv2plug.in/ns/ext/urid#map> ;\n";
  826. mainStream << "\n";
  827. if (pData->hints & PLUGIN_HAS_CUSTOM_UI)
  828. {
  829. mainStream << " ui:ui <ext-ui> ;\n";
  830. mainStream << "\n";
  831. }
  832. const uint32_t midiIns = getMidiInCount();
  833. const uint32_t midiOuts = getMidiOutCount();
  834. int portIndex = 0;
  835. if (midiIns > 0)
  836. {
  837. mainStream << " lv2:port [\n";
  838. mainStream << " a lv2:InputPort, atom:AtomPort ;\n";
  839. mainStream << " lv2:index 0 ;\n";
  840. mainStream << " lv2:symbol \"clv2_events_in\" ;\n";
  841. mainStream << " lv2:name \"Events Input\" ;\n";
  842. mainStream << " atom:bufferType atom:Sequence ;\n";
  843. mainStream << " atom:supports <http://lv2plug.in/ns/ext/midi#MidiEvent> ,\n";
  844. mainStream << " <http://lv2plug.in/ns/ext/time#Position> ;\n";
  845. mainStream << " ] ;\n";
  846. ++portIndex;
  847. for (uint32_t i=1; i<midiIns; ++i)
  848. {
  849. const String portIndexNum(portIndex++);
  850. const String portIndexLabel(portIndex);
  851. mainStream << " lv2:port [\n";
  852. mainStream << " a lv2:InputPort, atom:AtomPort ;\n";
  853. mainStream << " lv2:index " << portIndexNum << " ;\n";
  854. mainStream << " lv2:symbol \"clv2_midi_in_" << portIndexLabel << "\" ;\n";
  855. mainStream << " lv2:name \"MIDI Input " << portIndexLabel << "\" ;\n";
  856. mainStream << " ] ;\n";
  857. }
  858. }
  859. else
  860. {
  861. mainStream << " lv2:port [\n";
  862. mainStream << " a lv2:InputPort, atom:AtomPort ;\n";
  863. mainStream << " lv2:index 0 ;\n";
  864. mainStream << " lv2:symbol \"clv2_time_info\" ;\n";
  865. mainStream << " lv2:name \"Time Info\" ;\n";
  866. mainStream << " atom:bufferType atom:Sequence ;\n";
  867. mainStream << " atom:supports <http://lv2plug.in/ns/ext/time#Position> ;\n";
  868. mainStream << " ] ;\n";
  869. ++portIndex;
  870. }
  871. for (uint32_t i=0; i<midiOuts; ++i)
  872. {
  873. const String portIndexNum(portIndex++);
  874. const String portIndexLabel(portIndex);
  875. mainStream << " lv2:port [\n";
  876. mainStream << " a lv2:InputPort, atom:AtomPort ;\n";
  877. mainStream << " lv2:index " << portIndexNum << " ;\n";
  878. mainStream << " lv2:symbol \"clv2_midi_out_" << portIndexLabel << "\" ;\n";
  879. mainStream << " lv2:name \"MIDI Output " << portIndexLabel << "\" ;\n";
  880. mainStream << " atom:bufferType atom:Sequence ;\n";
  881. mainStream << " atom:supports <http://lv2plug.in/ns/ext/midi#MidiEvent> ;\n";
  882. mainStream << " ] ;\n";
  883. }
  884. mainStream << " lv2:port [\n";
  885. mainStream << " a lv2:InputPort, lv2:ControlPort ;\n";
  886. mainStream << " lv2:index " << String(portIndex++) << " ;\n";
  887. mainStream << " lv2:name \"freewheel\" ;\n";
  888. mainStream << " lv2:symbol \"clv2_freewheel\" ;\n";
  889. mainStream << " lv2:default 0 ;\n";
  890. mainStream << " lv2:minimum 0 ;\n";
  891. mainStream << " lv2:maximum 1 ;\n";
  892. mainStream << " lv2:designation lv2:freeWheeling ;\n";
  893. mainStream << " lv2:portProperty lv2:toggled , lv2:integer ;\n";
  894. mainStream << " lv2:portProperty <http://lv2plug.in/ns/ext/port-props#notOnGUI> ;\n";
  895. mainStream << " ] ;\n";
  896. for (uint32_t i=0; i<pData->audioIn.count; ++i)
  897. {
  898. const String portIndexNum(portIndex++);
  899. const String portIndexLabel(i+1);
  900. mainStream << " lv2:port [\n";
  901. mainStream << " a lv2:InputPort, lv2:AudioPort ;\n";
  902. mainStream << " lv2:index " << portIndexNum << " ;\n";
  903. mainStream << " lv2:symbol \"clv2_audio_in_" << portIndexLabel << "\" ;\n";
  904. mainStream << " lv2:name \"Audio Input " << portIndexLabel << "\" ;\n";
  905. mainStream << " ] ;\n";
  906. }
  907. for (uint32_t i=0; i<pData->audioOut.count; ++i)
  908. {
  909. const String portIndexNum(portIndex++);
  910. const String portIndexLabel(i+1);
  911. mainStream << " lv2:port [\n";
  912. mainStream << " a lv2:OutputPort, lv2:AudioPort ;\n";
  913. mainStream << " lv2:index " << portIndexNum << " ;\n";
  914. mainStream << " lv2:symbol \"clv2_audio_out_" << portIndexLabel << "\" ;\n";
  915. mainStream << " lv2:name \"Audio Output " << portIndexLabel << "\" ;\n";
  916. mainStream << " ] ;\n";
  917. }
  918. CarlaStringList uniqueSymbolNames;
  919. char strBufName[STR_MAX+1];
  920. char strBufSymbol[STR_MAX+1];
  921. strBufName[STR_MAX] = strBufSymbol[STR_MAX] = '\0';
  922. for (uint32_t i=0; i<pData->param.count; ++i)
  923. {
  924. const ParameterData& paramData(pData->param.data[i]);
  925. const ParameterRanges& paramRanges(pData->param.ranges[i]);
  926. const String portIndexNum(portIndex++);
  927. mainStream << " lv2:port [\n";
  928. if (paramData.type == PARAMETER_INPUT)
  929. mainStream << " a lv2:InputPort, lv2:ControlPort ;\n";
  930. else
  931. mainStream << " a lv2:OutputPort, lv2:ControlPort ;\n";
  932. if (paramData.hints & PARAMETER_IS_BOOLEAN)
  933. mainStream << " lv2:portProperty lv2:toggled ;\n";
  934. if (paramData.hints & PARAMETER_IS_INTEGER)
  935. mainStream << " lv2:portProperty lv2:integer ;\n";
  936. // TODO logarithmic, enabled (not on gui), automable, samplerate, scalepoints
  937. strBufName[0] = strBufSymbol[0] = '\0';
  938. getParameterName(i, strBufName);
  939. getParameterSymbol(i, strBufSymbol);
  940. if (strBufSymbol[0] == '\0')
  941. {
  942. CarlaString s(strBufName);
  943. s.toBasic();
  944. std::memcpy(strBufSymbol, s.buffer(), s.length()+1);
  945. if (strBufSymbol[0] >= '0' && strBufSymbol[0] <= '9')
  946. {
  947. const size_t len(std::strlen(strBufSymbol));
  948. std::memmove(strBufSymbol+1, strBufSymbol, len);
  949. strBufSymbol[0] = '_';
  950. strBufSymbol[len+1] = '\0';
  951. }
  952. }
  953. if (uniqueSymbolNames.contains(strBufSymbol))
  954. {
  955. std::snprintf(strBufSymbol, STR_MAX, "clv2_param_%d", i+1);
  956. strBufSymbol[STR_MAX] = '\0';
  957. }
  958. mainStream << " lv2:index " << portIndexNum << " ;\n";
  959. mainStream << " lv2:symbol \"" << strBufSymbol << "\" ;\n";
  960. mainStream << " lv2:name \"\"\"" << strBufName << "\"\"\" ;\n";
  961. mainStream << " lv2:default " << String(paramRanges.def) << " ;\n";
  962. mainStream << " lv2:minimum " << String(paramRanges.min) << " ;\n";
  963. mainStream << " lv2:maximum " << String(paramRanges.max) << " ;\n";
  964. // TODO midiCC, midiChannel
  965. mainStream << " ] ;\n";
  966. }
  967. char strBuf[STR_MAX];
  968. getMaker(strBuf);
  969. strBuf[STR_MAX-1] = '\0';
  970. mainStream << " rdfs:comment \"Plugin generated using Carla LV2 export.\" ;\n";
  971. mainStream << " doap:name \"\"\"" << getName() << "\"\"\" ;\n";
  972. mainStream << " doap:maintainer [ foaf:name \"\"\"" << strBuf << "\"\"\" ] .\n";
  973. mainStream << "\n";
  974. const CarlaString mainFilename(bundlepath + CARLA_OS_SEP_STR + symbol + ".ttl");
  975. const File mainFile(mainFilename.buffer());
  976. if (! mainFile.replaceWithData(mainStream.getData(), mainStream.getDataSize()))
  977. {
  978. pData->engine->setLastError("Failed to write main plugin ttl file");
  979. return false;
  980. }
  981. }
  982. const CarlaString binaryFilename(bundlepath + CARLA_OS_SEP_STR + symbol + CARLA_LIB_EXT);
  983. const File binaryFileSource(File::getSpecialLocation(File::currentExecutableFile).getSiblingFile("carla-bridge-lv2" CARLA_LIB_EXT));
  984. const File binaryFileTarget(binaryFilename.buffer());
  985. const EngineOptions& opts(pData->engine->getOptions());
  986. const CarlaString binFolderTarget(bundlepath + CARLA_OS_SEP_STR + "bin");
  987. const CarlaString resFolderTarget(bundlepath + CARLA_OS_SEP_STR + "res");
  988. #ifdef CARLA_OS_WIN
  989. if (! binaryFileSource.copyFileTo(binaryFileTarget))
  990. {
  991. pData->engine->setLastError("Failed to copy plugin binary");
  992. return false;
  993. }
  994. File(opts.resourceDir).copyDirectoryTo(File(resFolderTarget.buffer()));
  995. // Copying all the binaries is pointless, just go through the expected needed bits
  996. const File binFolder1(opts.binaryDir);
  997. const File binFolder2(binFolderTarget.buffer());
  998. binFolder2.createDirectory();
  999. static const char* files[] = {
  1000. "carla-bridge-native.exe",
  1001. "carla-bridge-win32.exe",
  1002. "carla-discovery-win32.exe",
  1003. "carla-discovery-win64.exe",
  1004. "libcarla_utils.dll"
  1005. };
  1006. for (int i=0; i<5; ++i)
  1007. binFolder1.getChildFile(files[i]).copyFileTo(binFolder2.getChildFile(files[i]));;
  1008. #else
  1009. if (! binaryFileSource.createSymbolicLink(binaryFileTarget, true))
  1010. {
  1011. pData->engine->setLastError("Failed to create symbolik link of plugin binary");
  1012. return false;
  1013. }
  1014. File(opts.binaryDir).createSymbolicLink(File(binFolderTarget.buffer()), true);
  1015. File(opts.resourceDir).createSymbolicLink(File(resFolderTarget.buffer()), true);
  1016. #endif
  1017. return true;
  1018. }
  1019. // -------------------------------------------------------------------
  1020. // Set data (internal stuff)
  1021. void CarlaPlugin::setId(const uint newId) noexcept
  1022. {
  1023. pData->id = newId;
  1024. }
  1025. void CarlaPlugin::setName(const char* const newName)
  1026. {
  1027. CARLA_SAFE_ASSERT_RETURN(newName != nullptr && newName[0] != '\0',);
  1028. if (pData->name != nullptr)
  1029. delete[] pData->name;
  1030. pData->name = carla_strdup(newName);
  1031. }
  1032. void CarlaPlugin::setOption(const uint option, const bool yesNo, const bool sendCallback)
  1033. {
  1034. CARLA_SAFE_ASSERT_RETURN(getOptionsAvailable() & option,);
  1035. if (yesNo)
  1036. pData->options |= option;
  1037. else
  1038. pData->options &= ~option;
  1039. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1040. if (sendCallback)
  1041. pData->engine->callback(ENGINE_CALLBACK_OPTION_CHANGED, pData->id, static_cast<int>(option), yesNo ? 1 : 0, 0.0f, nullptr);
  1042. #else
  1043. // unused
  1044. return; (void)sendCallback;
  1045. #endif
  1046. }
  1047. void CarlaPlugin::setEnabled(const bool yesNo) noexcept
  1048. {
  1049. if (pData->enabled == yesNo)
  1050. return;
  1051. pData->masterMutex.lock();
  1052. pData->enabled = yesNo;
  1053. if (yesNo && ! pData->client->isActive())
  1054. pData->client->activate();
  1055. pData->masterMutex.unlock();
  1056. }
  1057. void CarlaPlugin::setActive(const bool active, const bool sendOsc, const bool sendCallback) noexcept
  1058. {
  1059. if (pData->engineBridged) {
  1060. CARLA_SAFE_ASSERT_RETURN(!sendOsc && !sendCallback,);
  1061. } else if (pData->enginePlugin) {
  1062. // nothing here
  1063. } else {
  1064. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  1065. }
  1066. if (pData->active == active)
  1067. return;
  1068. {
  1069. const ScopedSingleProcessLocker spl(this, true);
  1070. if (active)
  1071. activate();
  1072. else
  1073. deactivate();
  1074. }
  1075. pData->active = active;
  1076. const float value = active ? 1.0f : 0.0f;
  1077. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1078. if (sendOsc && pData->engine->isOscControlRegistered())
  1079. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_ACTIVE, value);
  1080. #endif
  1081. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1082. if (sendCallback)
  1083. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_ACTIVE, 0, value, nullptr);
  1084. #endif
  1085. // maybe unused
  1086. return; (void)value;
  1087. }
  1088. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1089. void CarlaPlugin::setDryWet(const float value, const bool sendOsc, const bool sendCallback) noexcept
  1090. {
  1091. if (pData->engineBridged) {
  1092. CARLA_SAFE_ASSERT_RETURN(!sendOsc && !sendCallback,);
  1093. } else {
  1094. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  1095. }
  1096. CARLA_SAFE_ASSERT(value >= 0.0f && value <= 1.0f);
  1097. const float fixedValue(carla_fixedValue<float>(0.0f, 1.0f, value));
  1098. if (carla_isEqual(pData->postProc.dryWet, fixedValue))
  1099. return;
  1100. pData->postProc.dryWet = fixedValue;
  1101. # if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1102. if (sendOsc && pData->engine->isOscControlRegistered())
  1103. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_DRYWET, fixedValue);
  1104. # endif
  1105. if (sendCallback)
  1106. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_DRYWET, 0, fixedValue, nullptr);
  1107. }
  1108. void CarlaPlugin::setVolume(const float value, const bool sendOsc, const bool sendCallback) noexcept
  1109. {
  1110. if (pData->engineBridged) {
  1111. CARLA_SAFE_ASSERT_RETURN(!sendOsc && !sendCallback,);
  1112. } else {
  1113. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  1114. }
  1115. CARLA_SAFE_ASSERT(value >= 0.0f && value <= 1.27f);
  1116. const float fixedValue(carla_fixedValue<float>(0.0f, 1.27f, value));
  1117. if (carla_isEqual(pData->postProc.volume, fixedValue))
  1118. return;
  1119. pData->postProc.volume = fixedValue;
  1120. # if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1121. if (sendOsc && pData->engine->isOscControlRegistered())
  1122. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_VOLUME, fixedValue);
  1123. # endif
  1124. if (sendCallback)
  1125. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_VOLUME, 0, fixedValue, nullptr);
  1126. }
  1127. void CarlaPlugin::setBalanceLeft(const float value, const bool sendOsc, const bool sendCallback) noexcept
  1128. {
  1129. if (pData->engineBridged) {
  1130. CARLA_SAFE_ASSERT_RETURN(!sendOsc && !sendCallback,);
  1131. } else {
  1132. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  1133. }
  1134. CARLA_SAFE_ASSERT(value >= -1.0f && value <= 1.0f);
  1135. const float fixedValue(carla_fixedValue<float>(-1.0f, 1.0f, value));
  1136. if (carla_isEqual(pData->postProc.balanceLeft, fixedValue))
  1137. return;
  1138. pData->postProc.balanceLeft = fixedValue;
  1139. # if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1140. if (sendOsc && pData->engine->isOscControlRegistered())
  1141. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_BALANCE_LEFT, fixedValue);
  1142. # endif
  1143. if (sendCallback)
  1144. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_BALANCE_LEFT, 0, fixedValue, nullptr);
  1145. }
  1146. void CarlaPlugin::setBalanceRight(const float value, const bool sendOsc, const bool sendCallback) noexcept
  1147. {
  1148. if (pData->engineBridged) {
  1149. CARLA_SAFE_ASSERT_RETURN(!sendOsc && !sendCallback,);
  1150. } else {
  1151. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  1152. }
  1153. CARLA_SAFE_ASSERT(value >= -1.0f && value <= 1.0f);
  1154. const float fixedValue(carla_fixedValue<float>(-1.0f, 1.0f, value));
  1155. if (carla_isEqual(pData->postProc.balanceRight, fixedValue))
  1156. return;
  1157. pData->postProc.balanceRight = fixedValue;
  1158. # if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1159. if (sendOsc && pData->engine->isOscControlRegistered())
  1160. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_BALANCE_RIGHT, fixedValue);
  1161. # endif
  1162. if (sendCallback)
  1163. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_BALANCE_RIGHT, 0, fixedValue, nullptr);
  1164. }
  1165. void CarlaPlugin::setPanning(const float value, const bool sendOsc, const bool sendCallback) noexcept
  1166. {
  1167. if (pData->engineBridged) {
  1168. CARLA_SAFE_ASSERT_RETURN(!sendOsc && !sendCallback,);
  1169. } else {
  1170. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  1171. }
  1172. CARLA_SAFE_ASSERT(value >= -1.0f && value <= 1.0f);
  1173. const float fixedValue(carla_fixedValue<float>(-1.0f, 1.0f, value));
  1174. if (carla_isEqual(pData->postProc.panning, fixedValue))
  1175. return;
  1176. pData->postProc.panning = fixedValue;
  1177. # if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1178. if (sendOsc && pData->engine->isOscControlRegistered())
  1179. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_PANNING, fixedValue);
  1180. # endif
  1181. if (sendCallback)
  1182. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_PANNING, 0, fixedValue, nullptr);
  1183. }
  1184. void CarlaPlugin::setDryWetRT(const float value) noexcept
  1185. {
  1186. CARLA_SAFE_ASSERT(value >= 0.0f && value <= 1.0f);
  1187. const float fixedValue(carla_fixedValue<float>(0.0f, 1.0f, value));
  1188. if (carla_isEqual(pData->postProc.dryWet, fixedValue))
  1189. return;
  1190. pData->postProc.dryWet = fixedValue;
  1191. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, fixedValue);
  1192. }
  1193. void CarlaPlugin::setVolumeRT(const float value) noexcept
  1194. {
  1195. CARLA_SAFE_ASSERT(value >= 0.0f && value <= 1.27f);
  1196. const float fixedValue(carla_fixedValue<float>(0.0f, 1.27f, value));
  1197. if (carla_isEqual(pData->postProc.volume, fixedValue))
  1198. return;
  1199. pData->postProc.volume = fixedValue;
  1200. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, fixedValue);
  1201. }
  1202. void CarlaPlugin::setBalanceLeftRT(const float value) noexcept
  1203. {
  1204. CARLA_SAFE_ASSERT(value >= -1.0f && value <= 1.0f);
  1205. const float fixedValue(carla_fixedValue<float>(-1.0f, 1.0f, value));
  1206. if (carla_isEqual(pData->postProc.balanceLeft, fixedValue))
  1207. return;
  1208. pData->postProc.balanceLeft = fixedValue;
  1209. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, fixedValue);
  1210. }
  1211. void CarlaPlugin::setBalanceRightRT(const float value) noexcept
  1212. {
  1213. CARLA_SAFE_ASSERT(value >= -1.0f && value <= 1.0f);
  1214. const float fixedValue(carla_fixedValue<float>(-1.0f, 1.0f, value));
  1215. if (carla_isEqual(pData->postProc.balanceRight, fixedValue))
  1216. return;
  1217. pData->postProc.balanceRight = fixedValue;
  1218. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, fixedValue);
  1219. }
  1220. void CarlaPlugin::setPanningRT(const float value) noexcept
  1221. {
  1222. CARLA_SAFE_ASSERT(value >= -1.0f && value <= 1.0f);
  1223. const float fixedValue(carla_fixedValue<float>(-1.0f, 1.0f, value));
  1224. if (carla_isEqual(pData->postProc.panning, fixedValue))
  1225. return;
  1226. pData->postProc.panning = fixedValue;
  1227. }
  1228. #endif // ! BUILD_BRIDGE_ALTERNATIVE_ARCH
  1229. void CarlaPlugin::setCtrlChannel(const int8_t channel, const bool sendOsc, const bool sendCallback) noexcept
  1230. {
  1231. if (pData->engineBridged) {
  1232. CARLA_SAFE_ASSERT_RETURN(!sendOsc && !sendCallback,);
  1233. } else {
  1234. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  1235. }
  1236. CARLA_SAFE_ASSERT_RETURN(channel >= -1 && channel < MAX_MIDI_CHANNELS,);
  1237. if (pData->ctrlChannel == channel)
  1238. return;
  1239. pData->ctrlChannel = channel;
  1240. const float channelf = static_cast<float>(channel);
  1241. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1242. if (sendOsc && pData->engine->isOscControlRegistered())
  1243. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_CTRL_CHANNEL, channelf);
  1244. #endif
  1245. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1246. if (sendCallback)
  1247. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_CTRL_CHANNEL, 0, channelf, nullptr);
  1248. #endif
  1249. // maybe unused
  1250. return; (void)channelf;
  1251. }
  1252. // -------------------------------------------------------------------
  1253. // Set data (plugin-specific stuff)
  1254. void CarlaPlugin::setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept
  1255. {
  1256. if (pData->engineBridged) {
  1257. // NOTE: some LV2 plugins feedback messages to UI on purpose
  1258. CARLA_SAFE_ASSERT_RETURN(getType() == PLUGIN_LV2 || !sendGui,);
  1259. } else if (pData->enginePlugin) {
  1260. // nothing here
  1261. } else {
  1262. CARLA_SAFE_ASSERT_RETURN(sendGui || sendOsc || sendCallback,); // never call this from RT
  1263. }
  1264. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  1265. if (sendGui && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1266. uiParameterChange(parameterId, value);
  1267. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1268. if (sendOsc && pData->engine->isOscControlRegistered())
  1269. pData->engine->oscSend_control_set_parameter_value(pData->id, static_cast<int32_t>(parameterId), value);
  1270. #endif
  1271. if (sendCallback)
  1272. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, static_cast<int>(parameterId), 0, value, nullptr);
  1273. }
  1274. void CarlaPlugin::setParameterValueRT(const uint32_t parameterId, const float value) noexcept
  1275. {
  1276. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(parameterId), 0, value);
  1277. }
  1278. void CarlaPlugin::setParameterValueByRealIndex(const int32_t rindex, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept
  1279. {
  1280. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1281. CARLA_SAFE_ASSERT_RETURN(rindex > PARAMETER_MAX && rindex != PARAMETER_NULL,);
  1282. switch (rindex)
  1283. {
  1284. case PARAMETER_ACTIVE:
  1285. return setActive((value > 0.0f), sendOsc, sendCallback);
  1286. case PARAMETER_CTRL_CHANNEL:
  1287. return setCtrlChannel(int8_t(value), sendOsc, sendCallback);
  1288. case PARAMETER_DRYWET:
  1289. return setDryWet(value, sendOsc, sendCallback);
  1290. case PARAMETER_VOLUME:
  1291. return setVolume(value, sendOsc, sendCallback);
  1292. case PARAMETER_BALANCE_LEFT:
  1293. return setBalanceLeft(value, sendOsc, sendCallback);
  1294. case PARAMETER_BALANCE_RIGHT:
  1295. return setBalanceRight(value, sendOsc, sendCallback);
  1296. case PARAMETER_PANNING:
  1297. return setPanning(value, sendOsc, sendCallback);
  1298. }
  1299. #endif
  1300. CARLA_SAFE_ASSERT_RETURN(rindex >= 0,);
  1301. for (uint32_t i=0; i < pData->param.count; ++i)
  1302. {
  1303. if (pData->param.data[i].rindex == rindex)
  1304. {
  1305. //if (carla_isNotEqual(getParameterValue(i), value))
  1306. setParameterValue(i, value, sendGui, sendOsc, sendCallback);
  1307. break;
  1308. }
  1309. }
  1310. }
  1311. void CarlaPlugin::setParameterMidiChannel(const uint32_t parameterId, const uint8_t channel, const bool sendOsc, const bool sendCallback) noexcept
  1312. {
  1313. if (pData->engineBridged) {
  1314. CARLA_SAFE_ASSERT_RETURN(!sendOsc && !sendCallback,);
  1315. } else {
  1316. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  1317. }
  1318. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  1319. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1320. pData->param.data[parameterId].midiChannel = channel;
  1321. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1322. # if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1323. if (sendOsc && pData->engine->isOscControlRegistered())
  1324. pData->engine->oscSend_control_set_parameter_midi_channel(pData->id, parameterId, channel);
  1325. # endif
  1326. if (sendCallback)
  1327. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_MIDI_CHANNEL_CHANGED, pData->id, static_cast<int>(parameterId), channel, 0.0f, nullptr);
  1328. #endif
  1329. }
  1330. void CarlaPlugin::setParameterMidiCC(const uint32_t parameterId, const int16_t cc, const bool sendOsc, const bool sendCallback) noexcept
  1331. {
  1332. if (pData->engineBridged) {
  1333. CARLA_SAFE_ASSERT_RETURN(!sendOsc && !sendCallback,);
  1334. } else {
  1335. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  1336. }
  1337. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  1338. CARLA_SAFE_ASSERT_RETURN(cc >= -1 && cc < MAX_MIDI_CONTROL,);
  1339. pData->param.data[parameterId].midiCC = cc;
  1340. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1341. # if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1342. if (sendOsc && pData->engine->isOscControlRegistered())
  1343. pData->engine->oscSend_control_set_parameter_midi_cc(pData->id, parameterId, cc);
  1344. # endif
  1345. if (sendCallback)
  1346. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_MIDI_CC_CHANGED, pData->id, static_cast<int>(parameterId), cc, 0.0f, nullptr);
  1347. #endif
  1348. }
  1349. void CarlaPlugin::setCustomData(const char* const type, const char* const key, const char* const value, const bool)
  1350. {
  1351. CARLA_SAFE_ASSERT_RETURN(type != nullptr && type[0] != '\0',);
  1352. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  1353. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  1354. // Ignore some keys
  1355. if (std::strcmp(type, CUSTOM_DATA_TYPE_STRING) == 0)
  1356. {
  1357. const PluginType ptype = getType();
  1358. if ((ptype == PLUGIN_INTERNAL && std::strncmp(key, "CarlaAlternateFile", 18) == 0) ||
  1359. (ptype == PLUGIN_DSSI && std::strcmp (key, "guiVisible") == 0) ||
  1360. (ptype == PLUGIN_LV2 && std::strncmp(key, "OSC:", 4) == 0))
  1361. return;
  1362. }
  1363. // Check if we already have this key
  1364. for (LinkedList<CustomData>::Itenerator it = pData->custom.begin2(); it.valid(); it.next())
  1365. {
  1366. CustomData& customData(it.getValue(kCustomDataFallbackNC));
  1367. CARLA_SAFE_ASSERT_CONTINUE(customData.isValid());
  1368. if (std::strcmp(customData.key, key) == 0)
  1369. {
  1370. if (customData.value != nullptr)
  1371. delete[] customData.value;
  1372. customData.value = carla_strdup(value);
  1373. return;
  1374. }
  1375. }
  1376. // Otherwise store it
  1377. CustomData customData;
  1378. customData.type = carla_strdup(type);
  1379. customData.key = carla_strdup(key);
  1380. customData.value = carla_strdup(value);
  1381. pData->custom.append(customData);
  1382. }
  1383. void CarlaPlugin::setChunkData(const void* const data, const std::size_t dataSize)
  1384. {
  1385. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  1386. CARLA_SAFE_ASSERT_RETURN(dataSize > 0,);
  1387. CARLA_SAFE_ASSERT(false); // this should never happen
  1388. }
  1389. void CarlaPlugin::setProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback, const bool) noexcept
  1390. {
  1391. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->prog.count),);
  1392. pData->prog.current = index;
  1393. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1394. const bool reallySendOsc(sendOsc && pData->engine->isOscControlRegistered());
  1395. if (reallySendOsc && index < 50)
  1396. pData->engine->oscSend_control_set_current_program(pData->id, index);
  1397. #else
  1398. const bool reallySendOsc(false);
  1399. #endif
  1400. if (sendCallback)
  1401. pData->engine->callback(ENGINE_CALLBACK_PROGRAM_CHANGED, pData->id, index, 0, 0.0f, nullptr);
  1402. // Change default parameter values
  1403. if (index >= 0)
  1404. {
  1405. if (sendGui && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1406. uiProgramChange(static_cast<uint32_t>(index));
  1407. switch (getType())
  1408. {
  1409. case PLUGIN_SF2:
  1410. case PLUGIN_SFZ:
  1411. break;
  1412. default:
  1413. pData->updateParameterValues(this, reallySendOsc, sendCallback, true);
  1414. break;
  1415. }
  1416. }
  1417. // may be unused
  1418. return; (void)sendOsc;
  1419. }
  1420. void CarlaPlugin::setMidiProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback, const bool) noexcept
  1421. {
  1422. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->midiprog.count),);
  1423. pData->midiprog.current = index;
  1424. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1425. const bool reallySendOsc(sendOsc && pData->engine->isOscControlRegistered());
  1426. if (reallySendOsc && index < 50)
  1427. pData->engine->oscSend_control_set_current_midi_program(pData->id, index);
  1428. #else
  1429. const bool reallySendOsc(false);
  1430. #endif
  1431. if (sendCallback)
  1432. pData->engine->callback(ENGINE_CALLBACK_MIDI_PROGRAM_CHANGED, pData->id, index, 0, 0.0f, nullptr);
  1433. // Change default parameter values
  1434. if (index >= 0)
  1435. {
  1436. if (sendGui && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1437. uiMidiProgramChange(static_cast<uint32_t>(index));
  1438. switch (getType())
  1439. {
  1440. case PLUGIN_SF2:
  1441. case PLUGIN_SFZ:
  1442. break;
  1443. default:
  1444. pData->updateParameterValues(this, reallySendOsc, sendCallback, true);
  1445. break;
  1446. }
  1447. }
  1448. // may be unused
  1449. return; (void)sendOsc;
  1450. }
  1451. void CarlaPlugin::setMidiProgramById(const uint32_t bank, const uint32_t program, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept
  1452. {
  1453. for (uint32_t i=0; i < pData->midiprog.count; ++i)
  1454. {
  1455. if (pData->midiprog.data[i].bank == bank && pData->midiprog.data[i].program == program)
  1456. return setMidiProgram(static_cast<int32_t>(i), sendGui, sendOsc, sendCallback);
  1457. }
  1458. }
  1459. void CarlaPlugin::setProgramRT(const uint32_t uindex) noexcept
  1460. {
  1461. CARLA_SAFE_ASSERT_RETURN(uindex < pData->prog.count,);
  1462. const int32_t index = static_cast<int32_t>(uindex);
  1463. pData->prog.current = index;
  1464. // Change default parameter values
  1465. switch (getType())
  1466. {
  1467. case PLUGIN_SF2:
  1468. case PLUGIN_SFZ:
  1469. break;
  1470. default:
  1471. pData->updateDefaultParameterValues(this);
  1472. break;
  1473. }
  1474. pData->postponeRtEvent(kPluginPostRtEventProgramChange, index, 0, 0.0f);
  1475. }
  1476. void CarlaPlugin::setMidiProgramRT(const uint32_t uindex) noexcept
  1477. {
  1478. CARLA_SAFE_ASSERT_RETURN(uindex < pData->midiprog.count,);
  1479. const int32_t index = static_cast<int32_t>(uindex);
  1480. pData->midiprog.current = index;
  1481. // Change default parameter values
  1482. switch (getType())
  1483. {
  1484. case PLUGIN_SF2:
  1485. case PLUGIN_SFZ:
  1486. break;
  1487. default:
  1488. pData->updateDefaultParameterValues(this);
  1489. break;
  1490. }
  1491. pData->postponeRtEvent(kPluginPostRtEventMidiProgramChange, index, 0, 0.0f);
  1492. }
  1493. // -------------------------------------------------------------------
  1494. // Plugin state
  1495. void CarlaPlugin::reloadPrograms(const bool)
  1496. {
  1497. }
  1498. // -------------------------------------------------------------------
  1499. // Plugin processing
  1500. void CarlaPlugin::activate() noexcept
  1501. {
  1502. CARLA_SAFE_ASSERT(! pData->active);
  1503. }
  1504. void CarlaPlugin::deactivate() noexcept
  1505. {
  1506. CARLA_SAFE_ASSERT(pData->active);
  1507. }
  1508. void CarlaPlugin::bufferSizeChanged(const uint32_t)
  1509. {
  1510. }
  1511. void CarlaPlugin::sampleRateChanged(const double)
  1512. {
  1513. }
  1514. void CarlaPlugin::offlineModeChanged(const bool)
  1515. {
  1516. }
  1517. // -------------------------------------------------------------------
  1518. // Misc
  1519. void CarlaPlugin::idle()
  1520. {
  1521. if (! pData->enabled)
  1522. return;
  1523. const bool hasUI(pData->hints & PLUGIN_HAS_CUSTOM_UI);
  1524. const bool needsUiMainThread(pData->hints & PLUGIN_NEEDS_UI_MAIN_THREAD);
  1525. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1526. const bool sendOsc(pData->engine->isOscControlRegistered());
  1527. #endif
  1528. const uint32_t latency(getLatencyInFrames());
  1529. if (pData->latency.frames != latency)
  1530. {
  1531. carla_stdout("latency changed to %i samples", latency);
  1532. const ScopedSingleProcessLocker sspl(this, true);
  1533. pData->client->setLatency(latency);
  1534. #ifndef BUILD_BRIDGE
  1535. pData->latency.recreateBuffers(pData->latency.channels, latency);
  1536. #else
  1537. pData->latency.frames = latency;
  1538. #endif
  1539. }
  1540. const CarlaMutexLocker sl(pData->postRtEvents.getDataMutex());
  1541. for (RtLinkedList<PluginPostRtEvent>::Itenerator it = pData->postRtEvents.getDataIterator(); it.valid(); it.next())
  1542. {
  1543. const PluginPostRtEvent& event(it.getValue(kPluginPostRtEventFallback));
  1544. CARLA_SAFE_ASSERT_CONTINUE(event.type != kPluginPostRtEventNull);
  1545. switch (event.type)
  1546. {
  1547. case kPluginPostRtEventNull: {
  1548. } break;
  1549. case kPluginPostRtEventDebug: {
  1550. pData->engine->callback(ENGINE_CALLBACK_DEBUG, pData->id, event.value1, event.value2, event.value3, nullptr);
  1551. } break;
  1552. case kPluginPostRtEventParameterChange: {
  1553. // Update UI
  1554. if (event.value1 >= 0 && hasUI)
  1555. {
  1556. if (needsUiMainThread)
  1557. pData->postUiEvents.append(event);
  1558. else
  1559. uiParameterChange(static_cast<uint32_t>(event.value1), event.value3);
  1560. }
  1561. if (event.value2 != 1)
  1562. {
  1563. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1564. // Update OSC control client
  1565. if (sendOsc)
  1566. pData->engine->oscSend_control_set_parameter_value(pData->id, event.value1, event.value3);
  1567. #endif
  1568. // Update Host
  1569. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, event.value1, 0, event.value3, nullptr);
  1570. }
  1571. } break;
  1572. case kPluginPostRtEventProgramChange: {
  1573. // Update UI
  1574. if (event.value1 >= 0 && hasUI)
  1575. {
  1576. if (needsUiMainThread)
  1577. pData->postUiEvents.append(event);
  1578. else
  1579. uiProgramChange(static_cast<uint32_t>(event.value1));
  1580. }
  1581. // Update param values
  1582. for (uint32_t j=0; j < pData->param.count; ++j)
  1583. {
  1584. const float paramDefault(pData->param.ranges[j].def);
  1585. const float paramValue(getParameterValue(j));
  1586. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1587. if (sendOsc && j < 50)
  1588. {
  1589. pData->engine->oscSend_control_set_parameter_value(pData->id, static_cast<int32_t>(j), paramValue);
  1590. pData->engine->oscSend_control_set_default_value(pData->id, j, paramDefault);
  1591. }
  1592. #endif
  1593. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, static_cast<int>(j), 0, paramValue, nullptr);
  1594. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_DEFAULT_CHANGED, pData->id, static_cast<int>(j), 0, paramDefault, nullptr);
  1595. }
  1596. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1597. // Update OSC control client
  1598. if (sendOsc)
  1599. pData->engine->oscSend_control_set_current_program(pData->id, event.value1);
  1600. #endif
  1601. // Update Host
  1602. pData->engine->callback(ENGINE_CALLBACK_PROGRAM_CHANGED, pData->id, event.value1, 0, 0.0f, nullptr);
  1603. } break;
  1604. case kPluginPostRtEventMidiProgramChange: {
  1605. // Update UI
  1606. if (event.value1 >= 0 && hasUI)
  1607. {
  1608. if (needsUiMainThread)
  1609. pData->postUiEvents.append(event);
  1610. else
  1611. uiMidiProgramChange(static_cast<uint32_t>(event.value1));
  1612. }
  1613. // Update param values
  1614. for (uint32_t j=0; j < pData->param.count; ++j)
  1615. {
  1616. const float paramDefault(pData->param.ranges[j].def);
  1617. const float paramValue(getParameterValue(j));
  1618. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1619. if (sendOsc && j < 50)
  1620. {
  1621. pData->engine->oscSend_control_set_parameter_value(pData->id, static_cast<int32_t>(j), paramValue);
  1622. pData->engine->oscSend_control_set_default_value(pData->id, j, paramDefault);
  1623. }
  1624. #endif
  1625. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, static_cast<int>(j), 0, paramValue, nullptr);
  1626. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_DEFAULT_CHANGED, pData->id, static_cast<int>(j), 0, paramDefault, nullptr);
  1627. }
  1628. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1629. // Update OSC control client
  1630. if (sendOsc)
  1631. pData->engine->oscSend_control_set_current_midi_program(pData->id, event.value1);
  1632. #endif
  1633. // Update Host
  1634. pData->engine->callback(ENGINE_CALLBACK_MIDI_PROGRAM_CHANGED, pData->id, event.value1, 0, 0.0f, nullptr);
  1635. } break;
  1636. case kPluginPostRtEventNoteOn: {
  1637. CARLA_SAFE_ASSERT_BREAK(event.value1 >= 0 && event.value1 < MAX_MIDI_CHANNELS);
  1638. CARLA_SAFE_ASSERT_BREAK(event.value2 >= 0 && event.value2 < MAX_MIDI_NOTE);
  1639. CARLA_SAFE_ASSERT_BREAK(event.value3 >= 0 && event.value3 < MAX_MIDI_VALUE);
  1640. const uint8_t channel = static_cast<uint8_t>(event.value1);
  1641. const uint8_t note = static_cast<uint8_t>(event.value2);
  1642. const uint8_t velocity = uint8_t(event.value3);
  1643. // Update UI
  1644. if (hasUI)
  1645. {
  1646. if (needsUiMainThread)
  1647. pData->postUiEvents.append(event);
  1648. else
  1649. uiNoteOn(channel, note, velocity);
  1650. }
  1651. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1652. // Update OSC control client
  1653. if (sendOsc)
  1654. pData->engine->oscSend_control_note_on(pData->id, channel, note, velocity);
  1655. #endif
  1656. // Update Host
  1657. pData->engine->callback(ENGINE_CALLBACK_NOTE_ON, pData->id, event.value1, event.value2, event.value3, nullptr);
  1658. } break;
  1659. case kPluginPostRtEventNoteOff: {
  1660. CARLA_SAFE_ASSERT_BREAK(event.value1 >= 0 && event.value1 < MAX_MIDI_CHANNELS);
  1661. CARLA_SAFE_ASSERT_BREAK(event.value2 >= 0 && event.value2 < MAX_MIDI_NOTE);
  1662. const uint8_t channel = static_cast<uint8_t>(event.value1);
  1663. const uint8_t note = static_cast<uint8_t>(event.value2);
  1664. // Update UI
  1665. if (hasUI)
  1666. {
  1667. if (needsUiMainThread)
  1668. pData->postUiEvents.append(event);
  1669. else
  1670. uiNoteOff(channel, note);
  1671. }
  1672. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1673. // Update OSC control client
  1674. if (sendOsc)
  1675. pData->engine->oscSend_control_note_off(pData->id, channel, note);
  1676. #endif
  1677. // Update Host
  1678. pData->engine->callback(ENGINE_CALLBACK_NOTE_OFF, pData->id, event.value1, event.value2, 0.0f, nullptr);
  1679. } break;
  1680. }
  1681. }
  1682. pData->postRtEvents.clearData();
  1683. }
  1684. bool CarlaPlugin::tryLock(const bool forcedOffline) noexcept
  1685. {
  1686. if (forcedOffline)
  1687. {
  1688. #ifndef STOAT_TEST_BUILD
  1689. pData->masterMutex.lock();
  1690. return true;
  1691. #endif
  1692. }
  1693. return pData->masterMutex.tryLock();
  1694. }
  1695. void CarlaPlugin::unlock() noexcept
  1696. {
  1697. pData->masterMutex.unlock();
  1698. }
  1699. // -------------------------------------------------------------------
  1700. // Plugin buffers
  1701. void CarlaPlugin::initBuffers() const noexcept
  1702. {
  1703. pData->audioIn.initBuffers();
  1704. pData->audioOut.initBuffers();
  1705. pData->cvIn.initBuffers();
  1706. pData->cvOut.initBuffers();
  1707. pData->event.initBuffers();
  1708. }
  1709. void CarlaPlugin::clearBuffers() noexcept
  1710. {
  1711. pData->clearBuffers();
  1712. }
  1713. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1714. // -------------------------------------------------------------------
  1715. // OSC stuff
  1716. void CarlaPlugin::registerToOscClient() noexcept
  1717. {
  1718. if (! pData->engine->isOscControlRegistered())
  1719. return;
  1720. pData->engine->oscSend_control_add_plugin_start(pData->id, pData->name);
  1721. // Base data
  1722. {
  1723. char bufName[STR_MAX+1], bufLabel[STR_MAX+1], bufMaker[STR_MAX+1], bufCopyright[STR_MAX+1];
  1724. carla_zeroChars(bufName, STR_MAX);
  1725. carla_zeroChars(bufLabel, STR_MAX);
  1726. carla_zeroChars(bufMaker, STR_MAX);
  1727. carla_zeroChars(bufCopyright, STR_MAX);
  1728. getRealName(bufName);
  1729. getLabel(bufLabel);
  1730. getMaker(bufMaker);
  1731. getCopyright(bufCopyright);
  1732. pData->engine->oscSend_control_set_plugin_info1(pData->id, getType(), getCategory(), pData->hints, getUniqueId());
  1733. pData->engine->oscSend_control_set_plugin_info2(pData->id, bufName, bufLabel, bufMaker, bufCopyright);
  1734. }
  1735. // Base count
  1736. uint32_t paramIns, paramOuts;
  1737. {
  1738. getParameterCountInfo(paramIns, paramOuts);
  1739. if (paramIns > 49)
  1740. paramIns = 49;
  1741. if (paramOuts > 49)
  1742. paramOuts = 49;
  1743. pData->engine->oscSend_control_set_audio_count(pData->id, getAudioInCount(), getAudioOutCount());
  1744. pData->engine->oscSend_control_set_midi_count(pData->id, getMidiInCount(), getMidiOutCount());
  1745. pData->engine->oscSend_control_set_parameter_count(pData->id, paramIns, paramOuts);
  1746. }
  1747. // Plugin Parameters
  1748. if (const uint32_t count = std::min<uint32_t>(pData->param.count, 98U))
  1749. {
  1750. char bufName[STR_MAX+1], bufUnit[STR_MAX+1];
  1751. for (uint32_t i=0; i<count; ++i)
  1752. {
  1753. const ParameterData& paramData(pData->param.data[i]);
  1754. if (paramData.type == PARAMETER_INPUT)
  1755. {
  1756. if (--paramIns == 0)
  1757. break;
  1758. }
  1759. else if (paramData.type == PARAMETER_INPUT)
  1760. {
  1761. if (--paramOuts == 0)
  1762. break;
  1763. }
  1764. else
  1765. {
  1766. continue;
  1767. }
  1768. const ParameterRanges& paramRanges(pData->param.ranges[i]);
  1769. carla_zeroChars(bufName, STR_MAX);
  1770. carla_zeroChars(bufUnit, STR_MAX);
  1771. getParameterName(i, bufName);
  1772. getParameterUnit(i, bufUnit);
  1773. pData->engine->oscSend_control_set_parameter_data(pData->id, i, paramData.type, paramData.hints, bufName, bufUnit);
  1774. pData->engine->oscSend_control_set_parameter_ranges1(pData->id, i, paramRanges.def, paramRanges.min, paramRanges.max);
  1775. pData->engine->oscSend_control_set_parameter_ranges2(pData->id, i, paramRanges.step, paramRanges.stepSmall, paramRanges.stepLarge);
  1776. pData->engine->oscSend_control_set_parameter_value(pData->id, static_cast<int32_t>(i), getParameterValue(i));
  1777. if (paramData.midiCC >= 0)
  1778. pData->engine->oscSend_control_set_parameter_midi_cc(pData->id, i, paramData.midiCC);
  1779. if (paramData.midiChannel != 0)
  1780. pData->engine->oscSend_control_set_parameter_midi_channel(pData->id, i, paramData.midiChannel);
  1781. }
  1782. }
  1783. // Programs
  1784. if (const uint32_t count = std::min<uint32_t>(pData->prog.count, 50U))
  1785. {
  1786. pData->engine->oscSend_control_set_program_count(pData->id, count);
  1787. for (uint32_t i=0; i < count; ++i)
  1788. pData->engine->oscSend_control_set_program_name(pData->id, i, pData->prog.names[i]);
  1789. pData->engine->oscSend_control_set_current_program(pData->id, pData->prog.current);
  1790. }
  1791. // MIDI Programs
  1792. if (const uint32_t count = std::min<uint32_t>(pData->midiprog.count, 50U))
  1793. {
  1794. pData->engine->oscSend_control_set_midi_program_count(pData->id, count);
  1795. for (uint32_t i=0; i < count; ++i)
  1796. {
  1797. const MidiProgramData& mpData(pData->midiprog.data[i]);
  1798. pData->engine->oscSend_control_set_midi_program_data(pData->id, i, mpData.bank, mpData.program, mpData.name);
  1799. }
  1800. pData->engine->oscSend_control_set_current_midi_program(pData->id, pData->midiprog.current);
  1801. }
  1802. pData->engine->oscSend_control_add_plugin_end(pData->id);
  1803. // Internal Parameters
  1804. {
  1805. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_DRYWET, pData->postProc.dryWet);
  1806. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_VOLUME, pData->postProc.volume);
  1807. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_BALANCE_LEFT, pData->postProc.balanceLeft);
  1808. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_BALANCE_RIGHT, pData->postProc.balanceRight);
  1809. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_PANNING, pData->postProc.panning);
  1810. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_CTRL_CHANNEL, pData->ctrlChannel);
  1811. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_ACTIVE, pData->active ? 1.0f : 0.0f);
  1812. }
  1813. }
  1814. #endif
  1815. // FIXME
  1816. void CarlaPlugin::handleOscMessage(const char* const, const int, const void* const, const char* const, const lo_message)
  1817. {
  1818. // do nothing
  1819. }
  1820. //#endif // HAVE_LIBLO && ! BUILD_BRIDGE
  1821. // -------------------------------------------------------------------
  1822. // MIDI events
  1823. void CarlaPlugin::sendMidiSingleNote(const uint8_t channel, const uint8_t note, const uint8_t velo, const bool sendGui, const bool sendOsc, const bool sendCallback)
  1824. {
  1825. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1826. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1827. CARLA_SAFE_ASSERT_RETURN(velo < MAX_MIDI_VALUE,);
  1828. if (! pData->active)
  1829. return;
  1830. ExternalMidiNote extNote;
  1831. extNote.channel = static_cast<int8_t>(channel);
  1832. extNote.note = note;
  1833. extNote.velo = velo;
  1834. pData->extNotes.appendNonRT(extNote);
  1835. if (sendGui && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1836. {
  1837. if (velo > 0)
  1838. uiNoteOn(channel, note, velo);
  1839. else
  1840. uiNoteOff(channel, note);
  1841. }
  1842. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1843. if (sendOsc && pData->engine->isOscControlRegistered())
  1844. {
  1845. if (velo > 0)
  1846. pData->engine->oscSend_control_note_on(pData->id, channel, note, velo);
  1847. else
  1848. pData->engine->oscSend_control_note_off(pData->id, channel, note);
  1849. }
  1850. #endif
  1851. if (sendCallback)
  1852. pData->engine->callback((velo > 0) ? ENGINE_CALLBACK_NOTE_ON : ENGINE_CALLBACK_NOTE_OFF, pData->id, channel, note, velo, nullptr);
  1853. // may be unused
  1854. return; (void)sendOsc;
  1855. }
  1856. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1857. void CarlaPlugin::sendMidiAllNotesOffToCallback()
  1858. {
  1859. if (pData->ctrlChannel < 0 || pData->ctrlChannel >= MAX_MIDI_CHANNELS)
  1860. return;
  1861. PluginPostRtEvent postEvent;
  1862. postEvent.type = kPluginPostRtEventNoteOff;
  1863. postEvent.value1 = pData->ctrlChannel;
  1864. postEvent.value2 = 0;
  1865. postEvent.value3 = 0.0f;
  1866. for (int32_t i=0; i < MAX_MIDI_NOTE; ++i)
  1867. {
  1868. postEvent.value2 = i;
  1869. pData->postRtEvents.appendRT(postEvent);
  1870. }
  1871. }
  1872. #endif
  1873. // -------------------------------------------------------------------
  1874. // UI Stuff
  1875. void CarlaPlugin::showCustomUI(const bool yesNo)
  1876. {
  1877. if (yesNo) {
  1878. CARLA_SAFE_ASSERT(false);
  1879. }
  1880. }
  1881. void CarlaPlugin::uiIdle()
  1882. {
  1883. if (pData->hints & PLUGIN_NEEDS_UI_MAIN_THREAD)
  1884. {
  1885. // Update parameter outputs
  1886. for (uint32_t i=0; i < pData->param.count; ++i)
  1887. {
  1888. if (pData->param.data[i].type == PARAMETER_OUTPUT)
  1889. uiParameterChange(i, getParameterValue(i));
  1890. }
  1891. const CarlaMutexLocker sl(pData->postUiEvents.mutex);
  1892. for (LinkedList<PluginPostRtEvent>::Itenerator it = pData->postUiEvents.data.begin2(); it.valid(); it.next())
  1893. {
  1894. const PluginPostRtEvent& event(it.getValue(kPluginPostRtEventFallback));
  1895. CARLA_SAFE_ASSERT_CONTINUE(event.type != kPluginPostRtEventNull);
  1896. switch (event.type)
  1897. {
  1898. case kPluginPostRtEventNull:
  1899. case kPluginPostRtEventDebug:
  1900. break;
  1901. case kPluginPostRtEventParameterChange:
  1902. uiParameterChange(static_cast<uint32_t>(event.value1), event.value3);
  1903. break;
  1904. case kPluginPostRtEventProgramChange:
  1905. uiProgramChange(static_cast<uint32_t>(event.value1));
  1906. break;
  1907. case kPluginPostRtEventMidiProgramChange:
  1908. uiMidiProgramChange(static_cast<uint32_t>(event.value1));
  1909. break;
  1910. case kPluginPostRtEventNoteOn:
  1911. uiNoteOn(static_cast<uint8_t>(event.value1), static_cast<uint8_t>(event.value2), uint8_t(event.value3));
  1912. break;
  1913. case kPluginPostRtEventNoteOff:
  1914. uiNoteOff(static_cast<uint8_t>(event.value1), static_cast<uint8_t>(event.value2));
  1915. break;
  1916. }
  1917. }
  1918. pData->postUiEvents.data.clear();
  1919. }
  1920. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1921. if (pData->transientTryCounter == 0)
  1922. return;
  1923. if (++pData->transientTryCounter % 10 != 0)
  1924. return;
  1925. if (pData->transientTryCounter >= 200)
  1926. return;
  1927. carla_stdout("Trying to get window...");
  1928. CarlaString uiTitle(pData->name);
  1929. uiTitle += " (GUI)";
  1930. if (CarlaPluginUI::tryTransientWinIdMatch(getUiBridgeProcessId(), uiTitle,
  1931. pData->engine->getOptions().frontendWinId, pData->transientFirstTry))
  1932. {
  1933. pData->transientTryCounter = 0;
  1934. pData->transientFirstTry = false;
  1935. }
  1936. #endif
  1937. }
  1938. void CarlaPlugin::uiParameterChange(const uint32_t index, const float value) noexcept
  1939. {
  1940. CARLA_SAFE_ASSERT_RETURN(index < getParameterCount(),);
  1941. return;
  1942. // unused
  1943. (void)value;
  1944. }
  1945. void CarlaPlugin::uiProgramChange(const uint32_t index) noexcept
  1946. {
  1947. CARLA_SAFE_ASSERT_RETURN(index < getProgramCount(),);
  1948. }
  1949. void CarlaPlugin::uiMidiProgramChange(const uint32_t index) noexcept
  1950. {
  1951. CARLA_SAFE_ASSERT_RETURN(index < getMidiProgramCount(),);
  1952. }
  1953. void CarlaPlugin::uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) noexcept
  1954. {
  1955. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1956. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1957. CARLA_SAFE_ASSERT_RETURN(velo > 0 && velo < MAX_MIDI_VALUE,);
  1958. }
  1959. void CarlaPlugin::uiNoteOff(const uint8_t channel, const uint8_t note) noexcept
  1960. {
  1961. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1962. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1963. }
  1964. CarlaEngine* CarlaPlugin::getEngine() const noexcept
  1965. {
  1966. return pData->engine;
  1967. }
  1968. CarlaEngineClient* CarlaPlugin::getEngineClient() const noexcept
  1969. {
  1970. return pData->client;
  1971. }
  1972. CarlaEngineAudioPort* CarlaPlugin::getAudioInPort(const uint32_t index) const noexcept
  1973. {
  1974. return pData->audioIn.ports[index].port;
  1975. }
  1976. CarlaEngineAudioPort* CarlaPlugin::getAudioOutPort(const uint32_t index) const noexcept
  1977. {
  1978. return pData->audioOut.ports[index].port;
  1979. }
  1980. CarlaEngineCVPort* CarlaPlugin::getCVInPort(const uint32_t index) const noexcept
  1981. {
  1982. return pData->cvIn.ports[index].port;
  1983. }
  1984. CarlaEngineCVPort* CarlaPlugin::getCVOutPort(const uint32_t index) const noexcept
  1985. {
  1986. return pData->cvOut.ports[index].port;
  1987. }
  1988. CarlaEngineEventPort* CarlaPlugin::getDefaultEventInPort() const noexcept
  1989. {
  1990. return pData->event.portIn;
  1991. }
  1992. CarlaEngineEventPort* CarlaPlugin::getDefaultEventOutPort() const noexcept
  1993. {
  1994. return pData->event.portOut;
  1995. }
  1996. void* CarlaPlugin::getNativeHandle() const noexcept
  1997. {
  1998. return nullptr;
  1999. }
  2000. const void* CarlaPlugin::getNativeDescriptor() const noexcept
  2001. {
  2002. return nullptr;
  2003. }
  2004. uintptr_t CarlaPlugin::getUiBridgeProcessId() const noexcept
  2005. {
  2006. return 0;
  2007. }
  2008. // -------------------------------------------------------------------
  2009. uint32_t CarlaPlugin::getPatchbayNodeId() const noexcept
  2010. {
  2011. return pData->nodeId;
  2012. }
  2013. void CarlaPlugin::setPatchbayNodeId(const uint32_t nodeId) noexcept
  2014. {
  2015. pData->nodeId = nodeId;
  2016. }
  2017. // -------------------------------------------------------------------
  2018. void CarlaPlugin::restoreLV2State() noexcept
  2019. {
  2020. carla_stderr2("Warning: restoreLV2State() called for non-implemented type");
  2021. }
  2022. void CarlaPlugin::waitForBridgeSaveSignal() noexcept
  2023. {
  2024. }
  2025. // -------------------------------------------------------------------
  2026. // Scoped Disabler
  2027. CarlaPlugin::ScopedDisabler::ScopedDisabler(CarlaPlugin* const plugin) noexcept
  2028. : fPlugin(plugin),
  2029. fWasEnabled(false)
  2030. {
  2031. CARLA_SAFE_ASSERT_RETURN(plugin != nullptr,);
  2032. CARLA_SAFE_ASSERT_RETURN(plugin->pData != nullptr,);
  2033. CARLA_SAFE_ASSERT_RETURN(plugin->pData->client != nullptr,);
  2034. carla_debug("CarlaPlugin::ScopedDisabler(%p)", plugin);
  2035. plugin->pData->masterMutex.lock();
  2036. if (plugin->pData->enabled)
  2037. {
  2038. fWasEnabled = true;
  2039. plugin->pData->enabled = false;
  2040. if (plugin->pData->client->isActive())
  2041. plugin->pData->client->deactivate();
  2042. }
  2043. }
  2044. CarlaPlugin::ScopedDisabler::~ScopedDisabler() noexcept
  2045. {
  2046. CARLA_SAFE_ASSERT_RETURN(fPlugin != nullptr,);
  2047. CARLA_SAFE_ASSERT_RETURN(fPlugin->pData != nullptr,);
  2048. CARLA_SAFE_ASSERT_RETURN(fPlugin->pData->client != nullptr,);
  2049. carla_debug("CarlaPlugin::~ScopedDisabler()");
  2050. if (fWasEnabled)
  2051. {
  2052. fPlugin->pData->enabled = true;
  2053. fPlugin->pData->client->activate();
  2054. }
  2055. fPlugin->pData->masterMutex.unlock();
  2056. }
  2057. // -------------------------------------------------------------------
  2058. // Scoped Process Locker
  2059. CarlaPlugin::ScopedSingleProcessLocker::ScopedSingleProcessLocker(CarlaPlugin* const plugin, const bool block) noexcept
  2060. : fPlugin(plugin),
  2061. fBlock(block)
  2062. {
  2063. CARLA_SAFE_ASSERT_RETURN(fPlugin != nullptr,);
  2064. CARLA_SAFE_ASSERT_RETURN(fPlugin->pData != nullptr,);
  2065. carla_debug("CarlaPlugin::ScopedSingleProcessLocker(%p, %s)", plugin, bool2str(block));
  2066. if (! fBlock)
  2067. return;
  2068. plugin->pData->singleMutex.lock();
  2069. }
  2070. CarlaPlugin::ScopedSingleProcessLocker::~ScopedSingleProcessLocker() noexcept
  2071. {
  2072. CARLA_SAFE_ASSERT_RETURN(fPlugin != nullptr,);
  2073. CARLA_SAFE_ASSERT_RETURN(fPlugin->pData != nullptr,);
  2074. carla_debug("CarlaPlugin::~ScopedSingleProcessLocker()");
  2075. if (! fBlock)
  2076. return;
  2077. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  2078. if (fPlugin->pData->singleMutex.wasTryLockCalled())
  2079. fPlugin->pData->needsReset = true;
  2080. #endif
  2081. fPlugin->pData->singleMutex.unlock();
  2082. }
  2083. // -------------------------------------------------------------------
  2084. CARLA_BACKEND_END_NAMESPACE