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

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