A Rust/Python implementation of the Web Audio API, for use in non-browser contexts
Project description
Python bindings for web-audio-api-rs
A Rust/Python implementation of the Web Audio API, for use in non-browser contexts.
Usage
Install from PyPI:
pip install web-audio-api
Create a simple audio context and start an oscillator:
import web_audio_api
ctx = web_audio_api.AudioContext()
osc = ctx.createOscillator()
osc.frequency.value = 300
osc.connect(ctx.destination)
osc.start()
Examples
Runnable example scripts live in examples:
- examples/osc_gain.py: simple oscillator through a gain node
- examples/worklet_white_noise.py:
AudioWorkletwhite noise with message-based volume changes - examples/analyser_meter.py: analyser-based terminal level meter
- examples/buffer_source.py: build and play a short buffer
- examples/mic_input.py: microphone input into a graph
- examples/recorder.py: record graph output to a WAV file
Run an example after maturin develop:
.venv/bin/python examples/osc_gain.py
Advanced usage
The binding now exposes asyncio-native awaitables for the Web Audio methods that are async in
web-audio-api-rs.
This includes:
AudioContext.resume()AudioContext.suspend()AudioContext.close()OfflineAudioContext.startRendering()OfflineAudioContext.resume()OfflineAudioContext.suspend(suspendTime)BaseAudioContext.decodeAudioData(...)
Use them inside a running event loop:
import asyncio
import web_audio_api
async def main():
ctx = web_audio_api.AudioContext({"sinkId": "none"})
await ctx.resume()
await ctx.suspend()
await ctx.close()
asyncio.run(main())
Offline rendering is also async:
import asyncio
import web_audio_api
async def main():
ctx = web_audio_api.OfflineAudioContext(1, 2_000, 2_000.0)
src = ctx.createConstantSource()
src.offset.value = 0.25
src.connect(ctx.destination)
src.start(0.25)
src.stop(0.75)
rendered = await ctx.startRendering()
data = rendered.getChannelData(0)
print(data[:8])
asyncio.run(main())
decodeAudioData(...) returns an awaitable and also accepts optional callbacks:
import asyncio
import pathlib
import web_audio_api
async def main():
ctx = web_audio_api.OfflineAudioContext(1, 128, 44_100.0)
audio_bytes = pathlib.Path("example.wav").read_bytes()
def success(buffer):
print(buffer.length, buffer.sampleRate)
buffer = await ctx.decodeAudioData(audio_bytes, successCallback=success)
print(buffer.numberOfChannels)
asyncio.run(main())
One practical detail: create these awaitables inside the running loop. In other words, prefer
asyncio.run(main()) with the Web Audio calls inside main(), instead of constructing an
awaitable earlier and awaiting it later.
Local development
Create and activate a virtual environment:
uv venv --python 3.11 .venv
source .venv/bin/activate
Install the development build into the active environment:
.venv/bin/python -m pip install maturin
maturin develop
Try the binding:
import web_audio_api
ctx = web_audio_api.AudioContext()
osc = ctx.createOscillator()
osc.connect(ctx.destination)
osc.start()
osc.frequency.value = 300
The scripts in examples are a good next step after this quick smoke test.
Build
Build a wheel:
.venv/bin/python -m pip wheel . --no-deps --wheel-dir dist
Test
Run the Rust tests:
cargo test
Run the Python tests against an installed wheel:
maturin develop
.venv/bin/python -m unittest discover -s tests
Release
Update the version in pyproject.toml.
Create and push a tag matching the release version:
git tag v0.1.0
git push origin main
git push origin v0.1.0
Pushing the tag triggers the GitHub Actions release workflow, which builds the release artifacts and uploads them to PyPI.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file web_audio_api-0.3.1.tar.gz.
File metadata
- Download URL: web_audio_api-0.3.1.tar.gz
- Upload date:
- Size: 68.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
395883dc8ef1d90bbed93c8d7c6c187dbf54210e607fe3e38330331498200865
|
|
| MD5 |
a1700a473c85d43914dd254bab592b42
|
|
| BLAKE2b-256 |
616ea3db92e733c22ac1844487c073bf421a73456426a41ab49b0d308cf16889
|
File details
Details for the file web_audio_api-0.3.1-cp311-abi3-win_amd64.whl.
File metadata
- Download URL: web_audio_api-0.3.1-cp311-abi3-win_amd64.whl
- Upload date:
- Size: 3.0 MB
- Tags: CPython 3.11+, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
23ada710f2a600777e107ef3980453bdf70dda7b250ba74d23d95e0de560fc6e
|
|
| MD5 |
3ad0a1596ec4c099bc86b0fe8cd4ba65
|
|
| BLAKE2b-256 |
85cf3e239679ecf825adde08b0fb2a7f072a2d44f6e5746fd3085f419a65b8b5
|
File details
Details for the file web_audio_api-0.3.1-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: web_audio_api-0.3.1-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 3.9 MB
- Tags: CPython 3.11+, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d74bd5b2853217a3df1a4b6be7d2dc7c0f01618bc960c98920c9c17b4480a0dc
|
|
| MD5 |
e05740414304306e3991ae7203f42c21
|
|
| BLAKE2b-256 |
103e60f585caaa15029c6eb6a9a6bb409a36254dfa7a208c849a5146bf466e44
|
File details
Details for the file web_audio_api-0.3.1-cp311-abi3-macosx_11_0_arm64.whl.
File metadata
- Download URL: web_audio_api-0.3.1-cp311-abi3-macosx_11_0_arm64.whl
- Upload date:
- Size: 3.1 MB
- Tags: CPython 3.11+, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3911c3cf1380623e278cfb9d42be8694e38e1b71863bc3f69310cef3bd5855a2
|
|
| MD5 |
3f589dbeac71c0cfc83ea092c19c1eb5
|
|
| BLAKE2b-256 |
1298444d4abfd5184a04b6cf709c877f9cd1a3d314253f902fdbf8a798ac9f29
|
File details
Details for the file web_audio_api-0.3.1-cp311-abi3-macosx_10_12_x86_64.whl.
File metadata
- Download URL: web_audio_api-0.3.1-cp311-abi3-macosx_10_12_x86_64.whl
- Upload date:
- Size: 3.4 MB
- Tags: CPython 3.11+, macOS 10.12+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dee930c4615bf996bd3ae7a49893f9c2c013beee0c4ffb9d4128d83add5a171f
|
|
| MD5 |
9fef86a193c31356a6b68a091e3ad690
|
|
| BLAKE2b-256 |
6fa957caafa5af88e477655a26167ce3c1d25af40a86241d7b232b0cec90846d
|