OpenAL Lesson 1: Simple Static Sound(转载)

转载 2007年10月14日 19:45:00

转自http://www.devmaster.net/articles/openal-tutorials/lesson1.php

Welcome to the exciting world of OpenAL! OpenAL is still in a stage of growth, and even though there is an ever larger following to the API it still hasn't reached it's full potential. One of the big reasons for this is that there is still not yet hardware acceleration built in for specific cards. However, Creative Labs is a major contributor to the OpenAL project and also happens to be one of the largest soundcard manufacturers. So there is a promise of hardware accelerated features in the near future. OpenAL's only other major contributor, Loki, has gone the way of the dinosaur. So the future of OpenAL on Linux platforms is uncertain. You can still obtain the Linux binaries on some more obscure websites.

OpenAL has also not been seen in many major commercial products, which may have also hurt it's growth. As far as I know the only pc game to use OpenAL has been Metal Gear 2 (although recently I've discovered that Unreal 2 does as well). The popular modeling program, Blender3D, was also known to use OpenAL for all it's audio playback. Aside from these however the only other OpenAL uses have been in the sdk examples and a few obscure tutorials on the internet.

But lets face it, OpenAL has a lot of potential. There are many other audio libraries that claim to work with the hardware on a lower level (and this may be true), but the designers of OpenAL did several things in it's design which make it a superior API. First of all they emulated the OpenGL API which is one of the best ever designed. The API style is flexible, so different coding methods and hardware implementations will take advantage of this. People who have had a lot of experience with OpenGL will be able to pick up OpenAL quite fast. OpenAL also has the advantage of creating 3D surround sound which a lot of other API's cannot boast. On top of all of that it also has the ability to extend itself into EAX and AC3 flawlessly. To my knowledge no other audio library has that capability.

If you still haven't found a reason here to use OpenAL then here's another. It's just cool. It's a nice looking API and will integrate well into your code. You will be able to do many cool sound effects with it. But before we do that we have to learn the basics.

So let's get coding!

#include <conio.h>
#include 
<stdlib.h>
#include 
<al/al.h>
#include 
<al/alc.h>
#include 
<al/alu.h>
#include 
<al/alut.h>

You will notice there are similarities between the OpenAL headers and the OpenGL headers. There is an "al.h", "alu.h", and "alut.h" just like "gl.h", "glu.h", and "glut.h", but there is also an "alc.h". The Alc stands for Audio Library Context and it handles the sound devices across different platforms. It also handles situations where you would want to share a device over several windows. If you want to liken Alc to anything in OpenGL, it's probably most like the wgl and glX extensions. We won't go much into Alc since the Alut library handles it internally for us, and for now we'll just let it be. We will also not use Alu very much since it only provides some math functionality which we will not need for small demo's.

// Buffers hold sound data.
ALuint Buffer;

// Sources are points emitting sound.
ALuint Source;

Those familiar with OpenGL know that it uses "texture objects" (or "texture names") to handle textures used by a program. OpenAL does a similar thing with audio samples. There are essentially 3 kinds of objects in OpenAL. A buffer which stores all the information about how a sound should be played and the sound data itself, and a source which is a point in space that emits a sound. It's important to understand that a source is not itself an audio sample. A source only plays back sound data from a buffer bound to it. The source is also given special properties like position and velocity.

The third object which I have not mentioned yet is the listener. There is only one listener which represents where 'you' are, the user. The listener properties along with the source properties determine how the audio sample will be heard. For example their relative positions will determine the intensity of the sound.

// Position of the source sound.
ALfloat SourcePos[] = ...0.00.00.0 };

// Velocity of the source sound.
ALfloat SourceVel[] = ...0.00.00.0 };


// Position of the listener.
ALfloat ListenerPos[] = ...0.00.00.0 };

// Velocity of the listener.
ALfloat ListenerVel[] = ...0.00.00.0 };

// Orientation of the listener. (first 3 elements are "at", second 3 are "up")
ALfloat ListenerOri[] = ...0.00.0-1.0,  0.01.00.0 };

In the above code we specify the position and velocity of the source and listener objects. These arrays are vector based Cartesian coordinates. You could easily build a structure or class to do the same thing. In this example I used arrays for simplicity.

ALboolean LoadALData()
...{
    
// Variables to load into.

    ALenum format;
    ALsizei size;
    ALvoid
* data;
    ALsizei freq;
    ALboolean loop;

Here we will create a function that loads all of our sound data from a file. The variables are necessary to store some information that Alut will be giving us.

// Load wav data into a buffer.
    alGenBuffers(1&Buffer);
    
if (alGetError() != AL_NO_ERROR)
        
return AL_FALSE;

    alutLoadWAVFile(
"wavdata/FancyPants.wav"&format, &data, &size, &freq, &loop);
    alBufferData(Buffer, format, data, size, freq);
    alutUnloadWAV(format, data, size, freq);

The function 'alGenBuffers' will create the buffer objects and store them in the variable we passed it. It's important to do an error check to make sure everything went smoothly. There may be a case in which OpenAL could not generate a buffer object due to a lack of memory. In this case it would set the error bit.

The Alut library is very helpful here. It opens up the file for us and gives us all the information we need to create the buffer. And after we have attached all this data to the buffer it will help use dispose of the data. It all works in a clean and efficient manner.

    
// 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,  loop     );

We generate a source object in the same manner we generated the buffer object. Then we define the source properties that it will use when it's in playback. The most important of these properties is the buffer it should use. This tells the source which audio sample to playback. In this case we only have one so we bind it. We also tell the source it's position and velocity which we defined earlier.

One more thing on 'alGenBuffers' and 'alGenSources'. In some example code I have seen these functions will return an integer value for the number of buffers/sources created. I suppose this was meant as an error checking feature that was left out in a later version. If you see this done in other code don't use it yourself. If you want to do this check, use 'alGetError' instead (like we have done above).

    
// Do another error check and return.
    if (alGetError() == AL_NO_ERROR)
        
return AL_TRUE;

    
return AL_FALSE;

To end the function we just do one more check to make sure all is well, then we return success.

void SetListenerValues()
...{
    alListenerfv(AL_POSITION,    ListenerPos);
    alListenerfv(AL_VELOCITY,    ListenerVel);
    alListenerfv(AL_ORIENTATION, ListenerOri);
}

We created this function to update the listener properties.

void KillALData()
...{
    alDeleteBuffers(
1&Buffer);
    alDeleteSources(
1&Source);
    alutExit();
}

This will be our shutdown procedure. It is necessary to call this to release all the memory and audio devices that our program may be using.

int main(int argc, char *argv[])
...{
    
// Initialize OpenAL and clear the error bit.

    alutInit(
&argc, argv);
    alGetError();
// Load the wav data.
    if (LoadALData() == AL_FALSE)
        
return -1;

    SetListenerValues();

    
// Setup an exit procedure.
    atexit(KillALData);

We will check to see if the wav files loaded correctly. If not we must exit the program. Then we update the listener values, and finally we set our exit procedure.

    
   ALubyte c = ' ';

    
while (c != 'q')
    
...{
        c 
= getche();

        
switch (c)
        
...{
            
// Pressing 'p' will begin playing the sample.
            case 'p': alSourcePlay(Source); break;

            
// Pressing 's' will stop the sample from playing.
            case 's': alSourceStop(Source); break;

            
// Pressing 'h' will pause (hold) the sample.
            case 'h': alSourcePause(Source); break;
        }
;
    }


    
return 0;
}

This is the interesting part of the tutorial. It's a very basic loop that lets us control the playback of the audio sample. Pressing 'p' will replay the sample, pressing 's' will stop the sample, and pressing 'h' will pause the sample. Pressing 'q' will exit the program.

Well there it is. Your first delve into OpenAL. I hope it was made simple enough for you. It may have been a little too simple for the 1337 h4X0r, but we all got to start somewhere. Things will get more advanced as we go along. 

ALSA声卡驱动中的DAPM详解之五:建立widget之间的连接关系

前面我们主要着重于codec、platform、machine驱动程序中如何使用和建立dapm所需要的widget,route,这些是音频驱动开发人员必须要了解的内容,经过前几章的介绍,我们应该知道如...
  • DroidPhone
  • DroidPhone
  • 2013年11月04日 21:25
  • 20747

scala 基本语法-cp

scala 基本语法-cp
  • hzdxw
  • hzdxw
  • 2016年07月05日 20:34
  • 2090

简单Maven的Web项目之验证码(jcaptcha组件篇)

一:JCaptcha简介jcaptcha是一个开源的用来生成图形验证码的Java开源组件,使用起来也是非常的简单方便。 jcapthca是非常强大的,不光是可以生成图片式的验证码,还可以生成声音式的...
  • gao36951
  • gao36951
  • 2015年08月02日 23:01
  • 7019

A simple application that shows how to play and record sound

  • 2006年02月23日 09:05
  • 154KB
  • 下载

simple nes emu no sound/only support mapper0 nrom

  • 2015年10月23日 22:57
  • 736KB
  • 下载

IOS音频1:之采用四种方式播放音频文件(一)AudioToolbox AVFoundation OpenAL AUDIO QUEUE

在本卷你可以学到什么? 采用四种方法设计应用于各种场合的音频播放器: 基于AudioToolbox.framework框架。播放系统声音文件。 基于AVFoundation.framework框架。播...
  • u014011807
  • u014011807
  • 2014年10月17日 16:07
  • 2082

Java 简单工厂模式(Simple Factory Pattern)又叫静态工厂方法模式(Static FactoryMethod Pattern)

本文参考: http://blog.csdn.net/weiwenlongll/article/details/6918164 简单工厂模式解释:         简单工厂模式(...
  • abcyyjjkk
  • abcyyjjkk
  • 2017年02月10日 09:39
  • 232

知识库 tomcat-A Simple Web Server static

1 应用之间的通信 To send byte streams, you must first call the Socket class’s getOutputStream method to ob...
  • qfzhangwei
  • qfzhangwei
  • 2016年11月13日 11:49
  • 210

设计模式系列(四)简单工厂模式(Simple Factory Pattern/Static Factory Method)

设计模式系列(四)简单工厂模式 (Simple Factory Pattern/Static Factory Method)     简单工厂模式又被成为静态工厂方法,所以其外文名一般是Stati...
  • dan15188387481
  • dan15188387481
  • 2015年12月04日 09:56
  • 518

sound方案.part1

  • 2009年11月12日 13:02
  • 18MB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:OpenAL Lesson 1: Simple Static Sound(转载)
举报原因:
原因补充:

(最多只允许输入30个字)