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.

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