音频引擎IrrKlang学习笔记01:播放控制与音效设置

来源:互联网 发布:java应用程序的入口 编辑:程序博客网 时间:2024/05/16 05:31

本系列与FMOD系列、Irrlicht系列是并列的。

关于IrrKlang,可以在早先的几篇博文中找到踪迹。

Irrlicht例程04:创造一个有声世界(上) 

Irrlicht例程05:创造一个有声世界(下)

鬼火(irrlicht)的复燃

这几篇博文中已经涉及到IrrKlang的使用,但是本系列是专门研究Audio处理的,因此独立出来。

同时也方便与FMOD等进行对比。

与FMOD相比,个人觉得IrrKlang更加易用,首先它是开放源码的,其次因为它的代码简洁,框架简单。

如果是和Irrlicht配合,IrrKlang作为“亲儿子”无疑更好。

但从专业角度,FMOD支持更高级的处理,因此许多大型游戏都使用了FMOD作为游戏音频的驱动。

下面贴一个完整的示例,这个示例是根据官方示例代码修改的,具体代码如下

#include <stdio.h>#include <stdlib.h>#include <irrKlang.h>using namespace irrklang;#pragma comment(lib, "irrKlang.lib") #pragma warning(disable:4996)#define PATHLEN 256int main(void){system("color 2e");system("title Audio Mixer -- Archieved by IrrKlang");printf("Input audio file name:");char fileName[PATHLEN] = { 0 };scanf("%s", fileName);getchar();ISoundEngine* engine = createIrrKlangDevice(ESOD_AUTO_DETECT, ESEO_DEFAULT_OPTIONS);if (engine == NULL){printf("Failed to create the engine!\n");printf("Press ENTER to continue...");getchar();return -1;}ISound* sound = engine->play2D(fileName, true, true, false, ESM_AUTO_DETECT, true);if (sound == NULL){printf("Failed to play \'%s\'\n", fileName);printf("Press ENTER to continue...");engine->drop();getchar();return -2;}ISoundEffectControl* fx = sound->getSoundEffectControl();if (fx == NULL){printf("Failed to get sound effect control!\n");sound->drop();engine->drop();getchar();return -3;}float volume = 0.5;sound->setVolume(0.8);sound->setIsPaused(false);bool paused = false;bool efx_c = false;bool efx_e = false;bool efx_d = false;char key = 0;while (true){system("cls");printf("------------------------------------------\n");printf("| Sound File: \'%s\'\n", fileName);printf("------------------------------------------\n");if (paused) printf("| P: Resume\n");else printf("|  P: Pause\n");if (efx_c) printf("| C: Disable Chorus Effect\n");else printf("|  C: Enable Chorus Effect\n");if (efx_e) printf("| E: Disable Echo Effect\n");else printf("|  E: Enable Echo Effect\n");if (efx_d) printf("| D: Disable Distortion Effect\n");else printf("|  D: Enable Distortion Effect\n");printf("| --- Current Volume: %.1f\n", volume);printf("| A: Volume +0.1 (max 1.0)\n");printf("| B: Volume -0.1 (min 0.1)\n");printf("| Q: Stop and Quit\n");printf("------------------------------------------\n");printf("Input your command:");key = getchar();getchar();if (key == 'q' || key == 'Q') break;switch (key){case 'p':case 'P':paused =  !paused;sound->setIsPaused(paused);break;case 'c':case 'C':efx_c = !efx_c;if (efx_c){fx->enableChorusSoundEffect();}else{fx->disableChorusSoundEffect();}break;case 'e':case 'E':efx_e = !efx_e;if (efx_e){fx->enableEchoSoundEffect();}else{fx->disableEchoSoundEffect();}break;case 'd':case 'D':efx_d = !efx_d;if (efx_d){fx->enableDistortionSoundEffect();}else{fx->disableDistortionSoundEffect();}break;case 'a':case 'A':volume += 0.1f;if (volume > 1.0f) volume = 1.0f;sound->setVolume(volume);break;case 'b':case 'B':volume -= 0.1f;if (volume < 0.0f) volume = 0.1f;sound->setVolume(volume);break;default:break;}}sound->drop();engine->drop();}

运行截图如下

初始状态:音量50%,增加至100%,无任何特效


降低音量至70%,开启“扭曲”效果


接下来贴一张使用FMOD的截图


将本文代码与使用FMOD进行Audio处理的代码相比较可以发现,IrrKlang确实更加简洁。

如需参看使用FMOD的代码,可以点击下方链接跳转:

音频引擎FMOD学习记录02:控制暂停/继续,调节音量,设置声效

而且IrrKlang每次对Sound做更改后无需执行update()操作,这一点比FMOD要好得多。


本文原创,博文地址

http://blog.csdn.net/fengyhack/article/details/43488447



0 0
原创粉丝点击