OpenAL Lesson 2: Looping and Fadeaway(转载)
来源:互联网 发布:来宾用户 安装软件 编辑:程序博客网 时间:2024/06/06 13:24
转自http://www.devmaster.net/articles/openal-tutorials/lesson2.php
Hope you found the last tutorial of some use. I know I did. This will be a real quick and easy tutorial. It won't get too much more complicated at this point.
#include <conio.h>
#include <time.h>
#include <stdlib.h>
#include <al/al.h>
#include <al/alc.h>
#include <al/alu.h>
#include <al/alut.c>
// Buffers hold sound data.
ALuint Buffer;
// Sources are points of emitting sound.
ALuint Source;
// Position of the source sound.
ALfloat SourcePos[] = ...{ 0.0, 0.0, 0.0 };
// Velocity of the source sound.
ALfloat SourceVel[] = ...{ 0.0, 0.0, 0.1 };
// Position of the listener.
ALfloat ListenerPos[] = ...{ 0.0, 0.0, 0.0 };
// Velocity of the listener.
ALfloat ListenerVel[] = ...{ 0.0, 0.0, 0.0 };
// Orientation of the listener. (first 3 elements are "at", second 3 are "up")
ALfloat ListenerOri[] = ...{ 0.0, 0.0, -1.0, 0.0, 1.0, 0.0 };
There is only one change in the code since the last tutorial in this fist section. It is that we altered the sources velocity. It's 'z' field is now 0.1.
ALboolean LoadALData()
...{
// Variables to load into.
ALenum format;
ALsizei size;
ALvoid* data;
ALsizei freq;
ALboolean loop;
// Load wav data into a buffer.
alGenBuffers(1, &Buffer);
if (alGetError() != AL_NO_ERROR)
return AL_FALSE;
alutLoadWAVFile("wavdata/Footsteps.wav", &format, &data, &size, &freq, &loop);
alBufferData(Buffer, format, data, size, freq);
alutUnloadWAV(format, data, size, freq);
// Bind buffer with a source.
alGenSources(1, &Source);
if (alGetError() != AL_NO_ERROR)
return AL_FALSE;
alSourcei (Source, AL_BUFFER, Buffer );
alSourcef (Source, AL_PITCH, 1.0f );
alSourcef (Source, AL_GAIN, 1.0f );
alSourcefv(Source, AL_POSITION, SourcePos);
alSourcefv(Source, AL_VELOCITY, SourceVel);
alSourcei (Source, AL_LOOPING, AL_TRUE );
// Do an error check and return.
if (alGetError() != AL_NO_ERROR)
return AL_FALSE;
return AL_TRUE;
}
Two changes in this section. First we are loading the file "Footsteps.wav". We are also explicitly setting the sources 'AL_LOOPING' value to 'AL_TRUE'. What this means is that when the source is prompted to play it will continue to play until stopped. It will play over again after the sound clip has ended.
void SetListenerValues()
...{
alListenerfv(AL_POSITION, ListenerPos);
alListenerfv(AL_VELOCITY, ListenerVel);
alListenerfv(AL_ORIENTATION, ListenerOri);
}
void KillALData()
...{
alDeleteBuffers(1, &Buffer);
alDeleteSources(1, &Source);
alutExit();
}
Nothing has changed here.
int main(int argc, char *argv[])
...{
// Initialize OpenAL and clear the error bit.
alutInit(NULL,0);
alGetError();
// Load the wav data.
if (LoadALData() == AL_FALSE)
return 0;
SetListenerValues();
// Setup an exit procedure.
atexit(KillALData);
// Begin the source playing.
alSourcePlay(Source);
// Loop
ALint time = 0;
ALint elapse = 0;
while (!kbhit())
...{
elapse += clock() - time;
time += elapse;
if (elapse > 50)
...{
elapse = 0;
SourcePos[0] += SourceVel[0];
SourcePos[1] += SourceVel[1];
SourcePos[2] += SourceVel[2];
alSourcefv(Source, AL_POSITION, SourcePos);
}
}
return 0;
}
The only thing that has changed in this code is the loop. Instead of playing and stopping the audio sample it will slowly get quieter as the sources position grows more distant. We do this by slowly incrementing the position by it's velocity over time. The time is sampled by checking the system clock which gives us a tick count. It shouldn't be necessary to change this, but if the audio clip fades too fast you might want to change 50 to some higher number. Pressing any key will end the loop.
- OpenAL Lesson 2: Looping and Fadeaway(转载)
- OpenAL Lesson 6: Advanced Loading and Error Handles(转载)
- OpenAL Lesson 1: Simple Static Sound(转载)
- OpenAL Lesson 3: Multiple Sources(转载)
- OpenAL Lesson 4: The ALC(转载)
- OpenAL Lesson 5: Sources Sharing Buffers(转载)
- OpenAL Lesson 7: The Doppler Effect(转载)
- OpenAL Lesson 8: OggVorbis Streaming Using The Source Queue (转载)
- Chapter 02 Looping and counting
- [转载]OpenAL Tutorial 1
- Lesson 2 Matrices and vectors
- New Concept English 2 -- Lesson 1 and Lesson 2
- New Concept English 2 -- Lesson 3 and Lesson 4
- openal
- OpenAL
- Lesson 2: Triangle rasterization and back face culling
- Lesson 2: The Quartz API, Jobs And Triggers
- Codeforces Round #430 (Div. 2) D. Vitya and Strange Lesson
- OpenAL Lesson 1: Simple Static Sound(转载)
- ARM学习笔记(二)
- 使用 PreparedStatement 进行模糊查询
- XFire的下一代产品CXF的入门(二) - 与Spring的集成
- 日语单词
- OpenAL Lesson 2: Looping and Fadeaway(转载)
- OpenAL Lesson 3: Multiple Sources(转载)
- 在两个ASP.NET页面之间传递值
- Solaris安装
- OpenAL Lesson 4: The ALC(转载)
- 软件设计师专题: 算法分析与设计
- ftp
- 在.net 1.x 中, 实现多语言功能, by shawl.qiu
- ASP.Net中自定义Http处理及应用之HttpHandler篇