summaryrefslogtreecommitdiff
path: root/client
diff options
context:
space:
mode:
Diffstat (limited to 'client')
-rw-r--r--client/mic.py52
-rw-r--r--client/stt.py23
2 files changed, 38 insertions, 37 deletions
diff --git a/client/mic.py b/client/mic.py
index ccc322e..2bb8c57 100644
--- a/client/mic.py
+++ b/client/mic.py
@@ -4,7 +4,8 @@
"""
import os
-from wave import open as open_audio
+import tempfile
+import wave
import audioop
import pyaudio
import alteration
@@ -81,7 +82,6 @@ class Mic:
"""
THRESHOLD_MULTIPLIER = 1.8
- AUDIO_FILE = "passive.wav"
RATE = 16000
CHUNK = 1024
@@ -155,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, mode=TranscriptionMode.KEYWORD)
+
+ 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)
@@ -175,18 +177,10 @@ class Mic:
Records until a second of silence or times out after 12 seconds
"""
- 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()
@@ -226,16 +220,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()
-
- mode = TranscriptionMode.MUSIC if MUSIC else TranscriptionMode.NORMAL
- return self.active_stt_engine.transcribe(AUDIO_FILE, mode=mode)
+ 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
diff --git a/client/stt.py b/client/stt.py
index 29fb79c..4a46e71 100644
--- a/client/stt.py
+++ b/client/stt.py
@@ -33,7 +33,7 @@ class AbstractSTTEngine(object):
return True
@abstractmethod
- def transcribe(self, audio_file_path, mode=TranscriptionMode.NORMAL):
+ def transcribe(self, fp, mode=TranscriptionMode.NORMAL):
pass
class PocketSphinxSTT(AbstractSTTEngine):
@@ -107,7 +107,7 @@ class PocketSphinxSTT(AbstractSTTEngine):
config['dictd_music'] = profile['pocketsphinx']['dictd_music']
return config
- def transcribe(self, audio_file_path, mode=TranscriptionMode.NORMAL):
+ def transcribe(self, fp, mode=TranscriptionMode.NORMAL):
"""
Performs STT, transcribing an audio file and returning the result.
@@ -116,12 +116,17 @@ class PocketSphinxSTT(AbstractSTTEngine):
PERSONA_ONLY -- if True, uses the 'Persona' language model and dictionary
MUSIC -- if True, uses the 'Music' language model and dictionary
"""
+ decoder = self._decoders[mode]
- wavFile = file(audio_file_path, 'rb')
- wavFile.seek(44)
+ fp.seek(44)
- decoder = self._decoders[TranscriptionMode.NORMAL]
- decoder.decode_raw(wavFile)
+ # FIXME: Can't use the Decoder.decode_raw() here, because
+ # pocketsphinx segfaults with tempfile.SpooledTemporaryFile()
+ data = fp.read()
+ decoder.start_utt()
+ decoder.process_raw(data, False, True)
+ decoder.end_utt()
+
result = decoder.get_hyp()
with open(self._logfiles[mode], 'r+') as f:
if mode == TranscriptionMode.KEYWORD:
@@ -197,7 +202,7 @@ class GoogleSTT(AbstractSTTEngine):
config['api_key'] = profile['keys']['GOOGLE_SPEECH']
return config
- def transcribe(self, audio_fp, mode=TranscriptionMode.NORMAL):
+ def transcribe(self, fp, mode=TranscriptionMode.NORMAL):
"""
Performs STT via the Google Speech API, transcribing an audio file and returning an English
string.
@@ -205,11 +210,11 @@ class GoogleSTT(AbstractSTTEngine):
Arguments:
audio_file_path -- the path to the .wav file to be transcribed
"""
+
url = "https://www.google.com/speech-api/v2/recognize?output=json&client=chromium&key=%s&lang=%s&maxresults=6&pfilter=2" % (
self.api_key, "en-us")
- with open(audio_file_path, 'rb') as f:
- data = f.read()
+ data = fp.read()
try:
headers = {'Content-type': 'audio/l16; rate=%s' % GoogleSTT.RATE}