-
Notifications
You must be signed in to change notification settings - Fork 614
/
Copy pathP14_ScriptToPlaySongs.py
54 lines (49 loc) · 1.79 KB
/
P14_ScriptToPlaySongs.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
# This program upon execution will take your command to play music randomly.
import pyttsx3 #pip install pyttsx3
import speech_recognition as sr #pip install speech recognition
import os
import datetime
import random
engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
engine.setProperty('voice',voices[0].id) #voices[1].id for female assistant
#speak function to speak the string passed to it.
def speak(audio):
engine.say(audio)
engine.runAndWait()
#function to listen your command and process them
def takedata():
r= sr.Recognizer()
with sr.Microphone() as source:
print("Listening....")
audio = r.listen(source)
try:
print("Recognizing...")
query = r.recognize_google(audio,language='en-in') #language set is Indian English
print("The user said ",query)
except Exception :
print("Sorry i was unable to catch that. Please try speaking that again.")
return 'None'
return query
def wishme():
hours = datetime.datetime.now().hour
if hours>=0 and hours <12:
speak("good morning")
elif hours>=12 and hours <18:
speak("good afternoon")
else:
speak("good evening")
speak("sir i am your personal assistant. tell me how can i help you ")
wishme()
query = takedata()
if 'play music' or 'play songs' in query:
music_dir = "F:\\Songs" #put the location of the folder where you store your songs
songs = os.listdir(music_dir)
l = len(songs)
num = random.randrange(0,l,1)
os.startfile(os.path.join(music_dir,songs[num]))
speak("Thank you for using my sevices. All improvements on my github repository are welcome.")
print("www.github.com/tarun-sharma03")
exit()
else:
speak("Query type not supported")