Skip to content

Commit

Permalink
Add wavetable demo
Browse files Browse the repository at this point in the history
  • Loading branch information
FigBug committed Aug 21, 2023
1 parent 79ebb17 commit 44c8c45
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 6 deletions.
59 changes: 57 additions & 2 deletions examples/Demo/Source/MainComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1652,13 +1652,68 @@ struct WavetableDemo : public juce::Component
setName ("Wavetable");

juce::MemoryBlock mb (BinaryData::Analog_PWM_Saw_01_wav, BinaryData::Analog_PWM_Saw_01_wavSize);
juce::AudioSampleBuffer buffer;

int size = gin::getWavetableSize (mb);
if (int size = gin::getWavetableSize (mb); size > 0)
{
auto is = new juce::MemoryInputStream (mb.getData(), mb.getSize(), false);
auto reader = juce::WavAudioFormat().createReaderFor (is, true);

if (reader)
{
buffer.setSize (1, int (reader->lengthInSamples));
reader->read (&buffer, 0, int (reader->lengthInSamples), 0, true, false);

loadWavetables (bllt, buffer, reader->sampleRate, size);

osc = std::make_unique<gin::WTOscillator> ();
osc->setWavetable (bllt);
}
}
}

void paint (juce::Graphics&) override
void paint (juce::Graphics& g) override
{
auto note = gin::getMidiNoteFromHertz (44.1f);

juce::AudioSampleBuffer buf (2, 1024);

auto rc = getLocalBounds().withTrimmedLeft (bllt.size()).withTrimmedBottom (bllt.size());

int step = 4;
for (int i = bllt.size(); i >= 0; i -= step)
{
gin::WTOscillator::Params params;
params.pw = float (i) / bllt.size();

buf.clear();

osc->noteOn (0.0f);
osc->process (note, params, buf);

juce::Path p;
auto d = buf.getReadPointer (0);
for (auto x = 0; x < 1024; x++)
{
auto fx = x / 1024.0f * rc.getWidth() + rc.getX();
auto fy = -d[x] * rc.getHeight() / 2.0f + rc.getCentreY();

if (x == 0)
p.startNewSubPath (fx, fy);
else
p.lineTo (fx, fy);
}

g.setColour (juce::Colours::yellow.withAlpha (0.3f));
g.strokePath (p, juce::PathStrokeType (1.0f));

rc.setX (rc.getX() - step);
rc.setY (rc.getY() + step);
}
}

juce::OwnedArray<gin::BandLimitedLookupTable> bllt;
std::unique_ptr<gin::WTOscillator> osc;
};

//==============================================================================
Expand Down
4 changes: 2 additions & 2 deletions modules/gin_dsp/dsp/gin_bandlimitedlookuptable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ void BandLimitedLookupTable::loadFromBuffer (juce::AudioSampleBuffer& buffer, do
auto func = [&] (float phase)
{
auto data = buffer.getReadPointer (0);
return data[int (phase * float ( sz )) % sz];
return data[int (phase * float (sz)) % sz];
};

tables.add (new juce::dsp::LookupTableTransform<float> (func, 0.0f, 1.0f, (size_t) sz + 1));
Expand All @@ -136,7 +136,7 @@ void BandLimitedLookupTable::loadFromBuffer (juce::AudioSampleBuffer& buffer, do
auto func = [&] (float phase)
{
auto data = buffer.getReadPointer (0);
return data[int (phase * float( sz )) % sz];
return data[int (phase * float(sz)) % sz];
};

tables.add (new juce::dsp::LookupTableTransform<float> (func, 0.0f, 1.0f, (size_t) sz + 1));
Expand Down
4 changes: 2 additions & 2 deletions modules/gin_dsp/dsp/gin_wtoscillators.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ void WTOscillator::noteOn (float p)
void WTOscillator::process (float note, const Params& params, juce::AudioSampleBuffer& buffer)
{
if (bllt.size() == 0) return;
int ti = std::min (bllt.size() - 1, int (float ( bllt.size() ) * params.pw));
int ti = std::min (bllt.size() - 1, int (float (bllt.size()) * params.pw));

float freq = float (std::min (sampleRate / 2.0, 440.0 * std::pow (2.0, (note - 69.0) / 12.0)));
float delta = 1.0f / (float ((1.0f / freq) * sampleRate));
Expand All @@ -43,7 +43,7 @@ void WTOscillator::process (float note, const Params& params, juce::AudioSampleB
void WTOscillator::process (float noteL, float noteR, const Params& params, juce::AudioSampleBuffer& buffer)
{
if (bllt.size() == 0) return;
int ti = std::min (bllt.size() - 1, int (float ( bllt.size() ) * params.pw));
int ti = std::min (bllt.size() - 1, int (float (bllt.size()) * params.pw));

float freqL = float (std::min (sampleRate / 2.0, 440.0 * std::pow (2.0, (noteL - 69.0) / 12.0)));
float freqR = float (std::min (sampleRate / 2.0, 440.0 * std::pow (2.0, (noteR - 69.0) / 12.0)));
Expand Down

0 comments on commit 44c8c45

Please sign in to comment.