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.

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