使用 Avaudioengine 的低通滤波器 + 采样率转换 iOS

Low Pass filter + sample rate conversion using Avaudioengine iOS

我们正在进行一个项目,该项目允许我们使用一些低通滤波器和高通滤波器以 5k Hz 的采样率从麦克风录制一些声音。

我们正在使用什么

为此,我们正在使用 AvaudioEngine

我们正在使用 AVAudioConverter 来降低采样率。

我们正在使用 AVAudioUnitEQ 作为低通和高通滤波器。

代码

let bus = 0
let inputNode = engine.inputNode

let equalizer = AVAudioUnitEQ(numberOfBands: 2)

equalizer.bands[0].filterType = .lowPass
equalizer.bands[0].frequency = 3000
equalizer.bands[0].bypass = false

equalizer.bands[1].filterType = .highPass
equalizer.bands[1].frequency = 1000
equalizer.bands[1].bypass = false
engine.attach(equalizer) //Attach equalizer

// Connect nodes
engine.connect(inputNode, to: equalizer, format: inputNode.inputFormat(forBus: 0))
engine.connect(equalizer, to: engine.mainMixerNode, format: inputNode.inputFormat(forBus: 0))
engine.connect(engine.mainMixerNode, to: engine.outputNode, format: inputNode.inputFormat(forBus: 0))

let outputFormat = AVAudioFormat(commonFormat: .pcmFormatInt16,
                                            sampleRate: 5000,
                                            channels: 1,
                                            interleaved: false)!

// Converter to downgrade sample rate
guard let converter: AVAudioConverter = AVAudioConverter(from: inputNode.inputFormat(forBus: 0), to: outputFormat) else {
           print("Can't convert in to this format")
           return
       }

engine.mainMixerNode.installTap(onBus: bus, bufferSize: 2688, format: engine.mainMixerNode.outputFormat(forBus: 0)) { (buffer, time) in
           
     var newBufferAvailable = true
           
     let inputCallback: AVAudioConverterInputBlock = { inNumPackets, outStatus in
           if newBufferAvailable {
                outStatus.pointee = .haveData
                newBufferAvailable = false
                return buffer
           } else {
                outStatus.pointee = .noDataNow
                return nil
           }
     }
           
           
     let convertedBuffer = AVAudioPCMBuffer(pcmFormat: outputFormat, frameCapacity: AVAudioFrameCount(outputFormat.sampleRate) * buffer.frameLength / AVAudioFrameCount(buffer.format.sampleRate))!

           var error: NSError?
           let status = converter.convert(to: convertedBuffer, error: &error, withInputFrom: inputCallback)
           assert(status != .error)

           
           if status == .haveData {
             // Process with converted buffer
           }
            
       }
       
       engine.prepare()
       
       do {
           try engine.start()
       } catch {
           print("Can't start the engine: \(error)")
       }

问题

低通和高通滤波器不工作。

替代方法

为了检查代码是否正常工作,我们添加了混响效果而不是低通滤波器。 混响效果(使用 AVAudioUnitReverb)使用相同的代码。

任何人都可以帮助我在应用低通滤波器时哪里做错了吗?

我认为此代码的主要问题是 AVAudioConverter 在调用 engine.prepare() 之前创建,这可以而且将会更改 mainMixerNode 输出格式。除此之外,还有 mainMixerNodeoutputNode 的冗余连接,以及可能不正确的格式 - mainMixerNode 被记录为自动创建并“按需”连接到输出节点.水龙头也不需要格式。

let bus = 0
let inputNode = engine.inputNode

let equalizer = AVAudioUnitEQ(numberOfBands: 2)

equalizer.bands[0].filterType = .lowPass
equalizer.bands[0].frequency = 3000
equalizer.bands[0].bypass = false

equalizer.bands[1].filterType = .highPass
equalizer.bands[1].frequency = 1000
equalizer.bands[1].bypass = false
engine.attach(equalizer) //Attach equalizer

// Connect nodes
engine.connect(inputNode, to: equalizer, format: inputNode.inputFormat(forBus: 0))
engine.connect(equalizer, to: engine.mainMixerNode, format: inputNode.inputFormat(forBus: 0))

// call before creating converter because this changes the mainMixer's output format
engine.prepare()

let outputFormat = AVAudioFormat(commonFormat: .pcmFormatInt16,
                                 sampleRate: 5000,
                                 channels: 1,
                                 interleaved: false)!

// Downsampling converter
guard let converter: AVAudioConverter = AVAudioConverter(from: engine.mainMixerNode.outputFormat(forBus: 0), to: outputFormat) else {
    print("Can't convert in to this format")
    return
}

engine.mainMixerNode.installTap(onBus: bus, bufferSize: 2688, format: nil) { (buffer, time) in
    var newBufferAvailable = true
    
    let inputCallback: AVAudioConverterInputBlock = { inNumPackets, outStatus in
        if newBufferAvailable {
            outStatus.pointee = .haveData
            newBufferAvailable = false
            return buffer
        } else {
            outStatus.pointee = .noDataNow
            return nil
        }
    }
    
    
    let convertedBuffer = AVAudioPCMBuffer(pcmFormat: outputFormat, frameCapacity: AVAudioFrameCount(outputFormat.sampleRate) * buffer.frameLength / AVAudioFrameCount(buffer.format.sampleRate))!
    
    var error: NSError?
    let status = converter.convert(to: convertedBuffer, error: &error, withInputFrom: inputCallback)
    assert(status != .error)
    
    
    if status == .haveData {
        // Process with converted buffer
    }
}

do {
    try engine.start()
} catch {
    print("Can't start the engine: \(error)")
}
override func viewDidLoad() {
    super.viewDidLoad()
    lableItem.text = "Select Frequency"
    setUpDropdown()
    navigationItem.title = "High Pass Filter"
    
    do{
        try audioSession.setCategory(.playAndRecord, mode: .default, options: [.mixWithOthers, .defaultToSpeaker,.allowBluetoothA2DP,.allowAirPlay,.allowBluetooth])
        try audioSession.setActive(true)
    } catch{
        print(error.localizedDescription)
    }
    
    let bus = 0
    let inputNode = engine.inputNode
    
    let equalizer = AVAudioUnitEQ(numberOfBands: 2)
    
    equalizer.bands[0].filterType = .highPass
    equalizer.bands[0].frequency = 20000.0
    equalizer.bands[0].bypass = false
    
    equalizer.bands[1].filterType = .lowPass
    equalizer.bands[1].frequency = 1000.0
    equalizer.bands[1].bypass = false
    engine.attach(equalizer) //Attach equalizer
    
    
    // Connect nodes
    engine.connect(inputNode, to: equalizer, format: inputNode.inputFormat(forBus: 0))
    engine.connect(equalizer, to: engine.mainMixerNode, format: inputNode.inputFormat(forBus: 0))
    
    // call before creating converter because this changes the mainMixer's output format
    engine.prepare()
    
    let outputFormat = AVAudioFormat(commonFormat: .pcmFormatInt32,
                                     sampleRate: 44100,
                                     channels: 1,
                                     interleaved: false)!
    // Downsampling converter
    guard let converter: AVAudioConverter = AVAudioConverter(from: engine.mainMixerNode.outputFormat(forBus: 0), to: outputFormat) else {
        print("Can't convert in to this format")
        return
    }
    
    engine.mainMixerNode.installTap(onBus: bus, bufferSize: 2688, format: nil) { (buffer, time) in
        var newBufferAvailable = true
        
        let inputCallback: AVAudioConverterInputBlock = { inNumPackets, outStatus in
            if newBufferAvailable {
                outStatus.pointee = .haveData
                newBufferAvailable = false
                
                return buffer
            } else {
                outStatus.pointee = .noDataNow
                return nil
            }
        }
        
        
        let convertedBuffer = AVAudioPCMBuffer(pcmFormat: outputFormat, frameCapacity: AVAudioFrameCount(outputFormat.sampleRate) * buffer.frameLength / AVAudioFrameCount(buffer.format.sampleRate))!
        
        var error: NSError?
        let status = converter.convert(to: convertedBuffer, error: &error, withInputFrom: inputCallback)
        assert(status != .error)
        
        
        if status == .haveData {
            // Process with converted buffer
        }
    }
    
    do {
        try engine.start()
    } catch {
        print("Can't start the engine: \(error)")
    }
    
}