问题描述
有人可以指点文件说明不同iPhone版本的原生采样率,以避免 core-audio
内部采样率转换?
Can anybody point me to documentation stating the native sampling rates on the different iPhone versions in order to avoid core-audio
internal sampling rate conversion?
编辑:
否则,请您指点一个源代码示例,了解如何以编程方式获取这些值?
Otherwise, can you please point me to a source code example of how can I get those values programmatically?
修改:
此是指Canonical音频格式,但仅提及样本类型(PCM)和位深度(16位)。它没有提到捕获硬件直接支持的任何本机采样率。这些是我正在寻找的值。
This Apple document (page 26) refers to a Canonical audio format, but only makes mention of sample type (PCM) and bit depth (16-bit). It doesn't mention any native sampling rates supported directly by the capture hardware. Those are the values I'm looking for.
推荐答案
您需要做的是找到一种检测硬件采样率的方法,并使用您在后续代码中找到的任何内容。
What you need to do is find a way to detect the hardware sample rate, and use whatever you find in your subsequent code.
有一个音频会话属性可以为您提供:CurrentHardwareSampleRate
There is an audio session property which will give you this: CurrentHardwareSampleRate
- (void) logSampleRate {
Float64 sampleRate;
UInt32 srSize = sizeof (sampleRate);
OSStatus error =
AudioSessionGetProperty(
kAudioSessionProperty_CurrentHardwareSampleRate,
&srSize,
&sampleRate);
if (error == noErr) {
NSLog (@"CurrentHardwareSampleRate = %f", sampleRate);
}
}
iPhone 4S和iPhone 5S报告hardwareSampleRate = 44100.000000但其他设备可能(将)不同...
iPhone 4S and iPhone 5S report hardwareSampleRate = 44100.000000 but others devices may (will) differ...
编辑
在回答时问及阅读最新的文档,我看到iOS6中不推荐使用 CurrentHardwareSampleRate
。实际上,我应该知道更好,因为。
While answering the question and reading the latest docs, I see that CurrentHardwareSampleRate
is deprecated in iOS6. And really, I should have know better, given my own advice.
所以事情就是这样:
1 - 不要使用这个陈旧且弃用的C接口,使用 AVAudioSession
API
1 - don't use this antiquated and deprecated C interface, use the AVAudioSession
API
2 - 不要使用 HardwareSampleRate
,请使用 sampleRate
。
2 - don't use HardwareSampleRate
, use sampleRate
.
这引起了人们的怀疑,即Apple希望让我们(甚至)远离金属。但我们应该放心。虽然 sampleRate
的文档仅提供
This raises the suspicion that Apple wants to distance us (even) further from the metal. But we should rest assured. While the docs for sampleRate
only offer
明显省略单词 hardware
,我们也有这个方法:
notably omitting the word hardware
, we also have this method:
- (BOOL)setPreferredSampleRate:(double)sampleRate error:(NSError **)outError
其中 sampleRate 被充分描述为
您要使用的硬件采样率。硬件采样率的可用范围取决于设备。它的范围通常为8,000到48,000赫兹。
似乎我们应该使用它的方式是设置我们的首选速率,设备将根据硬件设置(?)实际速率,该速率可能接近可行的首选速率。因此,一旦设置了首选费率,就可以检查 sampleRate
的值(而不是 preferredSampleRate
)属性,以发现将使用的实际采样率。这是 - 我们希望 - 硬件采样。
It seems that the way we are supposed to use this, is to set our preferred rate, from which the device will set an (the?) actual rate which presumably will be as near to the preferred as feasible, based on the hardware. So once you have set your preferred rate, you can check the value of the sampleRate
(as opposed to preferredSampleRate
) property, to discover the actual sample rate that will be used. This is - we hope - hardware sampling.
这是现代的方式......
Here is the modern way...
- (void) logAudioFormatAV {
AVAudioSession* session = [AVAudioSession sharedInstance];
BOOL success;
NSError* error = nil;
double preferredSampleRate = 48000;
success = [session setPreferredSampleRate:preferredSampleRate error:&error];
if (success) {
NSLog (@"session.sampleRate = %f", session.sampleRate);
} else {
NSLog (@"error setting sample rate %@", error);
}
}
我尝试了各种首选采样率4S和5S总是回到44100.所以这似乎是做正确的事情,并报告实际 硬件采样率。但这值得进行更广泛的测试,以获得更大的确定性。
I have tried this with various preferred sample rates on the 4S and 5S and always get back 44100. So this seems to be doing the right thing, and reporting back the actual hardware sample rate. But this deserves wider testing for greater certainty.
我还想指出你对Chris Adamson出色的书学习核心音频的方向,在第10章 iOS上的核心音频中涉及到这个主题。
I'd like to also point you in the direction of Chris Adamson's excellent book Learning Core Audio, which touches on this subject in Chapter 10, Core Audio on iOS.
这篇关于如何获取本机(硬件支持)音频采样率以避免内部采样率转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!