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.

847 lines
25KB

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