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.

979 lines
29KB

  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 (sampleRate != 0.0 && 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. // 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. 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. break;
  434. case NATIVE_HOST_OPCODE_HOST_IDLE:
  435. hostCallback(audioMasterIdle);
  436. break;
  437. }
  438. // unused for now
  439. return 0;
  440. (void)index; (void)value; (void)ptr; (void)opt;
  441. }
  442. private:
  443. // VST stuff
  444. const audioMasterCallback fAudioMaster;
  445. AEffect* const fEffect;
  446. // Native data
  447. NativePluginHandle fHandle;
  448. NativeHostDescriptor fHost;
  449. const NativePluginDescriptor* const fDescriptor;
  450. // VST host data
  451. uint32_t fBufferSize;
  452. double fSampleRate;
  453. // Temporary data
  454. bool fIsActive;
  455. uint32_t fMidiEventCount;
  456. NativeMidiEvent fMidiEvents[kMaxMidiEvents];
  457. NativeTimeInfo fTimeInfo;
  458. ERect fVstRect;
  459. // Host data
  460. enum HostType {
  461. kHostTypeNull = 0,
  462. kHostTypeBitwig
  463. };
  464. HostType fHostType;
  465. // host callback
  466. intptr_t hostCallback(const int32_t opcode,
  467. const int32_t index = 0,
  468. const intptr_t value = 0,
  469. void* const ptr = nullptr,
  470. const float opt = 0.0f)
  471. {
  472. return fAudioMaster(fEffect, opcode, index, value, ptr, opt);
  473. }
  474. struct FixedVstEvents {
  475. int32_t numEvents;
  476. intptr_t reserved;
  477. VstEvent* data[kMaxMidiEvents];
  478. VstMidiEvent mdata[kMaxMidiEvents];
  479. FixedVstEvents()
  480. : numEvents(0),
  481. reserved(0)
  482. {
  483. for (uint32_t i=0; i<kMaxMidiEvents; ++i)
  484. data[i] = (VstEvent*)&mdata[i];
  485. carla_zeroStructs(mdata, kMaxMidiEvents);
  486. }
  487. CARLA_DECLARE_NON_COPY_STRUCT(FixedVstEvents);
  488. } fMidiOutEvents;
  489. char* fStateChunk;
  490. SharedResourcePointer<ScopedJuceInitialiser_GUI> sJuceInitialiser;
  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. #ifdef VESTIGE_HEADER
  547. # define validObject effect != nullptr && effect->ptr3 != nullptr
  548. # define validPlugin effect != nullptr && effect->ptr3 != nullptr && ((VstObject*)effect->ptr3)->plugin != nullptr
  549. # define vstObjectPtr (VstObject*)effect->ptr3
  550. #else
  551. # define validObject effect != nullptr && effect->object != nullptr
  552. # define validPlugin effect != nullptr && effect->object != nullptr && ((VstObject*)effect->object)->plugin != nullptr
  553. # define vstObjectPtr (VstObject*)effect->object
  554. #endif
  555. #define pluginPtr (vstObjectPtr)->plugin
  556. static intptr_t vst_dispatcherCallback(AEffect* effect, int32_t opcode, int32_t index, intptr_t value, void* ptr, float opt)
  557. {
  558. // handle base opcodes
  559. switch (opcode)
  560. {
  561. case effOpen:
  562. if (VstObject* const obj = vstObjectPtr)
  563. {
  564. // this must always be valid
  565. CARLA_SAFE_ASSERT_RETURN(obj->audioMaster != nullptr, 0);
  566. // some hosts call effOpen twice
  567. CARLA_SAFE_ASSERT_RETURN(obj->plugin == nullptr, 1);
  568. audioMasterCallback audioMaster = (audioMasterCallback)obj->audioMaster;
  569. d_lastBufferSize = static_cast<uint32_t>(audioMaster(effect, audioMasterGetBlockSize, 0, 0, nullptr, 0.0f));
  570. d_lastSampleRate = static_cast<double>(audioMaster(effect, audioMasterGetSampleRate, 0, 0, nullptr, 0.0f));
  571. // some hosts are not ready at this point or return 0 buffersize/samplerate
  572. if (d_lastBufferSize == 0)
  573. d_lastBufferSize = 2048;
  574. if (d_lastSampleRate <= 0.0)
  575. d_lastSampleRate = 44100.0;
  576. const NativePluginDescriptor* pluginDesc = nullptr;
  577. #if CARLA_PLUGIN_PATCHBAY
  578. # if CARLA_PLUGIN_16CH
  579. const char* const pluginLabel = "carlapatchbay16";
  580. # else
  581. const char* const pluginLabel = "carlapatchbay";
  582. # endif
  583. #else
  584. const char* const pluginLabel = "carlarack";
  585. #endif
  586. PluginListManager& plm(PluginListManager::getInstance());
  587. for (LinkedList<const NativePluginDescriptor*>::Itenerator it = plm.descs.begin2(); it.valid(); it.next())
  588. {
  589. const NativePluginDescriptor* const& tmpDesc(it.getValue());
  590. if (std::strcmp(tmpDesc->label, pluginLabel) == 0)
  591. {
  592. pluginDesc = tmpDesc;
  593. break;
  594. }
  595. }
  596. CARLA_SAFE_ASSERT_RETURN(pluginDesc != nullptr, 0);
  597. obj->plugin = new NativePlugin(audioMaster, effect, pluginDesc);
  598. return 1;
  599. }
  600. return 0;
  601. case effClose:
  602. if (VstObject* const obj = vstObjectPtr)
  603. {
  604. NativePlugin* const plugin(obj->plugin);
  605. if (plugin != nullptr)
  606. {
  607. obj->plugin = nullptr;
  608. delete plugin;
  609. }
  610. #if 0
  611. /* This code invalidates the object created in VSTPluginMain
  612. * Probably not safe against all hosts */
  613. obj->audioMaster = nullptr;
  614. # ifdef VESTIGE_HEADER
  615. effect->ptr3 = nullptr;
  616. # else
  617. vstObjectPtr = nullptr;
  618. # endif
  619. delete obj;
  620. #endif
  621. return 1;
  622. }
  623. //delete effect;
  624. return 0;
  625. case effGetPlugCategory:
  626. #if CARLA_PLUGIN_SYNTH
  627. return kPlugCategSynth;
  628. #else
  629. return kPlugCategEffect;
  630. #endif
  631. case effGetEffectName:
  632. if (char* const cptr = (char*)ptr)
  633. {
  634. #if CARLA_PLUGIN_PATCHBAY
  635. # if CARLA_PLUGIN_SYNTH
  636. # if CARLA_PLUGIN_16CH
  637. std::strncpy(cptr, "Carla-Patchbay16", 32);
  638. # else
  639. std::strncpy(cptr, "Carla-Patchbay", 32);
  640. # endif
  641. # else
  642. std::strncpy(cptr, "Carla-PatchbayFX", 32);
  643. # endif
  644. #else
  645. # if CARLA_PLUGIN_SYNTH
  646. std::strncpy(cptr, "Carla-Rack", 32);
  647. # else
  648. std::strncpy(cptr, "Carla-RackFX", 32);
  649. # endif
  650. #endif
  651. return 1;
  652. }
  653. return 0;
  654. case effGetVendorString:
  655. if (char* const cptr = (char*)ptr)
  656. {
  657. std::strncpy(cptr, "falkTX", 32);
  658. return 1;
  659. }
  660. return 0;
  661. case effGetProductString:
  662. if (char* const cptr = (char*)ptr)
  663. {
  664. #if CARLA_PLUGIN_PATCHBAY
  665. # if CARLA_PLUGIN_SYNTH
  666. # if CARLA_PLUGIN_16CH
  667. std::strncpy(cptr, "CarlaPatchbay16", 32);
  668. # else
  669. std::strncpy(cptr, "CarlaPatchbay", 32);
  670. # endif
  671. # else
  672. std::strncpy(cptr, "CarlaPatchbayFX", 32);
  673. # endif
  674. #else
  675. # if CARLA_PLUGIN_SYNTH
  676. std::strncpy(cptr, "CarlaRack", 32);
  677. # else
  678. std::strncpy(cptr, "CarlaRackFX", 32);
  679. # endif
  680. #endif
  681. return 1;
  682. }
  683. return 0;
  684. case effGetVendorVersion:
  685. return CARLA_VERSION_HEX;
  686. case effGetVstVersion:
  687. return kVstVersion;
  688. };
  689. // handle advanced opcodes
  690. if (validPlugin)
  691. return pluginPtr->vst_dispatcher(opcode, index, value, ptr, opt);
  692. return 0;
  693. }
  694. static float vst_getParameterCallback(AEffect* effect, int32_t index)
  695. {
  696. if (validPlugin)
  697. return pluginPtr->vst_getParameter(index);
  698. return 0.0f;
  699. }
  700. static void vst_setParameterCallback(AEffect* effect, int32_t index, float value)
  701. {
  702. if (validPlugin)
  703. pluginPtr->vst_setParameter(index, value);
  704. }
  705. static void vst_processCallback(AEffect* effect, float** inputs, float** outputs, int32_t sampleFrames)
  706. {
  707. if (validPlugin)
  708. pluginPtr->vst_processReplacing(const_cast<const float**>(inputs), outputs, sampleFrames);
  709. }
  710. static void vst_processReplacingCallback(AEffect* effect, float** inputs, float** outputs, int32_t sampleFrames)
  711. {
  712. if (validPlugin)
  713. pluginPtr->vst_processReplacing(const_cast<const float**>(inputs), outputs, sampleFrames);
  714. }
  715. #undef pluginPtr
  716. #undef validObject
  717. #undef validPlugin
  718. #undef vstObjectPtr
  719. // -----------------------------------------------------------------------
  720. CARLA_EXPORT
  721. #if defined(CARLA_OS_WIN) || defined(CARLA_OS_MAC)
  722. const AEffect* VSTPluginMain(audioMasterCallback audioMaster);
  723. #else
  724. const AEffect* VSTPluginMain(audioMasterCallback audioMaster) asm ("main");
  725. #endif
  726. CARLA_EXPORT
  727. const AEffect* VSTPluginMain(audioMasterCallback audioMaster)
  728. {
  729. // old version
  730. if (audioMaster(nullptr, audioMasterVersion, 0, 0, nullptr, 0.0f) == 0)
  731. return nullptr;
  732. AEffect* const effect(new AEffect);
  733. std::memset(effect, 0, sizeof(AEffect));
  734. // vst fields
  735. effect->magic = kEffectMagic;
  736. #ifdef VESTIGE_HEADER
  737. int32_t* const version = (int32_t*)&effect->unknown1;
  738. *version = CARLA_VERSION_HEX;
  739. #else
  740. effect->version = CARLA_VERSION_HEX;
  741. #endif
  742. static const int32_t uniqueId = CCONST('C', 'r', 'l', 'a');
  743. #if CARLA_PLUGIN_SYNTH
  744. # if CARLA_PLUGIN_PATCHBAY
  745. # if CARLA_PLUGIN_16CH
  746. effect->uniqueID = uniqueId+5;
  747. # else
  748. effect->uniqueID = uniqueId+4;
  749. # endif
  750. # else
  751. effect->uniqueID = uniqueId+3;
  752. # endif
  753. #else
  754. # if CARLA_PLUGIN_PATCHBAY
  755. effect->uniqueID = uniqueId+2;
  756. # else
  757. effect->uniqueID = uniqueId+1;
  758. # endif
  759. #endif
  760. // plugin fields
  761. effect->numParams = 0;
  762. effect->numPrograms = 0;
  763. #if CARLA_PLUGIN_16CH
  764. effect->numInputs = 16;
  765. effect->numOutputs = 16;
  766. #else
  767. effect->numInputs = 2;
  768. effect->numOutputs = 2;
  769. #endif
  770. // plugin flags
  771. effect->flags |= effFlagsCanReplacing;
  772. effect->flags |= effFlagsHasEditor;
  773. effect->flags |= effFlagsProgramChunks;
  774. #if CARLA_PLUGIN_SYNTH
  775. effect->flags |= effFlagsIsSynth;
  776. #endif
  777. // static calls
  778. effect->dispatcher = vst_dispatcherCallback;
  779. effect->process = vst_processCallback;
  780. effect->getParameter = vst_getParameterCallback;
  781. effect->setParameter = vst_setParameterCallback;
  782. effect->processReplacing = vst_processReplacingCallback;
  783. // pointers
  784. VstObject* const obj(new VstObject());
  785. obj->audioMaster = audioMaster;
  786. obj->plugin = nullptr;
  787. #ifdef VESTIGE_HEADER
  788. effect->ptr3 = obj;
  789. #else
  790. effect->object = obj;
  791. #endif
  792. return effect;
  793. }
  794. // -----------------------------------------------------------------------