Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d8fba8b

Browse files
authored
Create voice_activated_alarm.py
1 parent 7a91d85 commit d8fba8b

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

scripts/voice_activated_alarm.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import datetime
2+
from pyttsx3 import engine
3+
import speech_recognition as sr
4+
import pyttsx3 as py
5+
import winsound
6+
7+
engine = py.init()
8+
9+
10+
def alarm(timing):
11+
alTime = str(datetime.datetime.now().strptime(timing, "%I:%M %p"))
12+
13+
alTime = alTime[11:-3]
14+
print(alTime)
15+
Horeal = alTime[:2]
16+
Horeal = int(Horeal)
17+
Mireal = alTime[3:5]
18+
Mireal = int(Mireal)
19+
20+
print(f"Done, alarm is set for {timing}")
21+
22+
while True:
23+
if Horeal == datetime.datetime.now().hour and Mireal == \
24+
datetime.datetime.now().minute:
25+
print("alarm is running")
26+
winsound.PlaySound('abc', winsound.SND_LOOP)
27+
28+
elif Mireal < datetime.datetime.now().minute:
29+
break
30+
31+
32+
def speech(audio):
33+
engine.setProperty('rate', 200)
34+
voices = engine.getProperty('voices')
35+
# engine.setProperty('voice', voices[1].id)
36+
engine.setProperty('voice', voices[0].id)
37+
engine.say(audio)
38+
engine.runAndWait()
39+
40+
41+
def takeCommand():
42+
# It takes microphone input from the user and returns string output
43+
44+
r = sr.Recognizer()
45+
with sr.Microphone() as source:
46+
r.adjust_for_ambient_noise(source, duration=1)
47+
print()
48+
print("Listening...")
49+
print()
50+
# r.pause_threshold = 1
51+
audio = r.listen(source)
52+
53+
try:
54+
print("Recognizing...")
55+
print()
56+
query = r.recognize_google(audio, language='en-in')
57+
print(f"you said: {query}\n")
58+
59+
except Exception as e:
60+
print("Say that again please...")
61+
print()
62+
print(e)
63+
return "None"
64+
return query
65+
66+
67+
speechinput = takeCommand().lower()
68+
69+
if 'alarm' in speechinput:
70+
speech("say set alarm for 5:30 am ")
71+
print("say set alarm for 5:30 am")
72+
tt = takeCommand()
73+
tt = tt.replace("set alarm to ", "")
74+
tt = tt.replace(".", "")
75+
tt = tt.upper()
76+
alarm(tt)

0 commit comments

Comments
 (0)