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.

107 lines
3.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  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. @tags{Audio}
  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() override;
  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. /** Clears any buffers and filters that the resampler is using. */
  52. void flushBuffers();
  53. //==============================================================================
  54. void prepareToPlay (int samplesPerBlockExpected, double sampleRate) override;
  55. void releaseResources() override;
  56. void getNextAudioBlock (const AudioSourceChannelInfo&) override;
  57. private:
  58. //==============================================================================
  59. OptionalScopedPointer<AudioSource> input;
  60. double ratio = 1.0, lastRatio = 1.0;
  61. AudioBuffer<float> buffer;
  62. int bufferPos = 0, sampsInBuffer = 0;
  63. double subSampleOffset = 0.0;
  64. double coefficients[6];
  65. SpinLock ratioLock;
  66. CriticalSection callbackLock;
  67. const int numChannels;
  68. HeapBlock<float*> destBuffers;
  69. HeapBlock<const float*> srcBuffers;
  70. void setFilterCoefficients (double c1, double c2, double c3, double c4, double c5, double c6);
  71. void createLowPass (double proportionalRate);
  72. struct FilterState
  73. {
  74. double x1, x2, y1, y2;
  75. };
  76. HeapBlock<FilterState> filterStates;
  77. void resetFilters();
  78. void applyFilter (float* samples, int num, FilterState& fs);
  79. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ResamplingAudioSource)
  80. };
  81. } // namespace juce