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.

1084 lines
34KB

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