summaryrefslogtreecommitdiff
path: root/client/mic.py
diff options
context:
space:
mode:
authorschneefux <schneefux+commit@schneefux.xyz>2014-10-03 16:30:55 -0400
committerschneefux <schneefux+commit@schneefux.xyz>2014-10-03 16:30:55 -0400
commit4c22cd55978cd6d3b2abe51a12447f75992d9f1f (patch)
tree57d64608aa8e60aa58ad8c9730e37b8b113237a6 /client/mic.py
parent99373bc4772575f6d3a160a5bce852aa08226bfc (diff)
parent9cb951f6d7b3b38b39a55c8e8e03f78989e2c122 (diff)
downloadjasper-client-4c22cd55978cd6d3b2abe51a12447f75992d9f1f.tar.gz
jasper-client-4c22cd55978cd6d3b2abe51a12447f75992d9f1f.zip
Merge pull request #208 from Holzhaus/pyaudio-init-only-once
Initialize PyAudio in Mic.__init__
Diffstat (limited to 'client/mic.py')
-rw-r--r--client/mic.py21
1 files changed, 9 insertions, 12 deletions
diff --git a/client/mic.py b/client/mic.py
index 7956b1d..af5919c 100644
--- a/client/mic.py
+++ b/client/mic.py
@@ -29,6 +29,10 @@ class Mic:
self.speaker = speaker
self.passive_stt_engine = passive_stt_engine
self.active_stt_engine = active_stt_engine
+ self._audio = pyaudio.PyAudio()
+
+ def __del__(self):
+ self._audio.terminate()
def getScore(self, data):
rms = audioop.rms(data, 2)
@@ -46,8 +50,7 @@ class Mic:
THRESHOLD_TIME = 1
# prepare recording stream
- audio = pyaudio.PyAudio()
- stream = audio.open(format=pyaudio.paInt16,
+ stream = self._audio.open(format=pyaudio.paInt16,
channels=1,
rate=RATE,
input=True,
@@ -72,7 +75,6 @@ class Mic:
stream.stop_stream()
stream.close()
- audio.terminate()
# this will be the benchmark to cause a disturbance over!
THRESHOLD = average * THRESHOLD_MULTIPLIER
@@ -96,8 +98,7 @@ class Mic:
LISTEN_TIME = 10
# prepare recording stream
- audio = pyaudio.PyAudio()
- stream = audio.open(format=pyaudio.paInt16,
+ stream = self._audio.open(format=pyaudio.paInt16,
channels=1,
rate=RATE,
input=True,
@@ -145,7 +146,6 @@ class Mic:
print "No disturbance detected"
stream.stop_stream()
stream.close()
- audio.terminate()
return (None, None)
# cutoff any recording before this disturbance was detected
@@ -161,12 +161,11 @@ class Mic:
# save the audio data
stream.stop_stream()
stream.close()
- audio.terminate()
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.setsampwidth(pyaudio.get_sample_size(pyaudio.paInt16))
wav_fp.setframerate(RATE)
wav_fp.writeframes(''.join(frames))
wav_fp.close()
@@ -208,8 +207,7 @@ class Mic:
self.speaker.play(jasperpath.data('audio', 'beep_hi.wav'))
# prepare recording stream
- audio = pyaudio.PyAudio()
- stream = audio.open(format=pyaudio.paInt16,
+ stream = self._audio.open(format=pyaudio.paInt16,
channels=1,
rate=RATE,
input=True,
@@ -239,12 +237,11 @@ class Mic:
# save the audio data
stream.stop_stream()
stream.close()
- audio.terminate()
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.setsampwidth(pyaudio.get_sample_size(pyaudio.paInt16))
wav_fp.setframerate(RATE)
wav_fp.writeframes(''.join(frames))
wav_fp.close()