Added flask server to the client and /play_raw_audio request

This commit is contained in:
Mathieu B 2021-08-01 10:40:38 +02:00
parent 40746b6bbf
commit f6bb0d91e3
4 changed files with 44 additions and 4 deletions

View File

@ -1,3 +1,4 @@
{
"PORT": 5001,
"SERVER_PORT": 5000
}

View File

@ -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()

View File

@ -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)

View File

@ -3,3 +3,4 @@ PyAudio~=0.2.11
SpeechRecognition~=3.8.1
requests~=2.26.0
simpleaudio~=1.0.4
Flask~=2.0.1