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.

1009 lines
31KB

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