I'm having trouble updating old code to Snow Leopard. The code I
have was working on 10.4 (Tiger). I have extracted a small sample
program, included below, which demonstrates my error in a self-
contained way. The program is expected to compress a sine wave using
Apple Lossless compression. Unfortunately, my render callback is
never called, and AudioUnitRender returns a -50 error code the first
time it is called.
Am I missing something in the initialization of the AudioUnit? Is
there something wrong with my stream descriptions?
As far as I can tell the AudioUnit never gets to the point of seeing
my data, so I presume my set up is incorrect but I can't figure out
where.
Thanks for any assistance,
-tom
/* To compile:
*
* cc -Wall -g -framework Foundation -framework AudioUnit -framework
CoreServices -o HelpLossless HelpLossless.m
*/
#import <Foundation/Foundation.h>
#import <QuickTime/QuickTime.h>
#import <QuickTime/QuickTimeComponents.h>
#import <AudioUnit/AudioUnit.h>
#define DOERR(x, y) err = (x); \
if (err) { fprintf (stderr, "Failed to %s: %d\n", y, err); exit(1); }
#define LOSSLESS_DUR 4096
static OSStatus
compressCallback(void *inRefCon,
AudioUnitRenderActionFlags *ioActionFlags,
const AudioTimeStamp *inTimeStamp,
UInt32 inBusNumber,
UInt32 inNumberFrames,
AudioBufferList *ioData)
{
NSLog(@"compress callback");
memcpy (ioData->mBuffers[0].mData, inRefCon, LOSSLESS_DUR * 2);
return noErr;
}
int main(int argc, char **argv)
{
OSErr err;
// setup audio buffer and data array
SInt16 *data = (SInt16 *) malloc (LOSSLESS_DUR * 2);
AudioBufferList *outData =
(AudioBufferList *) malloc (sizeof (AudioBufferList));
outData->mNumberBuffers = 1;
outData->mBuffers[0].mNumberChannels = 1;
outData->mBuffers[0].mDataByteSize = LOSSLESS_DUR * 4; // in case
expands
outData->mBuffers[0].mData = malloc (LOSSLESS_DUR * 4);
// set up audio unit
ComponentDescription cd = {0};
cd.componentType = kAudioUnitType_FormatConverter;
cd.componentSubType = kAudioUnitSubType_AUConverter;
Component c2 = FindNextComponent (NULL, &cd);
if (! c2) {
fprintf(stderr, "Failed to find compress component\n");
return 1;
}
AudioUnit compress;
compress = OpenComponent(c2);
if (! compress) {
fprintf(stderr, "Failed to open lossless component\n");
return 1;
}
// set up the input format
AudioStreamBasicDescription asbd;
asbd.mSampleRate = 16000;
asbd.mChannelsPerFrame = 1;
asbd.mFormatID = kAudioFormatLinearPCM;
asbd.mFormatFlags = (kAudioFormatFlagIsSignedInteger
| kAudioFormatFlagIsPacked);
asbd.mFramesPerPacket = 1;
asbd.mBytesPerPacket = 2;
asbd.mBytesPerFrame = 2;
asbd.mBitsPerChannel = 16;
DOERR (AudioUnitSetProperty (compress,
kAudioUnitProperty_StreamFormat,
kAudioUnitScope_Input,
0, &asbd, sizeof (asbd)),
"set compress input format");
// set up the output format
asbd.mFormatID = kAudioFormatAppleLossless;
asbd.mFormatFlags = kAppleLosslessFormatFlag_16BitSourceData;
asbd.mFramesPerPacket = LOSSLESS_DUR;
asbd.mBytesPerPacket = 0;
asbd.mBytesPerFrame = 0;
asbd.mBitsPerChannel = 0;
DOERR (AudioUnitSetProperty (compress,
kAudioUnitProperty_StreamFormat,
kAudioUnitScope_Output,
0, &asbd, sizeof (asbd)),
"set compress output format");
AURenderCallbackStruct cb;
cb.inputProc = compressCallback;
cb.inputProcRefCon = data;
NSLog(@"setting compress callback");
DOERR (AudioUnitSetProperty (compress,
kAudioUnitProperty_SetRenderCallback,
kAudioUnitScope_Input, 0, &cb, sizeof
(cb)),
"set compress callback");
DOERR (AudioUnitInitialize (compress), "initialize compress");
// generate the sound and compress
int i;
AudioTimeStamp timeStamp;
for (i = 0; i < 40; i++) {
int j;
NSLog(@"generating sound");
for (j = 0; j < LOSSLESS_DUR; j++) {
double val = (sin (j * M_PI / 12.0)) * pow(2.0, 14.0);
data[j] = (SInt16) val;
}
NSLog(@"compressing sound");
DOERR (AudioUnitRender (compress, NULL, &timeStamp, 0, 1, outData),
"execute lossless");
NSLog(@"compressed output has %d bytes",
outData->mBuffers[0].mDataByteSize);
outData->mBuffers[0].mDataByteSize = LOSSLESS_DUR * 4;
}
return 0;
}
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Coreaudio-api mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden