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.

805 lines
24KB

  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. // tell host we want MIDI events
  97. fAudioMaster(fEffect, audioMasterWantMidi, 0, 0, nullptr, 0.0f);
  98. }
  99. ~NativePlugin()
  100. {
  101. if (fDescriptor->cleanup != nullptr && fHandle != nullptr)
  102. fDescriptor->cleanup(fHandle);
  103. fHandle = nullptr;
  104. if (fStateChunk != nullptr)
  105. {
  106. std::free(fStateChunk);
  107. fStateChunk = nullptr;
  108. }
  109. if (fHost.uiName != nullptr)
  110. {
  111. delete[] fHost.uiName;
  112. fHost.uiName = nullptr;
  113. }
  114. if (fHost.resourceDir != nullptr)
  115. {
  116. delete[] fHost.resourceDir;
  117. fHost.resourceDir = nullptr;
  118. }
  119. }
  120. bool init()
  121. {
  122. if (fDescriptor->instantiate == nullptr || fDescriptor->process == nullptr)
  123. {
  124. carla_stderr("Plugin is missing something...");
  125. return false;
  126. }
  127. fHandle = fDescriptor->instantiate(&fHost);
  128. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr, false);
  129. carla_zeroStruct<NativeMidiEvent>(fMidiEvents, kMaxMidiEvents*2);
  130. carla_zeroStruct<NativeTimeInfo>(fTimeInfo);
  131. return true;
  132. }
  133. // -------------------------------------------------------------------
  134. intptr_t vst_dispatcher(const int32_t opcode, const int32_t /*index*/, const intptr_t value, void* const ptr, const float opt)
  135. {
  136. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr, 0);
  137. intptr_t ret = 0;
  138. switch (opcode)
  139. {
  140. case effSetSampleRate:
  141. if (carla_compareFloats(fSampleRate, static_cast<double>(opt)))
  142. return 0;
  143. fSampleRate = opt;
  144. if (fDescriptor->dispatcher != nullptr)
  145. fDescriptor->dispatcher(fHandle, PLUGIN_OPCODE_SAMPLE_RATE_CHANGED, 0, 0, nullptr, (float)fSampleRate);
  146. break;
  147. case effSetBlockSize:
  148. if (fBufferSize == static_cast<uint32_t>(value))
  149. return 0;
  150. fBufferSize = static_cast<uint32_t>(value);
  151. if (fDescriptor->dispatcher != nullptr)
  152. fDescriptor->dispatcher(fHandle, PLUGIN_OPCODE_BUFFER_SIZE_CHANGED, 0, value, nullptr, 0.0f);
  153. break;
  154. case effMainsChanged:
  155. if (value != 0)
  156. {
  157. if (fDescriptor->activate != nullptr)
  158. fDescriptor->activate(fHandle);
  159. fMidiEventCount = 0;
  160. carla_zeroStruct<NativeMidiEvent>(fMidiEvents, kMaxMidiEvents*2);
  161. carla_zeroStruct<NativeTimeInfo>(fTimeInfo);
  162. }
  163. else
  164. {
  165. if (fDescriptor->deactivate != nullptr)
  166. fDescriptor->deactivate(fHandle);
  167. }
  168. break;
  169. case effEditGetRect:
  170. *(ERect**)ptr = &fVstRect;
  171. ret = 1;
  172. break;
  173. case effEditOpen:
  174. if (fDescriptor->ui_show != nullptr)
  175. {
  176. char strBuf[0xff+1];
  177. strBuf[0xff] = '\0';
  178. std::snprintf(strBuf, 0xff, P_INTPTR, (intptr_t)ptr);
  179. carla_setenv("CARLA_PLUGIN_EMBED_WINID", strBuf);
  180. fDescriptor->ui_show(fHandle, true);
  181. carla_setenv("CARLA_PLUGIN_EMBED_WINID", "0");
  182. ret = 1;
  183. }
  184. break;
  185. case effEditClose:
  186. if (fDescriptor->ui_show != nullptr)
  187. {
  188. fDescriptor->ui_show(fHandle, false);
  189. ret = 1;
  190. }
  191. break;
  192. case effEditIdle:
  193. if (fDescriptor->ui_idle != nullptr)
  194. fDescriptor->ui_idle(fHandle);
  195. break;
  196. case effGetChunk:
  197. if (ptr == nullptr || fDescriptor->get_state == nullptr)
  198. return 0;
  199. if (fStateChunk != nullptr)
  200. std::free(fStateChunk);
  201. fStateChunk = fDescriptor->get_state(fHandle);
  202. if (fStateChunk == nullptr)
  203. return 0;
  204. ret = static_cast<intptr_t>(std::strlen(fStateChunk)+1);
  205. *(void**)ptr = fStateChunk;
  206. break;
  207. case effSetChunk:
  208. if (value <= 0 || fDescriptor->set_state == nullptr)
  209. return 0;
  210. if (value == 1)
  211. return 1;
  212. if (const char* const state = (const char*)ptr)
  213. {
  214. fDescriptor->set_state(fHandle, state);
  215. ret = 1;
  216. }
  217. break;
  218. case effProcessEvents:
  219. if (const VstEvents* const events = (const VstEvents*)ptr)
  220. {
  221. if (events->numEvents == 0)
  222. break;
  223. for (int i=0, count=events->numEvents; i < count; ++i)
  224. {
  225. const VstMidiEvent* const vstMidiEvent((const VstMidiEvent*)events->events[i]);
  226. if (vstMidiEvent == nullptr)
  227. break;
  228. if (vstMidiEvent->type != kVstMidiType || vstMidiEvent->deltaFrames < 0)
  229. continue;
  230. if (fMidiEventCount >= kMaxMidiEvents)
  231. break;
  232. fMidiEvents[fMidiEventCount].port = 0;
  233. fMidiEvents[fMidiEventCount].time = static_cast<uint32_t>(vstMidiEvent->deltaFrames);
  234. fMidiEvents[fMidiEventCount].size = 3;
  235. for (uint32_t j=0; j < 3; ++j)
  236. fMidiEvents[fMidiEventCount].data[j] = static_cast<uint8_t>(vstMidiEvent->midiData[j]);
  237. fMidiEventCount += 1;
  238. }
  239. }
  240. break;
  241. case effCanDo:
  242. if (const char* const canDo = (const char*)ptr)
  243. {
  244. if (std::strcmp(canDo, "receiveVstEvents") == 0)
  245. return 1;
  246. if (std::strcmp(canDo, "receiveVstMidiEvent") == 0)
  247. return 1;
  248. if (std::strcmp(canDo, "receiveVstTimeInfo") == 0)
  249. return 1;
  250. #if 0
  251. // TODO - MIDI Out
  252. if (std::strcmp(canDo, "sendVstEvents") == 0)
  253. return 1;
  254. if (std::strcmp(canDo, "sendVstMidiEvent") == 0)
  255. return 1;
  256. #endif
  257. }
  258. break;
  259. }
  260. return ret;
  261. }
  262. float vst_getParameter(const int32_t /*index*/)
  263. {
  264. return 0.0f;
  265. }
  266. void vst_setParameter(const int32_t /*index*/, const float /*value*/)
  267. {
  268. }
  269. void vst_processReplacing(const float** const inputs, float** const outputs, const int32_t sampleFrames)
  270. {
  271. if (sampleFrames <= 0)
  272. return;
  273. static const int kWantVstTimeFlags(kVstTransportPlaying|kVstPpqPosValid|kVstTempoValid|kVstTimeSigValid);
  274. if (const VstTimeInfo* const vstTimeInfo = (const VstTimeInfo*)fAudioMaster(fEffect, audioMasterGetTime, 0, kWantVstTimeFlags, nullptr, 0.0f))
  275. {
  276. fTimeInfo.frame = static_cast<uint64_t>(vstTimeInfo->samplePos);
  277. fTimeInfo.playing = (vstTimeInfo->flags & kVstTransportPlaying);
  278. fTimeInfo.bbt.valid = ((vstTimeInfo->flags & kVstTempoValid) != 0 || (vstTimeInfo->flags & kVstTimeSigValid) != 0);
  279. // ticksPerBeat is not possible with VST
  280. fTimeInfo.bbt.ticksPerBeat = 960.0;
  281. if (vstTimeInfo->flags & kVstTempoValid)
  282. fTimeInfo.bbt.beatsPerMinute = vstTimeInfo->tempo;
  283. else
  284. fTimeInfo.bbt.beatsPerMinute = 120.0;
  285. if (vstTimeInfo->flags & (kVstPpqPosValid|kVstTimeSigValid))
  286. {
  287. const int ppqPerBar = vstTimeInfo->timeSigNumerator * 4 / vstTimeInfo->timeSigDenominator;
  288. const double barBeats = (std::fmod(vstTimeInfo->ppqPos, ppqPerBar) / ppqPerBar) * vstTimeInfo->timeSigDenominator;
  289. const double rest = std::fmod(barBeats, 1.0);
  290. fTimeInfo.bbt.bar = static_cast<int32_t>(vstTimeInfo->ppqPos)/ppqPerBar + 1;
  291. fTimeInfo.bbt.beat = static_cast<int32_t>(barBeats-rest+1.0);
  292. fTimeInfo.bbt.tick = static_cast<int32_t>(rest*fTimeInfo.bbt.ticksPerBeat+0.5);
  293. fTimeInfo.bbt.beatsPerBar = static_cast<float>(vstTimeInfo->timeSigNumerator);
  294. fTimeInfo.bbt.beatType = static_cast<float>(vstTimeInfo->timeSigDenominator);
  295. }
  296. else
  297. {
  298. fTimeInfo.bbt.bar = 1;
  299. fTimeInfo.bbt.beat = 1;
  300. fTimeInfo.bbt.tick = 0;
  301. fTimeInfo.bbt.beatsPerBar = 4.0f;
  302. fTimeInfo.bbt.beatType = 4.0f;
  303. }
  304. fTimeInfo.bbt.barStartTick = fTimeInfo.bbt.ticksPerBeat*fTimeInfo.bbt.beatsPerBar*(fTimeInfo.bbt.bar-1);
  305. }
  306. if (fHandle != nullptr)
  307. fDescriptor->process(fHandle, const_cast<float**>(inputs), outputs, static_cast<uint32_t>(sampleFrames), fMidiEvents, fMidiEventCount);
  308. // TODO: MIDI Out
  309. fMidiEventCount = 0;
  310. }
  311. protected:
  312. // -------------------------------------------------------------------
  313. uint32_t handleGetBufferSize() const
  314. {
  315. return fBufferSize;
  316. }
  317. double handleGetSampleRate() const
  318. {
  319. return fSampleRate;
  320. }
  321. bool handleIsOffline() const
  322. {
  323. return false;
  324. }
  325. const NativeTimeInfo* handleGetTimeInfo() const
  326. {
  327. return &fTimeInfo;
  328. }
  329. bool handleWriteMidiEvent(const NativeMidiEvent* const event)
  330. {
  331. CARLA_SAFE_ASSERT_RETURN(fDescriptor->midiOuts > 0, false);
  332. CARLA_SAFE_ASSERT_RETURN(event != nullptr, false);
  333. CARLA_SAFE_ASSERT_RETURN(event->data[0] != 0, false);
  334. // reverse-find first free event, and put it there
  335. for (uint32_t i=(kMaxMidiEvents*2)-1; i > fMidiEventCount; --i)
  336. {
  337. if (fMidiEvents[i].data[0] == 0)
  338. {
  339. std::memcpy(&fMidiEvents[i], event, sizeof(NativeMidiEvent));
  340. return true;
  341. }
  342. }
  343. return false;
  344. }
  345. void handleUiParameterChanged(const uint32_t /*index*/, const float /*value*/) const
  346. {
  347. }
  348. void handleUiCustomDataChanged(const char* const /*key*/, const char* const /*value*/) const
  349. {
  350. }
  351. void handleUiClosed()
  352. {
  353. }
  354. const char* handleUiOpenFile(const bool /*isDir*/, const char* const /*title*/, const char* const /*filter*/) const
  355. {
  356. // TODO
  357. return nullptr;
  358. }
  359. const char* handleUiSaveFile(const bool /*isDir*/, const char* const /*title*/, const char* const /*filter*/) const
  360. {
  361. // TODO
  362. return nullptr;
  363. }
  364. intptr_t handleDispatcher(const NativeHostDispatcherOpcode opcode, const int32_t index, const intptr_t value, void* const ptr, const float opt)
  365. {
  366. carla_debug("NativePlugin::handleDispatcher(%i, %i, " P_INTPTR ", %p, %f)", opcode, index, value, ptr, opt);
  367. return 0;
  368. // unused for now
  369. (void)opcode; (void)index; (void)value; (void)ptr; (void)opt;
  370. }
  371. private:
  372. // VST stuff
  373. const audioMasterCallback fAudioMaster;
  374. AEffect* const fEffect;
  375. // Native data
  376. NativePluginHandle fHandle;
  377. NativeHostDescriptor fHost;
  378. const NativePluginDescriptor* const fDescriptor;
  379. // VST host data
  380. uint32_t fBufferSize;
  381. double fSampleRate;
  382. // Temporary data
  383. uint32_t fMidiEventCount;
  384. NativeMidiEvent fMidiEvents[kMaxMidiEvents*2];
  385. NativeTimeInfo fTimeInfo;
  386. ERect fVstRect;
  387. char* fStateChunk;
  388. SharedResourcePointer<ScopedJuceInitialiser_GUI> sJuceInitialiser;
  389. // -------------------------------------------------------------------
  390. #define handlePtr ((NativePlugin*)handle)
  391. static uint32_t host_get_buffer_size(NativeHostHandle handle)
  392. {
  393. return handlePtr->handleGetBufferSize();
  394. }
  395. static double host_get_sample_rate(NativeHostHandle handle)
  396. {
  397. return handlePtr->handleGetSampleRate();
  398. }
  399. static bool host_is_offline(NativeHostHandle handle)
  400. {
  401. return handlePtr->handleIsOffline();
  402. }
  403. static const NativeTimeInfo* host_get_time_info(NativeHostHandle handle)
  404. {
  405. return handlePtr->handleGetTimeInfo();
  406. }
  407. static bool host_write_midi_event(NativeHostHandle handle, const NativeMidiEvent* event)
  408. {
  409. return handlePtr->handleWriteMidiEvent(event);
  410. }
  411. static void host_ui_parameter_changed(NativeHostHandle handle, uint32_t index, float value)
  412. {
  413. handlePtr->handleUiParameterChanged(index, value);
  414. }
  415. static void host_ui_custom_data_changed(NativeHostHandle handle, const char* key, const char* value)
  416. {
  417. handlePtr->handleUiCustomDataChanged(key, value);
  418. }
  419. static void host_ui_closed(NativeHostHandle handle)
  420. {
  421. handlePtr->handleUiClosed();
  422. }
  423. static const char* host_ui_open_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
  424. {
  425. return handlePtr->handleUiOpenFile(isDir, title, filter);
  426. }
  427. static const char* host_ui_save_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
  428. {
  429. return handlePtr->handleUiSaveFile(isDir, title, filter);
  430. }
  431. static intptr_t host_dispatcher(NativeHostHandle handle, NativeHostDispatcherOpcode opcode, int32_t index, intptr_t value, void* ptr, float opt)
  432. {
  433. return handlePtr->handleDispatcher(opcode, index, value, ptr, opt);
  434. }
  435. #undef handlePtr
  436. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(NativePlugin)
  437. };
  438. // -----------------------------------------------------------------------
  439. struct VstObject {
  440. audioMasterCallback audioMaster;
  441. NativePlugin* plugin;
  442. };
  443. #ifdef VESTIGE_HEADER
  444. # define validObject effect != nullptr && effect->ptr3 != nullptr
  445. # define validPlugin effect != nullptr && effect->ptr3 != nullptr && ((VstObject*)effect->ptr3)->plugin != nullptr
  446. # define vstObjectPtr (VstObject*)effect->ptr3
  447. #else
  448. # define validObject effect != nullptr && effect->object != nullptr
  449. # define validPlugin effect != nullptr && effect->object != nullptr && ((VstObject*)effect->object)->plugin != nullptr
  450. # define vstObjectPtr (VstObject*)effect->object
  451. #endif
  452. #define pluginPtr (vstObjectPtr)->plugin
  453. static intptr_t vst_dispatcherCallback(AEffect* effect, int32_t opcode, int32_t index, intptr_t value, void* ptr, float opt)
  454. {
  455. // handle base opcodes
  456. switch (opcode)
  457. {
  458. case effOpen:
  459. if (VstObject* const obj = vstObjectPtr)
  460. {
  461. // this must always be valid
  462. CARLA_SAFE_ASSERT_RETURN(obj->audioMaster != nullptr, 0);
  463. // some hosts call effOpen twice
  464. CARLA_SAFE_ASSERT_RETURN(obj->plugin == nullptr, 1);
  465. audioMasterCallback audioMaster = (audioMasterCallback)obj->audioMaster;
  466. d_lastBufferSize = static_cast<uint32_t>(audioMaster(effect, audioMasterGetBlockSize, 0, 0, nullptr, 0.0f));
  467. d_lastSampleRate = static_cast<double>(audioMaster(effect, audioMasterGetSampleRate, 0, 0, nullptr, 0.0f));
  468. // some hosts are not ready at this point or return 0 buffersize/samplerate
  469. if (d_lastBufferSize == 0)
  470. d_lastBufferSize = 2048;
  471. if (d_lastSampleRate <= 0.0)
  472. d_lastSampleRate = 44100.0;
  473. const NativePluginDescriptor* pluginDesc = nullptr;
  474. #ifdef CARLA_PLUGIN_PATCHBAY
  475. const char* const pluginLabel = "carlapatchbay";
  476. #else
  477. const char* const pluginLabel = "carlarack";
  478. #endif
  479. PluginListManager& plm(PluginListManager::getInstance());
  480. for (LinkedList<const NativePluginDescriptor*>::Itenerator it = plm.descs.begin(); it.valid(); it.next())
  481. {
  482. const NativePluginDescriptor* const& tmpDesc(it.getValue());
  483. if (std::strcmp(tmpDesc->label, pluginLabel) == 0)
  484. {
  485. pluginDesc = tmpDesc;
  486. break;
  487. }
  488. }
  489. CARLA_SAFE_ASSERT_RETURN(pluginDesc != nullptr, 0);
  490. obj->plugin = new NativePlugin(audioMaster, effect, pluginDesc);
  491. return 1;
  492. }
  493. return 0;
  494. case effClose:
  495. if (VstObject* const obj = vstObjectPtr)
  496. {
  497. if (obj->plugin != nullptr)
  498. {
  499. delete obj->plugin;
  500. obj->plugin = nullptr;
  501. }
  502. #if 0
  503. /* This code invalidates the object created in VSTPluginMain
  504. * Probably not safe against all hosts */
  505. obj->audioMaster = nullptr;
  506. # ifdef VESTIGE_HEADER
  507. effect->ptr3 = nullptr;
  508. # else
  509. vstObjectPtr = nullptr;
  510. # endif
  511. delete obj;
  512. #endif
  513. return 1;
  514. }
  515. //delete effect;
  516. return 0;
  517. case effGetPlugCategory:
  518. #ifdef CARLA_PLUGIN_SYNTH
  519. return kPlugCategSynth;
  520. #else
  521. return kPlugCategEffect;
  522. #endif
  523. case effGetEffectName:
  524. if (ptr != nullptr)
  525. {
  526. #ifdef CARLA_PLUGIN_PATCHBAY
  527. std::strncpy((char*)ptr, "Carla-Patchbay", 64);
  528. #else
  529. std::strncpy((char*)ptr, "Carla-Rack", 64);
  530. #endif
  531. return 1;
  532. }
  533. return 0;
  534. case effGetVendorString:
  535. if (ptr != nullptr)
  536. {
  537. std::strncpy((char*)ptr, "falkTX", 64);
  538. return 1;
  539. }
  540. return 0;
  541. case effGetProductString:
  542. if (ptr != nullptr)
  543. {
  544. #ifdef CARLA_PLUGIN_PATCHBAY
  545. std::strncpy((char*)ptr, "CarlaPatchbay", 32);
  546. #else
  547. std::strncpy((char*)ptr, "CarlaRack", 32);
  548. #endif
  549. return 1;
  550. }
  551. return 0;
  552. case effGetVendorVersion:
  553. return CARLA_VERSION_HEX;
  554. case effGetVstVersion:
  555. return kVstVersion;
  556. };
  557. // handle advanced opcodes
  558. if (validPlugin)
  559. return pluginPtr->vst_dispatcher(opcode, index, value, ptr, opt);
  560. return 0;
  561. }
  562. static float vst_getParameterCallback(AEffect* effect, int32_t index)
  563. {
  564. if (validPlugin)
  565. return pluginPtr->vst_getParameter(index);
  566. return 0.0f;
  567. }
  568. static void vst_setParameterCallback(AEffect* effect, int32_t index, float value)
  569. {
  570. if (validPlugin)
  571. pluginPtr->vst_setParameter(index, value);
  572. }
  573. static void vst_processCallback(AEffect* effect, float** inputs, float** outputs, int32_t sampleFrames)
  574. {
  575. if (validPlugin)
  576. pluginPtr->vst_processReplacing(const_cast<const float**>(inputs), outputs, sampleFrames);
  577. }
  578. static void vst_processReplacingCallback(AEffect* effect, float** inputs, float** outputs, int32_t sampleFrames)
  579. {
  580. if (validPlugin)
  581. pluginPtr->vst_processReplacing(const_cast<const float**>(inputs), outputs, sampleFrames);
  582. }
  583. #undef pluginPtr
  584. #undef validObject
  585. #undef validPlugin
  586. #undef vstObjectPtr
  587. // -----------------------------------------------------------------------
  588. CARLA_EXPORT
  589. #if defined(CARLA_OS_WIN) || defined(CARLA_OS_MAC)
  590. const AEffect* VSTPluginMain(audioMasterCallback audioMaster);
  591. #else
  592. const AEffect* VSTPluginMain(audioMasterCallback audioMaster) asm ("main");
  593. #endif
  594. CARLA_EXPORT
  595. const AEffect* VSTPluginMain(audioMasterCallback audioMaster)
  596. {
  597. // old version
  598. if (audioMaster(nullptr, audioMasterVersion, 0, 0, nullptr, 0.0f) == 0)
  599. return nullptr;
  600. AEffect* const effect(new AEffect);
  601. std::memset(effect, 0, sizeof(AEffect));
  602. // vst fields
  603. effect->magic = kEffectMagic;
  604. #ifdef VESTIGE_HEADER
  605. int32_t* const version = (int32_t*)&effect->unknown1;
  606. *version = CARLA_VERSION_HEX;
  607. #else
  608. effect->version = CARLA_VERSION_HEX;
  609. #endif
  610. static const int32_t uniqueId = CCONST('C', 'r', 'l', 'a');
  611. #ifdef CARLA_PLUGIN_SYNTH
  612. # ifdef CARLA_PLUGIN_PATCHBAY
  613. effect->uniqueID = uniqueId+4;
  614. # else
  615. effect->uniqueID = uniqueId+3;
  616. # endif
  617. #else
  618. # ifdef CARLA_PLUGIN_PATCHBAY
  619. effect->uniqueID = uniqueId+2;
  620. # else
  621. effect->uniqueID = uniqueId+1;
  622. # endif
  623. #endif
  624. // plugin fields
  625. effect->numParams = 0;
  626. effect->numPrograms = 0;
  627. effect->numInputs = 2;
  628. effect->numOutputs = 2;
  629. // plugin flags
  630. effect->flags |= effFlagsCanReplacing;
  631. effect->flags |= effFlagsHasEditor;
  632. effect->flags |= effFlagsProgramChunks;
  633. #ifdef CARLA_PLUGIN_SYNTH
  634. effect->flags |= effFlagsIsSynth;
  635. #endif
  636. // static calls
  637. effect->dispatcher = vst_dispatcherCallback;
  638. effect->process = vst_processCallback;
  639. effect->getParameter = vst_getParameterCallback;
  640. effect->setParameter = vst_setParameterCallback;
  641. effect->processReplacing = vst_processReplacingCallback;
  642. // pointers
  643. VstObject* const obj(new VstObject());
  644. obj->audioMaster = audioMaster;
  645. obj->plugin = nullptr;
  646. #ifdef VESTIGE_HEADER
  647. effect->ptr3 = obj;
  648. #else
  649. effect->object = obj;
  650. #endif
  651. return effect;
  652. }
  653. // -----------------------------------------------------------------------