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.

480 lines
15KB

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