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.

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