Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

NativePlugin.cpp 88KB

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