Files
NeuralSynth/Source/SynthVoice.h
2025-10-22 16:58:43 +00:00

101 lines
3.5 KiB
C++

#pragma once
#include <JuceHeader.h>
#include <functional>
#include "NeuralSharedParams.h"
#include "BlepOsc.h"
#include "WavetableOsc.h" // <-- new
//==============================================================================
// A single voice with BLEP osc + optional Wavetable osc (morph + anti-aliasing),
// per-voice ADSR, filter ADSR, flanger/delay/chorus/reverb/distortion/EQ/master.
class NeuralSynthVoice : public juce::MPESynthesiserVoice
{
public:
explicit NeuralSynthVoice (NeuralSharedParams& sharedParams);
// JUCE voice API
void prepare (const juce::dsp::ProcessSpec& spec);
void renderNextBlock (juce::AudioBuffer<float>& outputBuffer,
int startSample, int numSamples) override;
void noteStarted() override;
void noteStopped (bool allowTailOff) override;
void notePitchbendChanged() override;
void notePressureChanged() override {}
void noteTimbreChanged() override {}
void noteKeyStateChanged() override {}
// Called from the processor when the GUI waveform param changes
void changeWaveform (int wf) { setWaveform (wf); }
private:
void setWaveform (int waveformType);
//=== Processing chain (without oscillator) =================================
using DelayLine = juce::dsp::DelayLine<float,
juce::dsp::DelayLineInterpolationTypes::Linear>;
using IIR = juce::dsp::IIR::Filter<float>;
using Gain = juce::dsp::Gain<float>;
using WaveShaper = juce::dsp::WaveShaper<float, std::function<float(float)>>;
using Chorus = juce::dsp::Chorus<float>;
using Reverb = juce::dsp::Reverb;
using Limiter = juce::dsp::Limiter<float>;
enum ChainIndex
{
flangerIndex = 0,
delayIndex,
chorusIndex,
reverbIndex,
distortionPreGain,
distortionIndex,
distortionPostLPF,
eqLowIndex,
eqMidIndex,
eqHighIndex,
masterIndex,
limiterIndex
};
using Chain = juce::dsp::ProcessorChain<
DelayLine, // flanger
DelayLine, // simple delay
Chorus, // chorus
Reverb, // reverb
Gain, // distortion pre-gain (drive)
WaveShaper, // distortion waveshaper
IIR, // tone / post-EQ for distortion
IIR, // EQ low
IIR, // EQ mid
IIR, // EQ high
Gain, // master gain
Limiter // safety limiter
>;
private:
NeuralSharedParams& shared;
juce::dsp::ProcessSpec spec {};
// ==== Oscillators ========================================================
BlepOsc osc; // polyBLEP (existing)
std::atomic<int> pendingWaveform { -1 };
WT::Osc wtOsc; // wavetable oscillator (new)
static std::shared_ptr<WT::Bank> wtBank; // shared bank across voices
// ==== Envelopes & Filter =================================================
juce::ADSR adsr;
juce::ADSR filterAdsr;
juce::dsp::StateVariableTPTFilter<float> svf;
// ==== FX chain ===========================================================
Chain chain;
// ==== Scratch buffer =====================================================
juce::AudioBuffer<float> tempBuffer;
juce::dsp::AudioBlock<float> tempBlock;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NeuralSynthVoice)
};