Re: Callback problems
Re: Callback problems
- Subject: Re: Callback problems
- From: Philippe Wicker <email@hidden>
- Date: Sat, 21 Feb 2004 23:31:20 +0100
On Feb 21, 2004, at 6:45 PM, Frances Buck wrote:
Hi Frances,
You get crackle because you call fread in MyRenderer callback. This
call may take a much longer time to execute than the time slice
allocated to your callback. Beside, this kind of call is prohibited in
the context of the render callback because it may block for some time
(e.g. in this particular example if the data you're reading are not
already resident in the memory - ie the disk cache - then the disk head
must first move before data can be read).
In MyRenderer you return in mDataByteSize the number of bytes actually
read from the disk. It should work the other way. The size - expressed
as frames number - of the slice to render is passed to your callback
in inNumberFrames parameters and you **must** either render that much
of audio or return that much silence - no more no less - in the
buffers pointed to by ioData->mBuffers[x].mData .
ioData->mBuffers[x].mDataByteSize fields contain the size in bytes
corresponding to inNumberFrames frames. You don't need to modify these
fields.
In your case, you could read the whole file (if it's not too big) in
memory, and copy consecutive slices with a size of inNumberFrames from
the file buffer in memory to the ioData buffer each time MyRenderer is
called.
You may also have a look at the following sample available at (Jaguar
CoreAudio SDK):
http://developer.apple.com/samplecode/Sample_Code/Core_Audio/
PlayAudioFileLite.htm
Or on your hard disk (Panther CoreAudio SDK):
/Developer/Examples/CoreAudio/Services/PlayAudioFileLite
There's also a (much) more complex sample code installed with the
CoreAudio SDK on your hard disk (for both Jaguar and Panther):
/Developer/Examples/CoreAudio/Services/PlayAudioFile
This last sample shows how to read the data from disk chunk by chunk in
a "feeder" thread. The "render" thread uses data from one chunk while
the next chunk is being read by the feeder thread.
Hello,
I am new to this mailing - list and new to CoreAudio - Thing.
I start to stream a waveFile without a header to see if I get the idea
of the AudioUnit and its Callback.
I can hear the music but I do get some small crackle above the music.
So I think I am doing something wrong.
I hope that someone can help me.
Thank you,
Frances
Here ist the Code
FILE* waveFile;
char buffer[8192]; // if I try 4096 (or smaller) I get into trouble
long bytes_read;
OSStatus MyRenderer(void *inRefCon, AudioUnitRenderActionFlags
*ioActionFlags, 4
const AudioTimeStamp *inTimeStamp,
UInt32 inBusNumber,
UInt32 inNumberFrames, AudioBufferList
*ioData)
{
bytes_read = fread(buffer, 1, 8192, waveFile);
memcpy(ioData->mBuffers[0].mData, buffer, bytes_read);
ioData->mBuffers[0].mDataByteSize = bytes_read;
return noErr;
}
void ThePlayFunction()
{
OSStatus err = noErr;
AudioUnit gOutputUnit;
// The Wave - File without a Header
waveFile = fopen("test.wav" , "rb");
// Open the default output unit
ComponentDescription desc;
desc.componentType = kAudioUnitType_Output;
desc.componentSubType = kAudioUnitSubType_DefaultOutput;
desc.componentManufacturer = kAudioUnitManufacturer_Apple;
desc.componentFlags = 0;
desc.componentFlagsMask = 0;
Component comp = FindNextComponent(NULL, &desc);
err = OpenAComponent(comp, &gOutputUnit);
err = AudioUnitInitialize(gOutputUnit);
// Set up a callback function to generate output to the output unit
AURenderCallbackStruct input;
input.inputProc = MyRenderer;
input.inputProcRefCon = NULL;
err = AudioUnitSetProperty (gOutputUnit,
kAudioUnitProperty_SetRenderCallback,
kAudioUnitScope_Input, 0,
&input, sizeof(input));
// We tell the Output Unit what format we're going to supply data ...
AudioStreamBasicDescription streamFormat;
streamFormat.mSampleRate = 44100.0;
streamFormat.mFormatID = kAudioFormatLinearPCM;
streamFormat.mFormatFlags =
kLinearPCMFormatFlagIsSignedInteger;
streamFormat.mBytesPerPacket = 4;
streamFormat.mFramesPerPacket = 1;
streamFormat.mBytesPerFrame = 4;
streamFormat.mChannelsPerFrame = 2;
streamFormat.mBitsPerChannel = 16;
err = AudioUnitSetProperty (gOutputUnit,
kAudioUnitProperty_StreamFormat,
kAudioUnitScope_Input, 0,
&streamFormat,
sizeof(streamFormat));
// Start the rendering
err = AudioOutputUnitStart (gOutputUnit);
do{
}
while (true);
//AndTheRest...
}
_________________________________________________________________
Stay in touch with absent friends - get MSN Messenger
http://www.msn.co.uk/messenger
_______________________________________________
coreaudio-api mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/coreaudio-api
Do not post admin requests to the list. They will be ignored.
Philippe Wicker
email@hidden
_______________________________________________
coreaudio-api mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/coreaudio-api
Do not post admin requests to the list. They will be ignored.