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

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