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

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.3.1
git push origin main
git push origin v0.3.1

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.4.0.tar.gz (77.0 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.4.0-cp311-abi3-win_amd64.whl (3.0 MB view details)

Uploaded CPython 3.11+Windows x86-64

web_audio_api-0.4.0-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.4.0-cp311-abi3-macosx_11_0_arm64.whl (3.1 MB view details)

Uploaded CPython 3.11+macOS 11.0+ ARM64

web_audio_api-0.4.0-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.4.0.tar.gz.

File metadata

  • Download URL: web_audio_api-0.4.0.tar.gz
  • Upload date:
  • Size: 77.0 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.4.0.tar.gz
Algorithm Hash digest
SHA256 decaabc7ff0359ebc7b9484cbbd4271a3b0d2e48473626775ab5d16ec64b5873
MD5 4e7da96095281615cb53e0f2aff123e0
BLAKE2b-256 4141d9c225babc5e132331b561fbf8574e91c4d6317dfe7ba6eefb35394c1371

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for web_audio_api-0.4.0-cp311-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 cc9d5a985f77a50baaac99b3bd3d45425777a1d916c422945e014af1a98f51fd
MD5 e5289587f4e9fb4d916afe8f0b57d1d5
BLAKE2b-256 5358c6df3aeff6556da9b17c7d4f149102313fa2e1fe83262ae21bb55098c168

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for web_audio_api-0.4.0-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 375dd0e16c3c462cf933abaad916c6d066f6770b698fb0dc861abc0c47c1f606
MD5 f983023be2e55cfee368fd225001a564
BLAKE2b-256 02bedfd6e0bebc543b584ab9709e1c84ec40539e0f7f5766e57a17ade0128aed

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for web_audio_api-0.4.0-cp311-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7fe56e19c5fda310fdce5c621360543fff6b6d90ce264cd4f0bdfc8e8a5487b3
MD5 185c9a38d6882ff2515847beb929e243
BLAKE2b-256 688d80bd256d03e0ecb8962b9833678daac27e18aa29d88d69a4c5c276635320

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for web_audio_api-0.4.0-cp311-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 c99a2c57aa33e0b13ea72e83c74b9846e0bb25adacc998ff069da03c4de7b65e
MD5 eb53ffaed40104a0135510e69bc2eb82
BLAKE2b-256 c7301673c7f70fbea1cebc298dc76b9fb98b3ff7465642a4843b348f759c238f

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