How sebastianfeliciano/packet-sonics works
1. What this repo is
Sonic is a two-language, real-time packet-to-audio system. The core packet capture and packet-to-note mapping live in C, while Python provides the user-facing runtime, audio playback, GUI/web serving, and metrics.
2. Primary pipeline
Network traffic enters through a live capture interface, is filtered to TCP/UDP, and is transformed into discrete “note events” containing frequency and duration. Those events then flow into one of two synthesis paths:
- local playback via Python + sounddevice
- browser playback via Web Audio API from the web server path
3. C capture and mapping boundary
packet_processor.c is the low-level boundary to the network stack. It uses libpcap to sniff live packets, parses IP and transport headers, and prints note instructions to stdout in a simple text protocol like NOTE tcp <freq> <duration_ms>.
This is the repo’s most important separation: C handles privileged packet inspection; Python consumes its output.
4. Python orchestration boundary
sonic_gui.py appears to be the main desktop orchestrator. It launches packet_processor as a subprocess, reads the stdout note stream, queues events, and hands them to an audio worker thread.
The README makes clear that capture is privileged, but the GUI itself should not be run as root; instead, the capture subprocess is elevated when Start is clicked.
5. Local audio subsystem
In the desktop path, Python synthesizes tones directly with sounddevice and numpy. The GUI is responsible for Start/Stop control and status/logging, while a background playback loop turns queued (freq, duration) events into sound.
sonic_audio_helper.py likely contains shared audio-synthesis helpers used by the local playback path and possibly the browser-serving path.
6. Browser audio subsystem
sonic_server.py is the alternative, recommended runtime. In this mode, audio synthesis happens entirely in the browser via Web Audio API, so the server does not need Tkinter or local sound output.
Architecturally, this moves the synthesis boundary from the host OS to the client browser, but the upstream packet-capture-to-note mapping is still the same conceptually.
7. UI/runtime split
There are two front ends with different responsibilities:
- sonic_gui.py: desktop control surface for capture + local audio playback
- sonic_server.py: web-facing control surface for browser playback
Both are thin orchestration layers above the same packet-to-note concept, but they target different audio endpoints.
8. Metrics and observability
The GUI path exposes Prometheus metrics on localhost:9090/metrics using prometheus_client. The metrics track:
- packets sonified by protocol
- notes actually played
- current note queue size
This means the desktop process is also a telemetry source, and Prometheus becomes an external consumer that scrapes it.
9. Prometheus integration boundary
prometheus.yml configures scraping of Sonic’s metrics endpoint. Prometheus is not part of the audio path; it is an observability sidecar/tooling layer that reads counters and gauges for traffic visualization.
10. Build and deployment shape
Makefile builds the C binary packet_processor from packet_processor.c, while requirements.txt supplies the Python runtime dependencies (numpy, sounddevice, and Prometheus support). Dockerfile and .dockerignore suggest containerization support, but the runtime still depends on live packet capture permissions and host network access.
11. Key data flow to diagram
A good architecture diagram should show:
- live network interface → packet_processor (C/libpcap)
- packet_processor stdout note events → Python controller (sonic_gui.py or sonic_server.py)
- Python queue/worker → local audio output (sounddevice) or browser audio (Web Audio API)
- Python metrics endpoint → Prometheus scrape/graphing
Also show the privilege boundary around capture: the packet sniffer needs elevated permission, while the UI/server process remains unprivileged.
12. Secondary files
test_audio.py is likely a local validation utility for audio playback rather than a core subsystem. It supports development/testing, but it is not central to the runtime architecture.
Main entry points: sonic_gui.py Desktop GUI · packet_processor.c Sniffer · sonic_server.py Web server · sonic_audio_helper.py Audio helper
Components 15
Capture & mapping
Privileged packet sniffing and packet-to-note translation in C.
- Network · Incoming packets from the host network interface.
- Sniffer · Low-level packet capture boundary and protocol parsing.
packet_processor.c - Note stream · Text note events emitted from C for Python to consume.
Python runtime
Desktop and web orchestration layers plus shared audio helpers.
- Desktop GUI · Desktop orchestrator that starts capture and drives local playback.
sonic_gui.py - Web server · Server-side orchestration for browser-based playback.
sonic_server.py - Event queue · Queues note events before audio playback.
- Audio helper · Shared synthesis helpers for tone generation and playback setup.
sonic_audio_helper.py - Local audio · Host-side tone synthesis and speaker output.
- Browser audio · Client-side synthesis path used by the web runtime.
- Browser · Receives control and renders audio in the browser.
Operations
Metrics, scraping, and deployment/build support.
- Metrics · Exports packet, note, and queue metrics from the Python process.
- Prometheus · Scrapes Sonic metrics for monitoring and visualization.
prometheus.yml - Build · Builds the C capture binary from source.
Makefile - Deps · Python runtime dependencies for audio and metrics.
requirements.txt - Container · Containerization support for packaging and running the system.
Dockerfile
Connections 17
- Network → Sniffer: capture · Live traffic is sniffed and parsed.
- Sniffer → Note stream: emit notes · C prints note instructions to stdout.
- Note stream → Desktop GUI: consume · Desktop runtime reads the note stream.
- Note stream → Web server: consume · Web runtime also consumes note events.
- Desktop GUI → Event queue: enqueue · GUI hands events to the playback queue.
- Web server → Browser: serve · Server delivers browser-facing control and assets.
- Event queue → Audio helper: prepare · Queued events are converted into synthesis inputs.
- Audio helper → Local audio: play · Helpers drive host audio playback.
- Web server → Browser audio: audio path · Browser runtime uses Web Audio for synthesis.
- Browser audio → Browser: render · Audio is rendered inside the client browser.
- Desktop GUI → Metrics: export · GUI process exposes operational metrics.
- Metrics → Prometheus: scrape · Prometheus collects counters and gauges.
- Build → Sniffer: build · Make builds the capture binary.
- Deps → Desktop GUI: install · Python dependencies support the runtime.
- Container → Desktop GUI: package · Container definition packages the Python app.
- Desktop GUI → Sniffer: spawn root · GUI launches the capture subprocess with elevated privileges.
- Web server → Sniffer: spawn root · Web runtime also depends on privileged capture subprocesses.