swift tts
2.1.2 Error if built with macOS
該軟件包包含一些非常簡單的包裝器,圍繞Avoundation/avspeechsynthesizer的TTS部分包裝,使您可以輕鬆地使用文本語音。
SwiftTTS使用Swift並發與async await ,等待幾個AsyncStreamSwiftTTSDependency在上面的庫周圍包裝器,促進了與無點依賴庫的集成,或者由可合轉架構(TCA)製成的項目。SwiftTTSCombine此軟件包中仍然可用的OG庫speak(String) -> Void當您只想使用簡單字符串的TT時調用此方法isSpeaking() -> AsyncStream<Bool> - 知道何時開始聽到話語,何時停止speakingProgress() -> AsyncStream<Double> - 知道進度從0到1rateRatio() -> Float設置速度放慢或加速TTS發動機的速率setRateRatio(Float) -> Void設置速度放慢或加速TTS發動機的速率voice() -> AVSpeechSynthesisVoice? - 默認情況下,TTS引擎的聲音是en-GB的聲音setVoice(AVSpeechSynthesisVoice) -> Void設置TTS引擎的聲音import 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 " ) 在Reducer中添加@Dependency(.tts) var tts ,您將可以訪問上面提到的所有功能。
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
}
}
}
} 您可以實例化/注入TTSEngine對象,它具有此行為
func speak(string: String) :當您只想使用簡單字符串的TTS時調用此方法isSpeakingPublisher ,以了解何時開始聽到話語,何時停止speakingProgressPublisher以了解進度,從0到1var rateRatio: Float :設置速度減速或加速TTS發動機var voice: AVSpeechSynthesisVoice? :設置TTS引擎的聲音,默認情況下,這是en-GB的聲音import 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 " ) 您可以將Swifttts Libs添加到Xcode項目中,通過將其添加為軟件包依賴項。
編輯您的Package.swift以在可用的三個中添加您想要的庫之一。
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
] ) ,
...
]
)