我最近一直致力于将我的游戏移植到跨平台,并决定使用OpenAL作为我的跨平台音频引擎。
我有16个“通道”(OpenAL源),可同时播放多达16个声音。要播放声音,我会切换哪个缓冲区链接到给定的源,还设置增益,源位置等,以便在给定的“通道”(源)中播放声音。
问题是,我注意到我的“增益”设置似乎没有立即生效。例如,如果一个响亮的“闪电”声音在给定的声源中以0.5增益播放,那么当我有一个按钮点击声音在0.15增益后播放时,此点击开始FAR太大声。然后,每次播放时,音量都会减小,直到第3或第4次点击,听起来好像是在正确的0.15增益附近。
第一次点击按钮几乎听不见,它似乎在音量上升,直到达到0.15增益。
简而言之,“源”似乎是在记住以前的增益设置,即使我在同一个源中播放新声音之前重置了这些设置!这是一个错误吗?或者我对OpenAL不了解的事情?如何让它“立即”更改为新的增益/位置设置?
播放声音的相关代码:
[Channel是介于0和15之间的值,soundID是gBuffer数组的有效索引,stereoPosition是介于-255和255之间的整数,而volume是介于0到255之间。这来自一个函数,它是一个包装器我以前使用0到255之间的值的游戏,所以它将值转换为正确的OpenAL值。]
// Stop any sound currently playing in this channel ("source")
alSourceStop( gSource[channel] );
// What sound effect ("buffer") is currently linked with this channel? (Even if not currently playing)
alGetSourcei( gSource[channel], AL_BUFFER, &curBuffer );
// attach a different buffer (sound effect) to the source (channel) only if it's different than the previously-attached one.
// (Avoid error code by changing it only if it's different)
if (curBuffer != gBuffer[soundID])
alSourcei( gSource[channel], AL_BUFFER, gBuffer[soundID] );
// Loop the sound?
alSourcei( gSource[channel], AL_LOOPING, (loopType == kLoopForever) );
// For OpenAL, we do this BEFORE starting the sound effect, to avoid sudden changes a split second after it starts!
volume = (volume / 2) + 1; // Convert from 0-255 to 0-128
{
float sourcePos[3] = {(float)stereoPosition / 50, 0.0, 2.0};
// Set Source Position
alSourcefv( gSource[channelNum], AL_POSITION, sourcePos ); // fv = float vector
// Set source volume
alSourcef( gSource[channelNum], AL_GAIN, (float)newVolume / 255 );
}
// Start playing the sound!
alSourcePlay( gSource[channel] );
如果需要,我也可以发布设置代码,但没有什么花哨的。只需致电
alSourcef(gSource [n],AL_REFERENCE_DISTANCE,5.0f);
每个来源。