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.

1038 lines
32KB

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