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

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