Trying a simple FM synth but getting noise

Hey,

I am trying to modulate the frequency of an juce::dsp::Oscillator with another juce::dsp::Oscillator
sounds like I am doing something wrong since I am getting a lot of noise and my values are totally out of range. This is as far as my mind can go, please tell me what I’m doing wrong and sorry if it is something obvious.

void renderNextBlock(juce::AudioBuffer<float> &outputBuffer, int startSample, int numSamples)
{
  
        jassert (isPrepared);
        
        if (! isVoiceActive())
            return;
        
        synthBuffer.setSize (outputBuffer.getNumChannels(), numSamples, false, false, true);
        fmBuffer.setSize(outputBuffer.getNumChannels(), numSamples, false, false, true);
        synthBuffer.clear();
        fmBuffer.clear();
          
        gain.setGainLinear(volumeParam);
        
        juce::dsp::AudioBlock<float> audioBlock { synthBuffer };
        juce::dsp::AudioBlock<float> fmBlock { fmBuffer };
        fmOsc.process(juce::dsp::ProcessContextReplacing<float> (fmBlock));
        
        float processedSample;
        double newFreq;
          
        for (int channel = 0; channel < synthBuffer.getNumChannels(); ++channel)
        {
            auto* buffer = synthBuffer.getWritePointer (channel, 0);
            
            for (int sample = 0;sample < synthBuffer.getNumSamples(); ++sample){
                newFreq = freq + fmBuffer.getSample(channel, sample);
                osc.setFrequency(newFreq, true);
                processedSample = osc.processSample(sample);
                buffer[sample] = processedSample;
            }
        }
          
        gain.process(juce::dsp::ProcessContextReplacing<float> (audioBlock));
        
        for (int channel = 0; channel < synthBuffer.getNumChannels(); ++channel)
        {
        
            for (int sample = 0;sample < synthBuffer.getNumSamples(); ++sample){
                std::cout << "Sample Value:" <<  synthBuffer.getSample(channel, sample) << std::endl;
            }
  }

The JUCE Oscillator class isn’t really suitable for doing FM as it cannot have a negative phase increment. Plus it does other stuff you don’t really want, such as smoothing of frequencies.

1 Like

Thank you,
is this why I am getting the noise though ?
Could you provide an example that would work ?
And last but not least could you provide some good reads where such concepts are explained ?

For a demo of a basic FM synth, see the DX10 project here: GitHub - hollance/mda-plugins-juce: JUCE implementations of the classic MDA audio plug-ins

1 Like