剑痴乎

  • 首页
  • 文章分类
    • 音视频
    • WebRTC
    • 编程之美
    • Linux
    • Windows
    • 生活点滴
    • 校园生活
  • 参考
    • API参考
    • 实用工具
    • 测试音视频
    • 文档
  • 留言板
  • 关于
剑痴乎
代码为剑,如痴如醉
  1. 首页
  2. 编程之美
  3. 正文

Ogre中加入声音

2014年4月17日 1607点热度 0人点赞 1条评论

没有声音会觉得很乏味,不够生动形象,更何况是一个3D漫游系统呢。所以今天给自己的系统加入了声音效果。由于Ogre只是个图像渲染引擎,加入声音的话得加入第三方库,对比了一些音频库,最后决定使用irrKlang这个音频库,跨平台,支持3D音效,使用起来也不是太难。

安装配置
首先到http://ambiera.com/irrklang/downloads.html下载SDK,解压到Ogre SDK目录中(或者其他目录,放在Ogre目录下方便些),我下的是irrKlang-1.4.0版本,此时Ogre SDK就有了一个名为irrKlang-1.4.0的文件夹。

接着需要配置你的工程,VS中右键项目——>选择属性,找到左侧C/C++下的常规,在附加包含目录中加入$(OGRE_HOME)\irrKlang-1.4.0\include(前提是配置过环境变量$(OGRE_HOME)为Ogre SDK目录)。

然后到irrKlang-1.4.0/lib/Win32-visualStudio下找到irrKlang.lib文件放到OgreSDK\lib\release下,irrKlang.exp放到OgreSDK\lib\debug目录下,也可以右键项目——>属性——>左侧的链接器-常规下的附加库目录中加入$(OGRE_HOME)\irrKlang-1.4.0\lib\Win32-visualStudio,然后到irrKlang-1.4.0/bin/dotnet-1.1下找到所需dll文件放到你的项目下的debug与release文件夹,至此环境就配置好了。

使用
使用的话很简单,在你声明的类前加

1
2
3
#include <irrKlang.h>
using namespace irrklang;
#pragma comment(lib, "irrKlang.lib") // link with irrKlang.dll

然后使用

1
2
ISoundEngine* engine = createIrrKlangDevice();
engine->play2D("sample.ogg", true);

就可以播放了相应声音文件了。

简单示例如下(只有一个main.cpp):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// This example will show how to play sounds using irrKlang.
// It will play a looped background music and a sound every
// time the user presses a key.
 
// At the beginning, we need to include the irrKlang headers (irrKlang.h) and
// the iostream headers needed to print text to the console.
 
#include <stdio.h>
#include <irrKlang.h>
 
// include console I/O methods (conio.h for windows, our wrapper in linux)
#if defined(WIN32)
#include <conio.h>
#else
#include "../common/conio.h"
#endif
 
// Also, we tell the compiler to use the namespaces 'irrklang'.
// All classes and functions of irrKlang can be found in the namespace 'irrklang'.
// If you want to use a class of the engine,
// you'll have to type an irrklang:: before the name of the class.
// For example, to use the ISoundEngine, write: irrklang::ISoundEngine. To avoid having
// to put irrklang:: before of the name of every class, we tell the compiler that
// we use that namespaces here.
 
using namespace irrklang;
 
// To be able to use the irrKlang.dll file, we need to link with the irrKlang.lib.
// We could set this option in the project settings, but to make it easy we use
// a pragma comment:
 
#pragma comment(lib, "irrKlang.lib") // link with irrKlang.dll
 
// Now lets start with irrKlang 3D sound engine example 01, demonstrating simple 2D sound.
// Start up the sound engine using createIrrKlangDevice(). You can specify several
// options as parameters when invoking that function, but for this example, the default
// parameters are enough.
int main(int argc, const char** argv)
{
// start the sound engine with default parameters
ISoundEngine* engine = createIrrKlangDevice();
 
if (!engine)
{
printf("Could not startup engine\n");
return 0; // error starting up the engine
}
 
// To play a sound, we only to call play2D(). The second parameter
// tells the engine to play it looped.
 
// play some sound stream, looped
engine->play2D("../../media/getout.ogg", true);
 
// In a loop, wait until user presses 'q' to exit or another key to
// play another sound.
 
printf("\nHello World!\n");
 
do
{
printf("Press any key to play some sound, press 'q' to quit.\n");
 
// play a single sound
engine->play2D("../../media/bell.wav");
}
while(getch() != 'q');
 
// After we are finished, we have to delete the irrKlang Device created earlier
// with createIrrKlangDevice(). Use ::drop() to do that. In irrKlang, you should
// delete all objects you created with a method or function that starts with 'create'.
// (an exception is the play2D()- or play3D()-method, see the documentation or the
// next example for an explanation)
// The object is deleted simply by calling ->drop().
 
engine->drop(); // delete engine
return 0;
}

详细的使用大家可以去irrKlang官网上查看。

本作品采用 知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议 进行许可
标签: C++ OGRE
最后更新:2022年3月27日

Jeff

管理员——代码为剑,如痴如醉

打赏 点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理。

相关文章
  • 网络字节转换到本地字节的函数模板
  • MFC自绘带背景颜色标题栏
  • VC++获取本机IP地址
  • FLTK程序编译错误
  • C++实现windows重启

COPYRIGHT © 2025 jianchihu.net. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang