diff --git a/jarvis/main.py b/jarvis/main.py index e69de29..400a7dc 100644 --- a/jarvis/main.py +++ b/jarvis/main.py @@ -0,0 +1,46 @@ +import struct + +import pvporcupine +import pyaudio +import speech_recognition as sr + +from jarvis.utils import server_utils + +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: + print("Recognized") + record() + + +def record(): + # obtain audio from the microphone + r = sr.Recognizer() + with sr.Microphone() as source: + print("Say something!") + r.adjust_for_ambient_noise(source=source, duration=0.5) + audio = r.listen(source, timeout=2, phrase_time_limit=5) + + server_utils.send_record_to_server(audio.frame_data) + + +if __name__ == '__main__': + wake_word_listening() diff --git a/jarvis/utils/__init__.py b/jarvis/utils/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/jarvis/utils/server_utils.py b/jarvis/utils/server_utils.py new file mode 100644 index 0000000..87a1e66 --- /dev/null +++ b/jarvis/utils/server_utils.py @@ -0,0 +1,17 @@ +import requests +from requests.structures import CaseInsensitiveDict + + +def send_record_to_server(frame_data): + # TODO: use config or even ping to find the server on local network ? + url_service = "http://192.168.1.12:5000" + "/process_audio_request" + + headers = CaseInsensitiveDict() + headers["Content-Type"] = "text/xml; charset=utf8" + # headers["Authorization"] = config_utils.get_in_config("API_KEY") + + response = requests.post(url_service, + headers=headers, + data=frame_data) + + print(response.content) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..f2472fb --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +pvporcupine~=1.9.5 +PyAudio~=0.2.11 +SpeechRecognition~=3.8.1 +requests~=2.26.0 \ No newline at end of file