-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
100 lines (83 loc) · 2.74 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import json
import os
import warnings
from datetime import datetime
from http.client import HTTPException
from io import BytesIO
import os.path
from scipy.signal import resample
from scipy.io.wavfile import read
import numpy as np
from flask import Flask, send_from_directory, send_file, jsonify, Response
from flask import request, redirect
from flask_cors import CORS, cross_origin
from gevent.pywsgi import WSGIServer
from pydub import AudioSegment
from decode.demodulator import demodulator
from encode.modulator import get_data
static_dir='./website'
static_dir = os.path.abspath(static_dir)
app = Flask(__name__,static_folder=static_dir)
CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'
app.debug = True
warnings.filterwarnings("ignore")
class ServerError(Exception):
status_code = 400
def __init__(self, message, status_code=None, payload=None):
Exception.__init__(self)
self.message = message
if status_code is not None:
self.status_code = status_code
self.payload = payload
def to_dict(self):
error_dict = dict(self.payload or ())
error_dict['message'] = self.message
return error_dict
from scipy.io.wavfile import write
def write_wav(blob):
buf = BytesIO()
opus_data = BytesIO(blob)
audio = AudioSegment.from_file(opus_data)
duration = audio.duration_seconds
audio = audio.set_frame_rate(10000)
return audio.export(buf, format='wav')
@app.route('/')
def index() -> Response: # pylint: disable=unused-variable
return send_file(os.path.join(static_dir, 'home.html'))
@app.route('/<path:path>')
def static_proxy(path: str) -> Response:
if static_dir is not None:
return send_from_directory(static_dir, path)
else:
return send_file(os.path.join(static_dir, 'index.html'))
@app.route('/receive', methods=["GET"])
def receive():
return send_file(os.path.join(static_dir, 'index.html'))
@app.route('/asr', methods=['POST'])
@cross_origin()
def demodulate():
if request.method == 'POST':
wav = write_wav(request.data)
sr,data = read(wav)
message = demodulator(data)
res = {'result': message}
return json.dumps(res, ensure_ascii=False)
return "ASR"
@app.route('/encode/<path:message>', methods=["GET"])
def encode(message):
blob = get_data(message)
return blob
@app.route('/transmit', methods=["GET"])
def transmit():
return send_file(os.path.join(static_dir, 'send.html'))
'''
@app.errorhandler(Exception)
def handle_error(e):
code = 500
if isinstance(e, HTTPException):
code = e.code
return jsonify(error=str(e)), code
'''
if __name__ == "__main__":
app.run()