播放声音的基本 OpenAL 代码

发布于 2025-01-01 22:27:11 字数 2631 浏览 1 评论 0原文

我是 OpenAl 库的新手。我正在遵循 OpenAl 编程指南,但我找不到。

我从 OpenAl 编程指南第 10 页中提取了这段代码,但仍然没有声音。我使用 OSX Snow Leopard,我知道 OSX 没有定义 ALUT。

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>

#include <OpenAL/al.h>
#include <OpenAL/alc.h>

using namespace std;

#define NUM_BUFFERS 3
#define BUFFER_SIZE 4096

int main(int argc, char **argv)
{
    ALCdevice *dev;
    ALCcontext *ctx;
    struct stat statbuf;

    Aluint buffer[NUM_BUFFERS];
    Aluint source[NUM_SOURCES];

    ALsizei size, freq;
    ALenum format;
    ALvoid *data;

    // Initialization 
    dev = alcOpenDevice(NULL); // select the "preferred dev" 

    if (dev) 
    { 
        ctx = alcCreateContext(dev,NULL); 
        alcMakeContextCurrent(ctx);  
    } 
    // Check for EAX 2.0 support 
    // g_bEAX = alIsExtensionPresent("EAX2.0");

    // Generate Buffers 
    alGetError(); // clear error code 
    alGenBuffers(NUM_BUFFERS, buffer); 
    if ((error = alGetError()) != AL_NO_ERROR) 
    { 
        DisplayALError("alGenBuffers :", error); 
        return 1; 
    } 
    // Load test.wav 
    loadWAVFile("sample.wav", &format, &data, &size, &freq, &loop); 
    if ((error = alGetError()) != AL_NO_ERROR) 
    { 
        DisplayALError("LoadWAVFile sample.wav : ", error); 
        alDeleteBuffers(NUM_BUFFERS, buffer); 
     return 1; 
    }

    // Copy test.wav data into AL Buffer 0 
    alBufferData(buffer[0], format, data, size, freq); 
    if ((error = alGetError()) != AL_NO_ERROR) 
    { 
     DisplayALError("alBufferData buffer 0 : ", error); 
     alDeleteBuffers(NUM_BUFFERS, buffer); 
     return 1; 
    } 

    // Unload test.wav 
    unloadWAV(format, data, size, freq); 
    if ((error = alGetError()) != AL_NO_ERROR) 
    { 
        DisplayALError("UnloadWAV : ", error); 
        alDeleteBuffers(NUM_BUFFERS, buffer); 
        return 1; 
    } 
    // Generate Sources 
    alGenSources(1, source); 
    if ((error = alGetError()) != AL_NO_ERROR) 
    { 
        DisplayALError("alGenSources 1 : ", error); 
        return 1; 
    } 
    // Attach buffer 0 to source
    alSourcei(source[0], AL_BUFFER, buffer[0]); 
    if ((error = alGetError()) != AL_NO_ERROR) 
    { 
        DisplayALError("alSourcei AL_BUFFER 0 : ", error); 
    } 

    // Exit 
    ctx = alcGetCurrentContext(); 
    dev = alcGetContextsDevice(ctx); 
    alcMakeContextCurrent(NULL); 
    alcDestroyContext(ctx); 
    alcCloseDevice(dev);

    return 0;
}

为了使这段代码正常工作,我错过了哪些事情??? 我做错了什么???

任何建议都可以帮助,谢谢。

I'm new using OpenAl library. I'm following the OpenAl programming guide but i can't find.

I have this code extracted from page 10 of the OpenAl programming guide but still have no sound. I use OSX Snow Leopard, i know OSX doesn't have ALUT defined.

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>

#include <OpenAL/al.h>
#include <OpenAL/alc.h>

using namespace std;

#define NUM_BUFFERS 3
#define BUFFER_SIZE 4096

int main(int argc, char **argv)
{
    ALCdevice *dev;
    ALCcontext *ctx;
    struct stat statbuf;

    Aluint buffer[NUM_BUFFERS];
    Aluint source[NUM_SOURCES];

    ALsizei size, freq;
    ALenum format;
    ALvoid *data;

    // Initialization 
    dev = alcOpenDevice(NULL); // select the "preferred dev" 

    if (dev) 
    { 
        ctx = alcCreateContext(dev,NULL); 
        alcMakeContextCurrent(ctx);  
    } 
    // Check for EAX 2.0 support 
    // g_bEAX = alIsExtensionPresent("EAX2.0");

    // Generate Buffers 
    alGetError(); // clear error code 
    alGenBuffers(NUM_BUFFERS, buffer); 
    if ((error = alGetError()) != AL_NO_ERROR) 
    { 
        DisplayALError("alGenBuffers :", error); 
        return 1; 
    } 
    // Load test.wav 
    loadWAVFile("sample.wav", &format, &data, &size, &freq, &loop); 
    if ((error = alGetError()) != AL_NO_ERROR) 
    { 
        DisplayALError("LoadWAVFile sample.wav : ", error); 
        alDeleteBuffers(NUM_BUFFERS, buffer); 
     return 1; 
    }

    // Copy test.wav data into AL Buffer 0 
    alBufferData(buffer[0], format, data, size, freq); 
    if ((error = alGetError()) != AL_NO_ERROR) 
    { 
     DisplayALError("alBufferData buffer 0 : ", error); 
     alDeleteBuffers(NUM_BUFFERS, buffer); 
     return 1; 
    } 

    // Unload test.wav 
    unloadWAV(format, data, size, freq); 
    if ((error = alGetError()) != AL_NO_ERROR) 
    { 
        DisplayALError("UnloadWAV : ", error); 
        alDeleteBuffers(NUM_BUFFERS, buffer); 
        return 1; 
    } 
    // Generate Sources 
    alGenSources(1, source); 
    if ((error = alGetError()) != AL_NO_ERROR) 
    { 
        DisplayALError("alGenSources 1 : ", error); 
        return 1; 
    } 
    // Attach buffer 0 to source
    alSourcei(source[0], AL_BUFFER, buffer[0]); 
    if ((error = alGetError()) != AL_NO_ERROR) 
    { 
        DisplayALError("alSourcei AL_BUFFER 0 : ", error); 
    } 

    // Exit 
    ctx = alcGetCurrentContext(); 
    dev = alcGetContextsDevice(ctx); 
    alcMakeContextCurrent(NULL); 
    alcDestroyContext(ctx); 
    alcCloseDevice(dev);

    return 0;
}

What things I missed to make this code work ???
What i'm doing wrong ???

Any advice could help, thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你列表最软的妹 2025-01-08 22:27:11

您没有调用 alSourcePlay(source[0]) 来开始播放。

You are not calling alSourcePlay(source[0]) to start the playback.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文