Mac OSX版Google语音转文本

问题描述

我正在尝试为Mac osx应用程序实现Google Speech to Text。谷歌已经提供了针对iOS的演示(Speech-gRPC-Streaming),与我在Mac OS上使用的一样。在该(iOS)演示中,他们使用了AVAudioSession.sharedInstance,在Mac OS中不可用。因此在录制时,我已经为我的应用程序使用了AVAudioEngine。

let audioEngine = AVAudioEngine()
let node = audioEngine.inputNode
        
 if( node.inputFormat(forBus: 0).channelCount == 0 ){
     print("no input device attached")
     return
 }
    
  let recordingFormat = node.outputFormat(forBus: 0)
  node.installTap(onBus: 0,bufferSize: 1024,format: recordingFormat) { buffer,_ in
            
  let data = self.toNSData(PCMBuffer: buffer)
   dispatchQueue.main.async {
      self.delegate?.speechToTextConvertedText(data as Data)
   }
}

在ViewController.swift中,我具有委托方法,在该方法中,我必须发送NSData:

func speechToTextConvertedText(_ data: Data) {
        print("ohhh okayyy : \(data)")
        audioData.append(data)

        // We recommend sending samples in 100ms chunks
        let chunkSize : Int /* bytes/chunk */ = Int(0.1 /* seconds/chunk */
          * Double(SAMPLE_RATE) /* samples/second */
          * 2 /* bytes/sample */);
        print( "audioData.length = \(audioData.length),chunkSize = \(chunkSize)" )
        if (audioData.length > chunkSize) {
            print("call Google Api")
          SpeechRecognitionService.sharedInstance.streamAudioData(audioData,completion:
            {
                
                [weak self] (response,error) in
                print("got some response : \(String(describing: response)) \(String(describing: error))")
                guard let strongSelf = self else {
                    return
                }
                
                if let error = error {
                    strongSelf.textView.stringValue = error.localizedDescription
                } else if let response = response {
                    var finished = false
                    print("Response is: \(response)")
                    for result in response.resultsArray! {
                        if let result = result as? StreamingRecognitionResult {
                            if result.isFinal {
                                finished = true
                            }
                        }
                    }
                    strongSelf.textView.stringValue = response.description
                    if finished {
                        strongSelf.stopAudio(strongSelf)
                    }
                }
          })
//          self.audioData = NSMutableData()
        }
    }

现在的问题是它没有给出任何响应,类似的功能也适用于iOS。我不知道录制数据转换是否有问题,因为我什至尝试直接使用从audioEngine接收的缓冲数据创建音频文件,但这也不起作用(每次创建0个持续时间文件,但大小合适,例如一些kb或mb)。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...