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.

941 lines
28KB

  1. /*
  2. * Carla Native Plugins
  3. * Copyright (C) 2013-2014 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #ifndef CARLA_PLUGIN_PATCHBAY
  18. # error CARLA_PLUGIN_PATCHBAY undefined
  19. #endif
  20. #ifndef CARLA_PLUGIN_SYNTH
  21. # error CARLA_PLUGIN_SYNTH undefined
  22. #endif
  23. #define CARLA_NATIVE_PLUGIN_VST
  24. #include "carla-base.cpp"
  25. #include "CarlaMathUtils.hpp"
  26. #include "juce_core.h"
  27. #if defined(CARLA_OS_MAC) || defined(CARLA_OS_WIN)
  28. # include "juce_gui_basics.h"
  29. #else
  30. namespace juce {
  31. # include "juce_events/messages/juce_Initialisation.h"
  32. } // namespace juce
  33. #endif
  34. #ifdef VESTIGE_HEADER
  35. # include "vestige/aeffectx.h"
  36. #define effFlagsProgramChunks (1 << 5)
  37. #define effGetParamLabel 6
  38. #define effGetChunk 23
  39. #define effSetChunk 24
  40. #define effGetPlugCategory 35
  41. #define kPlugCategEffect 1
  42. #define kPlugCategSynth 2
  43. #define kVstVersion 2400
  44. struct ERect {
  45. int16_t top, left, bottom, right;
  46. };
  47. #else
  48. # include "vst2/aeffectx.h"
  49. #endif
  50. using juce::ScopedJuceInitialiser_GUI;
  51. using juce::SharedResourcePointer;
  52. static uint32_t d_lastBufferSize = 0;
  53. static double d_lastSampleRate = 0.0;
  54. static const int32_t kVstMidiEventSize = static_cast<int32_t>(sizeof(VstMidiEvent));
  55. // -----------------------------------------------------------------------
  56. class NativePlugin
  57. {
  58. public:
  59. static const uint32_t kMaxMidiEvents = 512;
  60. NativePlugin(const audioMasterCallback audioMaster, AEffect* const effect, const NativePluginDescriptor* desc)
  61. : fAudioMaster(audioMaster),
  62. fEffect(effect),
  63. fHandle(nullptr),
  64. fHost(),
  65. fDescriptor(desc),
  66. fBufferSize(d_lastBufferSize),
  67. fSampleRate(d_lastSampleRate),
  68. fIsActive(false),
  69. fMidiEventCount(0),
  70. fTimeInfo(),
  71. fVstRect(),
  72. fMidiOutEvents(),
  73. fStateChunk(nullptr),
  74. sJuceInitialiser(),
  75. leakDetector_NativePlugin()
  76. {
  77. fHost.handle = this;
  78. fHost.uiName = carla_strdup("CarlaVST");
  79. fHost.uiParentId = 0;
  80. // find resource dir
  81. using juce::File;
  82. File curExe = File::getSpecialLocation(File::currentExecutableFile).getLinkedTarget();
  83. File resDir = curExe.getSiblingFile("carla-resources");
  84. if (! resDir.exists())
  85. resDir = curExe.getSiblingFile("resources");
  86. if (! resDir.exists())
  87. resDir = File("/usr/share/carla/resources/");
  88. fHost.resourceDir = carla_strdup(resDir.getFullPathName().toRawUTF8());
  89. fHost.get_buffer_size = host_get_buffer_size;
  90. fHost.get_sample_rate = host_get_sample_rate;
  91. fHost.is_offline = host_is_offline;
  92. fHost.get_time_info = host_get_time_info;
  93. fHost.write_midi_event = host_write_midi_event;
  94. fHost.ui_parameter_changed = host_ui_parameter_changed;
  95. fHost.ui_custom_data_changed = host_ui_custom_data_changed;
  96. fHost.ui_closed = host_ui_closed;
  97. fHost.ui_open_file = host_ui_open_file;
  98. fHost.ui_save_file = host_ui_save_file;
  99. fHost.dispatcher = host_dispatcher;
  100. fVstRect.top = 0;
  101. fVstRect.left = 0;
  102. fVstRect.bottom = 512;
  103. fVstRect.right = 740;
  104. init();
  105. }
  106. ~NativePlugin()
  107. {
  108. if (fIsActive)
  109. {
  110. // host has not de-activated the plugin yet, nasty!
  111. fIsActive = false;
  112. if (fDescriptor->deactivate != nullptr)
  113. fDescriptor->deactivate(fHandle);
  114. }
  115. if (fDescriptor->cleanup != nullptr && fHandle != nullptr)
  116. fDescriptor->cleanup(fHandle);
  117. fHandle = nullptr;
  118. if (fStateChunk != nullptr)
  119. {
  120. std::free(fStateChunk);
  121. fStateChunk = nullptr;
  122. }
  123. if (fHost.uiName != nullptr)
  124. {
  125. delete[] fHost.uiName;
  126. fHost.uiName = nullptr;
  127. }
  128. if (fHost.resourceDir != nullptr)
  129. {
  130. delete[] fHost.resourceDir;
  131. fHost.resourceDir = nullptr;
  132. }
  133. }
  134. bool init()
  135. {
  136. if (fDescriptor->instantiate == nullptr || fDescriptor->process == nullptr)
  137. {
  138. carla_stderr("Plugin is missing something...");
  139. return false;
  140. }
  141. fHandle = fDescriptor->instantiate(&fHost);
  142. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr, false);
  143. carla_zeroStructs(fMidiEvents, kMaxMidiEvents);
  144. carla_zeroStruct(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_isEqual(fSampleRate, static_cast<double>(opt)))
  156. return 0;
  157. fSampleRate = opt;
  158. if (fDescriptor->dispatcher != nullptr)
  159. fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_SAMPLE_RATE_CHANGED, 0, 0, nullptr, opt);
  160. break;
  161. case effSetBlockSize:
  162. if (fBufferSize == static_cast<uint32_t>(value))
  163. return 0;
  164. fBufferSize = static_cast<uint32_t>(value);
  165. if (fDescriptor->dispatcher != nullptr)
  166. fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_BUFFER_SIZE_CHANGED, 0, value, nullptr, 0.0f);
  167. break;
  168. case effMainsChanged:
  169. if (value != 0)
  170. {
  171. fMidiEventCount = 0;
  172. carla_zeroStruct(fTimeInfo);
  173. // tell host we want MIDI events
  174. hostCallback(audioMasterWantMidi);
  175. // deactivate for possible changes
  176. if (fDescriptor->deactivate != nullptr && fIsActive)
  177. fDescriptor->deactivate(fHandle);
  178. // check if something changed
  179. const uint32_t bufferSize = static_cast<uint32_t>(hostCallback(audioMasterGetBlockSize));
  180. const double sampleRate = static_cast<double>(hostCallback(audioMasterGetSampleRate));
  181. if (bufferSize != 0 && fBufferSize != bufferSize)
  182. {
  183. fBufferSize = bufferSize;
  184. if (fDescriptor->dispatcher != nullptr)
  185. fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_BUFFER_SIZE_CHANGED, 0, (int32_t)value, nullptr, 0.0f);
  186. }
  187. if (sampleRate != 0.0 && carla_isNotEqual(fSampleRate, sampleRate))
  188. {
  189. fSampleRate = sampleRate;
  190. if (fDescriptor->dispatcher != nullptr)
  191. fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_SAMPLE_RATE_CHANGED, 0, 0, nullptr, (float)sampleRate);
  192. }
  193. if (fDescriptor->activate != nullptr)
  194. fDescriptor->activate(fHandle);
  195. fIsActive = true;
  196. }
  197. else
  198. {
  199. CARLA_SAFE_ASSERT_BREAK(fIsActive);
  200. if (fDescriptor->deactivate != nullptr)
  201. fDescriptor->deactivate(fHandle);
  202. fIsActive = false;
  203. }
  204. break;
  205. case effEditGetRect:
  206. *(ERect**)ptr = &fVstRect;
  207. ret = 1;
  208. break;
  209. case effEditOpen:
  210. if (fDescriptor->ui_show != nullptr)
  211. {
  212. char strBuf[0xff+1];
  213. strBuf[0xff] = '\0';
  214. std::snprintf(strBuf, 0xff, P_INTPTR, (intptr_t)ptr);
  215. // set CARLA_PLUGIN_EMBED_WINID for external process
  216. carla_setenv("CARLA_PLUGIN_EMBED_WINID", strBuf);
  217. // show UI now
  218. fDescriptor->ui_show(fHandle, true);
  219. // reset CARLA_PLUGIN_EMBED_WINID just in case
  220. carla_setenv("CARLA_PLUGIN_EMBED_WINID", "0");
  221. ret = 1;
  222. }
  223. break;
  224. case effEditClose:
  225. if (fDescriptor->ui_show != nullptr)
  226. {
  227. fDescriptor->ui_show(fHandle, false);
  228. ret = 1;
  229. }
  230. break;
  231. case effEditIdle:
  232. if (fDescriptor->ui_idle != nullptr)
  233. fDescriptor->ui_idle(fHandle);
  234. break;
  235. case effGetChunk:
  236. if (ptr == nullptr || fDescriptor->get_state == nullptr)
  237. return 0;
  238. if (fStateChunk != nullptr)
  239. std::free(fStateChunk);
  240. fStateChunk = fDescriptor->get_state(fHandle);
  241. if (fStateChunk == nullptr)
  242. return 0;
  243. ret = static_cast<intptr_t>(std::strlen(fStateChunk)+1);
  244. *(void**)ptr = fStateChunk;
  245. break;
  246. case effSetChunk:
  247. if (value <= 0 || fDescriptor->set_state == nullptr)
  248. return 0;
  249. if (value == 1)
  250. return 1;
  251. if (const char* const state = (const char*)ptr)
  252. {
  253. fDescriptor->set_state(fHandle, state);
  254. ret = 1;
  255. }
  256. break;
  257. case effProcessEvents:
  258. if (! fIsActive)
  259. {
  260. // host has not activated the plugin yet, nasty!
  261. vst_dispatcher(effMainsChanged, 0, 1, nullptr, 0.0f);
  262. }
  263. if (const VstEvents* const events = (const VstEvents*)ptr)
  264. {
  265. if (events->numEvents == 0)
  266. break;
  267. for (int i=0, count=events->numEvents; i < count; ++i)
  268. {
  269. const VstMidiEvent* const vstMidiEvent((const VstMidiEvent*)events->events[i]);
  270. if (vstMidiEvent == nullptr)
  271. break;
  272. if (vstMidiEvent->type != kVstMidiType || vstMidiEvent->deltaFrames < 0)
  273. continue;
  274. if (fMidiEventCount >= kMaxMidiEvents)
  275. break;
  276. const uint32_t j(fMidiEventCount++);
  277. fMidiEvents[j].port = 0;
  278. fMidiEvents[j].time = static_cast<uint32_t>(vstMidiEvent->deltaFrames);
  279. fMidiEvents[j].size = 3;
  280. for (uint32_t k=0; k<3; ++k)
  281. fMidiEvents[j].data[k] = static_cast<uint8_t>(vstMidiEvent->midiData[k]);
  282. }
  283. }
  284. break;
  285. case effCanDo:
  286. if (const char* const canDo = (const char*)ptr)
  287. {
  288. if (std::strcmp(canDo, "receiveVstEvents") == 0)
  289. return 1;
  290. if (std::strcmp(canDo, "receiveVstMidiEvent") == 0)
  291. return 1;
  292. if (std::strcmp(canDo, "receiveVstTimeInfo") == 0)
  293. return 1;
  294. if (std::strcmp(canDo, "sendVstEvents") == 0)
  295. return 1;
  296. if (std::strcmp(canDo, "sendVstMidiEvent") == 0)
  297. return 1;
  298. }
  299. break;
  300. }
  301. return ret;
  302. }
  303. float vst_getParameter(const int32_t /*index*/)
  304. {
  305. return 0.0f;
  306. }
  307. void vst_setParameter(const int32_t /*index*/, const float /*value*/)
  308. {
  309. }
  310. void vst_processReplacing(const float** const inputs, float** const outputs, const int32_t sampleFrames)
  311. {
  312. if (sampleFrames <= 0)
  313. return;
  314. if (! fIsActive)
  315. {
  316. // host has not activated the plugin yet, nasty!
  317. vst_dispatcher(effMainsChanged, 0, 1, nullptr, 0.0f);
  318. }
  319. static const int kWantVstTimeFlags(kVstTransportPlaying|kVstPpqPosValid|kVstTempoValid|kVstTimeSigValid);
  320. if (const VstTimeInfo* const vstTimeInfo = (const VstTimeInfo*)hostCallback(audioMasterGetTime, 0, kWantVstTimeFlags))
  321. {
  322. fTimeInfo.frame = static_cast<uint64_t>(vstTimeInfo->samplePos);
  323. fTimeInfo.playing = (vstTimeInfo->flags & kVstTransportPlaying);
  324. fTimeInfo.bbt.valid = ((vstTimeInfo->flags & kVstTempoValid) != 0 || (vstTimeInfo->flags & kVstTimeSigValid) != 0);
  325. // ticksPerBeat is not possible with VST
  326. fTimeInfo.bbt.ticksPerBeat = 960.0;
  327. if (vstTimeInfo->flags & kVstTempoValid)
  328. fTimeInfo.bbt.beatsPerMinute = vstTimeInfo->tempo;
  329. else
  330. fTimeInfo.bbt.beatsPerMinute = 120.0;
  331. if (vstTimeInfo->flags & (kVstPpqPosValid|kVstTimeSigValid))
  332. {
  333. const int ppqPerBar = vstTimeInfo->timeSigNumerator * 4 / vstTimeInfo->timeSigDenominator;
  334. const double barBeats = (std::fmod(vstTimeInfo->ppqPos, ppqPerBar) / ppqPerBar) * vstTimeInfo->timeSigDenominator;
  335. const double rest = std::fmod(barBeats, 1.0);
  336. fTimeInfo.bbt.bar = static_cast<int32_t>(vstTimeInfo->ppqPos)/ppqPerBar + 1;
  337. fTimeInfo.bbt.beat = static_cast<int32_t>(barBeats-rest+1.0);
  338. fTimeInfo.bbt.tick = static_cast<int32_t>(rest*fTimeInfo.bbt.ticksPerBeat+0.5);
  339. fTimeInfo.bbt.beatsPerBar = static_cast<float>(vstTimeInfo->timeSigNumerator);
  340. fTimeInfo.bbt.beatType = static_cast<float>(vstTimeInfo->timeSigDenominator);
  341. }
  342. else
  343. {
  344. fTimeInfo.bbt.bar = 1;
  345. fTimeInfo.bbt.beat = 1;
  346. fTimeInfo.bbt.tick = 0;
  347. fTimeInfo.bbt.beatsPerBar = 4.0f;
  348. fTimeInfo.bbt.beatType = 4.0f;
  349. }
  350. fTimeInfo.bbt.barStartTick = fTimeInfo.bbt.ticksPerBeat*fTimeInfo.bbt.beatsPerBar*(fTimeInfo.bbt.bar-1);
  351. }
  352. fMidiOutEvents.numEvents = 0;
  353. if (fHandle != nullptr)
  354. // FIXME
  355. fDescriptor->process(fHandle, const_cast<float**>(inputs), outputs, static_cast<uint32_t>(sampleFrames), fMidiEvents, fMidiEventCount);
  356. fMidiEventCount = 0;
  357. if (fMidiOutEvents.numEvents > 0)
  358. hostCallback(audioMasterProcessEvents, 0, 0, &fMidiOutEvents, 0.0f);
  359. }
  360. protected:
  361. // -------------------------------------------------------------------
  362. uint32_t handleGetBufferSize() const
  363. {
  364. return fBufferSize;
  365. }
  366. double handleGetSampleRate() const
  367. {
  368. return fSampleRate;
  369. }
  370. bool handleIsOffline() const
  371. {
  372. return false;
  373. }
  374. const NativeTimeInfo* handleGetTimeInfo() const
  375. {
  376. return &fTimeInfo;
  377. }
  378. bool handleWriteMidiEvent(const NativeMidiEvent* const event)
  379. {
  380. CARLA_SAFE_ASSERT_RETURN(fDescriptor->midiOuts > 0, false);
  381. CARLA_SAFE_ASSERT_RETURN(event != nullptr, false);
  382. CARLA_SAFE_ASSERT_RETURN(event->data[0] != 0, false);
  383. if (fMidiOutEvents.numEvents >= static_cast<int32_t>(kMaxMidiEvents))
  384. return false;
  385. VstMidiEvent& vstMidiEvent(fMidiOutEvents.mdata[fMidiOutEvents.numEvents++]);
  386. vstMidiEvent.type = kVstMidiType;
  387. vstMidiEvent.byteSize = kVstMidiEventSize;
  388. uint8_t i=0;
  389. for (; i<event->size; ++i)
  390. vstMidiEvent.midiData[i] = static_cast<char>(event->data[i]);
  391. for (; i<4; ++i)
  392. vstMidiEvent.midiData[i] = 0;
  393. return false;
  394. }
  395. void handleUiParameterChanged(const uint32_t /*index*/, const float /*value*/) const
  396. {
  397. }
  398. void handleUiCustomDataChanged(const char* const /*key*/, const char* const /*value*/) const
  399. {
  400. }
  401. void handleUiClosed()
  402. {
  403. }
  404. const char* handleUiOpenFile(const bool /*isDir*/, const char* const /*title*/, const char* const /*filter*/) const
  405. {
  406. // TODO
  407. return nullptr;
  408. }
  409. const char* handleUiSaveFile(const bool /*isDir*/, const char* const /*title*/, const char* const /*filter*/) const
  410. {
  411. // TODO
  412. return nullptr;
  413. }
  414. intptr_t handleDispatcher(const NativeHostDispatcherOpcode opcode, const int32_t index, const intptr_t value, void* const ptr, const float opt)
  415. {
  416. carla_debug("NativePlugin::handleDispatcher(%i, %i, " P_INTPTR ", %p, %f)", opcode, index, value, ptr, opt);
  417. switch (opcode)
  418. {
  419. case NATIVE_HOST_OPCODE_NULL:
  420. case NATIVE_HOST_OPCODE_UPDATE_PARAMETER:
  421. case NATIVE_HOST_OPCODE_UPDATE_MIDI_PROGRAM:
  422. case NATIVE_HOST_OPCODE_RELOAD_PARAMETERS:
  423. case NATIVE_HOST_OPCODE_RELOAD_MIDI_PROGRAMS:
  424. case NATIVE_HOST_OPCODE_RELOAD_ALL:
  425. case NATIVE_HOST_OPCODE_UI_UNAVAILABLE:
  426. break;
  427. case NATIVE_HOST_OPCODE_HOST_IDLE:
  428. hostCallback(audioMasterIdle);
  429. break;
  430. }
  431. // unused for now
  432. return 0;
  433. (void)index; (void)value; (void)ptr; (void)opt;
  434. }
  435. private:
  436. // VST stuff
  437. const audioMasterCallback fAudioMaster;
  438. AEffect* const fEffect;
  439. // Native data
  440. NativePluginHandle fHandle;
  441. NativeHostDescriptor fHost;
  442. const NativePluginDescriptor* const fDescriptor;
  443. // VST host data
  444. uint32_t fBufferSize;
  445. double fSampleRate;
  446. // Temporary data
  447. bool fIsActive;
  448. uint32_t fMidiEventCount;
  449. NativeMidiEvent fMidiEvents[kMaxMidiEvents];
  450. NativeTimeInfo fTimeInfo;
  451. ERect fVstRect;
  452. // host callback
  453. intptr_t hostCallback(const int32_t opcode,
  454. const int32_t index = 0,
  455. const intptr_t value = 0,
  456. void* const ptr = nullptr,
  457. const float opt = 0.0f)
  458. {
  459. return fAudioMaster(fEffect, opcode, index, value, ptr, opt);
  460. }
  461. struct FixedVstEvents {
  462. int32_t numEvents;
  463. intptr_t reserved;
  464. VstEvent* data[kMaxMidiEvents];
  465. VstMidiEvent mdata[kMaxMidiEvents];
  466. FixedVstEvents()
  467. : numEvents(0),
  468. reserved(0),
  469. data(),
  470. mdata()
  471. {
  472. for (uint32_t i=0; i<kMaxMidiEvents; ++i)
  473. data[i] = (VstEvent*)&mdata[i];
  474. carla_zeroStructs(mdata, kMaxMidiEvents);
  475. }
  476. CARLA_DECLARE_NON_COPY_STRUCT(FixedVstEvents);
  477. } fMidiOutEvents;
  478. char* fStateChunk;
  479. SharedResourcePointer<ScopedJuceInitialiser_GUI> sJuceInitialiser;
  480. // -------------------------------------------------------------------
  481. #define handlePtr ((NativePlugin*)handle)
  482. static uint32_t host_get_buffer_size(NativeHostHandle handle)
  483. {
  484. return handlePtr->handleGetBufferSize();
  485. }
  486. static double host_get_sample_rate(NativeHostHandle handle)
  487. {
  488. return handlePtr->handleGetSampleRate();
  489. }
  490. static bool host_is_offline(NativeHostHandle handle)
  491. {
  492. return handlePtr->handleIsOffline();
  493. }
  494. static const NativeTimeInfo* host_get_time_info(NativeHostHandle handle)
  495. {
  496. return handlePtr->handleGetTimeInfo();
  497. }
  498. static bool host_write_midi_event(NativeHostHandle handle, const NativeMidiEvent* event)
  499. {
  500. return handlePtr->handleWriteMidiEvent(event);
  501. }
  502. static void host_ui_parameter_changed(NativeHostHandle handle, uint32_t index, float value)
  503. {
  504. handlePtr->handleUiParameterChanged(index, value);
  505. }
  506. static void host_ui_custom_data_changed(NativeHostHandle handle, const char* key, const char* value)
  507. {
  508. handlePtr->handleUiCustomDataChanged(key, value);
  509. }
  510. static void host_ui_closed(NativeHostHandle handle)
  511. {
  512. handlePtr->handleUiClosed();
  513. }
  514. static const char* host_ui_open_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
  515. {
  516. return handlePtr->handleUiOpenFile(isDir, title, filter);
  517. }
  518. static const char* host_ui_save_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
  519. {
  520. return handlePtr->handleUiSaveFile(isDir, title, filter);
  521. }
  522. static intptr_t host_dispatcher(NativeHostHandle handle, NativeHostDispatcherOpcode opcode, int32_t index, intptr_t value, void* ptr, float opt)
  523. {
  524. return handlePtr->handleDispatcher(opcode, index, value, ptr, opt);
  525. }
  526. #undef handlePtr
  527. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(NativePlugin)
  528. };
  529. // -----------------------------------------------------------------------
  530. struct VstObject {
  531. audioMasterCallback audioMaster;
  532. NativePlugin* plugin;
  533. };
  534. #ifdef VESTIGE_HEADER
  535. # define validObject effect != nullptr && effect->ptr3 != nullptr
  536. # define validPlugin effect != nullptr && effect->ptr3 != nullptr && ((VstObject*)effect->ptr3)->plugin != nullptr
  537. # define vstObjectPtr (VstObject*)effect->ptr3
  538. #else
  539. # define validObject effect != nullptr && effect->object != nullptr
  540. # define validPlugin effect != nullptr && effect->object != nullptr && ((VstObject*)effect->object)->plugin != nullptr
  541. # define vstObjectPtr (VstObject*)effect->object
  542. #endif
  543. #define pluginPtr (vstObjectPtr)->plugin
  544. static intptr_t vst_dispatcherCallback(AEffect* effect, int32_t opcode, int32_t index, intptr_t value, void* ptr, float opt)
  545. {
  546. // handle base opcodes
  547. switch (opcode)
  548. {
  549. case effOpen:
  550. if (VstObject* const obj = vstObjectPtr)
  551. {
  552. // this must always be valid
  553. CARLA_SAFE_ASSERT_RETURN(obj->audioMaster != nullptr, 0);
  554. // some hosts call effOpen twice
  555. CARLA_SAFE_ASSERT_RETURN(obj->plugin == nullptr, 1);
  556. audioMasterCallback audioMaster = (audioMasterCallback)obj->audioMaster;
  557. d_lastBufferSize = static_cast<uint32_t>(audioMaster(effect, audioMasterGetBlockSize, 0, 0, nullptr, 0.0f));
  558. d_lastSampleRate = static_cast<double>(audioMaster(effect, audioMasterGetSampleRate, 0, 0, nullptr, 0.0f));
  559. // some hosts are not ready at this point or return 0 buffersize/samplerate
  560. if (d_lastBufferSize == 0)
  561. d_lastBufferSize = 2048;
  562. if (d_lastSampleRate <= 0.0)
  563. d_lastSampleRate = 44100.0;
  564. const NativePluginDescriptor* pluginDesc = nullptr;
  565. #if CARLA_PLUGIN_PATCHBAY
  566. const char* const pluginLabel = "carlapatchbay";
  567. #else
  568. const char* const pluginLabel = "carlarack";
  569. #endif
  570. PluginListManager& plm(PluginListManager::getInstance());
  571. for (LinkedList<const NativePluginDescriptor*>::Itenerator it = plm.descs.begin2(); it.valid(); it.next())
  572. {
  573. const NativePluginDescriptor* const& tmpDesc(it.getValue());
  574. if (std::strcmp(tmpDesc->label, pluginLabel) == 0)
  575. {
  576. pluginDesc = tmpDesc;
  577. break;
  578. }
  579. }
  580. CARLA_SAFE_ASSERT_RETURN(pluginDesc != nullptr, 0);
  581. obj->plugin = new NativePlugin(audioMaster, effect, pluginDesc);
  582. return 1;
  583. }
  584. return 0;
  585. case effClose:
  586. if (VstObject* const obj = vstObjectPtr)
  587. {
  588. NativePlugin* const plugin(obj->plugin);
  589. if (plugin != nullptr)
  590. {
  591. obj->plugin = nullptr;
  592. delete plugin;
  593. }
  594. #if 0
  595. /* This code invalidates the object created in VSTPluginMain
  596. * Probably not safe against all hosts */
  597. obj->audioMaster = nullptr;
  598. # ifdef VESTIGE_HEADER
  599. effect->ptr3 = nullptr;
  600. # else
  601. vstObjectPtr = nullptr;
  602. # endif
  603. delete obj;
  604. #endif
  605. return 1;
  606. }
  607. //delete effect;
  608. return 0;
  609. case effGetPlugCategory:
  610. #if CARLA_PLUGIN_SYNTH
  611. return kPlugCategSynth;
  612. #else
  613. return kPlugCategEffect;
  614. #endif
  615. case effGetEffectName:
  616. if (char* const cptr = (char*)ptr)
  617. {
  618. #if CARLA_PLUGIN_PATCHBAY
  619. # if CARLA_PLUGIN_SYNTH
  620. std::strncpy(cptr, "Carla-Patchbay", 32);
  621. # else
  622. std::strncpy(cptr, "Carla-PatchbayFX", 32);
  623. # endif
  624. #else
  625. # if CARLA_PLUGIN_SYNTH
  626. std::strncpy(cptr, "Carla-Rack", 32);
  627. # else
  628. std::strncpy(cptr, "Carla-RackFX", 32);
  629. # endif
  630. #endif
  631. return 1;
  632. }
  633. return 0;
  634. case effGetVendorString:
  635. if (char* const cptr = (char*)ptr)
  636. {
  637. std::strncpy(cptr, "falkTX", 32);
  638. return 1;
  639. }
  640. return 0;
  641. case effGetProductString:
  642. if (char* const cptr = (char*)ptr)
  643. {
  644. #if CARLA_PLUGIN_PATCHBAY
  645. # if CARLA_PLUGIN_SYNTH
  646. std::strncpy(cptr, "CarlaPatchbay", 32);
  647. # else
  648. std::strncpy(cptr, "CarlaPatchbayFX", 32);
  649. # endif
  650. #else
  651. # if CARLA_PLUGIN_SYNTH
  652. std::strncpy(cptr, "CarlaRack", 32);
  653. # else
  654. std::strncpy(cptr, "CarlaRackFX", 32);
  655. # endif
  656. #endif
  657. return 1;
  658. }
  659. return 0;
  660. case effGetVendorVersion:
  661. return CARLA_VERSION_HEX;
  662. case effGetVstVersion:
  663. return kVstVersion;
  664. };
  665. // handle advanced opcodes
  666. if (validPlugin)
  667. return pluginPtr->vst_dispatcher(opcode, index, value, ptr, opt);
  668. return 0;
  669. }
  670. static float vst_getParameterCallback(AEffect* effect, int32_t index)
  671. {
  672. if (validPlugin)
  673. return pluginPtr->vst_getParameter(index);
  674. return 0.0f;
  675. }
  676. static void vst_setParameterCallback(AEffect* effect, int32_t index, float value)
  677. {
  678. if (validPlugin)
  679. pluginPtr->vst_setParameter(index, value);
  680. }
  681. static void vst_processCallback(AEffect* effect, float** inputs, float** outputs, int32_t sampleFrames)
  682. {
  683. if (validPlugin)
  684. pluginPtr->vst_processReplacing(const_cast<const float**>(inputs), outputs, sampleFrames);
  685. }
  686. static void vst_processReplacingCallback(AEffect* effect, float** inputs, float** outputs, int32_t sampleFrames)
  687. {
  688. if (validPlugin)
  689. pluginPtr->vst_processReplacing(const_cast<const float**>(inputs), outputs, sampleFrames);
  690. }
  691. #undef pluginPtr
  692. #undef validObject
  693. #undef validPlugin
  694. #undef vstObjectPtr
  695. // -----------------------------------------------------------------------
  696. CARLA_EXPORT
  697. #if defined(CARLA_OS_WIN) || defined(CARLA_OS_MAC)
  698. const AEffect* VSTPluginMain(audioMasterCallback audioMaster);
  699. #else
  700. const AEffect* VSTPluginMain(audioMasterCallback audioMaster) asm ("main");
  701. #endif
  702. CARLA_EXPORT
  703. const AEffect* VSTPluginMain(audioMasterCallback audioMaster)
  704. {
  705. // old version
  706. if (audioMaster(nullptr, audioMasterVersion, 0, 0, nullptr, 0.0f) == 0)
  707. return nullptr;
  708. AEffect* const effect(new AEffect);
  709. std::memset(effect, 0, sizeof(AEffect));
  710. // vst fields
  711. effect->magic = kEffectMagic;
  712. #ifdef VESTIGE_HEADER
  713. int32_t* const version = (int32_t*)&effect->unknown1;
  714. *version = CARLA_VERSION_HEX;
  715. #else
  716. effect->version = CARLA_VERSION_HEX;
  717. #endif
  718. static const int32_t uniqueId = CCONST('C', 'r', 'l', 'a');
  719. #if CARLA_PLUGIN_SYNTH
  720. # if CARLA_PLUGIN_PATCHBAY
  721. effect->uniqueID = uniqueId+4;
  722. # else
  723. effect->uniqueID = uniqueId+3;
  724. # endif
  725. #else
  726. # if CARLA_PLUGIN_PATCHBAY
  727. effect->uniqueID = uniqueId+2;
  728. # else
  729. effect->uniqueID = uniqueId+1;
  730. # endif
  731. #endif
  732. // plugin fields
  733. effect->numParams = 0;
  734. effect->numPrograms = 0;
  735. effect->numInputs = 2;
  736. effect->numOutputs = 2;
  737. // plugin flags
  738. effect->flags |= effFlagsCanReplacing;
  739. effect->flags |= effFlagsHasEditor;
  740. effect->flags |= effFlagsProgramChunks;
  741. #if CARLA_PLUGIN_SYNTH
  742. effect->flags |= effFlagsIsSynth;
  743. #endif
  744. // static calls
  745. effect->dispatcher = vst_dispatcherCallback;
  746. effect->process = vst_processCallback;
  747. effect->getParameter = vst_getParameterCallback;
  748. effect->setParameter = vst_setParameterCallback;
  749. effect->processReplacing = vst_processReplacingCallback;
  750. // pointers
  751. VstObject* const obj(new VstObject());
  752. obj->audioMaster = audioMaster;
  753. obj->plugin = nullptr;
  754. #ifdef VESTIGE_HEADER
  755. effect->ptr3 = obj;
  756. #else
  757. effect->object = obj;
  758. #endif
  759. return effect;
  760. }
  761. // -----------------------------------------------------------------------