• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Sample Code Here
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


  • Prev by Date: Re: Has AudioUnits actually been tried in carbon?
  • Next by Date: Re: Request for Sample AudioUnit Sources
  • Previous by thread: Re: Has AudioUnits actually been tried in carbon?
  • Next by thread: More Audio Unit Questions
  • Index(es):
    • Date
    • Thread