Skip to content

Commit f7969ea

Browse files
committed
docs(README): add SwiftTTS and SwiftTTSDependency examples
1 parent 820ef9e commit f7969ea

File tree

1 file changed

+120
-10
lines changed

1 file changed

+120
-10
lines changed

README.md

Lines changed: 120 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,118 @@ This package contains some very straightforward wrappers around TTS part of AVFo
88

99
## Modern concurrency usage
1010

11-
🔨 TODO
11+
* `func speak(string: String)`: call this method when you simply want to use the TTS with a simple String
12+
* subscribe to `isSpeakingPublisher` to know when the utterance starts to be heard, and when it's stopped
13+
* subscribe to `speakingProgressPublisher` to know the progress, from 0 to 1
14+
* `var rateRatio: Float`: set the rate to slow down or accelerate the TTS engine
15+
* `var voice: AVSpeechSynthesisVoice?`: set the voice of the TTS engine, by default, it's the voice for `en-GB`
16+
17+
### Example
18+
19+
```swift
20+
import SwiftTTS
21+
22+
let tts = SwiftTTS.live
23+
24+
tts.speak("Hello World!")
25+
26+
Task {
27+
for await isSpeaking in tts.isSpeaking() {
28+
print("TTS is currently \(isSpeaking ? "speaking" : "not speaking")")
29+
}
30+
}
31+
32+
Task {
33+
for await progress in tts.speakingProgress() {
34+
print("Progress: \(Int(progress * 100))%")
35+
}
36+
}
37+
38+
tts.setRateRatio(3/4)
39+
40+
tts.speak("Hello World! But slower")
41+
```
1242

1343
## [Point-Free Dependencies](https://github.com/pointfreeco/swift-dependencies) usage
1444

15-
🔨 TODO
45+
Add `@Dependency(\.tts) var tts` in your `Reducer`, you will have access to all functions mentioned above.
46+
47+
### Example
48+
49+
```swift
50+
import ComposableArchitecture
51+
import Foundation
52+
import SwiftTTSDependency
53+
54+
public struct TTS: ReducerProtocol {
55+
public struct State: Equatable {
56+
public var text = ""
57+
public var isSpeaking = false
58+
public var speakingProgress = 1.0
59+
public var rateRatio: Float = 1.0
60+
61+
public init(
62+
text: String = "",
63+
isSpeaking: Bool = false,
64+
speakingProgress: Double = 1.0,
65+
rateRatio: Float = 1.0
66+
) {
67+
self.text = text
68+
self.isSpeaking = isSpeaking
69+
self.speakingProgress = speakingProgress
70+
self.rateRatio = rateRatio
71+
}
72+
}
73+
74+
public enum Action: Equatable {
75+
case changeRateRatio(Float)
76+
case speak
77+
case startSpeaking
78+
case stopSpeaking
79+
case changeSpeakingProgress(Double)
80+
}
81+
82+
@Dependency(\.tts) var tts
83+
84+
public init() {}
85+
86+
public var body: some ReducerProtocol<State, Action> {
87+
Reduce { state, action in
88+
switch action {
89+
case let .changeRateRatio(rateRatio):
90+
state.rateRatio = rateRatio
91+
tts.setRateRatio(rateRatio)
92+
return .none
93+
case .speak:
94+
tts.speak(state.text)
95+
return .run { send in
96+
for await isSpeaking in tts.isSpeaking() {
97+
if isSpeaking {
98+
await send(.startSpeaking)
99+
} else {
100+
await send(.stopSpeaking)
101+
}
102+
}
103+
}
104+
case .startSpeaking:
105+
state.isSpeaking = true
106+
return .run { send in
107+
for await progress in tts.speakingProgress() {
108+
await send(.changeSpeakingProgress(progress))
109+
}
110+
}
111+
case .stopSpeaking:
112+
state.isSpeaking = false
113+
return .none
114+
case let .changeSpeakingProgress(speakingProgress):
115+
state.speakingProgress = speakingProgress
116+
return .none
117+
}
118+
}
119+
}
120+
}
121+
122+
```
16123

17124
## Combine Usage
18125

@@ -24,7 +131,7 @@ You can instantiate/inject `TTSEngine` object, it has this behavior
24131
* `var rateRatio: Float`: set the rate to slow down or accelerate the TTS engine
25132
* `var voice: AVSpeechSynthesisVoice?`: set the voice of the TTS engine, by default, it's the voice for `en-GB`
26133

27-
Example
134+
### Example
28135

29136
```swift
30137
import Combine
@@ -56,28 +163,31 @@ engine.speak(string: "Hello World! But slower")
56163

57164
### Xcode
58165

59-
You can add SwiftTTSCombine to an Xcode project by adding it as a package dependency.
166+
You can add SwiftTTS libs to an Xcode project by adding it as a package dependency.
60167

61168
1. From the **File** menu, select **Swift Packages › Add Package Dependency...**
62-
2. Enter "https://github.com/renaudjenny/SwiftTTS" into the package repository URL test field
169+
2. Enter "https://github.com/renaudjenny/swift-tts" into the package repository URL test field
170+
3. Select one of the three package that your are interested in. See [above](#swifttts)
63171

64172
### As package dependency
65173

66-
Edit your `Package.swift` to add this library.
174+
Edit your `Package.swift` to add one of the library you want among the three available.
67175

68176
```swift
69177
let package = Package(
70178
...
71179
dependencies: [
72-
.package(url: "https://github.com/renaudjenny/SwiftTTS", from: "2.0.0"),
180+
.package(url: "https://github.com/renaudjenny/swift-tts", from: "2.0.0"),
73181
...
74182
],
75183
targets: [
76184
.target(
77185
name: "<Your project name>",
78-
dependencies: ["SwiftTTS"]), // <-- Modern concurrency
79-
dependencies: ["SwiftTTSDependency"]), // <-- Point-Free Dependencies library wrapper
80-
dependencies: ["SwiftTTSCombine"]), // <-- Combine wrapper
186+
dependencies: [
187+
.product(name: "SwiftTTS", package: "swift-tts"), // <-- Modern concurrency
188+
.product(name: "SwiftTTSDependency", package: "swift-tts"), // <-- Point-Free Dependencies library wrapper
189+
.product(name: "SwiftTTSCombine", package: "swift-tts"), // <-- Combine wrapper
190+
]),
81191
...
82192
]
83193
)

0 commit comments

Comments
 (0)