Sin Synth in AudioUnit

Apple APIs and Mac audio software development
Post Reply
merlin_1102
Posts: 1
Joined: 04 Jan 2009, 00:34

Sin Synth in AudioUnit

Post by merlin_1102 » 04 Jan 2009, 00:41

Hey

Not sure if anyone still posts here, but I have written a small tone generator for mac os x that uses HAL. I have 8 different patterns I can generate (saves me the time of purchasing hardware to do this), and now I would like to introduce mixing. From what I have read on AudioUnits, this can be really easy. I am however having great difficulty understanding/scrapping together even a basic sin wave in an audio unit form. I was wondering if someone could give me some pointers. I have created a AudioUnit and I can get it to load into AU Lab, but the tone I generate is not a sin wave. My render code is below (Sorry for the mess I have just been playing around with it). Most of my inspiration thus far has come from the AUPinkNoise sample.

Any tips would be appreciated. I know there are a couple of issues with it as is, I just want to first generate a proper sin sound and then I will go back and clean it up.

Thanks.

ComponentResult AudioUnitSinGen::Render(AudioUnitRenderActionFlags &ioActionFlags,
const AudioTimeStamp & inTimeStamp,
UInt32 nFrames)
{
static unsigned long long pt = 0;
static unsigned long long vpt = 0;
Float64 sampleRate = GetSampleRate();
float samplesPerSecond = 1.0/sampleRate;

AUOutputElement* outputBus = GetOutput(0);
outputBus->PrepareBuffer(nFrames); // prepare the output buffer list


AudioBufferList &outputBufList = outputBus->GetBufferList();
AUBufferList::ZeroBuffer(outputBufList);
float freq = Globals()->GetParameter(kParam_Volume);

// only render if the on parameter is true. Otherwise send the zeroed buffer
if (Globals()->GetParameter(kParam_On)) {
for (UInt32 i=0; i < outputBufList.mNumberBuffers; i++) {
Float32 *destP = (Float32*)outputBufList.mBuffers.mData;
int n = nFrames;
pt = pt + n;
vpt = pt;
while(n--) {
Float32 sample = 1.0 * sin( (vpt * (1.0/44100) * freq) * twopi );
*destP++ = sample;
vpt--;
}
}
}

Post Reply