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.

443 lines
13KB

  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-at1/jclient.cc"
  20. #include "zita-at1/retuner.cc"
  21. #include "water/maths/MathsFunctions.h"
  22. using water::roundToIntAccurate;
  23. using namespace AT1;
  24. // -----------------------------------------------------------------------
  25. // AT1 Plugin
  26. class AT1Plugin : public NativePluginAndUiClass
  27. {
  28. public:
  29. enum Parameters {
  30. // rotary knobs
  31. kParameterR_TUNE,
  32. kParameterR_FILT,
  33. kParameterR_BIAS,
  34. kParameterR_CORR,
  35. kParameterR_OFFS,
  36. // knob count
  37. kParameterNROTARY,
  38. // midi channel
  39. kParameterM_CHANNEL = kParameterNROTARY,
  40. // final count
  41. kNumParameters
  42. };
  43. AT1Plugin(const NativeHostDescriptor* const host)
  44. : NativePluginAndUiClass(host, "at1-ui"),
  45. fJackClient(),
  46. jclient(nullptr),
  47. notemask(0xfff)
  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[kParameterR_TUNE] = 440.0f;
  55. fParameters[kParameterR_FILT] = 0.1f;
  56. fParameters[kParameterR_BIAS] = 0.5f;
  57. fParameters[kParameterR_CORR] = 1.0f;
  58. fParameters[kParameterR_OFFS] = 0.0f;
  59. fParameters[kParameterM_CHANNEL] = 0.0f;
  60. _recreateZitaClient();
  61. }
  62. // -------------------------------------------------------------------
  63. // Plugin parameter calls
  64. uint32_t getParameterCount() const override
  65. {
  66. return kNumParameters;
  67. }
  68. const NativeParameter* getParameterInfo(const uint32_t index) const override
  69. {
  70. CARLA_SAFE_ASSERT_RETURN(index < kNumParameters, nullptr);
  71. static NativeParameter param;
  72. static NativeParameterScalePoint scalePoints[17];
  73. int hints = NATIVE_PARAMETER_IS_ENABLED|NATIVE_PARAMETER_IS_AUTOMABLE;
  74. // reset
  75. param.name = nullptr;
  76. param.unit = nullptr;
  77. param.ranges.def = 0.0f;
  78. param.ranges.min = 0.0f;
  79. param.ranges.max = 1.0f;
  80. param.ranges.step = 1.0f;
  81. param.ranges.stepSmall = 1.0f;
  82. param.ranges.stepLarge = 1.0f;
  83. param.scalePointCount = 0;
  84. param.scalePoints = nullptr;
  85. switch (index)
  86. {
  87. case kParameterR_TUNE:
  88. param.name = "Tuning";
  89. param.ranges.def = 440.0f;
  90. param.ranges.min = 400.0f;
  91. param.ranges.max = 480.0f;
  92. break;
  93. case kParameterR_FILT:
  94. hints |= NATIVE_PARAMETER_IS_LOGARITHMIC;
  95. param.name = "Filter";
  96. param.ranges.def = 0.1f;
  97. param.ranges.min = 0.02f;
  98. param.ranges.max = 0.5f;
  99. break;
  100. case kParameterR_BIAS:
  101. param.name = "Bias";
  102. param.ranges.def = 0.5f;
  103. param.ranges.min = 0.0f;
  104. param.ranges.max = 1.0f;
  105. break;
  106. case kParameterR_CORR:
  107. param.name = "Correction";
  108. param.ranges.def = 1.0f;
  109. param.ranges.min = 0.0f;
  110. param.ranges.max = 1.0f;
  111. break;
  112. case kParameterR_OFFS:
  113. param.name = "Offset";
  114. param.ranges.def = 0.0f;
  115. param.ranges.min = -2.0f;
  116. param.ranges.max = 2.0f;
  117. break;
  118. case kParameterM_CHANNEL:
  119. hints |= NATIVE_PARAMETER_USES_SCALEPOINTS;
  120. param.name = "MIDI Channel";
  121. param.ranges.def = 0.0f;
  122. param.ranges.min = 0.0f;
  123. param.ranges.max = 16.0f;
  124. param.scalePointCount = 17;
  125. param.scalePoints = scalePoints;
  126. scalePoints[ 0].value = 0.0f;
  127. scalePoints[ 0].label = "Omni";
  128. scalePoints[ 1].value = 1.0f;
  129. scalePoints[ 1].label = "1";
  130. scalePoints[ 2].value = 2.0f;
  131. scalePoints[ 2].label = "2";
  132. scalePoints[ 3].value = 3.0f;
  133. scalePoints[ 3].label = "3";
  134. scalePoints[ 4].value = 4.0f;
  135. scalePoints[ 4].label = "4";
  136. scalePoints[ 5].value = 5.0f;
  137. scalePoints[ 5].label = "5";
  138. scalePoints[ 6].value = 6.0f;
  139. scalePoints[ 6].label = "6";
  140. scalePoints[ 7].value = 7.0f;
  141. scalePoints[ 7].label = "7";
  142. scalePoints[ 8].value = 8.0f;
  143. scalePoints[ 8].label = "8";
  144. scalePoints[ 9].value = 9.0f;
  145. scalePoints[ 9].label = "9";
  146. scalePoints[10].value = 10.0f;
  147. scalePoints[10].label = "10";
  148. scalePoints[11].value = 11.0f;
  149. scalePoints[11].label = "11";
  150. scalePoints[12].value = 12.0f;
  151. scalePoints[12].label = "12";
  152. scalePoints[13].value = 13.0f;
  153. scalePoints[13].label = "13";
  154. scalePoints[14].value = 14.0f;
  155. scalePoints[14].label = "14";
  156. scalePoints[15].value = 15.0f;
  157. scalePoints[15].label = "15";
  158. scalePoints[16].value = 16.0f;
  159. scalePoints[16].label = "16";
  160. break;
  161. }
  162. param.hints = static_cast<NativeParameterHints>(hints);
  163. return &param;
  164. }
  165. float getParameterValue(const uint32_t index) const override
  166. {
  167. CARLA_SAFE_ASSERT_RETURN(index < kNumParameters, 0.0f);
  168. return fParameters[index];
  169. }
  170. // -------------------------------------------------------------------
  171. // Plugin state calls
  172. void setParameterValue(const uint32_t index, const float value) override
  173. {
  174. CARLA_SAFE_ASSERT_RETURN(index < kNumParameters,);
  175. fParameters[index] = value;
  176. Retuner* const retuner(jclient->retuner());
  177. switch (index)
  178. {
  179. case kParameterR_TUNE:
  180. retuner->set_refpitch(value);
  181. break;
  182. case kParameterR_FILT:
  183. retuner->set_corrfilt(value);
  184. break;
  185. case kParameterR_BIAS:
  186. retuner->set_notebias(value);
  187. break;
  188. case kParameterR_CORR:
  189. retuner->set_corrgain(value);
  190. break;
  191. case kParameterR_OFFS:
  192. retuner->set_corroffs(value);
  193. break;
  194. case kParameterM_CHANNEL:
  195. jclient->set_midichan(value-1.0f);
  196. break;
  197. }
  198. }
  199. // -------------------------------------------------------------------
  200. // Plugin process calls
  201. void process(float** const inBuffer, float** const outBuffer, const uint32_t frames,
  202. const NativeMidiEvent* const midiEvents, const uint32_t midiEventCount) override
  203. {
  204. if (! fJackClient.active)
  205. {
  206. carla_zeroFloats(outBuffer[0], frames);
  207. return;
  208. }
  209. fJackClient.portsAudioIn [0].buffer.audio = inBuffer [0];
  210. fJackClient.portsAudioOut[0].buffer.audio = outBuffer[0];
  211. fJackClient.portsMidiIn[0].buffer.midi.count = midiEventCount;
  212. fJackClient.portsMidiIn[0].buffer.midi.events = const_cast<NativeMidiEvent*>(midiEvents);
  213. fJackClient.processCallback(frames, fJackClient.processPtr);
  214. }
  215. // -------------------------------------------------------------------
  216. // Plugin UI calls
  217. void uiShow(const bool show) override
  218. {
  219. NativePluginAndUiClass::uiShow(show);
  220. if (show && isPipeRunning())
  221. {
  222. char tmpBuf[0xff+1];
  223. tmpBuf[0xff] = '\0';
  224. const CarlaMutexLocker cml(getPipeLock());
  225. writeMessage("zita-mask\n", 10);
  226. std::snprintf(tmpBuf, 0xff, "%i\n", notemask);
  227. writeMessage(tmpBuf);
  228. flushMessages();
  229. }
  230. }
  231. void uiIdle() override
  232. {
  233. NativePluginAndUiClass::uiIdle();
  234. if (! isPipeRunning())
  235. return;
  236. char tmpBuf[0xff+1];
  237. tmpBuf[0xff] = '\0';
  238. const CarlaMutexLocker cmlc(fClientMutex);
  239. const CarlaMutexLocker cmlp(getPipeLock());
  240. const ScopedLocale csl;
  241. Retuner* const retuner(jclient->retuner());
  242. writeMessage("zita-data\n", 10);
  243. std::snprintf(tmpBuf, 0xff, "%f\n", retuner->get_error());
  244. writeMessage(tmpBuf);
  245. std::snprintf(tmpBuf, 0xff, "%i\n", retuner->get_noteset());
  246. writeMessage(tmpBuf);
  247. std::snprintf(tmpBuf, 0xff, "%i\n", jclient->get_midiset());
  248. writeMessage(tmpBuf);
  249. flushMessages();
  250. }
  251. // -------------------------------------------------------------------
  252. // Plugin state calls
  253. char* getState() const override
  254. {
  255. char tmpBuf[0xff+1];
  256. tmpBuf[0xff] = '\0';
  257. std::snprintf(tmpBuf, 0xff, "%i", notemask);
  258. return strdup(tmpBuf);
  259. }
  260. void setState(const char* const data) override
  261. {
  262. CARLA_SAFE_ASSERT_RETURN(data != nullptr && data[0] != '\0',);
  263. notemask = std::atoi(data);
  264. const CarlaMutexLocker cml(fClientMutex);
  265. jclient->set_notemask(notemask);
  266. }
  267. // -------------------------------------------------------------------
  268. // Plugin dispatcher calls
  269. void bufferSizeChanged(const uint32_t bufferSize) override
  270. {
  271. fJackClient.bufferSize = bufferSize;
  272. // _recreateZitaClient(); // FIXME
  273. }
  274. void sampleRateChanged(const double sampleRate) override
  275. {
  276. fJackClient.sampleRate = sampleRate;
  277. // _recreateZitaClient(); // FIXME
  278. }
  279. // -------------------------------------------------------------------
  280. // Pipe Server calls
  281. bool msgReceived(const char* const msg) noexcept override
  282. {
  283. if (NativePluginAndUiClass::msgReceived(msg))
  284. return true;
  285. if (std::strcmp(msg, "zita-mask") == 0)
  286. {
  287. int mask;
  288. CARLA_SAFE_ASSERT_RETURN(readNextLineAsInt(mask), true);
  289. const CarlaMutexLocker cml(fClientMutex);
  290. try {
  291. if (mask < 0)
  292. {
  293. jclient->clr_midimask();
  294. }
  295. else
  296. {
  297. notemask = static_cast<uint>(mask);
  298. jclient->set_notemask(mask);
  299. }
  300. } CARLA_SAFE_EXCEPTION("msgReceived, zita-mask");
  301. return true;
  302. }
  303. return false;
  304. }
  305. // -------------------------------------------------------------------
  306. private:
  307. // Fake jack client
  308. jack_client_t fJackClient;
  309. // Zita stuff (core)
  310. ScopedPointer<Jclient> jclient;
  311. uint notemask;
  312. // Parameters
  313. float fParameters[kNumParameters];
  314. // mutex to make sure jclient is always valid
  315. CarlaMutex fClientMutex;
  316. void _recreateZitaClient()
  317. {
  318. const CarlaMutexLocker cml(fClientMutex);
  319. jclient = new Jclient(&fJackClient);
  320. jclient->set_notemask(notemask);
  321. jclient->set_midichan(roundToIntAccurate(fParameters[kParameterM_CHANNEL])-1);
  322. Retuner* const retuner(jclient->retuner());
  323. retuner->set_refpitch(fParameters[kParameterR_TUNE]);
  324. retuner->set_corrfilt(fParameters[kParameterR_FILT]);
  325. retuner->set_notebias(fParameters[kParameterR_BIAS]);
  326. retuner->set_corrgain(fParameters[kParameterR_CORR]);
  327. retuner->set_corroffs(fParameters[kParameterR_OFFS]);
  328. }
  329. PluginClassEND(AT1Plugin)
  330. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(AT1Plugin)
  331. };
  332. // -----------------------------------------------------------------------
  333. static const NativePluginDescriptor at1Desc = {
  334. /* category */ NATIVE_PLUGIN_CATEGORY_MODULATOR,
  335. /* hints */ static_cast<NativePluginHints>(NATIVE_PLUGIN_IS_RTSAFE
  336. |NATIVE_PLUGIN_HAS_UI
  337. |NATIVE_PLUGIN_NEEDS_FIXED_BUFFERS
  338. |NATIVE_PLUGIN_USES_STATE),
  339. /* supports */ NATIVE_PLUGIN_SUPPORTS_NOTHING,
  340. /* audioIns */ 1,
  341. /* audioOuts */ 1,
  342. /* midiIns */ 1,
  343. /* midiOuts */ 0,
  344. /* paramIns */ AT1Plugin::kNumParameters,
  345. /* paramOuts */ 0,
  346. /* name */ "AT1",
  347. /* label */ "at1",
  348. /* maker */ "falkTX, Fons Adriaensen",
  349. /* copyright */ "GPL v2+",
  350. PluginDescriptorFILL(AT1Plugin)
  351. };
  352. // -----------------------------------------------------------------------
  353. CARLA_EXPORT
  354. void carla_register_native_plugin_zita_at1();
  355. CARLA_EXPORT
  356. void carla_register_native_plugin_zita_at1()
  357. {
  358. carla_register_native_plugin(&at1Desc);
  359. }
  360. // -----------------------------------------------------------------------