mirror of
https://github.com/simon987/toolbox.git
synced 2025-12-14 07:49:02 +00:00
spectrograph
This commit is contained in:
81
api/app.py
81
api/app.py
@@ -10,6 +10,7 @@ import uvicorn
|
||||
import redis
|
||||
from subprocess import Popen, PIPE
|
||||
|
||||
from fastapi.params import Form
|
||||
from starlette.responses import Response
|
||||
|
||||
app = FastAPI()
|
||||
@@ -21,15 +22,79 @@ def read_root():
|
||||
return {"Hello": "World"}
|
||||
|
||||
|
||||
HOUR = 3600
|
||||
DAY = HOUR * 24
|
||||
FLAMEGRAPH_TTL = DAY * 7
|
||||
TTL = 60 * 60
|
||||
|
||||
@app.get("/flame_graph/{key}")
|
||||
def flame_graph_get(key: str):
|
||||
|
||||
data = rdb.get("toolbox:FlameGraph:" + key)
|
||||
return Response(content=data, media_type="image/svg+xml")
|
||||
@app.get("/data/{key}")
|
||||
def flame_graph_get(key: str, type: str):
|
||||
data = rdb.get("toolbox:data:" + key)
|
||||
return Response(content=data, media_type=type)
|
||||
|
||||
|
||||
class SpectrographRequest:
|
||||
x: int
|
||||
y: int
|
||||
z: int
|
||||
label: str
|
||||
window: str
|
||||
|
||||
def __init__(self, x, y, z, label, window, type):
|
||||
self.x = x
|
||||
self.y = y
|
||||
self.z = z
|
||||
self.label = label
|
||||
self.window = window
|
||||
self.type = type
|
||||
|
||||
def valid(self):
|
||||
if self.x < 100 or self.x > 200000:
|
||||
return False
|
||||
|
||||
if self.y < 100 or self.y > 10000:
|
||||
return False
|
||||
|
||||
if self.z < 20 or self.z > 180:
|
||||
return False
|
||||
|
||||
if self.window not in ("Hann", "Hamming", "Bartlett", "Rectangular", "Kaiser", "Dolph"):
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
@app.post("/spectrograph")
|
||||
def spectrograph(file: bytes = File(...), x: int = Form(...), y: int = Form(...), z: int = Form(...), label=Form(...),
|
||||
window=Form(...), type=Form(...)):
|
||||
key = str(uuid.uuid4())
|
||||
|
||||
req = SpectrographRequest(x, y, z, label, window, type)
|
||||
if not req.valid():
|
||||
return {
|
||||
"err": "Invalid request"
|
||||
}
|
||||
|
||||
p = Popen(
|
||||
["sox",
|
||||
"-t", type,
|
||||
"-",
|
||||
"-n", "remix", "1", "spectrogram",
|
||||
"-t", label,
|
||||
"-x", str(x), "-y", str(y), "-z", str(z),
|
||||
"-w", window,
|
||||
"-o", "-"],
|
||||
cwd="./FlameGraph",
|
||||
stdin=PIPE, stdout=PIPE, stderr=PIPE,
|
||||
)
|
||||
|
||||
p.stdin.write(file)
|
||||
p.stdin.close()
|
||||
|
||||
out = p.stdout.read()
|
||||
rdb.set("toolbox:data:" + key, out, ex=TTL)
|
||||
|
||||
return {
|
||||
"key": key
|
||||
}
|
||||
|
||||
|
||||
@app.post("/flame_graph")
|
||||
def flame_graph(file: bytes = File(...), width: int = 1200):
|
||||
@@ -64,7 +129,7 @@ def flame_graph(file: bytes = File(...), width: int = 1200):
|
||||
p3.stdin.close()
|
||||
|
||||
out = p3.stdout.read()
|
||||
rdb.set("toolbox:FlameGraph:" + key, out, ex=FLAMEGRAPH_TTL)
|
||||
rdb.set("toolbox:data:" + key, out, ex=TTL)
|
||||
|
||||
return {
|
||||
"key": key,
|
||||
|
||||
Reference in New Issue
Block a user