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.

481 lines
15KB

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