Set 8 Audio Level RMS Port
Set 8 Audio Level RMS Port validates deterministic RMS math parity over public synthetic samples without audio capture, microphone permission, or UI readiness authority.
The write-up
The Swift AudioLevelMonitor drives a live microphone level meter in a recording app. Almost all of that file is platform machinery: opening a capture session, picking a device, reading sample buffers off a callback. The Swift AudioLevelMonitor drives a live microphone level meter in a recording app. Almost all of that file is platform machinery: opening a capture session, picking a device, reading sample buffers off a callback. One small pure function inside it, normalizedLevel, turns a block of audio samples into a single number between zero and one. That number is the meter reading, and it is the only part that can be checked without a microphone.
This component ports that one function to Python and runs it over synthetic sample arrays that ship in the public clone. This component ports that one function to Python and runs it over synthetic sample arrays that ship in the public clone. It answers one question: does the Python re-implementation produce the same level value as the Swift original, on inputs anyone can inspect. It writes a result record with the per-case comparison and a pass or blocked verdict.
Everything device-specific stays on the Swift side. The port does not open an AVCaptureSession, request microphone permission, read recorded audio, or capture a device. Everything device-specific stays on the Swift side. The port does not open an AVCaptureSession, request microphone permission, read recorded audio, or capture a device.
Purpose
A live level meter is hard to test because it depends on real audio hardware and OS permissions that cannot live in a public fixture. A live level meter is hard to test because it depends on real audio hardware and OS permissions that cannot live in a public fixture. If you ported the whole monitor you would inherit all of that and prove nothing checkable. The narrow choice here is to isolate the amplitude arithmetic and hold everything else out.
What crosses into Python is the calculation alone. The claim is deliberately small: numeric parity for one function over sample arrays we can publish. What crosses into Python is the calculation alone. The claim is deliberately small: numeric parity for one function over sample arrays we can publish. That is why the interesting content is what the port refuses to include, not what it copies.
How it works
The math lives in normalized_level(samples, sample_format). It accepts two format tags, float32 and int16. The math lives in normalized_level(samples, sample_format). It accepts two format tags, float32 and int16. Any other tag raises ValueError, which is how the unsupported-format case is exercised. An empty buffer returns 0.0 before any arithmetic runs.
For a non-empty buffer it accumulates the square of each sample. Float samples are used as-is. For a non-empty buffer it accumulates the square of each sample. Float samples are used as-is. Int16 samples are first divided by 32767.0, the Swift Int16.max, to map the integer range onto roughly minus-one to one. It then takes the root mean square, sqrt(total / count), which summarises the block's energy as one amplitude. That value is multiplied by 8.0 and clamped with min(max(rms * 8.0, 0.0), 1.0). The gain of eight is a display choice carried over verbatim from the Swift source: quiet speech sits low on a zero-to-one meter without it, so the level is scaled up and then capped so loud input cannot overshoot one. The int16 divisor and the rms * 8 clamp are the two lines the exported bundle requires to match the copied Swift text.
The runtime is _audio_evaluator. It loads the probe manifest and runs three evaluation passes, then joins their findings. The runtime is _audio_evaluator. It loads the probe manifest and runs three evaluation passes, then joins their findings.
_evaluate_reference_cases reads the synthetic cases in the manifest. Each case carries samples, a format, an expected level, and a tolerance. _evaluate_reference_cases reads the synthetic cases in the manifest. Each case carries samples, a format, an expected level, and a tolerance. For each one it calls normalized_level and compares the observed value against the expected value within the tolerance. A case that is malformed, that raises, or that lands outside tolerance becomes a finding. It also checks that all three named cases in EXPECTED_CASES (float32_reference_buffer, int16_reference_buffer, clamp_over_one_buffer) are present, and records a finding for any that are missing.
_evaluate_byte_reference_cases covers the optional WAV cases. For each one _decode_int16_wav opens the file, and rejects anything that is not mono, 16-bit, uncompressed PCM, or whose byte length does not match its header. _evaluate_byte_reference_cases covers the optional WAV cases. For each one _decode_int16_wav opens the file, and rejects anything that is not mono, 16-bit, uncompressed PCM, or whose byte length does not match its header. It unpacks the frames to int16 samples, recomputes the level from the raw bytes, and compares against the manifest expectation. A missing file, a decode failure, or an unsupported format each becomes a distinct finding rather than a silent skip.
_evaluate_negative_exercises runs the three refusals directly. It confirms an empty buffer reads zero, that [1.0, -1.0, 0.5] scales past one and clamps to one, and that format pcm24 is refused with a ValueError. _evaluate_negative_exercises runs the three refusals directly. It confirms an empty buffer reads zero, that [1.0, -1.0, 0.5] scales past one and clamps to one, and that format pcm24 is refused with a ValueError. Each expectation is cross-checked against a small JSON fixture and turns into a finding if it does not hold. The declared codes for these cases live in EXPECTED_NEGATIVE_CASES.
Any finding from any pass flips the overall status from pass to blocked. run wraps the evaluator for fixture input and run_batch8_audio_level_rms_bundle wraps it for the exported source bundle; result_card projects the verdict, the reference-case counts, and the authority floor into a compact public card. Any finding from any pass flips the overall status from pass to blocked. run wraps the evaluator for fixture input and run_batch8_audio_level_rms_bundle wraps it for the exported source bundle; result_card projects the verdict, the reference-case counts, and the authority floor into a compact public card.
Diagram source & refs
flowchart TD manifest["Probe manifest synthetic arrays + WAV byte cases expected level per case"] evaluator["_audio_evaluator"] refcases["_evaluate_reference_cases float32, int16, clamp-over-one"] bytecases["_evaluate_byte_reference_cases decode mono 16-bit PCM WAV"] negatives["_evaluate_negative_exercises empty, clamp, unknown-format"] level["normalized_level(samples, format)"] compare["compare observed vs expected within tolerance"] verdict{"any finding?"} blocked["status: blocked"] passed["status: pass"] manifest --> evaluator evaluator --> refcases evaluator --> bytecases evaluator --> negatives refcases --> level bytecases --> level negatives --> level level --> compare compare --> verdict verdict -->|"yes"| blocked verdict -->|"no"| passedNegative cases
The three named negative cases are the reason the port is worth running. audio_level_empty_buffer_zero requires an empty buffer to read 0.0. The three named negative cases are the reason the port is worth running. audio_level_empty_buffer_zero requires an empty buffer to read 0.0. audio_level_clamps_over_one requires an over-one RMS-scaled buffer to clamp to 1.0. audio_level_unknown_format_refused requires a format the port does not know to raise ValueError. If any of these behaviors changed, the corresponding code (BATCH8_AUDIO_LEVEL_EMPTY_BUFFER_ZERO, BATCH8_AUDIO_LEVEL_CLAMP_REQUIRED, BATCH8_AUDIO_LEVEL_UNKNOWN_FORMAT_REFUSED) is recorded and the run blocks.
Prior Art Grounding
The math is standard digital-audio metering. Root mean square amplitude is a common way to summarise signal energy for a level display, and the practice of keeping OS capture APIs outside a pure numeric test is ordinary engineering hygiene. The math is standard digital-audio metering. Root mean square amplitude is a common way to summarise signal energy for a level display, and the practice of keeping OS capture APIs outside a pure numeric test is ordinary engineering hygiene. Two honest anchors for the surrounding platform work are Apple's AVFoundation media framework, which is the capture layer the Swift original uses, and the FFmpeg documentation, where audio streams and levels are handled as explicit inputs and transforms. This component borrows only the RMS-level calculation shape and binds it to fixture-based Python parity checks.
Validation Result record Path
Run these from the microcosm-substrate/ public root:
The first command writes the parity result and sign-off JSON. The second validates the copied Swift source module, its digest anchors, the negative exercises, and the body-exclusion scan. The first command writes the parity result and sign-off JSON. The second validates the copied Swift source module, its digest anchors, the negative exercises, and the body-exclusion scan. The test checks the Python port, bundle validation, result record body scan, and scope limit. A pass means the Python port reproduced every expected level within tolerance and refused every case it should refuse.
Scope boundary
Scope limit
Numeric parity with the copied Swift calculation
The strongest claim this supports is a deterministic Python port of the audio-level RMS calculation that matches the copied Swift text on published sample arrays, with a generated diagram view and navigation links available from the same source row. The strongest claim this supports is a deterministic Python port of the audio-level RMS calculation that matches the copied Swift text on published sample arrays, with a generated diagram view and navigation links available from the same source row. The proof boundary is numeric parity for normalized_level over public fixture inputs, nothing more.
The scope limit stops there. This is not macOS audio-session evidence, not microphone permission authority, not device capture, not UI readiness, not source-file changes, and not public sharing or launch-scope decision. The scope limit stops there. This is not macOS audio-session evidence, not microphone permission authority, not device capture, not UI readiness, not source-file changes, and not public sharing or launch-scope decision. The result records carry refs, digests, sample counts, and parity verdicts only; copied source bodies and audio samples stay out of them. Any claim past parity would need new evidence this component does not produce.
Context & evidence
In short Set 8 Audio Level RMS Port ports the Swift AudioLevelMonitor normalizedLevel RMS calculation into a bounded Python component and checks float, int16, clamp, empty-buffer, and unsupported-format cases over public synthetic sample arrays. It carries source-module refs, digests, anchors, sample counts, parity verdicts, negative cases, and an scope limit while excluding AVCaptureSession startup, microphone permission, recorded audio, device state, UI readiness, source-file changes, launch, public sharing, and whole-system correctness.
Scope limit Deterministic RMS parity evidence over public fixture inputs and copied source refs only; no macOS audio-session evidence, microphone permission authority, device capture, recorded audio, UI readiness, source-file changes, launch-scope decision, publishing-scope decision, or whole-system correctness.
Covers Audio Level RMS Port
Source
Source Source module: src/microcosm_core/organs/batch8_audio_level_rms_port.py · Design note · Source registry