# interstellers/audiovisualiser architecture

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

- Interactive diagram: https://gitdiagram.com/interstellers/audiovisualiser
- Repository: https://github.com/interstellers/audiovisualiser

## Overview

##### Project type and purpose
- This is a small full‑stack Node.js web application whose sole purpose is to serve a static web page that plays an audio file and draws its waveform/spectrum as an SVG‑based visualisation in the browser.
- The server side uses Express (in src/server/server.js) and Jade templates to render views; the client side (in src/client/) implements the AudioVisualizer, SvgBar, SvgBarChart and orchestration logic in visualiser.js.

##### Key architectural components
a. Frontend (Client‑side)
- AudioVisualizer.js: wraps the Web Audio API for capturing frequency/time data.
- SvgBar & SvgBarChart: generate and update SVG rectangles per audio data frame.
- visualiser.js: ties the UI, event handlers, audio source fetching and SVG updates together.
- Static assets under public/: CSS, images, the MP3 audio files, and vendor libraries (Mocha, Chai) for tests.
b. Backend (Server‑side)
- server.js: an Express app that serves both dynamic views (Jade) and static assets.
- Views under src/server/views/: Jade templates for the main visualizer page and any test pages.
c. Build & Test
- Gruntfile.js: defines the “web” task to launch a development server, serve files, possibly reload.
- specs/: client tests (Mocha in-browser harness) and server tests (node‑based with Mocha/Chai).
d. External dependencies
- Express, Jade (Pug), Grunt, Web Audio API (browser), Mocha, Chai.

##### Relationships & Data Flows
- The browser sends HTTP GET → Express server (server.js).
- Express chooses a Jade view (visualizer.jade), renders HTML + JS references + CSS, and responds.
- Browser loads static assets (style.css, visualiser.js, SvgBarChart.js, etc.) and the audio file (source.mp3 / tokyo.mp3).
- visualiser.js initializes AudioVisualizer, pulling PCM/frequency data from the Web Audio API.
- AudioVisualizer pushes data frames to SvgBarChart, which updates the SVG DOM in real time.
- Tests are separate: client tests run in an HTML page served under specs/client; server tests run via `npm test` or mocha CLI.

##### Architectural patterns & principles
- Separation of concerns:
- Server side handles routing & templating (Express + Jade)
- Client side handles data processing & rendering (Web Audio API + SVG)
- Static assets are served from a dedicated directory (public/)
- Layered architecture: Presentation (browser UI), Application (Express routes & controllers), Static resource layer.

- Client‑side:
- visualiser.html (rendered from Jade)
- visualiser.js → initializes AudioVisualizer and SvgBarChart
- public/audio/*.mp3
- public/styles/style.css
- public/vendors/js/mocha.js + chai.js (for client tests)
- Server‑side:
- server.js (Express app entry point)
- src/server/views/*.jade templates
- Routing layer (GET “/”, GET “/visualizer”, GET “/audio/*.mp3”)
- Build & Test:
- Grunt “web” task (serves on localhost, livereload)
- specs/client/test.html + testSvg.js + testVisualizer.js (Mocha harness)
- specs/server/test‑server.js (Mocha + Chai CLI)

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Browser/Client Layer
    subgraph "Browser/Client" 
        direction TB
        HTML["visualiser.html"]:::frontend
        VIS["visualiser.js"]:::frontend
        AV["AudioVisualizer"]:::frontend
        SB["SvgBar"]:::frontend
        SBC["SvgBarChart"]:::frontend
        WebAudio["Web Audio API"]:::frontend
    end

    %% Server Layer
    subgraph "Express Server" 
        direction TB
        Server["server.js"]:::server
        Layout["layout.jade"]:::server
        Header["header.jade"]:::server
        VisualizerTemplate["visualizer.jade"]:::server
        TestSvgTemplate["testSvg.jade"]:::server
        TestJadeTemplate["testJade.jade"]:::server
    end

    %% Static Assets
    subgraph "Static Asset Store"
        direction TB
        AudioSource1["source.mp3"]:::assets
        AudioSource2["tokyo.mp3"]:::assets
        StyleCSS["style.css"]:::assets
        MochaJS["mocha.js"]:::assets
        ChaiJS["chai.js"]:::assets
        MochaCSS["mocha.css"]:::assets
    end

    %% Build & Test
    subgraph "Build & Test"
        direction TB
        Grunt["Gruntfile.js"]:::build
        ClientTestHTML["test.html"]:::build
        ClientTest1["testSvg.js"]:::build
        ClientTest2["testVisualizer.js"]:::build
        ServerTest["test-server.js"]:::build
    end

    %% Connections
    HTML -->|"loads"| VIS
    VIS -->|"initializes"| AV
    VIS -->|"initializes"| SBC
    AV -->|"pushes data frames"| SBC
    SBC -->|"updates"| HTML
    AV -->|"uses"| WebAudio

    HTML -->|"HTTP GET /visualizer"| Server
    Server -->|"renders"| HTML
    Server -->|"uses template"| Layout
    Server -->|"includes partial"| Header
    Server -->|"renders view"| VisualizerTemplate
    Server -->|"renders test pages"| TestSvgTemplate
    Server -->|"renders test pages"| TestJadeTemplate

    HTML -->|"loads asset"| StyleCSS
    HTML -->|"loads asset"| VIS
    HTML -->|"loads audio"| AudioSource1
    HTML -->|"loads audio"| AudioSource2
    ClientTestHTML -->|"loads vendor"| MochaJS
    ClientTestHTML -->|"loads vendor"| ChaiJS
    ClientTestHTML -->|"loads vendor"| MochaCSS
    ClientTestHTML -->|"runs tests"| ClientTest1
    ClientTestHTML -->|"runs tests"| ClientTest2

    Grunt -->|"serves"| Server
    Grunt -->|"watches"| VIS
    Grunt -->|"reloads"| HTML

    ServerTest -->|"CLI run"| Server

    %% Click Events
    click AV "https://github.com/interstellers/audiovisualiser/blob/master/src/client/AudioVisualizer.js"
    click SB "https://github.com/interstellers/audiovisualiser/blob/master/src/client/SvgBar.js"
    click SBC "https://github.com/interstellers/audiovisualiser/blob/master/src/client/SvgBarChart.js"
    click VIS "https://github.com/interstellers/audiovisualiser/blob/master/src/client/visualiser.js"
    click Server "https://github.com/interstellers/audiovisualiser/blob/master/src/server/server.js"
    click Layout "https://github.com/interstellers/audiovisualiser/blob/master/src/server/views/layout.jade"
    click Header "https://github.com/interstellers/audiovisualiser/blob/master/src/server/views/header.jade"
    click VisualizerTemplate "https://github.com/interstellers/audiovisualiser/blob/master/src/server/views/visualizer.jade"
    click TestSvgTemplate "https://github.com/interstellers/audiovisualiser/blob/master/src/server/views/testSvg.jade"
    click TestJadeTemplate "https://github.com/interstellers/audiovisualiser/blob/master/src/server/views/testJade.jade"
    click AudioSource1 "https://github.com/interstellers/audiovisualiser/blob/master/public/audio/source.mp3"
    click AudioSource2 "https://github.com/interstellers/audiovisualiser/blob/master/public/audio/tokyo.mp3"
    click StyleCSS "https://github.com/interstellers/audiovisualiser/blob/master/public/styles/style.css"
    click MochaJS "https://github.com/interstellers/audiovisualiser/blob/master/public/vendors/js/mocha.js"
    click ChaiJS "https://github.com/interstellers/audiovisualiser/blob/master/public/vendors/js/chai.js"
    click MochaCSS "https://github.com/interstellers/audiovisualiser/blob/master/public/vendors/css/mocha.css"
    click Grunt "https://github.com/interstellers/audiovisualiser/blob/master/Gruntfile.js"
    click ClientTestHTML "https://github.com/interstellers/audiovisualiser/blob/master/specs/client/test.html"
    click ClientTest1 "https://github.com/interstellers/audiovisualiser/blob/master/specs/client/testSvg.js"
    click ClientTest2 "https://github.com/interstellers/audiovisualiser/blob/master/specs/client/testVisualizer.js"
    click ServerTest "https://github.com/interstellers/audiovisualiser/blob/master/specs/server/test-server.js"

    %% Styles
    classDef frontend fill:#D0E8FF,stroke:#007ACE,color:#000
    classDef server fill:#DFFFE0,stroke:#28A745,color:#000
    classDef assets fill:#F0F0F0,stroke:#666,color:#000
    classDef build fill:#FFE5B4,stroke:#E67E22,color:#000
```
