草庐IT

ios - 应该如何在 AVAudioEngine 图中配置 AUMatrixMixer?

coder 2024-01-22 原文

我完全坚持这一点,非常感谢任何帮助...

我在 AVAudioEngine 图表中实现了一个 AUMatrixMixer,但我听不到任何声音。如果我将 AUMatrixMixer 换成 AUMultiChannelMixer,我就能听到声音。 我在直接上游节点 (AUHighPassFilter) 上安装了一个分路器,我可以看到音频被 AUMatrixMixer 提取。如果我将水龙头移到 AUMatrixMixer 的输出端,我可以看到从下一个下游节点 - AVAudioEngine 主混音器节点 - 拉取数据,但它完全没有声音......

关于 AUMatrixMixers 的评论不多,所以它可能是一些我不知道的神奇设置。作为引用来源,我收到了一封来自 Apple 技术支持的电子邮件,其中包含以下关键观察结果:

“...要在您的 AVAudioEngine 设置中使用 Matrix-Mixer,您需要使用 +instantiateWithComponentDescription:options:completionHandler: API 创建一个 AVAudioUnit,在此处找到 AVAudioUnit:

由于 AVAudioUnit 是 AVAudioNode 的子类,因此您可以在 AVAudioEngine 设置中使用利用 Matrix-Mixer 的 AVAudioUnit。您将能够进行类似于以下的设置:

AVAudioPlayerNode -> AVAudioUnit(配置了一个矩阵混音器音频单元来分割你的 channel ) -> 主混音器(你会为多路由 channel 映射配置) -> 输出...”

所以它应该可以工作。我也分析了Apple提供的示例代码:

(MatrixMixerTest https://developer.apple.com/library/mac/samplecode/MatrixMixerTest/Introduction/Intro.html ) - 这不是我想要做的,但我看不出 API 等的使用有什么不同。

创建 AUMatrixMixer(一个输入总线,两个输出总线)的代码:

private func setupMatrixMixer() {
    AVAudioUnit.instantiate(with: matrixMixerDescr, options: [.loadOutOfProcess], completionHandler: {(audioUnit, auError) in
        if let au = audioUnit {
            self.matrixMixer = au
            var error:OSStatus = noErr
            var numInputBuses:UInt32 = 1
            var numOutputBuses:UInt32 = 2
            // Input bus config
            error = AudioUnitSetProperty(self.matrixMixer.audioUnit,
                                    AudioUnitPropertyID(kAudioUnitProperty_ElementCount),
                                    AudioUnitScope(kAudioUnitScope_Input),
                                    0,
                                    &numInputBuses,
                                    UInt32(MemoryLayout<UInt32>.size))
            if error != noErr {
                assert(true, "ERROR: Setting matrix mixer number of input buses")
                return
            }
            // Output bus config
            error = AudioUnitSetProperty(self.matrixMixer.audioUnit,
                                         AudioUnitPropertyID(kAudioUnitProperty_ElementCount),
                                         AudioUnitScope(kAudioUnitScope_Output),
                                         0,
                                         &numOutputBuses,
                                         UInt32(MemoryLayout<UInt32>.size))
            if error != noErr {
                assert(true, "ERROR: Setting matrix mixer number of output buses")
                return
            }
        }
        else { trace(level: .skim, items: "ERROR: failed to create matrix mixer. Error code: \(String(describing: auError))")}
    } )
}

图形创建:

private func makeEngineConnections() {
    // Get the engine's optional singleton main mixer node
    let output = engine.mainMixerNode
    // Connect nodes
    engine.connect(player, to: timePitch, fromBus: 0, toBus: 0, format: audioFormat)
    engine.connect(timePitch, to: lowPassFilter, fromBus: 0, toBus: 0, format: audioFormat)
    engine.connect(lowPassFilter, to: highPassFilter, fromBus: 0, toBus: 0, format: audioFormat)
    engine.connect(highPassFilter, to: matrixMixer, fromBus: 0, toBus: 0, format: audioFormat)
    engine.connect(matrixMixer, to: output, fromBus: 0, toBus: 0, format: audioFormat)
    engine.connect(matrixMixer, to: output, fromBus: 1, toBus: 1, format: audioFormat)
}

设置后引擎图的转储:

________ 图描述 ________ AVAudioEngineGraph 0x1701c6450:初始化=1,运行=1,节点数=8

 ******** output chain ********

 node 0x1700a9960 {'auou' 'rioc' 'appl'}, 'I'
     inputs = 1
         (bus0) <- (bus0) 0x1740ee180, {'aumx' 'mcmx' 'appl'}, [ 2 ch,  44100 Hz, 'lpcm' (0x00000029) 32-bit little-endian float, deinterleaved]

 node 0x1740ee180 {'aumx' 'mcmx' 'appl'}, 'I'
     inputs = 2
         (bus0) <- (bus0) 0x1700f0200, {'aumx' 'mxmx' 'appl'}, [ 2 ch,  44100 Hz, 'lpcm' (0x00000029) 32-bit little-endian float, deinterleaved]
         (bus1) <- (bus1) 0x1700f0200, {'aumx' 'mxmx' 'appl'}, [ 2 ch,  44100 Hz, 'lpcm' (0x00000029) 32-bit little-endian float, deinterleaved]
     outputs = 1
         (bus0) -> (bus0) 0x1700a9960, {'auou' 'rioc' 'appl'}, [ 2 ch,  44100 Hz, 'lpcm' (0x00000029) 32-bit little-endian float, deinterleaved]

 node 0x1700f0200 {'aumx' 'mxmx' 'appl'}, 'I'
     inputs = 1
         (bus0) <- (bus0) 0x1740ee100, {'aufx' 'hpas' 'appl'}, [ 2 ch,  44100 Hz, 'lpcm' (0x00000029) 32-bit little-endian float, deinterleaved]
     outputs = 2
         (bus0) -> (bus0) 0x1740ee180, {'aumx' 'mcmx' 'appl'}, [ 2 ch,  44100 Hz, 'lpcm' (0x00000029) 32-bit little-endian float, deinterleaved]
         (bus1) -> (bus1) 0x1740ee180, {'aumx' 'mcmx' 'appl'}, [ 2 ch,  44100 Hz, 'lpcm' (0x00000029) 32-bit little-endian float, deinterleaved]

 node 0x1740ee100 {'aufx' 'hpas' 'appl'}, 'I'
     inputs = 1
         (bus0) <- (bus0) 0x1740ee700, {'aufx' 'lpas' 'appl'}, [ 2 ch,  44100 Hz, 'lpcm' (0x00000029) 32-bit little-endian float, deinterleaved]
     outputs = 1
         (bus0) -> (bus0) 0x1700f0200, {'aumx' 'mxmx' 'appl'}, [ 2 ch,  44100 Hz, 'lpcm' (0x00000029) 32-bit little-endian float, deinterleaved]

 node 0x1740ee700 {'aufx' 'lpas' 'appl'}, 'I'
     inputs = 1
         (bus0) <- (bus0) 0x1740ee480, {'aufc' 'nutp' 'appl'}, [ 2 ch,  44100 Hz, 'lpcm' (0x00000029) 32-bit little-endian float, deinterleaved]
     outputs = 1
         (bus0) -> (bus0) 0x1740ee100, {'aufx' 'hpas' 'appl'}, [ 2 ch,  44100 Hz, 'lpcm' (0x00000029) 32-bit little-endian float, deinterleaved]

 node 0x1740ee480 {'aufc' 'nutp' 'appl'}, 'I'
     inputs = 1
         (bus0) <- (bus0) 0x174198fc0, {'augn' 'sspl' 'appl'}, [ 2 ch,  44100 Hz, 'lpcm' (0x00000029) 32-bit little-endian float, deinterleaved]
     outputs = 1
         (bus0) -> (bus0) 0x1740ee700, {'aufx' 'lpas' 'appl'}, [ 2 ch,  44100 Hz, 'lpcm' (0x00000029) 32-bit little-endian float, deinterleaved]

 node 0x174198fc0 {'augn' 'sspl' 'appl'}, 'I'
     outputs = 1
         (bus0) -> (bus0) 0x1740ee480, {'aufc' 'nutp' 'appl'}, [ 2 ch,  44100 Hz, 'lpcm' (0x00000029) 32-bit little-endian float, deinterleaved]

 ******** other nodes ********

 node 0x1700ef000 {'aumx' 'mcmx' 'appl'}, 'U'

AUMatrixMixer 内部的设置有点乏味。总结:

  • 启用输入总线
  • 在每个输入 channel (其中 2 个)上设置音量
  • 启用输出总线(实际上,这是多余的,因为它们都是永久启用的)
  • 在每个输出 channel (其中 4 个)上设置音量
  • 在每个交叉点上设置音量(我现在将所有设置为 1(最大音量))- 其中 8 个
  • 设置混音器的全局音量(1 个设置)

这是执行上述操作后的内部状态转储:

Matrix dimensions: [2, 4]
Input element count: 1
Input channel 0 volume: 1.0
Input channel 1 volume: 1.0
Output element count: 2
Output channel 0 volume: 1.0
Output channel 1 volume: 1.0
Output channel 2 volume: 1.0
Output channel 3 volume: 1.0
Crosspoint volumes: [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]
Input 0 enabled parameter: 1.0
Output 0 enabled parameter: 1.0
Output 1 enabled parameter: 1.0

如您所见,一切都已启用,所有音量都设置为最大但没有输出声音....

如上所述,通过点击不同节点的输出,我已经验证了良好的数据正在从高通滤波器移动到矩阵混音器,但在矩阵混音器和主混音器之间移动的是静音混合器节点。

有谁知道我还需要做些什么才能从中获得声音?

问候, 空调

最佳答案

几个星期以来,我一直面临同样的情况。刚才,我正在写一段示例代码来询问 Apple Code-Level 支持。我在准备发送时测试了它,假设它不会像往常一样工作,但令人惊讶的是它确实有效!我认为,像 AUGraph 一样,必须有一些特定的顺序来设置流格式、连接节点等,这是它正常工作所必需的。 (而且,与 AUGraph 一样,文档并没有准确解释该顺序是什么。)所以我不太确定这次我做了什么不同的事情,但至少它现在对我有用。

所以这是一个成功使用带有 AVAudioEngine 的矩阵混音器的准系统示例:

NSURL *audioURL = /*an audio URL*/
AVAudioFile *file = [[AVAudioFile alloc] initForReading:audioURL error:nil];
AVAudioPlayerNode *audioPlayer = [[AVAudioPlayerNode alloc] init];

_engine = [[AVAudioEngine alloc] init];

[_engine attachNode:audioPlayer];

AudioComponentDescription mixerDesc;
mixerDesc.componentType = kAudioUnitType_Mixer;
mixerDesc.componentSubType = kAudioUnitSubType_MatrixMixer;
mixerDesc.componentManufacturer = kAudioUnitManufacturer_Apple;
mixerDesc.componentFlags = kAudioComponentFlag_SandboxSafe;

[AVAudioUnit instantiateWithComponentDescription:mixerDesc options:kAudioComponentInstantiation_LoadInProcess completionHandler:^(__kindof AVAudioUnit * _Nullable mixerUnit, NSError * _Nullable error) {

    [_engine attachNode:mixerUnit];

    /*Give the mixer one input bus and one output bus*/
    UInt32 inBuses = 1;
    UInt32 outBuses = 1;
    AudioUnitSetProperty(mixerUnit.audioUnit, kAudioUnitProperty_ElementCount, kAudioUnitScope_Input, 0, &inBuses, sizeof(UInt32));
    AudioUnitSetProperty(mixerUnit.audioUnit, kAudioUnitProperty_ElementCount, kAudioUnitScope_Output, 0, &outBuses, sizeof(UInt32));

    /*Set the mixer's input format to have 2 channels*/
    UInt32 inputChannels = 2;
    AudioStreamBasicDescription mixerFormatIn;
    UInt32 size;
    AudioUnitGetProperty(mixerUnit.audioUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &mixerFormatIn, &size);
    mixerFormatIn.mChannelsPerFrame = inputChannels;
    AudioUnitSetProperty(mixerUnit.audioUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &mixerFormatIn, size);

    /*Set the mixer's output format to have 2 channels*/
    UInt32 outputChannels = 2;
    AudioStreamBasicDescription mixerFormatOut;
    AudioUnitGetProperty(mixerUnit.audioUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 0, &mixerFormatOut, &size);
    mixerFormatOut.mChannelsPerFrame = outputChannels;

    AudioUnitSetProperty(mixerUnit.audioUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 0, &mixerFormatOut, size);

    /*Connect the nodes*/
    [_engine connect:audioPlayer to:mixerUnit format:nil];
    [_engine connect:mixerUnit to:_engine.outputNode format:nil];

    /*Start the engine*/
    [_engine startAndReturnError:nil];

    /*Play the audio file*/
    [audioPlayer scheduleFile:file atTime:nil completionHandler:nil];
    [audioPlayer play];

    /*Set all matrix volumes to 1*/

    /*Set the master volume*/
    AudioUnitSetParameter(mixerUnit.audioUnit, kMatrixMixerParam_Volume, kAudioUnitScope_Global, 0xFFFFFFFF, 1.0, 0);

    for(UInt32 i = 0; i < inputChannels; i++) {

        /*Set input volumes*/
        AudioUnitSetParameter(mixerUnit.audioUnit, kMatrixMixerParam_Volume, kAudioUnitScope_Input, i, 1.0, 0);

        for(UInt32 j = 0; j < outputChannels; j++) {
            /*Set output volumes (only one outer iteration necessary)*/
            if(i == 0) {
                AudioUnitSetParameter(mixerUnit.audioUnit, kMatrixMixerParam_Volume, kAudioUnitScope_Output, j, 1.0, 0);
            }

            /*Set cross point volumes - 1.0 for corresponding
             inputs/outputs, otherwise 0.0*/
            UInt32 crossPoint = (i << 16) | (j & 0x0000FFFF);
            AudioUnitSetParameter(mixerUnit.audioUnit, kMatrixMixerParam_Volume, kAudioUnitScope_Global, crossPoint, (i == j) ? 1.0 : 0.0, 0);
        }

    }

    /*If you want to verify it's working, try something like this to silence only one channel of audio
    AudioUnitSetParameter(mixerUnit.audioUnit, kMatrixMixerParam_Volume, kAudioUnitScope_Output, 0, 0.0, 0);
    */
}];

关于ios - 应该如何在 AVAudioEngine 图中配置 AUMatrixMixer?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48059405/

有关ios - 应该如何在 AVAudioEngine 图中配置 AUMatrixMixer?的更多相关文章

  1. ruby - 如何在 Ruby 中顺序创建 PI - 2

    出于纯粹的兴趣,我很好奇如何按顺序创建PI,而不是在过程结果之后生成数字,而是让数字在过程本身生成时显示。如果是这种情况,那么数字可以自行产生,我可以对以前看到的数字实现垃圾收集,从而创建一个无限系列。结果只是在Pi系列之后每秒生成一个数字。这是我通过互联网筛选的结果:这是流行的计算机友好算法,类机器算法:defarccot(x,unity)xpow=unity/xn=1sign=1sum=0loopdoterm=xpow/nbreakifterm==0sum+=sign*(xpow/n)xpow/=x*xn+=2sign=-signendsumenddefcalc_pi(digits

  2. ruby - 如何在 buildr 项目中使用 Ruby 代码? - 2

    如何在buildr项目中使用Ruby?我在很多不同的项目中使用过Ruby、JRuby、Java和Clojure。我目前正在使用我的标准Ruby开发一个模拟应用程序,我想尝试使用Clojure后端(我确实喜欢功能代码)以及JRubygui和测试套件。我还可以看到在未来的不同项目中使用Scala作为后端。我想我要为我的项目尝试一下buildr(http://buildr.apache.org/),但我注意到buildr似乎没有设置为在项目中使用JRuby代码本身!这看起来有点傻,因为该工具旨在统一通用的JVM语言并且是在ruby中构建的。除了将输出的jar包含在一个独特的、仅限ruby​​

  3. ruby - 什么是填充的 Base64 编码字符串以及如何在 ruby​​ 中生成它们? - 2

    我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%

  4. ruby - 检查 "command"的输出应该包含 NilClass 的意外崩溃 - 2

    为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar

  5. ruby-on-rails - 如何在 ruby​​ 中使用两个参数异步运行 exe? - 2

    exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby​​中使用两个参数异步运行exe吗?我已经尝试过ruby​​命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何ruby​​gems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除

  6. ruby - 如何在续集中重新加载表模式? - 2

    鉴于我有以下迁移:Sequel.migrationdoupdoalter_table:usersdoadd_column:is_admin,:default=>falseend#SequelrunsaDESCRIBEtablestatement,whenthemodelisloaded.#Atthispoint,itdoesnotknowthatusershaveais_adminflag.#Soitfails.@user=User.find(:email=>"admin@fancy-startup.example")@user.is_admin=true@user.save!ende

  7. ruby - 如何在 Ruby 中拆分参数字符串 Bash 样式? - 2

    我正在为一个项目制作一个简单的shell,我希望像在Bash中一样解析参数字符串。foobar"helloworld"fooz应该变成:["foo","bar","helloworld","fooz"]等等。到目前为止,我一直在使用CSV::parse_line,将列分隔符设置为""和.compact输出。问题是我现在必须选择是要支持单引号还是双引号。CSV不支持超过一个分隔符。Python有一个名为shlex的模块:>>>shlex.split("Test'helloworld'foo")['Test','helloworld','foo']>>>shlex.split('Test"

  8. ruby-on-rails - 独立 ruby​​ 脚本的配置文件 - 2

    我有一个在Linux服务器上运行的ruby​​脚本。它不使用rails或任何东西。它基本上是一个命令行ruby​​脚本,可以像这样传递参数:./ruby_script.rbarg1arg2如何将参数抽象到配置文件(例如yaml文件或其他文件)中?您能否举例说明如何做到这一点?提前谢谢你。 最佳答案 首先,您可以运行一个写入YAML配置文件的独立脚本:require"yaml"File.write("path_to_yaml_file",[arg1,arg2].to_yaml)然后,在您的应用中阅读它:require"yaml"arg

  9. ruby - 如何在 Lion 上安装 Xcode 4.6,需要用 RVM 升级 ruby - 2

    我实际上是在尝试使用RVM在我的OSX10.7.5上更新ruby,并在输入以下命令后:rvminstallruby我得到了以下回复:Searchingforbinaryrubies,thismighttakesometime.Checkingrequirementsforosx.Installingrequirementsforosx.Updatingsystem.......Errorrunning'requirements_osx_brew_update_systemruby-2.0.0-p247',pleaseread/Users/username/.rvm/log/138121

  10. ruby-on-rails - 如何在 ruby​​ 交互式 shell 中有多行? - 2

    这可能是个愚蠢的问题。但是,我是一个新手......你怎么能在交互式ruby​​shell中有多行代码?好像你只能有一条长线。按回车键运行代码。无论如何我可以在不运行代码的情况下跳到下一行吗?再次抱歉,如果这是一个愚蠢的问题。谢谢。 最佳答案 这是一个例子:2.1.2:053>a=1=>12.1.2:054>b=2=>22.1.2:055>a+b=>32.1.2:056>ifa>b#Thecode‘if..."startsthedefinitionoftheconditionalstatement.2.1.2:057?>puts"f

随机推荐