SFSpeechRecognizerで音声認識をしながらAVSpeechSynthesizerを使って音声の出力をしようとしたんですけど、音量がすごく小さくなってしまいます。何か解決方法はありませんか?解決方法も色々調べたのですがあまりパッとしなかったので質問させてもらいます。
import UIKit import Speech class recording{ private var recognitionRequest = SFSpeechAudioBufferRecognitionRequest() private var speechRecognizer = SFSpeechRecognizer(locale: Locale(identifier: "ja-JP"))! private var recognitionTask: SFSpeechRecognitionTask? private var audioEngine = AVAudioEngine() private let synthesizer = AVSpeechSynthesizer() public var lang = "ja-JP" public var text = "肘です" override func viewDidLoad() { super.viewDidLoad() start() } func start() { recognitionRequest = SFSpeechAudioBufferRecognitionRequest() speechRecognizer = SFSpeechRecognizer(locale: Locale(identifier: lang))! audioEngine = AVAudioEngine() let recordingFormat = audioEngine.inputNode.outputFormat(forBus: 0) audioEngine.inputNode.installTap(onBus: 0, bufferSize: 1024, format: recordingFormat) { (buffer: AVAudioPCMBuffer, when: AVAudioTime) in self.recognitionRequest.append(buffer) } try! audioEngine.start() recognitionTask = speechRecognizer.recognitionTask(with: recognitionRequest) { result, error in print(result?.bestTranscription.formattedString ?? "") let speechresult=result!.bestTranscription.formattedString let words = speechresult.suffix(4) let trigger = ["ここどこ","これ何","ここは","これなに","これは","これどこ"] for i in trigger{ if words.lowercased().contains(i){ self.play() } } if let error = error { print("ERROR!") print(error.localizedDescription) } } DispatchQueue.main.asyncAfter(deadline: .now() + 55.0, execute: { self.recognitionTask?.cancel() self.recognitionTask?.finish() self.audioEngine.stop() print("RESTART") self.start() }) } public func play() { // 発音を準備する //let utterance = AVSpeechUtterance.init(string: self.text) let utterance = AVSpeechUtterance.init(string: text) let voice = AVSpeechSynthesisVoice.init(language: lang) utterance.voice = voice // 再生する synthesizer.speak(utterance) } }
トリガーとなる言葉を認識したらplay()が呼び出される仕様になっています。
初心者ですので色々拙いかもしれませんがお願いします。
あなたの回答
tips
プレビュー