OpenAL的一些知识点

首先说一下OpenAL的历史

OpenAL 最初是由 Loki Software 所开发。是为了将 Windows 商业游戏移植到 Linux 上。Loki 倒闭以后,这个专案由自由软件/开放源始码社群继续维护。不过现在最大的主导者(并大量发展)是创新科技,并得到来自 Apple 和自由软件/开放源代码爱好者的持续支援。

OpenAL结构功能

OpenAL 主要的功能是在来源物体、音效缓冲和收听者中编码。来源物体包含一个指向缓冲区的指标、声音的速度、位置和方向,以及声音强度。收听者物体包含收听者的速度、位置和方向,以及全部声音的整体增益。缓冲里包含 8 或 16 位元、单声道或立体声 PCM 格式的音效资料,表现引擎进行所有必要的计算,如距离衰减、多普勒效应等。 不同于 OpenGL 规格,OpenAL 规格包含两个API分支;以实际 OpenAL 函式组成的核心,和 ALC API,ALC 用于管理表现内容、资源使用情况,并将跨平台风格封在其中。还有“ALUT”程式库,提供高阶“易用”的函式,其定位相当于 OpenGL 的 GLUT。

OpenAL快速入门

1.为什么使用OpenAL

也许你已经用过AudioToolbox框架并用以下代码来播放一个音乐文件:

NSString* path = [[NSBundle mainBundle] pathForResource:@"soundEffect1" ofType:@"caf"];

NSURL * afUrl = [NSURL fileURLWithPath:path];

UInt32 soundID;

AudioServicesCreateSystemSoundID((CFURLRef)afUrl,&soundID);

AudioServicesPlaySystemSound (soundID);

这种播放方式很简单,但它每次都需要把音乐文件加载到缓存中,降低了播放的效率。当你需要实时播放的时候,它往往出现延时。而使用OpenAL将最大限度的减少这种延时,提供最佳播放效率。

2.声音格式的转换

为了节省iPhone在播放音乐时进行音频格式转换的时间,你可以手动对文件进行格式转换。
打开终端,输入以下命令:

/usr/bin/afconvert -f caff -d LEI16@44100 inputSoundFile.aiff outputSoundFile.caf

上述命令利用afconverter工具将inputSoundFile.aiff转换成了outputSoundFile.caf,采样频率为44.1k。

3.这是一个快速的入门教程,将教你使用OpenAL播放音乐的最少步骤。

OpenAL主要由3个实体构成:听众Listener, 声源Source, 以及缓存Buffer。

听众Listener:就是你。Listener的位置是可以移动的。

声源Source:类似一个话筒。它发出声音给听众听。和Listener一样,声源的位置也是可以移动的。例如oalTouch中实现了声音远近的控制(近响远轻),就是通过Listener和Source两张图片之间的距离实现的。

缓存Buffer:存着原始声音数据,就是你需要播放的声音。

 

还有2个重要的对象:设备device和环境context。

设备是播放声音的硬件。

环境是声源和听众所在的空间。

 

让OpenAL工作所需的最少步骤是:

1. 得到设备信息

2. 将环境与设备关联

3. 在缓存中加入声音数据

4. 在声源中加入缓存数据

5. 播放声源

 

以下是相关代码:

-(void)initOpenAL

{

    Initialization

    mDevice = alcOpenDevice(NULL); select the "preferred device"

    if (mDevice) {

        use the device to make a context

        mContext=alcCreateContext(mDevice,NULL);

        set my context to the currently active one

        alcMakeContextCurrent(mContext);

    }

}

 

上述代码初始化了设备device和环境context。

接下去要打开声音文件。

 

get the full path of the file

NSString* fileName = [[NSBundle mainBundle] pathForResource:@"neatoEffect" ofType:@"caf"];

first, open the file

AudioFileID fileID = [self openAudioFile:fileName];

 

上述代码打开了一个叫neatoEffect.caf的声音文件,并且得到了它的ID。

openAudioFile方法的实现在哪里?

 

-(AudioFileID)openAudioFile:(NSString*)filePath

{

    AudioFileID outAFID;

    use the NSURl instead of a cfurlref cuz it is easier

    NSURL * afUrl = [NSURL fileURLWithPath:filePath];

             OSStatus result = AudioFileOpenURL((CFURLRef)afUrl, kAudioFileReadPermission, 0, &outAFID);

    if (result != 0) NSLog(@"cannot openf file: %@",filePath);

        return outAFID;

}

 

这个方法中调用AudioFileOpenURL得到了一个AudioFileID outAFID。

接下去做什么?把声音数据读出来。

 

为了读取声音数据,先要知道数据的大小。

可以调用这个方法:

 

find out how big the actual audio data is

UInt32 fileSize = [self audioFileSize:fileID];

 

audioFileSize它的实现文件是:

-(UInt32)audioFileSize:(AudioFileID)fileDescriptor

{

    UInt64 outDataSize = 0;

    UInt32 thePropSize = sizeof(UInt64);

    OSStatus result = AudioFileGetProperty(fileDescriptor, kAudioFilePropertyAudioDataByteCount, &thePropSize, &outDataSize);

    if(result != 0) NSLog(@"cannot find file size");

        return (UInt32)outDataSize;

}

 

 

得到了声音数据的大小,我们可以把数据复制到缓存buffer里了:

this is where the audio data will live for the moment

unsigned char * outData = malloc(fileSize);

 

this where we actually get the bytes from the file and put them

into the data buffer

OSStatus result = noErr;

result = AudioFileReadBytes(fileID, false, 0, &fileSize, outData);

AudioFileClose(fileID); close the file

 

if (result != 0) NSLog(@"cannot load effect: %@",fileName);

NSUInteger bufferID;

grab a buffer ID from openAL

alGenBuffers(1, &bufferID);

 

jam the audio data into the new buffer

alBufferData(bufferID,AL_FORMAT_STEREO16,outData,fileSize,44100);

 

这段有点长,其实还好啦。首先我们为声音数据创建了一个空间outData,然后用AudioFileReadBytes将声音数据读到了这个空间里。接下去,把空间里的数据复制到了缓存buffer里。44100表示音频的采样频率4.41k,STEREO16表示16位立体声格式。

 

复制完数据,别忘了清空这个outData空间:

 

clean up the buffer

if (outData)

{

    free(outData);

    outData = NULL;

}

 

最后我们可以将buffer和声源source绑定了。

 

grab a source ID from openAL

alGenSources(1, &sourceID);

attach the buffer to the source

alSourcei(sourceID, AL_BUFFER, bufferID);

set loop sound

if (loops) alSourcei(sourceID, AL_LOOPING, AL_TRUE);

 

差不多完成了,我们播放声源吧:

- (void)playSound:(NSString*)soundKey

{

    alSourcePlay(sourceID);

}

 

要停下怎么办?

- (void)stopSound:(NSString*)soundKey

{

    alSourceStop(sourceID);

}

 

最后,退出前别忘了把所有东西都释放:

-(void)cleanUpOpenAL:(id)sender

{

    delete the sources

    alDeleteSources(1, &sourceID);

    delete the buffers

    alDeleteBuffers(1, &bufferID);

    destroy the context

    alcDestroyContext(mContext);

    close the device

    alcCloseDevice(mDevice);

}


我只是一个搬运工。。。。

你可能感兴趣的:(OpenAL的一些知识点)