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.

carla-vst.cpp 26KB

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