commit c7f19d0905e1b8e4a3a3d2cb5854297e05ddc782
parent 94b748ce7e00f005b0b440d77178b78d73db2cd1
Author: dsp56300 <dsp56300@users.noreply.github.com>
Date: Sun, 16 Jun 2024 23:52:17 +0200
preparations for graph editing
Diffstat:
10 files changed, 117 insertions(+), 53 deletions(-)
diff --git a/source/xtJucePlugin/weGraph.cpp b/source/xtJucePlugin/weGraph.cpp
@@ -6,7 +6,7 @@ namespace xtJucePlugin
{
Graph::Graph(WaveEditor& _editor) : m_editor(_editor), m_data(_editor.getGraphData())
{
- m_onSourceChanged.set(m_data.onSourceChanged, [this](const WaveData& _source)
+ m_onSourceChanged.set(m_data.onSourceChanged, [this](const WaveData&)
{
onSourceChanged();
});
@@ -16,7 +16,7 @@ namespace xtJucePlugin
{
g.fillAll(findColour(juce::TreeView::ColourIds::backgroundColourId));
- paint(g, 0, 0, getWidth(), getHeight());
+ paint(getData(), getDataSize(),g, 0, 0, getWidth(), getHeight());
}
void Graph::parentHierarchyChanged()
@@ -35,26 +35,26 @@ namespace xtJucePlugin
_g.setColour(juce::Colour(0xffffffff));
const float scaleX = static_cast<float>(_width - 1) / static_cast<float>(_size -1);
- const float scaleY = static_cast<float>(_height) / (m_maxValue - m_minValue);
+ const float scaleY = static_cast<float>(_height);
+
+ const float x = static_cast<float>(_x);
+ const float y = static_cast<float>(_y);
const float h = static_cast<float>(_height);
- for(uint32_t x=1; x<_size; ++x)
+ for(uint32_t i=0; i<_size; ++i)
{
- const auto x0 = static_cast<float>(x - 1) * scaleX + static_cast<float>(_x);
- const auto x1 = static_cast<float>(x ) * scaleX + static_cast<float>(_x);
+ const auto x0 = static_cast<float>(i ? (i - 1) : i) * scaleX + x;
+ const auto x1 = static_cast<float>(i ) * scaleX + x;
- const auto y0 = h - (_data[x - 1] - m_minValue) * scaleY + static_cast<float>(_y) - 1;
- const auto y1 = h - (_data[x ] - m_minValue) * scaleY + static_cast<float>(_y) - 1;
+ const auto y0 = h - (normalize(_data[i ? (i - 1) : i])) * scaleY + y - 1;
+ const auto y1 = h - (normalize(_data[i ])) * scaleY + y - 1;
- _g.drawLine(x0, y0, x1, y1, 3.0f);
- }
- }
+ if(i)
+ _g.drawLine(x0, y0, x1, y1, 3.0f);
- void Graph::setRange(const float _min, const float _max)
- {
- m_minValue = _min;
- m_maxValue = _max;
+ _g.fillEllipse(x0 - 5, y0 - 5, 10, 10);
+ }
}
void Graph::onSourceChanged()
diff --git a/source/xtJucePlugin/weGraph.h b/source/xtJucePlugin/weGraph.h
@@ -20,8 +20,6 @@ namespace xtJucePlugin
void paint(juce::Graphics& g) override;
void parentHierarchyChanged() override;
- virtual void paint(juce::Graphics& _g, int _x, int _y, int _width, int _height) = 0;
-
template<size_t Size> void paint(const std::array<float, Size>& _data, juce::Graphics& _g, const int _x, const int _y, const int _width, const int _height) const
{
paint(_data.data(), Size, _g, _x, _y, _width, _height);
@@ -29,7 +27,11 @@ namespace xtJucePlugin
void paint(const float* _data, size_t _size, juce::Graphics& _g, int _x, int _y, int _width, int _height) const;
- void setRange(float _min, float _max);
+ virtual float normalize(float _in) const = 0;
+ virtual float unnormalize(float _in) const = 0;
+
+ virtual const float* getData() const = 0;
+ virtual size_t getDataSize() const = 0;
protected:
GraphData& getGraphData() const { return m_data; }
@@ -39,7 +41,5 @@ namespace xtJucePlugin
WaveEditor& m_editor;
GraphData& m_data;
pluginLib::EventListener<WaveData> m_onSourceChanged;
- float m_minValue = 0.0f;
- float m_maxValue = 1.0f;
};
}
diff --git a/source/xtJucePlugin/weGraphData.cpp b/source/xtJucePlugin/weGraphData.cpp
@@ -1,13 +1,19 @@
#include "weGraphData.h"
-#include "juce_dsp/juce_dsp.h"
-
#include <complex>
namespace xtJucePlugin
{
constexpr float g_pi = 3.1415926535f;
+ constexpr auto g_size = std::tuple_size_v<WaveData>;
+ constexpr uint32_t g_fftOrder = 7;
+ static_assert((1 << g_fftOrder) == g_size);
+
+ GraphData::GraphData() : m_source({}), m_data({}), m_frequencies({}), m_phases({}), m_fft(g_fftOrder)
+ {
+ }
+
void GraphData::set(const WaveData& _data)
{
if(_data == m_source)
@@ -15,12 +21,6 @@ namespace xtJucePlugin
m_source = _data;
- constexpr auto size = std::tuple_size_v<WaveData>;
- constexpr uint32_t order = 7;
- static_assert((1 << order) == size);
-
- const juce::dsp::FFT fft(order);
-
for(uint32_t i=0; i<m_source.size(); ++i)
{
// m_data[i] = i >= (m_source.size()>>1) ? -1.0f : 1.0f;
@@ -28,30 +28,34 @@ namespace xtJucePlugin
m_data[i] = static_cast<float>(m_source[i]) / 128.0f;
}
- std::array<juce::dsp::Complex<float>, size> inData;
- std::array<juce::dsp::Complex<float>, size> outData;
+ updateFrequenciesAndPhases();
+ onSourceChanged(m_source);
+ }
+
+ void GraphData::updateFrequenciesAndPhases()
+ {
for(size_t i=0; i<m_data.size(); ++i)
- inData[i] = {m_data[i], 0.0f};
+ m_fftInData[i] = {m_data[i], 0.0f};
- outData.fill({0.f,0.f});
+ m_fftOutData.fill({0.f,0.f});
- fft.perform(inData.data(), outData.data(), false);
+ m_fft.perform(m_fftInData.data(), m_fftOutData.data(), false);
+
+ const auto scale = 1.0f / static_cast<float>(m_fft.getSize()>>1);
for(size_t i=0; i<m_frequencies.size(); ++i)
{
- const auto re = outData[i].real() / static_cast<float>(fft.getSize()>>1);
- const auto im = outData[i].imag() / static_cast<float>(fft.getSize()>>1);
+ const auto re = m_fftOutData[i].real();
+ const auto im = m_fftOutData[i].imag();
- std::complex c(re,im);
+ std::complex<float> c(re,im);
- m_frequencies[i] = std::abs(c);
+ m_frequencies[i] = std::abs(c) * scale;
// if(m_frequencies[i] > 0.01f)
m_phases[i] = std::arg(c) / g_pi;
// else
// m_phases[i] = 0;
}
-
- onSourceChanged(m_source);
}
}
diff --git a/source/xtJucePlugin/weGraphData.h b/source/xtJucePlugin/weGraphData.h
@@ -5,6 +5,8 @@
#include "../jucePluginLib/event.h"
+#include "juce_dsp/juce_dsp.h"
+
namespace xtJucePlugin
{
class GraphData
@@ -12,9 +14,7 @@ namespace xtJucePlugin
public:
pluginLib::Event<WaveData> onSourceChanged;
- GraphData() : m_source({}), m_data({}), m_frequencies({}), m_phases({})
- {
- }
+ GraphData();
void set(const WaveData& _data);
@@ -23,10 +23,17 @@ namespace xtJucePlugin
const auto& getPhases() const { return m_phases; }
private:
+ void updateFrequenciesAndPhases();
+
WaveData m_source;
std::array<float, std::tuple_size_v<WaveData>> m_data;
std::array<float, std::tuple_size_v<WaveData>/2> m_frequencies;
std::array<float, std::tuple_size_v<WaveData>/2> m_phases;
+
+ std::array<juce::dsp::Complex<float>, std::tuple_size_v<WaveData>> m_fftInData;
+ std::array<juce::dsp::Complex<float>, std::tuple_size_v<WaveData>> m_fftOutData;
+
+ const juce::dsp::FFT m_fft;
};
}
diff --git a/source/xtJucePlugin/weGraphFreq.cpp b/source/xtJucePlugin/weGraphFreq.cpp
@@ -4,8 +4,23 @@
namespace xtJucePlugin
{
- void GraphFreq::paint(juce::Graphics& _g, const int _x, const int _y, const int _width, const int _height)
+ float GraphFreq::normalize(const float _in) const
{
- Graph::paint(getGraphData().getFrequencies(), _g, _x, _y, _width, _height);
+ return _in;
+ }
+
+ float GraphFreq::unnormalize(const float _in) const
+ {
+ return _in;
+ }
+
+ const float* GraphFreq::getData() const
+ {
+ return getGraphData().getFrequencies().data();
+ }
+
+ size_t GraphFreq::getDataSize() const
+ {
+ return getGraphData().getFrequencies().size();
}
}
diff --git a/source/xtJucePlugin/weGraphFreq.h b/source/xtJucePlugin/weGraphFreq.h
@@ -11,7 +11,11 @@ namespace xtJucePlugin
{
}
- void paint(juce::Graphics& _g, int _x, int _y, int _width, int _height) override;
+ float normalize(float _in) const override;
+ float unnormalize(float _in) const override;
+
+ const float* getData() const override;
+ size_t getDataSize() const override;
private:
};
}
diff --git a/source/xtJucePlugin/weGraphPhase.cpp b/source/xtJucePlugin/weGraphPhase.cpp
@@ -6,11 +6,25 @@ namespace xtJucePlugin
{
GraphPhase::GraphPhase(WaveEditor& _editor): Graph(_editor)
{
- setRange(-1,1);
}
- void GraphPhase::paint(juce::Graphics& _g, const int _x, const int _y, const int _width, const int _height)
+ float GraphPhase::normalize(const float _in) const
{
- Graph::paint(getGraphData().getPhases(), _g, _x, _y, _width, _height);
+ return (_in + 1.0f) * 0.5f;
+ }
+
+ float GraphPhase::unnormalize(const float _in) const
+ {
+ return _in * 2.0f - 1.0f;
+ }
+
+ const float* GraphPhase::getData() const
+ {
+ return getGraphData().getPhases().data();
+ }
+
+ size_t GraphPhase::getDataSize() const
+ {
+ return getGraphData().getPhases().size();
}
}
diff --git a/source/xtJucePlugin/weGraphPhase.h b/source/xtJucePlugin/weGraphPhase.h
@@ -9,7 +9,10 @@ namespace xtJucePlugin
public:
explicit GraphPhase(WaveEditor& _editor);
- void paint(juce::Graphics& _g, int _x, int _y, int _width, int _height) override;
+ float normalize(float _in) const override;
+ float unnormalize(float _in) const override;
+ const float* getData() const override;
+ size_t getDataSize() const override;
private:
};
}
diff --git a/source/xtJucePlugin/weGraphTime.cpp b/source/xtJucePlugin/weGraphTime.cpp
@@ -6,11 +6,25 @@ namespace xtJucePlugin
{
GraphTime::GraphTime(WaveEditor& _editor): Graph(_editor)
{
- setRange(-1.0f, 1.0f);
}
- void GraphTime::paint(juce::Graphics& _g, const int _x, const int _y, const int _width, const int _height)
+ float GraphTime::normalize(const float _in) const
{
- Graph::paint(getGraphData().getData(), _g, _x, _y, _width, _height);
+ return (_in + 1.0f) * 0.5f;
+ }
+
+ float GraphTime::unnormalize(const float _in) const
+ {
+ return _in * 2.0f - 1.0f;
+ }
+
+ const float* GraphTime::getData() const
+ {
+ return getGraphData().getData().data();
+ }
+
+ size_t GraphTime::getDataSize() const
+ {
+ return getGraphData().getData().size();
}
}
diff --git a/source/xtJucePlugin/weGraphTime.h b/source/xtJucePlugin/weGraphTime.h
@@ -9,7 +9,10 @@ namespace xtJucePlugin
public:
explicit GraphTime(WaveEditor& _editor);
- void paint(juce::Graphics& _g, int _x, int _y, int _width, int _height) override;
+ float normalize(float _in) const override;
+ float unnormalize(float _in) const override;
+ const float* getData() const override;
+ size_t getDataSize() const override;
private:
};
}