gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:30:32 25 4
gpt4 key购买 nike

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

我在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com