diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..e6da905 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,6 @@ +[submodule "External/signalsmith-stretch"] + path = External/signalsmith-stretch + url = https://github.com/Signalsmith-Audio/signalsmith-stretch.git +[submodule "External/signalsmith-linear"] + path = External/signalsmith-linear + url = https://github.com/Signalsmith-Audio/linear.git diff --git a/CMakeLists.txt b/CMakeLists.txt index 9e15087..b165892 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,6 +17,8 @@ set(VST3_INSTALL_DIR "C:/Program Files/Common Files/VST3") set(RELEASE_DIR "${CMAKE_SOURCE_DIR}/Release") set(ASIO_SDK_DIR "C:/SDK/asio" CACHE PATH "Path to the Steinberg ASIO SDK") +add_subdirectory(External/signalsmith-stretch) + juce_add_plugin(CrilleBassStrip COMPANY_NAME "Crille Audio" BUNDLE_ID "org.crilleaudio.crillebassstrip" @@ -48,6 +50,8 @@ target_include_directories(CrilleBassStrip PRIVATE "${ASIO_SDK_DIR}/common" "${ASIO_SDK_DIR}/host" "${ASIO_SDK_DIR}/host/pc" + "${CMAKE_CURRENT_SOURCE_DIR}/External/signalsmith-stretch" + "${CMAKE_CURRENT_SOURCE_DIR}/External" ) target_link_libraries(CrilleBassStrip PRIVATE @@ -100,6 +104,12 @@ target_include_directories(CrilleBassStripApp PRIVATE "${ASIO_SDK_DIR}/common" "${ASIO_SDK_DIR}/host" "${ASIO_SDK_DIR}/host/pc" + "${CMAKE_CURRENT_SOURCE_DIR}/External/signalsmith-stretch" + "${CMAKE_CURRENT_SOURCE_DIR}/External" +) + +target_include_directories(CrilleBassStrip PRIVATE + "${CMAKE_CURRENT_SOURCE_DIR}/External/signalsmith-stretch" ) target_link_libraries(CrilleBassStripApp PRIVATE diff --git a/External/signalsmith-linear b/External/signalsmith-linear new file mode 160000 index 0000000..7bb1c8f --- /dev/null +++ b/External/signalsmith-linear @@ -0,0 +1 @@ +Subproject commit 7bb1c8f36794d6980080ce817c91f8da8fe56a66 diff --git a/External/signalsmith-stretch b/External/signalsmith-stretch new file mode 160000 index 0000000..88061fd --- /dev/null +++ b/External/signalsmith-stretch @@ -0,0 +1 @@ +Subproject commit 88061fd4eda56a2353c5d65c74aa3f4ccf76f9da diff --git a/Release/Windows/Standalone/Crille Bass Strip.exe b/Release/Windows/Standalone/Crille Bass Strip.exe index 76a5144..511aa52 100644 Binary files a/Release/Windows/Standalone/Crille Bass Strip.exe and b/Release/Windows/Standalone/Crille Bass Strip.exe differ diff --git a/Source/PluginEditor.cpp b/Source/PluginEditor.cpp index e0e34cf..19c21e0 100644 --- a/Source/PluginEditor.cpp +++ b/Source/PluginEditor.cpp @@ -1,24 +1,80 @@ #include "PluginEditor.h" -CrilleBassStripAudioProcessorEditor::LevelMeter::LevelMeter(Mode meterMode) +#include + +namespace +{ +juce::String frequencyToNoteName(float frequency) +{ + if (frequency <= 0.0f) + return "--"; + + const float midiNote = + 69.0f + + 12.0f + * std::log2(frequency / 440.0f); + + const int roundedMidiNote = + static_cast(std::round(midiNote)); + + static const juce::StringArray noteNames + { + "C", + "C#", + "D", + "Eb", + "E", + "F", + "F#", + "G", + "Ab", + "A", + "Bb", + "B" + }; + + const int noteIndex = + ((roundedMidiNote % 12) + 12) % 12; + + return noteNames[noteIndex]; +} + +float transposeFrequency(float frequency, int semitones) +{ + if (frequency <= 0.0f) + return 0.0f; + + return frequency + * std::pow( + 2.0f, + static_cast(semitones) / 12.0f + ); +} +} + +CrilleBassStripAudioProcessorEditor::LevelMeter::LevelMeter( + Mode meterMode) : mode(meterMode) { setInterceptsMouseClicks(false, false); } -void CrilleBassStripAudioProcessorEditor::LevelMeter::setLevel(float newLevelDb) +void CrilleBassStripAudioProcessorEditor::LevelMeter::setLevel( + float newLevelDb) { displayedDb = newLevelDb; repaint(); } -void CrilleBassStripAudioProcessorEditor::LevelMeter::setThreshold(float newThresholdDb) +void CrilleBassStripAudioProcessorEditor::LevelMeter::setThreshold( + float newThresholdDb) { thresholdDb = newThresholdDb; repaint(); } -void CrilleBassStripAudioProcessorEditor::LevelMeter::paint(juce::Graphics& g) +void CrilleBassStripAudioProcessorEditor::LevelMeter::paint( + juce::Graphics& g) { auto bounds = getLocalBounds().toFloat(); auto titleArea = bounds.removeFromTop(18.0f); @@ -27,10 +83,16 @@ void CrilleBassStripAudioProcessorEditor::LevelMeter::paint(juce::Graphics& g) 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.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); @@ -38,159 +100,803 @@ void CrilleBassStripAudioProcessorEditor::LevelMeter::paint(juce::Graphics& g) 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); + 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); - 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); + 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); + 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); + 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); + + const auto value = + juce::String(displayedDb, 1) + " dB"; + + g.drawText( + value, + valueArea, + juce::Justification::centred + ); } -CrilleBassStripAudioProcessorEditor::CrilleBassStripAudioProcessorEditor(CrilleBassStripAudioProcessor& p) - : AudioProcessorEditor(&p), processor(p) +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"); + downTuneButton.setButtonText( + "DOWN 1/2 STEP" + ); + + downTuneButton.setClickingTogglesState(true); + + downTuneButton.setColour( + juce::ToggleButton::textColourId, + juce::Colour(0xffd9dde3) + ); + + downTuneButton.setColour( + juce::ToggleButton::tickColourId, + juce::Colour(0xffe5a536) + ); + + downTuneButton.setColour( + juce::ToggleButton::tickDisabledColourId, + juce::Colour(0xff555b63) + ); + + addAndMakeVisible(downTuneButton); + + downTuneAttachment = + std::make_unique< + juce::AudioProcessorValueTreeState:: + ButtonAttachment + >( + processor.apvts, + "downTuneOn", + downTuneButton + ); + + 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); + setSize(760, 720); startTimerHz(30); } -void CrilleBassStripAudioProcessorEditor::addKnob(const juce::String& id, const juce::String& caption) +void CrilleBassStripAudioProcessorEditor::addKnob( + const juce::String& id, + const juce::String& caption) { auto k = std::make_unique(); - k->slider.setSliderStyle(juce::Slider::RotaryHorizontalVerticalDrag); - k->slider.setTextBoxStyle(juce::Slider::TextBoxBelow, false, 72, 20); - 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)); + + k->slider.setSliderStyle( + juce::Slider::RotaryHorizontalVerticalDrag + ); + + k->slider.setTextBoxStyle( + juce::Slider::TextBoxBelow, + false, + 72, + 20 + ); + + 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.0f, + juce::Font::bold + ) + ); + addAndMakeVisible(k->slider); addAndMakeVisible(k->label); - attachments.push_back(std::make_unique(processor.apvts, id, k->slider)); + + attachments.push_back( + std::make_unique< + juce::AudioProcessorValueTreeState:: + SliderAttachment + >( + processor.apvts, + id, + k->slider + ) + ); + knobs.push_back(std::move(k)); } -float CrilleBassStripAudioProcessorEditor::smoothMeter(float current, float target, float attack, float release) +float CrilleBassStripAudioProcessorEditor::smoothMeter( + float current, + float target, + float attack, + float release) { - const float coefficient = target > current ? attack : release; - return current + coefficient * (target - current); + 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); + smoothedGateDb = + smoothMeter( + smoothedGateDb, + processor.getGateInputLevelDb(), + 0.65f, + 0.10f + ); - gateInputMeter.setLevel(smoothedGateDb); - compressorInputMeter.setLevel(smoothedCompressorDb); - compressorReductionMeter.setLevel(smoothedReductionDb); + smoothedCompressorDb = + smoothMeter( + smoothedCompressorDb, + processor.getCompressorInputLevelDb(), + 0.65f, + 0.10f + ); - gateInputMeter.setThreshold(processor.apvts.getRawParameterValue("gateThreshold")->load()); - compressorInputMeter.setThreshold(processor.apvts.getRawParameterValue("compThreshold")->load()); + 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() + ); + + repaint(); } -void CrilleBassStripAudioProcessorEditor::paint(juce::Graphics& g) +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[] { 255, 265, 205, 315 }; - int x = 15; - for (int i = 0; i < 4; ++i) + g.setColour(juce::Colour(0xffe5a536)); + g.setFont( + juce::Font( + 28.0f, + juce::Font::bold + ) + ); + + g.drawText( + "CRILLE BASS STRIP", + 25, + 15, + getWidth() - 50, + 40, + juce::Justification::centredLeft + ); + + g.setColour(juce::Colour(0xff777f89)); + g.setFont(13.0f); + + g.drawText( + "DOWNTUNE > GATE > COMPRESSOR > DISTORTION > EQ", + 28, + 50, + getWidth() - 56, + 22, + juce::Justification::centredLeft + ); + + const struct { - 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; + juce::Rectangle bounds; + juce::String title; + juce::Colour border; } + panels[] + { + { + { 15, 85, 180, 300 }, + "DOWNTUNE", + juce::Colour(0xff4b79a8) + }, + { + { 205, 85, 265, 300 }, + "NOISE GATE", + juce::Colour(0xff3c424a) + }, + { + { 480, 85, 265, 300 }, + "COMPRESSOR", + juce::Colour(0xff3c424a) + }, + { + { 15, 395, 300, 310 }, + "DISTORTION", + juce::Colour(0xffb85c45) + }, + { + { 325, 395, 420, 310 }, + "EQUALIZER", + juce::Colour(0xff3c424a) + } + }; + + for (const auto& panel : panels) + { + auto bounds = panel.bounds; + + g.setColour(juce::Colour(0xff1b1f24)); + + g.fillRoundedRectangle( + bounds.toFloat(), + 10.0f + ); + + g.setColour(panel.border); + + g.drawRoundedRectangle( + bounds.toFloat(), + 10.0f, + 2.0f + ); + + g.setColour(juce::Colour(0xfff0f1f2)); + g.setFont(15.0f); + + g.drawText( + panel.title, + bounds.removeFromTop(38), + juce::Justification::centred + ); + } + + const float inputFrequency = + processor.getDetectedFrequencyHz(); + + const bool downTuneEnabled = + processor.apvts + .getRawParameterValue("downTuneOn") + ->load() > 0.5f; + + const float outputFrequency = + downTuneEnabled + ? transposeFrequency( + inputFrequency, + -1 + ) + : inputFrequency; + + const auto inputNote = + frequencyToNoteName(inputFrequency); + + const auto outputNote = + frequencyToNoteName(outputFrequency); + + g.setColour(juce::Colour(0xff777f89)); + g.setFont( + juce::Font( + 11.0f, + juce::Font::bold + ) + ); + + g.drawText( + "INPUT", + 30, + 220, + 150, + 18, + juce::Justification::centred + ); + + g.setColour(juce::Colour(0xfff0f1f2)); + g.setFont( + juce::Font( + 28.0f, + juce::Font::bold + ) + ); + + g.drawText( + inputNote, + 30, + 238, + 150, + 36, + juce::Justification::centred + ); + + g.setColour(juce::Colour(0xffaeb4bc)); + g.setFont(11.0f); + + const auto inputFrequencyText = + inputFrequency > 0.0f + ? juce::String(inputFrequency, 1) + + " Hz" + : "-- Hz"; + + g.drawText( + inputFrequencyText, + 30, + 270, + 150, + 20, + juce::Justification::centred + ); + + g.setColour(juce::Colour(0xff777f89)); + g.setFont( + juce::Font( + 11.0f, + juce::Font::bold + ) + ); + + g.drawText( + "OUTPUT", + 30, + 292, + 150, + 18, + juce::Justification::centred + ); + + g.setColour( + downTuneEnabled + ? juce::Colour(0xffffc247) + : juce::Colour(0xfff0f1f2) + ); + + g.setFont( + juce::Font( + 28.0f, + juce::Font::bold + ) + ); + + g.drawText( + outputNote, + 30, + 310, + 150, + 36, + juce::Justification::centred + ); + + g.setColour(juce::Colour(0xffaeb4bc)); + g.setFont(11.0f); + + const auto outputFrequencyText = + outputFrequency > 0.0f + ? juce::String(outputFrequency, 1) + + " Hz" + : "-- Hz"; + + g.drawText( + outputFrequencyText, + 30, + 342, + 150, + 20, + juce::Justification::centred + ); + + const auto latencyText = + "LATENCY: " + + juce::String( + processor.getDownTuneLatencyMs(), + 1 + ) + + " ms"; + + g.setColour(juce::Colour(0xffaeb4bc)); + g.setFont( + juce::Font( + 10.0f, + juce::Font::bold + ) + ); + + g.drawText( + latencyText, + 25, + 362, + 160, + 18, + juce::Justification::centred + ); } void CrilleBassStripAudioProcessorEditor::resized() { - const int starts[] { 70, 345, 565, 780 }; - const int counts[] { 3, 3, 3, 5 }; - int index = 0; + downTuneButton.setBounds( + 35, + 135, + 140, + 50 + ); - for (int section = 0; section < 4; ++section) + gateInputMeter.setBounds( + 215, + 125, + 42, + 225 + ); + + compressorInputMeter.setBounds( + 490, + 125, + 42, + 225 + ); + + compressorReductionMeter.setBounds( + 693, + 125, + 42, + 225 + ); + + // Noise gate + knobs[0]->slider.setBounds( + 265, + 125, + 92, + 100 + ); + + knobs[0]->label.setBounds( + 265, + 225, + 92, + 22 + ); + + knobs[1]->slider.setBounds( + 365, + 125, + 92, + 100 + ); + + knobs[1]->label.setBounds( + 365, + 225, + 92, + 22 + ); + + knobs[2]->slider.setBounds( + 315, + 250, + 92, + 100 + ); + + knobs[2]->label.setBounds( + 315, + 350, + 92, + 22 + ); + + // Compressor + knobs[3]->slider.setBounds( + 540, + 125, + 72, + 100 + ); + + knobs[3]->label.setBounds( + 535, + 225, + 82, + 22 + ); + + knobs[4]->slider.setBounds( + 615, + 125, + 72, + 100 + ); + + knobs[4]->label.setBounds( + 610, + 225, + 82, + 22 + ); + + knobs[5]->slider.setBounds( + 575, + 250, + 82, + 100 + ); + + knobs[5]->label.setBounds( + 575, + 350, + 82, + 22 + ); + + // Distortion + knobs[6]->slider.setBounds( + 35, + 455, + 82, + 100 + ); + + knobs[6]->label.setBounds( + 35, + 555, + 82, + 22 + ); + + knobs[7]->slider.setBounds( + 124, + 455, + 82, + 100 + ); + + knobs[7]->label.setBounds( + 124, + 555, + 82, + 22 + ); + + knobs[8]->slider.setBounds( + 213, + 455, + 82, + 100 + ); + + knobs[8]->label.setBounds( + 213, + 555, + 82, + 22 + ); + + // Equalizer + for (int i = 0; i < 5; ++i) { - 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); - } - } + const int knobIndex = 9 + i; + const int x = 335 + i * 80; - gateInputMeter.setBounds(25, 125, 42, 255); - compressorInputMeter.setBounds(290, 125, 42, 255); - compressorReductionMeter.setBounds(500, 125, 42, 255); -} + knobs[knobIndex]->slider.setBounds( + x, + 455, + 76, + 100 + ); + + knobs[knobIndex]->label.setBounds( + x, + 555, + 76, + 22 + ); + } +} \ No newline at end of file diff --git a/Source/PluginEditor.h b/Source/PluginEditor.h index b1b3cd7..4ec4959 100644 --- a/Source/PluginEditor.h +++ b/Source/PluginEditor.h @@ -38,6 +38,11 @@ private: CrilleBassStripAudioProcessor& processor; std::vector> knobs; std::vector> attachments; +juce::ToggleButton downTuneButton; + +std::unique_ptr< + juce::AudioProcessorValueTreeState::ButtonAttachment +> downTuneAttachment; LevelMeter gateInputMeter { LevelMeter::Mode::inputLevel }; LevelMeter compressorInputMeter { LevelMeter::Mode::inputLevel }; diff --git a/Source/PluginProcessor.cpp b/Source/PluginProcessor.cpp index c2869ab..3cb100f 100644 --- a/Source/PluginProcessor.cpp +++ b/Source/PluginProcessor.cpp @@ -21,6 +21,10 @@ juce::AudioProcessorValueTreeState::ParameterLayout CrilleBassStripAudioProcesso { using P = juce::AudioParameterFloat; juce::AudioProcessorValueTreeState::ParameterLayout p; + p.add(std::make_unique( + pid("downTuneOn"), + "DownTune", + false)); p.add(std::make_unique

(pid("gateThreshold"), "Gate Threshold", juce::NormalisableRange(-80.f, -10.f, 0.1f), -45.f, "dB")); p.add(std::make_unique

(pid("gateRelease"), "Gate Release", juce::NormalisableRange(20.f, 500.f, 1.f, 0.5f), 150.f, "ms")); p.add(std::make_unique

(pid("gateGain"), "Gate Gain", juce::NormalisableRange(-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(block), static_cast(getTotalNumOutputChannels()) }; compressor.prepare(spec); lowShelf.prepare(spec); @@ -75,6 +114,191 @@ float CrilleBassStripAudioProcessor::getBufferPeak(const juce::AudioBuffer(pitchWritePosition) + ] = input[i]; + + pitchWritePosition = + (pitchWritePosition + 1) % pitchBufferSize; + + pitchSamplesCollected = + juce::jmin( + pitchSamplesCollected + 1, + pitchBufferSize + ); + } + + pitchAnalysisCounter += numSamples; + + const int analysisInterval = + static_cast(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 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(i)] = + pitchBuffer[ + static_cast(sourcePosition) + ]; + + mean += samples[static_cast(i)]; + } + + mean /= static_cast(analysisSize); + + float energy = 0.0f; + + for (int i = 0; i < analysisSize; ++i) + { + auto& sample = + samples[static_cast(i)]; + + sample -= mean; + energy += sample * sample; + } + + const float rms = + std::sqrt( + energy / static_cast(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(currentSampleRate) + / static_cast(downsampleFactor); + + constexpr float minimumFrequencyHz = 30.0f; + constexpr float maximumFrequencyHz = 200.0f; + + const int minimumLag = + static_cast( + analysisSampleRate / maximumFrequencyHz + ); + + const int maximumLag = + juce::jmin( + analysisSize - 2, + static_cast( + 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(i)]; + + const float second = + samples[ + static_cast(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(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& 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( diff --git a/Source/PluginProcessor.h b/Source/PluginProcessor.h index ec49e8e..edd7f22 100644 --- a/Source/PluginProcessor.h +++ b/Source/PluginProcessor.h @@ -1,6 +1,9 @@ #pragma once + #include #include +#include +#include class CrilleBassStripAudioProcessor final : public juce::AudioProcessor { @@ -34,13 +37,36 @@ public: float getGateInputLevelDb() const noexcept { return gateInputLevelDb.load(); } float getCompressorInputLevelDb() const noexcept { return compressorInputLevelDb.load(); } float getCompressorGainReductionDb() const noexcept { return compressorGainReductionDb.load(); } +double getDownTuneLatencyMs() const noexcept { return downTuneLatencyMs; } +float getDetectedFrequencyHz() const noexcept { return detectedFrequencyHz.load(std::memory_order_relaxed);} private: using Filter = juce::dsp::IIR::Filter; + // DownTune + signalsmith::stretch::SignalsmithStretch downTune; + juce::AudioBuffer downTuneBuffer; + // Pitch detection + static constexpr int pitchBufferSize = 4096; + + std::array pitchBuffer {}; + int pitchWritePosition = 0; + int pitchSamplesCollected = 0; + int pitchAnalysisCounter = 0; + + std::atomic detectedFrequencyHz { 0.0f }; + + void updatePitchDetector( + const float* input, + int numSamples + ); + + void analysePitch(); + juce::dsp::Compressor compressor; juce::dsp::ProcessorDuplicator> lowShelf, lowMid, mid, highShelf; double currentSampleRate = 44100.0; + double downTuneLatencyMs = 0.0; std::vector gateEnvelope; std::atomic gateInputLevelDb { -80.0f }; @@ -51,4 +77,4 @@ private: static float getBufferPeak(const juce::AudioBuffer& buffer) noexcept; JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CrilleBassStripAudioProcessor) -}; +}; \ No newline at end of file diff --git a/Source/StandaloneMain.cpp b/Source/StandaloneMain.cpp index d9ba16c..99d4046 100644 --- a/Source/StandaloneMain.cpp +++ b/Source/StandaloneMain.cpp @@ -129,7 +129,7 @@ StandaloneMainComponent::StandaloneMainComponent() editor = std::make_unique(audioEngine.getProcessor()); addAndMakeVisible(*editor); - setSize(1080, 480); + setSize(760, 790); } StandaloneMainComponent::~StandaloneMainComponent()