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.

964 lines
29KB

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