OpenAL 快速入门(无延迟播放音频)

50 篇文章 0 订阅
1 篇文章 0 订阅
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,采样频率为4.41k。
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);
}

原文网址:
http://benbritten.com/2008/11/06/openal-sound-on-the-iphone/
你们还可以看看这个(同一位作者关于AudioSession的文章):
http://benbritten.com/2009/02/02 ... one/comment-page-1/



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值