I want to work out a bit of code that generates the oscillator wave-type in my tone generator app. The one in this example is a sine-wave, can someone tell me how the code works, as i want to in the future make custom wave-types and square, sawtooth and triangle types.
OSStatus RenderTone(
void *inRefCon,
AudioUnitRenderActionFlags *ioActionFlags,
const AudioTimeStamp *inTimeStamp,
UInt32 inBusNumber,
UInt32 inNumberFrames,
AudioBufferList *ioData)
{
// Fixed amplitude is good enough for our purposes
const double amplitude = 0.25;
// Get the tone parameters out of the view controller
ToneGeneratorViewController *viewController =
(ToneGeneratorViewController *)inRefCon;
double theta = viewController->theta;
double theta_increment = 2.0 * M_PI * viewController->frequency / viewController->sampleRate;
// This is a mono tone generator so we only need the first buffer
const int channel = 0;
Float32 *buffer = (Float32 *)ioData->mBuffers[channel].mData;
// Generate the samples
for (UInt32 frame = 0; frame < inNumberFrames; frame++)
{
buffer[frame] = sin(theta) * amplitude;
theta += theta_increment;
if (theta > 2.0 * M_PI)
{
theta -= 2.0 * M_PI;
}
}
// Store the theta back in the view controller
viewController->theta = theta;
return noErr;
}
The actual sine wave samples are being generated and are populating the buffer in the snippet below
In the line where
buffer[frame]is being assigned, you are callingsin(theta) * amplitude, and for each iteration of theforloop, you are incrementingthetaby some finite step size based on your frequency and sample rate, viaWhich is essentially dividing
2.0 * PI * frequencyby your sample rate.Incrementing the
thetavariable while looping through theforloop is basically advancing the time step one sample at a time until your buffer is full (i.e.frame == iNumberFrames).If you wanted to generate something other than a sine wave, you would simply replace the following line with some other function:
I.e. let’s say, for example, you wanted the first three terms in the infinite Fourier series that converges to a triangle wave; you might then have the following instead…