swift tts
2.1.2 Error if built with macOS
แพ็คเกจนี้มี wrappers ตรงไปตรงมารอบ ๆ TTS ส่วนหนึ่งของ Avfoundation/Avspeechsynthesizer เพื่อให้คุณใช้ข้อความเพื่อพูดได้อย่างง่ายดาย
SwiftTTS ใช้ การพร้อมกันอย่างรวดเร็ว กับ async await สอง AsyncStreamSwiftTTSDependency wrapper รอบ ๆ ห้องสมุดด้านบนอำนวยความสะดวกในการรวมเข้ากับไลบรารีการพึ่งพาแบบไม่มีจุดหรือโครงการที่ทำด้วยสถาปัตยกรรมคอมโพสิต (TCA)SwiftTTSCombine ห้องสมุด OG ยังคงมีอยู่ในแพ็คเกจนี้ speak(String) -> Void - เรียกวิธีนี้เมื่อคุณต้องการใช้ TTS กับสตริงง่ายๆisSpeaking() -> AsyncStream<Bool> - ต้องรู้ว่าเมื่อใดที่คำพูดเริ่มได้ยินและเมื่อมันหยุดลงspeakingProgress() -> AsyncStream<Double> - เพื่อทราบความคืบหน้าจาก 0 ถึง 1rateRatio() -> Float - ตั้งค่าอัตราการชะลอตัวหรือเร่งเครื่องยนต์ TTSsetRateRatio(Float) -> Void - ตั้งค่าอัตราการชะลอตัวหรือเร่งเครื่องยนต์ TTSvoice() -> AVSpeechSynthesisVoice? - เสียงของเอ็นจิ้น TTS โดยค่าเริ่มต้นมันเป็นเสียงสำหรับ en-GBsetVoice(AVSpeechSynthesisVoice) -> Void - ตั้งค่าเสียงของเครื่องยนต์ 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 " ) เพิ่ม @Dependency(.tts) var tts ใน Reducer ของคุณคุณจะสามารถเข้าถึงฟังก์ชั่นทั้งหมดที่กล่าวถึงข้างต้น
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 : ตั้งค่าอัตราการชะลอตัวหรือเร่งเครื่องยนต์ TTSvar voice: AVSpeechSynthesisVoice? : ตั้งค่าเสียงของเอ็นจิ้น TTS โดยค่าเริ่มต้นมันเป็นเสียงสำหรับ 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 " ) คุณสามารถเพิ่ม 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
] ) ,
...
]
)