Added flask server to the client and /play_raw_audio request
This commit is contained in:
parent
40746b6bbf
commit
f6bb0d91e3
@ -1,3 +1,4 @@
|
||||
{
|
||||
"PORT": 5001,
|
||||
"SERVER_PORT": 5000
|
||||
}
|
||||
|
@ -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()
|
||||
|
37
jarvis/utils/flask_utils.py
Normal file
37
jarvis/utils/flask_utils.py
Normal 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)
|
@ -3,3 +3,4 @@ PyAudio~=0.2.11
|
||||
SpeechRecognition~=3.8.1
|
||||
requests~=2.26.0
|
||||
simpleaudio~=1.0.4
|
||||
Flask~=2.0.1
|
Reference in New Issue
Block a user