Sample Code Here
Sample Code Here
- Subject: Sample Code Here
- From: Brian Barnes <email@hidden>
- Date: Thu, 27 Sep 2001 02:06:21 -0400
Terrible, terrible sample code, but I got it to play a sound! I feel
like I moved the a mountain to get here, though.
Just call start_audio to start the sound, end_audio to end it. I got
this to work, but I still don't understand lots of it, but it's a good
starting place for those with questions.
No error checking, off course. Plays a silly moving sine wave.
--------------------------------------------------------------------------
#include <Carbon/Carbon.h>
#include <AudioUnit/AudioUnit.h>
typedef struct AudioUnitInputCallback AudioUnitInputCallbacks;
AudioUnit audiounit;
float f_chg=0.001;
// this is where the audio unit calls me back for samples. I know the
samples are in floats, I don't know what rate it's using or how to set
it.
OSStatus my_render_callback(void *inRefCon, AudioUnitRenderActionFlags
inActionFlags, const AudioTimeStamp *inTimeStamp, UInt32 inBusNumber,
AudioBuffer *ioData)
{
int i,bufsz;
float f,*data;
bufsz=ioData->mDataByteSize/sizeof(float);
data=(float*)ioData->mData;
f=0;
for ((i=0);(i!=bufsz);i++) {
*data++=(float)sin(f);
f+=f_chg;
}
f_chg=f_chg+0.001;
if (f_chg>0.1) f_chg=0.001;
return(0);
}
void start_audio(void)
{
int err;
AudioUnitInputCallbacks aucallback;
audiounit=(AudioUnit)OpenDefaultComponent(kAudioUnitComponentType,kAudioUnitSubType_Output)
;
aucallback.inputProc=my_render_callback;
aucallback.inputProcRefCon=(void*)0;
err=AudioUnitSetProperty(audiounit,kAudioUnitProperty_SetInputCallback,kAudioUnitScope_Input,
0,(void*)&aucallback,sizeof(AudioUnitInputCallbacks));
err=AudioUnitInitialize(audiounit);
err=AudioOutputUnitStart(audiounit);
}
void end_audio(void)
{
AudioOutputUnitStart(audiounit);
AudioUnitUninitialize(audiounit);
CloseComponent(audiounit);
}
[>] Brian