IOS/OpenAl 声音干扰

发布于 2024-12-09 05:04:42 字数 1434 浏览 2 评论 0原文

我是 Objective-C 的新手...

我找到了如何使用 OpenAl 的源代码,复制此代码然后进行测试,但播放声音时受到干扰。你能看一下代码并找出问题所在吗?

ALCcontext *context = NULL;
ALCdevice *device = alcOpenDevice(NULL);
if (device)
{
    context = alcCreateContext(device, NULL);
    alcMakeContextCurrent(context);
}
NSString *path = [[NSBundle mainBundle] pathForResource:@"sound" ofType:@"caf"];

AudioFileID fileID = 0;
NSURL *url = [NSURL fileURLWithPath:path];

OSStatus result = AudioFileOpenURL((CFURLRef)url, kAudioFileReadPermission, 0, &fileID);
if (result != 0)
    NSLog(@"Faild to load file at path:%@",path);

UInt32 fileSize = 0;
UInt32 propSize = sizeof(UInt64);

OSStatus result1 = AudioFileGetProperty(fileID, kAudioFilePropertyAudioDataByteCount, &propSize, &fileSize);
if (result1 != 0)
    NSLog(@"Cannot get size of file!");

unsigned char *buffer = malloc(fileSize);
OSStatus result2 = noErr;
result2 = AudioFileReadBytes(fileID, false, 0, &fileSize, buffer);
AudioFileClose(fileID);
if (result2 != 0)
    NSLog(@"Cannot load data from file!");

ALuint bufferId = 0;
alGenBuffers(1, &bufferId);
alBufferData(bufferId, AL_FORMAT_STEREO16,  buffer, fileSize, 44100);

free(buffer); 

ALuint sourceId = 0;
alGenSources(1, &sourceId);
alSourcei(sourceId, AL_BUFFER, bufferId);
alSourcef(sourceId, AL_PITCH, 1.0f);
alSourcef(sourceId, AL_GAIN, 1.0f); 
alSourcei(sourceId, AL_LOOPING, AL_TRUE);

alSourcePlay(sourceId);

I'm new in Objective - C...

I found source code how to use OpenAl, copying this and then testing, but sound was played with interference. Can you look at code and tell what is wrong ?

ALCcontext *context = NULL;
ALCdevice *device = alcOpenDevice(NULL);
if (device)
{
    context = alcCreateContext(device, NULL);
    alcMakeContextCurrent(context);
}
NSString *path = [[NSBundle mainBundle] pathForResource:@"sound" ofType:@"caf"];

AudioFileID fileID = 0;
NSURL *url = [NSURL fileURLWithPath:path];

OSStatus result = AudioFileOpenURL((CFURLRef)url, kAudioFileReadPermission, 0, &fileID);
if (result != 0)
    NSLog(@"Faild to load file at path:%@",path);

UInt32 fileSize = 0;
UInt32 propSize = sizeof(UInt64);

OSStatus result1 = AudioFileGetProperty(fileID, kAudioFilePropertyAudioDataByteCount, &propSize, &fileSize);
if (result1 != 0)
    NSLog(@"Cannot get size of file!");

unsigned char *buffer = malloc(fileSize);
OSStatus result2 = noErr;
result2 = AudioFileReadBytes(fileID, false, 0, &fileSize, buffer);
AudioFileClose(fileID);
if (result2 != 0)
    NSLog(@"Cannot load data from file!");

ALuint bufferId = 0;
alGenBuffers(1, &bufferId);
alBufferData(bufferId, AL_FORMAT_STEREO16,  buffer, fileSize, 44100);

free(buffer); 

ALuint sourceId = 0;
alGenSources(1, &sourceId);
alSourcei(sourceId, AL_BUFFER, bufferId);
alSourcef(sourceId, AL_PITCH, 1.0f);
alSourcef(sourceId, AL_GAIN, 1.0f); 
alSourcei(sourceId, AL_LOOPING, AL_TRUE);

alSourcePlay(sourceId);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

老娘不死你永远是小三 2024-12-16 05:04:42

如果声音数据未存储在内存中,则不会播放声音。

free(buffer); 

您的声音数据将从内存中擦除,alBufferData() 仅将 OpenAL 绑定到声音数据。我建议将缓冲区指针存储在其他地方,以便当您想要从内存中删除声音时可以释放它。

The sound won't play if the sound data is not stored in memory.

free(buffer); 

Your sound data is wiped from memory, alBufferData() only binds OpenAL to the sound data. I suggest to store the buffer pointer elsewhere so it can be freed when you want the sound removed from memory.

只是在用心讲痛 2024-12-16 05:04:42

您对磁盘上的源数据做出了很多假设(字节序、通道数、交错、格式)。加载文件时,您应该查看 ASBD 并让音频服务为您进行转换。

以下是加载 OpenAL 中使用的音频数据的示例: https://github.com/kstenerud/ObjectAL-for-iPhone/blob/master/libs/ObjectAL/Support/OALAudioFile.m

You're making a lot of assumptions about the source data on disk (endianness, number of channels, interleaving, format). You should be looking at the ASBD and having audio services convert for you when loading the file.

Here's an example of loading audio data for use in OpenAL: https://github.com/kstenerud/ObjectAL-for-iPhone/blob/master/libs/ObjectAL/Support/OALAudioFile.m

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文