Skip to content

Python tool for aircraft Doppler audio geolocation using historical ADS-B flight data (Source-available)

Bound audio chunk residency instead of mapping the full cache (#4)

Cinna Davis1f95689915ed
diff --git a/docs/AUDIO_INTAKE.md b/docs/AUDIO_INTAKE.md
index 2d2146f..32c6dcc 100644
--- a/docs/AUDIO_INTAKE.md
+++ b/docs/AUDIO_INTAKE.md
@@ -16,6 +16,14 @@ overlap_s)` returns `(audio_relative_start_s, float32_samples)` from a disk-back
cache, bounding working memory independently of recording length. Acoustic
analysis is available before the recording UTC clock is known.
+Chunk iteration seeks and reads only the active float32 window. It does not map
+the entire cache into the process: touched memory-map pages otherwise caused
+resident memory to grow with recording length. With fixed 120 s / 60 s chunks,
+synthetic 32 MiB and 128 MiB raw caches formerly added 35.58 MiB / 131.73 MiB
+peak RSS; explicit window reads add 3.63 MiB / 3.67 MiB. These are Linux iterator
+stress checks, not media acquisitions or full-pipeline resource measurements.
+Consumer event/result storage and plotting have separate memory costs.
+
The shared `time_window` bounds the UTC time of sample zero, including recording
clock uncertainty and any known trimming. Pass an explicit defensible user
window or externally verified recording metadata via that contract. Ordinary
@@ -36,3 +44,6 @@ averaging/resampling, overlapping chunk offsets, a 50 ms clip, missing/ambiguous
UTC, malformed/missing/empty media, hash identity and refusal to overwrite output.
These tolerances establish behavior for the exercised codec/container versions;
unknown encoders that omit gapless metadata can retain unknown leading padding.
+The Linux RSS regression reads a synthetic 128 MiB cache with a 32 MiB growth
+budget and verifies end coverage/checksum. Exact canonical six-event detection
+JSON also matches the earlier frozen run after the I/O change.
diff --git a/src/aircraft_geoloc/intake.py b/src/aircraft_geoloc/intake.py
index 7efd50e..bdcf7f3 100644
--- a/src/aircraft_geoloc/intake.py
+++ b/src/aircraft_geoloc/intake.py
@@ -83,11 +83,13 @@ class IntakeRecording:
size, overlap = int(round(chunk_s * sr)), int(round(overlap_s * sr))
if size <= overlap:
raise ValueError("Chunk step must be at least one sample")
- data = np.memmap(self.samples_path, dtype="<f4", mode="r")
- for first in range(0, len(data), size - overlap):
- yield first / sr, np.array(data[first:first + size])
- if first + size >= len(data):
- break
+ count = self.samples_path.stat().st_size // 4
+ with self.samples_path.open("rb", buffering=0) as data:
+ for first in range(0, count, size - overlap):
+ data.seek(first * 4)
+ yield first / sr, np.fromfile(data, dtype="<f4", count=min(size, count - first))
+ if first + size >= count:
+ break
def intake_recording(source, output_dir, *, time_window=None, sample_rate_hz=4000):
diff --git a/tests/test_intake.py b/tests/test_intake.py
index ba2fdd1..a7d4aef 100644
--- a/tests/test_intake.py
+++ b/tests/test_intake.py
@@ -1,5 +1,6 @@
import json
import subprocess
+import sys
import numpy as np
import pytest
@@ -9,6 +10,34 @@ from aircraft_geoloc.contracts import validate_recording
from aircraft_geoloc.intake import AudioIntakeError, intake_recording, source_hash
[email protected](sys.platform != "linux", reason="Linux ru_maxrss is measured in KiB")
+def test_chunk_iterator_does_not_retain_recording_sized_rss(tmp_path):
+ # A synthetic raw-cache stress fixture, not a captured audio recording.
+ cache = tmp_path / "long.f32"
+ with cache.open("wb") as stream:
+ stream.truncate(128 * 1024**2)
+ script = """
+import json, resource, sys
+from pathlib import Path
+from aircraft_geoloc.intake import IntakeRecording
+path = Path(sys.argv[1])
+duration = path.stat().st_size / 4 / 4000
+metadata = dict(source_sha256="0" * 64, duration_s=duration, sample_rate_hz=4000,
+ channels=1, source_format="synthetic_raw_cache", time_window=None)
+recording = IntakeRecording(path, metadata, {})
+before = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
+checksum = 0.0
+for start, samples in recording.iter_chunks():
+ checksum += float(samples.sum())
+print(json.dumps(dict(growth_kib=resource.getrusage(resource.RUSAGE_SELF).ru_maxrss-before,
+ last_end_s=start+len(samples)/4000, checksum=checksum)))
+"""
+ result = json.loads(subprocess.check_output([sys.executable, "-c", script, str(cache)], text=True))
+ assert result["growth_kib"] < 32 * 1024, result
+ assert result["last_end_s"] == pytest.approx(cache.stat().st_size / 4 / 4000)
+ assert result["checksum"] == 0
+
+
def test_stereo_resampling_chunk_times_and_missing_clock(tmp_path):
sr = 48000
t = np.arange(sr * 3) / sr