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.

1074 lines
34KB

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