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

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