UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Recording Speech Synthesizer

Forums > Swift

I'm having trouble getting this to work. There isn't too much information about this that I can find. I'm hoping maybe some smarter than me people on here could shed some light. The save function was taken from the only answer I could find from StackOverflow.

import AVFoundation
import Foundation

class Coordinator {

    let synthesizer: AVSpeechSynthesizer

    init() {
        let synthesizer = AVSpeechSynthesizer()
        self.synthesizer = synthesizer
    }

    func speakPhrase(phrase: String) {
        let utterance = AVSpeechUtterance(string: phrase)
        utterance.voice = AVSpeechSynthesisVoice(language: "en")
        synthesizer.speak(utterance)
    }

    func saveAVSpeechUtteranceToFile() {

        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 = try? AVAudioFile(
                        forWriting: URL(fileURLWithPath: "test.caf"),
                        settings: pcmBuffer.format.settings,
                        commonFormat: .pcmFormatInt16,
                        interleaved: false)
                }
                try! output?.write(from: pcmBuffer)
            }
        }
    }
}
import SwiftUI

struct ContentView: View {

    let coordinator = Coordinator()

    var body: some View {
        VStack {
            HStack {
                Image(systemName: "globe")
                    .imageScale(.large)
                    .foregroundColor(.accentColor)
                Text("Speak")
            }
            .padding()
            .onTapGesture() {
                coordinator.speakPhrase(phrase: "This works fine.")
            }
            HStack {
                Image(systemName: "globe")
                    .imageScale(.large)
                    .foregroundColor(.accentColor)
                Text("Save to File")
            }
            .padding()
            .onTapGesture() {
                coordinator.saveAVSpeechUtteranceToFile()
            }
        }
        .font(.largeTitle)
        .padding()
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

2      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.