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.

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