3
3
from button import Button
4
4
from query import answerQuery as aq
5
5
import speech_recognition as sr
6
+ import os
7
+ import platform
6
8
7
9
pygame .init ()
8
10
@@ -39,6 +41,8 @@ def main():
39
41
btnSearch = Button (WIDTH // 4 * 3 , HEIGHT // 4 , btnSearch , 0.8 )
40
42
btnQuit = Button (WIDTH // 4 * 3 , HEIGHT // 8 * 7 , btnQuit , 0.8 )
41
43
btnSearchVoice = Button (WIDTH // 4 * 3 , HEIGHT // 2 , btnSearchVoice , 0.8 )
44
+ btnSolarSystem = Button (WIDTH // 10 , HEIGHT // 4 * 3 , btnSolarSystem , 0.8 )
45
+ btnTrackISS = Button (WIDTH // 5 * 3 , HEIGHT // 4 * 3 , btnTrackISS , 0.8 )
42
46
43
47
# TEXT INPUT
44
48
baseFont = pygame .font .Font (None , 40 )
@@ -75,22 +79,21 @@ def main():
75
79
if btnSearch .draw (SCREEN ):
76
80
spaceQuery (userText )
77
81
if btnSearchVoice .draw (SCREEN ):
78
- voiceText = "Ask your question."
79
-
80
- # DISPLAY VOICE INPUT
81
- voiceColour = voiceActiveColour
82
- pygame .draw .rect (SCREEN , voiceColour , voiceInputrect )
83
- voiceTextSurface = baseFont .render (
84
- voiceText , True , (255 , 255 , 255 ))
85
- SCREEN .blit (voiceTextSurface ,
86
- (voiceInputrect .x + 5 , voiceInputrect .y + 5 ))
87
- pygame .display .flip ()
82
+ voiceActive = True
88
83
searchVoice = True
84
+ voiceText = "Listening..."
85
+ if btnSolarSystem .draw (SCREEN ):
86
+ return
87
+ if btnTrackISS .draw (SCREEN ):
88
+ if platform .system () == "Windows" :
89
+ os .system ("python iss.py" )
90
+ else :
91
+ os .system ("python3 iss.py" )
89
92
if btnQuit .draw (SCREEN ):
90
93
pygame .quit ()
91
94
sys .exit ()
92
95
93
- # DISPLAY AND TYPE IN TEXT INPUT
96
+ # DISPLAY TEXT INPUT
94
97
if active :
95
98
colour = activeColour
96
99
else :
@@ -101,13 +104,17 @@ def main():
101
104
SCREEN .blit (textSurface , (inputRect .x + 5 , inputRect .y + 5 ))
102
105
103
106
# DISPLAY VOICE INPUT
104
- voiceColour = voicePassiveColour
107
+ if voiceActive :
108
+ voiceColour = voiceActiveColour
109
+ else :
110
+ voiceColour = voicePassiveColour
105
111
pygame .draw .rect (SCREEN , voiceColour , voiceInputrect )
106
112
voiceTextSurface = baseFont .render (
107
113
voiceText , True , (255 , 255 , 255 ))
108
114
SCREEN .blit (voiceTextSurface , (voiceInputrect .x + 5 , voiceInputrect .y + 5 ))
109
115
pygame .display .flip ()
110
116
117
+ # TEXT
111
118
# Quits program if application is closed
112
119
for event in pygame .event .get ():
113
120
if event .type == pygame .QUIT :
@@ -138,23 +145,30 @@ def main():
138
145
userText += event .unicode
139
146
if event .key == pygame .K_RETURN :
140
147
spaceQuery (userText )
148
+
149
+ # VOICE
150
+ if event .type == pygame .MOUSEBUTTONDOWN :
151
+ if btnSearchVoice .rect .collidepoint (event .pos ):
152
+ voiceActive = True
153
+ else :
154
+ voiceActive = False
155
+
141
156
if searchVoice :
142
- # variable for checking if the program did 't cath the voice
157
+ # variable for checking if the program didn 't catch the voice
143
158
correct = True
144
159
r = sr .Recognizer ()
145
160
while correct :
146
161
text = ''
147
162
with sr .Microphone () as source :
148
163
# listen for 1 second to calibrate the energy threshold for ambient noise levels
149
164
r .adjust_for_ambient_noise (source )
165
+ # read the audio data from the default microphone, will record for 10 seconds
166
+ # audio_data = r.record(source, duration=10)
150
167
audio_data = r .listen (source )
151
- correct = True
168
+ # convert speech to text
152
169
try :
153
170
text = r .recognize_google (audio_data )
154
- voiceText = "Did you say: " + text
155
-
156
- # DISPLAY VOICE INPUT
157
- voiceColour = voicePassiveColour
171
+ voiceText = text
158
172
pygame .draw .rect (
159
173
SCREEN , voiceColour , voiceInputrect )
160
174
voiceTextSurface = baseFont .render (
@@ -163,28 +177,13 @@ def main():
163
177
voiceTextSurface , (voiceInputrect .x + 5 , voiceInputrect .y + 5 ))
164
178
pygame .display .flip ()
165
179
166
- while correct :
167
- if btnSearchVoice .rect .collidepoint (event .pos ):
168
- spaceQuery (voiceText )
169
- elif background .rect .collidepoint (event .pos ):
170
- correct = False
171
- raise Exception ()
180
+ spaceQuery (voiceText )
172
181
except :
173
- voiceText = "Sorry, I didn't get that. Try again."
174
- # DISPLAY VOICE INPUT
175
- voiceColour = voicePassiveColour
176
- pygame .draw .rect (
177
- SCREEN , voiceColour , voiceInputrect )
178
- voiceTextSurface = baseFont .render (
179
- voiceText , True , (255 , 255 , 255 ))
180
- SCREEN .blit (
181
- voiceTextSurface , (voiceInputrect .x + 5 , voiceInputrect .y + 5 ))
182
- pygame .display .flip ()
182
+ voiceText = "Sorry, I didn't get that"
183
183
main ()
184
- searchVoice = False
185
184
186
- # except sr.RequestError as e:
187
- #print("Could not request results from Google Speech Recognition service; {0}".format(e))
185
+ # except sr.RequestError as e:
186
+ #print("Could not request results from Google Speech Recognition service; {0}".format(e))
188
187
189
188
pygame .display .update ()
190
189
0 commit comments