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.

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