Skip to content

Commit

Permalink
feat: add energy thresh and max phrase limits to speech server (#241)
Browse files Browse the repository at this point in the history
  • Loading branch information
m-barker authored Jul 6, 2024
1 parent dd3d699 commit f518ec5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# Energy threshold
float32 energy_threshold

# Max phrase duration
float32 max_phrase_limit
---
#result definition
string sequence
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,18 +115,34 @@ class TranscribeSpeechAction(object):
f"Could not find microphone with name: {self._model_params.mic_device}"
)

def _configure_recogniser(self) -> sr.Recognizer:
def _configure_recogniser(
self,
energy_threshold: Optional[float] = None,
pause_threshold: Optional[float] = None,
) -> sr.Recognizer:
"""Configures the speech recogniser object.
Args:
energy_threshold (float): Energy threshold for silence detection. Using this disables automatic adjustment.
pause_threshold (float): Seconds of non-speaking audio before a phrase is considered complete.
Returns:
sr.Recognizer: speech recogniser object.
"""
self._listening = True
recogniser = sr.Recognizer()

if self._model_params.pause_threshold:
if pause_threshold:
recogniser.pause_threshold = pause_threshold

elif self._model_params.pause_threshold:
recogniser.pause_threshold = self._model_params.pause_threshold

if energy_threshold:
recogniser.dynamic_energy_threshold = False
recogniser.energy_threshold = energy_threshold
return recogniser

if self._model_params.energy_threshold:
recogniser.dynamic_energy_threshold = False
recogniser.energy_threshold = self._model_params.energy_threshold
Expand Down Expand Up @@ -160,6 +176,18 @@ class TranscribeSpeechAction(object):
rospy.loginfo("Request Received")
if self._action_server.is_preempt_requested():
return

if goal.energy_threshold > 0.0 and goal.max_phrase_limit > 0.0:
self.recogniser = self._configure_recogniser(
goal.energy_threshold, goal.max_phrase_limit
)
elif goal.energy_threshold > 0.0:
self.recogniser = self._configure_recogniser(goal.energy_threshold)
elif goal.max_phrase_limit > 0.0:
self.recogniser = self._configure_recogniser(
pause_threshold=goal.max_phrase_limit
)

with self._configure_microphone() as src:
self._listening = True
wav_data = self.recogniser.listen(
Expand Down

0 comments on commit f518ec5

Please sign in to comment.