VST3: fetch midi mappings all at once, use it for note/sound-off
[carla.git] / source / modules / juce_audio_basics / sources / juce_ResamplingAudioSource.h
blobef189962e8d073b8b6b52146c8bfa396fe57aded
1 /*
2 ==============================================================================
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
10 The code included in this file is provided under the terms of the ISC license
11 http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12 To use, copy, modify, and/or distribute this software for any purpose with or
13 without fee is hereby granted provided that the above copyright notice and
14 this permission notice appear in all copies.
16 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18 DISCLAIMED.
20 ==============================================================================
23 namespace juce
26 //==============================================================================
27 /**
28 A type of AudioSource that takes an input source and changes its sample rate.
30 @see AudioSource, LagrangeInterpolator, CatmullRomInterpolator
32 @tags{Audio}
34 class JUCE_API ResamplingAudioSource : public AudioSource
36 public:
37 //==============================================================================
38 /** Creates a ResamplingAudioSource for a given input source.
40 @param inputSource the input source to read from
41 @param deleteInputWhenDeleted if true, the input source will be deleted when
42 this object is deleted
43 @param numChannels the number of channels to process
45 ResamplingAudioSource (AudioSource* inputSource,
46 bool deleteInputWhenDeleted,
47 int numChannels = 2);
49 /** Destructor. */
50 ~ResamplingAudioSource() override;
52 /** Changes the resampling ratio.
54 (This value can be changed at any time, even while the source is running).
56 @param samplesInPerOutputSample if set to 1.0, the input is passed through; higher
57 values will speed it up; lower values will slow it
58 down. The ratio must be greater than 0
60 void setResamplingRatio (double samplesInPerOutputSample);
62 /** Returns the current resampling ratio.
64 This is the value that was set by setResamplingRatio().
66 double getResamplingRatio() const noexcept { return ratio; }
68 /** Clears any buffers and filters that the resampler is using. */
69 void flushBuffers();
71 //==============================================================================
72 void prepareToPlay (int samplesPerBlockExpected, double sampleRate) override;
73 void releaseResources() override;
74 void getNextAudioBlock (const AudioSourceChannelInfo&) override;
76 private:
77 //==============================================================================
78 OptionalScopedPointer<AudioSource> input;
79 double ratio = 1.0, lastRatio = 1.0;
80 AudioBuffer<float> buffer;
81 int bufferPos = 0, sampsInBuffer = 0;
82 double subSampleOffset = 0.0;
83 double coefficients[6];
84 SpinLock ratioLock;
85 CriticalSection callbackLock;
86 const int numChannels;
87 HeapBlock<float*> destBuffers;
88 HeapBlock<const float*> srcBuffers;
90 void setFilterCoefficients (double c1, double c2, double c3, double c4, double c5, double c6);
91 void createLowPass (double proportionalRate);
93 struct FilterState
95 double x1, x2, y1, y2;
98 HeapBlock<FilterState> filterStates;
99 void resetFilters();
101 void applyFilter (float* samples, int num, FilterState& fs);
103 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ResamplingAudioSource)
106 } // namespace juce