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.

1108 lines
35KB

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