首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >禁用AudioUnit AGC失败

禁用AudioUnit AGC失败
EN

Stack Overflow用户
提问于 2020-07-14 05:20:55
回答 1查看 181关注 0票数 0

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

代码语言:javascript
复制
    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)")
    }
}

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

代码语言:javascript
复制
/// 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

我做错了什么吗?

EN

回答 1

Stack Overflow用户

发布于 2020-07-20 01:47:02

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

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

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

https://stackoverflow.com/questions/62884447

复制
相关文章

相似问题

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