Upload files to "Source"

This commit is contained in:
ed
2025-10-22 16:58:43 +00:00
parent 9a452b7c1b
commit 989632d1e0
3 changed files with 778 additions and 371 deletions

View File

@@ -1,247 +1,417 @@
#include "SynthVoice.h" #include "SynthVoice.h"
#include <cmath>
#include <cmath> std::shared_ptr<WT::Bank> NeuralSynthVoice::wtBank;
//============================================================================== //==============================================================================
NeuralSynthVoice::NeuralSynthVoice(NeuralSharedParams& sp) : shared(sp) {}
NeuralSynthVoice::NeuralSynthVoice (NeuralSharedParams& sp)
//============================================================================== : shared (sp) {}
void NeuralSynthVoice::prepare(const juce::dsp::ProcessSpec& spec)
{ //==============================================================================
setWaveform(0);
tempBlock = juce::dsp::AudioBlock<float>(heapBlock, spec.numChannels, spec.maximumBlockSize); void NeuralSynthVoice::prepare (const juce::dsp::ProcessSpec& newSpec)
processorChain.prepare(spec); {
adsr.setSampleRate(spec.sampleRate); spec = newSpec;
this->spec = spec; // --- Oscillator
} osc.prepare (spec.sampleRate);
setWaveform (0); // default to sine
//============================================================================== // --- Wavetable bank (build once), then prepare osc ---
void NeuralSynthVoice::renderNextBlock(juce::AudioBuffer<float>& outputBuffer, int startSample, int numSamples) if (!wtBank)
{ {
if (numSamples <= 0) return; wtBank = std::make_shared<WT::Bank>(2048, 16, 6); // N=2048, frames=16, levels=6
wtBank->generateDefaultMorph(); // Sine -> Saw -> Square -> Triangle
if (!adsr.isActive()) wtBank->buildMipmaps();
clearCurrentNote(); }
wtOsc.prepare(spec.sampleRate);
if (waveform != -1) { wtOsc.setBank(wtBank);
setWaveform(waveform);
waveform = -1; // --- Scratch buffer (IMPORTANT: allocate real memory)
} tempBuffer.setSize ((int) spec.numChannels, (int) spec.maximumBlockSize,
false, false, true);
const int numChannels = outputBuffer.getNumChannels(); tempBlock = juce::dsp::AudioBlock<float> (tempBuffer);
auto block = tempBlock.getSubBlock(0, (size_t)numSamples); // --- Prepare chain elements
block.clear(); chain.prepare (spec);
// ===================================================================== // Set maximum delay sizes BEFORE runtime changes
// Oscillator {
// ===================================================================== // Flanger: up to 20 ms
auto& osc = processorChain.get<oscIndex>(); auto& flanger = chain.get<flangerIndex>();
juce::dsp::ProcessContextReplacing<float> oscContext(block); const size_t maxFlangerDelay = (size_t) juce::jmax<size_t>(
osc.process(oscContext); 1, (size_t) std::ceil (0.020 * spec.sampleRate));
flanger.setMaximumDelayInSamples (maxFlangerDelay);
// ===================================================================== flanger.reset();
// Distortion }
// ===================================================================== {
const float driveDb = shared.distortionDrive->load(); // 0..30 // Simple delay: up to 2 s
//const float distMix = juce::jlimit(0.0f, 1.0f, shared.distortionMix->load()); auto& delay = chain.get<delayIndex>();
const float bias = juce::jlimit(-1.0f, 1.0f, shared.distortionBias->load()); const size_t maxDelay = (size_t) juce::jmax<size_t>(
const float toneHz = juce::jlimit(100.0f, 8000.0f, shared.distortionTone->load()); 1, (size_t) std::ceil (2.0 * spec.sampleRate));
const int shape = (int)std::lround(juce::jlimit(0.0f, 2.0f, shared.distortionShape->load())); delay.setMaximumDelayInSamples (maxDelay);
delay.reset();
auto& distDry = processorChain.get<distortionPreGain>(); }
auto& distWaveshaper = processorChain.template get<distortionIndex>(); // Envelopes
adsr.setSampleRate (spec.sampleRate);
if (shape == 0) { filterAdsr.setSampleRate (spec.sampleRate);
distWaveshaper.functionToUse = [bias](float x) noexcept {
return std::tanh(x + bias); // Filter
}; svf.reset();
} svf.prepare (spec);
else if (shape == 1) {
distWaveshaper.functionToUse = [bias](float x) noexcept { // Initial filter type
const float v = x + bias; const int type = (int) std::lround (juce::jlimit (0.0f, 2.0f,
return juce::jlimit(-1.0f, 1.0f, v); shared.filterType ? shared.filterType->load() : 0.0f));
}; switch (type)
} {
else if (shape == 2) { case 0: svf.setType (juce::dsp::StateVariableTPTFilterType::lowpass); break;
distWaveshaper.functionToUse = [bias](float x) noexcept { case 1: svf.setType (juce::dsp::StateVariableTPTFilterType::highpass); break;
const float v = x + bias; case 2: svf.setType (juce::dsp::StateVariableTPTFilterType::bandpass); break;
return (float)(std::atan(v) * (2.0 / juce::MathConstants<double>::pi)); default: break;
}; }
} }
auto& distPreGain = processorChain.template get<distortionPreGain>(); // [5]
distPreGain.setGainDecibels(driveDb); // [6] //==============================================================================
auto& distPostLPF = processorChain.template get<distortionPostLPF>(); void NeuralSynthVoice::renderNextBlock (juce::AudioBuffer<float>& outputBuffer,
distPostLPF.coefficients = *juce::dsp::IIR::Coefficients<float>::makePeakFilter( int startSample, int numSamples)
spec.sampleRate, {
toneHz, // cutoff if (numSamples <= 0)
0.707f, // Q return;
juce::Decibels::decibelsToGain(shared.highGainDbls->load())
); //if (! adsr.isActive())
// clearCurrentNote();
// =====================================================================
// Flanger // Apply pending waveform change (from GUI / processor thread)
// ===================================================================== const int wf = pendingWaveform.exchange (-1, std::memory_order_acq_rel);
// Get pointer to writable data if (wf != -1)
auto flanger = processorChain.get<flangerIndex>(); setWaveform (wf);
auto rate = shared.flangerPhase->load();
auto lfoPhase = shared.flangerPhase->load(); // --- Generate oscillator into temp buffer (WT or BLEP) ---
auto flangerDepth = shared.flangerDepth->load(); tempBuffer.clear();
auto mix = shared.flangerDryMix->load(); const int numCh = juce::jmin ((int) spec.numChannels, tempBuffer.getNumChannels());
auto feedback = shared.flangerFeedback->load();
const bool useWT = (shared.wtOn && shared.wtOn->load() > 0.5f);
// Step 2: Apply flanger sample-by-sample to the block if (useWT && shared.wtMorph)
auto* raw = block.getChannelPointer(0); wtOsc.setMorph(shared.wtMorph->load()); // 0..15 continuous
for (int i = 0; i < numSamples; ++i) for (int i = 0; i < numSamples; ++i)
{ {
float in = raw[i]; const float s = useWT ? wtOsc.process() : osc.process();
for (int ch = 0; ch < numCh; ++ch)
float lfo = std::sin(lfoPhase); tempBuffer.getWritePointer (ch)[i] = s;
float delayTime = (1.0f + lfo) * 0.5f * flangerDepth * spec.sampleRate; }
flanger.setDelay(delayTime); auto block = tempBlock.getSubBlock (0, (size_t) numSamples);
float delayed = flanger.popSample(0); // ================================================================
flanger.pushSample(0, in + delayed * feedback); // Flanger (pre-filter) manual per-sample to set varying delay
// ================================================================
raw[i] = in * (1.0f - mix) + delayed * mix; {
auto& flanger = chain.get<flangerIndex>();
lfoPhase += juce::MathConstants<float>::twoPi * rate / spec.sampleRate;
if (lfoPhase > juce::MathConstants<float>::twoPi) const bool enabled = shared.flangerOn && shared.flangerOn->load() > 0.5f;
lfoPhase -= juce::MathConstants<float>::twoPi; if (enabled)
} {
const float rate = shared.flangerRate ? shared.flangerRate->load() : 0.0f;
// Step 3: Run through ProcessorChain (filter + distortion) float lfoPhase = shared.flangerPhase ? shared.flangerPhase->load() : 0.0f;
juce::dsp::ProcessContextReplacing<float> fxContext(block); const float flangerDepth = shared.flangerDepth ? shared.flangerDepth->load() : 0.0f; // ms
processorChain.process(fxContext); const float mix = shared.flangerDryMix ? shared.flangerDryMix->load() : 0.0f;
const float feedback = shared.flangerFeedback ? shared.flangerFeedback->load() : 0.0f;
auto& master = processorChain.get<masterIndex>(); const float baseDelayMs = shared.flangerDelay ? shared.flangerDelay->load() : 0.25f;
const auto ex = shared.masterDbls->load();
master.setGainDecibels(shared.masterDbls->load()); for (int i = 0; i < numSamples; ++i)
{
auto& lowEQ = processorChain.get<eqLowIndex>(); const float in = tempBuffer.getReadPointer (0)[i];
lowEQ.coefficients = juce::dsp::IIR::Coefficients<float>::makeLowShelf(
spec.sampleRate, const float lfo = std::sin (lfoPhase);
100.0f, // cutoff const float delayMs = baseDelayMs + 0.5f * (1.0f + lfo) * flangerDepth;
0.707f, // Q, not used by all filters const float delaySamples = juce::jmax (0.0f, delayMs * 0.001f * (float) spec.sampleRate);
juce::Decibels::decibelsToGain(shared.lowGainDbls->load())
); flanger.setDelay (delaySamples);
auto& midEQ = processorChain.get<eqMidIndex>(); const float delayed = flanger.popSample (0);
midEQ.coefficients = *juce::dsp::IIR::Coefficients<float>::makePeakFilter( flanger.pushSample (0, in + delayed * feedback);
spec.sampleRate,
1000.0f, // center frequency const float out = in * (1.0f - mix) + delayed * mix;
1.0f, // Q for (int ch = 0; ch < numCh; ++ch)
juce::Decibels::decibelsToGain(shared.midGainDbls->load()) tempBuffer.getWritePointer (ch)[i] = out;
);
lfoPhase += juce::MathConstants<float>::twoPi * rate / (float) spec.sampleRate;
// HIGH SHELF if (lfoPhase > juce::MathConstants<float>::twoPi)
auto& highEQ = processorChain.get<eqHighIndex>(); lfoPhase -= juce::MathConstants<float>::twoPi;
highEQ.coefficients = *juce::dsp::IIR::Coefficients<float>::makePeakFilter( }
spec.sampleRate, }
10000.0f, // cutoff }
0.707f, // Q
juce::Decibels::decibelsToGain(shared.highGainDbls->load()) // ================================================================
); // Filter with per-sample ADSR modulation (poly)
// ================================================================
// 3. Apply ADSR envelope to tempBlock {
std::vector<float*> channelPtrs; const bool enabled = shared.filterOn && shared.filterOn->load() > 0.5f;
for (size_t ch = 0; ch < tempBlock.getNumChannels(); ++ch)
channelPtrs.push_back(tempBlock.getChannelPointer(ch)); // Update filter type every block (cheap)
const int ftype = (int) std::lround (juce::jlimit (0.0f, 2.0f,
juce::AudioBuffer<float> buffer(channelPtrs.data(), shared.filterType ? shared.filterType->load() : 0.0f));
static_cast<int>(tempBlock.getNumChannels()), switch (ftype)
static_cast<int>(tempBlock.getNumSamples())); {
case 0: svf.setType (juce::dsp::StateVariableTPTFilterType::lowpass); break;
adsr.applyEnvelopeToBuffer(buffer, 0, numSamples); case 1: svf.setType (juce::dsp::StateVariableTPTFilterType::highpass); break;
case 2: svf.setType (juce::dsp::StateVariableTPTFilterType::bandpass); break;
juce::dsp::AudioBlock<float>(outputBuffer) default: break;
.getSubBlock((size_t)startSample, (size_t)numSamples) }
.add(tempBlock);
} const float qOrRes = juce::jlimit (0.1f, 10.0f,
shared.filterResonance ? shared.filterResonance->load() : 0.7f);
//============================================================================== svf.setResonance (qOrRes);
void NeuralSynthVoice::noteStarted()
{ const float baseCutoff = juce::jlimit (20.0f, 20000.0f,
auto velocity = getCurrentlyPlayingNote().noteOnVelocity.asUnsignedFloat(); shared.filterCutoff ? shared.filterCutoff->load() : 1000.0f);
auto freqHz = (float)getCurrentlyPlayingNote().getFrequencyInHertz(); const float envAmt = shared.fenvAmount ? shared.fenvAmount->load() : 0.0f;
processorChain.get<oscIndex>().setFrequency(freqHz, true); for (int i = 0; i < numSamples; ++i)
{
auto& chorus = processorChain.get<chorusIndex>(); const float envVal = filterAdsr.getNextSample();
chorus.setCentreDelay(shared.chorusCentre->load()); const float cutoff = juce::jlimit (20.0f, 20000.0f,
chorus.setDepth(shared.chorusDepth->load()); baseCutoff * std::pow (2.0f, envAmt * envVal));
chorus.setFeedback(shared.chorusFeedback->load()); svf.setCutoffFrequency (cutoff);
chorus.setMix(shared.chorusMix->load());
chorus.setRate(shared.chorusRate->load()); if (enabled)
{
processorChain.get<delayIndex>().setDelay(shared.delayTime->load()); for (int ch = 0; ch < numCh; ++ch)
{
juce::Reverb::Parameters rp; float x = tempBuffer.getSample (ch, i);
x = svf.processSample (ch, x);
rp.damping = shared.reverbDamping->load(); tempBuffer.setSample (ch, i, x);
rp.dryLevel = shared.reverbDryLevel->load(); }
rp.freezeMode = shared.reverbFreezeMode->load(); }
rp.roomSize = shared.reverbRoomSize->load(); }
rp.wetLevel = shared.reverbWetLevel->load(); }
rp.width = shared.reverbWidth->load();
processorChain.get<reverbIndex>().setParameters(rp); // ================================================================
// Chorus
juce::ADSR::Parameters p; // ================================================================
p.attack = shared.adsrAttack->load(); if (shared.chorusOn && shared.chorusOn->load() > 0.5f)
p.decay = shared.adsrDecay->load(); {
p.sustain = shared.adsrSustain->load(); auto& chorus = chain.get<chorusIndex>();
p.release = shared.adsrRelease->load(); if (shared.chorusCentre) chorus.setCentreDelay (shared.chorusCentre->load());
if (shared.chorusDepth) chorus.setDepth (shared.chorusDepth->load());
adsr.setParameters(p); if (shared.chorusFeedback) chorus.setFeedback (shared.chorusFeedback->load());
adsr.noteOn(); if (shared.chorusMix) chorus.setMix (shared.chorusMix->load());
if (shared.chorusRate) chorus.setRate (shared.chorusRate->load());
}
chain.get<chorusIndex>().process (juce::dsp::ProcessContextReplacing<float> (block));
//============================================================================== }
void NeuralSynthVoice::notePitchbendChanged()
{ // ================================================================
auto freqHz = (float)getCurrentlyPlayingNote().getFrequencyInHertz(); // Simple Delay (per-voice)
processorChain.get<oscIndex>().setFrequency(freqHz, true); // ================================================================
} if (shared.delayOn && shared.delayOn->load() > 0.5f)
{
//============================================================================== auto& delay = chain.get<delayIndex>();
void NeuralSynthVoice::noteStopped(bool allowTailOff) const float time = shared.delayTime ? shared.delayTime->load() : 0.1f;
{ delay.setDelay (juce::jmax (0.0f, time * (float) spec.sampleRate));
adsr.noteOff(); //Triggers release phase delay.process (juce::dsp::ProcessContextReplacing<float> (block));
} }
//============================================================================== // ================================================================
void NeuralSynthVoice::notePressureChanged() {} // Reverb
void NeuralSynthVoice::noteTimbreChanged() {} // ================================================================
void NeuralSynthVoice::noteKeyStateChanged() {} if (shared.reverbOn && shared.reverbOn->load() > 0.5f)
{
void NeuralSynthVoice::setWaveform(int waveformType) juce::Reverb::Parameters rp;
{ rp.damping = shared.reverbDamping ? shared.reverbDamping->load() : 0.0f;
auto& osc = processorChain.template get<oscIndex>(); rp.dryLevel = shared.reverbDryLevel ? shared.reverbDryLevel->load() : 0.0f;
rp.freezeMode = shared.reverbFreezeMode ? shared.reverbFreezeMode->load() : 0.0f;
switch (waveformType) rp.roomSize = shared.reverbRoomSize ? shared.reverbRoomSize->load() : 0.0f;
{ rp.wetLevel = shared.reverbWetLevel ? shared.reverbWetLevel->load() : 0.0f;
case 0: rp.width = shared.reverbWidth ? shared.reverbWidth->load() : 0.0f;
osc.initialise([](float x) { return std::sin(x); });
break; chain.get<reverbIndex>().setParameters (rp);
chain.get<reverbIndex>().process (juce::dsp::ProcessContextReplacing<float> (block));
case 1: }
osc.initialise([](float x) { return x / juce::MathConstants<float>::pi; }); // Saw
break; // ================================================================
// Distortion + tone (post LPF/Peak)
case 2: // ================================================================
osc.initialise([](float x) { return x < 0.0f ? -1.0f : 1.0f; }); // Square {
break; const float driveDb = shared.distortionDrive ? shared.distortionDrive->load() : 0.0f;
const float bias = juce::jlimit (-1.0f, 1.0f, shared.distortionBias ? shared.distortionBias->load() : 0.0f);
case 3: const float toneHz = juce::jlimit (100.0f, 8000.0f, shared.distortionTone ? shared.distortionTone->load() : 3000.0f);
osc.initialise([](float x) { const int shape = (int) std::lround (juce::jlimit (0.0f, 2.0f,
return 2.0f * std::abs(2.0f * (x / juce::MathConstants<float>::twoPi) - 1.0f) - 1.0f; shared.distortionShape ? shared.distortionShape->load() : 0.0f));
}); // Triangle const float mix = shared.distortionMix ? shared.distortionMix->load() : 0.0f;
break;
} auto& pre = chain.get<distortionPreGain>();
} auto& sh = chain.get<distortionIndex>();
auto& tone = chain.get<distortionPostLPF>();
pre.setGainDecibels (driveDb);
// Explicit std::function target (works on MSVC)
if (shape == 0) sh.functionToUse = std::function<float(float)>{ [bias](float x) noexcept { return std::tanh (x + bias); } };
else if (shape == 1) sh.functionToUse = std::function<float(float)>{ [bias](float x) noexcept { return juce::jlimit (-1.0f, 1.0f, x + bias); } };
else sh.functionToUse = std::function<float(float)>{ [bias](float x) noexcept { return std::atan (x + bias) * (2.0f / juce::MathConstants<float>::pi); } };
tone.coefficients = juce::dsp::IIR::Coefficients<float>::makePeakFilter (
spec.sampleRate, toneHz, 0.707f,
juce::Decibels::decibelsToGain (shared.highGainDbls ? shared.highGainDbls->load() : 0.0f));
if (shared.distortionOn && shared.distortionOn->load() > 0.5f)
{
// Wet/dry blend around the shaper
juce::AudioBuffer<float> dryCopy (tempBuffer.getNumChannels(), numSamples);
for (int ch = 0; ch < numCh; ++ch)
dryCopy.copyFrom (ch, 0, tempBuffer, ch, 0, numSamples);
// pre -> shaper -> tone
pre.process (juce::dsp::ProcessContextReplacing<float> (block));
sh.process (juce::dsp::ProcessContextReplacing<float> (block));
tone.process (juce::dsp::ProcessContextReplacing<float> (block));
const float wet = mix, dry = 1.0f - mix;
for (int ch = 0; ch < numCh; ++ch)
{
auto* d = dryCopy.getReadPointer (ch);
auto* w = tempBuffer.getWritePointer (ch);
for (int i = 0; i < numSamples; ++i)
w[i] = dry * d[i] + wet * w[i];
}
}
}
// ================================================================
// EQ + Master + Limiter (EQ guarded by eqOn)
// ================================================================
{
const bool eqEnabled = shared.eqOn && shared.eqOn->load() > 0.5f;
auto& eqL = chain.get<eqLowIndex>();
auto& eqM = chain.get<eqMidIndex>();
auto& eqH = chain.get<eqHighIndex>();
if (eqEnabled)
{
eqL.coefficients = juce::dsp::IIR::Coefficients<float>::makeLowShelf (
spec.sampleRate, 100.0f, 0.707f,
juce::Decibels::decibelsToGain (shared.lowGainDbls ? shared.lowGainDbls->load() : 0.0f));
eqM.coefficients = juce::dsp::IIR::Coefficients<float>::makePeakFilter (
spec.sampleRate, 1000.0f, 1.0f,
juce::Decibels::decibelsToGain (shared.midGainDbls ? shared.midGainDbls->load() : 0.0f));
eqH.coefficients = juce::dsp::IIR::Coefficients<float>::makePeakFilter (
spec.sampleRate, 10000.0f, 0.707f,
juce::Decibels::decibelsToGain (shared.highGainDbls ? shared.highGainDbls->load() : 0.0f));
eqL.process (juce::dsp::ProcessContextReplacing<float> (block));
eqM.process (juce::dsp::ProcessContextReplacing<float> (block));
eqH.process (juce::dsp::ProcessContextReplacing<float> (block));
}
chain.get<masterIndex>().setGainDecibels (shared.masterDbls ? shared.masterDbls->load() : 0.0f);
chain.get<masterIndex>().process (juce::dsp::ProcessContextReplacing<float> (block));
chain.get<limiterIndex>().process (juce::dsp::ProcessContextReplacing<float> (block));
}
// ================================================================
// Apply AMP ADSR envelope
// ================================================================
{
juce::AudioBuffer<float> buf (tempBuffer.getArrayOfWritePointers(), numCh, numSamples);
adsr.applyEnvelopeToBuffer (buf, 0, numSamples);
}
// Mix into output
juce::dsp::AudioBlock<float> (outputBuffer)
.getSubBlock ((size_t) startSample, (size_t) numSamples)
.add (block);
}
//==============================================================================
void NeuralSynthVoice::noteStarted()
{
const float freqHz = (float) getCurrentlyPlayingNote().getFrequencyInHertz();
// Oscillator frequency + phase
osc.setFrequency (freqHz);
osc.resetPhase (0.0f);
// Wavetable oscillator too
wtOsc.setFrequency(freqHz);
wtOsc.resetPhase(0.0f);
// Chorus snapshot
if (shared.chorusCentre) chain.get<chorusIndex>().setCentreDelay (shared.chorusCentre->load());
if (shared.chorusDepth) chain.get<chorusIndex>().setDepth (shared.chorusDepth->load());
if (shared.chorusFeedback) chain.get<chorusIndex>().setFeedback (shared.chorusFeedback->load());
if (shared.chorusMix) chain.get<chorusIndex>().setMix (shared.chorusMix->load());
if (shared.chorusRate) chain.get<chorusIndex>().setRate (shared.chorusRate->load());
// Delay time (in samples)
if (shared.delayTime)
chain.get<delayIndex>().setDelay (juce::jmax (0.0f, shared.delayTime->load() * (float) spec.sampleRate));
// Reverb snapshot
juce::Reverb::Parameters rp;
rp.damping = shared.reverbDamping ? shared.reverbDamping->load() : 0.0f;
rp.dryLevel = shared.reverbDryLevel ? shared.reverbDryLevel->load() : 0.0f;
rp.freezeMode = shared.reverbFreezeMode ? shared.reverbFreezeMode->load() : 0.0f;
rp.roomSize = shared.reverbRoomSize ? shared.reverbRoomSize->load() : 0.0f;
rp.wetLevel = shared.reverbWetLevel ? shared.reverbWetLevel->load() : 0.0f;
rp.width = shared.reverbWidth ? shared.reverbWidth->load() : 0.0f;
chain.get<reverbIndex>().setParameters (rp);
// Amp ADSR
juce::ADSR::Parameters ap;
ap.attack = shared.adsrAttack ? shared.adsrAttack->load() : 0.01f;
ap.decay = shared.adsrDecay ? shared.adsrDecay->load() : 0.10f;
ap.sustain = shared.adsrSustain ? shared.adsrSustain->load() : 0.80f;
ap.release = shared.adsrRelease ? shared.adsrRelease->load() : 0.40f;
adsr.setParameters (ap);
adsr.noteOn();
// Filter ADSR
juce::ADSR::Parameters fp;
fp.attack = shared.fenvAttack ? shared.fenvAttack->load() : 0.01f;
fp.decay = shared.fenvDecay ? shared.fenvDecay->load() : 0.10f;
fp.sustain = shared.fenvSustain ? shared.fenvSustain->load() : 0.80f;
fp.release = shared.fenvRelease ? shared.fenvRelease->load() : 0.40f;
filterAdsr.setParameters (fp);
filterAdsr.noteOn();
}
//==============================================================================
void NeuralSynthVoice::notePitchbendChanged()
{
const float freqHz = (float) getCurrentlyPlayingNote().getFrequencyInHertz();
osc.setFrequency (freqHz);
wtOsc.setFrequency (freqHz);
}
//==============================================================================
void NeuralSynthVoice::noteStopped (bool allowTailOff)
{
juce::ignoreUnused (allowTailOff);
adsr.noteOff();
filterAdsr.noteOff();
}
//==============================================================================
void NeuralSynthVoice::setWaveform (int waveformType)
{
switch (juce::jlimit (0, 3, waveformType))
{
case 0: osc.setWave (BlepWave::Sine); break;
case 1: osc.setWave (BlepWave::Saw); break;
case 2: osc.setWave (BlepWave::Square); break;
case 3: osc.setWave (BlepWave::Triangle); break;
default: osc.setWave (BlepWave::Sine); break;
}
}

View File

@@ -1,124 +1,100 @@
#pragma once #pragma once
#include <JuceHeader.h>
#include <JuceHeader.h> #include <functional>
#include "NeuralSharedParams.h" #include "NeuralSharedParams.h"
#include "BlepOsc.h"
#include <iostream> #include "WavetableOsc.h" // <-- new
/*struct ADSRProcessor : public juce::dsp::ProcessorBase //==============================================================================
{ // A single voice with BLEP osc + optional Wavetable osc (morph + anti-aliasing),
// ----------------------------------------------------------------- // per-voice ADSR, filter ADSR, flanger/delay/chorus/reverb/distortion/EQ/master.
void prepare(const juce::dsp::ProcessSpec& spec) override class NeuralSynthVoice : public juce::MPESynthesiserVoice
{ {
adsr.setSampleRate(spec.sampleRate); public:
} explicit NeuralSynthVoice (NeuralSharedParams& sharedParams);
void reset() override { adsr.reset(); } // JUCE voice API
void prepare (const juce::dsp::ProcessSpec& spec);
void process(const juce::dsp::ProcessContextReplacing<float> &ctx) override void renderNextBlock (juce::AudioBuffer<float>& outputBuffer,
{ int startSample, int numSamples) override;
DBG("Processing...");
void noteStarted() override;
auto& outputBlock = context.getOutputBlock(); void noteStopped (bool allowTailOff) override;
const auto numSamples = (int)outputBlock.getNumSamples(); void notePitchbendChanged() override;
const auto numChannels = (int)outputBlock.getNumChannels();
void notePressureChanged() override {}
// Wrap the outputBlock into AudioBuffer void noteTimbreChanged() override {}
for (int ch = 0; ch < numChannels; ++ch) void noteKeyStateChanged() override {}
buffer.setWritePointer(ch, outputBlock.getChannelPointer(ch));
// Called from the processor when the GUI waveform param changes
adsr.applyEnvelopeToBuffer(buffer, 0, numSamples); void changeWaveform (int wf) { setWaveform (wf); }
}
private:
// ----------------------------------------------------------------- void setWaveform (int waveformType);
// These two are NOT part of the ProcessorBase interface <20> they are
// your private hooks that the voice will call on note events. //=== Processing chain (without oscillator) =================================
void noteOn(const juce::ADSR::Parameters& p) { using DelayLine = juce::dsp::DelayLine<float,
adsr.setParameters(p); adsr.noteOn(); juce::dsp::DelayLineInterpolationTypes::Linear>;
} using IIR = juce::dsp::IIR::Filter<float>;
void noteOff() { adsr.noteOff(); } using Gain = juce::dsp::Gain<float>;
using WaveShaper = juce::dsp::WaveShaper<float, std::function<float(float)>>;
private: using Chorus = juce::dsp::Chorus<float>;
juce::ADSR adsr; using Reverb = juce::dsp::Reverb;
juce::AudioBuffer<float> buffer; using Limiter = juce::dsp::Limiter<float>;
};*/
enum ChainIndex
//============================================================================== {
class NeuralSynthVoice : public juce::MPESynthesiserVoice flangerIndex = 0,
{ delayIndex,
public: chorusIndex,
NeuralSynthVoice(NeuralSharedParams& sp); reverbIndex,
distortionPreGain,
//============================================================================== distortionIndex,
void prepare(const juce::dsp::ProcessSpec& spec); distortionPostLPF,
eqLowIndex,
//============================================================================== eqMidIndex,
void noteStarted() override; eqHighIndex,
masterIndex,
//============================================================================== limiterIndex
void notePitchbendChanged() override; };
//============================================================================== using Chain = juce::dsp::ProcessorChain<
void noteStopped(bool) override; DelayLine, // flanger
DelayLine, // simple delay
//============================================================================== Chorus, // chorus
void notePressureChanged(); Reverb, // reverb
void noteTimbreChanged(); Gain, // distortion pre-gain (drive)
void noteKeyStateChanged(); WaveShaper, // distortion waveshaper
IIR, // tone / post-EQ for distortion
//============================================================================== IIR, // EQ low
void renderNextBlock(juce::AudioBuffer<float>& outputBuffer, int startSample, int numSamples); IIR, // EQ mid
IIR, // EQ high
void setWaveform(int waveformType); Gain, // master gain
Limiter // safety limiter
void changeWaveform(int waveform) noexcept { >;
this->waveform = waveform;
} private:
NeuralSharedParams& shared;
private: juce::dsp::ProcessSpec spec {};
//==============================================================================
juce::HeapBlock<char> heapBlock; // ==== Oscillators ========================================================
juce::dsp::AudioBlock<float> tempBlock; BlepOsc osc; // polyBLEP (existing)
std::atomic<int> pendingWaveform { -1 };
enum
{ WT::Osc wtOsc; // wavetable oscillator (new)
oscIndex, static std::shared_ptr<WT::Bank> wtBank; // shared bank across voices
distortionPreGain,
distortionIndex, // ==== Envelopes & Filter =================================================
distortionPostLPF, juce::ADSR adsr;
flangerIndex, juce::ADSR filterAdsr;
chorusIndex, juce::dsp::StateVariableTPTFilter<float> svf;
delayIndex,
reverbIndex, // ==== FX chain ===========================================================
eqLowIndex, Chain chain;
eqMidIndex,
eqHighIndex, // ==== Scratch buffer =====================================================
masterIndex juce::AudioBuffer<float> tempBuffer;
}; juce::dsp::AudioBlock<float> tempBlock;
juce::dsp::ProcessorChain< JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NeuralSynthVoice)
juce::dsp::Oscillator<float>, };
juce::dsp::Gain<float>,
juce::dsp::WaveShaper<float, std::function<float(float)>>,
juce::dsp::IIR::Filter<float>,
juce::dsp::DelayLine<float, juce::dsp::DelayLineInterpolationTypes::Linear>,
juce::dsp::Chorus<float>,
juce::dsp::DelayLine<float>,
juce::dsp::Reverb,
juce::dsp::IIR::Filter<float>, // Low shelf
juce::dsp::IIR::Filter<float>, // Mid peak
juce::dsp::IIR::Filter<float>, // High shelf
juce::dsp::Gain<float>
> processorChain;
juce::dsp::ProcessSpec spec;
juce::ADSR adsr;
NeuralSharedParams& shared;
static constexpr size_t lfoUpdateRate = 100;
static inline float msToSecs(float ms) { return ms * 0.001f; }
std::atomic<int> waveform { -1 };
};

261
Source/WavetableOsc.h Normal file
View File

@@ -0,0 +1,261 @@
#pragma once
#include <JuceHeader.h>
#include <vector>
#include <cmath>
// ============================== Design =======================================
// - Bank with F frames, each frame is a single-cycle table of N samples.
// - For each frame, we create L mip-levels: level 0 = full bandwidth,
// level l halves the permitted harmonics (spectral truncation).
// - Runtime chooses level from note frequency and sampleRate, then morphs
// between adjacent frames and crossfades between the two nearest levels.
// - Table read uses linear interpolation (cheap and good enough with N>=2048).
namespace WT
{
// Utility: complex array wrapper for JUCE FFT (interleaved real/imag floats)
struct ComplexBuf
{
std::vector<float> data; // size = 2 * N
explicit ComplexBuf(size_t N = 0) { resize(N); }
void resize(size_t N) { data.assign(2 * N, 0.0f); }
juce::dsp::Complex<float>* asComplex() { return reinterpret_cast<juce::dsp::Complex<float>*>(data.data()); }
};
// =======================================================================
// WavetableBank: holds raw frames + mipmapped versions
// =======================================================================
class Bank
{
public:
// N = table length (must be power-of-two for FFT), frames = number of morph frames
// mipLevels = how many spectral levels (>=1). 5 ~ 6 is plenty for synth use.
Bank(size_t N = 2048, int frames = 16, int mipLevels = 6)
: tableSize(N), numFrames(frames), numLevels(mipLevels),
fft((int)std::log2((double)N))
{
jassert(juce::isPowerOfTwo((int)N));
tables.resize((size_t)numLevels);
for (int l = 0; l < numLevels; ++l)
tables[(size_t)l].resize((size_t)numFrames, std::vector<float>(tableSize, 0.0f));
}
size_t getSize() const { return tableSize; }
int getFrames() const { return numFrames; }
int getLevels() const { return numLevels; }
// Provide raw “design” frames (time-domain single-cycle) then call buildMipmaps().
// framesRaw.size() must equal numFrames, each frame length must equal tableSize.
void setRawFrames(const std::vector<std::vector<float>>& framesRaw)
{
jassert((int)framesRaw.size() == numFrames);
for (const auto& f : framesRaw) jassert(f.size() == tableSize);
raw = framesRaw;
}
// Convenience: generate 16-frame bank morphing Sine -> Saw -> Square -> Triangle
void generateDefaultMorph()
{
std::vector<std::vector<float>> frames;
frames.resize((size_t)numFrames, std::vector<float>(tableSize, 0.0f));
auto fill = [&](int idx, auto func)
{
auto& t = frames[(size_t)idx];
for (size_t n = 0; n < tableSize; ++n)
{
const float ph = (float) (juce::MathConstants<double>::twoPi * (double)n / (double)tableSize);
t[n] = func(ph);
}
normalise(t);
};
// helper waves
auto sine = [](float ph) { return std::sin(ph); };
auto saw = [](float ph) { return (float)(2.0 * (ph / juce::MathConstants<float>::twoPi) - 1.0); };
auto sq = [](float ph) { return ph < juce::MathConstants<float>::pi ? 1.0f : -1.0f; };
auto tri = [](float ph) {
float v = (float)(2.0 * std::abs(2.0 * (ph / juce::MathConstants<float>::twoPi) - 1.0) - 1.0);
return v;
};
// 0..5: sine->saw, 6..10: saw->square, 11..15: square->triangle
const int F = numFrames;
for (int i = 0; i < F; ++i)
{
const float t = (float) i / (float) juce::jmax(1, F - 1);
std::function<float(float)> a, b;
float mix = 0.0f;
if (i <= 5) { a = sine; b = saw; mix = (float)i / 5.0f; }
else if (i <=10) { a = saw; b = sq; mix = (float)(i - 6) / 4.0f; }
else { a = sq; b = tri; mix = (float)(i - 11) / 4.0f; }
fill(i, [=](float ph){ return (1.0f - mix) * a(ph) + mix * b(ph); });
}
setRawFrames(frames);
}
// Build mip-levels by FFT → spectral truncation → IFFT
void buildMipmaps()
{
jassert(!raw.empty());
ComplexBuf freq(tableSize);
ComplexBuf time(tableSize);
for (int f = 0; f < numFrames; ++f)
{
// Forward FFT of raw frame
std::fill(freq.data.begin(), freq.data.end(), 0.0f);
for (size_t n = 0; n < tableSize; ++n)
{
time.data[2 * n + 0] = raw[(size_t)f][n];
time.data[2 * n + 1] = 0.0f;
}
fft.performRealOnlyForwardTransform(time.data.data());
// After JUCE real FFT, bins are laid out as: Re[0], Re[N/2], Re[1], Im[1], Re[2], Im[2], ...
// We'll reconstruct complex bins for easy masking.
// Helper to zero all harmonics above kMax (inclusive index in [0..N/2])
auto maskAndIFFT = [&](int level, int kMax)
{
// Copy time.data into working complex bins
auto* bins = freq.asComplex();
// DC & Nyquist are purely real in real-FFT
bins[0].real (time.data[0]);
bins[0].imag (0.0f);
bins[tableSize/2].real (time.data[1]);
bins[tableSize/2].imag (0.0f);
// Rebuild the rest (Re[k], Im[k]) packed starting at index 2
for (size_t k = 1; k < tableSize/2; ++k)
{
bins[k].real (time.data[2 * k + 0]);
bins[k].imag (time.data[2 * k + 1]);
}
// Mask
for (size_t k = (size_t)kMax + 1; k < tableSize/2; ++k)
bins[k] = { 0.0f, 0.0f };
// Pack back into real-FFT layout for inverse
time.data[0] = bins[0].real(); // DC
time.data[1] = bins[tableSize/2].real(); // Nyquist
for (size_t k = 1; k < tableSize/2; ++k)
{
time.data[2 * k + 0] = bins[k].real();
time.data[2 * k + 1] = bins[k].imag();
}
// IFFT
fft.performRealOnlyInverseTransform(time.data.data());
// Copy, normalise a little (scale JUCE inverse divides by N already)
auto& dst = tables[(size_t)level][(size_t)f];
for (size_t n = 0; n < tableSize; ++n)
dst[n] = time.data[2 * n + 0];
normalise(dst);
};
// Level 0 → all harmonics available up to N/2 - 1
for (int l = 0; l < numLevels; ++l)
{
const int maxH = (int)((tableSize / 2) >> l); // halve per level
const int kMax = juce::jmax(1, juce::jmin(maxH, (int)tableSize/2 - 1));
maskAndIFFT(l, kMax);
}
}
}
// sample at (frame, level, phase in [0,1))
inline float lookup (float frameIdx, int level, float phase) const noexcept
{
const int f0 = juce::jlimit(0, numFrames - 1, (int)std::floor(frameIdx));
const int f1 = juce::jlimit(0, numFrames - 1, f0 + 1);
const float t = juce::jlimit(0.0f, 1.0f, frameIdx - (float)f0);
const auto& T0 = tables[(size_t)level][(size_t)f0];
const auto& T1 = tables[(size_t)level][(size_t)f1];
const float pos = phase * (float)tableSize;
const int i0 = (int) std::floor(pos) & (int)(tableSize - 1);
const int i1 = (i0 + 1) & (int)(tableSize - 1);
const float a = pos - (float) std::floor(pos);
const float s0 = juce::jmap(a, T0[(size_t)i0], T0[(size_t)i1]);
const float s1 = juce::jmap(a, T1[(size_t)i0], T1[(size_t)i1]);
return juce::jmap(t, s0, s1);
}
// choose mip-level for given frequency (Hz) & sampleRate
inline int chooseLevel (float freq, double sampleRate) const noexcept
{
// permitted harmonics at this pitch:
const float maxH = (float) (0.5 * sampleRate / juce::jmax(1.0f, freq));
// level so that harmonic budget of level >= maxH, i.e. l = ceil(log2((N/2)/maxH))
const float base = (float)(tableSize * 0.5);
const float ratio = base / juce::jmax(1.0f, maxH);
int l = (int) std::ceil (std::log2 (ratio));
return juce::jlimit (0, numLevels - 1, l);
}
static void normalise (std::vector<float>& t)
{
float mx = 0.0f;
for (float v : t) mx = juce::jmax(mx, std::abs(v));
if (mx < 1.0e-6f) return;
for (float& v : t) v /= mx;
}
private:
size_t tableSize;
int numFrames;
int numLevels;
juce::dsp::FFT fft;
std::vector<std::vector<float>> raw;
// [level][frame][sample]
std::vector<std::vector<std::vector<float>>> tables;
};
// =======================================================================
// Wavetable Oscillator
// =======================================================================
class Osc
{
public:
void prepare (double sr) { sampleRate = sr; }
void setBank (std::shared_ptr<Bank> b) { bank = std::move(b); }
void setFrequency (float f) { freq = juce::jmax(0.0f, f); phaseInc = freq / (float)sampleRate; }
void setMorph (float m) { morph = m; } // 0..frames-1 (continuous)
void resetPhase (float p = 0.0f) { phase = juce::jlimit(0.0f, 1.0f, p); }
float process()
{
if (!bank) return 0.0f;
const int l0 = bank->chooseLevel(freq, sampleRate);
const int l1 = juce::jmin(l0 + 1, bank->getLevels() - 1);
const float preferL0 = 1.0f - juce::jlimit(0.0f, 1.0f,
(float)l0 - (float)bank->chooseLevel(freq * 0.99f, sampleRate));
const float s0 = bank->lookup(morph, l0, phase);
const float s1 = bank->lookup(morph, l1, phase);
const float out = juce::jmap(preferL0, s1, s0); // simple crossfade
phase += phaseInc;
while (phase >= 1.0f) phase -= 1.0f;
return out;
}
private:
std::shared_ptr<Bank> bank;
double sampleRate { 44100.0 };
float freq { 0.0f };
float morph { 0.0f }; // 0..frames-1
float phase { 0.0f };
float phaseInc { 0.0f };
};
} // namespace WT