audio_helper.api module
Audio Helper — FastAPI HTTP surface.
Exposes every public function in audio_helper.main as an HTTP
endpoint so audio-helper can be dropped behind any reverse proxy and
consumed by other services. Kept intentionally minimal:
Multipart uploads for audio inputs (
UploadFile), streamed straight to a temporary file so large clips don’t blow up memory.FileResponsefor single-file outputs (/convert,/chunk…).ZIP-encoded
StreamingResponsefor multi-file outputs (/split,/separate) so the client gets one download per call.BackgroundTaskscleans temp files after the response has been streamed — no leftover garbage on disk after a request.
Install the extra to get the runtime dependencies:
pip install 'audio-helper[api]'
Then run the app with any ASGI server:
uvicorn audio_helper.api:app --host 0.0.0.0 --port 8000
A minimal single-page GUI (“audition bench”) is served at GET /gui
(and GET / redirects there): drop an audio file, pick an operation,
run it against these same endpoints, then A/B the input vs output in
<audio> players and download the result.
Usage Example
>>> # Start the server:
>>> # uvicorn audio_helper.api:app --reload
>>> # Convert a file:
>>> # curl -F 'file=@in.mp3' -F 'freq=22050' -F 'channels=1' \
>>> # -o out.wav http://localhost:8000/convert
>>> # Get a file's duration (JSON):
>>> # curl -F 'file=@in.mp3' http://localhost:8000/duration
>>> # Full OpenAPI docs at http://localhost:8000/docs
- audio_helper.api.chunk(background, file=fastapi.File, start=fastapi.Form, end=fastapi.Form, output_format=fastapi.Form)
Extract a
[start, end]slice from the uploaded audio.
- audio_helper.api.concat(background, files=fastapi.File, output_format=fastapi.Form)
Concatenate multiple uploaded audio files head-to-tail.
- audio_helper.api.convert(background, file=fastapi.File, output_format=fastapi.Form, freq=fastapi.Form, channels=fastapi.Form, encoding=fastapi.Form)
Re-encode an uploaded file at the requested sample rate / channels / codec.
- audio_helper.api.duration(file=fastapi.File, background=None)
Return the duration in seconds of the uploaded audio file.
- Parameters:
file (fastapi.UploadFile)
background (fastapi.BackgroundTasks)
- Return type:
fastapi.responses.JSONResponse
- audio_helper.api.gui()
Serve the self-contained single-page ‘audition bench’ GUI.
The page (defined in
audio_helper.gui) is a build-step-free HTML + Tailwind-CDN + vanilla-JS client that calls the very same/convert//chunk/ … endpoints defined below. It adds no server-side logic — it is purely a friendlier front door to the API.- Returns:
The complete HTML document (status 200,
text/html).- Return type:
HTMLResponse
- audio_helper.api.health()
Simple liveness probe — no dependency check, just proves the app is up.
- Return type:
- audio_helper.api.resemblance(a=fastapi.File, b=fastapi.File)
MFCC-based similarity score in
[0, 1]between two uploaded audio files.- Parameters:
a (fastapi.UploadFile)
b (fastapi.UploadFile)
- Return type:
fastapi.responses.JSONResponse
- audio_helper.api.roomtone(background, file=fastapi.File, db=fastapi.Form, color=fastapi.Form, sample_rate=fastapi.Form, output_format=fastapi.Form)
Mix low-level colored ambient noise on top of an uploaded speech track.
- audio_helper.api.root()
Redirect the bare root to the GUI so opening the server just works.
- Return type:
fastapi.responses.RedirectResponse
- audio_helper.api.separate(background, file=fastapi.File, device=fastapi.Form, workers=fastapi.Form, output_format=fastapi.Form)
Run Demucs source separation; response is a ZIP with the 4 stems.
- audio_helper.api.silence(background, duration_seconds=fastapi.Form, sample_rate=fastapi.Form, output_format=fastapi.Form)
Generate a silent audio file of a given duration.
- audio_helper.api.split(background, file=fastapi.File, seconds=fastapi.Form, output_format=fastapi.Form, suffix=fastapi.Form)
Split the uploaded audio into fixed-duration chunks; response is a ZIP.