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