如何提高 iOS 应用程序中语音转文本的速度?

Saa*_*mer 1 speech-recognition avfoundation speech-to-text ios swift

我使用Apple 的Scrumdinger示例在我的应用程序中创建了语音转文本功能。正如您在下面的 gif 中看到的,在我开始讲话几秒钟后,文本开始输入: 缓慢行为的 GIF

与 Notes 等本机应用程序上的语音转文本功能相比,语音转文本功能非常慢,而且用户讨厌这种体验。如何提高响应能力?这是我使用的代码,可能与之相关,但我尝试更改一些值,但没有成功:

private static func prepareEngine() throws -> (AVAudioEngine, SFSpeechAudioBufferRecognitionRequest) {
  let audioEngine = AVAudioEngine()
  
  let request = SFSpeechAudioBufferRecognitionRequest()
  request.shouldReportPartialResults = true
  
  // Configure the audio session for the app.
  let audioSession = AVAudioSession.sharedInstance()
  try audioSession.setCategory(.record, mode: .measurement, options: .duckOthers)
  try audioSession.setActive(true, options: .notifyOthersOnDeactivation)
  let inputNode = audioEngine.inputNode
  
  // Configure the microphone input.
  let recordingFormat = inputNode.outputFormat(forBus: 0)
  inputNode.installTap(onBus: 0, bufferSize: 1024, format: recordingFormat) { (buffer: AVAudioPCMBuffer, when: AVAudioTime) in
      request.append(buffer)
  }
  audioEngine.prepare()
  try audioEngine.start()
  
  return (audioEngine, request)
}
Run Code Online (Sandbox Code Playgroud)

此外,您可以在此网站上下载 TestFlight 版本,并在Github上查看整个应用程序的源代码

小智 7

找到该DispatchQueue函数调用,尝试将qos参数从.background改为.userInteractive示例代码有错误,已报告给Apple。