-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
149 lines (112 loc) · 3.57 KB
/
server.py
File metadata and controls
149 lines (112 loc) · 3.57 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
"""
Diapason RESTful API.
"""
import inspect
import configparser
from io import BytesIO
from flask import Flask
from flask import abort
from flask import request
from flask import jsonify
from flask import send_file
import numpy
from numpy import linspace,sin,pi,int16
from scipy.io import wavfile
from diapason import note_frequency
from diapason import generate_wav
from diapason.dub import convert_wav
app = Flask('Diapason')
def list_routes(app, starting=''):
output = []
for rule in app.url_map.iter_rules():
if rule.endpoint.startswith('_'):
continue
if not str(rule).startswith(starting):
continue
output.append(dict(
name=rule.endpoint,
rule=rule.rule,
methods=','.join(rule.methods),
doc=inspect.getdoc(app.view_functions[rule.endpoint])
))
return output
def error_information(error):
info = {}
info['code'] = error.code
info['name'] = error.name
if error.response:
info['response'] = error.response
if error.description:
info['description'] = error.description
return jsonify(error=info), error.code
@app.errorhandler(400)
def handle_400(error):
return error_information(error)
@app.errorhandler(404)
def handle_404(error):
return error_information(error)
@app.route('/')
def root():
versions = dict(v0=request.url+'v0')
return jsonify(title='Diapason RESTful API', versions=versions)
@app.route('/v0')
def routes():
return jsonify(routes=list_routes(app, '/v0'))
@app.route('/v0/reverse/<string:query>')
def reverse(query):
"""
Return the reversed query string provided (for testing purposes).
"""
return jsonify(reverse=query[::-1])
@app.route('/v0/<note>')
def get(note):
"""
Get a note.
"""
coding_format = request.args.get('format', 'wav')
rate = int(request.args.get('rate', 44100))
duration = float(request.args.get('duration', 2))
octave = int(request.args.get('octave', 4))
sharp = int(request.args.get('sharp', 0))
flat = int(request.args.get('flat', 0))
mimetype = 'audio/' + coding_format
if '.' in note:
note = note.split('.')[0]
note = note.upper()
frequency = note_frequency(note, sharp=sharp, flat=flat, octave=octave)
note = generate_wav(frequency, duration, rate)
if coding_format != 'wav':
note = convert_wav(note, coding_format=coding_format,
**request.args.to_dict())
return send_file(note, mimetype=mimetype,
# For developing purposes only
add_etags=False, cache_timeout=0)
@app.route('/v0/alexa/<note>')
def alexa(note):
"""
Get a note in a convenient format to deal with Alexa.
"""
coding_format = 'mpeg'
rate = 16000
duration = 5.
octave = 4
sharp = 0
flat = 0
extra = {'bitrate': '48k'}
mimetype = 'audio/' + coding_format
if '.' in note:
note = note.split('.')[0]
note = note.upper()
frequency = note_frequency(note, sharp=sharp, flat=flat, octave=octave)
note = generate_wav(frequency, duration, rate)
note = convert_wav(note, coding_format=coding_format, **extra)
return send_file(note, mimetype=mimetype)
@app.route('/<path:path>', methods=['GET', 'POST'])
def _catch_all(path):
abort(404, 'Requested API call does not exist')
if __name__ == '__main__':
config = configparser.ConfigParser()
config.read('server.ini')
app.run(host=config['server']['host'],
port=int(config['server']['port']),
threaded=True, debug=True)