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

View file

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

View file

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