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.

1007 lines
31KB

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