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

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