A voice meter started from a button
Updated
A sphere grows with the loudness of your voice: a microphone toggle button starts and stops Input.microphone, a label says what it is doing, and the script eases the sphere towards level() every frame.
Press the microphone button and allow the microphone, then speak or clap. Press it again to stop. Needs a microphone and your permission. Drag to orbit the camera, scroll to zoom.
A voice meter started from a button
#pragma lifecycle(startup, update)
const SENSITIVITY = 3; // raise it to react to quieter sounds
let speaker;let button;let label;let level = 0;
// The button's icon and the label follow the microphone.function show(listening, text) { button.setProps({ icon: listening ? 'Mic' : 'MicOff' }); label.setProps({ text });}
function startup() { speaker = findEntity('Speaker'); button = findEntity('Mic Button').getComponent(UIToggleButtonComponent); label = findEntity('Mic Label').getComponent(UILabelComponent); if (!Input.microphone.isSupported()) return show(false, 'This browser cannot capture audio');
on('ui/listen', async (event) => { if (event.eventType !== EventType.UIToggleButton.Change) return; if (!event.data.checked) { if (!Input.microphone.isListening()) return; // unchecked below, after a refusal Input.microphone.stop(); return show(false, 'Microphone off'); } if (await Input.microphone.start()) return show(true, 'Listening: speak or clap'); button.setChecked(false); show(false, 'No microphone, or permission refused'); });}
function update(dt) { // level() is 0 while not listening, so the sphere rests at its own size. const loudness = Math.min(1, Input.microphone.level() * SENSITIVITY); level += (loudness - level) * Math.min(1, dt * 12); speaker.scale.setScalar(1 + level);}How it works
Section titled “How it works”The script is explained step by step in A voice meter started from a button.
Open it in XR Creator
Section titled “Open it in XR Creator”Download the project file below, then import it in XR Hub with Projects › New › Import, as described in Projects and templates. It opens in XR Creator with the same scene you see running above, each script already attached to its Behavior entity. From there you can edit the scripts, adjust components and their props, preview your changes, and deploy the result as a world of your own.
VoiceMeter.xrproj