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

cbjzeqam  于 9个月前  发布在  Swift
关注(0)|答案(1)|浏览(100)

我想在我播放音频(卡拉OK)时录制我的声音。我使用具有混响和延迟的混音器来监控我的声音
1.它与有线耳机和蓝牙配合使用效果很好
1.给予一个高音反馈使用内部麦克风在iPhone(显然)
我如何配置我的代码:
1.使用内部麦克风时不要使用监听
1.使用外部麦克风时使用监听
1.或任何其他想法,以避免反馈?(使用顶部扬声器,增益为0等)
我在某个地方读到我需要使用booster.gain = 0或类似的东西,但是新的audiokit根本没有任何booster。
现在gpt告诉我我需要更新或绕过我的混音器,这不起作用,困惑人工智能告诉我我需要将混音器的音量设置为零,这也不起作用。
如果使用内置麦克风,如何停止反馈?

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.")
    }
    
}

字符串

编辑1:我在另一个回复中读到我需要添加一个静音输入,我试过了,但我仍然听到大尖叫声。

func updateMixerStatus() {
           if isUsingInternalMic() {
                self.silencer = Fader(engine.input!, gain: 0)
               mixer.addInput(self.silencer!)
           } else {
               // Enable the mixer when not using internal mic
               //engine.output?.start()
               if mixer.hasInput(self.silencer!) {
                   mixer.removeInput(self.silencer!)
               }
           }
       }

编辑2:我试图实现一个推子建议在这里,但没有工作(甚至没有记录)https://github.com/AudioKit/Cookbook/blob/main/Cookbook/CookbookCommon/Sources/CookbookCommon/Recipes/MiniApps/Recorder.swift

z5btuh9x

z5btuh9x1#

当使用内置麦克风录音时,您可以尝试将输入节点的setVoiceProcessingEnabled属性设置为true,因为它应该取消从扬声器到麦克风的输出。但结果麦克风处理很可能会忽略通过手机播放的Karlington音频。您可以尝试在下一步中合并它。

相关问题