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.

1110 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. /*
  633. if (pluginDesc->hints & NATIVE_PLUGIN_HAS_UI)
  634. effect->flags |= effFlagsHasEditor;
  635. else
  636. effect->flags &= ~effFlagsHasEditor;
  637. */
  638. if (pluginDesc->hints & NATIVE_PLUGIN_IS_SYNTH)
  639. effect->flags |= effFlagsIsSynth;
  640. else
  641. effect->flags &= ~effFlagsIsSynth;
  642. #endif // CARLA_VST_SHELL
  643. obj->plugin = new NativePlugin(audioMaster, effect, pluginDesc);
  644. return 1;
  645. }
  646. return 0;
  647. case effClose:
  648. if (VstObject* const obj = vstObjectPtr)
  649. {
  650. NativePlugin* const plugin(obj->plugin);
  651. if (plugin != nullptr)
  652. {
  653. obj->plugin = nullptr;
  654. delete plugin;
  655. }
  656. #if 0
  657. /* This code invalidates the object created in VSTPluginMain
  658. * Probably not safe against all hosts */
  659. obj->audioMaster = nullptr;
  660. effect->object = nullptr;
  661. delete obj;
  662. #endif
  663. return 1;
  664. }
  665. //delete effect;
  666. return 0;
  667. case effGetPlugCategory:
  668. #if CARLA_VST_SHELL
  669. if (validPlugin)
  670. {
  671. const NativePluginDescriptor* const desc = pluginPtr->getDescriptor();
  672. return desc->category == NATIVE_PLUGIN_CATEGORY_SYNTH ? kPlugCategSynth : kPlugCategEffect;
  673. }
  674. return kPlugCategShell;
  675. #elif CARLA_PLUGIN_SYNTH
  676. return kPlugCategSynth;
  677. #else
  678. return kPlugCategEffect;
  679. #endif
  680. case effGetEffectName:
  681. if (char* const cptr = (char*)ptr)
  682. {
  683. #if CARLA_VST_SHELL
  684. if (validPlugin)
  685. {
  686. const NativePluginDescriptor* const desc = pluginPtr->getDescriptor();
  687. std::strncpy(cptr, desc->name, 32);
  688. }
  689. else
  690. {
  691. std::strncpy(cptr, "Carla-VstShell", 32);
  692. }
  693. #elif CARLA_PLUGIN_32CH
  694. std::strncpy(cptr, "Carla-Patchbay32", 32);
  695. #elif CARLA_PLUGIN_16CH
  696. std::strncpy(cptr, "Carla-Patchbay16", 32);
  697. #elif CARLA_PLUGIN_PATCHBAY
  698. # if CARLA_PLUGIN_SYNTH
  699. std::strncpy(cptr, "Carla-Patchbay", 32);
  700. # else
  701. std::strncpy(cptr, "Carla-PatchbayFX", 32);
  702. # endif
  703. #else // Rack mode
  704. # if CARLA_PLUGIN_SYNTH
  705. std::strncpy(cptr, "Carla-Rack", 32);
  706. # else
  707. std::strncpy(cptr, "Carla-RackFX", 32);
  708. # endif
  709. #endif
  710. return 1;
  711. }
  712. return 0;
  713. case effGetVendorString:
  714. if (char* const cptr = (char*)ptr)
  715. {
  716. #if CARLA_VST_SHELL
  717. if (validPlugin)
  718. {
  719. const NativePluginDescriptor* const desc = pluginPtr->getDescriptor();
  720. std::strncpy(cptr, desc->maker, 32);
  721. }
  722. else
  723. #endif
  724. std::strncpy(cptr, "falkTX", 32);
  725. return 1;
  726. }
  727. return 0;
  728. case effGetProductString:
  729. if (char* const cptr = (char*)ptr)
  730. {
  731. #if CARLA_VST_SHELL
  732. if (validPlugin)
  733. {
  734. const NativePluginDescriptor* const desc = pluginPtr->getDescriptor();
  735. std::strncpy(cptr, desc->label, 32);
  736. }
  737. else
  738. {
  739. std::strncpy(cptr, "CarlaVstShell", 32);
  740. }
  741. #elif CARLA_PLUGIN_32CH
  742. std::strncpy(cptr, "CarlaPatchbay32", 32);
  743. #elif CARLA_PLUGIN_16CH
  744. std::strncpy(cptr, "CarlaPatchbay16", 32);
  745. #elif CARLA_PLUGIN_PATCHBAY
  746. # if CARLA_PLUGIN_SYNTH
  747. std::strncpy(cptr, "CarlaPatchbay", 32);
  748. # else
  749. std::strncpy(cptr, "CarlaPatchbayFX", 32);
  750. # endif
  751. #else
  752. # if CARLA_PLUGIN_SYNTH
  753. std::strncpy(cptr, "CarlaRack", 32);
  754. # else
  755. std::strncpy(cptr, "CarlaRackFX", 32);
  756. # endif
  757. #endif
  758. return 1;
  759. }
  760. return 0;
  761. case effGetVendorVersion:
  762. return CARLA_VERSION_HEX;
  763. case effGetVstVersion:
  764. return kVstVersion;
  765. #if CARLA_VST_SHELL
  766. case effShellGetNextPlugin:
  767. if (char* const cptr = (char*)ptr)
  768. {
  769. CARLA_SAFE_ASSERT_RETURN(effect != nullptr, 0);
  770. CARLA_SAFE_ASSERT_RETURN(effect->uniqueID >= kShellUniqueID, 0);
  771. PluginListManager& plm(PluginListManager::getInstance());
  772. for (;;)
  773. {
  774. const uint index = effect->uniqueID - kShellUniqueID;
  775. if (index >= plm.descs.count())
  776. {
  777. effect->uniqueID = kShellUniqueID;
  778. return 0;
  779. }
  780. const NativePluginDescriptor* const desc = plm.descs.getAt(index, nullptr);
  781. CARLA_SAFE_ASSERT_RETURN(desc != nullptr, 0);
  782. ++effect->uniqueID;
  783. if (desc->midiIns > 1 || desc->midiOuts > 1)
  784. continue;
  785. if (std::strncmp(desc->label, "carlarack", 9) == 0)
  786. continue;
  787. if (std::strncmp(desc->label, "carlapatchbay", 13) == 0)
  788. continue;
  789. std::strncpy(cptr, desc->label, 32);
  790. return effect->uniqueID;
  791. }
  792. }
  793. #endif
  794. };
  795. // handle advanced opcodes
  796. if (validPlugin)
  797. return pluginPtr->vst_dispatcher(opcode, index, value, ptr, opt);
  798. return 0;
  799. }
  800. static float vst_getParameterCallback(AEffect* effect, int32_t index)
  801. {
  802. if (validPlugin)
  803. return pluginPtr->vst_getParameter(index);
  804. return 0.0f;
  805. }
  806. static void vst_setParameterCallback(AEffect* effect, int32_t index, float value)
  807. {
  808. if (validPlugin)
  809. pluginPtr->vst_setParameter(index, value);
  810. }
  811. static void vst_processCallback(AEffect* effect, float** inputs, float** outputs, int32_t sampleFrames)
  812. {
  813. if (validPlugin)
  814. pluginPtr->vst_processReplacing(const_cast<const float**>(inputs), outputs, sampleFrames);
  815. }
  816. static void vst_processReplacingCallback(AEffect* effect, float** inputs, float** outputs, int32_t sampleFrames)
  817. {
  818. if (validPlugin)
  819. pluginPtr->vst_processReplacing(const_cast<const float**>(inputs), outputs, sampleFrames);
  820. }
  821. #undef pluginPtr
  822. #undef validObject
  823. #undef validPlugin
  824. #undef vstObjectPtr
  825. // -----------------------------------------------------------------------
  826. CARLA_EXPORT
  827. #if defined(CARLA_OS_WIN) || defined(CARLA_OS_MAC)
  828. const AEffect* VSTPluginMain(audioMasterCallback audioMaster);
  829. #else
  830. const AEffect* VSTPluginMain(audioMasterCallback audioMaster) asm ("main");
  831. #endif
  832. CARLA_EXPORT
  833. const AEffect* VSTPluginMain(audioMasterCallback audioMaster)
  834. {
  835. // old version
  836. if (audioMaster(nullptr, audioMasterVersion, 0, 0, nullptr, 0.0f) == 0)
  837. return nullptr;
  838. AEffect* const effect(new AEffect);
  839. std::memset(effect, 0, sizeof(AEffect));
  840. // vst fields
  841. effect->magic = kEffectMagic;
  842. effect->version = CARLA_VERSION_HEX;
  843. #if CARLA_VST_SHELL
  844. if (const intptr_t uniqueID = audioMaster(nullptr, audioMasterCurrentId, 0, 0, nullptr, 0.0f))
  845. effect->uniqueID = uniqueID;
  846. else
  847. effect->uniqueID = kShellUniqueID;
  848. #elif CARLA_PLUGIN_32CH
  849. effect->uniqueID = kBaseUniqueID+6;
  850. #elif CARLA_PLUGIN_16CH
  851. effect->uniqueID = kBaseUniqueID+5;
  852. #elif CARLA_PLUGIN_PATCHBAY
  853. # if CARLA_PLUGIN_SYNTH
  854. effect->uniqueID = kBaseUniqueID+4;
  855. # else
  856. effect->uniqueID = kBaseUniqueID+3;
  857. # endif
  858. #else
  859. # if CARLA_PLUGIN_SYNTH
  860. effect->uniqueID = kBaseUniqueID+2;
  861. # else
  862. effect->uniqueID = kBaseUniqueID+1;
  863. # endif
  864. #endif
  865. // plugin fields
  866. effect->numParams = 0;
  867. effect->numPrograms = 0;
  868. #if CARLA_VST_SHELL
  869. effect->numInputs = 0;
  870. effect->numOutputs = 0;
  871. #elif CARLA_PLUGIN_32CH
  872. effect->numInputs = 32;
  873. effect->numOutputs = 32;
  874. #elif CARLA_PLUGIN_16CH
  875. effect->numInputs = 16;
  876. effect->numOutputs = 16;
  877. #else
  878. effect->numInputs = 2;
  879. effect->numOutputs = 2;
  880. #endif
  881. // plugin flags
  882. effect->flags |= effFlagsCanReplacing;
  883. effect->flags |= effFlagsProgramChunks;
  884. #if ! CARLA_VST_SHELL
  885. effect->flags |= effFlagsHasEditor;
  886. # if CARLA_PLUGIN_SYNTH
  887. effect->flags |= effFlagsIsSynth;
  888. # endif
  889. #endif
  890. // static calls
  891. effect->dispatcher = vst_dispatcherCallback;
  892. effect->process = vst_processCallback;
  893. effect->getParameter = vst_getParameterCallback;
  894. effect->setParameter = vst_setParameterCallback;
  895. effect->processReplacing = vst_processReplacingCallback;
  896. // pointers
  897. VstObject* const obj(new VstObject());
  898. obj->audioMaster = audioMaster;
  899. obj->plugin = nullptr;
  900. // done
  901. effect->object = obj;
  902. return effect;
  903. }
  904. // -----------------------------------------------------------------------