iPhone/OpenAL 获取样本的声音长度(播放时间)

发布于 2024-10-19 08:56:12 字数 183 浏览 3 评论 0原文

我是 OpenAL 新手。我设法获得了一个封装 iPhone 版 OpenAL 的声音管理器代码,这样我就可以加载声音并播放它们。

但我确实需要知道每个声音文件的长度(以秒为单位),因为我需要在声音完成后立即调用一个事件。

我注意到有一种方法可以在填充缓冲区时计算声音的长度(?)。有人可以帮我解决这个问题吗?提前致谢。

I'm new to OpenAL. I managed to get a soundmanager code that wraps OpenAL for iPhone, so I can load sounds and play them.

But I really need to know how long each sound file is in seconds because I need to call an event as soon as the sound as finished.

I've noticed that there is a way to calculate the length of a sound when populating the buffers(?). Can someone help me with this? Thanks in advance.

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

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

发布评论

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

评论(4

溺孤伤于心 2024-10-26 08:56:12
float result;
alGetSourcef(sourceID, AL_SEC_OFFSET, &result);
return result;

您可以使用此代码片段来获取声音的当前播放时间。

float result;
alGetSourcef(sourceID, AL_SEC_OFFSET, &result);
return result;

You can use this snippet to get the current playback time of the sound.

挽你眉间 2024-10-26 08:56:12

如果您使用已知格式的原始 PCM 音频样本填充已知大小的缓冲区,则:

duration = numberOfSampleFrames / sampleRate;  

其中,样本帧的数量通常是单声道 16 位样本的 number_of_bytes/2,或立体声的 number_of_bytes/4,等等。

If you are populating known size buffers with raw PCM audio samples of a known format, then:

duration = numberOfSampleFrames / sampleRate;  

where, typically, the number of sample frames is the number_of_bytes/2 for mono 16-bit samples, or the number_of_bytes/4 for stereo, etc.

过期以后 2024-10-26 08:56:12

我有同样的问题并提出了以下解决方案。第一个功能是可选的,但允许补偿经过的时间。然后我用生成的时间间隔触发 NSTimer。

玩得开心!短剑

static NSTimeInterval OPElapsedPlaybackTimeForSource(ALuint sourceID) {
    float result = 0.0;
    alGetSourcef(sourceID, AL_SEC_OFFSET, &result);
    return result;
}

static NSTimeInterval OPDurationFromSourceId(ALuint sourceID) {
    ALint bufferID, bufferSize, frequency, bitsPerSample, channels;
    alGetSourcei(sourceID, AL_BUFFER, &bufferID);
    alGetBufferi(bufferID, AL_SIZE, &bufferSize);
    alGetBufferi(bufferID, AL_FREQUENCY, &frequency);
    alGetBufferi(bufferID, AL_CHANNELS, &channels);    
    alGetBufferi(bufferID, AL_BITS, &bitsPerSample);    

    NSTimeInterval result = ((double)bufferSize)/(frequency*channels*(bitsPerSample/8));

    NSLog(@"duration in seconds %lf", result); 

    return result;
}

I have the same problem and came up with the following solution. The first function is optional, but allows to compensate for the elapsed time. I'm then firing an NSTimer with the resulting time interval.

Have fun! Dirk

static NSTimeInterval OPElapsedPlaybackTimeForSource(ALuint sourceID) {
    float result = 0.0;
    alGetSourcef(sourceID, AL_SEC_OFFSET, &result);
    return result;
}

static NSTimeInterval OPDurationFromSourceId(ALuint sourceID) {
    ALint bufferID, bufferSize, frequency, bitsPerSample, channels;
    alGetSourcei(sourceID, AL_BUFFER, &bufferID);
    alGetBufferi(bufferID, AL_SIZE, &bufferSize);
    alGetBufferi(bufferID, AL_FREQUENCY, &frequency);
    alGetBufferi(bufferID, AL_CHANNELS, &channels);    
    alGetBufferi(bufferID, AL_BITS, &bitsPerSample);    

    NSTimeInterval result = ((double)bufferSize)/(frequency*channels*(bitsPerSample/8));

    NSLog(@"duration in seconds %lf", result); 

    return result;
}
不气馁 2024-10-26 08:56:12
ALint bufferID, bufferSize;
alGetSourcei(sourceID, AL_BUFFER, &bufferID);
alGetBufferi(bufferID, AL_SIZE, &bufferSize);

NSLog(@"time in seconds %f", (1.0*bufferSize)/(44100*2*2)); //44100 * 2 chanel * 2byte (16bit)
ALint bufferID, bufferSize;
alGetSourcei(sourceID, AL_BUFFER, &bufferID);
alGetBufferi(bufferID, AL_SIZE, &bufferSize);

NSLog(@"time in seconds %f", (1.0*bufferSize)/(44100*2*2)); //44100 * 2 chanel * 2byte (16bit)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文