diff --git a/jarvis/config/config.json b/jarvis/config/config.json index 6eba7de..cd37757 100644 --- a/jarvis/config/config.json +++ b/jarvis/config/config.json @@ -1,3 +1,4 @@ { + "PORT": 5001, "SERVER_PORT": 5000 } diff --git a/jarvis/main.py b/jarvis/main.py index 1933d2d..e950e94 100644 --- a/jarvis/main.py +++ b/jarvis/main.py @@ -7,7 +7,7 @@ import pyaudio import simpleaudio as sa import speech_recognition as sr -from jarvis.utils import server_utils, config_utils +from jarvis.utils import server_utils, config_utils, flask_utils wake_word_handler = pvporcupine.create(keywords=['jarvis']) @@ -47,9 +47,10 @@ def record(): if __name__ == '__main__': - if config_utils.get_in_config('SERVER_IP') is None: print("No server IP specified in config, looking trough the entire network... (might take a few seconds)") server_utils.find_server_on_network() - wake_word_listening() + thread = threading.Thread(target=wake_word_listening).start() + + flask_utils.start_server() diff --git a/jarvis/utils/flask_utils.py b/jarvis/utils/flask_utils.py new file mode 100644 index 0000000..d17fba2 --- /dev/null +++ b/jarvis/utils/flask_utils.py @@ -0,0 +1,37 @@ +import json + +import simpleaudio as sa +from flask import request, jsonify, Flask + +from jarvis.utils import config_utils + +app = Flask(__name__) + + +@app.route("/play_raw_audio", methods=['POST']) +def play_raw_audio(): + data = get_data_in_request(request) + play_obj = sa.play_buffer(audio_data=data, num_channels=2, bytes_per_sample=2, sample_rate=44100) + + play_obj.wait_done() + return jsonify("OK") + + +def get_data_in_request(flask_request): + data_str = str(flask_request.data.decode('utf8')).replace('"', '\"').replace("\'", "'") + + # if no data return an empty json to avoid error with json.loads below + if not data_str: + return {} + + data_json = json.loads(data_str) + + if not isinstance(data_json, dict): + data_json = json.loads(data_json) + + return data_json + + +def start_server(): + app.config['JSON_AS_ASCII'] = False + app.run(port=config_utils.get_in_config("PORT"), debug=False, host='0.0.0.0', threaded=True) diff --git a/requirements.txt b/requirements.txt index 43899de..7323b0e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,4 +2,5 @@ pvporcupine~=1.9.5 PyAudio~=0.2.11 SpeechRecognition~=3.8.1 requests~=2.26.0 -simpleaudio~=1.0.4 \ No newline at end of file +simpleaudio~=1.0.4 +Flask~=2.0.1 \ No newline at end of file