AVSpeechSynthesizer output as file?

AVSpeechSynthesizer has a fairly simple API, which doesn't have support for saving to an audio file built-in.

I'm wondering if there's a way around this - perhaps recording the output as it's played silently, for playback later? Or something more efficient.

As of now AVSpeechSynthesizer does not support this . There in no way get the audio file using AVSpeechSynthesizer . I tried this few weeks ago for one of my apps and found out that it is not possible , Also nothing has changed for AVSpeechSynthesizer in iOS 8.

I too thought of recording the sound as it is being played , but there are so many flaws with that approach like user might be using headphones, the system sound might be low or mute , it might catch other external sound, so its not advisable to go with that approach.

AVSpeechSynthesizer iOS 13 write …, I have been trying to use the new method that writes a speech utterance to file: let file = try? AVAudioFile(forWriting: filePath!, settings: outputFormatSettings,  AVSpeechSynthesizer() Default constructor, initializes a new instance of this class. AVSpeechSynthesizer(IntPtr) A constructor used when creating managed representations of unmanaged objects; Called by the runtime. AVSpeechSynthesizer(NSObjectFlag) Constructor to call on derived classes to skip initialization and merely allocate the object.

This 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)
   } 
}

AVSpeech Record/Save as Audio File |Apple Developer Forums, var audioRecorder: AVAudioRecorder? let synthesizer = AVSpeechSynthesizer()​; lazy var speechUtterance : AVSpeechUtterance = {; let string =  Re: AVSpeechSynthesizer iOS 13 write to file Level 1 (0 points) IMPOSSIBLEr Jan 6, 2020 12:56 AM ( in response to futuresounds82 )

You can use OSX to prepare AIFF files (or, maybe, some OSX-based service) via NSSpeechSynthesizer method startSpeakingString:toURL:

AVSpeechSynthesizer output as file?, AVSpeechSynthesizer has a fairly simple API, which doesn't have support for saving to an audio file built-in. I'm wondering if there's a way around this - perhaps  Generate audio file form AVSpeechSynthesizer. Apple does not support saving AVSpeechSynthesizer output. This SO post discuss in detail about the alternates,

AVSpeechSynthesizer output as file?, AVSpeechSynthesizer has a fairly simple API, which doesn't have support for saving to an audio file built-in. I'm wondering if there's a way around this - perhaps  Yup, its a bug. As of iOS8 GM, it seems the first attempt to get AVSpeechSynthesizer to speak an AVSpeechUtterance will result in silence. My workaround is to make AVSpeechSynthesizer speak a single-character utterance immediately after it is initialized. This will be silent, and afterwards your AVSpeechSynthesizer will work as normal.

Generate audio file form AVSpeechSynthesizer, Unfortunately, Apple does not support saving AVSpeechSynthesizer output. This SO post discuss in detail about the alternates, hope it helps  A simple strategy would be saving your output from AVSpeechSynthesizerto a directory first, then changing its format. But, as far as I know AVSpeechSynthesizerdoes not let you save your outputs. I would suggest you to check the following post: Save audio stream to mp3 file (iOS)

AVSpeechSynthesizer Class (AVFoundation), All, null)] public class AVSpeechSynthesizer : Foundation.NSObject OutputChannels · Paused Called after the object has been loaded from the nib file. Even from the DOS days, we could redirect output to a file. This cmdlet is the PowerShell way to do it. Basic Out-File Usage. This cmdlet is simple to use as was the standard output redirection operator before it. Its sole purpose is to send the raw output directly to a text file with no regard. There are minimal ways to tweak how that output

Comments
  • When executing under Mac OS 10.15 the callback is never being called. The synthesizer just pronounces the text instead.
  • I just bumped into that on 10.15 too, any change/workaround @vasily ? oh well back to NSSpeechSynthesizer 🤷‍♂️
  • @glotcha No resolution so far