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.

DssiPlugin.cpp 74KB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
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
10 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064
  1. /*
  2. * Carla DSSI Plugin
  3. * Copyright (C) 2011-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_DSSI
  20. #include "CarlaDssiUtils.hpp"
  21. #include "CarlaMathUtils.hpp"
  22. #include <QtCore/QByteArray>
  23. #include <QtCore/QString>
  24. // -----------------------------------------------------
  25. CARLA_BACKEND_START_NAMESPACE
  26. #if 0
  27. }
  28. #endif
  29. // -----------------------------------------------------
  30. class DssiPlugin : public CarlaPlugin
  31. {
  32. public:
  33. DssiPlugin(CarlaEngine* const engine, const unsigned int id)
  34. : CarlaPlugin(engine, id),
  35. fHandle(nullptr),
  36. fHandle2(nullptr),
  37. fDescriptor(nullptr),
  38. fDssiDescriptor(nullptr),
  39. fUsesCustomData(false),
  40. fUiFilename(nullptr),
  41. fAudioInBuffers(nullptr),
  42. fAudioOutBuffers(nullptr),
  43. fParamBuffers(nullptr)
  44. {
  45. carla_debug("DssiPlugin::DssiPlugin(%p, %i)", engine, id);
  46. pData->osc.thread.setMode(CarlaPluginThread::PLUGIN_THREAD_DSSI_GUI);
  47. }
  48. ~DssiPlugin() override
  49. {
  50. carla_debug("DssiPlugin::~DssiPlugin()");
  51. // close UI
  52. if (pData->hints & PLUGIN_HAS_CUSTOM_UI)
  53. {
  54. showCustomUI(false);
  55. pData->osc.thread.stop(static_cast<int>(pData->engine->getOptions().uiBridgesTimeout * 2));
  56. }
  57. pData->singleMutex.lock();
  58. pData->masterMutex.lock();
  59. if (pData->client != nullptr && pData->client->isActive())
  60. pData->client->deactivate();
  61. if (pData->active)
  62. {
  63. deactivate();
  64. pData->active = false;
  65. }
  66. if (fDescriptor != nullptr)
  67. {
  68. if (pData->name != nullptr && fDssiDescriptor != nullptr && fDssiDescriptor->run_synth == nullptr && fDssiDescriptor->run_multiple_synths != nullptr)
  69. removeUniqueMultiSynth(fDescriptor->Label);
  70. if (fDescriptor->cleanup != nullptr)
  71. {
  72. if (fHandle != nullptr)
  73. fDescriptor->cleanup(fHandle);
  74. if (fHandle2 != nullptr)
  75. fDescriptor->cleanup(fHandle2);
  76. }
  77. fHandle = nullptr;
  78. fHandle2 = nullptr;
  79. fDescriptor = nullptr;
  80. fDssiDescriptor = nullptr;
  81. }
  82. if (fUiFilename != nullptr)
  83. {
  84. delete[] fUiFilename;
  85. fUiFilename = nullptr;
  86. }
  87. clearBuffers();
  88. }
  89. // -------------------------------------------------------------------
  90. // Information (base)
  91. PluginType getType() const noexcept override
  92. {
  93. return PLUGIN_DSSI;
  94. }
  95. PluginCategory getCategory() const noexcept override
  96. {
  97. CARLA_SAFE_ASSERT_RETURN(fDssiDescriptor != nullptr, PLUGIN_CATEGORY_NONE);
  98. if (pData->audioIn.count == 0 && pData->audioOut.count > 0 && (fDssiDescriptor->run_synth != nullptr || fDssiDescriptor->run_multiple_synths != nullptr))
  99. return PLUGIN_CATEGORY_SYNTH;
  100. return CarlaPlugin::getCategory();
  101. }
  102. long getUniqueId() const noexcept override
  103. {
  104. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, 0);
  105. return static_cast<long>(fDescriptor->UniqueID);
  106. }
  107. // -------------------------------------------------------------------
  108. // Information (count)
  109. // nothing
  110. // -------------------------------------------------------------------
  111. // Information (current data)
  112. int32_t getChunkData(void** const dataPtr) const noexcept override
  113. {
  114. CARLA_SAFE_ASSERT_RETURN(fUsesCustomData, 0);
  115. CARLA_SAFE_ASSERT_RETURN(pData->options & PLUGIN_OPTION_USE_CHUNKS, 0);
  116. CARLA_SAFE_ASSERT_RETURN(fDssiDescriptor != nullptr, 0);
  117. CARLA_SAFE_ASSERT_RETURN(fDssiDescriptor->get_custom_data != nullptr, 0);
  118. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr, 0);
  119. CARLA_SAFE_ASSERT_RETURN(fHandle2 == nullptr, 0);
  120. CARLA_SAFE_ASSERT_RETURN(dataPtr != nullptr, 0);
  121. int ret = 0;
  122. unsigned long dataSize = 0;
  123. try {
  124. ret = fDssiDescriptor->get_custom_data(fHandle, dataPtr, &dataSize);
  125. } catch(...) {}
  126. return (ret != 0) ? static_cast<int32_t>(dataSize) : 0;
  127. }
  128. // -------------------------------------------------------------------
  129. // Information (per-plugin data)
  130. unsigned int getOptionsAvailable() const noexcept override
  131. {
  132. #ifdef __USE_GNU
  133. const bool isAmSynth(strcasestr(pData->filename, "amsynth"));
  134. const bool isDssiVst(strcasestr(pData->filename, "dssi-vst"));
  135. #else
  136. const bool isAmSynth(std::strstr(pData->filename, "amsynth"));
  137. const bool isDssiVst(std::strstr(pData->filename, "dssi-vst"));
  138. #endif
  139. unsigned int options = 0x0;
  140. options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  141. if (! (isAmSynth || isDssiVst))
  142. {
  143. options |= PLUGIN_OPTION_FIXED_BUFFERS;
  144. if (pData->engine->getProccessMode() != ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  145. {
  146. if (pData->options & PLUGIN_OPTION_FORCE_STEREO)
  147. options |= PLUGIN_OPTION_FORCE_STEREO;
  148. else if (pData->audioIn.count <= 1 && pData->audioOut.count <= 1 && (pData->audioIn.count != 0 || pData->audioOut.count != 0))
  149. options |= PLUGIN_OPTION_FORCE_STEREO;
  150. }
  151. }
  152. if (fUsesCustomData)
  153. options |= PLUGIN_OPTION_USE_CHUNKS;
  154. if (fDssiDescriptor->run_synth != nullptr || fDssiDescriptor->run_multiple_synths != nullptr)
  155. {
  156. options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  157. options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  158. options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  159. options |= PLUGIN_OPTION_SEND_PITCHBEND;
  160. options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  161. }
  162. return options;
  163. }
  164. float getParameterValue(const uint32_t parameterId) const noexcept override
  165. {
  166. CARLA_SAFE_ASSERT_RETURN(fParamBuffers != nullptr, 0.0f);
  167. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0.0f);
  168. return fParamBuffers[parameterId];
  169. }
  170. void getLabel(char* const strBuf) const noexcept override
  171. {
  172. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  173. if (fDescriptor->Label != nullptr)
  174. std::strncpy(strBuf, fDescriptor->Label, STR_MAX);
  175. else
  176. CarlaPlugin::getLabel(strBuf);
  177. }
  178. void getMaker(char* const strBuf) const noexcept override
  179. {
  180. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  181. if (fDescriptor->Maker != nullptr)
  182. std::strncpy(strBuf, fDescriptor->Maker, STR_MAX);
  183. else
  184. CarlaPlugin::getMaker(strBuf);
  185. }
  186. void getCopyright(char* const strBuf) const noexcept override
  187. {
  188. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  189. if (fDescriptor->Copyright != nullptr)
  190. std::strncpy(strBuf, fDescriptor->Copyright, STR_MAX);
  191. else
  192. CarlaPlugin::getCopyright(strBuf);
  193. }
  194. void getRealName(char* const strBuf) const noexcept override
  195. {
  196. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  197. if (fDescriptor->Name != nullptr)
  198. std::strncpy(strBuf, fDescriptor->Name, STR_MAX);
  199. else
  200. CarlaPlugin::getRealName(strBuf);
  201. }
  202. void getParameterName(const uint32_t parameterId, char* const strBuf) const noexcept override
  203. {
  204. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  205. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  206. const int32_t rindex(pData->param.data[parameterId].rindex);
  207. if (rindex < static_cast<int32_t>(fDescriptor->PortCount))
  208. std::strncpy(strBuf, fDescriptor->PortNames[rindex], STR_MAX);
  209. else
  210. CarlaPlugin::getParameterName(parameterId, strBuf);
  211. }
  212. // -------------------------------------------------------------------
  213. // Set data (state)
  214. // nothing
  215. // -------------------------------------------------------------------
  216. // Set data (internal stuff)
  217. // nothing
  218. // -------------------------------------------------------------------
  219. // Set data (plugin-specific stuff)
  220. void setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  221. {
  222. CARLA_SAFE_ASSERT_RETURN(fParamBuffers != nullptr,);
  223. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  224. const float fixedValue(pData->param.getFixedValue(parameterId, value));
  225. fParamBuffers[parameterId] = fixedValue;
  226. CarlaPlugin::setParameterValue(parameterId, fixedValue, sendGui, sendOsc, sendCallback);
  227. }
  228. void setCustomData(const char* const type, const char* const key, const char* const value, const bool sendGui) override
  229. {
  230. CARLA_SAFE_ASSERT_RETURN(fDssiDescriptor != nullptr,);
  231. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  232. CARLA_SAFE_ASSERT_RETURN(type != nullptr && type[0] != '\0',);
  233. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  234. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  235. carla_debug("DssiPlugin::setCustomData(%s, %s, %s, %s)", type, key, value, bool2str(sendGui));
  236. if (std::strcmp(type, CUSTOM_DATA_TYPE_STRING) != 0)
  237. return carla_stderr2("DssiPlugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - type is not string", type, key, value, bool2str(sendGui));
  238. if (fDssiDescriptor->configure != nullptr)
  239. {
  240. fDssiDescriptor->configure(fHandle, key, value);
  241. if (fHandle2 != nullptr)
  242. fDssiDescriptor->configure(fHandle2, key, value);
  243. }
  244. if (sendGui && pData->osc.data.target != nullptr)
  245. osc_send_configure(pData->osc.data, key, value);
  246. if (std::strcmp(key, "reloadprograms") == 0 || std::strcmp(key, "load") == 0 || std::strncmp(key, "patches", 7) == 0)
  247. {
  248. const ScopedSingleProcessLocker spl(this, true);
  249. reloadPrograms(false);
  250. }
  251. CarlaPlugin::setCustomData(type, key, value, sendGui);
  252. }
  253. void setChunkData(const char* const stringData) override
  254. {
  255. CARLA_SAFE_ASSERT_RETURN(fUsesCustomData,);
  256. CARLA_SAFE_ASSERT_RETURN(pData->options & PLUGIN_OPTION_USE_CHUNKS,);
  257. CARLA_SAFE_ASSERT_RETURN(fDssiDescriptor != nullptr,);
  258. CARLA_SAFE_ASSERT_RETURN(fDssiDescriptor->set_custom_data != nullptr,);
  259. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  260. CARLA_SAFE_ASSERT_RETURN(fHandle2 == nullptr,);
  261. CARLA_SAFE_ASSERT_RETURN(stringData != nullptr,);
  262. QByteArray chunk(QByteArray::fromBase64(stringData));
  263. CARLA_SAFE_ASSERT_RETURN(chunk.size() > 0,);
  264. const ScopedSingleProcessLocker spl(this, true);
  265. fDssiDescriptor->set_custom_data(fHandle, chunk.data(), static_cast<ulong>(chunk.size()));
  266. }
  267. void setMidiProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  268. {
  269. CARLA_SAFE_ASSERT_RETURN(fDssiDescriptor != nullptr,);
  270. CARLA_SAFE_ASSERT_RETURN(fDssiDescriptor->select_program != nullptr,);
  271. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  272. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->midiprog.count),);
  273. if (index >= 0)
  274. {
  275. const uint32_t bank = pData->midiprog.data[index].bank;
  276. const uint32_t program = pData->midiprog.data[index].program;
  277. const ScopedSingleProcessLocker spl(this, (sendGui || sendOsc || sendCallback));
  278. try {
  279. fDssiDescriptor->select_program(fHandle, bank, program);
  280. } catch(...) {}
  281. if (fHandle2 != nullptr)
  282. {
  283. try {
  284. fDssiDescriptor->select_program(fHandle2, bank, program);
  285. } catch(...) {}
  286. }
  287. }
  288. CarlaPlugin::setMidiProgram(index, sendGui, sendOsc, sendCallback);
  289. }
  290. // -------------------------------------------------------------------
  291. // Set ui stuff
  292. void showCustomUI(const bool yesNo) override
  293. {
  294. if (yesNo)
  295. {
  296. pData->osc.data.free();
  297. pData->osc.thread.start();
  298. }
  299. else
  300. {
  301. pData->transientTryCounter = 0;
  302. if (pData->osc.data.target != nullptr)
  303. {
  304. osc_send_hide(pData->osc.data);
  305. osc_send_quit(pData->osc.data);
  306. pData->osc.data.free();
  307. }
  308. pData->osc.thread.stop(static_cast<int>(pData->engine->getOptions().uiBridgesTimeout * 2));
  309. }
  310. }
  311. // -------------------------------------------------------------------
  312. // Plugin state
  313. void reload() override
  314. {
  315. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr,);
  316. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  317. CARLA_SAFE_ASSERT_RETURN(fDssiDescriptor != nullptr,);
  318. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  319. carla_debug("DssiPlugin::reload() - start");
  320. const EngineProcessMode processMode(pData->engine->getProccessMode());
  321. // Safely disable plugin for reload
  322. const ScopedDisabler sd(this);
  323. if (pData->active)
  324. deactivate();
  325. clearBuffers();
  326. const float sampleRate(static_cast<float>(pData->engine->getSampleRate()));
  327. const uint32_t portCount(static_cast<uint32_t>(fDescriptor->PortCount));
  328. uint32_t aIns, aOuts, mIns, params;
  329. aIns = aOuts = mIns = params = 0;
  330. bool forcedStereoIn, forcedStereoOut;
  331. forcedStereoIn = forcedStereoOut = false;
  332. bool needsCtrlIn, needsCtrlOut;
  333. needsCtrlIn = needsCtrlOut = false;
  334. if (portCount > 0)
  335. {
  336. CARLA_ASSERT(fDescriptor->PortDescriptors != nullptr);
  337. CARLA_ASSERT(fDescriptor->PortRangeHints != nullptr);
  338. CARLA_ASSERT(fDescriptor->PortNames != nullptr);
  339. for (uint32_t i=0; i < portCount; ++i)
  340. {
  341. const LADSPA_PortDescriptor portType = fDescriptor->PortDescriptors[i];
  342. if (LADSPA_IS_PORT_AUDIO(portType))
  343. {
  344. if (LADSPA_IS_PORT_INPUT(portType))
  345. aIns += 1;
  346. else if (LADSPA_IS_PORT_OUTPUT(portType))
  347. aOuts += 1;
  348. }
  349. else if (LADSPA_IS_PORT_CONTROL(portType))
  350. params += 1;
  351. }
  352. }
  353. if ((pData->options & PLUGIN_OPTION_FORCE_STEREO) != 0 && (aIns == 1 || aOuts == 1))
  354. {
  355. if (fHandle2 == nullptr)
  356. fHandle2 = fDescriptor->instantiate(fDescriptor, (unsigned long)sampleRate);
  357. if (fHandle2 != nullptr)
  358. {
  359. if (aIns == 1)
  360. {
  361. aIns = 2;
  362. forcedStereoIn = true;
  363. }
  364. if (aOuts == 1)
  365. {
  366. aOuts = 2;
  367. forcedStereoOut = true;
  368. }
  369. }
  370. }
  371. if (fDssiDescriptor->run_synth != nullptr || fDssiDescriptor->run_multiple_synths != nullptr)
  372. {
  373. mIns = 1;
  374. needsCtrlIn = true;
  375. }
  376. if (aIns > 0)
  377. {
  378. pData->audioIn.createNew(aIns);
  379. fAudioInBuffers = new float*[aIns];
  380. for (uint32_t i=0; i < aIns; ++i)
  381. fAudioInBuffers[i] = nullptr;
  382. }
  383. if (aOuts > 0)
  384. {
  385. pData->audioOut.createNew(aOuts);
  386. fAudioOutBuffers = new float*[aOuts];
  387. needsCtrlIn = true;
  388. for (uint32_t i=0; i < aOuts; ++i)
  389. fAudioOutBuffers[i] = nullptr;
  390. }
  391. if (params > 0)
  392. {
  393. pData->param.createNew(params, true);
  394. fParamBuffers = new float[params];
  395. FLOAT_CLEAR(fParamBuffers, params);
  396. }
  397. const uint portNameSize(pData->engine->getMaxPortNameSize());
  398. CarlaString portName;
  399. for (uint32_t i=0, iAudioIn=0, iAudioOut=0, iCtrl=0; i < portCount; ++i)
  400. {
  401. const LADSPA_PortDescriptor portType = fDescriptor->PortDescriptors[i];
  402. const LADSPA_PortRangeHint portRangeHints = fDescriptor->PortRangeHints[i];
  403. CARLA_ASSERT(fDescriptor->PortNames[i] != nullptr);
  404. if (LADSPA_IS_PORT_AUDIO(portType))
  405. {
  406. portName.clear();
  407. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  408. {
  409. portName = pData->name;
  410. portName += ":";
  411. }
  412. portName += fDescriptor->PortNames[i];
  413. portName.truncate(portNameSize);
  414. if (LADSPA_IS_PORT_INPUT(portType))
  415. {
  416. uint32_t j = iAudioIn++;
  417. pData->audioIn.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true);
  418. pData->audioIn.ports[j].rindex = i;
  419. if (forcedStereoIn)
  420. {
  421. portName += "_2";
  422. pData->audioIn.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true);
  423. pData->audioIn.ports[1].rindex = i;
  424. }
  425. }
  426. else if (LADSPA_IS_PORT_OUTPUT(portType))
  427. {
  428. uint32_t j = iAudioOut++;
  429. pData->audioOut.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  430. pData->audioOut.ports[j].rindex = i;
  431. if (forcedStereoOut)
  432. {
  433. portName += "_2";
  434. pData->audioOut.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  435. pData->audioOut.ports[1].rindex = i;
  436. }
  437. }
  438. else
  439. carla_stderr2("WARNING - Got a broken Port (Audio, but not input or output)");
  440. }
  441. else if (LADSPA_IS_PORT_CONTROL(portType))
  442. {
  443. uint32_t j = iCtrl++;
  444. pData->param.data[j].hints = 0x0;
  445. pData->param.data[j].index = static_cast<int32_t>(j);
  446. pData->param.data[j].rindex = static_cast<int32_t>(i);
  447. pData->param.data[j].midiCC = -1;
  448. pData->param.data[j].midiChannel = 0;
  449. pData->param.special[j] = PARAMETER_SPECIAL_NULL;
  450. float min, max, def, step, stepSmall, stepLarge;
  451. // min value
  452. if (LADSPA_IS_HINT_BOUNDED_BELOW(portRangeHints.HintDescriptor))
  453. min = portRangeHints.LowerBound;
  454. else
  455. min = 0.0f;
  456. // max value
  457. if (LADSPA_IS_HINT_BOUNDED_ABOVE(portRangeHints.HintDescriptor))
  458. max = portRangeHints.UpperBound;
  459. else
  460. max = 1.0f;
  461. if (min > max)
  462. max = min;
  463. if (max - min == 0.0f)
  464. {
  465. carla_stderr2("WARNING - Broken plugin parameter '%s': max - min == 0.0f", fDescriptor->PortNames[i]);
  466. max = min + 0.1f;
  467. }
  468. // default value
  469. def = get_default_ladspa_port_value(portRangeHints.HintDescriptor, min, max);
  470. if (def < min)
  471. def = min;
  472. else if (def > max)
  473. def = max;
  474. if (LADSPA_IS_HINT_SAMPLE_RATE(portRangeHints.HintDescriptor))
  475. {
  476. min *= sampleRate;
  477. max *= sampleRate;
  478. def *= sampleRate;
  479. pData->param.data[j].hints |= PARAMETER_USES_SAMPLERATE;
  480. }
  481. if (LADSPA_IS_HINT_TOGGLED(portRangeHints.HintDescriptor))
  482. {
  483. step = max - min;
  484. stepSmall = step;
  485. stepLarge = step;
  486. pData->param.data[j].hints |= PARAMETER_IS_BOOLEAN;
  487. }
  488. else if (LADSPA_IS_HINT_INTEGER(portRangeHints.HintDescriptor))
  489. {
  490. step = 1.0f;
  491. stepSmall = 1.0f;
  492. stepLarge = 10.0f;
  493. pData->param.data[j].hints |= PARAMETER_IS_INTEGER;
  494. }
  495. else
  496. {
  497. float range = max - min;
  498. step = range/100.0f;
  499. stepSmall = range/1000.0f;
  500. stepLarge = range/10.0f;
  501. }
  502. if (LADSPA_IS_PORT_INPUT(portType))
  503. {
  504. pData->param.data[j].type = PARAMETER_INPUT;
  505. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  506. pData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  507. needsCtrlIn = true;
  508. // MIDI CC value
  509. if (fDssiDescriptor->get_midi_controller_for_port != nullptr)
  510. {
  511. int controller = fDssiDescriptor->get_midi_controller_for_port(fHandle, i);
  512. if (DSSI_CONTROLLER_IS_SET(controller) && DSSI_IS_CC(controller))
  513. {
  514. int16_t cc = DSSI_CC_NUMBER(controller);
  515. if (! MIDI_IS_CONTROL_BANK_SELECT(cc))
  516. pData->param.data[j].midiCC = cc;
  517. }
  518. }
  519. }
  520. else if (LADSPA_IS_PORT_OUTPUT(portType))
  521. {
  522. pData->param.data[j].type = PARAMETER_OUTPUT;
  523. if (std::strcmp(fDescriptor->PortNames[i], "latency") == 0 || std::strcmp(fDescriptor->PortNames[i], "_latency") == 0)
  524. {
  525. min = 0.0f;
  526. max = sampleRate;
  527. def = 0.0f;
  528. step = 1.0f;
  529. stepSmall = 1.0f;
  530. stepLarge = 1.0f;
  531. pData->param.special[j] = PARAMETER_SPECIAL_LATENCY;
  532. }
  533. else
  534. {
  535. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  536. pData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  537. needsCtrlOut = true;
  538. }
  539. }
  540. else
  541. {
  542. pData->param.data[j].type = PARAMETER_UNKNOWN;
  543. carla_stderr2("WARNING - Got a broken Port (Control, but not input or output)");
  544. }
  545. // extra parameter hints
  546. if (LADSPA_IS_HINT_LOGARITHMIC(portRangeHints.HintDescriptor))
  547. pData->param.data[j].hints |= PARAMETER_IS_LOGARITHMIC;
  548. pData->param.ranges[j].min = min;
  549. pData->param.ranges[j].max = max;
  550. pData->param.ranges[j].def = def;
  551. pData->param.ranges[j].step = step;
  552. pData->param.ranges[j].stepSmall = stepSmall;
  553. pData->param.ranges[j].stepLarge = stepLarge;
  554. // Start parameters in their default values
  555. fParamBuffers[j] = def;
  556. fDescriptor->connect_port(fHandle, i, &fParamBuffers[j]);
  557. if (fHandle2 != nullptr)
  558. fDescriptor->connect_port(fHandle2, i, &fParamBuffers[j]);
  559. }
  560. else
  561. {
  562. // Not Audio or Control
  563. carla_stderr2("ERROR - Got a broken Port (neither Audio or Control)");
  564. fDescriptor->connect_port(fHandle, i, nullptr);
  565. if (fHandle2 != nullptr)
  566. fDescriptor->connect_port(fHandle2, i, nullptr);
  567. }
  568. }
  569. if (needsCtrlIn)
  570. {
  571. portName.clear();
  572. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  573. {
  574. portName = pData->name;
  575. portName += ":";
  576. }
  577. portName += "events-in";
  578. portName.truncate(portNameSize);
  579. pData->event.portIn = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true);
  580. }
  581. if (needsCtrlOut)
  582. {
  583. portName.clear();
  584. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  585. {
  586. portName = pData->name;
  587. portName += ":";
  588. }
  589. portName += "events-out";
  590. portName.truncate(portNameSize);
  591. pData->event.portOut = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false);
  592. }
  593. if (forcedStereoIn || forcedStereoOut)
  594. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  595. else
  596. pData->options &= ~PLUGIN_OPTION_FORCE_STEREO;
  597. // plugin hints
  598. pData->hints = 0x0;
  599. if (LADSPA_IS_HARD_RT_CAPABLE(fDescriptor->Properties))
  600. pData->hints |= PLUGIN_IS_RTSAFE;
  601. if (fUiFilename != nullptr)
  602. pData->hints |= PLUGIN_HAS_CUSTOM_UI;
  603. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  604. pData->hints |= PLUGIN_CAN_DRYWET;
  605. if (aOuts > 0)
  606. pData->hints |= PLUGIN_CAN_VOLUME;
  607. if (aOuts >= 2 && aOuts % 2 == 0)
  608. pData->hints |= PLUGIN_CAN_BALANCE;
  609. // extra plugin hints
  610. pData->extraHints = 0x0;
  611. if (mIns > 0)
  612. pData->extraHints |= PLUGIN_EXTRA_HINT_HAS_MIDI_IN;
  613. if (aIns <= 2 && aOuts <= 2 && (aIns == aOuts || aIns == 0 || aOuts == 0))
  614. pData->extraHints |= PLUGIN_EXTRA_HINT_CAN_RUN_RACK;
  615. // check latency
  616. if (pData->hints & PLUGIN_CAN_DRYWET)
  617. {
  618. for (uint32_t i=0; i < pData->param.count; ++i)
  619. {
  620. if (pData->param.special[i] != PARAMETER_SPECIAL_LATENCY)
  621. continue;
  622. // we need to pre-run the plugin so it can update its latency control-port
  623. float tmpIn[aIns][2];
  624. float tmpOut[aOuts][2];
  625. for (uint32_t j=0; j < aIns; ++j)
  626. {
  627. tmpIn[j][0] = 0.0f;
  628. tmpIn[j][1] = 0.0f;
  629. fDescriptor->connect_port(fHandle, pData->audioIn.ports[j].rindex, tmpIn[j]);
  630. }
  631. for (uint32_t j=0; j < aOuts; ++j)
  632. {
  633. tmpOut[j][0] = 0.0f;
  634. tmpOut[j][1] = 0.0f;
  635. fDescriptor->connect_port(fHandle, pData->audioOut.ports[j].rindex, tmpOut[j]);
  636. }
  637. if (fDescriptor->activate != nullptr)
  638. fDescriptor->activate(fHandle);
  639. fDescriptor->run(fHandle, 2);
  640. if (fDescriptor->deactivate != nullptr)
  641. fDescriptor->deactivate(fHandle);
  642. const uint32_t latency = (uint32_t)fParamBuffers[i];
  643. if (pData->latency != latency)
  644. {
  645. pData->latency = latency;
  646. pData->client->setLatency(latency);
  647. pData->recreateLatencyBuffers();
  648. }
  649. break;
  650. }
  651. }
  652. bufferSizeChanged(pData->engine->getBufferSize());
  653. reloadPrograms(true);
  654. if (pData->active)
  655. activate();
  656. carla_debug("DssiPlugin::reload() - end");
  657. }
  658. void reloadPrograms(const bool doInit) override
  659. {
  660. carla_debug("DssiPlugin::reloadPrograms(%s)", bool2str(doInit));
  661. const uint32_t oldCount = pData->midiprog.count;
  662. const int32_t current = pData->midiprog.current;
  663. // Delete old programs
  664. pData->midiprog.clear();
  665. // Query new programs
  666. uint32_t newCount = 0;
  667. if (fDssiDescriptor->get_program != nullptr && fDssiDescriptor->select_program != nullptr)
  668. {
  669. for (; fDssiDescriptor->get_program(fHandle, newCount) != nullptr;)
  670. ++newCount;
  671. }
  672. if (newCount > 0)
  673. {
  674. pData->midiprog.createNew(newCount);
  675. // Update data
  676. for (uint32_t i=0; i < newCount; ++i)
  677. {
  678. const DSSI_Program_Descriptor* const pdesc(fDssiDescriptor->get_program(fHandle, i));
  679. CARLA_SAFE_ASSERT_CONTINUE(pdesc != nullptr);
  680. CARLA_SAFE_ASSERT(pdesc->Name != nullptr);
  681. pData->midiprog.data[i].bank = static_cast<uint32_t>(pdesc->Bank);
  682. pData->midiprog.data[i].program = static_cast<uint32_t>(pdesc->Program);
  683. pData->midiprog.data[i].name = carla_strdup(pdesc->Name);
  684. }
  685. }
  686. #ifndef BUILD_BRIDGE
  687. // Update OSC Names
  688. if (pData->engine->isOscControlRegistered())
  689. {
  690. pData->engine->oscSend_control_set_midi_program_count(pData->id, newCount);
  691. for (uint32_t i=0; i < newCount; ++i)
  692. 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);
  693. }
  694. #endif
  695. if (doInit)
  696. {
  697. if (newCount > 0)
  698. setMidiProgram(0, false, false, false);
  699. }
  700. else
  701. {
  702. // Check if current program is invalid
  703. bool programChanged = false;
  704. if (newCount == oldCount+1)
  705. {
  706. // one midi program added, probably created by user
  707. pData->midiprog.current = static_cast<int32_t>(oldCount);
  708. programChanged = true;
  709. }
  710. else if (current < 0 && newCount > 0)
  711. {
  712. // programs exist now, but not before
  713. pData->midiprog.current = 0;
  714. programChanged = true;
  715. }
  716. else if (current >= 0 && newCount == 0)
  717. {
  718. // programs existed before, but not anymore
  719. pData->midiprog.current = -1;
  720. programChanged = true;
  721. }
  722. else if (current >= static_cast<int32_t>(newCount))
  723. {
  724. // current midi program > count
  725. pData->midiprog.current = 0;
  726. programChanged = true;
  727. }
  728. else
  729. {
  730. // no change
  731. pData->midiprog.current = current;
  732. }
  733. if (programChanged)
  734. setMidiProgram(pData->midiprog.current, true, true, true);
  735. pData->engine->callback(ENGINE_CALLBACK_RELOAD_PROGRAMS, pData->id, 0, 0, 0.0f, nullptr);
  736. }
  737. }
  738. // -------------------------------------------------------------------
  739. // Plugin processing
  740. void activate() noexcept override
  741. {
  742. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  743. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  744. if (fDescriptor->activate != nullptr)
  745. {
  746. try {
  747. fDescriptor->activate(fHandle);
  748. } catch(...) {}
  749. if (fHandle2 != nullptr)
  750. {
  751. try {
  752. fDescriptor->activate(fHandle2);
  753. } catch(...) {}
  754. }
  755. }
  756. }
  757. void deactivate() noexcept override
  758. {
  759. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  760. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  761. if (fDescriptor->deactivate != nullptr)
  762. {
  763. try {
  764. fDescriptor->deactivate(fHandle);
  765. } catch(...) {}
  766. if (fHandle2 != nullptr)
  767. {
  768. try {
  769. fDescriptor->deactivate(fHandle2);
  770. } catch(...) {}
  771. }
  772. }
  773. }
  774. void process(float** const inBuffer, float** const outBuffer, const uint32_t frames) override
  775. {
  776. // --------------------------------------------------------------------------------------------------------
  777. // Check if active
  778. if (! pData->active)
  779. {
  780. // disable any output sound
  781. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  782. FLOAT_CLEAR(outBuffer[i], frames);
  783. return;
  784. }
  785. unsigned long midiEventCount = 0;
  786. // --------------------------------------------------------------------------------------------------------
  787. // Check if needs reset
  788. if (pData->needsReset)
  789. {
  790. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  791. {
  792. midiEventCount = MAX_MIDI_CHANNELS*2;
  793. carla_zeroStruct<snd_seq_event_t>(fMidiEvents, midiEventCount);
  794. for (unsigned char i=0, k=MAX_MIDI_CHANNELS; i < MAX_MIDI_CHANNELS; ++i)
  795. {
  796. fMidiEvents[i].type = SND_SEQ_EVENT_CONTROLLER;
  797. fMidiEvents[i].data.control.channel = i;
  798. fMidiEvents[i].data.control.param = MIDI_CONTROL_ALL_NOTES_OFF;
  799. fMidiEvents[k+i].type = SND_SEQ_EVENT_CONTROLLER;
  800. fMidiEvents[k+i].data.control.channel = i;
  801. fMidiEvents[k+i].data.control.param = MIDI_CONTROL_ALL_SOUND_OFF;
  802. }
  803. }
  804. else if (pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  805. {
  806. midiEventCount = MAX_MIDI_NOTE;
  807. carla_zeroStruct<snd_seq_event_t>(fMidiEvents, midiEventCount);
  808. for (unsigned char i=0; i < MAX_MIDI_NOTE; ++i)
  809. {
  810. fMidiEvents[i].type = SND_SEQ_EVENT_NOTEOFF;
  811. fMidiEvents[i].data.note.channel = static_cast<uchar>(pData->ctrlChannel);
  812. fMidiEvents[i].data.note.note = i;
  813. }
  814. }
  815. if (pData->latency > 0)
  816. {
  817. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  818. FLOAT_CLEAR(pData->latencyBuffers[i], pData->latency);
  819. }
  820. pData->needsReset = false;
  821. }
  822. // --------------------------------------------------------------------------------------------------------
  823. // Event Input and Processing
  824. if (pData->event.portIn != nullptr)
  825. {
  826. // ----------------------------------------------------------------------------------------------------
  827. // MIDI Input (External)
  828. if (pData->extNotes.mutex.tryLock())
  829. {
  830. for (; midiEventCount < kPluginMaxMidiEvents && ! pData->extNotes.data.isEmpty();)
  831. {
  832. const ExternalMidiNote& note(pData->extNotes.data.getFirst(true));
  833. CARLA_SAFE_ASSERT_CONTINUE(note.channel >= 0 && note.channel < MAX_MIDI_CHANNELS);
  834. snd_seq_event_t& midiEvent(fMidiEvents[midiEventCount++]);
  835. carla_zeroStruct<snd_seq_event_t>(midiEvent);
  836. midiEvent.type = (note.velo > 0) ? SND_SEQ_EVENT_NOTEON : SND_SEQ_EVENT_NOTEOFF;
  837. midiEvent.data.note.channel = static_cast<uchar>(note.channel);
  838. midiEvent.data.note.note = note.note;
  839. midiEvent.data.note.velocity = note.velo;
  840. }
  841. pData->extNotes.mutex.unlock();
  842. } // End of MIDI Input (External)
  843. // ----------------------------------------------------------------------------------------------------
  844. // Event Input (System)
  845. bool allNotesOffSent = false;
  846. bool isSampleAccurate = (pData->options & PLUGIN_OPTION_FIXED_BUFFERS) == 0;
  847. uint32_t numEvents = pData->event.portIn->getEventCount();
  848. uint32_t startTime = 0;
  849. uint32_t timeOffset = 0;
  850. uint32_t nextBankId;
  851. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  852. nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
  853. else
  854. nextBankId = 0;
  855. for (uint32_t i=0; i < numEvents; ++i)
  856. {
  857. const EngineEvent& event(pData->event.portIn->getEvent(i));
  858. if (event.time >= frames)
  859. continue;
  860. CARLA_ASSERT_INT2(event.time >= timeOffset, event.time, timeOffset);
  861. if (isSampleAccurate && event.time > timeOffset)
  862. {
  863. if (processSingle(inBuffer, outBuffer, event.time - timeOffset, timeOffset, midiEventCount))
  864. {
  865. startTime = 0;
  866. timeOffset = event.time;
  867. midiEventCount = 0;
  868. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  869. nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
  870. else
  871. nextBankId = 0;
  872. }
  873. else
  874. startTime += timeOffset;
  875. }
  876. switch (event.type)
  877. {
  878. case kEngineEventTypeNull:
  879. break;
  880. case kEngineEventTypeControl: {
  881. const EngineControlEvent& ctrlEvent(event.ctrl);
  882. switch (ctrlEvent.type)
  883. {
  884. case kEngineControlEventTypeNull:
  885. break;
  886. case kEngineControlEventTypeParameter: {
  887. #ifndef BUILD_BRIDGE
  888. // Control backend stuff
  889. if (event.channel == pData->ctrlChannel)
  890. {
  891. float value;
  892. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_DRYWET) != 0)
  893. {
  894. value = ctrlEvent.value;
  895. setDryWet(value, false, false);
  896. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, value);
  897. break;
  898. }
  899. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_VOLUME) != 0)
  900. {
  901. value = ctrlEvent.value*127.0f/100.0f;
  902. setVolume(value, false, false);
  903. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
  904. break;
  905. }
  906. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_BALANCE) != 0)
  907. {
  908. float left, right;
  909. value = ctrlEvent.value/0.5f - 1.0f;
  910. if (value < 0.0f)
  911. {
  912. left = -1.0f;
  913. right = (value*2.0f)+1.0f;
  914. }
  915. else if (value > 0.0f)
  916. {
  917. left = (value*2.0f)-1.0f;
  918. right = 1.0f;
  919. }
  920. else
  921. {
  922. left = -1.0f;
  923. right = 1.0f;
  924. }
  925. setBalanceLeft(left, false, false);
  926. setBalanceRight(right, false, false);
  927. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  928. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  929. break;
  930. }
  931. }
  932. #endif
  933. // Control plugin parameters
  934. uint32_t k;
  935. for (k=0; k < pData->param.count; ++k)
  936. {
  937. if (pData->param.data[k].midiChannel != event.channel)
  938. continue;
  939. if (pData->param.data[k].midiCC != ctrlEvent.param)
  940. continue;
  941. if (pData->param.data[k].type != PARAMETER_INPUT)
  942. continue;
  943. if ((pData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  944. continue;
  945. float value;
  946. if (pData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  947. {
  948. value = (ctrlEvent.value < 0.5f) ? pData->param.ranges[k].min : pData->param.ranges[k].max;
  949. }
  950. else
  951. {
  952. value = pData->param.ranges[k].getUnnormalizedValue(ctrlEvent.value);
  953. if (pData->param.data[k].hints & PARAMETER_IS_INTEGER)
  954. value = std::rint(value);
  955. }
  956. setParameterValue(k, value, false, false, false);
  957. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, value);
  958. break;
  959. }
  960. // check if event is already handled
  961. if (k != pData->param.count)
  962. break;
  963. if ((pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0 && ctrlEvent.param <= 0x5F)
  964. {
  965. if (midiEventCount >= kPluginMaxMidiEvents)
  966. continue;
  967. snd_seq_event_t& midiEvent(fMidiEvents[midiEventCount++]);
  968. carla_zeroStruct<snd_seq_event_t>(midiEvent);
  969. midiEvent.time.tick = isSampleAccurate ? startTime : event.time;
  970. midiEvent.type = SND_SEQ_EVENT_CONTROLLER;
  971. midiEvent.data.control.channel = event.channel;
  972. midiEvent.data.control.param = ctrlEvent.param;
  973. midiEvent.data.control.value = int8_t(ctrlEvent.value*127.0f);
  974. }
  975. break;
  976. } // case kEngineControlEventTypeParameter
  977. case kEngineControlEventTypeMidiBank:
  978. if (event.channel == pData->ctrlChannel && (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  979. nextBankId = ctrlEvent.param;
  980. break;
  981. case kEngineControlEventTypeMidiProgram:
  982. if (event.channel == pData->ctrlChannel && (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  983. {
  984. const uint32_t nextProgramId = ctrlEvent.param;
  985. for (uint32_t k=0; k < pData->midiprog.count; ++k)
  986. {
  987. if (pData->midiprog.data[k].bank == nextBankId && pData->midiprog.data[k].program == nextProgramId)
  988. {
  989. const int32_t index(static_cast<int32_t>(k));
  990. setMidiProgram(index, false, false, false);
  991. pData->postponeRtEvent(kPluginPostRtEventMidiProgramChange, index, 0, 0.0f);
  992. break;
  993. }
  994. }
  995. }
  996. break;
  997. case kEngineControlEventTypeAllSoundOff:
  998. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  999. {
  1000. if (midiEventCount >= kPluginMaxMidiEvents)
  1001. continue;
  1002. snd_seq_event_t& midiEvent(fMidiEvents[midiEventCount++]);
  1003. carla_zeroStruct<snd_seq_event_t>(midiEvent);
  1004. midiEvent.time.tick = isSampleAccurate ? startTime : event.time;
  1005. midiEvent.type = SND_SEQ_EVENT_CONTROLLER;
  1006. midiEvent.data.control.channel = event.channel;
  1007. midiEvent.data.control.param = MIDI_CONTROL_ALL_SOUND_OFF;
  1008. }
  1009. break;
  1010. case kEngineControlEventTypeAllNotesOff:
  1011. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1012. {
  1013. if (event.channel == pData->ctrlChannel && ! allNotesOffSent)
  1014. {
  1015. allNotesOffSent = true;
  1016. sendMidiAllNotesOffToCallback();
  1017. }
  1018. if (midiEventCount >= kPluginMaxMidiEvents)
  1019. continue;
  1020. snd_seq_event_t& midiEvent(fMidiEvents[midiEventCount++]);
  1021. carla_zeroStruct<snd_seq_event_t>(midiEvent);
  1022. midiEvent.time.tick = isSampleAccurate ? startTime : event.time;
  1023. midiEvent.type = SND_SEQ_EVENT_CONTROLLER;
  1024. midiEvent.data.control.channel = event.channel;
  1025. midiEvent.data.control.param = MIDI_CONTROL_ALL_NOTES_OFF;
  1026. midiEventCount += 1;
  1027. }
  1028. break;
  1029. } // switch (ctrlEvent.type)
  1030. break;
  1031. } // case kEngineEventTypeControl
  1032. case kEngineEventTypeMidi: {
  1033. if (midiEventCount >= kPluginMaxMidiEvents)
  1034. continue;
  1035. const EngineMidiEvent& engineEvent(event.midi);
  1036. uint8_t status = uint8_t(MIDI_GET_STATUS_FROM_DATA(engineEvent.data));
  1037. uint8_t channel = event.channel;
  1038. // Fix bad note-off (per DSSI spec)
  1039. if (MIDI_IS_STATUS_NOTE_ON(status) && engineEvent.data[2] == 0)
  1040. status = MIDI_STATUS_NOTE_OFF;
  1041. snd_seq_event_t& midiEvent(fMidiEvents[midiEventCount]);
  1042. carla_zeroStruct<snd_seq_event_t>(midiEvent);
  1043. midiEvent.time.tick = isSampleAccurate ? startTime : event.time;
  1044. switch (status)
  1045. {
  1046. case MIDI_STATUS_NOTE_OFF: {
  1047. const uint8_t note = engineEvent.data[1];
  1048. midiEvent.type = SND_SEQ_EVENT_NOTEOFF;
  1049. midiEvent.data.note.channel = channel;
  1050. midiEvent.data.note.note = note;
  1051. pData->postponeRtEvent(kPluginPostRtEventNoteOff, channel, note, 0.0f);
  1052. break;
  1053. }
  1054. case MIDI_STATUS_NOTE_ON: {
  1055. const uint8_t note = engineEvent.data[1];
  1056. const uint8_t velo = engineEvent.data[2];
  1057. midiEvent.type = SND_SEQ_EVENT_NOTEON;
  1058. midiEvent.data.note.channel = channel;
  1059. midiEvent.data.note.note = note;
  1060. midiEvent.data.note.velocity = velo;
  1061. pData->postponeRtEvent(kPluginPostRtEventNoteOn, channel, note, velo);
  1062. break;
  1063. }
  1064. case MIDI_STATUS_POLYPHONIC_AFTERTOUCH:
  1065. if (pData->options & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH)
  1066. {
  1067. const uint8_t note = engineEvent.data[1];
  1068. const uint8_t pressure = engineEvent.data[2];
  1069. midiEvent.type = SND_SEQ_EVENT_KEYPRESS;
  1070. midiEvent.data.note.channel = channel;
  1071. midiEvent.data.note.note = note;
  1072. midiEvent.data.note.velocity = pressure;
  1073. }
  1074. break;
  1075. case MIDI_STATUS_CONTROL_CHANGE:
  1076. if (pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES)
  1077. {
  1078. const uint8_t control = engineEvent.data[1];
  1079. const uint8_t value = engineEvent.data[2];
  1080. midiEvent.type = SND_SEQ_EVENT_CONTROLLER;
  1081. midiEvent.data.control.channel = channel;
  1082. midiEvent.data.control.param = control;
  1083. midiEvent.data.control.value = value;
  1084. }
  1085. break;
  1086. case MIDI_STATUS_CHANNEL_PRESSURE:
  1087. if (pData->options & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE)
  1088. {
  1089. const uint8_t pressure = engineEvent.data[1];
  1090. midiEvent.type = SND_SEQ_EVENT_CHANPRESS;
  1091. midiEvent.data.control.channel = channel;
  1092. midiEvent.data.control.value = pressure;
  1093. }
  1094. break;
  1095. case MIDI_STATUS_PITCH_WHEEL_CONTROL:
  1096. if (pData->options & PLUGIN_OPTION_SEND_PITCHBEND)
  1097. {
  1098. const uint8_t lsb = engineEvent.data[1];
  1099. const uint8_t msb = engineEvent.data[2];
  1100. midiEvent.type = SND_SEQ_EVENT_PITCHBEND;
  1101. midiEvent.data.control.channel = channel;
  1102. midiEvent.data.control.value = ((msb << 7) | lsb) - 8192;
  1103. }
  1104. break;
  1105. default:
  1106. continue;
  1107. break;
  1108. } // switch (status)
  1109. midiEventCount += 1;
  1110. break;
  1111. } // case kEngineEventTypeMidi
  1112. } // switch (event.type)
  1113. }
  1114. pData->postRtEvents.trySplice();
  1115. if (frames > timeOffset)
  1116. processSingle(inBuffer, outBuffer, frames - timeOffset, timeOffset, midiEventCount);
  1117. } // End of Event Input and Processing
  1118. // --------------------------------------------------------------------------------------------------------
  1119. // Plugin processing (no events)
  1120. else
  1121. {
  1122. processSingle(inBuffer, outBuffer, frames, 0, midiEventCount);
  1123. } // End of Plugin processing (no events)
  1124. CARLA_PROCESS_CONTINUE_CHECK;
  1125. // --------------------------------------------------------------------------------------------------------
  1126. // Control Output
  1127. if (pData->event.portOut != nullptr)
  1128. {
  1129. uint8_t channel;
  1130. uint16_t param;
  1131. float value;
  1132. for (uint32_t k=0; k < pData->param.count; ++k)
  1133. {
  1134. if (pData->param.data[k].type != PARAMETER_OUTPUT)
  1135. continue;
  1136. pData->param.ranges[k].fixValue(fParamBuffers[k]);
  1137. if (pData->param.data[k].midiCC > 0)
  1138. {
  1139. channel = pData->param.data[k].midiChannel;
  1140. param = static_cast<uint16_t>(pData->param.data[k].midiCC);
  1141. value = pData->param.ranges[k].getNormalizedValue(fParamBuffers[k]);
  1142. pData->event.portOut->writeControlEvent(0, channel, kEngineControlEventTypeParameter, param, value);
  1143. }
  1144. }
  1145. } // End of Control Output
  1146. }
  1147. bool processSingle(float** const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t timeOffset, const unsigned long midiEventCount)
  1148. {
  1149. CARLA_SAFE_ASSERT_RETURN(frames > 0, false);
  1150. if (pData->audioIn.count > 0)
  1151. {
  1152. CARLA_SAFE_ASSERT_RETURN(inBuffer != nullptr, false);
  1153. }
  1154. if (pData->audioOut.count > 0)
  1155. {
  1156. CARLA_SAFE_ASSERT_RETURN(outBuffer != nullptr, false);
  1157. }
  1158. // --------------------------------------------------------------------------------------------------------
  1159. // Try lock, silence otherwise
  1160. if (pData->engine->isOffline())
  1161. {
  1162. pData->singleMutex.lock();
  1163. }
  1164. else if (! pData->singleMutex.tryLock())
  1165. {
  1166. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1167. {
  1168. for (uint32_t k=0; k < frames; ++k)
  1169. outBuffer[i][k+timeOffset] = 0.0f;
  1170. }
  1171. return false;
  1172. }
  1173. // --------------------------------------------------------------------------------------------------------
  1174. // Reset audio buffers
  1175. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1176. FLOAT_COPY(fAudioInBuffers[i], inBuffer[i]+timeOffset, frames);
  1177. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1178. FLOAT_CLEAR(fAudioOutBuffers[i], frames);
  1179. // --------------------------------------------------------------------------------------------------------
  1180. // Run plugin
  1181. if (fDssiDescriptor->run_synth != nullptr)
  1182. {
  1183. fDssiDescriptor->run_synth(fHandle, frames, fMidiEvents, midiEventCount);
  1184. if (fHandle2 != nullptr)
  1185. fDssiDescriptor->run_synth(fHandle2, frames, fMidiEvents, midiEventCount);
  1186. }
  1187. else if (fDssiDescriptor->run_multiple_synths != nullptr)
  1188. {
  1189. unsigned long instances = (fHandle2 != nullptr) ? 2 : 1;
  1190. LADSPA_Handle handlePtr[2] = { fHandle, fHandle2 };
  1191. snd_seq_event_t* midiEventsPtr[2] = { fMidiEvents, fMidiEvents };
  1192. unsigned long midiEventCountPtr[2] = { midiEventCount, midiEventCount };
  1193. fDssiDescriptor->run_multiple_synths(instances, handlePtr, frames, midiEventsPtr, midiEventCountPtr);
  1194. }
  1195. else
  1196. {
  1197. fDescriptor->run(fHandle, frames);
  1198. if (fHandle2 != nullptr)
  1199. fDescriptor->run(fHandle2, frames);
  1200. }
  1201. #ifndef BUILD_BRIDGE
  1202. // --------------------------------------------------------------------------------------------------------
  1203. // Post-processing (dry/wet, volume and balance)
  1204. {
  1205. const bool doDryWet = (pData->hints & PLUGIN_CAN_DRYWET) != 0 && pData->postProc.dryWet != 1.0f;
  1206. const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) != 0 && (pData->postProc.balanceLeft != -1.0f || pData->postProc.balanceRight != 1.0f);
  1207. bool isPair;
  1208. float bufValue, oldBufLeft[doBalance ? frames : 1];
  1209. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1210. {
  1211. // Dry/Wet
  1212. if (doDryWet)
  1213. {
  1214. for (uint32_t k=0; k < frames; ++k)
  1215. {
  1216. // TODO
  1217. //if (k < pData->latency && pData->latency < frames)
  1218. // bufValue = (pData->audioIn.count == 1) ? pData->latencyBuffers[0][k] : pData->latencyBuffers[i][k];
  1219. //else
  1220. // bufValue = (pData->audioIn.count == 1) ? inBuffer[0][k-m_latency] : inBuffer[i][k-m_latency];
  1221. bufValue = fAudioInBuffers[(pData->audioIn.count == 1) ? 0 : i][k];
  1222. fAudioOutBuffers[i][k] = (fAudioOutBuffers[i][k] * pData->postProc.dryWet) + (bufValue * (1.0f - pData->postProc.dryWet));
  1223. }
  1224. }
  1225. // Balance
  1226. if (doBalance)
  1227. {
  1228. isPair = (i % 2 == 0);
  1229. if (isPair)
  1230. {
  1231. CARLA_ASSERT(i+1 < pData->audioOut.count);
  1232. FLOAT_COPY(oldBufLeft, fAudioOutBuffers[i], frames);
  1233. }
  1234. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  1235. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  1236. for (uint32_t k=0; k < frames; ++k)
  1237. {
  1238. if (isPair)
  1239. {
  1240. // left
  1241. fAudioOutBuffers[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  1242. fAudioOutBuffers[i][k] += fAudioOutBuffers[i+1][k] * (1.0f - balRangeR);
  1243. }
  1244. else
  1245. {
  1246. // right
  1247. fAudioOutBuffers[i][k] = fAudioOutBuffers[i][k] * balRangeR;
  1248. fAudioOutBuffers[i][k] += oldBufLeft[k] * balRangeL;
  1249. }
  1250. }
  1251. }
  1252. // Volume (and buffer copy)
  1253. {
  1254. for (uint32_t k=0; k < frames; ++k)
  1255. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k] * pData->postProc.volume;
  1256. }
  1257. }
  1258. #if 0
  1259. // Latency, save values for next callback, TODO
  1260. if (pData->latency > 0 && pData->latency < frames)
  1261. {
  1262. for (i=0; i < pData->audioIn.count; ++i)
  1263. FLOAT_COPY(pData->latencyBuffers[i], inBuffer[i] + (frames - pData->latency), pData->latency);
  1264. }
  1265. #endif
  1266. } // End of Post-processing
  1267. #else // BUILD_BRIDGE
  1268. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1269. {
  1270. for (uint32_t k=0; k < frames; ++k)
  1271. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k];
  1272. }
  1273. #endif
  1274. // --------------------------------------------------------------------------------------------------------
  1275. pData->singleMutex.unlock();
  1276. return true;
  1277. }
  1278. void bufferSizeChanged(const uint32_t newBufferSize) override
  1279. {
  1280. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  1281. carla_debug("DssiPlugin::bufferSizeChanged(%i) - start", newBufferSize);
  1282. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1283. {
  1284. if (fAudioInBuffers[i] != nullptr)
  1285. delete[] fAudioInBuffers[i];
  1286. fAudioInBuffers[i] = new float[newBufferSize];
  1287. }
  1288. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1289. {
  1290. if (fAudioOutBuffers[i] != nullptr)
  1291. delete[] fAudioOutBuffers[i];
  1292. fAudioOutBuffers[i] = new float[newBufferSize];
  1293. }
  1294. if (fHandle2 == nullptr)
  1295. {
  1296. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1297. {
  1298. CARLA_ASSERT(fAudioInBuffers[i] != nullptr);
  1299. fDescriptor->connect_port(fHandle, pData->audioIn.ports[i].rindex, fAudioInBuffers[i]);
  1300. }
  1301. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1302. {
  1303. CARLA_ASSERT(fAudioOutBuffers[i] != nullptr);
  1304. fDescriptor->connect_port(fHandle, pData->audioOut.ports[i].rindex, fAudioOutBuffers[i]);
  1305. }
  1306. }
  1307. else
  1308. {
  1309. if (pData->audioIn.count > 0)
  1310. {
  1311. CARLA_ASSERT(pData->audioIn.count == 2);
  1312. CARLA_ASSERT(fAudioInBuffers[0] != nullptr);
  1313. CARLA_ASSERT(fAudioInBuffers[1] != nullptr);
  1314. fDescriptor->connect_port(fHandle, pData->audioIn.ports[0].rindex, fAudioInBuffers[0]);
  1315. fDescriptor->connect_port(fHandle2, pData->audioIn.ports[1].rindex, fAudioInBuffers[1]);
  1316. }
  1317. if (pData->audioOut.count > 0)
  1318. {
  1319. CARLA_ASSERT(pData->audioOut.count == 2);
  1320. CARLA_ASSERT(fAudioOutBuffers[0] != nullptr);
  1321. CARLA_ASSERT(fAudioOutBuffers[1] != nullptr);
  1322. fDescriptor->connect_port(fHandle, pData->audioOut.ports[0].rindex, fAudioOutBuffers[0]);
  1323. fDescriptor->connect_port(fHandle2, pData->audioOut.ports[1].rindex, fAudioOutBuffers[1]);
  1324. }
  1325. }
  1326. carla_debug("DssiPlugin::bufferSizeChanged(%i) - end", newBufferSize);
  1327. }
  1328. void sampleRateChanged(const double newSampleRate) override
  1329. {
  1330. CARLA_ASSERT_INT(newSampleRate > 0.0, newSampleRate);
  1331. carla_debug("DssiPlugin::sampleRateChanged(%g) - start", newSampleRate);
  1332. // TODO
  1333. (void)newSampleRate;
  1334. carla_debug("DssiPlugin::sampleRateChanged(%g) - end", newSampleRate);
  1335. }
  1336. // -------------------------------------------------------------------
  1337. // Plugin buffers
  1338. void clearBuffers() override
  1339. {
  1340. carla_debug("DssiPlugin::clearBuffers() - start");
  1341. if (fAudioInBuffers != nullptr)
  1342. {
  1343. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1344. {
  1345. if (fAudioInBuffers[i] != nullptr)
  1346. {
  1347. delete[] fAudioInBuffers[i];
  1348. fAudioInBuffers[i] = nullptr;
  1349. }
  1350. }
  1351. delete[] fAudioInBuffers;
  1352. fAudioInBuffers = nullptr;
  1353. }
  1354. if (fAudioOutBuffers != nullptr)
  1355. {
  1356. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1357. {
  1358. if (fAudioOutBuffers[i] != nullptr)
  1359. {
  1360. delete[] fAudioOutBuffers[i];
  1361. fAudioOutBuffers[i] = nullptr;
  1362. }
  1363. }
  1364. delete[] fAudioOutBuffers;
  1365. fAudioOutBuffers = nullptr;
  1366. }
  1367. if (fParamBuffers != nullptr)
  1368. {
  1369. delete[] fParamBuffers;
  1370. fParamBuffers = nullptr;
  1371. }
  1372. CarlaPlugin::clearBuffers();
  1373. carla_debug("DssiPlugin::clearBuffers() - end");
  1374. }
  1375. // -------------------------------------------------------------------
  1376. // Post-poned UI Stuff
  1377. void uiParameterChange(const uint32_t index, const float value) noexcept override
  1378. {
  1379. CARLA_SAFE_ASSERT_RETURN(index < pData->param.count,);
  1380. if (pData->osc.data.target == nullptr)
  1381. return;
  1382. osc_send_control(pData->osc.data, pData->param.data[index].rindex, value);
  1383. }
  1384. void uiMidiProgramChange(const uint32_t index) noexcept override
  1385. {
  1386. CARLA_SAFE_ASSERT_RETURN(index < pData->midiprog.count,);
  1387. if (pData->osc.data.target == nullptr)
  1388. return;
  1389. osc_send_program(pData->osc.data, pData->midiprog.data[index].bank, pData->midiprog.data[index].program);
  1390. }
  1391. void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) noexcept override
  1392. {
  1393. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1394. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1395. CARLA_SAFE_ASSERT_RETURN(velo > 0 && velo < MAX_MIDI_VALUE,);
  1396. if (pData->osc.data.target == nullptr)
  1397. return;
  1398. #if 0
  1399. uint8_t midiData[4];
  1400. midiData[0] = 0;
  1401. midiData[1] = static_cast<uint8_t>(MIDI_STATUS_NOTE_ON + channel);
  1402. midiData[2] = note;
  1403. midiData[3] = velo;
  1404. osc_send_midi(pData->osc.data, midiData);
  1405. #endif
  1406. }
  1407. void uiNoteOff(const uint8_t channel, const uint8_t note) noexcept override
  1408. {
  1409. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1410. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1411. if (pData->osc.data.target == nullptr)
  1412. return;
  1413. #if 0
  1414. uint8_t midiData[4];
  1415. midiData[0] = 0;
  1416. midiData[1] = static_cast<uint8_t>(MIDI_STATUS_NOTE_OFF + channel);
  1417. midiData[2] = note;
  1418. midiData[3] = 0;
  1419. osc_send_midi(pData->osc.data, midiData);
  1420. #endif
  1421. }
  1422. // -------------------------------------------------------------------
  1423. const void* getExtraStuff() const noexcept override
  1424. {
  1425. return fUiFilename;
  1426. }
  1427. bool init(const char* const filename, const char* const name, const char* const label)
  1428. {
  1429. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  1430. // ---------------------------------------------------------------
  1431. // first checks
  1432. if (pData->client != nullptr)
  1433. {
  1434. pData->engine->setLastError("Plugin client is already registered");
  1435. return false;
  1436. }
  1437. if (filename == nullptr || filename[0] == '\0')
  1438. {
  1439. pData->engine->setLastError("null filename");
  1440. return false;
  1441. }
  1442. if (label == nullptr || label[0] == '\0')
  1443. {
  1444. pData->engine->setLastError("null label");
  1445. return false;
  1446. }
  1447. // ---------------------------------------------------------------
  1448. // open DLL
  1449. if (! pData->libOpen(filename))
  1450. {
  1451. pData->engine->setLastError(pData->libError(filename));
  1452. return false;
  1453. }
  1454. // ---------------------------------------------------------------
  1455. // get DLL main entry
  1456. const DSSI_Descriptor_Function descFn = (DSSI_Descriptor_Function)pData->libSymbol("dssi_descriptor");
  1457. if (descFn == nullptr)
  1458. {
  1459. pData->engine->setLastError("Could not find the DSSI Descriptor in the plugin library");
  1460. return false;
  1461. }
  1462. // ---------------------------------------------------------------
  1463. // get descriptor that matches label
  1464. unsigned long i = 0;
  1465. while ((fDssiDescriptor = descFn(i++)) != nullptr)
  1466. {
  1467. fDescriptor = fDssiDescriptor->LADSPA_Plugin;
  1468. if (fDescriptor != nullptr && fDescriptor->Label != nullptr && std::strcmp(fDescriptor->Label, label) == 0)
  1469. break;
  1470. }
  1471. if (fDescriptor == nullptr || fDssiDescriptor == nullptr)
  1472. {
  1473. pData->engine->setLastError("Could not find the requested plugin label in the plugin library");
  1474. return false;
  1475. }
  1476. // ---------------------------------------------------------------
  1477. // check if uses global instance
  1478. if (fDssiDescriptor->run_synth == nullptr && fDssiDescriptor->run_multiple_synths != nullptr)
  1479. {
  1480. if (! addUniqueMultiSynth(fDescriptor->Label))
  1481. {
  1482. pData->engine->setLastError("This plugin uses a global instance and can't be used more than once safely");
  1483. return false;
  1484. }
  1485. }
  1486. // ---------------------------------------------------------------
  1487. // get info
  1488. if (name != nullptr && name[0] != '\0')
  1489. pData->name = pData->engine->getUniquePluginName(name);
  1490. else if (fDescriptor->Name != nullptr && fDescriptor->Name[0] != '\0')
  1491. pData->name = pData->engine->getUniquePluginName(fDescriptor->Name);
  1492. else
  1493. pData->name = pData->engine->getUniquePluginName(fDescriptor->Label);
  1494. pData->filename = carla_strdup(filename);
  1495. // ---------------------------------------------------------------
  1496. // register client
  1497. pData->client = pData->engine->addClient(this);
  1498. if (pData->client == nullptr || ! pData->client->isOk())
  1499. {
  1500. pData->engine->setLastError("Failed to register plugin client");
  1501. return false;
  1502. }
  1503. // ---------------------------------------------------------------
  1504. // initialize plugin
  1505. fHandle = fDescriptor->instantiate(fDescriptor, (unsigned long)pData->engine->getSampleRate());
  1506. if (fHandle == nullptr)
  1507. {
  1508. pData->engine->setLastError("Plugin failed to initialize");
  1509. return false;
  1510. }
  1511. // ---------------------------------------------------------------
  1512. // check for custom data extension
  1513. if (fDssiDescriptor->configure != nullptr)
  1514. {
  1515. if (char* const error = fDssiDescriptor->configure(fHandle, DSSI_CUSTOMDATA_EXTENSION_KEY, ""))
  1516. {
  1517. if (std::strcmp(error, "true") == 0 && fDssiDescriptor->get_custom_data != nullptr && fDssiDescriptor->set_custom_data != nullptr)
  1518. fUsesCustomData = true;
  1519. std::free(error);
  1520. }
  1521. }
  1522. // ---------------------------------------------------------------
  1523. // gui stuff
  1524. if (const char* const guiFilename = find_dssi_ui(filename, fDescriptor->Label))
  1525. {
  1526. pData->osc.thread.setOscData(guiFilename, fDescriptor->Label);
  1527. fUiFilename = guiFilename;
  1528. }
  1529. // ---------------------------------------------------------------
  1530. // load plugin settings
  1531. {
  1532. #ifdef __USE_GNU
  1533. const bool isAmSynth(strcasestr(pData->filename, "amsynth"));
  1534. const bool isDssiVst(strcasestr(pData->filename, "dssi-vst"));
  1535. #else
  1536. const bool isAmSynth(std::strstr(pData->filename, "amsynth"));
  1537. const bool isDssiVst(std::strstr(pData->filename, "dssi-vst"));
  1538. #endif
  1539. // set default options
  1540. pData->options = 0x0;
  1541. pData->options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  1542. if (isAmSynth || isDssiVst)
  1543. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  1544. if (pData->engine->getOptions().forceStereo)
  1545. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  1546. if (fUsesCustomData)
  1547. pData->options |= PLUGIN_OPTION_USE_CHUNKS;
  1548. if (fDssiDescriptor->run_synth != nullptr || fDssiDescriptor->run_multiple_synths != nullptr)
  1549. {
  1550. pData->options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  1551. pData->options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  1552. pData->options |= PLUGIN_OPTION_SEND_PITCHBEND;
  1553. pData->options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  1554. if (fDssiDescriptor->run_synth == nullptr)
  1555. carla_stderr2("WARNING: Plugin can ONLY use run_multiple_synths!");
  1556. }
  1557. // set identifier string
  1558. CarlaString identifier("DSSI/");
  1559. if (const char* const shortname = std::strrchr(filename, OS_SEP))
  1560. {
  1561. identifier += shortname+1;
  1562. identifier += ",";
  1563. }
  1564. identifier += label;
  1565. pData->identifier = identifier.dup();
  1566. // load settings
  1567. pData->options = pData->loadSettings(pData->options, getOptionsAvailable());
  1568. // ignore settings, we need this anyway
  1569. if (isAmSynth || isDssiVst)
  1570. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  1571. }
  1572. return true;
  1573. }
  1574. // -------------------------------------------------------------------
  1575. private:
  1576. LADSPA_Handle fHandle;
  1577. LADSPA_Handle fHandle2;
  1578. const LADSPA_Descriptor* fDescriptor;
  1579. const DSSI_Descriptor* fDssiDescriptor;
  1580. bool fUsesCustomData;
  1581. const char* fUiFilename;
  1582. float** fAudioInBuffers;
  1583. float** fAudioOutBuffers;
  1584. float* fParamBuffers;
  1585. snd_seq_event_t fMidiEvents[kPluginMaxMidiEvents];
  1586. // -------------------------------------------------------------------
  1587. static LinkedList<const char*> sMultiSynthList;
  1588. static bool addUniqueMultiSynth(const char* const label)
  1589. {
  1590. CARLA_SAFE_ASSERT_RETURN(label != nullptr && label[0] != '\0', false);
  1591. for (LinkedList<const char*>::Itenerator it = sMultiSynthList.begin(); it.valid(); it.next())
  1592. {
  1593. const char*& itLabel(it.getValue());
  1594. if (std::strcmp(label, itLabel) == 0)
  1595. return false;
  1596. }
  1597. sMultiSynthList.append(carla_strdup(label));
  1598. return true;
  1599. }
  1600. static void removeUniqueMultiSynth(const char* const label)
  1601. {
  1602. CARLA_SAFE_ASSERT_RETURN(label != nullptr && label[0] != '\0',);
  1603. for (LinkedList<const char*>::Itenerator it = sMultiSynthList.begin(); it.valid(); it.next())
  1604. {
  1605. const char* const itLabel(it.getValue());
  1606. if (std::strcmp(label, itLabel) == 0)
  1607. {
  1608. sMultiSynthList.remove(it);
  1609. delete[] itLabel;
  1610. break;
  1611. }
  1612. }
  1613. }
  1614. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(DssiPlugin)
  1615. };
  1616. LinkedList<const char*> DssiPlugin::sMultiSynthList;
  1617. CARLA_BACKEND_END_NAMESPACE
  1618. #endif // WANT_DSSI
  1619. // -------------------------------------------------------------------------------------------------------------------
  1620. CARLA_BACKEND_START_NAMESPACE
  1621. CarlaPlugin* CarlaPlugin::newDSSI(const Initializer& init)
  1622. {
  1623. carla_debug("CarlaPlugin::newDSSI({%p, \"%s\", \"%s\", \"%s\"})", init.engine, init.filename, init.name, init.label);
  1624. #ifdef WANT_DSSI
  1625. DssiPlugin* const plugin(new DssiPlugin(init.engine, init.id));
  1626. if (! plugin->init(init.filename, init.name, init.label))
  1627. {
  1628. delete plugin;
  1629. return nullptr;
  1630. }
  1631. plugin->reload();
  1632. if (init.engine->getProccessMode() == ENGINE_PROCESS_MODE_CONTINUOUS_RACK && ! plugin->canRunInRack())
  1633. {
  1634. init.engine->setLastError("Carla's rack mode can only work with Mono or Stereo DSSI plugins, sorry!");
  1635. delete plugin;
  1636. return nullptr;
  1637. }
  1638. return plugin;
  1639. #else
  1640. init.engine->setLastError("DSSI support not available");
  1641. return nullptr;
  1642. #endif
  1643. }
  1644. CARLA_BACKEND_END_NAMESPACE
  1645. // -------------------------------------------------------------------------------------------------------------------