Source code for audiodev.wasm_audio

# SPDX-FileCopyrightText: 2026 Brad Barnett
#
# SPDX-License-Identifier: MIT

"""Web Audio PCM backend for the direct MicroPython WebAssembly runtime."""

import _wasm_bridge

from audiodev import AudioFormat, AudioLocked, PCMInput, PCMOutput, check_latency

DEFAULT_FORMAT = AudioFormat(48000, 2, 16, signed=True, byteorder="little")


def _format_args(fmt):
    return (fmt.rate, fmt.channels, fmt.bits, fmt.signed, fmt.byteorder == "big")


[docs] class WasmPCMOutput(PCMOutput): def __init__(self, format=DEFAULT_FORMAT, *, latency=None, **kwargs): check_latency(latency) self.latency = latency super().__init__(format, **kwargs) def _open(self): if not _wasm_bridge.audio_state(False): # The bridge turns audio on at the page's first click or key # press; `AudioOut` waits for that instead of failing. raise AudioLocked( "audio output is disabled; use the host Enable Audio control," " or click or press a key on the page" ) def _write(self, buf): return _wasm_bridge.audio_write(buf, *_format_args(self.format)) def _drain(self): while self.queued_size(): _wasm_bridge.sleep_ms(1)
[docs] def queued_size(self): return _wasm_bridge.audio_queued_size(False)
[docs] def is_active(self): return self.queued_size() > 0
[docs] def clear(self): _wasm_bridge.audio_clear(False)
[docs] class WasmPCMInput(PCMInput): def __init__(self, format=DEFAULT_FORMAT, *, latency=None, **kwargs): check_latency(latency) self.latency = latency super().__init__(format, **kwargs) def _open(self): if not _wasm_bridge.audio_state(True): raise RuntimeError( "microphone is disabled; use the host Enable Microphone control" ) def _readinto(self, buf): return _wasm_bridge.audio_readinto(buf, *_format_args(self.format))
[docs] def queued_size(self): return _wasm_bridge.audio_queued_size(True)
[docs] def is_active(self): return self.queued_size() > 0
[docs] def clear(self): _wasm_bridge.audio_clear(True)
def pcm_out(format=None, **kwargs): return WasmPCMOutput(format or DEFAULT_FORMAT, **kwargs) def pcm_in(format=None, **kwargs): return WasmPCMInput(format or DEFAULT_FORMAT, **kwargs)