swift tts
2.1.2 Error if built with macOS
Ce package contient des emballages très simples autour de la partie TTS de AvFoundation / AVSpeechSynthesizer pour vous permettre d'utiliser du texte à la parole avec facilité.
SwiftTTS utilisant la concurrence Swift avec async await , quelques AsyncStreamSwiftTTSDependency Un wrapper autour de la bibliothèque ci-dessus facilitant la bibliothèque d'intégration avec des dépendances sans point ou un projet réalisé avec l'architecture composable (TCA).SwiftTTSCombine la bibliothèque OG toujours disponible dans ce package speak(String) -> Void - Appelez cette méthode lorsque vous souhaitez simplement utiliser le TTS avec une chaîne simpleisSpeaking() -> AsyncStream<Bool> - savoir quand l'énoncé commence à être entendu, et quand il est arrêtéspeakingProgress() -> AsyncStream<Double> - pour connaître les progrès, de 0 à 1rateRatio() -> Float - réglez le taux pour ralentir ou accélérer le moteur TTSsetRateRatio(Float) -> Void - définissez le taux pour ralentir ou accélérer le moteur TTSvoice() -> AVSpeechSynthesisVoice? - La voix du moteur TTS, par défaut, c'est la voix pour en-GBsetVoice(AVSpeechSynthesisVoice) -> Void - Réglez la voix du moteur 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 " ) Ajoutez @Dependency(.tts) var tts Dans votre Reducer , vous aurez accès à toutes les fonctions mentionnées ci-dessus.
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
}
}
}
} Vous pouvez instancier / injecter un objet TTSEngine , il a ce comportement
func speak(string: String) : Appelez cette méthode lorsque vous souhaitez simplement utiliser le TTS avec une chaîne simpleisSpeakingPublisher pour savoir quand l'énoncé commence à être entendu, et quand il est arrêtéspeakingProgressPublisher pour connaître les progrès, de 0 à 1var rateRatio: Float : Réglez le taux pour ralentir ou accélérer le moteur TTSvar voice: AVSpeechSynthesisVoice? : Définissez la voix du moteur TTS, par défaut, c'est la voix pour 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 " ) Vous pouvez ajouter des LIB Swifttts à un projet Xcode en l'ajoutant comme dépendance de package.
Modifiez votre Package.swift pour ajouter l'une des bibliothèques que vous souhaitez parmi les trois 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
] ) ,
...
]
)