1+ # This program upon execution will take your command to play music randomly.
2+ import pyttsx3 #pip install pyttsx3
3+ import speech_recognition as sr #pip install speech recognition
4+ import os
5+ import datetime
6+ import random
7+
8+ engine = pyttsx3.init('sapi5')
9+ voices = engine.getProperty('voices')
10+ engine.setProperty('voice',voices[0].id) #voices[1].id for female assistant
11+
12+ #speak function to speak the string passed to it.
13+ def speak(audio):
14+ engine.say(audio)
15+ engine.runAndWait()
16+ #function to listen your command and process them
17+ def takedata():
18+ r= sr.Recognizer()
19+ with sr.Microphone() as source:
20+ print("Listening....")
21+ audio = r.listen(source)
22+ try:
23+ print("Recognizing...")
24+ query = r.recognize_google(audio,language='en-in') #language set is Indian English
25+ print("The user said ",query)
26+ except Exception :
27+ print("Sorry i was unable to catch that. Please try speaking that again.")
28+ return 'None'
29+ return query
30+
31+ def wishme():
32+ hours = datetime.datetime.now().hour
33+
34+ if hours>=0 and hours <12:
35+ speak("good morning")
36+ elif hours>=12 and hours <18:
37+ speak("good afternoon")
38+ else:
39+ speak("good evening")
40+ speak("sir i am your personal assistant. tell me how can i help you ")
41+
42+ wishme()
43+ query = takedata()
44+ if 'play music' or 'play songs' in query:
45+ music_dir = "F:\\Songs" #put the location of the folder where you store your songs
46+ songs = os.listdir(music_dir)
47+ l = len(songs)
48+ num = random.randrange(0,l,1)
49+ os.startfile(os.path.join(music_dir,songs[num]))
50+ speak("Thank you for using my sevices. All improvements on my github repository are welcome.")
51+ print("www.github.com/tarun-sharma03")
52+ exit()
53+ else:
54+ speak("Query type not supported")
0 commit comments