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.

carla-vst.cpp 43KB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338
  1. /*
  2. * Carla Native Plugins
  3. * Copyright (C) 2013-2020 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. #ifdef __WINE__
  18. #error This file is not supposed to be built with wine!
  19. #endif
  20. #ifndef CARLA_PLUGIN_SYNTH
  21. #error CARLA_PLUGIN_SYNTH undefined
  22. #endif
  23. #ifndef CARLA_VST_SHELL
  24. #ifndef CARLA_PLUGIN_PATCHBAY
  25. #error CARLA_PLUGIN_PATCHBAY undefined
  26. #endif
  27. #if defined(CARLA_PLUGIN_64CH) || defined(CARLA_PLUGIN_32CH) || defined(CARLA_PLUGIN_16CH)
  28. #if ! CARLA_PLUGIN_SYNTH
  29. #error CARLA_PLUGIN_16/32/64CH requires CARLA_PLUGIN_SYNTH
  30. #endif
  31. #endif
  32. #endif
  33. #define CARLA_NATIVE_PLUGIN_VST
  34. #include "carla-base.cpp"
  35. #include "carla-vst.hpp"
  36. #include "water/files/File.h"
  37. #include "CarlaMathUtils.hpp"
  38. #include "CarlaVstUtils.hpp"
  39. static uint32_t d_lastBufferSize = 0;
  40. static double d_lastSampleRate = 0.0;
  41. static const int32_t kBaseUniqueID = CCONST('C', 'r', 'l', 'a');
  42. static const int32_t kVstMidiEventSize = static_cast<int32_t>(sizeof(VstMidiEvent));
  43. #ifdef CARLA_VST_SHELL
  44. # if CARLA_PLUGIN_SYNTH
  45. static const int32_t kShellUniqueID = CCONST('C', 'r', 'l', 's');
  46. # else
  47. static const int32_t kShellUniqueID = CCONST('C', 'r', 'l', 'F');
  48. # endif
  49. #else
  50. static const int32_t kNumParameters = 100;
  51. #endif
  52. static const bool kIsUsingUILauncher = isUsingUILauncher();
  53. // --------------------------------------------------------------------------------------------------------------------
  54. // Carla Internal Plugin API exposed as VST plugin
  55. class NativePlugin
  56. {
  57. public:
  58. static const uint32_t kMaxMidiEvents = 512;
  59. NativePlugin(AEffect* const effect, const NativePluginDescriptor* desc)
  60. : fEffect(effect),
  61. fHandle(nullptr),
  62. fHost(),
  63. fDescriptor(desc),
  64. fBufferSize(d_lastBufferSize),
  65. fSampleRate(d_lastSampleRate),
  66. fIsActive(false),
  67. fMidiEventCount(0),
  68. fTimeInfo(),
  69. fVstRect(),
  70. fUiLauncher(nullptr),
  71. fHostType(kHostTypeNull),
  72. fMidiOutEvents(),
  73. fStateChunk(nullptr)
  74. {
  75. fHost.handle = this;
  76. fHost.uiName = carla_strdup("CarlaVST");
  77. fHost.uiParentId = 0;
  78. std::memset(fProgramName, 0, sizeof(fProgramName));
  79. std::strcpy(fProgramName, "Default");
  80. // find resource dir
  81. using water::File;
  82. using water::String;
  83. File curExe = File::getSpecialLocation(File::currentExecutableFile).getLinkedTarget();
  84. File resDir = curExe.getSiblingFile("resources");
  85. #ifndef CARLA_OS_MAC
  86. // FIXME: proper fallback path for other OSes
  87. if (! resDir.exists())
  88. resDir = File("/usr/local/share/carla/resources");
  89. if (! resDir.exists())
  90. resDir = File("/usr/share/carla/resources");
  91. #endif
  92. // find host type
  93. const String hostFilename(File::getSpecialLocation(File::hostApplicationPath).getFileName());
  94. /**/ if (hostFilename.startsWith("ardour"))
  95. fHostType = kHostTypeArdour;
  96. else if (hostFilename.startsWith("Bitwig"))
  97. fHostType = kHostTypeBitwig;
  98. fHost.resourceDir = carla_strdup(resDir.getFullPathName().toRawUTF8());
  99. fHost.get_buffer_size = host_get_buffer_size;
  100. fHost.get_sample_rate = host_get_sample_rate;
  101. fHost.is_offline = host_is_offline;
  102. fHost.get_time_info = host_get_time_info;
  103. fHost.write_midi_event = host_write_midi_event;
  104. fHost.ui_parameter_changed = host_ui_parameter_changed;
  105. fHost.ui_custom_data_changed = host_ui_custom_data_changed;
  106. fHost.ui_closed = host_ui_closed;
  107. fHost.ui_open_file = host_ui_open_file;
  108. fHost.ui_save_file = host_ui_save_file;
  109. fHost.dispatcher = host_dispatcher;
  110. fVstRect.top = 0;
  111. fVstRect.left = 0;
  112. if (kIsUsingUILauncher || (fDescriptor->hints & NATIVE_PLUGIN_USES_UI_SIZE) == 0x0)
  113. {
  114. fVstRect.right = ui_launcher_res::carla_uiWidth;
  115. fVstRect.bottom = ui_launcher_res::carla_uiHeight;
  116. }
  117. else
  118. {
  119. fVstRect.right = static_cast<int16_t>(fDescriptor->ui_width);
  120. fVstRect.bottom = static_cast<int16_t>(fDescriptor->ui_height);
  121. }
  122. init();
  123. }
  124. ~NativePlugin()
  125. {
  126. if (fIsActive)
  127. {
  128. // host has not de-activated the plugin yet, nasty!
  129. fIsActive = false;
  130. if (fDescriptor->deactivate != nullptr)
  131. fDescriptor->deactivate(fHandle);
  132. }
  133. if (fDescriptor->cleanup != nullptr && fHandle != nullptr)
  134. fDescriptor->cleanup(fHandle);
  135. fHandle = nullptr;
  136. if (fStateChunk != nullptr)
  137. {
  138. std::free(fStateChunk);
  139. fStateChunk = nullptr;
  140. }
  141. if (fHost.uiName != nullptr)
  142. {
  143. delete[] fHost.uiName;
  144. fHost.uiName = nullptr;
  145. }
  146. if (fHost.resourceDir != nullptr)
  147. {
  148. delete[] fHost.resourceDir;
  149. fHost.resourceDir = nullptr;
  150. }
  151. }
  152. bool init()
  153. {
  154. if (fDescriptor->instantiate == nullptr || fDescriptor->process == nullptr)
  155. {
  156. carla_stderr("Plugin is missing something...");
  157. return false;
  158. }
  159. fHandle = fDescriptor->instantiate(&fHost);
  160. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr, false);
  161. carla_zeroStructs(fMidiEvents, kMaxMidiEvents);
  162. carla_zeroStruct(fTimeInfo);
  163. return true;
  164. }
  165. const NativePluginDescriptor* getDescriptor() const noexcept
  166. {
  167. return fDescriptor;
  168. }
  169. // -------------------------------------------------------------------
  170. intptr_t vst_dispatcher(const int32_t opcode,
  171. const int32_t index, const intptr_t value, void* const ptr, const float opt)
  172. {
  173. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr, 0);
  174. intptr_t ret = 0;
  175. switch (opcode)
  176. {
  177. case effGetProgram:
  178. return 0;
  179. case effSetProgramName:
  180. if (char* const programName = (char*)ptr)
  181. {
  182. std::strncpy(fProgramName, programName, 32);
  183. return 1;
  184. }
  185. break;
  186. case effGetProgramName:
  187. if (char* const programName = (char*)ptr)
  188. {
  189. std::strncpy(programName, fProgramName, 23);
  190. programName[23] = '\0';
  191. return 1;
  192. }
  193. break;
  194. case effGetProgramNameIndexed:
  195. if (char* const programName = (char*)ptr)
  196. {
  197. std::strncpy(programName, fProgramName, 23);
  198. programName[23] = '\0';
  199. return 1;
  200. }
  201. break;
  202. case effGetParamDisplay:
  203. CARLA_SAFE_ASSERT_RETURN(index >= 0, 0);
  204. #ifndef CARLA_VST_SHELL
  205. CARLA_SAFE_ASSERT_RETURN(index < kNumParameters, 0);
  206. #endif
  207. if (char* const cptr = (char*)ptr)
  208. {
  209. const uint32_t uindex = static_cast<uint32_t>(index);
  210. CARLA_SAFE_ASSERT_RETURN(uindex < fDescriptor->paramIns, 0);
  211. const NativeParameter* const param = fDescriptor->get_parameter_info(fHandle, uindex);
  212. CARLA_SAFE_ASSERT_RETURN(param != nullptr, 0);
  213. float paramValue = fDescriptor->get_parameter_value(fHandle, uindex);
  214. if (param->hints & NATIVE_PARAMETER_IS_BOOLEAN)
  215. {
  216. const NativeParameterRanges& ranges(param->ranges);
  217. const float midRange = ranges.min + (ranges.max - ranges.min) / 2.0f;
  218. paramValue = paramValue > midRange ? ranges.max : ranges.min;
  219. }
  220. else if (param->hints & NATIVE_PARAMETER_IS_INTEGER)
  221. {
  222. paramValue = std::round(paramValue);
  223. }
  224. for (uint32_t i = 0; i < param->scalePointCount; ++i)
  225. {
  226. const NativeParameterScalePoint& scalePoint(param->scalePoints[uindex]);
  227. if (carla_isNotEqual(paramValue, scalePoint.value))
  228. continue;
  229. std::strncpy(cptr, scalePoint.label, 23);
  230. cptr[23] = '\0';
  231. return 1;
  232. }
  233. if (param->hints & NATIVE_PARAMETER_IS_INTEGER)
  234. {
  235. std::snprintf(cptr, 23, "%d%s%s",
  236. static_cast<int>(paramValue),
  237. param->unit != nullptr && param->unit[0] != '\0' ? " " : "",
  238. param->unit != nullptr && param->unit[0] != '\0' ? param->unit : "");
  239. cptr[23] = '\0';
  240. }
  241. else
  242. {
  243. std::snprintf(cptr, 23, "%.12g%s%s",
  244. static_cast<double>(paramValue),
  245. param->unit != nullptr && param->unit[0] != '\0' ? " " : "",
  246. param->unit != nullptr && param->unit[0] != '\0' ? param->unit : "");
  247. cptr[23] = '\0';
  248. }
  249. return 1;
  250. }
  251. break;
  252. case effGetParamName:
  253. CARLA_SAFE_ASSERT_RETURN(index >= 0, 0);
  254. #ifndef CARLA_VST_SHELL
  255. CARLA_SAFE_ASSERT_RETURN(index < kNumParameters, 0);
  256. #endif
  257. if (char* const cptr = (char*)ptr)
  258. {
  259. const uint32_t uindex = static_cast<uint32_t>(index);
  260. CARLA_SAFE_ASSERT_RETURN(uindex < fDescriptor->paramIns, 0);
  261. const NativeParameter* const param = fDescriptor->get_parameter_info(fHandle, uindex);
  262. CARLA_SAFE_ASSERT_RETURN(param != nullptr, 0);
  263. std::strncpy(cptr, param->name, 15);
  264. cptr[15] = '\0';
  265. return 1;
  266. }
  267. return 0;
  268. case effSetSampleRate:
  269. CARLA_SAFE_ASSERT_RETURN(opt > 0.0f, 0);
  270. if (carla_isEqual(fSampleRate, static_cast<double>(opt)))
  271. return 0;
  272. fSampleRate = opt;
  273. if (fDescriptor->dispatcher != nullptr)
  274. fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_SAMPLE_RATE_CHANGED, 0, 0, nullptr, opt);
  275. break;
  276. case effSetBlockSize:
  277. CARLA_SAFE_ASSERT_RETURN(value > 0, 0);
  278. if (fBufferSize == static_cast<uint32_t>(value))
  279. return 0;
  280. fBufferSize = static_cast<uint32_t>(value);
  281. if (fDescriptor->dispatcher != nullptr)
  282. fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_BUFFER_SIZE_CHANGED, 0, value, nullptr, 0.0f);
  283. break;
  284. case effMainsChanged:
  285. if (value != 0)
  286. {
  287. fMidiEventCount = 0;
  288. carla_zeroStruct(fTimeInfo);
  289. // tell host we want MIDI events
  290. if (fDescriptor->midiIns > 0)
  291. hostCallback(audioMasterWantMidi);
  292. // deactivate for possible changes
  293. if (fDescriptor->deactivate != nullptr && fIsActive)
  294. fDescriptor->deactivate(fHandle);
  295. // check if something changed
  296. const uint32_t bufferSize = static_cast<uint32_t>(hostCallback(audioMasterGetBlockSize));
  297. const double sampleRate = static_cast<double>(hostCallback(audioMasterGetSampleRate));
  298. if (bufferSize != 0 && fBufferSize != bufferSize && (fHostType != kHostTypeArdour || fBufferSize == 0))
  299. {
  300. fBufferSize = bufferSize;
  301. if (fDescriptor->dispatcher != nullptr)
  302. fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_BUFFER_SIZE_CHANGED, 0, bufferSize, nullptr, 0.0f);
  303. }
  304. if (carla_isNotZero(sampleRate) && carla_isNotEqual(fSampleRate, sampleRate))
  305. {
  306. fSampleRate = sampleRate;
  307. if (fDescriptor->dispatcher != nullptr)
  308. fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_SAMPLE_RATE_CHANGED, 0, 0, nullptr, (float)sampleRate);
  309. }
  310. if (fDescriptor->activate != nullptr)
  311. fDescriptor->activate(fHandle);
  312. fIsActive = true;
  313. }
  314. else
  315. {
  316. CARLA_SAFE_ASSERT_BREAK(fIsActive);
  317. if (fDescriptor->deactivate != nullptr)
  318. fDescriptor->deactivate(fHandle);
  319. fIsActive = false;
  320. }
  321. break;
  322. case effEditGetRect:
  323. *(ERect**)ptr = &fVstRect;
  324. ret = 1;
  325. break;
  326. case effEditOpen:
  327. if (fDescriptor->ui_show != nullptr)
  328. {
  329. if (kIsUsingUILauncher)
  330. {
  331. destoryUILauncher(fUiLauncher);
  332. fUiLauncher = createUILauncher((intptr_t)ptr, fDescriptor, fHandle);
  333. }
  334. else
  335. {
  336. char strBuf[0xff+1];
  337. std::snprintf(strBuf, 0xff, P_INTPTR, (intptr_t)ptr);
  338. strBuf[0xff] = '\0';
  339. // set CARLA_PLUGIN_EMBED_WINID for external process
  340. carla_setenv("CARLA_PLUGIN_EMBED_WINID", strBuf);
  341. // show UI now
  342. fDescriptor->ui_show(fHandle, true);
  343. // reset CARLA_PLUGIN_EMBED_WINID just in case
  344. carla_setenv("CARLA_PLUGIN_EMBED_WINID", "0");
  345. }
  346. ret = 1;
  347. }
  348. break;
  349. case effEditClose:
  350. if (fDescriptor->ui_show != nullptr)
  351. {
  352. if (kIsUsingUILauncher)
  353. {
  354. destoryUILauncher(fUiLauncher);
  355. fUiLauncher = nullptr;
  356. }
  357. else
  358. {
  359. fDescriptor->ui_show(fHandle, false);
  360. }
  361. ret = 1;
  362. }
  363. break;
  364. case effEditIdle:
  365. if (fUiLauncher != nullptr)
  366. idleUILauncher(fUiLauncher);
  367. if (fDescriptor->ui_idle != nullptr)
  368. fDescriptor->ui_idle(fHandle);
  369. break;
  370. case effGetChunk:
  371. if (ptr == nullptr || fDescriptor->get_state == nullptr)
  372. return 0;
  373. if (fStateChunk != nullptr)
  374. std::free(fStateChunk);
  375. fStateChunk = fDescriptor->get_state(fHandle);
  376. if (fStateChunk == nullptr)
  377. return 0;
  378. ret = static_cast<intptr_t>(std::strlen(fStateChunk)+1);
  379. *(void**)ptr = fStateChunk;
  380. break;
  381. case effSetChunk:
  382. if (value <= 0 || fDescriptor->set_state == nullptr)
  383. return 0;
  384. if (value == 1)
  385. return 1;
  386. if (const char* const state = (const char*)ptr)
  387. {
  388. fDescriptor->set_state(fHandle, state);
  389. ret = 1;
  390. }
  391. break;
  392. case effProcessEvents:
  393. if (! fIsActive)
  394. {
  395. // host has not activated the plugin yet, nasty!
  396. vst_dispatcher(effMainsChanged, 0, 1, nullptr, 0.0f);
  397. }
  398. if (const VstEvents* const events = (const VstEvents*)ptr)
  399. {
  400. if (events->numEvents == 0)
  401. break;
  402. for (int i=0, count=events->numEvents; i < count; ++i)
  403. {
  404. const VstMidiEvent* const vstMidiEvent((const VstMidiEvent*)events->events[i]);
  405. if (vstMidiEvent == nullptr)
  406. break;
  407. if (vstMidiEvent->type != kVstMidiType || vstMidiEvent->deltaFrames < 0)
  408. continue;
  409. if (fMidiEventCount >= kMaxMidiEvents)
  410. break;
  411. const uint32_t j(fMidiEventCount++);
  412. fMidiEvents[j].port = 0;
  413. fMidiEvents[j].time = static_cast<uint32_t>(vstMidiEvent->deltaFrames);
  414. fMidiEvents[j].size = 3;
  415. for (uint32_t k=0; k<3; ++k)
  416. fMidiEvents[j].data[k] = static_cast<uint8_t>(vstMidiEvent->midiData[k]);
  417. }
  418. }
  419. break;
  420. case effCanBeAutomated:
  421. ret = 1;
  422. break;
  423. case effCanDo:
  424. if (const char* const canDo = (const char*)ptr)
  425. {
  426. if (std::strcmp(canDo, "receiveVstEvents") == 0 || std::strcmp(canDo, "receiveVstMidiEvent") == 0)
  427. {
  428. if (fDescriptor->midiIns == 0)
  429. return -1;
  430. return 1;
  431. }
  432. if (std::strcmp(canDo, "sendVstEvents") == 0 || std::strcmp(canDo, "sendVstMidiEvent") == 0)
  433. {
  434. if (fDescriptor->midiOuts == 0)
  435. return -1;
  436. return 1;
  437. }
  438. if (std::strcmp(canDo, "receiveVstTimeInfo") == 0)
  439. return 1;
  440. }
  441. break;
  442. }
  443. return ret;
  444. }
  445. float vst_getParameter(const int32_t index)
  446. {
  447. CARLA_SAFE_ASSERT_RETURN(index >= 0, 0.0f);
  448. const uint32_t uindex = static_cast<uint32_t>(index);
  449. CARLA_SAFE_ASSERT_RETURN(uindex < fDescriptor->paramIns, 0.0f);
  450. const NativeParameter* const param = fDescriptor->get_parameter_info(fHandle, uindex);
  451. CARLA_SAFE_ASSERT_RETURN(param != nullptr, 0);
  452. const float realValue = fDescriptor->get_parameter_value(fHandle, uindex);
  453. return (realValue - param->ranges.min) / (param->ranges.max - param->ranges.min);
  454. }
  455. void vst_setParameter(const int32_t index, const float value)
  456. {
  457. CARLA_SAFE_ASSERT_RETURN(index >= 0,);
  458. const uint32_t uindex = static_cast<uint32_t>(index);
  459. CARLA_SAFE_ASSERT_RETURN(uindex < fDescriptor->paramIns,);
  460. const NativeParameter* const param = fDescriptor->get_parameter_info(fHandle, uindex);
  461. CARLA_SAFE_ASSERT_RETURN(param != nullptr,);
  462. float realValue;
  463. if (param->hints & NATIVE_PARAMETER_IS_BOOLEAN)
  464. {
  465. realValue = value > 0.5f ? param->ranges.max : param->ranges.min;
  466. }
  467. else
  468. {
  469. realValue = param->ranges.min + ((param->ranges.max - param->ranges.min) * value);
  470. if (param->hints & NATIVE_PARAMETER_IS_INTEGER)
  471. realValue = std::round(realValue);
  472. }
  473. fDescriptor->set_parameter_value(fHandle, uindex, realValue);
  474. }
  475. // FIXME for v3.0, use const for the input buffer
  476. void vst_processReplacing(float** const inputs, float** const outputs, const int32_t sampleFrames)
  477. {
  478. if (sampleFrames <= 0)
  479. return;
  480. if (fHostType == kHostTypeBitwig && static_cast<int32_t>(fBufferSize) != sampleFrames)
  481. {
  482. // deactivate first if needed
  483. if (fIsActive && fDescriptor->deactivate != nullptr)
  484. fDescriptor->deactivate(fHandle);
  485. fBufferSize = static_cast<uint32_t>(sampleFrames);
  486. if (fDescriptor->dispatcher != nullptr)
  487. fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_BUFFER_SIZE_CHANGED, 0, sampleFrames, nullptr, 0.0f);
  488. // activate again
  489. if (fDescriptor->activate != nullptr)
  490. fDescriptor->activate(fHandle);
  491. fIsActive = true;
  492. }
  493. if (! fIsActive)
  494. {
  495. // host has not activated the plugin yet, nasty!
  496. vst_dispatcher(effMainsChanged, 0, 1, nullptr, 0.0f);
  497. }
  498. static const int kWantVstTimeFlags = kVstTransportPlaying|kVstPpqPosValid|kVstTempoValid|kVstTimeSigValid;
  499. if (const VstTimeInfo* const vstTimeInfo = (const VstTimeInfo*)hostCallback(audioMasterGetTime, 0, kWantVstTimeFlags))
  500. {
  501. fTimeInfo.frame = static_cast<uint64_t>(std::max(0.0, vstTimeInfo->samplePos));
  502. fTimeInfo.playing = (vstTimeInfo->flags & kVstTransportPlaying);
  503. fTimeInfo.bbt.valid = ((vstTimeInfo->flags & kVstTempoValid) != 0 || (vstTimeInfo->flags & kVstTimeSigValid) != 0);
  504. // ticksPerBeat is not possible with VST
  505. fTimeInfo.bbt.ticksPerBeat = 960.0;
  506. if (vstTimeInfo->flags & kVstTempoValid)
  507. fTimeInfo.bbt.beatsPerMinute = vstTimeInfo->tempo;
  508. else
  509. fTimeInfo.bbt.beatsPerMinute = 120.0;
  510. if ((vstTimeInfo->flags & kVstPpqPosValid) != 0 && (vstTimeInfo->flags & kVstTimeSigValid) != 0)
  511. {
  512. const double ppqPos = std::abs(vstTimeInfo->ppqPos);
  513. const int ppqPerBar = vstTimeInfo->timeSigNumerator * 4 / vstTimeInfo->timeSigDenominator;
  514. const double barBeats = (std::fmod(ppqPos, ppqPerBar) / ppqPerBar) * vstTimeInfo->timeSigNumerator;
  515. const double rest = std::fmod(barBeats, 1.0);
  516. fTimeInfo.bbt.bar = static_cast<int32_t>(ppqPos) / ppqPerBar + 1;
  517. fTimeInfo.bbt.beat = static_cast<int32_t>(barBeats - rest + 0.5) + 1;
  518. fTimeInfo.bbt.tick = rest * fTimeInfo.bbt.ticksPerBeat;
  519. fTimeInfo.bbt.beatsPerBar = static_cast<float>(vstTimeInfo->timeSigNumerator);
  520. fTimeInfo.bbt.beatType = static_cast<float>(vstTimeInfo->timeSigDenominator);
  521. if (vstTimeInfo->ppqPos < 0.0)
  522. {
  523. fTimeInfo.bbt.bar = std::max(1, fTimeInfo.bbt.bar-1);
  524. fTimeInfo.bbt.beat = std::max(1, vstTimeInfo->timeSigNumerator - fTimeInfo.bbt.beat + 1);
  525. fTimeInfo.bbt.tick = std::max(0.0, fTimeInfo.bbt.ticksPerBeat - fTimeInfo.bbt.tick - 1);
  526. }
  527. }
  528. else
  529. {
  530. fTimeInfo.bbt.bar = 1;
  531. fTimeInfo.bbt.beat = 1;
  532. fTimeInfo.bbt.tick = 0.0;
  533. fTimeInfo.bbt.beatsPerBar = 4.0f;
  534. fTimeInfo.bbt.beatType = 4.0f;
  535. }
  536. #if 0
  537. // for testing bad host values
  538. static double last = -99999.0;
  539. if (carla_isNotEqual(vstTimeInfo->samplePos, last))
  540. {
  541. last = vstTimeInfo->samplePos;
  542. carla_stdout("time info %f %f %lu | %i %i",
  543. vstTimeInfo->samplePos, vstTimeInfo->ppqPos, fTimeInfo.frame,
  544. vstTimeInfo->timeSigNumerator, vstTimeInfo->timeSigDenominator);
  545. }
  546. #endif
  547. fTimeInfo.bbt.barStartTick = fTimeInfo.bbt.ticksPerBeat *
  548. static_cast<double>(fTimeInfo.bbt.beatsPerBar) *
  549. (fTimeInfo.bbt.bar - 1);
  550. }
  551. fMidiOutEvents.numEvents = 0;
  552. if (fHandle != nullptr)
  553. fDescriptor->process(fHandle,
  554. inputs, outputs, static_cast<uint32_t>(sampleFrames),
  555. fMidiEvents, fMidiEventCount);
  556. fMidiEventCount = 0;
  557. if (fMidiOutEvents.numEvents > 0)
  558. hostCallback(audioMasterProcessEvents, 0, 0, &fMidiOutEvents, 0.0f);
  559. }
  560. protected:
  561. // -------------------------------------------------------------------
  562. bool handleWriteMidiEvent(const NativeMidiEvent* const event)
  563. {
  564. CARLA_SAFE_ASSERT_RETURN(fDescriptor->midiOuts > 0, false);
  565. CARLA_SAFE_ASSERT_RETURN(event != nullptr, false);
  566. CARLA_SAFE_ASSERT_RETURN(event->data[0] != 0, false);
  567. if (fMidiOutEvents.numEvents >= static_cast<int32_t>(kMaxMidiEvents))
  568. {
  569. // send current events
  570. hostCallback(audioMasterProcessEvents, 0, 0, &fMidiOutEvents, 0.0f);
  571. // clear
  572. fMidiOutEvents.numEvents = 0;
  573. }
  574. VstMidiEvent& vstMidiEvent(fMidiOutEvents.mdata[fMidiOutEvents.numEvents++]);
  575. vstMidiEvent.type = kVstMidiType;
  576. vstMidiEvent.byteSize = kVstMidiEventSize;
  577. uint8_t i=0;
  578. for (; i<event->size; ++i)
  579. vstMidiEvent.midiData[i] = static_cast<char>(event->data[i]);
  580. for (; i<4; ++i)
  581. vstMidiEvent.midiData[i] = 0;
  582. return true;
  583. }
  584. void handleUiParameterChanged(const uint32_t index, const float value) const
  585. {
  586. const NativeParameter* const param = fDescriptor->get_parameter_info(fHandle, index);
  587. CARLA_SAFE_ASSERT_RETURN(param != nullptr,);
  588. const float normalizedValue = (value - param->ranges.min) / (param->ranges.max - param->ranges.min);
  589. hostCallback(audioMasterAutomate, static_cast<int32_t>(index), 0, nullptr, normalizedValue);
  590. }
  591. void handleUiParameterTouch(const uint32_t index, const bool touch) const
  592. {
  593. hostCallback(touch ? audioMasterBeginEdit : audioMasterEndEdit, static_cast<int32_t>(index));
  594. }
  595. void handleUiResize(const int16_t width, const int16_t height)
  596. {
  597. if (kIsUsingUILauncher)
  598. return;
  599. fVstRect.right = width;
  600. fVstRect.bottom = height;
  601. hostCallback(audioMasterSizeWindow, width, height);
  602. }
  603. void handleUiCustomDataChanged(const char* const /*key*/, const char* const /*value*/) const
  604. {
  605. }
  606. void handleUiClosed()
  607. {
  608. }
  609. const char* handleUiOpenFile(const bool /*isDir*/, const char* const /*title*/, const char* const /*filter*/) const
  610. {
  611. // TODO
  612. return nullptr;
  613. }
  614. const char* handleUiSaveFile(const bool /*isDir*/, const char* const /*title*/, const char* const /*filter*/) const
  615. {
  616. // TODO
  617. return nullptr;
  618. }
  619. intptr_t handleDispatcher(const NativeHostDispatcherOpcode opcode, const int32_t index, const intptr_t value, void* const ptr, const float opt)
  620. {
  621. carla_debug("NativePlugin::handleDispatcher(%i, %i, " P_INTPTR ", %p, %f)",
  622. opcode, index, value, ptr, static_cast<double>(opt));
  623. switch (opcode)
  624. {
  625. case NATIVE_HOST_OPCODE_NULL:
  626. case NATIVE_HOST_OPCODE_UPDATE_PARAMETER:
  627. case NATIVE_HOST_OPCODE_UPDATE_MIDI_PROGRAM:
  628. case NATIVE_HOST_OPCODE_RELOAD_PARAMETERS:
  629. case NATIVE_HOST_OPCODE_RELOAD_MIDI_PROGRAMS:
  630. case NATIVE_HOST_OPCODE_UI_UNAVAILABLE:
  631. case NATIVE_HOST_OPCODE_INTERNAL_PLUGIN:
  632. case NATIVE_HOST_OPCODE_QUEUE_INLINE_DISPLAY:
  633. case NATIVE_HOST_OPCODE_REQUEST_IDLE:
  634. case NATIVE_HOST_OPCODE_GET_FILE_PATH:
  635. // nothing
  636. break;
  637. case NATIVE_HOST_OPCODE_RELOAD_ALL:
  638. hostCallback(audioMasterUpdateDisplay);
  639. break;
  640. case NATIVE_HOST_OPCODE_HOST_IDLE:
  641. hostCallback(audioMasterIdle);
  642. break;
  643. case NATIVE_HOST_OPCODE_UI_TOUCH_PARAMETER:
  644. CARLA_SAFE_ASSERT_RETURN(index >= 0, 0);
  645. handleUiParameterTouch(static_cast<uint32_t>(index), value != 0);
  646. break;
  647. case NATIVE_HOST_OPCODE_UI_RESIZE:
  648. CARLA_SAFE_ASSERT_RETURN(index > 0 && index < INT16_MAX, 0);
  649. CARLA_SAFE_ASSERT_RETURN(value > 0 && value < INT16_MAX, 0);
  650. handleUiResize(static_cast<int16_t>(index), static_cast<int16_t>(value));
  651. break;
  652. }
  653. // unused for now
  654. return 0;
  655. (void)ptr; (void)opt;
  656. }
  657. private:
  658. // VST stuff
  659. AEffect* const fEffect;
  660. // Native data
  661. NativePluginHandle fHandle;
  662. NativeHostDescriptor fHost;
  663. const NativePluginDescriptor* const fDescriptor;
  664. // VST host data
  665. uint32_t fBufferSize;
  666. double fSampleRate;
  667. // Temporary data
  668. bool fIsActive;
  669. uint32_t fMidiEventCount;
  670. NativeMidiEvent fMidiEvents[kMaxMidiEvents];
  671. char fProgramName[32+1];
  672. NativeTimeInfo fTimeInfo;
  673. ERect fVstRect;
  674. // UI button
  675. CarlaUILauncher* fUiLauncher;
  676. // Host data
  677. enum HostType {
  678. kHostTypeNull = 0,
  679. kHostTypeArdour,
  680. kHostTypeBitwig
  681. };
  682. HostType fHostType;
  683. // host callback
  684. intptr_t hostCallback(const int32_t opcode,
  685. const int32_t index = 0,
  686. const intptr_t value = 0,
  687. void* const ptr = nullptr,
  688. const float opt = 0.0f) const
  689. {
  690. return VSTAudioMaster(fEffect, opcode, index, value, ptr, opt);
  691. }
  692. struct FixedVstEvents {
  693. int32_t numEvents;
  694. intptr_t reserved;
  695. VstEvent* data[kMaxMidiEvents];
  696. VstMidiEvent mdata[kMaxMidiEvents];
  697. FixedVstEvents()
  698. : numEvents(0),
  699. reserved(0)
  700. {
  701. for (uint32_t i=0; i<kMaxMidiEvents; ++i)
  702. data[i] = (VstEvent*)&mdata[i];
  703. carla_zeroStructs(mdata, kMaxMidiEvents);
  704. }
  705. CARLA_DECLARE_NON_COPY_STRUCT(FixedVstEvents);
  706. } fMidiOutEvents;
  707. char* fStateChunk;
  708. // -------------------------------------------------------------------
  709. #define handlePtr ((NativePlugin*)handle)
  710. static uint32_t host_get_buffer_size(NativeHostHandle handle)
  711. {
  712. return handlePtr->fBufferSize;
  713. }
  714. static double host_get_sample_rate(NativeHostHandle handle)
  715. {
  716. return handlePtr->fSampleRate;
  717. }
  718. static bool host_is_offline(NativeHostHandle /*handle*/)
  719. {
  720. // TODO
  721. return false;
  722. }
  723. static const NativeTimeInfo* host_get_time_info(NativeHostHandle handle)
  724. {
  725. return &(handlePtr->fTimeInfo);
  726. }
  727. static bool host_write_midi_event(NativeHostHandle handle, const NativeMidiEvent* event)
  728. {
  729. return handlePtr->handleWriteMidiEvent(event);
  730. }
  731. static void host_ui_parameter_changed(NativeHostHandle handle, uint32_t index, float value)
  732. {
  733. handlePtr->handleUiParameterChanged(index, value);
  734. }
  735. static void host_ui_custom_data_changed(NativeHostHandle handle, const char* key, const char* value)
  736. {
  737. handlePtr->handleUiCustomDataChanged(key, value);
  738. }
  739. static void host_ui_closed(NativeHostHandle handle)
  740. {
  741. handlePtr->handleUiClosed();
  742. }
  743. static const char* host_ui_open_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
  744. {
  745. return handlePtr->handleUiOpenFile(isDir, title, filter);
  746. }
  747. static const char* host_ui_save_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
  748. {
  749. return handlePtr->handleUiSaveFile(isDir, title, filter);
  750. }
  751. static intptr_t host_dispatcher(NativeHostHandle handle, NativeHostDispatcherOpcode opcode, int32_t index, intptr_t value, void* ptr, float opt)
  752. {
  753. return handlePtr->handleDispatcher(opcode, index, value, ptr, opt);
  754. }
  755. #undef handlePtr
  756. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(NativePlugin)
  757. };
  758. // -----------------------------------------------------------------------
  759. #define validObject effect != nullptr && effect->object != nullptr
  760. #define validPlugin effect != nullptr && effect->object != nullptr && ((VstObject*)effect->object)->plugin != nullptr
  761. #define vstObjectPtr (VstObject*)effect->object
  762. #define pluginPtr (vstObjectPtr)->plugin
  763. intptr_t vst_dispatcherCallback(AEffect* effect, int32_t opcode, int32_t index, intptr_t value, void* ptr, float opt)
  764. {
  765. // handle base opcodes
  766. switch (opcode)
  767. {
  768. case effOpen:
  769. if (VstObject* const obj = vstObjectPtr)
  770. {
  771. // this must always be valid
  772. CARLA_SAFE_ASSERT_RETURN(obj->audioMaster != nullptr, 0);
  773. // some hosts call effOpen twice
  774. CARLA_SAFE_ASSERT_RETURN(obj->plugin == nullptr, 1);
  775. d_lastBufferSize = static_cast<uint32_t>(VSTAudioMaster(effect, audioMasterGetBlockSize, 0, 0, nullptr, 0.0f));
  776. d_lastSampleRate = static_cast<double>(VSTAudioMaster(effect, audioMasterGetSampleRate, 0, 0, nullptr, 0.0f));
  777. // some hosts are not ready at this point or return 0 buffersize/samplerate
  778. if (d_lastBufferSize == 0)
  779. d_lastBufferSize = 2048;
  780. if (d_lastSampleRate <= 0.0)
  781. d_lastSampleRate = 44100.0;
  782. const NativePluginDescriptor* pluginDesc = nullptr;
  783. PluginListManager& plm(PluginListManager::getInstance());
  784. #ifdef CARLA_VST_SHELL
  785. if (effect->uniqueID == 0)
  786. effect->uniqueID = kShellUniqueID;
  787. if (effect->uniqueID == kShellUniqueID)
  788. {
  789. // first open for discovery, nothing to do
  790. effect->numParams = 0;
  791. effect->numPrograms = 0;
  792. effect->numInputs = 0;
  793. effect->numOutputs = 0;
  794. return 1;
  795. }
  796. const int32_t plugIndex = effect->uniqueID - kShellUniqueID - 1;
  797. CARLA_SAFE_ASSERT_RETURN(plugIndex >= 0, 0);
  798. pluginDesc = plm.descs.getAt(static_cast<size_t>(plugIndex), nullptr);
  799. #else // CARLA_VST_SHELL
  800. # if defined(CARLA_PLUGIN_64CH)
  801. const char* const pluginLabel = "carlapatchbay64";
  802. # elif defined(CARLA_PLUGIN_32CH)
  803. const char* const pluginLabel = "carlapatchbay32";
  804. # elif defined(CARLA_PLUGIN_16CH)
  805. const char* const pluginLabel = "carlapatchbay16";
  806. # elif CARLA_PLUGIN_PATCHBAY
  807. const char* const pluginLabel = "carlapatchbay";
  808. # else
  809. const char* const pluginLabel = "carlarack";
  810. # endif
  811. for (LinkedList<const NativePluginDescriptor*>::Itenerator it = plm.descs.begin2(); it.valid(); it.next())
  812. {
  813. const NativePluginDescriptor* const& tmpDesc(it.getValue(nullptr));
  814. CARLA_SAFE_ASSERT_CONTINUE(tmpDesc != nullptr);
  815. if (std::strcmp(tmpDesc->label, pluginLabel) == 0)
  816. {
  817. pluginDesc = tmpDesc;
  818. break;
  819. }
  820. }
  821. #endif // CARLA_VST_SHELL
  822. CARLA_SAFE_ASSERT_RETURN(pluginDesc != nullptr, 0);
  823. #ifdef CARLA_VST_SHELL
  824. effect->numPrograms = 1;
  825. effect->numParams = static_cast<int>(pluginDesc->paramIns);
  826. effect->numInputs = static_cast<int>(pluginDesc->audioIns);
  827. effect->numOutputs = static_cast<int>(pluginDesc->audioOuts);
  828. if (pluginDesc->hints & NATIVE_PLUGIN_HAS_UI)
  829. effect->flags |= effFlagsHasEditor;
  830. else
  831. effect->flags &= ~effFlagsHasEditor;
  832. /* carla as plugin always has NATIVE_PLUGIN_IS_SYNTH set
  833. if (pluginDesc->hints & NATIVE_PLUGIN_IS_SYNTH)
  834. effect->flags |= effFlagsIsSynth;
  835. else
  836. effect->flags &= ~effFlagsIsSynth;
  837. */
  838. #endif // CARLA_VST_SHELL
  839. #if CARLA_PLUGIN_SYNTH
  840. // override if requested
  841. effect->flags |= effFlagsIsSynth;
  842. #endif
  843. obj->plugin = new NativePlugin(effect, pluginDesc);
  844. return 1;
  845. }
  846. return 0;
  847. case effClose:
  848. if (VstObject* const obj = vstObjectPtr)
  849. {
  850. NativePlugin* const plugin(obj->plugin);
  851. if (plugin != nullptr)
  852. {
  853. obj->plugin = nullptr;
  854. delete plugin;
  855. }
  856. #if 0
  857. /* This code invalidates the object created in VSTPluginMain
  858. * Probably not safe against all hosts */
  859. obj->audioMaster = nullptr;
  860. effect->object = nullptr;
  861. delete obj;
  862. #endif
  863. return 1;
  864. }
  865. //delete effect;
  866. return 0;
  867. case effGetPlugCategory:
  868. #ifdef CARLA_VST_SHELL
  869. if (validPlugin)
  870. {
  871. #if CARLA_PLUGIN_SYNTH
  872. return kPlugCategSynth;
  873. #else
  874. const NativePluginDescriptor* const desc = pluginPtr->getDescriptor();
  875. return desc->category == NATIVE_PLUGIN_CATEGORY_SYNTH ? kPlugCategSynth : kPlugCategEffect;
  876. #endif
  877. }
  878. return kPlugCategShell;
  879. #elif CARLA_PLUGIN_SYNTH
  880. return kPlugCategSynth;
  881. #else
  882. return kPlugCategEffect;
  883. #endif
  884. case effGetEffectName:
  885. if (char* const cptr = (char*)ptr)
  886. {
  887. #if defined(CARLA_VST_SHELL)
  888. if (validPlugin)
  889. {
  890. const NativePluginDescriptor* const desc = pluginPtr->getDescriptor();
  891. #if CARLA_PLUGIN_SYNTH
  892. std::strncpy(cptr, desc->name, 32);
  893. #else
  894. std::snprintf(cptr, 32, "%s FX", desc->name);
  895. #endif
  896. }
  897. else
  898. {
  899. std::strncpy(cptr, "Carla-VstShell", 32);
  900. }
  901. #elif defined(CARLA_PLUGIN_64CH)
  902. std::strncpy(cptr, "Carla-Patchbay64", 32);
  903. #elif defined(CARLA_PLUGIN_32CH)
  904. std::strncpy(cptr, "Carla-Patchbay32", 32);
  905. #elif defined(CARLA_PLUGIN_16CH)
  906. std::strncpy(cptr, "Carla-Patchbay16", 32);
  907. #elif CARLA_PLUGIN_PATCHBAY
  908. # if CARLA_PLUGIN_SYNTH
  909. std::strncpy(cptr, "Carla-Patchbay", 32);
  910. # else
  911. std::strncpy(cptr, "Carla-PatchbayFX", 32);
  912. # endif
  913. #else // Rack mode
  914. # if CARLA_PLUGIN_SYNTH
  915. std::strncpy(cptr, "Carla-Rack", 32);
  916. # else
  917. std::strncpy(cptr, "Carla-RackFX", 32);
  918. # endif
  919. #endif
  920. return 1;
  921. }
  922. return 0;
  923. case effGetVendorString:
  924. if (char* const cptr = (char*)ptr)
  925. {
  926. #ifdef CARLA_VST_SHELL
  927. if (validPlugin)
  928. {
  929. const NativePluginDescriptor* const desc = pluginPtr->getDescriptor();
  930. std::strncpy(cptr, desc->maker, 32);
  931. }
  932. else
  933. #endif
  934. std::strncpy(cptr, "falkTX", 32);
  935. return 1;
  936. }
  937. return 0;
  938. case effGetProductString:
  939. if (char* const cptr = (char*)ptr)
  940. {
  941. #if defined(CARLA_VST_SHELL)
  942. if (validPlugin)
  943. {
  944. const NativePluginDescriptor* const desc = pluginPtr->getDescriptor();
  945. #if CARLA_PLUGIN_SYNTH
  946. std::strncpy(cptr, desc->label, 32);
  947. #else
  948. std::snprintf(cptr, 32, "%sFX", desc->label);
  949. #endif
  950. }
  951. else
  952. {
  953. std::strncpy(cptr, "CarlaVstShell", 32);
  954. }
  955. #elif defined(CARLA_PLUGIN_64CH)
  956. std::strncpy(cptr, "CarlaPatchbay64", 32);
  957. #elif defined(CARLA_PLUGIN_32CH)
  958. std::strncpy(cptr, "CarlaPatchbay32", 32);
  959. #elif defined(CARLA_PLUGIN_16CH)
  960. std::strncpy(cptr, "CarlaPatchbay16", 32);
  961. #elif CARLA_PLUGIN_PATCHBAY
  962. # if CARLA_PLUGIN_SYNTH
  963. std::strncpy(cptr, "CarlaPatchbay", 32);
  964. # else
  965. std::strncpy(cptr, "CarlaPatchbayFX", 32);
  966. # endif
  967. #else
  968. # if CARLA_PLUGIN_SYNTH
  969. std::strncpy(cptr, "CarlaRack", 32);
  970. # else
  971. std::strncpy(cptr, "CarlaRackFX", 32);
  972. # endif
  973. #endif
  974. return 1;
  975. }
  976. return 0;
  977. case effGetVendorVersion:
  978. return CARLA_VERSION_HEX;
  979. case effGetVstVersion:
  980. return kVstVersion;
  981. #ifdef CARLA_VST_SHELL
  982. case effShellGetNextPlugin:
  983. if (char* const cptr = (char*)ptr)
  984. {
  985. CARLA_SAFE_ASSERT_RETURN(effect != nullptr, 0);
  986. CARLA_SAFE_ASSERT_RETURN(effect->uniqueID >= kShellUniqueID, 0);
  987. PluginListManager& plm(PluginListManager::getInstance());
  988. for (;;)
  989. {
  990. const uint index2 = static_cast<uint>(effect->uniqueID - kShellUniqueID);
  991. if (index2 >= plm.descs.count())
  992. {
  993. effect->uniqueID = kShellUniqueID;
  994. return 0;
  995. }
  996. const NativePluginDescriptor* const desc = plm.descs.getAt(index2, nullptr);
  997. CARLA_SAFE_ASSERT_RETURN(desc != nullptr, 0);
  998. ++effect->uniqueID;
  999. if (desc->midiIns > 1 || desc->midiOuts > 1)
  1000. continue;
  1001. #if CARLA_PLUGIN_SYNTH
  1002. std::strncpy(cptr, desc->label, 32);
  1003. #else
  1004. std::snprintf(cptr, 32, "%sFX", desc->label);
  1005. #endif
  1006. return effect->uniqueID;
  1007. }
  1008. }
  1009. #endif
  1010. };
  1011. // handle advanced opcodes
  1012. if (validPlugin)
  1013. return pluginPtr->vst_dispatcher(opcode, index, value, ptr, opt);
  1014. return 0;
  1015. }
  1016. float vst_getParameterCallback(AEffect* effect, int32_t index)
  1017. {
  1018. if (validPlugin)
  1019. return pluginPtr->vst_getParameter(index);
  1020. return 0.0f;
  1021. }
  1022. void vst_setParameterCallback(AEffect* effect, int32_t index, float value)
  1023. {
  1024. if (validPlugin)
  1025. pluginPtr->vst_setParameter(index, value);
  1026. }
  1027. void vst_processCallback(AEffect* effect, float** inputs, float** outputs, int32_t sampleFrames)
  1028. {
  1029. if (validPlugin)
  1030. pluginPtr->vst_processReplacing(inputs, outputs, sampleFrames);
  1031. }
  1032. void vst_processReplacingCallback(AEffect* effect, float** inputs, float** outputs, int32_t sampleFrames)
  1033. {
  1034. if (validPlugin)
  1035. pluginPtr->vst_processReplacing(inputs, outputs, sampleFrames);
  1036. }
  1037. #undef pluginPtr
  1038. #undef validObject
  1039. #undef validPlugin
  1040. #undef vstObjectPtr
  1041. // -----------------------------------------------------------------------
  1042. const AEffect* VSTPluginMainInit(AEffect* const effect)
  1043. {
  1044. #if defined(CARLA_VST_SHELL)
  1045. if (const intptr_t uniqueID = VSTAudioMaster(effect, audioMasterCurrentId, 0, 0, nullptr, 0.0f))
  1046. effect->uniqueID = static_cast<int>(uniqueID);
  1047. else
  1048. effect->uniqueID = kShellUniqueID;
  1049. #elif defined(CARLA_PLUGIN_64CH)
  1050. effect->uniqueID = kBaseUniqueID+7;
  1051. #elif defined(CARLA_PLUGIN_32CH)
  1052. effect->uniqueID = kBaseUniqueID+6;
  1053. #elif defined(CARLA_PLUGIN_16CH)
  1054. effect->uniqueID = kBaseUniqueID+5;
  1055. #elif CARLA_PLUGIN_PATCHBAY
  1056. # if CARLA_PLUGIN_SYNTH
  1057. effect->uniqueID = kBaseUniqueID+4;
  1058. # else
  1059. effect->uniqueID = kBaseUniqueID+3;
  1060. # endif
  1061. #else
  1062. # if CARLA_PLUGIN_SYNTH
  1063. effect->uniqueID = kBaseUniqueID+2;
  1064. # else
  1065. effect->uniqueID = kBaseUniqueID+1;
  1066. # endif
  1067. #endif
  1068. // plugin fields
  1069. #ifndef CARLA_VST_SHELL
  1070. effect->numParams = kNumParameters;
  1071. effect->numPrograms = 1;
  1072. # if defined(CARLA_PLUGIN_64CH)
  1073. effect->numInputs = 64;
  1074. effect->numOutputs = 64;
  1075. # elif defined(CARLA_PLUGIN_32CH)
  1076. effect->numInputs = 32;
  1077. effect->numOutputs = 32;
  1078. # elif defined(CARLA_PLUGIN_16CH)
  1079. effect->numInputs = 16;
  1080. effect->numOutputs = 16;
  1081. # else
  1082. effect->numInputs = 2;
  1083. effect->numOutputs = 2;
  1084. # endif
  1085. #endif
  1086. // plugin flags
  1087. effect->flags |= effFlagsCanReplacing;
  1088. effect->flags |= effFlagsProgramChunks;
  1089. #ifndef CARLA_VST_SHELL
  1090. effect->flags |= effFlagsHasEditor;
  1091. #endif
  1092. #if CARLA_PLUGIN_SYNTH
  1093. effect->flags |= effFlagsIsSynth;
  1094. #endif
  1095. return effect;
  1096. // may be unused
  1097. (void)kBaseUniqueID;
  1098. }
  1099. // -----------------------------------------------------------------------