Amplitude & Smoothing (RMS + EMA)
The first stage of the Gatekeeper pipeline (State 0: IDLE) serves as a strict silence gate. Before frame processing, the system must mathematically prove that the incoming time-domain signal contains active audio energy rather than mere background noise.
Raw Power Calculation (RMS)
The pipeline first calculates the raw amplitude of the incoming audio frame using the Root Mean Square (RMS). This provides an objective measurement of the continuous power of the waveform across the frame’s buffer.
Given a time-domain buffer x of length L, the RMS is defined as:
RMS = \sqrt{\frac{1}{L} \sum_{n=0}^{L-1} x[n]^2}
While mathematically accurate, raw RMS fluctuates rapidly when analyzing acoustic instruments. In acoustic pianos, most notes consist of two or three strings struck simultaneously (unisons). Due to microscopic tuning imperfections, these strings frequently drift out of phase with one another, causing destructive interference. This creates momentary volume drop-offs that signal processing engineers refer to as “beating.”
If the pipeline evaluated the silence gate against the raw RMS, a severe beating dip could cause the RMS to momentarily plummet below the noise floor, causing the Gatekeeper to falsely trigger a Silence state in the middle of a sustained note.
The Exponential Moving Average (EMA)
To solve this, the pipeline smooths the raw RMS using an Exponential Moving Average (EMA). This technique blends the current frame’s amplitude with the historical average, creating a resilient envelope that bridges momentary drop-offs. In signal processing terms, we are smoothing the amplitude envelope of the audio signal, removing the high-frequency content.
The EMA is calculated using the following difference equation:
EMA_{\ell} = \alpha \cdot RMS_{\ell} + (1 - \alpha) \cdot EMA_{\ell-1}
Where:
- \ell is the current frame index.
- RMS_{\ell} is the raw amplitude of the current frame.
- EMA_{\ell-1} is the smoothed amplitude from the previous frame.
- EMA_{\ell} is the newly computed smoothed amplitude.
- \alpha is the smoothing coefficient (
rms_ema_alpha) (0 < \alpha \le 1).
The Gatekeeper configuration deliberately defaults to a strong smoothing factor of \alpha = 0.1 specifically to ride through unison beating dips without stuttering.
Those paying attention may notice that this formulation is identical to that of a first-order IIR filter:
y[\ell] = \alpha \cdot x[\ell] + (1 - \alpha) \cdot y[\ell-1]
Though the logic of the EMA operation is the absolute same, there is a fundamental mathematical shift given the time scale and the input signal, RMS.
- Block Level time scale: \ell represents an entire frame. The time between \ell and \ell-1 is 46.4 milliseconds (2048 samples). This differs from the sample level where the time scale is 1 sample (46.4 microseconds).
- RMS signal: x[\ell] is an RMS value calculated over 46.4 milliseconds. This differs from the sample level where x[\ell] is a single audio sample.
Because x[\ell] is an RMS value calculated over 46.4 milliseconds, your EMA is no longer filtering audio frequencies (like the DC blocker). Instead, it is filtering the volume envelope of the audio. Formally, the RMS calculation is a non-linear decimation operation (downsampling by a factor of 2048), so the EMA is operating on a non-linear signal.
Below is a simulation of the Gatekeeper’s amplitude processing pipeline to demonstrate the importance of the EMA smoothing.
Noise Floor Calibration & Gating
Now that we know how to smooth the amplitude envelope of the audio signal, we can use it to determine when the Gatekeeper should start or stop processing audio.
The gating mechanism adapts to the acoustic environment using a standalone calibration module.
- Measurement: Upon startup or recalibration, the system opens an independent audio stream and discards the first 10 frames to allow hardware components to stabilize. It then measures roughly 2 seconds of ambient room noise.
- Baseline Computation: The calibration routine computes the RMS for these frames, smoothed with an \alpha of 0.1, to determine the room’s average ambient energy baseline.
- Thresholding: This baseline is multiplied by a user-defined safety factor (defaulting to 1.0) to establish the absolute
silence_threshold.
Execution: On the main audio thread, the Gatekeeper compares the current frame’s EMA_{\ell} against this silence_threshold. If the amplitude falls below the threshold, the state machine evaluates to SignalState::Silence, immediately aborts further DSP processing for the frame, and resets all capture counters.
After this, the Gatekeeper will continue to monitor the audio signal. If the amplitude rises above the silence_threshold for a sustained period (typically 50 milliseconds), the state machine evaluates to SignalState::Active, and the main DSP pipeline is engaged.