from fastapi import FastAPI, UploadFile, File, Form, HTTPException, BackgroundTasks from fastapi.responses import HTMLResponse, FileResponse import subprocess, uuid, os, shutil, requests app = FastAPI() TMP = "/tmp/audio" os.makedirs(TMP, exist_ok=True) # 0.85 speed + 0.85 pitch via asetrate, then 40% reverb mix via aecho # aecho: in=1.0, out=1.0, delay=60ms (short room tail), decay=0.4 (40% wet) FILTER = ( "asetrate=44100*0.85," # slow to 85% + lower pitch to 85% "aresample=44100," # restore clock rate for players "aecho=1.0:1.0:60:0.4" # 40% reverb — 60ms tail, 0.4 decay = 40% wet ) HTML = """ Slowed + Reverb

⟁ SLOWED + REVERB

0.85x speed · 0.85x pitch · 40% reverb

// Upload audio file
🎵
mp3 · wav · flac · ogg · m4a
// Or paste direct audio URL

output: AAC 256kbps M4A

""" def run_ffmpeg(inp: str, out: str): cmd = [ "ffmpeg", "-y", "-i", inp, "-af", FILTER, "-c:a", "aac", "-b:a", "256k", "-ar", "44100", "-movflags", "+faststart", out ] result = subprocess.run(cmd, capture_output=True, text=True) if result.returncode != 0: raise HTTPException(status_code=500, detail=f"ffmpeg error:\n{result.stderr[-3000:]}") def cleanup(*paths: str): for p in paths: try: os.remove(p) except FileNotFoundError: pass @app.get("/", response_class=HTMLResponse) def home(): return HTML @app.post("/slowed-reverb") async def slowed_reverb(background_tasks: BackgroundTasks, file: UploadFile = File(...)): ext = os.path.splitext(file.filename)[1] or ".mp3" uid = uuid.uuid4() inp = os.path.join(TMP, f"{uid}_in{ext}") out = os.path.join(TMP, f"{uid}_out.m4a") with open(inp, "wb") as f: shutil.copyfileobj(file.file, f) run_ffmpeg(inp, out) background_tasks.add_task(cleanup, inp, out) return FileResponse(out, media_type="audio/mp4", filename="slowed_reverb.m4a") @app.post("/slowed-reverb-url") def slowed_reverb_url(background_tasks: BackgroundTasks, url: str = Form(...)): ext = os.path.splitext(url.split("?")[0])[1] or ".mp3" uid = uuid.uuid4() inp = os.path.join(TMP, f"{uid}_in{ext}") out = os.path.join(TMP, f"{uid}_out.m4a") try: r = requests.get(url, stream=True, timeout=30) r.raise_for_status() except requests.RequestException as e: raise HTTPException(status_code=400, detail=f"Failed to fetch URL: {e}") with open(inp, "wb") as f: for chunk in r.iter_content(chunk_size=65536): f.write(chunk) run_ffmpeg(inp, out) background_tasks.add_task(cleanup, inp, out) return FileResponse(out, media_type="audio/mp4", filename="slowed_reverb.m4a")