Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separate .cpp / .h files #3

Open
mhamilt opened this issue Mar 22, 2021 · 1 comment
Open

Separate .cpp / .h files #3

mhamilt opened this issue Mar 22, 2021 · 1 comment
Labels
enhancement New feature or request

Comments

@mhamilt
Copy link

mhamilt commented Mar 22, 2021

At this stage It would a wise idea to split your current .h files into separate .cpp / .h files.

If you add the files into your project with Projucer then you won't need to worry about configuring build phases.

@mhamilt
Copy link
Author

mhamilt commented Mar 23, 2021

Someone asked for an example of splitting up a .h file into .h and .cpp

Your header file should just be a declaration of the class and some documentation.

Lets take the Oscillator as an example

In the .h

class Oscillator
{
public:
    void setup(float sampleRate, float frequency);
    void setSampleRate(float sampleRate);
    void setFrequency(float frequency);
    
    float process();
private:
    float sampRate;
    float freq;
    float phaseDelta;
    float phase;
};

This is fine, but it is doesn't tell us much. So give yourself some documentation! And don't just say what something is, tell yourself (and others) what something is for, its limitation and requirements

/// A simple sine wave oscillator
class Oscillator
{
public:
    /// setup required parameters for oscillator to function
    ///
    ///  You can run setSampleRate and setFrequency separately but failing to call this method
    ///  or any other before `process` will result in undefined behaviour
    ///
    /// @see setSampleRate, setFrequency
    /// @param sampleRate current audio sampling rate.: typically 44100 or 48000, though other values will be allowed
    /// @param frequency fundamental frequency this should be greater than zero and less than sampleRate / 2 (i.e. Nyquist)
    void setup(float sampleRate, float frequency);
    //--------------------------------------------------------------------------
    /// Set the internal sample so that the correct phase delta is used. You **MUST** call this method before setFrequency
    /// @see setup, setFrequency
    /// @param sampleRate current audio sampling rate.: typically 44100 or 48000, though other values will be allowed
    void setSampleRate(float sampleRate);
    //--------------------------------------------------------------------------
    /// Set the fundamental frequency of the oscillator. Remember you **MUST** call setSampleRate before calling this method
    ///
    /// @warning Calling this method will reset the phase of the oscillator so take care to only call this at a zero crossing value to avoid
    ///          clipping audio
    /// @param frequency fundamental frequency this should be greater than zero and less than sampleRate / 2 (i.e. Nyquist)
    ///                  Negative frequency will be interpreted as _phase inverted_
    void setFrequency(float frequency);
    //--------------------------------------------------------------------------
    /// get the next amplitude of the oscillator. This method will increment the phase of the oscillator.
    /// @return sample value in floating point format scaled between -1.0 and 1.0
    float process();
    
private:
    /// remember to set sample rate before setting frequency
    /// if sampRate has not been set it should equal `0`. test for this and assert
    float sampRate = 0;
    /// fundamental frequency
    /// TODO: apply some kind of assertion that frequency is below Nyquist and not zero
    float freq;
    /// phase is the phase change between successive samples. This value should not be altered by any method EXCEPT setFrequency
    float phaseDelta;
    /// current phase step. Scaled between 0 and 1
    float phase;
};

This way if all someone has is the .h file open they can at least get an impression as to what your class does

in the .cpp

The .cpp is where you define what the methods actually do. Since it is not defined in the class declaration, definitions take a slightly different form, which is always return_type CLAS_NAME::METHOD_NAME(ARGUMENTS)

e.g.

void Oscillator::setup(float sampleRate, float frequency)
{
  // blah blah
}

void Oscillator::setSampleRate(float sampleRate)
{
  //  blah blah
}

float Oscillator::process()
{
  return 0.0f; // blah blah
}

I'd recommend also just taking a look at other source code. You don't have to understand it, but you will get a feel for how to organise.

@nestflow nestflow added the enhancement New feature or request label Mar 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants