Skip to content

Commit

Permalink
update spectrum
Browse files Browse the repository at this point in the history
  • Loading branch information
jerryuhoo committed May 22, 2024
1 parent dea5b2f commit 28f83fd
Show file tree
Hide file tree
Showing 10 changed files with 226 additions and 98 deletions.
4 changes: 4 additions & 0 deletions Fire.jucer
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
<FILE id="Cv5kLm" name="Multiband.h" compile="0" resource="0" file="Source/Panels/SpectrogramPanel/Multiband.h"/>
<FILE id="n4qez7" name="SoloButton.cpp" compile="1" resource="0" file="Source/Panels/SpectrogramPanel/SoloButton.cpp"/>
<FILE id="Pb8jAz" name="SoloButton.h" compile="0" resource="0" file="Source/Panels/SpectrogramPanel/SoloButton.h"/>
<FILE id="CeM277" name="SpectrumBackground.cpp" compile="1" resource="0"
file="Source/Panels/SpectrogramPanel/SpectrumBackground.cpp"/>
<FILE id="tuW7Zm" name="SpectrumBackground.h" compile="0" resource="0"
file="Source/Panels/SpectrogramPanel/SpectrumBackground.h"/>
<FILE id="hDi6B8" name="SpectrumComponent.cpp" compile="1" resource="0"
file="Source/Panels/SpectrogramPanel/SpectrumComponent.cpp"/>
<FILE id="DBAh4o" name="SpectrumComponent.h" compile="0" resource="0"
Expand Down
2 changes: 1 addition & 1 deletion Source/Panels/SpectrogramPanel/Multiband.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ void Multiband::setMasks (juce::Graphics& g, int index, int lineNumLimit, int x,
// set focus mask
if (lineNum > lineNumLimit && focusIndex == index)
{
juce::ColourGradient grad (COLOUR5.withAlpha (0.2f), 0, 0, COLOUR1.withAlpha (0.2f), getLocalBounds().getWidth(), 0, false);
juce::ColourGradient grad(COLOUR5.withAlpha(0.2f), 0, getLocalBounds().getHeight(), COLOUR1.withAlpha(0.0f), 0, getLocalBounds().getHeight() / 10.0f * 9.0f, false);
g.setGradientFill (grad);
g.fillRect (x, y, width, height);
}
Expand Down
62 changes: 62 additions & 0 deletions Source/Panels/SpectrogramPanel/SpectrumBackground.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
==============================================================================
SpectrumBackground.cpp
Created: 22 May 2024 3:13:06pm
Author: 羽翼深蓝Wings
==============================================================================
*/

#include "SpectrumBackground.h"
#include "SpectrumComponent.h"

const int SpectrumBackground::frequenciesForLines[] = { 20, 30, 40, 50, 60, 70, 80, 90, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000, 20000 };
const int SpectrumBackground::numberOfLines = 28;
//==============================================================================
SpectrumBackground::SpectrumBackground() : numberOfBins (1024), mBinWidth (44100 / (float) 2048)
{
}

SpectrumBackground::~SpectrumBackground()
{
}

void SpectrumBackground::paint (juce::Graphics& g)
{
// paint background
g.setColour (COLOUR6);
g.fillAll();

// paint horizontal lines and frequency numbers
g.setColour (juce::Colours::lightgrey.withAlpha (0.2f));
g.drawLine (0, getHeight() / 5, getWidth(), getHeight() / 5, 1);

for (int i = 0; i < numberOfLines; ++i)
{
const double proportion = frequenciesForLines[i] / 20000.0;
int xPos = SpectrumComponent::transformToLog (proportion * 20000) * (getWidth());
g.drawVerticalLine (xPos, getHeight() / 5, getHeight());
if (frequenciesForLines[i] == 10 || frequenciesForLines[i] == 100 || frequenciesForLines[i] == 200)
g.drawFittedText (static_cast<juce::String> (frequenciesForLines[i]), xPos - 30, 0, 60, getHeight() / 5, juce::Justification::centred, 2);
else if (frequenciesForLines[i] == 1000 || frequenciesForLines[i] == 10000 || frequenciesForLines[i] == 2000)
g.drawFittedText (static_cast<juce::String> (frequenciesForLines[i] / 1000) + "k", xPos - 30, 0, 60, getHeight() / 5, juce::Justification::centred, 2);
else if (frequenciesForLines[i] == 20)
g.drawFittedText (static_cast<juce::String> (frequenciesForLines[i]), xPos - 30, 0, 60, getHeight() / 5, juce::Justification::right, 2);
else if (frequenciesForLines[i] == 20000)
g.drawFittedText (static_cast<juce::String> (frequenciesForLines[i] / 1000) + "k", xPos - 30, 0, 60, getHeight() / 5, juce::Justification::left, 2);
}

// paint vertical db numbers
// float fontWidth = 50;
// float fontHeight = getHeight() / 5;
// float centerAlign = fontHeight / 2;
// g.drawFittedText("-20 db", 0, getHeight() / 6 * 2 - centerAlign, fontWidth, fontHeight, juce::Justification::centred, 2);
// g.drawFittedText("-40 db", 0, getHeight() / 6 * 3 - centerAlign, fontWidth, fontHeight, juce::Justification::centred, 2);
// g.drawFittedText("-60 db", 0, getHeight() / 6 * 4 - centerAlign, fontWidth, fontHeight, juce::Justification::centred, 2);
// g.drawFittedText("-80 db", 0, getHeight() / 6 * 5 - centerAlign, fontWidth, fontHeight, juce::Justification::centred, 2);
}

void SpectrumBackground::resized()
{
}
36 changes: 36 additions & 0 deletions Source/Panels/SpectrogramPanel/SpectrumBackground.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
==============================================================================
SpectrumBackground.h
Created: 22 May 2024 3:13:06pm
Author: 羽翼深蓝Wings
==============================================================================
*/

#pragma once

#include <JuceHeader.h>
#include "../../GUI/LookAndFeel.h"

//==============================================================================
/*
*/
class SpectrumBackground : public juce::Component
{
public:
SpectrumBackground();
~SpectrumBackground();

void paint (juce::Graphics& g) override;
void resized() override;

private:
int numberOfBins;

static const int frequenciesForLines[];
static const int numberOfLines;
float mBinWidth;

JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SpectrumBackground)
};
122 changes: 57 additions & 65 deletions Source/Panels/SpectrogramPanel/SpectrumComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,93 +11,69 @@
#include <JuceHeader.h>
#include "SpectrumComponent.h"

const int SpectrumComponent::frequenciesForLines[] = { 20, 30, 40, 50, 60, 70, 80, 90, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000, 20000 };
const int SpectrumComponent::numberOfLines = 28;
//==============================================================================
SpectrumComponent::SpectrumComponent() : numberOfBins (1024), mBinWidth (44100 / (float) 2048)
SpectrumComponent::SpectrumComponent() : mStyle(1), mDrawPeak(true), numberOfBins (1024), mBinWidth (44100 / (float) 2048)
{
}

SpectrumComponent::SpectrumComponent(int style, bool drawPeak) : numberOfBins (1024), mBinWidth (44100 / (float) 2048)
{
mStyle = style;
mDrawPeak = drawPeak;
}

SpectrumComponent::~SpectrumComponent()
{
}

void SpectrumComponent::paint (juce::Graphics& g)
{
// paint background
g.setColour (COLOUR6);
g.fillAll();

// paint horizontal lines and frequency numbers
g.setColour (juce::Colours::lightgrey.withAlpha (0.2f));
g.drawLine (0, getHeight() / 5, getWidth(), getHeight() / 5, 1);

for (int i = 0; i < numberOfLines; ++i)
{
const double proportion = frequenciesForLines[i] / 20000.0;
int xPos = transformToLog (proportion * 20000) * (getWidth());
g.drawVerticalLine (xPos, getHeight() / 5, getHeight());
if (frequenciesForLines[i] == 10 || frequenciesForLines[i] == 100 || frequenciesForLines[i] == 200)
g.drawFittedText (static_cast<juce::String> (frequenciesForLines[i]), xPos - 30, 0, 60, getHeight() / 5, juce::Justification::centred, 2);
else if (frequenciesForLines[i] == 1000 || frequenciesForLines[i] == 10000 || frequenciesForLines[i] == 2000)
g.drawFittedText (static_cast<juce::String> (frequenciesForLines[i] / 1000) + "k", xPos - 30, 0, 60, getHeight() / 5, juce::Justification::centred, 2);
else if (frequenciesForLines[i] == 20)
g.drawFittedText (static_cast<juce::String> (frequenciesForLines[i]), xPos - 30, 0, 60, getHeight() / 5, juce::Justification::right, 2);
else if (frequenciesForLines[i] == 20000)
g.drawFittedText (static_cast<juce::String> (frequenciesForLines[i] / 1000) + "k", xPos - 30, 0, 60, getHeight() / 5, juce::Justification::left, 2);
}

// paint vertical db numbers
// float fontWidth = 50;
// float fontHeight = getHeight() / 5;
// float centerAlign = fontHeight / 2;
// g.drawFittedText("-20 db", 0, getHeight() / 6 * 2 - centerAlign, fontWidth, fontHeight, juce::Justification::centred, 2);
// g.drawFittedText("-40 db", 0, getHeight() / 6 * 3 - centerAlign, fontWidth, fontHeight, juce::Justification::centred, 2);
// g.drawFittedText("-60 db", 0, getHeight() / 6 * 4 - centerAlign, fontWidth, fontHeight, juce::Justification::centred, 2);
// g.drawFittedText("-80 db", 0, getHeight() / 6 * 5 - centerAlign, fontWidth, fontHeight, juce::Justification::centred, 2);

// paint current spectrum
g.setColour (juce::Colours::white);
paintSpectrum();
currentSpectrumImage.multiplyAllAlphas (0.9);
currentSpectrumImage.moveImageSection (0, 10, 0, 0, currentSpectrumImage.getWidth(), currentSpectrumImage.getHeight());
g.drawImageAt (currentSpectrumImage, 0, 0);

// paint peak spectrum
maxSpectrumImage.multiplyAllAlphas (0.5);
g.drawImageAt (maxSpectrumImage, 0, 0);
if (mDrawPeak)
{
// paint peak spectrum
maxSpectrumImage.multiplyAllAlphas (0.5);
g.drawImageAt (maxSpectrumImage, 0, 0);

// paint peak text
float mouseX = getMouseXYRelative().getX();
float mouseY = getMouseXYRelative().getY();
// paint peak text
float mouseX = getMouseXYRelative().getX();
float mouseY = getMouseXYRelative().getY();

if (mouseX > 0 && mouseX < getWidth()
&& mouseY > 0 && mouseY < getHeight())
{
mouseOver = true;
}
else
{
mouseOver = false;
}
if (mouseX > 0 && mouseX < getWidth()
&& mouseY > 0 && mouseY < getHeight())
{
mouseOver = true;
}
else
{
mouseOver = false;
}

if (maxDecibelValue >= -99.9f && mouseOver)
{
float boxWidth = 100.0f;
g.setColour (juce::Colours::lightgrey);
g.drawText (juce::String (maxDecibelValue, 1) + " db", maxDecibelPoint.getX() - boxWidth / 2.0f, maxDecibelPoint.getY() - boxWidth / 4.0f, boxWidth, boxWidth, juce::Justification::centred);
g.drawText (juce::String (static_cast<int> (maxFreq)) + " Hz", maxDecibelPoint.getX() - boxWidth / 2.0f, maxDecibelPoint.getY(), boxWidth, boxWidth, juce::Justification::centred);
}
else
{
maxDecibelValue = -100.0f;
maxFreq = 0.0f;
maxDecibelPoint.setXY (-10.0f, -10.0f);
for (int i = 0; i < 1024; i++)
if (maxDecibelValue >= -99.9f && mouseOver)
{
float boxWidth = 100.0f;
g.setColour (juce::Colours::lightgrey);
g.drawText (juce::String (maxDecibelValue, 1) + " db", maxDecibelPoint.getX() - boxWidth / 2.0f, maxDecibelPoint.getY() - boxWidth / 4.0f, boxWidth, boxWidth, juce::Justification::centred);
g.drawText (juce::String (static_cast<int> (maxFreq)) + " Hz", maxDecibelPoint.getX() - boxWidth / 2.0f, maxDecibelPoint.getY(), boxWidth, boxWidth, juce::Justification::centred);
}
else
{
maxData[i] = 0;
maxDecibelValue = -100.0f;
maxFreq = 0.0f;
maxDecibelPoint.setXY (-10.0f, -10.0f);
for (int i = 0; i < 1024; i++)
{
maxData[i] = 0;
}
}
}

}

void SpectrumComponent::resized()
Expand Down Expand Up @@ -187,8 +163,20 @@ void SpectrumComponent::paintSpectrum()
// roundedMaxPath.closeSubPath();

gCurrent.setColour (COLOUR1);

juce::ColourGradient grad;

juce::ColourGradient grad (juce::Colours::red.withAlpha (0.8f), 0, 0, COLOUR1.withAlpha (0.8f), 0, getLocalBounds().getHeight(), false);
if (mStyle == 1)
{
grad = juce::ColourGradient(juce::Colours::red.withAlpha(specAlpha), 0, 0, COLOUR1.withAlpha(specAlpha), 0, getLocalBounds().getHeight(), false);
}
else
{
grad = juce::ColourGradient(juce::Colours::white.withAlpha(0.2f), 0, 0, juce::Colours::grey.withAlpha(0.2f), 0, getLocalBounds().getHeight(), false);
}

gCurrent.setGradientFill(grad);


gCurrent.setGradientFill (grad);
gCurrent.fillPath (roundedCurrentPath);
Expand All @@ -209,6 +197,10 @@ void SpectrumComponent::prepareToPaintSpectrum (int numBins, float* data, float
mBinWidth = binWidth;
}

void SpectrumComponent::setSpecAlpha (const float alp) {
specAlpha = alp;
}

float SpectrumComponent::transformToLog (double valueToTransform) // freq to x
{
// input: 20-20000
Expand Down
9 changes: 7 additions & 2 deletions Source/Panels/SpectrogramPanel/SpectrumComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,20 @@ class SpectrumComponent : public juce::Component
{
public:
SpectrumComponent();
SpectrumComponent(int style, bool drawPeak);
~SpectrumComponent();

void paint (juce::Graphics& g) override;
void prepareToPaintSpectrum (int numberOfBins, float* spectrumData, float binWidth);
void setSpecAlpha(const float alp);
static float transformToLog (double valueToTransform);
static float transformFromLog (double between0and1);
void resized() override;
void paintSpectrum();

private:
int mStyle;
bool mDrawPeak;
int numberOfBins;
float spectrumData[1024] = { 0 };
float maxData[1024] = { 0 };
Expand All @@ -41,8 +45,9 @@ class SpectrumComponent : public juce::Component
juce::Image currentSpectrumImage = juce::Image (juce::Image::ARGB, 1000, 300, true);
juce::Image maxSpectrumImage = juce::Image (juce::Image::ARGB, 1000, 300, true);

static const int frequenciesForLines[];
static const int numberOfLines;
float mBinWidth;
float specAlpha = 0.8f;

juce::ColourGradient specGrad;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SpectrumComponent)
};
38 changes: 27 additions & 11 deletions Source/PluginEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ FireAudioProcessorEditor::FireAudioProcessorEditor (FireAudioProcessor& p)
addAndMakeVisible (globalPanel);

// Spectrum
addAndMakeVisible(spectrum);
addAndMakeVisible(specBackground);
addAndMakeVisible(processedSpectrum);
addAndMakeVisible(originalSpectrum);
addAndMakeVisible(multiband);
multiband.addMouseListener(this, false);
updateWhenChangingFocus();
Expand All @@ -49,8 +51,9 @@ FireAudioProcessorEditor::FireAudioProcessorEditor (FireAudioProcessor& p)
bandPanel.getWidthButton (i).addListener (this);
}

spectrum.setInterceptsMouseClicks (false, false);
spectrum.prepareToPaintSpectrum (processor.getNumBins(), processor.getFFTData(), processor.getSampleRate() / (float) processor.getFFTSize());
processedSpectrum.setInterceptsMouseClicks (false, false);
processedSpectrum.prepareToPaintSpectrum (processor.getNumBins(), processor.getFFTData(1), processor.getSampleRate() / (float) processor.getFFTSize());
originalSpectrum.prepareToPaintSpectrum (processor.getNumBins(), processor.getFFTData(0), processor.getSampleRate() / (float) processor.getFFTSize());

// presets
addAndMakeVisible (stateComponent);
Expand Down Expand Up @@ -283,14 +286,18 @@ void FireAudioProcessorEditor::resized()
if (zoomButton.getToggleState())
{
juce::Rectangle<int> spectrumArea = area;
spectrum.setBounds (spectrumArea);
specBackground.setBounds (spectrumArea);
processedSpectrum.setBounds (spectrumArea);
originalSpectrum.setBounds (spectrumArea);
multiband.setBounds (spectrumArea);
filterControl.setBounds (spectrumArea);
}
else
{
juce::Rectangle<int> spectrumArea = area.removeFromTop (SPEC_HEIGHT);
spectrum.setBounds (spectrumArea);
specBackground.setBounds (spectrumArea);
processedSpectrum.setBounds (spectrumArea);
originalSpectrum.setBounds (spectrumArea);
multiband.setBounds (spectrumArea);
filterControl.setBounds (spectrumArea);

Expand Down Expand Up @@ -360,15 +367,24 @@ void FireAudioProcessorEditor::timerCallback()
//(1<<11)
// create a temp ddtData because sometimes pushNextSampleIntoFifo will replace the original
// fftData after doingProcess and before painting.
float tempFFTData[2 * 2048] = { 0 };
memmove (tempFFTData, processor.getFFTData(), sizeof (tempFFTData));
// doing process, fifo data to fft data
processor.processFFT (tempFFTData);

float tempFFTDataProcessed[2 * 2048] = { 0 };
memmove(tempFFTDataProcessed, processor.getFFTData(1), sizeof(tempFFTDataProcessed));
processor.processFFT(tempFFTDataProcessed, 1);
float tempFFTDataOriginal[2 * 2048] = { 0 };
memmove(tempFFTDataOriginal, processor.getFFTData(0), sizeof(tempFFTDataOriginal));
processor.processFFT(tempFFTDataOriginal, 0);

// prepare to paint the spectrum
spectrum.prepareToPaintSpectrum (processor.getNumBins(), tempFFTData, processor.getSampleRate() / (float) processor.getFFTSize());
float specAlpha = static_cast<float> (*processor.treeState.getRawParameterValue (MIX_ID));
processedSpectrum.setSpecAlpha(specAlpha);
originalSpectrum.setSpecAlpha(specAlpha);
processedSpectrum.prepareToPaintSpectrum (processor.getNumBins(), tempFFTDataProcessed, processor.getSampleRate() / (float) processor.getFFTSize());
originalSpectrum.prepareToPaintSpectrum (processor.getNumBins(), tempFFTDataOriginal, processor.getSampleRate() / (float) processor.getFFTSize());

graphPanel.repaint();
spectrum.repaint();
processedSpectrum.repaint();
originalSpectrum.repaint();
multiband.repaint();
bandPanel.repaint();
globalPanel.repaint();
Expand Down
Loading

0 comments on commit 28f83fd

Please sign in to comment.