Skip to main content

A Rust/Python implementation of the Web Audio API, for use in non-browser contexts

Project description

Python bindings for web-audio-api-rs

PyPI version

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:

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

web_audio_api-0.3.1.tar.gz (68.6 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

web_audio_api-0.3.1-cp311-abi3-win_amd64.whl (3.0 MB view details)

Uploaded CPython 3.11+Windows x86-64

web_audio_api-0.3.1-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.9 MB view details)

Uploaded CPython 3.11+manylinux: glibc 2.17+ x86-64

web_audio_api-0.3.1-cp311-abi3-macosx_11_0_arm64.whl (3.1 MB view details)

Uploaded CPython 3.11+macOS 11.0+ ARM64

web_audio_api-0.3.1-cp311-abi3-macosx_10_12_x86_64.whl (3.4 MB view details)

Uploaded CPython 3.11+macOS 10.12+ x86-64

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

Hashes for web_audio_api-0.3.1.tar.gz
Algorithm Hash digest
SHA256 395883dc8ef1d90bbed93c8d7c6c187dbf54210e607fe3e38330331498200865
MD5 a1700a473c85d43914dd254bab592b42
BLAKE2b-256 616ea3db92e733c22ac1844487c073bf421a73456426a41ab49b0d308cf16889

See more details on using hashes here.

File details

Details for the file web_audio_api-0.3.1-cp311-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for web_audio_api-0.3.1-cp311-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 23ada710f2a600777e107ef3980453bdf70dda7b250ba74d23d95e0de560fc6e
MD5 3ad0a1596ec4c099bc86b0fe8cd4ba65
BLAKE2b-256 85cf3e239679ecf825adde08b0fb2a7f072a2d44f6e5746fd3085f419a65b8b5

See more details on using hashes here.

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

File hashes

Hashes for web_audio_api-0.3.1-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 d74bd5b2853217a3df1a4b6be7d2dc7c0f01618bc960c98920c9c17b4480a0dc
MD5 e05740414304306e3991ae7203f42c21
BLAKE2b-256 103e60f585caaa15029c6eb6a9a6bb409a36254dfa7a208c849a5146bf466e44

See more details on using hashes here.

File details

Details for the file web_audio_api-0.3.1-cp311-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for web_audio_api-0.3.1-cp311-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 3911c3cf1380623e278cfb9d42be8694e38e1b71863bc3f69310cef3bd5855a2
MD5 3f589dbeac71c0cfc83ea092c19c1eb5
BLAKE2b-256 1298444d4abfd5184a04b6cf709c877f9cd1a3d314253f902fdbf8a798ac9f29

See more details on using hashes here.

File details

Details for the file web_audio_api-0.3.1-cp311-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for web_audio_api-0.3.1-cp311-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 dee930c4615bf996bd3ae7a49893f9c2c013beee0c4ffb9d4128d83add5a171f
MD5 9fef86a193c31356a6b68a091e3ad690
BLAKE2b-256 6fa957caafa5af88e477655a26167ce3c1d25af40a86241d7b232b0cec90846d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page