-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVoiceHAB.py
More file actions
92 lines (70 loc) · 3.29 KB
/
VoiceHAB.py
File metadata and controls
92 lines (70 loc) · 3.29 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import random, requests, re
import logging as L
import speech_recognition as sr
import Settings
import Messages as M
import OtherRequests as OR
import SpeechToText as STT
import VoiceRecorder as VR
import SoundPlayer as SP
Instance = None
def init():
L.debug('Initializing')
global Instance
Instance = Main()
class Main():
def ListenForWakeUp(self):
TTSReady = random.choice(open(Settings.FinalizationDataFile).readlines())
M.ProcessMessage(TTSReady)
while True:
L.debug('Listening for a wake-up phrase')
RecognizedWakeUp = ''
RecognizedWakeUp = STT.SpeechToText(Settings.UseOfflineWakeUp)
WakeUpRegExPattern = r'\b%s\b' % Settings.WakeUpPhrase
WakeUpRegExResult = re.search(WakeUpRegExPattern, RecognizedWakeUp, re.IGNORECASE)
if WakeUpRegExResult:
if Settings.UseTextToSpeech:
TTSGreeting = random.choice(open(Settings.GreetingDataFile).readlines())
M.ProcessMessage(TTSGreeting)
else:
SP.PlayAudioFile(FileName = 'StartBeep.mp3')
self.ListenForCommand()
def ListenForCommand(self):
L.debug('Listening for a voice command')
RecognizedCommand = ''
RecognizedCommand = STT.SpeechToText()
if RecognizedCommand != '':
CommandRegExPattern = r'\b%s\b' % Settings.GeneralKnowledgeTriggerPhrase
CommandRegExResult = re.search(CommandRegExPattern, RecognizedCommand, re.IGNORECASE)
SP.PlayAudioFile(FileName = 'EndBeep.mp3')
if (CommandRegExResult) and (Settings.UseGeneralKnowledge):
if Settings.GeneralKnowledgeEngine.lower() == 'apiai':
OR.QueryApiAI(RecognizedCommand)
else:
VoiceCommandItemURL = ''
if Settings.Port.strip() != '':
TrimmedHostAndPort = Settings.HostName.strip() + ':' + Settings.Port.strip()
else:
TrimmedHostAndPort = Settings.HostName.strip()
if Settings.SSLConnection:
URLPrefix = 'https://'
else:
URLPrefix = 'http://'
VoiceCommandItemURL = URLPrefix + TrimmedHostAndPort + '/CMD?' + Settings.VoiceCommandItem + '=' + '"' + RecognizedCommand + '"'
if (Settings.Username.strip() != '') and (Settings.Password.strip() != ''):
HTTPGetResult = requests.get(VoiceCommandItemURL, auth=(Settings.Username.strip(), Settings.Password.strip()))
else:
HTTPGetResult = requests.get(VoiceCommandItemURL)
def InitializeModules(self):
TTSInitialization = random.choice(open(Settings.InitializationDataFile).readlines())
M.ProcessMessage(TTSInitialization)
global Mic
global Rec
global MicThreshold
global AuthenticatedUserId
Mic = sr.Microphone()
Rec = sr.Recognizer()
AuthenticatedUserId = ''
with Mic as SourceInitialize:
Rec.adjust_for_ambient_noise(SourceInitialize)
MicThreshold = Rec.energy_threshold