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.

NativePlugin.cpp 88KB

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
10 years ago
11 years ago
10 years ago
10 years ago
10 years ago
11 years ago
10 years ago
11 years ago
10 years ago
10 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
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
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
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
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
10 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
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
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
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
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
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
10 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
10 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
10 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
10 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
11 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494
  1. /*
  2. * Carla Native Plugin
  3. * Copyright (C) 2012-2014 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 "CarlaMathUtils.hpp"
  20. #include "CarlaNative.h"
  21. #include "juce_core.h"
  22. using juce::String;
  23. using juce::StringArray;
  24. // -----------------------------------------------------
  25. static LinkedList<const NativePluginDescriptor*> gPluginDescriptors;
  26. void carla_register_native_plugin(const NativePluginDescriptor* desc)
  27. {
  28. gPluginDescriptors.append(desc);
  29. }
  30. // -----------------------------------------------------
  31. CARLA_BACKEND_START_NAMESPACE
  32. // -----------------------------------------------------
  33. struct NativePluginMidiData {
  34. uint32_t count;
  35. uint32_t* indexes;
  36. CarlaEngineEventPort** ports;
  37. NativePluginMidiData() noexcept
  38. : count(0),
  39. indexes(nullptr),
  40. ports(nullptr) {}
  41. ~NativePluginMidiData() noexcept
  42. {
  43. CARLA_SAFE_ASSERT_INT(count == 0, count);
  44. CARLA_SAFE_ASSERT(indexes == nullptr);
  45. CARLA_SAFE_ASSERT(ports == nullptr);
  46. }
  47. void createNew(const uint32_t newCount)
  48. {
  49. CARLA_SAFE_ASSERT_INT(count == 0, count);
  50. CARLA_SAFE_ASSERT_RETURN(indexes == nullptr,);
  51. CARLA_SAFE_ASSERT_RETURN(ports == nullptr,);
  52. CARLA_SAFE_ASSERT_RETURN(newCount > 0,);
  53. indexes = new uint32_t[newCount];
  54. ports = new CarlaEngineEventPort*[newCount];
  55. count = newCount;
  56. for (uint32_t i=0; i < newCount; ++i)
  57. indexes[i] = 0;
  58. for (uint32_t i=0; i < newCount; ++i)
  59. ports[i] = nullptr;
  60. }
  61. void clear() noexcept
  62. {
  63. if (ports != nullptr)
  64. {
  65. for (uint32_t i=0; i < count; ++i)
  66. {
  67. if (ports[i] != nullptr)
  68. {
  69. delete ports[i];
  70. ports[i] = nullptr;
  71. }
  72. }
  73. delete[] ports;
  74. ports = nullptr;
  75. }
  76. if (indexes != nullptr)
  77. {
  78. delete[] indexes;
  79. indexes = nullptr;
  80. }
  81. count = 0;
  82. }
  83. void initBuffers() const noexcept
  84. {
  85. for (uint32_t i=0; i < count; ++i)
  86. {
  87. if (ports[i] != nullptr)
  88. ports[i]->initBuffer();
  89. }
  90. }
  91. CARLA_DECLARE_NON_COPY_STRUCT(NativePluginMidiData)
  92. };
  93. // -----------------------------------------------------
  94. static const
  95. struct ScopedInitializer {
  96. ScopedInitializer()
  97. {
  98. carla_register_all_plugins();
  99. }
  100. ~ScopedInitializer()
  101. {
  102. gPluginDescriptors.clear();
  103. }
  104. } _si;
  105. // -----------------------------------------------------
  106. class NativePlugin : public CarlaPlugin
  107. {
  108. public:
  109. NativePlugin(CarlaEngine* const engine, const uint id)
  110. : CarlaPlugin(engine, id),
  111. fHandle(nullptr),
  112. fHandle2(nullptr),
  113. fHost(),
  114. fDescriptor(nullptr),
  115. fIsProcessing(false),
  116. fIsUiVisible(false),
  117. fAudioInBuffers(nullptr),
  118. fAudioOutBuffers(nullptr),
  119. fMidiEventCount(0),
  120. fMidiIn(),
  121. fMidiOut(),
  122. fTimeInfo(),
  123. leakDetector_NativePlugin()
  124. {
  125. carla_debug("NativePlugin::NativePlugin(%p, %i)", engine, id);
  126. carla_fill<int32_t>(fCurMidiProgs, 0, MAX_MIDI_CHANNELS);
  127. carla_zeroStruct<NativeMidiEvent>(fMidiEvents, kPluginMaxMidiEvents*2);
  128. carla_zeroStruct<NativeTimeInfo>(fTimeInfo);
  129. fHost.handle = this;
  130. fHost.resourceDir = carla_strdup(engine->getOptions().resourceDir);
  131. fHost.uiName = nullptr;
  132. fHost.uiParentId = engine->getOptions().frontendWinId;
  133. fHost.get_buffer_size = carla_host_get_buffer_size;
  134. fHost.get_sample_rate = carla_host_get_sample_rate;
  135. fHost.is_offline = carla_host_is_offline;
  136. fHost.get_time_info = carla_host_get_time_info;
  137. fHost.write_midi_event = carla_host_write_midi_event;
  138. fHost.ui_parameter_changed = carla_host_ui_parameter_changed;
  139. fHost.ui_custom_data_changed = carla_host_ui_custom_data_changed;
  140. fHost.ui_closed = carla_host_ui_closed;
  141. fHost.ui_open_file = carla_host_ui_open_file;
  142. fHost.ui_save_file = carla_host_ui_save_file;
  143. fHost.dispatcher = carla_host_dispatcher;
  144. }
  145. ~NativePlugin() override
  146. {
  147. carla_debug("NativePlugin::~NativePlugin()");
  148. // close UI
  149. if (pData->hints & PLUGIN_HAS_CUSTOM_UI)
  150. {
  151. if (fIsUiVisible && fDescriptor != nullptr && fDescriptor->ui_show != nullptr && fHandle != nullptr)
  152. fDescriptor->ui_show(fHandle, false);
  153. pData->transientTryCounter = 0;
  154. }
  155. pData->singleMutex.lock();
  156. pData->masterMutex.lock();
  157. if (pData->client != nullptr && pData->client->isActive())
  158. pData->client->deactivate();
  159. CARLA_ASSERT(! fIsProcessing);
  160. if (pData->active)
  161. {
  162. deactivate();
  163. pData->active = false;
  164. }
  165. if (fDescriptor != nullptr)
  166. {
  167. if (fDescriptor->cleanup != nullptr)
  168. {
  169. if (fHandle != nullptr)
  170. fDescriptor->cleanup(fHandle);
  171. if (fHandle2 != nullptr)
  172. fDescriptor->cleanup(fHandle2);
  173. }
  174. fHandle = nullptr;
  175. fHandle2 = nullptr;
  176. fDescriptor = nullptr;
  177. }
  178. if (fHost.resourceDir != nullptr)
  179. {
  180. delete[] fHost.resourceDir;
  181. fHost.resourceDir = nullptr;
  182. }
  183. if (fHost.uiName != nullptr)
  184. {
  185. delete[] fHost.uiName;
  186. fHost.uiName = nullptr;
  187. }
  188. clearBuffers();
  189. }
  190. // -------------------------------------------------------------------
  191. // Information (base)
  192. PluginType getType() const noexcept override
  193. {
  194. return PLUGIN_INTERNAL;
  195. }
  196. PluginCategory getCategory() const noexcept override
  197. {
  198. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, PLUGIN_CATEGORY_NONE);
  199. return static_cast<PluginCategory>(fDescriptor->category);
  200. }
  201. // -------------------------------------------------------------------
  202. // Information (count)
  203. uint32_t getMidiInCount() const noexcept override
  204. {
  205. return fMidiIn.count;
  206. }
  207. uint32_t getMidiOutCount() const noexcept override
  208. {
  209. return fMidiOut.count;
  210. }
  211. uint32_t getParameterScalePointCount(const uint32_t parameterId) const noexcept override
  212. {
  213. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, 0);
  214. CARLA_SAFE_ASSERT_RETURN(fDescriptor->get_parameter_info != nullptr, 0);
  215. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr, 0);
  216. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0);
  217. // FIXME - try
  218. if (const NativeParameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId))
  219. return param->scalePointCount;
  220. carla_safe_assert("const Parameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId)", __FILE__, __LINE__);
  221. return 0;
  222. }
  223. // -------------------------------------------------------------------
  224. // Information (current data)
  225. // nothing
  226. // -------------------------------------------------------------------
  227. // Information (per-plugin data)
  228. uint getOptionsAvailable() const noexcept override
  229. {
  230. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, 0x0);
  231. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr, 0);
  232. // FIXME - try
  233. const bool hasMidiProgs(fDescriptor->get_midi_program_count != nullptr && fDescriptor->get_midi_program_count(fHandle) > 0);
  234. uint options = 0x0;
  235. if (hasMidiProgs && (fDescriptor->supports & ::PLUGIN_SUPPORTS_PROGRAM_CHANGES) == 0)
  236. options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  237. if (getMidiInCount() == 0 && (fDescriptor->hints & ::PLUGIN_NEEDS_FIXED_BUFFERS) == 0)
  238. options |= PLUGIN_OPTION_FIXED_BUFFERS;
  239. if (pData->engine->getProccessMode() != ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  240. {
  241. if (pData->options & PLUGIN_OPTION_FORCE_STEREO)
  242. options |= PLUGIN_OPTION_FORCE_STEREO;
  243. else if (pData->audioIn.count <= 1 && pData->audioOut.count <= 1 && (pData->audioIn.count != 0 || pData->audioOut.count != 0))
  244. options |= PLUGIN_OPTION_FORCE_STEREO;
  245. }
  246. if (fDescriptor->supports & ::PLUGIN_SUPPORTS_CONTROL_CHANGES)
  247. options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  248. if (fDescriptor->supports & ::PLUGIN_SUPPORTS_CHANNEL_PRESSURE)
  249. options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  250. if (fDescriptor->supports & ::PLUGIN_SUPPORTS_NOTE_AFTERTOUCH)
  251. options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  252. if (fDescriptor->supports & ::PLUGIN_SUPPORTS_PITCHBEND)
  253. options |= PLUGIN_OPTION_SEND_PITCHBEND;
  254. if (fDescriptor->supports & ::PLUGIN_SUPPORTS_ALL_SOUND_OFF)
  255. options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  256. return options;
  257. }
  258. float getParameterValue(const uint32_t parameterId) const noexcept override
  259. {
  260. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, 0.0f);
  261. CARLA_SAFE_ASSERT_RETURN(fDescriptor->get_parameter_value != nullptr, 0.0f);
  262. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr, 0.0f);
  263. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0.0f);
  264. // FIXME - try
  265. return fDescriptor->get_parameter_value(fHandle, parameterId);
  266. }
  267. float getParameterScalePointValue(const uint32_t parameterId, const uint32_t scalePointId) const noexcept override
  268. {
  269. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, 0.0f);
  270. CARLA_SAFE_ASSERT_RETURN(fDescriptor->get_parameter_info != nullptr, 0.0f);
  271. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr, 0.0f);
  272. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0.0f);
  273. // FIXME - try
  274. if (const NativeParameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId))
  275. {
  276. CARLA_SAFE_ASSERT_RETURN(scalePointId < param->scalePointCount, 0.0f);
  277. const NativeParameterScalePoint* scalePoint(&param->scalePoints[scalePointId]);
  278. return scalePoint->value;
  279. }
  280. carla_safe_assert("const Parameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId)", __FILE__, __LINE__);
  281. return 0.0f;
  282. }
  283. void getLabel(char* const strBuf) const noexcept override
  284. {
  285. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  286. if (fDescriptor->label != nullptr)
  287. {
  288. std::strncpy(strBuf, fDescriptor->label, STR_MAX);
  289. return;
  290. }
  291. CarlaPlugin::getLabel(strBuf);
  292. }
  293. void getMaker(char* const strBuf) const noexcept override
  294. {
  295. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  296. if (fDescriptor->maker != nullptr)
  297. {
  298. std::strncpy(strBuf, fDescriptor->maker, STR_MAX);
  299. return;
  300. }
  301. CarlaPlugin::getMaker(strBuf);
  302. }
  303. void getCopyright(char* const strBuf) const noexcept override
  304. {
  305. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  306. if (fDescriptor->copyright != nullptr)
  307. {
  308. std::strncpy(strBuf, fDescriptor->copyright, STR_MAX);
  309. return;
  310. }
  311. CarlaPlugin::getCopyright(strBuf);
  312. }
  313. void getRealName(char* const strBuf) const noexcept override
  314. {
  315. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  316. if (fDescriptor->name != nullptr)
  317. {
  318. std::strncpy(strBuf, fDescriptor->name, STR_MAX);
  319. return;
  320. }
  321. CarlaPlugin::getRealName(strBuf);
  322. }
  323. void getParameterName(const uint32_t parameterId, char* const strBuf) const noexcept override
  324. {
  325. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  326. CARLA_SAFE_ASSERT_RETURN(fDescriptor->get_parameter_info != nullptr,);
  327. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  328. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  329. // FIXME - try
  330. if (const NativeParameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId))
  331. {
  332. if (param->name != nullptr)
  333. {
  334. std::strncpy(strBuf, param->name, STR_MAX);
  335. return;
  336. }
  337. carla_safe_assert("param->name != nullptr", __FILE__, __LINE__);
  338. return CarlaPlugin::getParameterName(parameterId, strBuf);
  339. }
  340. carla_safe_assert("const Parameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId)", __FILE__, __LINE__);
  341. CarlaPlugin::getParameterName(parameterId, strBuf);
  342. }
  343. void getParameterText(const uint32_t parameterId, char* const strBuf) const noexcept override
  344. {
  345. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  346. CARLA_SAFE_ASSERT_RETURN(fDescriptor->get_parameter_text != nullptr,);
  347. CARLA_SAFE_ASSERT_RETURN(fDescriptor->get_parameter_value != nullptr,);
  348. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  349. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  350. // FIXME - try
  351. if (const char* const text = fDescriptor->get_parameter_text(fHandle, parameterId /*, fDescriptor->get_parameter_value(fHandle, parameterId)*/))
  352. {
  353. std::strncpy(strBuf, text, STR_MAX);
  354. return;
  355. }
  356. carla_safe_assert("const char* const text = fDescriptor->get_parameter_text(fHandle, parameterId, value)", __FILE__, __LINE__);
  357. CarlaPlugin::getParameterText(parameterId, strBuf);
  358. }
  359. void getParameterUnit(const uint32_t parameterId, char* const strBuf) const noexcept override
  360. {
  361. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  362. CARLA_SAFE_ASSERT_RETURN(fDescriptor->get_parameter_info != nullptr,);
  363. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  364. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  365. // FIXME - try
  366. if (const NativeParameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId))
  367. {
  368. if (param->unit != nullptr)
  369. {
  370. std::strncpy(strBuf, param->unit, STR_MAX);
  371. return;
  372. }
  373. return CarlaPlugin::getParameterUnit(parameterId, strBuf);
  374. }
  375. carla_safe_assert("const Parameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId)", __FILE__, __LINE__);
  376. CarlaPlugin::getParameterUnit(parameterId, strBuf);
  377. }
  378. void getParameterScalePointLabel(const uint32_t parameterId, const uint32_t scalePointId, char* const strBuf) const noexcept override
  379. {
  380. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  381. CARLA_SAFE_ASSERT_RETURN(fDescriptor->get_parameter_info != nullptr,);
  382. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  383. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  384. // FIXME - try
  385. if (const NativeParameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId))
  386. {
  387. CARLA_SAFE_ASSERT_RETURN(scalePointId < param->scalePointCount,);
  388. const NativeParameterScalePoint* scalePoint(&param->scalePoints[scalePointId]);
  389. if (scalePoint->label != nullptr)
  390. {
  391. std::strncpy(strBuf, scalePoint->label, STR_MAX);
  392. return;
  393. }
  394. carla_safe_assert("scalePoint->label != nullptr", __FILE__, __LINE__);
  395. return CarlaPlugin::getParameterScalePointLabel(parameterId, scalePointId, strBuf);
  396. }
  397. carla_safe_assert("const Parameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId)", __FILE__, __LINE__);
  398. CarlaPlugin::getParameterScalePointLabel(parameterId, scalePointId, strBuf);
  399. }
  400. // -------------------------------------------------------------------
  401. // Set data (state)
  402. void prepareForSave() override
  403. {
  404. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  405. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  406. if (pData->midiprog.count > 0 && fDescriptor->category == ::PLUGIN_CATEGORY_SYNTH)
  407. {
  408. char strBuf[STR_MAX+1];
  409. std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i",
  410. fCurMidiProgs[0], fCurMidiProgs[1], fCurMidiProgs[2], fCurMidiProgs[3],
  411. fCurMidiProgs[4], fCurMidiProgs[5], fCurMidiProgs[6], fCurMidiProgs[7],
  412. fCurMidiProgs[8], fCurMidiProgs[9], fCurMidiProgs[10], fCurMidiProgs[11],
  413. fCurMidiProgs[12], fCurMidiProgs[13], fCurMidiProgs[14], fCurMidiProgs[15]);
  414. strBuf[STR_MAX] = '\0';
  415. CarlaPlugin::setCustomData(CUSTOM_DATA_TYPE_STRING, "midiPrograms", strBuf, false);
  416. }
  417. if (fDescriptor == nullptr || fDescriptor->get_state == nullptr || (fDescriptor->hints & ::PLUGIN_USES_STATE) == 0)
  418. return;
  419. if (char* data = fDescriptor->get_state(fHandle))
  420. {
  421. CarlaPlugin::setCustomData(CUSTOM_DATA_TYPE_CHUNK, "State", data, false);
  422. std::free(data);
  423. }
  424. }
  425. // -------------------------------------------------------------------
  426. // Set data (internal stuff)
  427. void setName(const char* const newName) override
  428. {
  429. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  430. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  431. CARLA_SAFE_ASSERT_RETURN(newName != nullptr && newName[0] != '\0',);
  432. char uiName[std::strlen(newName)+6+1];
  433. std::strcpy(uiName, newName);
  434. std::strcat(uiName, " (GUI)");
  435. if (fHost.uiName != nullptr)
  436. delete[] fHost.uiName;
  437. fHost.uiName = carla_strdup(uiName);
  438. if (fDescriptor->dispatcher != nullptr)
  439. fDescriptor->dispatcher(fHandle, PLUGIN_OPCODE_UI_NAME_CHANGED, 0, 0, uiName, 0.0f);
  440. CarlaPlugin::setName(newName);
  441. }
  442. void setCtrlChannel(const int8_t channel, const bool sendOsc, const bool sendCallback) noexcept override
  443. {
  444. if (channel >= 0 && channel < MAX_MIDI_CHANNELS && pData->midiprog.count > 0)
  445. pData->midiprog.current = fCurMidiProgs[channel];
  446. CarlaPlugin::setCtrlChannel(channel, sendOsc, sendCallback);
  447. }
  448. // -------------------------------------------------------------------
  449. // Set data (plugin-specific stuff)
  450. void setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  451. {
  452. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  453. CARLA_SAFE_ASSERT_RETURN(fDescriptor->set_parameter_value != nullptr,);
  454. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  455. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  456. const float fixedValue(pData->param.getFixedValue(parameterId, value));
  457. // FIXME - try
  458. fDescriptor->set_parameter_value(fHandle, parameterId, fixedValue);
  459. if (fHandle2 != nullptr)
  460. fDescriptor->set_parameter_value(fHandle2, parameterId, fixedValue);
  461. CarlaPlugin::setParameterValue(parameterId, fixedValue, sendGui, sendOsc, sendCallback);
  462. }
  463. void setCustomData(const char* const type, const char* const key, const char* const value, const bool sendGui) override
  464. {
  465. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  466. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  467. CARLA_SAFE_ASSERT_RETURN(type != nullptr && type[0] != '\0',);
  468. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  469. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  470. carla_debug("NativePlugin::setCustomData(%s, %s, %s, %s)", type, key, value, bool2str(sendGui));
  471. if (std::strcmp(type, CUSTOM_DATA_TYPE_STRING) != 0 && std::strcmp(type, CUSTOM_DATA_TYPE_CHUNK) != 0)
  472. return carla_stderr2("NativePlugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - type is invalid", type, key, value, bool2str(sendGui));
  473. if (std::strcmp(type, CUSTOM_DATA_TYPE_CHUNK) == 0)
  474. {
  475. if (fDescriptor->set_state != nullptr && (fDescriptor->hints & ::PLUGIN_USES_STATE) != 0)
  476. {
  477. const ScopedSingleProcessLocker spl(this, true);
  478. fDescriptor->set_state(fHandle, value);
  479. if (fHandle2 != nullptr)
  480. fDescriptor->set_state(fHandle2, value);
  481. }
  482. }
  483. else if (std::strcmp(key, "midiPrograms") == 0 && fDescriptor->set_midi_program != nullptr)
  484. {
  485. StringArray midiProgramList(StringArray::fromTokens(value, ":", ""));
  486. if (midiProgramList.size() == MAX_MIDI_CHANNELS)
  487. {
  488. uint8_t channel = 0;
  489. for (String *it=midiProgramList.begin(), *end=midiProgramList.end(); it != end; ++it)
  490. {
  491. const int index(it->getIntValue());
  492. if (index >= 0 && index < static_cast<int>(pData->midiprog.count))
  493. {
  494. const uint32_t bank = pData->midiprog.data[index].bank;
  495. const uint32_t program = pData->midiprog.data[index].program;
  496. fDescriptor->set_midi_program(fHandle, channel, bank, program);
  497. if (fHandle2 != nullptr)
  498. fDescriptor->set_midi_program(fHandle2, channel, bank, program);
  499. fCurMidiProgs[channel] = index;
  500. if (pData->ctrlChannel == static_cast<int32_t>(channel))
  501. {
  502. pData->midiprog.current = index;
  503. pData->engine->callback(ENGINE_CALLBACK_MIDI_PROGRAM_CHANGED, pData->id, index, 0, 0.0f, nullptr);
  504. }
  505. }
  506. ++channel;
  507. }
  508. CARLA_SAFE_ASSERT(channel == MAX_MIDI_CHANNELS);
  509. }
  510. }
  511. else
  512. {
  513. if (fDescriptor->set_custom_data != nullptr)
  514. {
  515. fDescriptor->set_custom_data(fHandle, key, value);
  516. if (fHandle2 != nullptr)
  517. fDescriptor->set_custom_data(fHandle2, key, value);
  518. }
  519. if (sendGui && fIsUiVisible && fDescriptor->ui_set_custom_data != nullptr)
  520. fDescriptor->ui_set_custom_data(fHandle, key, value);
  521. }
  522. CarlaPlugin::setCustomData(type, key, value, sendGui);
  523. }
  524. void setMidiProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  525. {
  526. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  527. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  528. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->midiprog.count),);
  529. // TODO, put into check below
  530. if ((pData->hints & PLUGIN_IS_SYNTH) != 0 && (pData->ctrlChannel < 0 || pData->ctrlChannel >= MAX_MIDI_CHANNELS))
  531. return CarlaPlugin::setMidiProgram(index, sendGui, sendOsc, sendCallback);
  532. if (index >= 0)
  533. {
  534. const uint8_t channel = uint8_t((pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS) ? pData->ctrlChannel : 0);
  535. const uint32_t bank = pData->midiprog.data[index].bank;
  536. const uint32_t program = pData->midiprog.data[index].program;
  537. const ScopedSingleProcessLocker spl(this, (sendGui || sendOsc || sendCallback));
  538. try {
  539. fDescriptor->set_midi_program(fHandle, channel, bank, program);
  540. } catch(...) {}
  541. if (fHandle2 != nullptr)
  542. {
  543. try {
  544. fDescriptor->set_midi_program(fHandle2, channel, bank, program);
  545. } catch(...) {}
  546. }
  547. fCurMidiProgs[channel] = index;
  548. }
  549. CarlaPlugin::setMidiProgram(index, sendGui, sendOsc, sendCallback);
  550. }
  551. // -------------------------------------------------------------------
  552. // Set ui stuff
  553. void showCustomUI(const bool yesNo) override
  554. {
  555. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  556. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  557. if (fDescriptor->ui_show == nullptr)
  558. return;
  559. fDescriptor->ui_show(fHandle, yesNo);
  560. if (fIsUiVisible == yesNo)
  561. return;
  562. fIsUiVisible = yesNo;
  563. if (! yesNo)
  564. {
  565. pData->transientTryCounter = 0;
  566. return;
  567. }
  568. #ifndef BUILD_BRIDGE
  569. if ((fDescriptor->hints & ::PLUGIN_USES_PARENT_ID) == 0 && std::strstr(fDescriptor->label, "file") == nullptr)
  570. pData->tryTransient();
  571. #endif
  572. if (fDescriptor->ui_set_custom_data != nullptr)
  573. {
  574. for (LinkedList<CustomData>::Itenerator it = pData->custom.begin(); it.valid(); it.next())
  575. {
  576. const CustomData& cData(it.getValue());
  577. if (std::strcmp(cData.type, CUSTOM_DATA_TYPE_STRING) == 0 && std::strcmp(cData.key, "midiPrograms") != 0)
  578. fDescriptor->ui_set_custom_data(fHandle, cData.key, cData.value);
  579. }
  580. }
  581. if (fDescriptor->ui_set_midi_program != nullptr && pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  582. {
  583. const int32_t index = pData->midiprog.current;
  584. const uint8_t channel = uint8_t((pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS) ? pData->ctrlChannel : 0);
  585. const uint32_t bank = pData->midiprog.data[index].bank;
  586. const uint32_t program = pData->midiprog.data[index].program;
  587. fDescriptor->ui_set_midi_program(fHandle, channel, bank, program);
  588. }
  589. if (fDescriptor->ui_set_parameter_value != nullptr)
  590. {
  591. for (uint32_t i=0; i < pData->param.count; ++i)
  592. fDescriptor->ui_set_parameter_value(fHandle, i, fDescriptor->get_parameter_value(fHandle, i));
  593. }
  594. }
  595. void idle() override
  596. {
  597. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  598. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  599. if (fIsUiVisible && fDescriptor->ui_idle != nullptr)
  600. fDescriptor->ui_idle(fHandle);
  601. CarlaPlugin::idle();
  602. }
  603. // -------------------------------------------------------------------
  604. // Plugin state
  605. void reload() override
  606. {
  607. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr,);
  608. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  609. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  610. carla_debug("NativePlugin::reload() - start");
  611. const EngineProcessMode processMode(pData->engine->getProccessMode());
  612. // Safely disable plugin for reload
  613. const ScopedDisabler sd(this);
  614. if (pData->active)
  615. deactivate();
  616. clearBuffers();
  617. const float sampleRate((float)pData->engine->getSampleRate());
  618. uint32_t aIns, aOuts, mIns, mOuts, params, j;
  619. bool forcedStereoIn, forcedStereoOut;
  620. forcedStereoIn = forcedStereoOut = false;
  621. bool needsCtrlIn, needsCtrlOut;
  622. needsCtrlIn = needsCtrlOut = false;
  623. aIns = fDescriptor->audioIns;
  624. aOuts = fDescriptor->audioOuts;
  625. mIns = fDescriptor->midiIns;
  626. mOuts = fDescriptor->midiOuts;
  627. params = (fDescriptor->get_parameter_count != nullptr && fDescriptor->get_parameter_info != nullptr) ? fDescriptor->get_parameter_count(fHandle) : 0;
  628. if ((pData->options & PLUGIN_OPTION_FORCE_STEREO) != 0 && (aIns == 1 || aOuts == 1) && mIns <= 1 && mOuts <= 1)
  629. {
  630. if (fHandle2 == nullptr)
  631. fHandle2 = fDescriptor->instantiate(&fHost);
  632. if (fHandle2 != nullptr)
  633. {
  634. if (aIns == 1)
  635. {
  636. aIns = 2;
  637. forcedStereoIn = true;
  638. }
  639. if (aOuts == 1)
  640. {
  641. aOuts = 2;
  642. forcedStereoOut = true;
  643. }
  644. }
  645. }
  646. if (aIns > 0)
  647. {
  648. pData->audioIn.createNew(aIns);
  649. fAudioInBuffers = new float*[aIns];
  650. for (uint32_t i=0; i < aIns; ++i)
  651. fAudioInBuffers[i] = nullptr;
  652. }
  653. if (aOuts > 0)
  654. {
  655. pData->audioOut.createNew(aOuts);
  656. fAudioOutBuffers = new float*[aOuts];
  657. needsCtrlIn = true;
  658. for (uint32_t i=0; i < aOuts; ++i)
  659. fAudioOutBuffers[i] = nullptr;
  660. }
  661. if (mIns > 0)
  662. {
  663. fMidiIn.createNew(mIns);
  664. needsCtrlIn = (mIns == 1);
  665. }
  666. if (mOuts > 0)
  667. {
  668. fMidiOut.createNew(mOuts);
  669. needsCtrlOut = (mOuts == 1);
  670. }
  671. if (params > 0)
  672. {
  673. pData->param.createNew(params, true);
  674. }
  675. const uint portNameSize(pData->engine->getMaxPortNameSize());
  676. CarlaString portName;
  677. // Audio Ins
  678. for (j=0; j < aIns; ++j)
  679. {
  680. portName.clear();
  681. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  682. {
  683. portName = pData->name;
  684. portName += ":";
  685. }
  686. if (aIns > 1 && ! forcedStereoIn)
  687. {
  688. portName += "input_";
  689. portName += CarlaString(j+1);
  690. }
  691. else
  692. portName += "input";
  693. portName.truncate(portNameSize);
  694. pData->audioIn.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true);
  695. pData->audioIn.ports[j].rindex = j;
  696. if (forcedStereoIn)
  697. {
  698. portName += "_2";
  699. pData->audioIn.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true);
  700. pData->audioIn.ports[1].rindex = j;
  701. break;
  702. }
  703. }
  704. // Audio Outs
  705. for (j=0; j < aOuts; ++j)
  706. {
  707. portName.clear();
  708. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  709. {
  710. portName = pData->name;
  711. portName += ":";
  712. }
  713. if (aOuts > 1 && ! forcedStereoOut)
  714. {
  715. portName += "output_";
  716. portName += CarlaString(j+1);
  717. }
  718. else
  719. portName += "output";
  720. portName.truncate(portNameSize);
  721. pData->audioOut.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  722. pData->audioOut.ports[j].rindex = j;
  723. if (forcedStereoOut)
  724. {
  725. portName += "_2";
  726. pData->audioOut.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  727. pData->audioOut.ports[1].rindex = j;
  728. break;
  729. }
  730. }
  731. // MIDI Input (only if multiple)
  732. if (mIns > 1)
  733. {
  734. for (j=0; j < mIns; ++j)
  735. {
  736. portName.clear();
  737. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  738. {
  739. portName = pData->name;
  740. portName += ":";
  741. }
  742. portName += "midi-in_";
  743. portName += CarlaString(j+1);
  744. portName.truncate(portNameSize);
  745. fMidiIn.ports[j] = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true);
  746. fMidiIn.indexes[j] = j;
  747. }
  748. }
  749. // MIDI Output (only if multiple)
  750. if (mOuts > 1)
  751. {
  752. for (j=0; j < mOuts; ++j)
  753. {
  754. portName.clear();
  755. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  756. {
  757. portName = pData->name;
  758. portName += ":";
  759. }
  760. portName += "midi-out_";
  761. portName += CarlaString(j+1);
  762. portName.truncate(portNameSize);
  763. fMidiOut.ports[j] = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false);
  764. fMidiOut.indexes[j] = j;
  765. }
  766. }
  767. for (j=0; j < params; ++j)
  768. {
  769. const NativeParameter* const paramInfo(fDescriptor->get_parameter_info(fHandle, j));
  770. CARLA_SAFE_ASSERT_CONTINUE(paramInfo != nullptr);
  771. pData->param.data[j].type = PARAMETER_UNKNOWN;
  772. pData->param.data[j].index = static_cast<int32_t>(j);
  773. pData->param.data[j].rindex = static_cast<int32_t>(j);
  774. float min, max, def, step, stepSmall, stepLarge;
  775. // min value
  776. min = paramInfo->ranges.min;
  777. // max value
  778. max = paramInfo->ranges.max;
  779. if (min > max)
  780. max = min;
  781. else if (max < min)
  782. min = max;
  783. if (carla_compareFloats(min, max))
  784. {
  785. carla_stderr2("WARNING - Broken plugin parameter '%s': max == min", paramInfo->name);
  786. max = min + 0.1f;
  787. }
  788. // default value
  789. def = paramInfo->ranges.def;
  790. if (def < min)
  791. def = min;
  792. else if (def > max)
  793. def = max;
  794. if (paramInfo->hints & ::PARAMETER_USES_SAMPLE_RATE)
  795. {
  796. min *= sampleRate;
  797. max *= sampleRate;
  798. def *= sampleRate;
  799. pData->param.data[j].hints |= PARAMETER_USES_SAMPLERATE;
  800. }
  801. if (paramInfo->hints & ::PARAMETER_IS_BOOLEAN)
  802. {
  803. step = max - min;
  804. stepSmall = step;
  805. stepLarge = step;
  806. pData->param.data[j].hints |= PARAMETER_IS_BOOLEAN;
  807. }
  808. else if (paramInfo->hints & ::PARAMETER_IS_INTEGER)
  809. {
  810. step = 1.0f;
  811. stepSmall = 1.0f;
  812. stepLarge = 10.0f;
  813. pData->param.data[j].hints |= PARAMETER_IS_INTEGER;
  814. }
  815. else
  816. {
  817. float range = max - min;
  818. step = range/100.0f;
  819. stepSmall = range/1000.0f;
  820. stepLarge = range/10.0f;
  821. }
  822. if (paramInfo->hints & ::PARAMETER_IS_OUTPUT)
  823. {
  824. pData->param.data[j].type = PARAMETER_OUTPUT;
  825. needsCtrlOut = true;
  826. }
  827. else
  828. {
  829. pData->param.data[j].type = PARAMETER_INPUT;
  830. needsCtrlIn = true;
  831. }
  832. // extra parameter hints
  833. if (paramInfo->hints & ::PARAMETER_IS_ENABLED)
  834. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  835. if (paramInfo->hints & ::PARAMETER_IS_AUTOMABLE)
  836. pData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  837. if (paramInfo->hints & ::PARAMETER_IS_LOGARITHMIC)
  838. pData->param.data[j].hints |= PARAMETER_IS_LOGARITHMIC;
  839. if (paramInfo->hints & ::PARAMETER_USES_SCALEPOINTS)
  840. pData->param.data[j].hints |= PARAMETER_USES_SCALEPOINTS;
  841. if (paramInfo->hints & ::PARAMETER_USES_CUSTOM_TEXT)
  842. pData->param.data[j].hints |= PARAMETER_USES_CUSTOM_TEXT;
  843. pData->param.ranges[j].min = min;
  844. pData->param.ranges[j].max = max;
  845. pData->param.ranges[j].def = def;
  846. pData->param.ranges[j].step = step;
  847. pData->param.ranges[j].stepSmall = stepSmall;
  848. pData->param.ranges[j].stepLarge = stepLarge;
  849. }
  850. if (needsCtrlIn)
  851. {
  852. portName.clear();
  853. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  854. {
  855. portName = pData->name;
  856. portName += ":";
  857. }
  858. portName += "events-in";
  859. portName.truncate(portNameSize);
  860. pData->event.portIn = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true);
  861. }
  862. if (needsCtrlOut)
  863. {
  864. portName.clear();
  865. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  866. {
  867. portName = pData->name;
  868. portName += ":";
  869. }
  870. portName += "events-out";
  871. portName.truncate(portNameSize);
  872. pData->event.portOut = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false);
  873. }
  874. if (forcedStereoIn || forcedStereoOut)
  875. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  876. else
  877. pData->options &= ~PLUGIN_OPTION_FORCE_STEREO;
  878. // plugin hints
  879. pData->hints = 0x0;
  880. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  881. pData->hints |= PLUGIN_CAN_DRYWET;
  882. if (aOuts > 0)
  883. pData->hints |= PLUGIN_CAN_VOLUME;
  884. if (aOuts >= 2 && aOuts % 2 == 0)
  885. pData->hints |= PLUGIN_CAN_BALANCE;
  886. // native plugin hints
  887. if (fDescriptor->hints & ::PLUGIN_IS_RTSAFE)
  888. pData->hints |= PLUGIN_IS_RTSAFE;
  889. if (fDescriptor->hints & ::PLUGIN_IS_SYNTH)
  890. pData->hints |= PLUGIN_IS_SYNTH;
  891. if (fDescriptor->hints & ::PLUGIN_HAS_UI)
  892. pData->hints |= PLUGIN_HAS_CUSTOM_UI;
  893. if (fDescriptor->hints & ::PLUGIN_NEEDS_SINGLE_THREAD)
  894. pData->hints |= PLUGIN_NEEDS_SINGLE_THREAD;
  895. if (fDescriptor->hints & ::PLUGIN_NEEDS_FIXED_BUFFERS)
  896. pData->hints |= PLUGIN_NEEDS_FIXED_BUFFERS;
  897. // extra plugin hints
  898. pData->extraHints = 0x0;
  899. if (aIns <= 2 && aOuts <= 2 && (aIns == aOuts || aIns == 0 || aOuts == 0) && mIns <= 1 && mOuts <= 1)
  900. pData->extraHints |= PLUGIN_EXTRA_HINT_CAN_RUN_RACK;
  901. if (fDescriptor->hints & ::PLUGIN_USES_MULTI_PROGS)
  902. pData->extraHints |= PLUGIN_EXTRA_HINT_USES_MULTI_PROGS;
  903. bufferSizeChanged(pData->engine->getBufferSize());
  904. reloadPrograms(true);
  905. if (pData->active)
  906. activate();
  907. carla_debug("NativePlugin::reload() - end");
  908. }
  909. void reloadPrograms(const bool doInit) override
  910. {
  911. carla_debug("NativePlugin::reloadPrograms(%s)", bool2str(doInit));
  912. uint32_t i, oldCount = pData->midiprog.count;
  913. const int32_t current = pData->midiprog.current;
  914. // Delete old programs
  915. pData->midiprog.clear();
  916. // Query new programs
  917. uint32_t count = 0;
  918. if (fDescriptor->get_midi_program_count != nullptr && fDescriptor->get_midi_program_info != nullptr && fDescriptor->set_midi_program != nullptr)
  919. count = fDescriptor->get_midi_program_count(fHandle);
  920. if (count > 0)
  921. {
  922. pData->midiprog.createNew(count);
  923. // Update data
  924. for (i=0; i < count; ++i)
  925. {
  926. const NativeMidiProgram* const mpDesc(fDescriptor->get_midi_program_info(fHandle, i));
  927. CARLA_SAFE_ASSERT_CONTINUE(mpDesc != nullptr);
  928. pData->midiprog.data[i].bank = mpDesc->bank;
  929. pData->midiprog.data[i].program = mpDesc->program;
  930. pData->midiprog.data[i].name = carla_strdup(mpDesc->name);
  931. }
  932. }
  933. #ifndef BUILD_BRIDGE
  934. // Update OSC Names
  935. if (pData->engine->isOscControlRegistered())
  936. {
  937. pData->engine->oscSend_control_set_midi_program_count(pData->id, count);
  938. for (i=0; i < count; ++i)
  939. pData->engine->oscSend_control_set_midi_program_data(pData->id, i, pData->midiprog.data[i].bank, pData->midiprog.data[i].program, pData->midiprog.data[i].name);
  940. }
  941. #endif
  942. if (doInit)
  943. {
  944. if (count > 0)
  945. setMidiProgram(0, false, false, false);
  946. }
  947. else
  948. {
  949. // Check if current program is invalid
  950. bool programChanged = false;
  951. if (count == oldCount+1)
  952. {
  953. // one midi program added, probably created by user
  954. pData->midiprog.current = static_cast<int32_t>(oldCount);
  955. programChanged = true;
  956. }
  957. else if (current < 0 && count > 0)
  958. {
  959. // programs exist now, but not before
  960. pData->midiprog.current = 0;
  961. programChanged = true;
  962. }
  963. else if (current >= 0 && count == 0)
  964. {
  965. // programs existed before, but not anymore
  966. pData->midiprog.current = -1;
  967. programChanged = true;
  968. }
  969. else if (current >= static_cast<int32_t>(count))
  970. {
  971. // current midi program > count
  972. pData->midiprog.current = 0;
  973. programChanged = true;
  974. }
  975. else
  976. {
  977. // no change
  978. pData->midiprog.current = current;
  979. }
  980. if (programChanged)
  981. setMidiProgram(pData->midiprog.current, true, true, true);
  982. pData->engine->callback(ENGINE_CALLBACK_RELOAD_PROGRAMS, pData->id, 0, 0, 0.0f, nullptr);
  983. }
  984. }
  985. // -------------------------------------------------------------------
  986. // Plugin processing
  987. void activate() noexcept override
  988. {
  989. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  990. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  991. if (fDescriptor->activate != nullptr)
  992. {
  993. try {
  994. fDescriptor->activate(fHandle);
  995. } catch(...) {}
  996. if (fHandle2 != nullptr)
  997. {
  998. try {
  999. fDescriptor->activate(fHandle2);
  1000. } catch(...) {}
  1001. }
  1002. }
  1003. }
  1004. void deactivate() noexcept override
  1005. {
  1006. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1007. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1008. if (fDescriptor->deactivate != nullptr)
  1009. {
  1010. try {
  1011. fDescriptor->deactivate(fHandle);
  1012. } catch(...) {}
  1013. if (fHandle2 != nullptr)
  1014. {
  1015. try {
  1016. fDescriptor->deactivate(fHandle2);
  1017. } catch(...) {}
  1018. }
  1019. }
  1020. }
  1021. void process(const float** const audioIn, float** const audioOut, const float** const cvIn, float** const cvOut, const uint32_t frames) override
  1022. {
  1023. // --------------------------------------------------------------------------------------------------------
  1024. // Check if active
  1025. if (! pData->active)
  1026. {
  1027. // disable any output sound
  1028. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1029. FloatVectorOperations::clear(audioOut[i], static_cast<int>(frames));
  1030. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  1031. FloatVectorOperations::clear(cvOut[i], static_cast<int>(frames));
  1032. return;
  1033. }
  1034. fMidiEventCount = 0;
  1035. carla_zeroStruct<NativeMidiEvent>(fMidiEvents, kPluginMaxMidiEvents*2);
  1036. // --------------------------------------------------------------------------------------------------------
  1037. // Check if needs reset
  1038. if (pData->needsReset)
  1039. {
  1040. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1041. {
  1042. fMidiEventCount = MAX_MIDI_CHANNELS*2;
  1043. for (uint8_t k=0, i=MAX_MIDI_CHANNELS; k < MAX_MIDI_CHANNELS; ++k)
  1044. {
  1045. fMidiEvents[k].data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (k & MIDI_CHANNEL_BIT));
  1046. fMidiEvents[k].data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  1047. fMidiEvents[k].data[2] = 0;
  1048. fMidiEvents[k].size = 3;
  1049. fMidiEvents[k+i].data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (k & MIDI_CHANNEL_BIT));
  1050. fMidiEvents[k+i].data[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  1051. fMidiEvents[k+i].data[2] = 0;
  1052. fMidiEvents[k+i].size = 3;
  1053. }
  1054. }
  1055. else if (pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  1056. {
  1057. fMidiEventCount = MAX_MIDI_NOTE;
  1058. for (uint8_t k=0; k < MAX_MIDI_NOTE; ++k)
  1059. {
  1060. fMidiEvents[k].data[0] = uint8_t(MIDI_STATUS_NOTE_OFF | (pData->ctrlChannel & MIDI_CHANNEL_BIT));
  1061. fMidiEvents[k].data[1] = k;
  1062. fMidiEvents[k].data[2] = 0;
  1063. fMidiEvents[k].size = 3;
  1064. }
  1065. }
  1066. pData->needsReset = false;
  1067. }
  1068. // --------------------------------------------------------------------------------------------------------
  1069. // Set TimeInfo
  1070. const EngineTimeInfo& timeInfo(pData->engine->getTimeInfo());
  1071. fTimeInfo.playing = timeInfo.playing;
  1072. fTimeInfo.frame = timeInfo.frame;
  1073. fTimeInfo.usecs = timeInfo.usecs;
  1074. if (timeInfo.valid & EngineTimeInfo::kValidBBT)
  1075. {
  1076. fTimeInfo.bbt.valid = true;
  1077. fTimeInfo.bbt.bar = timeInfo.bbt.bar;
  1078. fTimeInfo.bbt.beat = timeInfo.bbt.beat;
  1079. fTimeInfo.bbt.tick = timeInfo.bbt.tick;
  1080. fTimeInfo.bbt.barStartTick = timeInfo.bbt.barStartTick;
  1081. fTimeInfo.bbt.beatsPerBar = timeInfo.bbt.beatsPerBar;
  1082. fTimeInfo.bbt.beatType = timeInfo.bbt.beatType;
  1083. fTimeInfo.bbt.ticksPerBeat = timeInfo.bbt.ticksPerBeat;
  1084. fTimeInfo.bbt.beatsPerMinute = timeInfo.bbt.beatsPerMinute;
  1085. }
  1086. else
  1087. fTimeInfo.bbt.valid = false;
  1088. // --------------------------------------------------------------------------------------------------------
  1089. // Event Input and Processing
  1090. if (pData->event.portIn != nullptr)
  1091. {
  1092. // ----------------------------------------------------------------------------------------------------
  1093. // MIDI Input (External)
  1094. if (pData->extNotes.mutex.tryLock())
  1095. {
  1096. ExternalMidiNote note = { 0, 0, 0 };
  1097. for (; fMidiEventCount < kPluginMaxMidiEvents*2 && ! pData->extNotes.data.isEmpty();)
  1098. {
  1099. note = pData->extNotes.data.getFirst(note, true);
  1100. CARLA_SAFE_ASSERT_CONTINUE(note.channel >= 0 && note.channel < MAX_MIDI_CHANNELS);
  1101. NativeMidiEvent& nativeEvent(fMidiEvents[fMidiEventCount++]);
  1102. nativeEvent.data[0] = uint8_t((note.velo > 0 ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF) | (note.channel & MIDI_CHANNEL_BIT));
  1103. nativeEvent.data[1] = note.note;
  1104. nativeEvent.data[2] = note.velo;
  1105. nativeEvent.size = 3;
  1106. }
  1107. pData->extNotes.mutex.unlock();
  1108. } // End of MIDI Input (External)
  1109. // ----------------------------------------------------------------------------------------------------
  1110. // Event Input (System)
  1111. #ifndef BUILD_BRIDGE
  1112. bool allNotesOffSent = false;
  1113. #endif
  1114. bool sampleAccurate = (pData->options & PLUGIN_OPTION_FIXED_BUFFERS) == 0;
  1115. uint32_t startTime = 0;
  1116. uint32_t timeOffset = 0;
  1117. uint32_t nextBankId;
  1118. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  1119. nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
  1120. else
  1121. nextBankId = 0;
  1122. for (uint32_t i=0, numEvents=pData->event.portIn->getEventCount(); i < numEvents; ++i)
  1123. {
  1124. const EngineEvent& event(pData->event.portIn->getEvent(i));
  1125. if (event.time >= frames)
  1126. continue;
  1127. CARLA_ASSERT_INT2(event.time >= timeOffset, event.time, timeOffset);
  1128. if (event.time > timeOffset && sampleAccurate)
  1129. {
  1130. if (processSingle(audioIn, audioOut, cvIn, cvOut, event.time - timeOffset, timeOffset))
  1131. {
  1132. startTime = 0;
  1133. timeOffset = event.time;
  1134. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  1135. nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
  1136. else
  1137. nextBankId = 0;
  1138. if (fMidiEventCount > 0)
  1139. {
  1140. carla_zeroStruct<NativeMidiEvent>(fMidiEvents, fMidiEventCount);
  1141. fMidiEventCount = 0;
  1142. }
  1143. }
  1144. else
  1145. startTime += timeOffset;
  1146. }
  1147. // Control change
  1148. switch (event.type)
  1149. {
  1150. case kEngineEventTypeNull:
  1151. break;
  1152. case kEngineEventTypeControl: {
  1153. const EngineControlEvent& ctrlEvent = event.ctrl;
  1154. switch (ctrlEvent.type)
  1155. {
  1156. case kEngineControlEventTypeNull:
  1157. break;
  1158. case kEngineControlEventTypeParameter: {
  1159. #ifndef BUILD_BRIDGE
  1160. // Control backend stuff
  1161. if (event.channel == pData->ctrlChannel)
  1162. {
  1163. float value;
  1164. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_DRYWET) > 0)
  1165. {
  1166. value = ctrlEvent.value;
  1167. setDryWet(value, false, false);
  1168. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, value);
  1169. }
  1170. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_VOLUME) > 0)
  1171. {
  1172. value = ctrlEvent.value*127.0f/100.0f;
  1173. setVolume(value, false, false);
  1174. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
  1175. }
  1176. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_BALANCE) > 0)
  1177. {
  1178. float left, right;
  1179. value = ctrlEvent.value/0.5f - 1.0f;
  1180. if (value < 0.0f)
  1181. {
  1182. left = -1.0f;
  1183. right = (value*2.0f)+1.0f;
  1184. }
  1185. else if (value > 0.0f)
  1186. {
  1187. left = (value*2.0f)-1.0f;
  1188. right = 1.0f;
  1189. }
  1190. else
  1191. {
  1192. left = -1.0f;
  1193. right = 1.0f;
  1194. }
  1195. setBalanceLeft(left, false, false);
  1196. setBalanceRight(right, false, false);
  1197. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  1198. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  1199. }
  1200. }
  1201. #endif
  1202. // Control plugin parameters
  1203. for (uint32_t k=0; k < pData->param.count; ++k)
  1204. {
  1205. if (pData->param.data[k].midiChannel != event.channel)
  1206. continue;
  1207. if (pData->param.data[k].midiCC != ctrlEvent.param)
  1208. continue;
  1209. if (pData->param.data[k].type != PARAMETER_INPUT)
  1210. continue;
  1211. if ((pData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  1212. continue;
  1213. float value;
  1214. if (pData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  1215. {
  1216. value = (ctrlEvent.value < 0.5f) ? pData->param.ranges[k].min : pData->param.ranges[k].max;
  1217. }
  1218. else
  1219. {
  1220. value = pData->param.ranges[k].getUnnormalizedValue(ctrlEvent.value);
  1221. if (pData->param.data[k].hints & PARAMETER_IS_INTEGER)
  1222. value = std::rint(value);
  1223. }
  1224. setParameterValue(k, value, false, false, false);
  1225. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, value);
  1226. }
  1227. if ((pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0 && ctrlEvent.param <= 0x5F)
  1228. {
  1229. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1230. continue;
  1231. NativeMidiEvent& nativeEvent(fMidiEvents[fMidiEventCount++]);
  1232. carla_zeroStruct(nativeEvent);
  1233. nativeEvent.time = sampleAccurate ? startTime : event.time;
  1234. nativeEvent.data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  1235. nativeEvent.data[1] = uint8_t(ctrlEvent.param);
  1236. nativeEvent.data[2] = uint8_t(ctrlEvent.value*127.0f);
  1237. nativeEvent.size = 3;
  1238. }
  1239. break;
  1240. }
  1241. case kEngineControlEventTypeMidiBank:
  1242. if (event.channel == pData->ctrlChannel && (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  1243. nextBankId = ctrlEvent.param;
  1244. break;
  1245. case kEngineControlEventTypeMidiProgram:
  1246. if (event.channel < MAX_MIDI_CHANNELS && (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  1247. {
  1248. const uint32_t nextProgramId(ctrlEvent.param);
  1249. for (uint32_t k=0; k < pData->midiprog.count; ++k)
  1250. {
  1251. if (pData->midiprog.data[k].bank == nextBankId && pData->midiprog.data[k].program == nextProgramId)
  1252. {
  1253. fDescriptor->set_midi_program(fHandle, event.channel, nextBankId, nextProgramId);
  1254. if (fHandle2 != nullptr)
  1255. fDescriptor->set_midi_program(fHandle2, event.channel, nextBankId, nextProgramId);
  1256. fCurMidiProgs[event.channel] = static_cast<int32_t>(k);
  1257. if (event.channel == pData->ctrlChannel)
  1258. pData->postponeRtEvent(kPluginPostRtEventMidiProgramChange, static_cast<int32_t>(k), 0, 0.0f);
  1259. break;
  1260. }
  1261. }
  1262. }
  1263. break;
  1264. case kEngineControlEventTypeAllSoundOff:
  1265. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1266. {
  1267. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1268. continue;
  1269. NativeMidiEvent& nativeEvent(fMidiEvents[fMidiEventCount++]);
  1270. carla_zeroStruct(nativeEvent);
  1271. nativeEvent.port = 0;
  1272. nativeEvent.time = sampleAccurate ? startTime : event.time;
  1273. nativeEvent.data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  1274. nativeEvent.data[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  1275. nativeEvent.data[2] = 0;
  1276. nativeEvent.size = 3;
  1277. }
  1278. break;
  1279. case kEngineControlEventTypeAllNotesOff:
  1280. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1281. {
  1282. #ifndef BUILD_BRIDGE
  1283. if (event.channel == pData->ctrlChannel && ! allNotesOffSent)
  1284. {
  1285. allNotesOffSent = true;
  1286. sendMidiAllNotesOffToCallback();
  1287. }
  1288. #endif
  1289. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1290. continue;
  1291. NativeMidiEvent& nativeEvent(fMidiEvents[fMidiEventCount++]);
  1292. carla_zeroStruct(nativeEvent);
  1293. nativeEvent.time = sampleAccurate ? startTime : event.time;
  1294. nativeEvent.data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  1295. nativeEvent.data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  1296. nativeEvent.data[2] = 0;
  1297. nativeEvent.size = 3;
  1298. }
  1299. break;
  1300. }
  1301. break;
  1302. }
  1303. case kEngineEventTypeMidi: {
  1304. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1305. continue;
  1306. const EngineMidiEvent& midiEvent(event.midi);
  1307. if (midiEvent.size > 4)
  1308. continue;
  1309. static_assert(4 <= EngineMidiEvent::kDataSize, "Incorrect data");
  1310. uint8_t status = uint8_t(MIDI_GET_STATUS_FROM_DATA(midiEvent.data));
  1311. if (status == MIDI_STATUS_CHANNEL_PRESSURE && (pData->options & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) == 0)
  1312. continue;
  1313. if (status == MIDI_STATUS_CONTROL_CHANGE && (pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) == 0)
  1314. continue;
  1315. if (status == MIDI_STATUS_POLYPHONIC_AFTERTOUCH && (pData->options & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) == 0)
  1316. continue;
  1317. if (status == MIDI_STATUS_PITCH_WHEEL_CONTROL && (pData->options & PLUGIN_OPTION_SEND_PITCHBEND) == 0)
  1318. continue;
  1319. // Fix bad note-off
  1320. if (status == MIDI_STATUS_NOTE_ON && midiEvent.data[2] == 0)
  1321. status = MIDI_STATUS_NOTE_OFF;
  1322. NativeMidiEvent& nativeEvent(fMidiEvents[fMidiEventCount++]);
  1323. carla_zeroStruct(nativeEvent);
  1324. nativeEvent.port = midiEvent.port;
  1325. nativeEvent.time = sampleAccurate ? startTime : event.time;
  1326. nativeEvent.size = midiEvent.size;
  1327. nativeEvent.data[0] = uint8_t(status | (event.channel & MIDI_CHANNEL_BIT));
  1328. nativeEvent.data[1] = midiEvent.size >= 2 ? midiEvent.data[1] : 0;
  1329. nativeEvent.data[2] = midiEvent.size >= 3 ? midiEvent.data[2] : 0;
  1330. nativeEvent.data[3] = midiEvent.size == 4 ? midiEvent.data[3] : 0;
  1331. if (status == MIDI_STATUS_NOTE_ON)
  1332. pData->postponeRtEvent(kPluginPostRtEventNoteOn, event.channel, midiEvent.data[1], midiEvent.data[2]);
  1333. else if (status == MIDI_STATUS_NOTE_OFF)
  1334. pData->postponeRtEvent(kPluginPostRtEventNoteOff, event.channel, midiEvent.data[1], 0.0f);
  1335. } break;
  1336. } // switch (event.type)
  1337. }
  1338. pData->postRtEvents.trySplice();
  1339. if (frames > timeOffset)
  1340. processSingle(audioIn, audioOut, cvIn, cvOut, frames - timeOffset, timeOffset);
  1341. } // End of Event Input and Processing
  1342. // --------------------------------------------------------------------------------------------------------
  1343. // Plugin processing (no events)
  1344. else
  1345. {
  1346. processSingle(audioIn, audioOut, cvIn, cvOut, frames, 0);
  1347. } // End of Plugin processing (no events)
  1348. // --------------------------------------------------------------------------------------------------------
  1349. // Control and MIDI Output
  1350. if (fMidiOut.count > 0 || pData->event.portOut != nullptr)
  1351. {
  1352. #ifndef BUILD_BRIDGE
  1353. float value, curValue;
  1354. for (uint32_t k=0; k < pData->param.count; ++k)
  1355. {
  1356. if (pData->param.data[k].type != PARAMETER_OUTPUT)
  1357. continue;
  1358. curValue = fDescriptor->get_parameter_value(fHandle, k);
  1359. pData->param.ranges[k].fixValue(curValue);
  1360. if (pData->param.data[k].midiCC > 0)
  1361. {
  1362. value = pData->param.ranges[k].getNormalizedValue(curValue);
  1363. pData->event.portOut->writeControlEvent(0, pData->param.data[k].midiChannel, kEngineControlEventTypeParameter, static_cast<uint16_t>(pData->param.data[k].midiCC), value);
  1364. }
  1365. }
  1366. #endif
  1367. // reverse lookup MIDI events
  1368. for (uint32_t k = (kPluginMaxMidiEvents*2)-1; k >= fMidiEventCount; --k)
  1369. {
  1370. if (fMidiEvents[k].data[0] == 0)
  1371. break;
  1372. const uint8_t channel = uint8_t(MIDI_GET_CHANNEL_FROM_DATA(fMidiEvents[k].data));
  1373. const uint8_t port = fMidiEvents[k].port;
  1374. if (pData->event.portOut != nullptr)
  1375. pData->event.portOut->writeMidiEvent(fMidiEvents[k].time, channel, port, fMidiEvents[k].size, fMidiEvents[k].data);
  1376. else if (port < fMidiOut.count)
  1377. fMidiOut.ports[port]->writeMidiEvent(fMidiEvents[k].time, channel, port, fMidiEvents[k].size, fMidiEvents[k].data);
  1378. }
  1379. } // End of Control and MIDI Output
  1380. }
  1381. bool processSingle(const float** const audioIn, float** const audioOut, const float** const cvIn, float** const cvOut, const uint32_t frames, const uint32_t timeOffset)
  1382. {
  1383. CARLA_SAFE_ASSERT_RETURN(frames > 0, false);
  1384. if (pData->audioIn.count > 0)
  1385. {
  1386. CARLA_SAFE_ASSERT_RETURN(audioIn != nullptr, false);
  1387. }
  1388. if (pData->audioOut.count > 0)
  1389. {
  1390. CARLA_SAFE_ASSERT_RETURN(audioOut != nullptr, false);
  1391. }
  1392. if (pData->cvIn.count > 0)
  1393. {
  1394. CARLA_SAFE_ASSERT_RETURN(cvIn != nullptr, false);
  1395. }
  1396. if (pData->cvOut.count > 0)
  1397. {
  1398. CARLA_SAFE_ASSERT_RETURN(cvOut != nullptr, false);
  1399. }
  1400. // --------------------------------------------------------------------------------------------------------
  1401. // Try lock, silence otherwise
  1402. if (pData->engine->isOffline())
  1403. {
  1404. pData->singleMutex.lock();
  1405. }
  1406. else if (! pData->singleMutex.tryLock())
  1407. {
  1408. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1409. {
  1410. for (uint32_t k=0; k < frames; ++k)
  1411. audioOut[i][k+timeOffset] = 0.0f;
  1412. }
  1413. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  1414. {
  1415. for (uint32_t k=0; k < frames; ++k)
  1416. cvOut[i][k+timeOffset] = 0.0f;
  1417. }
  1418. return false;
  1419. }
  1420. // --------------------------------------------------------------------------------------------------------
  1421. // Set audio buffers
  1422. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1423. FloatVectorOperations::copy(fAudioInBuffers[i], audioIn[i]+timeOffset, static_cast<int>(frames));
  1424. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1425. FloatVectorOperations::clear(fAudioOutBuffers[i], static_cast<int>(frames));
  1426. #if 0
  1427. // --------------------------------------------------------------------------------------------------------
  1428. // Set CV buffers
  1429. for (uint32_t i=0; i < pData->cvIn.count; ++i)
  1430. FloatVectorOperations::copy(fCvInBuffers[i], cvIn[i]+timeOffset, static_cast<int>(frames));
  1431. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  1432. FloatVectorOperations::clear(fCvOutBuffers[i], static_cast<int>(frames));
  1433. #endif
  1434. // --------------------------------------------------------------------------------------------------------
  1435. // Run plugin
  1436. fIsProcessing = true;
  1437. if (fHandle2 == nullptr)
  1438. {
  1439. fDescriptor->process(fHandle, fAudioInBuffers, fAudioOutBuffers, frames, fMidiEvents, fMidiEventCount);
  1440. }
  1441. else
  1442. {
  1443. fDescriptor->process(fHandle,
  1444. (pData->audioIn.count > 0) ? &fAudioInBuffers[0] : nullptr,
  1445. (pData->audioOut.count > 0) ? &fAudioOutBuffers[0] : nullptr,
  1446. frames, fMidiEvents, fMidiEventCount);
  1447. fDescriptor->process(fHandle2,
  1448. (pData->audioIn.count > 0) ? &fAudioInBuffers[1] : nullptr,
  1449. (pData->audioOut.count > 0) ? &fAudioOutBuffers[1] : nullptr,
  1450. frames, fMidiEvents, fMidiEventCount);
  1451. }
  1452. fIsProcessing = false;
  1453. fTimeInfo.frame += frames;
  1454. #ifndef BUILD_BRIDGE
  1455. // --------------------------------------------------------------------------------------------------------
  1456. // Post-processing (dry/wet, volume and balance)
  1457. {
  1458. const bool doDryWet = (pData->hints & PLUGIN_CAN_DRYWET) != 0 && ! carla_compareFloats(pData->postProc.dryWet, 1.0f);
  1459. const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) != 0 && ! (carla_compareFloats(pData->postProc.balanceLeft, -1.0f) && carla_compareFloats(pData->postProc.balanceRight, 1.0f));
  1460. bool isPair;
  1461. float bufValue, oldBufLeft[doBalance ? frames : 1];
  1462. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1463. {
  1464. // Dry/Wet
  1465. if (doDryWet)
  1466. {
  1467. for (uint32_t k=0; k < frames; ++k)
  1468. {
  1469. bufValue = fAudioInBuffers[(pData->audioIn.count == 1) ? 0 : i][k];
  1470. fAudioOutBuffers[i][k] = (fAudioOutBuffers[i][k] * pData->postProc.dryWet) + (bufValue * (1.0f - pData->postProc.dryWet));
  1471. }
  1472. }
  1473. // Balance
  1474. if (doBalance)
  1475. {
  1476. isPair = (i % 2 == 0);
  1477. if (isPair)
  1478. {
  1479. CARLA_ASSERT(i+1 < pData->audioOut.count);
  1480. FloatVectorOperations::copy(oldBufLeft, fAudioOutBuffers[i], static_cast<int>(frames));
  1481. }
  1482. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  1483. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  1484. for (uint32_t k=0; k < frames; ++k)
  1485. {
  1486. if (isPair)
  1487. {
  1488. // left
  1489. fAudioOutBuffers[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  1490. fAudioOutBuffers[i][k] += fAudioOutBuffers[i+1][k] * (1.0f - balRangeR);
  1491. }
  1492. else
  1493. {
  1494. // right
  1495. fAudioOutBuffers[i][k] = fAudioOutBuffers[i][k] * balRangeR;
  1496. fAudioOutBuffers[i][k] += oldBufLeft[k] * balRangeL;
  1497. }
  1498. }
  1499. }
  1500. // Volume (and buffer copy)
  1501. {
  1502. for (uint32_t k=0; k < frames; ++k)
  1503. audioOut[i][k+timeOffset] = fAudioOutBuffers[i][k] * pData->postProc.volume;
  1504. }
  1505. }
  1506. } // End of Post-processing
  1507. #else
  1508. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1509. {
  1510. for (uint32_t k=0; k < frames; ++k)
  1511. audioOut[i][k+timeOffset] = fAudioOutBuffers[i][k];
  1512. }
  1513. #endif
  1514. #if 0
  1515. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  1516. {
  1517. for (uint32_t k=0; k < frames; ++k)
  1518. cvOut[i][k+timeOffset] = fCvOutBuffers[i][k];
  1519. }
  1520. #endif
  1521. // --------------------------------------------------------------------------------------------------------
  1522. pData->singleMutex.unlock();
  1523. return true;
  1524. }
  1525. void bufferSizeChanged(const uint32_t newBufferSize) override
  1526. {
  1527. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  1528. carla_debug("NativePlugin::bufferSizeChanged(%i)", newBufferSize);
  1529. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1530. {
  1531. if (fAudioInBuffers[i] != nullptr)
  1532. delete[] fAudioInBuffers[i];
  1533. fAudioInBuffers[i] = new float[newBufferSize];
  1534. }
  1535. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1536. {
  1537. if (fAudioOutBuffers[i] != nullptr)
  1538. delete[] fAudioOutBuffers[i];
  1539. fAudioOutBuffers[i] = new float[newBufferSize];
  1540. }
  1541. if (fDescriptor != nullptr && fDescriptor->dispatcher != nullptr)
  1542. {
  1543. fDescriptor->dispatcher(fHandle, PLUGIN_OPCODE_BUFFER_SIZE_CHANGED, 0, static_cast<intptr_t>(newBufferSize), nullptr, 0.0f);
  1544. if (fHandle2 != nullptr)
  1545. fDescriptor->dispatcher(fHandle2, PLUGIN_OPCODE_BUFFER_SIZE_CHANGED, 0, static_cast<intptr_t>(newBufferSize), nullptr, 0.0f);
  1546. }
  1547. }
  1548. void sampleRateChanged(const double newSampleRate) override
  1549. {
  1550. CARLA_ASSERT_INT(newSampleRate > 0.0, newSampleRate);
  1551. carla_debug("NativePlugin::sampleRateChanged(%g)", newSampleRate);
  1552. if (fDescriptor != nullptr && fDescriptor->dispatcher != nullptr)
  1553. {
  1554. fDescriptor->dispatcher(fHandle, PLUGIN_OPCODE_SAMPLE_RATE_CHANGED, 0, 0, nullptr, float(newSampleRate));
  1555. if (fHandle2 != nullptr)
  1556. fDescriptor->dispatcher(fHandle2, PLUGIN_OPCODE_SAMPLE_RATE_CHANGED, 0, 0, nullptr, float(newSampleRate));
  1557. }
  1558. }
  1559. void offlineModeChanged(const bool isOffline) override
  1560. {
  1561. if (fDescriptor != nullptr && fDescriptor->dispatcher != nullptr)
  1562. {
  1563. fDescriptor->dispatcher(fHandle, PLUGIN_OPCODE_OFFLINE_CHANGED, 0, isOffline ? 1 : 0, nullptr, 0.0f);
  1564. if (fHandle2 != nullptr)
  1565. fDescriptor->dispatcher(fHandle2, PLUGIN_OPCODE_OFFLINE_CHANGED, 0, isOffline ? 1 : 0, nullptr, 0.0f);
  1566. }
  1567. }
  1568. // -------------------------------------------------------------------
  1569. // Plugin buffers
  1570. void initBuffers() const noexcept override
  1571. {
  1572. fMidiIn.initBuffers();
  1573. fMidiOut.initBuffers();
  1574. CarlaPlugin::initBuffers();
  1575. }
  1576. void clearBuffers() noexcept override
  1577. {
  1578. carla_debug("NativePlugin::clearBuffers() - start");
  1579. if (fAudioInBuffers != nullptr)
  1580. {
  1581. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1582. {
  1583. if (fAudioInBuffers[i] != nullptr)
  1584. {
  1585. delete[] fAudioInBuffers[i];
  1586. fAudioInBuffers[i] = nullptr;
  1587. }
  1588. }
  1589. delete[] fAudioInBuffers;
  1590. fAudioInBuffers = nullptr;
  1591. }
  1592. if (fAudioOutBuffers != nullptr)
  1593. {
  1594. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1595. {
  1596. if (fAudioOutBuffers[i] != nullptr)
  1597. {
  1598. delete[] fAudioOutBuffers[i];
  1599. fAudioOutBuffers[i] = nullptr;
  1600. }
  1601. }
  1602. delete[] fAudioOutBuffers;
  1603. fAudioOutBuffers = nullptr;
  1604. }
  1605. fMidiIn.clear();
  1606. fMidiOut.clear();
  1607. CarlaPlugin::clearBuffers();
  1608. carla_debug("NativePlugin::clearBuffers() - end");
  1609. }
  1610. // -------------------------------------------------------------------
  1611. // Post-poned UI Stuff
  1612. void uiParameterChange(const uint32_t index, const float value) noexcept override
  1613. {
  1614. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1615. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1616. CARLA_SAFE_ASSERT_RETURN(index < pData->param.count,);
  1617. if (! fIsUiVisible)
  1618. return;
  1619. if (fDescriptor->ui_set_parameter_value != nullptr)
  1620. fDescriptor->ui_set_parameter_value(fHandle, index, value);
  1621. }
  1622. void uiMidiProgramChange(const uint32_t index) noexcept override
  1623. {
  1624. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1625. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1626. CARLA_SAFE_ASSERT_RETURN(index < pData->midiprog.count,);
  1627. if (! fIsUiVisible)
  1628. return;
  1629. if (index >= pData->midiprog.count)
  1630. return;
  1631. if (fDescriptor->ui_set_midi_program != nullptr)
  1632. fDescriptor->ui_set_midi_program(fHandle, 0, pData->midiprog.data[index].bank, pData->midiprog.data[index].program);
  1633. }
  1634. void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) noexcept override
  1635. {
  1636. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1637. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1638. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1639. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1640. CARLA_SAFE_ASSERT_RETURN(velo > 0 && velo < MAX_MIDI_VALUE,);
  1641. if (! fIsUiVisible)
  1642. return;
  1643. // TODO
  1644. }
  1645. void uiNoteOff(const uint8_t channel, const uint8_t note) noexcept override
  1646. {
  1647. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1648. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1649. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1650. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1651. if (! fIsUiVisible)
  1652. return;
  1653. if (fDescriptor == nullptr || fHandle == nullptr)
  1654. return;
  1655. // TODO
  1656. }
  1657. // -------------------------------------------------------------------
  1658. protected:
  1659. uint32_t handleGetBufferSize() const noexcept
  1660. {
  1661. return pData->engine->getBufferSize();
  1662. }
  1663. double handleGetSampleRate() const noexcept
  1664. {
  1665. return pData->engine->getSampleRate();
  1666. }
  1667. bool handleIsOffline() const noexcept
  1668. {
  1669. return pData->engine->isOffline();
  1670. }
  1671. const NativeTimeInfo* handleGetTimeInfo() const noexcept
  1672. {
  1673. CARLA_SAFE_ASSERT_RETURN(fIsProcessing, nullptr);
  1674. return &fTimeInfo;
  1675. }
  1676. bool handleWriteMidiEvent(const NativeMidiEvent* const event)
  1677. {
  1678. CARLA_ASSERT(pData->enabled);
  1679. CARLA_ASSERT(fIsProcessing);
  1680. CARLA_ASSERT(fMidiOut.count > 0 || pData->event.portOut != nullptr);
  1681. CARLA_ASSERT(event != nullptr);
  1682. CARLA_ASSERT(event->data[0] != 0);
  1683. if (! pData->enabled)
  1684. return false;
  1685. if (fMidiOut.count == 0)
  1686. return false;
  1687. if (event == nullptr)
  1688. return false;
  1689. if (event->data[0] == 0)
  1690. return false;
  1691. if (! fIsProcessing)
  1692. {
  1693. carla_stderr2("NativePlugin::handleWriteMidiEvent(%p) - received MIDI out event outside audio thread, ignoring", event);
  1694. return false;
  1695. }
  1696. // reverse-find first free event, and put it there
  1697. for (uint32_t i=(kPluginMaxMidiEvents*2)-1; i > fMidiEventCount; --i)
  1698. {
  1699. if (fMidiEvents[i].data[0] == 0)
  1700. {
  1701. std::memcpy(&fMidiEvents[i], event, sizeof(NativeMidiEvent));
  1702. return true;
  1703. }
  1704. }
  1705. return false;
  1706. }
  1707. void handleUiParameterChanged(const uint32_t index, const float value)
  1708. {
  1709. setParameterValue(index, value, false, true, true);
  1710. }
  1711. void handleUiCustomDataChanged(const char* const key, const char* const value)
  1712. {
  1713. setCustomData(CUSTOM_DATA_TYPE_STRING, key, value, false);
  1714. }
  1715. void handleUiClosed()
  1716. {
  1717. pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, 0, 0, 0.0f, nullptr);
  1718. fIsUiVisible = false;
  1719. }
  1720. const char* handleUiOpenFile(const bool isDir, const char* const title, const char* const filter)
  1721. {
  1722. return pData->engine->runFileCallback(FILE_CALLBACK_OPEN, isDir, title, filter);
  1723. }
  1724. const char* handleUiSaveFile(const bool isDir, const char* const title, const char* const filter)
  1725. {
  1726. return pData->engine->runFileCallback(FILE_CALLBACK_SAVE, isDir, title, filter);
  1727. }
  1728. intptr_t handleDispatcher(const NativeHostDispatcherOpcode opcode, const int32_t index, const intptr_t value, void* const ptr, const float opt)
  1729. {
  1730. carla_debug("NativePlugin::handleDispatcher(%i, %i, " P_INTPTR ", %p, %f)", opcode, index, value, ptr, opt);
  1731. intptr_t ret = 0;
  1732. switch (opcode)
  1733. {
  1734. case ::HOST_OPCODE_NULL:
  1735. break;
  1736. case ::HOST_OPCODE_UPDATE_PARAMETER:
  1737. // TODO
  1738. pData->engine->callback(ENGINE_CALLBACK_UPDATE, pData->id, -1, 0, 0.0f, nullptr);
  1739. break;
  1740. case ::HOST_OPCODE_UPDATE_MIDI_PROGRAM:
  1741. // TODO
  1742. pData->engine->callback(ENGINE_CALLBACK_UPDATE, pData->id, -1, 0, 0.0f, nullptr);
  1743. break;
  1744. case ::HOST_OPCODE_RELOAD_PARAMETERS:
  1745. reload(); // FIXME
  1746. pData->engine->callback(ENGINE_CALLBACK_RELOAD_PARAMETERS, pData->id, -1, 0, 0.0f, nullptr);
  1747. break;
  1748. case ::HOST_OPCODE_RELOAD_MIDI_PROGRAMS:
  1749. reloadPrograms(false);
  1750. pData->engine->callback(ENGINE_CALLBACK_RELOAD_PROGRAMS, pData->id, -1, 0, 0.0f, nullptr);
  1751. break;
  1752. case ::HOST_OPCODE_RELOAD_ALL:
  1753. reload();
  1754. pData->engine->callback(ENGINE_CALLBACK_RELOAD_ALL, pData->id, -1, 0, 0.0f, nullptr);
  1755. break;
  1756. case ::HOST_OPCODE_UI_UNAVAILABLE:
  1757. pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, -1, 0, 0.0f, nullptr);
  1758. break;
  1759. }
  1760. return ret;
  1761. // unused for now
  1762. (void)index;
  1763. (void)value;
  1764. (void)ptr;
  1765. (void)opt;
  1766. }
  1767. // -------------------------------------------------------------------
  1768. public:
  1769. static size_t getPluginCount() noexcept
  1770. {
  1771. return gPluginDescriptors.count();
  1772. }
  1773. static const NativePluginDescriptor* getPluginDescriptor(const size_t index) noexcept
  1774. {
  1775. CARLA_SAFE_ASSERT_RETURN(index < gPluginDescriptors.count(), nullptr);
  1776. return gPluginDescriptors.getAt(index, nullptr);
  1777. }
  1778. // -------------------------------------------------------------------
  1779. void* getNativeHandle() const noexcept override
  1780. {
  1781. return fHandle;
  1782. }
  1783. const void* getNativeDescriptor() const noexcept override
  1784. {
  1785. return fDescriptor;
  1786. }
  1787. // -------------------------------------------------------------------
  1788. bool init(const char* const name, const char* const label)
  1789. {
  1790. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  1791. // ---------------------------------------------------------------
  1792. // first checks
  1793. if (pData->client != nullptr)
  1794. {
  1795. pData->engine->setLastError("Plugin client is already registered");
  1796. return false;
  1797. }
  1798. if (label == nullptr && label[0] != '\0')
  1799. {
  1800. pData->engine->setLastError("null label");
  1801. return false;
  1802. }
  1803. // ---------------------------------------------------------------
  1804. // get descriptor that matches label
  1805. for (LinkedList<const NativePluginDescriptor*>::Itenerator it = gPluginDescriptors.begin(); it.valid(); it.next())
  1806. {
  1807. fDescriptor = it.getValue();
  1808. CARLA_SAFE_ASSERT_BREAK(fDescriptor != nullptr);
  1809. carla_debug("Check vs \"%s\"", fDescriptor->label);
  1810. if (fDescriptor->label != nullptr && std::strcmp(fDescriptor->label, label) == 0)
  1811. break;
  1812. fDescriptor = nullptr;
  1813. }
  1814. if (fDescriptor == nullptr)
  1815. {
  1816. pData->engine->setLastError("Invalid internal plugin");
  1817. return false;
  1818. }
  1819. // ---------------------------------------------------------------
  1820. // set icon
  1821. if (std::strcmp(fDescriptor->label, "audiofile") == 0)
  1822. pData->iconName = carla_strdup_safe("file");
  1823. else if (std::strcmp(fDescriptor->label, "midifile") == 0)
  1824. pData->iconName = carla_strdup_safe("file");
  1825. // ---------------------------------------------------------------
  1826. // get info
  1827. if (name != nullptr && name[0] != '\0')
  1828. pData->name = pData->engine->getUniquePluginName(name);
  1829. else if (fDescriptor->name != nullptr && fDescriptor->name[0] != '\0')
  1830. pData->name = pData->engine->getUniquePluginName(fDescriptor->name);
  1831. else
  1832. pData->name = pData->engine->getUniquePluginName(label);
  1833. {
  1834. CARLA_ASSERT(fHost.uiName == nullptr);
  1835. char uiName[std::strlen(pData->name)+6+1];
  1836. std::strcpy(uiName, pData->name);
  1837. std::strcat(uiName, " (GUI)");
  1838. fHost.uiName = carla_strdup(uiName);
  1839. }
  1840. // ---------------------------------------------------------------
  1841. // register client
  1842. pData->client = pData->engine->addClient(this);
  1843. if (pData->client == nullptr || ! pData->client->isOk())
  1844. {
  1845. pData->engine->setLastError("Failed to register plugin client");
  1846. return false;
  1847. }
  1848. // ---------------------------------------------------------------
  1849. // initialize plugin
  1850. fHandle = fDescriptor->instantiate(&fHost);
  1851. if (fHandle == nullptr)
  1852. {
  1853. pData->engine->setLastError("Plugin failed to initialize");
  1854. return false;
  1855. }
  1856. // ---------------------------------------------------------------
  1857. // set default options
  1858. const bool hasMidiProgs(fDescriptor->get_midi_program_count != nullptr && fDescriptor->get_midi_program_count(fHandle) > 0);
  1859. pData->options = 0x0;
  1860. if (hasMidiProgs && (fDescriptor->supports & ::PLUGIN_SUPPORTS_PROGRAM_CHANGES) == 0)
  1861. pData->options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  1862. if (getMidiInCount() > 0 || (fDescriptor->hints & ::PLUGIN_NEEDS_FIXED_BUFFERS) != 0)
  1863. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  1864. if (pData->engine->getOptions().forceStereo)
  1865. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  1866. if (fDescriptor->supports & ::PLUGIN_SUPPORTS_CHANNEL_PRESSURE)
  1867. pData->options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  1868. if (fDescriptor->supports & ::PLUGIN_SUPPORTS_NOTE_AFTERTOUCH)
  1869. pData->options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  1870. if (fDescriptor->supports & ::PLUGIN_SUPPORTS_PITCHBEND)
  1871. pData->options |= PLUGIN_OPTION_SEND_PITCHBEND;
  1872. if (fDescriptor->supports & ::PLUGIN_SUPPORTS_ALL_SOUND_OFF)
  1873. pData->options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  1874. return true;
  1875. }
  1876. private:
  1877. NativePluginHandle fHandle;
  1878. NativePluginHandle fHandle2;
  1879. NativeHostDescriptor fHost;
  1880. const NativePluginDescriptor* fDescriptor;
  1881. bool fIsProcessing;
  1882. bool fIsUiVisible;
  1883. float** fAudioInBuffers;
  1884. float** fAudioOutBuffers;
  1885. uint32_t fMidiEventCount;
  1886. NativeMidiEvent fMidiEvents[kPluginMaxMidiEvents*2];
  1887. int32_t fCurMidiProgs[MAX_MIDI_CHANNELS];
  1888. NativePluginMidiData fMidiIn;
  1889. NativePluginMidiData fMidiOut;
  1890. NativeTimeInfo fTimeInfo;
  1891. // -------------------------------------------------------------------
  1892. #define handlePtr ((NativePlugin*)handle)
  1893. static uint32_t carla_host_get_buffer_size(NativeHostHandle handle) noexcept
  1894. {
  1895. return handlePtr->handleGetBufferSize();
  1896. }
  1897. static double carla_host_get_sample_rate(NativeHostHandle handle) noexcept
  1898. {
  1899. return handlePtr->handleGetSampleRate();
  1900. }
  1901. static bool carla_host_is_offline(NativeHostHandle handle) noexcept
  1902. {
  1903. return handlePtr->handleIsOffline();
  1904. }
  1905. static const NativeTimeInfo* carla_host_get_time_info(NativeHostHandle handle) noexcept
  1906. {
  1907. return handlePtr->handleGetTimeInfo();
  1908. }
  1909. static bool carla_host_write_midi_event(NativeHostHandle handle, const NativeMidiEvent* event)
  1910. {
  1911. return handlePtr->handleWriteMidiEvent(event);
  1912. }
  1913. static void carla_host_ui_parameter_changed(NativeHostHandle handle, uint32_t index, float value)
  1914. {
  1915. handlePtr->handleUiParameterChanged(index, value);
  1916. }
  1917. static void carla_host_ui_custom_data_changed(NativeHostHandle handle, const char* key, const char* value)
  1918. {
  1919. handlePtr->handleUiCustomDataChanged(key, value);
  1920. }
  1921. static void carla_host_ui_closed(NativeHostHandle handle)
  1922. {
  1923. handlePtr->handleUiClosed();
  1924. }
  1925. static const char* carla_host_ui_open_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
  1926. {
  1927. return handlePtr->handleUiOpenFile(isDir, title, filter);
  1928. }
  1929. static const char* carla_host_ui_save_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
  1930. {
  1931. return handlePtr->handleUiSaveFile(isDir, title, filter);
  1932. }
  1933. static intptr_t carla_host_dispatcher(NativeHostHandle handle, NativeHostDispatcherOpcode opcode, int32_t index, intptr_t value, void* ptr, float opt)
  1934. {
  1935. return handlePtr->handleDispatcher(opcode, index, value, ptr, opt);
  1936. }
  1937. #undef handlePtr
  1938. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(NativePlugin)
  1939. };
  1940. // -----------------------------------------------------------------------
  1941. std::size_t CarlaPlugin::getNativePluginCount() noexcept
  1942. {
  1943. return NativePlugin::getPluginCount();
  1944. }
  1945. const NativePluginDescriptor* CarlaPlugin::getNativePluginDescriptor(const std::size_t index) noexcept
  1946. {
  1947. return NativePlugin::getPluginDescriptor(index);
  1948. }
  1949. // -----------------------------------------------------------------------
  1950. CarlaPlugin* CarlaPlugin::newNative(const Initializer& init)
  1951. {
  1952. carla_debug("CarlaPlugin::newNative({%p, \"%s\", \"%s\", \"%s\", " P_INT64 "})", init.engine, init.filename, init.name, init.label, init.uniqueId);
  1953. NativePlugin* const plugin(new NativePlugin(init.engine, init.id));
  1954. if (! plugin->init(init.name, init.label))
  1955. {
  1956. delete plugin;
  1957. return nullptr;
  1958. }
  1959. plugin->reload();
  1960. bool canRun = true;
  1961. if (init.engine->getProccessMode() == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  1962. {
  1963. if (! plugin->canRunInRack())
  1964. {
  1965. init.engine->setLastError("Carla's rack mode can only work with Mono or Stereo Internal plugins, sorry!");
  1966. canRun = false;
  1967. }
  1968. else if (plugin->getCVInCount() > 0 || plugin->getCVInCount() > 0)
  1969. {
  1970. init.engine->setLastError("Carla's rack mode cannot work with plugins that have CV ports, sorry!");
  1971. canRun = false;
  1972. }
  1973. }
  1974. else if (init.engine->getProccessMode() == ENGINE_PROCESS_MODE_PATCHBAY && (plugin->getCVInCount() > 0 || plugin->getCVInCount() > 0))
  1975. {
  1976. init.engine->setLastError("CV ports in patchbay mode is still TODO");
  1977. canRun = false;
  1978. }
  1979. if (! canRun)
  1980. {
  1981. delete plugin;
  1982. return nullptr;
  1983. }
  1984. return plugin;
  1985. }
  1986. // -----------------------------------------------------------------------
  1987. CARLA_BACKEND_END_NAMESPACE