禁用 AudioUnit AGC 失败

Posted

技术标签:

【中文标题】禁用 AudioUnit AGC 失败【英文标题】:Disabling AudioUnit AGC fails 【发布时间】:2020-07-13 21:20:54 【问题描述】:

我正在尝试禁用 AGC 并绕过 AudioUnit 的语音过滤器,但它无法设置这两个属性:

    private func setupAudioUnit() 
        
        var componentDesc:AudioComponentDescription = AudioComponentDescription(
            componentType: OSType(kAudioUnitType_Output),
            componentSubType: OSType(kAudioUnitSubType_RemoteIO), // Always this for ios.
            componentManufacturer: OSType(kAudioUnitManufacturer_Apple),
            componentFlags: 0,
            componentFlagsMask: 0)
        
        var osErr: OSStatus = 0
        
        // Get an audio component matching our description.
        let component: AudioComponent! = AudioComponentFindNext(nil, &componentDesc)
        assert(component != nil, "Couldn't find a default component")
        
        // Create an instance of the AudioUnit
        var tempAudioUnit: AudioUnit?
        osErr = AudioComponentInstanceNew(component, &tempAudioUnit)
        self.audioUnit = tempAudioUnit
        
        assert(osErr == noErr, "*** AudioComponentInstanceNew err \(osErr)")
        
        // Enable I/O for input.
        var one: UInt32 = 1
        var off: UInt32 = 0
        
        osErr = AudioUnitSetProperty(audioUnit,
            kAudioOutputUnitProperty_EnableIO,
            kAudioUnitScope_Input,
            inputBus,
            &one,
            UInt32(MemoryLayout<UInt32>.size))
        
        /// Bypass Voice Processing
        osErr = AudioUnitSetProperty(audioUnit,
            kAUVoiceIOProperty_BypassVoiceProcessing,
            kAudioUnitScope_Global,
            inputBus,
            &one,
            UInt32(MemoryLayout<UInt32>.size))
        print("AudioUnitSetProperty returned code:\(osErr)")
        
        /// Disable AGC
        osErr = AudioUnitSetProperty(audioUnit,
            kAUVoiceIOProperty_VoiceProcessingEnableAGC,
            kAudioUnitScope_Global,
            inputBus,
            &off,
            UInt32(MemoryLayout<UInt32>.size))
        print("AudioUnitSetProperty returned code:\(osErr)")
        assert(osErr == noErr, "*** AudioUnitSetProperty err \(osErr)")
        
        // Set format to 32 bit, floating point, linear PCM
        var streamFormatDesc:AudioStreamBasicDescription = AudioStreamBasicDescription(
            mSampleRate:        Double(sampleRate),
            mFormatID:          kAudioFormatLinearPCM,
            mFormatFlags:       kAudioFormatFlagsNativeFloatPacked | kAudioFormatFlagIsNonInterleaved, // floating point data - docs say this is fastest
            mBytesPerPacket:    4,
            mFramesPerPacket:   1,
            mBytesPerFrame:     4,
            mChannelsPerFrame:  UInt32(self.numberOfChannels),
            mBitsPerChannel:    4 * 8,
            mReserved: 0
        )
        
        // Set format for input and output busses
        osErr = AudioUnitSetProperty(audioUnit,
            kAudioUnitProperty_StreamFormat,
            kAudioUnitScope_Input, outputBus,
            &streamFormatDesc,
            UInt32(MemoryLayout<AudioStreamBasicDescription>.size))
        assert(osErr == noErr, "*** AudioUnitSetProperty err \(osErr)")
        
        osErr = AudioUnitSetProperty(audioUnit,
            kAudioUnitProperty_StreamFormat,
            kAudioUnitScope_Output,
            inputBus,
            &streamFormatDesc,
            UInt32(MemoryLayout<AudioStreamBasicDescription>.size))
        assert(osErr == noErr, "*** AudioUnitSetProperty err \(osErr)")


        // Set up our callback.
        var inputCallbackStruct = AURenderCallbackStruct(inputProc: recordingCallback, inputProcRefCon: UnsafeMutableRawPointer(Unmanaged.passUnretained(self).toOpaque()))
        osErr = AudioUnitSetProperty(audioUnit,
            AudioUnitPropertyID(kAudioOutputUnitProperty_SetInputCallback),
            AudioUnitScope(kAudioUnitScope_Global),
            inputBus,
            &inputCallbackStruct,
            UInt32(MemoryLayout<AURenderCallbackStruct>.size))
        assert(osErr == noErr, "*** AudioUnitSetProperty err \(osErr)")
        
        // Ask CoreAudio to allocate buffers for us on render. (This is true by default but just to be explicit about it...)
        osErr = AudioUnitSetProperty(audioUnit,
            AudioUnitPropertyID(kAudioUnitProperty_ShouldAllocateBuffer),
            AudioUnitScope(kAudioUnitScope_Output),
            inputBus,
            &one,
            UInt32(MemoryLayout<UInt32>.size))
        assert(osErr == noErr, "*** AudioUnitSetProperty err \(osErr)")
    

在这里我得到无效的属性响应:

/// Bypass Voice Processing
osErr = AudioUnitSetProperty(audioUnit,
    kAUVoiceIOProperty_BypassVoiceProcessing,
    kAudioUnitScope_Global,
    inputBus,
    &one,
    UInt32(MemoryLayout<UInt32>.size))
print("AudioUnitSetProperty returned code:\(osErr)")
// AudioUnitSetProperty returned code:-10879

/// Disable AGC
osErr = AudioUnitSetProperty(audioUnit,
    kAUVoiceIOProperty_VoiceProcessingEnableAGC,
    kAudioUnitScope_Global,
    inputBus,
    &off,
    UInt32(MemoryLayout<UInt32>.size))
print("AudioUnitSetProperty returned code:\(osErr)")
// AudioUnitSetProperty returned code:-10879

我做错了吗?

【问题讨论】:

【参考方案1】:

我没有在 iOS 上使用过 CoreAudio,也没有使用过语音处理 AU,但看起来问题出在您指定的子类型上。

有问题的属性似乎特定于kAudioUnitSubType_VoiceProcessingIO 的子类型,但您正在实例化子类型kAudioUnitSubType_RemoteIO。如果您使用前一个子类型,那么属性应该是有效的。

【讨论】:

以上是关于禁用 AudioUnit AGC 失败的主要内容,如果未能解决你的问题,请参考以下文章

如何在 HTC EVO(超音速)上禁用 AGC?

如何在 iPhone 内置麦克风上禁用 AEC、AGC 和 HPF

音频单元,设置格式失败并显示 -10581

agc045_d Lamps and Buttons

agc004dTeleporter

混音器 AudioUnit 到 RemoteIO AudioUnit