jjzjj

objective-c - 编写远程 I/O 渲染回调函数时遇到问题

coder 2023-09-26 原文

我正在编写一个 iOS 应用程序,它从麦克风获取输入,通过高通滤波器音频单元运行它,然后通过扬声器播放。我已经能够通过使用 AUGraph API 成功地做到这一点。在其中,我放置了两个节点:一个远程 I/O 单元和一个效果音频单元(kAudioUnitType_EffectkAudioUnitSubType_HighPassFilter),并将 io 单元的输入元素的输出范围连接到效果单元的输入,以及效果节点的输出到 io 单元的输出元素的输入范围。但是现在我需要根据处理后的音频样本做一些分析,所以我需要直接访问缓冲区。这意味着(如果我错了请纠正我)我不能再使用 AUGraphConnectNodeInput 在效果节点的输出和 io 单元的输出元素之间建立连接,并且必须附加一个渲染回调函数对于 io 单元的输出元素,这样我就可以在扬声器需要新样本时访问缓冲区。 我已经这样做了,但是当我在渲染回调中调用 AudioUnitRender 函数时出现 -50 错误。我相信我遇到了两个音频单元之间 ASBD 不匹配的情况,因为我没有在渲染回调中对此做任何事情(并且 AUGraph 之前处理过它)。 这是代码:

AudioController.h:

@interface AudioController : NSObject
{
    AUGraph mGraph;
    AudioUnit mEffects;
    AudioUnit ioUnit;
}

@property (readonly, nonatomic) AudioUnit mEffects;
@property (readonly, nonatomic) AudioUnit ioUnit;

-(void)initializeAUGraph;
-(void)startAUGraph;
-(void)stopAUGraph;

@end

AudioController.mm:

@implementation AudioController

…

static OSStatus renderInput(void *inRefCon, AudioUnitRenderActionFlags *ioActionFlags, const AudioTimeStamp *inTimeStamp, UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList *ioData)
{
    AudioController *THIS = (__bridge AudioController*)inRefCon;

    AudioBuffer buffer;

    AudioStreamBasicDescription fxOutputASBD;
    UInt32 fxOutputASBDSize = sizeof(fxOutputASBD);
    AudioUnitGetProperty([THIS mEffects], kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 0, &fxOutputASBD, &fxOutputASBDSize);

    buffer.mDataByteSize = inNumberFrames * fxOutputASBD.mBytesPerFrame;
    buffer.mNumberChannels = fxOutputASBD.mChannelsPerFrame;
    buffer.mData = malloc(buffer.mDataByteSize);

    AudioBufferList bufferList;
    bufferList.mNumberBuffers = 1;
    bufferList.mBuffers[0] = buffer;

    //TODO prender ARM y solucionar problema de memoria

    OSStatus result = AudioUnitRender([THIS mEffects], ioActionFlags, inTimeStamp, inBusNumber, inNumberFrames, &bufferList);
    [THIS hasError:result:__FILE__:__LINE__];

    memcpy(ioData, buffer.mData, buffer.mDataByteSize);

    return noErr;
}


- (void)initializeAUGraph
{
    OSStatus result = noErr;

    // create a new AUGraph
    result = NewAUGraph(&mGraph);

    AUNode outputNode;
    AUNode effectsNode;

    AudioComponentDescription effects_desc;
    effects_desc.componentType = kAudioUnitType_Effect;
    effects_desc.componentSubType = kAudioUnitSubType_LowPassFilter;
    effects_desc.componentFlags = 0;
    effects_desc.componentFlagsMask = 0;
    effects_desc.componentManufacturer = kAudioUnitManufacturer_Apple;

    AudioComponentDescription output_desc;
    output_desc.componentType = kAudioUnitType_Output;
    output_desc.componentSubType = kAudioUnitSubType_RemoteIO;
    output_desc.componentFlags = 0;
    output_desc.componentFlagsMask = 0;
    output_desc.componentManufacturer = kAudioUnitManufacturer_Apple;

    // Add nodes to the graph to hold the AudioUnits
    result = AUGraphAddNode(mGraph, &output_desc, &outputNode);
    [self hasError:result:__FILE__:__LINE__];
    result = AUGraphAddNode(mGraph, &effects_desc, &effectsNode );
    [self hasError:result:__FILE__:__LINE__];

    // Connect the effect node's output to the output node's input
    // This is no longer the case, as I need to access the buffer
    // result = AUGraphConnectNodeInput(mGraph, effectsNode, 0, outputNode, 0);
    [self hasError:result:__FILE__:__LINE__];

    // Connect the output node's input scope's output to the effectsNode input
    result = AUGraphConnectNodeInput(mGraph, outputNode, 1, effectsNode, 0);
    [self hasError:result:__FILE__:__LINE__];

    // open the graph AudioUnits
    result = AUGraphOpen(mGraph);
    [self hasError:result:__FILE__:__LINE__];

    // Get a link to the effect AU
    result = AUGraphNodeInfo(mGraph, effectsNode, NULL, &mEffects);
    [self hasError:result:__FILE__:__LINE__];

    // Same for io unit
    result = AUGraphNodeInfo(mGraph, outputNode, NULL, &ioUnit);
    [self hasError:result:__FILE__:__LINE__];

    // Enable input on io unit
    UInt32 flag = 1;
    result = AudioUnitSetProperty(ioUnit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input, 1, &flag, sizeof(flag));
    [self hasError:result:__FILE__:__LINE__];

    // Setup render callback struct
    AURenderCallbackStruct renderCallbackStruct;
    renderCallbackStruct.inputProc = &renderInput;
    renderCallbackStruct.inputProcRefCon = (__bridge void*)self;

    // Set a callback for the specified node's specified input
    result = AUGraphSetNodeInputCallback(mGraph, outputNode, 0, &renderCallbackStruct);
    [self hasError:result:__FILE__:__LINE__];

    // Get fx unit's input current stream format...
    AudioStreamBasicDescription fxInputASBD;
    UInt32 sizeOfASBD = sizeof(AudioStreamBasicDescription);

    result = AudioUnitGetProperty(mEffects, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &fxInputASBD, &sizeOfASBD);
    [self hasError:result:__FILE__:__LINE__];

    // ...and set it on the io unit's input scope's output 
    result = AudioUnitSetProperty(ioUnit,
                                  kAudioUnitProperty_StreamFormat,
                                  kAudioUnitScope_Output,
                                  1,
                                  &fxInputASBD,
                                  sizeof(fxInputASBD));
    [self hasError:result:__FILE__:__LINE__];

    // Set fx unit's output sample rate, just in case
    Float64 sampleRate = 44100.0;

    result = AudioUnitSetProperty(mEffects,
                                  kAudioUnitProperty_SampleRate,
                                  kAudioUnitScope_Output,
                                  0,
                                  &sampleRate,
                                  sizeof(sampleRate));
    [self hasError:result:__FILE__:__LINE__];

    // Once everything is set up call initialize to validate connections
    result = AUGraphInitialize(mGraph);
    [self hasError:result:__FILE__:__LINE__];
}

@end

正如我之前所说,我在调用 AudioUnitRender 时遇到了 -50 错误,而且我几乎找不到任何相关文档。

任何帮助将不胜感激。

感谢 Tim Bolstad (http://timbolstad.com/2010/03/14/core-audio-getting-started/) 提供了出色的起点教程。

最佳答案

有一些使用 RemoteIO 来播放音频缓冲区的更简单的工作示例。也许先从其中一个开始,而不是从图表开始。

关于objective-c - 编写远程 I/O 渲染回调函数时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12206875/

有关objective-c - 编写远程 I/O 渲染回调函数时遇到问题的更多相关文章

  1. ruby - 为什么我可以在 Ruby 中使用 Object#send 访问私有(private)/ protected 方法? - 2

    类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc

  2. ruby - 在 64 位 Snow Leopard 上使用 rvm、postgres 9.0、ruby 1.9.2-p136 安装 pg gem 时出现问题 - 2

    我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po

  3. ruby - 通过 rvm 升级 ruby​​gems 的问题 - 2

    尝试通过RVM将RubyGems升级到版本1.8.10并出现此错误:$rvmrubygemslatestRemovingoldRubygemsfiles...Installingrubygems-1.8.10forruby-1.9.2-p180...ERROR:Errorrunning'GEM_PATH="/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/ruby-1.9.2-p180@global:/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/rub

  4. ruby-on-rails - 渲染另一个 Controller 的 View - 2

    我想要做的是有2个不同的Controller,client和test_client。客户端Controller已经构建,我想创建一个test_clientController,我可以使用它来玩弄客户端的UI并根据需要进行调整。我主要是想绕过我在客户端中内置的验证及其对加载数据的管理Controller的依赖。所以我希望test_clientController加载示例数据集,然后呈现客户端Controller的索引View,以便我可以调整客户端UI。就是这样。我在test_clients索引方法中试过这个:classTestClientdefindexrender:template=>

  5. ruby - 在 Ruby 中编写命令行实用程序 - 2

    我想用ruby​​编写一个小的命令行实用程序并将其作为gem分发。我知道安装后,Guard、Sass和Thor等某些gem可以从命令行自行运行。为了让gem像二进制文件一样可用,我需要在我的gemspec中指定什么。 最佳答案 Gem::Specification.newdo|s|...s.executable='name_of_executable'...endhttp://docs.rubygems.org/read/chapter/20 关于ruby-在Ruby中编写命令行实用程序

  6. ruby - 主要 :Object when running build from sublime 的未定义方法 `require_relative' - 2

    我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby​​1.9+ 关于ruby-主要:Objectwhenrun

  7. ruby-on-rails - Rails HTML 请求渲染 JSON - 2

    在我的Controller中,我通过以下方式在我的index方法中支持HTML和JSON:respond_todo|format|format.htmlformat.json{renderjson:@user}end在浏览器中拉起它时,它会自然地以HTML呈现。但是,当我对/user资源进行内容类型为application/json的curl调用时(因为它是索引方法),我仍然将HTML作为响应。如何获取JSON作为响应?我还需要说明什么? 最佳答案 您应该将.json附加到请求的url,提供的格式在routes.rb的路径中定义。这

  8. ruby - 在没有 sass 引擎的情况下使用 sass 颜色函数 - 2

    我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re

  9. ruby - 通过 RVM (OSX Mountain Lion) 安装 Ruby 2.0.0-p247 时遇到问题 - 2

    我的最终目标是安装当前版本的RubyonRails。我在OSXMountainLion上运行。到目前为止,这是我的过程:已安装的RVM$\curl-Lhttps://get.rvm.io|bash-sstable检查已知(我假设已批准)安装$rvmlistknown我看到当前的稳定版本可用[ruby-]2.0.0[-p247]输入命令安装$rvminstall2.0.0-p247注意:我也试过这些安装命令$rvminstallruby-2.0.0-p247$rvminstallruby=2.0.0-p247我很快就无处可去了。结果:$rvminstall2.0.0-p247Search

  10. ruby-on-rails - 如果 Object::try 被发送到一个 nil 对象,为什么它会起作用? - 2

    如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象

随机推荐