From f0570b1313b3afcd516cfd67b668e926841655a9 Mon Sep 17 00:00:00 2001 From: Christian Ohlsson Date: Sat, 21 Nov 2026 14:02:18 +0100 Subject: [PATCH] =?UTF-8?q?Lade=20till=20visuella=20m=C3=A4tare=20p=C3=A5?= =?UTF-8?q?=20Gate=20och=20Kompressor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Source/PluginEditor.cpp | 188 ++++++++++++++++++++++++++++++++----- Source/PluginEditor.h | 40 +++++++- Source/PluginProcessor.cpp | 102 ++++++++++++++++---- Source/PluginProcessor.h | 13 +++ 4 files changed, 300 insertions(+), 43 deletions(-) diff --git a/Source/PluginEditor.cpp b/Source/PluginEditor.cpp index 3df614f..e0e34cf 100644 --- a/Source/PluginEditor.cpp +++ b/Source/PluginEditor.cpp @@ -1,13 +1,107 @@ #include "PluginEditor.h" +CrilleBassStripAudioProcessorEditor::LevelMeter::LevelMeter(Mode meterMode) + : mode(meterMode) +{ + setInterceptsMouseClicks(false, false); +} + +void CrilleBassStripAudioProcessorEditor::LevelMeter::setLevel(float newLevelDb) +{ + displayedDb = newLevelDb; + repaint(); +} + +void CrilleBassStripAudioProcessorEditor::LevelMeter::setThreshold(float newThresholdDb) +{ + thresholdDb = newThresholdDb; + repaint(); +} + +void CrilleBassStripAudioProcessorEditor::LevelMeter::paint(juce::Graphics& g) +{ + auto bounds = getLocalBounds().toFloat(); + auto titleArea = bounds.removeFromTop(18.0f); + auto valueArea = bounds.removeFromBottom(20.0f); + auto meterArea = bounds.reduced(5.0f, 3.0f); + + g.setColour(juce::Colour(0xffaeb4bc)); + g.setFont(juce::Font(10.0f, juce::Font::bold)); + g.drawText(mode == Mode::inputLevel ? "IN" : "GR", titleArea, juce::Justification::centred); + + g.setColour(juce::Colour(0xff0b0d10)); + g.fillRoundedRectangle(meterArea, 3.0f); + g.setColour(juce::Colour(0xff3c424a)); + g.drawRoundedRectangle(meterArea, 3.0f, 1.0f); + + auto inner = meterArea.reduced(3.0f); + + if (mode == Mode::inputLevel) + { + const float normalized = juce::jmap(juce::jlimit(-80.0f, 0.0f, displayedDb), -80.0f, 0.0f, 0.0f, 1.0f); + const float litHeight = inner.getHeight() * normalized; + auto lit = inner.withTop(inner.getBottom() - litHeight); + + juce::Colour colour = juce::Colour(0xff45c95a); + if (displayedDb > -12.0f) colour = juce::Colour(0xffffc247); + if (displayedDb > -3.0f) colour = juce::Colour(0xffff5c5c); + g.setColour(colour); + g.fillRoundedRectangle(lit, 2.0f); + + const float thresholdNorm = juce::jmap(juce::jlimit(-80.0f, 0.0f, thresholdDb), -80.0f, 0.0f, 0.0f, 1.0f); + const float thresholdY = inner.getBottom() - inner.getHeight() * thresholdNorm; + g.setColour(juce::Colours::white.withAlpha(0.9f)); + g.drawLine(inner.getX() - 2.0f, thresholdY, inner.getRight() + 2.0f, thresholdY, 1.5f); + } + else + { + const float normalized = juce::jlimit(0.0f, 1.0f, displayedDb / 24.0f); + const float litHeight = inner.getHeight() * normalized; + auto lit = inner.withHeight(litHeight); + g.setColour(juce::Colour(0xffd34a43)); + g.fillRoundedRectangle(lit, 2.0f); + + g.setColour(juce::Colour(0xff777f89)); + g.setFont(9.0f); + for (float db : { 6.0f, 12.0f, 18.0f }) + { + const float y = inner.getY() + inner.getHeight() * (db / 24.0f); + g.drawLine(inner.getX(), y, inner.getRight(), y, 1.0f); + } + } + + g.setColour(juce::Colour(0xffd9dde3)); + g.setFont(9.5f); + const auto value = mode == Mode::inputLevel + ? juce::String(displayedDb, 1) + " dB" + : juce::String(displayedDb, 1) + " dB"; + g.drawText(value, valueArea, juce::Justification::centred); +} + CrilleBassStripAudioProcessorEditor::CrilleBassStripAudioProcessorEditor(CrilleBassStripAudioProcessor& p) : AudioProcessorEditor(&p), processor(p) { - addKnob("gateThreshold", "THRESHOLD"); addKnob("gateRelease", "RELEASE"); addKnob("gateGain", "GAIN"); - addKnob("compThreshold", "THRESHOLD"); addKnob("compRatio", "RATIO"); addKnob("compGain", "GAIN"); - addKnob("drive", "DRIVE"); addKnob("mix", "MIX"); addKnob("distGain", "GAIN"); - addKnob("bass", "BASS"); addKnob("lowMid", "LOW MID"); addKnob("middle", "MIDDLE"); addKnob("treble", "TREBLE"); addKnob("output", "OUTPUT"); - setSize(980, 430); + addKnob("gateThreshold", "THRESHOLD"); + addKnob("gateRelease", "RELEASE"); + addKnob("gateGain", "GAIN"); + addKnob("compThreshold", "THRESHOLD"); + addKnob("compRatio", "RATIO"); + addKnob("compGain", "GAIN"); + addKnob("drive", "DRIVE"); + addKnob("mix", "MIX"); + addKnob("distGain", "GAIN"); + addKnob("bass", "BASS"); + addKnob("lowMid", "LOW MID"); + addKnob("middle", "MIDDLE"); + addKnob("treble", "TREBLE"); + addKnob("output", "OUTPUT"); + + addAndMakeVisible(gateInputMeter); + addAndMakeVisible(compressorInputMeter); + addAndMakeVisible(compressorReductionMeter); + + setSize(1080, 430); + startTimerHz(30); } void CrilleBassStripAudioProcessorEditor::addKnob(const juce::String& id, const juce::String& caption) @@ -18,33 +112,85 @@ void CrilleBassStripAudioProcessorEditor::addKnob(const juce::String& id, const k->slider.setColour(juce::Slider::rotarySliderFillColourId, juce::Colour(0xffe5a536)); k->slider.setColour(juce::Slider::rotarySliderOutlineColourId, juce::Colour(0xff30343a)); k->slider.setColour(juce::Slider::thumbColourId, juce::Colour(0xffffd275)); - k->label.setText(caption, juce::dontSendNotification); k->label.setJustificationType(juce::Justification::centred); - k->label.setColour(juce::Label::textColourId, juce::Colour(0xffd9dde3)); k->label.setFont(juce::Font(12.f, juce::Font::bold)); - addAndMakeVisible(k->slider); addAndMakeVisible(k->label); + k->label.setText(caption, juce::dontSendNotification); + k->label.setJustificationType(juce::Justification::centred); + k->label.setColour(juce::Label::textColourId, juce::Colour(0xffd9dde3)); + k->label.setFont(juce::Font(12.f, juce::Font::bold)); + addAndMakeVisible(k->slider); + addAndMakeVisible(k->label); attachments.push_back(std::make_unique(processor.apvts, id, k->slider)); knobs.push_back(std::move(k)); } +float CrilleBassStripAudioProcessorEditor::smoothMeter(float current, float target, float attack, float release) +{ + const float coefficient = target > current ? attack : release; + return current + coefficient * (target - current); +} + +void CrilleBassStripAudioProcessorEditor::timerCallback() +{ + smoothedGateDb = smoothMeter(smoothedGateDb, processor.getGateInputLevelDb(), 0.65f, 0.10f); + smoothedCompressorDb = smoothMeter(smoothedCompressorDb, processor.getCompressorInputLevelDb(), 0.65f, 0.10f); + smoothedReductionDb = smoothMeter(smoothedReductionDb, processor.getCompressorGainReductionDb(), 0.55f, 0.12f); + + gateInputMeter.setLevel(smoothedGateDb); + compressorInputMeter.setLevel(smoothedCompressorDb); + compressorReductionMeter.setLevel(smoothedReductionDb); + + gateInputMeter.setThreshold(processor.apvts.getRawParameterValue("gateThreshold")->load()); + compressorInputMeter.setThreshold(processor.apvts.getRawParameterValue("compThreshold")->load()); +} + void CrilleBassStripAudioProcessorEditor::paint(juce::Graphics& g) { g.fillAll(juce::Colour(0xff111317)); - g.setColour(juce::Colour(0xffe5a536)); g.setFont(juce::Font(28.f, juce::Font::bold)); - g.drawText("CRILLE BASS STRIP", 25, 15, getWidth()-50, 40, juce::Justification::centredLeft); - g.setColour(juce::Colour(0xff777f89)); g.setFont(13.f); - g.drawText("GATE > COMPRESSOR > DISTORTION > EQ", 28, 50, getWidth()-56, 22, juce::Justification::centredLeft); - const juce::StringArray names{"NOISE GATE", "COMPRESSOR", "DISTORTION", "EQUALIZER"}; - const int widths[]{205,205,205,315}; int x=15; - for (int i=0;i<4;++i) { auto r=juce::Rectangle(x,85,widths[i],325); g.setColour(juce::Colour(0xff1b1f24)); g.fillRoundedRectangle(r.toFloat(),10.f); g.setColour(i==2?juce::Colour(0xffb85c45):juce::Colour(0xff3c424a)); g.drawRoundedRectangle(r.toFloat(),10.f,2.f); g.setColour(juce::Colour(0xfff0f1f2)); g.setFont(15.f); g.drawText(names[i],r.removeFromTop(38),juce::Justification::centred); x+=widths[i]+10; } + g.setColour(juce::Colour(0xffe5a536)); + g.setFont(juce::Font(28.f, juce::Font::bold)); + g.drawText("CRILLE BASS STRIP", 25, 15, getWidth() - 50, 40, juce::Justification::centredLeft); + g.setColour(juce::Colour(0xff777f89)); + g.setFont(13.f); + g.drawText("GATE > COMPRESSOR > DISTORTION > EQ", 28, 50, getWidth() - 56, 22, juce::Justification::centredLeft); + + const juce::StringArray names { "NOISE GATE", "COMPRESSOR", "DISTORTION", "EQUALIZER" }; + const int widths[] { 255, 265, 205, 315 }; + int x = 15; + for (int i = 0; i < 4; ++i) + { + auto r = juce::Rectangle(x, 85, widths[i], 325); + g.setColour(juce::Colour(0xff1b1f24)); + g.fillRoundedRectangle(r.toFloat(), 10.f); + g.setColour(i == 2 ? juce::Colour(0xffb85c45) : juce::Colour(0xff3c424a)); + g.drawRoundedRectangle(r.toFloat(), 10.f, 2.f); + g.setColour(juce::Colour(0xfff0f1f2)); + g.setFont(15.f); + g.drawText(names[i], r.removeFromTop(38), juce::Justification::centred); + x += widths[i] + 10; + } } void CrilleBassStripAudioProcessorEditor::resized() { - const int starts[]{20,235,450,665}; const int counts[]{3,3,3,5}; int index=0; - for (int section=0;section<4;++section) { - int cols = section==3 ? 3 : 2; int cellW = section==3 ? 100 : 95; - for(int n=0;nslider.setBounds(x,y,88,102); knobs[index]->label.setBounds(x,y+102,88,22); + const int starts[] { 70, 345, 565, 780 }; + const int counts[] { 3, 3, 3, 5 }; + int index = 0; + + for (int section = 0; section < 4; ++section) + { + const int cols = section == 3 ? 3 : 2; + const int cellW = section == 3 ? 100 : 95; + for (int n = 0; n < counts[section]; ++n, ++index) + { + const int row = n / cols; + const int col = n % cols; + const int x = starts[section] + 8 + col * cellW; + const int y = 130 + row * 135; + knobs[index]->slider.setBounds(x, y, 88, 102); + knobs[index]->label.setBounds(x, y + 102, 88, 22); } } + + gateInputMeter.setBounds(25, 125, 42, 255); + compressorInputMeter.setBounds(290, 125, 42, 255); + compressorReductionMeter.setBounds(500, 125, 42, 255); } diff --git a/Source/PluginEditor.h b/Source/PluginEditor.h index 87d0e55..b1b3cd7 100644 --- a/Source/PluginEditor.h +++ b/Source/PluginEditor.h @@ -2,18 +2,54 @@ #include #include "PluginProcessor.h" -class CrilleBassStripAudioProcessorEditor final : public juce::AudioProcessorEditor +class CrilleBassStripAudioProcessorEditor final : public juce::AudioProcessorEditor, + private juce::Timer { public: explicit CrilleBassStripAudioProcessorEditor(CrilleBassStripAudioProcessor&); ~CrilleBassStripAudioProcessorEditor() override = default; + void paint(juce::Graphics&) override; void resized() override; + private: - struct Knob { juce::Slider slider; juce::Label label; }; + class LevelMeter final : public juce::Component + { + public: + enum class Mode { inputLevel, gainReduction }; + + explicit LevelMeter(Mode meterMode); + void setLevel(float newLevelDb); + void setThreshold(float newThresholdDb); + void paint(juce::Graphics&) override; + + private: + Mode mode; + float displayedDb = -80.0f; + float thresholdDb = -80.0f; + }; + + struct Knob + { + juce::Slider slider; + juce::Label label; + }; + CrilleBassStripAudioProcessor& processor; std::vector> knobs; std::vector> attachments; + + LevelMeter gateInputMeter { LevelMeter::Mode::inputLevel }; + LevelMeter compressorInputMeter { LevelMeter::Mode::inputLevel }; + LevelMeter compressorReductionMeter { LevelMeter::Mode::gainReduction }; + + float smoothedGateDb = -80.0f; + float smoothedCompressorDb = -80.0f; + float smoothedReductionDb = 0.0f; + void addKnob(const juce::String& parameter, const juce::String& caption); + void timerCallback() override; + static float smoothMeter(float current, float target, float attack, float release); + JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CrilleBassStripAudioProcessorEditor) }; diff --git a/Source/PluginProcessor.cpp b/Source/PluginProcessor.cpp index 7199180..4761e07 100644 --- a/Source/PluginProcessor.cpp +++ b/Source/PluginProcessor.cpp @@ -2,15 +2,19 @@ #include "PluginEditor.h" #include -namespace { -constexpr auto pid = [](const char* s) { return juce::ParameterID{s, 1}; }; +namespace +{ +constexpr auto pid = [](const char* s) { return juce::ParameterID { s, 1 }; }; float dbToGain(float db) { return juce::Decibels::decibelsToGain(db); } +constexpr float meterFloorDb = -80.0f; } CrilleBassStripAudioProcessor::CrilleBassStripAudioProcessor() : AudioProcessor(BusesProperties().withInput("Input", juce::AudioChannelSet::stereo(), true) .withOutput("Output", juce::AudioChannelSet::stereo(), true)), - apvts(*this, nullptr, "PARAMETERS", createParameterLayout()) {} + apvts(*this, nullptr, "PARAMETERS", createParameterLayout()) +{ +} juce::AudioProcessorValueTreeState::ParameterLayout CrilleBassStripAudioProcessor::createParameterLayout() { @@ -36,17 +40,37 @@ juce::AudioProcessorValueTreeState::ParameterLayout CrilleBassStripAudioProcesso void CrilleBassStripAudioProcessor::prepareToPlay(double sr, int block) { currentSampleRate = sr; - juce::dsp::ProcessSpec spec{sr, static_cast(block), static_cast(getTotalNumOutputChannels())}; - compressor.prepare(spec); lowShelf.prepare(spec); lowMid.prepare(spec); mid.prepare(spec); highShelf.prepare(spec); - compressor.reset(); lowShelf.reset(); lowMid.reset(); mid.reset(); highShelf.reset(); + juce::dsp::ProcessSpec spec { sr, static_cast(block), static_cast(getTotalNumOutputChannels()) }; + compressor.prepare(spec); + lowShelf.prepare(spec); + lowMid.prepare(spec); + mid.prepare(spec); + highShelf.prepare(spec); + compressor.reset(); + lowShelf.reset(); + lowMid.reset(); + mid.reset(); + highShelf.reset(); gateEnvelope.assign(static_cast(getTotalNumOutputChannels()), 0.f); + gateInputLevelDb.store(meterFloorDb); + compressorInputLevelDb.store(meterFloorDb); + compressorGainReductionDb.store(0.0f); updateFilters(); } bool CrilleBassStripAudioProcessor::isBusesLayoutSupported(const BusesLayout& l) const { const auto out = l.getMainOutputChannelSet(); - return (out == juce::AudioChannelSet::mono() || out == juce::AudioChannelSet::stereo()) && out == l.getMainInputChannelSet(); + return (out == juce::AudioChannelSet::mono() || out == juce::AudioChannelSet::stereo()) + && out == l.getMainInputChannelSet(); +} + +float CrilleBassStripAudioProcessor::getBufferPeak(const juce::AudioBuffer& buffer) noexcept +{ + float peak = 0.0f; + for (int channel = 0; channel < buffer.getNumChannels(); ++channel) + peak = juce::jmax(peak, buffer.getMagnitude(channel, 0, buffer.getNumSamples())); + return peak; } void CrilleBassStripAudioProcessor::updateFilters() @@ -64,52 +88,90 @@ void CrilleBassStripAudioProcessor::updateFilters() void CrilleBassStripAudioProcessor::processBlock(juce::AudioBuffer& buffer, juce::MidiBuffer&) { juce::ScopedNoDenormals noDenormals; - for (int c = getTotalNumInputChannels(); c < getTotalNumOutputChannels(); ++c) buffer.clear(c, 0, buffer.getNumSamples()); + for (int c = getTotalNumInputChannels(); c < getTotalNumOutputChannels(); ++c) + buffer.clear(c, 0, buffer.getNumSamples()); + + const float gateInputPeak = getBufferPeak(buffer); + gateInputLevelDb.store(juce::Decibels::gainToDecibels(gateInputPeak, meterFloorDb), std::memory_order_relaxed); const float threshold = dbToGain(apvts.getRawParameterValue("gateThreshold")->load()); const float releaseMs = apvts.getRawParameterValue("gateRelease")->load(); const float releaseCoeff = std::exp(-1.f / (0.001f * releaseMs * static_cast(currentSampleRate))); const float gateGain = dbToGain(apvts.getRawParameterValue("gateGain")->load()); - for (int c = 0; c < buffer.getNumChannels(); ++c) { - auto* x = buffer.getWritePointer(c); auto& env = gateEnvelope[static_cast(c)]; - for (int i = 0; i < buffer.getNumSamples(); ++i) { + + for (int c = 0; c < buffer.getNumChannels(); ++c) + { + auto* x = buffer.getWritePointer(c); + auto& env = gateEnvelope[static_cast(c)]; + for (int i = 0; i < buffer.getNumSamples(); ++i) + { const float target = std::abs(x[i]) >= threshold ? 1.f : 0.f; env = target > env ? target : releaseCoeff * env; x[i] *= env * gateGain; } } + const float compressorInputPeak = getBufferPeak(buffer); + compressorInputLevelDb.store(juce::Decibels::gainToDecibels(compressorInputPeak, meterFloorDb), std::memory_order_relaxed); + compressor.setThreshold(apvts.getRawParameterValue("compThreshold")->load()); compressor.setRatio(apvts.getRawParameterValue("compRatio")->load()); - compressor.setAttack(15.f); compressor.setRelease(100.f); + compressor.setAttack(15.f); + compressor.setRelease(100.f); + juce::dsp::AudioBlock block(buffer); juce::dsp::ProcessContextReplacing context(block); compressor.process(context); + + const float compressorOutputPeak = getBufferPeak(buffer); + const float gainReduction = compressorInputPeak > 0.0f + ? juce::jmax(0.0f, juce::Decibels::gainToDecibels(compressorInputPeak / juce::jmax(compressorOutputPeak, 1.0e-9f))) + : 0.0f; + compressorGainReductionDb.store(juce::jlimit(0.0f, 24.0f, gainReduction), std::memory_order_relaxed); + buffer.applyGain(dbToGain(apvts.getRawParameterValue("compGain")->load())); const float drive = dbToGain(apvts.getRawParameterValue("drive")->load()); const float mix = apvts.getRawParameterValue("mix")->load() * 0.01f; const float dg = dbToGain(apvts.getRawParameterValue("distGain")->load()); - for (int c = 0; c < buffer.getNumChannels(); ++c) { + for (int c = 0; c < buffer.getNumChannels(); ++c) + { auto* x = buffer.getWritePointer(c); - for (int i = 0; i < buffer.getNumSamples(); ++i) { - const float dry = x[i]; const float wet = std::tanh(dry * drive); + for (int i = 0; i < buffer.getNumSamples(); ++i) + { + const float dry = x[i]; + const float wet = std::tanh(dry * drive); x[i] = ((1.f - mix) * dry + mix * wet) * dg; } } updateFilters(); - lowShelf.process(context); lowMid.process(context); mid.process(context); highShelf.process(context); + lowShelf.process(context); + lowMid.process(context); + mid.process(context); + highShelf.process(context); buffer.applyGain(dbToGain(apvts.getRawParameterValue("output")->load())); } void CrilleBassStripAudioProcessor::getStateInformation(juce::MemoryBlock& dest) { - if (auto xml = apvts.copyState().createXml()) copyXmlToBinary(*xml, dest); + if (auto xml = apvts.copyState().createXml()) + copyXmlToBinary(*xml, dest); } + void CrilleBassStripAudioProcessor::setStateInformation(const void* data, int size) { - if (auto xml = getXmlFromBinary(data, size); xml != nullptr && xml->hasTagName(apvts.state.getType())) apvts.replaceState(juce::ValueTree::fromXml(*xml)); + if (auto xml = getXmlFromBinary(data, size); + xml != nullptr && xml->hasTagName(apvts.state.getType())) + apvts.replaceState(juce::ValueTree::fromXml(*xml)); +} + +juce::AudioProcessorEditor* CrilleBassStripAudioProcessor::createEditor() +{ + return new CrilleBassStripAudioProcessorEditor(*this); +} + +juce::AudioProcessor* JUCE_CALLTYPE createPluginFilter() +{ + return new CrilleBassStripAudioProcessor(); } -juce::AudioProcessorEditor* CrilleBassStripAudioProcessor::createEditor() { return new CrilleBassStripAudioProcessorEditor(*this); } -juce::AudioProcessor* JUCE_CALLTYPE createPluginFilter() { return new CrilleBassStripAudioProcessor(); } diff --git a/Source/PluginProcessor.h b/Source/PluginProcessor.h index 8dbd64c..d6b8649 100644 --- a/Source/PluginProcessor.h +++ b/Source/PluginProcessor.h @@ -1,5 +1,6 @@ #pragma once #include +#include class CrilleBassStripAudioProcessor final : public juce::AudioProcessor { @@ -30,12 +31,24 @@ public: juce::AudioProcessorValueTreeState apvts; static juce::AudioProcessorValueTreeState::ParameterLayout createParameterLayout(); + float getGateInputLevelDb() const noexcept { return gateInputLevelDb.load(); } + float getCompressorInputLevelDb() const noexcept { return compressorInputLevelDb.load(); } + float getCompressorGainReductionDb() const noexcept { return compressorGainReductionDb.load(); } + private: using Filter = juce::dsp::IIR::Filter; + juce::dsp::Compressor compressor; juce::dsp::ProcessorDuplicator> lowShelf, lowMid, mid, highShelf; double currentSampleRate = 44100.0; std::vector gateEnvelope; + + std::atomic gateInputLevelDb { -80.0f }; + std::atomic compressorInputLevelDb { -80.0f }; + std::atomic compressorGainReductionDb { 0.0f }; + void updateFilters(); + static float getBufferPeak(const juce::AudioBuffer& buffer) noexcept; + JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CrilleBassStripAudioProcessor) };