GetDunne Wiki

Notes from the desk of Shane Dunne, software development consultant

User Tools

Site Tools


the_synthesizer

This is an old revision of the document!


The VanillaJuce synthesizer

The synthesizer-related classes Synth, SynthSound, SynthParameters have already been covered on the overview page. This leaves only SynthOscillator, SynthEnvelopeGenerator, and SynthVoice. SynthVoice is by far the most important, so let's start with that. I'm not going to pore over every line of code; instead my goal is to point out what's important to help you understand the code as you read it for yourself.

SynthVoice

Because our synthesizer class Synth inherits from juce::Synthesiser, it inherits a whole pre-built mechanism for dynamically assigning SynthVoice objects to MIDI notes as they are played. We don't have to write any of that tricky code at all. (In fact, the main reason I wrote VanillaJuce at all was because Obxd, the only complete, uncomplicated JUCE-based synthesizer example I was able to find, was not based on juce::Synthesiser.)

The key juce::SynthesiserVoice member functions that SynthVoice overrides are:

    void startNote(int midiNoteNumber, float velocity, SynthesiserSound* sound, int currentPitchWheelPosition) override;
    void stopNote(float velocity, bool allowTailOff) override;
    void pitchWheelMoved(int newValue) override;
    void controllerMoved(int controllerNumber, int newValue) override;
 
    void renderNextBlock(AudioSampleBuffer& outputBuffer, int startSample, int numSamples) override;

The juce::Synthesiser::renderNextBlock() function calls each active voice's renderNextBlock() function once for every “block” (buffer) of output audio. Our implementation is this:

void SynthVoice::renderNextBlock(AudioSampleBuffer& outputBuffer, int startSample, int numSamples)
{
    while (--numSamples >= 0)
    {
        if (!ampEG.isRunning())
        {
            clearCurrentNote();
            break;
        }
        float aeg = ampEG.getSample();
        float osc = osc1.getSample() * osc1Level.getNextValue() + osc2.getSample() * osc2Level.getNextValue();
        float sample = aeg * osc;
        outputBuffer.addSample(0, startSample, sample);
        outputBuffer.addSample(1, startSample, sample);
        ++startSample;
    }
}

Don't worry about all the details yet. At this point, just note that the outer while loop iterates over all the samples in outputBuffer, and at each step, a new sample value sample is computed for this voice, and added in to whatever may already be there in outputBuffer, using its addSample() function. In this way, all of the active voices (sounding notes) are effectively summed together into the output buffer.

SynthOscillator

SynthEnvelopeGenerator

Summary: What happens when you play a note

the_synthesizer.1504125824.txt.gz · Last modified: 2017/08/30 20:43 by shane