eigensound|lab

For a project like this, moving from a single HTML file to a modular structure is a crucial step towards maintainability, scalability, and cleaner code. The answer is yes, you absolutely have modular options that work perfectly with GitHub Pages.

Here’s a breakdown of the software design, why to go modular, and a recommended file structure.


Why Go Modular? (The Problem with a Single HTML File)

The single-file approach of the previous versions was fine for a simple prototype, but for eigensound|vector, it will quickly become a problem:

  1. Readability & Maintenance: A 2000+ line HTML file with embedded CSS and JavaScript is extremely difficult to read, debug, and modify. Finding a specific function becomes a chore.
  2. No Code Re-use: If you want to use a helper function in two different places, you can’t easily share it. Everything is in one global scope.
  3. Collaboration is Difficult: If multiple people were to work on the project, they would constantly be editing the same giant file, leading to merge conflicts.
  4. No Separation of Concerns: The structure (HTML), styling (CSS), and logic (JS) are all tangled together. This is considered bad practice in modern web development.

The Solution: Modular JavaScript with ES6 Modules

Modern JavaScript has a native, browser-supported module system (ES6 Modules) that is perfect for this. It allows you to split your code into logical files and import/export functionality between them. This works out-of-the-box on GitHub Pages without any special build tools.

Key Concepts:

  • export: You place the export keyword in front of a function, class, or variable in a file (e.g., math-helpers.js) to make it available to other files.
  • import: You use the import statement at the top of another file (e.g., main.js) to bring in the exported functionality.
  • <script type="module">: In your main index.html, you link to your main JavaScript file using <script type="module" src="main.js"></script>. The type="module" attribute tells the browser to treat this file and its imports as part of the module system.

Here is a practical, professional file structure that is ideal for this project and works seamlessly on GitHub Pages.

Project Directory Structure:

eigensound-vector/
├── index.html              // The main HTML page (the "shell" of the app)
├── css/
│   └── style.css           // All CSS styles
└── js/
    ├── main.js             // The main entry point, initializes everything
    ├── core/
    │   ├── audioEngine.js  // Manages Web Audio API, oscillators, effects
    │   ├── systemMatrix.js // Class to manage the H matrix, presets, and updates
    │   └── eigenEngine.js  // Performs the math.eigs() calculation, manages results
    ├── ui/
    │   ├── uiController.js // Main UI class, orchestrates all UI modules
    │   ├── matrixView.js   // Code for drawing and interacting with the H-matrix grid
    │   ├── analysisView.js // Code for drawing the eigenvalue plot, etc.
    │   └── audioView.js    // Code for the spectrogram and spectrum analyzers
    └── vendor/
        └── math.min.js     // The external math.js library

How the Modules Interact (Example Workflow):

  1. index.html: This file is now very clean. It contains only the HTML structure (the divs for your panels) and a single script tag:
    <!-- in index.html -->
    ...
    <script src="js/vendor/math.min.js"></script> <!-- Load library first -->
    <script type="module" src="js/main.js"></script> <!-- Load your app's main script -->
    </body>
    </html>
    
  2. js/main.js (The Orchestrator): This file kicks everything off.
    // in js/main.js
    import { UIController } from './ui/uiController.js';
    import { SystemMatrix } from './core/systemMatrix.js';
    import { EigenEngine } from './core/eigenEngine.js';
    import { AudioEngine } from './core/audioEngine.js';
    
    // Wait for the DOM to be ready
    document.addEventListener('DOMContentLoaded', () => {
        // 1. Initialize the core components
        const systemMatrix = new SystemMatrix(8); // 8x8 matrix
        const eigenEngine = new EigenEngine();
        const audioEngine = new AudioEngine();
    
        // 2. Initialize the main UI controller, passing it the core components
        const ui = new UIController(systemMatrix, eigenEngine, audioEngine);
    
        // 3. Start the application
        ui.initialize();
    });
    
  3. js/ui/uiController.js (The UI “Brain”): This class manages the overall UI logic and communication between modules.
    // in js/ui/uiController.js
    import { MatrixView } from './matrixView.js';
    import { AnalysisView } from './analysisView.js';
    
    export class UIController {
        constructor(systemMatrix, eigenEngine, audioEngine) {
            this.systemMatrix = systemMatrix;
            // ... and so on
                
            // Create instances of the view-specific modules
            this.matrixView = new MatrixView(document.getElementById('matrix-container'));
            this.analysisView = new AnalysisView(document.getElementById('analysis-container'));
        }
    
        initialize() {
            // Add event listener for the matrix view
            this.matrixView.onCellUpdate((i, j, value) => {
                this.handleMatrixUpdate(i, j, value);
            });
        }
    
        handleMatrixUpdate(i, j, value) {
            // 1. Update the matrix model
            this.systemMatrix.set(i, j, value);
    
            // 2. Trigger the calculation
            const H = this.systemMatrix.getMatrix();
            const eigensystem = this.eigenEngine.calculate(H);
    
            // 3. Update the visualizations and audio engine
            this.analysisView.update(eigensystem);
            this.audioEngine.updateEigensystem(eigensystem);
        }
    }
    
  4. js/core/eigenEngine.js (The “Math” Module): This module has one job: math. It’s clean and decoupled from any UI or audio logic.
    // in js/core/eigenEngine.js
    export class EigenEngine {
        calculate(matrix) {
            try {
                // Use the math.js library (which is globally available)
                const eigensystem = math.eigs(matrix);
                return {
                    values: eigensystem.values,
                    vectors: eigensystem.vectors
                };
            } catch (error) {
                console.error("Eigen-decomposition failed:", error);
                return null;
            }
        }
    }
    

Summary of Benefits of This Modular Design

  • Scalability: Want to add a new visualization? Create a new file in js/ui/, import it into uiController.js, and initialize it. No need to touch the core audio or math logic.
  • Testability: You can test each module in isolation. It’s easy to write a test for eigenEngine.js without having to run the entire application UI.
  • Clarity: The purpose of each file is small, clear, and well-defined. This is the essence of the “Single Responsibility Principle” in software design.
  • No Build Step Needed: This entire structure works directly in the browser via native ES6 modules. You can edit the code, push to GitHub, and see the changes live on your GitHub Pages site immediately.

This modular design is the modern, professional way to build a web application of this complexity. It will save you immense time and effort in the long run and provides a solid foundation for the future development you envision.