Audio plugin host https://kx.studio/carla
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.

juce_ResamplingAudioSource.h 3.9KB

10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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_H_INCLUDED
  18. #define JUCE_RESAMPLINGAUDIOSOURCE_H_INCLUDED
  19. //==============================================================================
  20. /**
  21. A type of AudioSource that takes an input source and changes its sample rate.
  22. @see AudioSource
  23. */
  24. class JUCE_API ResamplingAudioSource : public AudioSource
  25. {
  26. public:
  27. //==============================================================================
  28. /** Creates a ResamplingAudioSource for a given input source.
  29. @param inputSource the input source to read from
  30. @param deleteInputWhenDeleted if true, the input source will be deleted when
  31. this object is deleted
  32. @param numChannels the number of channels to process
  33. */
  34. ResamplingAudioSource (AudioSource* inputSource,
  35. bool deleteInputWhenDeleted,
  36. int numChannels = 2);
  37. /** Destructor. */
  38. ~ResamplingAudioSource();
  39. /** Changes the resampling ratio.
  40. (This value can be changed at any time, even while the source is running).
  41. @param samplesInPerOutputSample if set to 1.0, the input is passed through; higher
  42. values will speed it up; lower values will slow it
  43. down. The ratio must be greater than 0
  44. */
  45. void setResamplingRatio (double samplesInPerOutputSample);
  46. /** Returns the current resampling ratio.
  47. This is the value that was set by setResamplingRatio().
  48. */
  49. double getResamplingRatio() const noexcept { return ratio; }
  50. /** Clears any buffers and filters that the resampler is using. */
  51. void flushBuffers();
  52. //==============================================================================
  53. void prepareToPlay (int samplesPerBlockExpected, double sampleRate) override;
  54. void releaseResources() override;
  55. void getNextAudioBlock (const AudioSourceChannelInfo&) override;
  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;
  67. HeapBlock<const float*> srcBuffers;
  68. void setFilterCoefficients (double c1, double c2, double c3, double c4, double c5, double c6);
  69. void createLowPass (double proportionalRate);
  70. struct FilterState
  71. {
  72. double x1, x2, y1, y2;
  73. };
  74. HeapBlock<FilterState> filterStates;
  75. void resetFilters();
  76. void applyFilter (float* samples, int num, FilterState& fs);
  77. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ResamplingAudioSource)
  78. };
  79. #endif // JUCE_RESAMPLINGAUDIOSOURCE_H_INCLUDED