# Audio Pipeline ## Overview The audio pipeline processes music from various sources and delivers it to TeamSpeak's voice system. It handles decoding, format conversion, volume control, and memory management. ## Pipeline Architecture ```mermaid flowchart TB subgraph "Input Sources" YT[YouTube Stream] URL[Direct URL] File[Local File] end subgraph "Decoding" NC[NetworkClient
Download] AD[AudioDecoder
FFmpeg] Raw[Raw PCM
Float 32-bit] end subgraph "Processing" MM[MusicManager] Vol[Volume Control] Resamp[Resampler
192kHz] end subgraph "Output" PI[PluginInterface] Queue[Audio Queue
20ms chunks] TS[TeamSpeak Voice] end YT --> NC URL --> NC File --> AD NC --> AD AD --> Raw Raw --> MM MM --> Vol Vol --> Resamp Resamp --> PI PI --> Queue Queue --> TS ``` ## Data Transformations ```mermaid flowchart LR subgraph "Source" A[AAC/Opus/MP3
Any format] end subgraph "Decode" B[Float PCM
Variable rate] end subgraph "Process" C[Float PCM
192kHz Stereo] end subgraph "Output" D[Int16 PCM
20ms chunks] end A -->|FFmpeg
Demux + Decode| B B -->|Volume + Resample| C C -->|Convert| D ``` ## Supported Input Formats | Format | Decoder | Common Source | |--------|---------|---------------| | MP3 | FFmpeg native | General | | AAC | FFmpeg native | YouTube | | Opus | FFmpeg native | YouTube/Spotify | | Vorbis | FFmpeg native | OGG | | FLAC | FFmpeg native | Lossless | | WebM | FFmpeg + libopus | YouTube | ## Audio Format Specifications ```mermaid classDiagram class InputFormat { +string codec +int sampleRate +int channels +SampleFormat format } class InternalFormat { +float samples +int sampleRate: 192000 +int channels: 2 +SampleFormat: Float } class OutputFormat { +int16 samples +int sampleRate: 192000 +int channels: 2 +SampleFormat: Int16 +int chunkSize: 20ms } InputFormat --> InternalFormat : Decode InternalFormat --> OutputFormat : Process ``` ### Format Details | Stage | Sample Rate | Channels | Format | Notes | |-------|-------------|----------|--------|-------| | Input | Variable | 1-8 | Various | Source dependent | | Internal | 192 kHz | 2 (stereo) | Float 32-bit | Processing | | Output | 192 kHz | 2 (stereo) | Int16 | TeamSpeak req | ## Decoder Components ```mermaid flowchart TB subgraph "FFmpeg Pipeline" FC[Format Context
Container] CC[Codec Context
Decoder] SW[SWR Context
Resampler] end Input[Input Stream] --> FC FC -->|Packets| CC CC -->|Frames| SW SW -->|PCM| Output[Audio Buffer] subgraph "Hardware Acceleration" CUDA[NVIDIA CUDA] VAAPI[Intel/AMD VAAPI] VT[Apple VideoToolbox] end CC -.->|Optional| CUDA CC -.->|Optional| VAAPI CC -.->|Optional| VT ``` ## Hardware Acceleration ```mermaid flowchart TB subgraph "HW Acceleration Types" Detect{Detect GPU} Detect -->|NVIDIA| CUDA[CUDA
NVDEC] Detect -->|Intel/AMD Linux| VAAPI[VAAPI] Detect -->|Windows| DXVA[DXVA2/D3D11VA] Detect -->|macOS| VT[VideoToolbox] Detect -->|None| SW[Software
Decode] end CUDA --> FFmpeg VAAPI --> FFmpeg DXVA --> FFmpeg VT --> FFmpeg SW --> FFmpeg ``` | Type | Platform | Notes | |------|----------|-------| | CUDA | Windows/Linux | NVIDIA GPUs | | VAAPI | Linux | Intel/AMD | | DXVA2 | Windows | Legacy | | D3D11VA | Windows | Modern | | VideoToolbox | macOS | Native | ## Memory Management ```mermaid flowchart TB subgraph "Memory Pool" Pool[Buffer Pool] Acquire[Acquire Buffer] Use[Use Buffer] Release[Release Buffer] end Request[Audio Request] --> Acquire Acquire -->|Get from pool| Buffer[Buffer] Buffer --> Use Use --> Release Release -->|Return to pool| Pool subgraph "Zero-Copy" View[Audio View
Non-owning] Data[Raw Data] end Buffer -.-> View View --> Data ``` ## Volume Control ```mermaid flowchart LR Input[Float PCM] --> Volume{Apply Volume} Volume -->|Gain * sample| Output[Float PCM] subgraph "Volume Curve" User[User: 0-100] Linear[Linear: 0.0-1.0] Perceptual[Perceptual Curve] end User -->|/100| Linear Linear -->|^(2.0)| Perceptual ``` ### Volume Characteristics | User Setting | Linear Gain | Perceptual | |--------------|-------------|------------| | 0 | 0.0 | Silent | | 25 | 0.25 | ~6% | | 50 | 0.5 | ~25% | | 75 | 0.75 | ~56% | | 100 | 1.0 | 100% | ## Threading Model ```mermaid flowchart TB subgraph "Audio Threads" DT[Decode Thread
FFmpeg] PT[Playback Thread
MusicManager] AT[Audio Thread
PluginInterface] end subgraph "Synchronization" Q1[Decoded Queue
Mutex + CV] Q2[Audio Queue
Mutex + CV] end DT -->|Push| Q1 PT -->|Pop| Q1 PT -->|Push| Q2 AT -->|Pop| Q2 ``` ## Resampling ```mermaid flowchart LR Source{Source Rate} -->|44.1kHz| Up1[Upsample to 192kHz] Source -->|48kHz| Up2[Upsample to 192kHz] Source -->|192kHz| Pass[Pass Through] Up1 --> SWR[SWR Context] Up2 --> SWR Pass --> SWR SWR --> Output[192kHz Stereo] ``` ## Buffer Management ```mermaid flowchart TB subgraph "Audio Queue" Push[Core Push] --> Queue[Queue] Queue --> Pop[Plugin Pop] Queue -->|Full| Wait1[Wait] Queue -->|Empty| Silence[Send Silence] end subgraph "Chunk Processing" Decode[Decode Frame] --> Buffer[Accumulate] Buffer -->|7680 bytes| Chunk[20ms Chunk] Chunk --> TS[TeamSpeak] end ``` ## Audio Output Flow ```mermaid sequenceDiagram participant AD as AudioDecoder participant MM as MusicManager participant PI as PluginInterface participant TS as TeamSpeak loop Every 20ms AD->>AD: Decode frame AD-->>MM: Float PCM MM->>MM: Apply volume MM->>MM: Convert to Int16 MM->>PI: Audio callback PI->>PI: Queue chunk PI->>TS: processCustomCaptureData end ``` ## Performance Considerations ```mermaid flowchart TB subgraph "Optimizations" Pool[Memory Pooling] SIMD[SIMD Operations] HW[Hardware Decode] Buffer[Buffer Sizing] end Pool -->|Reduce allocations| Fast[Low Latency] SIMD -->|Vectorized ops| Fast HW -->|GPU offload| Fast Buffer -->|20ms chunks| Fast ``` | Optimization | Benefit | |--------------|---------| | Memory Pool | Reduces allocation overhead | | SIMD | Faster volume application | | Hardware Decode | Lower CPU usage | | 20ms Chunks | Balance latency vs efficiency | ## Error Handling ```mermaid flowchart TB Error{Error Type} --> DecodeErr["Decode Error
Skip track"] Error --> NetworkErr["Network Error
Retry with backoff"] Error --> BufferErr["Buffer Underrun
Increase buffer"] Error --> FormatErr["Format Error
Skip track"] DecodeErr --> Log[Log Error] NetworkErr --> Log BufferErr --> Log FormatErr --> Log Log --> Notify[Notify User] ``` ## Audio Statistics ```mermaid classDiagram class AudioStats { +uint64 framesDecoded +uint64 framesDropped +uint64 bytesDecoded +double avgDecodeTimeMs +bool hwAccelerationActive +HwAccelType accelType } ``` | Metric | Description | |--------|-------------| | Frames Decoded | Total audio frames processed | | Frames Dropped | Frames skipped (buffer issues) | | Decode Time | Average time per frame | | HW Acceleration | Whether GPU decode is active |