首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >仅针对特定采样率调用AVAudioUnit (OS X)渲染块

仅针对特定采样率调用AVAudioUnit (OS X)渲染块
EN

Stack Overflow用户
提问于 2016-05-11 07:07:07
回答 1查看 662关注 0票数 1

我很难让AVAudioEngine (OS )在所有采样率下都表现得很好。

下面是我构建连接的代码:

代码语言:javascript
复制
- (void)makeAudioConnections {  

  auto hardwareFormat = [self.audioEngine.outputNode outputFormatForBus:0];  
  auto format = [[AVAudioFormat alloc] initStandardFormatWithSampleRate:hardwareFormat.sampleRate channels:2];  
  NSLog(@"format: %@", format);  

  @try {  
    [self.audioEngine connect:self.avNode to:self.audioEngine.mainMixerNode format:format];  
    [self.audioEngine connect:self.audioEngine.inputNode to:self.avNode format:format];  
  } @catch(NSException* e) {  
    NSLog(@"exception: %@", e);  
  }  
}  

在我的音频接口上,为44.1、48和176.4 the调用了渲染回调。对于96和192 kHz,不需要调用它。在内置音频上,回调是44.1、48、88,而不是96。

我的AU的allocateRenderResourcesAndReturnError的频率是96 for。不会返回任何错误。

代码语言:javascript
复制
- (BOOL) allocateRenderResourcesAndReturnError:(NSError * _Nullable *)outError {

  if(![super allocateRenderResourcesAndReturnError:outError]) {
    return NO;
  }

  _inputBus.allocateRenderResources(self.maximumFramesToRender);
  _sampleRate = _inputBus.bus.format.sampleRate;

  return YES;
}

这是我的AU的初始化方法,主要是从苹果的AUv3演示中剪切和粘贴的:

代码语言:javascript
复制
- (instancetype)initWithComponentDescription:(AudioComponentDescription)componentDescription options:(AudioComponentInstantiationOptions)options error:(NSError **)outError {

  self = [super initWithComponentDescription:componentDescription options:options error:outError];

  if (self == nil) {
    return nil;
  }

  // Initialize a default format for the busses.
  AVAudioFormat *defaultFormat = [[AVAudioFormat alloc] initStandardFormatWithSampleRate:44100. channels:2];

  // Create the input and output busses.
  _inputBus.init(defaultFormat, 8);
  _outputBus = [[AUAudioUnitBus alloc] initWithFormat:defaultFormat error:nil];

  // Create the input and output bus arrays.
  _inputBusArray  = [[AUAudioUnitBusArray alloc] initWithAudioUnit:self busType:AUAudioUnitBusTypeInput  busses: @[_inputBus.bus]];
  _outputBusArray = [[AUAudioUnitBusArray alloc] initWithAudioUnit:self busType:AUAudioUnitBusTypeOutput busses: @[_outputBus]];

  self.maximumFramesToRender = 256;

  return self;

}

为了简单起见,我在启动应用程序之前设置了采样率。

我不知道从哪里开始追查。

更新

这是一个小项目,它重现了我遇到的问题:

Xcode project to reproduce issue

在某些采样率下,从输入中提取数据时会出现错误。

在我以96 the运行的内置音频上,render块被调用,帧计数和错误分别为511和513,分别为-10863 (kAudioUnitErr_CannotDoInCurrentContext)和-10874 (kAudioUnitErr_TooManyFramesToProcess)。增加maximumFramesToRender似乎没有什么帮助。

更新2

我简化了测试,只需将输入连接到主混音器:

代码语言:javascript
复制
[self.audioEngine connect:self.audioEngine.inputNode to:self.audioEngine.mainMixerNode format:nil];

我尝试显式设置格式参数。

这仍然不能在96 This下播放。所以我认为这可能是AVAudioEngine中的一个错误。

EN

回答 1

Stack Overflow用户

发布于 2016-06-22 02:07:32

对于AVAudioEngine播放,输入和输出硬件格式以及所有连接格式必须具有相同的采样率。所以下面的方法应该是可行的。

代码语言:javascript
复制
AVAudioFormat *outputHWFormat = [self.audioEngine.outputNode outputFormatForBus:0];
AVAudioFormat *inputHWFormat  = [self.audioEngine.inputNode inputFormatForBus:0];

if (inputHWFormat.sampleRate == outputHWFormat.sampleRate) {
    [self.audioEngine connect:self.audioEngine.inputNode to:self.audioEngine.mainMixerNode format:inputHWFormat];
    [self.audioEngine connect:self.audioEngine.mainMixerNode to:self.audioEngine.outputNode format:inputHWFormat];

}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37150355

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档