sequenceDiagram
    participant U as User
    participant F as Frontend
    participant A as API Gateway
    participant S as Stream Server
    participant C as Compression Engine
    participant DB as PostgreSQL
    participant R as Redis
    participant CDN as CDN

    Note over U,CDN: Audio Streaming Flow

    U->>F: Select track to play
    F->>A: Request track metadata
    A->>S: GET /api/tracks/{id}
    S->>DB: Query track info
    DB->>S: Return track metadata
    S->>F: Return track details

    F->>S: Request audio stream
    S->>R: Check cache
    alt Track in cache
        R->>S: Return cached stream
    else Track not cached
        S->>C: Compress audio file
        C->>S: Return compressed stream
        S->>R: Cache compressed stream
    end

    S->>F: Stream audio data
    F->>U: Play audio

    Note over U,CDN: Adaptive Quality

    F->>S: Monitor bandwidth
    F->>S: Request quality change
    S->>C: Recompress at new quality
    C->>S: Return new quality stream
    S->>F: Stream at new quality

    Note over U,CDN: Live Streaming

    U->>F: Start live stream
    F->>S: WebSocket: StartLiveStream
    S->>DB: Create stream session
    S->>R: Register live stream
    S->>F: Stream ready

    U->>F: Send audio data
    F->>S: WebSocket: AudioChunk
    S->>C: Process live audio
    C->>S: Return processed chunk
    S->>CDN: Distribute to CDN
    S->>F: Stream to listeners

    Note over U,CDN: Analytics & Monitoring

    S->>DB: Log stream metrics
    S->>R: Update real-time stats
    F->>S: Request analytics
    S->>F: Return stream analytics
