# sebastianfeliciano/packet-sonics architecture

> Architecture diagram of the GitHub repository sebastianfeliciano/packet-sonics, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-13). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/sebastianfeliciano/packet-sonics
- Repository: https://github.com/sebastianfeliciano/packet-sonics

## Overview

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`](https://github.com/sebastianfeliciano/packet-sonics/blob/main/sonic_gui.py) (Desktop GUI), [`packet_processor.c`](https://github.com/sebastianfeliciano/packet-sonics/blob/main/packet_processor.c) (Sniffer), [`sonic_server.py`](https://github.com/sebastianfeliciano/packet-sonics/blob/main/sonic_server.py) (Web server), [`sonic_audio_helper.py`](https://github.com/sebastianfeliciano/packet-sonics/blob/main/sonic_audio_helper.py) (Audio helper)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_capture["Capture & mapping"]
  node_net_if(("Network<br/>live traffic"))
  node_pcap_sniffer["Sniffer<br/>C/libpcap<br/>[packet_processor.c]"]
  node_note_stream["Note stream<br/>stdout protocol"]
end

subgraph group_runtime["Python runtime"]
  node_gui_main["Desktop GUI<br/>Python controller<br/>[sonic_gui.py]"]
  node_web_main["Web server<br/>Python runtime<br/>[sonic_server.py]"]
  node_event_queue["Event queue<br/>buffer/worker"]
  node_audio_helper["Audio helper<br/>Python utility"]
  node_local_audio(("Local audio<br/>sounddevice/numpy"))
  node_browser_audio(("Browser audio<br/>Web Audio API"))
  node_browser_client(("Browser<br/>web client"))
end

subgraph group_ops["Operations"]
  node_metrics_ep[("Metrics<br/>Prometheus endpoint")]
  node_prometheus[("Prometheus<br/>scraper<br/>[prometheus.yml]")]
  node_makefile["Build<br/>Makefile"]
  node_deps["Deps<br/>Python requirements<br/>[requirements.txt]"]
  node_docker["Container<br/>Docker config"]
end

node_net_if -->|"capture"| node_pcap_sniffer
node_pcap_sniffer -->|"emit notes"| node_note_stream
node_note_stream -->|"consume"| node_gui_main
node_note_stream -->|"consume"| node_web_main
node_gui_main -->|"enqueue"| node_event_queue
node_web_main -->|"serve"| node_browser_client
node_event_queue -->|"prepare"| node_audio_helper
node_audio_helper -->|"play"| node_local_audio
node_web_main -->|"audio path"| node_browser_audio
node_browser_audio -->|"render"| node_browser_client
node_gui_main -->|"export"| node_metrics_ep
node_metrics_ep -->|"scrape"| node_prometheus
node_makefile -->|"build"| node_pcap_sniffer
node_deps -->|"install"| node_gui_main
node_docker -->|"package"| node_gui_main
node_gui_main -.->|"spawn root"| node_pcap_sniffer
node_web_main -.->|"spawn root"| node_pcap_sniffer

click node_pcap_sniffer "https://github.com/sebastianfeliciano/packet-sonics/blob/main/packet_processor.c"
click node_gui_main "https://github.com/sebastianfeliciano/packet-sonics/blob/main/sonic_gui.py"
click node_web_main "https://github.com/sebastianfeliciano/packet-sonics/blob/main/sonic_server.py"
click node_audio_helper "https://github.com/sebastianfeliciano/packet-sonics/blob/main/sonic_audio_helper.py"
click node_prometheus "https://github.com/sebastianfeliciano/packet-sonics/blob/main/prometheus.yml"
click node_makefile "https://github.com/sebastianfeliciano/packet-sonics/tree/main/Makefile"
click node_deps "https://github.com/sebastianfeliciano/packet-sonics/blob/main/requirements.txt"
click node_docker "https://github.com/sebastianfeliciano/packet-sonics/tree/main/Dockerfile"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_net_if,node_pcap_sniffer,node_note_stream toneBlue
class node_gui_main,node_web_main,node_event_queue,node_audio_helper,node_local_audio,node_browser_audio,node_browser_client toneAmber
class node_metrics_ep,node_prometheus,node_makefile,node_deps,node_docker toneMint
```

## Components

### Capture & mapping

Privileged packet sniffing and packet-to-note translation in C.

- **Network**: live traffic · Incoming packets from the host network interface.
- [Sniffer](https://github.com/sebastianfeliciano/packet-sonics/blob/main/packet_processor.c): C/libpcap · Low-level packet capture boundary and protocol parsing. · `packet_processor.c`
- **Note stream**: stdout protocol · Text note events emitted from C for Python to consume.

### Python runtime

Desktop and web orchestration layers plus shared audio helpers.

- [Desktop GUI](https://github.com/sebastianfeliciano/packet-sonics/blob/main/sonic_gui.py): Python controller · Desktop orchestrator that starts capture and drives local playback. · `sonic_gui.py`
- [Web server](https://github.com/sebastianfeliciano/packet-sonics/blob/main/sonic_server.py): Python runtime · Server-side orchestration for browser-based playback. · `sonic_server.py`
- **Event queue**: buffer/worker · Queues note events before audio playback.
- [Audio helper](https://github.com/sebastianfeliciano/packet-sonics/blob/main/sonic_audio_helper.py): Python utility · Shared synthesis helpers for tone generation and playback setup. · `sonic_audio_helper.py`
- **Local audio**: sounddevice/numpy · Host-side tone synthesis and speaker output.
- **Browser audio**: Web Audio API · Client-side synthesis path used by the web runtime.
- **Browser**: web client · Receives control and renders audio in the browser.

### Operations

Metrics, scraping, and deployment/build support.

- **Metrics**: Prometheus endpoint · Exports packet, note, and queue metrics from the Python process.
- [Prometheus](https://github.com/sebastianfeliciano/packet-sonics/blob/main/prometheus.yml): scraper · Scrapes Sonic metrics for monitoring and visualization. · `prometheus.yml`
- [Build](https://github.com/sebastianfeliciano/packet-sonics/tree/main/Makefile): Makefile · Builds the C capture binary from source. · `Makefile`
- [Deps](https://github.com/sebastianfeliciano/packet-sonics/blob/main/requirements.txt): Python requirements · Python runtime dependencies for audio and metrics. · `requirements.txt`
- [Container](https://github.com/sebastianfeliciano/packet-sonics/tree/main/Dockerfile): Docker config · Containerization support for packaging and running the system. · `Dockerfile`

## Connections

- 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.
