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.

107 lines
4.0KB

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