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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891
  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. fIsActive = true;
  242. if (fDescriptor->activate != nullptr)
  243. fDescriptor->activate(fHandle);
  244. }
  245. if (const VstEvents* const events = (const VstEvents*)ptr)
  246. {
  247. if (events->numEvents == 0)
  248. break;
  249. for (int i=0, count=events->numEvents; i < count; ++i)
  250. {
  251. const VstMidiEvent* const vstMidiEvent((const VstMidiEvent*)events->events[i]);
  252. if (vstMidiEvent == nullptr)
  253. break;
  254. if (vstMidiEvent->type != kVstMidiType || vstMidiEvent->deltaFrames < 0)
  255. continue;
  256. if (fMidiEventCount >= kMaxMidiEvents)
  257. break;
  258. const uint32_t j(fMidiEventCount++);
  259. fMidiEvents[j].port = 0;
  260. fMidiEvents[j].time = static_cast<uint32_t>(vstMidiEvent->deltaFrames);
  261. fMidiEvents[j].size = 3;
  262. for (uint32_t k=0; k<3; ++k)
  263. fMidiEvents[j].data[k] = static_cast<uint8_t>(vstMidiEvent->midiData[k]);
  264. }
  265. }
  266. break;
  267. case effCanDo:
  268. if (const char* const canDo = (const char*)ptr)
  269. {
  270. if (std::strcmp(canDo, "receiveVstEvents") == 0)
  271. return 1;
  272. if (std::strcmp(canDo, "receiveVstMidiEvent") == 0)
  273. return 1;
  274. if (std::strcmp(canDo, "receiveVstTimeInfo") == 0)
  275. return 1;
  276. if (std::strcmp(canDo, "sendVstEvents") == 0)
  277. return 1;
  278. if (std::strcmp(canDo, "sendVstMidiEvent") == 0)
  279. return 1;
  280. }
  281. break;
  282. }
  283. return ret;
  284. }
  285. float vst_getParameter(const int32_t /*index*/)
  286. {
  287. return 0.0f;
  288. }
  289. void vst_setParameter(const int32_t /*index*/, const float /*value*/)
  290. {
  291. }
  292. void vst_processReplacing(const float** const inputs, float** const outputs, const int32_t sampleFrames)
  293. {
  294. if (sampleFrames <= 0)
  295. return;
  296. if (! fIsActive)
  297. {
  298. // host has not activated the plugin yet, nasty!
  299. fIsActive = true;
  300. if (fDescriptor->activate != nullptr)
  301. fDescriptor->activate(fHandle);
  302. }
  303. static const int kWantVstTimeFlags(kVstTransportPlaying|kVstPpqPosValid|kVstTempoValid|kVstTimeSigValid);
  304. if (const VstTimeInfo* const vstTimeInfo = (const VstTimeInfo*)fAudioMaster(fEffect, audioMasterGetTime, 0, kWantVstTimeFlags, nullptr, 0.0f))
  305. {
  306. fTimeInfo.frame = static_cast<uint64_t>(vstTimeInfo->samplePos);
  307. fTimeInfo.playing = (vstTimeInfo->flags & kVstTransportPlaying);
  308. fTimeInfo.bbt.valid = ((vstTimeInfo->flags & kVstTempoValid) != 0 || (vstTimeInfo->flags & kVstTimeSigValid) != 0);
  309. // ticksPerBeat is not possible with VST
  310. fTimeInfo.bbt.ticksPerBeat = 960.0;
  311. if (vstTimeInfo->flags & kVstTempoValid)
  312. fTimeInfo.bbt.beatsPerMinute = vstTimeInfo->tempo;
  313. else
  314. fTimeInfo.bbt.beatsPerMinute = 120.0;
  315. if (vstTimeInfo->flags & (kVstPpqPosValid|kVstTimeSigValid))
  316. {
  317. const int ppqPerBar = vstTimeInfo->timeSigNumerator * 4 / vstTimeInfo->timeSigDenominator;
  318. const double barBeats = (std::fmod(vstTimeInfo->ppqPos, ppqPerBar) / ppqPerBar) * vstTimeInfo->timeSigDenominator;
  319. const double rest = std::fmod(barBeats, 1.0);
  320. fTimeInfo.bbt.bar = static_cast<int32_t>(vstTimeInfo->ppqPos)/ppqPerBar + 1;
  321. fTimeInfo.bbt.beat = static_cast<int32_t>(barBeats-rest+1.0);
  322. fTimeInfo.bbt.tick = static_cast<int32_t>(rest*fTimeInfo.bbt.ticksPerBeat+0.5);
  323. fTimeInfo.bbt.beatsPerBar = static_cast<float>(vstTimeInfo->timeSigNumerator);
  324. fTimeInfo.bbt.beatType = static_cast<float>(vstTimeInfo->timeSigDenominator);
  325. }
  326. else
  327. {
  328. fTimeInfo.bbt.bar = 1;
  329. fTimeInfo.bbt.beat = 1;
  330. fTimeInfo.bbt.tick = 0;
  331. fTimeInfo.bbt.beatsPerBar = 4.0f;
  332. fTimeInfo.bbt.beatType = 4.0f;
  333. }
  334. fTimeInfo.bbt.barStartTick = fTimeInfo.bbt.ticksPerBeat*fTimeInfo.bbt.beatsPerBar*(fTimeInfo.bbt.bar-1);
  335. }
  336. fMidiOutEvents.numEvents = 0;
  337. if (fHandle != nullptr)
  338. fDescriptor->process(fHandle, const_cast<float**>(inputs), outputs, static_cast<uint32_t>(sampleFrames), fMidiEvents, fMidiEventCount);
  339. fMidiEventCount = 0;
  340. if (fMidiOutEvents.numEvents > 0)
  341. fAudioMaster(fEffect, audioMasterProcessEvents, 0, 0, &fMidiOutEvents, 0.0f);
  342. }
  343. protected:
  344. // -------------------------------------------------------------------
  345. uint32_t handleGetBufferSize() const
  346. {
  347. return fBufferSize;
  348. }
  349. double handleGetSampleRate() const
  350. {
  351. return fSampleRate;
  352. }
  353. bool handleIsOffline() const
  354. {
  355. return false;
  356. }
  357. const NativeTimeInfo* handleGetTimeInfo() const
  358. {
  359. return &fTimeInfo;
  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. return false;
  368. VstMidiEvent& vstMidiEvent(fMidiOutEvents.mdata[fMidiOutEvents.numEvents++]);
  369. vstMidiEvent.type = kVstMidiType;
  370. vstMidiEvent.byteSize = kVstMidiEventSize;
  371. uint8_t i=0;
  372. for (; i<event->size; ++i)
  373. vstMidiEvent.midiData[i] = static_cast<char>(event->data[i]);
  374. for (; i<4; ++i)
  375. vstMidiEvent.midiData[i] = 0;
  376. return false;
  377. }
  378. void handleUiParameterChanged(const uint32_t /*index*/, const float /*value*/) const
  379. {
  380. }
  381. void handleUiCustomDataChanged(const char* const /*key*/, const char* const /*value*/) const
  382. {
  383. }
  384. void handleUiClosed()
  385. {
  386. }
  387. const char* handleUiOpenFile(const bool /*isDir*/, const char* const /*title*/, const char* const /*filter*/) const
  388. {
  389. // TODO
  390. return nullptr;
  391. }
  392. const char* handleUiSaveFile(const bool /*isDir*/, const char* const /*title*/, const char* const /*filter*/) const
  393. {
  394. // TODO
  395. return nullptr;
  396. }
  397. intptr_t handleDispatcher(const NativeHostDispatcherOpcode opcode, const int32_t index, const intptr_t value, void* const ptr, const float opt)
  398. {
  399. carla_debug("NativePlugin::handleDispatcher(%i, %i, " P_INTPTR ", %p, %f)", opcode, index, value, ptr, opt);
  400. return 0;
  401. // unused for now
  402. (void)opcode; (void)index; (void)value; (void)ptr; (void)opt;
  403. }
  404. private:
  405. // VST stuff
  406. const audioMasterCallback fAudioMaster;
  407. AEffect* const fEffect;
  408. // Native data
  409. NativePluginHandle fHandle;
  410. NativeHostDescriptor fHost;
  411. const NativePluginDescriptor* const fDescriptor;
  412. // VST host data
  413. uint32_t fBufferSize;
  414. double fSampleRate;
  415. // Temporary data
  416. bool fIsActive;
  417. uint32_t fMidiEventCount;
  418. NativeMidiEvent fMidiEvents[kMaxMidiEvents];
  419. NativeTimeInfo fTimeInfo;
  420. ERect fVstRect;
  421. struct FixedVstEvents {
  422. int32_t numEvents;
  423. intptr_t reserved;
  424. VstEvent* data[kMaxMidiEvents];
  425. VstMidiEvent mdata[kMaxMidiEvents];
  426. FixedVstEvents()
  427. : numEvents(0),
  428. reserved(0),
  429. data(),
  430. mdata()
  431. {
  432. for (uint32_t i=0; i<kMaxMidiEvents; ++i)
  433. data[i] = (VstEvent*)&mdata[i];
  434. carla_zeroStruct<VstMidiEvent>(mdata, kMaxMidiEvents);
  435. }
  436. CARLA_DECLARE_NON_COPY_STRUCT(FixedVstEvents);
  437. } fMidiOutEvents;
  438. char* fStateChunk;
  439. SharedResourcePointer<ScopedJuceInitialiser_GUI> sJuceInitialiser;
  440. // -------------------------------------------------------------------
  441. #define handlePtr ((NativePlugin*)handle)
  442. static uint32_t host_get_buffer_size(NativeHostHandle handle)
  443. {
  444. return handlePtr->handleGetBufferSize();
  445. }
  446. static double host_get_sample_rate(NativeHostHandle handle)
  447. {
  448. return handlePtr->handleGetSampleRate();
  449. }
  450. static bool host_is_offline(NativeHostHandle handle)
  451. {
  452. return handlePtr->handleIsOffline();
  453. }
  454. static const NativeTimeInfo* host_get_time_info(NativeHostHandle handle)
  455. {
  456. return handlePtr->handleGetTimeInfo();
  457. }
  458. static bool host_write_midi_event(NativeHostHandle handle, const NativeMidiEvent* event)
  459. {
  460. return handlePtr->handleWriteMidiEvent(event);
  461. }
  462. static void host_ui_parameter_changed(NativeHostHandle handle, uint32_t index, float value)
  463. {
  464. handlePtr->handleUiParameterChanged(index, value);
  465. }
  466. static void host_ui_custom_data_changed(NativeHostHandle handle, const char* key, const char* value)
  467. {
  468. handlePtr->handleUiCustomDataChanged(key, value);
  469. }
  470. static void host_ui_closed(NativeHostHandle handle)
  471. {
  472. handlePtr->handleUiClosed();
  473. }
  474. static const char* host_ui_open_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
  475. {
  476. return handlePtr->handleUiOpenFile(isDir, title, filter);
  477. }
  478. static const char* host_ui_save_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
  479. {
  480. return handlePtr->handleUiSaveFile(isDir, title, filter);
  481. }
  482. static intptr_t host_dispatcher(NativeHostHandle handle, NativeHostDispatcherOpcode opcode, int32_t index, intptr_t value, void* ptr, float opt)
  483. {
  484. return handlePtr->handleDispatcher(opcode, index, value, ptr, opt);
  485. }
  486. #undef handlePtr
  487. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(NativePlugin)
  488. };
  489. // -----------------------------------------------------------------------
  490. struct VstObject {
  491. audioMasterCallback audioMaster;
  492. NativePlugin* plugin;
  493. };
  494. #ifdef VESTIGE_HEADER
  495. # define validObject effect != nullptr && effect->ptr3 != nullptr
  496. # define validPlugin effect != nullptr && effect->ptr3 != nullptr && ((VstObject*)effect->ptr3)->plugin != nullptr
  497. # define vstObjectPtr (VstObject*)effect->ptr3
  498. #else
  499. # define validObject effect != nullptr && effect->object != nullptr
  500. # define validPlugin effect != nullptr && effect->object != nullptr && ((VstObject*)effect->object)->plugin != nullptr
  501. # define vstObjectPtr (VstObject*)effect->object
  502. #endif
  503. #define pluginPtr (vstObjectPtr)->plugin
  504. static intptr_t vst_dispatcherCallback(AEffect* effect, int32_t opcode, int32_t index, intptr_t value, void* ptr, float opt)
  505. {
  506. // handle base opcodes
  507. switch (opcode)
  508. {
  509. case effOpen:
  510. if (VstObject* const obj = vstObjectPtr)
  511. {
  512. // this must always be valid
  513. CARLA_SAFE_ASSERT_RETURN(obj->audioMaster != nullptr, 0);
  514. // some hosts call effOpen twice
  515. CARLA_SAFE_ASSERT_RETURN(obj->plugin == nullptr, 1);
  516. audioMasterCallback audioMaster = (audioMasterCallback)obj->audioMaster;
  517. d_lastBufferSize = static_cast<uint32_t>(audioMaster(effect, audioMasterGetBlockSize, 0, 0, nullptr, 0.0f));
  518. d_lastSampleRate = static_cast<double>(audioMaster(effect, audioMasterGetSampleRate, 0, 0, nullptr, 0.0f));
  519. // some hosts are not ready at this point or return 0 buffersize/samplerate
  520. if (d_lastBufferSize == 0)
  521. d_lastBufferSize = 2048;
  522. if (d_lastSampleRate <= 0.0)
  523. d_lastSampleRate = 44100.0;
  524. const NativePluginDescriptor* pluginDesc = nullptr;
  525. #ifdef CARLA_PLUGIN_PATCHBAY
  526. const char* const pluginLabel = "carlapatchbay";
  527. #else
  528. const char* const pluginLabel = "carlarack";
  529. #endif
  530. PluginListManager& plm(PluginListManager::getInstance());
  531. for (LinkedList<const NativePluginDescriptor*>::Itenerator it = plm.descs.begin(); it.valid(); it.next())
  532. {
  533. const NativePluginDescriptor* const& tmpDesc(it.getValue());
  534. if (std::strcmp(tmpDesc->label, pluginLabel) == 0)
  535. {
  536. pluginDesc = tmpDesc;
  537. break;
  538. }
  539. }
  540. CARLA_SAFE_ASSERT_RETURN(pluginDesc != nullptr, 0);
  541. obj->plugin = new NativePlugin(audioMaster, effect, pluginDesc);
  542. return 1;
  543. }
  544. return 0;
  545. case effClose:
  546. if (VstObject* const obj = vstObjectPtr)
  547. {
  548. if (obj->plugin != nullptr)
  549. {
  550. delete obj->plugin;
  551. obj->plugin = nullptr;
  552. }
  553. #if 0
  554. /* This code invalidates the object created in VSTPluginMain
  555. * Probably not safe against all hosts */
  556. obj->audioMaster = nullptr;
  557. # ifdef VESTIGE_HEADER
  558. effect->ptr3 = nullptr;
  559. # else
  560. vstObjectPtr = nullptr;
  561. # endif
  562. delete obj;
  563. #endif
  564. return 1;
  565. }
  566. //delete effect;
  567. return 0;
  568. case effGetPlugCategory:
  569. #ifdef CARLA_PLUGIN_SYNTH
  570. return kPlugCategSynth;
  571. #else
  572. return kPlugCategEffect;
  573. #endif
  574. case effGetEffectName:
  575. if (char* const cptr = (char*)ptr)
  576. {
  577. #ifdef CARLA_PLUGIN_PATCHBAY
  578. # ifdef CARLA_PLUGIN_SYNTH
  579. std::strncpy(cptr, "Carla-Patchbay", 32);
  580. # else
  581. std::strncpy(cptr, "Carla-PatchbayFX", 32);
  582. # endif
  583. #else
  584. # ifdef CARLA_PLUGIN_SYNTH
  585. std::strncpy(cptr, "Carla-Rack", 32);
  586. # else
  587. std::strncpy(cptr, "Carla-RackFX", 32);
  588. # endif
  589. #endif
  590. return 1;
  591. }
  592. return 0;
  593. case effGetVendorString:
  594. if (char* const cptr = (char*)ptr)
  595. {
  596. std::strncpy(cptr, "falkTX", 32);
  597. return 1;
  598. }
  599. return 0;
  600. case effGetProductString:
  601. if (char* const cptr = (char*)ptr)
  602. {
  603. #ifdef CARLA_PLUGIN_PATCHBAY
  604. # ifdef CARLA_PLUGIN_SYNTH
  605. std::strncpy(cptr, "CarlaPatchbay", 32);
  606. # else
  607. std::strncpy(cptr, "CarlaPatchbayFX", 32);
  608. # endif
  609. #else
  610. # ifdef CARLA_PLUGIN_SYNTH
  611. std::strncpy(cptr, "CarlaRack", 32);
  612. # else
  613. std::strncpy(cptr, "CarlaRackFX", 32);
  614. # endif
  615. #endif
  616. return 1;
  617. }
  618. return 0;
  619. case effGetVendorVersion:
  620. return CARLA_VERSION_HEX;
  621. case effGetVstVersion:
  622. return kVstVersion;
  623. };
  624. // handle advanced opcodes
  625. if (validPlugin)
  626. return pluginPtr->vst_dispatcher(opcode, index, value, ptr, opt);
  627. return 0;
  628. }
  629. static float vst_getParameterCallback(AEffect* effect, int32_t index)
  630. {
  631. if (validPlugin)
  632. return pluginPtr->vst_getParameter(index);
  633. return 0.0f;
  634. }
  635. static void vst_setParameterCallback(AEffect* effect, int32_t index, float value)
  636. {
  637. if (validPlugin)
  638. pluginPtr->vst_setParameter(index, value);
  639. }
  640. static void vst_processCallback(AEffect* effect, float** inputs, float** outputs, int32_t sampleFrames)
  641. {
  642. if (validPlugin)
  643. pluginPtr->vst_processReplacing(const_cast<const float**>(inputs), outputs, sampleFrames);
  644. }
  645. static void vst_processReplacingCallback(AEffect* effect, float** inputs, float** outputs, int32_t sampleFrames)
  646. {
  647. if (validPlugin)
  648. pluginPtr->vst_processReplacing(const_cast<const float**>(inputs), outputs, sampleFrames);
  649. }
  650. #undef pluginPtr
  651. #undef validObject
  652. #undef validPlugin
  653. #undef vstObjectPtr
  654. // -----------------------------------------------------------------------
  655. CARLA_EXPORT
  656. #if defined(CARLA_OS_WIN) || defined(CARLA_OS_MAC)
  657. const AEffect* VSTPluginMain(audioMasterCallback audioMaster);
  658. #else
  659. const AEffect* VSTPluginMain(audioMasterCallback audioMaster) asm ("main");
  660. #endif
  661. CARLA_EXPORT
  662. const AEffect* VSTPluginMain(audioMasterCallback audioMaster)
  663. {
  664. // old version
  665. if (audioMaster(nullptr, audioMasterVersion, 0, 0, nullptr, 0.0f) == 0)
  666. return nullptr;
  667. AEffect* const effect(new AEffect);
  668. std::memset(effect, 0, sizeof(AEffect));
  669. // vst fields
  670. effect->magic = kEffectMagic;
  671. #ifdef VESTIGE_HEADER
  672. int32_t* const version = (int32_t*)&effect->unknown1;
  673. *version = CARLA_VERSION_HEX;
  674. #else
  675. effect->version = CARLA_VERSION_HEX;
  676. #endif
  677. static const int32_t uniqueId = CCONST('C', 'r', 'l', 'a');
  678. #ifdef CARLA_PLUGIN_SYNTH
  679. # ifdef CARLA_PLUGIN_PATCHBAY
  680. effect->uniqueID = uniqueId+4;
  681. # else
  682. effect->uniqueID = uniqueId+3;
  683. # endif
  684. #else
  685. # ifdef CARLA_PLUGIN_PATCHBAY
  686. effect->uniqueID = uniqueId+2;
  687. # else
  688. effect->uniqueID = uniqueId+1;
  689. # endif
  690. #endif
  691. // plugin fields
  692. effect->numParams = 0;
  693. effect->numPrograms = 0;
  694. effect->numInputs = 2;
  695. effect->numOutputs = 2;
  696. // plugin flags
  697. effect->flags |= effFlagsCanReplacing;
  698. effect->flags |= effFlagsHasEditor;
  699. effect->flags |= effFlagsProgramChunks;
  700. #ifdef CARLA_PLUGIN_SYNTH
  701. effect->flags |= effFlagsIsSynth;
  702. #endif
  703. // static calls
  704. effect->dispatcher = vst_dispatcherCallback;
  705. effect->process = vst_processCallback;
  706. effect->getParameter = vst_getParameterCallback;
  707. effect->setParameter = vst_setParameterCallback;
  708. effect->processReplacing = vst_processReplacingCallback;
  709. // pointers
  710. VstObject* const obj(new VstObject());
  711. obj->audioMaster = audioMaster;
  712. obj->plugin = nullptr;
  713. #ifdef VESTIGE_HEADER
  714. effect->ptr3 = obj;
  715. #else
  716. effect->object = obj;
  717. #endif
  718. return effect;
  719. }
  720. // -----------------------------------------------------------------------