AVSpeechSynthesizer output as file?

AVSpeechSynthesizer has a fairly simple API that does not support saving the embedded audio file.

I am wondering if there is a way around this - perhaps recording the output when it was playing quietly, for playback later? Or something more effective.

+6
source share
3 answers

AVSpeechSynthesizer does not currently support this. Never receive an audio file with AVSpeechSynthesizer . I tried this a few weeks ago for one of my applications and found out that this was not possible, and nothing has changed for AVSpeechSynthesizer in iOS 8.

I also thought of recording the sound during its playback, but there are so many drawbacks with the approach that the user can use headphones, the system sound can be low or dumb, he can catch another external sound, so it is not recommended to go with this approach.

+3
source

It is finally possible, in iOS 13 AVSpeechSynthesizer now has write (_: toBufferCallback :) :

 let synthesizer = AVSpeechSynthesizer() let utterance = AVSpeechUtterance(string: "test 123") utterance.voice = AVSpeechSynthesisVoice(language: "en") var output: AVAudioFile? synthesizer.write(utterance) { (buffer: AVAudioBuffer) in guard let pcmBuffer = buffer as? AVAudioPCMBuffer else { fatalError("unknown buffer type: \(buffer)") } if pcmBuffer.frameLength == 0 { // done } else { // append buffer to file if output == nil { output = AVAudioFile( forWriting: URL(fileURLWithPath: "test.caf"), settings: pcmBuffer.format.settings, commonFormat: .pcmFormatInt16, interleaved: false) } output?.write(from: pcmBuffer) } } 
+1
source

You can use OSX to prepare AIFF files (or maybe some OSX-based services) using the NSSpeechSynthesizer startSpeakingString: toURL method:

0
source

Source: https://habr.com/ru/post/1203102/


All Articles