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.

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