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.

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