How pn-projects/tarangfx works
TarangFX is a Telegram audio-processing bot with a fairly clean three-layer shape: a Telegram interaction layer, a processing engine, and shared infrastructure for state, persistence, and limits.
1. Entry point and runtime
- bot.py is the main bootstrap. It likely loads configuration from .env, initializes the Telegram client, wires up handlers, and starts the event loop.
- The repo is designed to run either as a local Python service or in Docker (Dockerfile, docker/entrypoint.sh), with environment-driven deployment.
2. Telegram-facing interaction layer
- handlers/ is the user interaction boundary.
- start.py, help.py, and ui.py handle onboarding and inline menu flows.
- callbacks.py is the callback-query router for button presses and session choices.
- audio.py is the main media workflow handler: it receives user audio/media, kicks off processing, and returns results.
- sudo.py suggests privileged admin/owner commands or maintenance actions.
3. Session-based user workflow
- The README describes a v2 “configure first, process once” model.
- That means the bot likely stores per-user choices such as output format, bitrate, sample rate, and effects before starting the actual conversion job.
- This makes the UI flow stateful across multiple callback interactions rather than a single command.
4. Processing pipeline
- processors/ is the main media engine boundary.
- The README says the bot uses Spotify’s Pedalboard for studio-style effects and FFmpeg for conversion/encoding.
- The pipeline appears to combine multiple operations in one session, including format conversion, bitrate selection, sample-rate changes, normalization, and premium effects like reverb, delay, chorus, distortion, bass boost, and vocal enhancement.
5. Infrastructure for state and caching
- core/cache.py is the cache/session layer.
- Redis is a key part of the architecture for session state, rate limiting, and possibly temporary workflow metadata.
- The README indicates support for either a Redis URL or Upstash-style Redis REST settings, which means cache backend selection is environment-driven.
6. Concurrency and load control
- core/concurrency.py handles per-user or global operation limits.
- This is important because audio processing is expensive and the bot supports asynchronous downloads, transformations, and uploads.
- The architectural boundary here is “accept many Telegram updates, but only allow a controlled number of active jobs.”
7. Persistence layer
- core/database.py and models/ represent the database boundary.
- PostgreSQL is used for durable user data, likely including premium status, preferences, usage history, or admin records.
- The models package suggests a structured data layer instead of raw SQL scattered through handlers.
8. Premium and access control
- The feature set in the README distinguishes free vs premium capabilities: lossless formats, higher sample rates, and advanced effects.
- tests/test_premium_logic.py and handlers/sudo.py imply the system enforces tier-based access at the application layer.
- In the architecture diagram, premium checks should sit between the UI/handler layer and the processor pipeline.
9. Data flow for an audio job
- A user sends media to Telegram.
- handlers/audio.py receives the message, checks limits/access, and records session choices.
- The file is downloaded to a temp area, processed through the relevant processor chain, then uploaded back to Telegram.
- Temporary files are isolated per user and cleaned up after completion, which is an important security and resource boundary.
10. External services and deployment dependencies
- Telegram is the primary external API via Telethon.
- PostgreSQL provides durable storage.
- Redis provides fast ephemeral state and throttling.
- Docker is a first-class deployment mode, and the README also documents cloud-friendly deployments using Hugging Face Spaces plus optional Neon/Upstash managed services.
11. Validation and operational checks
- tests/ covers audio effects, premium rules, refinements, and environment/login verification.
- check_speed_deps.py and check_time.py look like operational or benchmark-style scripts for dependency/performance validation.
- These are useful indicators that processing latency and environment correctness are treated as first-order concerns.
12. Repository boundaries to show in a diagram
- Telegram update ingress: handlers
- Session/rate-limit cache: Redis-backed core/cache
- Durable user/config storage: PostgreSQL via core/database and models
- Job control: core/concurrency
- Audio engine: processors using Pedalboard + FFmpeg
- Deployment/runtime: bot.py, Docker, entrypoint
- QA/verification: tests and check scripts
Overall, the architecture is a stateful, async Telegram bot with a clear separation between message handling, session orchestration, media processing, and infrastructure services.
Main entry points: bot.py bot.py · audio.py Audio · __init__.py Processors · database.py Database
Components 18
Runtime
Bootstrap and deployment entrypoints
- bot.py · Main application bootstrap and event-loop startup
bot.py - Dockerfile · Container image definition for deployment
Dockerfile - entrypoint · Docker runtime entry script
docker/entrypoint.sh - Config · Environment-driven settings and service wiring
config.py
Telegram layer
User interaction and command handling
- Start · Entry commands and first-time user flow
handlers/start.py - Help · Usage guidance and bot help responses
handlers/help.py - UI · Inline menus and session configuration screens
handlers/ui.py - Callbacks · Button presses and session-choice routing
handlers/callbacks.py - Audio · Receives media, orchestrates processing, returns results
handlers/audio.py - Sudo · Privileged maintenance and owner actions
handlers/sudo.py
Core services
Shared state, limits, and persistence
- Cache · Ephemeral session state and rate-limit backing store
core/cache.py - Concurrency · Per-user and global active-job control
core/concurrency.py - Database · Durable storage access layer
core/database.py - Models · Structured persisted entities and records
models/__init__.py
Audio engine
Media transformation pipeline
- Processors · Media processing boundary using FFmpeg and Pedalboard
processors/__init__.py
Operations
Validation, checks, and tests
- Tests · Effects, premium rules, refinements, and env/login checks
tests/test_effects.py - Speed check · Dependency or performance verification script
check_speed_deps.py - Time check · Timing or latency verification script
check_time.py
Connections 23
- bot.py → Config: loads · Reads environment and runtime settings
- bot.py → Start: registers · Wires onboarding handlers
- bot.py → Help: registers · Wires help handler
- bot.py → UI: registers · Wires menu flows
- bot.py → Callbacks: registers · Wires callback router
- bot.py → Audio: registers · Wires media workflow
- bot.py → Sudo: registers · Wires admin commands
- UI → Callbacks: drives · UI actions continue through callback routing
- Callbacks → Cache: stores state · Persists session choices and workflow state
- Audio → Concurrency: acquires slot · Enforces active-job limits before processing
- Audio → Cache: reads session · Uses cached preferences and rate-limit state
- Audio → Database: checks account · Loads durable user and premium data
- Database → Models: uses models · Maps persisted records through structured entities
- Audio → Processors: invokes · Sends media into the audio engine
- Processors → Cache: temp metadata · May use ephemeral metadata during jobs
- Sudo → Database: admin writes · Updates durable account or access records
- Sudo → Cache: controls limits · Can adjust or inspect operational state
- Dockerfile → entrypoint: starts · Container launches through the entrypoint script
- entrypoint → bot.py: executes · Starts the bot process inside the container
- Tests → Processors: verifies · Covers effects and refinements in the engine
- Tests → Database: verifies · Covers premium and environment-related logic
- Speed check → Processors: benchmarks · Checks processing dependencies and speed-related behavior
- Time check → Processors: measures · Checks timing characteristics of processing flows