When playing audio from Text to Speech using AVSpeechSynthesizer write method with AVAudioFormat and AVAudioPCMBuffer, the audio is choppy and sounds distorted. This was not an issue on iOS 16 and below.

self.synthesizer.write(utterance) { buffer in
            DispatchQueue.main.async {
                guard let pcmBuffer = buffer as? AVAudioPCMBuffer, pcmBuffer.frameLength > 0 else {
                    return
                }
                // Need to convert the buffer to different format because AVAudioEngine does not support the format returned from AVSpeechSynthesizer
                
                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.bufferCounter += 1
                    self.scheduleNextBuffer(convertedBuffer)
                    self.converter?.reset()
                }
                catch {
                    self.handleTextToSpeechError()
                }
            }
        }
Add a Comment

Replies

This issue still exist on iOS 17 Beta 2.