Spaces:
Sleeping
Sleeping
File size: 6,853 Bytes
62c0892 65c2707 740942d d0c53c1 740942d d0c53c1 740942d 62c0892 65c2707 62c0892 65c2707 62c0892 d4e5f4f 62c0892 d4e5f4f 62c0892 d4e5f4f 62c0892 d4e5f4f 62c0892 d4e5f4f 62c0892 d4e5f4f 62c0892 d4e5f4f 62c0892 d4e5f4f 62c0892 d4e5f4f 62c0892 d4e5f4f 62c0892 d4e5f4f 62c0892 d4e5f4f 62c0892 d4e5f4f 62c0892 d4e5f4f 62c0892 d4e5f4f 62c0892 d4e5f4f 62c0892 d4e5f4f 62c0892 d4e5f4f 62c0892 d4e5f4f 65c2707 62c0892 d0c53c1 62c0892 d4e5f4f 62c0892 d4e5f4f 62c0892 65c2707 d0c53c1 65c2707 62c0892 d4e5f4f 62c0892 d4e5f4f 62c0892 65c2707 62c0892 740942d 62c0892 740942d 62c0892 65c2707 62c0892 d4e5f4f 62c0892 65c2707 d4e5f4f 65c2707 62c0892 | 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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 | 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 = """<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Slowed + Reverb</title>
<style>
@import url('https://fonts.googleapis.com/css2?family=Space+Mono:wght@400;700&display=swap');
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
body {
background: #0a0a0f;
color: #e8e8f0;
font-family: 'Space Mono', monospace;
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 40px 20px;
background-image:
radial-gradient(ellipse 80% 50% at 50% -10%, rgba(124,109,250,0.15) 0%, transparent 60%),
radial-gradient(ellipse 60% 40% at 80% 100%, rgba(250,109,196,0.08) 0%, transparent 60%);
}
h1 {
font-size: clamp(1.6rem, 5vw, 2.4rem);
font-weight: 700;
letter-spacing: -0.02em;
margin-bottom: 8px;
background: linear-gradient(135deg, #7c6dfa, #fa6dc4);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
background-clip: text;
}
.subtitle {
font-size: 0.7rem;
color: #555570;
letter-spacing: 0.14em;
text-transform: uppercase;
margin-bottom: 48px;
}
.card {
background: #13131a;
border: 1px solid #2a2a3a;
border-radius: 16px;
padding: 32px;
width: 100%;
max-width: 480px;
margin-bottom: 16px;
}
.card-label {
font-size: 0.62rem;
letter-spacing: 0.15em;
text-transform: uppercase;
color: #555570;
margin-bottom: 20px;
}
.file-drop {
border: 2px dashed #2a2a3a;
border-radius: 10px;
padding: 28px 20px;
text-align: center;
cursor: pointer;
transition: border-color 0.2s, background 0.2s;
margin-bottom: 16px;
position: relative;
}
.file-drop:hover { border-color: #7c6dfa; background: rgba(124,109,250,0.04); }
.file-drop input[type=file] {
position: absolute; inset: 0; opacity: 0; cursor: pointer; width: 100%; height: 100%;
}
.file-drop .icon { font-size: 2rem; margin-bottom: 8px; }
.file-drop .hint { font-size: 0.72rem; color: #555570; }
.file-drop .filename { font-size: 0.78rem; color: #7c6dfa; margin-top: 6px; min-height: 18px; }
input[type=text] {
width: 100%;
background: #0d0d14;
border: 1px solid #2a2a3a;
border-radius: 8px;
color: #e8e8f0;
font-family: inherit;
font-size: 0.78rem;
padding: 12px 14px;
outline: none;
transition: border-color 0.2s;
margin-bottom: 16px;
}
input[type=text]:focus { border-color: #7c6dfa; }
input[type=text]::placeholder { color: #33334a; }
button[type=submit] {
width: 100%;
background: linear-gradient(135deg, #7c6dfa, #fa6dc4);
border: none;
border-radius: 8px;
color: #fff;
font-family: inherit;
font-size: 0.78rem;
font-weight: 700;
letter-spacing: 0.08em;
padding: 13px;
cursor: pointer;
transition: opacity 0.2s, transform 0.1s;
}
button[type=submit]:hover { opacity: 0.88; }
button[type=submit]:active { transform: scale(0.98); }
.info {
font-size: 0.62rem;
color: #33334a;
text-align: center;
margin-top: 20px;
letter-spacing: 0.08em;
line-height: 2;
}
.info span { color: #7c6dfa; }
</style>
</head>
<body>
<h1>⟁ SLOWED + REVERB</h1>
<p class="subtitle">0.85x speed · 0.85x pitch · 40% reverb</p>
<div class="card">
<div class="card-label">// Upload audio file</div>
<form action="/slowed-reverb" method="post" enctype="multipart/form-data">
<div class="file-drop">
<input type="file" name="file" accept="audio/*"
onchange="this.parentElement.querySelector('.filename').textContent=this.files[0]?.name||''">
<div class="icon">🎵</div>
<div class="hint">mp3 · wav · flac · ogg · m4a</div>
<div class="filename"></div>
</div>
<button type="submit">Process File →</button>
</form>
</div>
<div class="card">
<div class="card-label">// Or paste direct audio URL</div>
<form action="/slowed-reverb-url" method="post">
<input type="text" name="url" placeholder="https://example.com/track.mp3">
<button type="submit">Process URL →</button>
</form>
</div>
<p class="info">output: <span>AAC 256kbps M4A</span></p>
</body>
</html>"""
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") |