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.

1049 lines
33KB

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