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.

773 lines
23KB

  1. /*
  2. * Carla Native Plugins
  3. * Copyright (C) 2013-2014 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #define CARLA_NATIVE_PLUGIN_VST
  18. #include "carla-base.cpp"
  19. #include "CarlaMathUtils.hpp"
  20. #include "juce_core.h"
  21. #if defined(CARLA_OS_MAC) || defined(CARLA_OS_WIN)
  22. # include "juce_gui_basics.h"
  23. #else
  24. namespace juce {
  25. # include "juce_events/messages/juce_Initialisation.h"
  26. } // namespace juce
  27. #endif
  28. #ifdef VESTIGE_HEADER
  29. # include "vestige/aeffectx.h"
  30. #define effFlagsProgramChunks (1 << 5)
  31. #define effGetParamLabel 6
  32. #define effGetChunk 23
  33. #define effSetChunk 24
  34. #define effGetPlugCategory 35
  35. #define kPlugCategEffect 1
  36. #define kPlugCategSynth 2
  37. #define kVstVersion 2400
  38. struct ERect {
  39. int16_t top, left, bottom, right;
  40. };
  41. #else
  42. # include "vst/aeffectx.h"
  43. #endif
  44. using juce::ScopedJuceInitialiser_GUI;
  45. using juce::SharedResourcePointer;
  46. static uint32_t d_lastBufferSize = 0;
  47. static double d_lastSampleRate = 0.0;
  48. // -----------------------------------------------------------------------
  49. class NativePlugin
  50. {
  51. public:
  52. static const uint32_t kMaxMidiEvents = 512;
  53. NativePlugin(const audioMasterCallback audioMaster, AEffect* const effect, const NativePluginDescriptor* desc)
  54. : fAudioMaster(audioMaster),
  55. fEffect(effect),
  56. fHandle(nullptr),
  57. fHost(),
  58. fDescriptor(desc),
  59. fBufferSize(d_lastBufferSize),
  60. fSampleRate(d_lastSampleRate),
  61. fMidiEventCount(0),
  62. fTimeInfo(),
  63. fVstRect(),
  64. fStateChunk(nullptr),
  65. sJuceInitialiser(),
  66. leakDetector_NativePlugin()
  67. {
  68. fHost.handle = this;
  69. fHost.uiName = carla_strdup("CarlaVST");
  70. fHost.uiParentId = 0;
  71. // find resource dir
  72. using juce::File;
  73. File curExe = File::getSpecialLocation(File::currentExecutableFile).getLinkedTarget();
  74. File resDir = curExe.getSiblingFile("carla-resources");
  75. if (! resDir.exists())
  76. resDir = curExe.getSiblingFile("resources");
  77. if (! resDir.exists())
  78. resDir = File("/usr/share/carla/resources/");
  79. fHost.resourceDir = carla_strdup(resDir.getFullPathName().toRawUTF8());
  80. fHost.get_buffer_size = host_get_buffer_size;
  81. fHost.get_sample_rate = host_get_sample_rate;
  82. fHost.is_offline = host_is_offline;
  83. fHost.get_time_info = host_get_time_info;
  84. fHost.write_midi_event = host_write_midi_event;
  85. fHost.ui_parameter_changed = host_ui_parameter_changed;
  86. fHost.ui_custom_data_changed = host_ui_custom_data_changed;
  87. fHost.ui_closed = host_ui_closed;
  88. fHost.ui_open_file = host_ui_open_file;
  89. fHost.ui_save_file = host_ui_save_file;
  90. fHost.dispatcher = host_dispatcher;
  91. fVstRect.top = 0;
  92. fVstRect.left = 0;
  93. fVstRect.bottom = 512;
  94. fVstRect.right = 740;
  95. init();
  96. }
  97. ~NativePlugin()
  98. {
  99. if (fDescriptor->cleanup != nullptr && fHandle != nullptr)
  100. fDescriptor->cleanup(fHandle);
  101. fHandle = nullptr;
  102. if (fStateChunk != nullptr)
  103. {
  104. std::free(fStateChunk);
  105. fStateChunk = nullptr;
  106. }
  107. if (fHost.uiName != nullptr)
  108. {
  109. delete[] fHost.uiName;
  110. fHost.uiName = nullptr;
  111. }
  112. if (fHost.resourceDir != nullptr)
  113. {
  114. delete[] fHost.resourceDir;
  115. fHost.resourceDir = nullptr;
  116. }
  117. }
  118. bool init()
  119. {
  120. if (fDescriptor->instantiate == nullptr || fDescriptor->process == nullptr)
  121. {
  122. carla_stderr("Plugin is missing something...");
  123. return false;
  124. }
  125. fHandle = fDescriptor->instantiate(&fHost);
  126. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr, false);
  127. carla_zeroStruct<NativeMidiEvent>(fMidiEvents, kMaxMidiEvents);
  128. carla_zeroStruct<NativeTimeInfo>(fTimeInfo);
  129. return true;
  130. }
  131. // -------------------------------------------------------------------
  132. intptr_t vst_dispatcher(const int32_t opcode, const int32_t /*index*/, const intptr_t value, void* const ptr, const float opt)
  133. {
  134. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr, 0);
  135. intptr_t ret = 0;
  136. switch (opcode)
  137. {
  138. case effSetSampleRate:
  139. if (carla_compareFloats(fSampleRate, static_cast<double>(opt)))
  140. return 0;
  141. fSampleRate = opt;
  142. if (fDescriptor->dispatcher != nullptr)
  143. fDescriptor->dispatcher(fHandle, PLUGIN_OPCODE_SAMPLE_RATE_CHANGED, 0, 0, nullptr, (float)fSampleRate);
  144. break;
  145. case effSetBlockSize:
  146. if (fBufferSize == static_cast<uint32_t>(value))
  147. return 0;
  148. fBufferSize = static_cast<uint32_t>(value);
  149. if (fDescriptor->dispatcher != nullptr)
  150. fDescriptor->dispatcher(fHandle, PLUGIN_OPCODE_BUFFER_SIZE_CHANGED, 0, value, nullptr, 0.0f);
  151. break;
  152. case effMainsChanged:
  153. if (value != 0)
  154. {
  155. if (fDescriptor->activate != nullptr)
  156. fDescriptor->activate(fHandle);
  157. fMidiEventCount = 0;
  158. carla_zeroStruct<NativeMidiEvent>(fMidiEvents, kMaxMidiEvents);
  159. carla_zeroStruct<NativeTimeInfo>(fTimeInfo);
  160. }
  161. else
  162. {
  163. if (fDescriptor->deactivate != nullptr)
  164. fDescriptor->deactivate(fHandle);
  165. }
  166. break;
  167. case effEditGetRect:
  168. *(ERect**)ptr = &fVstRect;
  169. ret = 1;
  170. break;
  171. case effEditOpen:
  172. if (fDescriptor->ui_show != nullptr)
  173. {
  174. char strBuf[0xff+1];
  175. strBuf[0xff] = '\0';
  176. std::snprintf(strBuf, 0xff, P_INTPTR, (intptr_t)ptr);
  177. carla_setenv("CARLA_PLUGIN_EMBED_WINID", strBuf);
  178. fDescriptor->ui_show(fHandle, true);
  179. carla_setenv("CARLA_PLUGIN_EMBED_WINID", "0");
  180. ret = 1;
  181. }
  182. break;
  183. case effEditClose:
  184. if (fDescriptor->ui_show != nullptr)
  185. {
  186. fDescriptor->ui_show(fHandle, false);
  187. ret = 1;
  188. }
  189. break;
  190. case effEditIdle:
  191. if (fDescriptor->ui_idle != nullptr)
  192. fDescriptor->ui_idle(fHandle);
  193. break;
  194. case effGetChunk:
  195. if (ptr == nullptr || fDescriptor->get_state == nullptr)
  196. return 0;
  197. if (fStateChunk != nullptr)
  198. std::free(fStateChunk);
  199. fStateChunk = fDescriptor->get_state(fHandle);
  200. if (fStateChunk == nullptr)
  201. return 0;
  202. ret = static_cast<intptr_t>(std::strlen(fStateChunk)+1);
  203. *(void**)ptr = fStateChunk;
  204. break;
  205. case effSetChunk:
  206. if (value <= 0 || fDescriptor->set_state == nullptr)
  207. return 0;
  208. if (value == 1)
  209. return 1;
  210. if (const char* const state = (const char*)ptr)
  211. {
  212. fDescriptor->set_state(fHandle, state);
  213. ret = 1;
  214. }
  215. break;
  216. case effProcessEvents:
  217. if (const VstEvents* const events = (const VstEvents*)ptr)
  218. {
  219. if (events->numEvents == 0)
  220. break;
  221. for (int i=0, count=events->numEvents; i < count; ++i)
  222. {
  223. const VstMidiEvent* const vstMidiEvent((const VstMidiEvent*)events->events[i]);
  224. if (vstMidiEvent == nullptr)
  225. break;
  226. if (vstMidiEvent->type != kVstMidiType || vstMidiEvent->deltaFrames < 0)
  227. continue;
  228. if (fMidiEventCount >= kMaxMidiEvents)
  229. break;
  230. fMidiEvents[fMidiEventCount].port = 0;
  231. fMidiEvents[fMidiEventCount].time = static_cast<uint32_t>(vstMidiEvent->deltaFrames);
  232. fMidiEvents[fMidiEventCount].size = 3;
  233. for (uint32_t j=0; j < 3; ++j)
  234. fMidiEvents[fMidiEventCount].data[j] = static_cast<uint8_t>(vstMidiEvent->midiData[j]);
  235. fMidiEventCount += 1;
  236. }
  237. }
  238. break;
  239. case effCanDo:
  240. if (const char* const canDo = (const char*)ptr)
  241. {
  242. if (std::strcmp(canDo, "receiveVstEvents") == 0)
  243. return 1;
  244. if (std::strcmp(canDo, "receiveVstMidiEvent") == 0)
  245. return 1;
  246. if (std::strcmp(canDo, "receiveVstTimeInfo") == 0)
  247. return 1;
  248. }
  249. break;
  250. }
  251. return ret;
  252. }
  253. float vst_getParameter(const int32_t /*index*/)
  254. {
  255. return 0.0f;
  256. }
  257. void vst_setParameter(const int32_t /*index*/, const float /*value*/)
  258. {
  259. }
  260. void vst_processReplacing(const float** const inputs, float** const outputs, const int32_t sampleFrames)
  261. {
  262. if (sampleFrames <= 0)
  263. return;
  264. static const int kWantVstTimeFlags(kVstTransportPlaying|kVstPpqPosValid|kVstTempoValid|kVstTimeSigValid);
  265. if (const VstTimeInfo* const vstTimeInfo = (const VstTimeInfo*)fAudioMaster(fEffect, audioMasterGetTime, 0, kWantVstTimeFlags, nullptr, 0.0f))
  266. {
  267. fTimeInfo.frame = static_cast<uint64_t>(vstTimeInfo->samplePos);
  268. fTimeInfo.playing = (vstTimeInfo->flags & kVstTransportPlaying);
  269. fTimeInfo.bbt.valid = ((vstTimeInfo->flags & kVstTempoValid) != 0 || (vstTimeInfo->flags & kVstTimeSigValid) != 0);
  270. // ticksPerBeat is not possible with VST
  271. fTimeInfo.bbt.ticksPerBeat = 960.0;
  272. if (vstTimeInfo->flags & kVstTempoValid)
  273. fTimeInfo.bbt.beatsPerMinute = vstTimeInfo->tempo;
  274. else
  275. fTimeInfo.bbt.beatsPerMinute = 120.0;
  276. if (vstTimeInfo->flags & (kVstPpqPosValid|kVstTimeSigValid))
  277. {
  278. const int ppqPerBar = vstTimeInfo->timeSigNumerator * 4 / vstTimeInfo->timeSigDenominator;
  279. const double barBeats = (std::fmod(vstTimeInfo->ppqPos, ppqPerBar) / ppqPerBar) * vstTimeInfo->timeSigDenominator;
  280. const double rest = std::fmod(barBeats, 1.0);
  281. fTimeInfo.bbt.bar = static_cast<int32_t>(vstTimeInfo->ppqPos)/ppqPerBar + 1;
  282. fTimeInfo.bbt.beat = static_cast<int32_t>(barBeats-rest+1.0);
  283. fTimeInfo.bbt.tick = static_cast<int32_t>(rest*fTimeInfo.bbt.ticksPerBeat+0.5);
  284. fTimeInfo.bbt.beatsPerBar = static_cast<float>(vstTimeInfo->timeSigNumerator);
  285. fTimeInfo.bbt.beatType = static_cast<float>(vstTimeInfo->timeSigDenominator);
  286. }
  287. else
  288. {
  289. fTimeInfo.bbt.bar = 1;
  290. fTimeInfo.bbt.beat = 1;
  291. fTimeInfo.bbt.tick = 0;
  292. fTimeInfo.bbt.beatsPerBar = 4.0f;
  293. fTimeInfo.bbt.beatType = 4.0f;
  294. }
  295. fTimeInfo.bbt.barStartTick = fTimeInfo.bbt.ticksPerBeat*fTimeInfo.bbt.beatsPerBar*(fTimeInfo.bbt.bar-1);
  296. }
  297. if (fHandle != nullptr)
  298. fDescriptor->process(fHandle, const_cast<float**>(inputs), outputs, static_cast<uint32_t>(sampleFrames), fMidiEvents, fMidiEventCount);
  299. fMidiEventCount = 0;
  300. }
  301. protected:
  302. // -------------------------------------------------------------------
  303. uint32_t handleGetBufferSize() const
  304. {
  305. return fBufferSize;
  306. }
  307. double handleGetSampleRate() const
  308. {
  309. return fSampleRate;
  310. }
  311. bool handleIsOffline() const
  312. {
  313. return false;
  314. }
  315. const NativeTimeInfo* handleGetTimeInfo() const
  316. {
  317. return &fTimeInfo;
  318. }
  319. bool handleWriteMidiEvent(const NativeMidiEvent* const event)
  320. {
  321. CARLA_SAFE_ASSERT_RETURN(fDescriptor->midiOuts > 0, false);
  322. CARLA_SAFE_ASSERT_RETURN(event != nullptr, false);
  323. CARLA_SAFE_ASSERT_RETURN(event->data[0] != 0, false);
  324. // reverse-find first free event, and put it there
  325. for (uint32_t i=(kMaxMidiEvents*2)-1; i > fMidiEventCount; --i)
  326. {
  327. if (fMidiEvents[i].data[0] == 0)
  328. {
  329. std::memcpy(&fMidiEvents[i], event, sizeof(NativeMidiEvent));
  330. return true;
  331. }
  332. }
  333. return false;
  334. }
  335. void handleUiParameterChanged(const uint32_t /*index*/, const float /*value*/) const
  336. {
  337. }
  338. void handleUiCustomDataChanged(const char* const /*key*/, const char* const /*value*/) const
  339. {
  340. }
  341. void handleUiClosed()
  342. {
  343. }
  344. const char* handleUiOpenFile(const bool /*isDir*/, const char* const /*title*/, const char* const /*filter*/) const
  345. {
  346. // TODO
  347. return nullptr;
  348. }
  349. const char* handleUiSaveFile(const bool /*isDir*/, const char* const /*title*/, const char* const /*filter*/) const
  350. {
  351. // TODO
  352. return nullptr;
  353. }
  354. intptr_t handleDispatcher(const NativeHostDispatcherOpcode opcode, const int32_t index, const intptr_t value, void* const ptr, const float opt)
  355. {
  356. carla_debug("NativePlugin::handleDispatcher(%i, %i, " P_INTPTR ", %p, %f)", opcode, index, value, ptr, opt);
  357. return 0;
  358. // unused for now
  359. (void)opcode; (void)index; (void)value; (void)ptr; (void)opt;
  360. }
  361. private:
  362. // VST stuff
  363. const audioMasterCallback fAudioMaster;
  364. AEffect* const fEffect;
  365. // Native data
  366. NativePluginHandle fHandle;
  367. NativeHostDescriptor fHost;
  368. const NativePluginDescriptor* const fDescriptor;
  369. // VST host data
  370. uint32_t fBufferSize;
  371. double fSampleRate;
  372. // Temporary data
  373. uint32_t fMidiEventCount;
  374. NativeMidiEvent fMidiEvents[kMaxMidiEvents];
  375. NativeTimeInfo fTimeInfo;
  376. ERect fVstRect;
  377. char* fStateChunk;
  378. SharedResourcePointer<ScopedJuceInitialiser_GUI> sJuceInitialiser;
  379. // -------------------------------------------------------------------
  380. #define handlePtr ((NativePlugin*)handle)
  381. static uint32_t host_get_buffer_size(NativeHostHandle handle)
  382. {
  383. return handlePtr->handleGetBufferSize();
  384. }
  385. static double host_get_sample_rate(NativeHostHandle handle)
  386. {
  387. return handlePtr->handleGetSampleRate();
  388. }
  389. static bool host_is_offline(NativeHostHandle handle)
  390. {
  391. return handlePtr->handleIsOffline();
  392. }
  393. static const NativeTimeInfo* host_get_time_info(NativeHostHandle handle)
  394. {
  395. return handlePtr->handleGetTimeInfo();
  396. }
  397. static bool host_write_midi_event(NativeHostHandle handle, const NativeMidiEvent* event)
  398. {
  399. return handlePtr->handleWriteMidiEvent(event);
  400. }
  401. static void host_ui_parameter_changed(NativeHostHandle handle, uint32_t index, float value)
  402. {
  403. handlePtr->handleUiParameterChanged(index, value);
  404. }
  405. static void host_ui_custom_data_changed(NativeHostHandle handle, const char* key, const char* value)
  406. {
  407. handlePtr->handleUiCustomDataChanged(key, value);
  408. }
  409. static void host_ui_closed(NativeHostHandle handle)
  410. {
  411. handlePtr->handleUiClosed();
  412. }
  413. static const char* host_ui_open_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
  414. {
  415. return handlePtr->handleUiOpenFile(isDir, title, filter);
  416. }
  417. static const char* host_ui_save_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
  418. {
  419. return handlePtr->handleUiSaveFile(isDir, title, filter);
  420. }
  421. static intptr_t host_dispatcher(NativeHostHandle handle, NativeHostDispatcherOpcode opcode, int32_t index, intptr_t value, void* ptr, float opt)
  422. {
  423. return handlePtr->handleDispatcher(opcode, index, value, ptr, opt);
  424. }
  425. #undef handlePtr
  426. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(NativePlugin)
  427. };
  428. // -----------------------------------------------------------------------
  429. #ifdef VESTIGE_HEADER
  430. # define handlePtr ((NativePlugin*)effect->ptr2)
  431. # define validEffect effect != nullptr && effect->ptr2 != nullptr
  432. #else
  433. # define handlePtr ((NativePlugin*)effect->resvd2)
  434. # define validEffect effect != nullptr && effect->resvd2 != 0
  435. #endif
  436. static intptr_t vst_dispatcherCallback(AEffect* effect, int32_t opcode, int32_t index, intptr_t value, void* ptr, float opt)
  437. {
  438. // handle base opcodes
  439. switch (opcode)
  440. {
  441. case effOpen:
  442. #ifdef VESTIGE_HEADER
  443. if (effect != nullptr && effect->ptr3 != nullptr)
  444. {
  445. audioMasterCallback audioMaster = (audioMasterCallback)effect->ptr3;
  446. #else
  447. if (effect != nullptr && effect->object != nullptr)
  448. {
  449. audioMasterCallback audioMaster = (audioMasterCallback)effect->object;
  450. #endif
  451. d_lastBufferSize = static_cast<uint32_t>(audioMaster(effect, audioMasterGetBlockSize, 0, 0, nullptr, 0.0f));
  452. d_lastSampleRate = static_cast<double>(audioMaster(effect, audioMasterGetSampleRate, 0, 0, nullptr, 0.0f));
  453. // some hosts are not ready at this point or return 0 buffersize/samplerate
  454. if (d_lastBufferSize == 0)
  455. d_lastBufferSize = 2048;
  456. if (d_lastSampleRate <= 0.0)
  457. d_lastSampleRate = 44100.0;
  458. const NativePluginDescriptor* pluginDesc = nullptr;
  459. #ifdef CARLA_PLUGIN_PATCHBAY
  460. const char* const pluginLabel = "carlapatchbay";
  461. #else
  462. const char* const pluginLabel = "carlarack";
  463. #endif
  464. PluginListManager& plm(PluginListManager::getInstance());
  465. for (LinkedList<const NativePluginDescriptor*>::Itenerator it = plm.descs.begin(); it.valid(); it.next())
  466. {
  467. const NativePluginDescriptor* const& tmpDesc(it.getValue());
  468. if (std::strcmp(tmpDesc->label, pluginLabel) == 0)
  469. {
  470. pluginDesc = tmpDesc;
  471. break;
  472. }
  473. }
  474. CARLA_SAFE_ASSERT_RETURN(pluginDesc != nullptr, 0);
  475. NativePlugin* const plugin(new NativePlugin(audioMaster, effect, pluginDesc));
  476. #ifdef VESTIGE_HEADER
  477. effect->ptr2 = plugin;
  478. #else
  479. effect->resvd2 = (intptr_t)plugin;
  480. #endif
  481. return 1;
  482. }
  483. return 0;
  484. case effClose:
  485. if (validEffect)
  486. {
  487. #ifdef VESTIGE_HEADER
  488. delete (NativePlugin*)effect->ptr2;
  489. effect->ptr2 = nullptr;
  490. effect->ptr3 = nullptr;
  491. #else
  492. delete (NativePlugin*)effect->resvd2;
  493. effect->resvd2 = 0;
  494. effect->object = nullptr;
  495. #endif
  496. delete effect;
  497. return 1;
  498. }
  499. return 0;
  500. case effGetPlugCategory:
  501. #ifdef CARLA_PLUGIN_SYNTH
  502. return kPlugCategSynth;
  503. #else
  504. return kPlugCategEffect;
  505. #endif
  506. case effGetEffectName:
  507. if (ptr != nullptr)
  508. {
  509. #ifdef CARLA_PLUGIN_PATCHBAY
  510. std::strncpy((char*)ptr, "Carla-Patchbay", 64);
  511. #else
  512. std::strncpy((char*)ptr, "Carla-Rack", 64);
  513. #endif
  514. return 1;
  515. }
  516. return 0;
  517. case effGetVendorString:
  518. if (ptr != nullptr)
  519. {
  520. std::strncpy((char*)ptr, "falkTX", 64);
  521. return 1;
  522. }
  523. return 0;
  524. case effGetProductString:
  525. if (ptr != nullptr)
  526. {
  527. #ifdef CARLA_PLUGIN_PATCHBAY
  528. std::strncpy((char*)ptr, "CarlaPatchbay", 32);
  529. #else
  530. std::strncpy((char*)ptr, "CarlaRack", 32);
  531. #endif
  532. return 1;
  533. }
  534. return 0;
  535. case effGetVendorVersion:
  536. return CARLA_VERSION_HEX;
  537. case effGetVstVersion:
  538. return kVstVersion;
  539. };
  540. // handle advanced opcodes
  541. if (validEffect)
  542. return handlePtr->vst_dispatcher(opcode, index, value, ptr, opt);
  543. return 0;
  544. }
  545. static float vst_getParameterCallback(AEffect* effect, int32_t index)
  546. {
  547. if (validEffect)
  548. return handlePtr->vst_getParameter(index);
  549. return 0.0f;
  550. }
  551. static void vst_setParameterCallback(AEffect* effect, int32_t index, float value)
  552. {
  553. if (validEffect)
  554. handlePtr->vst_setParameter(index, value);
  555. }
  556. static void vst_processCallback(AEffect* effect, float** inputs, float** outputs, int32_t sampleFrames)
  557. {
  558. if (validEffect)
  559. handlePtr->vst_processReplacing(const_cast<const float**>(inputs), outputs, sampleFrames);
  560. }
  561. static void vst_processReplacingCallback(AEffect* effect, float** inputs, float** outputs, int32_t sampleFrames)
  562. {
  563. if (validEffect)
  564. handlePtr->vst_processReplacing(const_cast<const float**>(inputs), outputs, sampleFrames);
  565. }
  566. #undef handlePtr
  567. // -----------------------------------------------------------------------
  568. CARLA_EXPORT
  569. #ifdef CARLA_OS_WIN
  570. const AEffect* VSTPluginMain(audioMasterCallback audioMaster);
  571. #else
  572. const AEffect* VSTPluginMain(audioMasterCallback audioMaster) asm ("main");
  573. #endif
  574. CARLA_EXPORT
  575. const AEffect* VSTPluginMain(audioMasterCallback audioMaster)
  576. {
  577. // old version
  578. if (audioMaster(nullptr, audioMasterVersion, 0, 0, nullptr, 0.0f) == 0)
  579. return nullptr;
  580. AEffect* const effect(new AEffect);
  581. std::memset(effect, 0, sizeof(AEffect));
  582. // vst fields
  583. effect->magic = kEffectMagic;
  584. #ifdef VESTIGE_HEADER
  585. int32_t* const version = (int32_t*)&effect->unknown1;
  586. *version = CARLA_VERSION_HEX;
  587. #else
  588. effect->version = CARLA_VERSION_HEX;
  589. #endif
  590. static const int32_t uniqueId = CCONST('C', 'r', 'l', 'a');
  591. #ifdef CARLA_PLUGIN_SYNTH
  592. # ifdef CARLA_PLUGIN_PATCHBAY
  593. effect->uniqueID = uniqueId+4;
  594. # else
  595. effect->uniqueID = uniqueId+3;
  596. # endif
  597. #else
  598. # ifdef CARLA_PLUGIN_PATCHBAY
  599. effect->uniqueID = uniqueId+2;
  600. # else
  601. effect->uniqueID = uniqueId+1;
  602. # endif
  603. #endif
  604. // plugin fields
  605. effect->numParams = 0;
  606. effect->numPrograms = 0;
  607. effect->numInputs = 2;
  608. effect->numOutputs = 2;
  609. // plugin flags
  610. effect->flags |= effFlagsCanReplacing;
  611. effect->flags |= effFlagsHasEditor;
  612. effect->flags |= effFlagsProgramChunks;
  613. #ifdef CARLA_PLUGIN_SYNTH
  614. effect->flags |= effFlagsIsSynth;
  615. #endif
  616. // static calls
  617. effect->dispatcher = vst_dispatcherCallback;
  618. effect->process = vst_processCallback;
  619. effect->getParameter = vst_getParameterCallback;
  620. effect->setParameter = vst_setParameterCallback;
  621. effect->processReplacing = vst_processReplacingCallback;
  622. // pointers
  623. #ifdef VESTIGE_HEADER
  624. effect->ptr3 = (void*)audioMaster;
  625. #else
  626. effect->object = (void*)audioMaster;
  627. #endif
  628. return effect;
  629. }
  630. // -----------------------------------------------------------------------