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.

1058 lines
33KB

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