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.

CarlaPluginNative.cpp 88KB

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