git subrepo clone --branch=sono6good https://github.com/essej/JUCE.git deps/juce
subrepo: subdir: "deps/juce" merged: "b13f9084e" upstream: origin: "https://github.com/essej/JUCE.git" branch: "sono6good" commit: "b13f9084e" git-subrepo: version: "0.4.3" origin: "https://github.com/ingydotnet/git-subrepo.git" commit: "2f68596"
This commit is contained in:
179
deps/juce/modules/juce_audio_basics/sources/juce_AudioSource.h
vendored
Normal file
179
deps/juce/modules/juce_audio_basics/sources/juce_AudioSource.h
vendored
Normal file
@ -0,0 +1,179 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2020 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
The code included in this file is provided under the terms of the ISC license
|
||||
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
|
||||
To use, copy, modify, and/or distribute this software for any purpose with or
|
||||
without fee is hereby granted provided that the above copyright notice and
|
||||
this permission notice appear in all copies.
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
Used by AudioSource::getNextAudioBlock().
|
||||
|
||||
@tags{Audio}
|
||||
*/
|
||||
struct JUCE_API AudioSourceChannelInfo
|
||||
{
|
||||
/** Creates an uninitialised AudioSourceChannelInfo. */
|
||||
AudioSourceChannelInfo() = default;
|
||||
|
||||
/** Creates an AudioSourceChannelInfo. */
|
||||
AudioSourceChannelInfo (AudioBuffer<float>* bufferToUse,
|
||||
int startSampleOffset, int numSamplesToUse) noexcept
|
||||
: buffer (bufferToUse),
|
||||
startSample (startSampleOffset),
|
||||
numSamples (numSamplesToUse)
|
||||
{
|
||||
}
|
||||
|
||||
/** Creates an AudioSourceChannelInfo that uses the whole of a buffer.
|
||||
Note that the buffer provided must not be deleted while the
|
||||
AudioSourceChannelInfo is still using it.
|
||||
*/
|
||||
explicit AudioSourceChannelInfo (AudioBuffer<float>& bufferToUse) noexcept
|
||||
: buffer (&bufferToUse),
|
||||
startSample (0),
|
||||
numSamples (bufferToUse.getNumSamples())
|
||||
{
|
||||
}
|
||||
|
||||
/** The destination buffer to fill with audio data.
|
||||
|
||||
When the AudioSource::getNextAudioBlock() method is called, the active section
|
||||
of this buffer should be filled with whatever output the source produces.
|
||||
|
||||
Only the samples specified by the startSample and numSamples members of this structure
|
||||
should be affected by the call.
|
||||
|
||||
The contents of the buffer when it is passed to the AudioSource::getNextAudioBlock()
|
||||
method can be treated as the input if the source is performing some kind of filter operation,
|
||||
but should be cleared if this is not the case - the clearActiveBufferRegion() is
|
||||
a handy way of doing this.
|
||||
|
||||
The number of channels in the buffer could be anything, so the AudioSource
|
||||
must cope with this in whatever way is appropriate for its function.
|
||||
*/
|
||||
AudioBuffer<float>* buffer;
|
||||
|
||||
/** The first sample in the buffer from which the callback is expected
|
||||
to write data. */
|
||||
int startSample;
|
||||
|
||||
/** The number of samples in the buffer which the callback is expected to
|
||||
fill with data. */
|
||||
int numSamples;
|
||||
|
||||
/** Convenient method to clear the buffer if the source is not producing any data. */
|
||||
void clearActiveBufferRegion() const
|
||||
{
|
||||
if (buffer != nullptr)
|
||||
buffer->clear (startSample, numSamples);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
Base class for objects that can produce a continuous stream of audio.
|
||||
|
||||
An AudioSource has two states: 'prepared' and 'unprepared'.
|
||||
|
||||
When a source needs to be played, it is first put into a 'prepared' state by a call to
|
||||
prepareToPlay(), and then repeated calls will be made to its getNextAudioBlock() method to
|
||||
process the audio data.
|
||||
|
||||
Once playback has finished, the releaseResources() method is called to put the stream
|
||||
back into an 'unprepared' state.
|
||||
|
||||
@see AudioFormatReaderSource, ResamplingAudioSource
|
||||
|
||||
@tags{Audio}
|
||||
*/
|
||||
class JUCE_API AudioSource
|
||||
{
|
||||
protected:
|
||||
//==============================================================================
|
||||
/** Creates an AudioSource. */
|
||||
AudioSource() = default;
|
||||
|
||||
public:
|
||||
/** Destructor. */
|
||||
virtual ~AudioSource() = default;
|
||||
|
||||
//==============================================================================
|
||||
/** Tells the source to prepare for playing.
|
||||
|
||||
An AudioSource has two states: prepared and unprepared.
|
||||
|
||||
The prepareToPlay() method is guaranteed to be called at least once on an 'unprepared'
|
||||
source to put it into a 'prepared' state before any calls will be made to getNextAudioBlock().
|
||||
This callback allows the source to initialise any resources it might need when playing.
|
||||
|
||||
Once playback has finished, the releaseResources() method is called to put the stream
|
||||
back into an 'unprepared' state.
|
||||
|
||||
Note that this method could be called more than once in succession without
|
||||
a matching call to releaseResources(), so make sure your code is robust and
|
||||
can handle that kind of situation.
|
||||
|
||||
@param samplesPerBlockExpected the number of samples that the source
|
||||
will be expected to supply each time its
|
||||
getNextAudioBlock() method is called. This
|
||||
number may vary slightly, because it will be dependent
|
||||
on audio hardware callbacks, and these aren't
|
||||
guaranteed to always use a constant block size, so
|
||||
the source should be able to cope with small variations.
|
||||
@param sampleRate the sample rate that the output will be used at - this
|
||||
is needed by sources such as tone generators.
|
||||
@see releaseResources, getNextAudioBlock
|
||||
*/
|
||||
virtual void prepareToPlay (int samplesPerBlockExpected,
|
||||
double sampleRate) = 0;
|
||||
|
||||
/** Allows the source to release anything it no longer needs after playback has stopped.
|
||||
|
||||
This will be called when the source is no longer going to have its getNextAudioBlock()
|
||||
method called, so it should release any spare memory, etc. that it might have
|
||||
allocated during the prepareToPlay() call.
|
||||
|
||||
Note that there's no guarantee that prepareToPlay() will actually have been called before
|
||||
releaseResources(), and it may be called more than once in succession, so make sure your
|
||||
code is robust and doesn't make any assumptions about when it will be called.
|
||||
|
||||
@see prepareToPlay, getNextAudioBlock
|
||||
*/
|
||||
virtual void releaseResources() = 0;
|
||||
|
||||
/** Called repeatedly to fetch subsequent blocks of audio data.
|
||||
|
||||
After calling the prepareToPlay() method, this callback will be made each
|
||||
time the audio playback hardware (or whatever other destination the audio
|
||||
data is going to) needs another block of data.
|
||||
|
||||
It will generally be called on a high-priority system thread, or possibly even
|
||||
an interrupt, so be careful not to do too much work here, as that will cause
|
||||
audio glitches!
|
||||
|
||||
@see AudioSourceChannelInfo, prepareToPlay, releaseResources
|
||||
*/
|
||||
virtual void getNextAudioBlock (const AudioSourceChannelInfo& bufferToFill) = 0;
|
||||
};
|
||||
|
||||
} // namespace juce
|
342
deps/juce/modules/juce_audio_basics/sources/juce_BufferingAudioSource.cpp
vendored
Normal file
342
deps/juce/modules/juce_audio_basics/sources/juce_BufferingAudioSource.cpp
vendored
Normal file
@ -0,0 +1,342 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2020 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
The code included in this file is provided under the terms of the ISC license
|
||||
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
|
||||
To use, copy, modify, and/or distribute this software for any purpose with or
|
||||
without fee is hereby granted provided that the above copyright notice and
|
||||
this permission notice appear in all copies.
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
BufferingAudioSource::BufferingAudioSource (PositionableAudioSource* s,
|
||||
TimeSliceThread& thread,
|
||||
bool deleteSourceWhenDeleted,
|
||||
int bufferSizeSamples,
|
||||
int numChannels,
|
||||
bool prefillBufferOnPrepareToPlay)
|
||||
: source (s, deleteSourceWhenDeleted),
|
||||
backgroundThread (thread),
|
||||
numberOfSamplesToBuffer (jmax (1024, bufferSizeSamples)),
|
||||
numberOfChannels (numChannels),
|
||||
prefillBuffer (prefillBufferOnPrepareToPlay)
|
||||
{
|
||||
jassert (source != nullptr);
|
||||
|
||||
jassert (numberOfSamplesToBuffer > 1024); // not much point using this class if you're
|
||||
// not using a larger buffer..
|
||||
}
|
||||
|
||||
BufferingAudioSource::~BufferingAudioSource()
|
||||
{
|
||||
releaseResources();
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
void BufferingAudioSource::prepareToPlay (int samplesPerBlockExpected, double newSampleRate)
|
||||
{
|
||||
auto bufferSizeNeeded = jmax (samplesPerBlockExpected * 2, numberOfSamplesToBuffer);
|
||||
|
||||
if (newSampleRate != sampleRate
|
||||
|| bufferSizeNeeded != buffer.getNumSamples()
|
||||
|| ! isPrepared)
|
||||
{
|
||||
backgroundThread.removeTimeSliceClient (this);
|
||||
|
||||
isPrepared = true;
|
||||
sampleRate = newSampleRate;
|
||||
|
||||
source->prepareToPlay (samplesPerBlockExpected, newSampleRate);
|
||||
|
||||
buffer.setSize (numberOfChannels, bufferSizeNeeded);
|
||||
buffer.clear();
|
||||
|
||||
const ScopedLock sl (bufferRangeLock);
|
||||
|
||||
bufferValidStart = 0;
|
||||
bufferValidEnd = 0;
|
||||
|
||||
backgroundThread.addTimeSliceClient (this);
|
||||
|
||||
do
|
||||
{
|
||||
const ScopedUnlock ul (bufferRangeLock);
|
||||
|
||||
backgroundThread.moveToFrontOfQueue (this);
|
||||
Thread::sleep (5);
|
||||
}
|
||||
while (prefillBuffer
|
||||
&& (bufferValidEnd - bufferValidStart < jmin (((int) newSampleRate) / 4, buffer.getNumSamples() / 2)));
|
||||
}
|
||||
}
|
||||
|
||||
void BufferingAudioSource::releaseResources()
|
||||
{
|
||||
isPrepared = false;
|
||||
backgroundThread.removeTimeSliceClient (this);
|
||||
|
||||
buffer.setSize (numberOfChannels, 0);
|
||||
|
||||
// MSVC2015 seems to need this if statement to not generate a warning during linking.
|
||||
// As source is set in the constructor, there is no way that source could
|
||||
// ever equal this, but it seems to make MSVC2015 happy.
|
||||
if (source != this)
|
||||
source->releaseResources();
|
||||
}
|
||||
|
||||
void BufferingAudioSource::getNextAudioBlock (const AudioSourceChannelInfo& info)
|
||||
{
|
||||
const auto bufferRange = getValidBufferRange (info.numSamples);
|
||||
|
||||
if (bufferRange.isEmpty())
|
||||
{
|
||||
// total cache miss
|
||||
info.clearActiveBufferRegion();
|
||||
return;
|
||||
}
|
||||
|
||||
const auto validStart = bufferRange.getStart();
|
||||
const auto validEnd = bufferRange.getEnd();
|
||||
|
||||
const ScopedLock sl (callbackLock);
|
||||
|
||||
if (validStart > 0)
|
||||
info.buffer->clear (info.startSample, validStart); // partial cache miss at start
|
||||
|
||||
if (validEnd < info.numSamples)
|
||||
info.buffer->clear (info.startSample + validEnd,
|
||||
info.numSamples - validEnd); // partial cache miss at end
|
||||
|
||||
if (validStart < validEnd)
|
||||
{
|
||||
for (int chan = jmin (numberOfChannels, info.buffer->getNumChannels()); --chan >= 0;)
|
||||
{
|
||||
jassert (buffer.getNumSamples() > 0);
|
||||
|
||||
const auto startBufferIndex = (int) ((validStart + nextPlayPos) % buffer.getNumSamples());
|
||||
const auto endBufferIndex = (int) ((validEnd + nextPlayPos) % buffer.getNumSamples());
|
||||
|
||||
if (startBufferIndex < endBufferIndex)
|
||||
{
|
||||
info.buffer->copyFrom (chan, info.startSample + validStart,
|
||||
buffer,
|
||||
chan, startBufferIndex,
|
||||
validEnd - validStart);
|
||||
}
|
||||
else
|
||||
{
|
||||
const auto initialSize = buffer.getNumSamples() - startBufferIndex;
|
||||
|
||||
info.buffer->copyFrom (chan, info.startSample + validStart,
|
||||
buffer,
|
||||
chan, startBufferIndex,
|
||||
initialSize);
|
||||
|
||||
info.buffer->copyFrom (chan, info.startSample + validStart + initialSize,
|
||||
buffer,
|
||||
chan, 0,
|
||||
(validEnd - validStart) - initialSize);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
nextPlayPos += info.numSamples;
|
||||
}
|
||||
|
||||
bool BufferingAudioSource::waitForNextAudioBlockReady (const AudioSourceChannelInfo& info, uint32 timeout)
|
||||
{
|
||||
if (source == nullptr || source->getTotalLength() <= 0)
|
||||
return false;
|
||||
|
||||
if ((nextPlayPos + info.numSamples < 0)
|
||||
|| (! isLooping() && nextPlayPos > getTotalLength()))
|
||||
return true;
|
||||
|
||||
const auto startTime = Time::getMillisecondCounter();
|
||||
auto now = startTime;
|
||||
|
||||
auto elapsed = (now >= startTime ? now - startTime
|
||||
: (std::numeric_limits<uint32>::max() - startTime) + now);
|
||||
|
||||
while (elapsed <= timeout)
|
||||
{
|
||||
const auto bufferRange = getValidBufferRange (info.numSamples);
|
||||
|
||||
const auto validStart = bufferRange.getStart();
|
||||
const auto validEnd = bufferRange.getEnd();
|
||||
|
||||
if (validStart <= 0
|
||||
&& validStart < validEnd
|
||||
&& validEnd >= info.numSamples)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (elapsed < timeout
|
||||
&& ! bufferReadyEvent.wait (static_cast<int> (timeout - elapsed)))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
now = Time::getMillisecondCounter();
|
||||
elapsed = (now >= startTime ? now - startTime
|
||||
: (std::numeric_limits<uint32>::max() - startTime) + now);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void BufferingAudioSource::setLoopRange (int64 loopStart, int64 loopLength)
|
||||
{
|
||||
source->setLoopRange(loopStart, loopLength);
|
||||
loopRangeChanged = true;
|
||||
backgroundThread.moveToFrontOfQueue (this);
|
||||
}
|
||||
|
||||
|
||||
int64 BufferingAudioSource::getNextReadPosition() const
|
||||
{
|
||||
jassert (source->getTotalLength() > 0);
|
||||
const auto pos = nextPlayPos.load();
|
||||
|
||||
if (source->isLooping()) {
|
||||
int64 loopstart, looplen;
|
||||
source->getLoopRange(loopstart, looplen);
|
||||
return ((pos - loopstart) % looplen) + loopstart ;
|
||||
}
|
||||
else return pos;
|
||||
}
|
||||
|
||||
|
||||
void BufferingAudioSource::setNextReadPosition (int64 newPosition)
|
||||
{
|
||||
const ScopedLock sl (bufferRangeLock);
|
||||
|
||||
nextPlayPos = newPosition;
|
||||
backgroundThread.moveToFrontOfQueue (this);
|
||||
}
|
||||
|
||||
Range<int> BufferingAudioSource::getValidBufferRange (int numSamples) const
|
||||
{
|
||||
const ScopedLock sl (bufferRangeLock);
|
||||
|
||||
const auto pos = nextPlayPos.load();
|
||||
|
||||
return { (int) (jlimit (bufferValidStart, bufferValidEnd, pos) - pos),
|
||||
(int) (jlimit (bufferValidStart, bufferValidEnd, pos + numSamples) - pos) };
|
||||
}
|
||||
|
||||
bool BufferingAudioSource::readNextBufferChunk()
|
||||
{
|
||||
int64 newBVS, newBVE, sectionToReadStart, sectionToReadEnd;
|
||||
|
||||
{
|
||||
const ScopedLock sl (bufferRangeLock);
|
||||
|
||||
if (wasSourceLooping != isLooping() || loopRangeChanged)
|
||||
{
|
||||
wasSourceLooping = isLooping();
|
||||
bufferValidStart = 0;
|
||||
bufferValidEnd = 0;
|
||||
loopRangeChanged = false;
|
||||
}
|
||||
|
||||
newBVS = jmax ((int64) 0, nextPlayPos.load());
|
||||
newBVE = newBVS + buffer.getNumSamples() - 4;
|
||||
sectionToReadStart = 0;
|
||||
sectionToReadEnd = 0;
|
||||
|
||||
constexpr int maxChunkSize = 2048;
|
||||
|
||||
if (newBVS < bufferValidStart || newBVS >= bufferValidEnd)
|
||||
{
|
||||
newBVE = jmin (newBVE, newBVS + maxChunkSize);
|
||||
|
||||
sectionToReadStart = newBVS;
|
||||
sectionToReadEnd = newBVE;
|
||||
|
||||
bufferValidStart = 0;
|
||||
bufferValidEnd = 0;
|
||||
}
|
||||
else if (std::abs ((int) (newBVS - bufferValidStart)) > 512
|
||||
|| std::abs ((int) (newBVE - bufferValidEnd)) > 512)
|
||||
{
|
||||
newBVE = jmin (newBVE, bufferValidEnd + maxChunkSize);
|
||||
|
||||
sectionToReadStart = bufferValidEnd;
|
||||
sectionToReadEnd = newBVE;
|
||||
|
||||
bufferValidStart = newBVS;
|
||||
bufferValidEnd = jmin (bufferValidEnd, newBVE);
|
||||
}
|
||||
}
|
||||
|
||||
if (sectionToReadStart == sectionToReadEnd)
|
||||
return false;
|
||||
|
||||
jassert (buffer.getNumSamples() > 0);
|
||||
|
||||
const auto bufferIndexStart = (int) (sectionToReadStart % buffer.getNumSamples());
|
||||
const auto bufferIndexEnd = (int) (sectionToReadEnd % buffer.getNumSamples());
|
||||
|
||||
if (bufferIndexStart < bufferIndexEnd)
|
||||
{
|
||||
readBufferSection (sectionToReadStart,
|
||||
(int) (sectionToReadEnd - sectionToReadStart),
|
||||
bufferIndexStart);
|
||||
}
|
||||
else
|
||||
{
|
||||
const auto initialSize = buffer.getNumSamples() - bufferIndexStart;
|
||||
|
||||
readBufferSection (sectionToReadStart,
|
||||
initialSize,
|
||||
bufferIndexStart);
|
||||
|
||||
readBufferSection (sectionToReadStart + initialSize,
|
||||
(int) (sectionToReadEnd - sectionToReadStart) - initialSize,
|
||||
0);
|
||||
}
|
||||
|
||||
{
|
||||
const ScopedLock sl2 (bufferRangeLock);
|
||||
|
||||
bufferValidStart = newBVS;
|
||||
bufferValidEnd = newBVE;
|
||||
}
|
||||
|
||||
bufferReadyEvent.signal();
|
||||
return true;
|
||||
}
|
||||
|
||||
void BufferingAudioSource::readBufferSection (int64 start, int length, int bufferOffset)
|
||||
{
|
||||
if (source->getNextReadPosition() != start)
|
||||
source->setNextReadPosition (start);
|
||||
|
||||
AudioSourceChannelInfo info (&buffer, bufferOffset, length);
|
||||
|
||||
const ScopedLock sl (callbackLock);
|
||||
source->getNextAudioBlock (info);
|
||||
}
|
||||
|
||||
int BufferingAudioSource::useTimeSlice()
|
||||
{
|
||||
return readNextBufferChunk() ? 1 : 100;
|
||||
}
|
||||
|
||||
} // namespace juce
|
134
deps/juce/modules/juce_audio_basics/sources/juce_BufferingAudioSource.h
vendored
Normal file
134
deps/juce/modules/juce_audio_basics/sources/juce_BufferingAudioSource.h
vendored
Normal file
@ -0,0 +1,134 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2020 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
The code included in this file is provided under the terms of the ISC license
|
||||
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
|
||||
To use, copy, modify, and/or distribute this software for any purpose with or
|
||||
without fee is hereby granted provided that the above copyright notice and
|
||||
this permission notice appear in all copies.
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
An AudioSource which takes another source as input, and buffers it using a thread.
|
||||
|
||||
Create this as a wrapper around another thread, and it will read-ahead with
|
||||
a background thread to smooth out playback. You can either create one of these
|
||||
directly, or use it indirectly using an AudioTransportSource.
|
||||
|
||||
@see PositionableAudioSource, AudioTransportSource
|
||||
|
||||
@tags{Audio}
|
||||
*/
|
||||
class JUCE_API BufferingAudioSource : public PositionableAudioSource,
|
||||
private TimeSliceClient
|
||||
{
|
||||
public:
|
||||
//==============================================================================
|
||||
/** Creates a BufferingAudioSource.
|
||||
|
||||
@param source the input source to read from
|
||||
@param backgroundThread a background thread that will be used for the
|
||||
background read-ahead. This object must not be deleted
|
||||
until after any BufferingAudioSources that are using it
|
||||
have been deleted!
|
||||
@param deleteSourceWhenDeleted if true, then the input source object will
|
||||
be deleted when this object is deleted
|
||||
@param numberOfSamplesToBuffer the size of buffer to use for reading ahead
|
||||
@param numberOfChannels the number of channels that will be played
|
||||
@param prefillBufferOnPrepareToPlay if true, then calling prepareToPlay on this object will
|
||||
block until the buffer has been filled
|
||||
*/
|
||||
BufferingAudioSource (PositionableAudioSource* source,
|
||||
TimeSliceThread& backgroundThread,
|
||||
bool deleteSourceWhenDeleted,
|
||||
int numberOfSamplesToBuffer,
|
||||
int numberOfChannels = 2,
|
||||
bool prefillBufferOnPrepareToPlay = true);
|
||||
|
||||
/** Destructor.
|
||||
|
||||
The input source may be deleted depending on whether the deleteSourceWhenDeleted
|
||||
flag was set in the constructor.
|
||||
*/
|
||||
~BufferingAudioSource() override;
|
||||
|
||||
//==============================================================================
|
||||
/** Implementation of the AudioSource method. */
|
||||
void prepareToPlay (int samplesPerBlockExpected, double sampleRate) override;
|
||||
|
||||
/** Implementation of the AudioSource method. */
|
||||
void releaseResources() override;
|
||||
|
||||
/** Implementation of the AudioSource method. */
|
||||
void getNextAudioBlock (const AudioSourceChannelInfo&) override;
|
||||
|
||||
//==============================================================================
|
||||
/** Implements the PositionableAudioSource method. */
|
||||
void setNextReadPosition (int64 newPosition) override;
|
||||
|
||||
/** Implements the PositionableAudioSource method. */
|
||||
int64 getNextReadPosition() const override;
|
||||
|
||||
/** Implements the PositionableAudioSource method. */
|
||||
int64 getTotalLength() const override { return source->getTotalLength(); }
|
||||
|
||||
/** Implements the PositionableAudioSource method. */
|
||||
bool isLooping() const override { return source->isLooping(); }
|
||||
|
||||
/** Implements the PositionableAudioSource method. */
|
||||
void setLooping(bool shouldLoop) override { return source->setLooping(shouldLoop); }
|
||||
|
||||
/** Implements the PositionableAudioSource method. */
|
||||
void setLoopRange (int64 loopStart, int64 loopLength) override;
|
||||
|
||||
/** Implements the PositionableAudioSource method. */
|
||||
void getLoopRange (int64 & loopStart, int64 & loopLength) const override { return source->getLoopRange(loopStart, loopLength); }
|
||||
|
||||
/** A useful function to block until the next the buffer info can be filled.
|
||||
|
||||
This is useful for offline rendering.
|
||||
*/
|
||||
bool waitForNextAudioBlockReady (const AudioSourceChannelInfo& info, const uint32 timeout);
|
||||
|
||||
private:
|
||||
//==============================================================================
|
||||
Range<int> getValidBufferRange (int numSamples) const;
|
||||
bool readNextBufferChunk();
|
||||
void readBufferSection (int64 start, int length, int bufferOffset);
|
||||
int useTimeSlice() override;
|
||||
|
||||
//==============================================================================
|
||||
OptionalScopedPointer<PositionableAudioSource> source;
|
||||
TimeSliceThread& backgroundThread;
|
||||
int numberOfSamplesToBuffer, numberOfChannels;
|
||||
AudioBuffer<float> buffer;
|
||||
CriticalSection callbackLock, bufferRangeLock;
|
||||
WaitableEvent bufferReadyEvent;
|
||||
int64 bufferValidStart = 0, bufferValidEnd = 0;
|
||||
std::atomic<int64> nextPlayPos { 0 };
|
||||
double sampleRate = 0;
|
||||
bool wasSourceLooping = false, isPrepared = false;
|
||||
bool loopRangeChanged = false;
|
||||
const bool prefillBuffer;
|
||||
|
||||
//==============================================================================
|
||||
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (BufferingAudioSource)
|
||||
};
|
||||
|
||||
} // namespace juce
|
187
deps/juce/modules/juce_audio_basics/sources/juce_ChannelRemappingAudioSource.cpp
vendored
Normal file
187
deps/juce/modules/juce_audio_basics/sources/juce_ChannelRemappingAudioSource.cpp
vendored
Normal file
@ -0,0 +1,187 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2020 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
The code included in this file is provided under the terms of the ISC license
|
||||
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
|
||||
To use, copy, modify, and/or distribute this software for any purpose with or
|
||||
without fee is hereby granted provided that the above copyright notice and
|
||||
this permission notice appear in all copies.
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
ChannelRemappingAudioSource::ChannelRemappingAudioSource (AudioSource* const source_,
|
||||
const bool deleteSourceWhenDeleted)
|
||||
: source (source_, deleteSourceWhenDeleted),
|
||||
requiredNumberOfChannels (2)
|
||||
{
|
||||
remappedInfo.buffer = &buffer;
|
||||
remappedInfo.startSample = 0;
|
||||
}
|
||||
|
||||
ChannelRemappingAudioSource::~ChannelRemappingAudioSource() {}
|
||||
|
||||
//==============================================================================
|
||||
void ChannelRemappingAudioSource::setNumberOfChannelsToProduce (const int requiredNumberOfChannels_)
|
||||
{
|
||||
const ScopedLock sl (lock);
|
||||
requiredNumberOfChannels = requiredNumberOfChannels_;
|
||||
}
|
||||
|
||||
void ChannelRemappingAudioSource::clearAllMappings()
|
||||
{
|
||||
const ScopedLock sl (lock);
|
||||
|
||||
remappedInputs.clear();
|
||||
remappedOutputs.clear();
|
||||
}
|
||||
|
||||
void ChannelRemappingAudioSource::setInputChannelMapping (const int destIndex, const int sourceIndex)
|
||||
{
|
||||
const ScopedLock sl (lock);
|
||||
|
||||
while (remappedInputs.size() < destIndex)
|
||||
remappedInputs.add (-1);
|
||||
|
||||
remappedInputs.set (destIndex, sourceIndex);
|
||||
}
|
||||
|
||||
void ChannelRemappingAudioSource::setOutputChannelMapping (const int sourceIndex, const int destIndex)
|
||||
{
|
||||
const ScopedLock sl (lock);
|
||||
|
||||
while (remappedOutputs.size() < sourceIndex)
|
||||
remappedOutputs.add (-1);
|
||||
|
||||
remappedOutputs.set (sourceIndex, destIndex);
|
||||
}
|
||||
|
||||
int ChannelRemappingAudioSource::getRemappedInputChannel (const int inputChannelIndex) const
|
||||
{
|
||||
const ScopedLock sl (lock);
|
||||
|
||||
if (inputChannelIndex >= 0 && inputChannelIndex < remappedInputs.size())
|
||||
return remappedInputs.getUnchecked (inputChannelIndex);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
int ChannelRemappingAudioSource::getRemappedOutputChannel (const int outputChannelIndex) const
|
||||
{
|
||||
const ScopedLock sl (lock);
|
||||
|
||||
if (outputChannelIndex >= 0 && outputChannelIndex < remappedOutputs.size())
|
||||
return remappedOutputs .getUnchecked (outputChannelIndex);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
void ChannelRemappingAudioSource::prepareToPlay (int samplesPerBlockExpected, double sampleRate)
|
||||
{
|
||||
source->prepareToPlay (samplesPerBlockExpected, sampleRate);
|
||||
}
|
||||
|
||||
void ChannelRemappingAudioSource::releaseResources()
|
||||
{
|
||||
source->releaseResources();
|
||||
}
|
||||
|
||||
void ChannelRemappingAudioSource::getNextAudioBlock (const AudioSourceChannelInfo& bufferToFill)
|
||||
{
|
||||
const ScopedLock sl (lock);
|
||||
|
||||
buffer.setSize (requiredNumberOfChannels, bufferToFill.numSamples, false, false, true);
|
||||
|
||||
const int numChans = bufferToFill.buffer->getNumChannels();
|
||||
|
||||
for (int i = 0; i < buffer.getNumChannels(); ++i)
|
||||
{
|
||||
const int remappedChan = getRemappedInputChannel (i);
|
||||
|
||||
if (remappedChan >= 0 && remappedChan < numChans)
|
||||
{
|
||||
buffer.copyFrom (i, 0, *bufferToFill.buffer,
|
||||
remappedChan,
|
||||
bufferToFill.startSample,
|
||||
bufferToFill.numSamples);
|
||||
}
|
||||
else
|
||||
{
|
||||
buffer.clear (i, 0, bufferToFill.numSamples);
|
||||
}
|
||||
}
|
||||
|
||||
remappedInfo.numSamples = bufferToFill.numSamples;
|
||||
|
||||
source->getNextAudioBlock (remappedInfo);
|
||||
|
||||
bufferToFill.clearActiveBufferRegion();
|
||||
|
||||
for (int i = 0; i < requiredNumberOfChannels; ++i)
|
||||
{
|
||||
const int remappedChan = getRemappedOutputChannel (i);
|
||||
|
||||
if (remappedChan >= 0 && remappedChan < numChans)
|
||||
{
|
||||
bufferToFill.buffer->addFrom (remappedChan, bufferToFill.startSample,
|
||||
buffer, i, 0, bufferToFill.numSamples);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
std::unique_ptr<XmlElement> ChannelRemappingAudioSource::createXml() const
|
||||
{
|
||||
auto e = std::make_unique<XmlElement> ("MAPPINGS");
|
||||
String ins, outs;
|
||||
|
||||
const ScopedLock sl (lock);
|
||||
|
||||
for (int i = 0; i < remappedInputs.size(); ++i)
|
||||
ins << remappedInputs.getUnchecked(i) << ' ';
|
||||
|
||||
for (int i = 0; i < remappedOutputs.size(); ++i)
|
||||
outs << remappedOutputs.getUnchecked(i) << ' ';
|
||||
|
||||
e->setAttribute ("inputs", ins.trimEnd());
|
||||
e->setAttribute ("outputs", outs.trimEnd());
|
||||
|
||||
return e;
|
||||
}
|
||||
|
||||
void ChannelRemappingAudioSource::restoreFromXml (const XmlElement& e)
|
||||
{
|
||||
if (e.hasTagName ("MAPPINGS"))
|
||||
{
|
||||
const ScopedLock sl (lock);
|
||||
|
||||
clearAllMappings();
|
||||
|
||||
StringArray ins, outs;
|
||||
ins.addTokens (e.getStringAttribute ("inputs"), false);
|
||||
outs.addTokens (e.getStringAttribute ("outputs"), false);
|
||||
|
||||
for (int i = 0; i < ins.size(); ++i)
|
||||
remappedInputs.add (ins[i].getIntValue());
|
||||
|
||||
for (int i = 0; i < outs.size(); ++i)
|
||||
remappedOutputs.add (outs[i].getIntValue());
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace juce
|
141
deps/juce/modules/juce_audio_basics/sources/juce_ChannelRemappingAudioSource.h
vendored
Normal file
141
deps/juce/modules/juce_audio_basics/sources/juce_ChannelRemappingAudioSource.h
vendored
Normal file
@ -0,0 +1,141 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2020 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
The code included in this file is provided under the terms of the ISC license
|
||||
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
|
||||
To use, copy, modify, and/or distribute this software for any purpose with or
|
||||
without fee is hereby granted provided that the above copyright notice and
|
||||
this permission notice appear in all copies.
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
An AudioSource that takes the audio from another source, and re-maps its
|
||||
input and output channels to a different arrangement.
|
||||
|
||||
You can use this to increase or decrease the number of channels that an
|
||||
audio source uses, or to re-order those channels.
|
||||
|
||||
Call the reset() method before using it to set up a default mapping, and then
|
||||
the setInputChannelMapping() and setOutputChannelMapping() methods to
|
||||
create an appropriate mapping, otherwise no channels will be connected and
|
||||
it'll produce silence.
|
||||
|
||||
@see AudioSource
|
||||
|
||||
@tags{Audio}
|
||||
*/
|
||||
class ChannelRemappingAudioSource : public AudioSource
|
||||
{
|
||||
public:
|
||||
//==============================================================================
|
||||
/** Creates a remapping source that will pass on audio from the given input.
|
||||
|
||||
@param source the input source to use. Make sure that this doesn't
|
||||
get deleted before the ChannelRemappingAudioSource object
|
||||
@param deleteSourceWhenDeleted if true, the input source will be deleted
|
||||
when this object is deleted, if false, the caller is
|
||||
responsible for its deletion
|
||||
*/
|
||||
ChannelRemappingAudioSource (AudioSource* source,
|
||||
bool deleteSourceWhenDeleted);
|
||||
|
||||
/** Destructor. */
|
||||
~ChannelRemappingAudioSource() override;
|
||||
|
||||
//==============================================================================
|
||||
/** Specifies a number of channels that this audio source must produce from its
|
||||
getNextAudioBlock() callback.
|
||||
*/
|
||||
void setNumberOfChannelsToProduce (int requiredNumberOfChannels);
|
||||
|
||||
/** Clears any mapped channels.
|
||||
|
||||
After this, no channels are mapped, so this object will produce silence. Create
|
||||
some mappings with setInputChannelMapping() and setOutputChannelMapping().
|
||||
*/
|
||||
void clearAllMappings();
|
||||
|
||||
/** Creates an input channel mapping.
|
||||
|
||||
When the getNextAudioBlock() method is called, the data in channel sourceChannelIndex of the incoming
|
||||
data will be sent to destChannelIndex of our input source.
|
||||
|
||||
@param destChannelIndex the index of an input channel in our input audio source (i.e. the
|
||||
source specified when this object was created).
|
||||
@param sourceChannelIndex the index of the input channel in the incoming audio data buffer
|
||||
during our getNextAudioBlock() callback
|
||||
*/
|
||||
void setInputChannelMapping (int destChannelIndex,
|
||||
int sourceChannelIndex);
|
||||
|
||||
/** Creates an output channel mapping.
|
||||
|
||||
When the getNextAudioBlock() method is called, the data returned in channel sourceChannelIndex by
|
||||
our input audio source will be copied to channel destChannelIndex of the final buffer.
|
||||
|
||||
@param sourceChannelIndex the index of an output channel coming from our input audio source
|
||||
(i.e. the source specified when this object was created).
|
||||
@param destChannelIndex the index of the output channel in the incoming audio data buffer
|
||||
during our getNextAudioBlock() callback
|
||||
*/
|
||||
void setOutputChannelMapping (int sourceChannelIndex,
|
||||
int destChannelIndex);
|
||||
|
||||
/** Returns the channel from our input that will be sent to channel inputChannelIndex of
|
||||
our input audio source.
|
||||
*/
|
||||
int getRemappedInputChannel (int inputChannelIndex) const;
|
||||
|
||||
/** Returns the output channel to which channel outputChannelIndex of our input audio
|
||||
source will be sent to.
|
||||
*/
|
||||
int getRemappedOutputChannel (int outputChannelIndex) const;
|
||||
|
||||
|
||||
//==============================================================================
|
||||
/** Returns an XML object to encapsulate the state of the mappings.
|
||||
@see restoreFromXml
|
||||
*/
|
||||
std::unique_ptr<XmlElement> createXml() const;
|
||||
|
||||
/** Restores the mappings from an XML object created by createXML().
|
||||
@see createXml
|
||||
*/
|
||||
void restoreFromXml (const XmlElement&);
|
||||
|
||||
//==============================================================================
|
||||
void prepareToPlay (int samplesPerBlockExpected, double sampleRate) override;
|
||||
void releaseResources() override;
|
||||
void getNextAudioBlock (const AudioSourceChannelInfo&) override;
|
||||
|
||||
|
||||
private:
|
||||
//==============================================================================
|
||||
OptionalScopedPointer<AudioSource> source;
|
||||
Array<int> remappedInputs, remappedOutputs;
|
||||
int requiredNumberOfChannels;
|
||||
|
||||
AudioBuffer<float> buffer;
|
||||
AudioSourceChannelInfo remappedInfo;
|
||||
CriticalSection lock;
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ChannelRemappingAudioSource)
|
||||
};
|
||||
|
||||
} // namespace juce
|
80
deps/juce/modules/juce_audio_basics/sources/juce_IIRFilterAudioSource.cpp
vendored
Normal file
80
deps/juce/modules/juce_audio_basics/sources/juce_IIRFilterAudioSource.cpp
vendored
Normal file
@ -0,0 +1,80 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2020 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
The code included in this file is provided under the terms of the ISC license
|
||||
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
|
||||
To use, copy, modify, and/or distribute this software for any purpose with or
|
||||
without fee is hereby granted provided that the above copyright notice and
|
||||
this permission notice appear in all copies.
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
IIRFilterAudioSource::IIRFilterAudioSource (AudioSource* const inputSource,
|
||||
const bool deleteInputWhenDeleted)
|
||||
: input (inputSource, deleteInputWhenDeleted)
|
||||
{
|
||||
jassert (inputSource != nullptr);
|
||||
|
||||
for (int i = 2; --i >= 0;)
|
||||
iirFilters.add (new IIRFilter());
|
||||
}
|
||||
|
||||
IIRFilterAudioSource::~IIRFilterAudioSource() {}
|
||||
|
||||
//==============================================================================
|
||||
void IIRFilterAudioSource::setCoefficients (const IIRCoefficients& newCoefficients)
|
||||
{
|
||||
for (int i = iirFilters.size(); --i >= 0;)
|
||||
iirFilters.getUnchecked(i)->setCoefficients (newCoefficients);
|
||||
}
|
||||
|
||||
void IIRFilterAudioSource::makeInactive()
|
||||
{
|
||||
for (int i = iirFilters.size(); --i >= 0;)
|
||||
iirFilters.getUnchecked(i)->makeInactive();
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
void IIRFilterAudioSource::prepareToPlay (int samplesPerBlockExpected, double sampleRate)
|
||||
{
|
||||
input->prepareToPlay (samplesPerBlockExpected, sampleRate);
|
||||
|
||||
for (int i = iirFilters.size(); --i >= 0;)
|
||||
iirFilters.getUnchecked(i)->reset();
|
||||
}
|
||||
|
||||
void IIRFilterAudioSource::releaseResources()
|
||||
{
|
||||
input->releaseResources();
|
||||
}
|
||||
|
||||
void IIRFilterAudioSource::getNextAudioBlock (const AudioSourceChannelInfo& bufferToFill)
|
||||
{
|
||||
input->getNextAudioBlock (bufferToFill);
|
||||
|
||||
const int numChannels = bufferToFill.buffer->getNumChannels();
|
||||
|
||||
while (numChannels > iirFilters.size())
|
||||
iirFilters.add (new IIRFilter (*iirFilters.getUnchecked (0)));
|
||||
|
||||
for (int i = 0; i < numChannels; ++i)
|
||||
iirFilters.getUnchecked(i)
|
||||
->processSamples (bufferToFill.buffer->getWritePointer (i, bufferToFill.startSample),
|
||||
bufferToFill.numSamples);
|
||||
}
|
||||
|
||||
} // namespace juce
|
68
deps/juce/modules/juce_audio_basics/sources/juce_IIRFilterAudioSource.h
vendored
Normal file
68
deps/juce/modules/juce_audio_basics/sources/juce_IIRFilterAudioSource.h
vendored
Normal file
@ -0,0 +1,68 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2020 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
The code included in this file is provided under the terms of the ISC license
|
||||
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
|
||||
To use, copy, modify, and/or distribute this software for any purpose with or
|
||||
without fee is hereby granted provided that the above copyright notice and
|
||||
this permission notice appear in all copies.
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
An AudioSource that performs an IIR filter on another source.
|
||||
|
||||
@tags{Audio}
|
||||
*/
|
||||
class JUCE_API IIRFilterAudioSource : public AudioSource
|
||||
{
|
||||
public:
|
||||
//==============================================================================
|
||||
/** Creates a IIRFilterAudioSource for a given input source.
|
||||
|
||||
@param inputSource the input source to read from - this must not be null
|
||||
@param deleteInputWhenDeleted if true, the input source will be deleted when
|
||||
this object is deleted
|
||||
*/
|
||||
IIRFilterAudioSource (AudioSource* inputSource,
|
||||
bool deleteInputWhenDeleted);
|
||||
|
||||
/** Destructor. */
|
||||
~IIRFilterAudioSource() override;
|
||||
|
||||
//==============================================================================
|
||||
/** Changes the filter to use the same parameters as the one being passed in. */
|
||||
void setCoefficients (const IIRCoefficients& newCoefficients);
|
||||
|
||||
/** Calls IIRFilter::makeInactive() on all the filters being used internally. */
|
||||
void makeInactive();
|
||||
|
||||
//==============================================================================
|
||||
void prepareToPlay (int samplesPerBlockExpected, double sampleRate) override;
|
||||
void releaseResources() override;
|
||||
void getNextAudioBlock (const AudioSourceChannelInfo&) override;
|
||||
|
||||
private:
|
||||
//==============================================================================
|
||||
OptionalScopedPointer<AudioSource> input;
|
||||
OwnedArray<IIRFilter> iirFilters;
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (IIRFilterAudioSource)
|
||||
};
|
||||
|
||||
} // namespace juce
|
273
deps/juce/modules/juce_audio_basics/sources/juce_MemoryAudioSource.cpp
vendored
Normal file
273
deps/juce/modules/juce_audio_basics/sources/juce_MemoryAudioSource.cpp
vendored
Normal file
@ -0,0 +1,273 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2020 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
The code included in this file is provided under the terms of the ISC license
|
||||
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
|
||||
To use, copy, modify, and/or distribute this software for any purpose with or
|
||||
without fee is hereby granted provided that the above copyright notice and
|
||||
this permission notice appear in all copies.
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
MemoryAudioSource::MemoryAudioSource (AudioBuffer<float>& bufferToUse, bool copyMemory, bool shouldLoop)
|
||||
: isCurrentlyLooping (shouldLoop), loopStartPos(0), loopLen(bufferToUse.getNumSamples())
|
||||
{
|
||||
if (copyMemory)
|
||||
buffer.makeCopyOf (bufferToUse);
|
||||
else
|
||||
buffer.setDataToReferTo (bufferToUse.getArrayOfWritePointers(),
|
||||
bufferToUse.getNumChannels(),
|
||||
bufferToUse.getNumSamples());
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
void MemoryAudioSource::prepareToPlay (int /*samplesPerBlockExpected*/, double /*sampleRate*/)
|
||||
{
|
||||
position = 0;
|
||||
}
|
||||
|
||||
void MemoryAudioSource::releaseResources() {}
|
||||
|
||||
void MemoryAudioSource::getNextAudioBlock (const AudioSourceChannelInfo& bufferToFill)
|
||||
{
|
||||
if (buffer.getNumSamples() == 0)
|
||||
{
|
||||
bufferToFill.clearActiveBufferRegion();
|
||||
return;
|
||||
}
|
||||
|
||||
auto& dst = *bufferToFill.buffer;
|
||||
auto channels = jmin (dst.getNumChannels(), buffer.getNumChannels());
|
||||
int max = 0, pos = 0;
|
||||
auto n = buffer.getNumSamples();
|
||||
auto m = bufferToFill.numSamples;
|
||||
|
||||
int i = position;
|
||||
for (; (i < n || isCurrentlyLooping) && (pos < m); i += max)
|
||||
{
|
||||
max = jmin (m - pos, n - (i % n));
|
||||
|
||||
int ch = 0;
|
||||
for (; ch < channels; ++ch)
|
||||
dst.copyFrom (ch, bufferToFill.startSample + pos, buffer, ch, i % n, max);
|
||||
|
||||
for (; ch < dst.getNumChannels(); ++ch)
|
||||
dst.clear (ch, bufferToFill.startSample + pos, max);
|
||||
|
||||
pos += max;
|
||||
}
|
||||
|
||||
if (pos < m)
|
||||
dst.clear (bufferToFill.startSample + pos, m - pos);
|
||||
|
||||
position = i;
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
void MemoryAudioSource::setNextReadPosition (int64 newPosition)
|
||||
{
|
||||
position = (int) newPosition;
|
||||
}
|
||||
|
||||
int64 MemoryAudioSource::getNextReadPosition() const
|
||||
{
|
||||
return position;
|
||||
}
|
||||
|
||||
int64 MemoryAudioSource::getTotalLength() const
|
||||
{
|
||||
return buffer.getNumSamples();
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
bool MemoryAudioSource::isLooping() const
|
||||
{
|
||||
return isCurrentlyLooping;
|
||||
}
|
||||
|
||||
void MemoryAudioSource::setLooping (bool shouldLoop)
|
||||
{
|
||||
isCurrentlyLooping = shouldLoop;
|
||||
}
|
||||
|
||||
void MemoryAudioSource::getLoopRange (int64 & loopStart, int64 & loopLength) const
|
||||
{
|
||||
loopStart = loopStartPos;
|
||||
loopLength = loopLen;
|
||||
}
|
||||
|
||||
void MemoryAudioSource::setLoopRange (int64 loopStart, int64 loopLength)
|
||||
{
|
||||
loopStartPos = jlimit((int64)0, (int64) (buffer.getNumSamples()-1) , loopStart);
|
||||
loopLen = jlimit((int64)1, (int64) (buffer.getNumSamples() - loopStartPos), loopLength);
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
//==============================================================================
|
||||
#if JUCE_UNIT_TESTS
|
||||
|
||||
static bool operator== (const AudioBuffer<float>& a, const AudioBuffer<float>& b)
|
||||
{
|
||||
if (a.getNumChannels() != b.getNumChannels())
|
||||
return false;
|
||||
|
||||
for (int channel = 0; channel < a.getNumChannels(); ++channel)
|
||||
{
|
||||
auto* aPtr = a.getReadPointer (channel);
|
||||
auto* bPtr = b.getReadPointer (channel);
|
||||
|
||||
if (std::vector<float> (aPtr, aPtr + a.getNumSamples())
|
||||
!= std::vector<float> (bPtr, bPtr + b.getNumSamples()))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
struct MemoryAudioSourceTests : public UnitTest
|
||||
{
|
||||
MemoryAudioSourceTests() : UnitTest ("MemoryAudioSource", UnitTestCategories::audio) {}
|
||||
|
||||
void runTest() override
|
||||
{
|
||||
constexpr int blockSize = 512;
|
||||
AudioBuffer<float> bufferToFill { 2, blockSize };
|
||||
AudioSourceChannelInfo channelInfo { bufferToFill };
|
||||
|
||||
beginTest ("A zero-length buffer produces silence, whether or not looping is enabled");
|
||||
{
|
||||
for (const bool enableLooping : { false, true })
|
||||
{
|
||||
AudioBuffer<float> buffer;
|
||||
MemoryAudioSource source { buffer, true, false };
|
||||
source.setLooping (enableLooping);
|
||||
source.prepareToPlay (blockSize, 44100.0);
|
||||
|
||||
for (int i = 0; i < 2; ++i)
|
||||
{
|
||||
play (source, channelInfo);
|
||||
expect (isSilent (bufferToFill));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
beginTest ("A short buffer without looping is played once and followed by silence");
|
||||
{
|
||||
auto buffer = getShortBuffer();
|
||||
MemoryAudioSource source { buffer, true, false };
|
||||
source.setLooping (false);
|
||||
source.prepareToPlay (blockSize, 44100.0);
|
||||
|
||||
play (source, channelInfo);
|
||||
|
||||
auto copy = buffer;
|
||||
copy.setSize (buffer.getNumChannels(), blockSize, true, true, false);
|
||||
|
||||
expect (bufferToFill == copy);
|
||||
|
||||
play (source, channelInfo);
|
||||
|
||||
expect (isSilent (bufferToFill));
|
||||
}
|
||||
|
||||
beginTest ("A short buffer with looping is played multiple times");
|
||||
{
|
||||
auto buffer = getShortBuffer();
|
||||
MemoryAudioSource source { buffer, true, false };
|
||||
source.setLooping (true);
|
||||
source.prepareToPlay (blockSize, 44100.0);
|
||||
|
||||
play (source, channelInfo);
|
||||
|
||||
for (int sample = 0; sample < buffer.getNumSamples(); ++sample)
|
||||
expect (bufferToFill.getSample (0, sample + buffer.getNumSamples()) == buffer.getSample (0, sample));
|
||||
|
||||
expect (! isSilent (bufferToFill));
|
||||
}
|
||||
|
||||
beginTest ("A long buffer without looping is played once");
|
||||
{
|
||||
auto buffer = getLongBuffer();
|
||||
MemoryAudioSource source { buffer, true, false };
|
||||
source.setLooping (false);
|
||||
source.prepareToPlay (blockSize, 44100.0);
|
||||
|
||||
play (source, channelInfo);
|
||||
|
||||
auto copy = buffer;
|
||||
copy.setSize (buffer.getNumChannels(), blockSize, true, true, false);
|
||||
|
||||
expect (bufferToFill == copy);
|
||||
|
||||
for (int i = 0; i < 10; ++i)
|
||||
play (source, channelInfo);
|
||||
|
||||
expect (isSilent (bufferToFill));
|
||||
}
|
||||
|
||||
beginTest ("A long buffer with looping is played multiple times");
|
||||
{
|
||||
auto buffer = getLongBuffer();
|
||||
MemoryAudioSource source { buffer, true, false };
|
||||
source.setLooping (true);
|
||||
source.prepareToPlay (blockSize, 44100.0);
|
||||
|
||||
for (int i = 0; i < 100; ++i)
|
||||
{
|
||||
play (source, channelInfo);
|
||||
expect (bufferToFill.getSample (0, 0) == buffer.getSample (0, (i * blockSize) % buffer.getNumSamples()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static AudioBuffer<float> getTestBuffer (int length)
|
||||
{
|
||||
AudioBuffer<float> buffer { 2, length };
|
||||
|
||||
for (int channel = 0; channel < buffer.getNumChannels(); ++channel)
|
||||
for (int sample = 0; sample < buffer.getNumSamples(); ++sample)
|
||||
buffer.setSample (channel, sample, jmap ((float) sample, 0.0f, (float) length, -1.0f, 1.0f));
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
static AudioBuffer<float> getShortBuffer() { return getTestBuffer (5); }
|
||||
static AudioBuffer<float> getLongBuffer() { return getTestBuffer (1000); }
|
||||
|
||||
static void play (MemoryAudioSource& source, AudioSourceChannelInfo& info)
|
||||
{
|
||||
info.clearActiveBufferRegion();
|
||||
source.getNextAudioBlock (info);
|
||||
}
|
||||
|
||||
static bool isSilent (const AudioBuffer<float>& b)
|
||||
{
|
||||
for (int channel = 0; channel < b.getNumChannels(); ++channel)
|
||||
if (b.findMinMax (channel, 0, b.getNumSamples()) != Range<float>{})
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
static MemoryAudioSourceTests memoryAudioSourceTests;
|
||||
|
||||
#endif
|
||||
|
||||
} // namespace juce
|
90
deps/juce/modules/juce_audio_basics/sources/juce_MemoryAudioSource.h
vendored
Normal file
90
deps/juce/modules/juce_audio_basics/sources/juce_MemoryAudioSource.h
vendored
Normal file
@ -0,0 +1,90 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2020 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
The code included in this file is provided under the terms of the ISC license
|
||||
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
|
||||
To use, copy, modify, and/or distribute this software for any purpose with or
|
||||
without fee is hereby granted provided that the above copyright notice and
|
||||
this permission notice appear in all copies.
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
An AudioSource which takes some float audio data as an input.
|
||||
|
||||
@tags{Audio}
|
||||
*/
|
||||
class JUCE_API MemoryAudioSource : public PositionableAudioSource
|
||||
{
|
||||
public:
|
||||
//==============================================================================
|
||||
/** Creates a MemoryAudioSource by providing an audio buffer.
|
||||
|
||||
If copyMemory is true then the buffer will be copied into an internal
|
||||
buffer which will be owned by the MemoryAudioSource. If copyMemory is
|
||||
false, then you must ensure that the lifetime of the audio buffer is
|
||||
at least as long as the MemoryAudioSource.
|
||||
*/
|
||||
MemoryAudioSource (AudioBuffer<float>& audioBuffer, bool copyMemory, bool shouldLoop = false);
|
||||
|
||||
//==============================================================================
|
||||
/** Implementation of the AudioSource method. */
|
||||
void prepareToPlay (int samplesPerBlockExpected, double sampleRate) override;
|
||||
|
||||
/** Implementation of the AudioSource method. */
|
||||
void releaseResources() override;
|
||||
|
||||
/** Implementation of the AudioSource method. */
|
||||
void getNextAudioBlock (const AudioSourceChannelInfo& bufferToFill) override;
|
||||
|
||||
//==============================================================================
|
||||
/** Implementation of the PositionableAudioSource method. */
|
||||
void setNextReadPosition (int64 newPosition) override;
|
||||
|
||||
/** Implementation of the PositionableAudioSource method. */
|
||||
int64 getNextReadPosition() const override;
|
||||
|
||||
/** Implementation of the PositionableAudioSource method. */
|
||||
int64 getTotalLength() const override;
|
||||
|
||||
//==============================================================================
|
||||
/** Implementation of the PositionableAudioSource method. */
|
||||
bool isLooping() const override;
|
||||
|
||||
/** Implementation of the PositionableAudioSource method. */
|
||||
void setLooping (bool shouldLoop) override;
|
||||
|
||||
/** Sets the start position of the looping in samples. */
|
||||
void setLoopRange (int64 loopStart, int64 loopLength) override;
|
||||
|
||||
/** Returns the position where the loop playback starts. */
|
||||
void getLoopRange(int64 & loopStart, int64 & loopLength) const override;
|
||||
|
||||
private:
|
||||
//==============================================================================
|
||||
AudioBuffer<float> buffer;
|
||||
int position = 0;
|
||||
bool isCurrentlyLooping;
|
||||
int64 loopStartPos = 0;
|
||||
int64 loopLen = 0;
|
||||
|
||||
//==============================================================================
|
||||
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MemoryAudioSource)
|
||||
};
|
||||
|
||||
} // namespace juce
|
158
deps/juce/modules/juce_audio_basics/sources/juce_MixerAudioSource.cpp
vendored
Normal file
158
deps/juce/modules/juce_audio_basics/sources/juce_MixerAudioSource.cpp
vendored
Normal file
@ -0,0 +1,158 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2020 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
The code included in this file is provided under the terms of the ISC license
|
||||
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
|
||||
To use, copy, modify, and/or distribute this software for any purpose with or
|
||||
without fee is hereby granted provided that the above copyright notice and
|
||||
this permission notice appear in all copies.
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
MixerAudioSource::MixerAudioSource()
|
||||
: currentSampleRate (0.0), bufferSizeExpected (0)
|
||||
{
|
||||
}
|
||||
|
||||
MixerAudioSource::~MixerAudioSource()
|
||||
{
|
||||
removeAllInputs();
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
void MixerAudioSource::addInputSource (AudioSource* input, const bool deleteWhenRemoved)
|
||||
{
|
||||
if (input != nullptr && ! inputs.contains (input))
|
||||
{
|
||||
double localRate;
|
||||
int localBufferSize;
|
||||
|
||||
{
|
||||
const ScopedLock sl (lock);
|
||||
localRate = currentSampleRate;
|
||||
localBufferSize = bufferSizeExpected;
|
||||
}
|
||||
|
||||
if (localRate > 0.0)
|
||||
input->prepareToPlay (localBufferSize, localRate);
|
||||
|
||||
const ScopedLock sl (lock);
|
||||
|
||||
inputsToDelete.setBit (inputs.size(), deleteWhenRemoved);
|
||||
inputs.add (input);
|
||||
}
|
||||
}
|
||||
|
||||
void MixerAudioSource::removeInputSource (AudioSource* const input)
|
||||
{
|
||||
if (input != nullptr)
|
||||
{
|
||||
std::unique_ptr<AudioSource> toDelete;
|
||||
|
||||
{
|
||||
const ScopedLock sl (lock);
|
||||
const int index = inputs.indexOf (input);
|
||||
|
||||
if (index < 0)
|
||||
return;
|
||||
|
||||
if (inputsToDelete [index])
|
||||
toDelete.reset (input);
|
||||
|
||||
inputsToDelete.shiftBits (-1, index);
|
||||
inputs.remove (index);
|
||||
}
|
||||
|
||||
input->releaseResources();
|
||||
}
|
||||
}
|
||||
|
||||
void MixerAudioSource::removeAllInputs()
|
||||
{
|
||||
OwnedArray<AudioSource> toDelete;
|
||||
|
||||
{
|
||||
const ScopedLock sl (lock);
|
||||
|
||||
for (int i = inputs.size(); --i >= 0;)
|
||||
if (inputsToDelete[i])
|
||||
toDelete.add (inputs.getUnchecked(i));
|
||||
|
||||
inputs.clear();
|
||||
}
|
||||
|
||||
for (int i = toDelete.size(); --i >= 0;)
|
||||
toDelete.getUnchecked(i)->releaseResources();
|
||||
}
|
||||
|
||||
void MixerAudioSource::prepareToPlay (int samplesPerBlockExpected, double sampleRate)
|
||||
{
|
||||
tempBuffer.setSize (2, samplesPerBlockExpected);
|
||||
|
||||
const ScopedLock sl (lock);
|
||||
|
||||
currentSampleRate = sampleRate;
|
||||
bufferSizeExpected = samplesPerBlockExpected;
|
||||
|
||||
for (int i = inputs.size(); --i >= 0;)
|
||||
inputs.getUnchecked(i)->prepareToPlay (samplesPerBlockExpected, sampleRate);
|
||||
}
|
||||
|
||||
void MixerAudioSource::releaseResources()
|
||||
{
|
||||
const ScopedLock sl (lock);
|
||||
|
||||
for (int i = inputs.size(); --i >= 0;)
|
||||
inputs.getUnchecked(i)->releaseResources();
|
||||
|
||||
tempBuffer.setSize (2, 0);
|
||||
|
||||
currentSampleRate = 0;
|
||||
bufferSizeExpected = 0;
|
||||
}
|
||||
|
||||
void MixerAudioSource::getNextAudioBlock (const AudioSourceChannelInfo& info)
|
||||
{
|
||||
const ScopedLock sl (lock);
|
||||
|
||||
if (inputs.size() > 0)
|
||||
{
|
||||
inputs.getUnchecked(0)->getNextAudioBlock (info);
|
||||
|
||||
if (inputs.size() > 1)
|
||||
{
|
||||
tempBuffer.setSize (jmax (1, info.buffer->getNumChannels()),
|
||||
info.buffer->getNumSamples());
|
||||
|
||||
AudioSourceChannelInfo info2 (&tempBuffer, 0, info.numSamples);
|
||||
|
||||
for (int i = 1; i < inputs.size(); ++i)
|
||||
{
|
||||
inputs.getUnchecked(i)->getNextAudioBlock (info2);
|
||||
|
||||
for (int chan = 0; chan < info.buffer->getNumChannels(); ++chan)
|
||||
info.buffer->addFrom (chan, info.startSample, tempBuffer, chan, 0, info.numSamples);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
info.clearActiveBufferRegion();
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace juce
|
99
deps/juce/modules/juce_audio_basics/sources/juce_MixerAudioSource.h
vendored
Normal file
99
deps/juce/modules/juce_audio_basics/sources/juce_MixerAudioSource.h
vendored
Normal file
@ -0,0 +1,99 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2020 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
The code included in this file is provided under the terms of the ISC license
|
||||
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
|
||||
To use, copy, modify, and/or distribute this software for any purpose with or
|
||||
without fee is hereby granted provided that the above copyright notice and
|
||||
this permission notice appear in all copies.
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
An AudioSource that mixes together the output of a set of other AudioSources.
|
||||
|
||||
Input sources can be added and removed while the mixer is running as long as their
|
||||
prepareToPlay() and releaseResources() methods are called before and after adding
|
||||
them to the mixer.
|
||||
|
||||
@tags{Audio}
|
||||
*/
|
||||
class JUCE_API MixerAudioSource : public AudioSource
|
||||
{
|
||||
public:
|
||||
//==============================================================================
|
||||
/** Creates a MixerAudioSource. */
|
||||
MixerAudioSource();
|
||||
|
||||
/** Destructor. */
|
||||
~MixerAudioSource() override;
|
||||
|
||||
//==============================================================================
|
||||
/** Adds an input source to the mixer.
|
||||
|
||||
If the mixer is running you'll need to make sure that the input source
|
||||
is ready to play by calling its prepareToPlay() method before adding it.
|
||||
If the mixer is stopped, then its input sources will be automatically
|
||||
prepared when the mixer's prepareToPlay() method is called.
|
||||
|
||||
@param newInput the source to add to the mixer
|
||||
@param deleteWhenRemoved if true, then this source will be deleted when
|
||||
no longer needed by the mixer.
|
||||
*/
|
||||
void addInputSource (AudioSource* newInput, bool deleteWhenRemoved);
|
||||
|
||||
/** Removes an input source.
|
||||
If the source was added by calling addInputSource() with the deleteWhenRemoved
|
||||
flag set, it will be deleted by this method.
|
||||
*/
|
||||
void removeInputSource (AudioSource* input);
|
||||
|
||||
/** Removes all the input sources.
|
||||
Any sources which were added by calling addInputSource() with the deleteWhenRemoved
|
||||
flag set will be deleted by this method.
|
||||
*/
|
||||
void removeAllInputs();
|
||||
|
||||
//==============================================================================
|
||||
/** Implementation of the AudioSource method.
|
||||
This will call prepareToPlay() on all its input sources.
|
||||
*/
|
||||
void prepareToPlay (int samplesPerBlockExpected, double sampleRate) override;
|
||||
|
||||
/** Implementation of the AudioSource method.
|
||||
This will call releaseResources() on all its input sources.
|
||||
*/
|
||||
void releaseResources() override;
|
||||
|
||||
/** Implementation of the AudioSource method. */
|
||||
void getNextAudioBlock (const AudioSourceChannelInfo&) override;
|
||||
|
||||
|
||||
private:
|
||||
//==============================================================================
|
||||
Array<AudioSource*> inputs;
|
||||
BigInteger inputsToDelete;
|
||||
CriticalSection lock;
|
||||
AudioBuffer<float> tempBuffer;
|
||||
double currentSampleRate;
|
||||
int bufferSizeExpected;
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MixerAudioSource)
|
||||
};
|
||||
|
||||
} // namespace juce
|
83
deps/juce/modules/juce_audio_basics/sources/juce_PositionableAudioSource.h
vendored
Normal file
83
deps/juce/modules/juce_audio_basics/sources/juce_PositionableAudioSource.h
vendored
Normal file
@ -0,0 +1,83 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2020 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
The code included in this file is provided under the terms of the ISC license
|
||||
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
|
||||
To use, copy, modify, and/or distribute this software for any purpose with or
|
||||
without fee is hereby granted provided that the above copyright notice and
|
||||
this permission notice appear in all copies.
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
A type of AudioSource which can be repositioned.
|
||||
|
||||
The basic AudioSource just streams continuously with no idea of a current
|
||||
time or length, so the PositionableAudioSource is used for a finite stream
|
||||
that has a current read position.
|
||||
|
||||
@see AudioSource, AudioTransportSource
|
||||
|
||||
@tags{Audio}
|
||||
*/
|
||||
class JUCE_API PositionableAudioSource : public AudioSource
|
||||
{
|
||||
protected:
|
||||
//==============================================================================
|
||||
/** Creates the PositionableAudioSource. */
|
||||
PositionableAudioSource() = default;
|
||||
|
||||
public:
|
||||
/** Destructor */
|
||||
~PositionableAudioSource() override = default;
|
||||
|
||||
//==============================================================================
|
||||
/** Tells the stream to move to a new position.
|
||||
|
||||
Calling this indicates that the next call to AudioSource::getNextAudioBlock()
|
||||
should return samples from this position.
|
||||
|
||||
Note that this may be called on a different thread to getNextAudioBlock(),
|
||||
so the subclass should make sure it's synchronised.
|
||||
*/
|
||||
virtual void setNextReadPosition (int64 newPosition) = 0;
|
||||
|
||||
/** Returns the position from which the next block will be returned.
|
||||
|
||||
@see setNextReadPosition
|
||||
*/
|
||||
virtual int64 getNextReadPosition() const = 0;
|
||||
|
||||
/** Returns the total length of the stream (in samples). */
|
||||
virtual int64 getTotalLength() const = 0;
|
||||
|
||||
/** Returns true if this source is actually playing in a loop. */
|
||||
virtual bool isLooping() const = 0;
|
||||
|
||||
/** Tells the source whether you'd like it to play in a loop. */
|
||||
virtual void setLooping (bool shouldLoop) { ignoreUnused (shouldLoop); }
|
||||
|
||||
/** Sets the start position of the looping in samples. */
|
||||
virtual void setLoopRange (int64 loopStart, int64 loopLength) { ignoreUnused(loopStart); ignoreUnused(loopLength); }
|
||||
|
||||
/** Returns the position where the loop playback starts. */
|
||||
virtual void getLoopRange(int64 & loopStart, int64 & loopLength) const { };
|
||||
|
||||
};
|
||||
|
||||
} // namespace juce
|
265
deps/juce/modules/juce_audio_basics/sources/juce_ResamplingAudioSource.cpp
vendored
Normal file
265
deps/juce/modules/juce_audio_basics/sources/juce_ResamplingAudioSource.cpp
vendored
Normal file
@ -0,0 +1,265 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2020 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
The code included in this file is provided under the terms of the ISC license
|
||||
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
|
||||
To use, copy, modify, and/or distribute this software for any purpose with or
|
||||
without fee is hereby granted provided that the above copyright notice and
|
||||
this permission notice appear in all copies.
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
ResamplingAudioSource::ResamplingAudioSource (AudioSource* const inputSource,
|
||||
const bool deleteInputWhenDeleted,
|
||||
const int channels)
|
||||
: input (inputSource, deleteInputWhenDeleted),
|
||||
numChannels (channels)
|
||||
{
|
||||
jassert (input != nullptr);
|
||||
zeromem (coefficients, sizeof (coefficients));
|
||||
}
|
||||
|
||||
ResamplingAudioSource::~ResamplingAudioSource() {}
|
||||
|
||||
void ResamplingAudioSource::setResamplingRatio (const double samplesInPerOutputSample)
|
||||
{
|
||||
jassert (samplesInPerOutputSample > 0);
|
||||
|
||||
const SpinLock::ScopedLockType sl (ratioLock);
|
||||
ratio = jmax (0.0, samplesInPerOutputSample);
|
||||
}
|
||||
|
||||
void ResamplingAudioSource::prepareToPlay (int samplesPerBlockExpected, double sampleRate)
|
||||
{
|
||||
const SpinLock::ScopedLockType sl (ratioLock);
|
||||
|
||||
auto scaledBlockSize = roundToInt (samplesPerBlockExpected * ratio);
|
||||
input->prepareToPlay (scaledBlockSize, sampleRate * ratio);
|
||||
|
||||
buffer.setSize (numChannels, scaledBlockSize + 32);
|
||||
|
||||
filterStates.calloc (numChannels);
|
||||
srcBuffers.calloc (numChannels);
|
||||
destBuffers.calloc (numChannels);
|
||||
createLowPass (ratio);
|
||||
|
||||
flushBuffers();
|
||||
}
|
||||
|
||||
void ResamplingAudioSource::flushBuffers()
|
||||
{
|
||||
const ScopedLock sl (callbackLock);
|
||||
|
||||
buffer.clear();
|
||||
bufferPos = 0;
|
||||
sampsInBuffer = 0;
|
||||
subSampleOffset = 0.0;
|
||||
resetFilters();
|
||||
}
|
||||
|
||||
void ResamplingAudioSource::releaseResources()
|
||||
{
|
||||
input->releaseResources();
|
||||
buffer.setSize (numChannels, 0);
|
||||
}
|
||||
|
||||
void ResamplingAudioSource::getNextAudioBlock (const AudioSourceChannelInfo& info)
|
||||
{
|
||||
const ScopedLock sl (callbackLock);
|
||||
|
||||
double localRatio;
|
||||
|
||||
{
|
||||
const SpinLock::ScopedLockType ratioSl (ratioLock);
|
||||
localRatio = ratio;
|
||||
}
|
||||
|
||||
if (lastRatio != localRatio)
|
||||
{
|
||||
createLowPass (localRatio);
|
||||
lastRatio = localRatio;
|
||||
}
|
||||
|
||||
const int sampsNeeded = roundToInt (info.numSamples * localRatio) + 3;
|
||||
|
||||
int bufferSize = buffer.getNumSamples();
|
||||
|
||||
if (bufferSize < sampsNeeded + 8)
|
||||
{
|
||||
bufferPos %= bufferSize;
|
||||
bufferSize = sampsNeeded + 32;
|
||||
buffer.setSize (buffer.getNumChannels(), bufferSize, true, true);
|
||||
}
|
||||
|
||||
bufferPos %= bufferSize;
|
||||
|
||||
int endOfBufferPos = bufferPos + sampsInBuffer;
|
||||
const int channelsToProcess = jmin (numChannels, info.buffer->getNumChannels());
|
||||
|
||||
while (sampsNeeded > sampsInBuffer)
|
||||
{
|
||||
endOfBufferPos %= bufferSize;
|
||||
|
||||
int numToDo = jmin (sampsNeeded - sampsInBuffer,
|
||||
bufferSize - endOfBufferPos);
|
||||
|
||||
AudioSourceChannelInfo readInfo (&buffer, endOfBufferPos, numToDo);
|
||||
input->getNextAudioBlock (readInfo);
|
||||
|
||||
if (localRatio > 1.0001)
|
||||
{
|
||||
// for down-sampling, pre-apply the filter..
|
||||
|
||||
for (int i = channelsToProcess; --i >= 0;)
|
||||
applyFilter (buffer.getWritePointer (i, endOfBufferPos), numToDo, filterStates[i]);
|
||||
}
|
||||
|
||||
sampsInBuffer += numToDo;
|
||||
endOfBufferPos += numToDo;
|
||||
}
|
||||
|
||||
for (int channel = 0; channel < channelsToProcess; ++channel)
|
||||
{
|
||||
destBuffers[channel] = info.buffer->getWritePointer (channel, info.startSample);
|
||||
srcBuffers[channel] = buffer.getReadPointer (channel);
|
||||
}
|
||||
|
||||
int nextPos = (bufferPos + 1) % bufferSize;
|
||||
|
||||
for (int m = info.numSamples; --m >= 0;)
|
||||
{
|
||||
jassert (sampsInBuffer > 0 && nextPos != endOfBufferPos);
|
||||
|
||||
const float alpha = (float) subSampleOffset;
|
||||
|
||||
for (int channel = 0; channel < channelsToProcess; ++channel)
|
||||
*destBuffers[channel]++ = srcBuffers[channel][bufferPos]
|
||||
+ alpha * (srcBuffers[channel][nextPos] - srcBuffers[channel][bufferPos]);
|
||||
|
||||
subSampleOffset += localRatio;
|
||||
|
||||
while (subSampleOffset >= 1.0)
|
||||
{
|
||||
if (++bufferPos >= bufferSize)
|
||||
bufferPos = 0;
|
||||
|
||||
--sampsInBuffer;
|
||||
|
||||
nextPos = (bufferPos + 1) % bufferSize;
|
||||
subSampleOffset -= 1.0;
|
||||
}
|
||||
}
|
||||
|
||||
if (localRatio < 0.9999)
|
||||
{
|
||||
// for up-sampling, apply the filter after transposing..
|
||||
for (int i = channelsToProcess; --i >= 0;)
|
||||
applyFilter (info.buffer->getWritePointer (i, info.startSample), info.numSamples, filterStates[i]);
|
||||
}
|
||||
else if (localRatio <= 1.0001 && info.numSamples > 0)
|
||||
{
|
||||
// if the filter's not currently being applied, keep it stoked with the last couple of samples to avoid discontinuities
|
||||
for (int i = channelsToProcess; --i >= 0;)
|
||||
{
|
||||
const float* const endOfBuffer = info.buffer->getReadPointer (i, info.startSample + info.numSamples - 1);
|
||||
FilterState& fs = filterStates[i];
|
||||
|
||||
if (info.numSamples > 1)
|
||||
{
|
||||
fs.y2 = fs.x2 = *(endOfBuffer - 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
fs.y2 = fs.y1;
|
||||
fs.x2 = fs.x1;
|
||||
}
|
||||
|
||||
fs.y1 = fs.x1 = *endOfBuffer;
|
||||
}
|
||||
}
|
||||
|
||||
jassert (sampsInBuffer >= 0);
|
||||
}
|
||||
|
||||
void ResamplingAudioSource::createLowPass (const double frequencyRatio)
|
||||
{
|
||||
const double proportionalRate = (frequencyRatio > 1.0) ? 0.5 / frequencyRatio
|
||||
: 0.5 * frequencyRatio;
|
||||
|
||||
const double n = 1.0 / std::tan (MathConstants<double>::pi * jmax (0.001, proportionalRate));
|
||||
const double nSquared = n * n;
|
||||
const double c1 = 1.0 / (1.0 + MathConstants<double>::sqrt2 * n + nSquared);
|
||||
|
||||
setFilterCoefficients (c1,
|
||||
c1 * 2.0f,
|
||||
c1,
|
||||
1.0,
|
||||
c1 * 2.0 * (1.0 - nSquared),
|
||||
c1 * (1.0 - MathConstants<double>::sqrt2 * n + nSquared));
|
||||
}
|
||||
|
||||
void ResamplingAudioSource::setFilterCoefficients (double c1, double c2, double c3, double c4, double c5, double c6)
|
||||
{
|
||||
const double a = 1.0 / c4;
|
||||
|
||||
c1 *= a;
|
||||
c2 *= a;
|
||||
c3 *= a;
|
||||
c5 *= a;
|
||||
c6 *= a;
|
||||
|
||||
coefficients[0] = c1;
|
||||
coefficients[1] = c2;
|
||||
coefficients[2] = c3;
|
||||
coefficients[3] = c4;
|
||||
coefficients[4] = c5;
|
||||
coefficients[5] = c6;
|
||||
}
|
||||
|
||||
void ResamplingAudioSource::resetFilters()
|
||||
{
|
||||
if (filterStates != nullptr)
|
||||
filterStates.clear ((size_t) numChannels);
|
||||
}
|
||||
|
||||
void ResamplingAudioSource::applyFilter (float* samples, int num, FilterState& fs)
|
||||
{
|
||||
while (--num >= 0)
|
||||
{
|
||||
const double in = *samples;
|
||||
|
||||
double out = coefficients[0] * in
|
||||
+ coefficients[1] * fs.x1
|
||||
+ coefficients[2] * fs.x2
|
||||
- coefficients[4] * fs.y1
|
||||
- coefficients[5] * fs.y2;
|
||||
|
||||
#if JUCE_INTEL
|
||||
if (! (out < -1.0e-8 || out > 1.0e-8))
|
||||
out = 0;
|
||||
#endif
|
||||
|
||||
fs.x2 = fs.x1;
|
||||
fs.x1 = in;
|
||||
fs.y2 = fs.y1;
|
||||
fs.y1 = out;
|
||||
|
||||
*samples++ = (float) out;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace juce
|
106
deps/juce/modules/juce_audio_basics/sources/juce_ResamplingAudioSource.h
vendored
Normal file
106
deps/juce/modules/juce_audio_basics/sources/juce_ResamplingAudioSource.h
vendored
Normal file
@ -0,0 +1,106 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2020 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
The code included in this file is provided under the terms of the ISC license
|
||||
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
|
||||
To use, copy, modify, and/or distribute this software for any purpose with or
|
||||
without fee is hereby granted provided that the above copyright notice and
|
||||
this permission notice appear in all copies.
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
A type of AudioSource that takes an input source and changes its sample rate.
|
||||
|
||||
@see AudioSource, LagrangeInterpolator, CatmullRomInterpolator
|
||||
|
||||
@tags{Audio}
|
||||
*/
|
||||
class JUCE_API ResamplingAudioSource : public AudioSource
|
||||
{
|
||||
public:
|
||||
//==============================================================================
|
||||
/** Creates a ResamplingAudioSource for a given input source.
|
||||
|
||||
@param inputSource the input source to read from
|
||||
@param deleteInputWhenDeleted if true, the input source will be deleted when
|
||||
this object is deleted
|
||||
@param numChannels the number of channels to process
|
||||
*/
|
||||
ResamplingAudioSource (AudioSource* inputSource,
|
||||
bool deleteInputWhenDeleted,
|
||||
int numChannels = 2);
|
||||
|
||||
/** Destructor. */
|
||||
~ResamplingAudioSource() override;
|
||||
|
||||
/** Changes the resampling ratio.
|
||||
|
||||
(This value can be changed at any time, even while the source is running).
|
||||
|
||||
@param samplesInPerOutputSample if set to 1.0, the input is passed through; higher
|
||||
values will speed it up; lower values will slow it
|
||||
down. The ratio must be greater than 0
|
||||
*/
|
||||
void setResamplingRatio (double samplesInPerOutputSample);
|
||||
|
||||
/** Returns the current resampling ratio.
|
||||
|
||||
This is the value that was set by setResamplingRatio().
|
||||
*/
|
||||
double getResamplingRatio() const noexcept { return ratio; }
|
||||
|
||||
/** Clears any buffers and filters that the resampler is using. */
|
||||
void flushBuffers();
|
||||
|
||||
//==============================================================================
|
||||
void prepareToPlay (int samplesPerBlockExpected, double sampleRate) override;
|
||||
void releaseResources() override;
|
||||
void getNextAudioBlock (const AudioSourceChannelInfo&) override;
|
||||
|
||||
private:
|
||||
//==============================================================================
|
||||
OptionalScopedPointer<AudioSource> input;
|
||||
double ratio = 1.0, lastRatio = 1.0;
|
||||
AudioBuffer<float> buffer;
|
||||
int bufferPos = 0, sampsInBuffer = 0;
|
||||
double subSampleOffset = 0.0;
|
||||
double coefficients[6];
|
||||
SpinLock ratioLock;
|
||||
CriticalSection callbackLock;
|
||||
const int numChannels;
|
||||
HeapBlock<float*> destBuffers;
|
||||
HeapBlock<const float*> srcBuffers;
|
||||
|
||||
void setFilterCoefficients (double c1, double c2, double c3, double c4, double c5, double c6);
|
||||
void createLowPass (double proportionalRate);
|
||||
|
||||
struct FilterState
|
||||
{
|
||||
double x1, x2, y1, y2;
|
||||
};
|
||||
|
||||
HeapBlock<FilterState> filterStates;
|
||||
void resetFilters();
|
||||
|
||||
void applyFilter (float* samples, int num, FilterState& fs);
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ResamplingAudioSource)
|
||||
};
|
||||
|
||||
} // namespace juce
|
83
deps/juce/modules/juce_audio_basics/sources/juce_ReverbAudioSource.cpp
vendored
Normal file
83
deps/juce/modules/juce_audio_basics/sources/juce_ReverbAudioSource.cpp
vendored
Normal file
@ -0,0 +1,83 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2020 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
The code included in this file is provided under the terms of the ISC license
|
||||
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
|
||||
To use, copy, modify, and/or distribute this software for any purpose with or
|
||||
without fee is hereby granted provided that the above copyright notice and
|
||||
this permission notice appear in all copies.
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
ReverbAudioSource::ReverbAudioSource (AudioSource* const inputSource, const bool deleteInputWhenDeleted)
|
||||
: input (inputSource, deleteInputWhenDeleted),
|
||||
bypass (false)
|
||||
{
|
||||
jassert (inputSource != nullptr);
|
||||
}
|
||||
|
||||
ReverbAudioSource::~ReverbAudioSource() {}
|
||||
|
||||
void ReverbAudioSource::prepareToPlay (int samplesPerBlockExpected, double sampleRate)
|
||||
{
|
||||
const ScopedLock sl (lock);
|
||||
input->prepareToPlay (samplesPerBlockExpected, sampleRate);
|
||||
reverb.setSampleRate (sampleRate);
|
||||
}
|
||||
|
||||
void ReverbAudioSource::releaseResources() {}
|
||||
|
||||
void ReverbAudioSource::getNextAudioBlock (const AudioSourceChannelInfo& bufferToFill)
|
||||
{
|
||||
const ScopedLock sl (lock);
|
||||
|
||||
input->getNextAudioBlock (bufferToFill);
|
||||
|
||||
if (! bypass)
|
||||
{
|
||||
float* const firstChannel = bufferToFill.buffer->getWritePointer (0, bufferToFill.startSample);
|
||||
|
||||
if (bufferToFill.buffer->getNumChannels() > 1)
|
||||
{
|
||||
reverb.processStereo (firstChannel,
|
||||
bufferToFill.buffer->getWritePointer (1, bufferToFill.startSample),
|
||||
bufferToFill.numSamples);
|
||||
}
|
||||
else
|
||||
{
|
||||
reverb.processMono (firstChannel, bufferToFill.numSamples);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ReverbAudioSource::setParameters (const Reverb::Parameters& newParams)
|
||||
{
|
||||
const ScopedLock sl (lock);
|
||||
reverb.setParameters (newParams);
|
||||
}
|
||||
|
||||
void ReverbAudioSource::setBypassed (bool b) noexcept
|
||||
{
|
||||
if (bypass != b)
|
||||
{
|
||||
const ScopedLock sl (lock);
|
||||
bypass = b;
|
||||
reverb.reset();
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace juce
|
74
deps/juce/modules/juce_audio_basics/sources/juce_ReverbAudioSource.h
vendored
Normal file
74
deps/juce/modules/juce_audio_basics/sources/juce_ReverbAudioSource.h
vendored
Normal file
@ -0,0 +1,74 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2020 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
The code included in this file is provided under the terms of the ISC license
|
||||
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
|
||||
To use, copy, modify, and/or distribute this software for any purpose with or
|
||||
without fee is hereby granted provided that the above copyright notice and
|
||||
this permission notice appear in all copies.
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
An AudioSource that uses the Reverb class to apply a reverb to another AudioSource.
|
||||
|
||||
@see Reverb
|
||||
|
||||
@tags{Audio}
|
||||
*/
|
||||
class JUCE_API ReverbAudioSource : public AudioSource
|
||||
{
|
||||
public:
|
||||
/** Creates a ReverbAudioSource to process a given input source.
|
||||
|
||||
@param inputSource the input source to read from - this must not be null
|
||||
@param deleteInputWhenDeleted if true, the input source will be deleted when
|
||||
this object is deleted
|
||||
*/
|
||||
ReverbAudioSource (AudioSource* inputSource,
|
||||
bool deleteInputWhenDeleted);
|
||||
|
||||
/** Destructor. */
|
||||
~ReverbAudioSource() override;
|
||||
|
||||
//==============================================================================
|
||||
/** Returns the parameters from the reverb. */
|
||||
const Reverb::Parameters& getParameters() const noexcept { return reverb.getParameters(); }
|
||||
|
||||
/** Changes the reverb's parameters. */
|
||||
void setParameters (const Reverb::Parameters& newParams);
|
||||
|
||||
void setBypassed (bool isBypassed) noexcept;
|
||||
bool isBypassed() const noexcept { return bypass; }
|
||||
|
||||
//==============================================================================
|
||||
void prepareToPlay (int samplesPerBlockExpected, double sampleRate) override;
|
||||
void releaseResources() override;
|
||||
void getNextAudioBlock (const AudioSourceChannelInfo&) override;
|
||||
|
||||
private:
|
||||
//==============================================================================
|
||||
CriticalSection lock;
|
||||
OptionalScopedPointer<AudioSource> input;
|
||||
Reverb reverb;
|
||||
std::atomic<bool> bypass;
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ReverbAudioSource)
|
||||
};
|
||||
|
||||
} // namespace juce
|
78
deps/juce/modules/juce_audio_basics/sources/juce_ToneGeneratorAudioSource.cpp
vendored
Normal file
78
deps/juce/modules/juce_audio_basics/sources/juce_ToneGeneratorAudioSource.cpp
vendored
Normal file
@ -0,0 +1,78 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2020 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
The code included in this file is provided under the terms of the ISC license
|
||||
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
|
||||
To use, copy, modify, and/or distribute this software for any purpose with or
|
||||
without fee is hereby granted provided that the above copyright notice and
|
||||
this permission notice appear in all copies.
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
ToneGeneratorAudioSource::ToneGeneratorAudioSource()
|
||||
: frequency (1000.0),
|
||||
sampleRate (44100.0),
|
||||
currentPhase (0.0),
|
||||
phasePerSample (0.0),
|
||||
amplitude (0.5f)
|
||||
{
|
||||
}
|
||||
|
||||
ToneGeneratorAudioSource::~ToneGeneratorAudioSource()
|
||||
{
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
void ToneGeneratorAudioSource::setAmplitude (const float newAmplitude)
|
||||
{
|
||||
amplitude = newAmplitude;
|
||||
}
|
||||
|
||||
void ToneGeneratorAudioSource::setFrequency (const double newFrequencyHz)
|
||||
{
|
||||
frequency = newFrequencyHz;
|
||||
phasePerSample = 0.0;
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
void ToneGeneratorAudioSource::prepareToPlay (int /*samplesPerBlockExpected*/, double rate)
|
||||
{
|
||||
currentPhase = 0.0;
|
||||
phasePerSample = 0.0;
|
||||
sampleRate = rate;
|
||||
}
|
||||
|
||||
void ToneGeneratorAudioSource::releaseResources()
|
||||
{
|
||||
}
|
||||
|
||||
void ToneGeneratorAudioSource::getNextAudioBlock (const AudioSourceChannelInfo& info)
|
||||
{
|
||||
if (phasePerSample == 0.0)
|
||||
phasePerSample = MathConstants<double>::twoPi / (sampleRate / frequency);
|
||||
|
||||
for (int i = 0; i < info.numSamples; ++i)
|
||||
{
|
||||
const float sample = amplitude * (float) std::sin (currentPhase);
|
||||
currentPhase += phasePerSample;
|
||||
|
||||
for (int j = info.buffer->getNumChannels(); --j >= 0;)
|
||||
info.buffer->setSample (j, info.startSample + i, sample);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace juce
|
71
deps/juce/modules/juce_audio_basics/sources/juce_ToneGeneratorAudioSource.h
vendored
Normal file
71
deps/juce/modules/juce_audio_basics/sources/juce_ToneGeneratorAudioSource.h
vendored
Normal file
@ -0,0 +1,71 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2020 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
The code included in this file is provided under the terms of the ISC license
|
||||
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
|
||||
To use, copy, modify, and/or distribute this software for any purpose with or
|
||||
without fee is hereby granted provided that the above copyright notice and
|
||||
this permission notice appear in all copies.
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
A simple AudioSource that generates a sine wave.
|
||||
|
||||
|
||||
@tags{Audio}
|
||||
*/
|
||||
class JUCE_API ToneGeneratorAudioSource : public AudioSource
|
||||
{
|
||||
public:
|
||||
//==============================================================================
|
||||
/** Creates a ToneGeneratorAudioSource. */
|
||||
ToneGeneratorAudioSource();
|
||||
|
||||
/** Destructor. */
|
||||
~ToneGeneratorAudioSource() override;
|
||||
|
||||
//==============================================================================
|
||||
/** Sets the signal's amplitude. */
|
||||
void setAmplitude (float newAmplitude);
|
||||
|
||||
/** Sets the signal's frequency. */
|
||||
void setFrequency (double newFrequencyHz);
|
||||
|
||||
|
||||
//==============================================================================
|
||||
/** Implementation of the AudioSource method. */
|
||||
void prepareToPlay (int samplesPerBlockExpected, double sampleRate) override;
|
||||
|
||||
/** Implementation of the AudioSource method. */
|
||||
void releaseResources() override;
|
||||
|
||||
/** Implementation of the AudioSource method. */
|
||||
void getNextAudioBlock (const AudioSourceChannelInfo&) override;
|
||||
|
||||
|
||||
private:
|
||||
//==============================================================================
|
||||
double frequency, sampleRate;
|
||||
double currentPhase, phasePerSample;
|
||||
float amplitude;
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ToneGeneratorAudioSource)
|
||||
};
|
||||
|
||||
} // namespace juce
|
Reference in New Issue
Block a user