Allow logging without voice

This commit is contained in:
Hugvey Central Command 2019-05-17 16:41:03 +02:00
parent be10bfe44c
commit 28d8d26ce7
3 changed files with 58 additions and 47 deletions

View file

@ -579,7 +579,7 @@ class HugveyState(object):
else:
if self.story is None:
return
if event['reloadStory']:
self.startMsgId = event['msg_id']
self.logger.debug(f"Restart from {self.startMsgId}")
@ -709,7 +709,7 @@ class HugveyState(object):
else:
# new story instance on each run
port = self.command.config['web']['port']
resuming = False
if Story.hugveyHasSavedState(self.id):
self.logger.info(f"Recovering from state :-)")
@ -721,20 +721,20 @@ class HugveyState(object):
else:
self.story = Story(self, port)
self.story.setStoryData(copy.deepcopy(self.command.languages[self.language_code]), self.language_code)
if not self.streamer:
await asyncio.sleep(1)
self.streamer.triggerStart()
startMsgId = self.startMsgId
self.startMsgId = None # use only once, reset before 'run'
self.startMsgId = None # use only once, reset before 'run'
if not startMsgId and self.story.currentMessage:
startMsgId = self.story.currentMessage.id
self.logger.info(f"Starting from {startMsgId}")
self.setLightStatus(False)
await self.story.run(startMsgId, resuming)
# self.story = None
@ -756,7 +756,9 @@ class HugveyState(object):
if self.command.config['voice']['record_dir']:
self.logger.warn("Record Audio of conversation")
self.recorder = Recorder( self.id,
self.command.config['voice']['src_rate'], self.command.config['voice']['record_dir'])
self.command.config['voice']['src_rate'], self.command.config['voice']['record_dir'],
self.command.config['voice']['record_voice'] if 'record_voice' in self.command.config['voice'] else False)
self.streamer.addConsumer(self.recorder)
self.logger.debug("Start Speech")

View file

@ -16,9 +16,9 @@ class Recorder:
Record the streamed audio
"""
def __init__(self, hv_id, src_rate, out_folder):
def __init__(self, hv_id, src_rate, out_folder, record_voice = True):
self.logger = mainLogger.getChild(f"{hv_id}").getChild('recorder')
if not os.path.exists(out_folder):
raise Exception(f"Invalid output folder for recordings: {out_folder}")
@ -26,62 +26,69 @@ class Recorder:
self.src_rate = src_rate
self.main_folder = out_folder # unfortunately not every device plays 16kHz audio streams
self.running = False
self.record_voice = record_voice
self.data = array('h')
self.currentLog = []
def start(self):
self.subsequentMutedFrames = 0
self.fragmentNr = 0
self.data = array('h')
self.currentTranscription = ""
self.currentLog = []
day = time.strftime("%Y%m%d")
t = time.strftime("%H:%M:%S")
self.out_folder = os.path.join(self.main_folder, day, f"{self.hv_id}", t)
if not os.path.exists(self.out_folder):
self.logger.debug(f"Create directory {self.out_folder}")
self.target_folder = os.makedirs(self.out_folder, exist_ok=True)
self.running = True
def writeData(self):
if len(self.data) < 1:
self.logger.info("Skip empty wave creation")
return
self.fragmentNr += 1
fn = os.path.join(self.out_folder, f"{self.fragmentNr}.wav")
self.logger.info(f"Write wave: {fn}")
self.wf = wave.open(fn, 'wb')
self.wf.setnchannels(1)
self.wf.setsampwidth(2)
self.wf.setframerate(self.src_rate)
# adapted from https://stackoverflow.com/questions/892199/detect-record-audio-in-python#6743593
self.wf.writeframes(pack('<' + ('h'*len(self.data)), *self.data))
self.wf.close()
with open(os.path.join(self.out_folder, "transcriptions.txt"), "a") as fp:
fp.write(f"{self.fragmentNr}\t{self.currentTranscription}\n")
if self.record_voice:
self.fragmentNr += 1
fn = os.path.join(self.out_folder, f"{self.fragmentNr}.wav")
self.logger.info(f"Write wave: {fn}")
self.wf = wave.open(fn, 'wb')
self.wf.setnchannels(1)
self.wf.setsampwidth(2)
self.wf.setframerate(self.src_rate)
# adapted from https://stackoverflow.com/questions/892199/detect-record-audio-in-python#6743593
self.wf.writeframes(pack('<' + ('h'*len(self.data)), *self.data))
self.wf.close()
with open(os.path.join(self.out_folder, "transcriptions.txt"), "a") as fp:
fp.write(f"{self.fragmentNr}\t{self.currentTranscription}\n")
self.log('-',self.currentTranscription)
self.data = array('h')
self.currentTranscription = ""
def receive(self, chunk):
if not self.record_voice:
return
if not self.running:
return
return
# self.logger.debug('receive {}'.format(len(chunk)))
if audioop.max(chunk, 2) == 0:
# mic is muted on client side.
self.subsequentMutedFrames += 1
#self.logger.debug(f"Empty frame {self.subsequentMutedFrames}")
if self.subsequentMutedFrames == 4:
@ -92,27 +99,27 @@ class Recorder:
return
else:
self.subsequentMutedFrames = 0
d = array('h', chunk)
self.data.extend(d)
# self.wf.writeframes(chunk)
def shutdown(self):
self.writeData()
self.running = False
# self.wf.close()
def triggerStart(self):
self.start()
def updateTranscription(self, text):
self.currentTranscription = text
def log(self, origin, msg, extra=None):
self.currentLog.append({'time':time.time(), 'origin': origin, 'msg': msg, 'extra': extra})
with open(os.path.join(self.out_folder, "log.txt"), "a") as fp:
fp.write(f"{origin}: {msg}\n")
def __del__(self):
self.logger.warn("Destroyed recorder object")
self.logger.warn("Destroyed recorder object")

View file

@ -7,6 +7,8 @@ voice:
port: 4444
chunk: 2972
google_credentials: "../test_googlespeech/My First Project-0c7833e0d5fa.json"
record_dir: "./recordings"
record_voice: false
hugveys: 26
languages:
- code: en-GB