openal在vs2010中的配置

 

下载openal开发工具:相关资料可以在OpenAL官网http://connect.creativelabs.com/openal/default.aspx上获得。这里下载的SDK为OpenAL11CoreSDK

安装运行:OpenAL11CoreSDK.exe,并将安装后的目录下的文件夹:include中的头文件复制到vs2010项目工程文件下;libs目录下的OpenAL32.lib(文件可能存在于win64目录下或者win32目录下,找到与pc系统相应的目录中文件)复制到vs2010安装目录中的vc/libs目录中。

配置vs2010:点击“项目”→“属性”→“配置属性”→“链接器”→“常规”,在“附加库目录”中添:OpenAL32.lib,即从openal安装目录路径下拷贝到vs2010安装目录中的那个文件。

编程实现:

 1 // openal_test.cpp : 定义控制台应用程序的入口点。
 2 //
 3 
 4 #include "stdafx.h"
 5 #include "al.h" // for glXzzz
 6 #include "alc.h" // for glcXzzz
 7 #include <stdio.h> // for printf
 8 #include <math.h> // for cosf
 9 
10 #define SHRT_MAX 8192
11 
12 ALuint Source;// 用于播放声音
13 ALuint Buffer;// 声音数据
14 
15 bool InitOpenAL()
16 {
17     ALCdevice * pDevice = alcOpenDevice(NULL);  // 打开默认音频设备
18     ALCcontext * pContext = alcCreateContext(pDevice, NULL);
19     alcMakeContextCurrent(pContext);
20     return true;
21     // 注意:这里没有进行失败检测,仅用于示例
22 }
23 
24 void ShutdownOpenAL()
25 {
26     ALCcontext *pContext;
27     ALCdevice *pDevice;
28 
29     pContext = alcGetCurrentContext();
30     pDevice = alcGetContextsDevice(pContext);
31     
32     alcMakeContextCurrent(NULL);
33     alcDestroyContext(pContext);
34     alcCloseDevice(pDevice);
35 }
36 
37 bool LoadData()
38 {
39     // 载入变量.
40     const ALsizei size = 800;
41     ALsizei freq = 8000;
42     ALboolean loop = 1; // 循环播放
43     // 使用一段正弦波作数据
44     short data[800];
45     alGenBuffers(1, &Buffer);
46     float max = SHRT_MAX / 4;
47     float rad = 0;
48     for(int  e=0 ; e<800;e++)
49     {
50         data[e] = (short)(max * cosf(rad));
51         rad += 1.f;
52     }
53     // 载入WAV数据
54     alBufferData(Buffer, AL_FORMAT_MONO16, data, size, freq);
55     alGenSources(1, &Source);
56     
57     // 源声音的位置
58     ALfloat SourcePos[] = { 0.0, 0.0, 0.0 };
59     // 源声音的速度
60     ALfloat SourceVel[] = { 0.0, 0.0, 0.0 };
61 
62     alSourcei (Source, AL_BUFFER,   Buffer   );
63     alSourcef (Source, AL_PITCH,    1.0f     );
64     alSourcef (Source, AL_GAIN,     1.0f     );
65     alSourcefv(Source, AL_POSITION, SourcePos);
66     alSourcefv(Source, AL_VELOCITY, SourceVel);
67     alSourcei (Source, AL_LOOPING,  loop     );
68 
69     return true;
70 }
71 void UnloadData()
72 {
73     alDeleteBuffers(1, &Buffer);
74     alDeleteSources(1, &Source);
75 }
76 
77 void Play()
78 {
79     // 播放
80     alSourcePlay(Source);
81     printf("Press Enter To Stop Sound\n");
82     getchar();
83     alSourceStop(Source);
84 }
85 
86 int main(int argc, char *argv[])
87 {    
88     InitOpenAL();                            // 初始化openal    
89     LoadData();                                 // 载入WAV数据
90     Play();                                            // 播放
91     UnloadData();                             // 卸载WAV数据
92     ShutdownOpenAL();                // 关闭openal
93     return 0;
94 }

(备注:代码来自友博客......粘贴在此,纯粹为了个人理解,再次谢谢共享的原著)

 

posted @ 2016-05-02 20:43  e-data  阅读(825)  评论(0编辑  收藏  举报