diff options
| author | schneefux <schneefux+commit@schneefux.xyz> | 2014-09-27 13:40:11 +0200 |
|---|---|---|
| committer | schneefux <schneefux+commit@schneefux.xyz> | 2014-09-27 13:40:11 +0200 |
| commit | 5d6d5177f9bf4947d861815db837dfb813a4964c (patch) | |
| tree | bbbc5b3e1b9125e2f7ff137d8ad3fbd6828c8368 /client/mic.py | |
| parent | 16905e89b0e0d15f86df4cdf6536a396e06c49e6 (diff) | |
| parent | 8d2e10462a79fc34c446d837cb9af6a5d5171efd (diff) | |
| download | jasper-client-5d6d5177f9bf4947d861815db837dfb813a4964c.tar.gz jasper-client-5d6d5177f9bf4947d861815db837dfb813a4964c.zip | |
Merge pull request #176 from Holzhaus/improved-stt-module
Improved stt module
Diffstat (limited to 'client/mic.py')
| -rw-r--r-- | client/mic.py | 51 |
1 files changed, 25 insertions, 26 deletions
diff --git a/client/mic.py b/client/mic.py index 0351592..2e8d603 100644 --- a/client/mic.py +++ b/client/mic.py @@ -4,11 +4,13 @@ """ import os -from wave import open as open_audio +import tempfile +import wave import audioop import pyaudio import alteration import jasperpath +from stt import TranscriptionMode class Mic: @@ -80,7 +82,6 @@ class Mic: """ THRESHOLD_MULTIPLIER = 1.8 - AUDIO_FILE = "passive.wav" RATE = 16000 CHUNK = 1024 @@ -154,15 +155,17 @@ class Mic: stream.stop_stream() stream.close() audio.terminate() - write_frames = open_audio(AUDIO_FILE, 'wb') - write_frames.setnchannels(1) - write_frames.setsampwidth(audio.get_sample_size(pyaudio.paInt16)) - write_frames.setframerate(RATE) - write_frames.writeframes(''.join(frames)) - write_frames.close() - - # check if PERSONA was said - transcribed = self.passive_stt_engine.transcribe(AUDIO_FILE, PERSONA_ONLY=True) + + with tempfile.NamedTemporaryFile(mode='w+b') as f: + wav_fp = wave.open(f, 'wb') + wav_fp.setnchannels(1) + wav_fp.setsampwidth(audio.get_sample_size(pyaudio.paInt16)) + wav_fp.setframerate(RATE) + wav_fp.writeframes(''.join(frames)) + wav_fp.close() + f.seek(0) + # check if PERSONA was said + transcribed = self.passive_stt_engine.transcribe(f, mode=TranscriptionMode.KEYWORD) if PERSONA in transcribed: return (THRESHOLD, PERSONA) @@ -187,18 +190,10 @@ class Mic: Returns a list of the matching options or None """ - AUDIO_FILE = "active.wav" RATE = 16000 CHUNK = 1024 LISTEN_TIME = 12 - # user can request pre-recorded sound - if not LISTEN: - if not os.path.exists(AUDIO_FILE): - return None - - return self.active_stt_engine.transcribe(AUDIO_FILE) - # check if no threshold provided if THRESHOLD == None: THRESHOLD = self.fetchThreshold() @@ -238,14 +233,18 @@ class Mic: stream.stop_stream() stream.close() audio.terminate() - write_frames = open_audio(AUDIO_FILE, 'wb') - write_frames.setnchannels(1) - write_frames.setsampwidth(audio.get_sample_size(pyaudio.paInt16)) - write_frames.setframerate(RATE) - write_frames.writeframes(''.join(frames)) - write_frames.close() - return self.active_stt_engine.transcribe(AUDIO_FILE, MUSIC=MUSIC) + with tempfile.SpooledTemporaryFile(mode='w+b') as f: + wav_fp = wave.open(f, 'wb') + wav_fp.setnchannels(1) + wav_fp.setsampwidth(audio.get_sample_size(pyaudio.paInt16)) + wav_fp.setframerate(RATE) + wav_fp.writeframes(''.join(frames)) + wav_fp.close() + f.seek(0) + mode = TranscriptionMode.MUSIC if MUSIC else TranscriptionMode.NORMAL + transcribed = self.active_stt_engine.transcribe(f, mode=mode) + return transcribed def say(self, phrase, OPTIONS=" -vdefault+m3 -p 40 -s 160 --stdout > say.wav"): # alter phrase before speaking |
