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):
"""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.input_rate = input_rate
self.target_rate = target_rate
@ -33,6 +33,7 @@ class VoiceServer(object):
self.input_name = input_name
self.ctx = Context.instance()
self.loop = loop
self.hugvey = hugvey
def get_input_idx(self):
input_device_idx = None
@ -58,14 +59,14 @@ class VoiceServer(object):
# 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)
# for s in self.clients:
try:
# self.loop.call_soon_threadsafe()
if self.hugvey.cmd_server.playPopen is not None:
logger.debug('block recording {}' .format( self.hugvey.cmd_server.playPopen))
# 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 )
# s.send(f)
except Exception as e:
# self.clients.remove(s)
logger.warn("Error sending to {}".format(e))
pass
return (None, pyaudio.paContinue)
@ -157,8 +158,10 @@ class CommandHandler(object):
if file is not None:
logger.info("Play: {}".format(file))
file = self.file_address + "/" + file
logger.debug(['play', file])
self.playPopen = subprocess.Popen(['play', file], stdout=subprocess.PIPE)
returnCode = self.playPopen.wait()
logger.debug('finished')
self.playPopen = None
else:
logger.info("Speak: {}".format(text))
@ -273,19 +276,20 @@ class Hugvey(object):
if self.config['voice']['play_device'] and 'alsaaudio' in sys.modules:
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(
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']
)
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')
# self.voice_server.asyncStart(loop)
# loop.run_until_complete(self.voice_server.start())

View file

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