Skip to content

Commit

Permalink
Add pocketsphinx to be used as wake word detector
Browse files Browse the repository at this point in the history
  • Loading branch information
celian-garcia committed Jan 8, 2021
1 parent 4a9a3f1 commit 826094f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
37 changes: 25 additions & 12 deletions milobella.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from pydub import AudioSegment
from pydub.playback import play
import io
from pocketsphinx import LiveSpeech

# Audio recording parameters
RATE = 16000
Expand All @@ -33,10 +34,11 @@ def __init__(self, rate, chunk):
self._buff = queue.Queue()
self.closed = True

def __enter__(self):
self._audio_interface = pyaudio.PyAudio()
for i in range(self._audio_interface.get_device_count()):
print(self._audio_interface.get_device_info_by_index(i))

def __enter__(self):
self._audio_stream = self._audio_interface.open(
format=pyaudio.paInt16,
# The API currently only supports 1-channel (mono) audio
Expand All @@ -57,16 +59,18 @@ def __exit__(self, type, value, traceback):
self._audio_stream.stop_stream()
self._audio_stream.close()
self.closed = True
# Signal the generator to terminate so that the client's
# streaming_recognize method will not block the process termination.
self._buff.put(None)
self._audio_interface.terminate()

def _fill_buffer(self, in_data, frame_count, time_info, status_flags):
"""Continuously collect data from the audio stream, into the buffer."""
self._buff.put(in_data)
return None, pyaudio.paContinue

def terminate(self):
# Signal the generator to terminate so that the client's
# streaming_recognize method will not block the process termination.
self._buff.put(None)
self._audio_interface.terminate()

def generator(self):
while not self.closed:
# Use a blocking get() to ensure there's at least one chunk of
Expand Down Expand Up @@ -143,7 +147,6 @@ def listen_print_loop(responses):
# one of our keywords.
if re.search(r'\b(stop)\b', transcript, re.I):
print('Exiting..')
break
else:
milobella_response = requests_pkg.post(
'https://milobella.com:10443/talk/text',
Expand Down Expand Up @@ -181,6 +184,7 @@ def listen_print_loop(responses):
play(song)

num_chars_printed = 0
break


def main():
Expand All @@ -197,15 +201,24 @@ def main():
config=config,
interim_results=True)

with MicrophoneStream(RATE, CHUNK) as stream:
audio_generator = stream.generator()
requests = (types.StreamingRecognizeRequest(audio_content=content)
sphinx_speech = LiveSpeech(lm=False, keyphrase='bella', kws_threshold=1e-20)
stream = MicrophoneStream(RATE, CHUNK)
for phrase in sphinx_speech:
with stream:
print("oui ?")
audio_generator = stream.generator()
print("Start listening...")
requests = (types.StreamingRecognizeRequest(audio_content=content)
for content in audio_generator)

responses = client.streaming_recognize(streaming_config, requests)

# Now, put the transcription responses to use.
listen_print_loop(responses)
responses = client.streaming_recognize(streaming_config, requests)

# Now, put the transcription responses to use.
listen_print_loop(responses)
print("Stopped listening.")
stream.terminate()



if __name__ == '__main__':
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ pyaudio~=0.2.11
google.cloud.speech
google.cloud.texttospeech
pydub
pocketsphinx

0 comments on commit 826094f

Please sign in to comment.