开发者

iOS SDK 4.3 OpenAL alGenSources results in AL_INVALID_OPERATION

I'm trying to get to grips with OpenAL, working through a tutorial here: http://benbritten.com/2008/11开发者_如何学编程/06/openal-sound-on-the-iphone/

My problem is that the sound does not play, although there are no iOS errors thrown. There is an OpenAL error though. The code sample below is the body of an IBAction method, and results in an AL_INVALID_OPERATION at alGenSources(1, &sourceID). sourceID reports as NULL.

I've tried this on the device and the simulator.

This code sample seems to be in pretty wide use, but I can't find anybody complaining of this particular problem. Can anybody throw any light on this? Many thanks for any help,

 NSString *audioFileName = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"caf"];
AudioFileID fileID = [self openAudioFile:audioFileName];

UInt32 filesize = [self audioFileSize:fileID];
unsigned char *outData = malloc(filesize);
OSStatus result = noErr;
result = AudioFileReadBytes(fileID, false, 0, &filesize, outData);
AudioFileClose(fileID);

if (result != 0) {
    NSLog(@"Can't load file..");
}

NSUInteger bufferID;
//NSLog(@"bufferID %@", [NSNumber numberWithUnsignedInteger:bufferID]);
alGenBuffers(1, &bufferID);
//NSLog(@"bufferID %@", [NSNumber numberWithUnsignedInteger:bufferID]);
alBufferData(bufferID, AL_FORMAT_STEREO16, outData, filesize, 44100);

[bufferStorageArray addObject:[NSNumber numberWithUnsignedInteger:bufferID]];

alGetError();
ALuint sourceID;
alGenSources(1, &sourceID);

if(alGetError() == AL_INVALID_OPERATION) 
{
    printf("\n++++ Error creating buffers INVALID_OPERATION!!\n");

    //exit(1);
}
else
{
    printf("No errors yet.");
}

alSourcei(sourceID, AL_BUFFER, bufferID);

alSourcef(sourceID, AL_PITCH, 1.0f);
alSourcef(sourceID, AL_GAIN, 1.0f);
if (loops) {
    alSourcei(sourceID, AL_LOOPING, AL_TRUE);
}

[soundDictionary setObject:[NSNumber numberWithUnsignedInt:sourceID] forKey:@"sound"];

if (outData) {
    free(outData);
    outData = NULL;
}

[self playSound:@"sound"];


For your pitch problem, make sure the sound file you are loading matches the sample rate you are feeding into alBufferData. Your caf file is probably saved at 22050 Hz.

AudioStreamBasicDescription's mSampleRate will tell you what the audio file's sample rate really is.

You should also check mChannelsPerFrame to make sure it really is stereo sound.

Also, OpenAL by default on iOS only generates 4 stereo sources. If you try to load more than 4 sources with stereo data, your audio will sound like garbage. You can change that by specifying attributes ALC_STEREO_SOURCES and ALC_MONO_SOURCES when you create a context. You have a maximum of 32 sources (by default it sets up 28 mono and 4 stereo sources).


Stupid mistake on my part - I had initialised OpenAL in initWithNibName, which was never being called. Moving the init into viewDidLoad has got everything working, although playback is chipmunk-style high speed

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜