OpenAL设置固定量

时间:2014-07-22 16:36:10

标签: ios iphone sprite-kit openal

我正在使用OpenAL在游戏中播放背景音乐和声音效果,播放背景音乐的代码就是这样;

- (void) playSoundBack
{


alGenSources(1, &sourceIDBack);

NSString *audioFilePath = [[NSBundle mainBundle] pathForResource:@"music" ofType:@"wav"];
NSURL *audioFileURL = [NSURL fileURLWithPath:audioFilePath];

AudioFileID afid;
OSStatus openAudioFileResult = AudioFileOpenURL((__bridge CFURLRef)audioFileURL, kAudioFileReadPermission, 0, &afid);

if (0 != openAudioFileResult)
{
    NSLog(@"An error occurred when attempting to open the audio file %@: %ld", audioFilePath, openAudioFileResult);
    return;
}

UInt64 audioDataByteCount = 0;
UInt32 propertySize = sizeof(audioDataByteCount);
OSStatus getSizeResult = AudioFileGetProperty(afid, kAudioFilePropertyAudioDataByteCount, &propertySize, &audioDataByteCount);

if (0 != getSizeResult)
{
    NSLog(@"An error occurred when attempting to determine the size of audio file %@: %ld", audioFilePath, getSizeResult);
}

UInt32 bytesRead = (UInt32)audioDataByteCount;

void *audioData = malloc(bytesRead);

OSStatus readBytesResult = AudioFileReadBytes(afid, false, 0, &bytesRead, audioData);

if (0 != readBytesResult)
{
    NSLog(@"An error occurred when attempting to read data from audio file %@: %ld", audioFilePath, readBytesResult);
}

AudioFileClose(afid);

ALuint outputBuffer;
alGenBuffers(1, &outputBuffer);

alBufferData(outputBuffer, AL_FORMAT_STEREO16, audioData, bytesRead, 44100);

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


alSourcef(sourceIDBack, AL_GAIN, 0.1f);

alSourcei(sourceIDBack, AL_BUFFER, outputBuffer);
alSourcei(sourceIDBack, AL_LOOPING, AL_TRUE);
alSourcePlay(sourceIDBack);

}

使用另一个类似的代码块,我会在游戏中发挥一些效果。所有声音都播放但我有音量问题...背景音乐音量变化,似乎不是一个绝对值......当其他效果正在播放时,背景音乐音量很低,因为我已经设置了AL_GAIN,但是当没有其他效果播放时,背景音乐声音太大(或者至少这是我的感觉......)。为什么音乐有这种行为?像0.1这样的音量设置是相对的吗?可以设置固定和通用值吗?

0 个答案:

没有答案