MockingBird/web/api/audio.py
Vega 0d0b55d3e9
Web server release v2 (#99)
* Init App

* init server.py (#93)

* init server.py

* Update requirements.txt

Add requirement

Co-authored-by: auau <auau@test.com>
Co-authored-by: babysor00 <babysor00@gmail.com>

* Run web.py!

Run web.py!

* Restruct readme and add instruction to use web server

* fix training preprocess of vocoder

* Init App

* init server.py (#93)

* init server.py

* Update requirements.txt

Add requirement

Co-authored-by: auau <auau@test.com>
Co-authored-by: babysor00 <babysor00@gmail.com>

* Run web.py!

Run web.py!

* fix training preprocess of vocoder

* Refactor to restful style

Co-authored-by: balala <Ozgay@users.noreply.github.com>
Co-authored-by: auau <auau@test.com>
2021-09-25 17:07:46 +08:00

43 lines
1.4 KiB
Python

import os
from pathlib import Path
from flask_restx import Namespace, Resource, fields
from flask import Response, current_app
api = Namespace('audios', description='Audios related operations')
audio = api.model('Audio', {
'name': fields.String(required=True, description='The audio name'),
})
def generate(wav_path):
with open(wav_path, "rb") as fwav:
data = fwav.read(1024)
while data:
yield data
data = fwav.read(1024)
@api.route('/')
class AudioList(Resource):
@api.doc('list_audios')
@api.marshal_list_with(audio)
def get(self):
'''List all audios'''
audio_samples = []
AUDIO_SAMPLES_DIR = current_app.config.get("AUDIO_SAMPLES_DIR")
if os.path.isdir(AUDIO_SAMPLES_DIR):
audio_samples = list(Path(AUDIO_SAMPLES_DIR).glob("*.wav"))
return list(a.name for a in audio_samples)
@api.route('/<name>')
@api.param('name', 'The name of audio')
@api.response(404, 'audio not found')
class Audio(Resource):
@api.doc('get_audio')
@api.marshal_with(audio)
def get(self, name):
'''Fetch a cat given its identifier'''
AUDIO_SAMPLES_DIR = current_app.config.get("AUDIO_SAMPLES_DIR")
if Path(AUDIO_SAMPLES_DIR + name).exists():
return Response(generate(AUDIO_SAMPLES_DIR + name), mimetype="audio/x-wav")
api.abort(404)