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.

940 lines
28KB

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