From c4f55c01c78416c2556cd673c8d9767e410313c3 Mon Sep 17 00:00:00 2001 From: Ruben van de Ven Date: Mon, 8 Apr 2019 12:16:41 +0200 Subject: [PATCH] Workaround for crash of voice fetch, sox audiodev in config, client waits a sec to allow mics to come up --- hugvey/central_command.py | 4 ++++ hugvey/client.py | 16 ++++++++++++---- hugvey/story.py | 4 ++-- hugvey/voice.py | 5 +++-- 4 files changed, 21 insertions(+), 8 deletions(-) diff --git a/hugvey/central_command.py b/hugvey/central_command.py index ad959ec..7358900 100644 --- a/hugvey/central_command.py +++ b/hugvey/central_command.py @@ -247,6 +247,10 @@ class CentralCommand(object): isVariable = bool(r['variable']) text = r['text'] fn = await self.voiceStorage.requestFile(text, isVariable) + if fn is None: + eventLogger.getChild(f"{hugvey_id}").critical("error: No voice file fetched, check logs.") + fn = 'local/crash.wav' + # TODO: trigger a repeat/crash event. await s.send_string(fn) except Exception as e: logger.critical(f"Exception while running voice loop:") diff --git a/hugvey/client.py b/hugvey/client.py index f55a37c..dca13b5 100644 --- a/hugvey/client.py +++ b/hugvey/client.py @@ -11,6 +11,7 @@ import zmq from zmq.asyncio import Context import sys from hugvey.communication import LOG_BS +import os try: import alsaaudio @@ -85,6 +86,9 @@ class VoiceServer(object): self.p = pyaudio.PyAudio() self.stopped = False + + # wait a sec for the input devices to come up + await asyncio.sleep(2) stream = self.p.open( format=FORMAT, @@ -127,7 +131,7 @@ class VoiceServer(object): class CommandHandler(object): - def __init__(self, hugvey_id, cmd_address, publish_address, file_address): + def __init__(self, hugvey_id, cmd_address, publish_address, file_address, play_audiodev = None): self.eventQueue = [] self.ctx = Context.instance() self.hugvey_id = hugvey_id @@ -136,6 +140,7 @@ class CommandHandler(object): self.playPopen = None self.file_address = file_address self.playingMsgId = None + self.play_audiodev = play_audiodev # self.showMyself() # queue message for connection request def handle(self, cmd): @@ -178,10 +183,12 @@ class CommandHandler(object): if value is True: continue playCmd.append(str(value)) - + environment_vars = dict(os.environ) + if self.play_audiodev is not None: + environment_vars['AUDIODEV'] = self.play_audiodev logger.debug(playCmd) self.playPopen = subprocess.Popen( - playCmd, stdout=subprocess.PIPE) + playCmd, stdout=subprocess.PIPE, env=environment_vars) returnCode = self.playPopen.wait() logger.debug('finished') @@ -308,7 +315,8 @@ class Hugvey(object): hugvey_id=self.id, cmd_address=self.config['events']['cmd_address'], publish_address=self.config['events']['publish_address'], - file_address=self.config['voice']['file_address'] + file_address=self.config['voice']['file_address'], + play_audiodev=self.config['voice']['play_audiodev'] ) self.voice_server = VoiceServer( loop=loop, diff --git a/hugvey/story.py b/hugvey/story.py index 57cb1d7..3a2002b 100644 --- a/hugvey/story.py +++ b/hugvey/story.py @@ -889,10 +889,10 @@ class Story(object): self.hugvey.eventLogger.info(f"message: {message.id} {message.uuid} start \"{message.text}\"") # TODO: prep events & timer etc. - # TODO: preload file paths if no variables are set, or once these are loaded + fn = await message.getAudioFilePath() self.hugvey.sendCommand({ 'action': 'play', - 'file': await message.getAudioFilePath(), + 'file': fn, 'id': message.id, 'params': message.getParams() }) diff --git a/hugvey/voice.py b/hugvey/voice.py index b40143b..2da33b8 100644 --- a/hugvey/voice.py +++ b/hugvey/voice.py @@ -70,6 +70,7 @@ class VoiceStorage(object): response = await http_client.fetch(request) except Exception as e: logger.exception(e) + logger.critical(request) self.pendingRequests[id].set() http_client.close() return None @@ -80,10 +81,10 @@ class VoiceStorage(object): http_client.close() return None - logger.debug(f"Wrote body: {response.code}") +# logger.debug(f"Wrote body: {response.code}") with open(fn, "wb") as f: f.write(response.body) self.pendingRequests[id].set() - print(type(fn), fn) +# print(type(fn), fn) http_client.close() return fn