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.

922 lines
29KB

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