-
Notifications
You must be signed in to change notification settings - Fork 0
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
Comments
Someone asked for an example of splitting up a Your header file should just be a declaration of the class and some documentation. Lets take the In the 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 in the The 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. |
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.
The text was updated successfully, but these errors were encountered: