Blocking audio at client instead of server

This commit is contained in:
Ruben van de Ven 2019-02-25 12:52:23 +01:00
parent c5d5db267e
commit 9b5759a4ac
2 changed files with 25 additions and 21 deletions

View file

@ -23,7 +23,7 @@ logger = logging.getLogger("client")
class VoiceServer(object): class VoiceServer(object):
"""A UDP server, providing mic data at 16 kHz""" """A UDP server, providing mic data at 16 kHz"""
def __init__(self, loop, voice_port: int, input_rate: int, input_name: str = None, target_rate: int = 16000): def __init__(self, loop, hugvey, voice_port: int, input_rate: int, input_name: str = None, target_rate: int = 16000):
self.voice_port = voice_port self.voice_port = voice_port
self.input_rate = input_rate self.input_rate = input_rate
self.target_rate = target_rate self.target_rate = target_rate
@ -33,6 +33,7 @@ class VoiceServer(object):
self.input_name = input_name self.input_name = input_name
self.ctx = Context.instance() self.ctx = Context.instance()
self.loop = loop self.loop = loop
self.hugvey = hugvey
def get_input_idx(self): def get_input_idx(self):
input_device_idx = None input_device_idx = None
@ -57,15 +58,15 @@ class VoiceServer(object):
# chunk 4096, with 2 bytes per frame gives len(in_data) of 8192 # chunk 4096, with 2 bytes per frame gives len(in_data) of 8192
# rate converted 44k1 -> 16k gives len(f) == 2972 (16/44.1 * 8192) # rate converted 44k1 -> 16k gives len(f) == 2972 (16/44.1 * 8192)
f, self.laststate = audioop.ratecv(in_data, 2, 1, self.input_rate, self.target_rate, self.laststate) f, self.laststate = audioop.ratecv(in_data, 2, 1, self.input_rate, self.target_rate, self.laststate)
# for s in self.clients:
try: try:
# self.loop.call_soon_threadsafe() if self.hugvey.cmd_server.playPopen is not None:
self.loop.call_soon_threadsafe( self.voice_socket.send, f ) logger.debug('block recording {}' .format( self.hugvey.cmd_server.playPopen))
# s.send(f) # multiply by 0 to disable audio recording while playback
f = audioop.mul(f, 2, 0)
self.loop.call_soon_threadsafe( self.voice_socket.send, f )
except Exception as e: except Exception as e:
# self.clients.remove(s)
logger.warn("Error sending to {}".format(e)) logger.warn("Error sending to {}".format(e))
pass pass
return (None, pyaudio.paContinue) return (None, pyaudio.paContinue)
@ -157,8 +158,10 @@ class CommandHandler(object):
if file is not None: if file is not None:
logger.info("Play: {}".format(file)) logger.info("Play: {}".format(file))
file = self.file_address + "/" + file file = self.file_address + "/" + file
logger.debug(['play', file])
self.playPopen = subprocess.Popen(['play', file], stdout=subprocess.PIPE) self.playPopen = subprocess.Popen(['play', file], stdout=subprocess.PIPE)
returnCode = self.playPopen.wait() returnCode = self.playPopen.wait()
logger.debug('finished')
self.playPopen = None self.playPopen = None
else: else:
logger.info("Speak: {}".format(text)) logger.info("Speak: {}".format(text))
@ -273,19 +276,20 @@ class Hugvey(object):
if self.config['voice']['play_device'] and 'alsaaudio' in sys.modules: if self.config['voice']['play_device'] and 'alsaaudio' in sys.modules:
alsaaudio.Mixer(self.config['voice']['play_device']).setvolume(self.config['voice']['play_volume']) alsaaudio.Mixer(self.config['voice']['play_device']).setvolume(self.config['voice']['play_volume'])
self.voice_server = VoiceServer(
loop = loop,
voice_port = int(self.config['voice']['port']),
input_rate = int(self.config['voice']['input_rate']),
input_name = self.config['voice']['input_name'],
target_rate = int(self.config['voice']['target_rate']),
)
self.cmd_server = CommandHandler( self.cmd_server = CommandHandler(
hugvey_id = self.id, hugvey_id = self.id,
cmd_address = self.config['events']['cmd_address'], cmd_address = self.config['events']['cmd_address'],
publish_address = self.config['events']['publish_address'], publish_address = self.config['events']['publish_address'],
file_address = self.config['voice']['file_address'] file_address = self.config['voice']['file_address']
) )
self.voice_server = VoiceServer(
loop = loop,
hugvey = self,
voice_port = int(self.config['voice']['port']),
input_rate = int(self.config['voice']['input_rate']),
input_name = self.config['voice']['input_name'],
target_rate = int(self.config['voice']['target_rate']),
)
logger.info('start') logger.info('start')
# self.voice_server.asyncStart(loop) # self.voice_server.asyncStart(loop)
# loop.run_until_complete(self.voice_server.start()) # loop.run_until_complete(self.voice_server.start())

View file

@ -533,9 +533,9 @@ class Story(object):
self.lastMsgFinishTime = self.timer.getElapsed() self.lastMsgFinishTime = self.timer.getElapsed()
# 2019-02-22 temporary disable listening while playing audio: # 2019-02-22 temporary disable listening while playing audio:
if self.hugvey.google is not None: # if self.hugvey.google is not None:
logger.warn("Temporary 'fix' -> resume recording?") # logger.warn("Temporary 'fix' -> resume recording?")
self.hugvey.google.resume() # self.hugvey.google.resume()
if self.currentMessage.id not in self.directionsPerMsg: if self.currentMessage.id not in self.directionsPerMsg:
logger.info("THE END!") logger.info("THE END!")
@ -647,9 +647,9 @@ class Story(object):
}) })
# 2019-02-22 temporary disable listening while playing audio: # 2019-02-22 temporary disable listening while playing audio:
if self.hugvey.google is not None: # if self.hugvey.google is not None:
logger.warn("Temporary 'fix' -> stop recording") # logger.warn("Temporary 'fix' -> stop recording")
self.hugvey.google.pause() # self.hugvey.google.pause()
logger.debug("Pending directions: ") logger.debug("Pending directions: ")