Upstream: crates/rune/README.md § Recording audio (
Recorder,recorder/) at65f8eb43f710, MIT © Anomaly. Bundled by build-rune-apps; links point at this snapshot or at GitHub at that rev.
Recording audio (Recorder, recorder/)
Recorder is a cheap-to-clone handle that records the microphone to a file, with elapsed time
and a smoothed input level to draw. It mirrors Player.
let rec = rune::Recorder::to_file("note.m4a"); // AAC; a .wav path records 16-bit WAV
rec.start();
// In a view, a level bar that redraws without re-running the view:
el().w(4.0).h(40.0).live_scale(&rec.live_level())
// Later:
let note = rec.stop()?; // Recording { path, duration }
let clip = rune::Player::audio(note.path);
- Controls and state:
start,pause,resume,stop(returns theRecording) andcancel(deletes a recording in progress).state()isIdle,Starting,Recording,Paused,Stopped,Interrupted(reason)orFailed(message), pluselapsed()(paused time excluded) andlevel(). Builders:.format(RecordFormat::Wav),.sample_rate(hz),.channels(n). The format decides the extension (AAC writes.m4a, or keeps.mp4; WAV writes.wav), soto_file("note.m4a").format(RecordFormat::Wav)writesnote.wav;Recording::pathis always the file written. Dropping the last handle mid-recording stops it and keeps the file. - Levels:
level()is aLevelwithpeak_db()andrms_db()in dBFS, andmeter()mapped to 0..1 (-60 dB to 0 dB). RMS rises over 30 ms and falls over 300 ms; the peak falls 20 dB a second. Reads take no lock and allocate nothing.live_level()is aLive<f32>ofmeter()forlive_scale,live_opacityand the like, so a bound meter redraws on its own while recording; an idle or paused recorder draws no frames.set_wakerhears state changes only (start, pause, stop, interruption, failure), not progress; for an elapsed-time label, ask for a coarse tick withtx.wake_after(Recorder::METER_INTERVAL)whileis_recording(). - Permission:
Recorder::permission()isGranted,Denied,UndeterminedorRestricted, and reading it never prompts.Recorder::request_permission(callback)asks (or answers at once when already decided); the callback may run on any thread.startasks when undetermined. Starting without access ends inFailedwith a message that says where to allow it; it never crashes. - macOS setup: the app's Info.plist needs
NSMicrophoneUsageDescription. macOS ends a bundled app that touches the microphone without it, so Rune checks first and fails to start with a clear message instead. Hardened-runtime apps also need thecom.apple.security.device.audio-inputentitlement (sandboxed appscom.apple.security.device.microphone). An unsigned or ad-hoc signed build may be denied without a prompt, or lose its grant when its signature changes. An unbundled binary run from a terminal is attributed to the terminal, which asks on its behalf. - Backend (macOS): AVAudioRecorder on a thread of its own. It encodes AAC (or writes WAV),
meters the input and finalizes the file, so Rune never touches samples. The thread polls
its meters 30 times a second while recording. If the input device goes away or the default
input changes mid-recording, the state becomes
Interrupted(reason):stopstill returns what was captured, andcanceldeletes it. - Headless (
--snap,--frames,--tree, tests): no device and no prompt. Recorders hear aFakeMicrophone(by default a 220 Hz tone that swells every 1.6 s, so a meter moves). Elapsed time follows the virtual clock, levels come from the fake samples in 10 ms blocks (independent of frame rate), andstopwrites a real 16-bit mono WAV thatPlayeropens (an.m4apath becomes.wav).FakeMicrophone::sine(hz, amp),silence(),samples(rate, vec)andwav(path)choose the input;.permission(p),.answers(p)and.unplug_after(secs)exercise denial and interruption..install()sets it for the thread, orRecorder::fake(mic)for one recorder. - Other platforms (Linux, Windows, the web):
startends inFailed("recording isn't supported on this platform").