synth-plugin-book icon indicating copy to clipboard operation
synth-plugin-book copied to clipboard

Chapter 5, Oscillator.h & Synth.cpp suggestions

Open tomaszrykala opened this issue 1 year ago • 1 comments

Hi, thank you for the book, I'm really enjoying it. This is my first suggestion to the book. Hope that's ok!

In Oscillator.h (p.102 in book) instead of asking the reader to manually hardcode the value of TWO_PI, with the possibility of making an error, not suggest to use the predefined Juce constant?

//const float TWO_PI = 6.2831853071795864f; // not this
const float TWO_PI = juce::MathConstants<float>::twoPi; // but this?

In Synth.cpp instead of repeating voice.osc multiple times, why not use a field reference, decreasing the boilerplate a little

void Synth::noteOn(int note, int velocity)
{
    voice.note = note;
    
    Oscillator& osc = voice.osc; // this is new, instead of multiple `voice.osc` below
    osc.amplitude = (velocity / 127.f) * 0.5f;
    osc.freq = 261.63f;
    osc.sampleRate = sampleRate;
    osc.phaseOffset = 0.0f;
    osc.reset();
}

I hope the code is correct. I'm a seasoned Java, Kotlin and Android developer, learning C++ at the moment. Thank you!

tomaszrykala avatar Jan 08 '24 23:01 tomaszrykala

These are fine suggestions and it's perfectly OK to write the code like you suggest. In fact, I encourage people to make these kinds of changes, because it means you're actually interacting with the code, rather than just typing it in. 😄

I wrote the synth code to be as independent of JUCE as possible, since not everyone will want to use JUCE, so that's why I didn't use juce::MathConstants<float>::twoPi.

hollance avatar Jan 11 '24 11:01 hollance