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.

475 lines
14KB

  1. /*
  2. * Carla Native Plugins
  3. * Copyright (C) 2012-2017 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. #include "CarlaNativeExtUI.hpp"
  18. #include "CarlaJuceUtils.hpp"
  19. #include "zita-rev1/jclient.cc"
  20. #include "zita-rev1/pareq.cc"
  21. #include "zita-rev1/reverb.cc"
  22. using namespace REV1;
  23. // -----------------------------------------------------------------------
  24. // REV1 Plugin
  25. class REV1Plugin : public NativePluginAndUiClass
  26. {
  27. public:
  28. enum Parameters {
  29. kParameterDELAY,
  30. kParameterXOVER,
  31. kParameterRTLOW,
  32. kParameterRTMID,
  33. kParameterFDAMP,
  34. kParameterEQ1FR,
  35. kParameterEQ1GN,
  36. kParameterEQ2FR,
  37. kParameterEQ2GN,
  38. kParameterOPMIXorRGXYZ,
  39. kParameterNROTARY
  40. };
  41. REV1Plugin(const NativeHostDescriptor* const host, const bool isAmbisonic)
  42. : NativePluginAndUiClass(host, "zita-rev1-ui"),
  43. kIsAmbisonic(isAmbisonic),
  44. kNumInputs(2),
  45. kNumOutputs(isAmbisonic ? 4 : 2),
  46. fJackClient(),
  47. jclient(nullptr)
  48. {
  49. CARLA_SAFE_ASSERT(host != nullptr);
  50. carla_zeroStruct(fJackClient);
  51. fJackClient.bufferSize = getBufferSize();
  52. fJackClient.sampleRate = getSampleRate();
  53. // set initial values
  54. fParameters[kParameterDELAY] = 0.04f;
  55. fParameters[kParameterXOVER] = 200.0f;
  56. fParameters[kParameterRTLOW] = 3.0f;
  57. fParameters[kParameterRTMID] = 2.0f;
  58. fParameters[kParameterFDAMP] = 6.0e3;
  59. fParameters[kParameterEQ1FR] = 160.0f;
  60. fParameters[kParameterEQ1GN] = 0.0f;
  61. fParameters[kParameterEQ2FR] = 2.5e3;
  62. fParameters[kParameterEQ2GN] = 0.0f;
  63. if (isAmbisonic)
  64. fParameters[kParameterOPMIXorRGXYZ] = 0.0f;
  65. else
  66. fParameters[kParameterOPMIXorRGXYZ] = 0.5f;
  67. _recreateZitaClient();
  68. }
  69. // -------------------------------------------------------------------
  70. // Plugin parameter calls
  71. uint32_t getParameterCount() const override
  72. {
  73. return kParameterNROTARY;
  74. }
  75. const NativeParameter* getParameterInfo(const uint32_t index) const override
  76. {
  77. CARLA_SAFE_ASSERT_RETURN(index < kParameterNROTARY, nullptr);
  78. static NativeParameter param;
  79. int hints = NATIVE_PARAMETER_IS_ENABLED|NATIVE_PARAMETER_IS_AUTOMABLE;
  80. // reset
  81. param.name = nullptr;
  82. param.unit = nullptr;
  83. param.ranges.def = 0.0f;
  84. param.ranges.min = 0.0f;
  85. param.ranges.max = 1.0f;
  86. param.ranges.step = 1.0f;
  87. param.ranges.stepSmall = 1.0f;
  88. param.ranges.stepLarge = 1.0f;
  89. param.scalePointCount = 0;
  90. param.scalePoints = nullptr;
  91. switch (index)
  92. {
  93. case kParameterDELAY:
  94. param.name = "Delay";
  95. param.ranges.def = 0.04f;
  96. param.ranges.min = 0.02f;
  97. param.ranges.max = 0.100f;
  98. break;
  99. case kParameterXOVER:
  100. hints |= NATIVE_PARAMETER_IS_LOGARITHMIC;
  101. param.name = "Crossover";
  102. param.ranges.def = 200.0f;
  103. param.ranges.min = 50.0f;
  104. param.ranges.max = 1000.0f;
  105. break;
  106. case kParameterRTLOW:
  107. hints |= NATIVE_PARAMETER_IS_LOGARITHMIC;
  108. param.name = "RT60 Low";
  109. param.ranges.def = 3.0f;
  110. param.ranges.min = 1.0f;
  111. param.ranges.max = 8.0f;
  112. break;
  113. case kParameterRTMID:
  114. hints |= NATIVE_PARAMETER_IS_LOGARITHMIC;
  115. param.name = "RT60 Mid";
  116. param.ranges.def = 2.0f;
  117. param.ranges.min = 1.0f;
  118. param.ranges.max = 8.0f;
  119. break;
  120. case kParameterFDAMP:
  121. hints |= NATIVE_PARAMETER_IS_LOGARITHMIC;
  122. param.name = "HF Damping";
  123. param.ranges.def = 6.0e3;
  124. param.ranges.min = 1.5e3;
  125. param.ranges.max = 24.0e3;
  126. break;
  127. case kParameterEQ1FR:
  128. hints |= NATIVE_PARAMETER_IS_LOGARITHMIC;
  129. param.name = "Eq1 Frequency";
  130. param.ranges.def = 160.0f;
  131. param.ranges.min = 40.0f;
  132. param.ranges.max = 2.5e3;
  133. break;
  134. case kParameterEQ1GN:
  135. param.name = "Eq1 Gain";
  136. param.ranges.def = 0.0f;
  137. param.ranges.min = -15.0;
  138. param.ranges.max = 15.0f;
  139. break;
  140. case kParameterEQ2FR:
  141. hints |= NATIVE_PARAMETER_IS_LOGARITHMIC;
  142. param.name = "Eq2 Frequency";
  143. param.ranges.def = 2.5e3;
  144. param.ranges.min = 160.0;
  145. param.ranges.max = 10e3;
  146. break;
  147. case kParameterEQ2GN:
  148. param.name = "Eq2 Gain";
  149. param.ranges.def = 0.0f;
  150. param.ranges.min = -15.0;
  151. param.ranges.max = 15.0f;
  152. break;
  153. case kParameterOPMIXorRGXYZ:
  154. if (kIsAmbisonic)
  155. {
  156. param.name = "XYZ gain";
  157. param.ranges.def = 0.0f;
  158. param.ranges.min = -9.0f;
  159. param.ranges.max = 9.0f;
  160. }
  161. else
  162. {
  163. param.name = "Dry/wet mix";
  164. param.ranges.def = 0.5f;
  165. param.ranges.min = 0.0f;
  166. param.ranges.max = 1.0f;
  167. }
  168. break;
  169. }
  170. param.hints = static_cast<NativeParameterHints>(hints);
  171. return &param;
  172. }
  173. float getParameterValue(const uint32_t index) const override
  174. {
  175. CARLA_SAFE_ASSERT_RETURN(index < kParameterNROTARY, 0.0f);
  176. return fParameters[index];
  177. }
  178. // -------------------------------------------------------------------
  179. // Plugin state calls
  180. void setParameterValue(const uint32_t index, const float value) override
  181. {
  182. CARLA_SAFE_ASSERT_RETURN(index < kParameterNROTARY,);
  183. Reverb* const reverb(jclient->reverb());
  184. fParameters[index] = value;
  185. switch (index)
  186. {
  187. case kParameterDELAY:
  188. reverb->set_delay(value);
  189. break;
  190. case kParameterXOVER:
  191. reverb->set_xover(value);
  192. break;
  193. case kParameterRTLOW:
  194. reverb->set_rtlow(value);
  195. break;
  196. case kParameterRTMID:
  197. reverb->set_rtmid(value);
  198. break;
  199. case kParameterFDAMP:
  200. reverb->set_fdamp(value);
  201. break;
  202. case kParameterEQ1FR:
  203. reverb->set_eq1(value, fParameters[kParameterEQ1GN]);
  204. break;
  205. case kParameterEQ1GN:
  206. reverb->set_eq1(fParameters[kParameterEQ1FR], value);
  207. break;
  208. case kParameterEQ2FR:
  209. reverb->set_eq2(value, fParameters[kParameterEQ2GN]);
  210. break;
  211. case kParameterEQ2GN:
  212. reverb->set_eq2(fParameters[kParameterEQ2FR], value);
  213. break;
  214. case kParameterOPMIXorRGXYZ:
  215. if (kIsAmbisonic)
  216. reverb->set_rgxyz(value);
  217. else
  218. reverb->set_opmix(value);
  219. break;
  220. }
  221. }
  222. // -------------------------------------------------------------------
  223. // Plugin process calls
  224. void process(float** const inBuffer, float** const outBuffer, const uint32_t frames, const NativeMidiEvent* const, const uint32_t) override
  225. {
  226. if (! fJackClient.active)
  227. {
  228. const int iframes(frames);
  229. for (uint32_t i=0; i<kNumInputs; ++i)
  230. carla_zeroFloats(outBuffer[i], iframes);
  231. return;
  232. }
  233. for (uint32_t i=0; i<kNumInputs; ++i)
  234. fJackClient.portsAudioIn[i].buffer.audio = inBuffer[i];
  235. for (uint32_t i=0; i<kNumOutputs; ++i)
  236. fJackClient.portsAudioOut[i].buffer.audio = outBuffer[i];
  237. fJackClient.processCallback(frames, fJackClient.processPtr);
  238. }
  239. // -------------------------------------------------------------------
  240. // Plugin dispatcher calls
  241. void bufferSizeChanged(const uint32_t bufferSize) override
  242. {
  243. fJackClient.bufferSize = bufferSize;
  244. // _recreateZitaClient(); // FIXME
  245. }
  246. void sampleRateChanged(const double sampleRate) override
  247. {
  248. fJackClient.sampleRate = sampleRate;
  249. // _recreateZitaClient(); // FIXME
  250. }
  251. // -------------------------------------------------------------------
  252. // Plugin UI calls
  253. void uiShow(const bool show) override
  254. {
  255. if (show)
  256. {
  257. if (isPipeRunning())
  258. {
  259. const CarlaMutexLocker cml(getPipeLock());
  260. writeMessage("focus\n", 6);
  261. flushMessages();
  262. return;
  263. }
  264. carla_stdout("Trying to start UI using \"%s\"", getExtUiPath());
  265. CarlaExternalUI::setData(getExtUiPath(), kIsAmbisonic ? "true" : "false", getUiName());
  266. if (! CarlaExternalUI::startPipeServer(true))
  267. {
  268. uiClosed();
  269. hostUiUnavailable();
  270. }
  271. }
  272. else
  273. {
  274. CarlaExternalUI::stopPipeServer(2000);
  275. }
  276. }
  277. // -------------------------------------------------------------------
  278. private:
  279. const bool kIsAmbisonic;
  280. const uint32_t kNumInputs;
  281. const uint32_t kNumOutputs;
  282. // Fake jack client
  283. jack_client_t fJackClient;
  284. // Zita stuff (core)
  285. ScopedPointer<Jclient> jclient;
  286. // Parameters
  287. float fParameters[kParameterNROTARY];
  288. void _recreateZitaClient()
  289. {
  290. jclient = new Jclient(&fJackClient, kIsAmbisonic);
  291. Reverb* const reverb(jclient->reverb());
  292. reverb->set_delay(fParameters[kParameterDELAY]);
  293. reverb->set_xover(fParameters[kParameterXOVER]);
  294. reverb->set_rtlow(fParameters[kParameterRTLOW]);
  295. reverb->set_rtmid(fParameters[kParameterRTMID]);
  296. reverb->set_fdamp(fParameters[kParameterFDAMP]);
  297. if (kIsAmbisonic)
  298. {
  299. reverb->set_opmix(0.5);
  300. reverb->set_rgxyz(fParameters[kParameterOPMIXorRGXYZ]);
  301. }
  302. else
  303. {
  304. reverb->set_opmix(fParameters[kParameterOPMIXorRGXYZ]);
  305. reverb->set_rgxyz(0.0);
  306. }
  307. reverb->set_eq1(fParameters[kParameterEQ1FR], fParameters[kParameterEQ1GN]);
  308. reverb->set_eq2(fParameters[kParameterEQ2FR], fParameters[kParameterEQ2GN]);
  309. }
  310. public:
  311. static NativePluginHandle _instantiateAmbisonic(const NativeHostDescriptor* host)
  312. {
  313. return (host != nullptr) ? new REV1Plugin(host, true) : nullptr;
  314. }
  315. static NativePluginHandle _instantiateStereo(const NativeHostDescriptor* host)
  316. {
  317. return (host != nullptr) ? new REV1Plugin(host, false) : nullptr;
  318. }
  319. static void _cleanup(NativePluginHandle handle)
  320. {
  321. delete (REV1Plugin*)handle;
  322. }
  323. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(REV1Plugin)
  324. };
  325. // -----------------------------------------------------------------------
  326. static const NativePluginDescriptor rev1AmbisonicDesc = {
  327. /* category */ NATIVE_PLUGIN_CATEGORY_DELAY,
  328. /* hints */ static_cast<NativePluginHints>(NATIVE_PLUGIN_IS_RTSAFE
  329. |NATIVE_PLUGIN_HAS_UI
  330. |NATIVE_PLUGIN_NEEDS_FIXED_BUFFERS),
  331. /* supports */ NATIVE_PLUGIN_SUPPORTS_NOTHING,
  332. /* audioIns */ 2,
  333. /* audioOuts */ 4,
  334. /* midiIns */ 0,
  335. /* midiOuts */ 0,
  336. /* paramIns */ REV1Plugin::kParameterNROTARY,
  337. /* paramOuts */ 0,
  338. /* name */ "REV1 (Ambisonic)",
  339. /* label */ "rev1-ambisonic",
  340. /* maker */ "falkTX, Fons Adriaensen",
  341. /* copyright */ "GPL v2+",
  342. REV1Plugin::_instantiateAmbisonic,
  343. REV1Plugin::_cleanup,
  344. REV1Plugin::_get_parameter_count,
  345. REV1Plugin::_get_parameter_info,
  346. REV1Plugin::_get_parameter_value,
  347. REV1Plugin::_get_midi_program_count,
  348. REV1Plugin::_get_midi_program_info,
  349. REV1Plugin::_set_parameter_value,
  350. REV1Plugin::_set_midi_program,
  351. REV1Plugin::_set_custom_data,
  352. REV1Plugin::_ui_show,
  353. REV1Plugin::_ui_idle,
  354. REV1Plugin::_ui_set_parameter_value,
  355. REV1Plugin::_ui_set_midi_program,
  356. REV1Plugin::_ui_set_custom_data,
  357. REV1Plugin::_activate,
  358. REV1Plugin::_deactivate,
  359. REV1Plugin::_process,
  360. REV1Plugin::_get_state,
  361. REV1Plugin::_set_state,
  362. REV1Plugin::_dispatcher
  363. };
  364. static const NativePluginDescriptor rev1StereoDesc = {
  365. /* category */ NATIVE_PLUGIN_CATEGORY_DELAY,
  366. /* hints */ static_cast<NativePluginHints>(NATIVE_PLUGIN_IS_RTSAFE
  367. |NATIVE_PLUGIN_HAS_UI
  368. |NATIVE_PLUGIN_NEEDS_FIXED_BUFFERS),
  369. /* supports */ NATIVE_PLUGIN_SUPPORTS_NOTHING,
  370. /* audioIns */ 2,
  371. /* audioOuts */ 2,
  372. /* midiIns */ 0,
  373. /* midiOuts */ 0,
  374. /* paramIns */ REV1Plugin::kParameterNROTARY,
  375. /* paramOuts */ 0,
  376. /* name */ "REV1 (Stereo)",
  377. /* label */ "rev1-stereo",
  378. /* maker */ "falkTX, Fons Adriaensen",
  379. /* copyright */ "GPL v2+",
  380. REV1Plugin::_instantiateStereo,
  381. REV1Plugin::_cleanup,
  382. REV1Plugin::_get_parameter_count,
  383. REV1Plugin::_get_parameter_info,
  384. REV1Plugin::_get_parameter_value,
  385. REV1Plugin::_get_midi_program_count,
  386. REV1Plugin::_get_midi_program_info,
  387. REV1Plugin::_set_parameter_value,
  388. REV1Plugin::_set_midi_program,
  389. REV1Plugin::_set_custom_data,
  390. REV1Plugin::_ui_show,
  391. REV1Plugin::_ui_idle,
  392. REV1Plugin::_ui_set_parameter_value,
  393. REV1Plugin::_ui_set_midi_program,
  394. REV1Plugin::_ui_set_custom_data,
  395. REV1Plugin::_activate,
  396. REV1Plugin::_deactivate,
  397. REV1Plugin::_process,
  398. REV1Plugin::_get_state,
  399. REV1Plugin::_set_state,
  400. REV1Plugin::_dispatcher
  401. };
  402. // -----------------------------------------------------------------------
  403. CARLA_EXPORT
  404. void carla_register_native_plugin_zita_rev1();
  405. CARLA_EXPORT
  406. void carla_register_native_plugin_zita_rev1()
  407. {
  408. carla_register_native_plugin(&rev1AmbisonicDesc);
  409. carla_register_native_plugin(&rev1StereoDesc);
  410. }
  411. // -----------------------------------------------------------------------