35 numChannels (channels)
37 jassert (input !=
nullptr);
38 zeromem (coefficients,
sizeof (coefficients));
60 filterStates.
calloc (numChannels);
61 srcBuffers.
calloc (numChannels);
62 destBuffers.
calloc (numChannels);
63 createLowPass (ratio);
73 subSampleOffset = 0.0;
79 input->releaseResources();
80 buffer.
setSize (numChannels, 0);
104 bufferPos %= bufferSize;
109 bufferPos %= bufferSize;
122 input->getNextAudioBlock (
readInfo);
138 destBuffers[channel] =
info.buffer->getWritePointer (channel,
info.startSample);
142 int nextPos = (bufferPos + 1) % bufferSize;
144 for (
int m =
info.numSamples; --
m >= 0;)
151 *destBuffers[channel]++ = srcBuffers[channel][bufferPos]
152 +
alpha * (srcBuffers[channel][
nextPos] - srcBuffers[channel][bufferPos]);
156 while (subSampleOffset >= 1.0)
158 if (++bufferPos >= bufferSize)
163 nextPos = (bufferPos + 1) % bufferSize;
164 subSampleOffset -= 1.0;
172 applyFilter (
info.buffer->getWritePointer (i,
info.startSample),
info.numSamples, filterStates[i]);
180 FilterState&
fs = filterStates[i];
182 if (
info.numSamples > 1)
196 jassert (sampsInBuffer >= 0);
199void ResamplingAudioSource::createLowPass (
const double frequencyRatio)
208 setFilterCoefficients (
c1,
216void ResamplingAudioSource::setFilterCoefficients (
double c1,
double c2,
double c3,
double c4,
double c5,
double c6)
218 const double a = 1.0 / c4;
226 coefficients[0] = c1;
227 coefficients[1] = c2;
228 coefficients[2] = c3;
229 coefficients[3] = c4;
230 coefficients[4] = c5;
231 coefficients[5] = c6;
234void ResamplingAudioSource::resetFilters()
236 if (filterStates !=
nullptr)
237 filterStates.
clear ((
size_t) numChannels);
240void ResamplingAudioSource::applyFilter (
float* samples,
int num, FilterState& fs)
244 const double in = *samples;
246 double out = coefficients[0] * in
247 + coefficients[1] * fs.x1
248 + coefficients[2] * fs.x2
249 - coefficients[4] * fs.y1
250 - coefficients[5] * fs.y2;
253 if (! (out < -1.0e-8 || out > 1.0e-8))
262 *samples++ = (float) out;
Holds a resizable array of primitive or copy-by-value objects.
void setSize(int newNumChannels, int newNumSamples, bool keepExistingContent=false, bool clearExtraSpace=false, bool avoidReallocating=false)
Changes the buffer's size or number of channels.
Type * getWritePointer(int channelNumber) noexcept
Returns a writeable pointer to one of the buffer's channels.
int getNumChannels() const noexcept
Returns the number of channels of audio data that this buffer contains.
int getNumSamples() const noexcept
Returns the number of samples allocated in each of the buffer's channels.
void clear() noexcept
Clears all the samples in all channels.
const Type * getReadPointer(int channelNumber) const noexcept
Returns a pointer to an array of read-only samples in one of the buffer's channels.
Base class for objects that can produce a continuous stream of audio.
void clear(SizeType numElements) noexcept
This fills the block with zeros, up to the number of elements specified.
void calloc(SizeType newNumElements, const size_t elementSize=sizeof(ElementType))
Allocates a specified amount of memory and clears it.
void releaseResources() override
Allows the source to release anything it no longer needs after playback has stopped.
void getNextAudioBlock(const AudioSourceChannelInfo &) override
Called repeatedly to fetch subsequent blocks of audio data.
void prepareToPlay(int samplesPerBlockExpected, double sampleRate) override
Tells the source to prepare for playing.
void flushBuffers()
Clears any buffers and filters that the resampler is using.
~ResamplingAudioSource() override
Destructor.
void setResamplingRatio(double samplesInPerOutputSample)
Changes the resampling ratio.
ResamplingAudioSource(AudioSource *inputSource, bool deleteInputWhenDeleted, int numChannels=2)
Creates a ResamplingAudioSource for a given input source.
Used by AudioSource::getNextAudioBlock().
Commonly used mathematical constants.