The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

713 lines
28KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. //===============================================================================
  20. /** Abstract class for the provided oversampling engines used internally in
  21. the Oversampling class.
  22. */
  23. template <typename SampleType>
  24. class OversamplingEngine
  25. {
  26. public:
  27. //===============================================================================
  28. OversamplingEngine (size_t newNumChannels, size_t newFactor)
  29. {
  30. numChannels = newNumChannels;
  31. factor = newFactor;
  32. }
  33. virtual ~OversamplingEngine() {}
  34. //===============================================================================
  35. virtual SampleType getLatencyInSamples() = 0;
  36. size_t getFactor() { return factor; }
  37. virtual void initProcessing (size_t maximumNumberOfSamplesBeforeOversampling)
  38. {
  39. buffer.setSize (static_cast<int> (numChannels), static_cast<int> (maximumNumberOfSamplesBeforeOversampling * factor), false, false, true);
  40. }
  41. virtual void reset()
  42. {
  43. buffer.clear();
  44. }
  45. dsp::AudioBlock<SampleType> getProcessedSamples (size_t numSamples)
  46. {
  47. return dsp::AudioBlock<SampleType> (buffer).getSubBlock (0, numSamples);
  48. }
  49. virtual void processSamplesUp (dsp::AudioBlock<SampleType> &inputBlock) = 0;
  50. virtual void processSamplesDown (dsp::AudioBlock<SampleType> &outputBlock) = 0;
  51. protected:
  52. //===============================================================================
  53. AudioBuffer<SampleType> buffer;
  54. size_t factor;
  55. size_t numChannels;
  56. };
  57. //===============================================================================
  58. /** Dummy oversampling engine class which simply copies and pastes the input
  59. signal, which could be equivalent to a "one time" oversampling processing.
  60. */
  61. template <typename SampleType>
  62. class OversamplingDummy : public OversamplingEngine<SampleType>
  63. {
  64. public:
  65. //===============================================================================
  66. OversamplingDummy (size_t numChannels) : OversamplingEngine<SampleType> (numChannels, 1) {}
  67. ~OversamplingDummy() {}
  68. //===============================================================================
  69. SampleType getLatencyInSamples() override
  70. {
  71. return 0.f;
  72. }
  73. void processSamplesUp (dsp::AudioBlock<SampleType> &inputBlock) override
  74. {
  75. jassert (inputBlock.getNumChannels() <= static_cast<size_t> (OversamplingEngine<SampleType>::buffer.getNumChannels()));
  76. jassert (inputBlock.getNumSamples() * OversamplingEngine<SampleType>::factor <= static_cast<size_t> (OversamplingEngine<SampleType>::buffer.getNumSamples()));
  77. for (size_t channel = 0; channel < inputBlock.getNumChannels(); channel++)
  78. OversamplingEngine<SampleType>::buffer.copyFrom (static_cast<int> (channel), 0,
  79. inputBlock.getChannelPointer (channel), static_cast<int> (inputBlock.getNumSamples()));
  80. }
  81. void processSamplesDown (dsp::AudioBlock<SampleType> &outputBlock) override
  82. {
  83. jassert (outputBlock.getNumChannels() <= static_cast<size_t> (OversamplingEngine<SampleType>::buffer.getNumChannels()));
  84. jassert (outputBlock.getNumSamples() * OversamplingEngine<SampleType>::factor <= static_cast<size_t> (OversamplingEngine<SampleType>::buffer.getNumSamples()));
  85. outputBlock.copy (OversamplingEngine<SampleType>::getProcessedSamples (outputBlock.getNumSamples()));
  86. }
  87. private:
  88. //===============================================================================
  89. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OversamplingDummy)
  90. };
  91. //===============================================================================
  92. /** Oversampling engine class performing 2 times oversampling using the Filter
  93. Design FIR Equiripple method. The resulting filter is linear phase,
  94. symmetric, and has every two samples but the middle one equal to zero,
  95. leading to specific processing optimizations.
  96. */
  97. template <typename SampleType>
  98. class Oversampling2TimesEquirippleFIR : public OversamplingEngine<SampleType>
  99. {
  100. public:
  101. //===============================================================================
  102. Oversampling2TimesEquirippleFIR (size_t numChannels,
  103. SampleType normalizedTransitionWidthUp,
  104. SampleType stopbandAttenuationdBUp,
  105. SampleType normalizedTransitionWidthDown,
  106. SampleType stopbandAttenuationdBDown) : OversamplingEngine<SampleType> (numChannels, 2)
  107. {
  108. coefficientsUp = *dsp::FilterDesign<SampleType>::designFIRLowpassHalfBandEquirippleMethod (normalizedTransitionWidthUp, stopbandAttenuationdBUp);
  109. coefficientsDown = *dsp::FilterDesign<SampleType>::designFIRLowpassHalfBandEquirippleMethod (normalizedTransitionWidthDown, stopbandAttenuationdBDown);
  110. auto N = coefficientsUp.getFilterOrder() + 1;
  111. stateUp.setSize (static_cast<int> (numChannels), static_cast<int> (N));
  112. N = coefficientsDown.getFilterOrder() + 1;
  113. auto Ndiv2 = N / 2;
  114. auto Ndiv4 = Ndiv2 / 2;
  115. stateDown.setSize (static_cast<int> (numChannels), static_cast<int> (N));
  116. stateDown2.setSize (static_cast<int> (numChannels), static_cast<int> (Ndiv4 + 1));
  117. position.resize (static_cast<int> (numChannels));
  118. }
  119. ~Oversampling2TimesEquirippleFIR() {}
  120. //===============================================================================
  121. SampleType getLatencyInSamples() override
  122. {
  123. return static_cast<SampleType> (coefficientsUp.getFilterOrder() + coefficientsDown.getFilterOrder()) * 0.5f;
  124. }
  125. void reset() override
  126. {
  127. OversamplingEngine<SampleType>::reset();
  128. stateUp.clear();
  129. stateDown.clear();
  130. stateDown2.clear();
  131. position.fill (0);
  132. }
  133. void processSamplesUp (dsp::AudioBlock<SampleType> &inputBlock) override
  134. {
  135. jassert (inputBlock.getNumChannels() <= static_cast<size_t> (OversamplingEngine<SampleType>::buffer.getNumChannels()));
  136. jassert (inputBlock.getNumSamples() * OversamplingEngine<SampleType>::factor <= static_cast<size_t> (OversamplingEngine<SampleType>::buffer.getNumSamples()));
  137. // Initialization
  138. auto fir = coefficientsUp.getRawCoefficients();
  139. auto N = coefficientsUp.getFilterOrder() + 1;
  140. auto Ndiv2 = N / 2;
  141. auto numSamples = inputBlock.getNumSamples();
  142. // Processing
  143. for (size_t channel = 0; channel < inputBlock.getNumChannels(); channel++)
  144. {
  145. auto bufferSamples = OversamplingEngine<SampleType>::buffer.getWritePointer (static_cast<int> (channel));
  146. auto buf = stateUp.getWritePointer (static_cast<int> (channel));
  147. auto samples = inputBlock.getChannelPointer (channel);
  148. for (size_t i = 0; i < numSamples; i++)
  149. {
  150. // Input
  151. buf[N - 1] = 2 * samples[i];
  152. // Convolution
  153. auto out = static_cast<SampleType> (0.0);
  154. for (size_t k = 0; k < Ndiv2; k += 2)
  155. out += (buf[k] + buf[N - k - 1]) * fir[k];
  156. // Outputs
  157. bufferSamples[i << 1] = out;
  158. bufferSamples[(i << 1) + 1] = buf[Ndiv2 + 1] * fir[Ndiv2];
  159. // Shift data
  160. for (size_t k = 0; k < N - 2; k += 2)
  161. buf[k] = buf[k + 2];
  162. }
  163. }
  164. }
  165. void processSamplesDown (dsp::AudioBlock<SampleType> &outputBlock) override
  166. {
  167. jassert (outputBlock.getNumChannels() <= static_cast<size_t> (OversamplingEngine<SampleType>::buffer.getNumChannels()));
  168. jassert (outputBlock.getNumSamples() * OversamplingEngine<SampleType>::factor <= static_cast<size_t> (OversamplingEngine<SampleType>::buffer.getNumSamples()));
  169. // Initialization
  170. auto fir = coefficientsDown.getRawCoefficients();
  171. auto N = coefficientsDown.getFilterOrder() + 1;
  172. auto Ndiv2 = N / 2;
  173. auto Ndiv4 = Ndiv2 / 2;
  174. auto numSamples = outputBlock.getNumSamples();
  175. // Processing
  176. for (size_t channel = 0; channel < outputBlock.getNumChannels(); channel++)
  177. {
  178. auto bufferSamples = OversamplingEngine<SampleType>::buffer.getWritePointer (static_cast<int> (channel));
  179. auto buf = stateDown.getWritePointer (static_cast<int> (channel));
  180. auto buf2 = stateDown2.getWritePointer (static_cast<int> (channel));
  181. auto samples = outputBlock.getChannelPointer (channel);
  182. auto pos = position.getUnchecked (static_cast<int> (channel));
  183. for (size_t i = 0; i < numSamples; i++)
  184. {
  185. // Input
  186. buf[N - 1] = bufferSamples[i << 1];
  187. // Convolution
  188. auto out = static_cast<SampleType> (0.0);
  189. for (size_t k = 0; k < Ndiv2; k += 2)
  190. out += (buf[k] + buf[N - k - 1]) * fir[k];
  191. // Output
  192. out += buf2[pos] * fir[Ndiv2];
  193. buf2[pos] = bufferSamples[(i << 1) + 1];
  194. samples[i] = out;
  195. // Shift data
  196. for (size_t k = 0; k < N - 2; k++)
  197. buf[k] = buf[k + 2];
  198. // Circular buffer
  199. pos = (pos == 0 ? Ndiv4 : pos - 1);
  200. }
  201. position.setUnchecked (static_cast<int> (channel), pos);
  202. }
  203. }
  204. private:
  205. //===============================================================================
  206. dsp::FIR::Coefficients<SampleType> coefficientsUp, coefficientsDown;
  207. AudioBuffer<SampleType> stateUp, stateDown, stateDown2;
  208. Array<size_t> position;
  209. //===============================================================================
  210. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Oversampling2TimesEquirippleFIR)
  211. };
  212. //===============================================================================
  213. /** Oversampling engine class performing 2 times oversampling using the Filter
  214. Design IIR Polyphase Allpass Cascaded method. The resulting filter is minimum
  215. phase, and provided with a method to get the exact resulting latency.
  216. */
  217. template <typename SampleType>
  218. class Oversampling2TimesPolyphaseIIR : public OversamplingEngine<SampleType>
  219. {
  220. public:
  221. //===============================================================================
  222. Oversampling2TimesPolyphaseIIR (size_t numChannels,
  223. SampleType normalizedTransitionWidthUp,
  224. SampleType stopbandAttenuationdBUp,
  225. SampleType normalizedTransitionWidthDown,
  226. SampleType stopbandAttenuationdBDown) : OversamplingEngine<SampleType> (2, numChannels)
  227. {
  228. auto structureUp = dsp::FilterDesign<SampleType>::designIIRLowpassHalfBandPolyphaseAllpassMethod (normalizedTransitionWidthUp, stopbandAttenuationdBUp);
  229. dsp::IIR::Coefficients<SampleType> coeffsUp = getCoefficients (structureUp);
  230. latency = static_cast<SampleType> (-(coeffsUp.getPhaseForFrequency (0.0001, 1.0)) / (0.0001 * 2 * double_Pi));
  231. auto structureDown = dsp::FilterDesign<SampleType>::designIIRLowpassHalfBandPolyphaseAllpassMethod (normalizedTransitionWidthDown, stopbandAttenuationdBDown);
  232. dsp::IIR::Coefficients<SampleType> coeffsDown = getCoefficients (structureDown);
  233. latency += static_cast<SampleType> (-(coeffsDown.getPhaseForFrequency (0.0001, 1.0)) / (0.0001 * 2 * double_Pi));
  234. for (auto i = 0; i < structureUp.directPath.size(); i++)
  235. coefficientsUp.add (structureUp.directPath[i].coefficients[0]);
  236. for (auto i = 1; i < structureUp.delayedPath.size(); i++)
  237. coefficientsUp.add (structureUp.delayedPath[i].coefficients[0]);
  238. for (auto i = 0; i < structureDown.directPath.size(); i++)
  239. coefficientsDown.add (structureDown.directPath[i].coefficients[0]);
  240. for (auto i = 1; i < structureDown.delayedPath.size(); i++)
  241. coefficientsDown.add (structureDown.delayedPath[i].coefficients[0]);
  242. v1Up.setSize (static_cast<int> (numChannels), coefficientsUp.size());
  243. v1Down.setSize (static_cast<int> (numChannels), coefficientsDown.size());
  244. delayDown.resize (static_cast<int> (numChannels));
  245. }
  246. ~Oversampling2TimesPolyphaseIIR() {}
  247. //===============================================================================
  248. SampleType getLatencyInSamples() override
  249. {
  250. return latency;
  251. }
  252. void reset() override
  253. {
  254. OversamplingEngine<SampleType>::reset();
  255. v1Up.clear();
  256. v1Down.clear();
  257. delayDown.fill (0);
  258. }
  259. void processSamplesUp (dsp::AudioBlock<SampleType> &inputBlock) override
  260. {
  261. jassert (inputBlock.getNumChannels() <= static_cast<size_t> (OversamplingEngine<SampleType>::buffer.getNumChannels()));
  262. jassert (inputBlock.getNumSamples() * OversamplingEngine<SampleType>::factor <= static_cast<size_t> (OversamplingEngine<SampleType>::buffer.getNumSamples()));
  263. // Initialization
  264. auto coeffs = coefficientsUp.getRawDataPointer();
  265. auto numStages = coefficientsUp.size();
  266. auto delayedStages = numStages / 2;
  267. auto directStages = numStages - delayedStages;
  268. auto numSamples = inputBlock.getNumSamples();
  269. // Processing
  270. for (size_t channel = 0; channel < inputBlock.getNumChannels(); channel++)
  271. {
  272. auto bufferSamples = OversamplingEngine<SampleType>::buffer.getWritePointer (static_cast<int> (channel));
  273. auto lv1 = v1Up.getWritePointer (static_cast<int> (channel));
  274. auto samples = inputBlock.getChannelPointer (channel);
  275. for (size_t i = 0; i < numSamples; i++)
  276. {
  277. // Direct path cascaded allpass filters
  278. auto input = samples[i];
  279. for (auto n = 0; n < directStages; n++)
  280. {
  281. auto alpha = coeffs[n];
  282. auto output = alpha * input + lv1[n];
  283. lv1[n] = input - alpha * output;
  284. input = output;
  285. }
  286. // Output
  287. bufferSamples[i << 1] = input;
  288. // Delayed path cascaded allpass filters
  289. input = samples[i];
  290. for (auto n = directStages; n < numStages; n++)
  291. {
  292. auto alpha = coeffs[n];
  293. auto output = alpha * input + lv1[n];
  294. lv1[n] = input - alpha * output;
  295. input = output;
  296. }
  297. // Output
  298. bufferSamples[(i << 1) + 1] = input;
  299. }
  300. }
  301. // Snap To Zero
  302. snapToZero (true);
  303. }
  304. void processSamplesDown (dsp::AudioBlock<SampleType> &outputBlock) override
  305. {
  306. jassert (outputBlock.getNumChannels() <= static_cast<size_t> (OversamplingEngine<SampleType>::buffer.getNumChannels()));
  307. jassert (outputBlock.getNumSamples() * OversamplingEngine<SampleType>::factor <= static_cast<size_t> (OversamplingEngine<SampleType>::buffer.getNumSamples()));
  308. // Initialization
  309. auto coeffs = coefficientsDown.getRawDataPointer();
  310. auto numStages = coefficientsDown.size();
  311. auto delayedStages = numStages / 2;
  312. auto directStages = numStages - delayedStages;
  313. auto numSamples = outputBlock.getNumSamples();
  314. // Processing
  315. for (size_t channel = 0; channel < outputBlock.getNumChannels(); channel++)
  316. {
  317. auto bufferSamples = OversamplingEngine<SampleType>::buffer.getWritePointer (static_cast<int> (channel));
  318. auto lv1 = v1Down.getWritePointer (static_cast<int> (channel));
  319. auto samples = outputBlock.getChannelPointer (channel);
  320. auto delay = delayDown.getUnchecked (static_cast<int> (channel));
  321. for (size_t i = 0; i < numSamples; i++)
  322. {
  323. // Direct path cascaded allpass filters
  324. auto input = bufferSamples[i << 1];
  325. for (auto n = 0; n < directStages; n++)
  326. {
  327. auto alpha = coeffs[n];
  328. auto output = alpha * input + lv1[n];
  329. lv1[n] = input - alpha * output;
  330. input = output;
  331. }
  332. auto directOut = input;
  333. // Delayed path cascaded allpass filters
  334. input = bufferSamples[(i << 1) + 1];
  335. for (auto n = directStages; n < numStages; n++)
  336. {
  337. auto alpha = coeffs[n];
  338. auto output = alpha * input + lv1[n];
  339. lv1[n] = input - alpha * output;
  340. input = output;
  341. }
  342. // Output
  343. samples[i] = (delay + directOut) * static_cast<SampleType> (0.5);
  344. delay = input;
  345. }
  346. delayDown.setUnchecked (static_cast<int> (channel), delay);
  347. }
  348. // Snap To Zero
  349. snapToZero (false);
  350. }
  351. void snapToZero (bool snapUpProcessing)
  352. {
  353. if (snapUpProcessing)
  354. {
  355. for (auto channel = 0; channel < OversamplingEngine<SampleType>::buffer.getNumChannels(); channel++)
  356. {
  357. auto lv1 = v1Up.getWritePointer (channel);
  358. auto numStages = coefficientsUp.size();
  359. for (auto n = 0; n < numStages; n++)
  360. JUCE_SNAP_TO_ZERO (lv1[n]);
  361. }
  362. }
  363. else
  364. {
  365. for (auto channel = 0; channel < OversamplingEngine<SampleType>::buffer.getNumChannels(); channel++)
  366. {
  367. auto lv1 = v1Down.getWritePointer (channel);
  368. auto numStages = coefficientsDown.size();
  369. for (auto n = 0; n < numStages; n++)
  370. JUCE_SNAP_TO_ZERO (lv1[n]);
  371. }
  372. }
  373. }
  374. private:
  375. //===============================================================================
  376. /** This function calculates the equivalent high order IIR filter of a given
  377. polyphase cascaded allpass filters structure.
  378. */
  379. const dsp::IIR::Coefficients<SampleType> getCoefficients (typename dsp::FilterDesign<SampleType>::IIRPolyphaseAllpassStructure &structure) const
  380. {
  381. dsp::Polynomial<SampleType> numerator1 ({ static_cast<SampleType> (1.0) });
  382. dsp::Polynomial<SampleType> denominator1 ({ static_cast<SampleType> (1.0) });
  383. dsp::Polynomial<SampleType> numerator2 ({ static_cast<SampleType> (1.0) });
  384. dsp::Polynomial<SampleType> denominator2 ({ static_cast<SampleType> (1.0) });
  385. dsp::Polynomial<SampleType> temp;
  386. for (auto n = 0; n < structure.directPath.size(); n++)
  387. {
  388. auto *coeffs = structure.directPath.getReference (n).getRawCoefficients();
  389. if (structure.directPath[n].getFilterOrder() == 1)
  390. {
  391. temp = dsp::Polynomial<SampleType> ({ coeffs[0], coeffs[1] });
  392. numerator1 = numerator1.getProductWith (temp);
  393. temp = dsp::Polynomial<SampleType> ({ static_cast<SampleType> (1.0), coeffs[2] });
  394. denominator1 = denominator1.getProductWith (temp);
  395. }
  396. else
  397. {
  398. temp = dsp::Polynomial<SampleType> ({ coeffs[0], coeffs[1], coeffs[2] });
  399. numerator1 = numerator1.getProductWith (temp);
  400. temp = dsp::Polynomial<SampleType> ({ static_cast<SampleType> (1.0), coeffs[3], coeffs[4] });
  401. denominator1 = denominator1.getProductWith (temp);
  402. }
  403. }
  404. for (auto n = 0; n < structure.delayedPath.size(); n++)
  405. {
  406. auto *coeffs = structure.delayedPath.getReference (n).getRawCoefficients();
  407. if (structure.delayedPath[n].getFilterOrder() == 1)
  408. {
  409. temp = dsp::Polynomial<SampleType> ({ coeffs[0], coeffs[1] });
  410. numerator2 = numerator2.getProductWith (temp);
  411. temp = dsp::Polynomial<SampleType> ({ static_cast<SampleType> (1.0), coeffs[2] });
  412. denominator2 = denominator2.getProductWith (temp);
  413. }
  414. else
  415. {
  416. temp = dsp::Polynomial<SampleType> ({ coeffs[0], coeffs[1], coeffs[2] });
  417. numerator2 = numerator2.getProductWith (temp);
  418. temp = dsp::Polynomial<SampleType> ({ static_cast<SampleType> (1.0), coeffs[3], coeffs[4] });
  419. denominator2 = denominator2.getProductWith (temp);
  420. }
  421. }
  422. dsp::Polynomial<SampleType> numeratorf1 = numerator1.getProductWith (denominator2);
  423. dsp::Polynomial<SampleType> numeratorf2 = numerator2.getProductWith (denominator1);
  424. dsp::Polynomial<SampleType> numerator = numeratorf1.getSumWith (numeratorf2);
  425. dsp::Polynomial<SampleType> denominator = denominator1.getProductWith (denominator2);
  426. dsp::IIR::Coefficients<SampleType> coeffs;
  427. coeffs.coefficients.clear();
  428. auto inversion = static_cast<SampleType> (1.0) / denominator[0];
  429. for (auto i = 0; i <= numerator.getOrder(); i++)
  430. coeffs.coefficients.add (numerator[i] * inversion);
  431. for (auto i = 1; i <= denominator.getOrder(); i++)
  432. coeffs.coefficients.add (denominator[i] * inversion);
  433. return coeffs;
  434. }
  435. //===============================================================================
  436. Array<SampleType> coefficientsUp, coefficientsDown;
  437. SampleType latency;
  438. AudioBuffer<SampleType> v1Up, v1Down;
  439. Array<SampleType> delayDown;
  440. //===============================================================================
  441. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Oversampling2TimesPolyphaseIIR)
  442. };
  443. //===============================================================================
  444. template <typename SampleType>
  445. Oversampling<SampleType>::Oversampling (size_t newNumChannels, size_t newFactor, FilterType newType, bool newMaxQuality)
  446. {
  447. jassert (newFactor >= 0 && newFactor <= 4 && newNumChannels > 0);
  448. factorOversampling = static_cast<size_t> (1) << newFactor;
  449. isMaximumQuality = newMaxQuality;
  450. type = newType;
  451. numChannels = newNumChannels;
  452. if (newFactor == 0)
  453. {
  454. numStages = 1;
  455. engines.add (new OversamplingDummy<SampleType> (numChannels));
  456. }
  457. else if (type == FilterType::filterHalfBandPolyphaseIIR)
  458. {
  459. numStages = newFactor;
  460. for (size_t n = 0; n < numStages; n++)
  461. {
  462. auto twUp = (isMaximumQuality ? 0.10f : 0.12f) * (n == 0 ? 0.5f : 1.f);
  463. auto twDown = (isMaximumQuality ? 0.12f : 0.15f) * (n == 0 ? 0.5f : 1.f);
  464. auto gaindBStartUp = (isMaximumQuality ? -75.f : -65.f);
  465. auto gaindBStartDown = (isMaximumQuality ? -70.f : -60.f);
  466. auto gaindBFactorUp = (isMaximumQuality ? 10.f : 8.f);
  467. auto gaindBFactorDown = (isMaximumQuality ? 10.f : 8.f);
  468. engines.add (new Oversampling2TimesPolyphaseIIR<SampleType> (numChannels,
  469. twUp, gaindBStartUp + gaindBFactorUp * n,
  470. twDown, gaindBStartDown + gaindBFactorDown * n));
  471. }
  472. }
  473. else if (type == FilterType::filterHalfBandFIREquiripple)
  474. {
  475. numStages = newFactor;
  476. for (size_t n = 0; n < numStages; n++)
  477. {
  478. auto twUp = (isMaximumQuality ? 0.10f : 0.12f) * (n == 0 ? 0.5f : 1.f);
  479. auto twDown = (isMaximumQuality ? 0.12f : 0.15f) * (n == 0 ? 0.5f : 1.f);
  480. auto gaindBStartUp = (isMaximumQuality ? -90.f : -70.f);
  481. auto gaindBStartDown = (isMaximumQuality ? -70.f : -60.f);
  482. auto gaindBFactorUp = (isMaximumQuality ? 10.f : 8.f);
  483. auto gaindBFactorDown = (isMaximumQuality ? 10.f : 8.f);
  484. engines.add (new Oversampling2TimesEquirippleFIR<SampleType> (numChannels,
  485. twUp, gaindBStartUp + gaindBFactorUp * n,
  486. twDown, gaindBStartDown + gaindBFactorDown * n));
  487. }
  488. }
  489. }
  490. template <typename SampleType>
  491. Oversampling<SampleType>::~Oversampling()
  492. {
  493. engines.clear();
  494. }
  495. //===============================================================================
  496. template <typename SampleType>
  497. SampleType Oversampling<SampleType>::getLatencyInSamples() noexcept
  498. {
  499. auto latency = static_cast<SampleType> (0);
  500. size_t order = 1;
  501. for (size_t n = 0; n < numStages; n++)
  502. {
  503. auto& engine = *engines[static_cast<int> (n)];
  504. order *= engine.getFactor();
  505. latency += engine.getLatencyInSamples() / static_cast<SampleType> (order);
  506. }
  507. return latency;
  508. }
  509. template <typename SampleType>
  510. size_t Oversampling<SampleType>::getOversamplingFactor() noexcept
  511. {
  512. return factorOversampling;
  513. }
  514. //===============================================================================
  515. template <typename SampleType>
  516. void Oversampling<SampleType>::initProcessing (size_t maximumNumberOfSamplesBeforeOversampling)
  517. {
  518. jassert (engines.size() > 0);
  519. auto currentNumSamples = maximumNumberOfSamplesBeforeOversampling;
  520. for (size_t n = 0; n < numStages; n++)
  521. {
  522. auto& engine = *engines[static_cast<int> (n)];
  523. engine.initProcessing (currentNumSamples);
  524. currentNumSamples *= engine.getFactor();
  525. }
  526. isReady = true;
  527. reset();
  528. }
  529. template <typename SampleType>
  530. void Oversampling<SampleType>::reset() noexcept
  531. {
  532. jassert (engines.size() > 0);
  533. if (isReady)
  534. for (auto n = 0; n < engines.size(); n++)
  535. engines[n]->reset();
  536. }
  537. template <typename SampleType>
  538. typename dsp::AudioBlock<SampleType> Oversampling<SampleType>::processSamplesUp (const dsp::AudioBlock<SampleType> &inputBlock) noexcept
  539. {
  540. jassert (engines.size() > 0);
  541. if (! isReady)
  542. return dsp::AudioBlock<SampleType>();
  543. dsp::AudioBlock<SampleType> audioBlock = inputBlock;
  544. for (size_t n = 0; n < numStages; n++)
  545. {
  546. auto& engine = *engines[static_cast<int> (n)];
  547. engine.processSamplesUp (audioBlock);
  548. audioBlock = engine.getProcessedSamples (audioBlock.getNumSamples() * engine.getFactor());
  549. }
  550. return audioBlock;
  551. }
  552. template <typename SampleType>
  553. void Oversampling<SampleType>::processSamplesDown (dsp::AudioBlock<SampleType> &outputBlock) noexcept
  554. {
  555. jassert (engines.size() > 0);
  556. if (! isReady)
  557. return;
  558. auto currentNumSamples = outputBlock.getNumSamples();
  559. for (size_t n = 0; n < numStages - 1; n++)
  560. currentNumSamples *= engines[static_cast<int> (n)]->getFactor();
  561. for (size_t n = numStages - 1; n > 0; n--)
  562. {
  563. auto& engine = *engines[static_cast<int> (n)];
  564. auto audioBlock = engines[static_cast<int> (n - 1)]->getProcessedSamples (currentNumSamples);
  565. engine.processSamplesDown (audioBlock);
  566. currentNumSamples /= engine.getFactor();
  567. }
  568. engines[static_cast<int> (0)]->processSamplesDown (outputBlock);
  569. }
  570. template class Oversampling<float>;
  571. template class Oversampling<double>;