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 (将#修改为@)