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.

445 lines
13KB

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