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. {
  76. fHost.handle = this;
  77. fHost.uiName = carla_strdup("CarlaVST");
  78. fHost.uiParentId = 0;
  79. // find resource dir
  80. using juce::File;
  81. File curExe = File::getSpecialLocation(File::currentExecutableFile).getLinkedTarget();
  82. File resDir = curExe.getSiblingFile("carla-resources");
  83. if (! resDir.exists())
  84. resDir = curExe.getSiblingFile("resources");
  85. if (! resDir.exists())
  86. resDir = File("/usr/share/carla/resources/");
  87. fHost.resourceDir = carla_strdup(resDir.getFullPathName().toRawUTF8());
  88. fHost.get_buffer_size = host_get_buffer_size;
  89. fHost.get_sample_rate = host_get_sample_rate;
  90. fHost.is_offline = host_is_offline;
  91. fHost.get_time_info = host_get_time_info;
  92. fHost.write_midi_event = host_write_midi_event;
  93. fHost.ui_parameter_changed = host_ui_parameter_changed;
  94. fHost.ui_custom_data_changed = host_ui_custom_data_changed;
  95. fHost.ui_closed = host_ui_closed;
  96. fHost.ui_open_file = host_ui_open_file;
  97. fHost.ui_save_file = host_ui_save_file;
  98. fHost.dispatcher = host_dispatcher;
  99. fVstRect.top = 0;
  100. fVstRect.left = 0;
  101. fVstRect.bottom = 512;
  102. fVstRect.right = 740;
  103. init();
  104. }
  105. ~NativePlugin()
  106. {
  107. if (fIsActive)
  108. {
  109. // host has not de-activated the plugin yet, nasty!
  110. fIsActive = false;
  111. if (fDescriptor->deactivate != nullptr)
  112. fDescriptor->deactivate(fHandle);
  113. }
  114. if (fDescriptor->cleanup != nullptr && fHandle != nullptr)
  115. fDescriptor->cleanup(fHandle);
  116. fHandle = nullptr;
  117. if (fStateChunk != nullptr)
  118. {
  119. std::free(fStateChunk);
  120. fStateChunk = nullptr;
  121. }
  122. if (fHost.uiName != nullptr)
  123. {
  124. delete[] fHost.uiName;
  125. fHost.uiName = nullptr;
  126. }
  127. if (fHost.resourceDir != nullptr)
  128. {
  129. delete[] fHost.resourceDir;
  130. fHost.resourceDir = nullptr;
  131. }
  132. }
  133. bool init()
  134. {
  135. if (fDescriptor->instantiate == nullptr || fDescriptor->process == nullptr)
  136. {
  137. carla_stderr("Plugin is missing something...");
  138. return false;
  139. }
  140. fHandle = fDescriptor->instantiate(&fHost);
  141. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr, false);
  142. carla_zeroStructs(fMidiEvents, kMaxMidiEvents);
  143. carla_zeroStruct(fTimeInfo);
  144. return true;
  145. }
  146. // -------------------------------------------------------------------
  147. intptr_t vst_dispatcher(const int32_t opcode, const int32_t /*index*/, const intptr_t value, void* const ptr, const float opt)
  148. {
  149. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr, 0);
  150. intptr_t ret = 0;
  151. switch (opcode)
  152. {
  153. case effSetSampleRate:
  154. if (carla_isEqual(fSampleRate, static_cast<double>(opt)))
  155. return 0;
  156. fSampleRate = opt;
  157. if (fDescriptor->dispatcher != nullptr)
  158. fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_SAMPLE_RATE_CHANGED, 0, 0, nullptr, opt);
  159. break;
  160. case effSetBlockSize:
  161. if (fBufferSize == static_cast<uint32_t>(value))
  162. return 0;
  163. fBufferSize = static_cast<uint32_t>(value);
  164. if (fDescriptor->dispatcher != nullptr)
  165. fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_BUFFER_SIZE_CHANGED, 0, value, nullptr, 0.0f);
  166. break;
  167. case effMainsChanged:
  168. if (value != 0)
  169. {
  170. fMidiEventCount = 0;
  171. carla_zeroStruct(fTimeInfo);
  172. // tell host we want MIDI events
  173. hostCallback(audioMasterWantMidi);
  174. // deactivate for possible changes
  175. if (fDescriptor->deactivate != nullptr && fIsActive)
  176. fDescriptor->deactivate(fHandle);
  177. // check if something changed
  178. const uint32_t bufferSize = static_cast<uint32_t>(hostCallback(audioMasterGetBlockSize));
  179. const double sampleRate = static_cast<double>(hostCallback(audioMasterGetSampleRate));
  180. if (bufferSize != 0 && fBufferSize != bufferSize)
  181. {
  182. fBufferSize = bufferSize;
  183. if (fDescriptor->dispatcher != nullptr)
  184. fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_BUFFER_SIZE_CHANGED, 0, (int32_t)value, nullptr, 0.0f);
  185. }
  186. if (sampleRate != 0.0 && carla_isNotEqual(fSampleRate, sampleRate))
  187. {
  188. fSampleRate = sampleRate;
  189. if (fDescriptor->dispatcher != nullptr)
  190. fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_SAMPLE_RATE_CHANGED, 0, 0, nullptr, (float)sampleRate);
  191. }
  192. if (fDescriptor->activate != nullptr)
  193. fDescriptor->activate(fHandle);
  194. fIsActive = true;
  195. }
  196. else
  197. {
  198. CARLA_SAFE_ASSERT_BREAK(fIsActive);
  199. if (fDescriptor->deactivate != nullptr)
  200. fDescriptor->deactivate(fHandle);
  201. fIsActive = false;
  202. }
  203. break;
  204. case effEditGetRect:
  205. *(ERect**)ptr = &fVstRect;
  206. ret = 1;
  207. break;
  208. case effEditOpen:
  209. if (fDescriptor->ui_show != nullptr)
  210. {
  211. char strBuf[0xff+1];
  212. strBuf[0xff] = '\0';
  213. std::snprintf(strBuf, 0xff, P_INTPTR, (intptr_t)ptr);
  214. // set CARLA_PLUGIN_EMBED_WINID for external process
  215. carla_setenv("CARLA_PLUGIN_EMBED_WINID", strBuf);
  216. // show UI now
  217. fDescriptor->ui_show(fHandle, true);
  218. // reset CARLA_PLUGIN_EMBED_WINID just in case
  219. carla_setenv("CARLA_PLUGIN_EMBED_WINID", "0");
  220. ret = 1;
  221. }
  222. break;
  223. case effEditClose:
  224. if (fDescriptor->ui_show != nullptr)
  225. {
  226. fDescriptor->ui_show(fHandle, false);
  227. ret = 1;
  228. }
  229. break;
  230. case effEditIdle:
  231. if (fDescriptor->ui_idle != nullptr)
  232. fDescriptor->ui_idle(fHandle);
  233. break;
  234. case effGetChunk:
  235. if (ptr == nullptr || fDescriptor->get_state == nullptr)
  236. return 0;
  237. if (fStateChunk != nullptr)
  238. std::free(fStateChunk);
  239. fStateChunk = fDescriptor->get_state(fHandle);
  240. if (fStateChunk == nullptr)
  241. return 0;
  242. ret = static_cast<intptr_t>(std::strlen(fStateChunk)+1);
  243. *(void**)ptr = fStateChunk;
  244. break;
  245. case effSetChunk:
  246. if (value <= 0 || fDescriptor->set_state == nullptr)
  247. return 0;
  248. if (value == 1)
  249. return 1;
  250. if (const char* const state = (const char*)ptr)
  251. {
  252. fDescriptor->set_state(fHandle, state);
  253. ret = 1;
  254. }
  255. break;
  256. case effProcessEvents:
  257. if (! fIsActive)
  258. {
  259. // host has not activated the plugin yet, nasty!
  260. vst_dispatcher(effMainsChanged, 0, 1, nullptr, 0.0f);
  261. }
  262. if (const VstEvents* const events = (const VstEvents*)ptr)
  263. {
  264. if (events->numEvents == 0)
  265. break;
  266. for (int i=0, count=events->numEvents; i < count; ++i)
  267. {
  268. const VstMidiEvent* const vstMidiEvent((const VstMidiEvent*)events->events[i]);
  269. if (vstMidiEvent == nullptr)
  270. break;
  271. if (vstMidiEvent->type != kVstMidiType || vstMidiEvent->deltaFrames < 0)
  272. continue;
  273. if (fMidiEventCount >= kMaxMidiEvents)
  274. break;
  275. const uint32_t j(fMidiEventCount++);
  276. fMidiEvents[j].port = 0;
  277. fMidiEvents[j].time = static_cast<uint32_t>(vstMidiEvent->deltaFrames);
  278. fMidiEvents[j].size = 3;
  279. for (uint32_t k=0; k<3; ++k)
  280. fMidiEvents[j].data[k] = static_cast<uint8_t>(vstMidiEvent->midiData[k]);
  281. }
  282. }
  283. break;
  284. case effCanDo:
  285. if (const char* const canDo = (const char*)ptr)
  286. {
  287. if (std::strcmp(canDo, "receiveVstEvents") == 0)
  288. return 1;
  289. if (std::strcmp(canDo, "receiveVstMidiEvent") == 0)
  290. return 1;
  291. if (std::strcmp(canDo, "receiveVstTimeInfo") == 0)
  292. return 1;
  293. if (std::strcmp(canDo, "sendVstEvents") == 0)
  294. return 1;
  295. if (std::strcmp(canDo, "sendVstMidiEvent") == 0)
  296. return 1;
  297. }
  298. break;
  299. }
  300. return ret;
  301. }
  302. float vst_getParameter(const int32_t /*index*/)
  303. {
  304. return 0.0f;
  305. }
  306. void vst_setParameter(const int32_t /*index*/, const float /*value*/)
  307. {
  308. }
  309. void vst_processReplacing(const float** const inputs, float** const outputs, const int32_t sampleFrames)
  310. {
  311. if (sampleFrames <= 0)
  312. return;
  313. if (! fIsActive)
  314. {
  315. // host has not activated the plugin yet, nasty!
  316. vst_dispatcher(effMainsChanged, 0, 1, nullptr, 0.0f);
  317. }
  318. static const int kWantVstTimeFlags(kVstTransportPlaying|kVstPpqPosValid|kVstTempoValid|kVstTimeSigValid);
  319. if (const VstTimeInfo* const vstTimeInfo = (const VstTimeInfo*)hostCallback(audioMasterGetTime, 0, kWantVstTimeFlags))
  320. {
  321. fTimeInfo.frame = static_cast<uint64_t>(vstTimeInfo->samplePos);
  322. fTimeInfo.playing = (vstTimeInfo->flags & kVstTransportPlaying);
  323. fTimeInfo.bbt.valid = ((vstTimeInfo->flags & kVstTempoValid) != 0 || (vstTimeInfo->flags & kVstTimeSigValid) != 0);
  324. // ticksPerBeat is not possible with VST
  325. fTimeInfo.bbt.ticksPerBeat = 960.0;
  326. if (vstTimeInfo->flags & kVstTempoValid)
  327. fTimeInfo.bbt.beatsPerMinute = vstTimeInfo->tempo;
  328. else
  329. fTimeInfo.bbt.beatsPerMinute = 120.0;
  330. if (vstTimeInfo->flags & (kVstPpqPosValid|kVstTimeSigValid))
  331. {
  332. const int ppqPerBar = vstTimeInfo->timeSigNumerator * 4 / vstTimeInfo->timeSigDenominator;
  333. const double barBeats = (std::fmod(vstTimeInfo->ppqPos, ppqPerBar) / ppqPerBar) * vstTimeInfo->timeSigDenominator;
  334. const double rest = std::fmod(barBeats, 1.0);
  335. fTimeInfo.bbt.bar = static_cast<int32_t>(vstTimeInfo->ppqPos)/ppqPerBar + 1;
  336. fTimeInfo.bbt.beat = static_cast<int32_t>(barBeats-rest+1.0);
  337. fTimeInfo.bbt.tick = static_cast<int32_t>(rest*fTimeInfo.bbt.ticksPerBeat+0.5);
  338. fTimeInfo.bbt.beatsPerBar = static_cast<float>(vstTimeInfo->timeSigNumerator);
  339. fTimeInfo.bbt.beatType = static_cast<float>(vstTimeInfo->timeSigDenominator);
  340. }
  341. else
  342. {
  343. fTimeInfo.bbt.bar = 1;
  344. fTimeInfo.bbt.beat = 1;
  345. fTimeInfo.bbt.tick = 0;
  346. fTimeInfo.bbt.beatsPerBar = 4.0f;
  347. fTimeInfo.bbt.beatType = 4.0f;
  348. }
  349. fTimeInfo.bbt.barStartTick = fTimeInfo.bbt.ticksPerBeat*fTimeInfo.bbt.beatsPerBar*(fTimeInfo.bbt.bar-1);
  350. }
  351. fMidiOutEvents.numEvents = 0;
  352. if (fHandle != nullptr)
  353. // FIXME
  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_zeroStructs(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.begin2(); 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. // -----------------------------------------------------------------------