这个包包含一些非常简单的 AVFoundation/AVSpeechSynthesizer 中 TTS 部分的包装器,方便您轻松地使用文本到语音转换功能。
SwiftTTS 使用 Swift 并发,通过 async await 和一些 AsyncStream 实现。SwiftTTSDependency 是上述库的包装器,方便与 Point-Free Dependencies 库或使用 Composable Architecture (TCA) 构建的项目集成。SwiftTTSCombine 是这个包中仍然可用的 OG 库。speak(String) -> Void - 当您只想使用 TTS 和一个简单的 String 时,调用此方法。isSpeaking() -> AsyncStream<Bool> - 了解语音何时开始播放以及何时停止。speakingProgress() -> AsyncStream<Double> - 了解进度,从 0 到 1。rateRatio() -> 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 和一个简单的 String 时,调用此方法。isSpeakingPublisher 以了解语音何时开始播放以及何时停止。speakingProgressPublisher 以了解进度,从 0 到 1。var 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 库添加到 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
]),
...
]
)