forked from baby-pilot/talking-plant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbt_speak.py
77 lines (60 loc) · 2.25 KB
/
bt_speak.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
"""
Implements speaking capability for the plant via bluetooth
"""
from gtts import gTTS
from enum import Enum
import time
import vlc
from datetime import datetime
# ## -------------------------------------- Generate scripts ---------------------------------------------
# tts = gTTS("hello, I am raspberry pi 4. This is a test with Google Text to speech (gTTS)")
# tts.save("hello.mp3")
water_alert = gTTS("Hey this is your plant speaking, please water me.")
water_alert.save("water_alert.mp3")
uv_alert = gTTS("Hey, it's me again, can you please move me, I need some sunshine.")
uv_alert.save("uv_alert.mp3")
defense_alert = gTTS("It's me, plant, you gotta defend me, I'll be eaten!")
defense_alert.save("defense_alert.mp3")
## ------------------------------------------------------------------------------------------------------
# can use mpg123 on rasp pi to play the file above manually from command line
class AlertMode(Enum):
NEED_WATER = "water_alert.mp3"
NEED_UV = "uv_alert.mp3"
NEED_DEFENSE = "defense_alert.mp3"
def generateAndPlay(message) -> None:
filename = f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}.mp3"
tts = gTTS(message)
tts.save(filename)
playFromFile(filename)
def playFromFile(filename) -> None:
player = vlc.MediaPlayer(filename)
player.play()
while player.get_state() in [
vlc.State.Playing,
vlc.State.Opening,
vlc.State.Buffering,
]:
time.sleep(1) # check every second whether audio is still playing
player.stop() # Stop player when playback complete
# can use mpg123 on rasp pi to play the file above manually from command line
def speak(mode: AlertMode):
audio_file = mode.value
# setup VLC player
player = vlc.MediaPlayer(audio_file)
time.sleep(3)
player.play()
print("played")
while player.get_state() in [
vlc.State.Playing,
vlc.State.Opening,
vlc.State.Buffering,
]:
print("playing")
time.sleep(1) # check every second whether audio is still playing
player.stop() # Stop player when playback complete
time.sleep(1)
print("stopped")
player.release()
time.sleep(1)
print("released")
# can use mpg123 on rasp pi to play the file above manually from command line