我的 AVAudioUnitEQ 与 AVAudioEngine() 的 AVAudioUnitTimePitch 不同

My AVAudioUnitEQ does not works unlike AVAudioUnitTimePitch for AVAudioEngine()

我正在玩 AVAudioEngine 以在应用程序中使用它。 我正在播放音乐时使用滑块更改音高和均衡器(增益)参数。

音高正常但均衡器不工作。 我找不到原因。

var audioEngine = AVAudioEngine()
var audioFilePlayer = AVAudioPlayerNode()
let auTimePitch = AVAudioUnitTimePitch()
var equalizer = AVAudioUnitEQ()

func startEngine(){
 guard let filePath: String = Bundle.main.path(forResource: "testMusic", ofType: "mp3") else{ return }
    let fileURL: URL = URL(fileURLWithPath: filePath)
    guard let audioFile = try? AVAudioFile(forReading: fileURL) else { return }
    
    
    let audioFormat = audioFile.processingFormat
    let audioFrameCount = UInt32(audioFile.length)
    guard let audioFileBuffer = AVAudioPCMBuffer(pcmFormat: audioFormat,
                                                 frameCapacity: audioFrameCount)  else {  return }
    
    do{
        try audioFile.read(into: audioFileBuffer)
    } catch{
        print("over")
    }
    
    let mainMixer = audioEngine.mainMixerNode
    



    auTimePitch.pitch = 1
    auTimePitch.rate = 1 


    equalizer = AVAudioUnitEQ(numberOfBands: 5)
    let bands = equalizer.bands
    let freqs = [60, 230, 910, 4000, 14000]
    
    for i in 0..<bands.count {
        bands[i].frequency  = Float(freqs[i])
        bands[i].bypass     = false
        bands[i].filterType = .parametric
        bands[i].gain = 0
        bands[i].bandwidth = 1
    }


    let format = mainMixer.outputFormat(forBus: 0)
    
    audioEngine.attach(audioFilePlayer)
    audioEngine.attach(auTimePitch)
    audioEngine.attach(equalizer)
    
    
    audioEngine.connect(audioFilePlayer,
                        to: equalizer,
                        format: format)
    
    audioEngine.connect(audioFilePlayer,
                        to:auTimePitch,
                        format: format)
    
    
    
  
    audioEngine.connect(equalizer,
                        to: mainMixer,
                        format: format)
    
    audioEngine.connect(auTimePitch,
                        to: mainMixer,
                        format: format)
    

   
        
    try? audioEngine.start()
    audioFilePlayer.play()
    
    audioFilePlayer.scheduleBuffer(audioFileBuffer, at: nil,
                                   options:AVAudioPlayerNodeBufferOptions.loops)
    
 }   

你的 AVAudioEngine 图表看起来有点奇怪(AVAudioPlayerNode 可以有两个输出吗?),但是如果你附上你的节点:

audioEngine.attach(audioFilePlayer)
audioEngine.attach(equalizer)
audioEngine.attach(auTimePitch)

并使用文件处理格式,让mainMixerNode进行格式转换

let format = audioFile.processingFormat

并以更线性的方式连接节点:

audioEngine.connect(audioFilePlayer,
                        to: equalizer,
                        format: format)

audioEngine.connect(equalizer,
                        to:auTimePitch,
                        format: format)

audioEngine.connect(auTimePitch,
                        to: mainMixer,
                        format: format)

您可以操纵均衡器,例如抑制较低频率并强调较高频率:

bands[0].gain = -50
bands[4].gain = 24