-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnoisy.py
More file actions
executable file
·43 lines (36 loc) · 1.42 KB
/
noisy.py
File metadata and controls
executable file
·43 lines (36 loc) · 1.42 KB
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
#!/usr/bin/env python3
import time
from gtts import gTTS
import os
import tempfile
class VoiceAlerts():
"""Plays sounds of a TTS voice reading messages.
"""
def __init__(self, cooldown=30, message:str="voice alert"):
self.message = message
self.cooldown = cooldown
self.last_sound_played_at = 0 # start at 0 so that sound always plays first time
self.temp_dir = tempfile.TemporaryDirectory()
self.sound_path = os.path.join(self.temp_dir.name, "alert.mp3")
self._generate_sound()
def _generate_sound(self):
"""Generates the TTS sound file from the message."""
tts = gTTS(text=self.message, lang='en')
tts.save(self.sound_path)
def alert(self):
"""Plays a sound if the last time it was played was more than `cooldown` seconds ago.
"""
elapsed = time.time() - self.last_sound_played_at
if elapsed > self.cooldown:
print(f"Playing voice alert: {self.message}")
os.system(f"mpg321 {self.sound_path}")
self.last_sound_played_at = time.time()
else:
print(f"Last sound was played {elapsed:.1f} seconds ago - too recent to play again")
def __del__(self):
"""Clean up the temporary directory."""
self.temp_dir.cleanup()
if __name__ == "__main__":
print("Testing voice alert...")
alert = VoiceAlerts(message="Voice alert test")
alert.alert()