Skip to content

Commit

Permalink
speech-rec rises event when not recognized, fixed calibration bug
Browse files Browse the repository at this point in the history
  • Loading branch information
igor-lirussi committed Aug 17, 2020
1 parent ceacebe commit d05816f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
2 changes: 2 additions & 0 deletions module_receiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ def main():
BaseSpeechReceiverModule.start()

if(False):
print("False, auto-detection not available")
#one-shot recording for at least 5 seconds
SpeechRecognition = ALProxy("SpeechRecognition")
SpeechRecognition.start()
Expand All @@ -122,6 +123,7 @@ def main():
SpeechRecognition.startRecording()

else:
print("True, auto-detection selected")
# auto-detection
SpeechRecognition = ALProxy("SpeechRecognition")
SpeechRecognition.start()
Expand Down
37 changes: 28 additions & 9 deletions module_speechrecognition.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ def processRemote( self, nbOfChannels, nbrOfSamplesByChannel, aTimeStamp, buffer
# start recording if we are not doing so already
if (self.isAutoDetectionEnabled and not self.isRecording and not self.isCalibrating):
self.startRecording()
print("threshold surpassed: " + str(rmsMicFront) + " more than " + str(self.autoDetectionThreshold))

# perform calibration
if( self.isCalibrating):
Expand Down Expand Up @@ -342,8 +343,8 @@ def stopCalibration(self):
self.isCalibrating = False

# calculate avg rms over self.framesCount
self.threshold = CALIBRATION_THRESHOLD_FACTOR * (self.rmsSum / self.framesCount)
print 'calibration done, RMS threshold is: ' + str(self.threshold)
self.autoDetectionThreshold = CALIBRATION_THRESHOLD_FACTOR * (self.rmsSum / self.framesCount)
print 'calibration done, RMS threshold is: ' + str(self.autoDetectionThreshold)
return

def enableAutoDetection(self):
Expand All @@ -353,10 +354,12 @@ def enableAutoDetection(self):

def disableAutoDetection(self):
self.isAutoDetectionEnabled = False
print 'INF: AutoDetection Disabled '
return

def setLanguage(self, language = DEFAULT_LANGUAGE):
self.language = language
print 'SET: language set to ' + language
return

# used for RMS calculation
Expand Down Expand Up @@ -404,32 +407,48 @@ def recognize(self, data):
try:
result = r.recognize_google(audio_data=buffer, samplerate=SAMPLE_RATE, language=self.language)
self.memory.raiseEvent("SpeechRecognition", result)
print 'RESULT: ' + result
print '---RESULT---: ' + result
except UnknownValueError:
print 'ERR: Recognition error'
self.memory.raiseEvent("SpeechRecognition", "error")
except RequestError, e:
print 'ERR: ' + str(e)
except socket.timeout:
print 'ERR: Socket timeout'
except:
print 'ERR: Unknown, probably timeout ' + str(sys.exc_info()[0])



def setAutoDetectionThreshold(self, threshold):
self.autoDetectionThreshold = threshold
print 'SET: AutoDetection Threshold set to ' + str(self.autoDetectionThreshold)

def setMaxRecordingDuration(self, duration):
self.recordingDuration = duration

def setLookaheadDuration(self, duration):
self.lookaheadBufferSize = duration * SAMPLE_RATE
self.preBuffer = []
self.preBufferLength = 0
print 'SET: lookahead duration set to ' + str(duration)

def setIdleReleaseTime(self, releaseTime):
self.idleReleaseTime = releaseTime
print 'SET: Idle release time set to ' + str(releaseTime)

def setHoldTime(self, holdTime):
self.holdTime = holdTime
print 'SET: Hold time set to ' + str(holdTime)

def setMaxRecordingDuration(self, duration):
self.recordingDuration = duration
def printInfo(self):
print "INFO"
print "language: "+ self.language
print "autoDetection: " + str(self.isAutoDetectionEnabled)
print 'Lookahead duration: ' + str(self.lookaheadBufferSize/SAMPLE_RATE)
print 'Idle release time: ' + str(self.idleReleaseTime)
print 'Hold time set to: ' + str(self.holdTime)

def setLookaheadDuration(self, duration):
self.lookaheadBufferSize = duration * SAMPLE_RATE
self.preBuffer = []
self.preBufferLength = 0

# SpeechRecognition - end

Expand Down

0 comments on commit d05816f

Please sign in to comment.