OpenAL Lesson 4: The ALC(转载)

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


Up until now we have been letting Alut do all the real tricky stuff for us. For example handling the audio devices. It's really nice that the Alut library is there to provide this functionality, but any smart coder will want to know exactly what their doing. We may want to, at some point, use the Alc directly. In this tutorial we will expose the Alc layer and take a look at how to handle the devices on our own.

ALCdevice* pDevice;

ALCubyte DeviceName[] 
= "DirectSound3D";

= alcOpenDevice(DeviceName);

So what is an Alc device? Try to think of it in terms of a resource. OpenAL grabs a handle to the hardware being used, which must in turn be shared with the entire system. A device can be of a specific implementation as well, as in this case where we are using DirectSound as the audio device. This code grabs a handle to the hardware device and readies it to be used by the application. Eventually we should see more devices made for specific soundcards.

Passing NULL to 'alcOpenDevice' is a perfectly valid argument. It forces the Alc to use a default device.

ALCcontext* pContext;

= alcCreateContext(pDevice, NULL);


What is an Alc context? OpenGL coders will recall that there was rendering contexts used by OpenGL that controlled the state management across different windows. An 'HGLRC' as they are called could be created several times to enable multiple rendering windows. And different rendering states for each context could be achieved. An Alc context works on the same principal. First we tell it which device to use (which we have already created), then we make that context current. In theory you could create multiple rendering contexts for different windows, and set the state variables differently and have it work just fine. Although the term "rendering context" usually applies to a visual rendering, this is the term preferred in the sdk docs and should be the term used.

You may notice too that the second parameter in 'alcCreateContext' has been set to NULL. The OpenAL sdk from Creative Labs defines the following variables which are optional flags to that parameter.


If you were to create multiple contexts you could make them interchangeable by making a call to 'alcMakeContextCurrent'. Sending NULL to 'alcMakeContextCurrent' is also a perfectly valid argument. It will prevent processing of any audio data. Be aware that even if you have multiple rendering contexts, you can only have one current at a time, and when your application needs to use two contexts interchangeably you must be the one to make sure the appropriate context is current. And if you do decide to do this, then there may be times when you want to know exactly which context is current without going through a big check.

ALcontext* pCurContext;
= alcGetCurrentContext();

Once you have your context you can also obtain the device in use by that context.

ALdevice* pCurDevice;
= alcGetContextsDevice(pCurContext);

Above we used the context we retrieved to find out which device it was using. There is also one other cool feature that was built into Alc for handling contexts.

// Processing has been suspended to pContext.
// Processing has been re-enabled to pContext.

What we have done above was stop, and then resume processing of audio data to the context. When processing has been suspended, no sound will be generated from data sent through that context. A further note on the rendering context: the OpenAL 1.0 spec does imply, but does not explicitly say, that sources and buffers may be used across contexts. The "lifetime" of a source or buffer during the application, is said to be valid as long as the source and buffer id is valid (i.e. they have not been deleted).


And that is how we clean up. The current context is defaulted to NULL, the context we created is released, and the handle to the device is given back to the system resources. There is but a few more Alc functions we have not yet covered.

ALenum alcGetError(ALvoid);

ALboolean alcIsExtensionPresent(ALCdevice
* device, ALubyte* extName);

* alcGetProcAddress(ALCdevice* device, ALubyte* funcName);

ALenum alcGetEnumValue(ALCdevice
* device, ALubyte* enumName);

* alcGetString(ALCdevice* device, ALenum token);

ALvoid alcGetIntegerv(ALCdevice
* device, ALenum token, ALsizei size, ALint* dest);

It may be pretty obvious to you what these do, but lets humour ourselves and have a closer look. First we have 'alcGetError' which is just like 'alGetError' but will return Alc errors. The next three functions are for querying Alc extensions. This was just the creators planning ahead, as there are no Alc extensions either. The last function, 'alcGetInteger', will return the Alc version when passed 'ALC_MAJOR_VERSION' or 'ALC_MINOR_VERSION'.

The function 'alcGetString' is pretty cool. It can take any of the following three parameters to 'token':


The first will return the device string which your OpenAL implementation will prefer you to use. In current OpenAL this should be "DirectSound3D", like we used above. The second token will return a list of specifiers, but in current OpenAL will only return "DirectSound" (without the "3D" for some reason). The last will return a list of Alc extensions, of which none exist yet.

Well that's most of Alc for you. I hope it gave you a better understanding of how OpenAL interacts with the operation system. You might try writing your own initialization routines so you can cast off Alut altogether. Either way have fun with it.


OpenAL Lesson 5: Sources Sharing Buffers(转载)

转自 this point in the OpenAL series I...
  • kongbu0622
  • kongbu0622
  • 2007年10月14日 20:29
  • 748

OpenAL Lesson 2: Looping and Fadeaway(转载)

转自 you found the last tutorial of ...
  • kongbu0622
  • kongbu0622
  • 2007年10月14日 19:51
  • 747

OpenAL Lesson 7: The Doppler Effect(转载)

转自 know this will be boring review fo...
  • kongbu0622
  • kongbu0622
  • 2007年10月14日 20:35
  • 1737

OpenAL Lesson 1: Simple Static Sound(转载)

转自 to the exciting world of Ope...
  • kongbu0622
  • kongbu0622
  • 2007年10月14日 19:45
  • 1458

OpenAL Lesson 3: Multiple Sources(转载)

转载 Its been a while since my las...
  • kongbu0622
  • kongbu0622
  • 2007年10月14日 19:53
  • 801

lesson4 线程同步

1. 互斥量mutex 2. 读写锁:pthread_rwlock_t rwlock //非常适合读次数大于写次数的程序 读写锁的三种状态:读模式加锁、写模式加锁、不加锁。一次只有一个线程占用写模式的...
  • wenwen111111
  • wenwen111111
  • 2017年02月14日 10:48
  • 73


OpenAL源码下载网站是 OpenAL官网是 ,在官网上可以下载用户手...
  • oilcode
  • oilcode
  • 2016年02月26日 11:39
  • 1467


OpenAL(Open Audio Library)是自由软件界的跨平台音效API。它设计给多通道三维位置音效的特效表现。其 API 风格模仿自 OpenGL。刚开始接触的时候可以下载工具包Alut,...
  • T20091
  • T20091
  • 2013年11月29日 19:04
  • 1945


因为之前在原公司上班的时候,用过openAL来播放PCM实时数据流.现在换了新公司又要求我用自己的方法写个音频播放器.我就选用这openAL H-,RzL/   但是这次使用openAL的时候,就...
  • xiaojun111111
  • xiaojun111111
  • 2015年03月05日 16:47
  • 665


1,OpenAL源码编译 ( 2,声音衰减,以及alDistanceModel函数使用详解 ...
  • oilcode
  • oilcode
  • 2016年02月26日 22:07
  • 854
您举报文章:OpenAL Lesson 4: The ALC(转载)