Skip to content

Commit 40256a5

Browse files
Create text-to-speech.md
1 parent a6c8d9d commit 40256a5

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

recipes/python/text-to-speech.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Speech to test
2+
3+
## Subprocess and say
4+
5+
Using `say` which is builtin on macOS. But robotic and limited.
6+
7+
```python
8+
import subprocess
9+
10+
11+
text_to_speak = "Hello, world!"
12+
subprocess.run(["say", text_to_speak])
13+
```
14+
15+
16+
## Using pyttsx3
17+
18+
A free library for generating text to speech locally.
19+
20+
Allows custom rate as below but still robotic.
21+
22+
```sh
23+
pip install pyttsx3
24+
```
25+
26+
```python
27+
import pyttsx3
28+
29+
30+
print("init")
31+
engine = pyttsx3.init()
32+
print("say")
33+
engine.say("I will speak this text")
34+
print("run")
35+
engine.runAndWait()
36+
37+
38+
rate = engine.getProperty("rate")
39+
print(rate)
40+
# 200.0
41+
engine.setProperty("rate", 150)
42+
43+
engine.say("I will speak this text slower")
44+
engine.runAndWait()
45+
```

0 commit comments

Comments
 (0)