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

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