-
Notifications
You must be signed in to change notification settings - Fork 6
/
voice-api.py
88 lines (71 loc) · 2.71 KB
/
voice-api.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
from random import choice
import os
from starlette.applications import Starlette
from starlette.responses import FileResponse
import uvicorn
from midi2voice.midi2xml import midi2xml
from Voice import renderize_voice, compose
app = Starlette(debug=False)
# Needed to avoid cross-domain issues
response_header = {
'Access-Control-Allow-Origin': '*',
}
@app.route('/voice', methods=['GET','POST'])
async def voice(request):
if request.method == 'GET':
params = request.query_params
elif request.method == 'POST':
params = await request.json()
lyrics = params.get('lyrics', ['oo '])
notes = params.get('notes', [0])
octave = params.get("octave", 6)
dur = params.get('dur', [1])
tempo = params.get('tempo', 120)
lang = params.get('lang', 'es')
scale = params.get('scale', [0,2,4,5,7,9,11])
root_note = params.get('root', 0) # C is the root note
notes = list(map(lambda x: x + root_note,notes))
# hardcodear filename
filename = 'output.wav'
renderize_voice(filename,lyrics,notes,dur,tempo,scale,lang)
return FileResponse(filename, headers=response_header)
@app.route('/midi', methods=['GET','POST'])
async def midi(request):
if request.method == 'GET':
params = request.query_params
elif request.method == 'POST':
params = await request.json()
lyrics = params.get('lyrics', 'oo ')
notes = params.get('notes', [0])
octave = params.get("octave", 6)
dur = params.get('dur', [1])
tempo = params.get('tempo', 120)
lang = params.get('lang', 'es')
scale = params.get('scale', [0,2,4,5,7,9,11])
root_note = params.get('root', 0) # C is the root note
notes = list(map(lambda x: x + root_note,notes))
filename = 'voice.mid'
compose(notes, dur, scale, filename)
return FileResponse(filename, headers=response_header)
@app.route('/sheet', methods=['GET','POST'])
async def sheet(request):
if request.method == 'GET':
params = request.query_params
elif request.method == 'POST':
params = await request.json()
lyrics = params.get('lyrics', 'oo ')
notes = params.get('notes', [0])
octave = params.get("octave", 6)
dur = params.get('dur', [1])
tempo = params.get('tempo', 120)
lang = params.get('lang', 'es')
scale = params.get('scale', [0,2,4,5,7,9,11])
root_note = params.get('root', 0) # C is the root note
notes = list(map(lambda x: x + root_note,notes))
MIDI_PATH = "voice.mid"
filename="voice.xml"
compose(notes, dur, scale, MIDI_PATH)
midi2xml(lyrics, MIDI_PATH, filename, tempo)
return FileResponse(filename, headers=response_header)
if __name__ == '__main__':
uvicorn.run(app, host='0.0.0.0', port=int(os.environ.get('PORT', 8080)))