Skip to content
Snippets Groups Projects
Commit 53ee2286 authored by Septen's avatar Septen
Browse files

CAR processor: added feature to use custom reference and affected channels combinations.

parent 7eaad3ab
Branches
No related tags found
No related merge requests found
......@@ -27,12 +27,11 @@
CAR::CAR()
: GenericProcessor("Common Avg Ref") //, threshold(200.0), state(true)
: GenericProcessor ("Common Avg Ref") //, threshold(200.0), state(true)
{
parameters.add (Parameter ("Gain (%)", 0.0, 100.0, 100.0, 0));
avgBuffer = AudioSampleBuffer (1, 10000); // 1-dimensional buffer to hold the avg
m_avgBuffer = AudioSampleBuffer (1, 10000); // 1-dimensional buffer to hold the avg
}
......@@ -56,35 +55,60 @@ void CAR::setParameter (int parameterIndex, float newValue)
void CAR::process (AudioSampleBuffer& buffer, MidiBuffer& events)
{
const int nChannels = buffer.getNumChannels();
const int numSamples = buffer.getNumSamples();
const int numSamples = buffer.getNumSamples();
const int numReferenceChannels = m_referenceChannels.size();
const int numAffectedChannels = m_affectedChannels.size();
// just use channel 0, since we can't have individual channel settings at the moment
const float gain = -1.0f * float (getParameterVar (0, 0)) / 100.0f;
// There are no sense to do any processing if either number of reference or affected channels is zero.
if (! numReferenceChannels
|| ! numAffectedChannels)
{
return;
}
avgBuffer.clear();
m_avgBuffer.clear();
for (int channel = 0; channel < nChannels; ++channel)
for (int i = 0; i < numReferenceChannels; ++i)
{
avgBuffer.addFrom (0, // destChannel
0, // destStartSample
buffer, // source
channel, // sourceChannel
0, // sourceStartSample
numSamples, // numSamples
1.0f); // gain to apply
m_avgBuffer.addFrom (0, // destChannel
0, // destStartSample
buffer, // source
m_referenceChannels[i], // sourceChannel
0, // sourceStartSample
numSamples, // numSamples
1.0f); // gain to apply
}
avgBuffer.applyGain (1.0f / float (nChannels));
m_avgBuffer.applyGain (1.0f / float (numReferenceChannels));
for (int channel = 0; channel < nChannels; ++channel)
// just use channel 0, since we can't have individual channel settings at the moment
const float gain = -1.0f * float (getParameterVar (0, 0)) / 100.0f;
for (int i = 0; i < numAffectedChannels; ++i)
{
buffer.addFrom (channel, // destChannel
0, // destStartSample
avgBuffer, // source
0, // sourceChannel
0, // sourceStartSample
numSamples, // numSamples
gain); // gain to apply
buffer.addFrom (m_affectedChannels[i], // destChannel
0, // destStartSample
m_avgBuffer, // source
0, // sourceChannel
0, // sourceStartSample
numSamples, // numSamples
gain); // gain to apply
}
}
void CAR::setReferenceChannels (const Array<int> newReferenceChannels)
{
const ScopedLock myScopedLock (objectLock);
m_referenceChannels = Array (newReferenceChannels);
}
void CAR::setAffectedChannels (const Array<int> newAffectedChannels)
{
const ScopedLock myScopedLock (objectLock);
m_affectedChannels = Array (newAffectedChannels);
}
......@@ -77,11 +77,32 @@ public:
other way, the application will crash. */
void setParameter (int parameterIndex, float newValue) override;
Array<int> getReferenceChannels() const { return m_referenceChannels; }
Array<int> getAffectedChannels() const { return m_affectedChannels; }
void setReferenceChannels (const Array<int>& newReferenceChannels);
void setAffectedChannels (const Array<int>& newAffectedChannels);
private:
AudioSampleBuffer avgBuffer;
AudioSampleBuffer m_avgBuffer;
/** We should add this for safety to prevent any app crashes or invalid data processing.
Since we use m_referenceChannels and m_affectedChannels arrays in the process() function,
which works in audioThread, we may stumble upon the situation when we start changing
either reference or affected channels by copying array and in the middle of copying process
we will be interrupted by audioThread. So it most probably will lead to app crash or
processing incorrect channels.
*/
CriticalSection objectLock;
/** Array of channels which will be used to calculate mean signal. */
Array<int> m_referenceChannels;
/** Array of channels that will be affected by adding/substracting of mean signal of reference channels */
Array<int> m_affectedChannels;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CAR);
// ==================================================================
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (CAR);
};
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment