live.rs

crates/veilvoice-audio/src/live.rs

veilvoice-audio · 901 lines · read the source here · or on GitHub

Live microphone scrambling.

Structure

Capture and playback run as two independent callbacks driven by the audio hardware, joined by a lock-free SPSC ring buffer. The de-identification runs inside the output callback, which is the shortest path: adding a worker thread would mean a second buffer and a second scheduling delay for no benefit, and veilvoice_core::Deidentifier::process is explicitly allocation-free and safe to call from an audio callback.

Rules the callbacks follow

An audio callback that blocks produces a dropout, so neither callback ever allocates, locks, or waits. Statistics are published through a mutex the callback only ever tries to take: if the UI thread happens to hold it, the update is skipped rather than the audio stalling.

Latency

Total latency is the input buffer, plus the ring backlog, plus the engine's one-frame group delay (~21 ms at the defaults), plus the output buffer. The ring is intentionally short, enough to absorb jitter between two clocks that are not synchronised, not enough to accumulate a delay the user would notice while speaking.

In plain words

This is live mode: your microphone in one end, a voice that is not yours out the other, fast enough to hold a conversation.

Sound arrives from the microphone in small pieces, and each one has to be dealt with before the next arrives. There is no room to be late. So the veiling happens on the same short path the sound is already travelling, with nothing queued up behind it and nothing that could pause to allocate memory or wait for another part of the program.

If the computer ever cannot keep up, that is counted and shown rather than hidden. A gap in the sound you can see explained is far better than one you cannot.

WHAT THIS FILE CONTAINS

901 lines defining 12 functions (4 public), 4 types and 2 constants. Everything below is read out of the source, so it cannot disagree with the code.

The types it owns.

  • struct Kept line 63 · The recorders a session was asked for, one per side of Keeping.
  • struct LiveSession line 71 · A running live-scramble session.
  • struct Shared line 87 · What the audio callbacks and the caller both touch, held behind one handle.
  • struct Troubles line 113 · What the platform has said about a session's streams, counted where it is said and read by whoever asks.

What happens when it runs. These are the ways in: public, and nothing else in this file calls them, so they are what an outside caller reaches first.

  • LiveSession::start line 390 · Start scrambling from input into output.
    reaches start_recording, agree_on_a_rate, agree_on_a_rate_for, at_rate, input_ranges, rate_they_agree_on
  • LiveSession::stats line 607 · Read the current statistics, resetting the peak meters.
  • LiveSession::interference line 626 · What the platform last reported about either stream.

WHAT CALLS WHAT

Troubles::report line 131 Troubles::count line 161 Troubles::latest line 167 rate_they_agree_on line 203 input_ranges line 235 at_rate line 250 agree_on_a_rate line 281 agree_on_a_rate_for line 295 LiveSession::start line 390 LiveSession::start_recording line 428 LiveSession::stats line 607 LiveSession::interference line 626 entry: a way in: public, and nothing in this file calls it api: public, and also used inside this file helper: private to this file dashed: a call that goes back up, or across a wrapped rank The functions this file defines, and the calls between them. An edge means the callee's name appears, called, inside the caller's body. This is a syntactic reading, not a type-resolved one.

The functions this file defines, and the calls between them. An edge means the callee's name appears, called, inside the caller's body. This is a syntactic reading, not a type-resolved one.

The same graph as Mermaid source
%%{init: {"theme":"base","themeVariables":{"background":"#1a1b26","primaryColor":"#1f2335","primaryTextColor":"#c0caf5","primaryBorderColor":"#7aa2f7","secondaryColor":"#16161e","tertiaryColor":"#16161e","lineColor":"#737aa2","textColor":"#c0caf5","mainBkg":"#1f2335","nodeBorder":"#7aa2f7","clusterBkg":"#16161e","clusterBorder":"#2f3549","fontFamily":"ui-monospace, SFMono-Regular, Consolas, monospace","fontSize":"14px"}}}%%
flowchart TD
    n_report["Troubles::report<br/>line 131"]
    n_count["Troubles::count<br/>line 161"]
    n_latest["Troubles::latest<br/>line 167"]
    n_rate_they_agree_on["rate_they_agree_on<br/>line 203"]
    n_input_ranges["input_ranges<br/>line 235"]
    n_at_rate["at_rate<br/>line 250"]
    n_agree_on_a_rate["agree_on_a_rate<br/>line 281"]
    n_agree_on_a_rate_for["agree_on_a_rate_for<br/>line 295"]
    n_start(["LiveSession::start<br/>line 390"])
    n_start_recording["LiveSession::start_recording<br/>line 428"]
    n_stats(["LiveSession::stats<br/>line 607"])
    n_interference(["LiveSession::interference<br/>line 626"])
    n_agree_on_a_rate --> n_agree_on_a_rate_for
    n_agree_on_a_rate_for --> n_at_rate
    n_agree_on_a_rate_for --> n_input_ranges
    n_agree_on_a_rate_for --> n_rate_they_agree_on
    n_latest --> n_count
    n_start --> n_start_recording
    n_start_recording --> n_agree_on_a_rate
    click n_report href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-audio/src/live.rs#L131" "open the source"
    click n_count href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-audio/src/live.rs#L161" "open the source"
    click n_latest href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-audio/src/live.rs#L167" "open the source"
    click n_rate_they_agree_on href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-audio/src/live.rs#L203" "open the source"
    click n_input_ranges href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-audio/src/live.rs#L235" "open the source"
    click n_at_rate href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-audio/src/live.rs#L250" "open the source"
    click n_agree_on_a_rate href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-audio/src/live.rs#L281" "open the source"
    click n_agree_on_a_rate_for href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-audio/src/live.rs#L295" "open the source"
    click n_start href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-audio/src/live.rs#L390" "open the source"
    click n_start_recording href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-audio/src/live.rs#L428" "open the source"
    click n_stats href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-audio/src/live.rs#L607" "open the source"
    click n_interference href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-audio/src/live.rs#L626" "open the source"
    classDef entry fill:#1f2335,stroke:#7aa2f7,color:#c0caf5
    class n_start,n_stats,n_interference entry
    classDef api fill:#1f2335,stroke:#7dcfff,color:#c0caf5
    class n_start_recording api
    classDef helper fill:#1f2335,stroke:#bb9af7,color:#c0caf5
    class n_report,n_count,n_latest,n_rate_they_agree_on,n_input_ranges,n_at_rate,n_agree_on_a_rate,n_agree_on_a_rate_for helper
    subgraph chart_key ["Key"]
        direction LR
        key_entry["entry: a way in: public, and nothing in this file calls it"]
        key_api["api: public, and also used inside this file"]
        key_helper["helper: private to this file"]
    end
    class key_entry entry
    class key_api api
    class key_helper helper

This site loads no third-party script, so it cannot run Mermaid; the diagram above is the same nodes and edges drawn by the generator instead. GitHub renders the source below directly.

ITEMS

ItemLineDocumentation
RING_MILLIS pub(crate) const54How much jitter the ring absorbs before it starts dropping samples.
Kept pub struct63The recorders a session was asked for, one per side of Keeping.
LiveSession pub struct71A running live-scramble session.
Shared struct87What the audio callbacks and the caller both touch, held behind one handle.
Troubles pub(crate) struct113What the platform has said about a session's streams, counted where it is said and read by whoever asks.
GLITCH const126What a glitch is called when somebody asks, since the audio thread that reported it does not stop to say.
Troubles::report pub(crate) fn131Record what the platform said about a stream.
Troubles::count pub(crate) fn161How many reports there have been, glitches included.
Troubles::latest pub(crate) fn167The newest report.
rate_they_agree_on pub(crate) fn203Which sample rate a set of devices can all run at, if any.
input_ranges pub(crate) fn235The ranges a device reports, as plain numbers.
at_rate pub(crate) fn250One of a device's configurations at rate, preferring f32.
agree_on_a_rate fn281A microphone and an output, configured to one rate.
agree_on_a_rate_for pub(crate) fn295The same, for any number of microphones.
LiveSession::start pub fn390Start scrambling from input into output.
LiveSession::start_recording pub fn428Start scrambling, keeping the sides of it Keeping asks for.
LiveSession::stats pub fn607Read the current statistics, resetting the peak meters.
LiveSession::interference pub fn626What the platform last reported about either stream.