self.synthesizer.write(utterance) { buffer in
DispatchQueue.main.async {
guard let pcmBuffer = buffer as? AVAudioPCMBuffer, pcmBuffer.frameLength > 0 else { return }
guard self.converter != nil,
let pcmFormat = AVAudioFormat(commonFormat: AVAudioCommonFormat.pcmFormatFloat32, sampleRate: pcmBuffer.format.sampleRate, channels: pcmBuffer.format.channelCount, interleaved: false), let convertedBuffer = AVAudioPCMBuffer(pcmFormat: pcmFormat, frameCapacity: pcmBuffer.frameCapacity) else {
return
}
do {
try self.converter?.convert(to: convertedBuffer, from: pcmBuffer)
self.playerNode?.scheduleBuffer(convertedBuffer, completionCallbackType: .dataPlayedBack, completionHandler: { _ in })
self.converter?.reset()
}
catch {
}
}
}
When playing audio from Text to Speech using AVSpeechSynthesizer write method with AVAudioFormat and AVAudioPCMBuffer, the audio is choppy and sounds distorted on iOS 17 Beta 1 & 2.
Beta 3 have no change.