2021-07-31 20:31:19 +02:00
|
|
|
import os
|
2021-07-31 12:59:26 +02:00
|
|
|
import struct
|
2021-08-01 10:49:58 +02:00
|
|
|
import sys
|
2021-07-31 20:31:19 +02:00
|
|
|
import threading
|
2021-08-01 18:23:43 +02:00
|
|
|
import time
|
2021-07-31 12:59:26 +02:00
|
|
|
|
|
|
|
import pvporcupine
|
|
|
|
import pyaudio
|
2021-07-31 20:31:19 +02:00
|
|
|
import simpleaudio as sa
|
2021-07-31 12:59:26 +02:00
|
|
|
import speech_recognition as sr
|
|
|
|
|
2021-08-01 18:23:43 +02:00
|
|
|
from jarvis.utils import server_utils, flask_utils
|
2021-07-31 12:59:26 +02:00
|
|
|
|
|
|
|
wake_word_handler = pvporcupine.create(keywords=['jarvis'])
|
|
|
|
|
|
|
|
|
|
|
|
def wake_word_listening():
|
|
|
|
pa = pyaudio.PyAudio()
|
|
|
|
|
|
|
|
audio_stream = pa.open(
|
|
|
|
rate=wake_word_handler.sample_rate,
|
|
|
|
channels=1,
|
|
|
|
format=pyaudio.paInt16,
|
|
|
|
input=True,
|
|
|
|
frames_per_buffer=wake_word_handler.frame_length
|
|
|
|
)
|
|
|
|
|
|
|
|
while True:
|
|
|
|
pcm = audio_stream.read(wake_word_handler.frame_length)
|
|
|
|
pcm = struct.unpack_from("h" * wake_word_handler.frame_length, pcm)
|
|
|
|
|
|
|
|
keyword_index = wake_word_handler.process(pcm)
|
|
|
|
|
|
|
|
if keyword_index >= 0:
|
2021-07-31 20:31:19 +02:00
|
|
|
threading.Thread(
|
|
|
|
target=sa.WaveObject.from_wave_file(os.getcwd() + "/sounds/" + "listening.wav").play).start()
|
2021-07-31 12:59:26 +02:00
|
|
|
record()
|
|
|
|
|
|
|
|
|
|
|
|
def record():
|
|
|
|
# obtain audio from the microphone
|
|
|
|
r = sr.Recognizer()
|
|
|
|
with sr.Microphone() as source:
|
2021-07-31 20:31:19 +02:00
|
|
|
r.adjust_for_ambient_noise(source=source, duration=0.7)
|
2021-07-31 12:59:26 +02:00
|
|
|
audio = r.listen(source, timeout=2, phrase_time_limit=5)
|
|
|
|
|
2021-07-31 20:31:19 +02:00
|
|
|
threading.Thread(target=sa.WaveObject.from_wave_file(os.getcwd() + "/sounds/" + "listened.wav").play).start()
|
2021-07-31 12:59:26 +02:00
|
|
|
server_utils.send_record_to_server(audio.frame_data)
|
|
|
|
|
|
|
|
|
2021-08-01 18:23:43 +02:00
|
|
|
def no_voice_input():
|
|
|
|
time.sleep(1)
|
|
|
|
while True:
|
|
|
|
sentence = input("Write something : ")
|
|
|
|
server_utils.send_sentence_to_server(sentence)
|
|
|
|
|
|
|
|
|
2021-07-31 12:59:26 +02:00
|
|
|
if __name__ == '__main__':
|
2021-08-01 10:49:58 +02:00
|
|
|
if server_utils.get_server_ip() is None:
|
|
|
|
sys.exit(1)
|
2021-07-31 19:34:24 +02:00
|
|
|
|
2021-08-01 18:23:43 +02:00
|
|
|
if '--no-voice' in sys.argv:
|
|
|
|
print("[WARN] No voice mode enabled")
|
|
|
|
thread = threading.Thread(target=no_voice_input).start()
|
|
|
|
else:
|
|
|
|
thread = threading.Thread(target=wake_word_listening).start()
|
2021-08-01 10:40:38 +02:00
|
|
|
|
|
|
|
flask_utils.start_server()
|