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.

104 lines
3.8KB

  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. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. //==============================================================================
  20. /**
  21. A type of AudioSource that takes an input source and changes its sample rate.
  22. @see AudioSource, LagrangeInterpolator, CatmullRomInterpolator
  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. } // namespace juce