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.

974 lines
30KB

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