Lade till en knapp för att stämma ner från E till Eb (1 semitone)
This commit is contained in:
1 parent
2a06474177
commit
a26de5091c
10 files changed
+1136
-110
No files matched your search
+276
-5
@@ -21,6 +21,10 @@ juce::AudioProcessorValueTreeState::ParameterLayout CrilleBassStripAudioProcesso
|
||||
{
|
||||
using P = juce::AudioParameterFloat;
|
||||
juce::AudioProcessorValueTreeState::ParameterLayout p;
|
||||
p.add(std::make_unique<juce::AudioParameterBool>(
|
||||
pid("downTuneOn"),
|
||||
"DownTune",
|
||||
false));
|
||||
p.add(std::make_unique<P>(pid("gateThreshold"), "Gate Threshold", juce::NormalisableRange<float>(-80.f, -10.f, 0.1f), -45.f, "dB"));
|
||||
p.add(std::make_unique<P>(pid("gateRelease"), "Gate Release", juce::NormalisableRange<float>(20.f, 500.f, 1.f, 0.5f), 150.f, "ms"));
|
||||
p.add(std::make_unique<P>(pid("gateGain"), "Gate Gain", juce::NormalisableRange<float>(-24.f, 12.f, 0.1f), 0.f, "dB"));
|
||||
@@ -41,6 +45,41 @@ juce::AudioProcessorValueTreeState::ParameterLayout CrilleBassStripAudioProcesso
|
||||
void CrilleBassStripAudioProcessor::prepareToPlay(double sr, int block)
|
||||
{
|
||||
currentSampleRate = sr;
|
||||
pitchBuffer.fill(0.0f);
|
||||
pitchWritePosition = 0;
|
||||
pitchSamplesCollected = 0;
|
||||
pitchAnalysisCounter = 0;
|
||||
detectedFrequencyHz.store(0.0f, std::memory_order_relaxed);
|
||||
|
||||
downTune.configure(1, 576, 144);
|
||||
downTune.setTransposeSemitones(-1.0);
|
||||
|
||||
downTuneBuffer.setSize(1, block, false, false, true);
|
||||
downTuneBuffer.clear();
|
||||
|
||||
setLatencySamples(
|
||||
downTune.inputLatency() +
|
||||
downTune.outputLatency());
|
||||
downTuneLatencyMs =
|
||||
1000.0 *
|
||||
(downTune.inputLatency() + downTune.outputLatency()) /
|
||||
sr;
|
||||
//debug börjar
|
||||
DBG("DownTune input latency: "
|
||||
<< downTune.inputLatency()
|
||||
<< " samples");
|
||||
|
||||
DBG("DownTune output latency: "
|
||||
<< downTune.outputLatency()
|
||||
<< " samples");
|
||||
|
||||
DBG("DownTune total latency: "
|
||||
<< downTune.inputLatency() + downTune.outputLatency()
|
||||
<< " samples");
|
||||
|
||||
DBG("DownTune latency ms: "
|
||||
<< 1000.0 * (downTune.inputLatency() + downTune.outputLatency()) / sr);
|
||||
// debug slutar
|
||||
juce::dsp::ProcessSpec spec { sr, static_cast<juce::uint32>(block), static_cast<juce::uint32>(getTotalNumOutputChannels()) };
|
||||
compressor.prepare(spec);
|
||||
lowShelf.prepare(spec);
|
||||
@@ -75,6 +114,191 @@ float CrilleBassStripAudioProcessor::getBufferPeak(const juce::AudioBuffer<float
|
||||
peak = juce::jmax(peak, buffer.getMagnitude(channel, 0, buffer.getNumSamples()));
|
||||
return peak;
|
||||
}
|
||||
void CrilleBassStripAudioProcessor::updatePitchDetector(
|
||||
const float* input,
|
||||
int numSamples)
|
||||
{
|
||||
for (int i = 0; i < numSamples; ++i)
|
||||
{
|
||||
pitchBuffer[
|
||||
static_cast<std::size_t>(pitchWritePosition)
|
||||
] = input[i];
|
||||
|
||||
pitchWritePosition =
|
||||
(pitchWritePosition + 1) % pitchBufferSize;
|
||||
|
||||
pitchSamplesCollected =
|
||||
juce::jmin(
|
||||
pitchSamplesCollected + 1,
|
||||
pitchBufferSize
|
||||
);
|
||||
}
|
||||
|
||||
pitchAnalysisCounter += numSamples;
|
||||
|
||||
const int analysisInterval =
|
||||
static_cast<int>(currentSampleRate * 0.1);
|
||||
|
||||
if (pitchSamplesCollected == pitchBufferSize
|
||||
&& pitchAnalysisCounter >= analysisInterval)
|
||||
{
|
||||
pitchAnalysisCounter = 0;
|
||||
analysePitch();
|
||||
}
|
||||
}
|
||||
|
||||
void CrilleBassStripAudioProcessor::analysePitch()
|
||||
{
|
||||
constexpr int downsampleFactor = 4;
|
||||
constexpr int analysisSize =
|
||||
pitchBufferSize / downsampleFactor;
|
||||
|
||||
std::array<float, analysisSize> samples {};
|
||||
|
||||
// pitchWritePosition points to the oldest sample.
|
||||
float mean = 0.0f;
|
||||
|
||||
for (int i = 0; i < analysisSize; ++i)
|
||||
{
|
||||
const int sourcePosition =
|
||||
(pitchWritePosition
|
||||
+ i * downsampleFactor)
|
||||
% pitchBufferSize;
|
||||
|
||||
samples[static_cast<std::size_t>(i)] =
|
||||
pitchBuffer[
|
||||
static_cast<std::size_t>(sourcePosition)
|
||||
];
|
||||
|
||||
mean += samples[static_cast<std::size_t>(i)];
|
||||
}
|
||||
|
||||
mean /= static_cast<float>(analysisSize);
|
||||
|
||||
float energy = 0.0f;
|
||||
|
||||
for (int i = 0; i < analysisSize; ++i)
|
||||
{
|
||||
auto& sample =
|
||||
samples[static_cast<std::size_t>(i)];
|
||||
|
||||
sample -= mean;
|
||||
energy += sample * sample;
|
||||
}
|
||||
|
||||
const float rms =
|
||||
std::sqrt(
|
||||
energy / static_cast<float>(analysisSize)
|
||||
);
|
||||
|
||||
// Ignore silence and very weak signals.
|
||||
if (rms < 0.0005f)
|
||||
{
|
||||
detectedFrequencyHz.store(
|
||||
0.0f,
|
||||
std::memory_order_relaxed
|
||||
);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
const float analysisSampleRate =
|
||||
static_cast<float>(currentSampleRate)
|
||||
/ static_cast<float>(downsampleFactor);
|
||||
|
||||
constexpr float minimumFrequencyHz = 30.0f;
|
||||
constexpr float maximumFrequencyHz = 200.0f;
|
||||
|
||||
const int minimumLag =
|
||||
static_cast<int>(
|
||||
analysisSampleRate / maximumFrequencyHz
|
||||
);
|
||||
|
||||
const int maximumLag =
|
||||
juce::jmin(
|
||||
analysisSize - 2,
|
||||
static_cast<int>(
|
||||
analysisSampleRate / minimumFrequencyHz
|
||||
)
|
||||
);
|
||||
|
||||
float bestCorrelation = 0.0f;
|
||||
int bestLag = 0;
|
||||
|
||||
for (int lag = minimumLag;
|
||||
lag <= maximumLag;
|
||||
++lag)
|
||||
{
|
||||
float correlation = 0.0f;
|
||||
float firstEnergy = 0.0f;
|
||||
float secondEnergy = 0.0f;
|
||||
|
||||
const int comparedSamples =
|
||||
analysisSize - lag;
|
||||
|
||||
for (int i = 0;
|
||||
i < comparedSamples;
|
||||
++i)
|
||||
{
|
||||
const float first =
|
||||
samples[static_cast<std::size_t>(i)];
|
||||
|
||||
const float second =
|
||||
samples[
|
||||
static_cast<std::size_t>(i + lag)
|
||||
];
|
||||
|
||||
correlation += first * second;
|
||||
firstEnergy += first * first;
|
||||
secondEnergy += second * second;
|
||||
}
|
||||
|
||||
const float denominator =
|
||||
std::sqrt(firstEnergy * secondEnergy);
|
||||
|
||||
if (denominator > 1.0e-9f)
|
||||
correlation /= denominator;
|
||||
else
|
||||
correlation = 0.0f;
|
||||
|
||||
if (correlation > bestCorrelation)
|
||||
{
|
||||
bestCorrelation = correlation;
|
||||
bestLag = lag;
|
||||
}
|
||||
}
|
||||
|
||||
// Prevent unstable note readings.
|
||||
if (bestLag <= 0 || bestCorrelation < 0.25f)
|
||||
{
|
||||
detectedFrequencyHz.store(
|
||||
0.0f,
|
||||
std::memory_order_relaxed
|
||||
);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
const float frequency =
|
||||
analysisSampleRate
|
||||
/ static_cast<float>(bestLag);
|
||||
|
||||
const float previousFrequency =
|
||||
detectedFrequencyHz.load(
|
||||
std::memory_order_relaxed
|
||||
);
|
||||
|
||||
const float smoothedFrequency =
|
||||
previousFrequency > 0.0f
|
||||
? previousFrequency * 0.7f
|
||||
+ frequency * 0.3f
|
||||
: frequency;
|
||||
|
||||
detectedFrequencyHz.store(
|
||||
smoothedFrequency,
|
||||
std::memory_order_relaxed
|
||||
);
|
||||
}
|
||||
|
||||
void CrilleBassStripAudioProcessor::updateFilters()
|
||||
{
|
||||
@@ -92,14 +316,61 @@ void CrilleBassStripAudioProcessor::processBlock(juce::AudioBuffer<float>& buffe
|
||||
{
|
||||
juce::ScopedNoDenormals noDenormals;
|
||||
|
||||
const int numSamples = buffer.getNumSamples();
|
||||
if (buffer.getNumChannels() > 0)
|
||||
{
|
||||
updatePitchDetector(
|
||||
buffer.getReadPointer(0),
|
||||
numSamples
|
||||
);
|
||||
}
|
||||
|
||||
// Clear output channels that do not contain input data.
|
||||
for (int c = getTotalNumInputChannels(); c < getTotalNumOutputChannels(); ++c)
|
||||
buffer.clear(c, 0, buffer.getNumSamples());
|
||||
for (int c = getTotalNumInputChannels();
|
||||
c < getTotalNumOutputChannels();
|
||||
++c)
|
||||
{
|
||||
buffer.clear(c, 0, numSamples);
|
||||
}
|
||||
|
||||
// Copy mono bass input to both stereo output channels.
|
||||
// Process the mono bass signal through DownTune.
|
||||
if (numSamples <= downTuneBuffer.getNumSamples())
|
||||
{
|
||||
const float* inputChannels[] =
|
||||
{
|
||||
buffer.getReadPointer(0)
|
||||
};
|
||||
|
||||
float* outputChannels[] =
|
||||
{
|
||||
downTuneBuffer.getWritePointer(0)
|
||||
};
|
||||
|
||||
downTune.process(
|
||||
inputChannels,
|
||||
numSamples,
|
||||
outputChannels,
|
||||
numSamples);
|
||||
|
||||
const bool downTuneEnabled =
|
||||
apvts.getRawParameterValue("downTuneOn")->load() > 0.5f;
|
||||
|
||||
if (downTuneEnabled)
|
||||
{
|
||||
buffer.copyFrom(
|
||||
0,
|
||||
0,
|
||||
downTuneBuffer,
|
||||
0,
|
||||
0,
|
||||
numSamples);
|
||||
}
|
||||
}
|
||||
|
||||
// Copy the processed mono bass signal to both stereo output channels.
|
||||
if (buffer.getNumChannels() >= 2)
|
||||
buffer.copyFrom(1, 0, buffer, 0, 0, buffer.getNumSamples());
|
||||
|
||||
buffer.copyFrom(1, 0, buffer, 0, 0, numSamples);
|
||||
|
||||
// Measure input level before the noise gate.
|
||||
const float gateInputPeak = getBufferPeak(buffer);
|
||||
gateInputLevelDb.store(
|
||||
|
||||
Reference in new issue
Block a user