This repository has been archived on 2023-06-09. You can view files and clone it, but cannot push or open issues or pull requests.
jarvis-client/jarvis/main.py

80 lines
2.0 KiB
Python
Raw Normal View History

import os
import struct
2021-08-01 10:49:58 +02:00
import sys
import threading
2021-08-01 18:23:43 +02:00
import time
import pvporcupine
import pyaudio
import simpleaudio as sa
import speech_recognition as sr
2021-08-01 18:23:43 +02:00
from jarvis.utils import server_utils, flask_utils
wake_word_handler = pvporcupine.create(keywords=['jarvis'], sensitivities=[0.65])
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:
threading.Thread(
target=sa.WaveObject.from_wave_file(
os.getcwd() + "/jarvis" + "/sounds/" + "listening.wav").play).start()
record()
def record():
# obtain audio from the microphone
r = sr.Recognizer()
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source=source, duration=0.7)
try:
2021-08-28 16:13:47 +02:00
print("Listening...")
audio = r.listen(source, timeout=2, phrase_time_limit=5)
except sr.WaitTimeoutError:
print("Sentence timeout")
return
threading.Thread(
target=sa.WaveObject.from_wave_file(os.getcwd() + "/jarvis" + "/sounds/" + "listened.wav").play).start()
if audio.frame_data:
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)
def start():
2021-08-01 10:49:58 +02:00
if server_utils.get_server_ip() is None:
sys.exit(1)
2021-08-01 18:23:43 +02:00
if '--no-voice' in sys.argv:
print("[WARN] No voice mode enabled")
threading.Thread(target=no_voice_input).start()
2021-08-01 18:23:43 +02:00
else:
threading.Thread(target=wake_word_listening).start()
flask_utils.start_server()