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.

944 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. {
  384. // send current events
  385. hostCallback(audioMasterProcessEvents, 0, 0, &fMidiOutEvents, 0.0f);
  386. // clear
  387. fMidiOutEvents.numEvents = 0;
  388. }
  389. VstMidiEvent& vstMidiEvent(fMidiOutEvents.mdata[fMidiOutEvents.numEvents++]);
  390. vstMidiEvent.type = kVstMidiType;
  391. vstMidiEvent.byteSize = kVstMidiEventSize;
  392. uint8_t i=0;
  393. for (; i<event->size; ++i)
  394. vstMidiEvent.midiData[i] = static_cast<char>(event->data[i]);
  395. for (; i<4; ++i)
  396. vstMidiEvent.midiData[i] = 0;
  397. return true;
  398. }
  399. void handleUiParameterChanged(const uint32_t /*index*/, const float /*value*/) const
  400. {
  401. }
  402. void handleUiCustomDataChanged(const char* const /*key*/, const char* const /*value*/) const
  403. {
  404. }
  405. void handleUiClosed()
  406. {
  407. }
  408. const char* handleUiOpenFile(const bool /*isDir*/, const char* const /*title*/, const char* const /*filter*/) const
  409. {
  410. // TODO
  411. return nullptr;
  412. }
  413. const char* handleUiSaveFile(const bool /*isDir*/, const char* const /*title*/, const char* const /*filter*/) const
  414. {
  415. // TODO
  416. return nullptr;
  417. }
  418. intptr_t handleDispatcher(const NativeHostDispatcherOpcode opcode, const int32_t index, const intptr_t value, void* const ptr, const float opt)
  419. {
  420. carla_debug("NativePlugin::handleDispatcher(%i, %i, " P_INTPTR ", %p, %f)", opcode, index, value, ptr, opt);
  421. switch (opcode)
  422. {
  423. case NATIVE_HOST_OPCODE_NULL:
  424. case NATIVE_HOST_OPCODE_UPDATE_PARAMETER:
  425. case NATIVE_HOST_OPCODE_UPDATE_MIDI_PROGRAM:
  426. case NATIVE_HOST_OPCODE_RELOAD_PARAMETERS:
  427. case NATIVE_HOST_OPCODE_RELOAD_MIDI_PROGRAMS:
  428. case NATIVE_HOST_OPCODE_RELOAD_ALL:
  429. case NATIVE_HOST_OPCODE_UI_UNAVAILABLE:
  430. break;
  431. case NATIVE_HOST_OPCODE_HOST_IDLE:
  432. hostCallback(audioMasterIdle);
  433. break;
  434. }
  435. // unused for now
  436. return 0;
  437. (void)index; (void)value; (void)ptr; (void)opt;
  438. }
  439. private:
  440. // VST stuff
  441. const audioMasterCallback fAudioMaster;
  442. AEffect* const fEffect;
  443. // Native data
  444. NativePluginHandle fHandle;
  445. NativeHostDescriptor fHost;
  446. const NativePluginDescriptor* const fDescriptor;
  447. // VST host data
  448. uint32_t fBufferSize;
  449. double fSampleRate;
  450. // Temporary data
  451. bool fIsActive;
  452. uint32_t fMidiEventCount;
  453. NativeMidiEvent fMidiEvents[kMaxMidiEvents];
  454. NativeTimeInfo fTimeInfo;
  455. ERect fVstRect;
  456. // host callback
  457. intptr_t hostCallback(const int32_t opcode,
  458. const int32_t index = 0,
  459. const intptr_t value = 0,
  460. void* const ptr = nullptr,
  461. const float opt = 0.0f)
  462. {
  463. return fAudioMaster(fEffect, opcode, index, value, ptr, opt);
  464. }
  465. struct FixedVstEvents {
  466. int32_t numEvents;
  467. intptr_t reserved;
  468. VstEvent* data[kMaxMidiEvents];
  469. VstMidiEvent mdata[kMaxMidiEvents];
  470. FixedVstEvents()
  471. : numEvents(0),
  472. reserved(0)
  473. {
  474. for (uint32_t i=0; i<kMaxMidiEvents; ++i)
  475. data[i] = (VstEvent*)&mdata[i];
  476. carla_zeroStructs(mdata, kMaxMidiEvents);
  477. }
  478. CARLA_DECLARE_NON_COPY_STRUCT(FixedVstEvents);
  479. } fMidiOutEvents;
  480. char* fStateChunk;
  481. SharedResourcePointer<ScopedJuceInitialiser_GUI> sJuceInitialiser;
  482. // -------------------------------------------------------------------
  483. #define handlePtr ((NativePlugin*)handle)
  484. static uint32_t host_get_buffer_size(NativeHostHandle handle)
  485. {
  486. return handlePtr->handleGetBufferSize();
  487. }
  488. static double host_get_sample_rate(NativeHostHandle handle)
  489. {
  490. return handlePtr->handleGetSampleRate();
  491. }
  492. static bool host_is_offline(NativeHostHandle handle)
  493. {
  494. return handlePtr->handleIsOffline();
  495. }
  496. static const NativeTimeInfo* host_get_time_info(NativeHostHandle handle)
  497. {
  498. return handlePtr->handleGetTimeInfo();
  499. }
  500. static bool host_write_midi_event(NativeHostHandle handle, const NativeMidiEvent* event)
  501. {
  502. return handlePtr->handleWriteMidiEvent(event);
  503. }
  504. static void host_ui_parameter_changed(NativeHostHandle handle, uint32_t index, float value)
  505. {
  506. handlePtr->handleUiParameterChanged(index, value);
  507. }
  508. static void host_ui_custom_data_changed(NativeHostHandle handle, const char* key, const char* value)
  509. {
  510. handlePtr->handleUiCustomDataChanged(key, value);
  511. }
  512. static void host_ui_closed(NativeHostHandle handle)
  513. {
  514. handlePtr->handleUiClosed();
  515. }
  516. static const char* host_ui_open_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
  517. {
  518. return handlePtr->handleUiOpenFile(isDir, title, filter);
  519. }
  520. static const char* host_ui_save_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
  521. {
  522. return handlePtr->handleUiSaveFile(isDir, title, filter);
  523. }
  524. static intptr_t host_dispatcher(NativeHostHandle handle, NativeHostDispatcherOpcode opcode, int32_t index, intptr_t value, void* ptr, float opt)
  525. {
  526. return handlePtr->handleDispatcher(opcode, index, value, ptr, opt);
  527. }
  528. #undef handlePtr
  529. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(NativePlugin)
  530. };
  531. // -----------------------------------------------------------------------
  532. struct VstObject {
  533. audioMasterCallback audioMaster;
  534. NativePlugin* plugin;
  535. };
  536. #ifdef VESTIGE_HEADER
  537. # define validObject effect != nullptr && effect->ptr3 != nullptr
  538. # define validPlugin effect != nullptr && effect->ptr3 != nullptr && ((VstObject*)effect->ptr3)->plugin != nullptr
  539. # define vstObjectPtr (VstObject*)effect->ptr3
  540. #else
  541. # define validObject effect != nullptr && effect->object != nullptr
  542. # define validPlugin effect != nullptr && effect->object != nullptr && ((VstObject*)effect->object)->plugin != nullptr
  543. # define vstObjectPtr (VstObject*)effect->object
  544. #endif
  545. #define pluginPtr (vstObjectPtr)->plugin
  546. static intptr_t vst_dispatcherCallback(AEffect* effect, int32_t opcode, int32_t index, intptr_t value, void* ptr, float opt)
  547. {
  548. // handle base opcodes
  549. switch (opcode)
  550. {
  551. case effOpen:
  552. if (VstObject* const obj = vstObjectPtr)
  553. {
  554. // this must always be valid
  555. CARLA_SAFE_ASSERT_RETURN(obj->audioMaster != nullptr, 0);
  556. // some hosts call effOpen twice
  557. CARLA_SAFE_ASSERT_RETURN(obj->plugin == nullptr, 1);
  558. audioMasterCallback audioMaster = (audioMasterCallback)obj->audioMaster;
  559. d_lastBufferSize = static_cast<uint32_t>(audioMaster(effect, audioMasterGetBlockSize, 0, 0, nullptr, 0.0f));
  560. d_lastSampleRate = static_cast<double>(audioMaster(effect, audioMasterGetSampleRate, 0, 0, nullptr, 0.0f));
  561. // some hosts are not ready at this point or return 0 buffersize/samplerate
  562. if (d_lastBufferSize == 0)
  563. d_lastBufferSize = 2048;
  564. if (d_lastSampleRate <= 0.0)
  565. d_lastSampleRate = 44100.0;
  566. const NativePluginDescriptor* pluginDesc = nullptr;
  567. #if CARLA_PLUGIN_PATCHBAY
  568. const char* const pluginLabel = "carlapatchbay";
  569. #else
  570. const char* const pluginLabel = "carlarack";
  571. #endif
  572. PluginListManager& plm(PluginListManager::getInstance());
  573. for (LinkedList<const NativePluginDescriptor*>::Itenerator it = plm.descs.begin2(); it.valid(); it.next())
  574. {
  575. const NativePluginDescriptor* const& tmpDesc(it.getValue());
  576. if (std::strcmp(tmpDesc->label, pluginLabel) == 0)
  577. {
  578. pluginDesc = tmpDesc;
  579. break;
  580. }
  581. }
  582. CARLA_SAFE_ASSERT_RETURN(pluginDesc != nullptr, 0);
  583. obj->plugin = new NativePlugin(audioMaster, effect, pluginDesc);
  584. return 1;
  585. }
  586. return 0;
  587. case effClose:
  588. if (VstObject* const obj = vstObjectPtr)
  589. {
  590. NativePlugin* const plugin(obj->plugin);
  591. if (plugin != nullptr)
  592. {
  593. obj->plugin = nullptr;
  594. delete plugin;
  595. }
  596. #if 0
  597. /* This code invalidates the object created in VSTPluginMain
  598. * Probably not safe against all hosts */
  599. obj->audioMaster = nullptr;
  600. # ifdef VESTIGE_HEADER
  601. effect->ptr3 = nullptr;
  602. # else
  603. vstObjectPtr = nullptr;
  604. # endif
  605. delete obj;
  606. #endif
  607. return 1;
  608. }
  609. //delete effect;
  610. return 0;
  611. case effGetPlugCategory:
  612. #if CARLA_PLUGIN_SYNTH
  613. return kPlugCategSynth;
  614. #else
  615. return kPlugCategEffect;
  616. #endif
  617. case effGetEffectName:
  618. if (char* const cptr = (char*)ptr)
  619. {
  620. #if CARLA_PLUGIN_PATCHBAY
  621. # if CARLA_PLUGIN_SYNTH
  622. std::strncpy(cptr, "Carla-Patchbay", 32);
  623. # else
  624. std::strncpy(cptr, "Carla-PatchbayFX", 32);
  625. # endif
  626. #else
  627. # if CARLA_PLUGIN_SYNTH
  628. std::strncpy(cptr, "Carla-Rack", 32);
  629. # else
  630. std::strncpy(cptr, "Carla-RackFX", 32);
  631. # endif
  632. #endif
  633. return 1;
  634. }
  635. return 0;
  636. case effGetVendorString:
  637. if (char* const cptr = (char*)ptr)
  638. {
  639. std::strncpy(cptr, "falkTX", 32);
  640. return 1;
  641. }
  642. return 0;
  643. case effGetProductString:
  644. if (char* const cptr = (char*)ptr)
  645. {
  646. #if CARLA_PLUGIN_PATCHBAY
  647. # if CARLA_PLUGIN_SYNTH
  648. std::strncpy(cptr, "CarlaPatchbay", 32);
  649. # else
  650. std::strncpy(cptr, "CarlaPatchbayFX", 32);
  651. # endif
  652. #else
  653. # if CARLA_PLUGIN_SYNTH
  654. std::strncpy(cptr, "CarlaRack", 32);
  655. # else
  656. std::strncpy(cptr, "CarlaRackFX", 32);
  657. # endif
  658. #endif
  659. return 1;
  660. }
  661. return 0;
  662. case effGetVendorVersion:
  663. return CARLA_VERSION_HEX;
  664. case effGetVstVersion:
  665. return kVstVersion;
  666. };
  667. // handle advanced opcodes
  668. if (validPlugin)
  669. return pluginPtr->vst_dispatcher(opcode, index, value, ptr, opt);
  670. return 0;
  671. }
  672. static float vst_getParameterCallback(AEffect* effect, int32_t index)
  673. {
  674. if (validPlugin)
  675. return pluginPtr->vst_getParameter(index);
  676. return 0.0f;
  677. }
  678. static void vst_setParameterCallback(AEffect* effect, int32_t index, float value)
  679. {
  680. if (validPlugin)
  681. pluginPtr->vst_setParameter(index, value);
  682. }
  683. static void vst_processCallback(AEffect* effect, float** inputs, float** outputs, int32_t sampleFrames)
  684. {
  685. if (validPlugin)
  686. pluginPtr->vst_processReplacing(const_cast<const float**>(inputs), outputs, sampleFrames);
  687. }
  688. static void vst_processReplacingCallback(AEffect* effect, float** inputs, float** outputs, int32_t sampleFrames)
  689. {
  690. if (validPlugin)
  691. pluginPtr->vst_processReplacing(const_cast<const float**>(inputs), outputs, sampleFrames);
  692. }
  693. #undef pluginPtr
  694. #undef validObject
  695. #undef validPlugin
  696. #undef vstObjectPtr
  697. // -----------------------------------------------------------------------
  698. CARLA_EXPORT
  699. #if defined(CARLA_OS_WIN) || defined(CARLA_OS_MAC)
  700. const AEffect* VSTPluginMain(audioMasterCallback audioMaster);
  701. #else
  702. const AEffect* VSTPluginMain(audioMasterCallback audioMaster) asm ("main");
  703. #endif
  704. CARLA_EXPORT
  705. const AEffect* VSTPluginMain(audioMasterCallback audioMaster)
  706. {
  707. // old version
  708. if (audioMaster(nullptr, audioMasterVersion, 0, 0, nullptr, 0.0f) == 0)
  709. return nullptr;
  710. AEffect* const effect(new AEffect);
  711. std::memset(effect, 0, sizeof(AEffect));
  712. // vst fields
  713. effect->magic = kEffectMagic;
  714. #ifdef VESTIGE_HEADER
  715. int32_t* const version = (int32_t*)&effect->unknown1;
  716. *version = CARLA_VERSION_HEX;
  717. #else
  718. effect->version = CARLA_VERSION_HEX;
  719. #endif
  720. static const int32_t uniqueId = CCONST('C', 'r', 'l', 'a');
  721. #if CARLA_PLUGIN_SYNTH
  722. # if CARLA_PLUGIN_PATCHBAY
  723. effect->uniqueID = uniqueId+4;
  724. # else
  725. effect->uniqueID = uniqueId+3;
  726. # endif
  727. #else
  728. # if CARLA_PLUGIN_PATCHBAY
  729. effect->uniqueID = uniqueId+2;
  730. # else
  731. effect->uniqueID = uniqueId+1;
  732. # endif
  733. #endif
  734. // plugin fields
  735. effect->numParams = 0;
  736. effect->numPrograms = 0;
  737. effect->numInputs = 2;
  738. effect->numOutputs = 2;
  739. // plugin flags
  740. effect->flags |= effFlagsCanReplacing;
  741. effect->flags |= effFlagsHasEditor;
  742. effect->flags |= effFlagsProgramChunks;
  743. #if CARLA_PLUGIN_SYNTH
  744. effect->flags |= effFlagsIsSynth;
  745. #endif
  746. // static calls
  747. effect->dispatcher = vst_dispatcherCallback;
  748. effect->process = vst_processCallback;
  749. effect->getParameter = vst_getParameterCallback;
  750. effect->setParameter = vst_setParameterCallback;
  751. effect->processReplacing = vst_processReplacingCallback;
  752. // pointers
  753. VstObject* const obj(new VstObject());
  754. obj->audioMaster = audioMaster;
  755. obj->plugin = nullptr;
  756. #ifdef VESTIGE_HEADER
  757. effect->ptr3 = obj;
  758. #else
  759. effect->object = obj;
  760. #endif
  761. return effect;
  762. }
  763. // -----------------------------------------------------------------------