Collection of tools useful for audio production
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.

512 lines
13KB

  1. /*
  2. * Carla UI bridge code
  3. * Copyright (C) 2011-2012 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * 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 COPYING file
  16. */
  17. #ifdef BRIDGE_VST
  18. #include "carla_bridge_client.h"
  19. #include "carla_vst.h"
  20. #include "carla_midi.h"
  21. #include <QtCore/QTimerEvent>
  22. #include <QtGui/QDialog>
  23. #ifdef Q_WS_X11
  24. #include <QtGui/QX11Info>
  25. #endif
  26. CARLA_BRIDGE_START_NAMESPACE
  27. // -------------------------------------------------------------------------
  28. // fake values
  29. uint32_t bufferSize = 512;
  30. double sampleRate = 44100.0;
  31. class CarlaVstClient : public CarlaClient, QObject
  32. {
  33. public:
  34. CarlaVstClient(CarlaToolkit* const toolkit)
  35. : CarlaClient(toolkit),
  36. QObject(nullptr)
  37. {
  38. effect = nullptr;
  39. idleTimer = 0;
  40. // make client valid
  41. unique1 = unique2 = rand();
  42. }
  43. ~CarlaVstClient()
  44. {
  45. // make client invalid
  46. unique2 += 1;
  47. }
  48. // ---------------------------------------------------------------------
  49. // ui initialization
  50. bool init(const char* binary, const char*)
  51. {
  52. // -----------------------------------------------------------------
  53. // open DLL
  54. if (! libOpen(binary))
  55. {
  56. qWarning("%s", libError());
  57. return false;
  58. }
  59. // -----------------------------------------------------------------
  60. // get DLL main entry
  61. VST_Function vstFn = (VST_Function)libSymbol("VSTPluginMain");
  62. if (! vstFn)
  63. vstFn = (VST_Function)libSymbol("main");
  64. if (! vstFn)
  65. return false;
  66. // -----------------------------------------------------------------
  67. // initialize plugin
  68. effect = vstFn(hostCallback);
  69. if (! (effect && effect->magic == kEffectMagic))
  70. return false;
  71. // -----------------------------------------------------------------
  72. // initialize VST stuff
  73. #ifdef VESTIGE_HEADER
  74. effect->ptr1 = this;
  75. #else
  76. effect->resvd1 = (intptr_t)this;
  77. #endif
  78. int32_t value = 0;
  79. #ifdef Q_WS_X11
  80. value = (int64_t)QX11Info::display();
  81. #endif
  82. effect->dispatcher(effect, effOpen, 0, 0, nullptr, 0.0f);
  83. #if ! VST_FORCE_DEPRECATED
  84. effect->dispatcher(effect, effSetBlockSizeAndSampleRate, 0, bufferSize, nullptr, sampleRate);
  85. #endif
  86. effect->dispatcher(effect, effSetSampleRate, 0, 0, nullptr, sampleRate);
  87. effect->dispatcher(effect, effSetBlockSize, 0, bufferSize, nullptr, 0.0f);
  88. effect->dispatcher(effect, effSetProcessPrecision, 0, kVstProcessPrecision32, nullptr, 0.0f);
  89. if (effect->dispatcher(effect, effEditOpen, 0, value, getContainerId(), 0.0f) != 1)
  90. {
  91. effect->dispatcher(effect, effClose, 0, 0, nullptr, 0.0f);
  92. return false;
  93. }
  94. // -----------------------------------------------------------------
  95. // initialize gui stuff
  96. ERect* vstRect = nullptr;
  97. effect->dispatcher(effect, effEditGetRect, 0, 0, &vstRect, 0.0f);
  98. if (vstRect)
  99. {
  100. int width = vstRect->right - vstRect->left;
  101. int height = vstRect->bottom - vstRect->top;
  102. if (width > 0 && height > 0)
  103. toolkitResize(width, height);
  104. }
  105. idleTimer = startTimer(50);
  106. return true;
  107. }
  108. void close()
  109. {
  110. if (effect)
  111. {
  112. effect->dispatcher(effect, effEditClose, 0, 0, nullptr, 0.0f);
  113. effect->dispatcher(effect, effClose, 0, 0, nullptr, 0.0f);
  114. }
  115. }
  116. // ---------------------------------------------------------------------
  117. // ui management
  118. void* getWidget() const
  119. {
  120. return nullptr; // VST always uses reparent
  121. }
  122. bool isResizable() const
  123. {
  124. return false;
  125. }
  126. bool needsReparent() const
  127. {
  128. return true;
  129. }
  130. // ---------------------------------------------------------------------
  131. // processing
  132. void setParameter(const int32_t rindex, const double value)
  133. {
  134. if (effect)
  135. effect->setParameter(effect, rindex, value);
  136. }
  137. void setProgram(const uint32_t index)
  138. {
  139. if (effect)
  140. effect->dispatcher(effect, effSetProgram, 0, index, nullptr, 0.0f);
  141. }
  142. void setMidiProgram(const uint32_t, const uint32_t)
  143. {
  144. }
  145. void noteOn(const uint8_t, const uint8_t, const uint8_t)
  146. {
  147. }
  148. void noteOff(const uint8_t, const uint8_t)
  149. {
  150. }
  151. // ---------------------------------------------------------------------
  152. void handleAudioMasterAutomate(const uint32_t index, const float value)
  153. {
  154. effect->setParameter(effect, index, value);
  155. sendOscControl(index, value);
  156. }
  157. intptr_t handleAudioMasterGetCurrentProcessLevel()
  158. {
  159. return kVstProcessLevelUser;
  160. }
  161. intptr_t handleAudioMasterProcessEvents(const VstEvents* const vstEvents)
  162. {
  163. for (int32_t i=0; i < vstEvents->numEvents; i++)
  164. {
  165. if (! vstEvents->events[i])
  166. break;
  167. const VstMidiEvent* const vstMidiEvent = (const VstMidiEvent*)vstEvents->events[i];
  168. if (vstMidiEvent->type != kVstMidiType)
  169. {
  170. uint8_t status = vstMidiEvent->midiData[0];
  171. // Fix bad note-off
  172. if (MIDI_IS_STATUS_NOTE_ON(status) && vstMidiEvent->midiData[2] == 0)
  173. status -= 0x10;
  174. uint8_t midiBuf[4] = { 0, status, (uint8_t)vstMidiEvent->midiData[1], (uint8_t)vstMidiEvent->midiData[2] };
  175. sendOscMidi(midiBuf);
  176. }
  177. }
  178. return 1;
  179. }
  180. intptr_t handleAdioMasterSizeWindow(int32_t width, int32_t height)
  181. {
  182. toolkitResize(width, height);
  183. return 1;
  184. }
  185. void handleAudioMasterUpdateDisplay()
  186. {
  187. sendOscConfigure("reloadprograms", "");
  188. }
  189. // ---------------------------------------------------------------------
  190. static intptr_t hostCanDo(const char* const feature)
  191. {
  192. qDebug("CarlaVstClient::hostCanDo(\"%s\")", feature);
  193. if (strcmp(feature, "supplyIdle") == 0)
  194. return 1;
  195. if (strcmp(feature, "sendVstEvents") == 0)
  196. return 1;
  197. if (strcmp(feature, "sendVstMidiEvent") == 0)
  198. return 1;
  199. if (strcmp(feature, "sendVstMidiEventFlagIsRealtime") == 0)
  200. return -1;
  201. if (strcmp(feature, "sendVstTimeInfo") == 0)
  202. return 1;
  203. if (strcmp(feature, "receiveVstEvents") == 0)
  204. return 1;
  205. if (strcmp(feature, "receiveVstMidiEvent") == 0)
  206. return 1;
  207. if (strcmp(feature, "receiveVstTimeInfo") == 0)
  208. return -1;
  209. if (strcmp(feature, "reportConnectionChanges") == 0)
  210. return -1;
  211. if (strcmp(feature, "acceptIOChanges") == 0)
  212. return 1;
  213. if (strcmp(feature, "sizeWindow") == 0)
  214. return 1;
  215. if (strcmp(feature, "offline") == 0)
  216. return -1;
  217. if (strcmp(feature, "openFileSelector") == 0)
  218. return -1;
  219. if (strcmp(feature, "closeFileSelector") == 0)
  220. return -1;
  221. if (strcmp(feature, "startStopProcess") == 0)
  222. return 1;
  223. if (strcmp(feature, "supportShell") == 0)
  224. return -1;
  225. if (strcmp(feature, "shellCategory") == 0)
  226. return -1;
  227. // unimplemented
  228. qWarning("CarlaVstClient::hostCanDo(\"%s\") - unknown feature", feature);
  229. return 0;
  230. }
  231. static intptr_t VSTCALLBACK hostCallback(AEffect* const effect, const int32_t opcode, const int32_t index, const intptr_t value, void* const ptr, const float opt)
  232. {
  233. #if DEBUG
  234. qDebug("CarlaVstClient::hostCallback(%p, %s, %i, " P_INTPTR ", %p, %f", effect, vstMasterOpcode2str(opcode), index, value, ptr, opt);
  235. #endif
  236. // Check if 'resvd1' points to this client
  237. CarlaVstClient* self = nullptr;
  238. #ifdef VESTIGE_HEADER
  239. if (effect && effect->ptr1)
  240. {
  241. self = (CarlaVstClient*)effect->ptr1;
  242. #else
  243. if (effect && effect->resvd1)
  244. {
  245. self = (CarlaVstClient*)effect->resvd1;
  246. #endif
  247. if (self->unique1 != self->unique2)
  248. self = nullptr;
  249. }
  250. intptr_t ret = 0;
  251. switch (opcode)
  252. {
  253. case audioMasterAutomate:
  254. if (self)
  255. self->handleAudioMasterAutomate(index, opt);
  256. break;
  257. case audioMasterVersion:
  258. ret = kVstVersion;
  259. break;
  260. case audioMasterCurrentId:
  261. // TODO
  262. break;
  263. case audioMasterIdle:
  264. //if (effect)
  265. // effect->dispatcher(effect, effEditIdle, 0, 0, nullptr, 0.0f);
  266. break;
  267. case audioMasterGetTime:
  268. static VstTimeInfo_R timeInfo;
  269. memset(&timeInfo, 0, sizeof(VstTimeInfo_R));
  270. timeInfo.sampleRate = sampleRate;
  271. // Tempo
  272. timeInfo.tempo = 120.0;
  273. timeInfo.flags |= kVstTempoValid;
  274. // Time Signature
  275. timeInfo.timeSigNumerator = 4;
  276. timeInfo.timeSigDenominator = 4;
  277. timeInfo.flags |= kVstTimeSigValid;
  278. ret = (intptr_t)&timeInfo;
  279. break;
  280. case audioMasterProcessEvents:
  281. if (self && ptr)
  282. ret = self->handleAudioMasterProcessEvents((const VstEvents*)ptr);
  283. break;
  284. #if ! VST_FORCE_DEPRECATED
  285. case audioMasterTempoAt:
  286. // Deprecated in VST SDK 2.4
  287. ret = 120 * 10000;
  288. break;
  289. #endif
  290. case audioMasterSizeWindow:
  291. if (self && index > 0 && value > 0)
  292. ret = self->handleAdioMasterSizeWindow(index, value);
  293. break;
  294. case audioMasterGetSampleRate:
  295. ret = sampleRate;
  296. break;
  297. case audioMasterGetBlockSize:
  298. ret = bufferSize;
  299. break;
  300. case audioMasterGetCurrentProcessLevel:
  301. ret = kVstProcessLevelUser;
  302. break;
  303. case audioMasterGetAutomationState:
  304. ret = kVstAutomationReadWrite;
  305. break;
  306. case audioMasterGetVendorString:
  307. if (ptr)
  308. strcpy((char*)ptr, "Cadence");
  309. break;
  310. case audioMasterGetProductString:
  311. if (ptr)
  312. strcpy((char*)ptr, "Carla-Bridge");
  313. break;
  314. case audioMasterGetVendorVersion:
  315. ret = 0x050; // 0.5.0
  316. break;
  317. case audioMasterCanDo:
  318. if (ptr)
  319. ret = hostCanDo((const char*)ptr);
  320. break;
  321. case audioMasterGetLanguage:
  322. ret = kVstLangEnglish;
  323. break;
  324. case audioMasterUpdateDisplay:
  325. if (self)
  326. self->handleAudioMasterUpdateDisplay();
  327. break;
  328. default:
  329. #ifdef DEBUG
  330. qDebug("CarlaVstClient::hostCallback(%p, %s, %i, " P_INTPTR ", %p, %f", effect, vstMasterOpcode2str(opcode), index, value, ptr, opt);
  331. #endif
  332. break;
  333. }
  334. return ret;
  335. }
  336. protected:
  337. void timerEvent(QTimerEvent* const event)
  338. {
  339. if (event->timerId() == idleTimer && effect)
  340. {
  341. effect->dispatcher(effect, effIdle, 0, 0, nullptr, 0.0f);
  342. effect->dispatcher(effect, effEditIdle, 0, 0, nullptr, 0.0f);
  343. }
  344. QObject::timerEvent(event);
  345. }
  346. private:
  347. int unique1;
  348. AEffect* effect;
  349. int idleTimer;
  350. int unique2;
  351. };
  352. CARLA_BRIDGE_END_NAMESPACE
  353. int main(int argc, char* argv[])
  354. {
  355. using namespace CarlaBridge;
  356. if (argc != 4)
  357. {
  358. qWarning("usage: %s <osc-url|\"null\"> <binary> <ui-title>", argv[0]);
  359. return 1;
  360. }
  361. const char* oscUrl = argv[1];
  362. const char* binary = argv[2];
  363. const char* uiTitle = argv[3];
  364. const bool useOsc = strcmp(oscUrl, "null");
  365. // try to get sampleRate value
  366. const char* const sampleRateStr = getenv("CARLA_SAMPLE_RATE");
  367. if (sampleRateStr)
  368. sampleRate = atof(sampleRateStr);
  369. // Init toolkit
  370. CarlaToolkit* const toolkit = CarlaToolkit::createNew(uiTitle);
  371. toolkit->init();
  372. // Init VST-UI
  373. CarlaVstClient client(toolkit);
  374. // Init OSC
  375. if (useOsc && ! client.oscInit(oscUrl))
  376. {
  377. toolkit->quit();
  378. delete toolkit;
  379. return -1;
  380. }
  381. // Load UI
  382. int ret;
  383. if (client.init(binary, nullptr))
  384. {
  385. toolkit->exec(&client, !useOsc);
  386. ret = 0;
  387. }
  388. else
  389. {
  390. qCritical("Failed to load VST UI");
  391. ret = 1;
  392. }
  393. // Close OSC
  394. if (useOsc)
  395. {
  396. client.oscClose();
  397. }
  398. // Close VST-UI
  399. client.close();
  400. // Close toolkit
  401. toolkit->quit();
  402. delete toolkit;
  403. return ret;
  404. }
  405. #endif