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.

106 lines
3.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #ifndef __JUCE_RESAMPLINGAUDIOSOURCE_JUCEHEADER__
  18. #define __JUCE_RESAMPLINGAUDIOSOURCE_JUCEHEADER__
  19. #include "juce_AudioSource.h"
  20. //==============================================================================
  21. /**
  22. A type of AudioSource that takes an input source and changes its sample rate.
  23. @see AudioSource
  24. */
  25. class JUCE_API ResamplingAudioSource : public AudioSource
  26. {
  27. public:
  28. //==============================================================================
  29. /** Creates a ResamplingAudioSource for a given input source.
  30. @param inputSource the input source to read from
  31. @param deleteInputWhenDeleted if true, the input source will be deleted when
  32. this object is deleted
  33. @param numChannels the number of channels to process
  34. */
  35. ResamplingAudioSource (AudioSource* inputSource,
  36. bool deleteInputWhenDeleted,
  37. int numChannels = 2);
  38. /** Destructor. */
  39. ~ResamplingAudioSource();
  40. /** Changes the resampling ratio.
  41. (This value can be changed at any time, even while the source is running).
  42. @param samplesInPerOutputSample if set to 1.0, the input is passed through; higher
  43. values will speed it up; lower values will slow it
  44. down. The ratio must be greater than 0
  45. */
  46. void setResamplingRatio (double samplesInPerOutputSample);
  47. /** Returns the current resampling ratio.
  48. This is the value that was set by setResamplingRatio().
  49. */
  50. double getResamplingRatio() const noexcept { return ratio; }
  51. //==============================================================================
  52. void prepareToPlay (int samplesPerBlockExpected, double sampleRate);
  53. void releaseResources();
  54. void getNextAudioBlock (const AudioSourceChannelInfo& bufferToFill);
  55. private:
  56. //==============================================================================
  57. OptionalScopedPointer<AudioSource> input;
  58. double ratio, lastRatio;
  59. AudioSampleBuffer buffer;
  60. int bufferPos, sampsInBuffer;
  61. double subSampleOffset;
  62. double coefficients[6];
  63. SpinLock ratioLock;
  64. const int numChannels;
  65. HeapBlock<float*> destBuffers, srcBuffers;
  66. void setFilterCoefficients (double c1, double c2, double c3, double c4, double c5, double c6);
  67. void createLowPass (double proportionalRate);
  68. struct FilterState
  69. {
  70. double x1, x2, y1, y2;
  71. };
  72. HeapBlock<FilterState> filterStates;
  73. void resetFilters();
  74. void applyFilter (float* samples, int num, FilterState& fs);
  75. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ResamplingAudioSource)
  76. };
  77. #endif // __JUCE_RESAMPLINGAUDIOSOURCE_JUCEHEADER__