Skip to content

Commit

Permalink
Add WavetableComponent. Fix filter.
Browse files Browse the repository at this point in the history
  • Loading branch information
FigBug committed Aug 25, 2023
1 parent 65c6c21 commit 15facfa
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 11 deletions.
102 changes: 102 additions & 0 deletions modules/gin_dsp/components/gin_wavetablecomponent.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@

WavetableComponent::WavetableComponent()
{
}

WavetableComponent::~WavetableComponent()
{
}

//==============================================================================

void WavetableComponent::setParams (WTOscillator::Params params_)
{
if (! almostEqual (params.pw, params_.pw))
{
params = params_;
repaint();
}
}

void WavetableComponent::setWavetables (juce::OwnedArray<BandLimitedLookupTable>* bllt_)
{
bllt = bllt_;
needsUpdate = true;
repaint();
}

void WavetableComponent::paint (juce::Graphics& g)
{
if (needsUpdate && bllt)
{
needsUpdate = false;

auto numTables = std::min (32, bllt->size());
for (auto i = 0; i < numTables; i++)
paths.add (createWavetablePath (float (i) / numTables));
}

g.setColour (findColour (waveColourId, true));
for (auto& p : paths)
g.strokePath (p, juce::PathStrokeType (0.75f));

g.setColour (findColour (activeWaveColourId, true));
g.strokePath (createWavetablePath (params.pw), juce::PathStrokeType (0.75f));
}

juce::Path WavetableComponent::createWavetablePath (float wtPos)
{
constexpr auto samples = 256;

juce::AudioSampleBuffer buf (2, samples);

{
WTOscillator osc;

auto hz = 44100.0f / samples;
auto note = getMidiNoteFromHertz (hz);
auto p = params;

p.pw = wtPos;

osc.setSampleRate (44100.0);
osc.setWavetable (*bllt);
osc.noteOn (0.0f);
osc.process (note, p, buf);
}

juce::Path p;

auto w = float (getWidth());
auto h = float (getHeight());

auto data = buf.getReadPointer (0);

auto xSpread = 0.5f;
auto yScale = -(1.0f / 4.5f);
auto dx = std::min (w, h);

auto xSlope = (dx * 1.5f * (1.0f - xSpread)) / float (samples);
auto ySlope = xSlope / 4.0f;

auto xOffset = (dx * xSpread) * wtPos;
auto yOffset = (dx * xSpread) - xOffset;

xOffset += (w - dx * xSpread - samples * xSlope) / 2.0f;
yOffset += (h - dx * xSpread - samples * ySlope) / 2.0f;

p.startNewSubPath (xOffset, data[0] * yScale * dx + yOffset);

for (auto s = 1; s < samples; s++)
{
p.lineTo (xOffset, data[s] * yScale * dx + yOffset);

xOffset += xSlope;
yOffset += ySlope;
}

return p;
}
//-------------------------------------------------------------------------------------------------


34 changes: 34 additions & 0 deletions modules/gin_dsp/components/gin_wavetablecomponent.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#pragma once

/** Draws a wavetable
*/
class WavetableComponent : public juce::Component
{
public:
WavetableComponent();
~WavetableComponent() override;

enum ColourIds
{
lineColourId = 0x3331e10,
backgroundColourId = 0x3331e11,
waveColourId = 0x3331e12,
activeWaveColourId = 0x3331f13,
};

//==============================================================================
void paint (juce::Graphics& g) override;
void setParams (WTOscillator::Params params);
void setWavetables (juce::OwnedArray<BandLimitedLookupTable>*);

private:
juce::Path createWavetablePath (float pos);

juce::OwnedArray<BandLimitedLookupTable>* bllt = nullptr;
WTOscillator::Params params;
juce::Array<juce::Path> paths;
bool needsUpdate = false;

//==============================================================================
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (WavetableComponent)
};
19 changes: 8 additions & 11 deletions modules/gin_dsp/dsp/gin_filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,15 @@ class Filter
auto o = (float**)buffer.getArrayOfWritePointers();
auto i = (const float**)buffer.getArrayOfReadPointers();

for (int ch = 0; ch < channels; ch++)
switch (slope)
{
switch (slope)
{
case db12:
filters[size_t (ch * 2)]->processBlock (o, i, numSamples);
break;
case db24:
filters[size_t (ch * 2 + 0)]->processBlock (o, i, numSamples);
filters[size_t (ch * 2 + 1)]->processBlock (o, i, numSamples);
break;
}
case db12:
filters[0]->processBlock (o, i, numSamples);
break;
case db24:
filters[0]->processBlock (o, i, numSamples);
filters[1]->processBlock (o, i, numSamples);
break;
}
}

Expand Down
1 change: 1 addition & 0 deletions modules/gin_dsp/gin_dsp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ namespace gin
#include "components/gin_levelmeter.cpp"
#include "components/gin_triggeredscope.cpp"
#include "components/gin_waveformcomponent.cpp"
#include "components/gin_wavetablecomponent.cpp"
#include "components/gin_xyscope.cpp"

}
1 change: 1 addition & 0 deletions modules/gin_dsp/gin_dsp.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ namespace gin
#include "components/gin_levelmeter.h"
#include "components/gin_triggeredscope.h"
#include "components/gin_waveformcomponent.h"
#include "components/gin_wavetablecomponent.h"
#include "components/gin_xyscope.h"

}
3 changes: 3 additions & 0 deletions modules/gin_plugin/lookandfeel/gin_copperlookandfeel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ CopperLookAndFeel::CopperLookAndFeel()
setColour (title2ColourId, juce::Colour (0xff25272B));
setColour (accentColourId, juce::Colour (0xffCC8866));

setColour (WavetableComponent::waveColourId, juce::Colours::green.withAlpha (0.3f));
setColour (WavetableComponent::activeWaveColourId, juce::Colours::yellow);

setColour (juce::MidiKeyboardComponent::whiteNoteColourId, juce::Colours::white.withAlpha (0.8f));
setColour (juce::MidiKeyboardComponent::blackNoteColourId, juce::Colours::black.withAlpha (0.8f));
setColour (juce::MidiKeyboardComponent::keySeparatorLineColourId, findColour (grey90ColourId));
Expand Down

0 comments on commit 15facfa

Please sign in to comment.