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.

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