crates/veilvoice-audio/src/live.rs
veilvoice-audio · 876 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
876 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 Keptline 63 · The recorders a session was asked for, one per side of Keeping.struct LiveSessionline 71 · A running live-scramble session.struct Sharedline 87 · What the audio callbacks and the caller both touch, held behind one handle.struct Troublesline 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::startline 386 · Start scrambling from input into output.
reachesstart_recording,agree_on_a_rate,agree_on_a_rate_for,at_rate,input_ranges,rate_they_agree_onLiveSession::statsline 603 · Read the current statistics, resetting the peak meters.LiveSession::interferenceline 622 · What the platform last reported about either stream.
WHAT CALLS WHAT
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 157"]
n_latest["Troubles::latest<br/>line 163"]
n_rate_they_agree_on["rate_they_agree_on<br/>line 199"]
n_input_ranges["input_ranges<br/>line 231"]
n_at_rate["at_rate<br/>line 246"]
n_agree_on_a_rate["agree_on_a_rate<br/>line 277"]
n_agree_on_a_rate_for["agree_on_a_rate_for<br/>line 291"]
n_start(["LiveSession::start<br/>line 386"])
n_start_recording["LiveSession::start_recording<br/>line 424"]
n_stats(["LiveSession::stats<br/>line 603"])
n_interference(["LiveSession::interference<br/>line 622"])
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#L157" "open the source"
click n_latest href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-audio/src/live.rs#L163" "open the source"
click n_rate_they_agree_on href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-audio/src/live.rs#L199" "open the source"
click n_input_ranges href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-audio/src/live.rs#L231" "open the source"
click n_at_rate href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-audio/src/live.rs#L246" "open the source"
click n_agree_on_a_rate href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-audio/src/live.rs#L277" "open the source"
click n_agree_on_a_rate_for href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-audio/src/live.rs#L291" "open the source"
click n_start href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-audio/src/live.rs#L386" "open the source"
click n_start_recording href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-audio/src/live.rs#L424" "open the source"
click n_stats href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-audio/src/live.rs#L603" "open the source"
click n_interference href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-audio/src/live.rs#L622" "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
| Item | Line | Documentation |
|---|---|---|
RING_MILLIS pub(crate) const | 54 | How much jitter the ring absorbs before it starts dropping samples. |
Kept pub struct | 63 | The recorders a session was asked for, one per side of Keeping. |
LiveSession pub struct | 71 | A running live-scramble session. |
Shared struct | 87 | What the audio callbacks and the caller both touch, held behind one handle. |
Troubles pub(crate) struct | 113 | What the platform has said about a session's streams, counted where it is said and read by whoever asks. |
GLITCH const | 126 | What a glitch is called when somebody asks, since the audio thread that reported it does not stop to say. |
Troubles::report pub(crate) fn | 131 | Record what the platform said about a stream. |
Troubles::count pub(crate) fn | 157 | How many reports there have been, glitches included. |
Troubles::latest pub(crate) fn | 163 | The newest report. |
rate_they_agree_on pub(crate) fn | 199 | Which sample rate a set of devices can all run at, if any. |
input_ranges pub(crate) fn | 231 | The ranges a device reports, as plain numbers. |
at_rate pub(crate) fn | 246 | One of a device's configurations at rate, preferring f32. |
agree_on_a_rate fn | 277 | A microphone and an output, configured to one rate. |
agree_on_a_rate_for pub(crate) fn | 291 | The same, for any number of microphones. |
LiveSession::start pub fn | 386 | Start scrambling from input into output. |
LiveSession::start_recording pub fn | 424 | Start scrambling, keeping the sides of it Keeping asks for. |
LiveSession::stats pub fn | 603 | Read the current statistics, resetting the peak meters. |
LiveSession::interference pub fn | 622 | What the platform last reported about either stream. |