swift tts
2.1.2 Error if built with macOS
Este pacote contém algumas embalagens muito simples em torno do TTS parte do avfoundation/AvSpeechSynthesizer para permitir que você use texto para falar com facilidade.
SwiftTTS usando a simultaneidade Swift com async await , um casal de AsyncStreamSwiftTTSDependency Um invólucro ao redor da biblioteca acima, facilitando a integração com a biblioteca de dependências sem pontos ou um projeto feito com a arquitetura composta (TCA).SwiftTTSCombine A biblioteca OG ainda disponível neste pacote speak(String) -> Void - chame esse método quando você simplesmente deseja usar o TTS com uma corda simplesisSpeaking() -> AsyncStream<Bool> - Para saber quando a expressão começa a ser ouvida, e quando é interrompidaspeakingProgress() -> AsyncStream<Double> - Para conhecer o progresso, de 0 a 1rateRatio() -> Float - Defina a taxa para desacelerar ou acelerar o motor TTSsetRateRatio(Float) -> Void - defina a taxa para desacelerar ou acelerar o motor TTSvoice() -> AVSpeechSynthesisVoice? - A voz do mecanismo TTS, por padrão, é a voz para en-GBsetVoice(AVSpeechSynthesisVoice) -> Void - Defina a voz do mecanismo 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 " ) Adicione @Dependency(.tts) var tts no seu Reducer , você terá acesso a todas as funções mencionadas acima.
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
}
}
}
} Você pode instanciar/injetar o objeto TTSEngine , ele tem esse comportamento
func speak(string: String) : chame esse método quando você simplesmente deseja usar o TTS com uma string simplesisSpeakingPublisher para saber quando o enunciado começa a ser ouvido, e quando é interrompidospeakingProgressPublisher para conhecer o progresso, de 0 a 1var rateRatio: Float : Defina a taxa para desacelerar ou acelerar o motor TTSvar voice: AVSpeechSynthesisVoice? : Defina a voz do mecanismo TTS, por padrão, é a 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 " ) Você pode adicionar as bibliotecas SWIFTTTS a um projeto Xcode, adicionando -o como uma dependência de pacote.
Edite seu Package.swift para adicionar uma das bibliotecas que você deseja entre os três disponíveis.
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
] ) ,
...
]
)