CoreAudio获取输出采样率 [英] CoreAudio get output sample rate
问题描述
我正在创建一个Mac OS X CoreAudio命令行程序,并通过AudioUnits专有渲染一些字母数字终端输入到实时音频信号中,以保持尽可能简单。所有工作都可以达到匹配输出采样率的水平。
首先,我使用Addisson Wesley的学习核心音频的第07章教程代码CH07_AUGraphSineWave。 / p>
我通过书初始化AudioComponent:
void CreateAndConnectOutputUnit(MyGenerator * generator)
{
AudioComponentDescription theoutput = {0};
theoutput.componentType = kAudioUnitType_Output;
theoutput.componentSubType = kAudioUnitSubType_DefaultOutput;
theoutput.componentManufacturer = kAudioUnitManufacturer_Apple;
AudioComponent comp = AudioComponentFindNext(NULL,& theoutput);
if(comp == NULL){
printf(无法获得输出单位);
出口(-1);
}
CheckError(AudioComponentInstanceNew(comp,& generator-> outputUnit),
无法为outputUnit打开组件);
AURenderCallbackStruct输入;
input.inputProc = MyRenderProc;
input.inputProcRefCon =生成器;
CheckError(AudioUnitSetProperty(generator-> outputUnit,
kAudioUnitProperty_SetRenderCallback,
kAudioUnitScope_Input,
0,
& input,
sizeof(input),
AudioUnitSetProperty失败);
CheckError(AudioUnitInitialize(generator-> outputUnit),
无法初始化输出单元);
}
我的主要问题是我不知道该如何退缩渲染AURenderCallbackStruct
的输出硬件采样率,因为它在信号生成过程中起着至关重要的作用。我无法承受将采样率硬编码到渲染回调中的过程,尽管知道这是最简单的方法,因为速率不匹配会导致信号以错误的音高播放。
有没有办法在如此低级的API上获得默认输出的采样率?
是否可以某种方式进行匹配,而又不会过于复杂?
我想念什么吗?
预先感谢。
致谢,
Tom
采样率是所有AudioUnit的属性-请参见 kAudioUnitProperty_SampleRate
(文档此处)-尽管最终由IO单元(iOS上为RemoteIO或MacOSX上为HAL单元)驱动音频接口的采样率。这在回调结构中不可用。您需要在初始化代码中使用 AudioUnitGetProperty()
来读取此属性。
在您的情况下,以下内容可能会这样做:
Float64 sampleRate;
CheckError(AudioUnitGetProperty(generator-> outputUnit,
kAudioUnitProperty_SampleRate,
kAudioUnitScope_Input,
0,
& sampleRate,
sizeof(sampleRate)),
如果您要定位的是iOS,则还需要与音频会话。
I'm creating a Mac OS X CoreAudio command-line program with proprietary rendering of some alphanumeric terminal input into a live audio signal, by means of AudioUnits, trying to stay as simple as possible. All works fine up to matching output sample rate.
As a starting point I'm using the Chapter 07 tutorial code of Addisson Wesley's "Learning Core Audio", CH07_AUGraphSineWave.
I initialize the AudioComponent "by the book":
void CreateAndConnectOutputUnit (MyGenerator *generator)
{
AudioComponentDescription theoutput = {0};
theoutput.componentType = kAudioUnitType_Output;
theoutput.componentSubType = kAudioUnitSubType_DefaultOutput;
theoutput.componentManufacturer = kAudioUnitManufacturer_Apple;
AudioComponent comp = AudioComponentFindNext (NULL, &theoutput);
if (comp == NULL) {
printf ("can't get output unit");
exit (-1);
}
CheckError (AudioComponentInstanceNew(comp, &generator->outputUnit),
"Couldn't open component for outputUnit");
AURenderCallbackStruct input;
input.inputProc = MyRenderProc;
input.inputProcRefCon = generator;
CheckError(AudioUnitSetProperty(generator->outputUnit,
kAudioUnitProperty_SetRenderCallback,
kAudioUnitScope_Input,
0,
&input,
sizeof(input)),
"AudioUnitSetProperty failed");
CheckError (AudioUnitInitialize(generator->outputUnit),
"Couldn't initialize output unit");
}
My main problem is in my not knowing how to retreive the output hardware sample rate for the rendering AURenderCallbackStruct since it does play a vital part in the signal generating process. I can't afford having the sample rate hard-coded into the rendering callback, although knowing it's the easiest way, since rate mismatch causes the signal being played at wrong pitch.
Is there a way of getting the default output's sample rate on such a low-level API? Is there a way of matching it somehow, without getting overly complicated? Have I missed something?
Thanks in advance.
Regards,
Tom
The sample-rate is a property of all AudioUnits - see kAudioUnitProperty_SampleRate
(documentation here) - although ultimately it's the IO Unit (RemoteIO on iOS or HAL unit on MacOSX) that drives the sample-rate at the audio interface. This is not available in the call-back structure; you need to read this property with AudioUnitGetProperty()
in your initialisation code.
In your case, the following would probably do it:
Float64 sampleRate;
CheckError(AudioUnitGetProperty(generator->outputUnit,
kAudioUnitProperty_SampleRate,
kAudioUnitScope_Input,
0,
&sampleRate,
sizeof(sampleRate)),
If you're targeting iOS, you also need to interact with the Audio Session.
这篇关于CoreAudio获取输出采样率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!