JCHub

  • Home
  • Category
    • A/V
    • WebRTC
    • Beauty of Programming
    • Linux
    • Windows
    • Moments of Life
    • Campus Life
  • Reference
    • API Reference
    • Utilities
    • AV Test
    • Doc
  • Message Board
  • About
JCHub
Code as My Sword, Lost in Obsession
  1. Main page
  2. Beauty of Programming
  3. Main content

Ogre中加入声音

2014年4月17日 1918hotness 0likes 1comments

没有声音会觉得很乏味,不够生动形象,更何况是一个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官网上查看。

This article is licensed with Creative Commons Attribution-NonCommercial-No Derivatives 4.0 International License
Tag: C++ OGRE
Last updated:2022年3月27日

Jeff

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

Tip the author Like
< Last article
Next article >

Comments

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Posts
  • Nodejs C++ addon开发简介
  • 网络字节转换到本地字节的函数模板
  • MFC自绘带背景颜色标题栏
  • VC++获取本机IP地址
  • FLTK程序编译错误
Categories

COPYRIGHT © 2026 jianchihu.net. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang