如何从AVAudioEngine获取声音的频率

时间:2019-07-01 11:38:49

标签: ios swift avaudioengine

我正在为应用程序实现语音识别模块。它工作正常,但是我还需要做一些其他事情。例如,我需要知道用户是在说话还是在喊。我知道,我可以通过知道声音的频率来实现。这是我的实现方式:

let audioEngine = AVAudioEngine()
let speechRecognizer: SFSpeechRecognizer? = SFSpeechRecognizer()
let request = SFSpeechAudioBufferRecognitionRequest()
var recognitionTask = SFSpeechRecognitionTask()

func recordAndRecognizeSpeech() {
    let node = audioEngine.inputNode
    let recordingFormat = node.outputFormat(forBus: 0)
    node.installTap(onBus: 0, bufferSize: 1024, format: recordingFormat) { (buffer, _) in
        self.request.append(buffer)
    }

    audioEngine.prepare()
    do {
        try audioEngine.start()
    } catch {
        return print(error)
    }

    guard let myRecoginizer = SFSpeechRecognizer() else {
        return
    }

    if !myRecoginizer.isAvailable {
        return
    }

    recognitionTask = (speechRecognizer?.recognitionTask(with: request, resultHandler: { (result, error) in
        //Handling speech recognition tasks here
    }))!

}

这对于语音识别效果很好,但是如何获得声音的频率或振幅值?

0 个答案:

没有答案