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.

1060 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 defined(CARLA_PLUGIN_32CH) || defined(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 *
  372. static_cast<double>(fTimeInfo.bbt.beatsPerBar) *
  373. (fTimeInfo.bbt.bar - 1);
  374. }
  375. fMidiOutEvents.numEvents = 0;
  376. if (fHandle != nullptr)
  377. // FIXME
  378. fDescriptor->process(fHandle, const_cast<float**>(inputs), outputs, static_cast<uint32_t>(sampleFrames), fMidiEvents, fMidiEventCount);
  379. fMidiEventCount = 0;
  380. if (fMidiOutEvents.numEvents > 0)
  381. hostCallback(audioMasterProcessEvents, 0, 0, &fMidiOutEvents, 0.0f);
  382. }
  383. protected:
  384. // -------------------------------------------------------------------
  385. bool handleWriteMidiEvent(const NativeMidiEvent* const event)
  386. {
  387. CARLA_SAFE_ASSERT_RETURN(fDescriptor->midiOuts > 0, false);
  388. CARLA_SAFE_ASSERT_RETURN(event != nullptr, false);
  389. CARLA_SAFE_ASSERT_RETURN(event->data[0] != 0, false);
  390. if (fMidiOutEvents.numEvents >= static_cast<int32_t>(kMaxMidiEvents))
  391. {
  392. // send current events
  393. hostCallback(audioMasterProcessEvents, 0, 0, &fMidiOutEvents, 0.0f);
  394. // clear
  395. fMidiOutEvents.numEvents = 0;
  396. }
  397. VstMidiEvent& vstMidiEvent(fMidiOutEvents.mdata[fMidiOutEvents.numEvents++]);
  398. vstMidiEvent.type = kVstMidiType;
  399. vstMidiEvent.byteSize = kVstMidiEventSize;
  400. uint8_t i=0;
  401. for (; i<event->size; ++i)
  402. vstMidiEvent.midiData[i] = static_cast<char>(event->data[i]);
  403. for (; i<4; ++i)
  404. vstMidiEvent.midiData[i] = 0;
  405. return true;
  406. }
  407. void handleUiParameterChanged(const uint32_t /*index*/, const float /*value*/) const
  408. {
  409. }
  410. void handleUiCustomDataChanged(const char* const /*key*/, const char* const /*value*/) const
  411. {
  412. }
  413. void handleUiClosed()
  414. {
  415. }
  416. const char* handleUiOpenFile(const bool /*isDir*/, const char* const /*title*/, const char* const /*filter*/) const
  417. {
  418. // TODO
  419. return nullptr;
  420. }
  421. const char* handleUiSaveFile(const bool /*isDir*/, const char* const /*title*/, const char* const /*filter*/) const
  422. {
  423. // TODO
  424. return nullptr;
  425. }
  426. intptr_t handleDispatcher(const NativeHostDispatcherOpcode opcode, const int32_t index, const intptr_t value, void* const ptr, const float opt)
  427. {
  428. carla_debug("NativePlugin::handleDispatcher(%i, %i, " P_INTPTR ", %p, %f)", opcode, index, value, ptr, opt);
  429. switch (opcode)
  430. {
  431. case NATIVE_HOST_OPCODE_NULL:
  432. case NATIVE_HOST_OPCODE_UPDATE_PARAMETER:
  433. case NATIVE_HOST_OPCODE_UPDATE_MIDI_PROGRAM:
  434. case NATIVE_HOST_OPCODE_RELOAD_PARAMETERS:
  435. case NATIVE_HOST_OPCODE_RELOAD_MIDI_PROGRAMS:
  436. case NATIVE_HOST_OPCODE_RELOAD_ALL:
  437. case NATIVE_HOST_OPCODE_UI_UNAVAILABLE:
  438. case NATIVE_HOST_OPCODE_INTERNAL_PLUGIN:
  439. break;
  440. case NATIVE_HOST_OPCODE_HOST_IDLE:
  441. hostCallback(audioMasterIdle);
  442. break;
  443. }
  444. // unused for now
  445. return 0;
  446. (void)index; (void)value; (void)ptr; (void)opt;
  447. }
  448. private:
  449. // VST stuff
  450. AEffect* const fEffect;
  451. // Native data
  452. NativePluginHandle fHandle;
  453. NativeHostDescriptor fHost;
  454. const NativePluginDescriptor* const fDescriptor;
  455. // VST host data
  456. uint32_t fBufferSize;
  457. double fSampleRate;
  458. // Temporary data
  459. bool fIsActive;
  460. uint32_t fMidiEventCount;
  461. NativeMidiEvent fMidiEvents[kMaxMidiEvents];
  462. NativeTimeInfo fTimeInfo;
  463. ERect fVstRect;
  464. // UI button
  465. CarlaUILauncher* fUiLauncher;
  466. // Host data
  467. enum HostType {
  468. kHostTypeNull = 0,
  469. kHostTypeBitwig
  470. };
  471. HostType fHostType;
  472. // host callback
  473. intptr_t hostCallback(const int32_t opcode,
  474. const int32_t index = 0,
  475. const intptr_t value = 0,
  476. void* const ptr = nullptr,
  477. const float opt = 0.0f)
  478. {
  479. return VSTAudioMaster(fEffect, opcode, index, value, ptr, opt);
  480. }
  481. struct FixedVstEvents {
  482. int32_t numEvents;
  483. intptr_t reserved;
  484. VstEvent* data[kMaxMidiEvents];
  485. VstMidiEvent mdata[kMaxMidiEvents];
  486. FixedVstEvents()
  487. : numEvents(0),
  488. reserved(0)
  489. {
  490. for (uint32_t i=0; i<kMaxMidiEvents; ++i)
  491. data[i] = (VstEvent*)&mdata[i];
  492. carla_zeroStructs(mdata, kMaxMidiEvents);
  493. }
  494. CARLA_DECLARE_NON_COPY_STRUCT(FixedVstEvents);
  495. } fMidiOutEvents;
  496. char* fStateChunk;
  497. #ifdef USING_JUCE
  498. juce::SharedResourcePointer<juce::ScopedJuceInitialiser_GUI> sJuceInitialiser;
  499. #endif
  500. // -------------------------------------------------------------------
  501. #define handlePtr ((NativePlugin*)handle)
  502. static uint32_t host_get_buffer_size(NativeHostHandle handle)
  503. {
  504. return handlePtr->fBufferSize;
  505. }
  506. static double host_get_sample_rate(NativeHostHandle handle)
  507. {
  508. return handlePtr->fSampleRate;
  509. }
  510. static bool host_is_offline(NativeHostHandle /*handle*/)
  511. {
  512. // TODO
  513. return false;
  514. }
  515. static const NativeTimeInfo* host_get_time_info(NativeHostHandle handle)
  516. {
  517. return &(handlePtr->fTimeInfo);
  518. }
  519. static bool host_write_midi_event(NativeHostHandle handle, const NativeMidiEvent* event)
  520. {
  521. return handlePtr->handleWriteMidiEvent(event);
  522. }
  523. static void host_ui_parameter_changed(NativeHostHandle handle, uint32_t index, float value)
  524. {
  525. handlePtr->handleUiParameterChanged(index, value);
  526. }
  527. static void host_ui_custom_data_changed(NativeHostHandle handle, const char* key, const char* value)
  528. {
  529. handlePtr->handleUiCustomDataChanged(key, value);
  530. }
  531. static void host_ui_closed(NativeHostHandle handle)
  532. {
  533. handlePtr->handleUiClosed();
  534. }
  535. static const char* host_ui_open_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
  536. {
  537. return handlePtr->handleUiOpenFile(isDir, title, filter);
  538. }
  539. static const char* host_ui_save_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
  540. {
  541. return handlePtr->handleUiSaveFile(isDir, title, filter);
  542. }
  543. static intptr_t host_dispatcher(NativeHostHandle handle, NativeHostDispatcherOpcode opcode, int32_t index, intptr_t value, void* ptr, float opt)
  544. {
  545. return handlePtr->handleDispatcher(opcode, index, value, ptr, opt);
  546. }
  547. #undef handlePtr
  548. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(NativePlugin)
  549. };
  550. // -----------------------------------------------------------------------
  551. #define validObject effect != nullptr && effect->object != nullptr
  552. #define validPlugin effect != nullptr && effect->object != nullptr && ((VstObject*)effect->object)->plugin != nullptr
  553. #define vstObjectPtr (VstObject*)effect->object
  554. #define pluginPtr (vstObjectPtr)->plugin
  555. intptr_t vst_dispatcherCallback(AEffect* effect, int32_t opcode, int32_t index, intptr_t value, void* ptr, float opt)
  556. {
  557. // handle base opcodes
  558. switch (opcode)
  559. {
  560. case effOpen:
  561. if (VstObject* const obj = vstObjectPtr)
  562. {
  563. // this must always be valid
  564. CARLA_SAFE_ASSERT_RETURN(obj->audioMaster != nullptr, 0);
  565. // some hosts call effOpen twice
  566. CARLA_SAFE_ASSERT_RETURN(obj->plugin == nullptr, 1);
  567. d_lastBufferSize = static_cast<uint32_t>(VSTAudioMaster(effect, audioMasterGetBlockSize, 0, 0, nullptr, 0.0f));
  568. d_lastSampleRate = static_cast<double>(VSTAudioMaster(effect, audioMasterGetSampleRate, 0, 0, nullptr, 0.0f));
  569. // some hosts are not ready at this point or return 0 buffersize/samplerate
  570. if (d_lastBufferSize == 0)
  571. d_lastBufferSize = 2048;
  572. if (d_lastSampleRate <= 0.0)
  573. d_lastSampleRate = 44100.0;
  574. const NativePluginDescriptor* pluginDesc = nullptr;
  575. PluginListManager& plm(PluginListManager::getInstance());
  576. #ifdef CARLA_VST_SHELL
  577. if (effect->uniqueID == 0)
  578. effect->uniqueID = kShellUniqueID;
  579. if (effect->uniqueID == kShellUniqueID)
  580. {
  581. // first open for discovery, nothing to do
  582. effect->numParams = 0;
  583. effect->numPrograms = 0;
  584. effect->numInputs = 0;
  585. effect->numOutputs = 0;
  586. return 1;
  587. }
  588. const int32_t plugIndex = effect->uniqueID - kShellUniqueID - 1;
  589. CARLA_SAFE_ASSERT_RETURN(plugIndex >= 0, 0);
  590. pluginDesc = plm.descs.getAt(static_cast<size_t>(plugIndex), nullptr);
  591. #else // CARLA_VST_SHELL
  592. # if defined(CARLA_PLUGIN_32CH)
  593. const char* const pluginLabel = "carlapatchbay32";
  594. # elif defined(CARLA_PLUGIN_16CH)
  595. const char* const pluginLabel = "carlapatchbay16";
  596. # elif CARLA_PLUGIN_PATCHBAY
  597. const char* const pluginLabel = "carlapatchbay";
  598. # else
  599. const char* const pluginLabel = "carlarack";
  600. # endif
  601. for (LinkedList<const NativePluginDescriptor*>::Itenerator it = plm.descs.begin2(); it.valid(); it.next())
  602. {
  603. const NativePluginDescriptor* const& tmpDesc(it.getValue(nullptr));
  604. CARLA_SAFE_ASSERT_CONTINUE(tmpDesc != nullptr);
  605. if (std::strcmp(tmpDesc->label, pluginLabel) == 0)
  606. {
  607. pluginDesc = tmpDesc;
  608. break;
  609. }
  610. }
  611. #endif // CARLA_VST_SHELL
  612. CARLA_SAFE_ASSERT_RETURN(pluginDesc != nullptr, 0);
  613. #ifdef CARLA_VST_SHELL
  614. effect->numParams = 0;
  615. effect->numPrograms = 0;
  616. effect->numInputs = static_cast<int>(pluginDesc->audioIns);
  617. effect->numOutputs = static_cast<int>(pluginDesc->audioOuts);
  618. if (pluginDesc->hints & NATIVE_PLUGIN_HAS_UI)
  619. effect->flags |= effFlagsHasEditor;
  620. else
  621. effect->flags &= ~effFlagsHasEditor;
  622. if (pluginDesc->hints & NATIVE_PLUGIN_IS_SYNTH)
  623. effect->flags |= effFlagsIsSynth;
  624. else
  625. effect->flags &= ~effFlagsIsSynth;
  626. #endif // CARLA_VST_SHELL
  627. #if CARLA_PLUGIN_SYNTH
  628. // override if requested
  629. effect->flags |= effFlagsIsSynth;
  630. #endif
  631. obj->plugin = new NativePlugin(effect, pluginDesc);
  632. return 1;
  633. }
  634. return 0;
  635. case effClose:
  636. if (VstObject* const obj = vstObjectPtr)
  637. {
  638. NativePlugin* const plugin(obj->plugin);
  639. if (plugin != nullptr)
  640. {
  641. obj->plugin = nullptr;
  642. delete plugin;
  643. }
  644. #if 0
  645. /* This code invalidates the object created in VSTPluginMain
  646. * Probably not safe against all hosts */
  647. obj->audioMaster = nullptr;
  648. effect->object = nullptr;
  649. delete obj;
  650. #endif
  651. return 1;
  652. }
  653. //delete effect;
  654. return 0;
  655. case effGetPlugCategory:
  656. #ifdef CARLA_VST_SHELL
  657. if (validPlugin)
  658. {
  659. #if CARLA_PLUGIN_SYNTH
  660. return kPlugCategSynth;
  661. #else
  662. const NativePluginDescriptor* const desc = pluginPtr->getDescriptor();
  663. return desc->category == NATIVE_PLUGIN_CATEGORY_SYNTH ? kPlugCategSynth : kPlugCategEffect;
  664. #endif
  665. }
  666. return kPlugCategShell;
  667. #elif CARLA_PLUGIN_SYNTH
  668. return kPlugCategSynth;
  669. #else
  670. return kPlugCategEffect;
  671. #endif
  672. case effGetEffectName:
  673. if (char* const cptr = (char*)ptr)
  674. {
  675. #if defined(CARLA_VST_SHELL)
  676. if (validPlugin)
  677. {
  678. const NativePluginDescriptor* const desc = pluginPtr->getDescriptor();
  679. std::strncpy(cptr, desc->name, 32);
  680. }
  681. else
  682. {
  683. std::strncpy(cptr, "Carla-VstShell", 32);
  684. }
  685. #elif defined(CARLA_PLUGIN_32CH)
  686. std::strncpy(cptr, "Carla-Patchbay32", 32);
  687. #elif defined(CARLA_PLUGIN_16CH)
  688. std::strncpy(cptr, "Carla-Patchbay16", 32);
  689. #elif CARLA_PLUGIN_PATCHBAY
  690. # if CARLA_PLUGIN_SYNTH
  691. std::strncpy(cptr, "Carla-Patchbay", 32);
  692. # else
  693. std::strncpy(cptr, "Carla-PatchbayFX", 32);
  694. # endif
  695. #else // Rack mode
  696. # if CARLA_PLUGIN_SYNTH
  697. std::strncpy(cptr, "Carla-Rack", 32);
  698. # else
  699. std::strncpy(cptr, "Carla-RackFX", 32);
  700. # endif
  701. #endif
  702. return 1;
  703. }
  704. return 0;
  705. case effGetVendorString:
  706. if (char* const cptr = (char*)ptr)
  707. {
  708. #ifdef CARLA_VST_SHELL
  709. if (validPlugin)
  710. {
  711. const NativePluginDescriptor* const desc = pluginPtr->getDescriptor();
  712. std::strncpy(cptr, desc->maker, 32);
  713. }
  714. else
  715. #endif
  716. std::strncpy(cptr, "falkTX", 32);
  717. return 1;
  718. }
  719. return 0;
  720. case effGetProductString:
  721. if (char* const cptr = (char*)ptr)
  722. {
  723. #if defined(CARLA_VST_SHELL)
  724. if (validPlugin)
  725. {
  726. const NativePluginDescriptor* const desc = pluginPtr->getDescriptor();
  727. std::strncpy(cptr, desc->label, 32);
  728. }
  729. else
  730. {
  731. std::strncpy(cptr, "CarlaVstShell", 32);
  732. }
  733. #elif defined(CARLA_PLUGIN_32CH)
  734. std::strncpy(cptr, "CarlaPatchbay32", 32);
  735. #elif defined(CARLA_PLUGIN_16CH)
  736. std::strncpy(cptr, "CarlaPatchbay16", 32);
  737. #elif CARLA_PLUGIN_PATCHBAY
  738. # if CARLA_PLUGIN_SYNTH
  739. std::strncpy(cptr, "CarlaPatchbay", 32);
  740. # else
  741. std::strncpy(cptr, "CarlaPatchbayFX", 32);
  742. # endif
  743. #else
  744. # if CARLA_PLUGIN_SYNTH
  745. std::strncpy(cptr, "CarlaRack", 32);
  746. # else
  747. std::strncpy(cptr, "CarlaRackFX", 32);
  748. # endif
  749. #endif
  750. return 1;
  751. }
  752. return 0;
  753. case effGetVendorVersion:
  754. return CARLA_VERSION_HEX;
  755. case effGetVstVersion:
  756. return kVstVersion;
  757. #ifdef CARLA_VST_SHELL
  758. case effShellGetNextPlugin:
  759. if (char* const cptr = (char*)ptr)
  760. {
  761. CARLA_SAFE_ASSERT_RETURN(effect != nullptr, 0);
  762. CARLA_SAFE_ASSERT_RETURN(effect->uniqueID >= kShellUniqueID, 0);
  763. PluginListManager& plm(PluginListManager::getInstance());
  764. for (;;)
  765. {
  766. const uint index2 = static_cast<uint>(effect->uniqueID - kShellUniqueID);
  767. if (index2 >= plm.descs.count())
  768. {
  769. effect->uniqueID = kShellUniqueID;
  770. return 0;
  771. }
  772. const NativePluginDescriptor* const desc = plm.descs.getAt(index2, nullptr);
  773. CARLA_SAFE_ASSERT_RETURN(desc != nullptr, 0);
  774. ++effect->uniqueID;
  775. if (desc->midiIns > 1 || desc->midiOuts > 1)
  776. continue;
  777. std::strncpy(cptr, desc->label, 32);
  778. return effect->uniqueID;
  779. }
  780. }
  781. #endif
  782. };
  783. // handle advanced opcodes
  784. if (validPlugin)
  785. return pluginPtr->vst_dispatcher(opcode, index, value, ptr, opt);
  786. return 0;
  787. }
  788. float vst_getParameterCallback(AEffect* effect, int32_t index)
  789. {
  790. if (validPlugin)
  791. return pluginPtr->vst_getParameter(index);
  792. return 0.0f;
  793. }
  794. void vst_setParameterCallback(AEffect* effect, int32_t index, float value)
  795. {
  796. if (validPlugin)
  797. pluginPtr->vst_setParameter(index, value);
  798. }
  799. void vst_processCallback(AEffect* effect, float** inputs, float** outputs, int32_t sampleFrames)
  800. {
  801. if (validPlugin)
  802. pluginPtr->vst_processReplacing(const_cast<const float**>(inputs), outputs, sampleFrames);
  803. }
  804. void vst_processReplacingCallback(AEffect* effect, float** inputs, float** outputs, int32_t sampleFrames)
  805. {
  806. if (validPlugin)
  807. pluginPtr->vst_processReplacing(const_cast<const float**>(inputs), outputs, sampleFrames);
  808. }
  809. #undef pluginPtr
  810. #undef validObject
  811. #undef validPlugin
  812. #undef vstObjectPtr
  813. // -----------------------------------------------------------------------
  814. const AEffect* VSTPluginMainInit(AEffect* const effect)
  815. {
  816. #if defined(CARLA_VST_SHELL)
  817. if (const intptr_t uniqueID = VSTAudioMaster(effect, audioMasterCurrentId, 0, 0, nullptr, 0.0f))
  818. effect->uniqueID = static_cast<int>(uniqueID);
  819. else
  820. effect->uniqueID = kShellUniqueID;
  821. #elif defined(CARLA_PLUGIN_32CH)
  822. effect->uniqueID = kBaseUniqueID+6;
  823. #elif defined(CARLA_PLUGIN_16CH)
  824. effect->uniqueID = kBaseUniqueID+5;
  825. #elif CARLA_PLUGIN_PATCHBAY
  826. # if CARLA_PLUGIN_SYNTH
  827. effect->uniqueID = kBaseUniqueID+4;
  828. # else
  829. effect->uniqueID = kBaseUniqueID+3;
  830. # endif
  831. #else
  832. # if CARLA_PLUGIN_SYNTH
  833. effect->uniqueID = kBaseUniqueID+2;
  834. # else
  835. effect->uniqueID = kBaseUniqueID+1;
  836. # endif
  837. #endif
  838. // plugin fields
  839. effect->numParams = 0;
  840. effect->numPrograms = 0;
  841. #if defined(CARLA_VST_SHELL)
  842. effect->numInputs = 0;
  843. effect->numOutputs = 0;
  844. #elif defined(CARLA_PLUGIN_32CH)
  845. effect->numInputs = 32;
  846. effect->numOutputs = 32;
  847. #elif defined(CARLA_PLUGIN_16CH)
  848. effect->numInputs = 16;
  849. effect->numOutputs = 16;
  850. #else
  851. effect->numInputs = 2;
  852. effect->numOutputs = 2;
  853. #endif
  854. // plugin flags
  855. effect->flags |= effFlagsCanReplacing;
  856. effect->flags |= effFlagsProgramChunks;
  857. #ifndef CARLA_VST_SHELL
  858. effect->flags |= effFlagsHasEditor;
  859. #endif
  860. #if CARLA_PLUGIN_SYNTH
  861. effect->flags |= effFlagsIsSynth;
  862. #endif
  863. return effect;
  864. }
  865. // -----------------------------------------------------------------------