Skip to content

Commit

Permalink
Added listen functionality for microphone using GAPI
Browse files Browse the repository at this point in the history
  • Loading branch information
etsardou authored and klpanagi committed Jan 20, 2021
1 parent dba0687 commit 3d07821
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 2 deletions.
6 changes: 4 additions & 2 deletions stream_simulator/controllers/effectors/controller_speaker.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,10 @@ def on_goal_speak(self, goalh):

self.speaker.volume = volume
self.speaker.async_write(response.audio_content, file_flag=False)
self.logger.info("Speaking...")
while self.speaker.playing:
print("Speaking...")
time.sleep(0.1)
self.logger.info("Speaking done")

self.logger.info("{} Speak finished".format(self.name))
self.blocked = False
Expand All @@ -236,9 +237,10 @@ def google_speak(self, language = None, texts = None, volume = None):

self.speaker.volume = volume
self.speaker.async_write(response.audio_content, file_flag=False)
self.logger.info("Speaking...")
while self.speaker.playing:
print("Speaking...")
time.sleep(0.1)
self.logger.info("Speaking done")

def on_goal_play(self, goalh):
self.logger.info("{} play started".format(self.name))
Expand Down
66 changes: 66 additions & 0 deletions stream_simulator/controllers/sensors/controller_microphone.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ def __init__(self, conf = None, package = None):
callback = self.on_goal,
action_name = info["base_topic"] + ".record"
)
self.listen_action_server = CommlibFactory.getActionServer(
callback = self.on_goal_listen,
action_name = info["base_topic"] + ".listen"
)
self.enable_rpc_server = CommlibFactory.getRPCService(
broker = "redis",
callback = self.enable_callback,
Expand Down Expand Up @@ -287,6 +291,64 @@ def on_goal(self, goalh):
self.blocked = False
return ret

def on_goal_listen(self, goalh):
self.logger.info("{} listening started".format(self.name))
if self.info["enabled"] == False:
return {}

# ELSA stuff
import os
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "/home/pi/google_ttsp.json"

# Concurrent speaker calls handling
while self.blocked:
time.sleep(0.1)
self.logger.info("Microphone unlocked")
self.blocked = True

try:
duration = goalh.data["duration"]
language = goalh.data["language"]
except Exception as e:
self.logger.error("{} goal had no duration and language as parameter".format(self.name))

if self.info["mode"] == "real": # The real deal
self.sensor.async_read(secs = duration, volume = 100, framerate = self.conf["framerate"])
now = time.time()
while time.time() - now < duration + 0.2:
if goalh.cancel_event.is_set():
self.logger.info("Cancel got")
self.blocked = False
return ret
time.sleep(0.1)

rec = base64.b64encode(self.sensor.record).decode("ascii")
rec = base64.b64decode(rec)

from google.cloud import speech
from google.cloud.speech import enums
from google.cloud.speech import types
self.client = speech.SpeechClient()
speech_config = types.RecognitionConfig(
encoding=enums.RecognitionConfig.AudioEncoding.LINEAR16,
sample_rate_hertz=44100,
language_code='el-GR'
)

text = self.client.recognize(
config = speech_config,
audio = types.RecognitionAudio(content = rec)
)
self.logger.info(f"Results: {text}")
if len(text.results):
text = text.results[0].alternatives[0].transcript
else:
text = ''

self.logger.info("Listening finished: " + str(text))
self.blocked = False
return {'text': text}

def enable_callback(self, message, meta):
self.info["enabled"] = True
return {"enabled": True}
Expand All @@ -297,12 +359,16 @@ def disable_callback(self, message, meta):

def start(self):
self.record_action_server.run()
self.listen_action_server.run()
self.enable_rpc_server.run()
self.disable_rpc_server.run()

def stop(self):
self.record_action_server._goal_rpc.stop()
self.record_action_server._cancel_rpc.stop()
self.record_action_server._result_rpc.stop()
self.listen_action_server._goal_rpc.stop()
self.listen_action_server._cancel_rpc.stop()
self.listen_action_server._result_rpc.stop()
self.enable_rpc_server.stop()
self.disable_rpc_server.stop()

0 comments on commit 3d07821

Please sign in to comment.