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.

898 lines
27KB

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