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.

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