如何解决openAL 缓冲区相互播放
我是音频编程的新手,尤其是 openAL。我遇到了音频样本播放速度过快的问题。这已经够糟糕了,但是当我将几个音频缓冲区排队时,它们会相互播放。
这是相关的函数:
AudioSystem* initAudioSystem(){
AudioSystem* audioSystem = malloc(sizeof(AudioSystem));
ALCdevice* device;
device = alcopenDevice(NULL);
if(!device){
// handle errors
}
ALCcontext* context;
context = alcCreateContext(device,NULL);
if(!alcMakeContextCurrent(context)){
// handle errors
}
ALfloat listenerOri[] = { 0.0f,0.0f,1.0f,0.0f };
alListener3f(AL_POSITION,1.0f);
// check for errors
alListener3f(AL_VELociTY,0);
// check for errors
alListenerfv(AL_ORIENTATION,listenerOri);
// check for errors
alGenSources((ALuint)1,audioSystem->sources);
// check for errors
alSourcef(audioSystem->sources[0],AL_PITCH,1);
// check for errors
alSourcef(audioSystem->sources[0],AL_GAIN,1);
// check for errors
alSource3f(audioSystem->sources[0],AL_POSITION,0);
// check for errors
alSource3f(audioSystem->sources[0],AL_VELociTY,0);
// check for errors
alSourcei(audioSystem->sources[0],AL_LOOPING,AL_FALSE);
// check for errros
alGenBuffers((ALuint)4,audioSystem->buffers);
// check for errors
drmp3* mp3 = malloc(sizeof(drmp3));
if (!drmp3_init_file(mp3,"game_data/assets/music/spacetime.mp3",NULL)) {
// Failed to open file
}
int frameCount = 65536 * 8;
drmp3_int16* data = malloc(frameCount * mp3->channels * sizeof(drmp3_int16));
//printf("frames read: %u\nchannels: %u\nsample rate: %u",(unsigned int) framesRead,mp3->channels,mp3->sampleRate);
for(int i = 0; i < 4; i++){
drmp3_uint64 framesRead = drmp3_read_pcm_frames_s16(mp3,frameCount,data);
alBufferData(audioSystem->buffers[i],AL_FORMAT_STEREO16,data,mp3->sampleRate);
//printf("frames read: %u\nframe count: %i\nchannels: %u\nsample rate: %u\n",mp3->sampleRate);
}
alSourceQueueBuffers(audioSystem->sources[0],4,audioSystem->buffers);
alSourcePlay(audioSystem->sources[0]);
drmp3_free(mp3,NULL);
drmp3_free(data,NULL);
// unsigned int channels;
// unsigned int sampleRate;
// drwav_uint64 totalPCMFrameCount;
// drwav_int16* data = drwav_open_file_and_read_pcm_frames_s16("game_data/assets/sfx/Collect_Point_00.wav",&channels,&sampleRate,&totalPCMFrameCount,NULL);
// if (data == NULL) {
// // Error opening and reading WAV file.
// }
// alBufferData(audioSystem->buffers[0],AL_FORMAT_MONO16,totalPCMFrameCount,sampleRate);
// alSourcei(audioSystem->sources[0],AL_BUFFER,audioSystem->buffers[0]);
// drwav_free(data,NULL);
audioSystem->context = context;
//audioSystem->mp3 = mp3;
return audioSystem;
}
我正在使用 the drwav and drmp3 libraries 加载音频文件。我敢肯定这只是某个地方的一个小疏忽。预先感谢您的帮助!
编辑:我想通了!当 alBufferData 想要缓冲区的总大小时,我将 frameCount(样本数)传递给了 alBufferData!现在它完美运行,希望这对其他人有所帮助。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。