簡體   English   中英

獲取帶麥克風的錄音機的頻率

[英]Get frequency of a sound recorder with microphone

我需要計算用麥克風錄制的聲音的頻率(以赫茲為單位)。 我現在正在做的是使用AVAudioRecorder來收聽麥克風,每 0.5 秒調用一個特定的 function 的計時器。 這里有一些代碼:


class ViewController: UIViewController {
    
    var audioRecorder: AVAudioRecorder?
    var timer: Timer?

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        let permission = AVAudioSession.sharedInstance().recordPermission
        
        if permission == AVAudioSession.RecordPermission.undetermined {
            
            AVAudioSession.sharedInstance().requestRecordPermission { (granted) in
                if granted {
                    print("Permission granted!")
                } else {
                    print("Permission not granted!")
                }
            }
        } else if permission == AVAudioSession.RecordPermission.granted {
            
            do {
                try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.record)
                
                let settings = [
                    AVSampleRateKey: 44100.0,
                    AVFormatIDKey: kAudioFormatAppleLossless,
                    AVNumberOfChannelsKey: 1,
                    AVEncoderAudioQualityKey: AVAudioQuality.max
                ] as [String : Any]
                
                audioRecorder = try AVAudioRecorder.init(url: NSURL.fileURL(withPath: "dev/null"), settings: settings)
                audioRecorder?.prepareToRecord()
                audioRecorder?.isMeteringEnabled = true
                audioRecorder?.record()
                
                timer = Timer.scheduledTimer(
                    timeInterval: 0.5,
                    target: self,
                    selector: #selector(analyze),
                    userInfo: nil,
                    repeats: true
                )
            } catch (let error) {
                print("Error! \(error.localizedDescription)")
            }
        }
    }


    @objc func analyze() {
        
        audioRecorder?.updateMeters()
        
        let peak = audioRecorder?.peakPower(forChannel: 0)
        
        print("Peak : \(peak)")
        
        audioRecorder?.updateMeters()
    }
}

我不知道如何獲得以赫茲為單位的聲音頻率。 也可以為我使用第 3 方框架。

謝謝。

任何給定的錄制聲音都不會具有單一頻率。 它將混合不同振幅的頻率。

您需要對輸入聲音進行頻率分析,通常對音頻數據使用 FFT(快速傅里葉變換)。

谷歌搜索顯示這篇關於使用 Accelerate 框架進行頻率分析的文章:

http://www.myuiviews.com/2016/03/04/visualizing-audio-frequency-spectrum-on-ios-via-accelerate-vdsp-fast-fourier-transform.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM