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

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
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
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
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
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449
  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(float** const inBuffer, float** const outBuffer, 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(outBuffer[i], static_cast<int>(frames));
  1030. return;
  1031. }
  1032. fMidiEventCount = 0;
  1033. carla_zeroStruct<NativeMidiEvent>(fMidiEvents, kPluginMaxMidiEvents*2);
  1034. // --------------------------------------------------------------------------------------------------------
  1035. // Check if needs reset
  1036. if (pData->needsReset)
  1037. {
  1038. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1039. {
  1040. for (uint8_t k=0, i=MAX_MIDI_CHANNELS; k < MAX_MIDI_CHANNELS; ++k)
  1041. {
  1042. fMidiEvents[k].data[0] = static_cast<uint8_t>(MIDI_STATUS_CONTROL_CHANGE + k);
  1043. fMidiEvents[k].data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  1044. fMidiEvents[k].data[2] = 0;
  1045. fMidiEvents[k].size = 3;
  1046. fMidiEvents[k+i].data[0] = static_cast<uint8_t>(MIDI_STATUS_CONTROL_CHANGE + k);
  1047. fMidiEvents[k+i].data[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  1048. fMidiEvents[k+i].data[2] = 0;
  1049. fMidiEvents[k+i].size = 3;
  1050. }
  1051. fMidiEventCount = MAX_MIDI_CHANNELS*2;
  1052. }
  1053. else if (pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  1054. {
  1055. for (uint8_t k=0; k < MAX_MIDI_NOTE; ++k)
  1056. {
  1057. fMidiEvents[k].data[0] = static_cast<uint8_t>(MIDI_STATUS_NOTE_OFF + pData->ctrlChannel);
  1058. fMidiEvents[k].data[1] = k;
  1059. fMidiEvents[k].data[2] = 0;
  1060. fMidiEvents[k].size = 3;
  1061. }
  1062. fMidiEventCount = MAX_MIDI_NOTE;
  1063. }
  1064. pData->needsReset = false;
  1065. }
  1066. // --------------------------------------------------------------------------------------------------------
  1067. // Set TimeInfo
  1068. const EngineTimeInfo& timeInfo(pData->engine->getTimeInfo());
  1069. fTimeInfo.playing = timeInfo.playing;
  1070. fTimeInfo.frame = timeInfo.frame;
  1071. fTimeInfo.usecs = timeInfo.usecs;
  1072. if (timeInfo.valid & EngineTimeInfo::kValidBBT)
  1073. {
  1074. fTimeInfo.bbt.valid = true;
  1075. fTimeInfo.bbt.bar = timeInfo.bbt.bar;
  1076. fTimeInfo.bbt.beat = timeInfo.bbt.beat;
  1077. fTimeInfo.bbt.tick = timeInfo.bbt.tick;
  1078. fTimeInfo.bbt.barStartTick = timeInfo.bbt.barStartTick;
  1079. fTimeInfo.bbt.beatsPerBar = timeInfo.bbt.beatsPerBar;
  1080. fTimeInfo.bbt.beatType = timeInfo.bbt.beatType;
  1081. fTimeInfo.bbt.ticksPerBeat = timeInfo.bbt.ticksPerBeat;
  1082. fTimeInfo.bbt.beatsPerMinute = timeInfo.bbt.beatsPerMinute;
  1083. }
  1084. else
  1085. fTimeInfo.bbt.valid = false;
  1086. // --------------------------------------------------------------------------------------------------------
  1087. // Event Input and Processing
  1088. if (pData->event.portIn != nullptr)
  1089. {
  1090. // ----------------------------------------------------------------------------------------------------
  1091. // MIDI Input (External)
  1092. if (pData->extNotes.mutex.tryLock())
  1093. {
  1094. ExternalMidiNote note = { 0, 0, 0 };
  1095. //for (RtLinkedList<ExternalMidiNote>::Itenerator it = pData->extNotes.data.begin(); it.valid(); it.next())
  1096. while (fMidiEventCount < kPluginMaxMidiEvents*2 && ! pData->extNotes.data.isEmpty())
  1097. {
  1098. note = pData->extNotes.data.getFirst(note, true);
  1099. CARLA_SAFE_ASSERT_CONTINUE(note.channel >= 0 && note.channel < MAX_MIDI_CHANNELS);
  1100. fMidiEvents[fMidiEventCount].data[0] = static_cast<uint8_t>((note.velo > 0 ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF) | (note.channel & MIDI_CHANNEL_BIT));
  1101. fMidiEvents[fMidiEventCount].data[1] = note.note;
  1102. fMidiEvents[fMidiEventCount].data[2] = note.velo;
  1103. fMidiEvents[fMidiEventCount].size = 3;
  1104. fMidiEventCount += 1;
  1105. }
  1106. pData->extNotes.mutex.unlock();
  1107. } // End of MIDI Input (External)
  1108. // ----------------------------------------------------------------------------------------------------
  1109. // Event Input (System)
  1110. #ifndef BUILD_BRIDGE
  1111. bool allNotesOffSent = false;
  1112. #endif
  1113. bool sampleAccurate = (pData->options & PLUGIN_OPTION_FIXED_BUFFERS) == 0;
  1114. uint32_t time, nEvents = pData->event.portIn->getEventCount();
  1115. uint32_t startTime = 0;
  1116. uint32_t timeOffset = 0;
  1117. uint32_t nextBankId = 0;
  1118. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  1119. nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
  1120. for (uint32_t i=0; i < nEvents; ++i)
  1121. {
  1122. const EngineEvent& event(pData->event.portIn->getEvent(i));
  1123. time = event.time;
  1124. if (time >= frames)
  1125. continue;
  1126. CARLA_ASSERT_INT2(time >= timeOffset, time, timeOffset);
  1127. if (time > timeOffset && sampleAccurate)
  1128. {
  1129. if (processSingle(inBuffer, outBuffer, time - timeOffset, timeOffset))
  1130. {
  1131. startTime = 0;
  1132. timeOffset = time;
  1133. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  1134. nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
  1135. else
  1136. nextBankId = 0;
  1137. if (fMidiEventCount > 0)
  1138. {
  1139. carla_zeroStruct<NativeMidiEvent>(fMidiEvents, fMidiEventCount);
  1140. fMidiEventCount = 0;
  1141. }
  1142. }
  1143. else
  1144. startTime += timeOffset;
  1145. }
  1146. // Control change
  1147. switch (event.type)
  1148. {
  1149. case kEngineEventTypeNull:
  1150. break;
  1151. case kEngineEventTypeControl: {
  1152. const EngineControlEvent& ctrlEvent = event.ctrl;
  1153. switch (ctrlEvent.type)
  1154. {
  1155. case kEngineControlEventTypeNull:
  1156. break;
  1157. case kEngineControlEventTypeParameter: {
  1158. #ifndef BUILD_BRIDGE
  1159. // Control backend stuff
  1160. if (event.channel == pData->ctrlChannel)
  1161. {
  1162. float value;
  1163. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_DRYWET) > 0)
  1164. {
  1165. value = ctrlEvent.value;
  1166. setDryWet(value, false, false);
  1167. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, value);
  1168. }
  1169. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_VOLUME) > 0)
  1170. {
  1171. value = ctrlEvent.value*127.0f/100.0f;
  1172. setVolume(value, false, false);
  1173. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
  1174. }
  1175. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_BALANCE) > 0)
  1176. {
  1177. float left, right;
  1178. value = ctrlEvent.value/0.5f - 1.0f;
  1179. if (value < 0.0f)
  1180. {
  1181. left = -1.0f;
  1182. right = (value*2.0f)+1.0f;
  1183. }
  1184. else if (value > 0.0f)
  1185. {
  1186. left = (value*2.0f)-1.0f;
  1187. right = 1.0f;
  1188. }
  1189. else
  1190. {
  1191. left = -1.0f;
  1192. right = 1.0f;
  1193. }
  1194. setBalanceLeft(left, false, false);
  1195. setBalanceRight(right, false, false);
  1196. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  1197. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  1198. }
  1199. }
  1200. #endif
  1201. // Control plugin parameters
  1202. for (uint32_t k=0; k < pData->param.count; ++k)
  1203. {
  1204. if (pData->param.data[k].midiChannel != event.channel)
  1205. continue;
  1206. if (pData->param.data[k].midiCC != ctrlEvent.param)
  1207. continue;
  1208. if (pData->param.data[k].type != PARAMETER_INPUT)
  1209. continue;
  1210. if ((pData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  1211. continue;
  1212. float value;
  1213. if (pData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  1214. {
  1215. value = (ctrlEvent.value < 0.5f) ? pData->param.ranges[k].min : pData->param.ranges[k].max;
  1216. }
  1217. else
  1218. {
  1219. value = pData->param.ranges[k].getUnnormalizedValue(ctrlEvent.value);
  1220. if (pData->param.data[k].hints & PARAMETER_IS_INTEGER)
  1221. value = std::rint(value);
  1222. }
  1223. setParameterValue(k, value, false, false, false);
  1224. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, value);
  1225. }
  1226. if ((pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0 && ctrlEvent.param <= 0x5F)
  1227. {
  1228. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1229. continue;
  1230. fMidiEvents[fMidiEventCount].port = 0;
  1231. fMidiEvents[fMidiEventCount].time = sampleAccurate ? startTime : time;
  1232. fMidiEvents[fMidiEventCount].data[0] = static_cast<uint8_t>(MIDI_STATUS_CONTROL_CHANGE + event.channel);
  1233. fMidiEvents[fMidiEventCount].data[1] = static_cast<uint8_t>(ctrlEvent.param);
  1234. fMidiEvents[fMidiEventCount].data[2] = uint8_t(ctrlEvent.value*127.0f);
  1235. fMidiEvents[fMidiEventCount].size = 3;
  1236. fMidiEventCount += 1;
  1237. }
  1238. break;
  1239. }
  1240. case kEngineControlEventTypeMidiBank:
  1241. if (event.channel == pData->ctrlChannel && (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  1242. nextBankId = ctrlEvent.param;
  1243. break;
  1244. case kEngineControlEventTypeMidiProgram:
  1245. if (event.channel < MAX_MIDI_CHANNELS && (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  1246. {
  1247. const uint32_t nextProgramId(ctrlEvent.param);
  1248. for (uint32_t k=0; k < pData->midiprog.count; ++k)
  1249. {
  1250. if (pData->midiprog.data[k].bank == nextBankId && pData->midiprog.data[k].program == nextProgramId)
  1251. {
  1252. fDescriptor->set_midi_program(fHandle, event.channel, nextBankId, nextProgramId);
  1253. if (fHandle2 != nullptr)
  1254. fDescriptor->set_midi_program(fHandle2, event.channel, nextBankId, nextProgramId);
  1255. fCurMidiProgs[event.channel] = static_cast<int32_t>(k);
  1256. if (event.channel == pData->ctrlChannel)
  1257. pData->postponeRtEvent(kPluginPostRtEventMidiProgramChange, static_cast<int32_t>(k), 0, 0.0f);
  1258. break;
  1259. }
  1260. }
  1261. }
  1262. break;
  1263. case kEngineControlEventTypeAllSoundOff:
  1264. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1265. {
  1266. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1267. continue;
  1268. fMidiEvents[fMidiEventCount].port = 0;
  1269. fMidiEvents[fMidiEventCount].time = sampleAccurate ? startTime : time;
  1270. fMidiEvents[fMidiEventCount].data[0] = static_cast<uint8_t>(MIDI_STATUS_CONTROL_CHANGE + event.channel);
  1271. fMidiEvents[fMidiEventCount].data[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  1272. fMidiEvents[fMidiEventCount].data[2] = 0;
  1273. fMidiEvents[fMidiEventCount].size = 3;
  1274. fMidiEventCount += 1;
  1275. }
  1276. break;
  1277. case kEngineControlEventTypeAllNotesOff:
  1278. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1279. {
  1280. #ifndef BUILD_BRIDGE
  1281. if (event.channel == pData->ctrlChannel && ! allNotesOffSent)
  1282. {
  1283. allNotesOffSent = true;
  1284. sendMidiAllNotesOffToCallback();
  1285. }
  1286. #endif
  1287. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1288. continue;
  1289. fMidiEvents[fMidiEventCount].port = 0;
  1290. fMidiEvents[fMidiEventCount].time = sampleAccurate ? startTime : time;
  1291. fMidiEvents[fMidiEventCount].data[0] = static_cast<uint8_t>(MIDI_STATUS_CONTROL_CHANGE + event.channel);
  1292. fMidiEvents[fMidiEventCount].data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  1293. fMidiEvents[fMidiEventCount].data[2] = 0;
  1294. fMidiEvents[fMidiEventCount].size = 3;
  1295. fMidiEventCount += 1;
  1296. }
  1297. break;
  1298. }
  1299. break;
  1300. }
  1301. case kEngineEventTypeMidi: {
  1302. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1303. continue;
  1304. const EngineMidiEvent& midiEvent(event.midi);
  1305. uint8_t status = uint8_t(MIDI_GET_STATUS_FROM_DATA(midiEvent.data));
  1306. uint8_t channel = event.channel;
  1307. if (MIDI_IS_STATUS_CHANNEL_PRESSURE(status) && (pData->options & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) == 0)
  1308. continue;
  1309. if (MIDI_IS_STATUS_CONTROL_CHANGE(status) && (pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) == 0)
  1310. continue;
  1311. if (MIDI_IS_STATUS_POLYPHONIC_AFTERTOUCH(status) && (pData->options & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) == 0)
  1312. continue;
  1313. if (MIDI_IS_STATUS_PITCH_WHEEL_CONTROL(status) && (pData->options & PLUGIN_OPTION_SEND_PITCHBEND) == 0)
  1314. continue;
  1315. // Fix bad note-off
  1316. if (status == MIDI_STATUS_NOTE_ON && midiEvent.data[2] == 0)
  1317. status = MIDI_STATUS_NOTE_OFF;
  1318. fMidiEvents[fMidiEventCount].port = 0;
  1319. fMidiEvents[fMidiEventCount].time = sampleAccurate ? startTime : time;
  1320. fMidiEvents[fMidiEventCount].size = midiEvent.size;
  1321. fMidiEvents[fMidiEventCount].data[0] = static_cast<uint8_t>(status + channel);
  1322. fMidiEvents[fMidiEventCount].data[1] = midiEvent.data[1];
  1323. fMidiEvents[fMidiEventCount].data[2] = midiEvent.data[2];
  1324. fMidiEvents[fMidiEventCount].data[3] = midiEvent.data[3];
  1325. fMidiEventCount += 1;
  1326. if (status == MIDI_STATUS_NOTE_ON)
  1327. pData->postponeRtEvent(kPluginPostRtEventNoteOn, channel, midiEvent.data[1], midiEvent.data[2]);
  1328. else if (status == MIDI_STATUS_NOTE_OFF)
  1329. pData->postponeRtEvent(kPluginPostRtEventNoteOff, channel, midiEvent.data[1], 0.0f);
  1330. break;
  1331. }
  1332. }
  1333. }
  1334. pData->postRtEvents.trySplice();
  1335. if (frames > timeOffset)
  1336. processSingle(inBuffer, outBuffer, frames - timeOffset, timeOffset);
  1337. } // End of Event Input and Processing
  1338. // --------------------------------------------------------------------------------------------------------
  1339. // Plugin processing (no events)
  1340. else
  1341. {
  1342. processSingle(inBuffer, outBuffer, frames, 0);
  1343. } // End of Plugin processing (no events)
  1344. // --------------------------------------------------------------------------------------------------------
  1345. // Control and MIDI Output
  1346. if (fMidiOut.count > 0 || pData->event.portOut != nullptr)
  1347. {
  1348. #ifndef BUILD_BRIDGE
  1349. float value, curValue;
  1350. for (uint32_t k=0; k < pData->param.count; ++k)
  1351. {
  1352. if (pData->param.data[k].type != PARAMETER_OUTPUT)
  1353. continue;
  1354. curValue = fDescriptor->get_parameter_value(fHandle, k);
  1355. pData->param.ranges[k].fixValue(curValue);
  1356. if (pData->param.data[k].midiCC > 0)
  1357. {
  1358. value = pData->param.ranges[k].getNormalizedValue(curValue);
  1359. pData->event.portOut->writeControlEvent(0, pData->param.data[k].midiChannel, kEngineControlEventTypeParameter, static_cast<uint16_t>(pData->param.data[k].midiCC), value);
  1360. }
  1361. }
  1362. #endif
  1363. // reverse lookup MIDI events
  1364. for (uint32_t k = (kPluginMaxMidiEvents*2)-1; k >= fMidiEventCount; --k)
  1365. {
  1366. if (fMidiEvents[k].data[0] == 0)
  1367. break;
  1368. const uint8_t channel = uint8_t(MIDI_GET_CHANNEL_FROM_DATA(fMidiEvents[k].data));
  1369. const uint8_t port = fMidiEvents[k].port;
  1370. if (pData->event.portOut != nullptr)
  1371. pData->event.portOut->writeMidiEvent(fMidiEvents[k].time, channel, port, fMidiEvents[k].size, fMidiEvents[k].data);
  1372. else if (port < fMidiOut.count)
  1373. fMidiOut.ports[port]->writeMidiEvent(fMidiEvents[k].time, channel, port, fMidiEvents[k].size, fMidiEvents[k].data);
  1374. }
  1375. } // End of Control and MIDI Output
  1376. }
  1377. bool processSingle(float** const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t timeOffset)
  1378. {
  1379. CARLA_ASSERT(frames > 0);
  1380. if (frames == 0)
  1381. return false;
  1382. if (pData->audioIn.count > 0)
  1383. {
  1384. CARLA_ASSERT(inBuffer != nullptr);
  1385. if (inBuffer == nullptr)
  1386. return false;
  1387. }
  1388. if (pData->audioOut.count > 0)
  1389. {
  1390. CARLA_ASSERT(outBuffer != nullptr);
  1391. if (outBuffer == nullptr)
  1392. return false;
  1393. }
  1394. uint32_t i, k;
  1395. // --------------------------------------------------------------------------------------------------------
  1396. // Try lock, silence otherwise
  1397. if (pData->engine->isOffline())
  1398. {
  1399. pData->singleMutex.lock();
  1400. }
  1401. else if (! pData->singleMutex.tryLock())
  1402. {
  1403. for (i=0; i < pData->audioOut.count; ++i)
  1404. {
  1405. for (k=0; k < frames; ++k)
  1406. outBuffer[i][k+timeOffset] = 0.0f;
  1407. }
  1408. return false;
  1409. }
  1410. // --------------------------------------------------------------------------------------------------------
  1411. // Reset audio buffers
  1412. for (i=0; i < pData->audioIn.count; ++i)
  1413. FloatVectorOperations::copy(fAudioInBuffers[i], inBuffer[i]+timeOffset, static_cast<int>(frames));
  1414. for (i=0; i < pData->audioOut.count; ++i)
  1415. FloatVectorOperations::clear(fAudioOutBuffers[i], static_cast<int>(frames));
  1416. // --------------------------------------------------------------------------------------------------------
  1417. // Run plugin
  1418. fIsProcessing = true;
  1419. if (fHandle2 == nullptr)
  1420. {
  1421. fDescriptor->process(fHandle, fAudioInBuffers, fAudioOutBuffers, frames, fMidiEvents, fMidiEventCount);
  1422. }
  1423. else
  1424. {
  1425. fDescriptor->process(fHandle,
  1426. (pData->audioIn.count > 0) ? &fAudioInBuffers[0] : nullptr,
  1427. (pData->audioOut.count > 0) ? &fAudioOutBuffers[0] : nullptr,
  1428. frames, fMidiEvents, fMidiEventCount);
  1429. fDescriptor->process(fHandle2,
  1430. (pData->audioIn.count > 0) ? &fAudioInBuffers[1] : nullptr,
  1431. (pData->audioOut.count > 0) ? &fAudioOutBuffers[1] : nullptr,
  1432. frames, fMidiEvents, fMidiEventCount);
  1433. }
  1434. fIsProcessing = false;
  1435. fTimeInfo.frame += frames;
  1436. #ifndef BUILD_BRIDGE
  1437. // --------------------------------------------------------------------------------------------------------
  1438. // Post-processing (dry/wet, volume and balance)
  1439. {
  1440. const bool doDryWet = (pData->hints & PLUGIN_CAN_DRYWET) != 0 && ! carla_compareFloats(pData->postProc.dryWet, 1.0f);
  1441. const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) != 0 && ! (carla_compareFloats(pData->postProc.balanceLeft, -1.0f) && carla_compareFloats(pData->postProc.balanceRight, 1.0f));
  1442. bool isPair;
  1443. float bufValue, oldBufLeft[doBalance ? frames : 1];
  1444. for (i=0; i < pData->audioOut.count; ++i)
  1445. {
  1446. // Dry/Wet
  1447. if (doDryWet)
  1448. {
  1449. for (k=0; k < frames; ++k)
  1450. {
  1451. bufValue = fAudioInBuffers[(pData->audioIn.count == 1) ? 0 : i][k];
  1452. fAudioOutBuffers[i][k] = (fAudioOutBuffers[i][k] * pData->postProc.dryWet) + (bufValue * (1.0f - pData->postProc.dryWet));
  1453. }
  1454. }
  1455. // Balance
  1456. if (doBalance)
  1457. {
  1458. isPair = (i % 2 == 0);
  1459. if (isPair)
  1460. {
  1461. CARLA_ASSERT(i+1 < pData->audioOut.count);
  1462. FloatVectorOperations::copy(oldBufLeft, fAudioOutBuffers[i], static_cast<int>(frames));
  1463. }
  1464. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  1465. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  1466. for (k=0; k < frames; ++k)
  1467. {
  1468. if (isPair)
  1469. {
  1470. // left
  1471. fAudioOutBuffers[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  1472. fAudioOutBuffers[i][k] += fAudioOutBuffers[i+1][k] * (1.0f - balRangeR);
  1473. }
  1474. else
  1475. {
  1476. // right
  1477. fAudioOutBuffers[i][k] = fAudioOutBuffers[i][k] * balRangeR;
  1478. fAudioOutBuffers[i][k] += oldBufLeft[k] * balRangeL;
  1479. }
  1480. }
  1481. }
  1482. // Volume (and buffer copy)
  1483. {
  1484. for (k=0; k < frames; ++k)
  1485. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k] * pData->postProc.volume;
  1486. }
  1487. }
  1488. } // End of Post-processing
  1489. #else
  1490. for (i=0; i < pData->audioOut.count; ++i)
  1491. {
  1492. for (k=0; k < frames; ++k)
  1493. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k];
  1494. }
  1495. #endif
  1496. // --------------------------------------------------------------------------------------------------------
  1497. pData->singleMutex.unlock();
  1498. return true;
  1499. }
  1500. void bufferSizeChanged(const uint32_t newBufferSize) override
  1501. {
  1502. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  1503. carla_debug("NativePlugin::bufferSizeChanged(%i)", newBufferSize);
  1504. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1505. {
  1506. if (fAudioInBuffers[i] != nullptr)
  1507. delete[] fAudioInBuffers[i];
  1508. fAudioInBuffers[i] = new float[newBufferSize];
  1509. }
  1510. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1511. {
  1512. if (fAudioOutBuffers[i] != nullptr)
  1513. delete[] fAudioOutBuffers[i];
  1514. fAudioOutBuffers[i] = new float[newBufferSize];
  1515. }
  1516. if (fDescriptor != nullptr && fDescriptor->dispatcher != nullptr)
  1517. {
  1518. fDescriptor->dispatcher(fHandle, PLUGIN_OPCODE_BUFFER_SIZE_CHANGED, 0, static_cast<intptr_t>(newBufferSize), nullptr, 0.0f);
  1519. if (fHandle2 != nullptr)
  1520. fDescriptor->dispatcher(fHandle2, PLUGIN_OPCODE_BUFFER_SIZE_CHANGED, 0, static_cast<intptr_t>(newBufferSize), nullptr, 0.0f);
  1521. }
  1522. }
  1523. void sampleRateChanged(const double newSampleRate) override
  1524. {
  1525. CARLA_ASSERT_INT(newSampleRate > 0.0, newSampleRate);
  1526. carla_debug("NativePlugin::sampleRateChanged(%g)", newSampleRate);
  1527. if (fDescriptor != nullptr && fDescriptor->dispatcher != nullptr)
  1528. {
  1529. fDescriptor->dispatcher(fHandle, PLUGIN_OPCODE_SAMPLE_RATE_CHANGED, 0, 0, nullptr, float(newSampleRate));
  1530. if (fHandle2 != nullptr)
  1531. fDescriptor->dispatcher(fHandle2, PLUGIN_OPCODE_SAMPLE_RATE_CHANGED, 0, 0, nullptr, float(newSampleRate));
  1532. }
  1533. }
  1534. void offlineModeChanged(const bool isOffline) override
  1535. {
  1536. if (fDescriptor != nullptr && fDescriptor->dispatcher != nullptr)
  1537. {
  1538. fDescriptor->dispatcher(fHandle, PLUGIN_OPCODE_OFFLINE_CHANGED, 0, isOffline ? 1 : 0, nullptr, 0.0f);
  1539. if (fHandle2 != nullptr)
  1540. fDescriptor->dispatcher(fHandle2, PLUGIN_OPCODE_OFFLINE_CHANGED, 0, isOffline ? 1 : 0, nullptr, 0.0f);
  1541. }
  1542. }
  1543. // -------------------------------------------------------------------
  1544. // Plugin buffers
  1545. void initBuffers() const noexcept override
  1546. {
  1547. fMidiIn.initBuffers();
  1548. fMidiOut.initBuffers();
  1549. CarlaPlugin::initBuffers();
  1550. }
  1551. void clearBuffers() noexcept override
  1552. {
  1553. carla_debug("NativePlugin::clearBuffers() - start");
  1554. if (fAudioInBuffers != nullptr)
  1555. {
  1556. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1557. {
  1558. if (fAudioInBuffers[i] != nullptr)
  1559. {
  1560. delete[] fAudioInBuffers[i];
  1561. fAudioInBuffers[i] = nullptr;
  1562. }
  1563. }
  1564. delete[] fAudioInBuffers;
  1565. fAudioInBuffers = nullptr;
  1566. }
  1567. if (fAudioOutBuffers != nullptr)
  1568. {
  1569. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1570. {
  1571. if (fAudioOutBuffers[i] != nullptr)
  1572. {
  1573. delete[] fAudioOutBuffers[i];
  1574. fAudioOutBuffers[i] = nullptr;
  1575. }
  1576. }
  1577. delete[] fAudioOutBuffers;
  1578. fAudioOutBuffers = nullptr;
  1579. }
  1580. fMidiIn.clear();
  1581. fMidiOut.clear();
  1582. CarlaPlugin::clearBuffers();
  1583. carla_debug("NativePlugin::clearBuffers() - end");
  1584. }
  1585. // -------------------------------------------------------------------
  1586. // Post-poned UI Stuff
  1587. void uiParameterChange(const uint32_t index, const float value) noexcept override
  1588. {
  1589. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1590. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1591. CARLA_SAFE_ASSERT_RETURN(index < pData->param.count,);
  1592. if (! fIsUiVisible)
  1593. return;
  1594. if (fDescriptor->ui_set_parameter_value != nullptr)
  1595. fDescriptor->ui_set_parameter_value(fHandle, index, value);
  1596. }
  1597. void uiMidiProgramChange(const uint32_t index) noexcept override
  1598. {
  1599. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1600. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1601. CARLA_SAFE_ASSERT_RETURN(index < pData->midiprog.count,);
  1602. if (! fIsUiVisible)
  1603. return;
  1604. if (index >= pData->midiprog.count)
  1605. return;
  1606. if (fDescriptor->ui_set_midi_program != nullptr)
  1607. fDescriptor->ui_set_midi_program(fHandle, 0, pData->midiprog.data[index].bank, pData->midiprog.data[index].program);
  1608. }
  1609. void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) noexcept override
  1610. {
  1611. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1612. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1613. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1614. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1615. CARLA_SAFE_ASSERT_RETURN(velo > 0 && velo < MAX_MIDI_VALUE,);
  1616. if (! fIsUiVisible)
  1617. return;
  1618. // TODO
  1619. }
  1620. void uiNoteOff(const uint8_t channel, const uint8_t note) noexcept override
  1621. {
  1622. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1623. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1624. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1625. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1626. if (! fIsUiVisible)
  1627. return;
  1628. if (fDescriptor == nullptr || fHandle == nullptr)
  1629. return;
  1630. // TODO
  1631. }
  1632. // -------------------------------------------------------------------
  1633. protected:
  1634. uint32_t handleGetBufferSize() const noexcept
  1635. {
  1636. return pData->engine->getBufferSize();
  1637. }
  1638. double handleGetSampleRate() const noexcept
  1639. {
  1640. return pData->engine->getSampleRate();
  1641. }
  1642. bool handleIsOffline() const noexcept
  1643. {
  1644. return pData->engine->isOffline();
  1645. }
  1646. const NativeTimeInfo* handleGetTimeInfo() const noexcept
  1647. {
  1648. CARLA_SAFE_ASSERT_RETURN(fIsProcessing, nullptr);
  1649. return &fTimeInfo;
  1650. }
  1651. bool handleWriteMidiEvent(const NativeMidiEvent* const event)
  1652. {
  1653. CARLA_ASSERT(pData->enabled);
  1654. CARLA_ASSERT(fIsProcessing);
  1655. CARLA_ASSERT(fMidiOut.count > 0 || pData->event.portOut != nullptr);
  1656. CARLA_ASSERT(event != nullptr);
  1657. CARLA_ASSERT(event->data[0] != 0);
  1658. if (! pData->enabled)
  1659. return false;
  1660. if (fMidiOut.count == 0)
  1661. return false;
  1662. if (event == nullptr)
  1663. return false;
  1664. if (event->data[0] == 0)
  1665. return false;
  1666. if (! fIsProcessing)
  1667. {
  1668. carla_stderr2("NativePlugin::handleWriteMidiEvent(%p) - received MIDI out event outside audio thread, ignoring", event);
  1669. return false;
  1670. }
  1671. // reverse-find first free event, and put it there
  1672. for (uint32_t i=(kPluginMaxMidiEvents*2)-1; i > fMidiEventCount; --i)
  1673. {
  1674. if (fMidiEvents[i].data[0] == 0)
  1675. {
  1676. std::memcpy(&fMidiEvents[i], event, sizeof(NativeMidiEvent));
  1677. return true;
  1678. }
  1679. }
  1680. return false;
  1681. }
  1682. void handleUiParameterChanged(const uint32_t index, const float value)
  1683. {
  1684. setParameterValue(index, value, false, true, true);
  1685. }
  1686. void handleUiCustomDataChanged(const char* const key, const char* const value)
  1687. {
  1688. setCustomData(CUSTOM_DATA_TYPE_STRING, key, value, false);
  1689. }
  1690. void handleUiClosed()
  1691. {
  1692. pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, 0, 0, 0.0f, nullptr);
  1693. fIsUiVisible = false;
  1694. }
  1695. const char* handleUiOpenFile(const bool isDir, const char* const title, const char* const filter)
  1696. {
  1697. return pData->engine->runFileCallback(FILE_CALLBACK_OPEN, isDir, title, filter);
  1698. }
  1699. const char* handleUiSaveFile(const bool isDir, const char* const title, const char* const filter)
  1700. {
  1701. return pData->engine->runFileCallback(FILE_CALLBACK_SAVE, isDir, title, filter);
  1702. }
  1703. intptr_t handleDispatcher(const NativeHostDispatcherOpcode opcode, const int32_t index, const intptr_t value, void* const ptr, const float opt)
  1704. {
  1705. carla_debug("NativePlugin::handleDispatcher(%i, %i, " P_INTPTR ", %p, %f)", opcode, index, value, ptr, opt);
  1706. intptr_t ret = 0;
  1707. switch (opcode)
  1708. {
  1709. case ::HOST_OPCODE_NULL:
  1710. break;
  1711. case ::HOST_OPCODE_UPDATE_PARAMETER:
  1712. // TODO
  1713. pData->engine->callback(ENGINE_CALLBACK_UPDATE, pData->id, -1, 0, 0.0f, nullptr);
  1714. break;
  1715. case ::HOST_OPCODE_UPDATE_MIDI_PROGRAM:
  1716. // TODO
  1717. pData->engine->callback(ENGINE_CALLBACK_UPDATE, pData->id, -1, 0, 0.0f, nullptr);
  1718. break;
  1719. case ::HOST_OPCODE_RELOAD_PARAMETERS:
  1720. reload(); // FIXME
  1721. pData->engine->callback(ENGINE_CALLBACK_RELOAD_PARAMETERS, pData->id, -1, 0, 0.0f, nullptr);
  1722. break;
  1723. case ::HOST_OPCODE_RELOAD_MIDI_PROGRAMS:
  1724. reloadPrograms(false);
  1725. pData->engine->callback(ENGINE_CALLBACK_RELOAD_PROGRAMS, pData->id, -1, 0, 0.0f, nullptr);
  1726. break;
  1727. case ::HOST_OPCODE_RELOAD_ALL:
  1728. reload();
  1729. pData->engine->callback(ENGINE_CALLBACK_RELOAD_ALL, pData->id, -1, 0, 0.0f, nullptr);
  1730. break;
  1731. case ::HOST_OPCODE_UI_UNAVAILABLE:
  1732. pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, -1, 0, 0.0f, nullptr);
  1733. break;
  1734. }
  1735. return ret;
  1736. // unused for now
  1737. (void)index;
  1738. (void)value;
  1739. (void)ptr;
  1740. (void)opt;
  1741. }
  1742. // -------------------------------------------------------------------
  1743. public:
  1744. static size_t getPluginCount() noexcept
  1745. {
  1746. return gPluginDescriptors.count();
  1747. }
  1748. static const NativePluginDescriptor* getPluginDescriptor(const size_t index) noexcept
  1749. {
  1750. CARLA_SAFE_ASSERT_RETURN(index < gPluginDescriptors.count(), nullptr);
  1751. return gPluginDescriptors.getAt(index, nullptr);
  1752. }
  1753. // -------------------------------------------------------------------
  1754. void* getNativeHandle() const noexcept override
  1755. {
  1756. return fHandle;
  1757. }
  1758. const void* getNativeDescriptor() const noexcept override
  1759. {
  1760. return fDescriptor;
  1761. }
  1762. // -------------------------------------------------------------------
  1763. bool init(const char* const name, const char* const label)
  1764. {
  1765. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  1766. // ---------------------------------------------------------------
  1767. // first checks
  1768. if (pData->client != nullptr)
  1769. {
  1770. pData->engine->setLastError("Plugin client is already registered");
  1771. return false;
  1772. }
  1773. if (label == nullptr && label[0] != '\0')
  1774. {
  1775. pData->engine->setLastError("null label");
  1776. return false;
  1777. }
  1778. // ---------------------------------------------------------------
  1779. // get descriptor that matches label
  1780. for (LinkedList<const NativePluginDescriptor*>::Itenerator it = gPluginDescriptors.begin(); it.valid(); it.next())
  1781. {
  1782. fDescriptor = it.getValue();
  1783. CARLA_SAFE_ASSERT_BREAK(fDescriptor != nullptr);
  1784. carla_debug("Check vs \"%s\"", fDescriptor->label);
  1785. if (fDescriptor->label != nullptr && std::strcmp(fDescriptor->label, label) == 0)
  1786. break;
  1787. fDescriptor = nullptr;
  1788. }
  1789. if (fDescriptor == nullptr)
  1790. {
  1791. pData->engine->setLastError("Invalid internal plugin");
  1792. return false;
  1793. }
  1794. // ---------------------------------------------------------------
  1795. // set icon
  1796. if (std::strcmp(fDescriptor->label, "audiofile") == 0)
  1797. pData->iconName = carla_strdup_safe("file");
  1798. else if (std::strcmp(fDescriptor->label, "midifile") == 0)
  1799. pData->iconName = carla_strdup_safe("file");
  1800. // ---------------------------------------------------------------
  1801. // get info
  1802. if (name != nullptr && name[0] != '\0')
  1803. pData->name = pData->engine->getUniquePluginName(name);
  1804. else if (fDescriptor->name != nullptr && fDescriptor->name[0] != '\0')
  1805. pData->name = pData->engine->getUniquePluginName(fDescriptor->name);
  1806. else
  1807. pData->name = pData->engine->getUniquePluginName(label);
  1808. {
  1809. CARLA_ASSERT(fHost.uiName == nullptr);
  1810. char uiName[std::strlen(pData->name)+6+1];
  1811. std::strcpy(uiName, pData->name);
  1812. std::strcat(uiName, " (GUI)");
  1813. fHost.uiName = carla_strdup(uiName);
  1814. }
  1815. // ---------------------------------------------------------------
  1816. // register client
  1817. pData->client = pData->engine->addClient(this);
  1818. if (pData->client == nullptr || ! pData->client->isOk())
  1819. {
  1820. pData->engine->setLastError("Failed to register plugin client");
  1821. return false;
  1822. }
  1823. // ---------------------------------------------------------------
  1824. // initialize plugin
  1825. fHandle = fDescriptor->instantiate(&fHost);
  1826. if (fHandle == nullptr)
  1827. {
  1828. pData->engine->setLastError("Plugin failed to initialize");
  1829. return false;
  1830. }
  1831. // ---------------------------------------------------------------
  1832. // set default options
  1833. const bool hasMidiProgs(fDescriptor->get_midi_program_count != nullptr && fDescriptor->get_midi_program_count(fHandle) > 0);
  1834. pData->options = 0x0;
  1835. if (hasMidiProgs && (fDescriptor->supports & ::PLUGIN_SUPPORTS_PROGRAM_CHANGES) == 0)
  1836. pData->options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  1837. if (getMidiInCount() > 0 || (fDescriptor->hints & ::PLUGIN_NEEDS_FIXED_BUFFERS) != 0)
  1838. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  1839. if (pData->engine->getOptions().forceStereo)
  1840. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  1841. if (fDescriptor->supports & ::PLUGIN_SUPPORTS_CHANNEL_PRESSURE)
  1842. pData->options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  1843. if (fDescriptor->supports & ::PLUGIN_SUPPORTS_NOTE_AFTERTOUCH)
  1844. pData->options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  1845. if (fDescriptor->supports & ::PLUGIN_SUPPORTS_PITCHBEND)
  1846. pData->options |= PLUGIN_OPTION_SEND_PITCHBEND;
  1847. if (fDescriptor->supports & ::PLUGIN_SUPPORTS_ALL_SOUND_OFF)
  1848. pData->options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  1849. return true;
  1850. }
  1851. private:
  1852. NativePluginHandle fHandle;
  1853. NativePluginHandle fHandle2;
  1854. NativeHostDescriptor fHost;
  1855. const NativePluginDescriptor* fDescriptor;
  1856. bool fIsProcessing;
  1857. bool fIsUiVisible;
  1858. float** fAudioInBuffers;
  1859. float** fAudioOutBuffers;
  1860. uint32_t fMidiEventCount;
  1861. NativeMidiEvent fMidiEvents[kPluginMaxMidiEvents*2];
  1862. int32_t fCurMidiProgs[MAX_MIDI_CHANNELS];
  1863. NativePluginMidiData fMidiIn;
  1864. NativePluginMidiData fMidiOut;
  1865. NativeTimeInfo fTimeInfo;
  1866. // -------------------------------------------------------------------
  1867. #define handlePtr ((NativePlugin*)handle)
  1868. static uint32_t carla_host_get_buffer_size(NativeHostHandle handle) noexcept
  1869. {
  1870. return handlePtr->handleGetBufferSize();
  1871. }
  1872. static double carla_host_get_sample_rate(NativeHostHandle handle) noexcept
  1873. {
  1874. return handlePtr->handleGetSampleRate();
  1875. }
  1876. static bool carla_host_is_offline(NativeHostHandle handle) noexcept
  1877. {
  1878. return handlePtr->handleIsOffline();
  1879. }
  1880. static const NativeTimeInfo* carla_host_get_time_info(NativeHostHandle handle) noexcept
  1881. {
  1882. return handlePtr->handleGetTimeInfo();
  1883. }
  1884. static bool carla_host_write_midi_event(NativeHostHandle handle, const NativeMidiEvent* event)
  1885. {
  1886. return handlePtr->handleWriteMidiEvent(event);
  1887. }
  1888. static void carla_host_ui_parameter_changed(NativeHostHandle handle, uint32_t index, float value)
  1889. {
  1890. handlePtr->handleUiParameterChanged(index, value);
  1891. }
  1892. static void carla_host_ui_custom_data_changed(NativeHostHandle handle, const char* key, const char* value)
  1893. {
  1894. handlePtr->handleUiCustomDataChanged(key, value);
  1895. }
  1896. static void carla_host_ui_closed(NativeHostHandle handle)
  1897. {
  1898. handlePtr->handleUiClosed();
  1899. }
  1900. static const char* carla_host_ui_open_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
  1901. {
  1902. return handlePtr->handleUiOpenFile(isDir, title, filter);
  1903. }
  1904. static const char* carla_host_ui_save_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
  1905. {
  1906. return handlePtr->handleUiSaveFile(isDir, title, filter);
  1907. }
  1908. static intptr_t carla_host_dispatcher(NativeHostHandle handle, NativeHostDispatcherOpcode opcode, int32_t index, intptr_t value, void* ptr, float opt)
  1909. {
  1910. return handlePtr->handleDispatcher(opcode, index, value, ptr, opt);
  1911. }
  1912. #undef handlePtr
  1913. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(NativePlugin)
  1914. };
  1915. // -----------------------------------------------------------------------
  1916. size_t CarlaPlugin::getNativePluginCount() noexcept
  1917. {
  1918. return NativePlugin::getPluginCount();
  1919. }
  1920. const NativePluginDescriptor* CarlaPlugin::getNativePluginDescriptor(const size_t index) noexcept
  1921. {
  1922. return NativePlugin::getPluginDescriptor(index);
  1923. }
  1924. // -----------------------------------------------------------------------
  1925. CarlaPlugin* CarlaPlugin::newNative(const Initializer& init)
  1926. {
  1927. carla_debug("CarlaPlugin::newNative({%p, \"%s\", \"%s\", \"%s\", " P_INT64 "})", init.engine, init.filename, init.name, init.label, init.uniqueId);
  1928. NativePlugin* const plugin(new NativePlugin(init.engine, init.id));
  1929. if (! plugin->init(init.name, init.label))
  1930. {
  1931. delete plugin;
  1932. return nullptr;
  1933. }
  1934. plugin->reload();
  1935. if (init.engine->getProccessMode() == ENGINE_PROCESS_MODE_CONTINUOUS_RACK && ! plugin->canRunInRack())
  1936. {
  1937. init.engine->setLastError("Carla's rack mode can only work with Mono or Stereo Internal plugins, sorry!");
  1938. delete plugin;
  1939. return nullptr;
  1940. }
  1941. return plugin;
  1942. }
  1943. // -----------------------------------------------------------------------
  1944. CARLA_BACKEND_END_NAMESPACE