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

This package currently requires Python 3.11 or newer.

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()

For realtime output diagnostics, AudioContext.playbackStats exposes underrun and latency metrics as a live Web Audio-style surface.

Examples

Runnable example scripts live in examples:

Run an example after maturin develop (see Local Development section)

.venv/bin/python examples/osc_gain.py

Async Methods

The binding exposes asyncio-native awaitables for the Web Audio methods that are async in web-audio-api-rs, such as AudioContext.resume() and OfflineAudioContext.startRendering().

import asyncio
import web_audio_api


async def main():
    ctx = web_audio_api.OfflineAudioContext(1, 3_000, 3_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())

AudioWorklet

Python worklets are regular AudioWorkletProcessor subclasses:

import web_audio_api


class GainHalfProcessor(web_audio_api.AudioWorkletProcessor):
    name = "gain-half"

    def process(self, inputs, outputs, parameters):
        if inputs and inputs[0]:
            for in_channel, out_channel in zip(inputs[0], outputs[0]):
                for i, sample in enumerate(in_channel):
                    out_channel[i] = sample * 0.5
        return True

Register the processor and create a node from it:

ctx.audioWorklet.addModule(GainHalfProcessor)
node = web_audio_api.AudioWorkletNode(ctx, "gain-half")

During process(...), the binding also exposes browser-style worklet globals:

  • sampleRate
  • currentTime
  • currentFrame
  • self.port

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

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

Run the IDL surface checker:

.venv/bin/python tools/check_idl_surface.py web-audio-api-idl.txt --both-directions

The checker reads the IDL file, imports web_audio_api, and verifies that the expected classes, methods, and properties are present on the Python surface.

Release Workflow

Update the version in Cargo.toml. Create and push a tag matching the release version; this uploads the artifacts to PyPI.

git tag v0.5.0
git push origin main
git push origin v0.5.0

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.5.0.tar.gz (83.5 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.5.0-cp311-abi3-win_amd64.whl (3.2 MB view details)

Uploaded CPython 3.11+Windows x86-64

web_audio_api-0.5.0-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.1 MB view details)

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

web_audio_api-0.5.0-cp311-abi3-macosx_11_0_arm64.whl (3.3 MB view details)

Uploaded CPython 3.11+macOS 11.0+ ARM64

web_audio_api-0.5.0-cp311-abi3-macosx_10_12_x86_64.whl (3.6 MB view details)

Uploaded CPython 3.11+macOS 10.12+ x86-64

File details

Details for the file web_audio_api-0.5.0.tar.gz.

File metadata

  • Download URL: web_audio_api-0.5.0.tar.gz
  • Upload date:
  • Size: 83.5 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.5.0.tar.gz
Algorithm Hash digest
SHA256 8313af49e792c1f0e30e4349b76f4eafa05c763156c1fcada2db5cd7e50c1e5f
MD5 2eb715c87225c4281f6a8cbdfbfbb7d3
BLAKE2b-256 cc217736e9999821343e1909e309d3e7407d6bec36a8ff5ec8857f50b1406fa9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for web_audio_api-0.5.0-cp311-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 05d3e7903b22e26bd89d52755f3903ff2cc5395000179a43c4abaa7ee6dbaf5a
MD5 9a57880a46c46d56f9e3567b4703dc18
BLAKE2b-256 388413c6301a3b521906bd54887bd9d775d1852e34d98c60ea138b55da4c3a9b

See more details on using hashes here.

File details

Details for the file web_audio_api-0.5.0-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for web_audio_api-0.5.0-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b3eec87e2af8d26492a288849a9b30a5c3d8bf8ae5d54b1d1f6942d4e75c7193
MD5 5eb1241f1a74c1329cd26421465ec092
BLAKE2b-256 69e103e0b5e29ca48e730aa8f21b1d50bb400341f8d16c8f22dbc68736f87446

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for web_audio_api-0.5.0-cp311-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 3d9dfcd1c9ff2e98c98b825b5029b22aa390465210b460ccb6fa209f9d633ef0
MD5 c3d93fb3d702bdb44c7e8ee0ccde49ac
BLAKE2b-256 f6b9130ac07885786301d64838464d60548feedafd85e1599b310aa73ded551b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for web_audio_api-0.5.0-cp311-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 b2ee5a000bec2c96a30c65235b92c22f0964663cdda0887b26e7a4ee44a5b208
MD5 92cfde69a18170b880d8c0ab18275ba2
BLAKE2b-256 a7bde7e841acf27720236a40c566795c7d5876fd2829f37c148870ddfb4d8254

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