swift tts
2.1.2 Error if built with macOS
Este paquete contiene algunos envoltorios muy sencillos alrededor de TTS parte de Avfoundation/AvSpEechsynthesizer para permitirle usar texto a voz con facilidad.
SwiftTTS usando la concurrencia rápida con async await , un par de AsyncStreamSwiftTTSDependency Un envoltorio alrededor de la biblioteca anterior facilita la integración con la biblioteca de dependencias sin puntos o un proyecto realizado con la arquitectura compuesta (TCA).SwiftTTSCombine La biblioteca OG todavía está disponible en este paquete speak(String) -> Void - Llame a este método cuando simplemente desee usar el TTS con una cadena simpleisSpeaking() -> AsyncStream<Bool> - para saber cuándo comienza a escuchar el enunciado y cuándo se detienespeakingProgress() -> AsyncStream<Double> - para conocer el progreso, de 0 a 1rateRatio() -> Float - Establezca la velocidad para reducir la velocidad o acelerar el motor TTSsetRateRatio(Float) -> Void - Establezca la velocidad para reducir la velocidad o acelerar el motor TTSvoice() -> AVSpeechSynthesisVoice? - La voz del motor TTS, por defecto, es la voz para en-GBsetVoice(AVSpeechSynthesisVoice) -> Void - Establecer la voz del motor TTSimport SwiftTTS
let tts = SwiftTTS . live
tts . speak ( " Hello World! " )
Task {
for await isSpeaking in tts . isSpeaking ( ) {
print ( " TTS is currently ( isSpeaking ? " speaking " : " not speaking " ) " )
}
}
Task {
for await progress in tts . speakingProgress ( ) {
print ( " Progress: ( Int ( progress * 100 ) ) % " )
}
}
tts . setRateRatio ( 3 / 4 )
tts . speak ( " Hello World! But slower " ) Agregue @Dependency(.tts) var tts En su Reducer , tendrá acceso a todas las funciones mencionadas anteriormente.
import ComposableArchitecture
import Foundation
import SwiftTTSDependency
public struct TTS : ReducerProtocol {
public struct State : Equatable {
public var text = " "
public var isSpeaking = false
public var speakingProgress = 1.0
public var rateRatio : Float = 1.0
public init (
text : String = " " ,
isSpeaking : Bool = false ,
speakingProgress : Double = 1.0 ,
rateRatio : Float = 1.0
) {
self . text = text
self . isSpeaking = isSpeaking
self . speakingProgress = speakingProgress
self . rateRatio = rateRatio
}
}
public enum Action : Equatable {
case changeRateRatio ( Float )
case speak
case startSpeaking
case stopSpeaking
case changeSpeakingProgress ( Double )
}
@ Dependency ( . tts ) var tts
public init ( ) { }
public var body : some ReducerProtocol < State , Action > {
Reduce { state , action in
switch action {
case let . changeRateRatio ( rateRatio ) :
state . rateRatio = rateRatio
tts . setRateRatio ( rateRatio )
return . none
case . speak :
tts . speak ( state . text )
return . run { send in
for await isSpeaking in tts . isSpeaking ( ) {
if isSpeaking {
await send ( . startSpeaking )
} else {
await send ( . stopSpeaking )
}
}
}
case . startSpeaking :
state . isSpeaking = true
return . run { send in
for await progress in tts . speakingProgress ( ) {
await send ( . changeSpeakingProgress ( progress ) )
}
}
case . stopSpeaking :
state . isSpeaking = false
return . none
case let . changeSpeakingProgress ( speakingProgress ) :
state . speakingProgress = speakingProgress
return . none
}
}
}
} Puede instanciar/inyección del objeto TTSEngine , tiene este comportamiento
func speak(string: String) : llame a este método cuando simplemente desea usar el TTS con una cadena simpleisSpeakingPublisher para saber cuándo comienza a escuchar el enunciado y cuándo se detienespeakingProgressPublisher para conocer el progreso, de 0 a 1var rateRatio: Float : Establezca la velocidad para reducir la velocidad o acelerar el motor TTSvar voice: AVSpeechSynthesisVoice? : Establezca la voz del motor TTS, de forma predeterminada, es la voz para en-GBimport Combine
import SwiftTTSCombine
let engine : TTSEngine = SwiftTTSCombine . Engine ( )
var cancellables = Set < AnyCancellable > ( )
engine . speak ( string : " Hello World! " )
engine . isSpeakingPublisher
. sink { isSpeaking in
print ( " TTS is currently ( isSpeaking ? " speaking " : " not speaking " ) " )
}
. store ( in : & cancellables )
engine . speakingProgressPublisher
. sink { progress in
print ( " Progress: ( Int ( progress * 100 ) ) % " )
}
. store ( in : & cancellables )
engine . rateRatio = 3 / 4
engine . speak ( string : " Hello World! But slower " ) Puede agregar SwiftTts Libs a un proyecto Xcode agregándolo como una dependencia del paquete.
Edite su Package.swift para agregar una de la biblioteca que desee entre los tres disponibles.
let package = Package (
...
dependencies : [
. package ( url : " https://github.com/renaudjenny/swift-tts " , from : " 2.0.0 " ) ,
...
] ,
targets : [
. target (
name : " <Your project name> " ,
dependencies : [
. product ( name : " SwiftTTS " , package : " swift-tts " ) , // <-- Modern concurrency
. product ( name : " SwiftTTSDependency " , package : " swift-tts " ) , // <-- Point-Free Dependencies library wrapper
. product ( name : " SwiftTTSCombine " , package : " swift-tts " ) , // <-- Combine wrapper
] ) ,
...
]
)