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.

1111 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("resources");
  70. if (! resDir.exists())
  71. resDir = File("/usr/share/carla/resources/");
  72. // find host type
  73. const String hostFilename(File::getSpecialLocation(File::hostApplicationPath).getFileName());
  74. /**/ if (hostFilename.startsWith("ardour"))
  75. fHostType = kHostTypeArdour;
  76. else 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 && (fHostType != kHostTypeArdour || fBufferSize == 0))
  179. {
  180. fBufferSize = bufferSize;
  181. if (fDescriptor->dispatcher != nullptr)
  182. fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_BUFFER_SIZE_CHANGED, 0, bufferSize, 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. kHostTypeArdour,
  482. kHostTypeBitwig
  483. };
  484. HostType fHostType;
  485. // host callback
  486. intptr_t hostCallback(const int32_t opcode,
  487. const int32_t index = 0,
  488. const intptr_t value = 0,
  489. void* const ptr = nullptr,
  490. const float opt = 0.0f)
  491. {
  492. return fAudioMaster(fEffect, opcode, index, value, ptr, opt);
  493. }
  494. struct FixedVstEvents {
  495. int32_t numEvents;
  496. intptr_t reserved;
  497. VstEvent* data[kMaxMidiEvents];
  498. VstMidiEvent mdata[kMaxMidiEvents];
  499. FixedVstEvents()
  500. : numEvents(0),
  501. reserved(0)
  502. {
  503. for (uint32_t i=0; i<kMaxMidiEvents; ++i)
  504. data[i] = (VstEvent*)&mdata[i];
  505. carla_zeroStructs(mdata, kMaxMidiEvents);
  506. }
  507. CARLA_DECLARE_NON_COPY_STRUCT(FixedVstEvents);
  508. } fMidiOutEvents;
  509. char* fStateChunk;
  510. // -------------------------------------------------------------------
  511. #define handlePtr ((NativePlugin*)handle)
  512. static uint32_t host_get_buffer_size(NativeHostHandle handle)
  513. {
  514. return handlePtr->fBufferSize;
  515. }
  516. static double host_get_sample_rate(NativeHostHandle handle)
  517. {
  518. return handlePtr->fSampleRate;
  519. }
  520. static bool host_is_offline(NativeHostHandle /*handle*/)
  521. {
  522. // TODO
  523. return false;
  524. }
  525. static const NativeTimeInfo* host_get_time_info(NativeHostHandle handle)
  526. {
  527. return &(handlePtr->fTimeInfo);
  528. }
  529. static bool host_write_midi_event(NativeHostHandle handle, const NativeMidiEvent* event)
  530. {
  531. return handlePtr->handleWriteMidiEvent(event);
  532. }
  533. static void host_ui_parameter_changed(NativeHostHandle handle, uint32_t index, float value)
  534. {
  535. handlePtr->handleUiParameterChanged(index, value);
  536. }
  537. static void host_ui_custom_data_changed(NativeHostHandle handle, const char* key, const char* value)
  538. {
  539. handlePtr->handleUiCustomDataChanged(key, value);
  540. }
  541. static void host_ui_closed(NativeHostHandle handle)
  542. {
  543. handlePtr->handleUiClosed();
  544. }
  545. static const char* host_ui_open_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
  546. {
  547. return handlePtr->handleUiOpenFile(isDir, title, filter);
  548. }
  549. static const char* host_ui_save_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
  550. {
  551. return handlePtr->handleUiSaveFile(isDir, title, filter);
  552. }
  553. static intptr_t host_dispatcher(NativeHostHandle handle, NativeHostDispatcherOpcode opcode, int32_t index, intptr_t value, void* ptr, float opt)
  554. {
  555. return handlePtr->handleDispatcher(opcode, index, value, ptr, opt);
  556. }
  557. #undef handlePtr
  558. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(NativePlugin)
  559. };
  560. // -----------------------------------------------------------------------
  561. struct VstObject {
  562. audioMasterCallback audioMaster;
  563. NativePlugin* plugin;
  564. };
  565. #define validObject effect != nullptr && effect->object != nullptr
  566. #define validPlugin effect != nullptr && effect->object != nullptr && ((VstObject*)effect->object)->plugin != nullptr
  567. #define vstObjectPtr (VstObject*)effect->object
  568. #define pluginPtr (vstObjectPtr)->plugin
  569. static intptr_t vst_dispatcherCallback(AEffect* effect, int32_t opcode, int32_t index, intptr_t value, void* ptr, float opt)
  570. {
  571. // handle base opcodes
  572. switch (opcode)
  573. {
  574. case effOpen:
  575. if (VstObject* const obj = vstObjectPtr)
  576. {
  577. // this must always be valid
  578. CARLA_SAFE_ASSERT_RETURN(obj->audioMaster != nullptr, 0);
  579. // some hosts call effOpen twice
  580. CARLA_SAFE_ASSERT_RETURN(obj->plugin == nullptr, 1);
  581. const audioMasterCallback audioMaster = (audioMasterCallback)obj->audioMaster;
  582. d_lastBufferSize = static_cast<uint32_t>(audioMaster(effect, audioMasterGetBlockSize, 0, 0, nullptr, 0.0f));
  583. d_lastSampleRate = static_cast<double>(audioMaster(effect, audioMasterGetSampleRate, 0, 0, nullptr, 0.0f));
  584. // some hosts are not ready at this point or return 0 buffersize/samplerate
  585. if (d_lastBufferSize == 0)
  586. d_lastBufferSize = 2048;
  587. if (d_lastSampleRate <= 0.0)
  588. d_lastSampleRate = 44100.0;
  589. const NativePluginDescriptor* pluginDesc = nullptr;
  590. PluginListManager& plm(PluginListManager::getInstance());
  591. #if CARLA_VST_SHELL
  592. if (effect->uniqueID == 0)
  593. effect->uniqueID = kShellUniqueID;
  594. if (effect->uniqueID == kShellUniqueID)
  595. {
  596. // first open for discovery, nothing to do
  597. effect->numParams = 0;
  598. effect->numPrograms = 0;
  599. effect->numInputs = 0;
  600. effect->numOutputs = 0;
  601. return 1;
  602. }
  603. const int32_t plugIndex = effect->uniqueID - kShellUniqueID - 1;
  604. CARLA_SAFE_ASSERT_RETURN(plugIndex >= 0, 0);
  605. pluginDesc = plm.descs.getAt(plugIndex, nullptr);
  606. #else // CARLA_VST_SHELL
  607. # if CARLA_PLUGIN_32CH
  608. const char* const pluginLabel = "carlapatchbay32";
  609. # elif CARLA_PLUGIN_16CH
  610. const char* const pluginLabel = "carlapatchbay16";
  611. # elif CARLA_PLUGIN_PATCHBAY
  612. const char* const pluginLabel = "carlapatchbay";
  613. # else
  614. const char* const pluginLabel = "carlarack";
  615. # endif
  616. for (LinkedList<const NativePluginDescriptor*>::Itenerator it = plm.descs.begin2(); it.valid(); it.next())
  617. {
  618. const NativePluginDescriptor* const& tmpDesc(it.getValue(nullptr));
  619. CARLA_SAFE_ASSERT_CONTINUE(tmpDesc != nullptr);
  620. if (std::strcmp(tmpDesc->label, pluginLabel) == 0)
  621. {
  622. pluginDesc = tmpDesc;
  623. break;
  624. }
  625. }
  626. #endif // CARLA_VST_SHELL
  627. CARLA_SAFE_ASSERT_RETURN(pluginDesc != nullptr, 0);
  628. #if CARLA_VST_SHELL
  629. effect->numParams = 0;
  630. effect->numPrograms = 0;
  631. effect->numInputs = pluginDesc->audioIns;
  632. effect->numOutputs = pluginDesc->audioOuts;
  633. /*
  634. if (pluginDesc->hints & NATIVE_PLUGIN_HAS_UI)
  635. effect->flags |= effFlagsHasEditor;
  636. else
  637. effect->flags &= ~effFlagsHasEditor;
  638. */
  639. if (pluginDesc->hints & NATIVE_PLUGIN_IS_SYNTH)
  640. effect->flags |= effFlagsIsSynth;
  641. else
  642. effect->flags &= ~effFlagsIsSynth;
  643. #endif // CARLA_VST_SHELL
  644. obj->plugin = new NativePlugin(audioMaster, effect, pluginDesc);
  645. return 1;
  646. }
  647. return 0;
  648. case effClose:
  649. if (VstObject* const obj = vstObjectPtr)
  650. {
  651. NativePlugin* const plugin(obj->plugin);
  652. if (plugin != nullptr)
  653. {
  654. obj->plugin = nullptr;
  655. delete plugin;
  656. }
  657. #if 0
  658. /* This code invalidates the object created in VSTPluginMain
  659. * Probably not safe against all hosts */
  660. obj->audioMaster = nullptr;
  661. effect->object = nullptr;
  662. delete obj;
  663. #endif
  664. return 1;
  665. }
  666. //delete effect;
  667. return 0;
  668. case effGetPlugCategory:
  669. #if CARLA_VST_SHELL
  670. if (validPlugin)
  671. {
  672. const NativePluginDescriptor* const desc = pluginPtr->getDescriptor();
  673. return desc->category == NATIVE_PLUGIN_CATEGORY_SYNTH ? kPlugCategSynth : kPlugCategEffect;
  674. }
  675. return kPlugCategShell;
  676. #elif CARLA_PLUGIN_SYNTH
  677. return kPlugCategSynth;
  678. #else
  679. return kPlugCategEffect;
  680. #endif
  681. case effGetEffectName:
  682. if (char* const cptr = (char*)ptr)
  683. {
  684. #if CARLA_VST_SHELL
  685. if (validPlugin)
  686. {
  687. const NativePluginDescriptor* const desc = pluginPtr->getDescriptor();
  688. std::strncpy(cptr, desc->name, 32);
  689. }
  690. else
  691. {
  692. std::strncpy(cptr, "Carla-VstShell", 32);
  693. }
  694. #elif CARLA_PLUGIN_32CH
  695. std::strncpy(cptr, "Carla-Patchbay32", 32);
  696. #elif CARLA_PLUGIN_16CH
  697. std::strncpy(cptr, "Carla-Patchbay16", 32);
  698. #elif CARLA_PLUGIN_PATCHBAY
  699. # if CARLA_PLUGIN_SYNTH
  700. std::strncpy(cptr, "Carla-Patchbay", 32);
  701. # else
  702. std::strncpy(cptr, "Carla-PatchbayFX", 32);
  703. # endif
  704. #else // Rack mode
  705. # if CARLA_PLUGIN_SYNTH
  706. std::strncpy(cptr, "Carla-Rack", 32);
  707. # else
  708. std::strncpy(cptr, "Carla-RackFX", 32);
  709. # endif
  710. #endif
  711. return 1;
  712. }
  713. return 0;
  714. case effGetVendorString:
  715. if (char* const cptr = (char*)ptr)
  716. {
  717. #if CARLA_VST_SHELL
  718. if (validPlugin)
  719. {
  720. const NativePluginDescriptor* const desc = pluginPtr->getDescriptor();
  721. std::strncpy(cptr, desc->maker, 32);
  722. }
  723. else
  724. #endif
  725. std::strncpy(cptr, "falkTX", 32);
  726. return 1;
  727. }
  728. return 0;
  729. case effGetProductString:
  730. if (char* const cptr = (char*)ptr)
  731. {
  732. #if CARLA_VST_SHELL
  733. if (validPlugin)
  734. {
  735. const NativePluginDescriptor* const desc = pluginPtr->getDescriptor();
  736. std::strncpy(cptr, desc->label, 32);
  737. }
  738. else
  739. {
  740. std::strncpy(cptr, "CarlaVstShell", 32);
  741. }
  742. #elif CARLA_PLUGIN_32CH
  743. std::strncpy(cptr, "CarlaPatchbay32", 32);
  744. #elif CARLA_PLUGIN_16CH
  745. std::strncpy(cptr, "CarlaPatchbay16", 32);
  746. #elif CARLA_PLUGIN_PATCHBAY
  747. # if CARLA_PLUGIN_SYNTH
  748. std::strncpy(cptr, "CarlaPatchbay", 32);
  749. # else
  750. std::strncpy(cptr, "CarlaPatchbayFX", 32);
  751. # endif
  752. #else
  753. # if CARLA_PLUGIN_SYNTH
  754. std::strncpy(cptr, "CarlaRack", 32);
  755. # else
  756. std::strncpy(cptr, "CarlaRackFX", 32);
  757. # endif
  758. #endif
  759. return 1;
  760. }
  761. return 0;
  762. case effGetVendorVersion:
  763. return CARLA_VERSION_HEX;
  764. case effGetVstVersion:
  765. return kVstVersion;
  766. #if CARLA_VST_SHELL
  767. case effShellGetNextPlugin:
  768. if (char* const cptr = (char*)ptr)
  769. {
  770. CARLA_SAFE_ASSERT_RETURN(effect != nullptr, 0);
  771. CARLA_SAFE_ASSERT_RETURN(effect->uniqueID >= kShellUniqueID, 0);
  772. PluginListManager& plm(PluginListManager::getInstance());
  773. for (;;)
  774. {
  775. const uint index = effect->uniqueID - kShellUniqueID;
  776. if (index >= plm.descs.count())
  777. {
  778. effect->uniqueID = kShellUniqueID;
  779. return 0;
  780. }
  781. const NativePluginDescriptor* const desc = plm.descs.getAt(index, nullptr);
  782. CARLA_SAFE_ASSERT_RETURN(desc != nullptr, 0);
  783. ++effect->uniqueID;
  784. if (desc->midiIns > 1 || desc->midiOuts > 1)
  785. continue;
  786. if (std::strncmp(desc->label, "carlarack", 9) == 0)
  787. continue;
  788. if (std::strncmp(desc->label, "carlapatchbay", 13) == 0)
  789. continue;
  790. std::strncpy(cptr, desc->label, 32);
  791. return effect->uniqueID;
  792. }
  793. }
  794. #endif
  795. };
  796. // handle advanced opcodes
  797. if (validPlugin)
  798. return pluginPtr->vst_dispatcher(opcode, index, value, ptr, opt);
  799. return 0;
  800. }
  801. static float vst_getParameterCallback(AEffect* effect, int32_t index)
  802. {
  803. if (validPlugin)
  804. return pluginPtr->vst_getParameter(index);
  805. return 0.0f;
  806. }
  807. static void vst_setParameterCallback(AEffect* effect, int32_t index, float value)
  808. {
  809. if (validPlugin)
  810. pluginPtr->vst_setParameter(index, value);
  811. }
  812. static void vst_processCallback(AEffect* effect, float** inputs, float** outputs, int32_t sampleFrames)
  813. {
  814. if (validPlugin)
  815. pluginPtr->vst_processReplacing(const_cast<const float**>(inputs), outputs, sampleFrames);
  816. }
  817. static void vst_processReplacingCallback(AEffect* effect, float** inputs, float** outputs, int32_t sampleFrames)
  818. {
  819. if (validPlugin)
  820. pluginPtr->vst_processReplacing(const_cast<const float**>(inputs), outputs, sampleFrames);
  821. }
  822. #undef pluginPtr
  823. #undef validObject
  824. #undef validPlugin
  825. #undef vstObjectPtr
  826. // -----------------------------------------------------------------------
  827. CARLA_EXPORT
  828. #if defined(CARLA_OS_WIN) || defined(CARLA_OS_MAC)
  829. const AEffect* VSTPluginMain(audioMasterCallback audioMaster);
  830. #else
  831. const AEffect* VSTPluginMain(audioMasterCallback audioMaster) asm ("main");
  832. #endif
  833. CARLA_EXPORT
  834. const AEffect* VSTPluginMain(audioMasterCallback audioMaster)
  835. {
  836. // old version
  837. if (audioMaster(nullptr, audioMasterVersion, 0, 0, nullptr, 0.0f) == 0)
  838. return nullptr;
  839. AEffect* const effect(new AEffect);
  840. std::memset(effect, 0, sizeof(AEffect));
  841. // vst fields
  842. effect->magic = kEffectMagic;
  843. effect->version = CARLA_VERSION_HEX;
  844. #if CARLA_VST_SHELL
  845. if (const intptr_t uniqueID = audioMaster(nullptr, audioMasterCurrentId, 0, 0, nullptr, 0.0f))
  846. effect->uniqueID = uniqueID;
  847. else
  848. effect->uniqueID = kShellUniqueID;
  849. #elif CARLA_PLUGIN_32CH
  850. effect->uniqueID = kBaseUniqueID+6;
  851. #elif CARLA_PLUGIN_16CH
  852. effect->uniqueID = kBaseUniqueID+5;
  853. #elif CARLA_PLUGIN_PATCHBAY
  854. # if CARLA_PLUGIN_SYNTH
  855. effect->uniqueID = kBaseUniqueID+4;
  856. # else
  857. effect->uniqueID = kBaseUniqueID+3;
  858. # endif
  859. #else
  860. # if CARLA_PLUGIN_SYNTH
  861. effect->uniqueID = kBaseUniqueID+2;
  862. # else
  863. effect->uniqueID = kBaseUniqueID+1;
  864. # endif
  865. #endif
  866. // plugin fields
  867. effect->numParams = 0;
  868. effect->numPrograms = 0;
  869. #if CARLA_VST_SHELL
  870. effect->numInputs = 0;
  871. effect->numOutputs = 0;
  872. #elif CARLA_PLUGIN_32CH
  873. effect->numInputs = 32;
  874. effect->numOutputs = 32;
  875. #elif CARLA_PLUGIN_16CH
  876. effect->numInputs = 16;
  877. effect->numOutputs = 16;
  878. #else
  879. effect->numInputs = 2;
  880. effect->numOutputs = 2;
  881. #endif
  882. // plugin flags
  883. effect->flags |= effFlagsCanReplacing;
  884. effect->flags |= effFlagsProgramChunks;
  885. #if ! CARLA_VST_SHELL
  886. effect->flags |= effFlagsHasEditor;
  887. # if CARLA_PLUGIN_SYNTH
  888. effect->flags |= effFlagsIsSynth;
  889. # endif
  890. #endif
  891. // static calls
  892. effect->dispatcher = vst_dispatcherCallback;
  893. effect->process = vst_processCallback;
  894. effect->getParameter = vst_getParameterCallback;
  895. effect->setParameter = vst_setParameterCallback;
  896. effect->processReplacing = vst_processReplacingCallback;
  897. // pointers
  898. VstObject* const obj(new VstObject());
  899. obj->audioMaster = audioMaster;
  900. obj->plugin = nullptr;
  901. // done
  902. effect->object = obj;
  903. return effect;
  904. }
  905. // -----------------------------------------------------------------------