如何使用内置麦克风录制我的声音而没有反馈?

问题描述 投票:0回答:1

我可以用耳机听到反馈来录音,这很好

但是如果我尝试使用内置麦克风,我会听到大声的尖叫声(显然)

如何将我的代码配置为:

  1. 使用内置麦克风时请勿使用监听
  2. 使用外接麦克风时使用监听

我在某处读到我需要使用 booster.gain = 0 或类似的东西,但音频套件根本没有任何助推器。

现在 gpt 告诉我需要更新或绕过我的混音器,但这不起作用 困惑 ai 告诉我,我需要将混音器的音量设置为零,但这也不起作用。

所以我在这里对我失败的人工智能感到谦卑,向audiokit社区询问......如果使用我的内部麦克风,我怎样才能停止反馈? (我不需要听到自己的声音)

import AudioKit
import AVFoundation

class Conductor {
    
    static let sharedInstance = Conductor()
    
    // Instantiate the audio engine and Mic Input node objects
    let engine = AudioEngine()
    var mic: AudioEngine.InputNode!
    
    // Add effects for the Mic Input.
    var delay: Delay!
    var reverb: Reverb!
    let mixer = Mixer()
    var recorder: NodeRecorder!
    
    
    // MARK: Initialize the audio engine settings.
    
    init() {
        
        do {
            Settings.bufferLength = .medium //short //veryshort
            try AVAudioSession.sharedInstance().setPreferredIOBufferDuration(Settings.bufferLength.duration)
            try AVAudioSession.sharedInstance().setCategory(.playAndRecord,
                                        options: [.defaultToSpeaker, .mixWithOthers, .allowBluetoothA2DP])
            try AVAudioSession.sharedInstance().setActive(true)
        } catch let err {
            print(err)
        }
        
        // The audio signal path with be:
        // input > mic > delay > reverb > mixer > output
                
        // Mic is connected to the audio engine's input...
        
        mic = engine.input

        // Mic goes into the delay...
        
        delay = Delay(mic)
        delay.time = AUValue(0.3)
        delay.feedback = AUValue(7.0)
        delay.dryWetMix = AUValue(7.0)
        
        // Delay output goes into the reverb...
        
        reverb = Reverb(delay)
        reverb.loadFactoryPreset(.mediumRoom)
        reverb.dryWetMix = AUValue(0.4)
        
        // Reverb output goes into the mixer...

        mixer.addInput(reverb)
        
        // Engine output is connected to the mixer.
        engine.output = mixer
        
        // Uncomment the following method, if you don't want to Start and stop the audio engine via the SceneDelegate.
        startAudioEngine()
        
    }
    
    // MARK: Start and stop the audio engine via the SceneDelegate
    
    func startAudioEngine() {
        do {
            print("Audio engine was started.")
            try engine.start()
        } catch {
            Log("AudioKit did not start! \(error)")
        }
    }
    
    func prepareToRecord() {
        
        updateMixerStatus() // Update mixer status before recording
        
        //only the first time
        if !engine.avEngine.isRunning {
            do {
                print("Audio engine was started.")
                try engine.start()
            } catch {
                Log("AudioKit did not start! \(error)")
            }
        }
        
        
        //this was old code in the prev implementation
        do {
            recorder = try NodeRecorder(node: mixer, shouldCleanupRecordings: true)
        } catch {
            print("error to start recording")
        }
        
        
        
        
    }
    
    // Method to enable/disable mixer based on mic usage
//    func updateMixerStatus() {
//        if isUsingInternalMic() {
//            // Bypass or disable the mixer when using internal mic
//            mixer.volume = 0 // or use mixer.isBypassed = true if available
//        } else {
//            // Enable the mixer when not using internal mic
//            mixer.volume = 1 // or use mixer.isBypassed = false if available
//        }
//    }
    
    func updateMixerStatus() {
           if isUsingInternalMic() {
               // Bypass or disable the mixer when using internal mic
               // Check if bypass is supported, if not, set the engine's output to reverb
               engine.output = reverb
               
           } else {
               // Enable the mixer when not using internal mic
               engine.output = mixer
           }
       }
    
    func isUsingInternalMic() -> Bool {
            let route = AVAudioSession.sharedInstance().currentRoute
            return route.inputs.contains { $0.portType == .builtInMic }
        }
    
    
    func startRecording() {
            do {
                try recorder.record()
            } catch {
                print("Error starting recording: \(error)")
            }
        }
        
        func stopRecording() {
            recorder.stop()
            
            if let mainMixerNode = Conductor.sharedInstance.engine.mainMixerNode {
                    mainMixerNode.removeInput(Conductor.sharedInstance.reverb)
                    mainMixerNode.removeInput(Conductor.sharedInstance.delay)
                }
        }

    func stopAudioEngine() {
        engine.stop()
        print("Audio engine was stopped.")
    }
    
}
swiftui audiokit
1个回答
0
投票

为了防止使用内部麦克风时出现反馈,您可以更新 Conductor 类中的 updateMixerStatus() 方法,如下所示:

func updateMixerStatus() {
    if isUsingInternalMic() {
        // Bypass or disable the mixer when using internal mic
        // Check if bypass is supported, if not, set the engine's output to reverb
        engine.output = reverb
    } else {
        // Enable the mixer when not using internal mic
        engine.output = mixer
    }
}

此修改可确保当检测到内部麦克风时,引擎的输出设置为混响而不是混音器。这样,您可以防止内部麦克风馈入混音器,这应该有助于消除反馈问题。请记住在录制之前调用 updateMixerStatus() 以根据所使用的麦克风更新混音器状态。

© www.soinside.com 2019 - 2024. All rights reserved.