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.

1108 lines
35KB

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