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 28KB

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