Skip to content
ReferenceSpecializedReact to sound levels.

Microphone

Group: Sound

Description

A Microphone listens to how loud things are and calls routes when the sound crosses a threshold: a clap starts the game, a sustained shout fills a meter, silence ends the turn. It does not record anything; it measures. (Recording is AudioRecorder's job.) Like every capture-style component, it starts with a permission prompt the visitor can refuse.

Syntax

Microphone(name)
Microphone(name, threshold=0.6, on_loud="route_name")

Parameters

Parameter Type Default Meaning
name str required The field's name; submitting the form delivers an AudioLevel under it.
threshold float 0.5 The loudness (0.0 to 1.0) that counts as "loud".
cooldown int 1000 Minimum milliseconds between on_loud calls.
rate int 0 Milliseconds between on_level calls, or 0 for never.
show bool True Display the permission UI and live meter.
visualize str "meter" The live display once granted: "meter", "waveform", or "bars".
on_loud / on_quiet route name or route function none Routes called when the volume rises past, and falls back below, the threshold.
on_level route name or route function none Route called every rate milliseconds with the current volume.
on_denied / on_error route name or route function none Routes for refusal and failure.

Examples

A noise meter for the classroom (or the dog):

from drafter import *
from dataclasses import dataclass


@dataclass
class State:
    barks: int


@route
def index(state: State) -> Page:
    return Page(state, [
        Header("Bark Counter"),
        Output("count", ["Barks detected: " + str(state.barks)]),
        "\n",
        Microphone("mic", threshold=0.6, cooldown=500,
                   on_loud="bark"),
        "\n",
        "Every loud noise counts as one bark. Ada disputes "
        "the methodology."
    ])


@route
def bark(state: State) -> Fragment:
    state.barks = state.barks + 1
    return Fragment(state, ["Barks detected: " + str(state.barks)],
                    target="#count")


start_server(State(0))

Notes

  • The loud and quiet routes can receive two parameters by name: volume (the loudness that was heard, from 0.0 to 1.0) and threshold (the component's own setting), both floats.
  • The level route can receive volume and pitch, both floats. The pitch is the strongest frequency in Hz, and the microphone sends it only when it detects one, so give that parameter a default value such as pitch: float = 0.0.
  • The denied and error routes can receive status ("denied", "unavailable", or "error") and message, both strings.
  • Each on_ parameter takes the route's name as a string or the route function itself. Route targets on the Button page compares the two.
  • An event route that returns a whole Page re-renders the component, briefly resetting the meter and peak_volume; return an Update or a Fragment for frequent events.
  • Tune threshold by watching the live meter, and use cooldown to stop one shout from firing five times.
  • The visitor can refuse the microphone; the permission entry covers handling "denied" gracefully.