Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace ad-hoc subprocess with python-sounddevice stream callbacks in TTS #33

Closed
PeterBowman opened this issue Jul 24, 2023 · 1 comment
Assignees

Comments

@PeterBowman
Copy link
Member

In #32, I reworked the microphone data acquisition pipeline for our ASR app by using a simple raw input stream callback managed by PortAudio (through the Python sounddevice package, see sounddevice.RawInputStream):

with sd.RawInputStream(blocksize=int(2880 / 2), # FIXME: hardcoded for pocketpshinx, vosk used to have 8000 here
device=args.device,
dtype='int16',
channels=1,
callback=lambda indata, frames, time, status: q.put(bytes(indata))) as stream:
responder = responder_factory.create(stream)
responder.yarp().attachAsServer(configPort)
while True:
frame = q.get()
isPartial, transcription = responder.transcribe(frame)
if transcription:
if not isPartial:
print('result: %s' % transcription)
b = asrPort.prepare()
b.clear()
b.addString(transcription)
asrPort.write()
else:
print('partial: %s' % transcription)

I presume a similar data flow could be implemented in the TTS app with sounddevice.RawOutputStream, thus replacing the ad-hoc subprocess and temporary file hacks currently used:

PLAY_PROGRAMS = ['paplay', 'play -q', 'aplay -q']

with tempfile.NamedTemporaryFile(mode='wb+', suffix='.wav') as wav_file:
wav_file.write(wav_bytes)
wav_file.seek(0)
for play_program in reversed(PLAY_PROGRAMS):
play_cmd = shlex.split(play_program)
if not shutil.which(play_cmd[0]):
continue
play_cmd.append(wav_file.name)
self.is_playing = True
with subprocess.Popen(play_cmd) as self.p:
try:
self.p.wait()
except: # e.g. on keyboard interrupt
self.p.kill()
self.is_playing = False
break

This would also pave the way for the migration from Mimic 3 to Piper, see #30 (comment).

@PeterBowman PeterBowman self-assigned this Dec 12, 2023
@PeterBowman
Copy link
Member Author

Done at c74f814 (I seized the opportunity to migrate to Piper).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant