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.

872 lines
26KB

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